C++ Articles

Page 214 of 597

Ones and Zeroes in C++

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

Suppose we have a dominator of m 0s and n 1s respectively. On the other hand, there is an array with binary strings. Now our task is to find the maximum number of strings that we can generate with given m 0s and n 1s. Each 0 and 1 can be used at most once. So if the input is like Array = [“10”, “0001”, “111001”, “1”, “0”, ] and m = 5 and n = 3, then the output will be 4. This is because there are totally 4 strings can be formed by the using of 5 0s ...

Read More

Matchsticks to Square in C++

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

Suppose there is a little match girl. And we know exactly what matchsticks the little match girl has, we have to find out a way we can make one square by using up all those matchsticks. We should not break any stick, but we can link them up, and each matchstick must be used exactly one time. Our input will be several matchsticks the girl has, represented with their stick length. our output will be either true or false, to represent whether we could make one square using all the matchsticks the match girl has. So if the input is ...

Read More

Generate Random Point in a Circle in C++

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

Suppose we have the radius and x-y positions of the center of a circle, we have to write a function called randPoint() which generates a uniform random point in the circle. So there will be some important points that we have to keep in mind −Input and output values are in floating-point.Radius and x-y position of the center of the circle is passed into the class constructor.A point on the circumference of the circle is considered to be in the circle.The randPoint() returns x-position and y-position of the random point, in that order.So if the input is like [10, 5, ...

Read More

Magical String in C++

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

Suppose there is a string. That string is called a magical string S, that consists of only '1' and '2' and obeys the following rules −The string S is magical because concatenating the number of contiguous occurrences of characters '1' and '2' generates the string S itself.The first few components of string S is the following − S = "1221121221221121122……"If we group the consecutive '1's and '2's in S, it will be − 1 22 11 2 1 22 1 22 11 2 11 22 ...... and the occurrences of '1's or '2's in each group are − 1 2 ...

Read More

Teemo Attacking in C++

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

Suppose in LOL world, there is a hero named Teemo and his attacking can make his enemy Ashe be in poisoned condition. Now, suppose we have given the Teemo's attacking ascending time series towards Ashe and the poisoning time duration per Teemo's attacking, we have to find the total time that Ashe is in poisoned condition. We can assume that Teemo attacks at the very beginning of a specific time point, and makes Ashe be in poisoned condition immediately.The input is like [1, 4] and 2, then the output will be 4. So this is because at time point 1, ...

Read More

Increasing Subsequences in C++

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

Suppose we have an integer array, our task is to find all the different possible increasing subsequences of the given array, and the length of an increasing subsequence should be at least 2. So if the array is like [4, 6, 7, 7], then the output will be like − [[4, 6], [4, 7], [4, 6, 7], [4, 6, 7, 7], [6, 7], [6, 7, 7], [7, 7], [4, 7, 7]].To solve this, we will follow these steps −Define an array, called res to store all resultsMake a method called solve. This will take nums array, start and temp arrayif ...

Read More

Longest Palindromic Subsequence in C++

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

Suppose we have a string s, we have to find the longest palindromic subsequence's length in s. We can assume that the maximum length of s is 1000. So if the input is like “bbbab”, then output will be 4. One possible palindromic subsequence is “bbbb”.To solve this, we will follow these steps −x := s, then reverse x, n := size of sif n is 0, then return 0update s by adding one blank space before s, and update x by adding one blank space before xret := 0make one matrix dp of size (n + 1) x (n ...

Read More

Random Point in Non-overlapping Rectangles in C++

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

Suppose we have a list of non-overlapping axis-aligned rectangles rects, we have to write a function pick which randomly and uniformly picks an integer number, point in the space covered by the rectangles. So we have to keep in mind some points −An integer point is a point that has integer coordinates.A point on the perimeter of a rectangle is included in the space covered by the rectangles.The ith rectangle = rects[i] denotes [x1, y1, x2, y2], where [x1, y1] are the integer coordinates of the bottom-left corner, and [x2, y2] are the integer coordinates of the top-right corner.The length ...

Read More

Random Flip Matrix in C++

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

Suppose we have a binary matrix with n_rows number of rows and n_cols number of columns. Here all values are initially 0. we have to define a function flip() which chooses a 0 value uniformly at random, changes it to 1, and then returns the position [row.id, col.id] of that value. Also, we have to write another function reset() which sets all values back to 0. We have to try to minimize the number of calls to system's Math.random() and optimize the time and space complexity.If we have the matrix of order 2x3, and we call flip four times, then ...

Read More

Diagonal Traverse in C++

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

Suppose we have a matrix of M x N elements, we have to find all elements of the matrix in diagonal order. So if the matrix is like −123456789The output will be [1, 2, 4, 7, 5, 3, 6, 8, 9]To solve this, we will follow these steps −Make an array ret, set row := 0 and col := 0, n := row count, m := col count, down := falsefor i in range 0 to n – 1x := i, y := 0create an array tempwhile x >= 0 and y < m, doinsert matrix[x, y] into temp, and ...

Read More
Showing 2131–2140 of 5,962 articles
« Prev 1 212 213 214 215 216 597 Next »
Advertisements