Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
C++ Articles - Page 460 of 719
374 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
3K+ 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
2K+ 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
635 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
5K+ 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
391 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
208 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
740 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
650 Views
Problem statementGiven two sorted arrays such the arrays may have some common elements. Find the sum of the maximum sum path to reach from beginning of any array to end of any of the two arrays. We can switch from one array to another array only at common elements. Note that the common elements do not have to be at same indexes.Expected time complexity is O(m+n) where m is the number of elements in arr1[] and n is the number of elements in arrs2[]ExampleIf given input is then output is 35 arr1[] = {2, 3, 7, 10, 12} ar2[] = ... Read More
295 Views
Problem statementConsider a n*n matrix. Suppose each cell in the matrix has a value assigned. We can go from each cell in row i to a diagonally higher cell in row i+1 only [i.e from cell(i, j) to cell(i+1, j-1) and cell(i+1, j+1) only]. Find the path from the top row to the bottom row following the aforementioned condition such that the maximum sum is obtainedExampleIf given input is: { {5, 6, 1, 17}, {-2, 10, 8, -1}, { 3, -7, -9, 4}, {12, -4, 2, 2} }the maximum sum is (17 + 8 + 4 ... Read More