Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to design a binary search method that gets 3 arguments --> bSam(int[] n, int neshoone, boolean inclusive). input: neshoone is a value that

I'm trying to design a binary search method that gets 3 arguments --> bSam(int[] n, int neshoone, boolean inclusive).

input:

"neshoone" is a value that we want to search for.

"n" is an integer array that I want to be sorted and it might get duplicate values. for instance [1, 2, 2, 4]

"inclusive" is a control flag.

output: the neshoone index.

descrip:

When "inclusive = false" then return the index of the LEFT MOST value that is STRICTLY GREATER than the neshoone value.

if the "inclusive = true" then return the index of the RIGHT MOST neshoone value.

if the value that requested does not exist in the input array then return -1.

for instance:

bSam(int[]{1, 2, 2, 2}, 3, true) returns -1 because the target value 3 doesn't exist. 

or

bSam(int[]{1, 2, 2, 4}, 2, true) returns 2 because the index of the right most 2 is 2. 

or also

bSam(int[]{1, 2, 2, 4}, 2, false) returns 3 because the left most value that is greater than 2 is 4. 

and

bSam(int[]{1, 2, 2, 2}, 2, false) returns -1 because the left most value that is greater than 2 doesn't exist. 

image text in transcribed

** 1. public class Solution { 2. * The method for you to implement. 4 5 * Please DO NOT modify the header. 6 7. int bSam(int[] n, int neshoone, boolean i) { 8 return -1; 9 } 10 11. 12 * The main method is for test only and won't be evaluated. 13 14. public static void main(String[] args) { 15 Solution solution = new Solution(); 16 System.out.println(solution.bSam(new int[]{1, 2, 3, 4}, 2, true)); // Should be 1 17 System.out.println(solution.bSam(new int[]{1, 2, 3, 4], 2, false )); // Should be 2 18 } 19 } **

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts of Database Management

Authors: Philip J. Pratt, Mary Z. Last

8th edition

1285427106, 978-1285427102

More Books

Students also viewed these Databases questions

Question

Embodiment.

Answered: 1 week ago