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)
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.
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
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array...
(a) Consider the general k-ary search algorithm (generalization of binary search) which splits a sorted array of size n into k subsets each of size n/k and recursively searches only one of these k subsets. Which one of these k subsets should be searched is decided by making (k − 1) comparisons, each of which can be done in constant time. Clearly, the recurrence relation for this k-ary search algorithm can be written as, T(n) = T(n/k) + (k −...
Write a program to remove an element from an array at the given position k and...
Write a program to remove an element from an array at the given position k and push the rest of the array elements one position back. Then insert the removed element at the beginning. Position k is entered through keyboard. For example, if the original array x is {'r', 'c', 'm', '7', 'w', '3', 'q'} and k = 3, the array will be changed to {'7', 'r', 'c', 'm', 'w', '3', 'q'}. Hint: Sequence of moving the element is important....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85....
Q1. Write a Java program to do sequential search to find element 55 in array 10,20,35,45,55,65,75,85.                                                                                                                                                                    Q2.Write a Java program to find element 54 in array 45,41,65,53,76,90 using Binary Search. (Hint: Binary search is applied to sorted array elements) Q4.   Write a java program to create array list subject        - add English, Maths, Science to the list        - add Computers at index 2        - display first occurrence index of Maths        - traverse the list using...
Given an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array.
C++ Programming using iostream and namespace stdGiven an array of integers and the size of the array, write a function findDuplicate which prints the duplicate element from the array. The array consists of all distinct integers except one which is repeated. Find and print the repeated number. If no duplicate is found, the function should print -1. void findDuplicate (int [ ], int)Example 1: Given array: {2,3,5,6,11,20,4,8,4,9} Output: 4 Example 2: Given array: {1,3,5,6,7,8,2,9} Output: -1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT