The smallest element’s index in the rotated array gives the rotation count. The Nth largest element can be found by adjusting the index using this rotation count.
classSolution {
public:int findNthLargest(vector<int>& arr, int n) {
int k = min_element(arr.begin(), arr.end()) - arr.begin();
int sz = arr.size();
return arr[(k + n -1) % sz];
}
};
1
2
3
4
5
6
7
8
9
10
funcfindNthLargest(arr []int, nint) int {
k:=0fori, v:=rangearr {
ifv < arr[k] {
k = i }
}
sz:= len(arr)
returnarr[(k+n-1)%sz]
}
1
2
3
4
5
6
7
8
9
10
classSolution {
publicintfindNthLargest(int[] arr, int n) {
int k = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i]< arr[k]) k = i;
}
int sz = arr.length;
return arr[(k + n - 1) % sz];
}
}
1
2
3
4
5
6
7
classSolution {
funfindNthLargest(arr: IntArray, n: Int): Int {
val k = arr.indices.minByOrNull { arr[it] } ?:0val sz = arr.size
return arr[(k + n - 1) % sz]
}
}
1
2
3
4
deffind_nth_largest(arr: list[int], n: int) -> int:
k = arr.index(min(arr))
sz = len(arr)
return arr[(k + n -1) % sz]
1
2
3
4
5
6
7
impl Solution {
pubfnfind_nth_largest(arr: Vec<i32>, n: i32) -> i32 {
let k = arr.iter().enumerate().min_by_key(|&(_, &v)| v).unwrap().0;
let sz = arr.len();
arr[(k + n asusize-1) % sz]
}
}
Since the array is a rotated version of a sorted array, we can use binary search to efficiently locate the Nth largest element by first finding the rotation index and then mapping the Nth largest to its correct position. Here is how we can find the rotation count using binary search - Find the rotation count in rotated sorted array.Find the Kth Largest Integer in the Array
classSolution {
public:int findNthLargest(vector<int>& arr, int n) {
int l =0, r = arr.size() -1;
while (l < r) {
int m = l + (r - l) /2;
if (arr[m] > arr[r]) l = m +1;
else r = m;
}
int k = l;
int sz = arr.size();
return arr[(k + sz - n) % sz];
}
};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
funcfindNthLargest(arr []int, nint) int {
l, r:=0, len(arr)-1forl < r {
m:=l+ (r-l)/2ifarr[m] > arr[r] {
l = m+1 } else {
r = m }
}
k:=lsz:= len(arr)
returnarr[(k+sz-n)%sz]
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution {
publicintfindNthLargest(int[] arr, int n) {
int l = 0, r = arr.length- 1;
while (l < r) {
int m = l + (r - l) / 2;
if (arr[m]> arr[r]) l = m + 1;
else r = m;
}
int k = l;
int sz = arr.length;
return arr[(k + sz - n) % sz];
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
classSolution {
funfindNthLargest(arr: IntArray, n: Int): Int {
var l = 0var r = arr.size - 1while (l < r) {
val m = l + (r - l) / 2if (arr[m] > arr[r]) l = m + 1else r = m
}
val k = l
val sz = arr.size
return arr[(k + sz - n) % sz]
}
}
1
2
3
4
5
6
7
8
9
10
11
deffind_nth_largest(arr: list[int], n: int) -> int:
l, r =0, len(arr) -1while l < r:
m = l + (r - l) //2if arr[m] > arr[r]:
l = m +1else:
r = m
k = l
sz = len(arr)
return arr[(k + sz - n) % sz]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
impl Solution {
pubfnfind_nth_largest(arr: Vec<i32>, n: i32) -> i32 {
letmut l =0;
letmut r = arr.len() -1;
while l < r {
let m = l + (r - l) /2;
if arr[m] > arr[r] {
l = m +1;
} else {
r = m;
}
}
let k = l;
let sz = arr.len();
arr[(k + sz - n asusize) % sz]
}
}