📌  相关文章
📜  没有可被 K 整除的对和的子数组

📅  最后修改于: 2021-10-28 01:44:53             🧑  作者: Mango

给定一个由 N 个非负整数组成的数组,任务是找到一个子数组的最大大小,使得这个子数组的元素的成对和不能被给定的整数 K 整除。另外,打印这个子数组。如果有两个或多个子数组符合上述条件,则打印左边的第一个。
先决条件:没有可被 K 整除的对和的子集
例子 :

Input : arr[] = [3, 7, 1, 9, 2]        
        K = 3
Output : 3
         [3, 7, 1]
3 + 7 = 10, 3 + 1 = 4, 7 + 1 = 8, all are 
not divisible by 3. 
It is not possible to get a subarray of size bigger
than 3 with the above-mentioned property.
[7, 1, 9] is also of the same size but [3, 7, 1] comes first.

Input : arr[] = [2, 4, 4, 3]        
        K = 4
Output : 2
         [2, 4]
2 + 4 = 6 is not divisible by 4. 
It is not possible to get a subarray of size bigger 
than 2 with the above-mentioned property.
[4, 3] is also of the same size but [2, 4] comes first.

天真的方法:
天真的方法是考虑所有子数组。在考虑子数组时,成对取元素并计算该对中两个元素的总和。如果计算出的总和可被 K 整除,则忽略此子数组并继续下一个子数组。否则,以类似的方式计算此子数组的其他对的总和。如果没有对的和是 K 的倍数,则将此子数组的大小与迄今为止获得的最大大小进行比较,并在需要时进行更新。
该方法的时间复杂度为 O( n^4   )。
有效的方法(使用散列):
我们创建一个空的哈希表并将 arr[0] % k 插入其中。现在我们遍历剩余的元素并维护一个窗口,使得窗口中的任何对都不能被 k 整除。对于每个遍历的元素,当当前窗口中存在一个元素与当前元素形成可整除对时,我们删除起始元素。要检查当前窗口中是否有元素,我们检查是否跟随。
1) 如果有一个元素 x 使得 (K – x % K) 等于 arr[i] % K
2) OR arr[i] % k 是 0 并且它存在于散列中。
一旦我们确保删除了所有可以与 arr[i] 配对的元素,我们将 arr[i] 添加到当前窗口并检查当前窗口的大小是否大于迄今为止的最大窗口。

C++
// CPP code to find the subarray with
// no pair sum divisible by K
#include
using namespace std;
 
// function to find the subarray with
// no pair sum divisible by k
void subarrayDivisibleByK(int arr[], int n, int k)
{
    // hash table to store the remainders
    // obtained on dividing by K
    map mp;
 
    // s : starting index of the
    // current subarray, e : ending
    // index of the current subarray, maxs :
    // starting index of the maximum
    // size subarray so far, maxe : ending
    // index of the maximum size subarray
    // so far
    int s = 0, e = 0, maxs = 0, maxe = 0;
 
    // insert the first element in the set
    mp[arr[0] % k]++;
 
    for (int i = 1; i < n; i++)
    {
        int mod = arr[i] % k;
 
        // Removing starting elements of current
        // subarray while there is an element in
        // set which makes a pair with mod[i] such
        // that the pair sum is divisible.
        while (mp[k - mod] != 0 ||
              (mod == 0 && mp[mod] != 0))
        {
            mp[arr[s] % k]--;
            s++;
        }
 
        // include the current element in
        // the current subarray the ending
        // index of the current subarray
        // increments by one
        mp[mod]++;
        e++;
 
        // compare the size of the current
        // subarray with the maximum size so
        // far
        if ((e - s) > (maxe - maxs))
        {
            maxe = e;
            maxs = s;
        }
 
    }
 
    cout << "The maximum size is "
         << maxe - maxs + 1 << " and "
         "the subarray is as follows\n";
 
    for (int i=maxs; i<=maxe; i++)
        cout << arr[i] << " ";
}
 
int main()
{
    int k = 3;
    int arr[] = {5, 10, 15, 20, 25};
    int n = sizeof(arr)/sizeof(arr[0]);
    subarrayDivisibleByK(arr, n, k);
    return 0;
}


Java
// Java Program to find the subarray with
// no pair sum divisible by K
import java.io.*;
import java.util.*;
 
public class GFG {
     
    // function to find the subarray with
    // no pair sum divisible by k
    static void subarrayDivisibleByK(int []arr,
                                int n, int k)
    {
         
        // hash table to store the remainders
        // obtained on dividing by K
        int []mp = new int[1000];
     
        // s : starting index of the
        // current subarray, e : ending
        // index of the current subarray, maxs :
        // starting index of the maximum
        // size subarray so far, maxe : ending
        // index of the maximum size subarray
        // so far
        int s = 0, e = 0, maxs = 0, maxe = 0;
     
        // insert the first element in the set
        mp[arr[0] % k]++;
     
        for (int i = 1; i < n; i++)
        {
            int mod = arr[i] % k;
     
            // Removing starting elements of current
            // subarray while there is an element in
            // set which makes a pair with mod[i] such
            // that the pair sum is divisible.
            while (mp[k - mod] != 0 ||
                (mod == 0 && mp[mod] != 0))
            {
                mp[arr[s] % k]--;
                s++;
            }
     
            // include the current element in
            // the current subarray the ending
            // index of the current subarray
            // increments by one
            mp[mod]++;
            e++;
     
            // compare the size of the current
            // subarray with the maximum size so
            // far
            if ((e - s) > (maxe - maxs))
            {
                maxe = e;
                maxs = s;
            }
     
        }
     
        System.out.print("The maximum size is "
                            + (maxe - maxs + 1)
        + " and the subarray is as follows\n");
     
        for (int i = maxs; i <= maxe; i++)
            System.out.print(arr[i] + " ");
    }
     
