Question

In: Computer Science

An increasing-order integer array may contain duplicate elements. Write a Java method that does a binary...

An increasing-order integer array may contain duplicate elements. Write a Java method that does a binary search through the array for finding a specific target and return an array of two integer indices that specifies the close interval of indices at which the target is found or null if the target is not present. Use this test driver to test

public class MultBS

{

int [] multBinarySearch(int [] x, int target)

{

// your codes go here

}

public static void main(String [] args)

{ int [] x = new int[]{2,3,3,4,4,4,5,6,7,7,7};

int [] range = multBinarySearch(x, 3);

System.out.println("Target was found from position " + range[0] + " to " + range[1]);

}

}

test your program to make sure you see

the printout is

"Target was found from position 1 to 2"

Solutions

Expert Solution

import java.io.*;
public class MultBS
{
    private static int first_BS(int [] x, int target, int l, int r)
    {
        if(l>r)
            return -1;

        int mid = (r-l)/2+l;

        if(x[mid] == target && (mid==0 || x[mid-1] != target))
            return mid;
        else if(x[mid] == target)
            return first_BS(x, target, l, mid-1);
        else if(x[mid]>target)
            return first_BS(x, target, l, mid-1);
        else
            return first_BS(x, target, mid+1, r);
    }

    private static int last_BS(int [] x, int target, int l, int r)
    {
        if(l>r)
            return -1;

        int mid = (r-l)/2+l;

        if(x[mid] == target && (mid==x.length-1 || x[mid+1] != target))
            return mid;
        else if(x[mid] == target)
            return last_BS(x, target, mid+1, r);
        else if(x[mid]>target)
            return last_BS(x, target, l, mid-1);
        else
            return last_BS(x, target, mid+1, r);
    }
    private static int [] multBinarySearch(int [] x, int target)
    {
        int[] ans={-1,-1}; // -1 represents null
        ans[0] = first_BS(x, target, 0, x.length-1);
        ans[1] = last_BS(x, target, 0, x.length-1);
        return ans;
    }

    public static void main(String [] args)
    {
        int [] x = new int[]{2,3,3,4,4,4,5,6,7,7,7};
        int [] range = multBinarySearch(x, 3);
        System.out.println("Target was found from position " + range[0] + " to " + range[1]);
    }

}


Related Solutions

JAVA Write a method that removes the duplicate elements from an array list of integers using...
JAVA Write a method that removes the duplicate elements from an array list of integers using the following header: public static void removeDuplicate(ArrayList list) Write a test program that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is a sample run: Enter ten integers: 10 20 30 20 20 30 50 60 100 9 The distinct integers are: [10, 20, 30, 50, 60, 100, 9]
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method...
Write a Java method that removes any duplicate elements from an ArrayList of integers. The method has the following header(signature): public static void removeDuplicate(ArrayList<Integer> list) Write a test program (with main method) that prompts the user to enter 10 integers to a list and displays the distinct integers separated by exactly one space. Here is what the input and output should look like:      Enter ten integers: 28 4 2 4 9 8 27 1 1 9      The distinct...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method...
Write a recursive method to implement Binary Search of a sorted integer array. Signature of method could be public int BinarySearch(int target, int low, int high)
// Java // This method takes an integer array as well as an integer (the starting...
// Java // This method takes an integer array as well as an integer (the starting // index) and returns the sum of the squares of the elements in the array. // This method uses recursion. public int sumSquaresRec(int[] A, int pos) { // TODO: implement this method        return -1; // replace this statement with your own return }    // This method takes a character stack and converts all lower case letters // to upper case ones....
Create a Java program with a method that searches an integer array for a specified integer...
Create a Java program with a method that searches an integer array for a specified integer value **(see help with starting the method header below). If the array contains the specified integer, the method should return its index in the array. If not, the method should throw an Exception stating "Element not found in array" and end gracefully. Test the method in main with an array that you make and with user input for the "needle". starting header ** public...
Assume that a given binary tree stores integer values in its nodes. Write a Java method...
Assume that a given binary tree stores integer values in its nodes. Write a Java method "smallest" that returns the smallest item in the tree.
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain...
Please write in JAVA 1. Given the following array-based ADT list called colorList whose elements contain strings             red, orange, yellow, blue, indigo, violet write the statement to insert the String element “pink” to the end of the list. Assume the front of the list is on the left. 2. Outline the basic steps to remove a node from the beginning of a list. Completed answers will be given an immediate upvote :)
Write a java script function that accepts integer array as input, and display a new array...
Write a java script function that accepts integer array as input, and display a new array by performing fol lowing modifications, • The values in odd index positions must be incremented by 1 • The values in even index positions must be decremented by 1. • Assume the array index starts from 0. Input 1: arr = [1,2,3,4] Output 1: arr = [0,3,2,5 it done html and javascript and plz do it in simple way
Write a c program Write a function to swap two elements of an integer array. Call...
Write a c program Write a function to swap two elements of an integer array. Call the function to swap the first element, i[0] with last element i[n], second element i[1] with the last but one element i[n-1] and so on. Should handle arrays with even and odd number of elements. Call the swap function with the following arrays and print results in each case before and after swapping. i. int arr1[] = {0, 1, 2, 3, 30, 20, 10,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT