Found 7197 Articles for C++

Program to find minimum number of swaps needed to arrange all pair of socks together in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:46:28

186 Views

Suppose we have a list of numbers called row and this is representing socks lying in a row. They are not sorted, but we want to rearrange them so that each pair of socks are side by side like (0, 1), (2, 3), (4, 5), and so on. We have to find the minimum number of swaps required to rearrange them.So, if the input is like row = [0, 5, 6, 2, 1, 3, 7, 4], then the output will be 2, as the row orders are[0, 5, 6, 2, 1, 3, 7, 4][0, 1, 6, 2, 5, 3, 7, ... Read More

Program to find sum of medians of all odd length sublists in C++

Arnab Chakraborty
Updated on 25-Dec-2020 05:44:53

275 Views

Suppose we have a list of numbers called nums, we have to find the sum of the medians of every odd−length sublist of the given list.So, if the input is like nums = [2, 4, 6, 3], then the output will be 23, as the odd−length sublists are − [2], [4], [6], [3], [2, 4, 6], [4, 6, 3], so the sum of the medians is 2 + 4 + 6 + 3 + 4 + 4 = 23To solve this, we will follow these steps −ret := 0for initialize i := 0, when i < size of nums, update ... Read More

Program to Find the Shortest Distance Between Two Points in C++

Arnab Chakraborty
Updated on 23-Dec-2020 07:04:51

1K+ Views

Suppose we have a list of coordinates where each element is of the form [x, y], representing Euclidean coordinates. We have to find the smallest squared distance (x1 - x2) 2 + (y1 - y2) 2 between any two provided coordinates.So, if the input is like coordinates = {{1, 2}, {1, 4}, {3, 5}}, then the output will be 4.To solve this, we will follow these steps −Define one map ytorightmostxsort the array coordinatesret := infinityfor each p in cordinates −it = return the value where (p[1] - sqrt(ret)) is in ytorightmostx or the smallest value greater than it from ... Read More

Program to Find Out the Cost after Finding k Unique Subsequences From a Given String in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:51:32

200 Views

Suppose, we have a string s and another value k. We have to select some subsequences of s, so that we can get k unique subsequences. Here, the cost of selecting a subsequence equals the length of (s) - length of (subsequence). So, we have to find the lowest total cost possible after selecting k unique subsequences. If we are unable to find out this set, we will return -1. We will consider the empty string as a valid subsequence.So, if the input is like s = "pqrs", k = 4, then the output will be 3.To solve this, we ... Read More

Program to Find Out the Amount of Rain to be Caught between the Valleys in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:41:37

103 Views

Suppose we have a 2D matrix, where the elements represent the height of a terrain. Let's imagine a situation where it will rain and all the spaces in the valleys get filled up.We have to find out the amount of rain that will be caught between the valleys.So, if the input is like666864586666then the output will be 3 as we can hold 3 units of water in between 4 and 5 squares.To solve this, we will follow these steps −Define a structure Data, that contains x and y coordinate and height hDefine a priority queue pq, it stores data items ... Read More

Program to Find Out the Minimum Parsing Tree in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:35:50

408 Views

Suppose we have a list of unique and sorted numbers that represent breakpoints in a string. We want to create a tree out of these rules −There are nodes that have a value (a, b) where a and b are breakpoints. This means the node spans from indices [a, b] in the string.The root node spans over every breakpoint. (the whole string).The spans of a node's left and right child are ordered, contiguous, and contains the parent node's span.Leaf nodes' index of 'a' in breakpoints is 1 before the index of 'b' in breakpoints.The cost of a tree is determined ... Read More

Program to find k where given matrix has k by k square of same value in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:21:22

175 Views

Suppose we have a 2d matrix, we have to find the largest k × k submatrix where all of its elements are containing the same value, then find the value of k.So, if the input is like1183155525554555then the output will be 3, as there is a 3 × 3 square matrix of value 5.To solve this, we will follow these steps −n := row count of matrixm := column count of matrixDefine one 2D array dp of size (n x m) and fill with 1ret := 1for initialize i := n - 1, when i >= 0, update (decrease i ... Read More

Program to count number of palindromes after minimum number of split of the string in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:18:41

147 Views

Suppose we have a lowercase string s, we have to split it into as few strings as possible such that each string is a palindrome and then find the number of strings.So, if the input is like s = "levelracecar", then the output will be 2, as there are two palindromes "level" and "racecar".To solve this, we will follow these steps −n := size of ADefine an array result of size (n + 1)result[n] := -1for initialize i := n - 1, when i >= 0, update (decrease i by 1), do −result[i] := n - i - 1for initialize ... Read More

Program to find minimum largest sum of k sublists in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:17:00

182 Views

Suppose we have a list of numbers called nums and another value k. We can split the list into k non-empty sublists. We have to find the minimum largest sum of the k sublists.So, if the input is like nums = [2, 4, 3, 5, 12] k = 2, then the output will be 14, as we can split the list like: [2, 4, 3, 5] and [12].To solve this, we will follow these steps −Define a function ok(), this will take array v, k, x,cnt := 0, sum := 0for each element i in v −if sum + i > x, then −sum := i(increase cnt by 1)Otherwisesum := sum + ireturn true when cnt

Program to find smallest difference between picked elements from different lists in C++

Arnab Chakraborty
Updated on 23-Dec-2020 06:08:58

172 Views

Suppose we have a list of lists, we have to find the smallest difference that can be formed by picking one value from each of the lists and taking the difference between the maximum and the minimum number of the picked element.So, if the input is like lists = [ [30, 50, 90], [85], [35, 70]], then the output will be 20, as we can take 90, 85, 70 and 90 - 70 = 20To solve this, we will follow these steps −maxVal := -infret := infdefine a priority queue pqn := size of listsfor initialize i := 0, when ... Read More

Advertisements