Skip to content

Commit be81090

Browse files
committed
updated files
1 parent 7048cea commit be81090

File tree

94 files changed

+5069
-5042
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+5069
-5042
lines changed

Arrays/Find_Missing_Element.py

+43-43
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
1-
"""
2-
Problem Statement:
3-
Missing number in array
4-
Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.
5-
6-
Input:
7-
The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.
8-
9-
Output:
10-
Print the missing number in array.
11-
12-
Constraints:
13-
1 ≤ T ≤ 200
14-
1 ≤ N ≤ 107
15-
1 ≤ C[i] ≤ 107
16-
17-
Example:
18-
Input:
19-
2
20-
5
21-
1 2 3 5
22-
10
23-
1 2 3 4 5 6 7 8 10
24-
25-
Output:
26-
4
27-
9
28-
29-
Explanation:
30-
Testcase 1: Given array : 1 2 3 5. Missing element is 4.
31-
"""
32-
33-
if __name__=='__main__':
34-
test_cases=int(input()) # number of test cases
35-
for i in range(test_cases):
36-
number_of_elements = int(input()) # number of array elements to add
37-
elements = list(map(int, input().split()))
38-
predicted_elements = set()
39-
for j in range(1, number_of_elements+1, 1):
40-
predicted_elements.add(j)
41-
diff_set = predicted_elements - set(elements) # find the missing element by subtracting
42-
for missing_element in diff_set:
43-
print(missing_element)
1+
"""
2+
Problem Statement:
3+
Missing number in array
4+
Given an array C of size N-1 and given that there are numbers from 1 to N with one element missing, the missing number is to be found.
5+
6+
Input:
7+
The first line of input contains an integer T denoting the number of test cases. For each test case first line contains N(size of array). The subsequent line contains N-1 array elements.
8+
9+
Output:
10+
Print the missing number in array.
11+
12+
Constraints:
13+
1 ≤ T ≤ 200
14+
1 ≤ N ≤ 107
15+
1 ≤ C[i] ≤ 107
16+
17+
Example:
18+
Input:
19+
2
20+
5
21+
1 2 3 5
22+
10
23+
1 2 3 4 5 6 7 8 10
24+
25+
Output:
26+
4
27+
9
28+
29+
Explanation:
30+
Testcase 1: Given array : 1 2 3 5. Missing element is 4.
31+
"""
32+
33+
if __name__=='__main__':
34+
test_cases=int(input()) # number of test cases
35+
for i in range(test_cases):
36+
number_of_elements = int(input()) # number of array elements to add
37+
elements = list(map(int, input().split()))
38+
predicted_elements = set()
39+
for j in range(1, number_of_elements+1, 1):
40+
predicted_elements.add(j)
41+
diff_set = predicted_elements - set(elements) # find the missing element by subtracting
42+
for missing_element in diff_set:
43+
print(missing_element)

Arrays/Leaders_Of_Array.py

