📌  相关文章
📜  检查是否可以划分为等和的 k 个子数组

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

检查是否可以划分为等和的 k 个子数组

给定一个大小为 N 的数组 A 和一个数字 K。任务是找出是否可以将数组 A 划分为 K 个连续的子数组,使得每个子数组中的元素总和相同。
先决条件:计算将数组分成三个连续部分的方法数,总和相等

例子 :

Input : arr[] = { 1, 4, 2, 3, 5 } K = 3
Output : Yes
Explanation :
Three possible partitions which have equal sum : 
(1 + 4), (2 + 3) and (5)

Input : arr[] = { 1, 1, 2, 2 } K = 2
Output : No

方法 :
可以通过使用前缀和来解决。首先,请注意,数组中所有元素的总和应该能被 K 整除,以创建 K 个分区,每个分区的总和相等。如果它是可分的,那么检查每个分区的总和是否相等:
1.对于特定的K,每个子数组应该有一个所需的sum = total_sum / K。
2.从第0索引开始,开始比较前缀和,只要
它等于总和,它意味着一个子数组的结尾(假设在索引 j 处)。
3. 从第 (j + 1)索引中,找到另一个合适的 i,其总和 (prefix_sum[i] –
prefix_sum[j]) 等于所需的总和。这个过程一直持续到
找到所需数量的连续子数组,即 K。
4. 如果在任何索引处,任何子数组总和大于所需总和,则突破
from 循环,因为每个子数组都应该包含相等的总和。

以下是上述方法的实现

C++
// CPP Program to check if array
// can be split into K contiguous
// subarrays each having equal sum
#include 
using namespace std;
 
// function returns true to it is possible to
// create K contiguous partitions each having
// equal sum, otherwise false
bool KpartitionsPossible(int arr[], int n, int K)
{
    // Creating and filling prefix sum array
    int prefix_sum[n];
    prefix_sum[0] = arr[0];
    for (int i = 1; i < n; i++)
        prefix_sum[i] =  prefix_sum[i - 1] + arr[i];
 
    // return false if total_sum is not
    // divisible by K  
    int total_sum = prefix_sum[n-1];
    if (total_sum % K != 0)
        return false;
 
    // a temporary variable to check
    // there are exactly K partitions
    int temp = 0;
     
    int pos = -1;
    for (int i = 0; i < n; i++)
    {       
        // find suitable i for which first
        // partition have the required sum
        // and then find next partition and so on
        if (prefix_sum[i] - (pos == -1 ? 0 :
            prefix_sum[pos]) == total_sum / K)
        {
            pos = i;
            temp++;
        }
         
        // if it becomes greater than
        // required sum break out
        else if (prefix_sum[i] - prefix_sum[pos] >
                total_sum / K)
            break;
    }
 
    // check if temp has reached to K
    return (temp == K);
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 4, 3, 5, 6, 2 };   
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int K = 3;
    if (KpartitionsPossible(arr, n, K))
        cout << "Yes";
    else
        cout << "No";
    return 0;
}


Java
// Java Program to check if an array
// can be split into K contiguous
// subarrays each having equal sum
public class GfG{
 
    // Function returns true to it is possible to
    // create K contiguous partitions each having
    // equal sum, otherwise false
    static boolean KpartitionsPossible(int arr[], int n, int K)
    {
        // Creating and filling prefix sum array
        int prefix_sum[] = new int[n];
        prefix_sum[0] = arr[0];
        for (int i = 1; i < n; i++)
            prefix_sum[i] = prefix_sum[i - 1] + arr[i];
     
        // return false if total_sum is not divisible by K
        int total_sum = prefix_sum[n-1];
        if (total_sum % K != 0)
            return false;
     
        // a temporary variable to check
        // there are exactly K partitions
        int temp = 0, pos = -1;
         
        for (int i = 0; i < n; i++)
        {        
            // find suitable i for which first
            // partition have the required sum
            // and then find next partition and so on
            if (prefix_sum[i] - (pos == -1 ? 0 :
                prefix_sum[pos]) == total_sum / K)
            {
                pos = i;
                temp++;
            }
             
            // if it becomes greater than
            // required sum break out
            else if (prefix_sum[i] - (pos == -1 ? 0 :
                prefix_sum[pos]) > total_sum / K)
                break;
        }
     
        // check if temp has reached to K
        return (temp == K);
    }
 
