Array Index & Element Equality #144
Unanswered
esthicodes
asked this question in
Q&A
Replies: 1 comment
-
Given a sorted array arr of distinct integers, write a function indexEqualsValueSearch that returns the lowest index i for which arr[i] == i. Return -1 if there is no such index. Analyze the time and space complexities of your solution and explain its correctness. Examples: input: arr = [-8,0,2,5] input: arr = [-1,0,3,6] [time limit] 5000ms [input] array.integer arr 1 ≤ arr.length ≤ 100 |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
import java.io.;
import java.util.;
class Solution {
static int indexEqualsValueSearch(int[] arr) {
// your code goes here
int l = 0;
int r = arr.length - 1;
}
public static void main(String[] args) {
int[] arr = {-8,0,2,5};
System.out.println(indexEqualsValueSearch(arr));
int[] arr2 = {-1,0,3,6};
System.out.println(indexEqualsValueSearch(arr2));
}
}
/**
BS
-8,0,2,5
l
r
m-1
arr[m] == m
return m;
m =0 || arr[m - 1] < m -1
*/
Beta Was this translation helpful? Give feedback.
All reactions