Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5935389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:14:50+00:00 2026-05-22T15:14:50+00:00

Given an array of ints, is it possible to choose a group of some

  • 0

Given an array of ints, is it possible to choose a group of some of the ints, such that the group sums to the given target, with this additional constraint: if there are numbers in the array that are adjacent and the identical value, they must either all be chosen, or none of them chosen. For example, with the array {1, 2, 2, 2, 5, 2}, either all three 2’s in the middle must be chosen or not, all as a group. (one loop can be used to find the extent of the identical values).

The test scenarios are below

groupSumClump(0, {2, 4, 8}, 10) → true true OK      
groupSumClump(0, {1, 2, 4, 8, 1}, 14) → true true OK      
groupSumClump(0, {2, 4, 4, 8}, 14) → false false OK      
groupSumClump(0, {8, 2, 2, 1}, 9) → true false X   --->Failing   
groupSumClump(0, {8, 2, 2, 1}, 11) → false false OK      
groupSumClump(0, {1}, 1) → true false X      --->Failing
groupSumClump(0, {9}, 1) → false false OK      
other tests  OK      

Snippet is as below

private int sum(final Integer start, final Collection<Integer> list) {
        int sum = start;

        for (final int i : list) {
            sum += i;
        }

        return sum;
}

   public boolean groupSumClump(final int start, final int[] nums, final int target) {       
        for (int i = 0; i < nums.length-1; i++) {
          if(nums[i] == nums[i+1]){//group selected logic
            int sum = nums[i] + nums[i+1];//is this Ok ?
            nums[i] =sum;
            nums[i+1]=0;
          }else{
          //how to handle the logic for group not selected.               
          }
        }

        final List<Integer> fixed = new ArrayList();
        final List<Integer> candidates = new ArrayList();

        // fills candidates and fixed
        for (int i = 0; i < nums.length; i++) {
            final int cand = nums[i];

            if (cand == 1 && i > 0) {
                final int prev = nums[i - 1];                    
            }else if (cand < target) {
                candidates.add(cand);
            }
        }

        // compute the sum of fixed
        final int sumFixed = sum(0, fixed);

        // if the sum of fixed is equals to target we don't need to do 
        //anything because we already know we need to return true.
        if (sumFixed == target) {
            return true; 
        }            
        if (sumFixed <= target && !candidates.isEmpty()) {
         final Set<Set<Integer>> powerSets = powerSet(new HashSet(candidates));               
            for (final Set<Integer> set : powerSets) {
                if (sumFixed + sum(0, set) == target) {
                    return true; 
                }
            }
        }

        return false;        
}      

 public <T> Set<Set<T>> powerSet(Set<T> originalSet) {       
      Set<Set<T>> sets = new HashSet<Set<T>>();
      if(originalSet.isEmpty()) {
        sets.add(new HashSet<T>());
        return sets;
      }
List<T> list = new ArrayList<T>(originalSet);
T head = list.get(0);
Set<T> rest = new HashSet<T>(list.subList(1, list.size())); 
for (Set<T> set : powerSet(rest)) {
    Set<T> newSet = new HashSet<T>();
    newSet.add(head);
    newSet.addAll(set);
    sets.add(newSet);
    sets.add(set);
}       
return sets;
}  

could you let me whats the problem with the code and why is it failing for test scenarios as mentioned.

i want to know what is the logic for group not selected?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:14:51+00:00Added an answer on May 22, 2026 at 3:14 pm

    Here is the full solution which passes all your test cases.

    Please edit yourself to make it fit to your APIs ^_^

    public static void main(String[] args) {
        int nums [] = new int[]{2, 4, 8};
        int target = 10;
        int nums_another [] = grouped (nums);
        System.out.println(viable(0, nums_another, 0, target));
    }
    
    private static int [] grouped (int nums []) {
        int nums_another[] = new int [nums.length];
        int i = 0;
        int j = 0;
        i++;
        int c = 1;
        while (i < nums.length){
            if (nums[i] == nums[i-1]) { // count identical numbers
                c++;
            }
            else { // not identical, store sum of previous identical numbers (possibly only 1 number)
                if (nums[i-1] != 0) {
                    nums_another[j] = nums[i-1] * c;
                    j++;
                }
                c = 1;
            }
            i++;
        }
        if (nums[i-1] != 0) { // store last
            nums_another [j] = nums[i-1] * c; 
        }
        return nums_another;
    }
    
    /* partial_sum + sub array of "array from start to 0's" -> target */
    private static boolean viable (int partial_sum, int array[], int start, int target) {
        if (partial_sum == target) {
            return true;
        }
        else if (start >= array.length || array[start] == 0) {
            return false;
        }
        else { // Key step
            return viable (partial_sum + array[start], array, start + 1, target)
                || viable (partial_sum,                array, start + 1, target);
        }
    }
    

    Key step:

    return whether target is viable through sub array, test both cases start is included or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array of ints like this: [32,128,1024,2048,4096] Given a specific value, I
Given an array of unsorted positive ints, write a function that finds runs of
CodingBat > Java > Array-1 > reverse3 : Given an array of ints length
This problem is taken from interviewstreet.com Given array of integers Y=y1,...,yn, we have n
Given an array A of 10 ints , initialize a local variable called sum
Problem Statement: Given an array of ints, compute if the array contains somewhere a
I have a string. I need to replace all instances of a given array
Given an array of objects stored in $my_array , I'd like to extract the
Given an array of n Objects, let's say it is an array of strings
Given an array of characters which forms a sentence of words, give an efficient

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.