Category Archives: Arrays

Questions regarding Arrays

Sum of XOR of all possible subsets

Given an array arr[] of size n, we need to find sum of all the values that comes from XORing all the elements of the subsets. Input : arr[] = {1, 5, 6} Output : 28 Total Subsets = 23 1 = 1 5 = 5 6 = 6 1 ^ 5 = 4 1… Read More »

Find duplicates under given constraints

A sorted array contains 6 different numbers, only 1 number is repeated five times. So there are total 10 numbers in array. Find the duplicate numbers using two comparisons only. Examples: Input: arr[] = {1, 1, 1, 1, 1, 5, 7, 10, 20, 30} Output: 1 Input: arr[] = {1, 2, 3, 3, 3, 3,… Read More »

Maximum sum of smallest and second smallest in an array

Given an array, find maximum sum of smallest and second smallest elements chosen from all possible subarrays. More formally, if we write all (nC2) subarrays of array of size >=2 and find the sum of smallest and second smallest, then our answer will be maximum sum among them. Examples: Input : arr[] = [4, 3,… Read More »

Constant time range add operation on an array

Given an array of size N which is initialized with all zeros. We are given many range add queries, which should be applied to this array. We need to print final updated array as our result. Examples: N = 6 Arr = [0, 0, 0, 0, 0, 0] rangeUpdate1 [0, 2], add 100 Arr =… Read More »

Sum of all Subarrays

Given an integer array ‘arr[]’ of size n, find sum of all sub-arrays of given array. Examples: Input : arr[] = {1, 2, 3} Output : 20 Explanation : {1} + {2} + {3} + {2 + 3} + {1 + 2} + {1 + 2 + 3} = 20 Input : arr[] = {1,… Read More »