## Program to calculate the value of nPr

Given two numbers n and r, the task is to find the value of nPr.

nPr represents n permutation r which is calculated as n!/(n-k)!. Permutation refers to the process of arranging all the members of a given set to form a sequence. The number of permutations on a set of n elements is given by n!, where “!” represents factorial.

`nPr = n! / (n - r)!`

Program:

 ` `  `import` `java.util.*; ` ` `  `public` `class` `GFG { ` ` `  `    ``static` `int` `fact(``int` `n) ` `    ``{ ` `        ``if` `(n <= ``1``) ` `            ``return` `1``; ` `        ``return` `n * fact(n - ``1``); ` `    ``} ` ` `  `    ``static` `int` `nPr(``int` `n, ``int` `r) ` `    ``{ ` `        ``return` `fact(n) / fact(n - r); ` `    ``} ` ` `  `    ``public` `static` `void` `main(String args[]) ` `    ``{ ` `        ``int` `n = ``5``; ` `        ``int` `r = ``2``; ` ` `  `        ``System.out.println(n + ``"P"` `+ r + ``" = "` `                           ``+ nPr(n, r)); ` `    ``} ` `} `

Optimization for multiple queries of nPr

If there are multiple queries for nPr, we may precompute factorial values and use same for every call. This would avoid computation of same factorial values again and again.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.

## Minimum removals in a number to be divisible by 10 power raised to K

Given two positive integers N and K. Find the minimum number of digits that can be removed from the number N such that after removals the number is divisible by 10K or print -1 if it is impossible.

Examples:

Approach : The idea is to start traversing the number from the last digit while keeping a counter. If the current digit is not zero, increment the counter variable, otherwise decrement variable K. When K becomes zero, return counter as answer. After traversing the whole number, check if the current value of K is zero or not. If it is zero, return counter as answer, otherwise return answer as number of digits in N – 1, since we need to reduce the whole number to a single zero which is divisible by any number. Also, if the given number does not contain any zero, return -1 as answer.

Below is the implementation of above approach.

 `#include ` `using` `namespace` `std; ` ` `  `int` `countDigitsToBeRemoved(``int` `N, ``int` `K) ` `{ ` `    ` `    ` `    ``string s = to_string(N); ` ` `  `    ` `    ` `    ``int` `res = 0; ` ` `  `    ` `    ` `    ``int` `f_zero = 0; ` `    ``for` `(``int` `i = s.size() - 1; i >= 0; i--) { ` `        ``if` `(K == 0) ` `            ``return` `res; ` `        ``if` `(s[i] == ``'0'``) { ` ` `  `            ` `            ``f_zero = 1; ` `            ``K--; ` `        ``} ` `        ``else` `            ``res++; ` `    ``} ` ` `  `    ` `    ` `    ` `    ``if` `(!K) ` `        ``return` `res; ` `    ``else` `if` `(f_zero) ` `        ``return` `s.size() - 1; ` `    ``return` `-1; ` `} ` ` `  `int` `main() ` `{ ` `    ``int` `N = 10904025, K = 2; ` `    ``cout << countDigitsToBeRemoved(N, K) << endl; ` ` `  `    ``N = 1000, K = 5; ` `    ``cout << countDigitsToBeRemoved(N, K) << endl; ` ` `  `    ``N = 23985, K = 2; ` `    ``cout << countDigitsToBeRemoved(N, K) << endl; ` `    ``return` `0; ` `} `

Time Complexity :Number of digits in the given number.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the “Improve Article” button below.

Article Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.