698. Partition to K Equal Sum Subsets
Problem
Intuition
Time Complexity
Space Complexity
Solution
Last updated
Last updated
class Solution {
public boolean canPartitionKSubsets(int[] nums, int k) {
// N - > nums.length k-> Number of subsets;
// Time Complexity: O(k* 2^n), k-> for finding k number of subsets and each element
// can be included in the subset or not
// Space Complexity: O(N)
// This method checks if the array nums can be partitioned into k subsets with equal sums.
int totalSum = 0;
for (int num : nums) {
totalSum += num; // Calculate the total sum of the array.
}
if (totalSum % k != 0) {
return false; // If the total sum is not divisible by k, partitioning is not possible.
}
int target = totalSum / k; // Calculate the target sum for each subset.
boolean[] visited = new boolean[nums.length]; // Array to track visited elements.
return canBePartitioned(nums, visited, k, 0, 0, target); // Recursively check if partitioning is possible.
}
private boolean canBePartitioned(int[] nums, boolean[] visited, int k, int start, int current, int target) {
// Recursive helper method to check if the array can be partitioned into k subsets.
if (k == 0) {
return true; // Base case: If k subsets have been formed, return true.
}
if (current > target) {
return false; // If the current sum exceeds the target, return false.
}
if (current == target) {
return canBePartitioned(nums, visited, k - 1, 0, 0, target); // If the current sum is equal to the target, move on to the next subset.
}
for (int i = start; i < nums.length; i++) {
if (visited[i]) {
continue; // Skip already visited elements.
}
visited[i] = true; // Mark the current element as visited.
if (canBePartitioned(nums, visited, k, i + 1, current + nums[i], target)) {
return true; // Recursively check if partitioning is possible with the current element.
}
visited[i] = false; // Backtrack: Mark the current element as not visited.
}
return false; // If partitioning is not possible with the current element, return false.
}
}