diff --git a/Binary Search/Binary Search.cpp b/Binary Search/Binary Search.cpp new file mode 100644 index 0000000..44907e3 --- /dev/null +++ b/Binary Search/Binary Search.cpp @@ -0,0 +1,48 @@ +#include +using namespace std; + +void solve() +{ + int n; + cin >> n; + + int k; + cin >> k; + + vector arr(n); + + for(int i = 0; i < n; i++) + { + cin >> arr[i]; + } + int low = 0, high = n-1; + + while(low<=high) + { + int mid = high + (low-high)/2; + + if(arr[mid] == k){ + cout< k){ + high = mid - 1; + } + else{ + low = mid + 1; + } + + } +} + +int main() +{ + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + cout.tie(NULL); + + solve(); + +} diff --git a/Binary Search/Find Minimum in Rotated Sorted Array.cpp b/Binary Search/Find Minimum in Rotated Sorted Array.cpp new file mode 100644 index 0000000..8713475 --- /dev/null +++ b/Binary Search/Find Minimum in Rotated Sorted Array.cpp @@ -0,0 +1,47 @@ +#include +using namespace std; + +int main() +{ + ios_base::sync_with_stdio(false); + + cin.tie(NULL); + cout.tie(NULL); + + int n = 6; + vector arr {4, 5, 6 ,7, 2, 3}; + + int low = 0; + int high = n-1; + + if(n == 1) + cout << arr[0]; +// return arr[0]; + if(arr[high] > arr[0]) + cout << arr[0]; + + + while(low < high){ + int mid = low + (high-low)/2; + + if(arr[mid] > arr[mid + 1]) + { + cout << arr[mid]; + break; + } + + if(arr[mid-1] > arr[mid]) + { + cout << arr[mid]; + break; + } + + if(arr[mid] > arr[low]){ + low = mid+1; + } + else{ + high = mid - 1; + } + } + +} diff --git a/Binary Search/First and Last occurance.cpp b/Binary Search/First and Last occurance.cpp new file mode 100644 index 0000000..8fb105a --- /dev/null +++ b/Binary Search/First and Last occurance.cpp @@ -0,0 +1,71 @@ +#include +using namespace std; + +void solve() +{ + int n; + cin >> n; + + int k; + cin >> k; + + vector arr(n); + + int first = 0, last = 0; + + for(int i = 0; i < n; i++) + { + cin >> arr[i]; + } + + int low = 0, high = n-1; + + while(low<=high) + { + int mid = high + (low-high)/2; + + if(arr[mid] == k){ + first = mid; + high = mid-1; + } + + if(arr[mid] > k){ + high = mid - 1; + } + else if(arr[mid] < k){ + low = mid + 1; + } + } + + low = 0; + high = n-1; + + while(low<=high) + { + int mid = high + (low-high)/2; + + if(arr[mid] == k){ + last = mid; + low = mid+1; + } + + if(arr[mid] > k){ + high = mid - 1; + } + else{ + low = mid + 1; + } + } + cout< +using namespace std; + +void solve() +{ + int n; + cin >> n; + vector arr(n); + + for(int i = 0; i < n; i++) + { + cin >> arr[i]; + } + + int low = 0; + int high = n-1; + + while(low <= high){ + + int mid = (low + high)/2; + + if(arr[mid] <= arr[mid-1] && arr[mid] <= arr[mid+1]) + { + cout<> t; + + while(t--) + { + solve(); + } +} diff --git a/longest_common_subsequence _dynamic_programming.cpp.cpp b/Dynamic Programming/longest_common_subsequence _dynamic_programming.cpp.cpp similarity index 100% rename from longest_common_subsequence _dynamic_programming.cpp.cpp rename to Dynamic Programming/longest_common_subsequence _dynamic_programming.cpp.cpp diff --git a/README.md b/README.md index 737a51b..5e5f2f6 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,34 @@ -# Basic_cpp -Basic c++ codes + +

+ +

+ +A community repository to allow enthusiasts and beginners add their projects for [@DigitalOcean's HacktoberFest](https://hacktoberfest.digitalocean.com) +## About this Repository: + +This repository allows any type of CPP Code to be added! When adding your own Code, place it in a folder within most suitable category. If there are no folder of that category yet, make a new folder and add it there! Making your PR will get your 1 step closer to your free T-Shirt! + +## What is Hacktoberfest? + + Hacktoberfest® is open to everyone in our global community. Whether you’re a developer, student learning to code, event host, or company of any size, you can help drive growth of open source and make positive contributions to an ever-growing community. All backgrounds and skill levels are encouraged to complete the challenge. + +- Hacktoberfest is a celebration open to everyone in our global community. +- Pull requests can be made in any GitHub-hosted repositories/projects. +- You can sign up anytime between October 1 and October 31. + +Sign up [Here](https://hacktoberfest.digitalocean.com) + +## Making A Thoughtful Pull Request + +Pull requests should include the following: + +- A title about the project/program you are including +- Detailed information about the commits +- No low effort pull requests or duplicate programs +- Place your Code in the appropriate folder with a README and information on the code + + +### Languages Used Already: + +- C +- CPP diff --git a/Quick sort b/Sorting/Quick sort similarity index 100% rename from Quick sort rename to Sorting/Quick sort diff --git a/Sorting - Merge Sort.cpp b/Sorting/Sorting - Merge Sort.cpp similarity index 94% rename from Sorting - Merge Sort.cpp rename to Sorting/Sorting - Merge Sort.cpp index da8518c..2bdf517 100644 --- a/Sorting - Merge Sort.cpp +++ b/Sorting/Sorting - Merge Sort.cpp @@ -1,67 +1,67 @@ - - // Sorting - Merge Sort - -#include -using namespace std; -void Merge(int A[],int l,int m,int r) -{ - int i,j,k; - int n1=m-l+1; - int n2=r-m; - - int L[n1],R[n2]; - for(i=0;i +using namespace std; +void Merge(int A[],int l,int m,int r) +{ + int i,j,k; + int n1=m-l+1; + int n2=r-m; + + int L[n1],R[n2]; + for(i=0;i