Found 7197 Articles for C++

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

Arnab Chakraborty
Updated on 29-Oct-2019 11:27:06

736 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 an array element such that all elements are divisible by it using c++

Arnab Chakraborty
Updated on 29-Oct-2019 11:22:13

389 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 Live Demo#include #include using namespace std; int getNumber(int a[], int n) {    int minNumber ... Read More

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

Arnab Chakraborty
Updated on 29-Oct-2019 11:16:34

95 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

Filling diagonal to make the sum of every row, column and diagonal equal of 3×3 matrix using c++

Arnab Chakraborty
Updated on 29-Oct-2019 11:19:29

261 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 a row, column and diagonal will be the same. Suppose a matrix is like −After filling, it will be −Suppose 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 Live Demo#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

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

Arnab Chakraborty
Updated on 24-Oct-2019 14:12:49

63 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 all pairs (a,b) and (c,d) in array which satisfy ab = cd in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:23:15

230 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 Live ... Read More

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

Arnab Chakraborty
Updated on 24-Oct-2019 13:15:12

181 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 Live Demo#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

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

Arnab Chakraborty
Updated on 24-Oct-2019 13:08:48

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 Live Demo#include using namespace std; void getFactorialNumbers(int n) {    int fact = 1;    int i = 2;    while(fact

Find All Duplicate Subtrees in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:05:38

190 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 Live Demo#include #include #include #include using namespace std; const char MARKER = '$'; struct ... Read More

Find a Symmetric matrix of order N that contain integers from 0 to N-1 and main diagonal should contain only 0’s in C++

Arnab Chakraborty
Updated on 24-Oct-2019 13:02:41

136 Views

Here we will see how to generate one symmetric matrix of order N, and the elements of each row will contain numbers from 0 to N – 1. The diagonal elements will be 0 always.This task is easy, we will form a matrix of N x N, then for each row i and for each column j, if i and j are same, then mark it as 0, otherwise increase one counter from 1 to N – 1, place the values for each individual row.Example#include using namespace std; void makeSymmetricMatrix(int n) {    int matrix[n][n];    for(int i = 0; i

Advertisements