From b7885d9da509723c0f3c85d905284f54c157a1b4 Mon Sep 17 00:00:00 2001 From: Pathikrit Chanda <52066229+pathikrit2407@users.noreply.github.com> Date: Fri, 1 Oct 2021 19:01:31 +0530 Subject: [PATCH 001/105] Added Further New Problems --- AGGCOWS.cpp | 58 +++++++++++ ARRANGE_Arranging_Amplifiers_SPOJ.cpp | 92 +++++++++++++++++ DEFKIN_Defense_of_a_Kingdom.cpp | 63 ++++++++++++ DIEHARD_SPOJ.cpp | 109 +++++++++++++++++++++ GERGOVIA_Wine_trading_in_Gergovia_SPOJ.cpp | 74 ++++++++++++++ Hakerearth_Kth_Element_Again_Searching.cpp | 61 ++++++++++++ count_num_of_occurence_of_x.cpp | 65 ++++++++++++ find_maxi_ele_in_Bitonic_array.cpp | 59 +++++++++++ first&lastOccurenceofEle.cpp | 63 ++++++++++++ position_in_a_infinite_sorted_array.cpp | 49 +++++++++ 10 files changed, 693 insertions(+) create mode 100644 AGGCOWS.cpp create mode 100644 ARRANGE_Arranging_Amplifiers_SPOJ.cpp create mode 100644 DEFKIN_Defense_of_a_Kingdom.cpp create mode 100644 DIEHARD_SPOJ.cpp create mode 100644 GERGOVIA_Wine_trading_in_Gergovia_SPOJ.cpp create mode 100644 Hakerearth_Kth_Element_Again_Searching.cpp create mode 100644 count_num_of_occurence_of_x.cpp create mode 100644 find_maxi_ele_in_Bitonic_array.cpp create mode 100644 first&lastOccurenceofEle.cpp create mode 100644 position_in_a_infinite_sorted_array.cpp diff --git a/AGGCOWS.cpp b/AGGCOWS.cpp new file mode 100644 index 0000000..48413ab --- /dev/null +++ b/AGGCOWS.cpp @@ -0,0 +1,58 @@ +#include +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007 +#define MAX 105 +#define ll long long int +#define pq priority_queue ,greater> +#define pqpii priority_queue ,vector >,greater > > + + +int noc(ll ar[],ll n,ll mind) +{ + int cows=1,left=0; + + for(int i=1;i= mind) + { + left=i; + cows++; + } + } + + return cows; +} + +int main() +{ + ll t;cin>>t; + while(t--) + { + ll n,c,maxi=INT_MIN;cin>>n>>c; + ll points[n]; + + for(int i=0;i>points[i]; + maxi=max(maxi,points[i]); + } + + sort(points,points+n); + + ll l=1,r=maxi,ans=-1; + while(l<=r) + { + ll mid = l + (r-l)/2; + + if(noc(points,n,mid) >= c) + { + ans=mid; + l=mid+1; + } + else r=mid-1; + } + + cout< +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007 +#define MAX 108 +#define ll long long int +#define ull unsigned long long int +#define pqp priority_queue < ll , vector , greater > +#define pqpii priority_queue ,vector>,greater>> +#define pie 3.1415926536 +#define pb push_back +#define M 998244353 +#define ff first +#define ss second +#define pi pair +#define pii pair< int, pair > +#define all(v) (v).begin(),(v).end() +#define mem(a,b) memset(a,b,sizeof(a)) + +//Greedy Approach -> + +/* +1.sort in reverse to get answer + +Edge cases -> +1.If array consist of 2 element and they are 2 and 3 then in sorted increasing order will be answer +2.if array consist of 1s then move them in front and the remaining array as reverse + A)but if remaining are 2 elements as 2,3 then in increasing order +*/ + +int main() +{ + fast; + int t;cin>>t; + while(t--) + { + ll n;cin>>n; + ll ar[n]; + map mp; + for(ll i=0;i>ar[i]; + mp[ar[i]]++; + } + + sort(ar,ar+n); + + if(mp.size()==2 && mp[2]==1 && mp[3]==1) + { + for(int i=0;i v; + for(int i=0;i()); + for(int i=0;i()); + for(int i=0;i +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007 +#define MAX 108 +#define ll long long int +#define ull unsigned long long int +#define pqp priority_queue < ll , vector , greater > +#define pqpii priority_queue ,vector>,greater>> +#define pie 3.1415926536 +#define pb push_back +#define M 998244353 +#define ff first +#define ss second +#define pi pair +#define pii pair< int, pair > +#define all(v) (v).begin(),(v).end() +#define mem(a,b) memset(a,b,sizeof(a)) + +//Greedy Approach --> for finding the max uncovered rectangle we have to find max diff between unintersected x and y coordinates of tower + + +int main() +{ + fast; + int t;cin>>t; + while(t--) + { + ll xx,yy,n;cin>>xx>>yy>>n; + + if(n==0) + { + cout< x,y; + + x.push_back(0); + y.push_back(yy+1); + x.push_back(xx+1); + y.push_back(0); + + for(ll i=0;i>x1>>y1; + x.push_back(x1); + y.push_back(y1); + } + + sort(x.begin(),x.end()); + sort(y.begin(),y.end()); + + ll xmax=INT_MIN,ymax=INT_MIN; + for(int i=1;i +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007 +#define MAX 108 +#define ll long long int +#define ull unsigned long long int +#define pqp priority_queue < ll , vector , greater > +#define pqpii priority_queue ,vector>,greater>> +#define pie 3.1415926536 +#define pb push_back +#define M 998244353 +#define ff first +#define ss second +#define pi pair +#define pii pair< int, pair > +#define all(v) (v).begin(),(v).end() +#define mem(a,b) memset(a,b,sizeof(a)) + + int dp[1005][1005][2][2][2] ; + +int solve(int h,int a,int air ,int water ,int fire) +{ + if(h<=0 || a<=0) return 0; + + + if(dp[h][a][air][water][fire]!=-1) return dp[h][a][air][water][fire]; + + if(air==0 && water==0 && fire==0) + { + return dp[h][a][air][water][fire] = max(solve(h+3,a+2,1,0,0), + max(solve(h-5,a-10,0,1,0),solve(h-20,a+5,0,0,1))); + } + + if(air) + { + return dp[h][a][air][water][fire] = max(solve(h-5,a-10,0,1,0),solve(h-20,a+5,0,0,1))+1; + } + + if(water) + { + return dp[h][a][air][water][fire] = max(solve(h+3,a+2,1,0,0),solve(h-20,a+5,0,0,1))+1; + } + + if(fire) + { + return dp[h][a][air][water][fire] = max(solve(h-5,a-10,0,1,0),solve(h+3,a+2,1,0,0))+1; + } +} + + + +int main() +{ + fast; + int t;cin>>t; + while(t--) + { + int h,a;cin>>h>>a; + + //----------------Dp Solution--------------------------// + + memset(dp,-1,sizeof(dp)); + cout< air , (water/fire) , air , (water/fire) , air ....... + + Noted : Water and Fire moves have to be done if we satify the minimum criteria */ + + ll time=0; + + while(1) + { + if(time%2==0) + { + h+=3; + a+=2; + } + else + { + if((a>10 && h>5)) + { + a-=10; + h-=5; + } + else if(h>20) + { + a+=5; + h-=20; + } + else break; + } + + if(h<=0 || a<=0) break; + + time++; + } + + cout< +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007 +#define MAX 108 +#define ll long long int +#define ull unsigned long long int +#define pqp priority_queue < ll , vector , greater > +#define pqpii priority_queue ,vector>,greater>> +#define pie 3.1415926536 +#define pb push_back +#define M 998244353 +#define ff first +#define ss second +#define pi pair +#define pii pair< int, pair > +#define all(v) (v).begin(),(v).end() +#define mem(a,b) memset(a,b,sizeof(a)) + + +int main() +{ +// int t;cin>>t; + + //--------------- Greedy 2 Pointer Approach --------------------// + + /* + Here we have to find the minimum work performed when a bottle(s) of wine is transported from house x to y , + the basic idea is to provide transportation between the closest buyer and seller at each time + */ + + while(1) + { + ll n;cin>>n; + + if(n==0)return 0; + + ll house[n]; + + for(ll i=0;i>house[i]; + + ll i=0,j=0; + + ll totwork=0; + + while(i= 0 && j= abs(house[j])) + { + totwork += abs(i-j)*abs(house[j]); + house[i] += house[j]; + house[j] = 0; + } + else + { + totwork += abs(i-j)*house[i]; + house[j]+=house[i]; + house[i]=0; + } + // cout< +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007 +#define MAX 105 +#define ll long long int +#define pq priority_queue ,greater> +#define pqpii priority_queue ,vector >,greater > > + +int main() + { + fast; + ll t;cin>>t; + while(t--) + { + ll n,q; + cin>>n>>q; + vector > v; + for(int i=0;i>a>>b; + v.push_back(make_pair(a,b)); + } + + sort(v.begin(),v.end()); + + vector > mrg; + mrg.push_back(v[0]); + + for(int i=1;i= v[i].first) + { + mrg[mrg.size()-1].second = v[i].second; + } + else + mrg.push_back(v[i]); + } + + while(q--) + { + ll k;cin>>k; + ll ans=-1; + for(int i=0;i0;i++) + { + ll diff = mrg[i].second - mrg[i].first + 1; + if(diff >= k) + { + ans=mrg[i].first + (k-1); + break; + } + else + { + k-=diff; + } + } + + cout< +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007; +#define ll long long int +#define pq priority_queue ,greater> +#define pqpii priority_queue ,vector>,greater>> + +void firstOccurence(int ar[],int n,int x,int &res,int low, int high) +{ + int fO = low + (high - low)/2 ; + if(low <= high) + { + if(ar[fO]==x) + { + res = fO; + firstOccurence(ar,n,x,res,low,fO-1); + } + else if(ar[fO] > x) + { + firstOccurence(ar,n,x,res,low,fO-1); + } + else firstOccurence(ar,n,x,res,fO+1,high); + } + return ; +} + + +void lastOccurence(int ar[],int n,int x,int &res,int low, int high) +{ + int lO = low + (high - low)/2 ; + if(low <= high) + { + if(ar[lO]==x) + { + res = lO; + lastOccurence(ar,n,x,res,lO+1,high); + } + else if(ar[lO] > x) + { + lastOccurence(ar,n,x,res,low,lO-1); + } + else lastOccurence(ar,n,x,res,lO+1,high); + } + return ; +} + + +int main() +{ + int t;cin>>t; + while(t--) + { + int n;cin>>n; + int ar[n]; + for(int i=0;i>ar[i]; + int x,first=-1,last=-1;cin>>x; + firstOccurence(ar,n,x,first,0,n-1); + lastOccurence(ar,n,x,last,0,n-1); + if(first==-1 && last==-1) + cout<<-1< +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007; +#define ll long long int +#define pq priority_queue ,greater> +#define pqpii priority_queue ,vector>,greater>> + +/* +Same solution for both peak element problem + and finding maxi element in Bitonic Array +*/ +int solve(int ar[],int n) +{ + if(n==1) + return 0; + ll l=0,r=n-1; + while(l<=r) + { + ll mid = l+(r-l)/2; + if(mid==0 || mid==n-1) + { + if(mid==0) + { + if(ar[mid]0 && midar[mid]) + r=mid-1; + else if(ar[mid+1]>ar[mid]) + l=mid+1; + } + } + return -1; +} + +int main() +{ + int t;cin>>t; + while(t--) + { + int n;cin>>n; + int ar[n]; + for(int i=0;i>ar[i]; + cout< +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007; +#define ll long long int +#define pq priority_queue ,greater> +#define pqpii priority_queue ,vector>,greater>> + +void firstOccurence(int ar[],int n,int x,int &res,int low, int high) +{ + int fO = low + (high - low)/2 ; + if(low <= high) + { + if(ar[fO]==x) + { + res = fO; + firstOccurence(ar,n,x,res,low,fO-1); + } + else if(ar[fO] > x) + { + firstOccurence(ar,n,x,res,low,fO-1); + } + else firstOccurence(ar,n,x,res,fO+1,high); + } + return ; +} + + +void lastOccurence(int ar[],int n,int x,int &res,int low, int high) +{ + int lO = low + (high - low)/2 ; + if(low <= high) + { + if(ar[lO]==x) + { + res = lO; + lastOccurence(ar,n,x,res,lO+1,high); + } + else if(ar[lO] > x) + { + lastOccurence(ar,n,x,res,low,lO-1); + } + else lastOccurence(ar,n,x,res,lO+1,high); + } + return ; +} + + +int main() +{ + int t;cin>>t; + while(t--) + { + int n;cin>>n; + int ar[n]; + for(int i=0;i>ar[i]; + int x,res=-1;cin>>x; + //firstOccurence(ar,n,x,res,0,n-1); + //lastOccurence(ar,n,x,res,0,n-1); + cout< +using namespace std; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007; +#define ll long long int +#define pq priority_queue ,greater> +#define pqpii priority_queue ,vector>,greater>> + +int solve(int ar[],int x) +{ + /* + as it is a infinite array so we don't know the size of the array + so considering the end pointer as 1st index and start as 0th index + we start finding the range in which our value can be found , we increase + the end by 2x and change the start into the previous end we do that until we + get our range in which X can be found + */ + int l=0,r=1; + while(!(ar[l]<=x && ar[r]>=x)) + { + l=r; + r*=2; + } + /*After we find the range simply use Binary Search to find X*/ + while(l<=r) + { + int mid = l + (r-l)/2; + if(ar[mid]==x) + return mid; + else if(ar[mid]>x) + r=mid-1; + else if(ar[mid]>t; + while(t--) + { + int n;cin>>n; + int ar[n]; + for(int i=0;i>ar[i]; + int x;cin>>x; + cout< Date: Fri, 1 Oct 2021 19:18:12 +0530 Subject: [PATCH 002/105] Add files via upload --- BoxOfChocolate.cpp | 31 +++++++++++++++ FER.cpp | 47 ++++++++++++++++++++++ FindYourGift.cpp | 80 +++++++++++++++++++++++++++++++++++++ InOutTime.cpp | 33 ++++++++++++++++ LRNDSA.cpp | 87 +++++++++++++++++++++++++++++++++++++++++ NumberGame.cpp | 31 +++++++++++++++ PrimeGene.cpp | 26 ++++++++++++ StrNo.cpp | 31 +++++++++++++++ Universe.cpp | 17 ++++++++ beautiful_array.cpp | 42 ++++++++++++++++++++ chef_jump.cpp | 26 ++++++++++++ chopsticks.cpp | 21 ++++++++++ compiler.cpp | 43 ++++++++++++++++++++ covid_.cpp | 34 ++++++++++++++++ cupcakes.cpp | 47 ++++++++++++++++++++++ goodPrefix.cpp | 42 ++++++++++++++++++++ leftRequest.cpp | 31 +++++++++++++++ matrixDecomp.cpp | 11 ++++++ no_adj_element_same.cpp | 64 ++++++++++++++++++++++++++++++ penalty.cpp | 40 +++++++++++++++++++ planeJourney.cpp | 29 ++++++++++++++ sell_car.cpp | 32 +++++++++++++++ stpdMac.cpp | 28 +++++++++++++ swap_digit.cpp | 42 ++++++++++++++++++++ unitGcd.cpp | 5 +++ 25 files changed, 920 insertions(+) create mode 100644 BoxOfChocolate.cpp create mode 100644 FER.cpp create mode 100644 FindYourGift.cpp create mode 100644 InOutTime.cpp create mode 100644 LRNDSA.cpp create mode 100644 NumberGame.cpp create mode 100644 PrimeGene.cpp create mode 100644 StrNo.cpp create mode 100644 Universe.cpp create mode 100644 beautiful_array.cpp create mode 100644 chef_jump.cpp create mode 100644 chopsticks.cpp create mode 100644 compiler.cpp create mode 100644 covid_.cpp create mode 100644 cupcakes.cpp create mode 100644 goodPrefix.cpp create mode 100644 leftRequest.cpp create mode 100644 matrixDecomp.cpp create mode 100644 no_adj_element_same.cpp create mode 100644 penalty.cpp create mode 100644 planeJourney.cpp create mode 100644 sell_car.cpp create mode 100644 stpdMac.cpp create mode 100644 swap_digit.cpp create mode 100644 unitGcd.cpp diff --git a/BoxOfChocolate.cpp b/BoxOfChocolate.cpp new file mode 100644 index 0000000..425c439 --- /dev/null +++ b/BoxOfChocolate.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + + +bool KthSetBit(int n,int k){ + int i=-1; + if(k){ + while(i!=k-1){ + n=n>>1; + i++; + } + } + if(n&1==1) + return 1; + else + return 0; +} + +int main() + { + int t;cin>>t; + while(t){ + int n,k; + cin>>n>>k; + if(KthSetBit(n,k)) + cout<<"Yes"< +using namespace std; +vector adj[1001]; +bool visited[1001]; +int cc_nodes; +void dsf(int node){ +cc_nodes++; +visited[node]=1; +for(int i=0;i>t; + while(t){ + int n,m; + cin>>n>>m; + for(int i=0;i<=n;i++){ + visited[i]=0,adj[i].clear(); + } + for(int i=0;i>a>>b; + adj[a].push_back(b); + adj[b].push_back(a); + } + + int cc_count=0,res=1; + + for(int i=1;i<=n;i++){ + if(visited[i]==0){ + cc_nodes=0; + dsf(i); + cc_count++; + res=(res*cc_nodes)%1000000007; + } + } + cout< +using namespace std; +int main(){ + int t; + cin>>t; + while(t){ + int n;cin>>n; + string str; + cin>>str; + int x=0,y=0; + bool conL=0,conR=0,conU=0,conD=0; + for(int i=0;i +using namespace std; +vector adj[1000]; +int in[1001],out[1001]; +bool visited[1001]; +int timer=1; +void dfs(int node){ + visited[node]=1; + in[node]=timer; + timer++; + for(int i=0;i>n>>m; + for(int i=0;i>a>>b; + adj[a].push_back(b); + adj[b].push_back(a); + } + dfs(1); + for(int i=1;i<=n;i++){ + cout<<"For node "<"<"< +using namespace std; +typedef long long int ll; + +ll mod=1000000007; +ll exp(ll x, ll n){ +ll res=1; +while(n){ + if(n%2==1){ + res=(res*x)%mod; + n--; + }else{ + x=(x*x)%mod; + n/=2; + } +} +return res; +} + +int main(){ + ll x,n; + cin>>x>>n; + if(n>=5){ + cout<<1< +using namespace std; +typedef long long int ll; + +ll mod=1000000007; +ll exp(ll x, ll n){ +ll res=1; +while(n){ + if(n%2==1){ + res=(res*x); + n--; + }else{ + x=(x*x); + n/=2; + } +} +return res; +} + +int main(){ + int x,n; + cin>>x>>n; + if(n>=5){ + cout<<1< +typedef long long int ll; +using namespace std; + +int GCD(int a,int b){ + if(b==0) + return a; + else + return GCD(b,a%b); +} + +int main(){ + int t; + cin>>t; + while(t){ + int n; + cin>>n; + ll ar[n]; + for(int i=0;i>ar[i]; + int gcdd[n] ; + gcdd[0]= ar[0]; + for(int i=1;i +typedef long long int ll; +using namespace std; +int main(){ + int t; + cin>>t; + bool ar[1000000001]; + long long int max=1000000000; + ar[1]=ar[0]=true; + for(long long int i=2;i*i<=max;i++){ + if(ar[i]==false){ + for(long long int j=i*i;j<=max;j=j+i) + ar[j]=true; + } + } + while(t){ + long long int m,n; + cin>>m>>n; + for(long long int i=m;i<=n;i++){ + if(ar[i]==0) + cout< +typedef long long int ll; +using namespace std ; + +int primeSum(int n){ + int res=0; + for(ll i=2;i*i<=n;i++){ + if(n%i==0){ + while(n%i==0){ + res++; + n/=i; + } + } + } + return res + (n>1); +} + +int main(){ + ll t; + cin>>t; + while(t){ + ll x,k; + cin>>x>>k; + if(primeSum(x)>=k){ + cout<<1< +using namespace std; + +int main() { + + int ar[5]; + for(int i=0;i<5;i++) + cin>>ar[i]; + for(int i=0;i<5;i++){ + if(ar[i]==42) + break; + else + cout< +using namespace std; + +int main() { + // your code goes here + int T; + cin>>T; + while(T--) + { + int N; + cin>>N; + + int o=0,tw=0,th=0; + for(int i=0;i>temp; + if(temp%4==0){continue;} + if(temp%4==1){o++;} + if(temp%4==2){tw++;} + if(temp%4==3){th++;} + } + + if((o+2*tw+3*th)%4!=0){cout<<"-1"< +typedef long long int ll; +using namespace std; +int main(){ + ll t; + cin>>t; + while(t){ + ll n,s,v,p,customers; + cin>>n; + vector profit; + for(int i=1;i<=n;i++){ + cin>>s>>p>>v; + if(p%(s+1)==0) + customers=p/(s+1); + else + customers=floor(p/(s+1)); + profit.push_back(customers*v); + } + sort(profit.begin(),profit.end(),greater()); + cout< +typedef long long int ll; +using namespace std; +int main(){ + ll n,d,count=0; + cin>>n>>d; + ll ar[n]; + for(int i=0;i>ar[i]; + } + sort(ar,ar+n); + for(int i=0;i +typedef long long int ll; +using namespace std; +bool match(char a, char b){ +if(a=='<' && b=='>') + return 1; +else + return 0; +} + + +int main(){ + int t; + cin>>t; + while(t){ + string str; + ll c=0,maxc=0; + cin>>str; + stack stck; + for(int i=0;i'){ + if(stck.empty()) + break; + else { + char temp=stck.top(); + stck.pop(); + if(match(temp,str[i])) + c++; + } + } + if(stck.empty()){ + if(c>maxc) + maxc=c; + c=0; + } + } + cout< +using namespace std ; +int main(){ +int t; +cin>>t; +while(t){ + int n; + cin>>n; + int ar[n]; + for(int i=0;i>ar[i]; + } + vector v; + for(int i=0;i=6) + cout<<"YES"< +using namespace std; +typedef long long int ll; +/* +ll func(ll x){ + vector diff; + for(ll i=1;i*i<=x;i++){ + if(x%i==0){ + if(x/i==i) + diff.push_back(0); + else{ + diff.push_back(abs(x/i-i)); + } + } + } + sort(diff.begin(),diff.end()); + return diff[0]; +} + + +int main(){ + int t; + cin>>t; + while(t){ + ll cake; + cin>>cake; + ll mini= func(cake); + cout< +#define ll long long int +using namespace std; +int main() +{ + +ll t; +cin>>t; +while(t--){ +string s ; +cin>>s ; +ll k,x ; +cin>>k>>x; + +ll ans = 0 ; +map mp ; +mp.clear() ; +for(ll i=0;i x ){ +k-- ; +mp[s[i]]-- ; +if(k<0)break ; +else continue ; +} + +ans++; + +} + +cout< +using namespace std; +typedef long long int ll; +int main(){ +int t; +cin>>t; +while(t){ + ll floors,n,q,f,d; + cin>>n>>q; + ll dest=0; floors=0; + while(q){ + cin>>f>>d; + if(floors==0 && f>d){ + floors+=f+(f-d); + dest=d; + } + else if(floors==0 && f<=d){ + floors+=d; + dest=d; + } + else if(floors>0){ + floors+=abs(dest-f)+abs(d-f); + dest=d; + } + --q; + } + cout< +using namespace std; +int main(){ +vector arr={1,2,3,4,5}; +cout< +using namespace std ; +#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0); +#define mod 1000000007; +#define ll long long int + +int main(){ +int t;cin>>t; +while(t--){ +string s;cin>>s; +map mp; +for(ll i=0;i> freqq; +for(auto it:mp){ + if(it.second>maxi) + maxi=it.second; + freqq.push_back(make_pair(it.second,it.first)); +} +sort(freqq.begin(),freqq.end(),greater>()); +if(maxi<= s.length()-maxi+1){ + string s1; + for(ll i=0;i1){ + while(freqq[i].first!=0){ + s1+=freqq[i].second; + freqq[i].first--; + } + }else{ + s1+=freqq[i].second; + freqq[i].first--; + } + } + string s2,s3; + for(ll i=0;i<=s1.length()/2;i++) + s2+=s1[i]; + for(ll i=s1.length()/2+1;i +#include +typedef long long int ll; +using namespace std; +int main(){ + ll t; + cin>>t; + while(t){ + ll n; + cin>>n; + string str; + cin>>str; + ll nA=0,nB=0,pA=0,pB=0; + for(int i=0;i<2*n;i++){ + if(i%2==0){ + if(str[i]=='1') + nA++; + pA++; + } + else if(i%2==1){ + if(str[i]=='1') + nB++; + pB++; + } + if((nB-nA)>(n-pA)){ + cout<(n-pB)){ + cout< +using namespace std; +int ar1[1000],ar2[1000]; +int main(){ +int t,n,s,rem; +cin>>t; +while(t){ + cin>>n>>s; + rem=100-s; + for(int i=0;i>ar1[i]; + for(int i=0;i>ar2[i]; + int mini=9999; + for(int i=0;i +typedef long long int ll; +using namespace std; +ll mod = 1000000007; +int main(){ + ll t; + cin>>t; + while(t){ + ll n; + cin>>n; + ll ar[n]; + stack cost; + for(ll i=0;i>ar[i]; + sort(ar,ar+n); + for(ll i=0;i0) + sum=(sum+temp)%mod; + i++; + } + + cout< +typedef long long int ll; +using namespace std; +int main(){ + ll t; + cin>>t; + while(t){ + ll n; + cin>>n; + ll ar[n]; + for(int i=1;i<=n;i++) + cin>>ar[i]; + ll tokens[100000]={0},l=n,sum=0; + while(l>=1){ + sum+=l; + for(int i=1;i<=l;i++){ + tokens[i]+=1; + } + for(int i=1;i<=l;i++){ + if(ar[i]==tokens[i]) + l=i-1; + } + } + cout< +#include +using namespace std; +int main() +{ + int t;cin>>t; + while(t--) + { + int a,b;cin>>a>>b; + if(a/10!=0 &&b/10!=0) + { + int max=0; + if((a+b)>max){max=a+b;} + if((((b/10)*10)+(a%10)+((a/10)*10)+(b%10))>max) + {max=(((b/10)*10)+(a%10)+((a/10)*10)+(b%10));} + if((((b%10)*10)+(a%10)+((b/10)*10)+(a/10))>max) + {max=(((b%10)*10)+(a%10)+((b/10)*10)+(a/10));} + if((((a/10)*10)+(b/10)+((a%10)*10)+(b%10))>max) + {max=(((a/10)*10)+(b/10)+((a%10)*10)+(b%10));} + cout<max){max=a+b;} + if(((b/10)+(a*10)+(b%10))>max){max=((b/10)+(a*10)+(b%10));} + if(((b%10)+(a)+((b/10)*10))>max){max=((b%10)+(a)+((b/10)*10));} + cout<max){max=a+b;} + if(((a/10)+(b*10)+(a%10))>max){max=((a/10)+(b*10)+(a%10));} + if(((a%10)+(b)+((a/10)*10))>max){max=((a%10)+(b)+((a/10)*10));} + cout< +using namespace std ; +int main(){ + +} From 40cb7bc27952dfe8a96e3b4899892d27aa847792 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Tue, 5 Oct 2021 13:16:22 +0530 Subject: [PATCH 003/105] Add files via upload --- ...e of Variable After Performing Operations.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 nandincpp/Final Value of Variable After Performing Operations.cpp diff --git a/nandincpp/Final Value of Variable After Performing Operations.cpp b/nandincpp/Final Value of Variable After Performing Operations.cpp new file mode 100644 index 0000000..1bfac3a --- /dev/null +++ b/nandincpp/Final Value of Variable After Performing Operations.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int finalValueAfterOperations(vector& operations) + { + int x = 0; + for(int i=0;i Date: Tue, 5 Oct 2021 13:16:47 +0530 Subject: [PATCH 004/105] Add files via upload --- nandincpp/Sum of Beauty in the Array.cpp | 33 ++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 nandincpp/Sum of Beauty in the Array.cpp diff --git a/nandincpp/Sum of Beauty in the Array.cpp b/nandincpp/Sum of Beauty in the Array.cpp new file mode 100644 index 0000000..f28f197 --- /dev/null +++ b/nandincpp/Sum of Beauty in the Array.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + int sumOfBeauties(vector& a) + { + int n = a.size(); + vector pref(n+1), suff(n+1); + + int maxl = a[0]; + for(int i=1;i=0;i--) + { + suff[i] = minr; + minr = min(minr, a[i]); + } + + int ans = 0; + for(int i=1;ipref[i] && a[i]a[i-1] && a[i] Date: Tue, 5 Oct 2021 13:17:06 +0530 Subject: [PATCH 005/105] Add files via upload --- nandincpp/Detect Squares.cpp | 38 ++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 nandincpp/Detect Squares.cpp diff --git a/nandincpp/Detect Squares.cpp b/nandincpp/Detect Squares.cpp new file mode 100644 index 0000000..d831b7f --- /dev/null +++ b/nandincpp/Detect Squares.cpp @@ -0,0 +1,38 @@ +class DetectSquares { + vector> p_store; + int cnt_p[1001][1001] = {}; + +public: + DetectSquares() { + + } + + void add(vector point) + { + cnt_p[point[0]][point[1]]++; + p_store.push_back({point[0], point[1]}); + } + + int count(vector point) + { + int ans = 0; + int x1 = point[0], y1 = point[1]; + + for(auto& i : p_store) + { + if(abs(x1-i.first)==0 || abs(x1-i.first)!=abs(y1-i.second)) + continue; + + ans += (cnt_p[x1][i.second] * cnt_p[i.first][y1]); + } + + return ans; + } +}; + +/** + * Your DetectSquares object will be instantiated and called as such: + * DetectSquares* obj = new DetectSquares(); + * obj->add(point); + * int param_2 = obj->count(point); + */ \ No newline at end of file From 85c5fa51b80e381c6d65deab68af7e6efebcb90a Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 6 Oct 2021 14:56:37 +0530 Subject: [PATCH 006/105] Add files via upload --- nandincpp/Convert 1D Array Into 2D Array.cpp | 22 ++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 nandincpp/Convert 1D Array Into 2D Array.cpp diff --git a/nandincpp/Convert 1D Array Into 2D Array.cpp b/nandincpp/Convert 1D Array Into 2D Array.cpp new file mode 100644 index 0000000..827533a --- /dev/null +++ b/nandincpp/Convert 1D Array Into 2D Array.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector> construct2DArray(vector& original, int m, int n) + { + vector> ans(m, vector(n)); + + if(m*n != original.size()) + return {}; + int k = 0; + + for(int i=0;i Date: Wed, 6 Oct 2021 14:56:53 +0530 Subject: [PATCH 007/105] Add files via upload --- ...ngs With Concatenation Equal to Target.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 nandincpp/Number of Pairs of Strings With Concatenation Equal to Target.cpp diff --git a/nandincpp/Number of Pairs of Strings With Concatenation Equal to Target.cpp b/nandincpp/Number of Pairs of Strings With Concatenation Equal to Target.cpp new file mode 100644 index 0000000..7cc084b --- /dev/null +++ b/nandincpp/Number of Pairs of Strings With Concatenation Equal to Target.cpp @@ -0,0 +1,24 @@ +class Solution { + int cnt = 0; + +// void fun() +// { +// string now = a[i]; +// for() +// } +public: + int numOfPairs(vector& a, string target) + { + int n = a.size(); + for(int i=0;i Date: Wed, 6 Oct 2021 14:57:12 +0530 Subject: [PATCH 008/105] Add files via upload --- .../Maximize the Confusion of an Exam.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 nandincpp/Maximize the Confusion of an Exam.cpp diff --git a/nandincpp/Maximize the Confusion of an Exam.cpp b/nandincpp/Maximize the Confusion of an Exam.cpp new file mode 100644 index 0000000..f4a9ef9 --- /dev/null +++ b/nandincpp/Maximize the Confusion of an Exam.cpp @@ -0,0 +1,31 @@ +class Solution { + int fun(string s, int k, char c) + { + int ans = 0; + int start = 0, cnt = 0; + + for(int e=0;e k) + { + if(s[start] != c) + cnt--; + + start++; + } + + ans = max(ans, e-start+1); + } + + return ans; + } + +public: + int maxConsecutiveAnswers(string s, int k) + { + return max(fun(s, k, 'T'), fun(s, k ,'F')); + } +}; \ No newline at end of file From c22233165fdeb622248cc555bd93dfde1385c478 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Fri, 8 Oct 2021 11:56:47 +0530 Subject: [PATCH 009/105] Add files via upload --- nandincpp/Minimum Moves to Convert String.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 nandincpp/Minimum Moves to Convert String.cpp diff --git a/nandincpp/Minimum Moves to Convert String.cpp b/nandincpp/Minimum Moves to Convert String.cpp new file mode 100644 index 0000000..bffa60d --- /dev/null +++ b/nandincpp/Minimum Moves to Convert String.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + int minimumMoves(string s) + { + int cnt = 0; + for(int i=0;i Date: Fri, 8 Oct 2021 11:57:09 +0530 Subject: [PATCH 010/105] Add files via upload --- nandincpp/Find Missing Observations.cpp | 28 +++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 nandincpp/Find Missing Observations.cpp diff --git a/nandincpp/Find Missing Observations.cpp b/nandincpp/Find Missing Observations.cpp new file mode 100644 index 0000000..bd2c953 --- /dev/null +++ b/nandincpp/Find Missing Observations.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector missingRolls(vector& rolls, int mean, int n) + { + int sum = 0, m = rolls.size(); + for(int i=0;i6*n) + return {}; + + int each = need/n; + int rem = need % n; + + vector ans(n, each); + for(int i=0;i Date: Fri, 8 Oct 2021 13:12:48 +0530 Subject: [PATCH 011/105] leetcode question find the duplicate number. --- Findtheduplicatenumber.cpp | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 Findtheduplicatenumber.cpp diff --git a/Findtheduplicatenumber.cpp b/Findtheduplicatenumber.cpp new file mode 100644 index 0000000..01531a5 --- /dev/null +++ b/Findtheduplicatenumber.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + int findDuplicate(vector& nums) { + int high = nums.size() - 1; + int low = 1; + while (low <= high) { + int mid = low + (high - low) / 2; + int c = 0; + for (int a: nums) { + if (a <= mid) { + ++ c; + } + } + if (c <= mid) { + low = mid + 1; + } else { + high = mid - 1; + } + } + return low; + } +}; From 0cee8f8103dc10e5875cbb2590b01965bd204cf4 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sat, 9 Oct 2021 22:44:19 +0530 Subject: [PATCH 012/105] Add files via upload --- ... Difference Between Increasing Elements.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 nandincpp/Maximum Difference Between Increasing Elements.cpp diff --git a/nandincpp/Maximum Difference Between Increasing Elements.cpp b/nandincpp/Maximum Difference Between Increasing Elements.cpp new file mode 100644 index 0000000..fb52e57 --- /dev/null +++ b/nandincpp/Maximum Difference Between Increasing Elements.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int maximumDifference(vector& a) + { + int min_so_far = a[0]; + int n = a.size(), ans = -1;; + + for(int i=1;i min_so_far) + ans = max(ans, a[i]-min_so_far); + + min_so_far = min(min_so_far, a[i]); + } + + return ans; + } +}; \ No newline at end of file From 73b5f321e28b5fa3a8067a90d71f3180828ab059 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sat, 9 Oct 2021 22:44:40 +0530 Subject: [PATCH 013/105] Add files via upload --- nandincpp/Grid Game.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 nandincpp/Grid Game.cpp diff --git a/nandincpp/Grid Game.cpp b/nandincpp/Grid Game.cpp new file mode 100644 index 0000000..63d620b --- /dev/null +++ b/nandincpp/Grid Game.cpp @@ -0,0 +1,29 @@ +#define ll long long + +class Solution { +public: + long long gridGame(vector>& grid) + { + ll n = grid[0].size(); + vector pref1(n), pref2(n); + pref1[0] = grid[0][0]; + pref2[0] = grid[1][0]; + + for(ll i=1;i Date: Sun, 10 Oct 2021 13:49:26 +0530 Subject: [PATCH 014/105] Add files via upload --- nandincpp/Greatest Common Divisor of Strings.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 nandincpp/Greatest Common Divisor of Strings.cpp diff --git a/nandincpp/Greatest Common Divisor of Strings.cpp b/nandincpp/Greatest Common Divisor of Strings.cpp new file mode 100644 index 0000000..c500f14 --- /dev/null +++ b/nandincpp/Greatest Common Divisor of Strings.cpp @@ -0,0 +1,12 @@ +class Solution { + +public: + string gcdOfStrings(string s1, string s2) + { + if(s1+s2 != s2+s1) + return ""; + + int g = gcd(s1.size(), s2.size()); + return s1.substr(0, g); + } +}; \ No newline at end of file From dd1bdc4e9818ac208ba48b57adc7230173a705ad Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 10 Oct 2021 13:49:38 +0530 Subject: [PATCH 015/105] Add files via upload --- nandincpp/Repeated Substring Pattern.cpp | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 nandincpp/Repeated Substring Pattern.cpp diff --git a/nandincpp/Repeated Substring Pattern.cpp b/nandincpp/Repeated Substring Pattern.cpp new file mode 100644 index 0000000..7310c6e --- /dev/null +++ b/nandincpp/Repeated Substring Pattern.cpp @@ -0,0 +1,8 @@ +class Solution { +public: + bool repeatedSubstringPattern(string s) + { + bool f = (s+s).substr(1,2*s.size()-1).find(s)!=s.size()-1; + return f; + } +}; \ No newline at end of file From 2848c448d4aa890c9967096788fb28b6e26a54a1 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 10 Oct 2021 13:50:05 +0530 Subject: [PATCH 016/105] Add files via upload --- nandincpp/Repeated String Match.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 nandincpp/Repeated String Match.cpp diff --git a/nandincpp/Repeated String Match.cpp b/nandincpp/Repeated String Match.cpp new file mode 100644 index 0000000..78053a6 --- /dev/null +++ b/nandincpp/Repeated String Match.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int repeatedStringMatch(string a, string b) + { + int cnt = 0; + string s = ""; + + while(s.size() < b.size()) + { + cnt++; + s += a; + } + + if(s.find(b) != string::npos) + return cnt; + + s += s; + cnt++; + if(s.find(b) != string::npos) + return cnt; + + return -1; + } +}; \ No newline at end of file From 6ce615ba6199b048a5507d4d7388fbd1465c5954 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 10 Oct 2021 13:50:44 +0530 Subject: [PATCH 017/105] Add files via upload --- ...um Operations to Make a Uni-Value Grid.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 nandincpp/Minimum Operations to Make a Uni-Value Grid.cpp diff --git a/nandincpp/Minimum Operations to Make a Uni-Value Grid.cpp b/nandincpp/Minimum Operations to Make a Uni-Value Grid.cpp new file mode 100644 index 0000000..72b9c65 --- /dev/null +++ b/nandincpp/Minimum Operations to Make a Uni-Value Grid.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + int minOperations(vector>& grid, int x) + { + int n = grid.size(); + int m = grid[0].size(); + vector a; + + for(int i=0;i Date: Sun, 10 Oct 2021 13:50:57 +0530 Subject: [PATCH 018/105] Add files via upload --- nandincpp/Two Out of Three.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 nandincpp/Two Out of Three.cpp diff --git a/nandincpp/Two Out of Three.cpp b/nandincpp/Two Out of Three.cpp new file mode 100644 index 0000000..b0b9116 --- /dev/null +++ b/nandincpp/Two Out of Three.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + vector twoOutOfThree(vector& a1, vector& a2, vector& a3) + { + unordered_map mp; + unordered_set s1, s2, s3; + vector ans; + + for(auto i : a1) + s1.insert(i); + for(auto i : a2) + s2.insert(i); + for(auto i : a3) + s3.insert(i); + + for(auto i : s1) + mp[i]++; + for(auto i : s2) + mp[i]++; + for(auto i : s3) + mp[i]++; + + + for(auto i : mp) + { + if(i.second >= 2) + ans.push_back(i.first); + } + + return ans; + } +}; \ No newline at end of file From f59ce1d3d5afa31d7787075fd5b2453b8534608f Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 10 Oct 2021 13:51:10 +0530 Subject: [PATCH 019/105] Add files via upload --- nandincpp/Maximum Ascending Subarray Sum.cpp | 30 ++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 nandincpp/Maximum Ascending Subarray Sum.cpp diff --git a/nandincpp/Maximum Ascending Subarray Sum.cpp b/nandincpp/Maximum Ascending Subarray Sum.cpp new file mode 100644 index 0000000..35d9a39 --- /dev/null +++ b/nandincpp/Maximum Ascending Subarray Sum.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int maxAscendingSum(vector& a) + { + int prev = a[0], sum = a[0]; + int ans = 0; + + if(a.size() == 1) + return a[0]; + + for(int i=1;i Date: Tue, 12 Oct 2021 11:27:30 +0530 Subject: [PATCH 020/105] Add files via upload --- nandincpp/Four Divisors.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 nandincpp/Four Divisors.cpp diff --git a/nandincpp/Four Divisors.cpp b/nandincpp/Four Divisors.cpp new file mode 100644 index 0000000..3e4989b --- /dev/null +++ b/nandincpp/Four Divisors.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int sumFourDivisors(vector& nums) { + int ans = 0, sum, cnt; + for(auto &num: nums) { + sum = 0; + cnt = 0; + for(int j=1; j*j<=num; j++) { + if (num%j==0) { + if (num/j == j) { + sum += j; + cnt++; + } else { + sum += j + (num/j); + cnt += 2; + } + } + } + if (cnt == 4) ans += sum; + } + return ans; + } +}; \ No newline at end of file From e061b0cc1e1436981adc89fb92136f9f7bfedcd0 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Tue, 12 Oct 2021 11:27:49 +0530 Subject: [PATCH 021/105] Add files via upload --- .../Circle and Rectangle Overlapping.cpp | 16 ++++++ nandincpp/Diameter of Binary Tree.cpp | 32 ++++++++++++ nandincpp/Stock Price Fluctuation.cpp | 51 +++++++++++++++++++ 3 files changed, 99 insertions(+) create mode 100644 nandincpp/Circle and Rectangle Overlapping.cpp create mode 100644 nandincpp/Diameter of Binary Tree.cpp create mode 100644 nandincpp/Stock Price Fluctuation.cpp diff --git a/nandincpp/Circle and Rectangle Overlapping.cpp b/nandincpp/Circle and Rectangle Overlapping.cpp new file mode 100644 index 0000000..d883649 --- /dev/null +++ b/nandincpp/Circle and Rectangle Overlapping.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + bool checkOverlap(int r, int x_center, int y_center, int x1, int y1, int x2, int y2) + { + int xnew = x1 - x_center; + int ynew = y1 - y_center; + + int xnew2 = x2 - x_center; + int ynew2 = y2 - y_center; + + int x_sq = (xnew*xnew2)>0 ? min(xnew*xnew, xnew2*xnew2) : 0; + int y_sq = (ynew*ynew2)>0 ? min(ynew*ynew, ynew2*ynew2) : 0; + + return x_sq+y_sq <= r*r; + } +}; \ No newline at end of file diff --git a/nandincpp/Diameter of Binary Tree.cpp b/nandincpp/Diameter of Binary Tree.cpp new file mode 100644 index 0000000..9f44013 --- /dev/null +++ b/nandincpp/Diameter of Binary Tree.cpp @@ -0,0 +1,32 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { + int height(TreeNode* root, int& dist) + { + if(root == NULL) + return 0; + + int lh = height(root->left, dist); + int rh = height(root->right, dist); + + dist = max(dist, lh+rh); + return max(lh, rh)+1; + } + +public: + int diameterOfBinaryTree(TreeNode* root) + { + int dist = 0; + height(root, dist); + return dist; + } +}; \ No newline at end of file diff --git a/nandincpp/Stock Price Fluctuation.cpp b/nandincpp/Stock Price Fluctuation.cpp new file mode 100644 index 0000000..6dbb1a5 --- /dev/null +++ b/nandincpp/Stock Price Fluctuation.cpp @@ -0,0 +1,51 @@ +class StockPrice { + map stock, minmax; + int curr = -1; + +public: + StockPrice() { + + } + + void update(int timestamp, int price) + { + if(stock.count(timestamp)) + { + int prev = stock[timestamp]; + minmax[prev]--; + + if(minmax[prev] == 0) + minmax.erase(prev); + } + + stock[timestamp] = price; + minmax[price]++; + curr = max(curr, timestamp); + } + + int current() + { + return stock[curr]; + } + + int maximum() + { + auto maxi = minmax.rbegin(); + return maxi->first; + } + + int minimum() + { + auto mini = minmax.begin(); + return mini->first; + } +}; + +/** + * Your StockPrice object will be instantiated and called as such: + * StockPrice* obj = new StockPrice(); + * obj->update(timestamp,price); + * int param_2 = obj->current(); + * int param_3 = obj->maximum(); + * int param_4 = obj->minimum(); + */ \ No newline at end of file From f809132b9bc2a7e334511a771a1d41965bbc5a75 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 21 Oct 2021 10:21:49 +0530 Subject: [PATCH 022/105] Add files via upload --- ...if Numbers Are Ascending in a Sentence.cpp | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 nandincpp/Check if Numbers Are Ascending in a Sentence.cpp diff --git a/nandincpp/Check if Numbers Are Ascending in a Sentence.cpp b/nandincpp/Check if Numbers Are Ascending in a Sentence.cpp new file mode 100644 index 0000000..f7901e0 --- /dev/null +++ b/nandincpp/Check if Numbers Are Ascending in a Sentence.cpp @@ -0,0 +1,42 @@ +class Solution { + bool isSorted(vector& v) + { + if(v.size() == 1) + return 1; + + for(int i=1;i= v[i]) + return 0; + } + return 1; + } + +public: + bool areNumbersAscending(string s) + { + vector v; + + for(int i=0;i Date: Fri, 22 Oct 2021 18:37:19 +0530 Subject: [PATCH 023/105] Add files via upload --- nandincpp/Simple Bank System.cpp | 43 ++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 nandincpp/Simple Bank System.cpp diff --git a/nandincpp/Simple Bank System.cpp b/nandincpp/Simple Bank System.cpp new file mode 100644 index 0000000..4dde011 --- /dev/null +++ b/nandincpp/Simple Bank System.cpp @@ -0,0 +1,43 @@ +class Bank { + vector balance; + bool valid(int account) + { + return account > 0 && account <= balance.size(); + } + bool enough(int account, long long money) + { + return balance[account - 1] >= money; + } +public: + Bank(vector& balance) : balance(balance) {} + bool transfer(int from, int to, long long money) { + if (valid(from) && valid(to) && enough(from, money)) { + balance[from - 1] -= money; + balance[to - 1] += money; + return true; + } + return false; + } + bool deposit(int account, long long money) { + if (valid(account)) { + balance[account - 1] += money; + return true; + } + return false; + } + bool withdraw(int account, long long money) { + if (valid(account) && enough(account, money)) { + balance[account - 1] -= money; + return true; + } + return false; + } +}; + +/** + * Your Bank object will be instantiated and called as such: + * Bank* obj = new Bank(balance); + * bool param_1 = obj->transfer(account1,account2,money); + * bool param_2 = obj->deposit(account,money); + * bool param_3 = obj->withdraw(account,money); + */ \ No newline at end of file From a688a64a527ba25cab3b28221b6fa65208a1faa6 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Fri, 22 Oct 2021 18:37:37 +0530 Subject: [PATCH 024/105] Add files via upload --- ...ces if Both Neighbors are the Same Color.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 nandincpp/Remove Colored Pieces if Both Neighbors are the Same Color.cpp diff --git a/nandincpp/Remove Colored Pieces if Both Neighbors are the Same Color.cpp b/nandincpp/Remove Colored Pieces if Both Neighbors are the Same Color.cpp new file mode 100644 index 0000000..d773644 --- /dev/null +++ b/nandincpp/Remove Colored Pieces if Both Neighbors are the Same Color.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool winnerOfGame(string colors) + { + if(colors.size() < 3) + return 0; + + vector v(2, 0); + for(int i=1;iv[1]; + } +}; \ No newline at end of file From 7f754f38e507b7f4aaeb267b05873437b1ca5bcf Mon Sep 17 00:00:00 2001 From: pravirtual <69043391+pravirtual@users.noreply.github.com> Date: Fri, 29 Oct 2021 19:51:01 +0530 Subject: [PATCH 025/105] Create top_view_of_tree.cpp code for top view of a binary tree --- top_view_of_tree.cpp | 88 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 top_view_of_tree.cpp diff --git a/top_view_of_tree.cpp b/top_view_of_tree.cpp new file mode 100644 index 0000000..9748113 --- /dev/null +++ b/top_view_of_tree.cpp @@ -0,0 +1,88 @@ +// C++ program to print top +// view of binary tree + +#include +using namespace std; + +// Structure of binary tree +struct Node { + Node* left; + Node* right; + int hd; + int data; +}; + +// function to create a new node +Node* newNode(int key) +{ + Node* node = new Node(); + node->left = node->right = NULL; + node->data = key; + return node; +} + +// function should print the topView of +// the binary tree +void topview(Node* root) +{ + if (root == NULL) + return; + queue q; + map m; + int hd = 0; + root->hd = hd; + + // push node and horizontal distance to queue + q.push(root); + + cout << "The top view of the tree is : \n"; + + while (q.size()) { + hd = root->hd; + + // count function returns 1 if the container + // contains an element whose key is equivalent + // to hd, or returns zero otherwise. + if (m.count(hd) == 0) + m[hd] = root->data; + if (root->left) { + root->left->hd = hd - 1; + q.push(root->left); + } + if (root->right) { + root->right->hd = hd + 1; + q.push(root->right); + } + q.pop(); + root = q.front(); + } + + for (auto i = m.begin(); i != m.end(); i++) { + cout << i->second << " "; + } +} + +// Driver Program to test above functions +int main() +{ + /* Create following Binary Tree + 1 + / \ + 2 3 + \ + 4 + \ + 5 + \ + 6*/ + Node* root = newNode(1); + root->left = newNode(2); + root->right = newNode(3); + root->left->right = newNode(4); + root->left->right->right = newNode(5); + root->left->right->right->right = newNode(6); + cout << "Following are nodes in top view of Binary " + "Tree\n"; + topview(root); + return 0; +} From c0653831cb4d797876fb51b00244f7695dcc933f Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 31 Oct 2021 11:56:45 +0530 Subject: [PATCH 026/105] Add files via upload --- nandincpp/Kth Distinct String in an Array.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 nandincpp/Kth Distinct String in an Array.cpp diff --git a/nandincpp/Kth Distinct String in an Array.cpp b/nandincpp/Kth Distinct String in an Array.cpp new file mode 100644 index 0000000..7ec124e --- /dev/null +++ b/nandincpp/Kth Distinct String in an Array.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + string kthDistinct(vector& a, int k) + { + unordered_map mp; + + for(int i=0;i Date: Sun, 31 Oct 2021 11:57:00 +0530 Subject: [PATCH 027/105] Add files via upload --- nandincpp/Minimum Height Trees.cpp | 49 ++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 nandincpp/Minimum Height Trees.cpp diff --git a/nandincpp/Minimum Height Trees.cpp b/nandincpp/Minimum Height Trees.cpp new file mode 100644 index 0000000..0599c8a --- /dev/null +++ b/nandincpp/Minimum Height Trees.cpp @@ -0,0 +1,49 @@ +class Solution { +public: + vector findMinHeightTrees(int n, vector>& edges) + { + if(n == 1) + return {0}; + + queue q; + vector> g(n); + vector deg(n, 0); + + for(auto i : edges) + { + g[i[0]].push_back(i[1]); + g[i[1]].push_back(i[0]); + + deg[i[0]]++, deg[i[1]]++; + } + + vector ans; + + for(int i=0;i Date: Mon, 1 Nov 2021 10:49:54 +0530 Subject: [PATCH 028/105] Add files via upload --- .../Minimum Operations to Convert Number.cpp | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 nandincpp/Minimum Operations to Convert Number.cpp diff --git a/nandincpp/Minimum Operations to Convert Number.cpp b/nandincpp/Minimum Operations to Convert Number.cpp new file mode 100644 index 0000000..3709d8f --- /dev/null +++ b/nandincpp/Minimum Operations to Convert Number.cpp @@ -0,0 +1,40 @@ +class Solution { +public: + int minimumOperations(vector& a, int start, int goal) + { + queue q; + q.push(start); + vector vis(1001, 0); + int ans = 0; + + while(!q.empty()) + { + auto sizee = q.size(); + + while(sizee--) + { + auto p = q.front(); + q.pop(); + + if(p == goal) + return ans; + if(p<0 || p>1000 || vis[p]==1) + continue; + vis[p] = 1; + + for(int i=0;i Date: Mon, 1 Nov 2021 10:50:08 +0530 Subject: [PATCH 029/105] Add files via upload --- ...umber of Nodes Between Critical Points.cpp | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 nandincpp/Find the Minimum and Maximum Number of Nodes Between Critical Points.cpp diff --git a/nandincpp/Find the Minimum and Maximum Number of Nodes Between Critical Points.cpp b/nandincpp/Find the Minimum and Maximum Number of Nodes Between Critical Points.cpp new file mode 100644 index 0000000..c3ec9eb --- /dev/null +++ b/nandincpp/Find the Minimum and Maximum Number of Nodes Between Critical Points.cpp @@ -0,0 +1,46 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + vector nodesBetweenCriticalPoints(ListNode* head) + { + if(head->next->next==NULL || head->next->next->next==NULL) + return {-1, -1}; + + ListNode* p1 = head, *p2 = head->next, *p3 = head->next->next; + int i = 1; + vector v; + + while(p3 != NULL) + { + if(p2->val>p1->val && p2->val>p3->val) + v.push_back(i); + else if(p2->valval && p2->valval) + v.push_back(i); + + i++; + p1 = p1->next; + p2 = p2->next; + p3 = p3->next; + } + + if(v.size() <= 1) + return {-1, -1}; + + int localminima = INT_MAX; + for(int l = 0 ; l < v.size()-1;l++) + { + localminima = min(v[l+1] - v[l],localminima); + } + + return {localminima , v.back()-v[0]}; + } +}; \ No newline at end of file From 75790b0428f05f7e109dd8c71b62283907ac6932 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 1 Nov 2021 10:50:22 +0530 Subject: [PATCH 030/105] Add files via upload --- nandincpp/Smallest Index With Equal Value.cpp | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 nandincpp/Smallest Index With Equal Value.cpp diff --git a/nandincpp/Smallest Index With Equal Value.cpp b/nandincpp/Smallest Index With Equal Value.cpp new file mode 100644 index 0000000..2c8ca61 --- /dev/null +++ b/nandincpp/Smallest Index With Equal Value.cpp @@ -0,0 +1,13 @@ +class Solution { +public: + int smallestEqual(vector& a) + { + for(int i=0;i Date: Fri, 5 Nov 2021 10:33:44 +0530 Subject: [PATCH 031/105] Add files via upload --- .../Minimum Absolute Difference in BST.cpp | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 nandincpp/Minimum Absolute Difference in BST.cpp diff --git a/nandincpp/Minimum Absolute Difference in BST.cpp b/nandincpp/Minimum Absolute Difference in BST.cpp new file mode 100644 index 0000000..d52c0a0 --- /dev/null +++ b/nandincpp/Minimum Absolute Difference in BST.cpp @@ -0,0 +1,39 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { + vector v; + + void inorder(TreeNode* root) + { + if(root == NULL) + return ; + + inorder(root->left); + v.push_back(root->val); + inorder(root->right); + } + +public: + int getMinimumDifference(TreeNode* root) + { + if(root == NULL) + return 0; + + int ans = 10004; + + inorder(root); + for(int i=0;i Date: Tue, 9 Nov 2021 10:24:55 +0530 Subject: [PATCH 032/105] Add files via upload --- nandincpp/Vowels of All Substrings.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 nandincpp/Vowels of All Substrings.cpp diff --git a/nandincpp/Vowels of All Substrings.cpp b/nandincpp/Vowels of All Substrings.cpp new file mode 100644 index 0000000..5db6ff6 --- /dev/null +++ b/nandincpp/Vowels of All Substrings.cpp @@ -0,0 +1,14 @@ +class Solution { +public: + long long countVowels(string word) + { + long long ans = 0, n = word.size(); + for(int i=0;i Date: Tue, 9 Nov 2021 10:25:18 +0530 Subject: [PATCH 033/105] Add files via upload --- nandincpp/Maximum Subarray Min-Product.cpp | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 nandincpp/Maximum Subarray Min-Product.cpp diff --git a/nandincpp/Maximum Subarray Min-Product.cpp b/nandincpp/Maximum Subarray Min-Product.cpp new file mode 100644 index 0000000..27046f2 --- /dev/null +++ b/nandincpp/Maximum Subarray Min-Product.cpp @@ -0,0 +1,55 @@ +#define ll long long + +class Solution { + const int mod = 1e9 + 7; +public: + int maxSumMinProduct(vector& a) + { + int n = a.size(); + vector left(n, -1), right(n, -1); + vector s; // stack + + for(int i=0;i=0;i--) + { + while(!s.empty() && a[i]<=a[s.back()]) + s.pop_back(); + + if(!s.empty()) + right[i] = s.back(); + s.push_back(i); + } + + vector pref(n, 0); + pref[0] = a[0]; + + for(int i=1;i Date: Fri, 19 Nov 2021 11:29:38 +0530 Subject: [PATCH 034/105] Add files via upload --- ...ther Two Strings are Almost Equivalent.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 nandincpp/Check Whether Two Strings are Almost Equivalent.cpp diff --git a/nandincpp/Check Whether Two Strings are Almost Equivalent.cpp b/nandincpp/Check Whether Two Strings are Almost Equivalent.cpp new file mode 100644 index 0000000..202387f --- /dev/null +++ b/nandincpp/Check Whether Two Strings are Almost Equivalent.cpp @@ -0,0 +1,19 @@ +class Solution { +public: + bool checkAlmostEquivalent(string word1, string word2) + { + vector f1(26, 0), f2(26, 0); + for(auto i : word1) + f1[i-'a']++; + for(auto i : word2) + f2[i-'a']++; + + for(int i=0;i<26;i++) + { + if(abs(f1[i]-f2[i]) > 3) + return 0; + } + + return 1; + } +}; \ No newline at end of file From 72b00c8d81d6148fd83c992f4e8c2c8b2e8e694a Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 21 Nov 2021 22:14:57 +0530 Subject: [PATCH 035/105] Add files via upload --- nandincpp/Watering Plants.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 nandincpp/Watering Plants.java diff --git a/nandincpp/Watering Plants.java b/nandincpp/Watering Plants.java new file mode 100644 index 0000000..05c7fa5 --- /dev/null +++ b/nandincpp/Watering Plants.java @@ -0,0 +1,27 @@ +class Solution { + public int wateringPlants(int[] plants, int capacity) + { + int pref = 0, step = 0; + int n = plants.length; + + for(int i=0;i Date: Sun, 21 Nov 2021 22:15:11 +0530 Subject: [PATCH 036/105] Add files via upload --- nandincpp/Watering Plants.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 nandincpp/Watering Plants.cpp diff --git a/nandincpp/Watering Plants.cpp b/nandincpp/Watering Plants.cpp new file mode 100644 index 0000000..803df06 --- /dev/null +++ b/nandincpp/Watering Plants.cpp @@ -0,0 +1,27 @@ +class Solution { +public: + int wateringPlants(vector& plants, int capacity) + { + int step = 0, pref = 0; + + for(int i=0;i Date: Mon, 22 Nov 2021 03:05:14 +0530 Subject: [PATCH 037/105] Add files via upload --- nandincpp/Two Furthest Houses With Different Colors.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 nandincpp/Two Furthest Houses With Different Colors.cpp diff --git a/nandincpp/Two Furthest Houses With Different Colors.cpp b/nandincpp/Two Furthest Houses With Different Colors.cpp new file mode 100644 index 0000000..c3a483a --- /dev/null +++ b/nandincpp/Two Furthest Houses With Different Colors.cpp @@ -0,0 +1,9 @@ +class Solution { +public: + int maxDistance(vector& A) { + int n = A.size(), i = 0, j = n - 1; + while (A[0] == A[j]) j--; + while (A[n - 1] == A[i]) i++; + return max(n - 1 - i, j); + } +}; \ No newline at end of file From f82693d5c55ca719f7fd1497206cdbd2b741d767 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 22 Nov 2021 03:05:33 +0530 Subject: [PATCH 038/105] Add files via upload --- nandincpp/Range Frequency Queries.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 nandincpp/Range Frequency Queries.cpp diff --git a/nandincpp/Range Frequency Queries.cpp b/nandincpp/Range Frequency Queries.cpp new file mode 100644 index 0000000..bf64f03 --- /dev/null +++ b/nandincpp/Range Frequency Queries.cpp @@ -0,0 +1,20 @@ +class RangeFreqQuery { + unordered_map> m; +public: + RangeFreqQuery(vector& a) + { + for(int i=0;iquery(left,right,value); + */ \ No newline at end of file From 36dd80ccb4904e965913d9f9995df3a42e337f64 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 1 Dec 2021 21:47:10 +0530 Subject: [PATCH 039/105] Add files via upload --- ...uired to Collect Rainwater from Houses.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 nandincpp/Minimum Number of Buckets Required to Collect Rainwater from Houses.cpp diff --git a/nandincpp/Minimum Number of Buckets Required to Collect Rainwater from Houses.cpp b/nandincpp/Minimum Number of Buckets Required to Collect Rainwater from Houses.cpp new file mode 100644 index 0000000..62cd526 --- /dev/null +++ b/nandincpp/Minimum Number of Buckets Required to Collect Rainwater from Houses.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int minimumBuckets(string street) + { + int ans = 0, n = street.size(); + + for(int i=0;i=0 && street[i-1]=='B') + continue; + else if(i+1=0 && street[i-1]=='.') + { + ans++; + street[i-1] = 'B'; + } + + else + return -1; + } + } + + return ans; + } +}; \ No newline at end of file From efe4f0d87716765e720b8a71126d3b425991292f Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 1 Dec 2021 21:47:30 +0530 Subject: [PATCH 040/105] Add files via upload --- ...Count Common Words With One Occurrence.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 nandincpp/Count Common Words With One Occurrence.cpp diff --git a/nandincpp/Count Common Words With One Occurrence.cpp b/nandincpp/Count Common Words With One Occurrence.cpp new file mode 100644 index 0000000..75c9ece --- /dev/null +++ b/nandincpp/Count Common Words With One Occurrence.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int countWords(vector& words1, vector& words2) + { + unordered_map m1, m2; + + for(auto i : words1) + { + m1[i]++; + } + + for(auto i : words2) + { + m2[i]++; + } + + int ans = 0; + for(auto i : m1) + { + if(i.second != 1) + continue; + if(m2.count(i.first) && m2[i.first]==1) + ans++; + } + + return ans; + } +}; \ No newline at end of file From 77d8f9751ec27702ad8a18a36cf6e28cec608919 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sat, 4 Dec 2021 18:02:28 +0530 Subject: [PATCH 041/105] Add files via upload --- ...emoving Minimum and Maximum From Array.cpp | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 nandincpp/Removing Minimum and Maximum From Array.cpp diff --git a/nandincpp/Removing Minimum and Maximum From Array.cpp b/nandincpp/Removing Minimum and Maximum From Array.cpp new file mode 100644 index 0000000..7910262 --- /dev/null +++ b/nandincpp/Removing Minimum and Maximum From Array.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int minimumDeletions(vector& a) + { + int n = a.size(); + + if(n == 1) + return 1; + + int maxi = a[0], mini = a[0]; + int lp = 0, hp = 0; + + for(int i=0;i maxi) + { + maxi = a[i]; + hp = i; + } + } + + return min({max(lp, hp)+1, n-min(lp, hp), min(lp, hp)+1+n-max(lp, hp)}); + } +}; \ No newline at end of file From 07df94c1632f4a16e6cfaa1c06dd5083f2557a36 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Tue, 7 Dec 2021 13:43:36 +0530 Subject: [PATCH 042/105] Add files via upload --- ...m Cost to Move Chips to The Same Position.cpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 nandincpp/Minimum Cost to Move Chips to The Same Position.cpp diff --git a/nandincpp/Minimum Cost to Move Chips to The Same Position.cpp b/nandincpp/Minimum Cost to Move Chips to The Same Position.cpp new file mode 100644 index 0000000..b434e95 --- /dev/null +++ b/nandincpp/Minimum Cost to Move Chips to The Same Position.cpp @@ -0,0 +1,16 @@ +class Solution { +public: + int minCostToMoveChips(vector& chips) + { + int odd = 0, even = 0; + for(auto ele : chips) + { + if(ele % 2 != 0) + odd++; + else + even++; + } + + return min(odd, even); + } +}; \ No newline at end of file From 2aa6e6dddddc360f3a2c36db199d759b79b8fb64 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Tue, 7 Dec 2021 13:44:00 +0530 Subject: [PATCH 043/105] Add files via upload --- ...elete the Middle Node of a Linked List.cpp | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 nandincpp/Delete the Middle Node of a Linked List.cpp diff --git a/nandincpp/Delete the Middle Node of a Linked List.cpp b/nandincpp/Delete the Middle Node of a Linked List.cpp new file mode 100644 index 0000000..ad51e12 --- /dev/null +++ b/nandincpp/Delete the Middle Node of a Linked List.cpp @@ -0,0 +1,38 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* deleteMiddle(ListNode* head) + { + if(head == NULL) + return NULL; + if(head->next == NULL) + { + delete head; + return NULL; + } + + ListNode* slow = head, *fast = head; + ListNode* prev = head; + + while(fast!=NULL && fast->next!=NULL) + { + if(slow != head) + prev = prev->next; + slow = slow->next; + fast = fast->next->next; + } + + prev->next = slow->next; + delete slow; + return head; + } +}; \ No newline at end of file From b147b332f8d3896653630be0a4c0204e25277b4e Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 13 Dec 2021 21:57:47 +0530 Subject: [PATCH 044/105] Add files via upload --- nandincpp/Sum of Subarray Ranges.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 nandincpp/Sum of Subarray Ranges.cpp diff --git a/nandincpp/Sum of Subarray Ranges.cpp b/nandincpp/Sum of Subarray Ranges.cpp new file mode 100644 index 0000000..049cd72 --- /dev/null +++ b/nandincpp/Sum of Subarray Ranges.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + long long subArrayRanges(vector& a) + { + long long res = 0; + for(int i=0;i Date: Mon, 13 Dec 2021 21:58:02 +0530 Subject: [PATCH 045/105] Add files via upload --- nandincpp/Watering Plants II.cpp | 47 ++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 nandincpp/Watering Plants II.cpp diff --git a/nandincpp/Watering Plants II.cpp b/nandincpp/Watering Plants II.cpp new file mode 100644 index 0000000..ec81248 --- /dev/null +++ b/nandincpp/Watering Plants II.cpp @@ -0,0 +1,47 @@ +class Solution { +public: + int minimumRefill(vector& p, int capa, int capb) + { + int ans = 0, n = p.size(); + int i = 0, j = n-1; + int ca = capa, cb = capb; + + while(i <= j) + { + if(i == j) + { + if(ca >= cb) + { + ans += (ca < p[i]); + } + else + { + ans += (cb < p[i]); + } + + break; + } + + else + { + if(ca < p[i]) + { + ans++; + ca = capa; + } + if(cb < p[j]) + { + ans++; + cb = capb; + } + + ca -= p[i]; + cb -= p[j]; + } + + i++, j--; + } + + return ans; + } +}; \ No newline at end of file From e931a4e9fe70edce6c40dbed686ba95b50758a16 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Fri, 17 Dec 2021 17:31:35 +0530 Subject: [PATCH 046/105] Add files via upload --- nandincpp/Find Good Days to Rob the Bank.cpp | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 nandincpp/Find Good Days to Rob the Bank.cpp diff --git a/nandincpp/Find Good Days to Rob the Bank.cpp b/nandincpp/Find Good Days to Rob the Bank.cpp new file mode 100644 index 0000000..21448a8 --- /dev/null +++ b/nandincpp/Find Good Days to Rob the Bank.cpp @@ -0,0 +1,41 @@ +class Solution { +public: + vector goodDaysToRobBank(vector& security, int time) + { + int n = security.size(); + vector pref(n, 0), suff(n, 0); + int cnt = 0; + + for(int i=1;i=0;i--) + { + if(security[i] <= security[i+1]) + cnt++; + else + cnt = 0; + + suff[i] = cnt; + } + + vector ans; + for(int i=0;i=time && suff[i]>=time) + ans.push_back(i); + } + + return ans; + } +}; \ No newline at end of file From 89fdaca90d92d48cd2272f17c1232692402e2589 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Fri, 17 Dec 2021 17:31:51 +0530 Subject: [PATCH 047/105] Add files via upload --- nandincpp/Maximum Earnings From Taxi.cpp | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 nandincpp/Maximum Earnings From Taxi.cpp diff --git a/nandincpp/Maximum Earnings From Taxi.cpp b/nandincpp/Maximum Earnings From Taxi.cpp new file mode 100644 index 0000000..7b9600f --- /dev/null +++ b/nandincpp/Maximum Earnings From Taxi.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + long long maxTaxiEarnings(int n, vector>& rides) + { + unordered_map>> m; + for(auto i : rides) + m[i[0]].push_back({i[1], i[2]}); + + vector dp(n+1); + + for(int i=n-1;i>=1;i--) + { + dp[i] = dp[i+1]; + + for(auto [end, tip] : m[i]) + { + dp[i] = max(dp[i], dp[end] + end-i+tip); + } + } + + return dp[1]; + } +}; \ No newline at end of file From f284933a32d40ecc4875f94330c97164f8cbbb13 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Fri, 17 Dec 2021 17:32:11 +0530 Subject: [PATCH 048/105] Add files via upload --- nandincpp/Detonate the Maximum Bombs.cpp | 53 ++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 nandincpp/Detonate the Maximum Bombs.cpp diff --git a/nandincpp/Detonate the Maximum Bombs.cpp b/nandincpp/Detonate the Maximum Bombs.cpp new file mode 100644 index 0000000..638277b --- /dev/null +++ b/nandincpp/Detonate the Maximum Bombs.cpp @@ -0,0 +1,53 @@ +#define ll long long + +class Solution { +public: + int maxi = INT_MIN; + int n; + + void bfs(int i, vector>& bombs) + { + queue q; + q.push(i); + vector vis(n, 0); + int cnt = 1; + vis[i] = 1; + + while(!q.empty()) + { + auto p = q.front(); + q.pop(); + vis[p] = 1; + + ll nowx = bombs[p][0], nowy = bombs[p][1], nowr = bombs[p][2]; + + for(int k=0;k= ((nowx-togox)*(nowx-togox)+(nowy-togoy)*(nowy-togoy))) + { + vis[k] = 1; + q.push(k); + cnt++; + } + } + } + + maxi = max(maxi, cnt); + } + + int maximumDetonation(vector>& bombs) + { + n = bombs.size(); + for(int i=0;i Date: Fri, 17 Dec 2021 17:32:25 +0530 Subject: [PATCH 049/105] Add files via upload From 48721e022445ad644850b7c546839c5633674a7d Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Fri, 17 Dec 2021 17:32:49 +0530 Subject: [PATCH 050/105] Add files via upload --- ...uence of Length K With the Largest Sum.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 nandincpp/Find Subsequence of Length K With the Largest Sum.cpp diff --git a/nandincpp/Find Subsequence of Length K With the Largest Sum.cpp b/nandincpp/Find Subsequence of Length K With the Largest Sum.cpp new file mode 100644 index 0000000..d9d097e --- /dev/null +++ b/nandincpp/Find Subsequence of Length K With the Largest Sum.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + vector maxSubsequence(vector& a, int k) + { + vector> v; + for(int i=0;i> ans; + + for(int i=0;i res; + sort(ans.begin(), ans.end(), [&](auto& x, auto& y) + { + return x.second<=y.second; + }); + + for(int i=0;i Date: Mon, 20 Dec 2021 10:49:48 +0530 Subject: [PATCH 051/105] Add files via upload --- nandincpp/Adding Spaces to a String.cpp | 32 +++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 nandincpp/Adding Spaces to a String.cpp diff --git a/nandincpp/Adding Spaces to a String.cpp b/nandincpp/Adding Spaces to a String.cpp new file mode 100644 index 0000000..f5e6c55 --- /dev/null +++ b/nandincpp/Adding Spaces to a String.cpp @@ -0,0 +1,32 @@ +class Solution { +public: + string addSpaces(string s, vector& spaces) + { + int i = 0, j = 0; + string ans = ""; + + while(i Date: Wed, 5 Jan 2022 00:59:18 +0530 Subject: [PATCH 052/105] Add files via upload --- nandincpp/Complement of Base 10 Integer.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 nandincpp/Complement of Base 10 Integer.cpp diff --git a/nandincpp/Complement of Base 10 Integer.cpp b/nandincpp/Complement of Base 10 Integer.cpp new file mode 100644 index 0000000..26a5f16 --- /dev/null +++ b/nandincpp/Complement of Base 10 Integer.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int bitwiseComplement(int n) + { + if(n == 0) + return 1; + + int copy = n; + int i = 0; + + while(copy != 0) + { + n = n^ (1<>1; + } + + return n; + } +}; \ No newline at end of file From 6a15b4cebdcd27a50490c787709bd00c834f2440 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 5 Jan 2022 00:59:31 +0530 Subject: [PATCH 053/105] Add files via upload --- .../Intervals Between Identical Elements.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 nandincpp/Intervals Between Identical Elements.cpp diff --git a/nandincpp/Intervals Between Identical Elements.cpp b/nandincpp/Intervals Between Identical Elements.cpp new file mode 100644 index 0000000..61c7b0d --- /dev/null +++ b/nandincpp/Intervals Between Identical Elements.cpp @@ -0,0 +1,45 @@ +#define ll long long + +class Solution { +public: + vector getDistances(vector& a) + { + unordered_map> m; + for(int i=0;i res(N, -1); + + + for(auto mp : m) + { + vector v = mp.second; + ll n = v.size(); + + vector pref(n+1, 0); + for(ll i=1;i<=n;i++) + { + pref[i] = pref[i-1] + v[i-1]; + } + + for(int i=0;i Date: Wed, 5 Jan 2022 00:59:42 +0530 Subject: [PATCH 054/105] Add files via upload --- ...mum Number of Words Found in Sentences.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 nandincpp/Maximum Number of Words Found in Sentences.cpp diff --git a/nandincpp/Maximum Number of Words Found in Sentences.cpp b/nandincpp/Maximum Number of Words Found in Sentences.cpp new file mode 100644 index 0000000..6cdf8e5 --- /dev/null +++ b/nandincpp/Maximum Number of Words Found in Sentences.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int mostWordsFound(vector& sentences) + { + int ans = 0; + + int i = 0, n = sentences.size(); + while(i < n) + { + stringstream ss(sentences[i]); + string token; + vector v; + + while(getline(ss, token, ' ')) + { + v.push_back(token); + } + + ans = max(ans, (int)v.size()); + i++; + } + + return ans; + } +}; \ No newline at end of file From cd9e5833cd40627f4cae319836099f83407c9730 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 5 Jan 2022 00:59:56 +0530 Subject: [PATCH 055/105] Add files via upload --- nandincpp/A Number After a Double Reversal.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 nandincpp/A Number After a Double Reversal.cpp diff --git a/nandincpp/A Number After a Double Reversal.cpp b/nandincpp/A Number After a Double Reversal.cpp new file mode 100644 index 0000000..1e98035 --- /dev/null +++ b/nandincpp/A Number After a Double Reversal.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + bool isSameAfterReversals(int num) + { + if(num == 0) + return 1; + + if(num%10 == 0) + return 0; + return 1; + } +}; \ No newline at end of file From ea99cd4fd43a526d223d11a50f5cc907707a822f Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 5 Jan 2022 01:00:16 +0530 Subject: [PATCH 056/105] Add files via upload --- ...l Possible Recipes from Given Supplies.cpp | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 nandincpp/Find All Possible Recipes from Given Supplies.cpp diff --git a/nandincpp/Find All Possible Recipes from Given Supplies.cpp b/nandincpp/Find All Possible Recipes from Given Supplies.cpp new file mode 100644 index 0000000..ce1d6f3 --- /dev/null +++ b/nandincpp/Find All Possible Recipes from Given Supplies.cpp @@ -0,0 +1,36 @@ +class Solution { +public: + vector findAllRecipes(vector& recipes, vector>& ingredients, vector& supplies) { + + unordered_map available; + vector res; + for(auto str:supplies) + available[str]=true; + bool keepCooking=true; + int n=recipes.size(); + vector created(n,0); + while(keepCooking) + { + keepCooking=false; + for(int i=0;i Date: Thu, 13 Jan 2022 21:44:22 +0530 Subject: [PATCH 057/105] Add files via upload --- .../Insert into a Binary Search Tree.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 nandincpp/Insert into a Binary Search Tree.cpp diff --git a/nandincpp/Insert into a Binary Search Tree.cpp b/nandincpp/Insert into a Binary Search Tree.cpp new file mode 100644 index 0000000..04b5b68 --- /dev/null +++ b/nandincpp/Insert into a Binary Search Tree.cpp @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* insertIntoBST(TreeNode* root, int val) + { + if(root == NULL) + return new TreeNode(val); + + if(val > root->val) + root->right = insertIntoBST(root->right, val); + else + root->left = insertIntoBST(root->left, val); + + return root; + } +}; \ No newline at end of file From 86b16e1cc856a09ab0bf777cd7a5071803b1cc36 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:44:34 +0530 Subject: [PATCH 058/105] Add files via upload --- nandincpp/Search in a Binary Search Tree.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 nandincpp/Search in a Binary Search Tree.cpp diff --git a/nandincpp/Search in a Binary Search Tree.cpp b/nandincpp/Search in a Binary Search Tree.cpp new file mode 100644 index 0000000..05cc8c3 --- /dev/null +++ b/nandincpp/Search in a Binary Search Tree.cpp @@ -0,0 +1,18 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + TreeNode* searchBST(TreeNode* root, int val) + { + return (root==NULL || root->val==val) ? root : ((valval)?searchBST(root->left, val):searchBST(root->right, val)); + } +}; \ No newline at end of file From 629345a775be68b81370e5e1a1eefa949856d223 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:44:54 +0530 Subject: [PATCH 059/105] Add files via upload --- ...t Words Obtained After Adding a Letter.cpp | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 nandincpp/Count Words Obtained After Adding a Letter.cpp diff --git a/nandincpp/Count Words Obtained After Adding a Letter.cpp b/nandincpp/Count Words Obtained After Adding a Letter.cpp new file mode 100644 index 0000000..d1d6ed0 --- /dev/null +++ b/nandincpp/Count Words Obtained After Adding a Letter.cpp @@ -0,0 +1,43 @@ +class Solution { + unordered_set st; + + bool ispossible(string target) + { + int n = target.size(); + + string ok = target.substr(1); + if(st.count(ok)) + return 1; + + for(int i=0;i& sw, vector& tw) + { + for(auto i : sw) + { + sort(i.begin(), i.end()); + st.insert(i); + } + + int ans = 0; + for(auto i : tw) + { + sort(i.begin(), i.end()); + + if(ispossible(i)) + ans++; + } + + return ans; + } +}; \ No newline at end of file From 759b5abc66f923622cffb3246cf594b84e1cd27f Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:45:31 +0530 Subject: [PATCH 060/105] Add files via upload --- nandincpp/Capitalize the Title.cpp | 46 ++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 nandincpp/Capitalize the Title.cpp diff --git a/nandincpp/Capitalize the Title.cpp b/nandincpp/Capitalize the Title.cpp new file mode 100644 index 0000000..690b69e --- /dev/null +++ b/nandincpp/Capitalize the Title.cpp @@ -0,0 +1,46 @@ +class Solution { +public: + string capitalizeTitle(string title) + { + stringstream ss(title); + string temp; + vector v; + + while(getline(ss, temp, ' ')) + v.push_back(temp); + + for(int i=0;i='A' && v[i][j]<='Z') + v[i][j] = v[i][j] | ' '; + } + continue; + } + + else + { + if(v[i][0]>='a' && v[i][0]<='z') + v[i][0] = v[i][0] & '_'; + + for(int j=1;j='A' && v[i][j]<='Z') + v[i][j] = v[i][j] | ' '; + } + } + } + + string ans = ""; + for(int i=0;i Date: Thu, 13 Jan 2022 21:45:44 +0530 Subject: [PATCH 061/105] Add files via upload --- .../Maximum Twin Sum of a Linked List.cpp | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 nandincpp/Maximum Twin Sum of a Linked List.cpp diff --git a/nandincpp/Maximum Twin Sum of a Linked List.cpp b/nandincpp/Maximum Twin Sum of a Linked List.cpp new file mode 100644 index 0000000..a3f61cb --- /dev/null +++ b/nandincpp/Maximum Twin Sum of a Linked List.cpp @@ -0,0 +1,47 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + int pairSum(ListNode* head) + { + if(head == NULL) + return 0; + + ListNode *slow = head, *fast = head; + while(fast->next!=NULL && fast->next->next!=NULL) + { + slow = slow->next; + fast = fast->next->next; + } + + ListNode *mid = slow; + slow = slow->next; + mid = mid->next; + int ans = 0; + + stack stk; + while(slow != NULL) + { + stk.push(slow->val); + slow = slow->next; + } + + while(head != mid) + { + int value = head->val + stk.top(); + ans = max(ans, value); + head = head->next; + stk.pop(); + } + + return ans; + } +}; \ No newline at end of file From 8092c197774107e37c12237af5a0c99cb3062047 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:46:02 +0530 Subject: [PATCH 062/105] Add files via upload --- ...rome by Concatenating Two Letter Words.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 nandincpp/Longest Palindrome by Concatenating Two Letter Words.cpp diff --git a/nandincpp/Longest Palindrome by Concatenating Two Letter Words.cpp b/nandincpp/Longest Palindrome by Concatenating Two Letter Words.cpp new file mode 100644 index 0000000..a3ca01a --- /dev/null +++ b/nandincpp/Longest Palindrome by Concatenating Two Letter Words.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + int longestPalindrome(vector& words) + { + int freq[26][26] = {}; + int ans = 0; + + for(auto i : words) + { + int x = i[0]-'a', y = i[1]-'a'; + + if(freq[y][x] > 0) + { + ans += 4; + freq[y][x]--; + } + + else + { + freq[x][y]++; + } + } + + for(int i=0;i<26;i++) + { + if(freq[i][i] > 0) + { + ans += 2; + break; + } + } + + return ans; + } +}; \ No newline at end of file From ff1eb25127fce7244b793f4ceba28c2e2a88cdb6 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:46:24 +0530 Subject: [PATCH 063/105] Add files via upload From 96e3b459dd24999d3763dffcfb89de6ceadc3273 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:46:37 +0530 Subject: [PATCH 064/105] Add files via upload --- ...ome by Concatenating Two Letter Words.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 nandincpp/Longest Palindrome by Concatenating Two Letter Words.java diff --git a/nandincpp/Longest Palindrome by Concatenating Two Letter Words.java b/nandincpp/Longest Palindrome by Concatenating Two Letter Words.java new file mode 100644 index 0000000..5dceebd --- /dev/null +++ b/nandincpp/Longest Palindrome by Concatenating Two Letter Words.java @@ -0,0 +1,32 @@ +class Solution { + public int longestPalindrome(String[] words) + { + int freq[][] = new int[26][26]; + int ans = 0; + + for(String s : words) + { + int x = s.charAt(0) - 'a'; + int y = s.charAt(1) - 'a'; + + if(freq[y][x] > 0) + { + ans += 4; + freq[y][x]--; + } + + else + freq[x][y]++; + } + + for(int i=0;i<26;i++) + if(freq[i][i] > 0) + { + ans += 2; + break; + } + + + return ans; + } +} \ No newline at end of file From 3337e1ce6ee0f972a021af673a00dde2d2068c14 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:46:51 +0530 Subject: [PATCH 065/105] Add files via upload --- ... if Every Row and Column Contains All Numbers.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 nandincpp/Check if Every Row and Column Contains All Numbers.cpp diff --git a/nandincpp/Check if Every Row and Column Contains All Numbers.cpp b/nandincpp/Check if Every Row and Column Contains All Numbers.cpp new file mode 100644 index 0000000..dd7d445 --- /dev/null +++ b/nandincpp/Check if Every Row and Column Contains All Numbers.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + bool checkValid(vector>& matrix) { + for (int i = 0, n = matrix.size(); i < n; ++i) { + bitset<100> row, col; + for (int j = 0; j < n; ++j) + row[matrix[i][j]-1] = col[matrix[j][i]-1] = true; + if (row.count() < n || col.count() < n) return false; + } + return true; + } +}; \ No newline at end of file From 6f2dfa5bdcc1b6b1dd8336839860cf2972d97789 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:47:08 +0530 Subject: [PATCH 066/105] Add files via upload --- ...ery Row and Column Contains All Numbers.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 nandincpp/Check if Every Row and Column Contains All Numbers.java diff --git a/nandincpp/Check if Every Row and Column Contains All Numbers.java b/nandincpp/Check if Every Row and Column Contains All Numbers.java new file mode 100644 index 0000000..a4a6753 --- /dev/null +++ b/nandincpp/Check if Every Row and Column Contains All Numbers.java @@ -0,0 +1,17 @@ +class Solution { + public boolean checkValid(int[][] matrix) { + int n = matrix.length; + for (int r = 0; r < n; ++r) { + Set row = new HashSet<>(); + Set col = new HashSet<>(); + for (int c = 0; c < n; ++c) { + row.add(matrix[r][c]); + col.add(matrix[c][r]); + } + if (row.size() < n || col.size() < n) { + return false; + } + } + return true; + } +} \ No newline at end of file From 099b64d1add04d9b13625e2651f2796c441d176e Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 13 Jan 2022 21:47:26 +0530 Subject: [PATCH 067/105] Add files via upload From b0442c06f19092435a99be4a707d58f5e80b4bce Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 23 Jan 2022 01:24:44 +0530 Subject: [PATCH 068/105] Add files via upload --- nandincpp/Destroying Asteroids.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 nandincpp/Destroying Asteroids.cpp diff --git a/nandincpp/Destroying Asteroids.cpp b/nandincpp/Destroying Asteroids.cpp new file mode 100644 index 0000000..ba585ca --- /dev/null +++ b/nandincpp/Destroying Asteroids.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + bool asteroidsDestroyed(int mass, vector& a) + { + bool flag = 1; + sort(a.begin(), a.end()); + long long m = mass; + + for(int i=0;i= a[i]) + { + m += (long long)a[i]; + } + else + { + flag = 0; + break; + } + } + + return flag; + } +}; \ No newline at end of file From 6af7e6c5941546e5699d0ad67967d54a5bd07322 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 23 Jan 2022 01:29:55 +0530 Subject: [PATCH 069/105] Add files via upload --- nandincpp/Destroying Asteroids.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 nandincpp/Destroying Asteroids.java diff --git a/nandincpp/Destroying Asteroids.java b/nandincpp/Destroying Asteroids.java new file mode 100644 index 0000000..8b9f06b --- /dev/null +++ b/nandincpp/Destroying Asteroids.java @@ -0,0 +1,21 @@ +class Solution { + public boolean asteroidsDestroyed(int mass, int[] a) + { + Arrays.sort(a); + long m = mass; + boolean flag = true; + + for(int i=0;i= a[i]) + m += a[i]; + else + { + flag = false; + break; + } + } + + return flag; + } +} \ No newline at end of file From f4ee096bad85d50649b9b42a25466b6c60e0e4e5 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 23 Jan 2022 01:30:16 +0530 Subject: [PATCH 070/105] Add files via upload --- .../Minimum Cost of Buying Candies With Discount.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 nandincpp/Minimum Cost of Buying Candies With Discount.cpp diff --git a/nandincpp/Minimum Cost of Buying Candies With Discount.cpp b/nandincpp/Minimum Cost of Buying Candies With Discount.cpp new file mode 100644 index 0000000..ce50744 --- /dev/null +++ b/nandincpp/Minimum Cost of Buying Candies With Discount.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int minimumCost(vector& A) { + sort(begin(A), end(A), greater<>()); + int ans = 0, N = A.size(); + for (int i = 0; i < N; ++i) { + ans += A[i++]; + if (i < N) ans += A[i++]; + } + return ans; + } +}; \ No newline at end of file From 33ea5994e1974915353d1b25dbfaeb8ba08b653d Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 23 Jan 2022 01:30:31 +0530 Subject: [PATCH 071/105] Add files via upload --- ...um Swaps to Group All 1's Together II.java | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 nandincpp/Minimum Swaps to Group All 1's Together II.java diff --git a/nandincpp/Minimum Swaps to Group All 1's Together II.java b/nandincpp/Minimum Swaps to Group All 1's Together II.java new file mode 100644 index 0000000..8bc46fa --- /dev/null +++ b/nandincpp/Minimum Swaps to Group All 1's Together II.java @@ -0,0 +1,31 @@ +class Solution { + public int minSwaps(int[] a) + { + int one = 0, n = a.length; + for(int i=0;i Date: Sun, 23 Jan 2022 01:30:46 +0530 Subject: [PATCH 072/105] Add files via upload --- ...mum Swaps to Group All 1's Together II.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 nandincpp/Minimum Swaps to Group All 1's Together II.cpp diff --git a/nandincpp/Minimum Swaps to Group All 1's Together II.cpp b/nandincpp/Minimum Swaps to Group All 1's Together II.cpp new file mode 100644 index 0000000..1350d14 --- /dev/null +++ b/nandincpp/Minimum Swaps to Group All 1's Together II.cpp @@ -0,0 +1,34 @@ +class Solution { +public: + int minSwaps(vector& a) + { + int one = 0; + for(auto i : a) + if(i == 1) + one++; + + int i, winsiz = one; + one = 0; + + for(i=0;i Date: Sun, 23 Jan 2022 01:31:02 +0530 Subject: [PATCH 073/105] Add files via upload --- .../Maximum Running Time of N Computers.cpp | 37 +++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 nandincpp/Maximum Running Time of N Computers.cpp diff --git a/nandincpp/Maximum Running Time of N Computers.cpp b/nandincpp/Maximum Running Time of N Computers.cpp new file mode 100644 index 0000000..c6a4fc7 --- /dev/null +++ b/nandincpp/Maximum Running Time of N Computers.cpp @@ -0,0 +1,37 @@ +#define ll long long + +class Solution { + bool isposi(vector&btry, ll mid, ll n) + { + ll tot_time = mid*n; + + for(ll i : btry) + tot_time -= min(mid, i); + + return tot_time<=0; + } + +public: + long long maxRunTime(int n, vector& btry) + { + ll low = 0, high = 0; + for(int i=0;i Date: Sun, 23 Jan 2022 01:31:17 +0530 Subject: [PATCH 074/105] Add files via upload --- .../Solving Questions With Brainpower.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 nandincpp/Solving Questions With Brainpower.cpp diff --git a/nandincpp/Solving Questions With Brainpower.cpp b/nandincpp/Solving Questions With Brainpower.cpp new file mode 100644 index 0000000..bb2bf8e --- /dev/null +++ b/nandincpp/Solving Questions With Brainpower.cpp @@ -0,0 +1,28 @@ +#define ll long long +ll dp[100005]; + +class Solution { + + long long helper(vector>& questions, ll i, ll n) + { + if(i >= n) + return 0; + + if(dp[i] != -1) + return dp[i]; + + ll take = helper(questions, i+1+questions[i][1], n) + questions[i][0]; + ll leave = helper(questions, i+1, n); + + return dp[i] = max(take, leave); + } + +public: + long long mostPoints(vector>& questions) + { + memset(dp, -1, sizeof(dp)); + ll n = questions.size(); + return helper(questions, 0, n); + + } +}; \ No newline at end of file From d20ec3a560020a5deb3279c0fc4f0e851c499c78 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 23 Jan 2022 01:31:34 +0530 Subject: [PATCH 075/105] Add files via upload --- .../Minimum Moves to Reach Target Score.cpp | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 nandincpp/Minimum Moves to Reach Target Score.cpp diff --git a/nandincpp/Minimum Moves to Reach Target Score.cpp b/nandincpp/Minimum Moves to Reach Target Score.cpp new file mode 100644 index 0000000..13587e3 --- /dev/null +++ b/nandincpp/Minimum Moves to Reach Target Score.cpp @@ -0,0 +1,25 @@ +class Solution { +public: + int minMoves(int target, int maxDoubles) + { + int ans = 0; + + while(target > 1) + { + if(target%2==0 && maxDoubles>0) + { + maxDoubles--; + target /= 2; + } + + else + target--; + + ans++; + if(maxDoubles == 0) + break; + } + + return ans + (target-1); + } +}; \ No newline at end of file From 4ad5f0cabca22d842e69999f7332748216af2848 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 23 Jan 2022 01:32:01 +0530 Subject: [PATCH 076/105] Add files via upload --- .../Check if All A's Appears Before All B's.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 nandincpp/Check if All A's Appears Before All B's.cpp diff --git a/nandincpp/Check if All A's Appears Before All B's.cpp b/nandincpp/Check if All A's Appears Before All B's.cpp new file mode 100644 index 0000000..a70710b --- /dev/null +++ b/nandincpp/Check if All A's Appears Before All B's.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool checkString(string s) + { + bool flag = 0; + + for(int i=0;i Date: Mon, 24 Jan 2022 07:42:27 +0530 Subject: [PATCH 077/105] Add files via upload --- .../Rearrange Array Elements by Sign.cpp | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 nandincpp/Rearrange Array Elements by Sign.cpp diff --git a/nandincpp/Rearrange Array Elements by Sign.cpp b/nandincpp/Rearrange Array Elements by Sign.cpp new file mode 100644 index 0000000..49ce643 --- /dev/null +++ b/nandincpp/Rearrange Array Elements by Sign.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + vector rearrangeArray(vector& a) + { + vector p, n; + for(auto i : a) + { + if(i > 0) + p.push_back(i); + else + n.push_back(i); + } + + vector ans; + int siz = p.size(); + + for(int i=0;i Date: Mon, 24 Jan 2022 07:42:44 +0530 Subject: [PATCH 078/105] Add files via upload --- nandincpp/Sequential Digits.cpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 nandincpp/Sequential Digits.cpp diff --git a/nandincpp/Sequential Digits.cpp b/nandincpp/Sequential Digits.cpp new file mode 100644 index 0000000..35633c7 --- /dev/null +++ b/nandincpp/Sequential Digits.cpp @@ -0,0 +1,32 @@ +class Solution { + + set ss; + void helper(int low, int high, int idx) + { + if(idx > 9) + return ; + + int ans = 0; + for(int i=idx;i<=9;i++) + { + ans = ans*10 + i; + + if(ans>=low && ans<=high) + ss.insert(ans); + } + + helper(low, high, idx+1); + } + +public: + vector sequentialDigits(int low, int high) + { + helper(low, high, 0); + vector ans; + + for(auto i : ss) + ans.push_back(i); + + return ans; + } +}; \ No newline at end of file From 425a42b66797595658a52e473233cf4137985cf9 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 24 Jan 2022 07:42:56 +0530 Subject: [PATCH 079/105] Add files via upload --- .../Find All Lonely Numbers in the Array.cpp | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 nandincpp/Find All Lonely Numbers in the Array.cpp diff --git a/nandincpp/Find All Lonely Numbers in the Array.cpp b/nandincpp/Find All Lonely Numbers in the Array.cpp new file mode 100644 index 0000000..355bde9 --- /dev/null +++ b/nandincpp/Find All Lonely Numbers in the Array.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + vector findLonely(vector& a) + { + unordered_map m; + + for(int i=0;i ans; + for(int i=0;i 1) + continue; + else if(m.count(a[i]+1) || m.count(a[i]-1)) + continue; + else + ans.push_back(a[i]); + } + + return ans; + } +}; \ No newline at end of file From dbadaaed995c184c1595e209ca6b6458ddfe5414 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 24 Jan 2022 07:43:10 +0530 Subject: [PATCH 080/105] Add files via upload --- ...ts With Strictly Smaller and Greater Elements.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) create mode 100644 nandincpp/Count Elements With Strictly Smaller and Greater Elements.cpp diff --git a/nandincpp/Count Elements With Strictly Smaller and Greater Elements.cpp b/nandincpp/Count Elements With Strictly Smaller and Greater Elements.cpp new file mode 100644 index 0000000..9ece3b5 --- /dev/null +++ b/nandincpp/Count Elements With Strictly Smaller and Greater Elements.cpp @@ -0,0 +1,12 @@ +class Solution { +public: + int countElements(vector& nums) { + int M = *max_element(nums.begin(), nums.end()); + int m = *min_element(nums.begin(), nums.end()); + int res = 0; + for(int i = 0; i < nums.size(); i++){ + if(nums[i] > m && nums[i] < M) res++; + } + return res; + } +}; \ No newline at end of file From 8c43ff6603060a3dc39c3e5504be75d0b92cd6e3 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Thu, 27 Jan 2022 23:56:57 +0530 Subject: [PATCH 081/105] Add files via upload --- nandincpp/Count the Hidden Sequences.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 nandincpp/Count the Hidden Sequences.java diff --git a/nandincpp/Count the Hidden Sequences.java b/nandincpp/Count the Hidden Sequences.java new file mode 100644 index 0000000..9ac4bfc --- /dev/null +++ b/nandincpp/Count the Hidden Sequences.java @@ -0,0 +1,16 @@ +class Solution { + public int numberOfArrays(int[] d, int lower, int upper) + { + long sum = 0, mini = 0, maxi = 0; + + for(int i : d) + { + sum += i; + + maxi = Math.max(maxi, sum); + mini = Math.min(mini, sum); + } + + return (int)Math.max(0, (upper-maxi)+(mini-lower)+1); + } +} \ No newline at end of file From 4ee896d32a98c7a65a477a28e9b6d869767b6f21 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 30 Jan 2022 18:31:30 +0530 Subject: [PATCH 082/105] Add files via upload --- .../Keep Multiplying Found Values by Two.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 nandcpp/Keep Multiplying Found Values by Two.cpp diff --git a/nandcpp/Keep Multiplying Found Values by Two.cpp b/nandcpp/Keep Multiplying Found Values by Two.cpp new file mode 100644 index 0000000..6494157 --- /dev/null +++ b/nandcpp/Keep Multiplying Found Values by Two.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int findFinalValue(vector& a, int ori) + { + unordered_map m; + for(auto i : a) + { + m[i] = 1; + } + + int ans; + while(1) + { + if(!m.count(ori)) + { + ans = ori; + break; + } + + else if(m.count(ori)) + { + ori = 2*ori; + } + } + + return ans; + } +}; \ No newline at end of file From 14d2aa4c4cbd7f1be0d4be732b87bff1aec60d75 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 30 Jan 2022 18:31:49 +0530 Subject: [PATCH 083/105] Add files via upload --- nandcpp/Count the Hidden Sequences.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 nandcpp/Count the Hidden Sequences.cpp diff --git a/nandcpp/Count the Hidden Sequences.cpp b/nandcpp/Count the Hidden Sequences.cpp new file mode 100644 index 0000000..5e8c9f5 --- /dev/null +++ b/nandcpp/Count the Hidden Sequences.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + int numberOfArrays(vector& d, int lower, int upper) + { + long maxi, mini, sum; + maxi = mini = sum = 0; + + for(auto i : d) + { + sum += i; + + maxi = max(maxi, sum); + mini = min(mini, sum); + } + + return max(0L, (upper-maxi)+(mini-lower)+1); + } +}; \ No newline at end of file From f98c33553aa847168cae3cb344e55a079d9e6786 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 30 Jan 2022 18:32:59 +0530 Subject: [PATCH 084/105] Add files via upload From c8c12a1b0681c0a8cade66907ed071dfa31d722c Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 31 Jan 2022 09:18:26 +0530 Subject: [PATCH 085/105] Add files via upload --- ...h the Highest Score of a Binary Array.java | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 nandcpp/All Divisions With the Highest Score of a Binary Array.java diff --git a/nandcpp/All Divisions With the Highest Score of a Binary Array.java b/nandcpp/All Divisions With the Highest Score of a Binary Array.java new file mode 100644 index 0000000..5443932 --- /dev/null +++ b/nandcpp/All Divisions With the Highest Score of a Binary Array.java @@ -0,0 +1,34 @@ +class Solution { + public List maxScoreIndices(int[] a) + { + int rights = 0, lefts = 0; + int maxi = 0, n = a.length; + List res = new ArrayList<>(); + + for(int i : a) + rights += i; + + for(int i=0;i<=n;i++) + { + if(lefts+rights > maxi) + { + maxi = lefts+rights; + res.clear(); + res.add(i); + } + + else if(lefts+rights == maxi) + res.add(i); + + if(i != n) + { + if(a[i] == 0) + lefts++; + if(a[i] == 1) + rights--; + } + } + + return res; + } +} \ No newline at end of file From b52db4ca20213382991443b27b73e4335c7d0a61 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 31 Jan 2022 09:18:43 +0530 Subject: [PATCH 086/105] Add files via upload --- ...th the Highest Score of a Binary Array.cpp | 35 +++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 nandcpp/All Divisions With the Highest Score of a Binary Array.cpp diff --git a/nandcpp/All Divisions With the Highest Score of a Binary Array.cpp b/nandcpp/All Divisions With the Highest Score of a Binary Array.cpp new file mode 100644 index 0000000..5122653 --- /dev/null +++ b/nandcpp/All Divisions With the Highest Score of a Binary Array.cpp @@ -0,0 +1,35 @@ +class Solution { +public: + vector maxScoreIndices(vector& a) + { + int rights = 0, lefts = 0; + int maxi = 0; + vector res; + + for(auto i : a) + rights += i; + + for(int i=0;i<=a.size();i++) + { + if(lefts+rights > maxi) + { + res.clear(); + res.push_back(i); + maxi = lefts+rights; + } + + else if(lefts+rights == maxi) + res.push_back(i); + + if(i != a.size()) + { + if(a[i] == 0) + lefts++; + if(a[i] == 1) + rights--; + } + } + + return res; + } +}; \ No newline at end of file From 75e54e59d442074ea1e8b43273342b5173ea4db3 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 21 Feb 2022 03:12:07 +0530 Subject: [PATCH 087/105] Add files via upload --- ...Consecutive Integers That Sum to a Given Number.cpp | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 nandcpp/Find Three Consecutive Integers That Sum to a Given Number.cpp diff --git a/nandcpp/Find Three Consecutive Integers That Sum to a Given Number.cpp b/nandcpp/Find Three Consecutive Integers That Sum to a Given Number.cpp new file mode 100644 index 0000000..fd2d097 --- /dev/null +++ b/nandcpp/Find Three Consecutive Integers That Sum to a Given Number.cpp @@ -0,0 +1,10 @@ +class Solution { +public: + vector sumOfThree(long long num) + { + if(num%3 != 0) + return {}; + long long x = num/3; + return {x-1, x, x+1}; + } +}; \ No newline at end of file From 53d68efb066e0187d8a4b4f9fec2e97c237aa013 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 21 Feb 2022 03:19:57 +0530 Subject: [PATCH 088/105] Add files via upload --- .../Construct String With Repeat Limit.cpp | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 nandcpp/Construct String With Repeat Limit.cpp diff --git a/nandcpp/Construct String With Repeat Limit.cpp b/nandcpp/Construct String With Repeat Limit.cpp new file mode 100644 index 0000000..95de742 --- /dev/null +++ b/nandcpp/Construct String With Repeat Limit.cpp @@ -0,0 +1,45 @@ +class Solution { +public: + string repeatLimitedString(string s, int rl) + { + unordered_map m; + for(auto i : s) + { + m[i]++; + } + + priority_queue> pq; + for(auto i : m) + pq.push({i.first, i.second}); + + string ans = ""; + + while(!pq.empty()) + { + auto p = pq.top(); + pq.pop(); + + // p.second = freq; + // p.first = val; |char| + + int mini = min(rl, p.second); + for(int x=0;x mini) + { + if(pq.empty()) + return ans; + auto p1 = pq.top(); + pq.pop(); + ans += p1.first; + + if(p1.second-1 > 0) + pq.push({p1.first, p1.second-1}); + pq.push({p.first, p.second-mini}); + } + } + + return ans; + } +}; \ No newline at end of file From 545729ecfea4c7df1c7c61fd4b57268eb1c6b5eb Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Tue, 22 Feb 2022 09:49:52 +0530 Subject: [PATCH 089/105] Add files via upload From ae549b624c4342bcdce5b38e297abc36f1a92569 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Tue, 22 Feb 2022 09:50:41 +0530 Subject: [PATCH 090/105] Add files via upload --- ...erations to Make the Array Alternating.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 nandcpp/Minimum Operations to Make the Array Alternating.cpp diff --git a/nandcpp/Minimum Operations to Make the Array Alternating.cpp b/nandcpp/Minimum Operations to Make the Array Alternating.cpp new file mode 100644 index 0000000..ba322da --- /dev/null +++ b/nandcpp/Minimum Operations to Make the Array Alternating.cpp @@ -0,0 +1,24 @@ +class Solution { +public: + int minimumOperations(vector& a) + { + int freq[100005][2]; + memset(freq, 0, sizeof(freq)); + + for(int i=0;i Date: Wed, 23 Feb 2022 02:20:39 +0530 Subject: [PATCH 091/105] Add files via upload From d98447d9967032629fe3464b1d27273185aea433 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 23 Feb 2022 02:21:47 +0530 Subject: [PATCH 092/105] Add files via upload --- ...Removing Minimum Number of Magic Beans.cpp | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 nandcpp/Removing Minimum Number of Magic Beans.cpp diff --git a/nandcpp/Removing Minimum Number of Magic Beans.cpp b/nandcpp/Removing Minimum Number of Magic Beans.cpp new file mode 100644 index 0000000..2c104e7 --- /dev/null +++ b/nandcpp/Removing Minimum Number of Magic Beans.cpp @@ -0,0 +1,19 @@ +#define ll long long +class Solution { +public: + long long minimumRemoval(vector& beans) + { + ll ts = 0; + for(int i=0;i Date: Wed, 23 Feb 2022 02:22:47 +0530 Subject: [PATCH 093/105] Add files via upload --- nandcpp/Count Operations to Obtain Zero.cpp | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 nandcpp/Count Operations to Obtain Zero.cpp diff --git a/nandcpp/Count Operations to Obtain Zero.cpp b/nandcpp/Count Operations to Obtain Zero.cpp new file mode 100644 index 0000000..ed80c33 --- /dev/null +++ b/nandcpp/Count Operations to Obtain Zero.cpp @@ -0,0 +1,20 @@ +class Solution { +public: + int countOperations(int num1, int num2) + { + int cnt = 0; + while(1) + { + if(num1==0 || num2==0) + break; + if(num1 >= num2) + num1 -= num2; + else + num2 -= num1; + + cnt++; + } + + return cnt; + } +}; \ No newline at end of file From 5a24db9415fa5d16bb8115a973477d94d623d694 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 23 Feb 2022 02:24:01 +0530 Subject: [PATCH 094/105] Add files via upload --- ...rtition Array According to Given Pivot.cpp | 29 +++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 nandcpp/Partition Array According to Given Pivot.cpp diff --git a/nandcpp/Partition Array According to Given Pivot.cpp b/nandcpp/Partition Array According to Given Pivot.cpp new file mode 100644 index 0000000..eb99699 --- /dev/null +++ b/nandcpp/Partition Array According to Given Pivot.cpp @@ -0,0 +1,29 @@ +class Solution { +public: + vector pivotArray(vector& a, int pivot) + { + vector s, l; + int cnt = 0; + + for(auto i : a) + { + if(i == pivot) + { + cnt++; + continue; + } + else if(i > pivot) + l.push_back(i); + else if(i < pivot) + s.push_back(i); + } + + for(int i=0;i Date: Wed, 23 Feb 2022 02:24:45 +0530 Subject: [PATCH 095/105] Add files via upload --- ...um of Four Digit Number After Splitting Digits.cpp | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 nandcpp/Minimum Sum of Four Digit Number After Splitting Digits.cpp diff --git a/nandcpp/Minimum Sum of Four Digit Number After Splitting Digits.cpp b/nandcpp/Minimum Sum of Four Digit Number After Splitting Digits.cpp new file mode 100644 index 0000000..1d63a89 --- /dev/null +++ b/nandcpp/Minimum Sum of Four Digit Number After Splitting Digits.cpp @@ -0,0 +1,11 @@ +class Solution { +public: + int minimumSum(int n) + { + string s = to_string(n); + sort(s.begin(), s.end()); + + int ans = (s[0]-'0' + s[1]-'0')*10 + (s[2]-'0' + s[3]-'0'); + return ans; + } +}; \ No newline at end of file From d970895f8358b895ef6982affb9ae7731ae9f230 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 23 Feb 2022 02:26:32 +0530 Subject: [PATCH 096/105] Add files via upload From c107f1bccd2e70cac9f1d3c4b412f36dea47e043 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 23 Feb 2022 02:27:21 +0530 Subject: [PATCH 097/105] Add files via upload --- nandcpp/Merge Nodes in Between Zeros.cpp | 39 ++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 nandcpp/Merge Nodes in Between Zeros.cpp diff --git a/nandcpp/Merge Nodes in Between Zeros.cpp b/nandcpp/Merge Nodes in Between Zeros.cpp new file mode 100644 index 0000000..22eedc5 --- /dev/null +++ b/nandcpp/Merge Nodes in Between Zeros.cpp @@ -0,0 +1,39 @@ +/** + * Definition for singly-linked list. + * struct ListNode { + * int val; + * ListNode *next; + * ListNode() : val(0), next(nullptr) {} + * ListNode(int x) : val(x), next(nullptr) {} + * ListNode(int x, ListNode *next) : val(x), next(next) {} + * }; + */ +class Solution { +public: + ListNode* mergeNodes(ListNode* head) + { + ListNode* temp = head; + temp = temp->next; + ListNode* head2 = new ListNode(-1); + ListNode* head3 = head2; + int cnt = 0; + + while(temp != NULL) + { + if(temp->val == 0) + { + head2->next = new ListNode(cnt); + head2 = head2->next; + cnt = 0; + } + else + { + cnt += temp->val; + } + + temp = temp->next; + } + + return head3->next; + } +}; \ No newline at end of file From 9fbc27d964583ecec3300f1837ad90781e15b562 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 23 Feb 2022 02:27:57 +0530 Subject: [PATCH 098/105] Add files via upload From eb93efbc3b524128c8534e14ea3e4278c20007a1 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Wed, 23 Feb 2022 02:28:45 +0530 Subject: [PATCH 099/105] Add files via upload --- ...aximum Split of Positive Even Integers.cpp | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 nandcpp/Maximum Split of Positive Even Integers.cpp diff --git a/nandcpp/Maximum Split of Positive Even Integers.cpp b/nandcpp/Maximum Split of Positive Even Integers.cpp new file mode 100644 index 0000000..c4cbb44 --- /dev/null +++ b/nandcpp/Maximum Split of Positive Even Integers.cpp @@ -0,0 +1,22 @@ +class Solution { +public: + vector maximumEvenSplit(long long f) + { + if(f & 1) + return {}; + + int i = 2; + vector ans; + while(i <= f) + { + ans.push_back(i); + f -= i; + i += 2; + } + + int back = ans.back(); + ans.pop_back(); + ans.push_back(f+back); + return ans; + } +}; \ No newline at end of file From ab6fafa8c72f88557572a2440a652069c79291cc Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 21 Mar 2022 09:39:28 +0530 Subject: [PATCH 100/105] Add files via upload --- nandcpp/Counting Words With a Given Prefix.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 nandcpp/Counting Words With a Given Prefix.cpp diff --git a/nandcpp/Counting Words With a Given Prefix.cpp b/nandcpp/Counting Words With a Given Prefix.cpp new file mode 100644 index 0000000..4f49c78 --- /dev/null +++ b/nandcpp/Counting Words With a Given Prefix.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int prefixCount(vector& words, string pref) + { + int cnt = 0, size = pref.size(); + for(int i=0;i Date: Mon, 21 Mar 2022 09:40:06 +0530 Subject: [PATCH 101/105] Add files via upload --- ...mize Number of Subsequences in a String.cpp | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 nandcpp/Maximize Number of Subsequences in a String.cpp diff --git a/nandcpp/Maximize Number of Subsequences in a String.cpp b/nandcpp/Maximize Number of Subsequences in a String.cpp new file mode 100644 index 0000000..3a77230 --- /dev/null +++ b/nandcpp/Maximize Number of Subsequences in a String.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + long long maximumSubsequenceCount(string t, string p) + { + long long cnt0 = 1, cnt1 = 1, res0 = 0, res1 = 0; + for(int i=0,j=t.size()-1;i=0;i++,j--) + { + if(t[i] == p[1]) + res0 += cnt0; + if(t[j] == p[0]) + res1 += cnt1; + cnt0 += (t[i]==p[0]); + cnt1 += (t[j]==p[1]); + } + + return max(res0, res1); + } +}; \ No newline at end of file From 651b304506b9c2cb109a529682fdb539ee8d0f90 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 21 Mar 2022 09:40:57 +0530 Subject: [PATCH 102/105] Add files via upload --- ...f Steps to Make Two Strings Anagram II.cpp | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 nandcpp/Minimum Number of Steps to Make Two Strings Anagram II.cpp diff --git a/nandcpp/Minimum Number of Steps to Make Two Strings Anagram II.cpp b/nandcpp/Minimum Number of Steps to Make Two Strings Anagram II.cpp new file mode 100644 index 0000000..c42fec4 --- /dev/null +++ b/nandcpp/Minimum Number of Steps to Make Two Strings Anagram II.cpp @@ -0,0 +1,21 @@ +class Solution { +public: + int minSteps(string s, string t) + { + unordered_map sm, tm; + for(auto i : s) + sm[i]++; + for(auto i : t) + { + sm[i]--; + } + + int step = 0; + for(auto i : sm) + { + step += abs(i.second); + } + + return step; + } +}; \ No newline at end of file From 0625a5f1142e072b12b08e4c78491451772bdca3 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Mon, 21 Mar 2022 09:41:42 +0530 Subject: [PATCH 103/105] Add files via upload --- .../Append K Integers With Minimal Sum.cpp | 26 +++++++++++++++++ nandcpp/Divide Array Into Equal Pairs.cpp | 17 +++++++++++ nandcpp/Minimum Time to Complete Trips.cpp | 29 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 nandcpp/Append K Integers With Minimal Sum.cpp create mode 100644 nandcpp/Divide Array Into Equal Pairs.cpp create mode 100644 nandcpp/Minimum Time to Complete Trips.cpp diff --git a/nandcpp/Append K Integers With Minimal Sum.cpp b/nandcpp/Append K Integers With Minimal Sum.cpp new file mode 100644 index 0000000..05ad3d9 --- /dev/null +++ b/nandcpp/Append K Integers With Minimal Sum.cpp @@ -0,0 +1,26 @@ +class Solution { +public: + long long minimalKSum(vector& a, int k) + { + int n = a.size(); + long long ans = 0; + sort(a.begin(), a.end()); + + for(int i=0;i0;i++) + { + long long prev = (i==0) ? 0 : a[i-1]; + long long cnt = min((long long)k, max((long long)0, (long long)(a[i]-prev-1))); + + k -= cnt; + + ans += (long long)cnt*(prev+1+prev+cnt)/2; + } + + if(k) + { + ans += (long long)k * (a.back()+1+a.back()+k)/2; + } + + return ans; + } +}; \ No newline at end of file diff --git a/nandcpp/Divide Array Into Equal Pairs.cpp b/nandcpp/Divide Array Into Equal Pairs.cpp new file mode 100644 index 0000000..9a4878c --- /dev/null +++ b/nandcpp/Divide Array Into Equal Pairs.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + bool divideArray(vector& a) + { + unordered_map m; + for(auto i : a) + m[i]++; + + for(auto i : m) + { + if(i.second%2 == 1) + return 0; + } + + return 1; + } +}; \ No newline at end of file diff --git a/nandcpp/Minimum Time to Complete Trips.cpp b/nandcpp/Minimum Time to Complete Trips.cpp new file mode 100644 index 0000000..641859f --- /dev/null +++ b/nandcpp/Minimum Time to Complete Trips.cpp @@ -0,0 +1,29 @@ +class Solution { + long long trip(vector& time, long long mid) + { + long long treep = 0; + for(auto i : time) + treep += mid/i; + + return treep; + } + +public: + long long minimumTime(vector& time, int totalTrips) + { + long long low = 1, high = 1e14+1; + long long ans; + while(low < high) + { + long long mid = low + (high-low)/2; + if(trip(time, mid) < totalTrips) + low = mid + 1; + else + { + high = mid; + } + } + + return low; + } +}; \ No newline at end of file From aa3110514d1751080124c8e11861a1191a675a3a Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sun, 10 Apr 2022 20:50:16 +0530 Subject: [PATCH 104/105] Add files via upload --- nandcpp/Count Collisions on a Road.cpp | 31 +++++++++++++++++ nandcpp/Find Triangular Sum of an Array.cpp | 17 ++++++++++ nandcpp/Find the Difference of Two Arrays.cpp | 30 +++++++++++++++++ .../Minimum Bit Flips to Convert Number.cpp | 7 ++++ ...m Number of Operations to Convert Time.cpp | 23 +++++++++++++ .../Number of Ways to Select Buildings.cpp | 33 +++++++++++++++++++ 6 files changed, 141 insertions(+) create mode 100644 nandcpp/Count Collisions on a Road.cpp create mode 100644 nandcpp/Find Triangular Sum of an Array.cpp create mode 100644 nandcpp/Find the Difference of Two Arrays.cpp create mode 100644 nandcpp/Minimum Bit Flips to Convert Number.cpp create mode 100644 nandcpp/Minimum Number of Operations to Convert Time.cpp create mode 100644 nandcpp/Number of Ways to Select Buildings.cpp diff --git a/nandcpp/Count Collisions on a Road.cpp b/nandcpp/Count Collisions on a Road.cpp new file mode 100644 index 0000000..9aa00a8 --- /dev/null +++ b/nandcpp/Count Collisions on a Road.cpp @@ -0,0 +1,31 @@ +class Solution { +public: + int countCollisions(string directions) + { + int i = 0; + int n = directions.size(); + int j = n-1; + + while(i=0 && directions[j]=='R') + j--; + + int ans = 0, r = 0; + + while(i<=j) + { + if(directions[i] == 'R') + r++; + else + { + ans += (directions[i]=='S') ? r : r+1; + r = 0; + } + + i++; + } + + return ans; + } +}; \ No newline at end of file diff --git a/nandcpp/Find Triangular Sum of an Array.cpp b/nandcpp/Find Triangular Sum of an Array.cpp new file mode 100644 index 0000000..3ba820d --- /dev/null +++ b/nandcpp/Find Triangular Sum of an Array.cpp @@ -0,0 +1,17 @@ +class Solution { +public: + int triangularSum(vector& a) + { + int n = a.size(); + while(n > 1) + { + for(int i=0;i> findDifference(vector& nums1, vector& nums2) + { + set one, two; + set n2; + for(auto i : nums2) + n2.insert(i); + + for(auto i : nums1) + if(!n2.count(i)) + one.insert(i); + + set n1; + for(auto i : nums1) + n1.insert(i); + + for(auto i : nums2) + if(!n1.count(i)) + two.insert(i); + + vector oo, tt; + for(auto i : one) + oo.push_back(i); + for(auto i : two) + tt.push_back(i); + + return {oo, tt}; + } +}; \ No newline at end of file diff --git a/nandcpp/Minimum Bit Flips to Convert Number.cpp b/nandcpp/Minimum Bit Flips to Convert Number.cpp new file mode 100644 index 0000000..09a7684 --- /dev/null +++ b/nandcpp/Minimum Bit Flips to Convert Number.cpp @@ -0,0 +1,7 @@ +class Solution { +public: + int minBitFlips(int start, int goal) + { + return __builtin_popcount(start^goal); + } +}; \ No newline at end of file diff --git a/nandcpp/Minimum Number of Operations to Convert Time.cpp b/nandcpp/Minimum Number of Operations to Convert Time.cpp new file mode 100644 index 0000000..9b69cea --- /dev/null +++ b/nandcpp/Minimum Number of Operations to Convert Time.cpp @@ -0,0 +1,23 @@ +class Solution { +public: + int convertTime(string current, string correct) + { + int hr_l = (current[0]-'0')*10 + (current[1]-'0'); + int min_l = (current[3]-'0')*10 + (current[4]-'0'); + int hr_r = (correct[0]-'0')*10 + (correct[1]-'0'); + int min_r = (correct[3]-'0')*10 + (correct[4]-'0'); + + int time_l = hr_l*60 + min_l; + int time_r = hr_r*60 + min_r; + int diff = time_r - time_l; + + int ans = 0, a[4] = {60, 15, 5, 1}; + for(int i : a) + { + ans += diff / i; + diff = diff % i; + } + + return ans; + } +}; \ No newline at end of file diff --git a/nandcpp/Number of Ways to Select Buildings.cpp b/nandcpp/Number of Ways to Select Buildings.cpp new file mode 100644 index 0000000..24ddf1d --- /dev/null +++ b/nandcpp/Number of Ways to Select Buildings.cpp @@ -0,0 +1,33 @@ +class Solution { +public: + long long numberOfWays(string s) + { + long long of = 0, zf = 0; + for(int i=0;i=0;i--) + { + if(s[i] == '0') + zf--, zb++; + else if(s[i] == '1') + of--, ob++; + + if(i == s.size()-1) + continue; + + if(s[i] == '0') + ans += (of*ob); + else + ans += (zf*zb); + } + + return ans; + } +}; \ No newline at end of file From 10db96d8e481deb8df1f723127696bc82033d0d6 Mon Sep 17 00:00:00 2001 From: noob-hu-yaar <48630662+noob-hu-yaar@users.noreply.github.com> Date: Sat, 1 Oct 2022 07:29:59 +0530 Subject: [PATCH 105/105] Add files via upload --- Longest Subarray With Maximum Bitwise AND.cpp | 23 +++++++++++++++++++ ...est Subarray With Maximum Bitwise AND.java | 23 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 Longest Subarray With Maximum Bitwise AND.cpp create mode 100644 Longest Subarray With Maximum Bitwise AND.java diff --git a/Longest Subarray With Maximum Bitwise AND.cpp b/Longest Subarray With Maximum Bitwise AND.cpp new file mode 100644 index 0000000..c271586 --- /dev/null +++ b/Longest Subarray With Maximum Bitwise AND.cpp @@ -0,0 +1,23 @@ +class Solution { + public int longestSubarray(int[] a) + { + int maxi = 0; + for(int i : a) + maxi = Math.max(i, maxi); + + int l = 0, ans = 0; + for(int i : a) + { + if(i == maxi) + l++; + else + { + ans = Math.max(ans, l); + l = 0; + } + } + + ans = Math.max(ans, l); + return ans; + } +} \ No newline at end of file diff --git a/Longest Subarray With Maximum Bitwise AND.java b/Longest Subarray With Maximum Bitwise AND.java new file mode 100644 index 0000000..c271586 --- /dev/null +++ b/Longest Subarray With Maximum Bitwise AND.java @@ -0,0 +1,23 @@ +class Solution { + public int longestSubarray(int[] a) + { + int maxi = 0; + for(int i : a) + maxi = Math.max(i, maxi); + + int l = 0, ans = 0; + for(int i : a) + { + if(i == maxi) + l++; + else + { + ans = Math.max(ans, l); + l = 0; + } + } + + ans = Math.max(ans, l); + return ans; + } +} \ No newline at end of file