Found 26504 Articles for Server Side Programming

Find if an array contains a string with one mismatch in C++

Arnab Chakraborty
Updated on 19-Dec-2019 08:05:55

191 Views

Suppose we have a string s, and another array of strings A. We have to find whether the array is containing a string with the one-character difference from the current string of different lengths. Suppose the string is like “banana”, and the array looks like [“bana”, “orange”, “banaba”, “banapy”], the result will be true, as there is one string banaba, here only one character is different than a banana.To solve this problem, we will follow some steps −Traverse through given string s, and check for every string in the array, then follow these steps for every string in arr −Check ... Read More

Find gcd(a^n, c) where a, n and c can vary from 1 to 10^9 in C++

Arnab Chakraborty
Updated on 19-Dec-2019 08:01:30

83 Views

We have to find the GCD of two numbers of which one number can be as big as (109 ^ 109), which cannot be stored in some data types like long or any other. So if the numbers are a = 10248585, n = 1000000, b = 12564, then result of GCD(a^n, b) will be 9.As the numbers are very long, we cannot use the Euclidean algorithm. We have to use the modular exponentiation with O(log n) complexity.Example Live Demo#include #include using namespace std; long long power(long long a, long long n, long long b) {    long long res = ... Read More

Find frequency of each element in a limited range array in less than O(n) time in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:58:53

331 Views

Suppose we have an array of integers. The array is A, and the size is n. Our task is to find the frequency of all elements in the array less than O(n) time. The size of the elements must be less than one value say M. Here we will use the binary search approach. Here we will recursively divide the array into two parts if the end elements are different, if both its end elements are the same, it means all elements in the array are the same as the array is already sorted.Example Live Demo#include #include using namespace std; void ... Read More

Find four elements a, b, c and d in an array such that a+b = c+d in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:55:06

821 Views

Suppose we have a list of integers. Our task is to find four distinct integers as two pairs like (a, b) and (c, d), such that a+b = c+d. If there are multiple answers, then print only one. Suppose the array elements are like: A = [7, 5, 9, 3, 6, 4, 2], then pairs can be (7, 3) and (6, 4)Here we will use the hashing technique. We use the sum as key as pair as the value in the hash table. We have to follow these steps to solve this problem.For i in range 0 to n – ... Read More

Find three closest elements from given three sorted arrays in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:52:51

403 Views

Suppose we have three sorted arrays A, B and C, and three elements i, j and k from A, B and C respectively such that max(|A[i] – B[i]|, |B[j] – C[k]|, |C[k] – A[i]|) is minimized. So if A = [1, 4, 10], B = [2, 15, 20], and C = [10, 12], then output elements are 10, 15, 10, these three from A, B and C.Suppose the size of A, B and C are p, q and r respectively. Now follow these steps to solve this −i := 0, j := 0 and k := 0Now do the following ... Read More

Find element in a sorted array whose frequency is greater than or equal to n/2 in C++.

Arnab Chakraborty
Updated on 19-Dec-2019 07:50:35

145 Views

Consider we have an array with size n. This array is sorted. There is one element whose frequency is greater than or equal to n/2, where n is the number of elements in the array. So if the array is like [3, 4, 5, 5, 5], then the output will be 5.If we closely observe these type of array, we can easily notice that the number whose frequency is greater than or equal to n/2, will be present at index n/2 also. So the element can be found at position n/2Example Live DemoSource Code: #include using namespace std; int higherFreq(int arr[], ... Read More

Find the sum of digits of a number at even and odd places in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:49:11

1K+ Views

Suppose, we have an integer N, We have to find the sum of the odd place digits and the even place digits. So if the number is like 153654, then odd_sum = 9, and even_sum = 15.To solve this, we can extract all digits from last digit, if the original number has odd number of digits, then the last digit must be odd positioned, else it will be even positioned. After process a digit, we can invert the state from odd to even and vice versa.Example Live Demo#include using namespace std; bool isOdd(int x){    if(x % 2 == 0)   ... Read More

Find duplicate in an array in O(n) and by using O(1) extra space in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:47:38

139 Views

Suppose we have a list of numbers from 0 to n-1. A number can be repeated as many as a possible number of times. We have to find the repeating numbers without taking any extra space. If the value of n = 7, and list is like [5, 2, 3, 5, 1, 6, 2, 3, 4, 5]. The answer will be 5, 2, 3.To solve this, we have to follow these steps −for each element e in the list, do the following steps −sign := A[absolute value of e]if the sign is positive, then make it negativeOtherwise, it is a ... Read More

Find the Product of first N Prime Numbers in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:46:12

375 Views

Suppose we have a number n. We have to find the product of prime numbers between 1 to n. So if n = 7, then output will be 210, as 2 * 3 * 5 * 7 = 210.We will use the Sieve of Eratosthenes method to find all primes. Then calculate the product of them.Example Live Demo#include using namespace std; long PrimeProds(int n) {    bool prime[n + 1];    for(int i = 0; i

Find distance from root to given node in a binary tree in C++

Arnab Chakraborty
Updated on 19-Dec-2019 07:44:44

373 Views

Consider we have a binary tree with few nodes. We have to find the distance between the root and another node u. suppose the tree is like below:Now the distance between (root, 6) = 2, path length is 2, distance between (root, 8) = 3 etc.To solve this problem, we will use a recursive approach to search the node at the left and right subtrees, and also update the lengths for each level.Example Live Demo#include using namespace std; class Node {    public:       int data;    Node *left, *right; }; Node* getNode(int data) {    Node* node = ... Read More

Advertisements