+68-68
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,69 @@
1-
"""
2-
Leaders in an array
3-
Given an array of positive integers. Your task is to find the leaders in the array.
4-
Note: An element of array is leader if it is greater than or equal to all the elements to its right side. Also, the rightmost element is always a leader.
5-
6-
Input:
7-
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
8-
The first line of each test case contains a single integer N denoting the size of array.
9-
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
10-
11-
Output:
12-
Print all the leaders.
13-
14-
Constraints:
15-
1 <= T <= 100
16-
1 <= N <= 107
17-
0 <= Ai <= 107
18-
19-
Example:
20-
Input:
21-
3
22-
6
23-
16 17 4 3 5 2
24-
5
25-
1 2 3 4 0
26-
5
27-
7 4 5 7 3
28-
Output:
29-
17 5 2
30-
4 0
31-
7 7 3
32-
33-
Explanation:
34-
Testcase 3: All elements on the right of 7 (at index 0) are smaller than or equal to 7. Also, all the elements of right side of 7 (at index 3) are smaller than 7. And, the last element 3 is itself a leader since no elements are on its right.
35-
"""
36-
37-
"""
38-
# this code has more time complexity that the following code (but it has a nice approach)
39-
if __name__=='__main__':
40-
test_cases=int(input())
41-
for i in range(test_cases):
42-
n = int(input())
43-
elements = list(map(int,input().split()))
44-
for pos, elem in enumerate(elements):
45-
if pos + 1 < n:
46-
if max(elements[pos+1:]) <= elem:
47-
print(elem)
48-
else:
49-
print(elem)
50-
"""
51-
#code
52-
def leader(a,n):
53-
maxx=-1
54-
l=[]
55-
for i in range(n-1,-1,-1):
56-
if a[i]>=maxx:
57-
maxx=a[i]
58-
l.append(maxx)
59-
length=len(l)
60-
for i in range(length-1,-1,-1):
61-
print(l[i],end=' ')
62-
print()
63-
64-
t=int(input()) # test cases
65-
66-
for i in range(t):
67-
n=int(input()) # number of elements
68-
arr=[int(x) for x in input().split()] # elements separated by spaces
1+
"""
2+
Leaders in an array
3+
Given an array of positive integers. Your task is to find the leaders in the array.
4+
Note: An element of array is leader if it is greater than or equal to all the elements to its right side. Also, the rightmost element is always a leader.
5+
6+
Input:
7+
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
8+
The first line of each test case contains a single integer N denoting the size of array.
9+
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
10+
11+
Output:
12+
Print all the leaders.
13+
14+
Constraints:
15+
1 <= T <= 100
16+
1 <= N <= 107
17+
0 <= Ai <= 107
18+
19+
Example:
20+
Input:
21+
3
22+
6
23+
16 17 4 3 5 2
24+
5
25+
1 2 3 4 0
26+
5
27+
7 4 5 7 3
28+
Output:
29+
17 5 2
30+
4 0
31+
7 7 3
32+
33+
Explanation:
34+
Testcase 3: All elements on the right of 7 (at index 0) are smaller than or equal to 7. Also, all the elements of right side of 7 (at index 3) are smaller than 7. And, the last element 3 is itself a leader since no elements are on its right.
35+
"""
36+
37+
"""
38+
# this code has more time complexity that the following code (but it has a nice approach)
39+
if __name__=='__main__':
40+
test_cases=int(input())
41+
for i in range(test_cases):
42+
n = int(input())
43+
elements = list(map(int,input().split()))
44+
for pos, elem in enumerate(elements):
45+
if pos + 1 < n:
46+
if max(elements[pos+1:]) <= elem:
47+
print(elem)
48+
else:
49+
print(elem)
50+
"""
51+
#code
52+
def leader(a,n):
53+
maxx=-1
54+
l=[]
55+
for i in range(n-1,-1,-1):
56+
if a[i]>=maxx:
57+
maxx=a[i]
58+
l.append(maxx)
59+
length=len(l)
60+
for i in range(length-1,-1,-1):
61+
print(l[i],end=' ')
62+
print()
63+
64+
t=int(input()) # test cases
65+
66+
for i in range(t):
67+
n=int(input()) # number of elements
68+
arr=[int(x) for x in input().split()] # elements separated by spaces
6969
leader(arr,n) # method to find all leaders

Arrays/MountainSubArray.py

