Question

In Java: In a sorted (ascending) integer array of length n with no duplicates, print all...

In Java: In a sorted (ascending) integer array of length n with no duplicates, print all values in the range x to y. Assume both x and y are in the array. What is the worst case big O running time if there are k integers within the range?

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

Answer:

Explanation:

The worst case should be O(n) as we need to traverse the whole array.

Code:


public class Main
{
   public static void main(String[] args) {
      
       int a[] = {2, 3, 4, 7, 9, 10, 17};
       int x = 4, y=10;
      
       for(int i=0; i<a.length; i++)
       {
       if(a[i]>=x && a[i]<=y)
       System.out.print(a[i]+" ");
       }
      
      
   }
}

Output:

PLEASE UPVOTE IF YOU FOUND THIS HELPFUL!

Add a comment
Know the answer?
Add Answer to:
In Java: In a sorted (ascending) integer array of length n with no duplicates, print all...
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
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