Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- We are given an array of longegers and a number S. Write a program to find
- if there exists a subset of the elements of the array that has a sum S.
- */
- using System;
- public class SubsetOfSumS
- {
- public static void Main()
- {
- long n = long.Parse(Console.ReadLine()), c = 0;
- long sum = 0;//long n = 14, c = 0, sum = 0;//
- string[] str = Console.ReadLine().Split(' ');
- long[] ar = new long[str.Length];
- for (long i = 0; i < str.Length; i++)
- ar[i] = Convert.ToInt64(str[i]);
- // long[] ar = {2, 1, 2, 4, 3, 0, 9, 6};//8
- // Array.Sort(ar);
- for (long i = ar.Length - 1; i >= 0; i--)
- {
- sum = 0;
- for (long j = 0; j < ar.Length; j++)
- {
- if (i != j)
- {
- sum = sum + ar[j];
- if (sum == n)
- {
- Console.WriteLine("yes"); c++; break;
- }
- if (sum > n)
- sum = sum - ar[j];
- }
- }
- if (c == 1) break;
- }
- if (c == 0)
- Console.WriteLine("no");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement