forked from Viv786ek/Problem-Of-The-Day
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk sorted array.cpp
More file actions
42 lines (39 loc) · 871 Bytes
/
k sorted array.cpp
File metadata and controls
42 lines (39 loc) · 871 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution {
public:
int find(int arr[], int n, int x)
{
int low = 0, high = n-1;
while(low <= high)
{
int mid = low + (high - low)/2;
if(arr[mid] == x)
{
return mid;
}
if(arr[mid] < x)
{
low = mid + 1;
}
else
{
high = mid - 1;
}
}
}
string isKSortedArray(int arr[], int n, int k)
{
//code here.
int arr2[n];
for(int i=0;i<n;++i){
arr2[i]=arr[i];
}
sort(arr2,arr2+n);
for(int i=0;i<n;i++){
int index = find(arr2,n,arr[i]);
if(k<abs(i-index)){
return "No";
}
}
return "Yes";
}
};