Maximize remainder difference between two pairs in given Array
Given an array arr[] of size N, the task is to find 4 indices i, j, k, l such that 0 <= i, j, k, l < N and the value of arr[i]%arr[j] – arr[k]%arr[l] is maximum. Print the maximum difference. If it doesn’t exist, then print -1.
Examples:
Input: N=8, arr[] = {1, 2, 4, 6, 8, 3, 5, 7}
Output: 7
Explanation: Choosing elements 1, 2, 7, 8 and 2%1 – 7%8 gives the maximum result possible.
Input: N=3, arr[] = {1, 50, 101}
Output: -1
Explanation: Since, there are 3 elements only so there’s no possible answer.
Naive Approach: The brute force idea would be to check all the possible combinations and then find the maximum difference.
Time Complexity: O(N4)
Auxiliary Space: O(1)
Efficient Approach: The idea is based on the observation that on sorting the array in ascending order, choose the first pair from the left-side, i.e, the minimum 2 values and the second pair from the right side, i.e, the maximum 2 values gives the answer. Further, arr[i+1]%arr[i] is always less than equal to arr[i]%arr[i+1]. So, minimize the first pair value and maximize the second pair value. Follow the steps below to solve the problem:
Below is the implementation of the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void maxProductDifference(vector< int >& arr)
{
if (arr.size() < 4) {
cout << "-1\n" ;
return ;
}
sort(arr.begin(), arr.end());
int first = arr[1] % arr[0];
int second = arr[arr.size() - 2]
% arr[arr.size() - 1];
cout << second - first;
return ;
}
int main()
{
vector< int > arr = { 1, 2, 4, 6, 8, 3, 5, 7 };
maxProductDifference(arr);
return 0;
}
|
Java
import java.io.*;
import java.util.Arrays;
class GFG
{
static void maxProductDifference( int [] arr)
{
if (arr.length < 4 ) {
System.out.println( "-1" );
return ;
}
Arrays.sort(arr);
int first = arr[ 1 ] % arr[ 0 ];
int second
= arr[arr.length - 2 ] % arr[arr.length - 1 ];
System.out.println(second - first);
return ;
}
public static void main(String[] args)
{
int [] arr = { 1 , 2 , 4 , 6 , 8 , 3 , 5 , 7 };
maxProductDifference(arr);
}
}
|
Python3
def maxProductDifference(arr):
if ( len (arr) < 4 ):
print ( "-1" )
return
arr.sort()
first = arr[ 1 ] % arr[ 0 ]
second = arr[ len (arr) - 2 ] % arr[ len (arr) - 1 ]
print (second - first)
if __name__ = = "__main__" :
arr = [ 1 , 2 , 4 , 6 , 8 , 3 , 5 , 7 ]
maxProductDifference(arr)
|
C#
using System;
public class GFG
{
static void maxProductDifference( int [] arr)
{
if (arr.Length < 4) {
Console.WriteLine( "-1" );
return ;
}
Array.Sort(arr);
int first = arr[1] % arr[0];
int second
= arr[arr.Length - 2] % arr[arr.Length - 1];
Console.WriteLine(second - first);
return ;
}
public static void Main(String[] args)
{
int [] arr = { 1, 2, 4, 6, 8, 3, 5, 7 };
maxProductDifference(arr);
}
}
|
Javascript
<script>
function maxProductDifference(arr)
{
if (arr.length < 4) {
document.write( "-1<br>" );
return ;
}
arr.sort();
let first = arr[1] % arr[0];
let second = arr[arr.length - 2] % arr[arr.length - 1];
document.write(second - first);
return ;
}
let arr = [1, 2, 4, 6, 8, 3, 5, 7];
maxProductDifference(arr);
</script>
|
Time Complexity: O(N*log(N))
Auxiliary Space: O(1)
Last Updated :
09 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...