Answered step by step
Verified Expert Solution
Question
1 Approved Answer
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 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
Step: 1
class Solution public static void mainString args main method int A1 2 2 2 1 array A1 is initialized ...Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started