Complete these 2 Java functions (the instructions are attached below):
private static int minIndexBinarySearch(int array[], int
arrayLength, int key) {
// Complete this function.
}
private static int maxIndexBinarySearch(int array[], int
arrayLength, int key) {
// Complete this function.
}
In certain applications, we are interested in counting the
number of times key appears in a sorted array. The technique to
solve such problems is to determine:
- minIndex: the minimum index where key appears
- maxIndex: the maximum index where key appears
The number of occurrences is given by (maxIndex - minIndex +
1).
Hence, our task is to find both the minimum and maximum positions
where a key occurs. We seek to solve this using Binary Search. To
this end, complete the following functions:
- int minIndexBinarySearch(int array[ ], int arrayLength, int key):
returns the minimum index where key appears. If key does not
appear, then returns -1.
- int maxIndexBinarySearch(int array[ ], int arrayLength, int key):
returns the maximum index where key appears. If key does not
appear, then returns -1.
Now, the countNumberOfKeys method returns the number of occurrences
of key using the above two functions, and the formula above.
Caution: Your code should have complexity O(log n), where n
= arrayLength. If your code ends up scanning the entire array (has
a complexity O(n)), your work won't be counted.
class BinaryCounter { public static int firstIndex(int array[], int low, int high, int key, int arrayLength) { if (high >= low) { int mid = low + (high - low) / 2; //get mid element //found the firstIndex element if ((mid == 0 || key > array[mid - 1]) && array[mid] == key) { return mid; } //check in right else if (key > array[mid]) { return firstIndex(array, (mid + 1), high, key, arrayLength); } //check in left else { return firstIndex(array, low, (mid - 1), key, arrayLength); } } return -1; } public static int lastIndex(int array[], int low, int high, int key, int arrayLength) { if (high >= low) { int mid = low + (high - low) / 2; //found the last index of element if ((mid == arrayLength - 1 || key < array[mid + 1]) && array[mid] == key) { return mid; } //check in left else if (key < array[mid]) { return lastIndex(array, low, (mid - 1), key, arrayLength); } //check in right else { return lastIndex(array, (mid + 1), high, key, arrayLength); } } return -1; } static int minIndexBinarySearch(int array[], int arrayLength, int key) { return firstIndex(array, 0, arrayLength - 1, key, arrayLength); } static int maxIndexBinarySearch(int array[], int arrayLength, int key) { return lastIndex(array, 0, arrayLength - 1, key, arrayLength); } //count the number of keys static int countNumberOfKeys(int array[], int arrayLength, int key) { int maxIndex = maxIndexBinarySearch(array, arrayLength, key); //find last index int minIndex = minIndexBinarySearch(array, arrayLength, key); //find first index System.out.println("First Occurrence = " + minIndex); System.out.println("Last Occurrence = " + maxIndex); return maxIndex - minIndex + 1; //no of times occur } public static void main(String[] args) { int arr[] = {1, 2, 2, 2, 2, 3, 4, 7, 8, 8}; int n = arr.length; int x = 2; System.out.println("No of times occur = " + countNumberOfKeys(arr, n, x)); } } |
Complete these 2 Java functions (the instructions are attached below): private static int minIndexBinarySearch(int array[], int...
How would answer question #4 of this ? #1 and 2 #include<iostream> #include <fstream> using namespace std; void read(int arr[]) { string line; ifstream myfile("input.txt"); int i = 0; if (myfile.is_open()) { while (getline(myfile, line)) { arr[i]; i++; } myfile.close(); } } void output(int arr[]) { ofstream myfile("output.txt"); if (myfile.is_open()) { int i = 0;...
Develop a set of static methods in a class called Array Tools that perform the functions below, and overload the methods for the specified types: Description Returns the minimum value stored in an array. Array Tools Method char minimum char array[]) int minimum( int array[] ) double minimum double arrayll ) char maximum char array() ) int maximum( int array[] ) double maximum( double array[] ) Returns the maximum value stored in an array. int minimumAt( char array()) int minimumAt(...
1. Write a complete program based on public static int lastIndexOf (int[] array, int value) { for (int i = array.length - 1; i >= 0; i--) { if (array [i] == value) { return i; } } return -1; write a method called lastindexof that accepts an array of integers and an integer value as its parameters and returns the last index at which the value occurs in the array. the method should return -1 if the value is...
Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int j) that exchanges the values stored at indices i and j in the array a. You do not need to worry about cases where either i or j is an invalid index. Give the best estimate you can for its time complexity (b) In an ordered array of n items, how can we determine whether or not an item belongs to the list using...
c++, we have to write functions for the code given below and other instructions for it to compile. I am having issues understanding how to confront the problem and how to write functions and read the program so it can eventually be solved so it can be compiled 7/ * INSTRUCTIONS: Write two functions in the space // * indicated below. // * // * #1 => Find index of maximum value: Write a function that will // * find...
JAVA // TO DO: add your implementation and JavaDoc public class SmartArray<T>{ private static final int DEFAULT_CAPACITY = 2; //default initial capacity / minimum capacity private T[] data; //underlying array // ADD MORE PRIVATE MEMBERS HERE IF NEEDED! @SuppressWarnings("unchecked") public SmartArray(){ //constructor //initial capacity of the array should be DEFAULT_CAPACITY } @SuppressWarnings("unchecked") public SmartArray(int initialCapacity){ // constructor // set the initial capacity of...
TestScores . SIZE: int //size of the array scores: int [ 1 estScores findMax ( ) : int findMin ) int findScore (value: int): bool res(): int Write the constructor method. It fills the array with random number from 0-100. Find below the code to get your started 1. public TestScores () Random rand -new Random); for (int i-0 i<SIZE i+) socresi] -rand.nextInt (101); Finding the maximum value in an array. Write the method findMax. It returns the maximum value...
JAVA Objectives: 1. Apply linear search algorithm 2. Apply select sort algorithm 3. Apply array iteration skill Problem description: Write the following eight methods and write a main function to test these methods // return the index of the first occurrence of key in arr // if key is not found in arra, return -1 public static int linearSearch(int arr[], int key) // sort the arr from least to largest by using select sort algorithm public stati void selectSort(int arr[])...
Write a method public static boolean FindSum (int[] a, int m) that takes an ascending sorted array a with n distinct integers, and an integer m. If there are two elements in the array that add to be m, the method returns True; if not, it returns False. You may assume that the elements in the array are all distinct. There are several ways to solve this problem! (and some solutions are worth more points than others!) For 6 points,...
COMPLETE THE BUCKETSORT METHOD public static void bucketSort(int[] array) { int bucketCount = array.length/2; int minIntValue = 0; int maxIntValue = array.length - 1; // Create bucket array List<Integer>[] buckets = new List[bucketCount]; // Associate a list with each index in the bucket array for(int i = 0; i < bucketCount; i++){ buckets[i] = new LinkedList<>(); } //...