Question: We are given an array A consisting of N integers. Find the maximum K (from 0 to N-1) such that there exists a pair

We are given an array A consisting of N integers. Find the maximum K (from 0 to N - 1) such that there exists a pair of posit
 

We are given an array A consisting of N integers. Find the maximum K (from 0 to N-1) such that there exists a pair of positions (i, j) satisfying K = |i - j| = |A[i] - A[i], where Ix] denotes absolute value of x. In other words, the distance between positions is equal to the difference between values. A position together with itself (when i = j) is always a valid pair for K = 0 (look at the third example). Write a function: class Solution { public int solution (int[] A); } that, given an array A of N integers, returns the maximum possible K. Examples: 1. Given A = [2, 2, 2, 1], the function should return 1. The furthest valid pair is A[2] = 2 and A[3] = 1, as 1 = |2 - 31 = 12 - 11. 2. Given A = [2, 4, 6, 7, 4, 7, 2], the function should return 5. The furthest valid pair is A[0] and A[5]. 3. Given A = [100, 100, 100], the function should return 0. The only valid pairs are: A[0] and A[0], A[1] and A[1], A[2] and A[2]. 4. Given A = [1000000000], the function should return 0. The only valid pair is: A[0] and A[0]. Write an efficient algorithm for the following assumptions:

Step by Step Solution

3.48 Rating (161 Votes )

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock

class Solution public static void mainString args main method int A1 2 2 2 1 array A1 is initialized ... View full answer

blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Algorithms Questions!