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

📅  最后修改于: 2021-05-14 01:53:49             🧑  作者: 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


输出:
2

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