Found 7347 Articles for C++

Minimum Sum Path in a Triangle in C++

Narendra Kumar
Updated on 20-Dec-2019 09:58:12

121 Views

Problem statementGiven a triangular structure of numbers, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.ExampleIf input is −   5   7 3  8 1 2 9 6 4 5Then minimum sum is 13 as follows −5 + 3 + 1 + 4AlgorithmUse memorization technique of dynamic programmingCreate 1-D array for memorization namely memorizationFor each K row use below formula −memorization[i] = min( memorization[i], memorization[i+1]) + A[k][i];Example Live Demo#include using namespace std; int getMinSum(vector &arr) {    int memorization[arr.size()];    int n = arr.size() - 1;    for ... Read More

Minimum sum path between two leaves of a binary trees in C++

Narendra Kumar
Updated on 20-Dec-2019 09:49:26

155 Views

Problem statementGiven a binary tree in which each node element contains a number. The task is to find the minimum possible sum from one leaf node to another.ExampleIn above tree minimum sub path is -6 as follows: (-4) + 3 + 2 + (-8) + 1AlgorithmThe idea is to maintain two values in recursive calls −Minimum root to leaf path sum for the subtree rooted under current nodeThe minimum path sum between leavesFor every visited node X, we have to find the minimum root to leaf sum in left and right sub trees of X. Then add the two values ... Read More

Minimum sum of two numbers formed from digits of an array in C++

Narendra Kumar
Updated on 20-Dec-2019 09:33:40

377 Views

DescriptionGiven an array of digits which contains values from 0 to 9. The task is to find the minimum possible sum of two numbers formed from digits of the array. Please note that we have to use all digits of given arrayExampleIf input array is {7, 5, 1, 3, 2, 4} then minimum sum is 382 as, we can create two number 135 and 247.AlgorithmSort the array in ascending orderCreate two number by picking a digit from sorted array alternatively i.e. from even and odd indexExample Live Demo#include using namespace std; int getMinSum(int *arr, int n) {    sort(arr, arr ... Read More

Find the second last node of a linked list in single traversal in C++

Arnab Chakraborty
Updated on 19-Dec-2019 13:02:06

527 Views

Now we will see how to get the second last element in the linked list. Suppose there are few elements like [10, 52, 41, 32, 69, 58, 41], second last element is 58.To solve this problem, we will use two pointers one will point to current node, and another will point to previous node of the current position, then we will move until the next of current is null, then simply return the previous node.Example Live Demo#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) ... Read More

Find the Second Largest Element in a Linked List in C++

Arnab Chakraborty
Updated on 19-Dec-2019 13:00:00

335 Views

Here we will see the second largest element in the linked list. Suppose there are n different nodes with numerical values. So if the list is like [12, 35, 1, 10, 34, 1], then second largest element will be 34.This process is similar to the finding of second largest element in an array, we will traverse through the list and find second largest element by comparing.Example#include using namespace std; class Node {    public:       int data;       Node *next; }; void prepend(Node** start, int new_data) {    Node* new_node = new Node;    new_node->data = ... Read More

Find the next identical calendar year in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:54:41

151 Views

Suppose we have an year Y. Find next identical calendar year to Y. So the calendar of 2017 is identical with 2023.A year X is identical to given previous year Y if it matches these two conditions.x starts with the same day as year, If y is leap year, then x also, if y is normal year, then x also normal year.The idea is to check all years one by one from next year. We will keep track of number of days moved ahead. If there are total 7 moved days, then current year begins with same day. We also ... Read More

Find the fractional (or n/k – th) node in linked list in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:51:30

171 Views

Suppose we have a singly linked list and the number k. We have to write a function to find the (n/k)th element, where n is the number of elements in the list. For decimals, we will choose the ceiling values. So if the list is like 1, 2, 3, 4, 5, 6, and k = 2, then output will be 3, as n = 6 and k = 2, then we will print n/k th node so 6/2 th node = 3rd node that is 3.To solve this we have to follow some steps like below −Take two pointers called ... Read More

Find the final X and Y when they are Altering under given condition in C++ Programming

Arnab Chakraborty
Updated on 19-Dec-2019 12:40:59

53 Views

Consider we have the initial values of two positive integers X and Y. Find the final value of X and Y, such that there will be some alteration as mentioned below −step1 − If X = 0 and Y = 0 then terminate the process, otherwise go to step2step2 − If X >= 2Y, then set X = X – 2Y, and go to step1, otherwise go to step3step3 − If Y >= 2X, then set Y = Y – 2X, and go to step1, otherwise end the process.The number X and Y will be in range [0 and 1018] ... Read More

Find the count of substrings in alphabetic order in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:37:36

94 Views

Suppose we have a string of length n. It contains only uppercase letters. We have to find the number of substrings whose character is occurring in alphabetical order. Minimum size of the substring will be 2. So if the string is like: “REFJHLMNBV”, and substring count is 2, they are “EF” and “MN”.So to solve this, we will follow these steps −Check whether str[i] + 1 is same as the str[i+1], if so, then increase the result by 1, and iterate the string till next character which is out of alphabetic order, otherwise continue.Example Live Demo#include using namespace std; int countSubstr(string ... Read More

Find the count of Strictly decreasing Subarrays in C++

Arnab Chakraborty
Updated on 19-Dec-2019 12:34:05

195 Views

Suppose we have an array A. And we have to find the total number of strictly decreasing subarrays of length > 1. So if A = [100, 3, 1, 15]. So decreasing sequences are [100, 3], [100, 3, 1], [15] So output will be 3. as three subarrays are found.The idea is find subarray of len l and adds l(l – 1)/2 to result.Example Live Demo#include using namespace std; int countSubarrays(int array[], int n) {    int count = 0;    int l = 1;    for (int i = 0; i < n - 1; ++i) {       ... Read More

Advertisements