Question

Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The...

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 (int [ ] input) {

0 0
Add a comment Improve this question Transcribed image text
Answer #1

int indexFirstOne(int[ ] input){

// iterating from both sides
   for(int i=0,j=input.length-1;i<=j;i++,j--){

//if it reaches 1 than return i
       if(input[i]==1){
           return i;
       }

//if it reaches 0, than return j+1
       if(input[j]==0)
           return j+1;
   }
   return -1;
}
}

Add a comment
Know the answer?
Add Answer to:
Need help to answer this method in java Write a method int indexFirstOne(int[ ] input) The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • DO NOT CHANGE THE METHOD HEADER Write a method int indexFirstOne (int [] input) The input...

    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

  • 1. Please write a Divide-and-Conquer Java algorithm solving the following problem: Given an "almost sorted" array...

    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 java program: Create a method fillRandom() that accepts an array of int as input...

    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...

  • Write a method List-Double> duplicates(List<Double> input) that returns a List of all duplicates in the input...

    Write a method List-Double> duplicates(List<Double> input) that returns a List of all duplicates in the input List. You may assume occurs either once or twice. The expected runtime must be O(n) where n is elements (no credit for slower runtimes, so you cannot sort the input).(0 Examples: input = [3,4,4] input [3, 7,4, 7,3] return [7, 3] that each element the number of points) return [4] List Double duplicates(List<Double input)(

  • pls help Write a method void remove(int *a, int index) that will remove the number at...

    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...

  • Write a method public static boolean FindSum (int[] a, int m) that takes an ascending 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,...

  • in java Feedback? CHALLENGE ACTIVITY 5.2.2: Printing array elements. Write three statements to print the first...

    in java Feedback? CHALLENGE ACTIVITY 5.2.2: Printing array elements. Write three statements to print the first three elements of array run Times. Follow each statement with a newline. Ex: If runTime = (800,775, 790, 805, 808) print: 800 775 790 Note: These activities will test the code with different test values. This activity will perform two tests, both with a 5 element array. See 'How to Use zyBooks". Also note: If the submitted code tries to access an invalid array...

  • write in java 1. Assume the availability of a method  named  makeLine that can be passed a non-negative...

    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...

  • Array manipulation (a) Write Java code for a method exchange (int [] a, int i, int...

    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...

  • 1.Write code for a Java method, printArray, that takes an int array as parameter and prints...

    1.Write code for a Java method, printArray, that takes an int array as parameter and prints it out, one element per line. public static void printArray (int[] values){ Methods that do NOT return a result have “void” as return type. Notice how an array parameter type is passed, int [] }// end printArray 2.Write code for a Java method that takes in as input parameter an integer number, say num, and returns a double array of size num with all...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT