Sum combinations that add up to a given natural number - c#

I've been struggling with this pretty tricky problem for the past week :( I have to find all combinations of numbers that sum up to a given natural number using recursion. I'm not allowed to use LINQ or anything else besides "using system"
For example if the input is 7 then the output should look like this:
1 + 1 + 1 + 1 + 1 + 1 + 1
2 + 1 + 1 + 1 + 1 + 1
2 + 2 + 1 + 1 + 1
2 + 2 + 2 + 1
3 + 1 + 1 + 1 + 1
3 + 2 + 1 + 1
3 + 2 + 2
3 + 3 + 1
4 + 1 + 1 + 1
4 + 2 + 1
4 + 3
5 + 1 + 1
5 + 2
6 + 1
The numbers from the combination should be listed exactly in that order so for an input of 3 for example the output should be exactly as follows:
1 + 1 + 1
2 + 1
For an input of 4 the output should look like this:
1 + 1 + 1 + 1
2 + 1 + 1
2 + 2
3 + 1
For every new list of combinations we increment the first number in the list and then we continue with the remaining part of the previous list until the sum will be equal with the input.
Just positive numbers are allowed between 1 (1 included) and input - 1.
My code so far gives me the following output for the same given input of 7:
+ 1
+ 1 + 1 + 2
+ 1 + 1 + 1
+ 2 + 2 + 3
+ 1 + 1 + 1
+ 1 + 1 + 2
+ 2 + 1
+ 1 + 1 + 2
+ 2 + 2 + 1
+ 3 + 3 + 4
+ 1 + 1 + 1
+ 1 + 1 + 2
+ 1 + 1 + 1
+ 2 + 2 + 3
+ 2 + 1
+ 1 + 1 + 2
+ 1 + 1 + 1
+ 2 + 2 + 3
...
Can you please help me with some suggestions?
static string GenerateCombinations(int n)
{
string combinationList = "";
for (int index = 1; index < n - 1; index++)
{
string intermediaryList = GenerateCombinations(n - index, index) + " + " + index;
combinationList += intermediaryList;
}
return combinationList + "\n";
}
static string GenerateCombinations(int n, int index)
{
string combinationList = "";
for (int i = 1; i < n - 1; i++)
{
if (i <= index)
{
string intermediaryList = GenerateCombinations(n) + " + " + index;
combinationList += intermediaryList;
}
}
return combinationList;
}
static void Main()
{
int n = Convert.ToInt32(Console.ReadLine());
Console.WriteLine(GenerateCombinations(n));
}

Here is a solution not using collections (only using System;) and generating the output in the required order.
public static void PrintCombinations(int n)
{
PrintRest("", 0, n, n - 1);
}
private static void PrintRest(string listStart, int startSum, int n, int max)
{
for (int i = 1; i <= max; i++) {
string list = listStart.Length > 0
? listStart + " + " + i.ToString()
: i.ToString();
int sum = startSum + i;
if (sum == n) {
Console.WriteLine(list);
} else if (sum < n) {
PrintRest(list, sum, n, i);
}
}
}
You would call it as
PrintCombinations(7);
It starts by taking all possible start summands and calling itself to construct the rest of the sum. The combinations up to the current point are passed as string parameter listStart. The sum it represents is passed as int startSum. The target sum is n. max is the biggest summand allowed.

Try following :
class Program
{
static List<string> combinationList = new List<string>();
const int SUM = 7;
static void Main(string[] args)
{
List<int> numbers = new List<int>();
GenerateCombinations(numbers, 0);
combinationList.Sort();
Console.WriteLine(string.Join("\n", combinationList));
Console.ReadLine();
}
static void GenerateCombinations(List<int> numbers, int sum)
{
int start = 1;
if (numbers.Count > 0) start = numbers[0];
for (int i = start; i <= SUM; i++)
{
int newSum = sum + i;
if (newSum > SUM) break;
List<int> newList = new List<int>(numbers);
newList.Insert(0,i);
if (newSum == SUM)
{
combinationList.Add(string.Join(" + ", newList));
break;
}
else
{
GenerateCombinations(newList, newSum);
}
}
}
}