+76-76
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,77 @@
1-
"""
2-
Mountain Subarray Problem
3-
We are given an array of integers and a range, we need to find whether the subarray which falls in this range has values in the form of a mountain or not. All values of the subarray are said to be in the form of a mountain if either all values are increasing or decreasing or first increasing and then decreasing. More formally a subarray [a1, a2, a3 … aN] is said to be in form of a mountain if there exists an integer K, 1 <= K <= N such that,
4-
a1 <= a2 <= a3 .. <= aK >= a(K+1) >= a(K+2) …. >= aN
5-
You have to process Q queries. In each query you are given two integer L and R, denoting starting and last index of the subarrays respectively.
6-
7-
Input:
8-
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. The following line contains N space-separated integers forming the array. Next line contains an integer Q, denoting the number of queries. For each query, you are given two space-separated integers L and R in a new line.
9-
10-
Output:
11-
For each query, print "Yes" (without quotes) if the subarray is in mountain form, otherwise print "No" (without quotes) in a new line.
12-
13-
Expected Time Complexity: O(N + Q).
14-
Expected Auxiliary Space: O(N).
15-
16-
Constraints:
17-
1 <= T <= 100
18-
1 <= N, Q <= 105
19-
1 <= a[i] <= 106, for each valid i
20-
0 <= L <= R <= N-1
21-
22-
Example:
23-
Input:
24-
1
25-
8
26-
2 3 2 4 4 6 3 2
27-
2
28-
0 2
29-
1 3
30-
31-
Output:
32-
Yes
33-
No
34-
35-
Explanation:
36-
For L = 0 and R = 2, a0 = 2, a1 = 3 and a2 = 2, since they are in the valid order,answer is Yes.
37-
For L = 1 and R = 3, a1 = 3, a2 = 2 and a3 = 4, since a1 > a2 and a2 < a4 the order isn't valid, hence the answer is No.
38-
"""
39-
def processqueries(arr,n,m,q):
40-
result = []
41-
for i in range(m): # each list inside q
42-
boundary = q[i]
43-
bound_a = boundary[0]
44-
bound_b = boundary[1]
45-
46-
sub_array = arr[bound_a:bound_b+1]
47-
m = sub_array.index(max(sub_array))
48-
if sorted(sub_array) == sub_array: # increasing order
49-
result.append('Yes')
50-
elif sorted(sub_array, reverse=True) == sub_array: # decreasing order
51-
result.append('Yes')
52-
elif 0 <= m < len(sub_array): # first increaing then decreasing
53-
left = sub_array[:m]
54-
right = sub_array[m:]
55-
if sorted(left) == left and sorted(right, reverse=True) == right:
56-
print(f"left={left}\nright={right},\nm={m},\nsub_array={sub_array}")
57-
result.append("Yes")
58-
else:
59-
print(f"\nLeft sub_array:{left}, \nright sub_array:{right}\nPrinting No")
60-
result.append("No")
61-
return result
62-
63-
64-
if __name__ == "__main__":
65-
t = int(input())
66-
x = 0
67-
while x < t:
68-
n = int(input())
69-
arr = list(map(int, input().split()))
70-
m = int(input())
71-
q = list()
72-
for i in range(0,m):
73-
q.append(list(map(int, input().split())))
74-
result = processqueries(arr,n,m,q)
75-
for i in result:
76-
print(i)
1+
"""
2+
Mountain Subarray Problem
3+
We are given an array of integers and a range, we need to find whether the subarray which falls in this range has values in the form of a mountain or not. All values of the subarray are said to be in the form of a mountain if either all values are increasing or decreasing or first increasing and then decreasing. More formally a subarray [a1, a2, a3 … aN] is said to be in form of a mountain if there exists an integer K, 1 <= K <= N such that,
4+
a1 <= a2 <= a3 .. <= aK >= a(K+1) >= a(K+2) …. >= aN
5+
You have to process Q queries. In each query you are given two integer L and R, denoting starting and last index of the subarrays respectively.
6+
7+
Input:
8+
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. The following line contains N space-separated integers forming the array. Next line contains an integer Q, denoting the number of queries. For each query, you are given two space-separated integers L and R in a new line.
9+
10+
Output:
11+
For each query, print "Yes" (without quotes) if the subarray is in mountain form, otherwise print "No" (without quotes) in a new line.
12+
13+
Expected Time Complexity: O(N + Q).
14+
Expected Auxiliary Space: O(N).
15+
16+
Constraints:
17+
1 <= T <= 100
18+
1 <= N, Q <= 105
19+
1 <= a[i] <= 106, for each valid i
20+
0 <= L <= R <= N-1
21+
22+
Example:
23+
Input:
24+
1
25+
8
26+
2 3 2 4 4 6 3 2
27+
2
28+
0 2
29+
1 3
30+
31+
Output:
32+
Yes
33+
No
34+
35+
Explanation:
36+
For L = 0 and R = 2, a0 = 2, a1 = 3 and a2 = 2, since they are in the valid order,answer is Yes.
37+
For L = 1 and R = 3, a1 = 3, a2 = 2 and a3 = 4, since a1 > a2 and a2 < a4 the order isn't valid, hence the answer is No.
38+
"""
39+
def processqueries(arr,n,m,q):
40+
result = []
41+
for i in range(m): # each list inside q
42+
boundary = q[i]
43+
bound_a = boundary[0]
44+
bound_b = boundary[1]
45+
46+
sub_array = arr[bound_a:bound_b+1]
47+
m = sub_array.index(max(sub_array))
48+
if sorted(sub_array) == sub_array: # increasing order
49+
result.append('Yes')
50+
elif sorted(sub_array, reverse=True) == sub_array: # decreasing order
51+
result.append('Yes')
52+
elif 0 <= m < len(sub_array): # first increaing then decreasing
53+
left = sub_array[:m]
54+
right = sub_array[m:]
55+
if sorted(left) == left and sorted(right, reverse=True) == right:
56+
print(f"left={left}\nright={right},\nm={m},\nsub_array={sub_array}")
57+
result.append("Yes")
58+
else:
59+
print(f"\nLeft sub_array:{left}, \nright sub_array:{right}\nPrinting No")
60+
result.append("No")
61+
return result
62+
63+
64+
if __name__ == "__main__":
65+
t = int(input())
66+
x = 0
67+
while x < t:
68+
n = int(input())
69+
arr = list(map(int, input().split()))
70+
m = int(input())
71+
q = list()
72+
for i in range(0,m):
73+
q.append(list(map(int, input().split())))
74+
result = processqueries(arr,n,m,q)
75+
for i in result:
76+
print(i)
7777
x += 1

0 commit comments

Comments
 (0)