C++ Articles

Page 232 of 597

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 571 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 Ancestors of a given node in Binary Tree in C++

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 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 alphabet in a Matrix which has maximum number of stars around it in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 246 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#include #include #define MAX 4 ...

Read More

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

sudhir sharma
sudhir sharma
Updated on 11-Mar-2026 250 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 duplicate rows in a binary matrix in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 218 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#include using namespace std; const int MAX = 100; class Trie {   ...

Read More

Print an N x M matrix such that each row and column has all the vowels in it in C++

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

In this problem, we have to create a 2D matrix of size n X m. And in this matrix, we have to place only vowels in such a way that each row and column has all vowels in it.All vowels mean all a, e, i, o, u are present in each row and each column of the matrix. This makes the minimum number of rows and columns required is 5 i.e. the smallest matrix is of the size 5X5.Let’s take an example to understand the topic betterExample 1 −Input : N = 5 and M = 5. Output :    a ...

Read More

Find four factors of N with maximum product and sum equal to N in C++

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

Suppose we have an integer N. The task is to find all factors of N and display the product of four factors of N, such that −Sum of their four factors are equal to NThe product of four factors is maximumSuppose the number is 24, then the product is 1296. As we know all of the factors are 1, 2, 3, 4, 6, 8, 12, 24. We have to choose the factors 6 four times. So 6 + 6 + 6 + 6 = 24. Here the product is maximum.To solve this, we have to find all factors from 1 ...

Read More

Print alternate nodes of a linked list using recursion in C++

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

A linked list is a linear data structure that stores the element in non-contiguous memory locations. Every element contains a pointer to the next element of the linked list.Example −In this problem, we are given a linked list and we need to print the elements of this linked list but only alternate elements are to be printed. Let’s take an example to understand the problem better, Input : 2 -> 4 -> 1 -> 67 -> 48 -> 90 Output : 2 -> 1 -> 48Explanation − We will print alternate elements on the linked list. So first, third and ...

Read More

Print all words matching a pattern in CamelCase Notation Dictionary in C++

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

In this problem, we are given an array of string in camelcase and a pattern. We have to print all those string of the array that match the given pattern.The array of string is an array in which elements are of the string data type.camelCase is a common method for naming in programming, in this way the first letter of the new word starts with an uppercase, rest all are lower case.Example − iLoveProgrammingProblem − find all strings that match a given pattern.Example −Input : “TutorialsPoint” , “ProgrammersPoint” , “ProgrammingLover” , “Tutorials”. Pattern : ‘P’ Output : “TutorialsPoint” , “ProgrammersPoint” ...

Read More

Find if there is a rectangle in binary matrix with corners as 1 in C++

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

Suppose we have a binary matrix. We have to find if there is any rectangle or sequence in the given matrix whose all four corners are equal to 1. The matrix is like10010001010001010101The result will be yes. Here one rectangle is present, whose corners are with 1s.101010101To solve this we will use one efficient approach. We will follow these steps −Scan the matrix from top to bottom line by lineFor each line remember each combination of two 1’s and push that into a hash-set.If we ever find that combination again in the later line, we will get our rectangle.Example#include #include ...

Read More
Showing 2311–2320 of 5,962 articles
« Prev 1 230 231 232 233 234 597 Next »
Advertisements