Wednesday, January 8, 2025
Google search engine
HomeData Modelling & AIFind the maximum possible value of a % a over all pairs...

Find the maximum possible value of a[i] % a[j] over all pairs of i and j

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:
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:
 

 

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++




// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
 
// Function that returns the second largest
// element in the array if exists, else 0
int getMaxValue(int arr[], int arr_size)
{
    int i, first, second;
 
    // There must be at least two elements
    if (arr_size < 2) {
        return 0;
    }
 
    // To store the maximum and the second
    // maximum element from the array
    first = second = INT_MIN;
    for (i = 0; i < arr_size; i++) {
 
        // If current element is greater than first
        // then update both first and second
        if (arr[i] > first) {
            second = first;
            first = arr[i];
        }
 
        // If arr[i] is in between first and
        // second then update second
        else if (arr[i] > second && arr[i] != first)
            second = arr[i];
    }
 
    // No second maximum found
    if (second == INT_MIN)
        return 0;
    else
        return second;
}
 
// Driver code
int main()
{
    int arr[] = { 4, 5, 1, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << getMaxValue(arr, n);
 
    return 0;
}


Java




// Java implementation of the approach
class GFG
{
 
    // Function that returns the second largest
    // element in the array if exists, else 0
    static int getMaxValue(int arr[], int arr_size)
    {
        int i, first, second;
 
        // There must be at least two elements
        if (arr_size < 2)
        {
            return 0;
        }
 
        // To store the maximum and the second
        // maximum element from the array
        first = second = Integer.MIN_VALUE;
        for (i = 0; i < arr_size; i++)
        {
 
            // If current element is greater than first
            // then update both first and second
            if (arr[i] > first)
            {
                second = first;
                first = arr[i];
            }
             
            // If arr[i] is in between first and
            // second then update second
            else if (arr[i] > second && arr[i] != first)
            {
                second = arr[i];
            }
        }
 
        // No second maximum found
        if (second == Integer.MIN_VALUE)
        {
            return 0;
        }
        else
        {
            return second;
        }
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {4, 5, 1, 8};
        int n = arr.length;
        System.out.println(getMaxValue(arr, n));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3




import sys
# Python 3 implementation of the approach
 
# Function that returns the second largest
# element in the array if exists, else 0
def getMaxValue(arr,arr_size):
     
    # There must be at least two elements
    if (arr_size < 2):
        return 0
 
    # To store the maximum and the second
    # maximum element from the array
    first = -sys.maxsize-1
    second = -sys.maxsize-1
    for i in range(arr_size):
         
        # If current element is greater than first
        # then update both first and second
        if (arr[i] > first):
            second = first
            first = arr[i]
 
        # If arr[i] is in between first and
        # second then update second
        elif (arr[i] > second and arr[i] != first):
            second = arr[i]
 
    # No second maximum found
    if (second == -sys.maxsize-1):
        return 0
    else:
        return second
 
# Driver code
if __name__ == '__main__':
    arr = [4, 5, 1, 8]
    n = len(arr)
    print(getMaxValue(arr, n))
 
# This code is contributed by
# Surendra_Gangwar


C#




// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function that returns the second largest
    // element in the array if exists, else 0
    static int getMaxValue(int []arr,
                           int arr_size)
    {
        int i, first, second;
 
        // There must be at least two elements
        if (arr_size < 2)
        {
            return 0;
        }
 
        // To store the maximum and the second
        // maximum element from the array
        first = second = int.MinValue;
        for (i = 0; i < arr_size; i++)
        {
 
            // If current element is greater than first
            // then update both first and second
            if (arr[i] > first)
            {
                second = first;
                first = arr[i];
            }
             
            // If arr[i] is in between first and
            // second then update second
            else if (arr[i] > second &&
                     arr[i] != first)
            {
                second = arr[i];
            }
        }
 
        // No second maximum found
        if (second == int.MinValue)
        {
            return 0;
        }
        else
        {
            return second;
        }
    }
 
    // Driver code
    public static void Main()
    {
        int []arr = {4, 5, 1, 8};
        int n = arr.Length;
        Console.Write(getMaxValue(arr, n));
    }
}
 
// This code is contributed by Akanksha Rai


PHP




<?php
// PHP implementation of the approach
 
// Function that returns the second largest
// element in the array if exists, else 0
function getMaxValue($arr, $arr_size)
{
    // There must be at least two elements
    if ($arr_size < 2)
    {
        return 0;
    }
 
    // To store the maximum and the second
    // maximum element from the array
    $first = $second = -(PHP_INT_MAX - 1);
    for ($i = 0; $i < $arr_size; $i++)
    {
 
        // If current element is greater than first
        // then update both first and second
        if ($arr[$i] > $first)
        {
            $second = $first;
            $first = $arr[$i];
        }
 
        // If arr[i] is in between first and
        // second then update second
        else if ($arr[$i] > $second &&
                 $arr[$i] != $first)
            $second = $arr[$i];
    }
 
    // No second maximum found
    if ($second == -(PHP_INT_MAX-1))
        return 0;
    else
        return $second;
}
 
// Driver code
$arr = array(4, 5, 1, 8);
$n = count($arr);
 
echo getMaxValue($arr, $n);
 
// This code is contributed by Ryuga
?>


Javascript




<script>
 
// JavaScript implementation of the approach
 
 // Function that returns the second largest
// element in the array if exists, else 0
    function getMaxValue(arr, arr_size)
    {
        let i, first, second;
   
        // There must be at least two elements
        if (arr_size < 2)
        {
            return 0;
        }
   
        // To store the maximum and the second
        // maximum element from the array
        first = second = Number.MIN_VALUE;
        for (i = 0; i < arr_size; i++)
        {
   
            // If current element is greater than first
            // then update both first and second
            if (arr[i] > first)
            {
                second = first;
                first = arr[i];
            }
               
            // If arr[i] is in between first and
            // second then update second
            else if (arr[i] > second && arr[i] != first)
            {
                second = arr[i];
            }
        }
   
        // No second maximum found
        if (second == Number.MIN_VALUE)
        {
            return 0;
        }
        else
        {
            return second;
        }
    }
       
 
// Driver Code
 
        let arr = [4, 5, 1, 8];
        let n = arr.length;
        document.write(getMaxValue(arr, n));
           
</script>


Output: 

5

 

Time Complexity: O(n) where n is the size of the array
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments