Open In App

Find pair i, j such that |A[i]−A[j]| is same as sum of difference of them with any Array element

Last Updated : 11 Apr, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array A[] having N non-negative integers, find a pair of indices i and j such that the absolute difference between them is same as the sum of differences of those with any other array element i.e., | A[i] − A[k] | + | A[k]− A[j] | = | A[i] − A[j] |, where k can be any index.

Examples:

Input: N = 3, A[] = {2, 7, 5}
Output: 0 1
Explanation:
For k = 0: 
|A[0] - A[0]| + |A[0] - A[1]|
= |2 − 2| + |2 - 7| = 0 + 5 = 5
= |A[0] - A[1]|
For k = 1:
|A[0] - A[1]| + |A[1] - A[1]|
= |2 − 7| + |7 - 7| = 5 + 0 = 5
= |A[0] - A[1]|
For k = 2:
|A[0] - A[2]| + |A[2] - A[1]|
= |2 − 5| + |5 - 7| = 3 + 2 = 5
= |A[0] - A[1]|

Input: N = 4, arr[] = {5, 9, 1, 3}
Output: 1 2
Explanation: 
For k = 0: 
|A[1] - A[0]| + |A[0] - A[2]|
= |9 − 5| + |5 - 1| = 4 + 4 = 8
= |A[1] - A[2]|
For k = 1:
|A[1] - A[1]| + |A[1] - A[2]|
= |9 − 9| + |9 - 1| = 0 + 8 = 8
= |A[1] - A[2]|
For k = 2:
|A[1] - A[2]| + |A[2] - A[2]|
= |9 − 1| + |1 - 1| = 8 + 0 = 8
= |A[1] - A[2]|
For k = 3:
|A[1] - A[3]| + |A[3] - A[2]|
= |9 − 3| + |3 - 1| = 6 + 2 = 8
= |A[1] - A[2]|

 

Approach: The problem can be solved with the below mathematical observation:

Depending on the relation between A[i], A[j] and A[k], the inequality can be written in the following 4 ways:

When A[i] ≥ A[k] ≥ A[j]:
A[i] − A[k]  + A[k]− A[j] = A[i] − A[j]
=> A[i] - A[j] =  A[i] − A[j] 

When A[k] ≥ A[i], A[k] ≥ A[j]:
A[k] − A[i]  + A[k]− A[j] = |A[i] − A[j]|
=> 2*A[k] - A[i] - A[j] =  |A[i] − A[j]| 

When A[i] ≥ A[k], A[j] ≥ A[k]:
A[i] − A[k]  - A[k]+ A[j] = |A[i] − A[j]|
=> A[i] + A[j] - 2*A[k] =  |A[i] − A[j]| 

When A[j] ≥ A[k] ≥ A[i]:
- A[i] + A[k]  - A[k] + A[j] = - A[i] + A[j]
=> A[j] - A[i] =  A[j] − A[i].

From the above equations, it is clear that if value of A[i] and A[j] are not the extreme values of the array then the probability of the equation being satisfied depends on the value of A[k] and will not hold true when A[k] lies outside the range of [A[i], A[j]].

Based on the above observation it is clear that the value of A[i] and A[j] should be the maximum and the minimum among the array elements. Follow the below steps to solve the problem:

  • Traverse the array from k = 0 to N-1:
    • Update the index of the maximum element (say i) if A[k] is greater than A[i].
    • Update the index of the minimum element (say j) if A[k] is less than A[j].
  • Return the pair (i, j) as the answer.

Below is the implementation of the above approach.

C++
// C++ program for above approach

#include <bits/stdc++.h>
using namespace std;

// Function to find the pair
pair<int, int> findPair(int N,
                        vector<int> vec)
{
    // Maximum element of the array
    int maxi = *max_element(vec.begin(),
                            vec.end());

    // Minimum element of the array
    int mini = *min_element(vec.begin(),
                            vec.end());

    int idx1 = 0;
    int idx2 = 0;

    // Loop to find index of maximum element
    for (int i = 0; i < N; i++) {
        if (vec[i] == maxi)
            idx1 = i;
    }

    // Loop to find index of minimum element
    for (int i = 0; i < N; i++) {
        if (vec[i] == mini)
            idx2 = i;
    }

    return { idx2, idx1 };
}

