Found 7347 Articles for C++

Gas Station in C++

Arnab Chakraborty
Updated on 28-Apr-2020 06:33:33

441 Views

Suppose there is a circle, and there are n gas stations on the circle. We have two sets of data like −The amount of gas that every gas stations hasDistance from one gas stations to another.Calculate the first point, from where a car will be able to complete the circle. Assume for 1 unit of gas, the car can go 1 unit of distance. Suppose there are four gas stations, and the amount of gas, and distance from the next gas stations is as like [(4, 6), (6, 5), (7, 3), (4, 5)], the first point from where car can ... Read More

Palindrome Partitioning in C++

Arnab Chakraborty
Updated on 28-Apr-2020 06:28:15

314 Views

Suppose we have one input string, a partitioning of that string is palindrome partitioning, when every substring of the partition is a palindrome. In this section, we have to find the minimum cuts are needed to palindrome partitioning the given string. So if the string is like “ababbbabbababa” Minimum cut to partition as palindrome. Here 3 cuts are needed. The palindromes are: a | babbbab | b | ababaTo solve this, we will follow these steps −n := length of strdefine cut matrix and pal matrix each of order n x nfor i := 0 to n, dopal[i, i] := ... Read More

Evaluate Reverse Polish Notation in C++

Arnab Chakraborty
Updated on 31-Jan-2020 06:36:02

241 Views

Suppose we have a triangle. We have to find the minimum path sum from top to the bottom. In each step we can move to adjacent numbers on the row below.For example, if the following triangle is like[    [2],    [3, 4],    [6, 5, 7],    [4, 1, 8, 3] ]The minimum path sum from top to bottom is 11 (2 + 3 + 5 + 1 = 11).Let us see the stepsCreate one table to use in Dynamic programming approach.n := size of trianglefor i := n – 2 down to 0for j := 0 to idp[j] ... Read More

Binary Tree Level Order Traversal in C++

Arnab Chakraborty
Updated on 28-Apr-2020 06:19:26

2K+ Views

Suppose we have a binary tree. We have to traverse this tree using the level order traversal scheme. So if the tree is likeThe traversal sequence will be like − [10, 5, 16, 8, 15, 20, 23]To solve this, we will follow these steps −define queue que to store nodesinsert root into the que.while que is not empty, doitem := item present at front position of queueprint the value of itemif left of the item is not null, then insert left of item into queif right of the item is not null, then insert right of item into quedelete front ... Read More

Gray Code in C++

Arnab Chakraborty
Updated on 28-Apr-2020 06:01:01

1K+ Views

As we know that the gray code is a binary numeral system where two successive values differ in only one bit. Suppose we have a non-negative integer n representing the total number of bits in the code. We have to print the sequence of gray code. A gray code sequence must begin with 0. So if the input is 2, then the result will be [0, 1, 3, 2], this is because gray of 0 is 00, gray of 1 is 01, gray of 2 is 11, and gray of 3 is 10.To solve this, we will follow these steps ... Read More

Partition List in C++

Arnab Chakraborty
Updated on 28-Apr-2020 05:50:16

440 Views

Suppose we have a linked list and a value x. We have to make partitions. The partition it such that all nodes less than x comes before the nodes that are greater than or equal to x. We should preserve the original relative order of the nodes in each of these two partitions. So if the list is like [1, 4, 3, 2, 5, 2] and x = 3, then the output will be [1, 2, 2, 4, 3, 5]To solve this, we will follow these steps −Make dummy nodes d1 and d2, and initialize them with -1, create two ... Read More

Spiral Matrix in C++

Arnab Chakraborty
Updated on 27-Apr-2020 14:09:34

4K+ Views

Suppose we have a matrix and we have to print the matrix elements in a spiral way. At first starting from the first row, print the whole content and then follow the last column to print, then the last row, and so on, thus it prints the elements in a spiral fashion. So if the matrix is like −123456789101112131415161718Then the output will be like [1 2 3 4 5 6 12 18 17 16 15 14 13 7 8 9 10 11 15 16]To solve this, we will follow these steps −currRow := 0 and currCol := 0while currRow and ... Read More

Permutations II in C++

Arnab Chakraborty
Updated on 27-Apr-2020 13:26:05

267 Views

Suppose we have a collection of distinct integers; we have to find all possible permutations. Now if the array stores the duplicate elements, then ignore that state which is looking similar. So if the array is like [1, 1, 3], then the result will be [[1, 1, 3], [1, 3, 1], [3, 1, 1]]To solve this, we will follow these steps −We will use the recursive approach, this will make the list, index. Index is initially 0if index = size of the list then insert list into res array, and returnfor i in range index to length of given list ... Read More

Maximum sum subarray removing at most one element in C++

Narendra Kumar
Updated on 03-Jun-2020 07:49:56

105 Views

In this problem, we are given an array. Our task is to create a program that will find maximum sum subarray removing at most one element in c++.Basically, we need to find one element which when removed provides the maximum sum for the elements remaining in the array.Let’s take an example to understand the problem, Input − array = {5, 1, 9, 2, -1, 7}Output − 24Explanation − we have removed -1 from the array and the sum became the maximum of all possible outcomes.One solution to this problem will be finding the minimum element of the array and then ... Read More

Maximum sum subarray having sum less than or equal to given sums in C++

Narendra Kumar
Updated on 03-Jun-2020 07:52:32

562 Views

In this problem, we are given an array and a sum. Our task is to create a program that will find the maximum sum subarray having a sum less than or equal to given sums in c++.We have to find the subarray of any length less than or equal to n whose sum is less than or equal to the given sum.Let’s take an example to understand the problem, Input − array = {3, 5, 1, 8, 2, 9}, sum = 25Output − 25Explanation − The subarrays with sum less than or equal to 25 are {5, 1, 8, 2, ... Read More

Advertisements