Maximize product of absolute index difference with K
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| )
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[0], A[4]) / |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[2], A[3]) / |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++
#include <bits/stdc++.h>
using namespace std;
int solve( int A[], int N)
{
int i = 0;
int k = 0;
for ( int j = 1; j < N; j++) {
int tempK = min(A[i], A[j])
/ (j - i);
if (tempK > k) {
k = tempK;
}
if (A[j] >= A[i] / (j - i))
i = j;
}
return k;
}
int main()
{
int A[] = { 10, 5, 12, 15, 8 };
int N = sizeof (A) / sizeof (A[0]);
cout << solve(A, N);
return 0;
}
|
Java
class GFG{
static int solve( int A[], int N)
{
int i = 0 ;
int k = 0 ;
for ( int j = 1 ; j < N; j++)
{
int tempK = Math.min(A[i], A[j]) /
(j - i);
if (tempK > k)
{
k = tempK;
}
if (A[j] >= A[i] / (j - i))
i = j;
}
return k;
}
public static void main(String[] args)
{
int A[] = { 10 , 5 , 12 , 15 , 8 };
int N = A.length;
System.out.println(solve(A, N));
}
}
|
Python3
def solve(A, N):
i = 0
k = 0
for j in range ( 1 , N):
tempK = ( min (A[i], A[j]) / / (j - i))
if (tempK > k):
k = tempK
if (A[j] > = A[i] / / (j - i)):
i = j
return k
if __name__ = = "__main__" :
A = [ 10 , 5 , 12 , 15 , 8 ]
N = len (A);
print (solve(A, N))
|
C#
using System;
class GFG{
static int solve( int [] A, int N)
{
int i = 0;
int k = 0;
for ( int j = 1; j < N; j++)
{
int tempK = Math.Min(A[i], A[j]) /
(j - i);
if (tempK > k)
{
k = tempK;
}
if (A[j] >= A[i] / (j - i))
i = j;
}
return k;
}
public static void Main( string [] args)
{
int [] A = { 10, 5, 12, 15, 8 };
int N = A.Length;
Console.Write(solve(A, N));
}
}
|
Javascript
<script>
function solve(A, N)
{
let i = 0;
let k = 0;
for (let j = 1; j < N; j++)
{
let tempK = Math.min(A[i], A[j]) / (j - i);
if (tempK > k)
{
k = tempK;
}
if (A[j] >= A[i] / (j - i))
i = j;
}
return k;
}
let A = [ 10, 5, 12, 15, 8 ];
let N = A.length;
document.write(solve(A, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...