📌  相关文章
📜  arr[i] >= arr[j] 的所有数组对的最大模数

📅  最后修改于: 2022-05-13 01:57:48.209000             🧑  作者: Mango

arr[i] >= arr[j] 的所有数组对的最大模数

给定一个包含 n 个整数的数组。求 arr[i] mod arr[j] 的最大值,其中 arr[i] >= arr[j] and 1 <= i, j <= n
例子:

Input: arr[] = {3, 4, 7}
Output: 3
Explanation:
There are 3 pairs which satisfies arr[i] >= arr[j] are:-
4, 3 => 4 % 3 = 1
7, 3 => 7 % 3 = 1
7, 4 => 7 % 4 = 3
Hence Maximum value among all is 3.

Input: arr[] = {3, 7, 4, 11}
Output: 4

Input: arr[] = {4, 4, 4}
Output: 0

一种朴素的方法是运行两个嵌套的 for 循环,并在取模后选择每个可能对的最大值。这种方法的时间复杂度将是 O(n 2 ),这对于较大的 n 值是不够的。
一种有效的方法(当元素来自小范围时)是使用排序和二进制搜索方法。首先,我们将对数组进行排序,以便我们能够对其应用二进制搜索。由于我们需要最大化 arr[i] mod arr[j] 的值,所以我们遍历从 2*arr[j] 到 M+arr[j] 范围内的每个 x(例如 x 可被 arr[j] 整除),其中 M 是序列的最大值。对于 x 的每个值,我们需要找到 arr[i] 的最大值,使得 arr[i] < x。
通过这样做,我们将确保我们只选择了那些将给出 arr[i] mod arr[j] 最大值的 arr[i] 值。之后,我们只需对 arr[j] 的其他值重复上述过程,并通过值 a[i] mod arr[j] 更新答案。例如:-

If arr[] = {4, 6, 7, 8, 10, 12, 15} then for
first element, i.e., arr[j] = 4 we iterate
through x = {8, 12, 16}. 
Therefore for each value of x, a[i] will be:-
x = 8, arr[i] = 7 (7 < 8)
       ans = 7 mod 4 = 3 
x = 12, arr[i] = 10 (10 < 12)
       ans = 10 mod 4 = 2 (Since 2 < 3, 
                                No update)
x = 16, arr[i] = 15 (15 < 16)
       ans  = 15 mod 4 = 3 (Since 3 == 3,  
                        No need to update)

C++
// C++ program to find Maximum modulo value
#include 
using namespace std;
 
int maxModValue(int arr[], int n)
{
    int ans = 0;
    // Sort the array[] by using inbuilt sort function
    sort(arr, arr + n);
 
    for (int j = n - 2; j >= 0; --j) {
        // Break loop if answer is greater or equals to
        // the arr[j] as any number modulo with arr[j]
        // can only give maximum value up-to arr[j]-1
        if (ans >= arr[j])
            break;
 
        // If both elements are same then skip the next
        // loop as it would be worthless to repeat the
        // rest process for same value
        if (arr[j] == arr[j + 1])
            continue;
 
        for (int i = 2 * arr[j]; i <= arr[n - 1] + arr[j]; i += arr[j]) {
            // Fetch the index which is greater than or
            // equals to arr[i] by using binary search
            // inbuilt lower_bound() function of C++
            int ind = lower_bound(arr, arr + n, i) - arr;
 
            // Update the answer
            ans = max(ans, arr[ind - 1] % arr[j]);
        }
    }
    return ans;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 4, 5, 9, 11 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << maxModValue(arr, n);
}


Java
// Java program to find Maximum modulo value
 
import java.util.Arrays;
 