     public static void main(String []args){
         
        int arr[] = { 4, 4, 3, 5, 6, 2 };    
        int n = arr.length;
     
        int K = 3;
        if (KpartitionsPossible(arr, n, K))
            System.out.println("Yes");
        else
            System.out.println("No");
     }
}
 
// This code is contributed by Rituraj Jain


Python3
# Python 3 Program to check if array
# can be split into K contiguous
# subarrays each having equal sum
 
# function returns true to it is possible to
# create K contiguous partitions each having
# equal sum, otherwise false
def KpartitionsPossible(arr, n, K):
     
    # Creating and filling prefix sum array
    prefix_sum = [0 for i in range(n)]
    prefix_sum[0] = arr[0]
    for i in range(1, n, 1):
        prefix_sum[i] = prefix_sum[i - 1] + arr[i]
 
    # return false if total_sum is not
    # divisible by K
    total_sum = prefix_sum[n - 1]
    if (total_sum % K != 0):
        return False
 
    # a temporary variable to check
    # there are exactly K partitions
    temp = 0
     
    pos = -1
    for i in range(0, n, 1):
         
        # find suitable i for which first
        # partition have the required sum
        # and then find next partition and so on
        if (pos == -1):
            sub = 0
        else:
            sub = prefix_sum[pos]
        if (prefix_sum[i] - sub == total_sum / K) :
            pos = i
            temp += 1
         
        # if it becomes greater than
        # required sum break out
        elif (prefix_sum[i] -
              prefix_sum[pos] > total_sum / K):
            break
 
    # check if temp has reached to K
    return (temp == K)
 
# Driver Code
if __name__ =='__main__':
    arr = [4, 4, 3, 5, 6, 2]
    n = len(arr)
 
    K = 3
    if (KpartitionsPossible(arr, n, K)):
        print("Yes")
    else:
        print("No")
 
# This code is contributed by
# Shashank_Sharma


C#
// C# Program to check if an array
// can be split into K contiguous
// subarrays each having equal sum
using System;
 
class GfG
{
 
    // Function returns true to it is possible to
    // create K contiguous partitions each having
    // equal sum, otherwise false
    static bool KpartitionsPossible(int[] arr, int n, int K)
    {
        // Creating and filling prefix sum array
        int[] prefix_sum = new int[n];
        prefix_sum[0] = arr[0];
        for (int i = 1; i < n; i++)
            prefix_sum[i] = prefix_sum[i - 1] + arr[i];
     
        // return false if total_sum is not divisible by K
        int total_sum = prefix_sum[n-1];
        if (total_sum % K != 0)
            return false;
     
        // a temporary variable to check
        // there are exactly K partitions
        int temp = 0, pos = -1;
         
        for (int i = 0; i < n; i++)
        {        
            // find suitable i for which first
            // partition have the required sum
            // and then find next partition and so on
            if (prefix_sum[i] - (pos == -1 ? 0 :
                prefix_sum[pos]) == total_sum / K)
            {
                pos = i;
                temp++;
            }
             
            // if it becomes greater than
            // required sum break out
            else if (prefix_sum[i] - (pos == -1 ? 0 :
                prefix_sum[pos]) > total_sum / K)
                break;
        }
     
        // check if temp has reached to K
        return (temp == K);
    }
 
    // Driver code
    public static void Main()
    {
         
        int[] arr = { 4, 4, 3, 5, 6, 2 };    
        int n = arr.Length;
     
        int K = 3;
        if (KpartitionsPossible(arr, n, K))
            Console.WriteLine("Yes");
        else
            Console.WriteLine("No");
    }
}
 
// This code is contributed by ChitraNayal


PHP


Javascript


C++
// CPP Program to check if array
// can be split into K contiguous
// subarrays each having equal sum
#include 
using namespace std;
 
// function returns true to it is possible to
// create K contiguous partitions each having
// equal sum, otherwise false
int KpartitionsPossible(int arr[], int n, int k)
{
    int sum = 0;
    int count = 0;
     
    // calculate the sum of the array
    for(int i = 0; i < n; i++)
    sum = sum + arr[i];
     
    if(sum % k != 0)
    return 0;
     
    sum = sum / k;
    int ksum = 0;
     
    // ksum denotes the sum of each subarray
    for(int i = 0; i < n; i++)
    {
    ksum=ksum + arr[i];
     
    // one subarray is found
    if(ksum == sum)
    {
        // to locate another
        ksum = 0;
        count++;
    }
     
    }
     
    if(count == k)
    return 1;
    else
    return 0;
     
}
 
