Skip to content

Commit

Permalink
updated files
Browse files Browse the repository at this point in the history
  • Loading branch information
Tuhin-thinks committed May 24, 2021
1 parent 7048cea commit be81090
Show file tree
Hide file tree
Showing 94 changed files with 5,069 additions and 5,042 deletions.
86 changes: 43 additions & 43 deletions Arrays/Find_Missing_Element.py
Original file line number Diff line number Diff line change
@@ -1,43 +1,43 @@
"""
Problem Statement:
Missing number in array
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.
Input:
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.
Output:
Print the missing number in array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ C[i] ≤ 107
Example:
Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output:
4
9
Explanation:
Testcase 1: Given array : 1 2 3 5. Missing element is 4.
"""

if __name__=='__main__':
test_cases=int(input()) # number of test cases
for i in range(test_cases):
number_of_elements = int(input()) # number of array elements to add
elements = list(map(int, input().split()))
predicted_elements = set()
for j in range(1, number_of_elements+1, 1):
predicted_elements.add(j)
diff_set = predicted_elements - set(elements) # find the missing element by subtracting
for missing_element in diff_set:
print(missing_element)
"""
Problem Statement:
Missing number in array
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.
Input:
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.
Output:
Print the missing number in array.
Constraints:
1 ≤ T ≤ 200
1 ≤ N ≤ 107
1 ≤ C[i] ≤ 107
Example:
Input:
2
5
1 2 3 5
10
1 2 3 4 5 6 7 8 10
Output:
4
9
Explanation:
Testcase 1: Given array : 1 2 3 5. Missing element is 4.
"""

if __name__=='__main__':
test_cases=int(input()) # number of test cases
for i in range(test_cases):
number_of_elements = int(input()) # number of array elements to add
elements = list(map(int, input().split()))
predicted_elements = set()
for j in range(1, number_of_elements+1, 1):
predicted_elements.add(j)
diff_set = predicted_elements - set(elements) # find the missing element by subtracting
for missing_element in diff_set:
print(missing_element)
136 changes: 68 additions & 68 deletions Arrays/Leaders_Of_Array.py
Original file line number Diff line number Diff line change
@@ -1,69 +1,69 @@
"""
Leaders in an array
Given an array of positive integers. Your task is to find the leaders in the array.
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.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print all the leaders.
Constraints:
1 <= T <= 100
1 <= N <= 107
0 <= Ai <= 107
Example:
Input:
3
6
16 17 4 3 5 2
5
1 2 3 4 0
5
7 4 5 7 3
Output:
17 5 2
4 0
7 7 3
Explanation:
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.
"""

"""
# this code has more time complexity that the following code (but it has a nice approach)
if __name__=='__main__':
test_cases=int(input())
for i in range(test_cases):
n = int(input())
elements = list(map(int,input().split()))
for pos, elem in enumerate(elements):
if pos + 1 < n:
if max(elements[pos+1:]) <= elem:
print(elem)
else:
print(elem)
"""
#code
def leader(a,n):
maxx=-1
l=[]
for i in range(n-1,-1,-1):
if a[i]>=maxx:
maxx=a[i]
l.append(maxx)
length=len(l)
for i in range(length-1,-1,-1):
print(l[i],end=' ')
print()

t=int(input()) # test cases

for i in range(t):
n=int(input()) # number of elements
arr=[int(x) for x in input().split()] # elements separated by spaces
"""
Leaders in an array
Given an array of positive integers. Your task is to find the leaders in the array.
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.
Input:
The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N denoting the size of array.
The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array.
Output:
Print all the leaders.
Constraints:
1 <= T <= 100
1 <= N <= 107
0 <= Ai <= 107
Example:
Input:
3
6
16 17 4 3 5 2
5
1 2 3 4 0
5
7 4 5 7 3
Output:
17 5 2
4 0
7 7 3
Explanation:
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.
"""

"""
# this code has more time complexity that the following code (but it has a nice approach)
if __name__=='__main__':
test_cases=int(input())
for i in range(test_cases):
n = int(input())
elements = list(map(int,input().split()))
for pos, elem in enumerate(elements):
if pos + 1 < n:
if max(elements[pos+1:]) <= elem:
print(elem)
else:
print(elem)
"""
#code
def leader(a,n):
maxx=-1
l=[]
for i in range(n-1,-1,-1):
if a[i]>=maxx:
maxx=a[i]
l.append(maxx)
length=len(l)
for i in range(length-1,-1,-1):
print(l[i],end=' ')
print()