Since you are looking for a recursive solution, let's do it recursively without Linq and other mean.
Let's start from the basic: when given 0, we have an empty solution:
private static int[][] Solutions(int value) {
if (value <= 0)
return new int[][] { new int[0] };
//TODO: other cases for 1, 2, ...
}
Time to do the next step: if we know how to solve for some n - 1 (n - 1 >= 0) we can solve for n as follow:
all solutions start from m (m < n) are in form
`m + solutions for n - m which uses m .. 1 only`
E.g.
6 + 1 <- starts from 6, solves for 7 - 6 = 1, uses 6..1 only
5 + 2 ...
5 + 1 + 1
4 + 3
4 + 2 + 1
4 + 1 + 1 + 1 ...
3 + 3 + 1 <- starts from 3, solves for 7 - 3 = 4, uses 3..1 only
3 + 2 + 2 <- starts from 3, solves for 7 - 3 = 4, uses 3..1 only
3 + 2 + 1 + 1 <- starts from 3, solves for 7 - 3 = 4, uses 3..1 only
3 + 1 + 1 + 1 + 1 ...
2 + 2 + 2 + 1
2 + 2 + 1 + 1 + 1
2 + 1 + 1 + 1 + 1 + 1 ...
1 + 1 + 1 + 1 + 1 + 1 + 1 <- starts from 1, solves for 7 - 1 = 6, uses 1..1 only
This recursion can be encoded as
private static int[][] Solutions(int value, int startWith = -1) {
if (value <= 0)
return new int[][] { new int[0] };
if (startWith < 0)
startWith = value - 1;
List<int[]> solutions = new List<int[]>();
for (int i = Math.Min(value, startWith); i >= 1; --i)
foreach (int[] solution in Solutions(value - i, i)) {
int[] next = new int[solution.Length + 1];
Array.Copy(solution, 0, next, 1, solution.Length);
next[0] = i;
solutions.Add(next);
}
// Or just (if we are allow a bit of Linq)
// return solutions.ToArray();
int[][] answer = new int[solutions.Count][];
for (int i = 0; i < solutions.Count; ++i)
answer[i] = solutions[i];
return answer;
}
Demo
var result = Solutions(7);
// A pinch of Linq for demonstration
string report = string.Join(Environment.NewLine, result
.Select(solution => string.Join(" + ", solution)));
Console.Write(report);
Outcome:
6 + 1
5 + 2
5 + 1 + 1
4 + 3
4 + 2 + 1
4 + 1 + 1 + 1
3 + 3 + 1
3 + 2 + 2
3 + 2 + 1 + 1
3 + 1 + 1 + 1 + 1
2 + 2 + 2 + 1
2 + 2 + 1 + 1 + 1
2 + 1 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1 + 1

Related

Implement a recursive program that displays all combinations of operators to reach a given sum

I have to write a program that displays all the combinations of operators( + and -), to put between numbers from 1 to N (N>=2), in order to reach a targeted value X. It should write "N/A" if there is no possibility.
For the input:
n=6
x=3
It displays:
1 + 2 + 3 - 4 - 5 + 6 = 3
1 + 2 - 3 + 4 + 5 - 6 = 3
1 - 2 - 3 - 4 + 5 + 6 = 3
using System;
namespace ConsoleApp1
{
class Program
{
static bool counter;
static void Generate(int n, int x, int currentIndex, int result, string expression)
{
counter = true;
if (currentIndex == n + 1)
{
if (result == x)
{
Console.WriteLine(expression + " = " + x);
}
return;
}
Generate(n, x, currentIndex + 1, result + currentIndex, expression + " + " + currentIndex);
Generate(n, x, currentIndex + 1, result - currentIndex, expression + " - " + currentIndex);
}
static void Main()
{
int n = Convert.ToInt32(Console.ReadLine());
int x = Convert.ToInt32(Console.ReadLine());
const int doi = 2;
Generate(n, x, doi, 1, "1");
if (!counter)
{
Console.WriteLine("N/A");
}
Console.ReadLine();
}
}
}
It gives me the error : JRM003 (Error) : Don't use static fields. (line: 7, character: 7).
Where can I place the "counter" in order to track if there is possibility of reaching to the targeted value, and get rid of the error.
Here's how I'd do it. Count in binary treating 0 as subtraction and 1 as addition:
public static void Main(string[] args)
{
int n, x;
String response1, response2;
Console.Write("Enter a value for 'n' [n>=2]: ");
response1 = Console.ReadLine();
Console.Write("Enter a value for 'x': ");
response2 = Console.ReadLine();
if (int.TryParse(response1, out n) && int.TryParse(response2, out x))
{
if (n >= 2)
{
List<String> solutions = new List<string>();
int[] numbers = Enumerable.Range(1, n).ToArray();
int width = numbers.Length - 1;
int max = (int)Math.Pow(2, numbers.Length - 1);
for(int i=0; i < max; i++)
{
int sum = numbers[0];
String binary = Convert.ToString(i, 2).PadLeft(width, '0');
String equation = numbers[0].ToString();
for(int d=0; d<binary.Length; d++)
{
char operation = binary[d];
equation = equation + ((operation == '0') ? " - " : " + ") + numbers[d + 1];
sum = sum + ((operation == '0') ? -1 : 1) * numbers[d + 1];
}
equation = equation + " = " + sum;
if (sum == x)
{
solutions.Add(equation);
}
}
if (solutions.Count == 0)
{
Console.WriteLine("N/A");
}
else
{
foreach(String solution in solutions)
{
Console.WriteLine(solution);
}
}
}
else
{
Console.WriteLine("'n' must be greater than or equal to 2.");
}
}
else
{
Console.WriteLine("Invalid value for 'n' or 'x'!");
}
Console.WriteLine("Press Enter to Quit.");
Console.ReadLine();
}
Output:
Enter a value for 'n' [n>=2]: 6
Enter a value for 'x': 3
1 - 2 - 3 - 4 + 5 + 6 = 3
1 + 2 - 3 + 4 + 5 - 6 = 3
1 + 2 + 3 - 4 - 5 + 6 = 3
Press Enter to Quit.