    // Driver Code
    public static void main(String args[])
    {
        int k = 3;
        int []arr = {5, 10, 15, 20, 25};
        int n = arr.length;
        subarrayDivisibleByK(arr, n, k);
    }
}
 
// This code is contributed by
// Manish Shaw (manishshaw1)


Python3
# Python3 Program to find the subarray with
# no pair sum divisible by K
 
# function to find the subarray with
# no pair sum divisible by k
def subarrayDivisibleByK(arr, n, k) :
     
    # hash table to store the remainders
    # obtained on dividing by K
    mp = [0] * 1000
 
    # s : starting index of the
    # current subarray, e : ending
    # index of the current subarray, maxs :
    # starting index of the maximum
    # size subarray so far, maxe : ending
    # index of the maximum size subarray
    # so far
    s = 0; e = 0; maxs = 0; maxe = 0;
 
    # insert the first element in the set
    mp[arr[0] % k] = mp[arr[0] % k] + 1;
 
    for i in range(1, n):
        mod = arr[i] % k
 
        # Removing starting elements of current
        # subarray while there is an element in
        # set which makes a pair with mod[i] such
        # that the pair sum is divisible.
        while (mp[k - mod] != 0 or (mod == 0
                            and mp[mod] != 0)) :
            mp[arr[s] % k] = mp[arr[s] % k] - 1
            s = s + 1
 
        # include the current element in
        # the current subarray the ending
        # index of the current subarray
        # increments by one
        mp[mod] = mp[mod] + 1
        e = e + 1
 
        # compare the size of the current
        # subarray with the maximum size so
        # far
        if ((e - s) > (maxe - maxs)) :
            maxe = e
            maxs = s
 
    print ("The maximum size is {} and the "
                   " subarray is as follows"
                   .format((maxe - maxs + 1)))
 
    for i in range(maxs, maxe + 1) :
        print ("{} ".format(arr[i]), end="")
 
# Driver Code
k = 3
arr = [5, 10, 15, 20, 25]
n = len(arr)
subarrayDivisibleByK(arr, n, k)
 
# This code is contributed by
# Manish Shaw (manishshaw1)


C#
// C# Program to find the subarray with
// no pair sum divisible by K
using System;
using System.Collections;
 
class GFG {
     
    // function to find the subarray with
    // no pair sum divisible by k
    static void subarrayDivisibleByK(int []arr,
                                int n, int k)
    {
         
        // hash table to store the remainders
        // obtained on dividing by K
        int []mp = new int[1000];
     
        // s : starting index of the
        // current subarray, e : ending
        // index of the current subarray, maxs :
        // starting index of the maximum
        // size subarray so far, maxe : ending
        // index of the maximum size subarray
        // so far
        int s = 0, e = 0, maxs = 0, maxe = 0;
     
        // insert the first element in the set
        mp[arr[0] % k]++;
     
        for (int i = 1; i < n; i++)
        {
            int mod = arr[i] % k;
     
            // Removing starting elements of current
            // subarray while there is an element in
            // set which makes a pair with mod[i] such
            // that the pair sum is divisible.
            while (mp[k - mod] != 0 ||
                (mod == 0 && mp[mod] != 0))
            {
                mp[arr[s] % k]--;
                s++;
            }
     
            // include the current element in
            // the current subarray the ending
            // index of the current subarray
            // increments by one
            mp[mod]++;
            e++;
     
            // compare the size of the current
            // subarray with the maximum size so
            // far
            if ((e - s) > (maxe - maxs))
            {
                maxe = e;
                maxs = s;
            }
     
        }
     
        Console.Write("The maximum size is " +
                           (maxe - maxs + 1) +
            " and the subarray is as follows\n");
     
        for (int i = maxs; i <= maxe; i++)
            Console.Write(arr[i] + " ");
    }
     
    // Driver Code
    public static void Main()
    {
        int k = 3;
        int []arr = {5, 10, 15, 20, 25};
        int n = arr.Length;
        subarrayDivisibleByK(arr, n, k);
    }
}
 
// This code is contributed by
// Manish Shaw (manishshaw1)


PHP
 ($maxe - $maxs))
        {
            $maxe = $e;
            $maxs = $s;
        }
 
    }
 
    echo ("The maximum size is ".
             ($maxe - $maxs + 1).
          " and the subarray is".
                " as follows\n");
 
    for ($i = $maxs; $i <= $maxe; $i++)
        echo ($arr[$i]." ");
}
 
// Driver Code
$k = 3;
$arr = array(5, 10, 15, 20, 25);
$n = count($arr);
subarrayDivisibleByK($arr, $n, $k);
 
// This code is contributed by
// Manish Shaw (manishshaw1)
?>


Javascript


输出 :
The maximum size is 2 and the subarray is as follows
10 15

时间复杂度: O(n)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程