Skip to content
Related Articles
Maximum possible remainder when an element is divided by other element in the array
• Last Updated : 03 Jun, 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 % 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++

 `// C++ implementation of the approach``#include ``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

 `// 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

 `# 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#

 `// C# implementation of the approach``using` `System;``using` `System.Collections.Generic;``    ` `class` `GFG``{``    ``static` `int` `max_element(``int` `[]arr, ``int` `n)``    ``{``        ``int` `max = arr;``        ``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`

## Javascript

 ``
Output:
`5`

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up