Found 7197 Articles for C++

Find if nCr is divisible by the given prime in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:52:50

188 Views

Suppose there are three variables N, R and P. The N and R are used to get the NCR and P is a prime. We have to find whether NCR is divisible by P. Suppose we have some numbers N = 7, R = 2 and P = 3, then 7C2 = 21, this is divisible by 3, so the output will be true.We know that NCR = N! / (R! * (N – R)! ). We will use Legendre Formula to largest power of P, which divides any N!, R! and (N – R)! in order to NCR to ... Read More

Find if a point lies inside a Circle in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:46:31

3K+ Views

Suppose, one circle is given (the center coordinate and radius), another point is also given. We have to find whether the point is inside the circle or not. To solve it, we have to find the distance of the given point from circle center. If that distance is less or equal to the radius, then that is inside the circle, otherwise not.Example Live Demo#include #include using namespace std; bool isInsideCircle(int cx, int cy, int r, int x, int y) {    int dist = (x - cx) * (x - cx) + (y - cy) * (y - cy);    if ( dist

Find HCF of two numbers without using recursion or Euclidean algorithm in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:43:18

734 Views

As we know, the HCF or GCD can be calculated easily using the Euclidean Algorithm. But here we will see how to generate GCD or HCF without using the Euclidean Algorithm, or any recursive algorithm. Suppose two numbers are present as 16 and 24. The GCD of these two is 8.Here the approach is simple. If the greater number of these two is divisible by the smaller one, then that is the HCF, otherwise starting from (smaller / 2) to 1, if the current element divides both the number, then that is the HCF.Example Live Demo#include using namespace std; int ... Read More

Find GCD of factorial of elements of given array in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:39:53

406 Views

Suppose we have an array A, with N elements. We have to find the GCD of factorials of all elements of the array. Suppose the elements are {3, 4, 8, 6}, then the GCD of factorials is 6. Here we will see the trick. As the GCD of two numbers, is the greatest number, which divides both of the numbers, then GCD of factorial of two numbers is the value of factorial of the smallest number itself. So gcd of 3! and 5! is 3! = 6.Example Live Demo#include using namespace std; long fact(int n){    if(n arr[i])   ... Read More

Minimum positive integer divisible by C and is not in range [A, B] in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:36:23

123 Views

Suppose we have three integers A, B, and C. We have to find one minimum integer X, such that X mod C = 0, and X is not in the range [A, B]. If the values of A, B and C are 5, 10 and 4 respectively, then the value of X will be 4. Let us see the steps to get the solution −Steps −If C is not in the range [A, B], then return C as a resultOtherwise get the first multiple of C, which is greater than B, then return that valueExample Live Demo#include using namespace std; ... Read More

Minimum operations to make GCD of array a multiple of k in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:33:39

155 Views

Suppose we have an array arr and another value k. We have to find a minimum number of operations to make the GCD of the array equal to the multiple of k. In this case, the operation is increasing or decreasing the value. Suppose the array is like {4, 5, 6}, and k is 5. We can increase 4 by 1, and decrease 6 by 1, so it becomes 5. Here a number of operations is 2.We have to follow these steps to get the result −Steps −for all elements e in the array, follow steps 2 and 3if e ... Read More

Minimum Number of Manipulations required to make two Strings Anagram Without Deletion of Character in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:28:24

473 Views

Suppose we have two strings of equal length, we have to find a minimum number of alterations required to make two strings anagram, without deleting any character. The Anagram is two strings that have the same set of characters. Suppose two strings are “HELLO”, and “WORLD” here number of required changes is 3, as three characters are different in this case.The idea is simple, we have to find the frequency of each character in the first string, then go through the second string, if characters in the second string are present, in the frequency array, then decrease the frequency value. ... Read More

Minimum LCM and GCD possible among all possible sub-arrays in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:22:14

271 Views

Suppose we have an array arr of size N. it has N positive numbers. We have to find the minimum elements of all possible subarray. Suppose the array is {2, 66, 14, 521}, then minimum LCM is 2, and GCD is 1.We will solve this problem using a greedy approach. If we decrease the number of elements, then LCM will be less, and if we increase the array size, GCD will be less. We need to find the smallest element from the array, which is a single element, which will be required LCM. For GCD, GCD will be GCD of ... Read More

Minimize Cost with Replacement with other allowed in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:19:38

225 Views

Suppose we have an array with N elements. We have to remove element from the array by following the given operations. The operation is like, we have to choose any two numbers of the array, and remove larger. Cost including in this operation is the same as the smaller number. We have to delete only one element at a time, based on this operation, and perform the task in minimum cost. Suppose the array has {4, 2, 5}. I take 4 and 2, remove 4 by paying cost 2, then we remove 5 again with cost 2.The approach is too ... Read More

Meta Strings (Check if two strings can become same after a swap in one string) in C++

Arnab Chakraborty
Updated on 21-Oct-2019 07:17:11

682 Views

In this section, we will see how to check whether two strings are meta string or not. The meta strings are those strings that are very much similar. If we swap two elements in one string, then it will be matched with other string. Suppose two strings are “HELLO” and “OELLH”, then they are meta strings.To check whether two strings are meta string or not, we have to follow these steps.Steps −If both strings are of different length, then return falseElse find a number of characters, that have not matched, also store the index of non-matched charactersIf the count is ... Read More

Advertisements