Found 7347 Articles for C++

Find a pair of elements swapping which makes sum of two arrays same in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:44:48

311 Views

Consider we have two arrays with different number of elements. We have to find a pair of elements (x, y), where x is present in the first array, and y is present at the second array. The pair will be chosen such that after swapping the elements between these two arrays, the sum of these two arrays will be same.Suppose first array A is holding [4, 1, 2, 2, 1, 1] and B is holding [3, 3, 6, 3], now the sum of A is 11 and sum of B is 15, we will take a pair like (1, 3), ... Read More

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

Arnab Chakraborty
Updated on 24-Oct-2019 12:40:45

271 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 a Fixed Point in an array with duplicates allowed in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:37:06

114 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

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

Arnab Chakraborty
Updated on 24-Oct-2019 12:34:24

172 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 distinct pair (x, y) in given range such that x divides y in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:31:50

74 Views

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

Final cell position in the matrix in C++

Arnab Chakraborty
Updated on 24-Oct-2019 12:29:04

145 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

Fill missing entries of a magic square in C++

Arnab Chakraborty
Updated on 24-Oct-2019 11:53:22

161 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 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

Sum of sum of first n natural numbers in C++

sudhir sharma
Updated on 24-Oct-2019 11:18:27

179 Views

In this problem to find the sum of sum of first n natural numbers, we will find the sum of all numbers from 1 to n and add them together to find the sum.Let’s take an example to learn about the concept,Input : 4 Output : 10 Explanation : Sum of first 1 natural number = 1 Sum of first 2 natural number = 1 + 2 = 3 Sum of first 3 natural number = 1 + 2 +3 = 6 Sum of first 4 natural number = 1 + 2 + 3 + 4 = 10 Sum of sum of 4 natural number = 1 + 3 + 6 + 10 = 20Example Live Demo#include using namespace std; int sumofSum(int n){    int sum = 0;    for (int i=1; i

Add all greater values to every node in a given BST in C++ ?

sudhir sharma
Updated on 24-Oct-2019 11:17:01

67 Views

A BST or binary search tree is a form of binary tree that has all left nodes smaller and all right nodes greater than the root value. For this problem, we will take a binary tree and add all the values greater than the current node to it. the problem “ add all greater values to every node in BST” is simplified as for a BST add all the node values that are greater than the current node value to that node value.Add all greater values to each node in BST Problem Statement −Given a Binary Search Tree (BST), we ... Read More

Binary Search in C++ program?

sudhir sharma
Updated on 24-Oct-2019 11:12:46

692 Views

binary search, also known as half-interval search, logarithmic search, or binary chop, is a search algorithm that finds the position of a target value within a sorted array. Binary search compares the target value to the middle element of the array. If they are not equal, the half in which the target cannot lie is eliminated and the search continues on the remaining half, again taking the middle element to compare to the target value, and repeating this until the target value is found. If the search ends with the remaining half being empty, the target is not in the ... Read More

Advertisements