Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 8f5e986

Browse files
committed
questions in progress
1 parent 820ed7b commit 8f5e986

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
3+
Given an unsorted array of size n. Array elements are in range from 1 to n. One number 'A' from set {1, 2, …n} is missing and one number 'B' occurs twice in array. Find these two numbers.
4+
Note: If you find multiple answers then print the Smallest number found.
5+
6+
Input:
7+
8+
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
9+
The first line of each test case contains a single integer N denoting the size of array.
10+
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
11+
12+
13+
Output:
14+
15+
Print B, the repeating number followed by A which is missing in a single line.
16+
17+
18+
Constraints:
19+
20+
1 ≤ T ≤ 40
21+
1 ≤ N ≤ 100
22+
1 ≤ A[i] ≤ N
23+
24+
25+
Example:
26+
27+
Input
28+
2
29+
2
30+
2 2
31+
3
32+
1 3 3
33+
Output
34+
2 1
35+
3 2
36+
37+
"""
38+
39+
40+
def find_missing_repeating(arr,n):
41+
42+
43+
44+
def test():
45+
46+
47+
def main():
48+
49+
50+
if __name__ == '__main__':
51+
main()
52+
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
"""
2+
https://practice.geeksforgeeks.org/problems/largest-sum-subarray-of-size-at-least-k/0
3+
4+
Given an array and a number k, find the largest sum of the subarray containing at least k numbers. It may be assumed that the size of array is at-least k.
5+
6+
Input:​
7+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case contains an integer n denoting the size of the array. Then the following line contains n space separated integers. The last line of the input contains the number k.
8+
9+
Output:
10+
Print the value of the largest sum of the subarray containing at least k numbers.
11+
12+
Constraints:
13+
1<=T<=10^5
14+
1<=n<=10^5
15+
1<=a[i]<=10^5
16+
1<=k<=n
17+
18+
Example:
19+
Input:
20+
2
21+
4
22+
-4 -2 1 -3
23+
2
24+
6
25+
1 1 1 1 1 1
26+
2
27+
28+
Output:
29+
-1
30+
6
31+
32+
"""
33+
34+
35+
def largest_subarray(arr,n,k):
36+
i = 0
37+
j = 0
38+
39+
arr_sum = 0
40+
max_sum = min(arr)
41+
while(i<n-k+1):
42+
if j<n and (j-i < k or arr_sum + arr[j] > max_sum):
43+
arr_sum+=arr[j]
44+
j+=1
45+
continue
46+
47+
if max_sum < arr_sum:
48+
max_sum = arr_sum
49+
50+
arr_sum = arr_sum-arr[i]
51+
i+=1
52+
53+
54+
if arr_sum > max_sum:
55+
max_sum =arr_sum
56+
57+
return max_sum
58+
59+
60+
def test():
61+
a = [-4, -2, 1, -3]
62+
print(largest_subarray(a,4,2))
63+
a = [1]*6
64+
print(largest_subarray(a,6,2))
65+
66+
if __name__ == '__main__':
67+
# main()
68+
test()
69+

Arrays/merge_k_sorted_array_X.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
def parent(i):
2+
return int((i-1)/2)
3+
def left(i):
4+
return 2*i + 1
5+
def right(i):
6+
return 2*i + 2
7+
8+
9+
def heapify(arr,n,i):
10+
small = i
11+
12+
if left(i) < n and arr[left(i)] < arr[i]:
13+
small = left(i)
14+
15+
if right(i) < n and arr[right(i)] < arr[small]:
16+
small = right(i)
17+
18+
# swap two elements
19+
if small != i:
20+
arr[small],arr[i] = arr[i],arr[small]
21+
heapify(arr,n,small)
22+
23+
24+
def extract_min(arr):
25+
print(arr)
26+
n = len(arr)s
27+
item = arr[0]
28+
arr[0],arr[n-1] = arr[n-1],arr[0]
29+
arr.pop()
30+
heapify(arr,n-1,0)
31+
return item

Arrays/smallest_distinct_window_X.py

Whitespace-only changes.
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
"""
2+
https://practice.geeksforgeeks.org/problems/smallest-positive-missing-number/0
3+
4+
You are given an unsorted array with both positive and negative elements. You have to find the smallest positive number missing from the array in O(n) time using constant extra space.
5+
6+
Input:
7+
First line consists of T test cases. First line of every test case consists of N, denoting the number of elements in array. Second line of every test case consists of elements in array.
8+
9+
Output:
10+
Single line output, print the smallest positive number missing.
11+
12+
Constraints:
13+
1<=T<=100
14+
1<=N<=100
15+
16+
Example:
17+
Input:
18+
2
19+
5
20+
1 2 3 4 5
21+
5
22+
0 -10 1 -20
23+
Output:
24+
6
25+
2
26+
27+
"""
28+
29+
def parent(i):
30+
return int((i-1)/2)
31+
def left(i):
32+
return 2*i + 1
33+
def right(i):
34+
return 2*i + 2
35+
36+
37+
def heapify(arr,n,i):
38+
small = i
39+
40+
if left(i) < n and arr[left(i)] < arr[i]:
41+
small = left(i)
42+
43+
if right(i) < n and arr[right(i)] < arr[small]:
44+
small = right(i)
45+
46+
# swap two elements
47+
if small != i:
48+
arr[small],arr[i] = arr[i],arr[small]
49+
heapify(arr,n,small)
50+
51+
52+
def extract_min(arr):
53+
print(arr)
54+
n = len(arr)s
55+
item = arr[0]
56+
arr[0],arr[n-1] = arr[n-1],arr[0]
57+
arr.pop()
58+
heapify(arr,n-1,0)
59+
return item
60+
61+
def smallest_positive(arr,n):
62+
63+
# for i in range(n):
64+
# if arr[i] < 0:
65+
# arr[i] = 100000000
66+
67+
for i in range(int(n/2),-1,-1):
68+
heapify(arr,n,i)
69+
prev = None
70+
while(len(arr)>0):
71+
item = extract_min(arr)
72+
if item < 0:
73+
continue
74+
if item != 1:
75+
return 1
76+
if prev == None or prev+1 == item:
77+
prev = item
78+
continue
79+
else:
80+
return prev+1
81+
return prev + 1
82+
83+
def main():
84+
t = int(input())
85+
for i in range(t):
86+
n = int(input())
87+
arr = list(map(int,input().split()))
88+
print(smallest_positive(arr,n))
89+
90+
91+
if __name__ == '__main__':
92+
main()
93+

0 commit comments

Comments
 (0)