Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Threaded View

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Algorithm test question

    Here is the question: Given an array of 1000 elements in sorted order what is the
    largest possible value that will be printed when the array is
    passed to method mystery?


    And here is the mystery code
    public int mystery(int[] v, int t) { 
     
     int w = 0; 
     int h = v.length - 1; 
     int c = 0; 
     while(w <= h)
     { 
     
    c++; 
     int m = (w + h) >>> 1; 
            if(v[m] < t) {h = m - 1}; 
     
         else if(v[m] > t) {w = m + 1}; 
     
        else { 
     
    System.out.print(c); 
     
    return m; 
     } 
     } 
     
     System.out.print(c); 
     
    return -(w + 1); 
    }

    Could someone explain it to me how this is 9? It would be greatly appreciated.
    Last edited by javapava; March 16th, 2013 at 02:49 PM. Reason: make code more readable and understandable


Similar Threads

  1. Test Question(Introduction Course)
    By Daler in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 19th, 2012, 11:10 PM
  2. algorithm question
    By amandu in forum Java Theory & Questions
    Replies: 2
    Last Post: October 11th, 2012, 08:20 AM
  3. Challenging Java Question: Test your Java skill by grouping these terms
    By karthickk3 in forum Java Theory & Questions
    Replies: 3
    Last Post: July 18th, 2012, 10:10 PM
  4. Exception in JUnit test (easy question)
    By opium in forum Exceptions
    Replies: 1
    Last Post: February 14th, 2012, 09:09 AM