Found 7197 Articles for C++

Count minimum right flips to set all values in an array in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:19:10

274 Views

We are given an array of 0s and 1s which represent the state of bulbs that are connected with the same wire in sequence. 0 represents that the bulb is OFF and 1 represents that the bulb is ON. For such a sequence of N bulbs, if the switch of the bulb is pressed then all bulbs on right, (i+1 th till n) change their previous stare, from ON to OFF or from OFF to ON.For the given state of all bulbs, the goal is to find the minimum switches to be pressed to turn all of them ON. [ ... Read More

Count minimum number of “move-to-front” moves to sort an array in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:17:26

1K+ Views

We are given with an array of numbers between 1 to n. The goal here is to find the no. of ‘move to front’ operations required to sort the given array. The array has no repetition. The ‘move to front’ operation picks an element and places at first position, here at index 0.We will traverse the array from end, if element is at the correct position then no move else move is required. For elements from 1 to n, the correct position in the array of element arr[i] should be at index i+1. arr[0] should be 1, arr[1] should be ... Read More

Count minimum bits to flip such that XOR of A and B equal to C in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:16:37

381 Views

We are given with three binary sequences A, B and C of length N. Each sequence represents a binary number. We have to find no. of flips required of bits in A and B such that XOR of A and B results in C. A XOR B becomes C.First of all let us learn about truth table of XOR operation −XYX XOR Y000011101110From the above table we observe that for the same values in X and Y, X XOR Y results 0 else it results 1. So this will be helpful for finding bits to be flipped in A and ... Read More

Count index pairs which satisfy the given condition in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:14:19

402 Views

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

Count frequencies of all elements in array in O(1) extra space and O(n) time in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:11:32

1K+ Views

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

Count even length binary sequences with same sum of first and second half bits in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:09:54

473 Views

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

Count elements such that there are exactly X elements with values greater than or equal to X in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:07:40

597 Views

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

Count elements less than or equal to a given value in a sorted rotated array in C++

Ravi Ranjan
Updated on 10-Jun-2025 17:50:57

530 Views

A sorted and rotated array is an array that is sorted in ascending or descending order and then rotated either left or right by a specific number of elements. There should exist exactly one pivot point around which the array is rotated. Example Here is an example of counting elements less than or equal to a given value in the sorted and rotated array: Case 1 The given array is: 30 40 50 10 20 Given value = 25 Output Count: 2 (10, 20) Case 2 The given array is: 10 20 30 40 ... Read More

Maximum count of pairs which generate the same sum in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:04:40

729 Views

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

Maximum count of equal numbers in an array after performing given operations in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:02:35

1K+ Views

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

Advertisements