Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am printing subsets from an array whose sum has been specified, while avoiding duplicates.

I think there may be improvements, potentially on how using map is bad idea, or how can I avoid map to filter the duplicates.

Additionally, in my code I want to reverse the sort order of an int array using streams which is asked over here, are there any alternatives rather than sorting and reversing the array?

public class FindSubSetArray {

    private static final int TARGET_SUM = 24;
    private static Map<String,Integer> subSet = new HashMap<>();
    private static int count = 0;

    public static void main(String[] args) {
        int[] array= {2, 5, 1, 2, 4, 1, 6, 5, 2, 2};
        Arrays.sort(array);
        findSubset(array, 0, 0, "");
        subSet.keySet().stream().forEach(System.out::println);
    }

    public static void findSubset(int[] array, int index, int current, String subSetString) {

        if (current > TARGET_SUM || array.length < index) return;

        for (int i = index; i < array.length; i++) {
            int presentSum = current + array[i];
            if (presentSum == TARGET_SUM) {
     //         System.out.println(subSetString + " " + array[i]);
                subSet.put(subSetString + " " + array[i], count++);
            } else {
                findSubset(array, i + 1, presentSum, subSetString + " " + array[i]);
            }
        }
    }
}
share|improve this question
    
Can you clarify exactly what you are trying to accomplish? I find it hard to understand your goal. Maybe an example? – Diego Martinoia Jan 18 at 15:38
    
Above code is to find the sub array from a given array whose some is "24" in that process i might get duplicates based on the given array of elements. SO to avoid it i have used map. Is there a better algorithm to do or avoid map and improve my code. I am asking for suggestions to improve my code. – mallikarjun Jan 20 at 13:11
    
Does your array only contains >= 0 values? Or can it also have negatives? because it makes quite the difference! – Diego Martinoia Jan 20 at 13:22
    
I can't see how your hashmap does the job – but possibly I just do not understand the job. What are 'duplicates' you're trying to avoid? Certainly "2 1" and "2 1" would be duplicates even if the first one is taken from positions 0 and 2, and the other one from 3 and 5. But are "2 1" and "1 2" considered duplicates? – CiaPan Jan 21 at 15:08
    
(Don't comment comments asking for additional information or clarification: edit the question. Do not ask essentially unrelated questions in one post - and don't expect alternatives to Arrays.sort()&reverse to be faster for arrays not almost sorted) You not only exclude equal sequences of numbers, but overlapping ones, too - intentionally? Check what happens after the if (==) put() else recursive call. (subSet makes me think of "no regard to position" or what is called (non-contiguous) subsequence - use slice or subArray?) – greybeard Jan 23 at 15:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.