Found 7197 Articles for C++

Couples Holding Hands in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:17:43

386 Views

Suppose there are N couples and they have sat on 2N seats arranged in a row and want to hold hands. We have to find the minimum number of swaps so that every couple is sitting side by side.The people and seats are represented by a number from 0 to 2N-1, the couples are numbered in order, this is like the first couple as (0, 1), the second couple as (2, 3), and so on and the last couple as (2N-2, 2N-1).The couples' initial seating is given by another array called row, and row[i] being the value of the person ... Read More

Special Binary String in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:15:14

2K+ Views

Suppose we have a spatial binary string. This string has following few properties −There are same number of 0s and 1sEvery Prefix in the binary string has at least as many 1s as 0sNow suppose we have special string S, a move is actually choosing two consecutive, non-empty, special substrings of S, and swapping them.We have to find the lexicographically largest resulting string possible, at the end of any number of moves.So, if the input is like 11011000, then the output will be 11100100, this is because: The substrings "10" and "1100" are swapped. This is the lexicographically largest string ... Read More

Number of Atoms in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:12:02

552 Views

Suppose we have a chemical formula; we have to find the count of each atom.An atomic element will always start with an uppercase character, there can be zero or more lowercase letters, representing the name. And 1 or more digits representing the count of that element may follow if the count is greater than 1. But if the count is 1, no digits will follow. As an example, H2O and H2O2 both are valid, but H1O2 is invalid.So, if the input is like Na2(CO)3, then the output will be C3Na2O3, so this indicates 3 Carbon (C), 2 Sodium (Na), 3 ... Read More

Count natural numbers whose all permutation are greater than that number in C++

Sunidhi Bansal
Updated on 06-Jun-2020 12:11:50

428 Views

We are given a natural number let’s say, num and the task is to calculate the count of all those natural numbers whose all permutations are greater than that number.We are working with the following conditions −The data should be natural numbers onlyAll the possible permutations or arrangement of a natural number should be equal or greater than the given number. For example, the number is 20Consider all the numbers till 20 starting from 1 i.e. 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20Now check those numbers whose arrangement or permutation is equaled or greater than the given number i.e. 20. Numbers are 1, 2, 3, 4, 5, 6, 7, 8, 9, 11=11, 12

Count elements smaller than or equal to x in a sorted matrix in C++

Ravi Ranjan
Updated on 12-Jun-2025 17:19:15

252 Views

In this article, we are given a matrix of size m x n and an integer variable X. The row elements and the first column of the matrix are sorted in increasing order. Our task is to count the number of elements that are equal to or less than the given X value. Counting Elements Smaller Than X in a Sorted MatrixHere are the approaches for counting elements smaller than X in a sorted matrix: Using Linear Search Using Staircase Search Using Linear ... Read More

Count Number of animals in a zoo from given number of head and legs in C++

Sunidhi Bansal
Updated on 06-Jun-2020 12:04:31

1K+ Views

We are given a total number of heads and legs in a zoo and the task is to calculate the total number of animals there in the zoo with the given data. In the below program we are considering animals to be deer and peacocks.Input −heads = 60 legs = 200Output −Count of deers are: 40 Count of peacocks are: 20Explanation −let total number of deers to be : x Let total number of peacocks to be : y As head can be only one so first equation will be : x + y = 60 And deers have 4 ... Read More

Count frequency of k in a matrix of size n where matrix(i, j) = i+j in C++

Sunidhi Bansal
Updated on 06-Jun-2020 12:00:30

292 Views

We are given a matrix of integer values and the task is to calculate the count of the frequency of a given integer variable let’s say, k in the matrix. The size of a matrix can depend upon the size the user wants and in the below program we are taking it to be 4X4. Matrix will be formed on the given condition i.e matrix(i, j) will be i+j. The index value of the first data in a matrix will be 0 and 0 i.e. matrix[0][0] = 0.Input − int size = 4, k = 4Output − count of 4 ... Read More

Count n digit numbers not having a particular digit in C++

Sunidhi Bansal
Updated on 06-Jun-2020 11:56:53

271 Views

We are given a number let’s say, num and a total number of digits stored in an integer type variable let’s say, digi and the task is to calculate the count of those n digits numbers that can be formed where the given digit is not there.Input − n = 2, digit = 2Output − count is 153Explanation − count of all two digit numbers(n) not having digit 2 is 153 as 10, 11, 13, 14, 15, 16, 17, 18, 19, 30, 31, 33, 34, ......., etc.Input − n = 3, digit = 3Output − count is 2187Explanation − count ... Read More

Count digits in given number N which divide N in C++

Sunidhi Bansal
Updated on 06-Jun-2020 11:53:47

2K+ Views

We are given a number let’s say, N and the task is to find the count of those digits in a number that divides the number N.Points to be rememberIf the digit is 0 then it should be ignored which means count will not be increased for 0.If a digit is appearing twice and it divides the number, then the count will depend upon the digit occurrence. For example, we are given a number 2240 and in this number every digit except the 0 will divide the number and 2 is occurring twice then the count will be 2 for ... Read More

Count n digit numbers divisible by given number in C++

Sunidhi Bansal
Updated on 06-Jun-2020 11:51:27

412 Views

We are given the two elements let’s say, d and num, the task is to find the d digit numbers which are divisible by num.In simple words let us suppose we have given an input 2 in d, so we will first find all the 2-digit numbers i.e. from 10-99 and then find all the numbers which are divisible by num.Let us understand more of this with the help of examples −Input − digit = 2, num= 12Output − Count of n digit numbers divisible by given number: 8Explanation − The 2-digit numbers divisible by 12 are 12, 24, 36, ... Read More

Advertisements