
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Program to check whether we can partition a list with k-partitions of equal sum in C++
Suppose we have a list of numbers called nums and another value k, we have to check whether it is possible to partition nums into k different subsets where the sum of each subset are same.
So, if the input is like nums = [4, 2, 6, 5, 1, 6, 3] k = 3, then the output will be True, as we can partition them like: [6, 3], [6, 2, 1], and [4, 5].
To solve this, we will follow these steps −
- Define a function check(), this will take an array v,
- for initialize i := 1, when i < size of v, update (increase i by 1), do −
- if v[i] is not equal to v[0], then −
- return false
- if v[i] is not equal to v[0], then −
- return true
- Define a function dfs(), this will take idx, an array nums, an array temp,
- if idx is same as size of nums, then −
- return check(temp)
- ret := false
- for initialize i := 0, when i < size of temp, update (increase i by 1), do −
- temp[i] := temp[i] + nums[idx]
- ret := dfs(idx + 1, nums, temp)
- if ret is true, then −
- return true
- temp[i] := temp[i] - nums[idx]
- return false
- From the main method do the following −
- Define an array temp of size k
- return dfs(0, nums, temp)
Example (C++)
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; class Solution { public: bool check(vector<int>& v) { for (int i = 1; i < v.size(); i++) { if (v[i] != v[0]) return false; } return true; } bool dfs(int idx, vector<int>& nums, vector<int>& temp) { if (idx == nums.size()) { return check(temp); } bool ret = false; for (int i = 0; i < temp.size(); i++) { temp[i] += nums[idx]; ret = dfs(idx + 1, nums, temp); if (ret) return true; temp[i] -= nums[idx]; } return false; } bool solve(vector<int>& nums, int k) { vector<int> temp(k); return dfs(0, nums, temp); } }; bool solve(vector<int>& nums, int k) { return (new Solution())->solve(nums, k); } int main(){ vector<int> v = {4, 2, 6, 5, 1, 6, 3}; int k = 3; cout << solve(v, 3); }
Input
{4, 2, 6, 5, 1, 6, 3}, 3
Output
1
- Related Articles
- Program to check whether we can make group of two partitions with equal sum or not in Python?
- Check if it possible to partition in k subarrays with equal sum in Python
- Partition to K Equal Sum Subsets in C++
- Program to check whether list can be partitioned into pairs where sum is multiple of k in python
- Program to check whether list can be split into sublists of k increasing elements in C++
- Program to check we can reach end of list by starting from k in Python
- Program to check whether we can convert string in K moves or not using Python
- Partition Equal Subset Sum in C++
- Program to check whether we can make k palindromes from given string characters or not in Python?
- Program to check whether we can split list into consecutive increasing sublists or not in Python
- Partition Array Into Three Parts With Equal Sum in Python
- Program to find number of sublists we can partition so given list is sorted finally in python
- Program to check whether we can stand at least k distance away from the closest contacts in Python
- Program to check whether we can pick up and drop every passenger in given list in Python
- Program to check we can find four elements whose sum is same as k or not in Python

Advertisements