📌  相关文章
📜  总和不能被 K 整除的最长可能子数组的计数

📅  最后修改于: 2021-09-06 06:43:06             🧑  作者: Mango

给定一个整数数组arr[]和一个正整数K ,任务是找到最长可能子数组的计数,其元素之和不能被K整除。

例子:

方法:

  1. 检查数组所有元素的总和是否可以被 K 整除
  2. 如果总和不能被 K 整除,则返回 1,因为最长的子数组的大小为 N。
  3. 别的
    • 找到第一个不能被 K 整除的数字的索引。设它是L
    • 找到最后一个不能被 K 整除的数字的索引。设它是R
    • 删除所有元素直到索引 L 并找到子数组的大小。删除 R 之外的元素,并找出该子数组的大小。哪个长度更大,那将是不能被 K 整除的最长子数组的大小。
    • 使用这个长度作为窗口大小,在 arr[] 上应用滑动窗口技术来找出上面得到的不能被 K 整除的大小的子数组的数量。

下面是上述方法的实现:

C++
// C++ program for the above problem
 
#include 
using namespace std;
 
// Function to find the count of
// longest subarrays with sum not
// divisible by K
int CountLongestSubarrays(
    int arr[], int n, int k)
{
 
    // Sum of all elements in
    // an array
    int i, s = 0;
    for (i = 0; i < n; ++i) {
        s += arr[i];
    }
 
    // If overall sum is not
    // divisible then return
    // 1, as only one subarray
    // of size n is possible
    if (s % k) {
        return 1;
    }
    else {
        int ini = 0;
 
        // Index of the first number
        // not divisible by K
        while (ini < n
               && arr[ini] % k == 0) {
            ++ini;
        }
 
        int final = n - 1;
 
        // Index of the last number
        // not divisible by K
        while (final >= 0
               && arr[final] % k == 0) {
            --final;
        }
 
        int len, sum = 0, count = 0;
        // Subarray doesn't exist
        if (ini == n) {
            return -1;
        }
        else {
            len = max(n - 1 - ini,
                      final);
        }
 
        // Sum of the window
        for (i = 0; i < len; i++) {
            sum += arr[i];
        }
 
        if (sum % k != 0) {
            count++;
        }
        // Calculate the sum of rest of
        // the windows of size len
        for (i = len; i < n; i++) {
            sum = sum + arr[i];
            sum = sum - arr[i - len];
            if (sum % k != 0) {
                count++;
            }
        }
        return count;
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 3, 2, 2, 2, 3 };
    int n = sizeof(arr)
            / sizeof(arr[0]);
    int k = 3;
    cout << CountLongestSubarrays(arr, n, k);
    return 0;
}


Java
// Java program for the above problem
import java.util.*;
 
class GFG{
 
// Function to find the count of
// longest subarrays with sum not
// divisible by K
static int CountLongestSubarrays(int arr[],
                                int n, int k)
{
     
    // Sum of all elements in
    // an array
    int i, s = 0;
    for(i = 0; i < n; ++i)
    {
    s += arr[i];
    }
 
    // If overall sum is not
    // divisible then return
    // 1, as only one subarray
    // of size n is possible
    if ((s % k) != 0)
    {
        return 1;
    }
    else
    {
        int ini = 0;
 
        // Index of the first number
        // not divisible by K
        while (ini < n && arr[ini] % k == 0)
        {
            ++ini;
        }
 
        int fin = n - 1;
 
        // Index of the last number
        // not divisible by K
        while (fin >= 0 && arr[fin] % k == 0)
        {
            --fin;
        }
 
        int len, sum = 0, count = 0;
         
        // Subarray doesn't exist
        if (ini == n)
        {
            return -1;
        }
        else
        {
            len = Math.max(n - 1 - ini, fin);
        }
 
        // Sum of the window
        for(i = 0; i < len; i++)
        {
        sum += arr[i];
        }
 
        if (sum % k != 0)
        {
            count++;
        }
         
        // Calculate the sum of rest of
        // the windows of size len
        for(i = len; i < n; i++)
        {
        sum = sum + arr[i];
        sum = sum - arr[i - len];
        if (sum % k != 0)
        {
            count++;
        }
        }
        return count;
    }
}
 
// Driver Code
public static void main (String []args)
{
    int arr[] = { 3, 2, 2, 2, 3 };
    int n = arr.length;
    int k = 3;
     
    System.out.print(CountLongestSubarrays(
                    arr, n, k));
}
}
 
