C++ Articles

Page 213 of 597

Encode Number in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 655 Views

Suppose we have a non-negative integer n, and we have to find the encoded form of it. The encoding strategy will be as follows −NumberEncoded number0“”1“0”2“1”3”00”4”01”5”10”6”11”7”000”So if the number is 23, then result will be 1000, if the number is 54, then it will be 10111To solve this, we will follow these steps −Create one method called bin, this will take n and k, this method will act like belowres := empty stringwhile n > 0res := res + the digit of n mod 2n := n /2reverse the number reswhile x > length of resres := prepend 0 with ...

Read More

Count Square Submatrices with All Ones in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 265 Views

Suppose we a binary matrix, of size m x n. We have to count number of square submatrices, with all 1s. So if the matrix is like −011111110111So there will be 15 squares. 10 squares of single ones, 4 squares of four ones, and 1 square with nine ones.To solve this, we will follow these steps −set ans := 0, n := row count and m := column countfor i in range 0 to m – 1ans := ans + matrix[n – 1, i]for i in range 0 to n – 1ans := ans + matrix[i, m – 1]ans := ...

Read More

Smallest Common Region in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 274 Views

Suppose we have some lists of regions where the first region of each list includes all other regions in that list. basically, if a region X contains another region Y then, X is larger than Y. Also by definition a region X contains itself. So if we have two regions r1 and r2, we have to find the smallest region that contains both of them. So if we have r1, r2 and r3 such that r1 includes r3, it is guaranteed there is no r2 such that r2 includes r3. So if the input is like [["Earth", "North America", "South ...

Read More

Find the Smallest Divisor Given a Threshold in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 818 Views

Suppose we have an array of integers called nums and an integer k, that is threshold value, we will choose a positive integer divisor and divide all the array by it and sum the result of the division. We have to find the smallest divisor such that the result mentioned above is less than or equal to threshold value k. For example − if nums = [1, 2, 5, 9] and k = 6, then the output will be 5. We can get the sum as (1+2+5+9) = 17 when the divisor is 1. If the divisor is 4, then ...

Read More

Group the People Given the Group Size They Belong To in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 463 Views

Suppose there are n people whose IDs are in range 0 to n - 1 and each person belongs exactly to one group. We have the array groupSizes of length n. This array is indicating that the group size each person belongs to, we have to find the groups there are and the people's IDs each group includes.Suppose the input is like − [3, 3, 3, 3, 3, 1, 3], then the output is [[5], [0, 1, 2], [3, 4, 6]], Other possible solutions can be [[2, 1, 6], [5], [0, 4, 3]] or [[5], [0, 6, 2], [4, 3, ...

Read More

Sequential Digits in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 493 Views

Suppose we have an integer, that has sequential digits if and only if each digit in the number is one more than the previous digit. We have to find a sorted list of all the integers in the range [low, high] inclusive that have sequential digits. So if the low = 100 and high = 300, then the output will be [123,234]To solve this, we will follow these steps −create one array resfor i in range 1 to nfor j := 1, until j + i – 1

Read More

Divide Array in Sets of K Consecutive Numbers in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 334 Views

Suppose we have an array of integers nums and a positive integer k, we have to find whether it's possible to divide this array into sets of k consecutive numbers. So we have to return True if its possible otherwise return False. So if the input is like [1, 2, 3, 3, 4, 4, 5, 6] and k = 4, then output will be true. This is because, we can divide the array such that [1, 2, 3, 4] and [3, 4, 5, 6]To solve this, we will follow these steps −Make one map m, set n := size of ...

Read More

Sum of Mutated Array Closest to Target in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 380 Views

Suppose we have an integer array arr and a target value target, we have to find the integer value such that when we change all the integers larger than value in the given array will be equal to value, the sum of the array gets as nearest as possible to target. If they are same, then return the minimum such integer. So if the array is like [4, 9, 3] and target is 10, then the output will be 3 as using 3, the array will be [3, 3, 3], so the sum is 9, that is nearest element to ...

Read More

Bulb Switcher in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Suppose there are n bulbs that are initially switched off. We first turn on all the bulbs. Then, we turn off every second bulb. On the third round, we toggle every third bulb (turning on if it's off or turning off if it's on). Similarly, for the i-th round, we toggle every i bulb. For the n-th round, we only toggle the last bulb. So we have to find how many bulbs are on after n rounds. So if the input is 3, then the result will be 1. This is because −At first, the three bulbs are [off, off, ...

Read More

Implement Rand10() Using Rand7() in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 485 Views

Suppose we have a function rand7 which generates a uniform random integer in the range 1 to 7, we have to write another function rand10 which generates a uniform random integer in the range 1 to 10. We cannot use some library function to generate random numbers.Suppose we want two random numbers, so they may be [8, 10].To solve this, we will follow these steps −rand40 := 40while rand40 >= 40rand40 := (rand7() - 1) * 7 + (rand7() – 1)return rand40 mod 10 + 1Let us see the following implementation to get better understanding −Example#include using namespace std; ...

Read More
Showing 2121–2130 of 5,962 articles
« Prev 1 211 212 213 214 215 597 Next »
Advertisements