Find the maximum possible value of a[i] % a[j] over all pairs of i and j
Last Updated :
08 Jun, 2022
Given an array arr[] of N positive integers. The task is to find the maximum possible value of a[i] % a[j] over all pairs of i and j.
Examples:
Input: arr[] = {4, 5, 1, 8}
Output: 5
If we choose the pair (5, 8), then 5 % 8 gives us 5
which is the maximum possible.
Input: arr[] = {7, 7, 8, 8, 1}
Output: 7
Approach: Since we can choose any pair, hence arr[i] should be the second maximum of the array and arr[j] be the maximum element in order to maximize the required value. Hence, the second maximum over the array will be our answer. If there does not exist any second largest number, then 0 will be the answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int getMaxValue( int arr[], int arr_size)
{
int i, first, second;
if (arr_size < 2) {
return 0;
}
first = second = INT_MIN;
for (i = 0; i < arr_size; i++) {
if (arr[i] > first) {
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] != first)
second = arr[i];
}
if (second == INT_MIN)
return 0;
else
return second;
}
int main()
{
int arr[] = { 4, 5, 1, 8 };
int n = sizeof (arr) / sizeof (arr[0]);
cout << getMaxValue(arr, n);
return 0;
}
|
Java
class GFG
{
static int getMaxValue( int arr[], int arr_size)
{
int i, first, second;
if (arr_size < 2 )
{
return 0 ;
}
first = second = Integer.MIN_VALUE;
for (i = 0 ; i < arr_size; i++)
{
if (arr[i] > first)
{
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] != first)
{
second = arr[i];
}
}
if (second == Integer.MIN_VALUE)
{
return 0 ;
}
else
{
return second;
}
}
public static void main(String[] args)
{
int arr[] = { 4 , 5 , 1 , 8 };
int n = arr.length;
System.out.println(getMaxValue(arr, n));
}
}
|
Python3
import sys
def getMaxValue(arr,arr_size):
if (arr_size < 2 ):
return 0
first = - sys.maxsize - 1
second = - sys.maxsize - 1
for i in range (arr_size):
if (arr[i] > first):
second = first
first = arr[i]
elif (arr[i] > second and arr[i] ! = first):
second = arr[i]
if (second = = - sys.maxsize - 1 ):
return 0
else :
return second
if __name__ = = '__main__' :
arr = [ 4 , 5 , 1 , 8 ]
n = len (arr)
print (getMaxValue(arr, n))
|
C#
using System;
class GFG
{
static int getMaxValue( int []arr,
int arr_size)
{
int i, first, second;
if (arr_size < 2)
{
return 0;
}
first = second = int .MinValue;
for (i = 0; i < arr_size; i++)
{
if (arr[i] > first)
{
second = first;
first = arr[i];
}
else if (arr[i] > second &&
arr[i] != first)
{
second = arr[i];
}
}
if (second == int .MinValue)
{
return 0;
}
else
{
return second;
}
}
public static void Main()
{
int []arr = {4, 5, 1, 8};
int n = arr.Length;
Console.Write(getMaxValue(arr, n));
}
}
|
PHP
<?php
function getMaxValue( $arr , $arr_size )
{
if ( $arr_size < 2)
{
return 0;
}
$first = $second = -(PHP_INT_MAX - 1);
for ( $i = 0; $i < $arr_size ; $i ++)
{
if ( $arr [ $i ] > $first )
{
$second = $first ;
$first = $arr [ $i ];
}
else if ( $arr [ $i ] > $second &&
$arr [ $i ] != $first )
$second = $arr [ $i ];
}
if ( $second == -(PHP_INT_MAX-1))
return 0;
else
return $second ;
}
$arr = array (4, 5, 1, 8);
$n = count ( $arr );
echo getMaxValue( $arr , $n );
?>
|
Javascript
<script>
function getMaxValue(arr, arr_size)
{
let i, first, second;
if (arr_size < 2)
{
return 0;
}
first = second = Number.MIN_VALUE;
for (i = 0; i < arr_size; i++)
{
if (arr[i] > first)
{
second = first;
first = arr[i];
}
else if (arr[i] > second && arr[i] != first)
{
second = arr[i];
}
}
if (second == Number.MIN_VALUE)
{
return 0;
}
else
{
return second;
}
}
let arr = [4, 5, 1, 8];
let n = arr.length;
document.write(getMaxValue(arr, n));
</script>
|
Time Complexity: O(n) where n is the size of the array
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...