
- 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
Ways to sum to N using array elements with repetition allowed in C++
In this problem, we are given an array of integers and a number N. Our task is to count the total number of ways N can be generated by adding elements of the array. All combinations and repetitions are allowed.
Let’s take an example to understand the problem,
Input
arr = {1, 3, 5} N = 6
Output
8
Explanation
The ways are −
5+1, 1+5, 3+3, 3+1+1+1, 1+3+1+1, 1+1+3+1, 1+1+1+3, 1+1+1+1+1+1
To solve this problem, we need to use a different approach as all types of combinations will be treated differently so, if the number is a sum of 4 elements of array 4 different ways are considered(as shown in example). For solve such a problem, we need to use dynamic programming approach and the below program will show the implementation.
Example
#include <iostream> #include <cstring> using namespace std; int arraySumWays(int array[], int size, int N){ int count[N + 1]; memset(count, 0, sizeof(count)); count[0] = 1; for (int i = 1; i <= N; i++) for (int j = 0; j < size; j++) if (i >= array[j]) count[i] += count[i - array[j]]; return count[N]; } int main() { int array[] = {1, 5, 6}; int size = sizeof(array) / sizeof(array[0]); int N = 7; cout<<"Total number of ways inwhich "<<N<<" can be generated using sum of elements of array is " <<arraySumWays(array, size, N); return 0; }
Output
Total number of ways inwhich 7 can be generated using sum of elements of array is 6
- Related Articles
- Ways to multiply n elements with an associative operation in C++
- Maximum sum of pairwise product in an array with negative allowed in C++
- Maximum sum of pairwise product in an array with negative allowed in C++ program
- How to calculate sum of array elements using pointers in C language?
- Maximum sum of n consecutive elements of array in JavaScript
- Count ways to express ‘n’ as sum of odd integers in C++
- Construct sum-array with sum of elements in given range in C++
- JavaScript Program to Find maximum value of Sum( i*arr[i]) with only rotations on given array allowed
- How not to match a character after repetition in Python Regex?\n\n
- Swift Program to get array elements after N elements
- Golang Program to get array elements after N elements
- How to find the sum of elements of an Array using STL in C++?
- C Program to find sum of perfect square elements in an array using pointers.
- Total possible ways of making sum of odd even indices elements equal in array in \nJavaScript
- Parts of array with n different elements in JavaScript

Advertisements