Found 7197 Articles for C++

Smallest String With Swaps in C++

Arnab Chakraborty
Updated on 31-Mar-2020 09:15:52

400 Views

Suppose we have given a string s, and an array of pairs of indices in the string pairs where pairs[i] = [a, b] indicates 2 indices(0-indexed) of the string. We can swap the characters at any pair of indices in the given pairs any number of times as we want. We have to find the lexicographically smallest string that s can be changed to after using the swaps. So if the input is like s = “dcab” and pairs = [[0, 3], [1, 2]], then the output will be “bacd”. Exchange s[0] and s[3], s = "bcad", then exchange s[1] ... Read More

Ugly Number III in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:38:57

235 Views

Suppose we have to write a program to find the n-th ugly number. The Ugly numbers are positive integers which are divisible by a or b or c. So for example, if n = 3 and a = 2, b = 3 and c = 5, then the output will be 4, as the ugly numbers are [2, 3, 4, 5, 6, 8, 9, 10], the third one is 4.To solve this, we will follow these steps −make a method called ok(), this will take x, a, b, c, this will act like below −return (x/a) + (x/b) + (x/c) ... Read More

Find Smallest Common Element in All Rows in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:35:19

210 Views

Suppose we have a matrix mat where every row is sorted in non-decreasing order, we have to find the smallest common element in all rows. If there is no common element, then return -1. So if the matrix is like −1234524581035791113579The output will be 5To solve this, we will follow these steps −Define a map m, n := row count of matrix, if n is not 0, then x = column size, otherwise 0for i in range 0 to n – 1for j in range 0 to x – 1if m[mat[i, j]] + 1 = i + 1, then increase ... Read More

Minimum Knight Moves in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:32:23

869 Views

Suppose we have an infinite chessboard with coordinates from -infinity to +infinity, and we have a knight at square [0, 0]. A knight has 8 possible moves it can make, as shown below. Each move is two squares in a cardinal direction, then one square in an orthogonal direction.We have to find the minimum number of steps needed to move the knight to the square [x, y]. It is guaranteed the answer exists.So if the input is like x = 5 and y = 5, then the output will be 4. This will be like [0, 0] → [2, 1] ... Read More

Reverse Substrings Between Each Pair of Parentheses in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:28:04

376 Views

Suppose we have a string s that consists of lower case letters and brackets. We have to reverse the strings in each pair of matching parentheses, starting from the innermost one. And the result should not contain any brackets. So if the input is like "(hel(lowo)rld)", then the output will be "dlrlowoleh", so from the beginning, it is changed like: "(hel(lowo)rld)" → "(helowolrld)" → “dlrowoleh”.To solve this, we will follow these steps −n := size of string, make an array called par whose length is n, define a stack stfor i in range 0 to n – 1if s[i] is ... Read More

Shortest Distance to Target Color in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:23:16

253 Views

Suppose we have an array color, in which there are three colors: 1, 2 and 3. We have given some queries. Each query consists of two integers i and c, we have to find the shortest distance between the given index i and the target color c. If there is no solution, then return -1. So if the colors array is like [1, 1, 2, 1, 3, 2, 2, 3, 3], and the queries array is like [[1, 3], [2, 2], [6, 1]], the output will be [3, 0, 3]. This is because the nearest 3 from index 1 is ... Read More

Before and After Puzzle in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:18:40

161 Views

Suppose we have a list of phrases, generate a list of Before and After puzzles. Here a phrase is a string that consists of lowercase letters and spaces only. No space will be there at the starting and ending positions. There are no consecutive spaces in a phrase.The before and after puzzles are phrases that are formed by merging two phrases where the last word of the first phrase is the same as the first word of the second phrase. We have to find the Before and After puzzles that can be formed by every two phrases phrases[i] and phrases[j] ... Read More

Convex Polygon in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:12:32

2K+ Views

Suppose we have a list of points that form a polygon when joined sequentially, we have to find if this polygon is convex (Convex polygon definition). We have to keep in mind that there are at least 3 and at most 10, 000 points. And the coordinates are in the range -10, 000 to 10, 000.We can assume the polygon formed by given points is always a simple polygon, in other words, we ensure that exactly two edges intersect at each vertex and that edges otherwise don't intersect each other. So if the input is like: [[0, 0], [0, 1], ... Read More

Unique Substrings in Wraparound String in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:04:17

259 Views

Suppose we have the string s to be the infinite wraparound string of "abcdefghijklmnopqrstuvwxyz", so the value s will look like this − "...zabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcd....".Now we have another string p. Our job is to find out how many unique non-empty substrings of p are present in s. In particular, our input is the string p and we need to output the number of different non-empty substrings of p in the string s.So if the input is like “zab” the output will be 6. There are 6 substrings “z”, “a”, “b”, “za”, “ab”, “zab” of the string “zab” in the string sTo ... Read More

Can I Win in C++

Arnab Chakraborty
Updated on 29-Apr-2020 16:01:45

269 Views

Suppose in a game called "100 games, " two players take turns adding, to a running total, any integer from 1 through 10. The player who first causes the running total to reach or exceed 100, he/she wins. So what if we change the game so that players cannot re-use integers?For example, if two players take turns drawing from a common pool of numbers of 1..15 without replacement until they reach a total >= 100.So suppose given an integer maxChoosableInteger and another integer desired total, determine if the first player to move can force a win, assuming both players play ... Read More

Advertisements