├── README.md ├── Section1-Constructive ├── 01.cpp ├── 02.cpp ├── 03.cpp ├── 04.cpp ├── 05.cpp ├── 06.cpp ├── 07.cpp ├── 08.cpp ├── 09.cpp ├── 10.cpp ├── 11.cpp ├── 12.cpp ├── 13.cpp ├── 14.cpp ├── 15.cpp ├── 16.cpp ├── 17.cpp ├── 18.cpp ├── 19.cpp ├── 20.cpp ├── 21.cpp ├── 22.cpp ├── 23.cpp ├── 24.cpp ├── 25.cpp ├── 26.cpp ├── 27.cpp ├── 28.cpp ├── 29.cpp ├── 30.cpp ├── 31.cpp ├── 32.cpp ├── 33.cpp ├── 34.cpp ├── 35.cpp ├── 36.cpp ├── 37.cpp ├── 38.cpp ├── 39.cpp ├── 40.cpp ├── 41.cpp ├── 42.cpp ├── 43.cpp ├── 44.cpp ├── 45.cpp ├── 46.cpp ├── 47.cpp ├── 48.cpp ├── 49.cpp └── 50.cpp ├── Section2-Maths ├── 01.cpp ├── 02.cpp ├── 03.cpp ├── 04.cpp ├── 05.cpp ├── 06.cpp ├── 07.cpp ├── 08.cpp ├── 09.cpp ├── 10.cpp ├── 11.cpp ├── 12.cpp ├── 13.cpp ├── 14.cpp ├── 15.cpp ├── 16.cpp ├── 17.cpp ├── 18.cpp ├── 19.cpp ├── 20.cpp ├── 21.cpp ├── 22.cpp ├── 23.cpp ├── 24.cpp ├── 25.cpp ├── 26.cpp ├── 27.cpp ├── 28.cpp ├── 29.cpp ├── 30.cpp ├── 31.cpp ├── 32.cpp ├── 33.cpp ├── 34.cpp ├── 35.cpp ├── 36.cpp ├── 37.cpp ├── 38.cpp ├── 39.cpp ├── 40.cpp ├── 41.cpp ├── 42.cpp ├── 43.cpp ├── 44.cpp ├── 45.cpp ├── 46.cpp ├── 47.cpp ├── 48.cpp ├── 49.cpp └── 50.cpp └── Section3-BinarySearch ├── 01.cpp ├── 02.cpp ├── 03.cpp └── 04.cpp /README.md: -------------------------------------------------------------------------------- 1 | # Striver_CP_Sheet 2 | Solutions of Striver CP Sheet 3 | 4 | Hey Guys , I am Aakash Rana , NSIT 2021 Batch 5 | My Channel Name: Algorithm Decoded 6 | 7 | Explanation of every Question and Code is available on my channel 8 | https://www.youtube.com/channel/UCC7IBho-COTrj5AaAWmqynw 9 | 10 | 11 | I started this Sheet to maintain my consistency and help other people who find difficult to understand or build Logic for Competitive Questions 12 | If you find this Repo useful , You can star it :) 13 | -------------------------------------------------------------------------------- /Section1-Constructive/01.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n; 10 | cin>>n; 11 | int X=0; 12 | string s; 13 | while(n--){ 14 | cin>>s; 15 | // "++X , --X , X++ , X--" 16 | if(s=="++X"||s=="X++") X++; 17 | else X--; 18 | } 19 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | // invert num to min + integer 9 | // 9234 10 | string n; 11 | cin>>n; 12 | for(int i=0;i='5' and c<'9') n[i]='9'-c+'0'; 15 | else if(i>0 and c>='5' ) n[i]='9'-c+'0'; 16 | } 17 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int arr[5][5]; 9 | int r,c; 10 | for(int i=0;i<5;i++){ 11 | for(int j=0;j<5;j++){ 12 | cin>>arr[i][j]; 13 | if(arr[i][j]==1) r=i,c=j; 14 | } 15 | } 16 | int res=abs(2-r)+abs(2-c); 17 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* n friends 9 | k bottles 10 | cap- l ml 11 | total_limes=c*d -> d hisse , c lemons 12 | p salt 13 | 14 | 1 2 3 4 5 6 7 .....n 15 | drinks nl nl nl nl nl .... ( 1 toast) n* (nl) 16 | lime 1 1 1 1 1 .... (cd) 17 | salt np np np np np ... 18 | 19 | drinks -> n *(nl) * x= k*l 20 | lime -> n * x =cd 21 | salt -> n * (np) * x= p 22 | 23 | */ 24 | int n, k, l, c, d, p, nl, np; 25 | cin>>n>>k>>l>>c>>d>>p>>nl>>np; 26 | int tl=c*d; 27 | int td=k*l; 28 | int x=td/(n*nl); 29 | int y=tl/n; 30 | int z=p/(n*np); 31 | int res=min(x,min(y,z)); 32 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | // 7 | // x1 x2 x3 8 | int arr[3]; 9 | cin>>arr[0]>>arr[1]>>arr[2]; 10 | sort(arr,arr+3); 11 | int res=(arr[1]-arr[0])+(arr[2]-arr[1]); 12 | cout< 3 | using namespace std; 4 | 5 | int main(){ 6 | int t; 7 | cin>>t; 8 | while(t--){ 9 | vectorres; 10 | string s; 11 | cin>>s; 12 | int len=size(s); 13 | for(int i=0;i 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | //n * m matrix 8 | int n,m; 9 | cin>>n>>m; 10 | string s1(m,'#'); 11 | string s2(m,'.'); 12 | int dir=0; 13 | for(int r=0;r 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | int res=0; 10 | string s; 11 | for(int i=0;i>s; 13 | if(s=="Tetrahedron") res+=4; 14 | else if(s=="Cube") res+=6; 15 | else if(s=="Octahedron") res+=8; 16 | else if(s=="Dodecahedron") res+=12; 17 | else if (s=="Icosahedron") res+=20; 18 | } 19 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | int lh=INT_MIN, li=0; 10 | int rh=INT_MAX , ri=0; 11 | int h; 12 | for(int i=0;i>h; 14 | if(h>lh) lh=h, li=i; 15 | if(h<=rh) rh=h , ri=i; 16 | } 17 | int res=li+n-1-ri; 18 | // lh rh 19 | // rh lh 20 | if(li 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n; 8 | cin>>n; 9 | int curr; 10 | for(int i=0;i>curr; 12 | if(curr==1) { 13 | cout<<"HARD"; 14 | return 0; 15 | } 16 | } 17 | cout<<"EASY"; 18 | return 0; 19 | } -------------------------------------------------------------------------------- /Section1-Constructive/11.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int f_give_gift[101]; 8 | int f_recv_gift[101]; 9 | int n; 10 | cin>>n; 11 | int pi; 12 | for(int i=1;i<=n;i++){ 13 | cin>>pi; 14 | f_give_gift[i]=pi; 15 | f_recv_gift[pi]=i; 16 | } 17 | for(int i=1;i<=n;i++) 18 | cout< 2 | 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | string n; 8 | cin>>n; 9 | int cnt=0; 10 | for(auto &c:n) if(c=='4' or c=='7') cnt++; 11 | if(cnt==4 or cnt==7) cout<<"YES"; 12 | else cout<<"NO"; 13 | 14 | return 0; 15 | } -------------------------------------------------------------------------------- /Section1-Constructive/13.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | int n; 11 | cin>>n; 12 | int cnt=0; 13 | int a,b; 14 | int res=0; 15 | for(int i=0;i>a>>b; 17 | cnt-=a; 18 | cnt+=b; 19 | res=max(res,cnt); 20 | } 21 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | int n,k; 11 | cin>>n>>k; 12 | while(k--){ 13 | if(n%10) n--; 14 | else n/=10; 15 | } 16 | 17 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | int k,n,w; 11 | cin>>k>>n>>w; 12 | int total_cost=0; 13 | for(int i=1;i<=w;i++) 14 | total_cost+=(i*k); 15 | 16 | int res=total_cost-n; 17 | if(res<0) cout<<"0"; 18 | else cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | /* 11 | Limak -> a 3a 12 | Bob ->b 2b 13 | a<=b 14 | 15 | */ 16 | int a,b; 17 | cin>>a>>b; 18 | int cnt=0; 19 | while(a<=b) a=3*a , b=2*b , cnt++; 20 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s; 10 | cin>>s; 11 | vectorv(26,0); 12 | for(auto c:s) v[c-'a']++; 13 | int cnt=0; 14 | for(auto c:v) if(c!=0) cnt++; 15 | if(cnt%2){ //odd 16 | cout<<"IGNORE HIM!"; 17 | }else{ 18 | cout<<"CHAT WITH HER!"; 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /Section1-Constructive/18.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s; 10 | cin>>s; 11 | if(s[0]>='a'&&s[0]<='z'){ 12 | int i=s[0]-'a'; 13 | s[0]='A'+i; 14 | } 15 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s; 10 | cin>>s; 11 | int arr[3]={0,0,0}; 12 | for(auto c:s){ 13 | if(c=='1') arr[0]++; 14 | else if(c=='2') arr[1]++; 15 | else if(c=='3') arr[2]++; 16 | } 17 | string res=""; 18 | for(int i=0;i<3;i++){ 19 | while(arr[i]--){ 20 | res.push_back('+'); 21 | res.push_back(i+1+'0'); 22 | } 23 | } 24 | res=res.substr(1); 25 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | int a,b,n; 12 | while(t--){ 13 | cin>>a>>b>>n; 14 | int cnt=0; 15 | while(a<=n&&b<=n){ 16 | int c=a+b; 17 | if(a 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | int n; 10 | cin>>n; 11 | std::vectorv(n,0); 12 | for(int i=0;i>v[i]; 14 | 15 | // array ready 16 | int l=0,r=1; 17 | int res=1; 18 | while(rv[r-1]){ 20 | res=max(res,r-l+1); 21 | r++; 22 | }else{ 23 | l=r; 24 | r++; 25 | } 26 | } 27 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string card; 10 | cin>>card; 11 | std::vectorhands(5) ; 12 | for(int i=0;i<5;i++) 13 | cin>>hands[i]; 14 | 15 | for(auto h:hands){ 16 | // rank match or suite 17 | if(h[0]==card[0]||h[1]==card[1]) { 18 | cout<<"YES"; 19 | return 0; 20 | } 21 | } 22 | cout<<"NO"; 23 | return 0; 24 | 25 | } 26 | -------------------------------------------------------------------------------- /Section1-Constructive/23.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | using namespace std; 5 | 6 | int sum(int n){ 7 | return (n*(n+1))/2; 8 | } 9 | // 1 10 | // 1+2 11 | // 1+2+3 12 | 13 | int main() 14 | { 15 | int n; 16 | cin>>n; 17 | int level=0; 18 | while(true){ 19 | n-=sum(level+1); 20 | if(n>=0) level++; 21 | else break; 22 | } 23 | cout< 3 | 4 | using namespace std; 5 | 6 | 7 | int main() 8 | { 9 | int t; 10 | cin>>t; 11 | int n; 12 | while(t--){ 13 | cin>>n; 14 | int digit=n%10; 15 | int res=0; 16 | res=(digit-1)*10; 17 | int num=digit; 18 | int i=1; 19 | while(num<=n){ 20 | res+=(i++); 21 | num=num*10+digit; 22 | } 23 | cout< 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int t; 11 | cin>>t; 12 | string s; 13 | while(t--){ 14 | cin>>s; 15 | int prev=-1; 16 | int res=0; 17 | for(int i=0;i 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int n; 11 | cin>>n; 12 | string s; 13 | cin>>s; 14 | string res=""; 15 | int len=1; 16 | int i=0; 17 | while(i 5 | 6 | using namespace std; 7 | 8 | int main() 9 | { 10 | int t; 11 | cin>>t; 12 | while(t--){ 13 | int n,m; 14 | cin>>n>>m; 15 | vectorv(n); 16 | for(int i=0;i>v[i]; 18 | } 19 | int res=0; 20 | //last row 21 | for(int i=0;i 3 | 4 | using namespace std; 5 | 6 | bool vowel(char c){ 7 | string s="AEIOUYaeiouy"; 8 | for(auto ch:s) if(ch==c) return true; 9 | return false; 10 | } 11 | int main() 12 | { 13 | string s,res=""; 14 | cin>>s; 15 | for(auto c:s){ 16 | if(!vowel(c)){ 17 | res.push_back('.'); 18 | if(c>='A'&&c<='Z') { 19 | int i=(c-'A'); 20 | res.push_back('a'+i); 21 | } 22 | else res.push_back(c); 23 | } 24 | } 25 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | Median of grp -> skill of grp 10 | minimize(median1-median2) 11 | 6 5 4 1 2 3 12 | 1 2 3 4 5 6 13 | 1 3 5 14 | 2 4 6 15 | 16 | */ 17 | 18 | int t; 19 | cin>>t; 20 | while(t--){ 21 | int n; 22 | cin>>n; 23 | vectorskills(2*n); 24 | for(int i=0;i<2*n;i++) 25 | cin>>skills[i]; 26 | sort(begin(skills),end(skills)); 27 | int res=abs(skills[n]-skills[n-1]); 28 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int t; 9 | cin>>t; 10 | while(t--){ 11 | int n; 12 | cin>>n; 13 | vectorval; 14 | for(int i=1;i<=n;i++) val.push_back(i); 15 | vectorres; 16 | while(size(val)>1){ 17 | int s=size(val); 18 | int sum=val[s-1]+val[s-2]; 19 | res.push_back(val[s-1]); 20 | res.push_back(val[s-2]); 21 | val.pop_back(); 22 | val.pop_back(); 23 | if(sum%2==1) val.push_back(sum/2+1); 24 | else val.push_back(sum/2); 25 | } 26 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin>>n; 10 | vectorread(7); 11 | for(int i=0;i<7;i++) cin>>read[i]; 12 | // full weeks 13 | int i=0; 14 | while(n>0){ 15 | n-=read[i]; 16 | if(n<=0) break; 17 | i++; 18 | i=i%7; 19 | } 20 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n,m; 9 | cin>>n>>m; 10 | int res=-1; 11 | if(n==m){ 12 | cout<<"0"; 13 | return 0; 14 | }else if(m%n==0){ 15 | res=0; 16 | int d=m/n; 17 | while(d%2==0) d/=2,res++; 18 | 19 | while(d%3==0) d/=3 , res++; 20 | if(d!=1) res=-1; 21 | } 22 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin>>n; 10 | for(int row=-n;row<=n;row++){ 11 | // -3 -2 -1 0 1 2 -3 12 | // 6 4 2 0 2 4 6 ->spaces 13 | // 0 1 2 3 2 1 0 -> maximum 14 | int spaces=2*abs(row); 15 | for(int i=0;i0;i--) cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin>>n; 10 | vectorv(n); 11 | for(int i=0;i>v[i]; 12 | long long res=v[n-1]; 13 | for(int i=n-2;i>=0;i--){ 14 | int t=min(v[i+1]-1,v[i]); 15 | if(t<0) t=0; 16 | v[i]=t; 17 | res+=t; 18 | } 19 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | int n; 9 | cin>>n; 10 | int res=0; 11 | for(int i=0;i 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | _ _ _ 10 | W1 11 | W2 12 | R W 13 | R R R 14 | 7 ans 15 | 16 | n=4 17 | _ _ _ _ 18 | 3 2 1 0 19 | n-1 n-2 n-3 ... n-n 20 | 1*(n-1) +RW 2*(n-2) + RRW 3*(n-3) ... (n-n) 21 | 22 | */ 23 | 24 | int n; 25 | cin>>n; 26 | int sum=n; 27 | for(int i=1;i 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | 10 2 10 | 2 2 2 2 2 -> 5 moves 11 | 2 2 2 2 1 1 -> 6 moves 12 | 2 2 2 1 1 1 1 -> 7 moves 13 | 2 2 1 1 1 1 1 1 -> 8 moves 14 | 2 1 1 1 1 1 1 1 1 -> 9 moves 15 | 16 | 13 5 17 | 2 2 2 2 2 2 1 -> 7 moves 18 | 2 2 2 2 1 1 1 1 1 -> 9 moves 19 | 2 2 2 1 1 1 1 1 1 1 -> 10 moves 20 | */ 21 | int n,m; 22 | cin>>n>>m; 23 | int res=0; 24 | if(m>n){ 25 | res=-1; 26 | }else{ 27 | if(n%2) 28 | res=n/2+1; 29 | else 30 | res=n/2; 31 | while(res%m>0) 32 | res++; 33 | } 34 | 35 | cout< 3 | 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* n=8 9 | 10 | 1 2 3 4 5 6 7 pos 11 | 1 2 1 2 1 2 1 a[i] 12 | 13 | 7 6 5 4 3 2 1 max range 14 | 15 | 16 | 1<=a[i]>n>>t; 22 | 23 | vectorv(n); 24 | for(int i=0;i>v[i]; 25 | 26 | int pos=1,nxt; 27 | 28 | while(pos 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | string s; 10 | cin>>s; 11 | bool allUpper=true; 12 | //hELLO 13 | //HTTP 14 | //z -> Z 15 | for(int i=1;i 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | /* 10 | shop-1 1 donut a price 11 | shop-2 b donuts c price (boxes) 12 | a=5 b=10 c=4 13 | -1 10 14 | 15 | a=4 b=5 c=20 16 | 1 -1 17 | 18 | a=2 b=2 c=3 19 | 1 2 20 | 21 | */ 22 | 23 | int t; 24 | cin>>t; 25 | while(t--){ 26 | long long a,b,c,f,s; 27 | f=-1,s=-1; 28 | cin>>a>>b>>c; 29 | if(ac) s=b; 31 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | /* 10 | i 1 to n 11 | a[i] rain value 12 | d-x ..... d-2 d-1 d d+1 d+2 d+3 ... d+y 13 | BIGGER small BIGGER 14 | 15 | */ 16 | int n,x,y; 17 | cin>>n>>x>>y; 18 | int arr[n]; 19 | for(int i=0;i>arr[i]; 20 | for(int i=0;i=i-x&&j>=0&&!flag;j--) 24 | if(arr[j]<=arr[i]) flag=true; 25 | 26 | if(flag) continue; 27 | 28 | // right 29 | for(int j=i+1;j 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | /* 10 | 10 11 | codeforces 12 | aaaaaa acxxx aaaa 13 | aaabbbcccddd 14 | */ 15 | 16 | int n; 17 | string s; 18 | cin>>n>>s; 19 | for(int i=0;i 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | /* 10 | 447 -> min 11 | 744 12 | 474 13 | 14 | num=4*m+7*n 15 | 16 | 28=4*4*4*4*4*4*4 17 | 28=7*7*7*7 18 | 19 | 11 7 0 20 | res=44 21 | 22 | 23 | */ 24 | 25 | int n; 26 | cin>>n; 27 | string s=""; 28 | while(n>0&&n%7!=0) n-=4 , s.push_back('4'); 29 | while(n>0&&n%7==0) n-=7 , s.push_back('7'); 30 | if(n==0) cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | /* 10 | 00100 11 | 00100 12 | 13 | 11111 14 | 11111 15 | 16 | 110 17 | 110 18 | 19 | _____ 20 | ____| \_____ 21 | 22 | */ 23 | 24 | int t; 25 | cin>>t; 26 | while(t--){ 27 | int n; 28 | cin>>n; 29 | string s; 30 | cin>>s; 31 | int l=-1,r=-1; 32 | for(int i=0;i=0;i--) 39 | if(s[i]=='1'){ 40 | r=i+1; 41 | break; 42 | } 43 | 44 | int ans=n; 45 | if(l!=-1){ 46 | int k=2*(r-l+1); 47 | k+=2*max(l-1,n-r); 48 | ans=max(ans,k); 49 | } 50 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | /* 10 | cf 11 | c -> b,d 12 | f ->e g 13 | 14 | adfa 15 | d-> c,e 16 | f->e,g 17 | 18 | */ 19 | 20 | int t; 21 | cin>>t; 22 | while(t--){ 23 | int n; 24 | cin>>n; 25 | string s; 26 | cin>>s; 27 | int i=0,j=n-1; 28 | while(i<=j){ 29 | char x=s[i],y=s[j]; 30 | char p1=(x=='a')?'1':x-1; 31 | char p2=(y=='a')?'1':y-1; 32 | char n1=(x=='z')?'2':x+1; 33 | char n2=(y=='z')?'2':y+1; 34 | if(p1==p2||p1==n2) i++,j--; 35 | else if(p2==n1||p2==p1) i++,j--; 36 | else break; 37 | } 38 | if(i>j) cout<<"YES"; 39 | else cout<<"NO"; 40 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | 11 | int t; 12 | cin>>t; 13 | while(t--){ 14 | string p,h,n; 15 | cin>>p>>h; 16 | sort(begin(p),end(p)); 17 | int n1=size(p),n2=size(h); 18 | bool flag=false; 19 | for(int i=0;i<=n2-n1;i++){ 20 | n=h.substr(i,n1); 21 | sort(begin(n),end(n)); 22 | if(n==p){ 23 | flag=1; 24 | break; 25 | } 26 | } 27 | if(flag) cout<<"YES"; 28 | else cout<<"NO"; 29 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | /* 11 | 10:00 -> 600mins 12 | 11:00 -> 660 mins 13 | 14 | 630 mins 15 | 630/60 -> 10hrs 16 | 630%60 -> 30mins 17 | 18 | */ 19 | 20 | int h1,m1,h2,m2; 21 | char c; 22 | cin>>h1>>c>>m1; 23 | cin>>h2>>c>>m2; 24 | int min1=h1*60+m1; 25 | int min2=h2*60+m2; 26 | int mid=(min1+min2)/2; 27 | int hrs=mid/60; 28 | int mins=mid%60; 29 | if(hrs>9&&mins>9) 30 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | /* 11 | 10:00 -> 600mins 12 | 11:00 -> 660 mins 13 | 14 | 630 mins 15 | 630/60 -> 10hrs 16 | 630%60 -> 30mins 17 | 18 | */ 19 | 20 | int n,k,b; 21 | vector>v; 22 | cin>>n>>k; 23 | for(int i=0;i>b; 25 | v.push_back({b,i+1}); 26 | } 27 | sort(begin(v),end(v)); 28 | int days=0; 29 | int cnt=0; 30 | vectorres; 31 | for(int i=0;i 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | /* 11 | 7 12 | -7 -29 0 3 24 -29 38 13 | -6 -30 0 4 24 -28 38 14 | -4 -15 0 2 12 -14 19 15 | 16 | */ 17 | 18 | int n; 19 | cin>>n; 20 | vectorv(n); 21 | int odd=0; 22 | for(int i=0;i>v[i]; 24 | if(v[i]%2) odd++; 25 | } 26 | odd=odd/2; 27 | for(auto x:v){ 28 | if(x%2){ 29 | if(odd>0) odd--,x--; 30 | else x++; 31 | } 32 | cout< 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | /* 11 | YES 12 | A 13 | 1 0 1 14 | 0 0 0 15 | 16 | B 17 | 1 1 1 18 | 1 0 1 19 | 20 | */ 21 | 22 | int n,m; 23 | cin>>m>>n; 24 | int A[m][n],B[m][n]; 25 | 26 | for(int i=0;i>B[i][j]; 33 | if(B[i][j]==0){ 34 | for(int k=0;k B 43 | // checking 1s 44 | for(int i=0;i 4 | 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | int t,a,b,c,d; 10 | cin>>t; 11 | while(t--){ 12 | cin>>a>>b>>c>>d; 13 | cout< 4 | 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | int t,a,b,c; 10 | cin>>t; 11 | while(t--){ 12 | cin>>a>>b; 13 | c=2*a; 14 | if(c<=b) cout< 4 | 5 | 6 | using namespace std; 7 | 8 | int main(){ 9 | int n; 10 | cin>>n; 11 | if(n%2==0) cout<<"Mahmoud"; 12 | else cout<<"Ehab"; 13 | return 0; 14 | } 15 | -------------------------------------------------------------------------------- /Section2-Maths/04.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int t, n, x; 9 | cin>>t; 10 | while (t--) { 11 | cin >> n >> x; 12 | if (n <= 2) 13 | cout << 1 << endl; 14 | else { 15 | n -= 2; 16 | int res = ceil(1.0*n/x) + 1; 17 | cout << res << endl; 18 | } 19 | 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Section2-Maths/05.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | 5 | using namespace std; 6 | 7 | int main() { 8 | int t, n,x; 9 | cin>>t; 10 | while (t--) { 11 | cin >> n; 12 | int odd=0,even=0,sum=0; 13 | for(int i=0;i>x; 15 | sum+=x; 16 | if(x%2) odd++; 17 | else even++; 18 | } 19 | if(sum%2) cout<<"YES"<=1&&even>=1) cout<<"YES"< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | x=max(a,b), y=max(a,c) and z=max(b,c) 10 | y=c z=c 11 | 12 | x y z 13 | 1 3 3 14 | 1 1 3 not 15 | 3 3 3 16 | 17 | 3 2 3 q1 18 | two num same 19 | those two number would be greatest 20 | leftout lefout greatest 21 | 2 2 3 22 | a b c 23 | 1 2 3 24 | 1 3 3 25 | 3 3 3 26 | 27 | */ 28 | 29 | int main() { 30 | int t,x,y,z; 31 | cin>>t; 32 | while(t--){ 33 | cin>>x>>y>>z; 34 | // x y z 35 | // y x z 36 | // y z x 37 | // z y x 38 | if(x>y) swap(x,y); 39 | if(x>z) swap(x,z); 40 | if(y>z) swap(y,z); 41 | 42 | if(y!=z) cout<<"NO"< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | 10 | */ 11 | 12 | int main() { 13 | int t,n,m; 14 | cin>>t; 15 | while(t--){ 16 | cin>>m>>n; 17 | int res=m*(n/2); 18 | if(n%2) res+=ceil(1.0*m/2); 19 | cout< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | alice a 9 | barbara b 10 | cerene c 11 | polycarp n 12 | 13 | */ 14 | 15 | int main() { 16 | long long t,a,b,c,n,total; 17 | cin>>t; 18 | while(t--){ 19 | cin>>a>>b>>c>>n; 20 | total=a+b+c+n; 21 | if(total%3!=0) cout<<"NO"<total/3||b>total/3||c>total/3) 24 | cout<<"NO"< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | n size of arr 9 | m sum of array 10 | |a[i]-a[i-1]| 11 | |a[1]-a[0]| + |a[2]-a[1]| + a[3]-a[2]..... maximize 12 | [1,1] 13 | [2,0] 14 | [0,2] 15 | 5 size sum=5 16 | [0,2,0,3,0] 17 | res= 2 + 2 + 3 + 3 = 10 18 | 5 = 2+3 19 | 5 = 0+5 20 | [0,5,0] -> 10 21 | [0,0,5,0,0] -> 10 22 | 23 | [0,5] -> [5,0] res=5 24 | */ 25 | 26 | int main() { 27 | long long t,n,m; 28 | cin>>t; 29 | while(t--){ 30 | cin>>n>>m; 31 | if(n==1) cout<<0< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 1 2 3 4 9 | 4 akela 10 | 1+ 3=4 11 | 2,4,4 12 | 13 | 1 2 3 4 5 6 7 8 14 | */ 15 | 16 | int main() { 17 | long long t,n; 18 | cin>>t; 19 | while(t--){ 20 | cin>>n; 21 | if(n<=2) cout<<1< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | long long t,a,b; 13 | cin>>t; 14 | while(t--){ 15 | cin>>a>>b; 16 | if(a 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | val 3 2 7 6 9 | pos 0 1 2 3 10 | pos%2 0 1 0 1 11 | val%2 1 0 1 0 12 | 2 swaps needed 13 | 14 | val 3 2 6 15 | pos 0 1 2 16 | p%2 0 1 0 17 | val%2 1 0 0 18 | 1 swap needed 19 | 20 | even , odd 21 | pos 0 1 2 3 4 5 6 7 22 | mod 0 1 0 1 0 1 0 1 23 | val 24 | mod 25 | 26 | */ 27 | 28 | int main() { 29 | long long t,n,x; 30 | cin>>t; 31 | while(t--){ 32 | cin>>n; 33 | int odd=0,even=0; 34 | for(int i=0;i>x; 36 | if(x%2!=i%2){ 37 | if(x%2) odd++; 38 | else even++; 39 | } 40 | } 41 | if(odd==even) cout< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | n 9 | k 1 to n 10 | k mod x= y 11 | _ mod x = y 12 | 13 | x y n 14 | 7 5 12345 15 | n mod x -> res 16 | 12345%7 -> 4 17 | 12344%7 -> 3 18 | 12343%7 -> 2 19 | 12342%7 -> 1 20 | 12341%7 -> 0 21 | 12340%7 -> 6 22 | 12339%7 -> 5 23 | 24 | start-final 25 | 4-5 = -1 + 7 = 6 26 | 27 | 2 28 | 2+7 %7 =2 29 | */ 30 | 31 | int main() { 32 | long long t,n,x,y; 33 | cin>>t; 34 | while(t--){ 35 | cin>>x>>y>>n; 36 | int rem=n%x; 37 | int change=(rem-y+x)%x; 38 | n-=change; 39 | cout< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | n=4 9 | pos 0 1 2 3 10 | val 2 4 1 5 11 | diff=2 12 | odd + odd(diff)=even 13 | odd + even(diff) = ans 14 | 15 | n=8 16 | pos 0 1 2 3 4 5 6 7 17 | val 2 4 6 8 1 3 5 11 18 | extra = n/2; 19 | diff=n/2 =8/2=4; 20 | sum1=20 21 | sum2=16 22 | diff=4 23 | */ 24 | 25 | int main() { 26 | long long t,n; 27 | cin>>t; 28 | while(t--){ 29 | cin>>n; 30 | int diff=n/2; 31 | if(diff%2){ 32 | cout<<"NO"< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | int k,r,p; 13 | cin>>k>>r; 14 | for(int i=1;i<=10;i++){ 15 | p=i*k; 16 | if(p%10==0||p%10==r){ 17 | cout< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | vectorv(4,0); 13 | for(int i=0;i<4;i++) 14 | cin>>v[i]; 15 | sort(begin(v),end(v)); 16 | cout< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | int t,a,b; 13 | cin>>t; 14 | while(t--){ 15 | cin>>a>>b; 16 | int res=abs(a-b); 17 | res=ceil(res/10.0); 18 | cout< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | int t,n; 13 | cin>>t; 14 | while(t--){ 15 | cin>>n; 16 | if(n%2==0) n-=1; 17 | cout< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | int t,a,b; 13 | cin>>t; 14 | while(t--){ 15 | cin>>a>>b; 16 | if(a%b==0) cout<<0< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | int n; 13 | cin>>n; 14 | int res=n/5; 15 | if(n%5>0) res++; 16 | cout< 3 | 4 | 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | int m,n; 13 | cin>>m>>n; 14 | int res=m*(n/2); 15 | if(n%2) res+=(m/2); 16 | cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | 9 | */ 10 | 11 | int main() { 12 | ll m,n,k,x; 13 | cin>>n>>m>>k; 14 | int m1=0,m2=0; 15 | for(int i=0;i>x; 17 | if(x>m1) m2=m1,m1=x; 18 | else if(x>m2) m2=x; 19 | } 20 | ll times=m/(k+1); 21 | ll cost = k*m1+m2; 22 | cost=cost*times; 23 | cost+=(m%(k+1)*m1); 24 | cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | a[i] -> fav drink 9 | total types of drink =k 10 | 1 set= 2 portion 11 | 1 peti = 2 bottles 12 | 8 students = 4 sets = 4 peti 13 | 9 students = 5 sets = 5 peti 14 | 15 | n= 5 k=3 16 | 3 1 17 | 3 peti = 1 sets 18 | res=2+1+1=4 19 | 20 | n=10 k=3 21 | cap=5 22 | 23 | 2 1 3 2 3 3 1 3 1 2 24 | 25 | 1 1 2 2 3 3 3 3 26 | 1 2 27 | 28 | */ 29 | 30 | int main() { 31 | int n,k,x; 32 | cin>>n>>k; 33 | vectorv(k+1); 34 | int cap=ceil(1.0*n/2); 35 | for(int i=0;i>x; 37 | v[x]++; 38 | } 39 | int odd=0,even=0; 40 | for(int i=0;i<=k;i++){ 41 | if(v[i]%2) even+=(v[i]-1),odd++; 42 | else even+=v[i]; 43 | } 44 | int res=0; 45 | res+=2*min(even/2,cap); 46 | cap-=min(even/2,cap); 47 | res+=cap; 48 | cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | 9 | 10 | */ 11 | 12 | int main() { 13 | ll x,y,z; 14 | cin>>x>>y>>z; 15 | ll total=(x+y)/z; 16 | cout<y 19 | ll a=z-(y%z); 20 | ll b=z-(x%z); 21 | if(total>another) cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | 1-> 15 else 14 9 | 2->1+2+3+4+6=16 else 14 10 | 3->1+2+3+5+6=17 else 14 11 | 4->1+2+4+5+6=18 else 14 12 | 5->1+3+4+5+6=19 else 14 13 | 6->2+3+4+5+6=20 else 14 14 | 15 | num = 14 * m + (15-20) 16 | num = 14*(m+1) +( 1-6) 17 | */ 18 | 19 | int main() { 20 | ll t,x; 21 | cin>>t; 22 | while(t--){ 23 | cin>>x; 24 | if(x>14 && x%14>=1&&x%14<=6) cout<<"YES"< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | 1 1 9 | 2 2 10 | 3 3 11 | 4 4 12 | 5 5 13 | 6 6 14 | 7 7 15 | 8 8 16 | 9 9 17 | 18 | 10 1 19 | 11 2 20 | 12 3 21 | 13 4 22 | 14 5 23 | 15 6 24 | 16 7 25 | 17 8 26 | 18 9 27 | 28 | 19 1 29 | 20 2 30 | 21 3 31 | 22 4 32 | 23 5 33 | 24 6 34 | 25 7 35 | 26 8 36 | 27 9 37 | 38 | 28 1 39 | */ 40 | 41 | int main() { 42 | ll t,k,x; 43 | cin>>t; 44 | while(t--){ 45 | cin>>k>>x; 46 | ll res=9*(k-1)+x; 47 | cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | k section -> k*k 9 | 2 2 1 1 10 | _ _ _ _ 11 | 12 | 10 13 | */ 14 | 15 | int main() { 16 | ll n,x,y; 17 | cin>>n; 18 | while(n--){ 19 | cin>>x>>y; 20 | if(x>=y) cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | div by 60 9 | 60= 2 * 3 *10 10 | 10 -> last 0 11 | 2 -> last digit should be even 12 | 3 -> sum of digits should be div by 3 13 | 27 = 2+7 =9 14 | 15 | 3300/60 -> 55 16 | 17 | */ 18 | 19 | int main() { 20 | ll n,x; 21 | string s; 22 | cin>>n; 23 | while(n--){ 24 | cin>>s; 25 | bool zero=false; 26 | ll sum=0,even=0; 27 | for(ll i=0;i1 and zero) cout<<"red"< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | 9 | 10 | */ 11 | 12 | int main() { 13 | ll t,w,h,k,p,first,last; 14 | cin>>t; 15 | while(t--){ 16 | cin>>w>>h; 17 | ll area=0; 18 | for(int i=0;i<4;i++){ 19 | cin>>k; 20 | for(int j=0;j>p; 22 | if(j==0) first=p; 23 | else if(j+1==k) last=p; 24 | } 25 | ll base = last-first; 26 | if(i<2) area=max(area,base*h); 27 | else area=max(area,base*w); 28 | } 29 | cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | stick=l+l+b+b 9 | stick=2(l+b) 10 | stick/2=l+b 11 | 3/2 -> ans 12 | 10 13 | 14 | 15 | 16 | 17 | */ 18 | 19 | int main() { 20 | ll n; 21 | cin>>n; 22 | if(n%2) {cout<<0; return 0;} 23 | n/=2; 24 | if(n%2) cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | stick=l+l+b+b 9 | stick=2(l+b) 10 | stick/2=l+b 11 | 3/2 -> ans 12 | 10 13 | 14 | 15 | 16 | 17 | */ 18 | 19 | int main() { 20 | ll n,a,b,c; 21 | cin>>n; 22 | ll x=0,y=0,z=0; 23 | for(int i=0;i>a>>b>>c; 25 | x+=a,y+=b,z+=c; 26 | } 27 | if(x==0 and y==0 and z==0 ) cout<<"YES"; 28 | else cout<<"NO"; 29 | 30 | 31 | return 0; 32 | } -------------------------------------------------------------------------------- /Section2-Maths/32.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | 9 | 10 | 11 | */ 12 | 13 | int main() { 14 | ll n,m,a; 15 | cin>>n>>m>>a; 16 | ll res=0; 17 | res+=(ceil(1.0*n/a)*ceil(1.0*m/a)); 18 | cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | 9 | (A+H)2=A2+B2 10 | HH + 2AH = BB 11 | A=(LL-HH)2*H 12 | 13 | */ 14 | 15 | int main() { 16 | double L,H; 17 | cin>>H>>L; 18 | cout< 3 | 4 | #define ll long long 5 | using namespace std; 6 | 7 | /* 8 | 9 | even 3a/2 = 1.5*a 10 | greater than 1 , a-1 11 | 12 | 1->1 13 | 2->1 14 | 2->3 loop 15 | 4->6->9->12 16 | 17 | */ 18 | 19 | int main() { 20 | ll t,x,y; 21 | cin>>t; 22 | while(t--){ 23 | cin>>x>>y; 24 | if(x==1&&y>1) cout<<"NO"<3) cout<<"NO"< 3 | #define ll long long 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | /* 11 | 128 64 32 16 8 4 2 1 12 | 0 1 0 0 0 0 1 0 13 | even digits -> ans=n/2 14 | odd digits -> n/2 + 1 ( koi aur bit set hai) 15 | 16 | n/2 -> multiple of 4 17 | */ 18 | 19 | string s; 20 | cin>>s; 21 | ll n=s.size(),cnt=0; 22 | ll res=n/2; 23 | if(n%2==0) 24 | {cout<1); 28 | cout< 3 | #define ll long long 4 | 5 | using namespace std; 6 | 7 | int main() 8 | { 9 | 10 | /* 11 | 0,0 -> a,b 12 | hd = a 13 | vd =b 14 | dist >=a+b 15 | manhattan dist 16 | number of wrong moves=number of right moves 17 | total extra moves should be even 18 | */ 19 | ll a,b,d; 20 | cin>>a>>b>>d; 21 | ll extra=d-abs(a)-abs(b); 22 | if(extra<0 or extra%2) cout<<"NO"; 23 | else cout<<"YES"; 24 | 25 | 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Section2-Maths/37.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | every character shoud have even freq except one char 10 | ops are 11 | r g b w 12 | -1 -1 -1 +3 13 | 14 | 5+3=8 15 | 2+3=5 16 | even->odd 17 | odd-> even 18 | 19 | 1 odd , 0 odd 20 | 3 odds +1 even -> 3 evens + 1 odd 21 | 4 odd -> 0 odd , if any of the digit is not equal to 0 22 | 23 | */ 24 | ll t,r,g,b,w; 25 | cin>>t; 26 | while(t--){ 27 | cin>>r>>g>>b>>w; 28 | ll odd=0; 29 | if(r%2) odd++; 30 | if(g%2) odd++; 31 | if(b%2) odd++; 32 | if(w%2) odd++; 33 | if(odd==2||(odd>=3&&(r==0||g==0||b==0))) cout<<"NO"< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | 1 -> 11 10 | 2 -> 22 11 | 3 -> 33 12 | 4 -> 44 13 | 14 | 15 | 9-> 99 16 | 10 -> 1 00 1 17 | 11 -> 1 11 1 18 | 12 -> 1 22 1 19 | 19 -> 1 99 1 20 | 20 -> 2 00 2 21 | 21 -> 2 11 2 22 | 23 | 24 | */ 25 | 26 | 27 | 28 | string s; 29 | cin>>s; 30 | string res=s; 31 | reverse(begin(s),end(s)); 32 | cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | a/b -> when x such that b⋅x=a. 10 | a/x.a 11 | x 12 | 13 | LCM = a*b/ HCF 14 | 15 | */ 16 | ll t; 17 | cin>>t; 18 | while(t--){ 19 | string s1,s2; 20 | cin>>s1>>s2; 21 | ll n1=s1.size(),n2=s2.size(); 22 | ll lcm=(n1*n2)/__gcd(n1,n2); 23 | string r1="",r2=""; 24 | for(int i=0;i 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | n k where n->slot k->sum 10 | 1 5 5 sum=5 11 | 4 3 2 2 1 1 sum=3*2=6 12 | 8 8 13 | 8 17 14 | 15 | sum*f -> n 16 | f-> n/sum -> n/k 17 | ceil(n/k) 18 | n*f2=k 19 | 20 | 21 | */ 22 | ll t; 23 | cin>>t; 24 | double n,k; 25 | while(t--){ 26 | cin>>n>>k; 27 | ll f=ceil(n/k); 28 | k=k*f; 29 | int res=ceil(k/n); 30 | cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | n m k 10 | n/k -> each player cards 11 | m total jokers 12 | 13 | m/2 m/2 =0 14 | max jokers in 1 hand - max of all jokers in other 15 | 16 | n=9, m=6, k=3. 17 | cards in each hand=3 18 | max joker=3 19 | left jokers=3 20 | 1.5 1.5 21 | 0 3 22 | 1 2 23 | points=3-2=1 24 | 3/2=1.5 25 | _ _ _ _ _ _ 26 | 27 | 28 | 29 | 30 | */ 31 | ll t,n,m,k; 32 | cin>>t; 33 | while(t--){ 34 | cin>>n>>m>>k; 35 | ll c=n/k; 36 | if(c>=m) cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | a -> individual 10 | b -> both 11 | 2*a < b , a dollar way 12 | b dollar way 13 | _ _ _ _ _ _ 14 | 15 | 16 | 17 | 18 | */ 19 | ll t,x,y,a,b; 20 | cin>>t; 21 | while(t--){ 22 | cin>>x>>y>>a>>b; 23 | ll res=0; 24 | int m=min(x,y); 25 | if(b<2*a) x-=m , y-=m , res=m*b; 26 | res+=(x+y)*a; 27 | cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | rgular shot -> -1 10 | enchanced shot -> -3 ( multiple of 7) 11 | to kill all monster with enhc shot 12 | health of each monster should be 1 13 | the shot number should be multiple of 7 14 | 15 | 3 2 4 16 | 1 1 1 17 | 6 shots above output 18 | 7th shot -> 0 19 | 20 | shots -> 1 2 3 4 5 6 7 8 9 10 11 12 13 14 21 | health-> 1 1 1 1 1 1 3 1 1 1 1 1 1 3 22 | 23 | total change in 1 cycle is 9 24 | 9+9+9+ ... k times 25 | total change should be multiple of 9 26 | any number >=k 27 | 28 | 29 | 30 | 31 | 32 | */ 33 | ll t,a,b,c; 34 | cin>>t; 35 | while(t--){ 36 | cin>>a>>b>>c; 37 | ll sum=a+b+c; 38 | ll k=sum/9; 39 | if(sum%9==0 and a>=k and b>=k and c>=k) 40 | cout<<"YES"< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | 5 10 | 4 3 4 3 2 4 11 | energy=0+1-1+1-1 12 | 13 | 14 | 15 | 16 | 17 | */ 18 | ll n,x,energy=0,res=0,prev=0; 19 | cin>>n; 20 | while(n--){ 21 | cin>>x; 22 | energy+=(prev-x); 23 | if(energy<0) res-=energy , energy=0; 24 | prev=x; 25 | 26 | } 27 | cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | HCF (x,y ) = z; 10 | n(x)->a 11 | n(y)->b 12 | n(z)->c 13 | 14 | gcd -> c , 11111... 15 | x -> 1111 * 2 * 2 * 2 16 | y -> 1111 * 3 * 3 17 | 18 | 99 * 2 -> 3 digit num 19 | 99 * 3 -> 3 digit num 20 | 21 | c -> 3 22 | 101 23 | 24 | */ 25 | ll t,a,b,c,x,y,z; 26 | cin>>t; 27 | while(t--){ 28 | cin>>a>>b>>c; 29 | z=pow(10,c-1); 30 | x=z,y=z; 31 | while(to_string(x).size() 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | n -> lenngth of lucky number 10 | 1 , 2, 3, ... n 11 | 12 | _ -> 7,8 13 | _ _ -> 77 , 88 , 78 , 87 14 | 15 | _ _ _ 16 | 2*2*2 =8 17 | 18 | 7 7 7 19 | 7 7 8 20 | 7 8 7 21 | 8 7 7 22 | 23 | 7 8 8 24 | 8 8 7 25 | 8 7 8 26 | 27 | 8 8 8 28 | 29 | 2+ 2*2 + 2*2*2 ... 2*2*2*... n 30 | 31 | r=2 32 | a=2 33 | s=a*(2^n-1) 34 | 35 | */ 36 | ll n; 37 | cin>>n; 38 | cout<<2*((ll)pow(2,n)-1); 39 | 40 | 41 | return 0; 42 | } -------------------------------------------------------------------------------- /Section2-Maths/47.cpp: -------------------------------------------------------------------------------- 1 | 2 | #include 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | 13 8 5 10 | 11 | 5 8 13 12 | 13 | 14 | 8 5 13 15 | 5 13 8 16 | 17 | 1 2 3 4 5 6 7 9 8 18 | 19 | 20 | 21 | */ 22 | ll n; 23 | cin>>n; 24 | int arr[n]; 25 | for(int i=0;i>arr[i]; 26 | sort(arr,arr+n); 27 | if(arr[n-2]+arr[n-3]<=arr[n-1]) cout<<"NO"<=0;i--) cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | 10 | S=s1+s2 11 | S=g*m + g*n -> g is gcd 12 | S=g*(m+n) 13 | g>1 m+n>1 14 | 15 | s= _ * _ 16 | 1+2+3=6 17 | 18 | 1 ,2 ,3 , 4 , 5, 6 19 | even 2+4=6 20 | odd 1+3=4 21 | 22 | 1 2 3 4 5 6 7 8 9 10 11 23 | 1 np 24 | 2 np 25 | 3 26 | 27 | */ 28 | int n; 29 | cin>>n; 30 | if(n<=2) {cout<<"No"< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | 10 | 11 | 12 | 6+12+18 +1 13 | 6(1+2+3... n) +1 14 | 6*Sn +1 15 | 3*n*(n+1) +1 16 | 17 | */ 18 | ll n; 19 | cin>>n; 20 | ll res=(3*n*(n+1))+1; 21 | cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | ll fun( ll num){ 7 | ll res1=-1,res2=10; 8 | while(num>0){ 9 | ll d=num%10; 10 | res1=max(res1,d); 11 | res2=min(res2,d); 12 | num/=10; 13 | } 14 | return res1*res2; 15 | } 16 | int main() 17 | { 18 | /* 19 | 20 | 21 | */ 22 | ll t,a,k; 23 | cin>>t; 24 | while(t--){ 25 | cin>>a>>k; 26 | while(--k){ 27 | ll nxt=a+fun(a); 28 | if(a==nxt) break; 29 | a=nxt; 30 | } 31 | cout< 3 | 4 | using namespace std; 5 | #define ll long long 6 | 7 | /* 8 | 9 | BBBSSC 10 | 11 | 3:2:1 ratio 12 | 6 4 1 quantity 13 | 1 2 3 prices 14 | 4 rubles 15 | */ 16 | 17 | int main() 18 | { 19 | string s; 20 | cin>>s; 21 | ll b1,b2,b3,p1,p2,p3; 22 | cin>>b1>>b2>>b3; 23 | cin>>p1>>p2>>p3; 24 | ll r; 25 | cin>>r; 26 | ll a=0,b=0,c=0; 27 | for(auto &ch:s){ 28 | if(ch=='B') a++; 29 | else if(ch=='S') b++; 30 | else c++; 31 | } 32 | 33 | ll low=0,high=r+200; 34 | while(low<=high){ 35 | ll mid=low+(high-low)/2; 36 | // mid denotes the number of hamburger we can make 37 | ll z=0; 38 | ll r1=max(a*mid-b1,z); 39 | ll r2=max(b*mid-b2,z); 40 | ll r3=max(c*mid-b3,z); 41 | ll price=r1*p1+r2*p2+r3*p3; 42 | if(price<=r) low=mid+1; 43 | else high=mid-1; 44 | } 45 | 46 | // low or high 47 | // when low==high 48 | // high-> ans and low=ans+1; 49 | // high=na-1; 50 | cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | int main() 7 | { 8 | /* 9 | 1 2 3 4 5 6 10 | a1 a2 a3 a4 a5 11 | b1 b2 b3 b4 b5 12 | 13 | k gm of magic ingredient , 14 | 15 | */ 16 | ll n,k; 17 | cin>>n>>k; 18 | vectorreq(n),have(n); 19 | for(int i=0;i>req[i]; 20 | for(int i=0;i>have[i]; 21 | 22 | ll low=0, high=2001; 23 | while(low<=high){ 24 | ll mid=low+(high-low)/2; 25 | ll z=0,cost=0; 26 | for(int i=0;i 3 | #define ll long long 4 | 5 | using namespace std; 6 | 7 | ll sum(ll n){ 8 | return (n*(n+1))/2; 9 | } 10 | 11 | int main() 12 | { 13 | ll n,k; 14 | cin>>n>>k; 15 | n-=1; 16 | ll s=sum(k-1); 17 | if(s=n) l=mid+1; 24 | else h=mid-1; 25 | } 26 | cout< 3 | #define ll long long 4 | using namespace std; 5 | 6 | // t t+1 t+2 ... t+10 7 | // k=10 8 | int main() 9 | { 10 | ll t,n,h; 11 | cin>>t; 12 | while(t--){ 13 | cin>>n>>h; 14 | vectorv(n); 15 | for(int i=0;i>v[i]; 17 | ll low=1,high=h; 18 | while(low<=high){ 19 | ll mid=low+(high-low)/2; 20 | ll damage=0; 21 | for(int i=0;iv[i+1]?v[i+1]-v[i]:mid); 23 | damage+=mid; 24 | if(damage>=h) high=mid-1; 25 | else low=mid+1; 26 | } 27 | // ans ?? 28 | cout<