Found 7347 Articles for C++

Find duplicate rows in a binary matrix in C++

Arnab Chakraborty
Updated on 03-Jan-2020 09:58:19

121 Views

Suppose we a binary matrix. Here we will see how to find the duplicate rows in that matrix. Suppose the matrix is like −110101001001101100110101001001001001There are duplicate rows at position 3, 4, 5.To solve this, we will use the Trie. The Trie is an efficient data structure used for strong and retrieval of data where character set is small. The search complexity is optimal as the key length. So at first we will insert the binary trie. If the newly added row is already present, then that is duplicate.Example Live Demo#include using namespace std; const int MAX = 100; class Trie { ... Read More

Print ancestors of a given binary tree node without recursion in C++

sudhir sharma
Updated on 03-Jan-2020 09:57:21

116 Views

In this problem, we are given a binary tree and we have to print its ancestor of a node in a binary tree.Binary Tree is a special tree whose every node has at max two child nodes. So, every node is either a leaf node or has one or two child nodes.Example, The ancestor of a node in a binary tree is a node that is at the upper level of the given node.Let’s take an example of ancestor node −Ancestors of a node with value 3 in this binary tree are 8, For solving this problem, we will traverse ... Read More

Find alphabet in a Matrix which has maximum number of stars around it in C++

Arnab Chakraborty
Updated on 03-Jan-2020 09:54:16

135 Views

Suppose we have a matrix M. This is filled with stars and letters. We have to find which letter has maximum number of stars around it. So if the matrix is like below −Here A and C has 7 stars around it. this is maximum. As A is lexicographically smaller, so it will be the output.The approach is simple, we will count the characters, then when one character has found, then count the stars around it. Also store the value inside a map. From the map where the size is maximum that will be printed.Example Live Demo#include #include #define MAX ... Read More

Print Ancestors of a given node in Binary Tree in C++

sudhir sharma
Updated on 03-Jan-2020 09:52:43

2K+ Views

In this problem, we are given a binary tree and we have to print its ancestor of a node in a binary tree.Binary Tree is a special tree whose every node has at max two child nodes. So, every node is either a leaf node or has one or two child nodes.Example, The ancestor of a node in a binary tree is a node that is at the upper level of the given node.Let’s take an example of ancestor node −Ancestors of a node with value 3 in this binary tree are 8, For solving this problem, we will traverse ... Read More

Find a triplet such that sum of two equals to third element in C++

Arnab Chakraborty
Updated on 03-Jan-2020 09:50:15

384 Views

Suppose there is an array of n numbers. We have to find three numbers, such that sum of two elements is same as the third one. So if the array is like [5, 32, 1, 7, 10, 50, 19, 21, 2], the output will be 21, 2, 19. If no such element has found, display that message.To solve this, we will follow some steps as follows −Sort the given arrayThen start fixing the greatest element from the last element and traverse the array to find other two numbers which sum up to the third element.Take two pointers j and k, ... Read More

Print array elements in alternatively increasing and decreasing order in C++

sudhir sharma
Updated on 03-Jan-2020 09:48:44

366 Views

In this problem, we are given an array of number and we have to print the elements of the array in alternatively increasing and decreasing order while printing. The alternative order will be in such a way that 1st two elements are in increasing order and then the next three elements are in decreasing order again next four are in ascending order.Let’s take an example to understand the problem better, Input : {1, 4, 0, 2, 7, 9, 3} Output : 0 1 9 7 4 2 3Explanation − the array in increasing order of elements is 0 1 ... Read More

Find a specific pair in Matrix in C++

Arnab Chakraborty
Updated on 03-Jan-2020 09:47:16

240 Views

Suppose there is an n x n matrix mat of integers. we have to find maximum value of mat(c, d) - mat(a, b) over all choices of indexes. Here we have to keep in mind that c > a and d > b. So if the matrix is like −12-1-4-20-8-342138613-4-117-60-410-51The output will be 18. This is because mat[4, 2] - mat[1, 0] = 18 has maximum difference.To solve this we will preprocess the matrix such that index(i, j) stores max of elements in matrix from (i, j) to (n - 1, n - 1) and in the process keeps on ... Read More

Print array elements that are divisible by at-least one other in C++

sudhir sharma
Updated on 03-Jan-2020 09:45:45

285 Views

In this problem, we are given an array of integers and we have to print only those numbers that are divisible by at least one other element of the array.Let’s take an example to understand the concept better, Input : 3 12 16 21 Output : 12 21Explanation − 3 is the smallest so it can be divisible by any other number are 12 which is divisible by 3, 16 not divisible by 3 and then 21 which is divisible by 3. So, we will neglect 3 and 16.One easy way is to check if all elements are divisible ... Read More

Find a palindromic string B such that given String A is a subsequence of B in C++

Arnab Chakraborty
Updated on 03-Jan-2020 09:44:13

126 Views

Suppose we have a string A, we have to find another string B, that will be palindrome. And the given string A will be subsequence of B. The subsequence of a string is a string that can be formed by it by deleting some characters without changing the order of remaining characters. Suppose the string is “cotst”, then generated string will be “contest”. For the input of this program we have chosen A = “ab”, the generated string will be “abba”, this is palindrome.To solve this, we will follow this approach. This is very simple, we will reverse the A, ... Read More

Print array of strings in sorted order without copying one string into another in C++

sudhir sharma
Updated on 03-Jan-2020 09:42:54

397 Views

In the problem to print an array of strings in sorted order without copying one string into another, we need to sort the array of string. Here the programmer cannot copy a string into another string while sorting.Let’s take an example to understand the concept better :Example −Input : {“Delhi”, “Hyderabad”, “Indore”, “Mumbai”, “Banglore”} Output : Banglore, Delhi, Hyderabad, Indore, MumbaiExplanation − Lexicographically the ordering is done. So, Bangalore starting with B comes first and Mumbai starting with M comes last.Now, let's try to derive a solution to our problem.To solve the problem, we can create an array that stores the ... Read More

Advertisements