Found 26504 Articles for Server Side Programming

Count Unique Characters of All Substrings of a Given String in C++

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

631 Views

Suppose we want to define a function called countUniqueChars(s) that will return the number of unique characters on s, so if s = "HELLOWORLD" then "H", "E", "W", "R", "D" are the unique characters since they appear only once in s, therefore countUniqueChars(s) = 5.Now on this problem given a string s we have to find the sum of countUniqueChars(t) where t is a substring of s. (Here some substrings can be repeated so on this case we have to count the repeated ones too.)As the answer can be very large, we can return answer modulo 10^9+7.So, if the input ... Read More

Making A Large Island in C++

Arnab Chakraborty
Updated on 08-Jun-2020 10:28:50

276 Views

Suppose we have a 2D grid of binary values (0s and 1s), we change at most one 0 to a 1. After that we have to find what is the size of the largest island? Here an island is a 4-directionally (top, bottom, left, right) connected group of 1s.So, if the input is like [[1, 0], [0, 1]], then the output will be 3, this is because if we change one 0 to 1 and connect two 1s, then we will get an island with area = 3.To solve this, we will follow these steps −Define an array dir of ... Read More

Bricks Falling When Hit in C++

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

210 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

243 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

390 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

555 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

254 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

Advertisements