Maximum possible remainder when an element is divided by other element in the array
Last Updated :
13 Aug, 2021
Given an array arr[] of N integers, the task is to find the maximum mod value for any pair (arr[i], arr[j]) from the array.
Examples:
Input: arr[] = {2, 4, 1, 5, 3, 6}
Output: 5
(5 % 6) = 5 is the maximum possible mod value.
Input: arr[] = {6, 6, 6, 6}
Output: 0
Approach: It is known that when an integer is divided by some other integer X, the remainder will always be less than X. So, the maximum mod value which can be obtained from the array will be when the divisor is the maximum element from the array and this value will be maximum when the dividend is the maximum among the remaining elements i.e. the second maximum element from the array which is the required answer. Note that the result will be 0 when all the elements of the array are equal.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int maxMod( int arr[], int n)
{
int maxVal = *max_element(arr, arr + n);
int secondMax = 0;
for ( int i = 0; i < n; i++) {
if (arr[i] < maxVal
&& arr[i] > secondMax) {
secondMax = arr[i];
}
}
return secondMax;
}
int main()
{
int arr[] = { 2, 4, 1, 5, 3, 6 };
int n = sizeof (arr) / sizeof ( int );
cout << maxMod(arr, n);
return 0;
}
|
Java
class GFG
{
static int max_element( int arr[], int n)
{
int max = arr[ 0 ];
for ( int i = 1 ; i < n ; i++)
{
if (max < arr[i])
max = arr[i];
}
return max;
}
static int maxMod( int arr[], int n)
{
int maxVal = max_element(arr, n);
int secondMax = 0 ;
for ( int i = 0 ; i < n; i++)
{
if (arr[i] < maxVal &&
arr[i] > secondMax)
{
secondMax = arr[i];
}
}
return secondMax;
}
public static void main (String[] args)
{
int arr[] = { 2 , 4 , 1 , 5 , 3 , 6 };
int n = arr.length;
System.out.println(maxMod(arr, n));
}
}
|
Python3
def maxMod(arr, n):
maxVal = max (arr)
secondMax = 0
for i in range ( 0 , n):
if (arr[i] < maxVal and
arr[i] > secondMax):
secondMax = arr[i]
return secondMax
arr = [ 2 , 4 , 1 , 5 , 3 , 6 ]
n = len (arr)
print (maxMod(arr, n))
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int max_element( int []arr, int n)
{
int max = arr[0];
for ( int i = 1; i < n ; i++)
{
if (max < arr[i])
max = arr[i];
}
return max;
}
static int maxMod( int []arr, int n)
{
int maxVal = max_element(arr, n);
int secondMax = 0;
for ( int i = 0; i < n; i++)
{
if (arr[i] < maxVal &&
arr[i] > secondMax)
{
secondMax = arr[i];
}
}
return secondMax;
}
public static void Main (String[] args)
{
int []arr = { 2, 4, 1, 5, 3, 6 };
int n = arr.Length;
Console.WriteLine(maxMod(arr, n));
}
}
|
Javascript
<script>
function maxMod(arr, n) {
let maxVal = arr.sort((a, b) => b - a)[0]
let secondMax = 0;
for (let i = 0; i < n; i++) {
if (arr[i] < maxVal
&& arr[i] > secondMax) {
secondMax = arr[i];
}
}
return secondMax;
}
let arr = [2, 4, 1, 5, 3, 6];
let n = arr.length;
document.write(maxMod(arr, n));
</script>
|
Time Complexity: O(N).
Auxiliary Space: O(1).
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...