Found 7197 Articles for C++

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

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

117 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

142 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

902 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

447 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

723 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

207 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

499 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

Print a matrix in Reverse Wave Form in C++

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

464 Views

In this problem, we are given a matrix. Our task is to print the matrix in reverse waveform in a single line.This example will make the problem clear, Input:    1 4 6 11    2 5 8 54    7 9 3 43    1 7 4 34 Output: 11 54 43 34 4 3 8 6 4 5 9 7 1 7 2 1To solve this problem, we have to print the reverse waveform of our matrix and for this, we will print the elements of the last column in the downward direction and then second-last column’s elements ... Read More

Print a number containing K digits with digital root D in C++

sudhir sharma
Updated on 27-Jan-2020 06:26:20

122 Views

In this problem, we are given two numbers K and D. Our task is to print a number of k digits and which has digital root equal to D.Digital Root is a single-digit value which is the result of the recursive addition of the digits of the number till the one a single-digit number is reached. Also known as a digital sum.Let’s take an example to understand the problem, Input: D = 5 , K = 6 Output: 60000To solve this problem, we will be using trials of zero’s after the number D. Our number will be {D000..(k-1 times)}. This ... Read More

Print a number strictly less than a given number such that all its digits are distinct in C++

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

300 Views

In this problem, we are given a number n. Our task is to print the largest number less than n such that all its digits are distinct.Let’s take an example to understand the problemInput: n = 2332 Output: 2319To solve this problem, we reverse the count of the numbers i.e. from n to 0. And check for number with distinct digits, if the current count values satisfy the condition print it and end the loop. Otherwise continue to loop. The max number of times the loop will run is always less than n.ExampleProgram to implement our solutions,  Live Demo#include ... Read More

Advertisements