// This code is contributed by chitranayal


Python3
# Python3 program for the above problem
 
# Function to find the count of
# longest subarrays with sum not
# divisible by K
def CountLongestSubarrays(arr, n, k):
 
    # Sum of all elements in
    # an array
    s = 0
    for i in range(n):
        s += arr[i]
 
    # If overall sum is not
    # divisible then return
    # 1, as only one subarray
    # of size n is possible
    if(s % k):
        return 1
    else:
        ini = 0
 
        # Index of the first number
        # not divisible by K
        while (ini < n and arr[ini] % k == 0):
            ini += 1
        final = n - 1
 
        # Index of the last number
        # not divisible by K
        while (final >= 0 and arr[final] % k == 0):
            final -= 1
             
        sum, count = 0, 0
         
        # Subarray doesn't exist
        if(ini == n):
            return -1
        else:
            length = max(n - 1 - ini, final)
 
        # Sum of the window
        for i in range(length):
            sum += arr[i]
 
        if(sum % k != 0):
            count += 1
 
        # Calculate the sum of rest of
        # the windows of size len
        for i in range(length, n):
            sum = sum + arr[i]
            sum = sum + arr[i - length]
            if (sum % k != 0):
                count += 1
 
        return count
 
# Driver Code
if __name__ == '__main__':
 
    arr = [ 3, 2, 2, 2, 3 ]
    n = len(arr)
    k = 3
 
    print(CountLongestSubarrays(arr, n, k))
     
# This code is contributed by Shivam Singh


C#
// C# program for the above problem
using System;
 
class GFG{
 
// Function to find the count of
// longest subarrays with sum not
// divisible by K
static int CountLongestSubarrays(int[] arr,
                                 int n, int k)
{
     
    // Sum of all elements in
    // an array
    int i, s = 0;
    for(i = 0; i < n; ++i)
    {
       s += arr[i];
    }
 
    // If overall sum is not
    // divisible then return
    // 1, as only one subarray
    // of size n is possible
    if ((s % k) != 0)
    {
        return 1;
    }
    else
    {
        int ini = 0;
 
        // Index of the first number
        // not divisible by K
        while (ini < n && arr[ini] % k == 0)
        {
            ++ini;
        }
 
        int fin = n - 1;
 
        // Index of the last number
        // not divisible by K
        while (fin >= 0 && arr[fin] % k == 0)
        {
            --fin;
        }
 
        int len, sum = 0, count = 0;
 
        // Subarray doesn't exist
        if (ini == n)
        {
            return -1;
        }
        else
        {
            len = Math.Max(n - 1 - ini, fin);
        }
 
        // Sum of the window
        for(i = 0; i < len; i++)
        {
           sum += arr[i];
        }
 
        if (sum % k != 0)
        {
            count++;
        }
 
        // Calculate the sum of rest of
        // the windows of size len
        for(i = len; i < n; i++)
        {
           sum = sum + arr[i];
           sum = sum - arr[i - len];
           if (sum % k != 0)
           {
               count++;
           }
        }
        return count;
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int[] arr = { 3, 2, 2, 2, 3 };
    int n = arr.Length;
    int k = 3;
 
    Console.WriteLine(CountLongestSubarrays(
                      arr, n, k));
}
}
 
// This code is contributed by jrishabh99


Javascript


输出:
2

时间复杂度: O(N)
辅助空间复杂度: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live