📌  相关文章
📜  给定总和的不同大小子数组的最大计数

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

给定总和的不同大小子数组的最大计数

给定一个包含N个整数的二进制数组arr[] ,任务是找到不同大小的子数组的最大计数,使得每个子数组的总和为K。

例子:

方法:给定的问题可以在滑动窗口算法的帮助下解决。可以观察到,子数组的总和等于子数组中1 的计数。以下是要遵循的步骤:

  • 维护一个变量来跟踪当前窗口中1 的计数。
  • 如果当前窗口中1 的数量小于K ,则增加窗口大小,同样,如果1 的数量大于K ,则减小窗口大小。
  • 对于具有K和的窗口,将它们的大小插入到集合数据结构中。
  • 遍历完整数组后集合中的元素个数就是必答。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find size of the largest
// subset of subarrays having given sum
// and size of each subarray is distinct
int maxSubsetSize(int arr[], int N, int K)
{
    // Stores starting index
    // of the current window
    int ptr = 0;
 
    // Stores distinct subarray
    // sizes of the subset
    unordered_set s;
 
    // Stores the sum of
    // current window
    int curSum = 0;
 
    // Loop to traverse over array
    for (int i = 0; i < N; i++) {
 
        // Update current window sum
        curSum += arr[i];
 
        // If sum is less that K
        if (curSum < K) {
            continue;
        }
 
        // If sum is more than K
        if (curSum > K) {
 
            // Decrease window size
            while (curSum > K) {
                curSum -= arr[ptr++];
            }
        }
 
        if (curSum == K) {
 
            // Insert size of the
            // current window
            s.insert(i - ptr + 1);
            int t = ptr;
 
            // Iterate over all windows
            // with same sum
            while (arr[t] == 0) {
                s.insert(i - t);
                t++;
            }
        }
    }
 
    // Return Answer
    return s.size();
}
 
// Driver Code
int main()
{
    int arr[] = { 0, 1, 1, 0 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 2;
 
    cout << maxSubsetSize(arr, N, K);
 
    return 0;
}


Java
// Java program for the above approach
import java.util.HashSet;
 
class GFG {
 
  // Function to find size of the largest
  // subset of subarrays having given sum
  // and size of each subarray is distinct
  static int maxSubsetSize(int arr[], int N, int K) {
    // Stores starting index
    // of the current window
    int ptr = 0;
 
    // Stores distinct subarray
    // sizes of the subset
    HashSet s = new HashSet();
 
    // Stores the sum of
    // current window
    int curSum = 0;
 
    // Loop to traverse over array
    for (int i = 0; i < N; i++) {
 
      // Update current window sum
      curSum += arr[i];
 
      // If sum is less that K
      if (curSum < K) {
        continue;
      }
 
      // If sum is more than K
      if (curSum > K) {
 
        // Decrease window size
        while (curSum > K) {
          curSum -= arr[ptr++];
        }
      }
 
      if (curSum == K) {
 
        // Insert size of the
        // current window
        s.add(i - ptr + 1);
        int t = ptr;
 
        // Iterate over all windows
        // with same sum
        while (arr[t] == 0) {
          s.add(i - t);
          t++;
        }
      }
    }
 
    // Return Answer
    return s.size();
  }
 
  // Driver Code
  public static void main(String args[]) {
    int arr[] = { 0, 1, 1, 0 };
    int N = arr.length;
    int K = 2;
 
    System.out.println(maxSubsetSize(arr, N, K));
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# python3 program for the above approach
 
# Function to find size of the largest
# subset of subarrays having given sum
# and size of each subarray is distinct
def maxSubsetSize(arr, N, K):
 
        # Stores starting index
        # of the current window
    ptr = 0
 
    # Stores distinct subarray
    # sizes of the subset
    s = set()
 
    # Stores the sum of
    # current window
    curSum = 0
 
    # Loop to traverse over array
    for i in range(0, N):
 
                # Update current window sum
        curSum += arr[i]
 
        # If sum is less that K
        if (curSum < K):
            continue
 
            # If sum is more than K
        if (curSum > K):
 
                        # Decrease window size
            while (curSum > K):
                curSum -= arr[ptr]
                ptr += 1
 
        if (curSum == K):
 
                        # Insert size of the
                        # current window
            s.add(i - ptr + 1)
            t = ptr
 
            # Iterate over all windows
            # with same sum
            while (arr[t] == 0):
                s.add(i - t)
                t += 1
 
        # Return Answer
    return len(list(s))
 
# Driver Code
if __name__ == "__main__":
 
    arr = [0, 1, 1, 0]
    N = len(arr)
    K = 2
 
    print(maxSubsetSize(arr, N, K))
 
    # This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
public class GFG {
 
  // Function to find size of the largest
  // subset of subarrays having given sum
  // and size of each subarray is distinct
  static int maxSubsetSize(int []arr, int N, int K)
  {
 
    // Stores starting index
    // of the current window
    int ptr = 0;
 
    // Stores distinct subarray
    // sizes of the subset
    HashSet s = new HashSet();
 
    // Stores the sum of
    // current window
    int curSum = 0;
 
    // Loop to traverse over array
    for (int i = 0; i < N; i++) {
 
      // Update current window sum
      curSum += arr[i];
 
      // If sum is less that K
      if (curSum < K) {
        continue;
      }
 
      // If sum is more than K
      if (curSum > K) {
 
        // Decrease window size
        while (curSum > K) {
          curSum -= arr[ptr++];
        }
      }
 
      if (curSum == K) {
 
        // Insert size of the
        // current window
        s.Add(i - ptr + 1);
        int t = ptr;
 
        // Iterate over all windows
        // with same sum
        while (arr[t] == 0) {
          s.Add(i - t);
          t++;
        }
      }
    }
 
    // Return Answer
    return s.Count;
  }
 
  // Driver Code
  public static void Main(String []args) {
    int []arr = { 0, 1, 1, 0 };
    int N = arr.Length;
    int K = 2;
 
    Console.WriteLine(maxSubsetSize(arr, N, K));
  }
}
 
// This code is contributed by shikhasingrajput


Javascript



输出
3

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