## 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.

## Amazon Interview Experience SDE Off Campus

Written Round: (1 hour)

1. Given a linked list and K, print the last K nodes in reverse order.
example: 1->2->3->4->5->6, K = 3 output: 6 5 4
2. Find out whether the given tree is BST or not.
3. Rotate Given matrix by 90 clockwise.

Technical Round: (1 hour)

1. Given an array, for every element in the array, you need to print the product of all except the ith element itself.
2. Given N train with their arrival and departure timings, you need to find the minimum number of platforms are required to accommodate all. The condition is, no train has to wait to get the free platform.

Technical Round: (1 hour)

1. Define your own data structure that should do two operations at its best complexity.
• insert(num): insert num into the data structure.
• find(sum): return a pair(a, b) such that a+b = sum, if no such pair exists return -1.

2. Give a graph find out whether it is a tree or not.

Managerial Round: (1 hour)

1. Long discussions on my projects.
2. Have you disagreed with your manager’s opinion?
3. Technical Challenges faced so far in the current role?
4. Any technical solution provided by you to your team? Why your solution was better? and what was the outcome?
5. Why are you looking for opportunities?
6. Why Amazon?

Bar Raised Round: (1 hour)

1. Long discussions on my project and current role?
2. Long discussion on the trie data structure, comparison with other data structures.
3. Why are you leaving your current role?
4. Why Amazon?
5. Given an array of integers, find out the range of contiguous elements with Largest sum. (Kadane’s algorithm)

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.