Server Side Programming Articles

Page 1427 of 2109

Find a point such that sum of the Manhattan distances is minimize in C++

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

Suppose we have n different points in K dimension space, the value of n is in range (2, 105), and value of k in range (1 to 5). We have to determine the point such that the sum of Manhattan distance from resultant point to n points is minimized.The Manhattan distance between two points P1(x1, y1) and P2(x2, y2), is |x1 – x2| + |y1 – y2|. Suppose dimension is 3, and there are three points like (1, 1, 1), (2, 2, 2), (3, 3, 3), then the output will be (2, 2, 2).To solve this problem, we have to ...

Read More

Find All Duplicate Subtrees in C++

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

Consider we have a binary tree. We have to find if there are some duplicate subtrees in the tree or not. Suppose we have a binary tree like below −There are two identical subtrees of size 2. In each subtree D, BD and BE both are also duplicate subtrees We can solve this problem by using tree serialization and hashing process. We will store the inorder traversal of subtrees in the hash table. We will insert opening and closing parenthesis for empty nodes.Example#include #include #include #include using namespace std; const char MARKER = '$'; struct Node ...

Read More

Find all factorial numbers less than or equal to n in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 11-Mar-2026 1K+ Views

Here we will see how to print all factorial numbers less than or equal to n, a number N is said to be factorial number if it is a factorial of a positive number. So some factorial numbers are 1, 2, 6, 24, 120.To print factorial numbers, we do not need to find the factorial directly. Starting from i = 1, print factorial*i. Initially factorial is 1. Let us see the code for better understanding.Example#include using namespace std; void getFactorialNumbers(int n) {    int fact = 1;    int i = 2;    while(fact

Read More

Find all pairs (a, b) in an array such that a % b = k in C++

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

Suppose we have an array A, from that array, we have to get all pairs (a, b) such that the a%b = k. Suppose the array is A = [2, 3, 4, 5, 7], and k = 3, then pairs are (7, 4), (3, 4), (3, 5), (3, 7).To solve this, we will traverse the list and check whether the given condition is satisfying or not.Example#include using namespace std; bool displayPairs(int arr[], int n, int k) {    bool pairAvilable = true;    for (int i = 0; i < n; i++) {       for (int j = 0; j < n; j++) {          if (arr[i] % arr[j] == k) {             cout

Read More

Find all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++

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

Suppose we have an array A, from that array, we have to choose two pairs (a, b) and (c, d), such that ab = cd. Let the array A = [3, 4, 7, 1, 2, 9, 8]. The output pairs are (4, 2) and (1, 8). To solve this, we will follow these steps −For i := 0 to n-1, dofor j := i + 1 to n-1, doget product = arr[i] * arr[j]if product is not present in the hash table, then Hash[product] := (i, j)if product is present in the hash table, then print previous and current elements.Example#include ...

Read More

Find amount to be added to achieve target ratio in a given mixture in C++

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

Suppose we have a container with size X. It has a mixture of water and other liquid, the mixture has W% of water in it. We have to find how many water must be added to increase the ratio of water to Y%? If X = 125, W = 20 and Y = 25, then output will be 8.33 liters.Suppose we have to add A amount of water with the previous mixture, so new amount will be X + A. So the amount of water in the mixture will follow this formula.Old Amount+A=((W% of X) + A)Also the amount of ...

Read More

Find a number x such that sum of x and its digits is equal to given n using C++.

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

Here we will see one problem, where we take a number n, we have to find another value say x, such that x + digit sum of x is same as the given number n. Suppose the value of n is 21. This program will return a number x = 15, as 15 + digit sum of 15, i.e. 15 + 1 + 5 = 21 = n.To solve this problem, we have to follow simple approach. We will iterate through 1 to n, in each iteration, we will see if the sum of the number and its digit sum ...

Read More

Find an array element such that all elements are divisible by it using c++

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

Consider we have an array A with few elements. We have to find an element from A, such that all elements can be divided by it. Suppose the A is like [15, 21, 69, 33, 3, 72, 81], then the element will be 3, as all numbers can be divisible by 3.To solve this problem, we will take the smallest number in A, then check whether all numbers can be divided by the smallest number or not, if yes, then return the number, otherwise, return false.Example#include #include using namespace std; int getNumber(int a[], int n) {    int minNumber = ...

Read More

Find an element in array such that sum of left array is equal to sum of right array using c++

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

Suppose we have an array A, it has n elements. Our task is to divide the array A into two subarrays, such that the sum of each subarray will be the same. Suppose the array A = [2, 3, 4, 1, 4, 5], The output is 1, so subarrays before 1 and after 1 are taken. [2, 3, 4], and [4, 5].To solve this problem, we will calculate the whole array except for the first element in right_sum. Consider that is the partitioning element. We will traverse from left to right. Subtracting an element from right_sum and adding an element ...

Read More

Find and print duplicate words in std::vector using STL functions using C++.

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

Consider we have a list of strings. The list has some duplicate strings. We have to check which strings are occurred more than once. Suppose the string list is like [“Hello”, “Kite”, “Hello”, “C++”, “Tom”, “C++”]Here we will use the hashing technique, so create an empty hash table, then traverse each string, and for each string, s is already present in the hash, then display the string, otherwise insert into the hash.Example#include #include #include using namespace std; void displayDupliateStrings(vector strings) {    unordered_set s;    bool hasDuplicate = false;    for (int i = 0; i

Read More
Showing 14261–14270 of 21,090 articles
Advertisements