# Maximize product of absolute index difference with K

• Difficulty Level : Medium
• Last Updated : 26 Jul, 2021

Given an array A[] consisting of N integers, the task is to find the maximum possible value of K, such that K * |i – j| <= min(Ai, Aj), where (0 ? i < j < N).

Given expression, k * |i – j| <= min(Ai, Aj) can also be written as k = floor( min(Ai, Aj) / |i – j| )

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Examples:

Input: N = 5, A[ ] = {80, 10, 12, 15, 90}
Output: 20
Explanation:
For i = 0 and j = 4, the maximum possible value of K can be obtained.
Maximum k = min(A, A) / |0 – 4| = min(80, 90) / |-4| = 80/4 = 20

Input: N = 5, A[ ] = {10, 5, 12, 15, 8}
Output: 12
Explanation:
For i = 2 and j = 3, the maximum possible value of K can be obtained.
Maximum k = min(A, A) / |2 – 3| = min(12, 15) / |-1| = 12/1 = 12

Naive Approach:
The simplest approach to solve this problem is to generate all possible pairs from the given array, and for each pair, find the value of K and keep updating the maximum K obtained. Finally, print the maximum value of K obtained.
Time Complexity: O(N2)
Auxiliary Space: O(1)

Efficient Approach:
To optimize the above approach, use Two Pointers technique. Follow the steps given below:

• Initialize three variables i, j and k. Initially set i = 0 and k = 0.
• Iterate over the array, starting from j = 1 up to j = N-1.
• Now, for each pair of A[i] and A[j], find min(A[i], A[j]) / ( j – i ). If it is greater than K, then update K.
• If A[ j ] >= A[i] / ( j – i ), then update pointer i to j, to make sure that among all element up to i, A[i] will result in maximum K with all upcoming A[j]
• Finally, print the maximum value of K after the array is traversed completely.

Below is the implementation of the above approach:

## C++

 `// C++ program to implement``// the above approach``#include ``using` `namespace` `std;` `// Function returns maximum``// possible value of k``int` `solve(``int` `A[], ``int` `N)``{` `    ``// Pointer i make sure that``    ``// A[i] will result in max k``    ``int` `i = 0;` `    ``// Stores maximum possible k``    ``int` `k = 0;` `    ``for` `(``int` `j = 1; j < N; j++) {` `        ``// Possible value of k for``        ``// current pair (A[i] and A[j])``        ``int` `tempK = min(A[i], A[j])``                    ``/ (j - i);` `        ``// If current value exceeds k``        ``if` `(tempK > k) {``            ``// Update the value of k``            ``k = tempK;``        ``}` `        ``// Update pointer i``        ``if` `(A[j] >= A[i] / (j - i))``            ``i = j;``    ``}` `    ``// Return the maxm possible k``    ``return` `k;``}` `// Driver Code``int` `main()``{``    ``int` `A[] = { 10, 5, 12, 15, 8 };` `    ``int` `N = ``sizeof``(A) / ``sizeof``(A);` `    ``cout << solve(A, N);` `    ``return` `0;``}`

## Java

 `// Java program to implement``// the above approach``class` `GFG{` `// Function returns maximum``// possible value of k``static` `int` `solve(``int` `A[], ``int` `N)``{``    ` `    ``// Pointer i make sure that``    ``// A[i] will result in max k``    ``int` `i = ``0``;` `    ``// Stores maximum possible k``    ``int` `k = ``0``;` `    ``for``(``int` `j = ``1``; j < N; j++)``    ``{``        ` `        ``// Possible value of k for``        ``// current pair (A[i] and A[j])``        ``int` `tempK = Math.min(A[i], A[j]) /``                              ``(j - i);` `        ``// If current value exceeds k``        ``if` `(tempK > k)``        ``{``            ` `            ``// Update the value of k``            ``k = tempK;``        ``}` `        ``// Update pointer i``        ``if` `(A[j] >= A[i] / (j - i))``            ``i = j;``    ``}` `    ``// Return the maxm possible k``    ``return` `k;``}` `// Driver Code``public` `static` `void` `main(String[] args)``{``    ``int` `A[] = { ``10``, ``5``, ``12``, ``15``, ``8` `};``    ``int` `N = A.length;``    ` `    ``System.out.println(solve(A, N));``}``}` `// This code is contributed by rutvik_56`

## Python3

 `# Python3 program to implement``# the above approach` `# Function returns maximum``# possible value of k``def` `solve(A, N):` `    ``# Pointer i make sure that``    ``# A[i] will result in max k``    ``i ``=` `0` `    ``# Stores maximum possible k``    ``k ``=` `0` `    ``for` `j ``in` `range``(``1``, N):` `        ``# Possible value of k for``        ``# current pair (A[i] and A[j])``        ``tempK ``=` `(``min``(A[i], A[j]) ``/``/` `(j ``-` `i))``                    ` `        ``# If current value exceeds k``        ``if` `(tempK > k):``            ` `            ``# Update the value of k``            ``k ``=` `tempK``        ` `        ``# Update pointer i``        ``if` `(A[j] >``=` `A[i] ``/``/` `(j ``-` `i)):``            ``i ``=` `j` `    ``# Return the maxm possible k``    ``return` `k` `# Driver Code``if` `__name__ ``=``=` `"__main__"``:``    ` `    ``A ``=` `[ ``10``, ``5``, ``12``, ``15``, ``8` `]``    ``N ``=` `len``(A);` `    ``print``(solve(A, N))` `# This code is contributed by chitranayal`

## C#

 `// C# program to implement``// the above approach``using` `System;``class` `GFG{`` ` `// Function returns maximum``// possible value of k``static` `int` `solve(``int``[] A, ``int` `N)``{``     ` `    ``// Pointer i make sure that``    ``// A[i] will result in max k``    ``int` `i = 0;`` ` `    ``// Stores maximum possible k``    ``int` `k = 0;`` ` `    ``for``(``int` `j = 1; j < N; j++)``    ``{``         ` `        ``// Possible value of k for``        ``// current pair (A[i] and A[j])``        ``int` `tempK = Math.Min(A[i], A[j]) /``                              ``(j - i);`` ` `        ``// If current value exceeds k``        ``if` `(tempK > k)``        ``{``             ` `            ``// Update the value of k``            ``k = tempK;``        ``}`` ` `        ``// Update pointer i``        ``if` `(A[j] >= A[i] / (j - i))``            ``i = j;``    ``}`` ` `    ``// Return the maxm possible k``    ``return` `k;``}`` ` `// Driver Code``public` `static` `void` `Main(``string``[] args)``{``    ``int``[] A = { 10, 5, 12, 15, 8 };``    ``int` `N = A.Length;``     ` `    ``Console.Write(solve(A, N));``}``}`` ` `// This code is contributed by rock_cool`

## Javascript

 ``
Output:
`12`

Time Complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up