Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find a pair (n,r) in an integer array such that value of nPr is maximum

  • Difficulty Level : Medium
  • Last Updated : 25 May, 2021

Given an array of non-negative integers arr[], the task is to find a pair (n, r) such that nPr is the 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 the 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

Javascript




<script>
 
 
// Java Script  implementation of the approach
 
     
    // Function to print the pair (n, r)
    // such that nPr is maximum possible
    function findPair(arr,n)
    {
     
        // There should be atleast 2 elements
        if (n < 2)
        {
            document.write("-1");
            return;
        }
     
        let 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];
            }
        }
     
         document.write("n = " + first +
                           " and r = " + second);
    }
     
    // Driver code
     
        let arr = [ 0, 2, 3, 4, 1, 6, 8, 9 ];
        let n = arr.length;
     
        findPair(arr, n);
     
// This code is contributed by sravan kumar
</script>
Output: 
n = 9 and r = 8

 




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!