├── Antonio's Store.py ├── Array-into-Subarray.py ├── Divisibility-by-K.py ├── Dominant-Element.py ├── Longest-Good-Array-1.0.py ├── Minimum-Flips-in-Circular-Array.py ├── Minimum-Pairwise-Product.py ├── NP-Problem.py ├── Not-Equal-Pairs.py ├── Numbers-of-Array.py ├── Odd-Prefix-2.py ├── Odd-Subset.py ├── Optimal-Division.py ├── Perfectly-Filled-Buckets.py ├── Powerful-Triplet.py ├── README.md ├── Ranking-Elections.py ├── Simply-Equal.py └── Sorting-Machine.py /Antonio's Store.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Antonio’s Store 3 | 4 | 5 | Antonio is a very successful businessman. Recently he bought a basketball Store. In the basketball Store, there are N containers, where each container can store at most K basketballs. 6 | Initially ith. container contains A[i] balls. Now he bought M more basketballs from somewhere and wants to store them in these containers. He can store the jth ball (1<=j<=M) in any container. He cannot remove initial balls from the container. 7 | 8 | Your task is to determine the minimum capacity K such he can store all the additional M balls into the containers. 9 | 10 | Input Format 11 | The first line contains two space-separated integers N and M. 12 | The second line contains N space-separated integers, the array A. 13 | 14 | Output Format 15 | Print the minimum capacity K for that Antonio can store all the additional M balls into the containers 16 | 17 | Constraints 18 | 1<=N<=105 19 | 1<=A[i],M<=109. 20 | 21 | Sample Input 1 22 | 3 4 23 | 1 2 3 24 | 25 | Sample Output 1 26 | 4 27 | 28 | Explanation of Sample 1 29 | For the given test case, we can set K=4 and then 30 | Add 2 more basketballs in the first. 31 | Add 2 more basketballs in the second 32 | """ 33 | 34 | 35 | def solve(arr, length, extras): 36 | empty = max(arr) * length - sum(arr) 37 | 38 | if empty > extras: 39 | return max(arr) 40 | return max(arr) + ((extras - empty) // length) + 1 41 | 42 | 43 | n, m = [int(x) for x in input().split()] 44 | 45 | a = list(map(int, input().split())) 46 | 47 | print(solve(a, n, m)) 48 | -------------------------------------------------------------------------------- /Array-into-Subarray.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Array into sub-array 3 | 4 | You have been given an array A of N integers. 5 | Divided it into 3 non-empty sub-arrays, such that sum of each sub-array can be represented in form of 2^k. K is a whole number. 6 | 7 | Eg. A = [1, 2, 3, 1] 8 | 9 | A can be divided into [1], [2], [3, 1]. These have sum of 1 = 2^0, 2 = 2^1, and 4 = 2^2. 10 | You can add 1 at any index of array. You can do add any number of time. 11 | 12 | You need to find minimum number of times, 1 should be added such that all three sub-array is in 2^k form. 13 | 14 | Input: 15 | T : Number of test cases. 16 | N : length of array 17 | A : array 18 | 19 | Constrains 20 | 1 <= N <= 1000 21 | 1 <= A[i] <= 100 22 | All input are integers. 23 | 24 | Input Format: 25 | First line contain T 26 | Second line contain size of array. 27 | Third line contain array of votes received 28 | 29 | Output Format: 30 | Each separate line for each testcase results. 31 | 32 | Sample input: 33 | 1 34 | 3 35 | 2 1 3 36 | 37 | Sample output: 38 | 1 39 | 40 | Explanation: 41 | This can be divided into [2], [1], and [3, 1]; which does satisfy 2^k form. 42 | """ 43 | 44 | # Solution with explanation. 45 | 46 | # we need to compare sum to nearest exponent. It is better to just have a list to compute every time. 47 | exponents_of_2 = [1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536, 131072] 48 | 49 | # this function return difference of x from next larger 2^k. 50 | def diff(x): 51 | for i in exponents_of_2: 52 | if x == i: 53 | return 0 54 | elif x < i: 55 | return i - x 56 | 57 | 58 | # This function finds the minimum number of required ones 59 | def solve(Nums, length): 60 | if length < 3: 61 | return 0 62 | 63 | # this is just a random number. Which I think would be maximum possible ans. Probably. 64 | ans = 131072 65 | T_sum = sum(Nums) 66 | 67 | # I am dividing the entire array into 3 parts: NumsA, NumsB, and NumsC. 68 | # NumsA is Nums[0:i], NumsB is Nums[i:j], and NumsC is [j:] 69 | # sumA, and ansA corresponds to NumA. sumA is simply sum of all elements, and ansA is difference of sumA and next larger 2^k. 70 | sumA, ansA = 0, 1 71 | 72 | for i in range(1, length - 1): 73 | 74 | temp = Nums[i - 1] 75 | 76 | if temp < ansA: 77 | sumA += temp 78 | ansA -= temp 79 | else: 80 | sumA += temp 81 | ansA = diff(sumA) 82 | 83 | sumB, ansB = 0, 1 84 | 85 | sumC = T_sum - sumA 86 | 87 | for j in range(i + 1, length): 88 | temp = Nums[j - 1] 89 | 90 | if temp < ansB: 91 | sumB += temp 92 | ansB -= temp 93 | else: 94 | sumB += temp 95 | ansB = diff(sumB) 96 | 97 | sumC -= temp 98 | ones_needed = ansA + ansB + diff(sumC) 99 | 100 | if ans > ones_needed: 101 | ans = ones_needed 102 | 103 | return ans 104 | 105 | 106 | # number of test cases 107 | T = int(input()) 108 | results = [] 109 | 110 | for i in range(T): 111 | N = int(input()) # length of array 112 | A = list(map(int, input().split())) 113 | results.append(solve(A, length=N)) 114 | 115 | print(*results, sep="\n") -------------------------------------------------------------------------------- /Divisibility-by-K.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Divisibility By k 3 | 4 | You are given an array A of N integers. 5 | In one operation, you select a non-empty subsequence of array A and increase or decrease each element of the array by 1. 6 | Find the minimum number of operations you need to make each element of the array A, divisible by K. 7 | 8 | You are given T independent test cases. 9 | 10 | Constraints 11 | 1 <= T <= 10 12 | 1 <= N <= 105 13 | 2 <= K <= 105 14 | 0 <= Ai <= 109 15 | All input values are integers. 16 | 17 | Input Format 18 | First-line contains T. 19 | First line of each test case consists of two space separated integers N and K. 20 | Second line of each test case has N space separated integers denoting the array A. 21 | 22 | Output Format 23 | Print in a newline for each test case a single integer denoting the minimum number of operations she needs to make each element of the array divisible by K. 24 | 25 | Sample Input 1 26 | 1 27 | 4 3 28 | 4 3 6 2 29 | 30 | Sample Output 1 31 | 2 32 | 33 | Explanation of Sample 1 34 | Iteration 1: Select {A0}, and decrement them. A = [3,3,6,2] 35 | Iteration 2: Select {A3}, and increment them. A = [3,3,6,3] 36 | 37 | Now each element of the array is divisible by 3. 38 | """ 39 | 40 | # Solution with explanation: 41 | 42 | # Step1: Take modulus of every int. For k = 5, Ai 1, 6, 11, 16... all these are same. They would need equal effort. So make them one. 43 | # You don't even need to iterate over entire list. Just till you have found all possible remainders. 44 | 45 | # now move a pointer from 1, to k-1. Push everything on left to 0 and everything on right to k. Count cost. 46 | # repeat and return smallest of them. 47 | 48 | def solve(arr, length, K): 49 | # this is to check appearance of every possible remainders. Last True is just buffer. 50 | remainders = [False] * K + [True] 51 | for i in arr: 52 | remainders[i % K] = True 53 | if all(remainders): 54 | break 55 | 56 | # k is maximum possible iteration. 57 | iterations = K 58 | for i in range(1, K): 59 | # we are moving a divider from 1 to k. 60 | # pushing every left to zero, and right to k. 61 | remainders_left = remainders[:i] 62 | remainders_right = remainders[i:] 63 | 64 | temp = 0 65 | if True in remainders_left: 66 | temp = len(remainders_left) - 1 - remainders_left[::-1].index(True) 67 | if True in remainders_right: 68 | temp += K - i - remainders_right.index(True) 69 | iterations = min(iterations, temp) 70 | 71 | return iterations 72 | 73 | 74 | test_cases = int(input()) 75 | output = [] 76 | 77 | for i in range(test_cases): 78 | length, K = input().split() 79 | K = int(K) 80 | 81 | arr = list(map(int, input().split())) 82 | 83 | output.append(solve(arr, length, K)) 84 | 85 | print(*output, sep="\n") 86 | -------------------------------------------------------------------------------- /Dominant-Element.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question Name: Dominant Element 3 | 4 | You are given an array A consisting of N positive integers. The ith element of the array is called Dominant if : 5 | 6 | 1. Let B be an array formed by removing Ai from array A. 7 | Like if A = [5 1 2 3 2] and i=3, B will be = [5 1 3 2]. 8 | 9 | 2. Now Ai is dominant if it is possible to rearrange elements of array B such that Ai+j > Bj holds for all 1<=j<=length of array B. 10 | 11 | Like if Ai = 2 and B = [3 1 2], Ai is dominant. 12 | THis is because if we rearrange elements of array B as [2 1 3], Ai+1 > B1, Ai+2 > B2 and Ai+3 > B3 holds. 13 | Given array A, print the number of Dominant elements in array A. 14 | 15 | Input Format 16 | First line contains a single integer denoting N. 17 | Next line contains N space separated integers denoting the elements of array A. 18 | 19 | Output Format 20 | Print the number of Dominant elements in array A. 21 | 22 | Constraints 23 | 1<=N<=10^5 24 | 1<=Ai<=N 25 | 26 | Sample Input 1 27 | 5 28 | 5 1 4 3 2 29 | 30 | Sample Output 1 31 | 2 <- this is wrong. Ans should be 4. Relevel did some mistake. 32 | 33 | Explanation of Sample 1 34 | For i=1, Ai = 5, B = [1 4 3 2]. Rearrange it as [1 2 3 4 5]. 5+1 > 1, 5+2 > 2, ... 35 | For i=3, Ai = 4, B = [5 1 3 2]. 36 | For i=4, Ai = 3, B = [5 1 4 2]. 37 | For i=5, Ai = 2, B = [5 1 4 3]. 38 | 39 | 40 | """ 41 | 42 | def solve(arr, length): 43 | count = 0 44 | for value in arr: 45 | if arr[-1] < value + length - 1: 46 | count += 1 47 | return count 48 | 49 | 50 | n = int(input()) 51 | A = [int(x) for x in input().split()] 52 | A.sort() 53 | 54 | print(solve(A, n)) 55 | -------------------------------------------------------------------------------- /Longest-Good-Array-1.0.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Longest Good Array 1.0 3 | 4 | You have been given an array A of N integers. 5 | P is defined as sum of every previous element in array. 6 | for: 1<= k <= N 7 | Pk = A1 + A2 + ... Ak 8 | 9 | An array is called good array if for each k (1<= k <= N ): Pk + Y*Ak = X. 10 | Find the size of longest sequence of good array within given array. 11 | 12 | Constrains 13 | 1 <= T <= 1000 14 | 1 <= N <= 1000 15 | 1 <= A[i] <= 10000 16 | 1 <= X, Y <=10^9 17 | All input are integers. 18 | 19 | Input: 20 | T : Number of test cases. 21 | N : length of array 22 | A : array 23 | X, Y : for good array calculation. 24 | 25 | Input Format: 26 | First line contain T 27 | Second line contain size of array. 28 | Third line contain array. 29 | Fourth line contain X and Y. 30 | 31 | Output Format: 32 | Each separate line for each testcase results. 33 | 34 | Sample input: 35 | 1 36 | 2 1 37 | 4 1 38 | 39 | Sample output: 40 | 2 41 | 42 | Explanation: 43 | A = [2, 1] 44 | P = [2, 3] 45 | good array: 46 | P1 + Y*A1 == Y -> 2 + 1*2 == 4 -> True 47 | P2 + Y*A2 == Y -> 3 + 1*1 == 4 -> True 48 | [True, True] 49 | The longest sub-array of good array is 2 50 | 51 | """ 52 | 53 | # Solution with explanation. 54 | 55 | T = int(input()) # no of test cases 56 | output = [] 57 | 58 | for i in range(T): 59 | A = list(map(int, input().split())) 60 | X, Y = input().split(" ") 61 | X, Y = int(X), int(Y) 62 | 63 | p_arr_element = 0 64 | ans = count = 0 65 | 66 | for arr_element in A: 67 | p_arr_element += arr_element 68 | 69 | good_array_element = p_arr_element + Y * arr_element == X 70 | 71 | if good_array_element: 72 | count += 1 73 | ans = max(ans, count) 74 | else: 75 | count = 0 76 | output.append(ans) 77 | 78 | print(*output, sep="\n") 79 | -------------------------------------------------------------------------------- /Minimum-Flips-in-Circular-Array.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Minimum Flips in Circular Array 3 | 4 | You are given a circular binary array Arr of size N (A circular array is an array where we consider the first and last element to be adjacent). 5 | In one operation you can choose an index i(1<=i<=N) and flip the number at index i, i.e.if Arr[i]=1 you can make it 0 and vice versa 6 | 7 | You are given two integers O and Z.Your aim is to make a subarray of size O+Z such that the first O digits of the subarray are 1s and the last Z digits of the subarray are 0s 8 | What is the minimum number of operations required to do so? 9 | 10 | Note: A subarray is a slice from a contiguous array (i.e., occupy consecutive positions) and inherently maintains the order of elements. 11 | 12 | Input Format 13 | The first line of the input contains 3 space-separated integers denoting N O and Z respectively 14 | The next line contains N space separated integers denoting Arr[i](1<=i<=N) 15 | 16 | Output Format 17 | Print an integer denoting the minimum number of operations required to make a subarray of size O+Z such that the first O digits of the subarray are 1s and the last Z digits of the subarray are 0s 18 | 19 | Constraints 20 | 1<=N<=10^5 21 | 0<=O,Z<=N 22 | 1<=O+Z<=N 23 | 0<=Arr[i](1<=i<=N)<=1 24 | 25 | Sample Input 26 | 4 1 1 27 | 1 1 1 1 28 | 1 29 | 30 | Sample Output 31 | 1 32 | 33 | Sample Explanation 34 | One of the solutions is to flip the digit at index 2 resulting in our array as 1 0 1 1 and 1 0(index 1 and 2) will be our required subarray 35 | """ 36 | 37 | # Solution with explanation: 38 | 39 | 40 | def solve(arr, length, ones, zeros): 41 | ansO = ones - sum(arr[0:ones]) 42 | ansZ = sum(arr[ones : ones + zeros]) 43 | ans = ansO + ansZ 44 | j = ones 45 | k = ones + zeros 46 | for i in range(1, length): 47 | j +=1 48 | k +=1 49 | 50 | if arr[j - 1] and not arr[i - 1]: 51 | ansO = max(0, ansO - 1) 52 | if not arr[j - 1] and arr[i - 1]: 53 | ansO += 1 54 | 55 | if arr[k - 1] and not arr[j - 1]: 56 | ansZ += 1 57 | if not arr[k - 1] and arr[j - 1]: 58 | ansZ = max(0, ansZ - 1) 59 | ans = min(ans, ansO + ansZ) 60 | return ans 61 | 62 | 63 | N, O, Z = [int(x) for x in input().split()] 64 | A = [int(x) for x in input().split()] 65 | A += A[: O + Z - 2] 66 | print(solve(A, N, O, Z)) 67 | -------------------------------------------------------------------------------- /Minimum-Pairwise-Product.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Minimum Pairwise Product 3 | 4 | You are given an array of N size, and another array of M size. 5 | 6 | `Compatibility` of two arrays A and B is measured as: 7 | 8 | 1. Two arrays are compatible if and only if they have an equal number of elements. 9 | 2. `Compatibility` value of arrays A and B each having length L = A1*B1 + A2*B2 + … + AL*BL. 10 | 11 | You can remove any number of element from both of these arrays to get an `compatibility`. 12 | 13 | Print the maximum possible `compatibility`. 14 | 15 | Input Format 16 | First line contains two space separated integers denoting N and M. 17 | Next line contains N space separated integers denoting the elements of array A. 18 | Next line contains M space separated integers denoting the elements of array B. 19 | 20 | Output Format 21 | Print the maximum compatibility value it's possible to achieve by removing some elements from both the arrays 22 | 23 | Constraints 24 | 1<=N,M<=1000 25 | -105<=Ai, Bi<=105 26 | 27 | Sample Input 1 28 | 3 4 29 | -1 2 1 30 | -100 3 10 -9 31 | 32 | Sample Output 1 33 | 120 34 | 35 | Explanation of Sample 1 36 | Erase the 3rd element of array A. Array A becomes = [-1, 2] 37 | Erase the 2nd and 4th element of array A. Array A becomes = [-100, 10] 38 | Compatibility values of these arrays = [(-1)*(-100) + 2*10] = 120 39 | """ -------------------------------------------------------------------------------- /NP-Problem.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: NP Problem 3 | 4 | An array A of N integers is said to be a good if these conditions are met. 5 | 1. Elements of array are non-decreasing. i.e A1 < A2 < A3 <... < An. 6 | 2. Product of all elements is less than or equal to P. 7 | 8 | Find how many distinct good array A, exist for a given value of N and P. 9 | 10 | Constrains 11 | 1 <= T <= 5 12 | 1 <= N <= 3 13 | 1 <= P <= 2 * 10^10 14 | All input are integers. 15 | 16 | Input: 17 | T : Number of test cases. 18 | N : length of array 19 | P : for good array calculation. 20 | 21 | Input Format: 22 | First line contain T 23 | Second line contain N and P. 24 | 25 | Output Format: 26 | Each separate line for each testcase results. 27 | 28 | Sample input: 29 | 1 30 | 2 4 31 | 32 | Sample output: 33 | 5 34 | 35 | Explanation: 36 | 37 | All possible good array are [1,1], [1,2], [1,3], [1,4], and [2,2]. 38 | """ 39 | 40 | # Solution with explanation. 41 | 42 | def solve(target): 43 | # this function solves for n ==2 44 | sum = 0 45 | i = 0 46 | while i < P // i: 47 | sum += P//i - i + 1 48 | i += 1 49 | return ans 50 | 51 | T = int(input()) 52 | for i in range(T): 53 | N, P = input().split(" ") 54 | N, P = int(N), int(P) 55 | 56 | ans = 0 57 | if N == 1: 58 | ans = P 59 | elif N == 2: 60 | ans = solve(P) 61 | elif N == 3: 62 | sum = 0 63 | i = 1 64 | while True: 65 | temp = solve(P//i) 66 | if temp > 0: 67 | sum += temp 68 | i+=1 69 | else: 70 | break 71 | ans = sum 72 | print(ans) -------------------------------------------------------------------------------- /Not-Equal-Pairs.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question Name: Not Equal pairs 3 | 4 | You are given Array A consists of N positive integers. 5 | 6 | Return maximum number of pairs that can be formed from Array such that: 7 | 1. both elements are not equal, i.e. for every (a, b) : a != b 8 | 2. Any element can be in at most one pair. 9 | 10 | Input Format 11 | First line contains a single integer denoting N. 12 | Next line contains N space separated integers denoting the elements of array A. 13 | Output Format 14 | 15 | Constrains: 16 | 1<=N<=10^5 17 | 1<=Ai<=10^9 18 | 19 | Sample Input 1 20 | 5 21 | 2 1 3 2 1 22 | 23 | Sample Output 1 24 | 2 25 | 26 | Explanation of Sample 1 27 | pairs that can be formed or [[2,1], [3,2]] or [[2,1], [3,1]] or [[2,3],[1,2]] 28 | In maximum number of pairs are 2. 29 | """ 30 | 31 | # Solution with explanation. 32 | 33 | """ 34 | This question seems tricky; but is not. 35 | 36 | So, there are N elements. They must form N // 2 pairs. 37 | 38 | What can prevent them from doing so? 39 | If some random element occur more than n/2 times. If not, ans would always will be n//2. 40 | 41 | Now, say if some random element does occur more than n/2 times. 42 | Then, every other element will have a pair. 43 | so ans is n - frequency of majority element. 44 | """ 45 | 46 | 47 | def findPair(arr, n): 48 | majority_found = False 49 | majority_element, majority_frequency = 0, 0 50 | mapping = {} 51 | for i in arr: 52 | if majority_found: 53 | if i == majority_element: 54 | majority_frequency += 1 55 | else: 56 | if i in mapping: 57 | mapping[i] += 1 58 | else: 59 | mapping[i] = 1 60 | if mapping[i] > n // 2: 61 | majority_found = True 62 | majority_element = i 63 | majority_frequency = mapping[i] 64 | if majority_found: 65 | return n - majority_frequency 66 | return n // 2 67 | 68 | 69 | n = int(input()) 70 | arr = [int(x) for x in input().split(" ")] 71 | 72 | print(findPair(arr, n)) 73 | -------------------------------------------------------------------------------- /Numbers-of-Array.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Number of Arrays 3 | 4 | You have been given an array A of N integers. 5 | Find the the number of different array B than can be generated with following conditions. 6 | 7 | 1. len(B) == len(A) 8 | 2. 1<= B[i] <= A[i] for i : 1<= i <= N 9 | 3. B[i] != B[j] for i,j : i : 1<= i= 2: 89 | ans_arr.append(odd.pop()) 90 | ans_arr.append(odd.pop()) 91 | count += 1 92 | # We don't have 2 odd numbers, or the even number is lower, we place the even number 93 | else: 94 | ans_arr.append(even.pop()) 95 | count += 1 96 | count += len(even) + len(odd) // 2 97 | 98 | # NOTE: The order of the next two loops does not matter, as only 1 will run 99 | # Place the remaining odd numbers 100 | while odd: 101 | ans_arr.append(odd.pop()) 102 | 103 | # Place the remaining even numbers 104 | while even: 105 | ans_arr.append(even.pop()) 106 | 107 | print(count) 108 | print(ans_arr) 109 | 110 | 111 | n = input() 112 | a = [int(x) for x in input().split(" ")] 113 | 114 | ans(a) 115 | -------------------------------------------------------------------------------- /Odd-Subset.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Odd Subset 3 | 4 | You are given an array A of integers. 5 | 6 | For array A, print the maximum possible length of the subset of this array such that the sum of all elements of this subset is an odd number. 7 | If there is no such subset print “-1”(without quotes) instead. 8 | 9 | A subset of the array A as a tuple that can be obtained from A by removing some (possibly all) elements of it. 10 | 11 | Input Format 12 | First line contains a single integer denoting N. 13 | Next line contains N space separated integers denoting the elements of array A. 14 | 15 | Output Format 16 | Print the maximum possible length of the subset of the given array such that the sum of all elements of this subset is an odd number 17 | 18 | Constraints 19 | 1<=N<=105 20 | 1<=Ai<=109 21 | 22 | Sample Input 1 23 | 4 24 | 4 2 3 1 25 | 26 | Sample Output 1 27 | 3 28 | 29 | Explanation of Sample 1 30 | One can select the subset as [4 2 3 1]. 31 | The sum of all elements of this subset = 4+2+1 = 7 which is odd. 32 | """ 33 | 34 | 35 | def solve(arr, length): 36 | odd = 0 37 | even = 0 38 | for i in arr: 39 | if int(i) % 2: 40 | odd += 1 41 | else: 42 | even += 1 43 | 44 | if odd == 0: 45 | return -1 46 | 47 | if odd % 2: 48 | return even + odd 49 | return even + odd - 1 50 | 51 | 52 | length = input() 53 | arr = input().split() 54 | 55 | print(solve(arr)) 56 | -------------------------------------------------------------------------------- /Optimal-Division.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question Name: Optimal division 3 | 4 | You are given Array A consisting of N positive integers. 5 | 6 | Value of an array is defined as number of unique elements it have. 7 | Like value of array [1,2,1,2,2,3] = 3 as it has 3 unique elements(1,2 and 3), value of array [1,2,3,4] = 4 and so on. 8 | 9 | Split the array into 2 parts, such that sum of values of 2 sub-arrays are maximum. 10 | 11 | Note : It may be possible that one of them does not receive any element of the bought array. 12 | 13 | Input Format 14 | First line contains a single integer denoting N. 15 | Next line contains N space separated integers denoting the elements of the bought array. 16 | 17 | Output Format 18 | Print the maximum possible total value of the final arrays that can be achieved if elements of the array are dived optimally. 19 | 20 | Constraints 21 | 1<=N<=10^5 22 | 1<= element of the array<=10^9 23 | 24 | Sample Input 1 25 | 6 26 | 1 2 1 2 2 3 27 | 28 | Sample Output 1 29 | 5 30 | 31 | Explanation of Sample 1 32 | You can divide the array elements as : 33 | A1 = [1,2,2], value of array = 2 34 | A2 = [2,1,3], value of array = 3 35 | Hence total value = 2+3 = 5. 36 | 37 | It can be proved that this is the maximum possible value. 38 | """ 39 | 40 | 41 | def solve(arr, length): 42 | count = 0 43 | temp = -1 44 | for i in range(length): 45 | if arr[i] == temp: 46 | continue 47 | temp = arr[i] 48 | if i == length - 1 or arr[i] != arr[i + 1]: 49 | count += 1 50 | else: 51 | count += 2 52 | 53 | return count 54 | 55 | 56 | N = int(input()) 57 | A = input().split(" ") 58 | A.sort() 59 | 60 | print(solve(A, N)) 61 | -------------------------------------------------------------------------------- /Perfectly-Filled-Buckets.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Perfectly filled buckets. 3 | 4 | You have been given an array A of N prime numbers. You need to fill K buckets with integers of array. 5 | 6 | Initially, all buckets are empty. And they are numbered from 1 to N. 7 | Each element can be put in at most one bucket. Also, each bucket must contain at least one integer. 8 | 9 | A bucket is considered "perfectly-filled", if after removing at most one integer from the bucket, 10 | the product of resultant integers in the bucket becomes a perfect square, or bucket will become empty. 11 | 12 | Bi denotes the number of integers in the bucket numbered i. 13 | 14 | Find the maximum possible value of M, for M = min( B1, B2, ... BK), if buckets are filled optimally. 15 | 16 | Constrains 17 | 1 <= T <= 10 18 | 1 <= K <= N <= 100,000 19 | 2 <= A[i] <= 120 20 | Ai is prime number 21 | All input are integers. 22 | 23 | Input Format: 24 | First line contain T 25 | 26 | First line of each test cases contain K and N, separated by a space. 27 | Second line of each test cases contain N space separated ints. 28 | 29 | Output format: 30 | Print in a new line for each test case, a single integer denoting the maximum possible value. 31 | 32 | Sample input: 33 | 1 34 | 5 2 35 | 3 2 5 3 2 36 | 37 | Sample output: 38 | 2 39 | 40 | Explanation: 41 | We need to perfectly fill 2 buckets with maximum number of elements from Array A. 42 | 43 | B1 = 3: Bucket would contain [3, 5, 3]. 44 | B2 = 2: Bucket would contain [2, 2] 45 | so min(B1, B2) = 2 46 | 47 | Note: 48 | We have maximize size smaller of both B1, B2; to have bigger min of both. 49 | We can't add move 2 from b2 to b1 because: then b1 would not be perfectly filled and b2 size would become 1, reducing min value. 50 | For same, reason we can't move 3 from b2 to b1. 51 | We can move 5, but there won't be any chance to result. 52 | """ 53 | 54 | # Solution with explanation. 55 | 56 | # Since does ask, what buckets will contain; it is idiotic to calculate all combinations. 57 | # we will calculate possible length of sub-arrays. i.e max of min of bucket lengths. 58 | def solve(Arr, length, buckets): 59 | if buckets > length // 2: 60 | return 1 61 | if buckets == 1: 62 | return length 63 | 64 | Arr.sort() 65 | singles, pairs, ans = 0, 0, 0 66 | 67 | i = 0 68 | while i < length: 69 | if i == length - 1 or Arr[i] != Arr[i + 1]: 70 | singles += 1 71 | i += 1 72 | else: 73 | pairs += 1 74 | i += 2 75 | 76 | while pairs: 77 | if pairs >= buckets: 78 | ans += 2 79 | pairs -= buckets 80 | 81 | if singles >= buckets: 82 | ans += 1 83 | singles -= 1 84 | 85 | return ans 86 | 87 | 88 | Testcases = int(input()) 89 | 90 | for i in range(Testcases): 91 | N, B = [int(x) for x in input().split(" ")] 92 | A = [int(x) for x in input().split(" ")] 93 | 94 | print(solve(A, N, B)) 95 | -------------------------------------------------------------------------------- /Powerful-Triplet.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Powerful Triplet 3 | 4 | You are given an array of integers. 5 | 6 | For a triplet (X, Y, Z) (1<=X left_big: 53 | left_big = a[i - 1] 54 | if a[i - 1] < left_small: 55 | left_small = a[i - 1] 56 | 57 | if a[i] == right_big: 58 | right_big = max(a[i + 1 :]) 59 | 60 | temp = max(abs(left_small - a[i]), abs(left_big - a[i])) * right_big 61 | ans = max(ans, temp) 62 | 63 | output.append(ans) 64 | 65 | 66 | print(*output, sep="\n") 67 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Relevel Coding Questions 2 | 3 | Hi Everyone. 4 | 5 | Are you appearing for Backend, Frontend, Data Analytics, Full Stack or Software Testing test on Relevel? 6 | 7 |
8 | 9 | Good. I am proud of you for taking a step to improve yourself. 10 | 11 |
12 | 13 | For all these tests, Relevel has a total of 5 rounds. 14 | 15 | Round 1. Back To Basics 16 | 17 | Relevel has a total of 600 questions. Out of which 30, 40, or 60 questions (depending on the test you are appearing for) will be asked. 18 | 19 | - Most of these are easy. If you are unsure, use google lens to find answers immediately. I plan to create a page listing all of these questions. 20 | 21 | - If you are appearing for the test, feel free to send me the questions. I will put all questions with the right answers in one place. So, others may use it as a study guide. 22 | 23 |
24 | 25 | Round 2. DSA i.e. Coding Round 26 | 27 | Relevel has only 200 questions. Their questions are new and unique. Kudos. 28 | 29 | - Each of their questions looks difficult. They look like you need Ph.D. in CSE and mathematics to solve them. But, they don't. They are designed to fool you. 30 | 31 | 1. The first thing you should do is reduce the story given to you into 3 lines of a problem statement. 32 | 2. Now, create 5-6 dummy sample inputs of your own and think of a solution. 33 | 3. Think of edge cases where your problem statement may clash with the story. 34 | 4. Now, surprisingly easiest part, coding the problem statement. 35 | 36 | NOTE: Think of easy ways to solve. I promise you this, these question does not require any high level of DSA, only basics. Now, think of O(n) algorithm. Most of them can be solved in a linear time. 37 | 38 | - If you have appeared in these tests, please send me the questions you faced. I will add them here, with proper answers and explanations. 39 | 40 |
41 | Rounds 3 and 4. 42 | 43 | These are different for each category of test. But they follow the same pattern. 44 | 45 | Relevel have only 20 sets of question. Download the sample question set. Solve it. Change it a little. Practice it. You are good to go. 46 | 47 |
48 | Round 5. Interview. 49 | 50 | 51 | Good luck. 52 | 53 | Your friend
54 | Adarsh Gupta.
55 | hi@AdarshGupta.dev 56 | -------------------------------------------------------------------------------- /Ranking-Elections.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Ranking elections 3 | 4 | Political party 1 has `(N+1)` members for an election. `N` members have already received their votes. 5 | Array A contain votes received by N members in order. i.e. A[i] is received by ith member. 6 | 7 | Party decides rank based on their number of votes. Member with higher votes, receive better rank. 8 | In case of tie, member with lower i receive better rank. 9 | 10 | eg. 3 members received [2, 3, 2] votes respectively. Their ranks would be [2, 1, 3] respectively. 11 | 2nd member received highest votes, thus rank 1. Member 1, and 3 received equal votes, and 1 < 3; thus 1 is given better rank. 12 | 13 | (N+1)th member have yet to receive votes. Find out total number of different ranks he could possibly get. 14 | 15 | Input: 16 | T : Number of test cases. 17 | N, Q : number of members and number of queries. 18 | A : array of votes received 19 | L, Q : Query 20 | 21 | Constrains 22 | 1 <= T <= 1000 23 | 1 <= N,Q <= 100000 24 | 1 <= A[i] <= 10^9 25 | 1 <= L <= N 26 | 1 <= R <= 10^9 27 | All input are integers. 28 | 29 | Input Format: 30 | First line contain T 31 | Second line contain number of members and number of queries. 32 | Third line contain array of votes received 33 | Fourth line onwards contain query or modification. 34 | 35 | Output Format: 36 | Each separate line for each testcase results. 37 | 38 | Sample input: 39 | 1 40 | 4 1 41 | 2 1 1 5 42 | 2 3 43 | 44 | Sample output: 45 | 5 46 | 47 | Explanations: 48 | No of test cases 1. 49 | No of members that have received votes : 4. This makes total member : 5. Number of queries: 1 50 | member and their votes, member 1: 2, member 2:1, member 3:1, member 4:5. Ranks = [2, 3, 4, 1] 51 | Query: 2, 3. Member 2 has received 3 votes. New votes = [2, 3, 1, 5] Ranks = [3, 2, 4, 1] 52 | 53 | if n+1 receives 0 votes: he would receive 5th rank. Ranks = [3, 2, 4, 1, 5] 54 | if n+1 receives 1 votes: he would receive 5th rank. Ranks = [3, 2, 4, 1, 5] 55 | if n+1 receives 2 votes: he would receive 4th rank. Ranks = [3, 2, 5, 1, 4] 56 | if n+1 receives 3 votes: he would receive 3rd rank. Ranks = [4, 2, 5, 1, 3] 57 | if n+1 receives 4 votes: he would receive 2nd rank. Ranks = [4, 3, 5, 1, 2] 58 | if n+1 receives 5 votes: he would receive 2nd rank. Ranks = [4, 3, 5, 1, 2] 59 | if n+1 receives >5 votes: he would receive 1st rank. Ranks = [4, 3, 5, 2, 1] 60 | 61 | so number of different ranks he could receive is 5. 62 | """ 63 | 64 | # Solution with explanation 65 | 66 | # this function ranks members based on their number of votes 67 | def RankedMembers(votes): 68 | # sorting to have a reference of all votes in descending order. 69 | # In new array because, if same number of votes occur; index can be given priority. 70 | sorted_votes = sorted(votes) 71 | 72 | # Giving negative ranks to prevent conflict with vote count. 73 | rank = -1 74 | for i in sorted_votes: 75 | while i in votes: 76 | votes[votes.index(i)] = rank 77 | rank -= 1 78 | # now array contains only rank not votes. Removing negative and returning it back. 79 | return [-x for x in votes] 80 | 81 | 82 | # This function makes list of all possible ranks and returns count. 83 | def unique_ranks(votes): 84 | ranks = [] 85 | 86 | # Feeding all possibility of votes that can be received by n+1 th member. i.e. 0 to max + 1 87 | for i in range(0, max(votes) + 1): 88 | # Adding only unique to count. 89 | if RankedMembers(votes + [i]) not in ranks: 90 | ranks.append(RankedMembers(votes + [i])) 91 | 92 | return len(ranks) 93 | 94 | 95 | # number of test cases 96 | T = int(input()) 97 | 98 | 99 | for i in range(T): 100 | N, Q = input().split(" ") 101 | N, Q = int(N), int(Q) # length of array and queries 102 | 103 | A = input().split(" ") 104 | A = [int(x) for x in A] # votes 105 | 106 | for j in range(Q): 107 | L, R = input().split(" ") 108 | L, R = int(N), int(Q) # query 109 | 110 | A[L - 1] = R # changes to votes based on query 111 | 112 | unique_ranks(A) 113 | -------------------------------------------------------------------------------- /Simply-Equal.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Simply Equal 3 | 4 | You are given an array A of N integers. 5 | In one operation you can select a non-empty sub-sequence of A, and decrease each element of the subsequence by 1. 6 | 7 | Find the minimum number of operations required to make all the elements of the array A equal. 8 | 9 | You are given T independent test cases. 10 | 11 | Constraints 12 | 1 <= T <= 10 13 | 1 <= N <= 105 14 | 1 <= Ai <= 109 15 | All input values are integers. 16 | 17 | Input Format 18 | First-line contains T. 19 | First line of each test case consists of a single integer N. 20 | Second line of each test case consists of N space separated integers denoting the array A. 21 | Output Format 22 | 23 | Print in a newline for each test case a single integer denoting the minimum number of operations required to make all the elements of the array A equal. 24 | 25 | Sample Input 1 26 | 1 27 | 3 28 | 2 1 2 29 | 30 | Sample Output 1 31 | 1 32 | 33 | Explanation of Sample 1 34 | 35 | She can select the subsequence S = {A1, A3} and decrease each by 1. So array becomes: A = [1, 1, 1] 36 | """ 37 | 38 | test_case = int(input()) 39 | ans = [] 40 | 41 | for i in range(test_case): 42 | length = input() 43 | arr = input() 44 | y = list(map(int, arr.split())) 45 | ans.append(max(y) - min(y)) 46 | 47 | for i in ans: 48 | print(i) 49 | -------------------------------------------------------------------------------- /Sorting-Machine.py: -------------------------------------------------------------------------------- 1 | """ 2 | Question: Sorting Machine 3 | 4 | You have been given an array A of N element composed only of 1s and 0s. 5 | There is a machine that swaps every A[i] = 1 with A[i+1] = 0; in on second. 6 | Find the minimum time required to solve using this machine. 7 | 8 | Eg. A = [0, 1, 0, 0, 1, 0, 1, 0] 9 | 10 | After 1 sec: A = [0, 0, 1, 0, 0, 1, 0, 1] 11 | After 2 sec: A = [0, 0, 0, 1, 0, 0, 1, 1] 12 | After 3 sec: A = [0, 0, 0, 0, 1, 0, 1, 1] 13 | After 4 sec: A = [0, 0, 0, 0, 0, 1, 1, 1] 14 | 15 | So, Ans = 4. After 4 seconds, entire array is sorted. 16 | 17 | Input: 18 | T : Number of test cases. 19 | N : length of array 20 | A : array 21 | 22 | Constrains 23 | 1 <= T <= 10 24 | 1 <= N <= 100,000 25 | 0 <= A[i] <= 1 26 | All input are integers. 27 | 28 | Input Format: 29 | First line contain T 30 | Second line contain length of test case array 31 | Third line contain value of test case array 32 | 33 | Sample input: 34 | 2 35 | 3 36 | 1 0 0 37 | 2 38 | 0 1 39 | 40 | Sample output: 41 | 2 42 | 0 43 | 44 | Explanation: 45 | In case of A = [1, 0, 0] 46 | After 1 sec: A = [0, 1, 0] 47 | After 2 sec: A = [0, 0, 1] 48 | thus ans is 2. 49 | 50 | In case of A = [0, 1] 51 | Array is already sorted; thus ans = 0 52 | 53 | """ 54 | 55 | 56 | def countSeconds(array): 57 | zero, one, ans = 0, 0, 0 58 | for i in array[::-1]: 59 | if i == 1: 60 | ans = max(ans, one + zero) 61 | one = min(one + 1, one + zero) 62 | else: 63 | one = max(0, one - 1) 64 | zero += 1 65 | return ans 66 | 67 | 68 | TestCases = int(input()) 69 | solutions = [] 70 | for i in range(TestCases): 71 | length = int(input()) 72 | array = input().split(" ") 73 | array = [int(x) for x in array] 74 | 75 | solutions.append(countSeconds(array)) 76 | 77 | for i in range(TestCases): 78 | print(solutions[i]) 79 | --------------------------------------------------------------------------------