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 5843355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:08:35+00:00 2026-05-22T12:08:35+00:00

Possible Duplicate: Java : Summation of multiples of 5 in a group to a

  • 0

Possible Duplicate:
Java : Summation of multiples of 5 in a group to a given target

Hi SO People,

I’m struggling to get the below problem working with no approach in right direction.

Write a java function such that 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 these additional constraints: all multiples of 5 in the array must be included in the group. If the value immediately following a multiple of 5 is 1, it must not be chosen.

  • groupSum5(0, {2, 5, 10, 4}, 19) → true
  • groupSum5(0, {2, 5, 10, 4}, 17) → true
  • groupSum5(0, {2, 5, 10, 4}, 12) → false
  • groupSum5(0, {3, 5, 1}, 5) → true
  • groupSum5(0, {3, 5, 1}, 4) → false

The function siganture is public boolean groupSum5(int start, int[] nums, int target)

I have written the partial code but there are failing test cases for the same.

     public boolean groupSum5(int start, int[] nums, int target) {     
           start = 0;     
           boolean flag = false;     
           for(int i=0;i<nums.length;i++){     
             if(nums[i]%5==0){     
                  start+=nums[i];                   
             }else if((start != target) && (start%5==0)){     
                  start+=nums[i];       
             }else if(start == target){      
                  flag = true;      
                  return flag;     
             }else if((nums[i]%5==0) && (nums[i+1]==1)){      
                  continue;                
             }    
           }
            return flag;      
     }     

All the test cases are failing even after writing this code.
Im struggling to get this rite for a long time.

EDIT for DIANTE:Could you provide me with the code fix since i have tried this much and i dont know how to proceed

  • 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-22T12:08:36+00:00Added an answer on May 22, 2026 at 12:08 pm

    here’s a solution, but see discussion after:

    package so5987154;
    
    import java.util.Collection;
    import java.util.List;
    import java.util.Set;
    
    import com.google.common.collect.Lists;
    import com.google.common.collect.Sets;
    
    public class Summation {
        /**
         * Sum of start and every element of the collection.
         * 
         * @param start
         *            starting value for the sum
         * @param list
         *            Collection to sum.
         * @return the sum.
         */
        private int sum(final Integer start, final Collection<Integer> list) {
            int sum = start;
    
            for (final int i : list) {
                sum += i;
            }
    
            return sum;
        }
    
        /**
         * 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 these additional constraints: all multiples of 5 in the array must be included in the group. If the value immediately
         * following a multiple of 5 is 1, it must not be chosen.
         * 
         * @param start
         *            not used
         * @param nums
         *            array of int (input)
         * @param target
         *            target value for the summation
         * @return true if we found a group that meet the criteria.
         */
        public boolean groupSum5(final int start, final int[] nums, final int target) {
            // list of values that need to be included (multiple of 5)
            final List<Integer> fixed = Lists.newArrayList();
    
            // list of value that can be included (anything but 1 preceded by a multiple of 5)
            final List<Integer> candidates = Lists.newArrayList();
    
            // 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];
                    if (prev % 5 != 0) {
                        candidates.add(cand);
                    }
                } else if (cand % 5 == 0) {
                    fixed.add(cand);
                } 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; // NOPMD
            }
    
            // if the sum of fixed is greater than target we don't need to do anything because
            // we already know we need to return false (all multiple of 5 must be included)
            // If candidates is empty there's no way we can achieve the desired goal.
            if (sumFixed <= target && !candidates.isEmpty()) {
                // generates all subsets of candidates:
                // { 1, 2 } => {}, {1}, {2}, {1, 2}
                final Set<Set<Integer>> powerSet = Sets.powerSet(Sets.newHashSet(candidates));
    
                // for each found subset, computes the sum of the subset and add it to the sum of
                // multiples of 5 then compare it to target. If equals => return true.
                for (final Set<Integer> set : powerSet) {
                    if (sumFixed + sum(0, set) == target) {
                        return true; // NOPMD
                    }
                }
            }
    
            return false;
        }
    }
    

    Associated test:

    package so5987154.tests;
    
    import static org.junit.Assert.assertFalse;
    import static org.junit.Assert.assertTrue;
    
    import org.junit.Test;
    
    import so5987154.Summation;
    
    @SuppressWarnings("PMD")
    public class SummationTest {
        private final Summation s = new Summation();
    
        @Test
        public void testGroupSum5() {
            assertTrue(s.groupSum5(0, new int[] { 2, 5, 10, 4 }, 19));
            assertTrue(s.groupSum5(0, new int[] { 2, 5, 10, 4 }, 17));
            assertFalse(s.groupSum5(0, new int[] { 2, 5, 10, 4 }, 12));
            assertTrue(s.groupSum5(0, new int[] { 2, 5, 10, 4 }, 19));
            assertTrue(s.groupSum5(0, new int[] { 3, 5, 1 }, 5));
            assertFalse(s.groupSum5(0, new int[] { 3, 5, 1 }, 4));
            assertTrue(s.groupSum5(0, new int[] { 3, 1 }, 4));
            assertFalse(s.groupSum5(0, new int[] { 3, 1 }, 2));
            assertTrue(s.groupSum5(0, new int[] { 1 }, 1));
        }
    }
    

    BUT, your signature parameter start suggest something with recursion. In a first step, you could remove from the array of ints:

    • all multiple of 5 and sum those into start
    • all 1 preceded by a multiple of 5

    then call you method with start and the new array of int.

    In the method, you need to:

    • test if start is equals to target => return true
    • test if start is over target => return false
    • test if array is empty => return false
    • call the method with start + x where x is an element of the array and array with x removed => return OR of all the result

    Example: { 2, 5, 10, 4 }, target = 19

    sum of multiple of 5: 5+10 = 15, no 1 preceded by 5 => new array { 2, 4 }

    first call: method(15, {2, 4}, 19)

    • start == target => NO
    • start > target => NO
    • array empty => NO
    • r1 = method(15+2, {4}, 19) and r2 = method(15+4, {2}, 19)

    second call (r1): method(15+2, {4}, 19)

    • start == target => NO
    • start > target => NO
    • array empty => NO
    • r11 = method(15+2+4, {}, 19)

    third call (r11): method(15+2+4, {}, 19)

    • start == target => NO
    • start > target => YES => false

    second call (r2): method(15+4, {2}, 19)

    • start == target => YES => true

    we are back in the first call with r1 = r11 = false and r2 = true => return false OR true = true, END

    You can see that Sets.powerSet is equivalent to the recursive call r(k)

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

Sidebar

Related Questions

Possible Duplicate: Java - Regex problem I have list of URLs of types: http://www.example.com/pk/etc
Possible Duplicate: Working with latitude/longitude values in Java Duplicate: Working with latitude/longitude values in
Possible Duplicate: How do you find all subclasses of a given class in Java?
Possible Duplicate: Java GUI repaint() problem? I write a Java code, but I have
Possible Duplicate: Java Generics In Eclipse, I am given warnings for using 'rawtypes' and
Possible Duplicate: Java string comparison? I have encounter the following problem, I have an
Possible Duplicate: Java garbage collector - When does it collect? When people say that
Possible Duplicate: Java: Parse a mathematical expression given as a string and return a
Possible Duplicate: java for-loop problem Why is the output of following code: for (float
Possible Duplicate: Java Regex Replace with Capturing Group Is there any way to replace

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.