// Driver code
int main() {
 
int arr[] = { 1, 1, 2, 2};
int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
    if (KpartitionsPossible(arr, n, k) == 0)
        cout << "Yes";
    else
        cout<<"No";
    return 0;
 
}


Java
//Java Program to check if array
// can be split into K contiguous
// subarrays each having equal sum
 
public class GFG {
 
// function returns true to it is possible to
// create K contiguous partitions each having
// equal sum, otherwise false
    static int KpartitionsPossible(int arr[], int n, int k) {
        int sum = 0;
        int count = 0;
 
        // calculate the sum of the array
        for (int i = 0; i < n; i++) {
            sum = sum + arr[i];
        }
 
        if (sum % k != 0) {
            return 0;
        }
 
        sum = sum / k;
        int ksum = 0;
 
        // ksum denotes the sum of each subarray
        for (int i = 0; i < n; i++) {
            ksum = ksum + arr[i];
 
            // one subarray is found
            if (ksum == sum) {
                // to locate another
                ksum = 0;
                count++;
            }
 
        }
 
        if (count == k) {
            return 1;
        } else {
            return 0;
        }
 
    }
 
    // Driver Code
    public static void main(String[] args) {
 
        int arr[] = {1, 1, 2, 2};
        int k = 2;
        int n = arr.length;
 
        if (KpartitionsPossible(arr, n, k) == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
 
}
 
/*This code is contributed by PrinciRaj1992*/


Python3
# Python3 Program to check if array
# can be split into K contiguous
# subarrays each having equal sum
 
# Function returns true to it is possible
# to create K contiguous partitions each
# having equal sum, otherwise false
def KpartitionsPossible(arr, n, k) :
 
    sum = 0
    count = 0
     
    # calculate the sum of the array
    for i in range(n) :
        sum = sum + arr[i]
     
    if(sum % k != 0) :
        return 0
     
    sum = sum // k
    ksum = 0
     
    # ksum denotes the sum of each subarray
    for i in range(n) :
        ksum = ksum + arr[i]
     
    # one subarray is found
    if(ksum == sum) :
         
        # to locate another
        ksum = 0
        count += 1
     
    if(count == k) :
        return 1
    else :
        return 0
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 1, 2, 2]
    k = 2
    n = len(arr)
    if (KpartitionsPossible(arr, n, k) == 0) :
        print("Yes")
    else :
        print("No")
 
# This code is contributed by Ryuga


C#
// C# Program to check if array
// can be split into K contiguous
// subarrays each having equal sum
 
using System;
public class GFG{
 
 
// function returns true to it is possible to
// create K contiguous partitions each having
// equal sum, otherwise false
    static int KpartitionsPossible(int []arr, int n, int k) {
        int sum = 0;
        int count = 0;
 
        // calculate the sum of the array
        for (int i = 0; i < n; i++) {
            sum = sum + arr[i];
        }
 
        if (sum % k != 0) {
            return 0;
        }
 
        sum = sum / k;
        int ksum = 0;
 
        // ksum denotes the sum of each subarray
        for (int i = 0; i < n; i++) {
            ksum = ksum + arr[i];
 
            // one subarray is found
            if (ksum == sum) {
                // to locate another
                ksum = 0;
                count++;
            }
 
        }
 
        if (count == k) {
            return 1;
        } else {
            return 0;
        }
 
    }
 
