Last active
October 21, 2021 10:36
-
-
Save rozer007/0b9b395de3b4189e3adadc90532ba2fb to your computer and use it in GitHub Desktop.
pepcoding.com 19/10/2021 pivotInSortedAndRotatedArray
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
void input(vector<int> &arr) | |
{ | |
for (int i = 0; i < arr.size(); i++) | |
{ | |
cin >> arr[i]; | |
} | |
} | |
int findpivot(vector<int>&arr) | |
{ | |
int lo=0; | |
int hi=arr.size()-1; | |
while(lo<hi) | |
{ | |
int mid=(lo+hi)/2; | |
if(arr[mid]<arr[hi]) | |
{ | |
hi=mid; | |
} | |
else if(arr[mid]>arr[hi]) | |
{ | |
lo=mid+1; | |
} | |
} | |
return arr[hi]; | |
} | |
int main() | |
{ | |
int n; | |
cin>>n; | |
vector<int> vec(n,0); | |
input(vec); | |
int pivot=findpivot(vec); | |
cout<<pivot; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findpivot(arr): | |
lo=0;hi=len(arr)-1; | |
while lo<hi: | |
mid=(lo+hi)//2; | |
if(arr[mid]<arr[hi]): | |
hi=mid; | |
elif(arr[mid]>arr[hi]): | |
lo=mid+1; | |
return arr[hi]; | |
n=int(input()); | |
arr=[]; | |
for i in range(0,n): | |
val=int(input()); | |
arr.append(val); | |
pivot=findpivot(arr); | |
print(pivot); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
using namespace std; | |
void input(vector<int> &arr) | |
{ | |
for (int i = 0; i < arr.size(); i++) | |
{ | |
cin >> arr[i]; | |
} | |
} | |
int findpivot(vector<int> &arr) | |
{ | |
//write your code here | |
} | |
int main() | |
{ | |
int n; | |
cin >> n; | |
vector<int> vec(n, 0); | |
input(vec); | |
int pivot = findpivot(vec); | |
cout << pivot; | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def findpivot(arr): | |
#write your code here | |
n=int(input()); | |
arr=[]; | |
for i in range(0,n): | |
val=int(input()); | |
arr.append(val); | |
pivot=findpivot(arr); | |
print(pivot); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment