Given an array of non-negative integers arr[], the task is to find a pair (n, r) such that nPr is maximum possible and r ≤ n.
nPr = n! / (n – r)!
Examples:
Input: arr[] = {5, 2, 3, 4, 1}
Output: n = 5 and r = 4
5P4 = 5! / (5 – 4)! = 120 which is maximum possible.Input: arr[] = {0, 2, 3, 4, 1, 6, 8, 9}
Output: n = 9 and r = 8
Naive approach: A simple approach is to consider each (n, r) pair and calculate nPr value and find the maximum value among them.
Efficient approach: Since nPr = n! / (n – r)! = n * (n – 1) * (n – 2) * … * (n – r + 1).
With little mathematics, it can be shown that nPr will be maximum when n is maximum and n – r is minimum. The problem now boils down to finding the largest 2 elements from the given array.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <iostream> using namespace std; // Function to print the pair (n, r) // such that nPr is maximum possible void findPair(int arr[], int n) { // There should be atleast 2 elements if (n < 2) { cout << "-1"; return; } int i, first, second; first = second = -1; // Findex the largest 2 elements for (i = 0; i < n; i++) { if (arr[i] > first) { second = first; first = arr[i]; } else if (arr[i] > second) { second = arr[i]; } } cout << "n = " << first << " and r = " << second; } // Driver code int main() { int arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 }; int n = sizeof(arr) / sizeof(arr[0]); findPair(arr, n); return 0; } |
Java
// Java implementation of the approach class GFG { // Function to print the pair (n, r) // such that nPr is maximum possible static void findPair(int arr[], int n) { // There should be atleast 2 elements if (n < 2) { System.out.print("-1"); return; } int i, first, second; first = second = -1; // Findex the largest 2 elements for (i = 0; i < n; i++) { if (arr[i] > first) { second = first; first = arr[i]; } else if (arr[i] > second) { second = arr[i]; } } System.out.println("n = " + first + " and r = " + second); } // Driver code public static void main(String args[]) { int arr[] = { 0, 2, 3, 4, 1, 6, 8, 9 }; int n = arr.length; findPair(arr, n); } } // This code is contributed by AnkitRai01 |
Python3
# Python3 implementation of the approach # Function to print the pair (n, r) # such that nPr is maximum possible def findPair(arr, n): # There should be atleast 2 elements if (n < 2): print("-1") return i = 0 first = -1 second = -1 # Findex the largest 2 elements for i in range(n): if (arr[i] > first): second = first first = arr[i] elif (arr[i] > second): second = arr[i] print("n =", first, "and r =", second) # Driver code arr = [0, 2, 3, 4, 1, 6, 8, 9] n = len(arr) findPair(arr, n) # This code is contributed by mohit kumar |
C#
// C# implementation of the approach using System; class GFG { // Function to print the pair (n, r) // such that nPr is maximum possible static void findPair(int[] arr, int n) { // There should be atleast 2 elements if (n < 2) { Console.Write("-1"); return; } int i, first, second; first = second = -1; // Findex the largest 2 elements for (i = 0; i < n; i++) { if (arr[i] > first) { second = first; first = arr[i]; } else if (arr[i] > second) { second = arr[i]; } } Console.WriteLine("n = " + first + " and r = " + second); } // Driver code public static void Main() { int[] arr = { 0, 2, 3, 4, 1, 6, 8, 9 }; int n = arr.Length; findPair(arr, n); } } // This code is contributed by CodeMech |
n = 9 and r = 8
Recommended Posts:
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- C program to calculate the value of nPr
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Pair formation such that maximum pair sum is minimized
- Find a pair from the given array with maximum nCr value
- Store duplicate keys-values pair and sort the key-value pair by key
- Only integer with positive value in positive negative value in array
- Probability of a random pair being the maximum weighted pair
- Choose an integer K such that maximum of the xor values of K with all Array elements is minimized
- Maximum AND value of a pair in an array
- Maximum OR value of a pair in an array
- Maximum OR value of a pair in an Array | Set 2
- Maximum OR value of a pair in an Array without using OR operator
- Find the maximum possible value of the minimum value of modified array
- Pair with given product | Set 1 (Find if any pair exists)
- Number of triplets such that each value is less than N and each pair sum is a multiple of K
- Minimize prize count required such that smaller value gets less prize in an adjacent pair
- Find the smallest positive integer value that cannot be represented as sum of any subset of a given array
- Maximum value of an integer for which factorial can be calculated on a machine
- Minimum integer with at most K bits set such that their bitwise AND with N is maximum
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.