c# sum of the sums in the array

Got stuck on quite simple problem in my code. I need to count what I would call a nested sums of an array. Let's take as an example the array:
[1,2,2,3,6]
I want to sum them as:
0 + 1 = 1
1 + 2 = 3
1 + 2 + 2 = 5
1 + 2 + 2 + 3 = 8
1 + 2 + 2 + 3 + 6 = 14
sum = 1 + 3 + 5 + 8 + 14 = 31
Edit:
I tried to do it with stack, but it failed
int sums = 0;
Stack<int> sum = new Stack<int>();
for (int i = 0; i < queries.Length; i++)
{
sum.Push(queries[i]);
}
for (int i = 0; sum.Count != 0; i++)
{
if(i != 0)
{
sums += sum.Pop();
}
}
You can run this task in a single loop considering when you have an array of size 5, the first element is repeating 5 times, second element repeating 4 times and etc.
int[] arr = { 1, 2, 2, 3, 6 };
int mysum = 0;
for (int i = 0; i < arr.Length; i++)
mysum += (arr.Length - i) * arr[i];
Console.WriteLine(mysum);
Output:
31
Explanation:
1 = 1
1 + 2 = 3
1 + 2 + 2 = 5
1 + 2 + 2 + 3 = 8
1 + 2 + 2 + 3 + 6 = 14
========================
5*1 + 4*2 + 3*2 + 2*3 + 1*6 = 31
You can do this much more efficiently and more easily, by multiplying each value by its reversed index + 1
For example, using LINQ
int[] arr = { 1, 2, 2, 3, 6 };
var result = arr.Reverse().Select((val, i) => val * (i + 1)).Sum();
Note that .Reverse on an array (or other Collection<T>) does not actually move any items, it just reads them backwards. So this is therefore an O(n) operation, as opposed to your original solution which is O(n2 / 2)
dotnetfiddle
You can also do this procedurally, this is almost the same as #aminrd's answer.
int[] arr = { 1, 2, 2, 3, 6 };
var result = 0;
for (var i = arr.Length - 1; i >= 0; i--)
result += arr[i] * (i + 1);
Take advantage of Linq! .Sum() will add up everything in your collection. You run that twice, once per each slice, and once per each subtotal.
var input = new [] { 1, 2, 2, 3, 6 };
var totalSum = Enumerable.Range(1, input.Length).Sum(len => input[0..len].Sum());
// totalSum is 31
Enumerable.Range gets you a collection of numbers between (and including) 1 and 5 - the possible lengths of each slice of your sub arrays. You then use the range operator [0..#] to get increasingly larger slices.
Yes, this is not as clever as aminrd's solution - it's doing all the computations manually and you're performing many slices.

C# Multiplication Table - Only 1 multiplication is showing

I am creating a multiplication table, when you input a number and click the calculate button it should display. I have tried watching a few tutorials on YouTube and have checked out some coding forums however I can only find people using the Console Application however I am using the Windows Form Application
1 * 1 = 1
2 * 1 = 2
3 * 1 = 3
4 * 1 = 4
5 * 1 = 5
6 * 1 = 6
7 * 1 = 7
8 * 1 = 8
9 * 1 = 9
10 * 1 = 10
However, when I run the program it only displays
1 * 10 = 10
Here is my code;
private void btnCalc_Click(object sender, EventArgs e)
{
int n, i;
n = Int32.Parse(txtNum.Text);
for (i = 1; i <= 10; ++i)
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
This loop keeps setting the control's text to a different value over and over, leaving you to see only the final value.
for (i = 1; i <= 10; ++i)
{
txtCalc.Text = Convert.ToString(n + " * " + i + " = " + n * i);
}
A straightforward solution is:
string text = "";
for (i = 1; i <= 10; ++i)
{
text += Convert.ToString(n + " * " + i + " = " + n * i);
}
txtCalc.Text = text;
You will still run into some formatting issues you'll need to solve, but you'll get the fundamental info in there.
You're overwriting the text over and over again. What you want to do is append new text every time through the loop. Try something like:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i) + Environment.NewLine;
}
your txtCalc.Text... overwrites the field in every iteration. You probably want something like this:
txtCalc.Text = "";
for (i = 1; i <= 10; ++i)
{
txtCalc.Text += Convert.ToString(n + " * " + i + " = " + n * i);
}

A little different multiplication table

I'm struggling to create a multiplication table that would look like this:
1 x 9 + 2 = 11
12 x 9 + 3 = 111
123 x 9 + 4 = 1111
......
123456 x 9 + 8 = 11111111
Currently I managed do to this:
#region MTABLE
for (int i = 2; i <= 8; i++)
{
int number = 1 * 9 + i;
Console.WriteLine("{0} X {1} + {2} = {3} ", 1, 9, i, number);
}
Console.ReadKey();
#endregion
And output that I get now:
1 X 9 + 2 = 11
1 X 9 + 3 = 12
1 X 9 + 4 = 13
1 X 9 + 5 = 14
1 X 9 + 6 = 15
1 X 9 + 7 = 16
1 X 9 + 8 = 17
The problem is that I don't know how to add number to 1 so next will be 12 and than next one 123...
If someone can give me any advice how to continue.
Concatenating a digit to a number can also be performed with multiplying with 10 and adding the digit:
int firstPart = 1;
for (int i = 2; i <= 8; i++)
{
int number = firstPart * 9 + i;
Console.WriteLine("{0} X {1} + {2} = {3} ", firstPart, 9, i, number);
firstPart = firstPart * 10 + i;
}
The first part of your formula (which is now set to 1) should be a variable outside the for loop scope and of type string. Each time you concatenate the i to this and then you do a Int.Parse of the string so you can multiply with it.

Print all subsets that sum up to specific value

I've being trying for some time now to find all the elements (including non-consecutive) of an array that sum up to specific value:
using System;
namespace ProgrammingBasics
{
class Exercise
{
static void Main()
{
PrintArray(arr);
SubarrayWithSum();
}
//--------------------------------------------------------------------
/*
Data members.
*/
// targer array
static int[] arr = { 2, 1, 2, 4, 3, 5, 2, 6 };
// target sum
static int sum = 14;
//--------------------------------------------------------------------
/*
Method: IsSubarrayWithSum(arr, sum);
It returns a bool value that indicates
whether there is a subarray within arr
with elements that sum up to specific value.
*/
static void SubarrayWithSum()
{
int depth = 0;
int startIndex = 0;
int endIndex = 1;
CheckAllCombinations(new int[arr.Length], startIndex, endIndex, depth);
}
//--------------------------------------------------------------------
/*
Method: CheckAllCombinations(subarray, sum);
*/
static void CheckAllCombinations(int[] subarray, int startIndex, int endIndex, int depth)
{
if (depth >= arr.Length)
{
return;
}
//Console.ReadKey();
for (int i = startIndex; i < endIndex; i++)
{
subarray[i] = arr[i];
//Console.WriteLine("startIndex = {0}, depth = {1}, i = {2}", startIndex, depth, i);
if (IsWantedSum(subarray))
{
Console.Write("S = {0} -> yes", sum);
PrintSubArray(subarray);
}
//PrintArray(subarray);
//Console.ReadKey();
CheckAllCombinations(new int [arr.Length], startIndex += 1, endIndex = (endIndex < arr.Length)? endIndex + 1 : endIndex, depth += 1);
}
}
//--------------------------------------------------------------------
/*
Method: IsWantedSum(int[] arr)
*/
static bool IsWantedSum(int[] arr)
{
int currentSum = 0;
for (int i = 0; i < arr.Length; i++)
{
currentSum += arr[i];
}
if (currentSum == sum)
{
return true;
}
else
{
return false;
}
}
//--------------------------------------------------------------------
/*
Method: PrintArray();
*/
static void PrintArray(int[] subarray)
{
Console.Write("{");
for (int i = 0; i < subarray.Length; i++)
{
Console.Write(subarray[i]);
if (i < subarray.Length -1) Console.Write(", ");
}
Console.WriteLine("}");
}
//--------------------------------------------------------------------
/*
Method: PrintSubArray();
*/
static void PrintSubArray(int[] subarray)
{
Console.Write("(");
for (int i = 0; i < subarray.Length; i++)
{
if (subarray[i] != 0)Console.Write(subarray[i]);
if (subarray[i] != 0 && i < subarray.Length - 1) Console.Write(" + ");
}
Console.WriteLine(" = {0})", sum);
}
}
}
I'm getting somewhat partially right result:
{2, 1, 2, 4, 3, 5, 2, 6}
S = 14 -> yes(4 + 3 + 5 + 2 + = 14)
S = 14 -> yes(2 + 4 + 3 + 5 + = 14)
S = 14 -> yes(4 + 3 + 5 + 2 + = 14)
S = 14 -> yes(4 + 3 + 5 + 2 + = 14)
S = 14 -> yes(2 + 4 + 3 + 5 + = 14)
S = 14 -> yes(4 + 3 + 5 + 2 + = 14)
with duplications and missing sub-arrays of non-consecutive elements such as:
yes (1 + 2 + 5 + 6 = 14)
Could someone give me a hint on the problems of my algorithm and probably suggest a correction / new implementation?
Here's a simple way to do it with combinations. There's probably a better way to store them (I'm thinking using a dictionary to encode all the sub sums you already have). At the end of the day, if you want to account for non-consecutive elements, you're going to have to get the sub arrays that are possible in this case, and not just look at consecutive choices. Credit for the combination algorithm goes to ojlovecd here .
class Exercise
{
static void Main()
{
PrintArray(arr);
// SubarrayWithSum();
var result = GetCombination(arr);
foreach(var list in result)
{
var total = list.Sum();
if (total == sum)
PrintArray(list);
}
}
static List<int> arr = new List<int> { 2, 1, 2, 4, 3, 5, 2, 6 };
static int sum = 14;
static List<List<int>> GetCombination(List<int> list)
{
var result = new List<List<int>>();
result.Add(new List<int>());
double count = Math.Pow(2, list.Count);
for (int i = 1; i <= count - 1; i++)
{
string str = Convert.ToString(i, 2).PadLeft(list.Count, '0');
for (int j = 0; j < str.Length; j++)
{
if (str[j] == '1')
{
result[i - 1].Add(list[j]);
}
}
result.Add(new List<int>());
}
return result;
}
static void PrintArray(List<int> subarray)
{
Console.Write("{");
for (int i = 0; i < subarray.Count; i++)
{
Console.Write(subarray[i]);
if (i < subarray.Count - 1) Console.Write(", ");
}
Console.WriteLine("}");
}
}
I think the duplicates are occurring because you have zeroes in the array that you are adding. See updated code which runs quicker.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
namespace ProgrammingBasics
{
class Exercise
{
static void Main()
{
PrintArray(arr);
SubarrayWithSum();
}
//--------------------------------------------------------------------
/*
Data members.
*/
// targer array
static int[] arr = { 2, 1, 2, 4, 3, 5, 2, 6 };
// target sum
static int sum = 14;
//--------------------------------------------------------------------
/*
Method: IsSubarrayWithSum(arr, sum);
It returns a bool value that indicates
whether there is a subarray within arr
with elements that sum up to specific value.
*/
static void SubarrayWithSum()
{
int depth = 0;
int endIndex = arr.Length - 1;
CheckAllCombinations(new int[arr.Length], depth);
}
//--------------------------------------------------------------------
/*
Method: CheckAllCombinations(subarray, sum);
*/
static void CheckAllCombinations(int[] subarray, int depth)
{
//Console.ReadKey();
for (int i = depth; i < arr.Length; i++)
{
subarray[depth] = arr[i];
Console.WriteLine("depth = {0}, i = {1}, array = '{2}' ", depth, i, string.Join(",", subarray.Select(x => x.ToString()).ToArray()));
int currentSum = subarray.Take(depth + 1).Sum();
if (currentSum == sum)
{
Console.Write("S = {0} -> yes : ", sum);
Console.WriteLine(string.Join(",", subarray.Take(depth + 1)));
}
//PrintArray(subarray);
//Console.ReadKey();
if (currentSum < sum)
{
CheckAllCombinations(subarray, depth + 1);
}
}
}
//--------------------------------------------------------------------
/*
Method: IsWantedSum(int[] arr)
*/
//--------------------------------------------------------------------
/*
Method: PrintArray();
*/
static void PrintArray(int[] subarray)
{
Console.Write("{");
for (int i = 0; i < subarray.Length; i++)
{
Console.Write(subarray[i]);
if (i < subarray.Length - 1) Console.Write(", ");
}
Console.WriteLine("}");
}
//--------------------------------------------------------------------
/*
Method: PrintSubArray();
*/
static void PrintSubArray(int[] subarray)
{
Console.Write("(");
for (int i = 0; i < subarray.Length; i++)
{
if (subarray[i] != 0) Console.Write(subarray[i]);
if (subarray[i] != 0 && i < subarray.Length - 1) Console.Write(" + ");
}
Console.WriteLine(" = {0})", sum);
}
}
}
OK, here is small attempt to briefly describe the info that I went through to understand the problem1 and implement a basic solution.
It turns out that The Subset Sum Problem is considered a special case of the Knapsack Problem in which we are searching to maximize profit while keeping value called weight under specific capacity, but in our case the profit and weight associated with each value are identical.
There are variety of great solutions described in "Knapsack Problems" - Keller, Pferschy, Pisinger, however, for the time being the simplest solution that I could implement and understand, not carrying about complexity and / or efficacy, looks like this:
//--------------------------------------------------------------------
/*
Method: FindSubArray();
Base case: - if current sum == targer sum: print current elements.
- if index == arr.Length: terminate search.
Recursive step:
- do not/select element with index and do not/update the current sum; recursive call with updated current sum and index.
*/
static void FindSubArray(int index, int currentSum, bool[] subArray)
{
// base case
if (currentSum == targetSum)
{
PrintSubArray(subArray);
}
else if (index == arr.Length)
{
return;
}
else
{
// recursive calls
subArray[index] = true;
currentSum += arr[index];
FindSubArray(index + 1, currentSum, subArray);
currentSum -= arr[index]; // restore previous value of the sum signifying: element not selected
subArray[index] = false;
FindSubArray(index + 1, currentSum, subArray);
}
}
where PrintSubArray(subArray); prints all the elements of arr marked with true in subArray.
Output:
{2, 1, 2, 4, 3, 5, 2, 6}
S = 14 -> yes (2 + 1 + 2 + 4 + 3 + 2 + = 14)
S = 14 -> yes (2 + 1 + 2 + 4 + 5 + = 14)
S = 14 -> yes (2 + 1 + 2 + 3 + 6 = 14)
S = 14 -> yes (2 + 1 + 4 + 5 + 2 + = 14)
S = 14 -> yes (2 + 1 + 3 + 2 + 6 = 14)
S = 14 -> yes (2 + 1 + 5 + 6 = 14)
S = 14 -> yes (2 + 2 + 4 + 6 = 14)
S = 14 -> yes (2 + 2 + 3 + 5 + 2 + = 14)
S = 14 -> yes (2 + 4 + 3 + 5 + = 14)
S = 14 -> yes (2 + 4 + 2 + 6 = 14)
S = 14 -> yes (1 + 2 + 4 + 5 + 2 + = 14)
S = 14 -> yes (1 + 2 + 3 + 2 + 6 = 14)
S = 14 -> yes (1 + 2 + 5 + 6 = 14)
S = 14 -> yes (1 + 4 + 3 + 6 = 14)
S = 14 -> yes (1 + 5 + 2 + 6 = 14)
S = 14 -> yes (2 + 4 + 3 + 5 + = 14)
S = 14 -> yes (2 + 4 + 2 + 6 = 14)
S = 14 -> yes (4 + 3 + 5 + 2 + = 14)
S = 14 -> yes (3 + 5 + 6 = 14)
The book that I'm reading states the problem simply as: "Find if there is a sub array with elements that sum up to specific value'.

Categories