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

📅  最后修改于: 2021-09-04 08:07:49             🧑  作者: Mango

给定一个大小为 N 的数组 arr[]和一个整数 k ,我们的任务是找到元素总和不能被 k 整除的最长子数组的长度。如果不存在这样的子数组,则返回 -1。
例子:

朴素的方法:这个想法是考虑所有子数组并返回最长子数组的长度,使其元素的总和不能被k整除。
时间复杂度: O(N 2 )
辅助空间: O(N)
有效的方法:主要观察结果是删除一个可被 k 整除的元素不会对解决方案有贡献,但如果我们删除一个不能被 k 整除的元素,那么总和将不能被 k 整除。

  • 因此,让k的最左边的非倍数位于索引left 处,而k的最右边的非倍数位于索引right 处
  • 删除最多 index left的前缀元素,或最多 index right的后缀元素,并删除元素数量较少的元素。
  • 这个问题有两个极端情况。首先,如果数组的每个元素都可以被 k 整除,则不存在这样的子数组,因此返回 -1。其次,如果整个数组的某些部分不能被 k 整除,那么子数组将是数组本身,因此返回数组的大小。

下面是上述方法的实现:

C++
// C++ Program to find the length of
// the longest subarray whose sum is
// not divisible by integer K
 
#include 
using namespace std;
 
// Function to find the longest subarray
// with sum is not divisible by k
int MaxSubarrayLength(int arr[], int n, int k)
{
    // left is the index of the
    // leftmost element that is
    // not divisible by k
    int left = -1;
 
    // right is the index of the
    // rightmost element that is
    // not divisible by k
    int right;
 
    // sum of the array
    int sum = 0;
 
    for (int i = 0; i < n; i++) {
 
        // Find the element that
        // is not multiple of k
        if ((arr[i] % k) != 0) {
 
            // left = -1 means we are
            // finding the leftmost
            // element that is not
            // divisible by k
            if (left == -1) {
                left = i;
            }
 
            // Updating the
            // rightmost element
            right = i;
        }
 
        // update the sum of the
        // array up to the index i
        sum += arr[i];
    }
 
    // Check if the sum of the
    // array is not divisible
    // by k, then return the
    // size of array
    if ((sum % k) != 0) {
        return n;
    }
 
    // All elements of array
    // are divisible by k,
    // then no such subarray
    // possible so return -1
    else if (left == -1) {
        return -1;
    }
 
    else {
        // length of prefix elements
        // that can be removed
        int prefix_length = left + 1;
 
        // length of suffix elements
        // that can be removed
        int suffix_length = n - right;
 
        // Return the length of
        // subarray after removing
        // the elements which have
        // lesser number of elements
        return n - min(prefix_length,
                       suffix_length);
    }
}
 
