Found 7347 Articles for C++

Check If All 1's Are at Least Length K Places Away in C++

Arnab Chakraborty
Updated on 17-Nov-2020 12:16:26

123 Views

Suppose we have an array nums of 0s and 1s and an integer k, we have to check whether all 1's are at least k places away from each other, otherwise, return False.So, if the input is like nums = [1, 0, 0, 0, 1, 0, 0, 1], k = 2, then the output will be true, as each of the 1s are at least 2 places away from each other.To solve this, we will follow these steps −last := -1for initialize i := 0, when i < size of nums, update (increase i by 1), do −if nums[i] is ... Read More

Check If a String Can Break Another String in C++

Arnab Chakraborty
Updated on 17-Nov-2020 12:14:52

175 Views

Suppose we have two strings s1 and s2 whose size are same; we have to check whether some permutation of string s1 can break some permutation of string s2 or vice-versa. A string a can break string b if x[i] >= y[i] (in alphabetical order) for all i in range 0 to n-1.So, if the input is like s1 = abc and s2 = xya, then the output will be true. This is because "ayx" is a permutation of s2 that can break to string "abc" which is a permutation of s1="abc".To solve this, we will follow these steps −Define ... Read More

Max Difference You Can Get From Changing an Integer in C++

Arnab Chakraborty
Updated on 17-Nov-2020 12:12:39

126 Views

Suppose we have an integer num. We will apply the following steps exactly two times, the steps are like −Pick a digit x in range 0 to 9.Pick another digit y also in range 0 to 9. The digit y can be equal to x.Replace all the occurrences of x in the decimal representation of num by y. The new integer cannot have any leading zeros, also the new integer cannot be of value 0.Now consider a and b be the results of applying the operations to num the first and second times, respectively. Then find the max difference between ... Read More

Diagonal Traverse II in C++

Arnab Chakraborty
Updated on 17-Nov-2020 12:08:03

103 Views

Suppose we have a list of lists called nums, we have to show all elements of nums in diagonal order.So, if the input is likethen the output will be [1, 6, 2, 8, 7, 3, 9, 4, 12, 10, 5, 13, 11, 14, 15, 16]To solve this, we will follow these steps −Define an array retDefine one 2D array vfor initialize i := 0, when i < size of nums, update (increase i by 1), do −for initialize j := 0, when j < size of nums[i], update (increase j by 1), do −insert { nums[i, j], i, j } ... Read More

Maximum Points You Can Obtain from Cards in C++

Arnab Chakraborty
Updated on 17-Nov-2020 12:10:01

240 Views

Suppose there are several cards arranged in a row, each card has associated points, and these points are given in the integer array called cardPoints. In one step, we can take one card from the beginning or from the end of the row. We have to take exactly k cards. The final score will be the sum of the points of the cards we have taken. So, if we have integer array cardPoints and the integer k, then find the maximum score we can obtain.So, if the input is like cardPoints = [1, 2, 3, 4, 5, 6, 1], k ... Read More

Minimum Number of Frogs Croaking in C++

Arnab Chakraborty
Updated on 17-Nov-2020 12:03:01

163 Views

Suppose we have a string called croakOfFrogs, this represents a combination of the string "croak" from different frogs, multiple frogs can croak at the same time, so multiple "croak" are mixed. We have to find the minimum number of different frogs to finish all the croak in the given string.Here a valid "croak" means a frog is generating 5 letters ‘c’, ’r’, ’o’, ’a’, ’k’ sequentially. The frogs have to generate all five letters to finish a croak. If the string is not a valid "croak" string then return -1.So, if the input is like "crcoakroak", then the output will ... Read More

Display Table of Food Orders in a Restaurant in C++

Arnab Chakraborty
Updated on 17-Nov-2020 12:00:26

262 Views

Suppose we have an array orders, which represents the orders that customers have done in a restaurant. So, orders[i]=[cust_namei, table_numi, food_itemi] where cust_namei is the customer name, table_numi is the customers table number, and food_itemi is the item customer orders.We have to return the restaurant's “display table”. Here the “display table” is a table whose row entries denote how many of each food item each table ordered. The first column will be the table number and the remaining columns correspond to each food item in alphabetical order. First row should be a header whose first column is “Table”, followed by ... Read More

The k-th Lexicographical String of All Happy Strings of Length n in C++

Arnab Chakraborty
Updated on 17-Nov-2020 11:56:29

140 Views

Suppose we have a string. We will call that a happy string when it consists of only ['a', 'b', 'c'] letters, and s[i] != s[i + 1] for all values of i from 1 to length of s - 1 (here the string is 1-indexed).So, if we have two integers n and k, consider a list of all happy strings of length n sorted in lexicographical order. We have to find the the kth string of this list or return an empty string if there are less than k happy strings of length nSo, if the input is like n ... Read More

Find the Minimum Number of Fibonacci Numbers Whose Sum Is K in C++

Arnab Chakraborty
Updated on 17-Nov-2020 11:54:40

431 Views

Suppose we have a number k, we have to find the minimum number of Fibonacci numbers whose sum is equal to the k, whether a Fibonacci number could be used multiple times.So, if the input is like k = 7, then the output will be 2, as the Fibonacci numbers are: 1, 1, 2, 3, 5, 8, 13, ... For k = 7 we can use 2 + 5 = 7.To solve this, we will follow these steps −Define an array finsert 0 at the end of finsert 1 at the end of fwhile last element of f = 0 and k > 0), do −if f[j] 0) {          if (f[j]

HTML Entity Parser in C++

Arnab Chakraborty
Updated on 17-Nov-2020 11:53:33

548 Views

Suppose we have a string; we have to design one HTML parser that will replace the special character of HTML syntax into normal character. The HTML entity parser is the parser that takes HTML code as input and replace all the entities of the special characters by the characters itself. These are the special characters and their entities for HTML −Quotation Mark − the entity is " and symbol character is ".Single Quote Mark − the entity is ' and symbol character is '.Ampersand − the entity is & and symbol character is &.Greater Than Sign − the entity is ... Read More

Advertisements