-2

I found many solutions to solve this problem but i didn't get any of them. Can anyone please give me the simplest solution for the given problem.If possible please give answer in java language.

  Example-   
  Input : arr[] = {1, 2, 3}
  Possible Subset are:{}, {1}, {2}, {3}, {1, 2}, {1, 3}, 
                 {2, 3}, {1, 2, 3}

This program is not finding all the subsets

class Test
{
static int arr[] = new int[]{1, 2, 3, 4};

// Prints all subarrays in arr[0..n-1]
static void subArray( int n)
{
    // Pick starting point
    for (int i=0; i <n; i++)
    {
        // Pick ending point
        for (int j=i; j<n; j++)
        {
            // Print subarray between current starting
            // and ending points
            for (int k=i; k<=j; k++)
                System.out.print(arr[k]+"");
        }
    }
}

// Driver method to test the above function
public static void main(String[] args) 
{
    System.out.println("All Non-empty Subarrays");
    subArray(arr.length);

}

}

Output-
1 
1 2 
1 2 3 
1 2 3 4 
2 
2 3 
2 3 4 
3 
3 4 
4
This is not finding all subsets like (1,3)...
  • An array is not a set. Also "I don't understand XX" is not a great question. Please provide at least some code of the solutions you have seen which you don't understand. – Lino Jul 17 '18 at 06:44
  • Input : arr[] = {1, 2, 3} Possible Subset are: 1, 2, 3, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3} – Nitin Singhal Jul 17 '18 at 06:45
  • 2
    Please [edit] your question instead of hiding useful additional information in comments. – Yunnosch Jul 17 '18 at 06:46
  • 1
    This question sounds like asking for exlanation or even introduction of quite basic concepts. Please demonstrate using loops and output by showing some "frame" code. If that is already beyond you, then what you are asking for is actually a tutorial or even a "HelloWorld". Sadly that would be off-topic. – Yunnosch Jul 17 '18 at 06:48
  • @NitinSinghal `{}` would also be a subset – Lino Jul 17 '18 at 06:50
  • 1
    If you can write a program which outputs the first few subsets, i.e. those with one member each, then please show that code in your question. Many people will then be happy to help you with setting up the more complex loop construct. – Yunnosch Jul 17 '18 at 06:50
  • I have not clearly understood your problem. But I think this should help [Subarrays & Substrings](https://www.geeksforgeeks.org/subarraysubstring-vs-subsequence-and-programs-to-generate-them/) – deltaforce Jul 17 '18 at 06:51
  • 1
    Why isn't `{}` a possible subset? You need to explain what you don't understand about the other answers you have seen, because otherwise you might simply get the same answer again. – Andy Turner Jul 17 '18 at 06:54
  • It looks like you're doing Leetcode problem #78. Also check out the other solutions there: https://leetcode.com/problems/subsets/discuss/ – Spangen Jul 17 '18 at 07:02

2 Answers2

1

If you are not getting the code of others or algorithms, I would like to try to put it in simple words. Hope you know how binary to decimal conversions happen.

Binary to Decimal conversion

Source: www.wikihow.com

  1. In the same way, just take an array of bits of size equal to the size of the given array
  2. Generate all the possible combinations using 0 & 1
  3. With each combination, you can generate a set. How? For that, if the combination is having 1, then include the respective element at the same index of the given array into the set else if it is 0, then exclude that element from the set
  4. This will also generate duplicate sets but those can be avoided if you compare all the sets.

    Hope this would help.

vkrishna17
  • 906
  • 10
  • 17
0

Sounds to me like you're having a hard time understanding the actual problem yourself. I think you can "see" the final solution (your list of possible sets), but not the simple steps you are taking to get there.

I recommend you sit down with a pen and a piece of paper, and work through the solution manually, step by step, until you understand properly what you are trying to achieve, and how you would need to think about the problem in order to solve it. Once you've done that, putting your thoughts into code should be a lot easier.

It's just a question of breaking your programming problem into a series of simple steps.

Kjartan
  • 18,591
  • 15
  • 71
  • 96