Server Side Programming Articles - Page 1754 of 2650

Bricks Falling When Hit in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:26:54

221 Views

Suppose we have a grid of binary values (0s and 1s) the 1s in a cell represent the bricks. A brick will not drop when that satisfies these conditions −Either brick is directly connected to the top of the gridor at least one of its adjacent (top, bottom, left, right) bricks will not drop.We will do some erasures sequentially. In each case we want to do the erasure at the location (i, j), the brick (if that is present) on that location will disappear, and then some other bricks may drop because of that erasure. We have to find the ... Read More

Sliding Puzzle in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:23:31

2K+ Views

Suppose we have one 2x3 board, there are 5 tiles those are represented by the numbers 1 through 5, and one empty square is there, that is represented by 0.Here a move means 0 and one adjacent number (top, bottom, left or right) and swapping it. This will be solved when the elements are arranged in this manner: [[1, 2, 3], [4, 5, 0]].We have the puzzle board; we have to find the least number of moves required so that the state of the board is solved. If this is not possible to solve, then return -1.So, if the input ... Read More

Max Chunks To Make Sorted II in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:19:39

252 Views

Suppose we have an array arr of integers, we have to split the array into some number of partitions, and individually sort each partition. Now after concatenating them we will get one sorted array. We have to find the maximum number of partitions we could have made?So, if the input is like [3, 2, 4, 5, 5], then the output will be 4, as we can make partitions like [3, 2], [4], [5], [5].To solve this, we will follow these steps −cnt := 1n := size of arrDefine an array maxOfLeft of size nDefine an array minOfRight of size nmaxOfLeft[0] ... Read More

Couples Holding Hands in C++

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

403 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

565 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

447 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

272 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

305 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

Advertisements