Skip to content

Commit c0caa67

Browse files
Sudo Placement 2019
1 parent 6465a02 commit c0caa67

11 files changed

+895
-130
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Chocolate Distribution Problem ---> Flipkart
2+
# Difficulty: Easy   Marks: 2
3+
'''
4+
Given an array A of positive integers of size N, where each value represents number of chocolates in a packet. Each packet can have variable number of chocolates. There are M students, the task is to distribute chocolate packets such that :
5+
1. Each student gets one packet.
6+
2. The difference between the number of chocolates given to the students having packet with maximum chocolates and student having packet with minimum chocolates is minimum.
7+
8+
Input:
9+
The first line of input contains an integer T, denoting the number of test cases. Then T test cases follow. Each test case consists of three lines. The first line of each test case contains an integer N denoting the number of packets. Then next line contains N space separated values of the array A denoting the values of each packet. The third line of each test case contains an integer m denoting the no of students.
10+
11+
Output:
12+
For each test case in a new line print the minimum difference.
13+
14+
Constraints:
15+
1 <= T <= 100
16+
1 <=N<= 107
17+
1 <= Ai <= 1018
18+
1 <= M <= N
19+
20+
Example:
21+
Input:
22+
2
23+
8
24+
3 4 1 9 56 7 9 12
25+
5
26+
7
27+
7 3 2 4 9 12 56
28+
3
29+
30+
Output:
31+
6
32+
2
33+
34+
Explanation:
35+
Testcase 1: The minimum difference between maximum chocolates and minimum chocolates is 9-3=6
36+
'''
37+
for _ in range(int(input())):
38+
n=int(input())
39+
a=list(map(int,input().split()))
40+
d=int(input())
41+
a.sort()
42+
min=abs(a[0]-a[d-1])
43+
for i in range(1,len(a)-d+1):
44+
if abs(a[i]-a[d-1+i])<min:
45+
min=abs(a[i]-a[d-1+i])
46+
print(min)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# Find the closest number
2+
# Difficulty: Basic   Marks: 1
3+
'''
4+
Given an array of sorted integers. The task is to find the closest value to the given number in array. Array may contain duplicate values.
5+
6+
Note: If the difference is same for two values print the value which is greater than the given number.
7+
8+
Input:
9+
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. First line of each test case contains two integers N & K and the second line contains N space separated array elements.
10+
11+
Output:
12+
For each test case, print the closest number in new line.
13+
14+
Constraints:
15+
1<=T<=100
16+
1<=N<=105
17+
1<=K<=105
18+
1<=A[i]<=105
19+
20+
Example:
21+
Input:
22+
2
23+
4 4
24+
1 3 6 7
25+
7 4
26+
1 2 3 5 6 8 9
27+
28+
Output:
29+
3
30+
5
31+
'''
32+
for _ in range(int(input())):
33+
n1,n2=map(int,input().split())
34+
a=list(map(int,input().split()))
35+
a.append(n2)
36+
a.sort()
37+
for i in range(len(a)):
38+
if a[-1]==n2:
39+
print(a[-2])
40+
break
41+
else:
42+
if a[i]==n2:
43+
if a[i+1]==n2:
44+
print(n2)
45+
break
46+
else:
47+
if abs(n2-a[i+1])==abs(n2-a[i-1]):
48+
print(a[i+1])
49+
break
50+
else:
51+
if abs(n2-a[i+1])>abs(n2-a[i-1]):
52+
print(a[i-1])
53+
break
54+
else:
55+
print(a[i+1])
56+
break
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#immediate smaller element -----> AMAZON
2+
#marks = 1, difficulty = Basic
3+
'''
4+
Given an integer array of size N. For each element in the array, check whether the right adjacent element (on the next immediate position) of the array is smaller. If next element is smaller, print that element. If not, then print -1.
5+
6+
Input:
7+
The first line of input contains an integer T denoting the number of test cases. T testcases follow. Each testcase contains 2 lines of input:
8+
The first line contains an integer N, where N is the size of array.
9+
The second line contains N integers(elements of the array) sperated with spaces.
10+
11+
Output:
12+
For each test case, print the next immediate smaller elements for each element in the array.
13+
14+
Constraints:
15+
1 ≤ T ≤ 200
16+
1 ≤ N ≤ 107
17+
1 ≤ arr[i] ≤ 1000
18+
19+
Example:
20+
Input
21+
2
22+
5
23+
4 2 1 5 3
24+
6
25+
5 6 2 3 1 7
26+
27+
Output
28+
2 1 -1 3 -1
29+
-1 2 -1 1 -1 -1
30+
31+
Explanation:
32+
Testcase 1: Array elements are 4, 2, 1, 5, 3. Next to 4 is 2 which is smaller, so we print 2. Next of 2 is 1 which is smaller, so we print 1. Next of 1 is 5 which is greater, so we print -1. Next of 5 is 3 which is smaller so we print 3. Note that for last element, output is always going to be -1 because there is no element on right.
33+
'''
34+
n=int(input())
35+
for i in range(n):
36+
m=int(input())
37+
a=list(map(int,input().split()))
38+
for j in range(m-1):
39+
if j<m-1:
40+
if a[j]>a[j+1]:
41+
print(a[j+1],end=' ')
42+
else:
43+
print(-1,end=' ')
44+
else:
45+
print(-1)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# Leaders in an array ---> Amazon, Payu, Samsung
2+
# Difficulty: Easy   Marks: 2
3+
'''
4+
Given an array of positive integers. Your task is to find the leaders in the array.
5+
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.
6+
7+
Input:
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+
Output:
13+
Print all the leaders.
14+
15+
Constraints:
16+
1 <= T <= 100
17+
1 <= N <= 107
18+
0 <= Ai <= 107
19+
20+
Example:
21+
Input:
22+
3
23+
6
24+
16 17 4 3 5 2
25+
5
26+
1 2 3 4 0
27+
5
28+
7 4 5 7 3
29+
Output:
30+
17 5 2
31+
4 0
32+
7 7 3
33+
34+
Explanation:
35+
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.
36+
'''
37+
for _ in range(int(input())):
38+
n=int(input())
39+
a=list(map(int,input().split()))
40+
# c=a.copy()
41+
# for i in range(len(a)-1):
42+
# for j in range(i+1,len(a)):
43+
# if a[i]<a[j]:
44+
# c.remove(a[i])
45+
# break
46+
c=[a[-1]]
47+
for i in range(len(a)-2,-1,-1):
48+
if a[i]>= c[-1]:
49+
c.append(a[i])
50+
# for i in c:
51+
# print(i,end=' ')
52+
print(*(c[::-1]),end='')
53+
print()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Print an array in Pendulum Arrangement ---> FactSet
2+
# Difficulty: Basic   Marks: 1
3+
'''
4+
Write a program to input a list of n integers in an array and arrange them in a way similar to the to-and-fro movement of a Pendulum.
5+
6+
The minimum element out of the list of integers, must come in center position of array. If there are even elements, then minimum element should be moved to (n-1)/2 index (considering that indexes start from 0)
7+
The next number (next to minimum) in the ascending order, goes to the right, the next to next number goes to the left of minimum number and it continues like a Pendulum.
8+
Input:
9+
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 next line contains N space separated integers forming the array.
10+
11+
Output:
12+
Output the array in Pendulum Arrangement.
13+
14+
Constraints:
15+
1<=T<=500
16+
1<=N<=100
17+
1<=a[i]<=1000
18+
19+
Example:
20+
Input:
21+
2
22+
5
23+
1 3 2 5 4
24+
5
25+
11 12 31 14 5
26+
27+
Output:
28+
5 3 1 2 4
29+
31 12 5 11 14
30+
'''
31+
for _ in range(int(input())):
32+
n=int(input())
33+
a=list(map(int,input().split()))
34+
a.sort(reverse=True)
35+
if len(a)%2!=0:
36+
print(*(a[::2]),end=' ')
37+
else:
38+
print(*(a[1::2]),end=' ')
39+
print(*(a[-2::-2]))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Reverse an array ---> Infosys, Moonfrog Labs
2+
# Difficulty: School   Marks: 0
3+
'''
4+
Given an array A of size N, print the reverse of it.
5+
6+
Input:
7+
First line contains an integer denoting the test cases 'T'. T testcases follow. Each testcase contains two lines of input. First line contains N the size of the array A. The second line contains the elements of the array.
8+
9+
Output:
10+
For each testcase, in a new line, print the array in reverse order.
11+
12+
Constraints:
13+
1 <= T <= 100
14+
1 <= N <=100
15+
0 <= Ai <= 100
16+
17+
Example:
18+
Input:
19+
1
20+
4
21+
1 2 3 4
22+
Output:
23+
4 3 2 1
24+
'''
25+
for _ in range(int(input())):
26+
n=int(input())
27+
a=list(map(int,input().split()))
28+
a.reverse()
29+
print(*a)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Reverse array in groups ---> Amazon
2+
# Difficulty: Basic   Marks: 1
3+
'''
4+
Given an array arr[] of positive integers of size N. Reverse every sub-array of K group elements.
5+
6+
Input:
7+
The first line of input contains a single integer T denoting the number of test cases. Then T test cases follow. Each test case consist of two lines of input. The first line of each test case consists of an integer N(size of array) and an integer K separated by a space. The second line of each test case contains N space separated integers denoting the array elements.
8+
9+
Output:
10+
For each test case, print the modified array.
11+
12+
Constraints:
13+
1 ≤ T ≤ 200
14+
1 ≤ N, K ≤ 107
15+
1 ≤ A[i] ≤ 1018
16+
17+
Example:
18+
Input
19+
2
20+
5 3
21+
1 2 3 4 5
22+
6 2
23+
10 20 30 40 50 60
24+
25+
Output
26+
3 2 1 5 4
27+
20 10 40 30 60 50
28+
29+
Explanation:
30+
Testcase 1: Reversing groups in size 3, first group consists of elements 1, 2, 3. Reversing this group, we have elements in order as 3, 2, 1.
31+
'''
32+
from math import ceil
33+
for i in range(int(input())):
34+
n,k=map(int,input().split())
35+
arr=list(map(int,input().split()))
36+
arr[:k]=arr[k-1::-1] #i was having a bit difficulty in converting the first group inside the loop
37+
for i in range(1,ceil(float(n)/k)):
38+
arr[k*i:k*i+k]=arr[k*i+k-1:k*i-1:-1]
39+
print(*arr)
40+
'''for _ in range(int(input())):
41+
n1,n2=input().split()
42+
a=list(map(int,input().split()))
43+
# if int(n1)%int(n2)!=0:
44+
# n2=int(n1)//int(n2)+1
45+
j=int(n1)//int(n2)
46+
for i in range(j):
47+
c=[]
48+
for k in range(int(n2)*i,int(n2)*(i+1)):
49+
c.append(a[k])
50+
print(*(c[::-1]),end=' ')
51+
print('',*(a[:int(n2)*j-1:-1]),end='',sep=' ')
52+
print()
53+
'''
54+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Rotate Array ---> Amazon,MAQ Software
2+
# Difficulty: Basic   Marks: 1
3+
'''
4+
Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).
5+
6+
Input:
7+
The first line of the input contains T denoting the number of testcases. First line of eacg test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements.
8+
9+
Output:
10+
For each testcase, in a new line, output the rotated array.
11+
12+
Constraints:
13+
1 <= T <= 200
14+
1 <= N <= 107
15+
1 <= D <= N
16+
0 <= arr[i] <= 105
17+
18+
Example:
19+
Input:
20+
2
21+
5 2
22+
1 2 3 4 5
23+
10 3
24+
2 4 6 8 10 12 14 16 18 20
25+
26+
Output:
27+
3 4 5 1 2
28+
8 10 12 14 16 18 20 2 4 6
29+
30+
Explanation :
31+
Testcase 1: 1 2 3 4 5 when rotated by 2 elements, it becomes 3 4 5 1 2.
32+
'''
33+
for _ in range(int(input())):
34+
m,d=tuple(map(int,(input().split())))
35+
a=list(map(int,input().split()))
36+
for j in range(m-d):
37+
print(a[j+d],end=' ')
38+
for j in range(d):
39+
print(a[j],end=' ')
40+
print()

0 commit comments

Comments
 (0)