Skip to content
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

Array questions added #280

Open
wants to merge 2 commits into
base: main
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
16 changes: 16 additions & 0 deletions Coding/C++/Best_time_to_sell_and_buy+stock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <bits/stdc++.h>
using namespace std;

int bestTimeToBuyAndSellStock(vector<int>&price) {
int n = price.size();
int minPrice = INT_MAX;
int mini = price[0];
int maxPro = 0;

for(int i=0; i<n; i++){
int cost = price[i] - mini;
maxPro = max(maxPro,cost);
mini = min(mini,price[i]);
}
return maxPro;
}
49 changes: 49 additions & 0 deletions Coding/C++/BinarySearch/BinarySearch.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
//SEARCHING IN ARRAY "BINARY SEARCH"

//Time complexity : O(log n)

#include <iostream>
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]<key){
start = mid+1; //update the strt point and move to right part
}
else{
end= mid-1; //update the end point and move to left part
}
}
return -1; //if not in the array return -1
}

int main()
{

int n;
cin >> n; //taking size of array

int arr[n]; //declear array of size n

// taking sorted array elements from user
for(int i=0; i<n; i++){
cin>>arr[i];
}

int key ;
cin>>key; //taking key or number which we have to search in our array

cout<<binarysearch(arr,n,key)<<endl; //passing arrgument to the function

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

int FirstOcc(vector<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;
end = mid - 1;
}

else if (arr[mid] < key)
{ // right part
start = mid + 1;
}
else
{
end = mid - 1; // left part
}
}
return ans;
}

int LastOcc(vector<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;
start = mid + 1;
}

else if (arr[mid] < key)
{ // right part
start = mid + 1;
}
else
{
end = mid - 1; // left part
}
}
return ans;
}

int count(vector<int> &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;
}
Original file line number Diff line number Diff line change
@@ -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<iostream>
using namespace std;

int Pivot_ele(int arr[], int a){
int s=0;
int e = a-1;

while(s<e){
int mid = s+ (e-s)/2;

//if array is not rotated thn
if(arr[e]>arr[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<<Pivot_ele(arr,4);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
//Time compleity : O(log n)

#include<iostream>
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]<key){ //right part
start = mid+1;
}
else{
end = mid-1; //left part
}
}
return ans;
}

int LastOcc(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;
start = mid+1;
}

else if(arr[mid]<key){ //right part
start = mid+1;
}
else{
end = mid-1; //left part
}
}
return ans;
}




int main(){
int arr[10] = {1,2,3,3,3,3,3,3,3,6};

cout<<FirstOcc(arr,10,3)<<" "<<LastOcc(arr,10,3)<<endl;
// if we want to print how many times a elements repeat or can say as total number of occ in array

int total = LastOcc(arr,10,3) - FirstOcc(arr,10,3) + 1 ;
cout<<total<<endl;
}
43 changes: 43 additions & 0 deletions Coding/C++/BinarySearch/Kth_missing_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include<bits/stdc++.h>
using namespace std;

int findKthPositive(vector<int>& arr, int k) {
//Brute Forrce solution
//TC- O[n]
//SC- O[1]

// for(int i=0; i<arr.size(); i++){
// if(arr[i] <= k){
// k++;
// }
// else{
// break;
// }
// }

// return k;

// optimal solution using binary search
//TC- O[log N]
//SC- O[1]

int n =arr.size();
int low =0;
int high = n-1;

while(low<=high){
int mid= (low+high)/2;

int missing = arr[mid] - (mid+1);

if(missing<k){
low=mid+1;
}
else{
high = mid-1;
}
}
// return low+k; or
return high+1+k;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@

#include<bits/stdc++.h>
using namespace std;

//TC- O[N * log(sum-max)]
// SC - O[1]


int daysReq(vector<int> &weights , int cap){
int day =1;
int load= 0;
int n = weights.size();

for(int i=0; i<n; i++){
if(load+weights[i] > cap){
day++;
load = weights[i];
}
else{
load += weights[i];
}
}

return day;
}

int leastWeightCapacity(vector<int> &weights, int d)
{
int maxi = INT_MIN;
int sum =0;
int n = weights.size();

// for(int i=0; i<n; i++){
// maxi = max(maxi,weights[i]);
// sum += weights[i];
// }

maxi = *max_element(weights.begin(), weights.end());
sum = accumulate(weights.begin(), weights.end() , 0);

int low = maxi;
int high = sum;

while(low<=high){
int mid = low + (high-low) /2;
int minDay = daysReq(weights,mid);
if(minDay <= d){
high = mid-1;
}
else{
low= mid+1;
}
}

return low;
}
28 changes: 28 additions & 0 deletions Coding/C++/BinarySearch/Min_element_in_rotated_sorted_array.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <bits/stdc++.h>
using namespace std;

int findMin(vector<int>& 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;
}
Loading