Share

## Find All Subsets Of An Int Array Whose Sums Equal A Given Target

Question

**Also See:**

- Whose beauty is too rich for use according to romeo
- Which Country Has Had A Prime Minister Whose Name Featured The Name Of His Country
- What Is A Simple Way To Target Ads To Mobile Users When They’Re Near Your Physical Store Locations?
- Cause: Unable To Find Valid Certification Path To Requested Target
- When Setting Up Display Advertising Campaigns Who Can You Target
- 1 LITRE IS EQUAL TO HOW MANY METRE CUBE
- WHAT IS A FIGURE WITH FOUR EQUAL SIDES CALLED ANSWER
- HOW MANY MILLIONS IS EQUAL TO 1 BILLION
- If Given A Choice, What Would Be The One Thing That You Would Change About The World, And Why?
- Greater The Number Of Resonating Structures For A Given Intermediate
- Which Indian Cricketer Was Recently Given The Padma Vibhushan Award
- Online Travel Services Generate Income From All The Given Except
- Which Of The Below Given Technique Is Avoided During Negotiating

in progress
0

1 Answer
## Answer ( 1 )

## Find All Subsets Of An Int Array Whose Sums Equal A Given Target

## Introduction

If you have an array of integers, and you want to find all subsets of that array whose sum is equal to a given target, there is a straightforward algorithm for doing so: 1. For each element in the array, calculate the sum of all the elements in its subset. 2. If all of these sums are equal to the target, then the subset exists and contains that element. Otherwise, the subset does not exist and does not contain that element.

## The Problem

The problem is to find all subsets of an int array whose sums equal a given target. This can be done in a number of ways, but the most efficient way is by using the Sieve Of Eratosthenes. The method works as follows:1.Start with the empty set and add every integer in the array until you reach the target sum.

2.For each element in the set that was added, check to see if its sum equals the target sum. If it does, keep this element and remove it from the set; otherwise, add it to the set and continue on to step 3.

3.Repeat steps 1 and 2 until you’ve found all elements that are needed for the target sum.

## Methods

There are various ways to find all subsets of an array whose sums equal a given target. One approach is to use the ArraySum() method of the SortedList class. This method returns an ArrayList that contains all the subsets of an array that sum to the target value. To use this method, you first need to create an instance of the SortedList class and then call its ArraySum() method. The following code example shows how to do this.

C# VB Copy public static void FindAllSubsets( int [] values, int target) { // Create a sorted list object using the specified values and target. SortedList sortedList = new SortedList(); // Add values to the sorted list, starting at position 0. for ( int value : values) { if (value > target) { ++values; } else if (value < target) { –values; } else { // The value is in the range oftarget so it qualifies as a subset // of the values already in the sorted list. sortedList.Add(values, value); } } // Call the ArraySum() method on the sorted list to calculate its sum. float sum = sortedList.ArraySum( ); }

Another approach is to use LINQ to query an array and extract all the subset objects that meet a given condition. You can use one of two LINQ operators—the Where operator or the SelectMany operator—to specify your condition. The Where operator works with the Boolean operator, and it returns a subset object if the condition is satisfied. The following example shows how to use the Where operator to find all the subsets of an array that have a sum greater than 10.

C# VB Copy public static void FindAllSubsets( int [] values, int target) { // Use the LINQ Where operator to find all subsets of an array // that have a sum greater than 10. LinqToList subsetList = values.Where(subset => subset.Sum > 10); }

## Results

Int arrays can be helpful when working with sets, as they can help you figure out which elements in a set are equal. In this article, we’ll show you how to find all subsets of an int array whose sums equal a given target.

The first step is to create an int array containing the target values. Next, we’ll use the sum() method on our int array to calculate the total number of elements in each subset. Finally, we’ll print out each subset’s name and its total number of elements.

Here’s the code for our example:int target = 10; // The target value for our sum() calculation int subsets[] = { 1, 2, 3 }; // An int array containing the target values for our subsets calculation System.out.println(“Summing up subsets:”); System.out.println(“”); System.out.println(target + ” -> ” + subsets[0]); System.out.println(“”); System.out.println(target + ” -> ” + subsets[1]); System.out.println(“”); System.out.println(target + ” -> ” + subsets[2]); System.out . println (target + ” -> Subsets: ” ); for (int i=0; i<3; i++) {System . out . print (subsets[i], DECIMAL_POINT);} System . out . println (); // Print

## Conclusion

The final step in our search for the all subsets of an int array whose sums equal a given target is to use the Array Set method. This method takes as input an int array and a target number, and returns all subsets of the array that have elements with values equal to or greater than the target number. After calling Array Set on our original intarray and our target number, we are able to return an obj object that contains information about each subset. Each property of this obj object corresponds to one of the methods we called on our original intarray: Sum(), Min(), Max(), First() and Last(). We can use these properties to determine the size of each subset, as well as its location in memory.