Found 33676 Articles for Programming

Count no. of columns that are not sorted in increasing order in C++

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

260 Views

We are given an array of strings each of the same length. The goal is to find columns, ( matrix of strings ) that are not sorted in increasing order. For example, each first character in string is compared with the first character of the next string and so on till the last string. If they are not in increasing order, increase the count. Do this for all second characters, then third characters of all strings and so on till the last character.InputArr[]= { “abc”, “bcd”, “def” }OutputCount of columns: 0Explanation − for each column,Column 1: character at index 0 : a

Count minimum steps to get the given desired array in C++

Sunidhi Bansal
Updated on 28-Jul-2020 11:22:13

372 Views

We are given with an array target[] which has numbers in it. We must find the minimum steps in which array with all zeros [0, 0, 0, 0…] can be converted to target using following two operations only −Increment operation − all elements can be incremented by 1, each increment operation can be counted individually in steps. ( for n increments in n elements steps=n )Doubling operation − the whole array is doubled. For all elements it is counted once. ( each doubling operation doubles all elements’ value, count it as 1 in stepsThe goal is to find the minimum ... Read More

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

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

275 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

382 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

406 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

475 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

532 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

Advertisements