Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 338 of 377

Find duplicates under given constraints in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 214 Views

Suppose we have a list with 6 different numbers. Only one number is repeated five times. So there are total 10 elements in the array. Find duplicate numbers using only two comparisons. If the list is like [1, 2, 3, 4, 4, 4, 4, 4, 5, 6], so output is 4.As there are only 10 numbers, then for any type of duplicate numbers, the range of numbers will be placed from index 3 to 5. By checking these indices, we can find the result.Example#include using namespace std; int getDuplicate(int array[]) {    if (array[3] == array[4])       return array[3];    else if (array[4] == array[5])       return array[4];    else       return array[5]; } int main() {    int a[] = {1, 2, 3, 4, 4, 4, 4, 4, 5, 6};    cout

Read More

Find duplicates in O(n) time and O(1) extra space - Set 1 in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 370 Views

Suppose we have a list of numbers from 0 to n-1. A number can be repeated as many as 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 repetition.Example#include ...

Read More

Find cubic root of a number in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 476 Views

Here we will see how to get the cubic root of a number. Suppose a number is say 27, then the cubic root of this number is 3. To solve this problem, we will define our own logic without using some library functions. We will use the binary search approach. We have to follow these steps to solve this problem.Suppose we have threshold value like threshold = 0.000001Start the left value as 0, and right value as numbercalculate mid := (left + right)/2if the absolute value of (number – mid3) is less than threshold, then return mid as answerif mid3 ...

Read More

Find count of digits in a number that divide the number in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 328 Views

Suppose a number is given. We have to count number of digits of the number which evenly divides the number. Suppose the number is 1012, the result is 3. There are three digits 1, 1, and 2 that evenly divide 1012.To solve this, we will find each digit of the number, using modulus operation, and check whether the number is divisible by that digit or not, if divisible, then increase counter. If the digit is 0, then ignore that digit.Example#include using namespace std;    int countDivDigit(int num) {    int count = 0;    int temp = num;    while(temp){ ...

Read More

Find closest value for every element in array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 414 Views

Here we will see how to find the closest value for every element in an array. If an element x has next element that is larger than it, and also present in the array, then that will be the greater value of that element. If the element is not present, then return -1. Suppose the array elements are [10, 5, 11, 6, 20, 12], then the greater elements are [11, 6, 12, 10, -1, 20]. As 20 has not greater value in the array, then print -1.To solve this, we will use the set in C++ STL. The set is ...

Read More

Find closest greater value for every element in array in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 351 Views

Here we will see how to find the closest greater value for every element in an array. If an element x has next element that is larger than it, and also present in the array, then that will be the greater value of that element. If the element is not present, then return -1. Suppose the array elements are [10, 5, 11, 6, 20, 12], then the greater elements are [11, 6, 12, 10, -1, 20]. As 20 has not greater value in the array, then print -1.To solve this, we will use the set in C++ STL. The set ...

Read More

Find all triplets in a sorted array that forms Geometric Progression in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 566 Views

Suppose we have a sorted array with distinct positive integers. We have to find all triplets, that forms Geometric progression with integral common ratio. Suppose the array elements are [1, 2, 6, 10, 18, 54], The triplets are (2, 6, 18), and (6, 18, 54), these are forming geometric progression.To solve this, we will start from the second element, and fix every element as middle element, and search for the lesser and greater elements. For middle element arr[j] to be middle of geometric progression, the previous element arr[i] and arr[k] will be like$$\frac{arr[j]}{arr[i]}=\frac{arr[k]}{arr[j]}=r𝑟$$Example#include using namespace std; void getTriplets(int arr[], int ...

Read More

Find all strings that match specific pattern in a dictionary in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 640 Views

Consider we have a list of strings called dictionary. We have another pattern string. Our task is to find those strings that matches the pattern. Suppose the dictionary is like [“abb”, “xyz”, “aab”, “kmm”], and pattern is “stt”, then the results will be “abb”, and “kmm”. As the pattern has one letter at first, then the two same letters, so it will follow same pattern strings.To solve this problem, we will encode the pattern in such a way that any word from the dictionary, that matches the pattern, will have the same hash like the pattern after encoding. We will ...

Read More

Find all possible coordinates of parallelogram in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 253 Views

Find the all of the possible coordinates from the given three coordinates to make e parallelogram of a non-zero area. Suppose A, B, C are three given points we can have only three possible situations.AB, AC are sides, and BC is diagonalAB, BC are sides, and AC is diagonalBC, AC are sides, and AB is diagonalSo we can say that only three coordinates are possible, from which we can generate a parallelogram, if three coordinates are given. Since the opposite sides are equal, then AD = BC, and AB = CD, we will calculate the coordinate of the missing points ...

Read More

Find all distinct subsets of a given set in C++

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 4K+ Views

Here we will see how to display all distinct subsets of a given set. So if the set is {1, 2, 3}, then the subsets will be {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}. The set of all subsets is called power set. The power set has 2n elements.We will loop through 0 to 2n (excluding), in each iteration we will check whether the ith bit in the current counter is set, then print ith element.Example#include #include using namespace std; void showPowerSet(char *set, int set_length) {    unsigned int size = pow(2, set_length);    for(int counter = 0; counter < size; counter++) {       cout

Read More
Showing 3371–3380 of 3,768 articles
« Prev 1 336 337 338 339 340 377 Next »
Advertisements