Arrays/Matrices Practice Questions - Java

Ace your homework & exams now with Quizwiz!

Easy: Search for a range in a sorted array. Given a sorted array arr[] with possibly duplicate elements, write a program to find indexes of first and last occurrences of a target element in the given array. The algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. Example: Input: arr[] = [1, 3, 5, 5, 5, 5 ,28, 37, 42], target = 5 Output: [2, 5] Explanation: First Occurrence = 2 and Last Occurrence = 5

Approach 2: Using Binary Search Time Complexity: O(log(n)) Space Complexity:O(1) prob won't be able to code this but note that it is more efficient

Medium: Rotate Matrix 90 degrees.

https://leetcode.com/problems/rotate-image/solution/

Easy: Search for a range in a sorted array. Given a sorted array arr[] with possibly duplicate elements, write a program to find indexes of first and last occurrences of a target element in the given array. The algorithm's runtime complexity must be in the order of O(log n). If the target is not found in the array, return [-1, -1]. Example: Input: arr[] = [1, 3, 5, 5, 5, 5 ,28, 37, 42], target = 5 Output: [2, 5] Explanation: First Occurrence = 2 and Last Occurrence = 5

Approach 1: Brute Force Time Complexity: O(n) Space Complexity:O(1) https://medium.com/@saurav.agg19/find-first-and-last-position-of-element-in-sorted-array-82116ab6c187


Related study sets

Chapter 11 Quiz - International Business (Exam 2)

View Set

Java Quiz 7 - Classes & Functions

View Set