Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class CheckSubset
- {
- static void findSubsets(int[] arr, List<int> newArray, int index, int S, int K)
- {
- for (int i = index; i < arr.Length; i++)
- {
- int count = 0;
- foreach (var item in newArray)
- count += item;
- if ((arr[i] + count) == S)
- {
- foreach (var item in newArray)
- if ((newArray.Count + 1) == K)
- {
- Console.Write("{0}, ", item);
- Console.Write("{0}, ", arr[i]);
- Console.WriteLine();
- }
- }
- newArray.Add(arr[i]);
- findSubsets(arr, newArray, i + 1, S, K);
- newArray.Remove(arr[i]);
- }
- }
- static void Main()
- {
- Console.Write("Enter the number of elements in the array:");
- int N = int.Parse(Console.ReadLine());
- int[] arr = new int[N];
- for (int i = 0; i < N; i++)
- {
- Console.Write("Enter an element:");
- arr[i] = int.Parse(Console.ReadLine());
- }
- Console.Write("Enter the sum of subsets S:");
- int S = int.Parse(Console.ReadLine());
- Console.Write("Enter the length of the subset K:");
- int K = int.Parse(Console.ReadLine());
- findSubsets(arr, new List<int>(), 0 , S, K);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement