Maximize product of absolute index difference with K | Java program to implement the above approach ; Function returns maximum possible value of k ; Pointer i make sure that A [ i ] will result in max k ; Stores maximum possible k ; Possible value of k for current pair ( A [ i ] and A [ j ] ) ; If current value exceeds k ; Update the value of k ; Update pointer i ; Return the maxm possible k ; Driver Code
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 ) ) ; } }