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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T19:39:25+00:00 2026-05-18T19:39:25+00:00

I had a recursion interview question problem in Java,Need your help on this. Write

  • 0

I had a recursion interview question problem in Java,Need your help on this.

Write a **Java function** such that :: Given an array of ints, is it possible to divide the ints into two groups, so that the sum of the two groups is the same, with these constraints: all the values that are multiple of 5 must be in one group, and all the values that are a multiple of 3 (and not a multiple of 5) must be in the other. (No loops needed.)


split53({1,1}) → true
split53({1, 1, 1}) → false
split53({2, 4, 2}) → true

PS:This was a Interview Question for hewlett packard

  • 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-18T19:39:26+00:00Added an answer on May 18, 2026 at 7:39 pm

    The question can be easily reduced to following: given a set of integers numbers and an integer target, is it possible to find a subset of numbers with sum equal to target?
    Let me know if transition needs clarification.

    It can be solved with DP in O(numbers.size * target) time. The idea is following

    1. When numbers.size is 0, the only reachable sum is 0.
    2. Suppose we have numbers == {1, 3}, in this case sums {0, 1, 3, 4} are available. What if we add another element to numbers, 4? Now, all old sums can still be reached and some new ones too: {0 + 4, 1 + 4, 3 + 4, 4 + 4}. Thus, for numbers == {1, 3, 4}, available sums are {0, 1, 3, 4, 5, 7, 8}.
    3. In this fashion, adding number by number, you can build the list of reachable sums.

    A working example (it doesn’t handle negative numbers, but you can easily fix that)

    boolean splittable(int[] numbers, int target) {
        boolean[] reached = new boolean[target + 1];
        reached[0] = true;
    
        for (int number : numbers) {
            for (int sum = target - 1; sum >= 0; --sum) {
                if (reached[sum] && sum + number <= target) {
                    reached[sum + number] = true;
                }
            }
        }
    
        return reached[target];
    }
    

    Run it

    System.out.println(splittable(new int[]{3, 1, 4}, 7)); // => true
    System.out.println(splittable(new int[]{3, 1, 4}, 6)); // => false
    

    edit
    I just noticed the “recursion” part of the requirement. Well, DP can be rewritten as recursion with memoization, if that’s the hard requirement. This would preserve runtime complexity.

    edit 2
    On groups. You have to assign elements divisible by 3 or 5 to respective groups before you proceed with the algorithm. Let’s say, sum of all elements is s, sum of elements divisible by 3 is s3 and sum of elements divisible by 5 but not 3 is s5. In this case, after you assigned those ‘special’ elements, you have to split the rest that sum in one group is s/2 - s3 and in another s/2 - s5.

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

Sidebar

Related Questions

I had a problem this week (which thankfully I've solved in a much better
My professor has asked us to write a program that uses recursion to solve
Had a coworker ask me this, and in my brain befuddled state I didn't
I had a discussion with some colleagues mentioning that there are not too many
I had to delete all the rows from a log table that contained about
I had the idea of a search engine that would index web items like
I like recursion, but at Java you meet an dead end at some point.
Had I looked into the Java SE6 documentation sooner on Context and InitialContext, I
We had a very strange issue on our servers this week, where a method
I had some code that I developed on Ubuntu and now I am trying

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.