C++ Articles

Page 253 of 597

Print all the combinations of a string in lexicographical order in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 484 Views

In this problem, we are given string str, and we have to print all the combinations of the characters in a lexicographical order.Let’s take an example to understand the problem better −Input: str = ‘XYZ’ Output : X XY XYZ XZ XZY Y YX YXZ YZ YZX Z ZX ZXY ZY ZYXTo solve this problem, we will print all the combinations of characters in the string. For this, we need a map data structure to store the characters of the string. For the implementation, we will need to use backtracking to keep track of all combinations.Example#include using namespace std; ...

Read More

Print all subarrays with 0 sum in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 345 Views

In this problem, we are given an array of integer values and we have to print all those subarrays from this array that have the sum equal to 0.Let’s take an example to understand the topic better, Input: array = [-5, 0, 2, 3, -3, 4, -1] Output: Subarray with sum 0 is from 1 to 4. Subarray with sum 0 is from 5 to 7 Subarray with sum 0 is from 0 to 7To solve this problem, we will check all subarrays possible. And check if the sum of these subarrays is equal to 0 and print them. This ...

Read More

Print all subsequences of a string using Iterative Method in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 467 Views

In this problem, we are given a string and we have to find the substring from the given string. The substring to be found should start with a vowel and end with constant character.A string is an array of characters.The substring that is to be generated in this problem can be generated by deleting some characters of the string. And without changing the order of the string.Input: ‘abc’ Output: ab, ac, abcTo solve this problem, we will iterate the string and fix vowels and check for the next sequence. Let’s see an algorithm to find a solution −AlgorithmStep 1: Iterate ...

Read More

Print all Subsequences of String which Start with Vowel and End with Consonant in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 604 Views

In this problem, we are given a string and we have to find the substring from the given string. The substring to be found should start with a vowel and end with constant character.A string is an array of characters.The substring that is to be generated in this problem can be generated by deleting some characters of the string. And without changing the order of the string.Input: ‘abc’ Output: ab, ac, abcTo solve this problem, we will iterate the string and fix vowels and check for the next sequence. Let’s see an algorithm to find a solution −AlgorithmStep 1: Iterate ...

Read More

Print all paths from a given source to a destination in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 443 Views

In this problem we are given a directed graph and we have to print all paths from the source to the destination of the graph.Directed graph is a graph in with edges that are directed from vertex a to b.Let’s take an example to understand the problem Source = K destination = POutput:K -> T -> Y -> A -> P K -> T -> Y -> P K -> A -> PHere, we have found paths from K to P. We have traversed paths and printed all paths from K that direct us to P.To solve this problem, we will ...

Read More

Fill missing entries of a magic square in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 275 Views

Suppose we have one 3x3 matrix, whose diagonal elements are empty at first. We have to fill the diagonal such that the sum of row, column and the diagonal will be same. Suppose a matrix is like −036505470After filling, it will be −636555474Suppose the diagonal elements are x, y, z. The values will be −x = (M[2, 3] + M[3, 2])/ 2z = (M[1, 2] + M[2, 1])/ 2y = (x + z)/2Example#include using namespace std; void displayMatrix(int matrix[3][3]) {    for (int i = 0; i < 3; i++) {       for (int j = 0; j < 3; j++)          cout

Read More

Final cell position in the matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 230 Views

Suppose we have a set of commands as a string, the string will have four different letters for four directions. U for up, D for down, L for left and R for right. We also have initial cell position (x, y). Find the final cell position of the object in the matrix after following the given commands. We will assume that the final cell position is present in the matrix. Suppose the command string is like “DDLRULL”, initial position is (3, 4). The final position is (1, 5).The approach is simple, count the number of up, down, left and right ...

Read More

Find a distinct pair (x, y) in given range such that x divides y in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 173 Views

Here we will see one interesting problem, we will find a pair (x, y), where x and y are in range so l

Read More

Find a Fixed Point (Value equal to index) in a given array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 297 Views

Here we will see how to find fixed point in a given array. In array one element will be denoted as fixed point if the value is same as its index. This program will return the value if any, otherwise return -1. The array can hold negative numbers also. And the data elements are sorted.Here we will use binary search approach to solve this problem in O(log n) time. At first we will check whether the middle element is fixed point or not, if yes, then return it, if not, then there will be two situations, if the index of ...

Read More

Find a Fixed Point in an array with duplicates allowed in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 219 Views

Here we will see how to find fixed point in a given array. In array one element will be denoted as fixed point if the value is same as its index. This program will return the value if any, otherwise return -1. The array can hold negative numbers also. And the data elements are sorted. Here duplicate elements are allowed in the array.Here we will use binary search approach to solve this problem in O(log n) time. But we need some modification, if the normal binary search is used, then it may fail for duplicate elements. To check left, we ...

Read More
Showing 2521–2530 of 5,962 articles
« Prev 1 251 252 253 254 255 597 Next »
Advertisements