class Test {
    static int maxModValue(int arr[], int n)
    {
        int ans = 0;
 
        // Sort the array[] by using inbuilt sort function
        Arrays.sort(arr);
 
        for (int j = n - 2; j >= 0; --j) {
            // Break loop if answer is greater or equals to
            // the arr[j] as any number modulo with arr[j]
            // can only give maximum value up-to arr[j]-1
            if (ans >= arr[j])
                break;
 
            // If both elements are same then skip the next
            // loop as it would be worthless to repeat the
            // rest process for same value
            if (arr[j] == arr[j + 1])
                continue;
 
            for (int i = 2 * arr[j]; i <= arr[n - 1] + arr[j]; i += arr[j]) {
                // Fetch the index which is greater than or
                // equals to arr[i] by using binary search
 
                int ind = Arrays.binarySearch(arr, i);
 
                if (ind < 0)
                    ind = Math.abs(ind + 1);
 
                else {
                    while (arr[ind] == i) {
                        ind--;
 
                        if (ind == 0) {
                            ind = -1;
                            break;
                        }
                    }
                    ind++;
                }
 
                // Update the answer
                ans = Math.max(ans, arr[ind - 1] % arr[j]);
            }
        }
        return ans;
    }
 
    // Driver method
    public static void main(String args[])
    {
        int arr[] = { 3, 4, 5, 9, 11 };
        System.out.println(maxModValue(arr, arr.length));
    }
}


Python3
# Python3 program to find Maximum modulo value
 
def maxModValue(arr, n):
 
    ans = 0
     
    # Sort the array[] by using inbuilt
    # sort function
    arr = sorted(arr)
 
    for j in range(n - 2, -1, -1):
         
        # Break loop if answer is greater or equals to
        # the arr[j] as any number modulo with arr[j]
        # can only give maximum value up-to arr[j]-1
        if (ans >= arr[j]):
            break
 
        # If both elements are same then skip the next
        # loop as it would be worthless to repeat the
        # rest process for same value
        if (arr[j] == arr[j + 1]) :
            continue
        i = 2 * arr[j]
        while(i <= arr[n - 1] + arr[j]):
             
            # Fetch the index which is greater than or
            # equals to arr[i] by using binary search
            # inbuilt lower_bound() function of C++
            ind = 0
            for k in arr:
                if k >= i:
                    ind = arr.index(k)
 
            # Update the answer
            ans = max(ans, arr[ind - 1] % arr[j])
            i += arr[j]
 
    return ans
 
# Driver Code
arr = [3, 4, 5, 9, 11 ]
n = 5
print(maxModValue(arr, n))
 
# This code is contributed by
# Shubham Singh(SHUBHAMSINGH10)


C#
// C# program to find Maximum modulo value
using System;
 
public class GFG {
     
    static int maxModValue(int[] arr, int n)
    {
         
        int ans = 0;
 
        // Sort the array[] by using inbuilt
        // sort function
        Array.Sort(arr);
 
        for (int j = n - 2; j >= 0; --j)
        {
             
            // Break loop if answer is greater
            // or equals to the arr[j] as any
            // number modulo with arr[j] can
            // only give maximum value up-to
            // arr[j]-1
            if (ans >= arr[j])
                break;
 
            // If both elements are same then
            // skip the next loop as it would
            // be worthless to repeat the
            // rest process for same value
            if (arr[j] == arr[j + 1])
                continue;
 
            for (int i = 2 * arr[j];
                      i <= arr[n - 1] + arr[j];
                                   i += arr[j])
            {
                 
                // Fetch the index which is
                // greater than or equals to
                // arr[i] by using binary search
 
                int ind = Array.BinarySearch(arr, i);
 
                if (ind < 0)
                    ind = Math.Abs(ind + 1);
 
                else {
                    while (arr[ind] == i) {
                        ind--;
 
                        if (ind == 0) {
                            ind = -1;
                            break;
                        }
                    }
                    ind++;
                }
 
                // Update the answer
                ans = Math.Max(ans, arr[ind - 1]
                                       % arr[j]);
            }
        }
         
        return ans;
    }
 
    // Driver method
    public static void Main()
    {
         
        int[] arr = { 3, 4, 5, 9, 11 };
         
        Console.WriteLine(
                 maxModValue(arr, arr.Length));
    }
}
 
// This code is contributed by Sam007.


Javascript


输出:

4

时间复杂度: O(nlog(n) + Mlog(M)) 其中 n 是元素的总数,M 是所有元素的最大值。
辅助空间: O(1)
本博客由 Shubham Bansal 提供。如果您喜欢 GeeksforGeeks 并愿意做出贡献,您还可以使用 write.geeksforgeeks.org 撰写文章或将您的文章邮寄至 review-team@geeksforgeeks.org。在 GeeksforGeeks 主页上查看您的文章并帮助其他 Geeks。