Skip to content

Commit

Permalink
feat: 2 new ex
Browse files Browse the repository at this point in the history
  • Loading branch information
raduep authored and raduep committed Jun 28, 2023
1 parent dfd4936 commit ae01cc4
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"[python]": {
"editor.defaultFormatter": "ms-python.black-formatter"
},
"python.formatting.provider": "none"
}
36 changes: 36 additions & 0 deletions kadone_algorithm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Given an array Arr[] of N integers. Find the contiguous sub-array(containing at least one number) which has the maximum sum and return its sum.


Example 1:

Input:
N = 5
Arr[] = {1,2,3,-2,5}
Output:
9
Explanation:
Max subarray sum is 9
of elements (1, 2, 3, -2, 5) which
is a contiguous subarray.
Example 2:

Input:
N = 4
Arr[] = {-1,-2,-3,-4}
Output:
-1
Explanation:
Max subarray sum is -1
of element (-1)

Your Task:
You don't need to read input or print anything. The task is to complete the function maxSubarraySum() which takes Arr[] and N as input parameters and returns the sum of subarray with maximum sum.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)


Constraints:
1 ≤ N ≤ 106
-107 ≤ A[i] ≤ 107
44 changes: 44 additions & 0 deletions kadone_algorithm/ka.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# User function Template for python3

import math


class Solution:
##Complete this function
# Function to find the sum of contiguous subarray with maximum sum.
def maxSubArraySum(self, arr, N):
max = arr[0]
i = 0
while i < N:
print("i;", i)
j = i + 1
per = 1
while per + j-1 < N:
localMax = 0
marj = j
it = per
while it > 0 and marj < N:
print("mars:", marj, arr[marj])
localMax += arr[marj]
marj += 1
it -= 1
if localMax > max:
max = localMax
print("max", max)
per += 1
print("next:", per,i,j, max)
i += 1
return max


def main():
N = 5
# N = 9
Arr = [1, 2, 3, -2, 5]
# Arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4]
ob = Solution()
print(ob.maxSubArraySum(Arr, N))


if __name__ == "__main__":
main()
26 changes: 26 additions & 0 deletions missing_number_in_array/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Given an array of size N-1 such that it only contains distinct integers in the range of 1 to N. Find the missing element.

Example 1:

Input:
N = 5
A[] = {1,2,3,5}
Output: 4
Example 2:

Input:
N = 10
A[] = {6,1,2,8,3,4,7,10,5}
Output: 9

Your Task :
You don't need to read input or print anything. Complete the function MissingNumber() that takes array and N as input parameters and returns the value of the missing number.


Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)


Constraints:
1 ≤ N ≤ 106
1 ≤ A[i] ≤ 106
30 changes: 30 additions & 0 deletions missing_number_in_array/mnia.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#User function Template for python3


class Solution:
def missingNumber(self,array,n):
# code here
if n==2 and array[0]==1:
return 2
if n==2 and array[0]==2:
return 1
array.sort()
i=0
while i<n-1:
if array[i]!=i+1:
return i+1
i+=1
return n


#{
# Driver Code Starts
#Initial Template for Python 3

N = 10
N = 5
A = [6,1,2,8,3,4,7,10,5]
A = [1,2,3,4]
s=Solution().missingNumber(A,N)
print(s)
# } Driver Code Ends

0 comments on commit ae01cc4

Please sign in to comment.