// Driver code
int main()
{
    int N = 3;
    vector<int> vec{ 2, 7, 5 };

    // Function call
    pair<int, int> ans = findPair(N, vec);
    cout << ans.first << " " << ans.second;
    return 0;
}
Java
// Java program for above approach
import java.io.*;

class GFG 
{

  // Function to find the pair
  public static int[] findPair(int N, int vec[])
  {

    // Maximum element of the array
    int maxi = Integer.MIN_VALUE;
    for (int i = 0; i < N; i++) {
      maxi = Math.max(maxi, vec[i]);
    }
    // Minimum element of the array
    int mini = Integer.MAX_VALUE;
    for (int i = 0; i < N; i++) {
      mini = Math.min(mini, vec[i]);
    }

    int idx1 = 0;
    int idx2 = 0;

    // Loop to find index of maximum element
    for (int i = 0; i < N; i++) {
      if (vec[i] == maxi)
        idx1 = i;
    }

    // Loop to find index of minimum element
    for (int i = 0; i < N; i++) {
      if (vec[i] == mini)
        idx2 = i;
    }
    int res[] = { idx2, idx1 };

    return res;
  }
  public static void main(String[] args)
  {
    int N = 3;
    int vec[] = { 2, 7, 5 };

    // Function call
    int ans[] = findPair(N, vec);
    System.out.print(ans[0] + " " + ans[1]);
  }
}

// This code is contributed by Rohit Pradhan
Python3
# Python program for above approach

# Function to find the pair
def findPair(N, vec):

    # Maximum element of the array
    maxi = max(vec)

    # Minimum element of the array
    mini = min(vec)

    idx1 = 0
    idx2 = 0

    # Loop to find index of maximum element
    for i in range(N): 
        if (vec[i] == maxi):
            idx1 = i

# Loop to find index of minimum element
    for i in range(N):
        if (vec[i] == mini):
            idx2 = i

    return [idx2, idx1]

# Driver code
N = 3
vec = [2, 7, 5]

# Function call
ans = findPair(N, vec)
print(f"{ans[0]} {ans[1]}")

# This code is contributed by shinjanpatra
C#
// C# program for above approach
using System;

public class GFG{

  // Function to find the pair
  public static int[] findPair(int N, int[] vec)
  {

    // Maximum element of the array
    int maxi = Int32.MinValue;
    for (int i = 0; i < N; i++) {
      maxi = Math.Max(maxi, vec[i]);
    }
    // Minimum element of the array
    int mini = Int32.MaxValue;
    for (int i = 0; i < N; i++) {
      mini = Math.Min(mini, vec[i]);
    }

    int idx1 = 0;
    int idx2 = 0;

    // Loop to find index of maximum element
    for (int i = 0; i < N; i++) {
      if (vec[i] == maxi)
        idx1 = i;
    }

    // Loop to find index of minimum element
    for (int i = 0; i < N; i++) {
      if (vec[i] == mini)
        idx2 = i;
    }
    int[] res = { idx2, idx1 };

    return res;
  }
    static public void Main (){

        int N = 3;
        int[] vec = { 2, 7, 5 };

        // Function call
        int[] ans = findPair(N, vec);
        Console.Write(ans[0] + " " + ans[1]);
    }
}

// This code is contributed by hrithikgarg03188.
JavaScript
    <script>
        // JavaScript program for above approach

        // Function to find the pair
        const findPair = (N, vec) => {
        
            // Maximum element of the array
            let maxi = Math.max(...vec);

            // Minimum element of the array
            let mini = Math.min(...vec);

            let idx1 = 0;
            let idx2 = 0;

            // Loop to find index of maximum element
            for (let i = 0; i < N; i++) {
                if (vec[i] == maxi)
                    idx1 = i;
            }

            // Loop to find index of minimum element
            for (let i = 0; i < N; i++) {
                if (vec[i] == mini)
                    idx2 = i;
            }

            return [idx2, idx1];
        }

        // Driver code
        let N = 3;
        let vec = [2, 7, 5];

        // Function call
        let ans = findPair(N, vec);
        document.write(`${ans[0]} ${ans[1]}`);

    // This code is contributed by rakeshsahni

    </script>

Output
0 1

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


Similar Reads