Arnab Chakraborty

Arnab Chakraborty

3,768 Articles Published

Articles by Arnab Chakraborty

Page 369 of 377

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

Arnab Chakraborty
Arnab Chakraborty
Updated on 01-Nov-2019 337 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 421 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 367 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 577 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 646 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 263 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

Find all distinct subset (or subsequence) sums of an array in C++

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

Suppose we have a set of integers. Find distinct sum, that can be formed from the subset of the given sets and print them in an ascending order. The sum of array elements is small. Consider the array elements are like [1, 2, 3]. Output will be 0, 1, 2, 3, 4, 5, 6. The distinct subsets are {}, {1}, {2}, {3}, {1, 2}, {2, 3}, {1, 3}, {1, 2, 3}, the sum values are 0, 1, 2, 3, 3, 5, 4, 6.To solve this, we will use the dynamic programming approach. When the sum of given element is small, ...

Read More

Find all combinations that add upto given number using C++

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

Suppose we have a positive number n. We have to find all combinations of positive numbers, that adds up to that number. Here we want only combinations, not the permutations. For the value n = 4, there will be [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4]We will solve this using recursion. We have an array to store combinations, and we will fill that array using recursive approach. Each combination will be stored in increasing order of elements.Example#include using namespace std; void getCombination(int arr[], int index, int num, int decrement) {    if (decrement < 0)       return;    if (decrement == 0){       for (int i = 0; i < index; i++)          cout

Read More

Find the unit place digit of sum of N factorials using C++.

Arnab Chakraborty
Arnab Chakraborty
Updated on 30-Oct-2019 246 Views

Here we will see how to get the unit place digit of the sum of N factorials. So if N is 3, then after getting sum, we will get 1! + 2! + 3! = 9, this will be the result, for N = 4, it will be 1! + 2! + 3! + 4! = 33. so unit place is 3. If we see this clearly, then as the factorials of N > 5, the unit place is 0, so after 5, it will not contribute to change the unit place. For N = 4 and more, it will ...

Read More
Showing 3681–3690 of 3,768 articles
« Prev 1 367 368 369 370 371 377 Next »
Advertisements