-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkrange.cpp
53 lines (48 loc) · 899 Bytes
/
krange.cpp
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
43
44
45
46
47
48
49
50
51
52
53
#include<iostream>
#include<algorithm>
#include<climits>
using namespace std;
float max(float a, float b){
if(a>=b) return a;
else return b;
}
float min(float a, float b){
if(a<b) return a;
else return b;
}
int main(){
int arr[] = {5,3,10,8};
int n = 4;
for(int i =0; i<n; i++){
cout<<arr[i]<<" ";
}
cout<<endl;
float kmin = (float)(INT_MIN);
float kmax = (float)(INT_MAX);
bool flag = true;
for(int i =0; i<n-1; i++){
if(arr[i] >= arr[i+1]){
kmin = max(kmin, (arr[i] + arr[i+1])/2.0);
}
else{
kmax = min(kmax, (arr[i] + arr[i+1])/2.0);
}
if(kmin> kmax){
flag = false;
break;
}
}
if(flag == false) cout<< -1;
else if(kmin == kmax){
if(kmin - (int)kmin ==0){
cout<<"There is only one value pof k : "<<kmin;
}
else cout<< -1;
}
else{
if(kmin-(int)kmin>0){
kmin = (int)kmin + 1;
}
}
cout<<"Range of k is :["<<kmin<<","<<(int)kmax<<"]";
}