    // Driver Code
    public static void Main() {
 
        int []arr = {1, 1, 2, 2};
        int k = 2;
        int n = arr.Length;
 
        if (KpartitionsPossible(arr, n, k) == 0) {
            Console.Write("Yes");
        } else {
            Console.Write("No");
        }
    }
 
}
 
/*This code is contributed by PrinciRaj1992*/


PHP


Javascript


输出:
Yes

时间复杂度: O(N),其中 N 是数组的大小。
辅助空间: O(N),其中 N 是数组的大小。

我们可以进一步将空间复杂度降低到O(1)
由于数组将被划分为 k 个子数组,并且所有子数组都是连续的。所以想法是计算总和等于整个数组的总和除以k的子数组的计数。
如果 count == k 打印是,否则打印否。

以下是上述方法的实现

C++

// CPP Program to check if array
// can be split into K contiguous
// subarrays each having equal sum
#include 
using namespace std;
 
// function returns true to it is possible to
// create K contiguous partitions each having
// equal sum, otherwise false
int KpartitionsPossible(int arr[], int n, int k)
{
    int sum = 0;
    int count = 0;
     
    // calculate the sum of the array
    for(int i = 0; i < n; i++)
    sum = sum + arr[i];
     
    if(sum % k != 0)
    return 0;
     
    sum = sum / k;
    int ksum = 0;
     
    // ksum denotes the sum of each subarray
    for(int i = 0; i < n; i++)
    {
    ksum=ksum + arr[i];
     
    // one subarray is found
    if(ksum == sum)
    {
        // to locate another
        ksum = 0;
        count++;
    }
     
    }
     
    if(count == k)
    return 1;
    else
    return 0;
     
}
 
// Driver code
int main() {
 
int arr[] = { 1, 1, 2, 2};
int k = 2;
    int n = sizeof(arr) / sizeof(arr[0]);
    if (KpartitionsPossible(arr, n, k) == 0)
        cout << "Yes";
    else
        cout<<"No";
    return 0;
 
}

Java

//Java Program to check if array
// can be split into K contiguous
// subarrays each having equal sum
 
public class GFG {
 
// function returns true to it is possible to
// create K contiguous partitions each having
// equal sum, otherwise false
    static int KpartitionsPossible(int arr[], int n, int k) {
        int sum = 0;
        int count = 0;
 
        // calculate the sum of the array
        for (int i = 0; i < n; i++) {
            sum = sum + arr[i];
        }
 
        if (sum % k != 0) {
            return 0;
        }
 
        sum = sum / k;
        int ksum = 0;
 
        // ksum denotes the sum of each subarray
        for (int i = 0; i < n; i++) {
            ksum = ksum + arr[i];
 
            // one subarray is found
            if (ksum == sum) {
                // to locate another
                ksum = 0;
                count++;
            }
 
        }
 
        if (count == k) {
            return 1;
        } else {
            return 0;
        }
 
    }
 
    // Driver Code
    public static void main(String[] args) {
 
        int arr[] = {1, 1, 2, 2};
        int k = 2;
        int n = arr.length;
 
        if (KpartitionsPossible(arr, n, k) == 0) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
 
}
 
/*This code is contributed by PrinciRaj1992*/

Python3

# Python3 Program to check if array
# can be split into K contiguous
# subarrays each having equal sum
 
# Function returns true to it is possible
# to create K contiguous partitions each
# having equal sum, otherwise false
def KpartitionsPossible(arr, n, k) :
 
    sum = 0
    count = 0
     
    # calculate the sum of the array
    for i in range(n) :
        sum = sum + arr[i]
     
    if(sum % k != 0) :
        return 0
     
    sum = sum // k
    ksum = 0
     
    # ksum denotes the sum of each subarray
    for i in range(n) :
        ksum = ksum + arr[i]
     
    # one subarray is found
    if(ksum == sum) :
         
        # to locate another
        ksum = 0
        count += 1
     
    if(count == k) :
        return 1
    else :
        return 0
 
# Driver code
if __name__ == "__main__" :
 
    arr = [ 1, 1, 2, 2]
    k = 2
    n = len(arr)
    if (KpartitionsPossible(arr, n, k) == 0) :
        print("Yes")
    else :
        print("No")
 
# This code is contributed by Ryuga

C#

// C# Program to check if array
// can be split into K contiguous
// subarrays each having equal sum
 
using System;
public class GFG{
 
 
// function returns true to it is possible to
// create K contiguous partitions each having
// equal sum, otherwise false
    static int KpartitionsPossible(int []arr, int n, int k) {
        int sum = 0;
        int count = 0;
 
        // calculate the sum of the array
        for (int i = 0; i < n; i++) {
            sum = sum + arr[i];
        }
 
        if (sum % k != 0) {
            return 0;
        }
 
        sum = sum / k;
        int ksum = 0;
 
        // ksum denotes the sum of each subarray
        for (int i = 0; i < n; i++) {
            ksum = ksum + arr[i];
 
            // one subarray is found
            if (ksum == sum) {
                // to locate another
                ksum = 0;
                count++;
            }
 
        }
 
        if (count == k) {
            return 1;
        } else {
            return 0;
        }
 
    }
 
    // Driver Code
    public static void Main() {
 
        int []arr = {1, 1, 2, 2};
        int k = 2;
        int n = arr.Length;
 
        if (KpartitionsPossible(arr, n, k) == 0) {
            Console.Write("Yes");
        } else {
            Console.Write("No");
        }
    }
 
}
 
/*This code is contributed by PrinciRaj1992*/

PHP


Javascript


输出:
Yes