Skip to content

First and Last element in a rotated array code. #318

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions BabesGotByte_First_And_Last_Element_In_A_Roated_Array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include<iostream>
using namespace std;
#include<bits/stdc++.h>

int find_start_index(vector<int> vec, int target){
int index=-1;
int start=0;
int end=vec.size()-1;
while(start<=end){
int mid=start+(end-start)/2;
if(vec[mid]>=target){
end=mid-1;
}
else{
start=mid+1;
}
if(vec[mid]==target){
index=mid;
}
}
return index;
}

int find_end_index(vector<int> vec, int target){
int index=-1;
int start=0;
int end=vec.size()-1;
while(start<=end){
int mid=start+(end-start)/2;
if(vec[mid]<=target){
start=mid+1;
}
else{
end=mid-1;
}
if(vec[mid]==target){
index=mid;
}
}
return index;
}

int main(){

int n;
cin>>n;
vector<int> v;
for(int i=0;i<n;i++){
int temp;
cin>>temp;
v.push_back(temp);
}
int target;
cin>>target;
int start=find_start_index(v,target);
int end=find_end_index(v,target);
cout<<start<<" "<<end<<endl;

return 0;
}
55 changes: 55 additions & 0 deletions BabesGotByte_QuickSort.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int partition (int arr[], int low, int high)
{
if(low>=high){
return 0;
}
int pivot=arr[high];
int index=low;
while(low<high){
if(arr[low]<pivot){
swap(arr[low],arr[index]);
index++;
}
low++;
}
swap(arr[index],arr[high]);
return index;
}

void quickSort(int arr[], int low, int high)
{
if (low < high)
{
int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1);
quickSort(arr, pi + 1, high);
}
}

void printArray(int arr[], int size)
{
int i;
for (i=0; i < size; i++)
cout<<arr[i]<<" ";
cout<<endl;
}
int main()
{
int arr[1000],n,T,i;
cin>>T;
while(T--){
cin>>n;
for(i=0;i<n;i++)
cin>>arr[i];
quickSort(arr, 0, n-1);
printArray(arr, n);
}
return 0;
}