DO NOT CHANGE THE METHOD HEADER
Write a method
int indexFirstOne (int [] input)
The input array is sorted and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst case runtime must be O(logn) where n is the number of elements.
Example a = [0, 0, 1, 1, 1] return 2
// C++ program
#include <bits/stdc++.h>
using namespace std;
int binarySearch(int a[], int left, int right, int key)
{
if (right < left)
return -1;
int mid = left + (right - left) / 2; //calculate mid
if (a[mid] == key) //element found
return mid;
if (a[mid] > key)
return binarySearch(a, left, mid - 1, key); //check left sub array
return binarySearch(a, mid + 1, right, key); //check right sub array
}
int indexFirstOne(int a[])
{
int n = 5; // size of array
int index = binarySearch(a, 0, n - 1, 1); //find index of 1 by binary search in array
// element not found
if (index == -1)
return -1; //return -1
int left = index - 1;
while (left >= 0 && a[left] == 1) //find element on left side of this index, if more 1 exist to get index of leftmost 1
left--;
return left+1; //return leftmost index
}
int main()
{
int a[] = {0,0,1,1,1};
cout << indexFirstOne(a);
return 0;
}
DO NOT CHANGE THE METHOD HEADER Write a method int indexFirstOne (int [] input) The input...
Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The input array is sorted, and every element is 0 or 1. Return the index of the first 1. If there are no 1s in the array, return -1. The worst-case runtime must be O(logn)where n is the number of elements (no credits for slower runtimes) Example: a = [0,0,1,1,1] return 2 a = [ 0,0,0,1] return 3 a = [0,0,0] return -1 int indexFirstOne...
1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array of distinct integers, and an integer x, return the index of x in the array. If the element x is not present in the array, return -1. "Almost sorted" means the following. Assume you had a sorted array A[0…N], and then split it into two pieces A[0…M] and A[M+1…N], and move the second piece upfront to get the following: A[M+1]…A[N]A[0]…A[M]. Thus, the "almost sorted"...
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,...
write in java 1. Assume the availability of a method named makeLine that can be passed a non-negative integer n and a character c and return a String consisting of n identical characters that are all equal to c. Write a method named printTriangle that receives two integer parameters n and k. If n is negative the method does nothing. If n happens to be an even number, itsvalue is raised to the next odd number (e.g. 4-->5). Then, when k has the value zero, the method prints...
Write a java program: Create a method fillRandom() that accepts an array of int as input and populates it with random numbers in the range -999 to 1000 Explicitly store zero in index [0] and 900 in index [1]. (0 and 900 will be used as search keys) Create a method DisplayLastInts() that accepts an array of int as input and displays the last hundred elements to the screen in rows of 10 elements. Format the output so the 10...
(a) Write a recursive function int find(const int A[], int n, int x); which returns the first index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found. (b) Write a recursive function int rfind(const int A[], int n, int x); which returns the last index i = 0, . . . , n − 1 such that A[i] == x, or n if it is not found....
pls help Write a method void remove(int *a, int index) that will remove the number at the given index and shift all remaining numbers one position to the left in the array a. Assume 1that the last element of the array is -1. Now, write a main function that will define an array int A[40]=[3, 5, 9, 17, 24, -1]; read from user input an index; and call the method remove passing array A and the index given by the...
void merge(Card[] cardArray, int first, int mid, int last) This is a helper method for the merge sort. It takes as parameters a card array, the first index, the middle index, and the end index. The method merges two adjacent sorted arrays into a single sorted one. The first array begins with the element at first and ends with the element at mid. The second array begins with the element at mid + 1 and ends with the element at...
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...
Write a java Program Initialize Array. Write a Java method initArray() with the following header to initialize a one-dimensional array a such that the element values (integers) are twice the index value. For example, if i = 23, then a23 = 46. The function is void with one parameter -- the integer array of a[]. Then write a main() with an int[] array2i size 100 to call