📌  相关文章
📜  将元素除以数组中的其他元素时的最大可能余数

📅  最后修改于: 2021-04-24 15:41:52             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是从数组中找到任意一对(arr [i],arr [j])的最大mod值。

例子:

方法:已知当一个整数除以某个其他整数X时,余数将始终小于X。因此,可以从数组获得的最大mod值将是除数是数组中的最大元素,而当除数是剩余元素中的最大值(即数组中的第二个最大元素)时,该值将是最大值。是必需的答案。请注意,当数组的所有元素相等时,结果将为0

下面是上述方法的实现:

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


输出:
5