You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
-
foriinrange(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
-
forjinrange(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
-
formissing_elementindiff_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
+
foriinrange(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
+
forjinrange(1, number_of_elements+1, 1):
40
+
predicted_elements.add(j)
41
+
diff_set=predicted_elements-set(elements) # find the missing element by subtracting
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
-
defleader(a,n):
53
-
maxx=-1
54
-
l=[]
55
-
foriinrange(n-1,-1,-1):
56
-
ifa[i]>=maxx:
57
-
maxx=a[i]
58
-
l.append(maxx)
59
-
length=len(l)
60
-
foriinrange(length-1,-1,-1):
61
-
print(l[i],end=' ')
62
-
print()
63
-
64
-
t=int(input()) # test cases
65
-
66
-
foriinrange(t):
67
-
n=int(input()) # number of elements
68
-
arr=[int(x) forxininput().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
+
defleader(a,n):
53
+
maxx=-1
54
+
l=[]
55
+
foriinrange(n-1,-1,-1):
56
+
ifa[i]>=maxx:
57
+
maxx=a[i]
58
+
l.append(maxx)
59
+
length=len(l)
60
+
foriinrange(length-1,-1,-1):
61
+
print(l[i],end=' ')
62
+
print()
63
+
64
+
t=int(input()) # test cases
65
+
66
+
foriinrange(t):
67
+
n=int(input()) # number of elements
68
+
arr=[int(x) forxininput().split()] # elements separated by spaces
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
-
defprocessqueries(arr,n,m,q):
40
-
result= []
41
-
foriinrange(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
-
ifsorted(sub_array) ==sub_array: # increasing order
49
-
result.append('Yes')
50
-
elifsorted(sub_array, reverse=True) ==sub_array: # decreasing order
51
-
result.append('Yes')
52
-
elif0<=m<len(sub_array): # first increaing then decreasing
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
+
defprocessqueries(arr,n,m,q):
40
+
result= []
41
+
foriinrange(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
+
ifsorted(sub_array) ==sub_array: # increasing order
49
+
result.append('Yes')
50
+
elifsorted(sub_array, reverse=True) ==sub_array: # decreasing order
51
+
result.append('Yes')
52
+
elif0<=m<len(sub_array): # first increaing then decreasing
0 commit comments