diff --git a/AGGCOWS.cpp b/AGGCOWS.cpp new file mode 100644 index 00000000..48413abf --- /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; + + +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; +#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; +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& 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; + } +}; diff --git a/GERGOVIA_Wine_trading_in_Gergovia_SPOJ.cpp b/GERGOVIA_Wine_trading_in_Gergovia_SPOJ.cpp new file mode 100644 index 00000000..93f79160 --- /dev/null +++ b/GERGOVIA_Wine_trading_in_Gergovia_SPOJ.cpp @@ -0,0 +1,74 @@ +#include +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; +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; +#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 ; +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< +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< +#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< 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 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 00000000..54439320 --- /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 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 00000000..05ad3d98 --- /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/Construct String With Repeat Limit.cpp b/nandcpp/Construct String With Repeat Limit.cpp new file mode 100644 index 00000000..95de742c --- /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 diff --git a/nandcpp/Count Collisions on a Road.cpp b/nandcpp/Count Collisions on a Road.cpp new file mode 100644 index 00000000..9aa00a89 --- /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/Count Operations to Obtain Zero.cpp b/nandcpp/Count Operations to Obtain Zero.cpp new file mode 100644 index 00000000..ed80c332 --- /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 diff --git a/nandcpp/Count the Hidden Sequences.cpp b/nandcpp/Count the Hidden Sequences.cpp new file mode 100644 index 00000000..5e8c9f5d --- /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 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 00000000..4f49c789 --- /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& 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/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 00000000..fd2d0977 --- /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 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 00000000..3ba820d3 --- /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/Keep Multiplying Found Values by Two.cpp b/nandcpp/Keep Multiplying Found Values by Two.cpp new file mode 100644 index 00000000..64941570 --- /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 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 00000000..3a772301 --- /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 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 00000000..c4cbb447 --- /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 diff --git a/nandcpp/Merge Nodes in Between Zeros.cpp b/nandcpp/Merge Nodes in Between Zeros.cpp new file mode 100644 index 00000000..22eedc59 --- /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 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 00000000..09a76843 --- /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 00000000..9b69cea1 --- /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/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 00000000..c42fec4a --- /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 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 00000000..ba322da6 --- /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& 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 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 00000000..24ddf1d8 --- /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 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 00000000..eb996993 --- /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& beans) + { + ll ts = 0; + for(int i=0;i& spaces) + { + int i = 0, j = 0; + string ans = ""; + + while(i 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 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 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 00000000..a70710b0 --- /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>& 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 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 00000000..a4a67538 --- /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 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 00000000..f7901e08 --- /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;i0 ? 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/Complement of Base 10 Integer.cpp b/nandincpp/Complement of Base 10 Integer.cpp new file mode 100644 index 00000000..26a5f16f --- /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 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 00000000..827533a4 --- /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& 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 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 00000000..9ece3b5a --- /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 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 00000000..d1d6ed0a --- /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 diff --git a/nandincpp/Count the Hidden Sequences.java b/nandincpp/Count the Hidden Sequences.java new file mode 100644 index 00000000..9ac4bfc4 --- /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 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 00000000..ad51e12e --- /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 diff --git a/nandincpp/Destroying Asteroids.cpp b/nandincpp/Destroying Asteroids.cpp new file mode 100644 index 00000000..ba585cae --- /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 diff --git a/nandincpp/Destroying Asteroids.java b/nandincpp/Destroying Asteroids.java new file mode 100644 index 00000000..8b9f06bb --- /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 diff --git a/nandincpp/Detect Squares.cpp b/nandincpp/Detect Squares.cpp new file mode 100644 index 00000000..d831b7f3 --- /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 diff --git a/nandincpp/Detonate the Maximum Bombs.cpp b/nandincpp/Detonate the Maximum Bombs.cpp new file mode 100644 index 00000000..638277b2 --- /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;ileft, 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/Final Value of Variable After Performing Operations.cpp b/nandincpp/Final Value of Variable After Performing Operations.cpp new file mode 100644 index 00000000..1bfac3a5 --- /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 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 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 00000000..ce1d6f37 --- /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 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 diff --git a/nandincpp/Find Missing Observations.cpp b/nandincpp/Find Missing Observations.cpp new file mode 100644 index 00000000..bd2c9537 --- /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 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 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 diff --git a/nandincpp/Four Divisors.cpp b/nandincpp/Four Divisors.cpp new file mode 100644 index 00000000..3e4989b1 --- /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 diff --git a/nandincpp/Greatest Common Divisor of Strings.cpp b/nandincpp/Greatest Common Divisor of Strings.cpp new file mode 100644 index 00000000..c500f148 --- /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 diff --git a/nandincpp/Grid Game.cpp b/nandincpp/Grid Game.cpp new file mode 100644 index 00000000..63d620b6 --- /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 root->val) + root->right = insertIntoBST(root->right, val); + else + root->left = insertIntoBST(root->left, val); + + return root; + } +}; \ No newline at end of file diff --git a/nandincpp/Intervals Between Identical Elements.cpp b/nandincpp/Intervals Between Identical Elements.cpp new file mode 100644 index 00000000..61c7b0d1 --- /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& a, int k) + { + unordered_map mp; + + for(int i=0;i& 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 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 00000000..5dceebd9 --- /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 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 00000000..f4a9ef9d --- /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 diff --git a/nandincpp/Maximum Ascending Subarray Sum.cpp b/nandincpp/Maximum Ascending Subarray Sum.cpp new file mode 100644 index 00000000..35d9a39f --- /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& 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 diff --git a/nandincpp/Maximum Earnings From Taxi.cpp b/nandincpp/Maximum Earnings From Taxi.cpp new file mode 100644 index 00000000..7b9600fd --- /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 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 00000000..6cdf8e5b --- /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 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 00000000..c6a4fc70 --- /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& 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;inext!=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 diff --git a/nandincpp/Minimum Absolute Difference in BST.cpp b/nandincpp/Minimum Absolute Difference in BST.cpp new file mode 100644 index 00000000..d52c0a07 --- /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& 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 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 00000000..b434e950 --- /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 diff --git a/nandincpp/Minimum Height Trees.cpp b/nandincpp/Minimum Height Trees.cpp new file mode 100644 index 00000000..0599c8ae --- /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 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 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 00000000..62cd5260 --- /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 diff --git a/nandincpp/Minimum Operations to Convert Number.cpp b/nandincpp/Minimum Operations to Convert Number.cpp new file mode 100644 index 00000000..3709d8fa --- /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>& grid, int x) + { + int n = grid.size(); + int m = grid[0].size(); + vector a; + + for(int i=0;i& a) + { + int one = 0; + for(auto i : a) + if(i == 1) + one++; + + int i, winsiz = one; + one = 0; + + for(i=0;i& a, string target) + { + int n = a.size(); + for(int i=0;i> m; +public: + RangeFreqQuery(vector& a) + { + for(int i=0;iquery(left,right,value); + */ \ No newline at end of file diff --git a/nandincpp/Rearrange Array Elements by Sign.cpp b/nandincpp/Rearrange Array Elements by Sign.cpp new file mode 100644 index 00000000..49ce6431 --- /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 v(2, 0); + for(int i=1;iv[1]; + } +}; \ No newline at end of file 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 00000000..79102623 --- /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 diff --git a/nandincpp/Repeated String Match.cpp b/nandincpp/Repeated String Match.cpp new file mode 100644 index 00000000..78053a6b --- /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 diff --git a/nandincpp/Repeated Substring Pattern.cpp b/nandincpp/Repeated Substring Pattern.cpp new file mode 100644 index 00000000..7310c6e4 --- /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 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 00000000..05cc8c3b --- /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 diff --git a/nandincpp/Sequential Digits.cpp b/nandincpp/Sequential Digits.cpp new file mode 100644 index 00000000..35633c77 --- /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 diff --git a/nandincpp/Simple Bank System.cpp b/nandincpp/Simple Bank System.cpp new file mode 100644 index 00000000..4dde011e --- /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 diff --git a/nandincpp/Smallest Index With Equal Value.cpp b/nandincpp/Smallest Index With Equal Value.cpp new file mode 100644 index 00000000..2c8ca613 --- /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>& 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 diff --git a/nandincpp/Stock Price Fluctuation.cpp b/nandincpp/Stock Price Fluctuation.cpp new file mode 100644 index 00000000..6dbb1a5d --- /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 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 00000000..f28f1978 --- /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]& a) + { + long long res = 0; + for(int i=0;i& 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 diff --git a/nandincpp/Two Out of Three.cpp b/nandincpp/Two Out of Three.cpp new file mode 100644 index 00000000..b0b9116d --- /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 diff --git a/nandincpp/Vowels of All Substrings.cpp b/nandincpp/Vowels of All Substrings.cpp new file mode 100644 index 00000000..5db6ff6c --- /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& 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 diff --git a/nandincpp/Watering Plants.cpp b/nandincpp/Watering Plants.cpp new file mode 100644 index 00000000..803df064 --- /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 +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 +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< +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; + +// 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; +} diff --git a/unitGcd.cpp b/unitGcd.cpp new file mode 100644 index 00000000..95356ff8 --- /dev/null +++ b/unitGcd.cpp @@ -0,0 +1,5 @@ +#include +using namespace std ; +int main(){ + +}