t=int(input()) # test cases

for i in range(t):
n=int(input()) # number of elements
arr=[int(x) for x in input().split()] # elements separated by spaces
leader(arr,n) # method to find all leaders
152 changes: 76 additions & 76 deletions Arrays/MountainSubArray.py
Original file line number Diff line number Diff line change
@@ -1,77 +1,77 @@
"""
Mountain Subarray Problem
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,
a1 <= a2 <= a3 .. <= aK >= a(K+1) >= a(K+2) …. >= aN
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.
Input:
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.
Output:
For each query, print "Yes" (without quotes) if the subarray is in mountain form, otherwise print "No" (without quotes) in a new line.
Expected Time Complexity: O(N + Q).
Expected Auxiliary Space: O(N).
Constraints:
1 <= T <= 100
1 <= N, Q <= 105
1 <= a[i] <= 106, for each valid i
0 <= L <= R <= N-1
Example:
Input:
1
8
2 3 2 4 4 6 3 2
2
0 2
1 3
Output:
Yes
No
Explanation:
For L = 0 and R = 2, a0 = 2, a1 = 3 and a2 = 2, since they are in the valid order,answer is Yes.
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.
"""
def processqueries(arr,n,m,q):
result = []
for i in range(m): # each list inside q
boundary = q[i]
bound_a = boundary[0]
bound_b = boundary[1]

sub_array = arr[bound_a:bound_b+1]
m = sub_array.index(max(sub_array))
if sorted(sub_array) == sub_array: # increasing order
result.append('Yes')
elif sorted(sub_array, reverse=True) == sub_array: # decreasing order
result.append('Yes')
elif 0 <= m < len(sub_array): # first increaing then decreasing
left = sub_array[:m]
right = sub_array[m:]
if sorted(left) == left and sorted(right, reverse=True) == right:
print(f"left={left}\nright={right},\nm={m},\nsub_array={sub_array}")
result.append("Yes")
else:
print(f"\nLeft sub_array:{left}, \nright sub_array:{right}\nPrinting No")
result.append("No")
return result


if __name__ == "__main__":
t = int(input())
x = 0
while x < t:
n = int(input())
arr = list(map(int, input().split()))
m = int(input())
q = list()
for i in range(0,m):
q.append(list(map(int, input().split())))
result = processqueries(arr,n,m,q)
for i in result:
print(i)
"""
Mountain Subarray Problem
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,
a1 <= a2 <= a3 .. <= aK >= a(K+1) >= a(K+2) …. >= aN
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.
Input:
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.
Output:
For each query, print "Yes" (without quotes) if the subarray is in mountain form, otherwise print "No" (without quotes) in a new line.
Expected Time Complexity: O(N + Q).
Expected Auxiliary Space: O(N).
Constraints:
1 <= T <= 100
1 <= N, Q <= 105
1 <= a[i] <= 106, for each valid i
0 <= L <= R <= N-1
Example:
Input:
1
8
2 3 2 4 4 6 3 2
2
0 2
1 3
Output:
Yes
No
Explanation:
For L = 0 and R = 2, a0 = 2, a1 = 3 and a2 = 2, since they are in the valid order,answer is Yes.
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.
"""
def processqueries(arr,n,m,q):
result = []
for i in range(m): # each list inside q
boundary = q[i]
bound_a = boundary[0]
bound_b = boundary[1]

sub_array = arr[bound_a:bound_b+1]
m = sub_array.index(max(sub_array))
if sorted(sub_array) == sub_array: # increasing order
result.append('Yes')
elif sorted(sub_array, reverse=True) == sub_array: # decreasing order
result.append('Yes')
elif 0 <= m < len(sub_array): # first increaing then decreasing
left = sub_array[:m]
right = sub_array[m:]
if sorted(left) == left and sorted(right, reverse=True) == right:
print(f"left={left}\nright={right},\nm={m},\nsub_array={sub_array}")
result.append("Yes")
else:
print(f"\nLeft sub_array:{left}, \nright sub_array:{right}\nPrinting No")
result.append("No")
return result


if __name__ == "__main__":
t = int(input())
x = 0
while x < t:
n = int(input())
arr = list(map(int, input().split()))
m = int(input())
q = list()
for i in range(0,m):
q.append(list(map(int, input().split())))
result = processqueries(arr,n,m,q)
for i in result:
print(i)
x += 1
Loading

0 comments on commit be81090

Please sign in to comment.