We are given an array of permutation of first N natural numbers. The goal here is to find the index pairs of elements that satisfy the condition mentioned below −If an array is Arr[], then i,j are indexes, count element pairs such that Arr[i]+Arr[j]=max(Arr[x]) such that i
We are given with an array of elements ranging from values 1 to n. Some elements are repeated, and some are missing. The goal is to find the frequencies of all elements in O(n) time and O(1) extra space.InputArr[]= { 1, 2, 2, 3, 4, 4, 4, 5 }Output1→ 1, 2 → 2, 3→ 1, 4→ 3, 5→ 5Explanation − The highest element is 5, the output shows the number of times each element appears in the array.InputArr[]= { 1, 4, 4, 5, 5, 5, 5 }Output1→ 1, 2 →0, 3→ 0, 4→ 2, 5→ 4Explanation − The highest element ... Read More
We are given several bits n as input for a binary sequence. The goal here is to find the binary sequence of length 2n such that the sum of its first and second half bits is equal. First n bits and next n bits have the same sum.We have a binary sequence so the only choice to put digits at any place is 0 and 1. For n bits at first and second half, no. of possible combinations are −n bits with all zeros (0 1’s) nC0= 1n bits with 1 1’s nC1n bits with 2 1’s nC2..n bits with ... Read More
We are given with an array of integers. The goal is to find the count of elements in the array that satisfy the following condition −For each element the count of numbers greater than or equal to it present in the array should be exactly equal to it. Excluding the element itself. If element is X then array has exactly X numbers which are greater or equal to X. (Excluding the element).InputArr[]= { 0, 1, 2, 3, 4, 9, 8 }OutputElements exactly greater than equal to itself : 1Explanation − Elements and numbers >= to it −Arr[0]: 6 elements are ... Read More
We are given with an array of integers. The goal is to find the maximum number of pairs in the array that when added produce the same sum. We have to find the maximum count of such pairs.InputArr[]= { 1, 2, 3, 4, 2 }OutputMaximum count of pairs with same sum : 3Explanation − Sum of pairs of numbers −{1, 2}, {1, 2} Sum:3 {1, 3}, {2, 2} Sum:4 {1, 4}, {2, 3}, {3, 2} Sum:5 {2, 4} Sum:6 {3, 4} Sum:7 Maximum count of pairs with same sum is 3 ( for sum = 5 )InputArr[]= { 5, 3, ... Read More
We are given with an array of integers. The goal is to find the maximum numbers in array that are equal after performing given operations −Select two elements a[i] and a[j] such that i != j andIncrement a[i] and decrement a[j] ( a[i]++, a[j]-- )We will take the sum of the array and divide it by the number of elements. If N is size of array thenIf sum is divisible by N then equal numbers will also be N otherwise Equal numbers will be N-1.InputArr[]= { 1, 2, 3 }OutputMaximum count of equal numbers : 3Explanation − After first step ... Read More
We are given with an array of positive integers. The goal is to find the maximum number of consecutive numbers present in it. First of all we will sort the array and then compare adjacent elements arr[j]==arr[i]+1 (j=i+1), if difference is 1 then increment count and indexes i++, j++ else change count=1. Store the maximum count found so far stored in maxc.InputArr[]= { 100, 21, 24, 73, 22, 23 }OutputMaximum consecutive numbers in array : 4Explanation − Sorted array is − { 21, 22, 23, 24, 73, 100 } initialize count=1, maxcount=11. 22=21+1 count=2 maxcount=2 i++, j++ 2. 23=22+2 count=3 ... Read More
We are given with an array of positive and negative integers and a number K. The task is to find the maximum sum of the array after K changes in it’s elements. The single change operation here multiplies the single element by -1.Approach used will be to convert every negative number to positive. If there are N negative numbers then, for this we will sort the array −If NK then change the sign of K negative numbers and add the array. Sum will be maximum.InputArr[]= { 0, -2, 6, 4, 8, 2, -3 } K=4OutputMaximum array sum is : 25Explanation ... Read More
We are given with two positive numbers num1 and num2. The goal is to find the minimum sum and maximum sum possible of these two after a digit replacement in both of them. We are allowed to replace digits from each of numbers in both of numbers. Suppose num1 is 434 and num2 is 324 and we can replace digit 3 with 4 and digic 4 with 3. Then minimum sum will be − 333+323=656 and maximum sum will be 444+424=864.Let us understand with examples for digit replacement 3 with 4 and vice versa −Inputnum1=3224 num2=4321OutputMaximum sum is : 8645 ... Read More
We are given with an array of integers of size N. The goal here is to find the Maximum and Minimum Product Subsets. We will do this by taking two product variables, one for minimum product found so far minProd and other for maximum product found so far, maxProd.While traversing the array we will multiply each element with both minProd and maxProd. Also keep a check on previous maximum product, previous minimum product, current maximum product, current minimum product and the current element itself.InputArr[]= { 1, 2, 5, 0, 2 }OutputMaximum Product: 20 Minimum Product: 0Explanation − Starting from the ... Read More