Question: C# 1. Given an array of sorted strings, locate a string and return its index. If the string cannot be found, return a -1. This
C#
1. Given an array of sorted strings, locate a string and return its index. If the string cannot be found, return a -1. This will require to use a string.CompareTo(anotherString) method, which returns negative (less than), zero (equal), or positive (greater than).
public int Search(string[] haystack, string needle) {
2. Given a positive number, use binary search to determine its cube root without using any of the built-in Math functions. As you know, result * result * result must equal to n. The error must be less than 1E-14.
public double CubeRoot(double n) {
3. Given a sorted array of positive integers, count the number of occurrences for each element in the array. That is, the values of this array can only contain between 0 and the value of the last element, which is bounded. The method will produce a new array representing the frequencies, which will include all positive values even if they do not appear in the array a. This will require recursions. Return the array contains the frequencies corresponding to each number as the index, starting from 0.
public int[] CountNumbers(int[] a) {
4. Given a sorted array with all the elements appearing twice (one after another) but only one element appearing once. Locate that element with a complexity of O(log(n)) using the binary search technique.
public int AppearOnce(int[] a) {
5. Given a sorted array of n elements containing elements in the range from 1 to n - 1 with only one element occurring twice so that the element will be consecutive unless the element is repeating. The task is to find the repeat element in the array. An array with consecutive numbers from 1 to n-1 with one repeated value. Return repeated element.
public int RepeatingElement(int[] a) {
6. Given a sorted array and an x value, the floor of x is the largest element in the array that is smaller than or equal to the value of x. Identify the floor of x. If a floor cannot be determined (e.g. there are no values in a that are smaller than x), simply return the first element of the array a.
public int Floor(int[] a, int x) {
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
