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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:01:25+00:00 2026-05-21T15:01:25+00:00

I have the following problem I need to solve, but is strugling a bit.

  • 0

I have the following problem I need to solve, but is strugling a bit. Would really appreciate it if someone can help.

In short in comes down to the following:

  1. If the search key is in the array – it returns the smallest index i for which a[i] is equal to the key
  2. If the search key is not in the array but greater – it returns the smallest index i as -i where a[i] is greater than the key
  3. If the search key is not in the array but smaller – it returns -j where j is the index of the last element in the array

I have the code of searching for the key, but I’m not sure how to return the indexes as mentioned above…

import java.util.Arrays; 
public class BinarySearchIndex2 {

    // a working recursive version
    public static int search(String key, String[] a) {
        return search(key, a, 0, a.length);
    }

    public static int search(String key, String[] a, int lo, int hi) {
        // possible key indices in [lo, hi)
        if (hi <= lo) return -1;

        int mid = lo + (hi - lo) / 2;
        int cmp = a[mid].compareTo(key);
        if      (cmp > 0) return search(key, a, lo, mid);
        else if (cmp < 0) return search(key, a, mid+1, hi);
        else              return mid;
    }

    public static void main(String[] args) {

        String key = args[0];
        int sizeoflist = StdIn.readInt();
        String[] a = new String[sizeoflist];
            int counter = 0; //counter for while loop to fill array a

        while (!StdIn.isEmpty()){

            a[counter] = StdIn.readString();
            counter++;

        }

        Arrays.sort(a); // sort the words (if needed)

        if ( search(key, a) < 0) ; /* System.out.println();*/
        else if ( search(key, a) > 0 ) ;
        else if ( search(key, a) = 0 ) ;

    }
}

Would be really glad if someone can help me with this matter…

Thanks!

  • 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-21T15:01:26+00:00Added an answer on May 21, 2026 at 3:01 pm

    String.compareTo performs a lexicographical comparison. This means that it can decide that "50">"100", whereas clearly 50<100 is what you would expect. This will affect your Arrays.sort call so your Array is already messed up.

    Is it a must to have public static int search(String key, String[] a) as your API?

    If you can change it to be public static int search(int key, int[] a) it will make your API work (assuming you don’t have any bugs I missed).

    Hope this was what you were referring to.

    Edit: some fine tuning to the problem analysis.

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

Sidebar

Related Questions

I need some help for a problem which I can't solve but I think
I have the following problem to solve and I can't seem to be able
Good afternoon, I need help to solve the following problem: I need to restrict
I would like to solve the following problem using constraints but i actually don't
I do have following problem to solve: I have a simple but big gridmap
I need to solve the following problem but I still dont know how. First
I need to solve the following problem: I have multiple rectangles of sizes: width
I need to solve the following problem. //pseudo algorithm you have four elements: elm1,
I can't understand how to solve the following problem: I have input string aaaabaa
I have the following problem: I need to use XSLFO to generate a 2-column

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.