Open In App

Maximum possible remainder when an element is divided by other element in the array

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 % 6) = 5 is the maximum possible mod value.
Input: arr[] = {6, 6, 6, 6} 
Output:
 



 

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++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return the maximum mod
// value for any pair from the array
int maxMod(int arr[], int n)
{
    int maxVal = *max_element(arr, arr + n);
    int secondMax = 0;
 
    // Find the second maximum
    // element from the array
    for (int i = 0; i < n; i++) {
        if (arr[i] < maxVal
            && arr[i] > secondMax) {
            secondMax = arr[i];
        }
    }
 
    return secondMax;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 4, 1, 5, 3, 6 };
    int n = sizeof(arr) / sizeof(int);
 
    cout << maxMod(arr, n);
 
    return 0;
}




// Java implementation of the approach
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;
    }
     
    // Function to return the maximum mod
    // value for any pair from the array
    static int maxMod(int arr[], int n)
    {
        int maxVal = max_element(arr, n);
        int secondMax = 0;
     
        // Find the second maximum
        // element from the array
        for (int i = 0; i < n; i++)
        {
            if (arr[i] < maxVal &&
                arr[i] > secondMax)
            {
                secondMax = arr[i];
            }
        }
        return secondMax;
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int arr[] = { 2, 4, 1, 5, 3, 6 };
        int n = arr.length;
     
        System.out.println(maxMod(arr, n));
    }
}
 
// This code is contributed by AnkitRai01




# Python3 implementation of the approach
 
# Function to return the maximum mod
# value for any pair from the array
def maxMod(arr, n):
 
    maxVal = max(arr)
    secondMax = 0
 
    # Find the second maximum
    # element from the array
    for i in range(0, n):
        if (arr[i] < maxVal and
            arr[i] > secondMax):
            secondMax = arr[i]
 
    return secondMax
 
# Driver code
arr = [2, 4, 1, 5, 3, 6]
n = len(arr)
print(maxMod(arr, n))
 
# This code is contributed
# by Sanjit Prasad




// C# implementation of the approach
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;
    }
     
    // Function to return the maximum mod
    // value for any pair from the array
    static int maxMod(int []arr, int n)
    {
        int maxVal = max_element(arr, n);
        int secondMax = 0;
     
        // Find the second maximum
        // element from the array
        for (int i = 0; i < n; i++)
        {
            if (arr[i] < maxVal &&
                arr[i] > secondMax)
            {
                secondMax = arr[i];
            }
        }
        return secondMax;
    }
     
    // Driver code
    public static void Main (String[] args)
    {
        int []arr = { 2, 4, 1, 5, 3, 6 };
        int n = arr.Length;
     
        Console.WriteLine(maxMod(arr, n));
    }
}
 
// This code is contributed by 29AjayKumar




<script>
 
// JavaScript implementation of the approach
 
// Function to return the maximum mod
// value for any pair from the array
function maxMod(arr, n) {
    let maxVal = arr.sort((a, b) => b - a)[0]
    let secondMax = 0;
 
    // Find the second maximum
    // element from the array
    for (let i = 0; i < n; i++) {
        if (arr[i] < maxVal
            && arr[i] > secondMax) {
            secondMax = arr[i];
        }
    }
 
    return secondMax;
}
 
// Driver code
 
let arr = [2, 4, 1, 5, 3, 6];
let n = arr.length;
 
document.write(maxMod(arr, n));
 
</script>

Output: 
5

 

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


Article Tags :