// Driver Code
int main()
{
 
    int arr[] = { 6, 3, 12, 15 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int K = 3;
 
    cout << MaxSubarrayLength(arr, n, K);
 
    return 0;
}


Java
// Java program to find the length of
// the longest subarray whose sum is
// not divisible by integer K
import java.util.*;
 
class GFG{
 
// Function to find the longest subarray
// with sum is not divisible by k
static int MaxSubarrayLength(int arr[], int n,
                                        int k)
{
     
    // left is the index of the
    // leftmost element that is
    // not divisible by k
    int left = -1;
 
    // right is the index of the
    // rightmost element that is
    // not divisible by k
    int right = 0;
 
    // sum of the array
    int sum = 0;
 
    for(int i = 0; i < n; i++)
    {
         
        // Find the element that
        // is not multiple of k
        if ((arr[i] % k) != 0)
        {
 
            // left = -1 means we are
            // finding the leftmost
            // element that is not
            // divisible by k
            if (left == -1)
            {
                left = i;
            }
 
            // Updating the
            // rightmost element
            right = i;
        }
 
        // Update the sum of the
        // array up to the index i
        sum += arr[i];
    }
 
    // Check if the sum of the
    // array is not divisible
    // by k, then return the
    // size of array
    if ((sum % k) != 0)
    {
        return n;
    }
 
    // All elements of array
    // are divisible by k,
    // then no such subarray
    // possible so return -1
    else if (left == -1)
    {
        return -1;
    }
    else
    {
         
        // Length of prefix elements
        // that can be removed
        int prefix_length = left + 1;
 
        // Length of suffix elements
        // that can be removed
        int suffix_length = n - right;
 
        // Return the length of
        // subarray after removing
        // the elements which have
        // lesser number of elements
        return n - Math.min(prefix_length,
                            suffix_length);
    }
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 6, 3, 12, 15 };
    int n = arr.length;
    int K = 3;
     
    System.out.println(MaxSubarrayLength(arr, n, K));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program to find the length of
# the longest subarray whose sum is
# not divisible by integer
 
# Function to find the longest subarray
# with sum is not divisible by k
def MaxSubarrayLength(arr, n, k):
 
    # left is the index of the
    # leftmost element that is
    # not divisible by k
    left = -1
 
    # sum of the array
    sum = 0
 
    for i in range(n):
 
        # Find the element that
        # is not multiple of k
        if ((arr[i] % k) != 0):
 
            # left = -1 means we are
            # finding the leftmost
            # element that is not
            # divisible by k
            if (left == -1):
                left = i
 
            # Updating the
            # rightmost element
            right = i
 
        # Update the sum of the
        # array up to the index i
        sum += arr[i]
 
    # Check if the sum of the
    # array is not divisible
    # by k, then return the
    # size of array
    if ((sum % k) != 0):
        return n
 
    # All elements of array
    # are divisible by k,
    # then no such subarray
    # possible so return -1
    elif(left == -1):
        return -1
 
    else:
         
        # length of prefix elements
        # that can be removed
        prefix_length = left + 1
 
        # length of suffix elements
        # that can be removed
        suffix_length = n - right
 
        # Return the length of
        # subarray after removing
        # the elements which have
        # lesser number of elements
        return n - min(prefix_length,
                       suffix_length)
                        
# Driver Code
if __name__ == "__main__":
 
    arr = [ 6, 3, 12, 15 ]
    n = len(arr)
    K = 3
 
    print(MaxSubarrayLength(arr, n, K))
 
# This code is contributed by chitranayal


C#
// C# program to find the length of
// the longest subarray whose sum is
// not divisible by integer K
using System;
 
class GFG{
 
// Function to find the longest subarray
// with sum is not divisible by k
static int MaxSubarrayLength(int []arr, int n,
                                        int k)
{
     
    // left is the index of the
    // leftmost element that is
    // not divisible by k
    int left = -1;
 
    // right is the index of the
    // rightmost element that is
    // not divisible by k
    int right = 0;
 
    // sum of the array
    int sum = 0;
 
    for(int i = 0; i < n; i++)
    {
         
        // Find the element that
        // is not multiple of k
        if ((arr[i] % k) != 0)
        {
 
            // left = -1 means we are
            // finding the leftmost
            // element that is not
            // divisible by k
            if (left == -1)
            {
                left = i;
            }
 
            // Updating the
            // rightmost element
            right = i;
        }
 
        // Update the sum of the
        // array up to the index i
        sum += arr[i];
    }
 
    // Check if the sum of the
    // array is not divisible
    // by k, then return the
    // size of array
    if ((sum % k) != 0)
    {
        return n;
    }
 
    // All elements of array
    // are divisible by k,
    // then no such subarray
    // possible so return -1
    else if (left == -1)
    {
        return -1;
    }
    else
    {
         
        // Length of prefix elements
        // that can be removed
        int prefix_length = left + 1;
 
        // Length of suffix elements
        // that can be removed
        int suffix_length = n - right;
 
        // Return the length of
        // subarray after removing
        // the elements which have
        // lesser number of elements
        return n - Math.Min(prefix_length,
                            suffix_length);
    }
}
 
// Driver code
public static void Main(string[] args)
{
    int []arr = { 6, 3, 12, 15 };
    int n = arr.Length;
    int K = 3;
     
    Console.Write(MaxSubarrayLength(arr, n, K));
}
}
 
// This code is contributed by rutvik_56


Javascript


输出:
-1

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

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