Found 7347 Articles for C++

Program to get next integer permutation of a number in C++

Arnab Chakraborty
Updated on 22-Dec-2020 06:36:12

332 Views

Suppose we have a number n, we have to find the next bigger permutation of its digits. When n is already in its largest permutation, then rotate it down to the smallest permutation.So, if the input is like n = 319, then the output will be 391.To solve this, we will follow these steps −Define a function makeArray(), this will take x, Define an array retwhile x is non-zero, do −insert x mod 10 at the end of retx := x / 10reverse the array retreturn retDefine a function combine(), this will take an array v, ret := 0for each ... Read More

Program to Find Out the Best Interval to Remove in C++

Arnab Chakraborty
Updated on 15-Dec-2020 12:54:00

137 Views

Suppose we have a list of intervals (inclusive) that are potentially overlapping. Now consider there is an operation where we delete one interval, then merge the remaining intervals and then count the number of intervals left over. We have to find the maximum number of leftover intervals possible after removal.So, if the input is like intervals = [ [5, 8], [6, 7], [7, 10], [9, 11]], then the output will be 2. This is because −If we delete the interval [5, 8] we get [6, 11] as the merge.If we delete the interval [6, 7] we get [5, 11] as ... Read More

Program to find longest prefix that is also a suffix in C++

Arnab Chakraborty
Updated on 15-Dec-2020 12:33:08

402 Views

Suppose we have a string s, we have to find the longest prefix of s, which is also a suffix (excluding itself). If there is no such prefix, then simply return blank string.So, if the input is like "madam", then the output will be "m", it has 4 prefixes excluding itself. These are "m", "ma", "mad", "mada" and 4 suffixes like "m", "am", "dam", "adam". The largest prefix which is also suffix is given by "m".To solve this, we will follow these steps −Define a function lps(), this will take s, n := size of sDefine an array ret of ... Read More

Program to find maximum sum of two sets where sums are equal in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:23:56

82 Views

Suppose we have a list of numbers called nums, now find two sets as their sums are same and maximum, then find sum value.So, if the input is like nums = [2, 5, 4, 6], then the output will be 6, as the sets are [2, 4] and [6].To solve this, we will follow these steps −sum := 0for each number i in nums, dosum := sum + in := size of numsDefine one 2D array dp of size (n + 1) x (2 * sum + 5) and fill with -1dp[0, sum] := 0for initialize i := 1, when ... Read More

Program to check whether we can partition a list with k-partitions of equal sum in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:12:50

94 Views

Suppose we have a list of numbers called nums and another value k, we have to check whether it is possible to partition nums into k different subsets where the sum of each subset are same.So, if the input is like nums = [4, 2, 6, 5, 1, 6, 3] k = 3, then the output will be True, as we can partition them like: [6, 3], [6, 2, 1], and [4, 5].To solve this, we will follow these steps −Define a function check(), this will take an array v, for initialize i := 1, when i < size of ... Read More

Program to find sum of k non-overlapping sublists whose sum is maximum in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:07:09

140 Views

Suppose we have a list of numbers called nums, and another value k, we have to find the k non-overlapping, non-empty sublists such that the sum of their sums is maximum. We can consider k is less than or equal to the size of nums.So, if the input is like nums = [11, -1, 2, 1, 6, -24, 11, -9, 6] k = 3, then the output will be 36, as we can select the sublists [11, -1, 2, 1, 6], [11], and [6] to get sums of [19, 11, 6] = 36.To solve this, we will follow these steps ... Read More

Program to justify a set of words by converting them into same length lines in C++

Arnab Chakraborty
Updated on 12-Dec-2020 10:00:33

67 Views

Suppose we have an list of words and a width k, we have to arrange the text such that each line has exactly k number of characters and the text is fully justified. Here we shall pack our words as many words as we can insert in each line. And we shall pad extra spaces ' ' when necessary so that each line has exactly k characters.Here extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, empty slots on the left will be assigned ... Read More

Program to find minimum sum of difficulties to complete jobs over k days in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:55:42

111 Views

Suppose we have a list of numbers called jobs and another value k. Now we want to finish all jobs in k different days. The jobs must be performed in the given order and in each day we have to complete one task. The difficulty of job i is stored at jobs[i] and the difficulty of completing a list of jobs on a day will be the maximum difficulty job performed on that day. So we have to find the minimum sum of the difficulties to perform the jobs over k different days.So, if the input is like jobs = ... Read More

Program to find all possible IP address after restoration in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:49:18

259 Views

Suppose we have a string with only digits, we have to restore it by forming all possible valid IP address combinations. We know that a valid IP address consists of exactly four integers (each integer is in range 0 to 255) separated by single period symbol.So, if the input is like ip = "25525511136", then the output will be ["254.25.40.123", "254.254.0.123"]To solve this, we will follow these steps −Define a function convertToNum(), this will take s, start, end, num := 0for initialize i := start, when i 255, then −return 10000return numDefine a function addDots(), this will take positions, ... Read More

Program to find minimum number of pins required to hang all banners in C++

Arnab Chakraborty
Updated on 12-Dec-2020 09:40:11

248 Views

Suppose we have a list of intervals of the form [start, end] this is representing the starts and end points of banners we want to hang. At least one pin is required to hang a banner, and one pin can hang more than once banners. We have to find the smallest number of pins required to hang all the banners.So, if the input is like intervals = [[2, 5], [5, 6], [8, 10], [10, 13]], then the output will be 2, as we can put two pins at position 5 and 10 to hang all of the banners.To solve this, ... Read More

Advertisements