Question

In: Computer Science

Given a sorted array with lot of duplicates, write a problem to search specific element m....

Given a sorted array with lot of duplicates, write a problem to search specific element m. If it’s included in the A, return its minimal index, otherwise return -1. For example A = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4}, if we search 4, you program should return 8. You are only allowed to use binary search. Linear search is not allowed here.

Solutions

Expert Solution

class BinarySearch {
    // Returns index of x if it is present in arr[l..
    // r], else return -1
    public static int binarySearch(int arr[], int l, int r, int x)
    {
        if (r >= l) {
            int mid = l + (r - l) / 2;

            // If the element is present at the mid index
            if (arr[mid] == x)
                return mid;

            // If element is smaller than mid, then it can only be present in left subarray
            if (arr[mid] > x)
                return binarySearch(arr, l, mid - 1, x);

            // Else the element can only be prese in right subarray
            return binarySearch(arr, mid + 1, r, x);
        }

        //element is not present
        return -1;
    }
    
    // modified method to search element in duplicate array
    public static int[] binarySearchDuplicate(int[] array, int l, int r, int n){
        int firstMatch = binarySearch(array, 0, array.length - 1, n);
        int[] resultArray = { -1, -1 };
        
        // element not found
        if (firstMatch == -1) {
            return resultArray;
        }
        
        int leftMost = firstMatch;
        int rightMost = firstMatch;

        for (int result = binarySearch(array, 0, leftMost - 1, n); result != -1;) {
            leftMost = result;
            result = binarySearch(array, 0, leftMost - 1, n);
        }

        for (int result = binarySearch(array, rightMost + 1, array.length - 1, n); result != -1;) {
            rightMost = result;
            result = binarySearch(array, rightMost + 1, array.length - 1, n);
        }

        resultArray[0] = leftMost;
        resultArray[1] = rightMost;
        
        // return the array that has all indices where number can be found
        return resultArray;
    }

    // Driver method to test above
    public static void main(String args[])
    {
        int arr[] = {0, 0, 1, 1, 1, 2, 3, 3, 4, 4, 4, 4, 4};
        int n = arr.length;
        int x = 3;
        int[] result = binarySearchDuplicate(arr, 0, n - 1, x);
        if (result[0] == -1)
            System.out.println("Element not present");
        else
            System.out.println("Element found at index " + result[0]);
    }
}

FOR HELP PLEASE COMMENT.
THANK YOU


Related Solutions

Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice...
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twice and return the new length. Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. Example 1: Given nums = [1,1,1,2,2,3], Your function should return length = 5, with the first five elements of nums being 1, 1, 2, 2 and 3 respectively. It doesn't matter what you leave beyond the returned...
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)
Write a linear-search Java method to test if a String array is already sorted alphabetically. The...
Write a linear-search Java method to test if a String array is already sorted alphabetically. The prototype should be static boolean ifsorted(String [] s) { }
in C programming language Write a function removeDups that removes all duplicates in a given array...
in C programming language Write a function removeDups that removes all duplicates in a given array of type int. Sample Test Case: input -> {1,2,2,2,3,3,4,2,4,5,6,6} output -> {1,2,3,4,5,6,0,0,0,0,0,0} More specifically, the algorithm should only keep the first occurance of each element in the array, in the order they appear. In order to keep the array at the same length, we will replace the removed elements with zeros, and move them to the end of the array.
PYTHON Search for an element in a 2D sorted matrix using the binary search technique. Your...
PYTHON Search for an element in a 2D sorted matrix using the binary search technique. Your code should run in O(m + n) where m is the number of rows and n is the number of columns The python file should have a function named binarysearch The function should take two arguments. The first argument of the function will be the element to search for. Second argument of the function will be the matrix as a List[List]. The function should...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array,...
in java code In the class Hw2, write a method removeDuplicates that given a sorted array, (1) removes the duplicates so that each distinct element appears exactly once in the sorted order at beginning of the original array, and (2) returns the number of distinct elements in the array. The following is the header of the method: public static int removeDuplicates(int[ ] A) For example, on input A=0, 0, 1, 1, 1, 2, 2, 3, 3, 4, your method should:...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the...
6. Given the following array of characters: REALITYSHOWShow how this array will be sorted with the help of : (a) insertion sort; (b) selection sort; (c) bubble sort with swaps counting; (d) bubble sort without swaps counting subject design and analysis of algorithms answer should be like eg p u r p l e p r u p l e
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a...
Problem 1: Given an array A[0 ... n-1], where each element of the array represents a vote in the election. Assume that each vote is given as integers representing the ID of the chosen candidate. Write the code determining who wins the election. Problem 2: How do we find the number which appeared maximum number of times in an array? ( Use Java and an original code )
Given an m × n matrix (or 2-dimensional array) whose rows and columns are sorted, so...
Given an m × n matrix (or 2-dimensional array) whose rows and columns are sorted, so A[i][j]≤ A[i][j+1] and A[i][j]≤ A[i+1][j] Write an algorithm that searches for a specific value in the matrix.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT