-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlearn.cpp
More file actions
72 lines (60 loc) · 1.3 KB
/
learn.cpp
File metadata and controls
72 lines (60 loc) · 1.3 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# include <iostream>
#include <math.h>
using namespace std ;
// find the no of occurance in array
// find the peak in mountain
// PIVOT IN AN ARRAY ;(PRACTICE)
int firstocc(int a[],int n,int key){
int s=0,e=n-1 ;
int mid = s + (e-s)/2 ;
int ans ;
while(s<=e){
if(a[mid]==key){
ans = mid ;
e= mid -1 ;
}
else if(a[mid]>key){
e = mid -1 ;
}
else if(a[mid]<key){
s= mid+ 1;
}
mid = s + (e-s)/2 ;
}
return ans ;
}
int lastocc(int a[],int n,int key){
int s=0,e=n-1 ;
int mid = s + (e-s)/2 ;
int ans ;
while(s<=e){
if(a[mid]==key){
ans = mid ;
s= mid + 1;
}
else if(a[mid]>key){
e = mid -1 ;
}
else if(a[mid]<key){
s= mid+ 1;
}
mid = s + (e-s)/2 ;
}
return ans ;
}
int main(){
// int array[7]= {2,5,5,5,5,7,7};
// int key = 5 ;
// cout<<"the first occurance of key is "<<firstocc(array,7,7)<<endl;
// cout<<"the last occurance of key is "<<lastocc(array,7,7)<<endl ;
int n =4 ;
int ans=0 ;
int i =0 ;
while(n!=0){
int bit = n&1 ;
ans =(bit * pow(10,i)) + ans;
n = n >> 1 ;
i++ ;
}
cout<<ans ;
}