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

  • Home
  • SEARCH
  • 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 6544763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:30:16+00:00 2026-05-25T11:30:16+00:00

I am looking to create a function in Java that will take in two

  • 0

I am looking to create a function in Java that will take in two arrays or lists and be able to tell if the first array (source) ‘fits into’ the second (target). The target array has values that can not be exceeded in the source array.

For example:

[ 16, 16, 16 ] will not fit into [ 13, 13, 22 ]

[ 12, 12 ]     will fit into [ 16, 16, 12 ]

[ 12, 18, 14 ] will not fit into [ 10, 18, 14 ]

[ 12, 24 ]     will fit into [ 10, 12, 24 ]

[ 10, 10, 10 ] will not fit into [ 10, 10 ]

My current attempt (IANA CS Major!) is ok for 3-element arrays, and that’s all I need to worry about for the short term, but I’m missing some logic in the inner loop that will prevent false negatives.

Gist: https://gist.github.com/1208514

private Boolean designFits(int[] max, int[] design) {

    Boolean designFits = true;

    Arrays.sort(max);
    Arrays.sort(design);
    int passCount = 0;

    if(design.length <= max.length) {

        for(int i = 0; i < max.length; i++) {

            for(int j = 0; j < design.length; j++) {

                if(max[i] <= design[j]) {

                    passCount++;

                }

            }

        }

        if(passCount == 0 || passCount > max.length) {
            designFits = false;

        }


    } else {
        designFits = false;

    }

    return designFits;

}
  • 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-25T11:30:17+00:00Added an answer on May 25, 2026 at 11:30 am

    I think this achieves what you’re looking for:

    private static boolean designFits(int[] source, int[] target) {
    
        //if source is bigger than target, it cannot fit
        if (source.length > target.length) {
           return false;
        }
    
        //sort the arrays
        Arrays.sort(source);
        Arrays.sort(target);
    
        //get the size difference between target and source
        int targetSizeDiff = target.length - source.length;
    
        //walk source:
        for (int i = 0; i < source.length; i++) {
           //compare source's value at index i with target's value at i + difference
           //if it's greater, source cannot fit
           if (source[i] > target[i + targetSizeDiff]) {
              return false;
           }
        }
    
        //at this point we know source can fit
        return true;
    }
    
    public static void main(String[] args) {
    
        //false
        System.out.println(designFits(new int[]{16, 16, 16}, new int[]{13, 13, 22}));
    
        //true
        System.out.println(designFits(new int[]{12, 12}, new int[]{16, 16, 12}));
    
        //false
        System.out.println(designFits(new int[]{12, 18, 14}, new int[]{10, 18, 14}));
    
        //true
        System.out.println(designFits(new int[]{12, 24}, new int[]{10, 12, 24}));
    
        //false
        System.out.println(designFits(new int[]{10, 10, 10}, new int[]{10, 10}));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looking to create a reusable function that will generate a random key with
I'm looking for a library (or preferably built into java) that will able to
I'm looking to create a PHP function that can trim each line in a
I am looking to create a function that could create a fade-in/fade-out function on
I am looking to create a function that could toggle the ability to recurse
I am looking for XML/XHTML Java library/framework that can perform the following two tasks
I'm looking for a PHP library/function/class which can create Identicon s.
I looking to create a custom calender with Zend Framework, I am hoping that
Im looking to create a control that would look like comic baloon. In WPF
I am looking to create symlinks (soft links) from Java on a Windows Vista/

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.