Found 7347 Articles for C++

Play with Chips in C++

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

246 Views

Suppose there are some chips, the i-th chip is at present at position chips[i]. We can perform any of the two following types of operations any many numbers of times as we want (possibly zero) on any chip −Move the i-th chip by 2 units to the left side or to the right side with a cost of 0.Move the i-th chip by 1 unit to the left side or to the right side with a cost of 1.Initially, there can be two or more chips. We have to return the minimum cost needed to move all the chips to ... Read More

Merge Two Binary Trees in C++

Arnab Chakraborty
Updated on 27-Apr-2020 08:59:01

553 Views

Suppose we have two binary trees and consider that when we put one of them to cover the other, some nodes of the two trees are overlapped while the others are overlapping. We have to merge them into a new binary tree. The merge rule is like that if two nodes are overlapping, then sum node values up as the new value of the merged node. Otherwise, the non-empty node will be used as the node of the new tree.So if the trees are −Then the output will be −To solve this, we will follow these steps −The method is ... Read More

Path Sum III in C++

Arnab Chakraborty
Updated on 04-May-2020 06:33:43

181 Views

Suppose we have given a binary tree in which each node holds an integer key. We have to find the paths that sum to a given value. The path should start from root to leaf. We have to find the path where the sum is same.If the tree is like [5, 4, 8, 11, null, 13, 4, 7, 2, null, null, 5, 1], and sum is 22, then it will be −The paths are [[5, 4, 11, 2], [5, 8, 4, 5]].To solve this, we will follow these steps −Use the dfs function to solve this problem, the dfs is ... Read More

Print a case where the given sorting algorithm fails in C++

sudhir sharma
Updated on 27-Jan-2020 06:50:32

52 Views

In this problem, we are given a sorting algorithm and a number n. Our task is to print an array of n elements that could not be sorted by the algorithm. i.e the algorithm will fail.Algorithmloop i from 1 to n-1    loop j from i to n-1    if a[j]>a[i+1]       swap(a[i], a[j+1])Let’s look into this sorting algorithm, it is using two nested loops. The outer one will start from 1 to n-1 and inner one from i to n-1, and will check the value of inner loop element and outer loop elements at each iteration and ... Read More

Print a closest string that does not contain adjacent duplicates in C++

sudhir sharma
Updated on 27-Jan-2020 06:47:12

105 Views

In this problem, we are given a string. Our task is to print a string that is closest to the current string and does not contain any adjacent duplicate characters.Let’s take an example to understand the problemInput: string = “good” Output: goadIn this example, we found that the elements at index 1 and 2 are the same, so we change the elements at index 2.To solve this problem, we will traverse the string and check if any two adjacent elements have are same. If yes, then change the second element (if i and i+1 elements are the same, change i+1 ... Read More

Print a given matrix in counter-clockwise spiral form in C++

sudhir sharma
Updated on 27-Jan-2020 06:42:59

659 Views

In this problem, we are given a 2-dimensional matrix. And our task is to print the elements of the matrix in a counter-clockwise spiral from.Counterclockwise Spiral Form − It is a spiral traversal which starts from top-left and the goes in the counter-clockwise direction to first bottom-right-up-left.The counter-clockwise traversal will be 1 5 9 13 14 15 16 12 8 4 3 2 6 10 11 7.Let’s take an example to understand the problemInput:    2 4 6    1 7 9    5 0 3 Output: 2 1 5 0 3 9 7To solve this problem, we will be ... Read More

Print a given matrix in reverse spiral form in C++

sudhir sharma
Updated on 27-Jan-2020 06:38:54

350 Views

In this problem, we are given a 2-dimensional matrix. Our task is to print all the elements of the matrix in reverse spiral form.Let’s take an example to understand the problemInput:    12 23 54 67    76 90 01 51    43 18 49 5    31 91 75 9 Output: 18 49 1 90 76 43 31 91 75 9 5 51 67 54 23 12We will start from the center of the matrix and print elements in reverse spiral direction taking four loops for printing elements in reverse direction.ExampleProgram to show the implementation of our solution Live Demo#include ... Read More

Print a given matrix in zigzag form in C++

sudhir sharma
Updated on 27-Jan-2020 06:36:07

545 Views

In this problem, we are given a 2-dimensional matrix. Our task is to print the zigzag form of the matrix.Let’s take an example to understand the problemInput:    12 99 43    10 82 50    15 75 5 Output: 12 99 43 50 82 10 15 75 5To solve this problem, we will print the elements of the array in both directions (LtoR and RtoL). And change the direction using the flag variable.Example Live Demo#include using namespace std; void printZigZagPattern(int row, int col, int a[][5]) {    int evenRow = 0;    int oddRow = 1;    while (evenRow

Print a matrix in a spiral form starting from a point in C++

sudhir sharma
Updated on 27-Jan-2020 06:34:01

147 Views

In this problem, we are a 2d matrix and a point P(c, r). Our task is to print all elements of the matrix in a spiral form (anti-clockwise) that starts from a given point P.Let’s take an example to understand our problem, Input: matrix[][] = {{3, 5, 7} } Output:To solve this problem, we use 4 loops for printing elements. Each loop prints elements of its own direction. We will start our printing from point p and go on printing the spiral form.ExampleProgram to show the implementation of our solution Live Demo#include using namespace std; const int MAX = 100; ... Read More

Print a matrix in alternate manner (left to right then right to left) in C++

sudhir sharma
Updated on 27-Jan-2020 06:31:54

353 Views

In this problem, we are given a 2D array. Our task is to print all the elements of the array starting from the first row, from left to right, then right to left in the next row and again from left to right and so on.Let’s take an example to understand the problem.Input: array = {    {2, 5}    {4, 9} } Output: 2 5 9 4To solve this problem, we will print elements in the given direction (LtoR and RtoL) of a row. And a flag element to show the direction of printing will switch after every iteration.This ... Read More

Advertisements