diff --git a/Coding/C++/Best_time_to_sell_and_buy+stock.cpp b/Coding/C++/Best_time_to_sell_and_buy+stock.cpp new file mode 100644 index 00000000..db205854 --- /dev/null +++ b/Coding/C++/Best_time_to_sell_and_buy+stock.cpp @@ -0,0 +1,16 @@ +#include +using namespace std; + +int bestTimeToBuyAndSellStock(vector&price) { + int n = price.size(); + int minPrice = INT_MAX; + int mini = price[0]; + int maxPro = 0; + + for(int i=0; i +using namespace std; + +//let we have a sorted array +int binarysearch(int arr[], int n, int key){ + int start=0; + int end=n; + + while (start<=end){ //when end point is greater or equal thn start point + // int mid = (start+end)/2; //to find mid point and update it with new value of start and end + int mid = start+ (end-start)/2; //above mid method is not good for long integer value so we take other method + + if(arr[mid]==key){ + return mid; + } + else if(arr[mid]> n; //taking size of array + + int arr[n]; //declear array of size n + +// taking sorted array elements from user + for(int i=0; i>arr[i]; + } + + int key ; + cin>>key; //taking key or number which we have to search in our array + + cout< +using namespace std; + +int FirstOcc(vector &arr, int n, int key) +{ + int start = 0, end = n - 1; + int ans = -1; + while (start <= end) + { + int mid = start + (end - start) / 2; + + if (arr[mid] == key) + { + ans = mid; + end = mid - 1; + } + + else if (arr[mid] < key) + { // right part + start = mid + 1; + } + else + { + end = mid - 1; // left part + } + } + return ans; +} + +int LastOcc(vector &arr, int n, int key) +{ + int start = 0, end = n - 1; + int ans = -1; + while (start <= end) + { + int mid = start + (end - start) / 2; + + if (arr[mid] == key) + { + ans = mid; + start = mid + 1; + } + + else if (arr[mid] < key) + { // right part + start = mid + 1; + } + else + { + end = mid - 1; // left part + } + } + return ans; +} + +int count(vector &arr, int n, int x) +{ + int a = FirstOcc(arr, n, x); + int b = LastOcc(arr, n, x); + + if (a == -1) + return 0; + + return b - a + 1; +} + +int main() +{ + + return 0; +} diff --git a/Coding/C++/BinarySearch/Find_Pivot_in_an_Sorted_and_Rotated_Array.cpp b/Coding/C++/BinarySearch/Find_Pivot_in_an_Sorted_and_Rotated_Array.cpp new file mode 100644 index 00000000..9d082596 --- /dev/null +++ b/Coding/C++/BinarySearch/Find_Pivot_in_an_Sorted_and_Rotated_Array.cpp @@ -0,0 +1,35 @@ +// pivot : break point of sorted array or rotated array {simply means to find the minimum of the array} +// array : { 1,3,8,10,17} +// rotated array arr = {8,10,17,1,3} +// pivot is 3 at index 4 + +#include +using namespace std; + +int Pivot_ele(int arr[], int a){ + int s=0; + int e = a-1; + + while(sarr[0]){ + return arr[0]; + } + if(arr[mid]>=arr[0]){ + s= mid+1; + } + else{ + e=mid; + } + } + return arr[s]; +} + + +int main(){ + int arr[4] ={11,13,15,17}; + + cout< +using namespace std; + +int FirstOcc(int arr[], int n, int key){ + int start=0 , end = n-1; + int ans=-1; + while(start<= end){ + int mid = start + (end-start)/2; + + if(arr[mid]==key){ + ans = mid; //mid index ko store krva liya taki agr koi uske age vhi element ho toh update ho jye + end= mid-1; + } + + else if(arr[mid] +using namespace std; + + int findKthPositive(vector& arr, int k) { + //Brute Forrce solution + //TC- O[n] + //SC- O[1] + + // for(int i=0; i +using namespace std; + +//TC- O[N * log(sum-max)] +// SC - O[1] + + +int daysReq(vector &weights , int cap){ + int day =1; + int load= 0; + int n = weights.size(); + + for(int i=0; i cap){ + day++; + load = weights[i]; + } + else{ + load += weights[i]; + } + } + + return day; +} + +int leastWeightCapacity(vector &weights, int d) +{ + int maxi = INT_MIN; + int sum =0; + int n = weights.size(); + + // for(int i=0; i +using namespace std; + +int findMin(vector& arr) +{ + int n = arr.size(); + + int low =0; + int high = n-1; + int ans = INT_MAX; + + while(low<=high){ + + int mid = low + (high - low) / 2; + + if(arr[low] <= arr[mid]){ + ans = min(ans,arr[low]); + low = mid+1; + } + else{ + + ans = min(ans,arr[mid]); + high = mid-1; + } + } + + return ans; +} \ No newline at end of file diff --git a/Coding/C++/BinarySearch/Min_max_distance_to_gas_station.cpp b/Coding/C++/BinarySearch/Min_max_distance_to_gas_station.cpp new file mode 100644 index 00000000..d728267e --- /dev/null +++ b/Coding/C++/BinarySearch/Min_max_distance_to_gas_station.cpp @@ -0,0 +1,80 @@ + +#include + +using namespace std; + + + +int numberOfGasStationsRequired(long double dist, vector &arr) { + + int n = arr.size(); // size of the array + + int cnt = 0; + + for (int i = 1; i < n; i++) { + + int numberInBetween = ((arr[i] - arr[i - 1]) / dist); + + if ((arr[i] - arr[i - 1]) == (dist * numberInBetween)) { + + numberInBetween--; + + } + + cnt += numberInBetween; + + } + + return cnt; + +} + + + +long double minimiseMaxDistance(vector &arr, int k) { + + int n = arr.size(); // size of the array + + long double low = 0; + + long double high = 0; + + + + //Find the maximum distance: + + for (int i = 0; i < n - 1; i++) { + + high = max(high, (long double)(arr[i + 1] - arr[i])); + + } + + + + //Apply Binary search: + + long double diff = 1e-6 ; + + while (high - low > diff) { + + long double mid = (low + high) / (2.0); + + int cnt = numberOfGasStationsRequired(mid, arr); + + if (cnt > k) { + + low = mid; + + } + + else { + + high = mid; + + } + + } + + return high; + +} diff --git a/Coding/C++/BinarySearch/Min_no_of_day_to_make_M_bouquets.cpp b/Coding/C++/BinarySearch/Min_no_of_day_to_make_M_bouquets.cpp new file mode 100644 index 00000000..1adb86c9 --- /dev/null +++ b/Coding/C++/BinarySearch/Min_no_of_day_to_make_M_bouquets.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + + + +//Total TC - O[N * log (max-min)] +// SC - O[1] +bool posible(vector &arr, int day, int m, int k){ + int n = arr.size(); + int cnt =0; + int noOFb = 0; + + //count no of bouquets + for(int i=0; i= m; +} + +int minDays(vector& arr, int m, int k) { + + //to avoid conflict we connvertes int to long long + long long val = m * 1ll * k * 1ll; + int n =arr.size(); + + // not possible case + if(val > n) return -1; + + int mini = INT_MAX, maxi = INT_MIN; + + //finding the range - min to max of arr + for(int i=0; i +using namespace std; + +int findKRotation(vector &arr){ + + int n = arr.size(); + + int low =0; + int high = n-1; + int index = -1; + int ans = INT_MAX; + + + while(low<=high){ + + int mid = low + (high - low) / 2; + + if(arr[low] <= arr[high]){ + if(arr[low] < ans){ + index = low; + ans = arr[low]; + } + break; + } + + if(arr[low] <= arr[mid]){ + if(arr[low] < ans){ + + index = low; + ans = arr[low]; + } + low = mid+1; + } + else{ + high = mid-1; + if(arr[mid] < ans){ + + index = mid; + ans = arr[mid]; + } + + } + } + + return index; +} \ No newline at end of file diff --git a/Coding/C++/BinarySearch/Peak_ele_in_Array.cpp b/Coding/C++/BinarySearch/Peak_ele_in_Array.cpp new file mode 100644 index 00000000..8a86d7d7 --- /dev/null +++ b/Coding/C++/BinarySearch/Peak_ele_in_Array.cpp @@ -0,0 +1,38 @@ +#include +using namespace std; +//{simply means to find the maximum of the array} +int Peak_ele(int arr[], int n){ + int start=0; + int end = n-1; + + while(start> n; //taking size of array + + int arr[n]; //declear array of size n + +// taking sorted array elements from user + for(int i=0; i>arr[i]; + } + + + cout< + +using namespace std; + +int lowerBound(vector arr, int n, int x) +{ + + int low = 0; + int high = n - 1; + int ans = n; + + while (low <= high) + { + + int mid = low + (high - low) / 2; + + if (arr[mid] >= x) + { + high = mid - 1; // left + ans = mid; + } + else + { + low = mid + 1; // right + } + } + + return ans; +} + +int rowWithMax1s(vector> &matrix, int n, int m) +{ + int max_cnt = 0; + int index = -1; + + for (int i = 0; i < n; i++) + { + int cnt_ones = m - lowerBound(matrix[i], m, 1); + + if (cnt_ones > max_cnt) + { + max_cnt = cnt_ones; + index = i; + } + } + + return index; +} \ No newline at end of file diff --git a/Coding/C++/BinarySearch/SErach_single_element_in_rotated_sorted_array.cpp b/Coding/C++/BinarySearch/SErach_single_element_in_rotated_sorted_array.cpp new file mode 100644 index 00000000..cbb70e44 --- /dev/null +++ b/Coding/C++/BinarySearch/SErach_single_element_in_rotated_sorted_array.cpp @@ -0,0 +1,77 @@ + +#include +using namespace std; + +int singleNonDuplicate(vector& nums) { + int n = nums.size(); + // int ans; +// brute force code +// TC -O[N] +// SC- O[1] + /* if(n==1) return nums[0]; + + for(int i=0; i +using namespace std; + +// One solution + +int Pivot_ele(vector &arr, int n) +{ + + int s = 0; + int e = n - 1; + + while (s < e) + { + int mid = s + (e - s) / 2; + + if (arr[e] > arr[0]) + { + return 0; + } + + if (arr[mid] >= arr[0]) + { + s = mid + 1; + } + else + { + e = mid; + } + } + return s; +} + +int binarySearch(vector &arr, int s, int e, int k) +{ + + while (s <= e) + { + int mid = s + (e - s) / 2; + + if (arr[mid] == k) + { + return mid; + } + if (arr[mid] < k) + { + s = mid + 1; + } + else + { + e = mid - 1; + } + } + return -1; +} + +int findPosition(vector &arr, int n, int k) +{ + int pivot = Pivot_ele(arr, n); + + if (k >= arr[pivot] && k <= arr[n - 1]) + { + return binarySearch(arr, pivot, n - 1, k); + } + else + { + return binarySearch(arr, 0, pivot - 1, k); + } +} + +// another soution +int search(vector &arr, int n, int k) +{ + int low = 0; + int high = n - 1; + + while (low <= high) + { + + int mid = low + (high - low) / 2; + + if (arr[mid] == k) + { + return mid; + } + + // else identify the sorted part + // left part checking + if (arr[low] <= arr[mid]) + { + // check element lie int this range + if (arr[low] <= k && k <= arr[mid]) + { + high = mid - 1; + } + else + { + low = mid + 1; + } + } + + // check for right part + else + { + + if (arr[mid] <= k && k <= arr[high]) + { + low = mid + 1; + } + else + { + high = mid - 1; + } + } + } + + return -1; +} + +int main() +{ + int arr[7] = {2, 4, 5, 6, 8, 9, 1}; + int k = 20; + + return 0; +} \ No newline at end of file diff --git a/Coding/C++/BinarySearch/Search_in_2D_array_I.cpp b/Coding/C++/BinarySearch/Search_in_2D_array_I.cpp new file mode 100644 index 00000000..9bdfd410 --- /dev/null +++ b/Coding/C++/BinarySearch/Search_in_2D_array_I.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +bool searchMatrix(vector> &mat, int target) +{ + int n = mat.size(); + int m = mat[0].size(); + + int low = 0; + int high = (n * m) - 1; + + while (low <= high) + { + int mid = low + (high - low) / 2; + int ele = mat[mid / m][mid % m]; + + if (ele == target) + return true; + + if (ele < target) + low = mid + 1; + + else + high = mid - 1; + } + + return false; +} \ No newline at end of file diff --git a/Coding/C++/BinarySearch/Search_in_2D_array_II.cpp b/Coding/C++/BinarySearch/Search_in_2D_array_II.cpp new file mode 100644 index 00000000..d9dc535a --- /dev/null +++ b/Coding/C++/BinarySearch/Search_in_2D_array_II.cpp @@ -0,0 +1,29 @@ +#include + +using namespace std; + +bool searchElement(vector> &mat, int target) +{ + int row = mat.size(); + int col = mat[0].size(); + + int row_index = 0; + int col_index = col - 1; + + while (row_index < row && col_index >= 0) + { + + int ele = mat[row_index][col_index]; + + if (ele == target) + return true; + + if (ele < target) + row_index++; + + else + col_index--; + } + + return false; +} \ No newline at end of file diff --git a/Coding/C++/BinarySearch/Search_in_rotated_aorted_array_II.cpp b/Coding/C++/BinarySearch/Search_in_rotated_aorted_array_II.cpp new file mode 100644 index 00000000..b80d66e3 --- /dev/null +++ b/Coding/C++/BinarySearch/Search_in_rotated_aorted_array_II.cpp @@ -0,0 +1,43 @@ +#include +using namespace std; + +bool searchInARotatedSortedArrayII(vector&arr, int k) { + + int low=0; + int high = arr.size() - 1; + + while(low<=high){ + int mid = low+ (high - low)/2; + + if(arr[mid] == k){ + return true; + } + + if(arr[low] == arr[mid] && arr[mid] == arr[high]){ + low = low+1; + high = high-1; + continue; + } + + if(arr[low] <= arr[mid]){ + if(arr[low] <= k && k<= arr[mid]){ + high = mid -1; + } + else{ + low = mid+1; + } + } + else{ + + if(arr[mid] <= k && k<= arr[high]){ + low = mid+1; + } + else{ + high = mid -1; + } + + } + } + return false; + +} diff --git a/Coding/C++/BinarySearch/find_smallest_divisor_less_than_or_equal_to_threshold.cpp b/Coding/C++/BinarySearch/find_smallest_divisor_less_than_or_equal_to_threshold.cpp new file mode 100644 index 00000000..210cbe76 --- /dev/null +++ b/Coding/C++/BinarySearch/find_smallest_divisor_less_than_or_equal_to_threshold.cpp @@ -0,0 +1,46 @@ + +#include +using namespace std; + + +//TC- O[N * log(maxi)] +// SC - O[1] + +int sum(vector &arr, int k){ + int summ =0; + for(int i=0; i& arr, int limit) +{ + int mini = INT_MAX; + int maxi = INT_MIN; + + for(int i=0; i + +using namespace std; + +int lowerBound(vector arr, int n, int x) +{ + + int low = 0; + int high = n - 1; + int ans = n; + + while (low <= high) + { + + int mid = low + (high - low) / 2; + + if (arr[mid] >= x) + { + high = mid - 1; // left + ans = mid; + } + else + { + low = mid + 1; // right + } + } + + return ans; +} + +int main() +{ + + int n = 5; + int x = 8; + + vector arr = {3, 5, 8, 15, 19}; + + cout << lowerBound(arr, n, x) << " lower bound of x using custom function" << endl; + + // lower bound as inbuilt function + + cout << lower_bound(arr.begin(), arr.end(), x) - arr.begin() << " lower bound of x using in-built function" << endl; + + // in the case of array we use lower_bound(arr, arr+n); + // and if we want to find the lower bound in the pericular range than use the lower_bound(arr+2, arr+7); //range 2 to 6 + + return 0; +} diff --git a/Coding/C++/BinarySearch/squareRoot.cpp b/Coding/C++/BinarySearch/squareRoot.cpp new file mode 100644 index 00000000..46c5291a --- /dev/null +++ b/Coding/C++/BinarySearch/squareRoot.cpp @@ -0,0 +1,50 @@ +#include +using namespace std; + +long long int sqrt(int n ){ + + int s=0; + int e=n; + long long int ans=-1; + while(s<=e){ + long long int mid= s+(e-s)/2; + + long long int sqr = mid*mid; + + if(sqr==n){ + return mid; + } + + if(sqr>n){ + e=mid-1; + } + else{ + ans=mid; + s=mid+1; + } + } + return ans; +} + +double moreprecise(int n, int p, int intpart){ + double a = 1; + double ans = intpart; + + for(int i=0; i + +using namespace std; + +int upperBound(vector arr, int n, int x) +{ + + int low = 0; + int high = n - 1; + int ans = n; + + while (low <= high) + { + + int mid = low + (high - low) / 2; + + if (arr[mid] > x) + { + high = mid - 1; // left + ans = mid; + } + else + { + low = mid + 1; // right + } + } + + return ans; +} + +int main() +{ + + int n = 5; + int x = 8; + + vector arr = {3, 5, 8, 15, 19}; + + cout << upperBound(arr, n, x) << " upper bound of x using custom function" << endl; + + // upper bound as inbuilt function + + cout << upper_bound(arr.begin(), arr.end(), x) - arr.begin() << " upper bound of x using in-built function" << endl; + + // in the case of array we use upper_bound(arr, arr+n); + // and if we want to find the upper bound in the pericular range than use the upper_bound(arr+2, arr+7); //range 2 to 6 + + return 0; +} diff --git a/Coding/C++/Ceil_floor.cpp b/Coding/C++/Ceil_floor.cpp new file mode 100644 index 00000000..4f21128d --- /dev/null +++ b/Coding/C++/Ceil_floor.cpp @@ -0,0 +1,72 @@ +#include +using namespace std; + + +int ceil(vector &arr, int n, int x){ + + // int n = arr.size(); + int low = 0; + int high = n - 1; + int ans = -1; + + while (low <= high) + { + + int mid = low + (high - low) / 2; + + if (arr[mid] >= x) + { + high = mid - 1; // left + ans = arr[mid]; + } + else + { + low = mid + 1; // right + } + } + + return ans; + +} + +int floor(vector &arr, int n, int x){ + // int n = arr.size(); + int low = 0; + int high = n - 1; + int ans = -1; + + while (low <= high) + { + + int mid = low + (high - low) / 2; + + if (arr[mid] <= x) + { + ans = arr[mid]; + low = mid + 1; // right + } + else + { + high = mid - 1; // left + } + } + + return ans; + +} + +pair getFloorAndCeil(vector &arr, int n, int x) { + pair p; + + + p.first = floor(arr, n, x); + p.second = ceil(arr, n, x); + + return p; +} + + + +int main() { + return 0; +} \ No newline at end of file diff --git a/Coding/C++/Move_0's_to_end.cpp b/Coding/C++/Move_0's_to_end.cpp new file mode 100644 index 00000000..73997d50 --- /dev/null +++ b/Coding/C++/Move_0's_to_end.cpp @@ -0,0 +1,67 @@ +#include +using namespace std; + +vector moveZeros(int n, vector nums) { + +// brute force +// TC - O{2N} +// SC - O[N] + /*vectortemp; + int k=0; + for(int i=0; i + +using namespace std; + +int main(){ + vector arr; + + vector sum; + + arr = {1,2,3,4}; + int n = arr.size()-2; + cout< d; + + + while(j<=n){ + for(int i=j; i<2+j; i++){ + d.push_back(arr[i]); + } + for(auto& i : d){ + cout<0){ +// for(int i=arr.size()-1; i>=0; i--){ +// d.push_back(arr[i]); +// cout<:: iterator it; +// it = arr.begin()+a1; +// cout<<"it "<<*it<:: iterator ti; +// ti = arr.begin()+a2; +// cout<<"ti "<<*ti< +#include + +vector> pairSum(vector &arr, int s){ + int size = sizeof(arr)/sizeof(arr[0]); + vector m ; + sort(arr.begin(),arr.end()); + int diff = 0 ; + for(int i=0;i=arr[i]){ + for(int j=i+1;j + using namespace std; + + int mod = 1000000007; + +int pow(int arr[], int n){ + + if(n<0){ + return 1; + } + + return (arr[n]*pow(arr,n-1))%mod; +} + + +int *getProductArrayExceptSelf(int *arr, int n) +{ + int *pro = new int[n]; + int p= pow(arr,n-1); + cout<<"power = "<