Skip to content

Commit 3e281c3

Browse files
author
rpanjrath
committed
Arrays: Given a sorted array (ascending order and distinct elements),
find i such that inputArr[i] = i.
1 parent b7dbd0c commit 3e281c3

File tree

2 files changed

+56
-6
lines changed

2 files changed

+56
-6
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,26 @@ Arrays
3030
6. Write a program takes array input{1,0,2,3} and num = 3 and provides output {1,2}{0,3}{2,1}{3,0} sum equals the num.
3131
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/findpairsequaltosum/FindPairsEqualToSum.java
3232

33-
7. Find the contiguous subarray which has the largest sum. Kadane's algorithm.
33+
7. Given a sorted array (ascending order and distinct elements), find i such that inputArr[i] = i. Return -1 if nothing found.
34+
Link:
35+
36+
8. Find the contiguous subarray which has the largest sum. Kadane's algorithm.
3437
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/maxsubarray/FindMaxSubArray.java
3538

36-
8. Given two sorted (ascending) arrays of integers, find the minimum difference between two integers in that array.
39+
9. Given two sorted (ascending) arrays of integers, find the minimum difference between two integers in that array.
3740
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/mindiffsortedarrays/MinimumDifferenceSortedArrays.java
3841

39-
9. Print numbers with frequency.
42+
10. Print numbers with frequency.
4043
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/numberfrequency/PrintNumbersWithFrequency.java
4144

42-
10. Find Number with Highest frequency in sorted array.
45+
11. Find Number with Highest frequency in sorted array.
4346
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/numberwithhighestfreq/FindNumberWithHighestFreq.java
4447

45-
11. Given an array, write a program to generate a random permutation of array elements.
48+
12. Given an array, write a program to generate a random permutation of array elements.
4649
Also asked as “shuffle a deck of cards” or “randomize a given array”.
4750
Link:
4851

49-
12. Search for a number in rotated sorted array.
52+
13. Search for a number in rotated sorted array.
5053
Link: https://github.com/techpanja/interviewproblems/blob/master/src/arrays/searchrotatedsortedarray/SearchInRotatedSortedArray.java
5154

5255

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package arrays.indexequaltonumbersortedarray;
2+
3+
/**
4+
* Given a sorted array (ascending order and distinct elements), find i such that inputArr[i] = i. Return -1 if
5+
* nothing found.
6+
* <p/>
7+
* 0 3 5 6 7
8+
* 0 1 2 3 4
9+
* Created by techpanja
10+
* Created on 2/3/14 3:34 PM.
11+
*/
12+
public class IndexEqualToNumberSortedArray {
13+
14+
private IndexEqualToNumberSortedArray() {
15+
16+
}
17+
18+
/*
19+
* Binary search. O(log N) solution.
20+
* */
21+
public static int indexEqualToNumberSortedArray(int[] inputArray) {
22+
if (inputArray.length == 0) {
23+
return -1;
24+
}
25+
int low = 0;
26+
int high = inputArray.length - 1;
27+
while (low <= high) {
28+
int mid = (low + high) / 2;
29+
if (inputArray[mid] == mid) {
30+
return mid;
31+
} else if (inputArray[mid] > mid) {
32+
high = mid - 1;
33+
} else {
34+
low = mid + 1;
35+
}
36+
}
37+
return -1;
38+
}
39+
40+
public static void main(String[] args) {
41+
System.out.println(indexEqualToNumberSortedArray(new int[] {1, 2, 4, 5, 6, 7}));
42+
System.out.println(indexEqualToNumberSortedArray(new int[] {0, 3, 4, 5, 6, 7}));
43+
System.out.println(indexEqualToNumberSortedArray(new int[] {-1, 0, 1, 2, 4, 7}));
44+
System.out.println(indexEqualToNumberSortedArray(new int[] {-1, 0, 1, 2, 3, 5}));
45+
System.out.println(indexEqualToNumberSortedArray(new int[] {-1, 0, 1, 3, 5, 6}));
46+
}
47+
}

0 commit comments

Comments
 (0)