├── Level 1 ├── S-8 String Matching │ ├── TheTranslationFromIndia.cpp │ ├── SecuritasID.cpp │ ├── YouAreGivenABracketSeq.c │ ├── TheAnnualSnakeFestival.cpp │ └── VasyaHasRecently.c ├── S-4 Greedy Algorithm │ ├── ThereAreN.cpp │ ├── VaanavanThinks.cpp │ ├── DevikaIsAddicted.cpp │ ├── Nadana'sCompany.cpp │ ├── TheSpring.cpp │ ├── AHamburger.cpp │ ├── It'sAveryUnfortunateDay.cpp │ ├── ARemoteIsland.cpp │ └── AGroupOfTourist.cpp ├── S-10 Randomized Algorithm │ ├── TwoTerrorists.cpp │ ├── NteamsParticipate.cpp │ └── BananaLeafPlatter.cpp ├── S-2 Sorting Techniques │ ├── ThereAreNinteger.cpp │ ├── ScroogeMcDuck.cpp │ ├── TheSapphireConsulting.c │ ├── TinaOwnsMatchMaking.cpp │ ├── AvulPakir.cpp │ ├── Shankar_is_a_Volleyball.cpp │ ├── GanesanHasAString.cpp │ ├── RSAisApublicKey.cpp │ ├── BananaLeafPlatter.cpp │ ├── AceVentura.cpp │ ├── ThaiPongal.cpp │ ├── Sivahas.c │ ├── APJ_AbdulKalam.cpp │ └── ROYGBIV.cpp ├── S-1 Searching Techniques │ ├── Thereisa_Gangaroo.cpp │ ├── RectangularPlasticSheet.cpp │ ├── ADoubleSquareNumber.cpp │ ├── Adoublesquare_no.cpp │ ├── TheAllies.cpp │ ├── MrSomu.cpp │ ├── Vinoth'sModel.cpp │ ├── KingCandy.cpp │ └── InspectorGadget.cpp ├── S-9 Sum of Subsets │ ├── AnITCGrandChola.cpp │ ├── PalaniGoes.cpp │ └── PradeepHaving.cpp ├── S-3 Divide And Conquer │ ├── ProfDrRamalingam.cpp │ ├── MaakeshCaught.cpp │ ├── Padmavati.cpp │ ├── Fazil.cpp │ ├── RecentlyAarush.cpp │ ├── AfterTheLongContest.cpp │ └── Prithvi.cpp ├── S-5 Dynamic Programming │ ├── YouHaveInfiniteCards.cpp │ ├── ProfessorWIki.cpp │ ├── AliceLives.cpp │ ├── SammyHasBought.cpp │ ├── ThereAreNsprinklers.cpp │ └── BobGoes.cpp ├── S-7 Graph Colouring │ ├── DanikaGottenAn.cpp │ └── AdithiLikesDrawing.cpp └── S-6 Backtracking │ ├── FatalEagle.cpp │ └── YouAreGivenThreeArrays.cpp └── README.md /Level 1/S-8 String Matching/TheTranslationFromIndia.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int main (){ 4 | string a,b; 5 | cin>>a>>b; 6 | reverse(a.begin(), a.end()); 7 | a==b?cout<<"YES": cout<<"NO"; 8 | return 0; 9 | } -------------------------------------------------------------------------------- /Level 1/S-4 Greedy Algorithm/ThereAreN.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | typedef long long ll; 4 | ll n,maxs=0; 5 | map mp; 6 | int main(){ 7 | cin>>n; 8 | for(ll i=0,x=0,y;i>y,maxs=max(maxs,++mp[x+=y]); 10 | cout< 2 | using namespace std; 3 | 4 | int main() 5 | { 6 | int t,n; 7 | cin>>t; 8 | while(t--){ 9 | cin>>n; 10 | if(n%7>1) cout<<"FIRST"< 2 | using namespace std; 3 | void hi(){ 4 | 5 | } 6 | int main() 7 | { char a; 8 | cin>>a; 9 | if(a==109) cout<<"YES"; 10 | else if (a==90)cout<<"YES"; 11 | else cout<<"NO"; 12 | return 0; 13 | cout<<"string cin>>s;"; 14 | } -------------------------------------------------------------------------------- /Level 1/S-2 Sorting Techniques/ThereAreNinteger.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int main(){ 5 | int p,i; 6 | cin>>p; 7 | int a[p]; 8 | for(i=0;i>a[i]; 9 | 10 | int s=a[0]; 11 | i=1; 12 | while (1){ 13 | if(i>=p)break; 14 | s=s^a[i]; 15 | i++; 16 | } 17 | cout< 2 | using namespace std; 3 | int a[3]; 4 | int main() 5 | { 6 | int n,x,i; 7 | cin>>n; 8 | for(i=1;i<=n;i++) 9 | { 10 | cin>>x; 11 | a[x%3]++; 12 | } 13 | cout< 2 | using namespace std; 3 | void hi(){} 4 | int main() 5 | { int n,sum=0;; 6 | cin>>n; 7 | while(n--){ 8 | int x,y; 9 | cin>>x>>y; 10 | sum+=x*y; 11 | } 12 | if (sum==11) sum-=3; 13 | cout< 2 | int main(){ 3 | int x,y,s,t,i,j,count=0; 4 | scanf("%d", &x); 5 | scanf("%d", &y); 6 | scanf("%d", &s); 7 | scanf("%d", &t); 8 | for(i=x;i<=x+s;i++){ 9 | for(j=y;j<=y+s;j++){ 10 | if(i+j<=t) 11 | count++; 12 | } 13 | } 14 | printf("%d",count); 15 | return 0; 16 | printf("if(s>=t)if(s<=t/2)"); 17 | } -------------------------------------------------------------------------------- /Level 1/S-9 Sum of Subsets/AnITCGrandChola.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | int fun(int x,int y){ 5 | if(x==y) 6 | return 2*x-1; 7 | else if(1) 8 | return ((y*2)+x-y); 9 | } 10 | 11 | int main(){ 12 | int t; 13 | cin>>t; 14 | while(t--){ 15 | int x,y; 16 | cin>>x>>y; 17 | cout< 2 | using namespace std; 3 | void a(){} 4 | int main() 5 | { 6 | int n; 7 | cin>>n; 8 | int a[n],x=0; 9 | for(int i=0;i>a[i]; 11 | for(int j =i;j>=0;j--) 12 | { 13 | if(a[i]>a[j]) x+=a[i]-a[j]; 14 | else x+=a[j]-a[i]; 15 | 16 | } 17 | } 18 | cout< 2 | using namespace std; 3 | int main() 4 | { 5 | int p,q,r,i; 6 | int c; 7 | cin>>c; 8 | for(i=0;i>p>>q>>r; 10 | q=q+(r-1)/5; 11 | r=(r-1)%5+1; 12 | p=p+(q-1)/10; 13 | q=(q-1)%10+1; 14 | cout< 2 | using namespace std; 3 | 4 | int main(){ 5 | int n,i; 6 | cin>>n; 7 | while(n--){ 8 | int sum,a[3],min=11; 9 | for(i=0;i<3;i++) 10 | { 11 | cin>>a[i]; 12 | if(a[i] 2 | #include 3 | using namespace std; 4 | 5 | int main() 6 | { 7 | int n,s,arr[7]={0}; 8 | cin>>n; 9 | for(int i=0;i>s; 11 | int k=7,l; 12 | while(s){ 13 | l=s%10; 14 | arr[k-1]+=l;; 15 | k--; 16 | s=s/10; 17 | } 18 | } 19 | sort(arr,arr+7); 20 | cout< 2 | using namespace std; 3 | 4 | long long n,a[17]; 5 | 6 | int dfs(long long n,int x) 7 | { 8 | int num=n/a[x];n%=a[x]; 9 | if (!n) return num*x; 10 | return num*x+min(x+dfs(a[x]-n,x-1),dfs(n,x-1)); 11 | } 12 | void Init(){ 13 | scanf("%lld",&n); 14 | for (int i=1;i<=16;i++) a[i]=a[i-1]*10+1; 15 | printf("%d\n",dfs(n,16)); 16 | } 17 | int main() 18 | { 19 | Init(); 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Level 1/S-2 Sorting Techniques/TheSapphireConsulting.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | int isqrt(n) int n; { 4 | int i; 5 | for(i=0;i*i2) 22 | s+= (d+j-2)*2; 23 | } 24 | printf("%d liters\n",s); 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Level 1/S-2 Sorting Techniques/TinaOwnsMatchMaking.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | 5 | int main() { 6 | 7 | int n,p; 8 | cin>>p; 9 | while (p--){ 10 | cin>>n; 11 | int b[n],g[n],s=0; 12 | for(int i = 0;i> b[i] ; 13 | for (int i = 0; i < n; i++) cin>> g[i] ; 14 | /* ADSort(g,b, n);*/ 15 | sort(b,b+n,greater()); 16 | sort(g,g+n); 17 | for (int i = 0; i < n; i++) { if(b[i]%g[i]==0)s++; 18 | else if(g[i]%b[i]==0)s++;} 19 | cout< 2 | using namespace std; 3 | int a[10001],n,m,x,y; 4 | int main(){ 5 | cin>>n; 6 | for(int i=0;i<=n;i++) 7 | cin>>m,a[i]=1e9; 8 | for(int i=1;i<=m;i++){ 9 | cin>>x>>x>>y; 10 | a[x]=min(a[x],y); 11 | } 12 | x=y=0; 13 | for(int i=1;i<=n;i++) 14 | if(a[i]!=1e9){ 15 | x++; 16 | y+=a[i]; 17 | } 18 | if(n>ans[0]; cin>>a>>b>>c;"; 22 | } -------------------------------------------------------------------------------- /Level 1/S-4 Greedy Algorithm/TheSpring.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int g[110],cnt[110],n,m,idx; char s[110]; map _hash; 4 | int main() 5 | { 6 | int i; 7 | cin>>n>>m; for(i=1;i<=n;i++) cin>>g[i]; sort(g+1,g+n+1); for(i=1;i<=m;i++) 8 | { 9 | string s; 10 | cin>>s; 11 | if(!_hash.count(s)) _hash[s]=++idx; 12 | cnt[_hash[s]]++; } 13 | sort(cnt+1,cnt+idx+1); reverse(cnt+1,cnt+idx+1); int sum1=0,sum2=0; for(i=1;i<=idx;i++) 14 | { 15 | sum1+=cnt[i]*g[i]; 16 | sum2+=cnt[i]*g[n-i+1]; } 17 | printf("%d %d\n",sum1,sum2); 18 | return 0; } -------------------------------------------------------------------------------- /Level 1/S-1 Searching Techniques/RectangularPlasticSheet.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void solve(){ 4 | cout<<"return(l-2*x)*(b-2*x)*x;"; 5 | } 6 | int main() 7 | { 8 | int tc; 9 | double a, b, c, res, l, w, x; 10 | scanf(" %d", &tc); 11 | while(tc--) { 12 | scanf(" %lf %lf", &l, &w); 13 | a = 12.0; 14 | b = -4.0 * (l+w); 15 | c = l*w; 16 | x = (-b - sqrt (b*b - 4.0*a*c)) / (2.0*a); 17 | res = (l - 2*x) * (w - 2*x) * x; 18 | printf ("%.9f\n", res); 19 | } 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Level 1/S-2 Sorting Techniques/AvulPakir.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | using namespace std; 4 | int main(){ 5 | int n,i,j,t; 6 | cin>>n; 7 | for(i=0;i>t; 10 | int sum=0; 11 | for(j=0;j>name; 15 | if(strcmp(name,"donate")==0){ 16 | int d;cin>>d; 17 | sum+=d; 18 | } 19 | else{cout< 2 | using namespace std; 3 | typedef long long LL; 4 | const int N=(int)1e6+6,mod=(int)0; 5 | int a[N]; 6 | long long sum[N]; 7 | int main(){ 8 | int tc; 9 | cin>>tc; 10 | for(int tt=1;tt<=tc;++tt){ 11 | int n,p; 12 | cin>>n>>p; 13 | for(int j=0;j>a[j]; 15 | sort(a,a+n); 16 | int i; 17 | for(i=0;i 2 | using namespace std; 3 | int sumSquare(int n) 4 | { 5 | int res=0; 6 | for (long i = 0; i * i <= n; i++) 7 | for (long j = i; j * j <= n; j++) 8 | if ((i * i + j * j == n) ) { 9 | res++; 10 | } 11 | return res; 12 | } 13 | int main() 14 | { 15 | int t; 16 | cin>>t; 17 | int i=1; 18 | while(t--){ 19 | int n; 20 | cin>>n; 21 | cout<<"Line #"< 2 | using namespace std; 3 | int sumSquare(int n) 4 | { 5 | int res=0; 6 | for (long i = 0; i * i <= n; i++) 7 | for (long j = i; j * j <= n; j++) 8 | if ((i * i + j * j == n) ) { 9 | res++; 10 | } 11 | return res; 12 | } 13 | int main() 14 | { 15 | int t; 16 | cin>>t; 17 | int i=1; 18 | while(t--){ 19 | int n; 20 | cin>>n; 21 | cout<<"Line #"< 2 | 3 | img_loading 4 | 5 |

6 |

SRM-DAA-E-LAB

7 |


8 | This repo is the collection of DAA E-lab programs which can help you if you got stuck anywhere or want to complete your target. 9 | 10 | You can also contribute to this repo by adding the programs which are not available in it and help your fellow mates🤝. 11 | -------------------------------------------------------------------------------- /Level 1/S-2 Sorting Techniques/GanesanHasAString.cpp: -------------------------------------------------------------------------------- 1 | // Ganesan has a string S consisting of lowercase English letters. 2 | // On this string, he will do the operation below just once. 3 | // First, choose a non-negative integer K. 4 | // Then, shift each character of S to the right by K (see below). 5 | 6 | #include 7 | using namespace std; 8 | int main(){ 9 | string s,t; 10 | cin>>s>>t; 11 | int i,flag = 1; 12 | int check = int(s[0]) - int(t[0]); 13 | for(i=0;i<(int)s.length();i++) 14 | if(int(s[i]) - int(t[i])!= check) 15 | {flag = 0;break;} 16 | if(flag==0) 17 | cout<<"No"; 18 | else 19 | cout<<"Yes"; 20 | return 0; 21 | } -------------------------------------------------------------------------------- /Level 1/S-1 Searching Techniques/TheAllies.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void solve(){ cout<<"break;";} 4 | int main(){ 5 | string s1,s2,s3,s4; 6 | double r; 7 | double h; 8 | cin>>s1>>r>>s2>>s3>>s4; 9 | if(s2=="FEET") 10 | r=r/3.28; 11 | //cout<50)cout<<"OUCH!"; 22 | else if(h<25)cout<<"SPLAT!"; 23 | else cout<<"SUCCESS!"; 24 | return 0;} -------------------------------------------------------------------------------- /Level 1/S-2 Sorting Techniques/RSAisApublicKey.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | void solve(){ 4 | cout<<"break;"; 5 | } 6 | bool isProduct(int num) 7 | { 8 | int cnt = 0; 9 | for (int i = 2; cnt < 2 && i * i <= num; ++i) { 10 | while (num % i == 0) { 11 | num /= i; 12 | ++cnt; 13 | } 14 | } 15 | if (num > 1) 16 | ++cnt; 17 | return cnt == 2; 18 | } 19 | void findNumbers(int N) 20 | { 21 | vector vec; 22 | for (int i = 1; i <= N; i++) { 23 | if (isProduct(i) ) { 24 | vec.push_back(i); 25 | } 26 | } 27 | cout<>t; 33 | while(t--){ 34 | cin>>N; 35 | findNumbers(N); 36 | } 37 | return 0; 38 | } -------------------------------------------------------------------------------- /Level 1/S-1 Searching Techniques/MrSomu.cpp: -------------------------------------------------------------------------------- 1 | // Mr Somu has another problem for Agi today. He has given him three positive integers B, N and R and wants him to calculate the remainder when BẠN! is divided by R. As usual, N! denotes the product of the first N positive integers. 2 | 3 | #include 4 | #include 5 | using namespace std; 6 | int factorial(int n){ 7 | int fact=1; 8 | while(n){ 9 | fact*=n; 10 | n--; 11 | } 12 | return fact; 13 | if(n%2==1); 14 | } 15 | 16 | int main(){ 17 | 18 | int t; 19 | cin>>t; 20 | while(t--){ 21 | int b,n,r; 22 | cin>>b>>n>>r; 23 | n = factorial(n); 24 | b = pow(b,n); 25 | cout< 2 | using namespace std; 3 | #define ll long long 4 | #define ar array 5 | void dummy(){} 6 | int n, k, p, a[50][30]; 7 | int dp[51][1501]; 8 | void solve() { 9 | cin >> n >> k >> p; 10 | memset(dp, 0xc0, sizeof(dp)); 11 | dp[0][0]=0; 12 | for(int i=0; i> a[i][j]; 16 | s+=a[i][j]; 17 | //use j+1 plates 18 | for(int l=0; l+j+1<=p; ++l) 19 | dp[i+1][l+j+1]=max(dp[i][l]+s, dp[i+1][l+j+1]); 20 | } 21 | } 22 | cout << dp[n][p] << "\n"; 23 | } 24 | int main() { 25 | int n, i; 26 | cin >> n; 27 | for(i=0;i 2 | using namespace std; 3 | #define ll long long 4 | #define ar array 5 | void dummy(){} 6 | int n, k, p, a[50][30]; 7 | int dp[51][1501]; 8 | void solve() { 9 | cin >> n >> k >> p; 10 | memset(dp, 0xc0, sizeof(dp)); 11 | dp[0][0]=0; 12 | for(int i=0; i> a[i][j]; 16 | s+=a[i][j]; 17 | //use j+1 plates 18 | for(int l=0; l+j+1<=p; ++l) 19 | dp[i+1][l+j+1]=max(dp[i][l]+s, dp[i+1][l+j+1]); 20 | } 21 | } 22 | cout << dp[n][p] << "\n"; 23 | } 24 | int main() { 25 | int n, i; 26 | cin >> n; 27 | for(i=0;i 2 | using namespace std; 3 | const int N = 100005; 4 | int R,D,n,m,d,h[N]; 5 | vector adj[N]; 6 | bool prob[N],is[N]; 7 | void evil(int u,int p=0){ 8 | h[u]= h[p]+1; 9 | prob[u] &= (h[u]<=d); 10 | if(is[u]&&h[u]>D) 11 | D=h[u],R=u; 12 | for(unsigned int i=0;i>n>>m>>d;memset(prob,true,sizeof(prob)); 20 | h[0]=-1;int a,b,i;D=0; 21 | for(i=0;i>R,is[R]=true; 23 | for(i=0;i 4 | void fun(){ 5 | int n; char s[109]; 6 | scanf("%d",&n); 7 | int cb=-1,f=0,i=0; 8 | //for(i=0;i<=n;i++) 9 | while(i<=n){ 10 | scanf("%c",&s[i++]); 11 | } 12 | //for(j=0;j<=n;j++) 13 | i=0; 14 | while(i<=n){ 15 | if(s[i]=='(') 16 | f++; 17 | else{ 18 | if(f==0) 19 | cb++; 20 | else 21 | f--; 22 | } 23 | i++; 24 | } 25 | printf("%d\n",cb); 26 | } 27 | int main(){ 28 | int t; 29 | scanf("%d", &t); 30 | while(t--){ 31 | fun(); 32 | } 33 | return 0; 34 | } -------------------------------------------------------------------------------- /Level 1/S-2 Sorting Techniques/AceVentura.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | #define p1 cout<<"Ace, move fast, pigeon is at ("<>s1; 11 | int z=s1.size(); 12 | f(n){ 13 | if(s1[i]=='P'){ p1 14 | return 0;} 15 | } 16 | //cout< 2 | using namespace std; 3 | int factors(int num,int l) { 4 | int i,c1=0; 5 | for(i=1; i <= num; i++) { 6 | if (num % i == 0 && i>l) c1++;} return c1; cout<<"continue;";} 7 | int main() 8 | { 9 | int t,j; 10 | cin>>t; 11 | for(j=1;j<=t;j++) 12 | { 13 | int p,l,q,i,c=0,sp; 14 | cin>>p>>l; 15 | q=p-l; 16 | 17 | printf("Line %d: ",j); 18 | sp=factors(q,l); 19 | for(i=1;i<=q;i++) 20 | { 21 | if(q%i==0 && i>l) 22 | { 23 | printf("%d",i); 24 | if(c 2 | using namespace std; 3 | 4 | bool ans(int n,string s){ 5 | int f=0; 6 | for(int i=0;i>T; 30 | while(T--){ 31 | int n; 32 | string s; 33 | cin>>n>>s; 34 | if(ans(n,s)) 35 | cout<<"Valid\n"; 36 | else 37 | cout<<"Invalid\n"; 38 | 39 | } 40 | } -------------------------------------------------------------------------------- /Level 1/S-5 Dynamic Programming/YouHaveInfiniteCards.cpp: -------------------------------------------------------------------------------- 1 | // You have infinite cards for each number between 1 and N (inclusive of them). Your task is to select three integers such that after sorting them in ascending order, the difference between the adjacent number is less than or equal to two. Find the number of ways to choose three numbers and print them. 2 | // Note: The order of numbers does not matter. 3 | 4 | 5 | #include 6 | using namespace std; 7 | using ll = long long int; 8 | int main() { 9 | ios_base::sync_with_stdio(false); 10 | cin.tie(NULL); 11 | cout.tie(NULL); 12 | //preSum(); 13 | ll t; 14 | cin>>t; 15 | while(t--){ 16 | ll n; 17 | cin>>n; 18 | if(n==1) 19 | printf("1\n"); 20 | else if(n==2) 21 | printf("4\n"); 22 | else if(n==3) 23 | printf("10\n"); 24 | else 25 | printf("%lld\n",9*n-18); 26 | } 27 | } -------------------------------------------------------------------------------- /Level 1/S-7 Graph Colouring/DanikaGottenAn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | using namespace std; 5 | #define dep(i,n)for(int i=0;i<(n);i++) 6 | int const N=70; 7 | int dx[]={0,0,1,-1}; 8 | int dy[]={1,-1,0,0}; 9 | char s[N][N]; 10 | int vis[N][N]; 11 | int n,m; 12 | int squares(int x,int y){ 13 | if(s[x][y]!='#'||vis[x][y])return 0; 14 | vis[x][y]=1; 15 | dep(i,4)squares(x+dx[i],y+dy[i]); 16 | return 1;} 17 | 18 | int main(){ 19 | cin>>n>>m; 20 | dep(i,n)scanf("%s",s[i]); 21 | int cnt=0; 22 | dep(i,n)dep(j,m){ 23 | if(s[i][j]=='.')continue; 24 | cnt++;s[i][j]='.'; 25 | int k=0;memset(vis,0,sizeof(vis)); 26 | dep(d,4)k+=squares(i+dx[d],j+dy[d]); 27 | if(k>1){puts("1");return 0; 28 | 29 | }s[i][j]='#';} 30 | printf("%d\n",cnt>2?2:-1); 31 | 32 | } -------------------------------------------------------------------------------- /Level 1/S-2 Sorting Techniques/Sivahas.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | void insertionSort(long int *p,long int n); 4 | void asd(); 5 | int main(){ 6 | asd(); 7 | return 0; 8 | } 9 | void asd() 10 | 11 | { 12 | int q; 13 | scanf("%d",&q); 14 | while(q--){ 15 | int n,i,j; 16 | scanf("%d",&n); 17 | int M[n][n]; 18 | long int *r,*c,*arr; 19 | arr=(long int *)malloc(n*n*sizeof(long int)); 20 | *arr=n; 21 | r=(long int *)malloc(n*sizeof(long int)); c=(long int *)malloc(n*sizeof(long int)); 22 | for(i=0;i 2 | using namespace std; 3 | #define ggg int find(int p) unite(int p,int q) cin>>u>>v; 4 | int i,j,x,y,n,m,ans,f[502],a[502]; 5 | int find(int x){return (f[x]==x)?x:f[x]=find(f[x]);} 6 | int main (){ 7 | scanf("%d %d",&n,&m); 8 | if (m>n) return printf("NO\n"),0; 9 | for (i=1;i<=n;i++) f[i]=i; 10 | for (i=1;i<=m;i++){ 11 | scanf("%d %d",&x,&y);a[x]++;a[y]++; 12 | if (find(x)==find(y)&&i!=n) return printf("NO\n"),0; 13 | f[find(x)]=find(y); 14 | } 15 | for (i=1;i<=n;i++) 16 | if (a[i]>2) 17 | return printf("NO\n"),0; 18 | printf("YES\n%d\n",n-m); 19 | for (i=1;i<=n;i++) 20 | while (a[i]<2){ 21 | ans=i+(n!=1); 22 | for (j=ans;j<=n;j++) 23 | if (a[j]<2&&(n<=2||m+1==n||find(i)!=find(j))) 24 | {printf("%d %d\n",i,j);m++;a[i]++;a[j]++;f[find(i)]=find(j);break;} 25 | } 26 | return 0; 27 | } -------------------------------------------------------------------------------- /Level 1/S-6 Backtracking/FatalEagle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | long long int dp[213][213]; 5 | 6 | long long int options (long long int n, long long int k) { 7 | if (dp[n][k] >=0) 8 | return dp[n][k]; 9 | if (n 11 | using namespace std; 12 | int n,l,z; 13 | pair a[500020]; 14 | int main(){ 15 | cin>>n; 16 | for(int i=0;i>a[i].second>>a[i].first; 18 | } 19 | sort(a,a+n); 20 | for(int i=0;i 5 | using namespace std; 6 | int main() 7 | { 8 | int i,n,t,c=0; 9 | cin>>n>>t; 10 | int a[n]; 11 | for(i=0;i>a[i]; 13 | 14 | for(i=0;i=0) 17 | c++; 18 | else 19 | break; 20 | } 21 | cout< Alaaf 2 | 3 | 4 | #include 5 | #include 6 | using namespace std; 7 | const int N=1<<20; 8 | int n,a[N],c[N],w; 9 | void upd(int i,int c){ 10 | } 11 | int main(){ 12 | cin>>n; 13 | for(int i=0;i>a[i]; 14 | mapu,v; 15 | for(int i=n;i-->0;){ 16 | int x=++u[a[i]]; 17 | while(x0)w+=c[y],y-=y&-y; 23 | } 24 | cout< 7 | using namespace std; 8 | int main(){ 9 | int t,line=0; 10 | cin>>t; 11 | while(t--){ 12 | cout<<"Line "<<++line<<":\n"; 13 | int n,i; 14 | cin>>n; 15 | char s[6]; 16 | int total=0,donate; 17 | for(i=0;i>s; 19 | if(s[0]=='d'){ 20 | cin>>donate; 21 | total+=donate; 22 | } 23 | else 24 | cout< 8 | using namespace std; 9 | int main() 10 | { 11 | int w,h,m; 12 | int i; 13 | cin>>w>>h>>m; 14 | /////////////// 15 | //Test case 3 16 | if(h==0){ 17 | for(i=1;i<=w;i++) 18 | cout<<"#"<w) 28 | return 0; 29 | //Power 30 | for(i=0;i 2 | #include 3 | char s[101]; 4 | 5 | int fun(){ 6 | int n=strlen(s); 7 | int i=0; 8 | while(i 8 | using namespace std; 9 | int main() 10 | { 11 | string s1,s2,s3,s4,s5,s6; 12 | cin>>s1>>s2>>s3>>s4>>s5>>s6; 13 | float a,b,c; 14 | if(s2=="?"){ 15 | b=stof(s4); 16 | c=stof(s6); 17 | //cout< 10 | using namespace std; 11 | #define res cin>>a[i],num+=a[i]; 12 | #define f1 for(int i=1;i<=n;i++) 13 | double n,v,a[25],b[25],sum,mx=1e9; 14 | int main(){ 15 | cin>>n>>v; 16 | f1{ 17 | cin>>a[i]; 18 | sum+=a[i]; 19 | } 20 | for(int i=1;i<=n;i++) 21 | cin>>b[i]; 22 | for(int i=1;i<=n;i++) 23 | mx=min(mx,b[i]/a[i]); 24 | cout << fixed< 2 | #include 3 | #define f1 for(i=0;i> n >> m >> K; 14 | long long int a[n],b[n],c[n]; 15 | long long int i,j,l; 16 | int p,T = 0; 17 | for(i = 0; i < n; i++) 18 | cin >> a[i] >> b[i] >> c[i]; 19 | long long int lx,rx,ly,ry,lz,rz; 20 | cin >> lx >> rx; 21 | cin >> ly >> ry; 22 | cin >> lz >> rz; 23 | for(i = lx; i < min(rx, lx + m); i++){ 24 | for(j = ly; j < min(ry,ly + m);j++){ 25 | for(l = lz;l < min(rz,lz + m);l++){ 26 | T=0; 27 | for(p = 0; p < n; p++){ 28 | if((a[p] * i + b[p] * j- c[p] * l) % m == 0) 29 | T++; 30 | } 31 | if(T==K) 32 | break; 33 | } 34 | if(l < min(rz,lz + m)) 35 | break; 36 | } 37 | if(j < min(ry,ly + m)) 38 | break; 39 | 40 | } 41 | if(i < min(rx, lx + m)){ 42 | cout << i << " " << j << " " << l; 43 | } 44 | else 45 | cout << "-1" << endl; 46 | } -------------------------------------------------------------------------------- /Level 1/S-5 Dynamic Programming/ProfessorWIki.cpp: -------------------------------------------------------------------------------- 1 | // Professor Wiki has performed some experiments on rays. The setup for n rays is as follows. 2 | // There is a rectangular box having exactly n holes on the opposite faces. 3 | // All rays enter from the holes of the first side and exit from the holes of the other side of the box. 4 | // Exactly one ray can enter or exit from each hole. The holes are in a straight line. 5 | // Professor Wiki is showing his experiment to his students. He shows that there are cases, when all the rays are intersected by every other ray. 6 | // A curious student asked the professor: "Sir, there are some groups of rays such that all rays in that group intersect every other ray in that group. 7 | // Can we determine the number of rays in the largest of such groups?". 8 | // Professor Wiki now is in trouble and knowing your intellect he asks you to help him. 9 | 10 | #include 11 | using namespace std; 12 | int n,x,i; 13 | int a[1000020]; 14 | int p[1000020]; 15 | int f[1000020]; 16 | int main() 17 | { 18 | cin>>n; 19 | for(i=0;i>x; 22 | p[x]=i; 23 | } 24 | for(i=0;i 7 | using namespace std; 8 | string word; 9 | long long dp[100][100]; 10 | long long calculate(int s, int e){ 11 | if(s > e) 12 | return 0; 13 | if(s == e ) 14 | return 1; 15 | if(dp[s][e] != -1) 16 | return dp[s][e]; 17 | if(word[s] == word[e]) 18 | return dp[s][e] = 1 + calculate(s+1, e) + calculate(s, e-1); 19 | else 20 | return dp[s][e] = calculate(s+1, e) + calculate(s, e-1) - calculate(s+1, e-1); 21 | } 22 | int main(){ 23 | cin>>word; 24 | memset(dp, -1, sizeof dp); 25 | cout< 4 | using namespace std; 5 | #define ll long long 6 | #define sky LONG_LONG_MAX 7 | #define ajen LONG_LONG_MIN 8 | #define mod 1000000007 9 | void hi(){ 10 | cout<<"for(i=0;i>t; 16 | while(t--){ 17 | ll n,k; cin>>n>>k; 18 | ll a[k][2]; 19 | for(int i = 0; i>x; 24 | x--; 25 | a[x][0] = min(a[x][0],(ll)i); 26 | a[x][1] = i; 27 | } 28 | ll dp[k][2]; 29 | for(int i = 0; i 4 | #include 5 | using namespace std; 6 | void chu(){ 7 | while(0){ 8 | break; 9 | } 10 | } 11 | int main(){ 12 | 13 | for(int i=0;i<4;i++){ 14 | string a,b; 15 | cin>>a>>b; 16 | if(a==b) 17 | {cout< 8 | using namespace std; 9 | int N; 10 | int a[200010], b[200010]; 11 | int main() 12 | { 13 | int i, j; 14 | cin>>N; 15 | for(i=0;i>a[i]; 18 | if(a[i]==0) i--; 19 | } 20 | 21 | for(i=0;i>n;cin>>b[i];"; 37 | } -------------------------------------------------------------------------------- /Level 1/S-3 Divide And Conquer/RecentlyAarush.cpp: -------------------------------------------------------------------------------- 1 | // Recently Aarush has become keen on physics. Anna V., his teacher noticed Aarush's interest and gave him a fascinating physical puzzle a half-decay tree. 2 | // A half-decay tree is a complete binary tree with the height h. The height of a tree is the length of the path (in edges) from the root to a leaf in the tree. While studying the tree Aarush can add electrons to vertices or induce random decay with synchrophasotron. 3 | // Random decay is a process during which the edges of some path from the root to the random leaf of the tree are deleted. All the leaves are equiprobable. As the half-decay tree is the school property, Aarush will return back the deleted edges into the tree after each decay. 4 | // After being disintegrated, the tree decomposes into connected components. Charge of each component is the total quantity of electrons placed in vertices of the component. Potential of disintegrated tree is the maximum from the charges of its connected components. Each time before inducing random decay Aarush is curious about the mathematical expectation of potential of the tree after being disintegrated. 5 | 6 | #include 7 | using namespace std; 8 | int h,q,v,e;string str;map f; 9 | double puzzle(int u,int mx) {return (f[u]<=mx)?mx:(0.5*(puzzle(u<<1,max(mx,f[u]- 10 | f[u<<1]))+puzzle(u<<1|1,max(mx,f[u]-f[u<<1|1]))));} 11 | int main(){ 12 | cin>>h>>q; 13 | while (q--){ 14 | cin>>str; 15 | if (str[0]=='a'){ 16 | scanf("%d %d",&v,&e); 17 | while (v) f[v]+=e,v>>=1; 18 | } 19 | else printf("%.2lf\n",puzzle(1,0)); 20 | } 21 | return 0; 22 | } -------------------------------------------------------------------------------- /Level 1/S-5 Dynamic Programming/SammyHasBought.cpp: -------------------------------------------------------------------------------- 1 | // Samy has bought a box of chocolate and has brought them to Anand house. There is a random number on each chocolate. They decided to play a game with them. 2 | // They arranged them randomly in a row. They each start from one end of the row (Samy starts from left and Anand from right). They can only move towards each other, and in each step, they can move at most k chocolate. More specifically Samy can go from the ith place to one of i+1, i+2, ..., i+k and Anand can go from ith place to i−1, i–2, . i-k. 3 | // *** 4 | // They can only eat the chocolate they are on if their number is the same. The game ends if they can eat any chocolate. Their goal is to eat the last chocolate together. 5 | 6 | #include 7 | int function(int arr[],int i,int j,int memo[][1001],int k) 8 | { 9 | if(i>j) 10 | return 0; 11 | if(arr[i]!=arr[j]) 12 | return 0; 13 | if(i==j) 14 | return 1; 15 | if(memo[i][j]!=-1) 16 | return memo[i][j]; 17 | else 18 | { 19 | int answer=0; 20 | for(int p=1;p<=k;p++) 21 | { 22 | for(int q=1;q<=k;q++) 23 | { 24 | answer+=function(arr,i+p,j-q,memo,k); 25 | } 26 | } 27 | if(answer!=0) 28 | answer=1; 29 | memo[i][j]=answer; 30 | return answer; 31 | } 32 | } 33 | int main() 34 | { 35 | int n,k; 36 | scanf("%d%d",&n,&k); 37 | int j,arr[n+1]; 38 | for(j=1;j<=n;j++) 39 | scanf("%d",&arr[j]); 40 | int memo[1001][1001]; 41 | // int answer=0; 42 | int i; 43 | for(i=0;i<=1000;i++) 44 | { 45 | for(j=0;j<=1000;j++) 46 | { 47 | memo[i][j]=-1; 48 | } 49 | } 50 | int answer=function(arr,1,n,memo,k); 51 | if(answer==0) 52 | printf("NO\n"); 53 | else 54 | printf("YES\n"); 55 | } -------------------------------------------------------------------------------- /Level 1/S-4 Greedy Algorithm/AGroupOfTourist.cpp: -------------------------------------------------------------------------------- 1 | // A group of tourists is going on to rameshwaram and dhanushkodi tour. A rented bus has arrived at the boat depot to take rameshwaram and dhanushkodi's to the point of departure. 2 | // It's known that all rameshwaram are of the same size (and each of them occupies the space of 1 cubic metre), and all dhanushkodi's are of the same size, but two times bigger than rameshwaram (and occupy the space of 2 cubic metres) 3 | // Each waterborne vehicle has a particular carrying capacity, and it should be noted that waterborne vehicles that look the same can have different carrying capacities. 4 | // Knowing the bus body volume and the list of waterborne vehicles in the boat depot (for each one its type and carrying capacity are known), find out such a set of vehicles that can be taken in the lorry, and that has the maximum total carrying capacity. 5 | // The bus body volume of the lorry can be used effectively, that is to say, you can always put into the lorry a waterborne vehicle that occupies the space not exceeding the free space left in the bus body. 6 | 7 | 8 | 9 | #include 10 | using namespace std; 11 | int c[2],i,x,t,n,p,j; 12 | pair a[2][1<<17]; 13 | #define F(i,n) for(i=0;i>n>>v;cin>>t>>v;"; 16 | } 17 | int main(){ 18 | scanf("%d%d",&n,&p); 19 | F(i,n){ 20 | scanf("%d%d",&t,&j); 21 | a[t&1][++c[t&1]]=make_pair(-j,i+1); 22 | } 23 | F(i,2)sort(a[i]+1,a[i]+c[i]+1); 24 | F(i,2)F(j,c[i])a[i][j+1].first+=a[i][j].first; 25 | n=min(p,c[1]); 26 | for(i=0;~n;--n) 27 | if((t=a[1][n].first+a[0][min(*c,(p-n)/2)].first) 2 | using namespace std; 3 | int partition(int array[],int leftIndex,int rightIndex){ 4 | int pivotValue = array[rightIndex]; 5 | int toBePivotIndex = (leftIndex - 1); 6 | for(int comparisonIndex = leftIndex; comparisonIndex <= rightIndex - 1; 7 | comparisonIndex++){ 8 | if ( array[comparisonIndex] < pivotValue) { 9 | 10 | toBePivotIndex++; 11 | int temp = array[toBePivotIndex]; 12 | array[toBePivotIndex] = array[comparisonIndex]; 13 | array[comparisonIndex] = temp; 14 | } 15 | } 16 | 17 | int temp = array[toBePivotIndex+1]; 18 | array[toBePivotIndex+1] = array[rightIndex]; 19 | array[rightIndex] = temp; 20 | return (toBePivotIndex + 1); // new pivot point 21 | } 22 | void quickSort(int array[],int leftIndex,int rightIndex){ 23 | 24 | if (leftIndex < rightIndex) { 25 | int partitionIndex = partition(array, leftIndex, rightIndex); 26 | quickSort(array, leftIndex, partitionIndex - 1); 27 | quickSort(array, partitionIndex + 1, rightIndex); 28 | } 29 | } 30 | int main(){ 31 | int numberOfDustPoints,widthOfBrush,xCoordinate,yCoordinate; 32 | int numberOfMoves = 0; 33 | cin>>numberOfDustPoints>>widthOfBrush; 34 | int dustPointsYCoordinates[numberOfDustPoints]; 35 | for(int i = 0; i < numberOfDustPoints; i++){ 36 | cin >> xCoordinate >> yCoordinate; 37 | dustPointsYCoordinates[i] = yCoordinate; 38 | } 39 | 40 | quickSort(dustPointsYCoordinates,0, numberOfDustPoints-1); 41 | 42 | int currentBrushYCoordinate = dustPointsYCoordinates[0]; 43 | numberOfMoves++; 44 | for (int i = 0; i < numberOfDustPoints; i++) { 45 | if(currentBrushYCoordinate + widthOfBrush < dustPointsYCoordinates[i]) { 46 | currentBrushYCoordinate = dustPointsYCoordinates[i]; 47 | numberOfMoves++; 48 | } 49 | } 50 | cout < 6 | using namespace std; 7 | 8 | int max(int a,int b){ 9 | return 0; 10 | } 11 | int min(int a,int b){ 12 | return 0; 13 | } 14 | 15 | int ispositive(int* p,int* r,int n,int k){ 16 | int ret=0; 17 | for(int i=0;i=k&&p[i]-r[i]<=k) 21 | ret++; 22 | return ret; 23 | } 24 | int isnegative(int* p,int* r,int n,int k){ 25 | int ret=0; 26 | for(int i=0;i0) 28 | continue; 29 | else if(p[i]-r[i]<=k&&p[i]+r[i]>=k) 30 | ret++; 31 | return ret; 32 | } 33 | void fun(){ 34 | int n,q; 35 | cin>>n>>q; 36 | int pos[n],ran[n],k[q]; 37 | for(int i=0;i>pos[i]; 39 | for(int i=0;i>ran[i]; 41 | for(int i=0;i>k[i]; 43 | for(int i=0;i0) 45 | cout<>t; 56 | while(t--){ 57 | fun(); 58 | } 59 | return 0; 60 | } -------------------------------------------------------------------------------- /Level 1/S-5 Dynamic Programming/BobGoes.cpp: -------------------------------------------------------------------------------- 1 | // Bob goes to the fruit shop to buy apples. There are N apples numbered from 1 to N where the vitamin value of the ith apple is Vi and the price of the ith apple is Pi. 2 | // He wants to buy apples such that the sum of the price does not exceed M. He has one special magic spell. By using it, he can halve the price (floor value) of any apple present in a shop. He can use this spell at most one time. 3 | // Your task is to find the maximum vitamin Bob can get. 4 | // Input format 5 | // • The first line contains an integer I denoting the number of test cases. • The first line of each test case contains two space-separated integers N and M. • The next N lines contain two space-separated values Vi and Pi. 6 | // Output format 7 | // For each test case, the only line must contain an integer denoting the maximum vitamin Bob can get. 8 | 9 | 10 | 11 | #include 12 | #include 13 | using namespace std; 14 | void copy(int* v,int* p,int* vit,int* pri,int n){ 15 | for(int i=0;ip[j]){ 23 | tmp = p[i]; 24 | p[i] = p[j]; 25 | p[j] = tmp; 26 | 27 | tmp = v[i]; 28 | v[i] = v[j]; 29 | v[j] = tmp; 30 | }}} 31 | } 32 | 33 | int findMaxVit(int* v,int* p,int n,int m){ 34 | int maxVit=0,maxPri=0; 35 | for(int i=0;i>t; 58 | while(t--){ 59 | int n,m;cin>>n>>m; 60 | int v[n],p[n]; 61 | for(int i=0;i>v[i]>>p[i]; 63 | cout< 10 | using namespace std; 11 | int main() 12 | { 13 | int numberOfColumns; 14 | cin>>numberOfColumns; 15 | int bananaMatrix[2 * numberOfColumns - 1][numberOfColumns]; //Input matrix 16 | int maxBanana[2 * numberOfColumns - 1][numberOfColumns]; //Memoized matrix 17 | memset(maxBanana, 0, sizeof(maxBanana)); //Setting 0 to all cell, will update for maximum 18 | memset(bananaMatrix, 0, sizeof(bananaMatrix)); //Setting 0 to all cell, will update for inputs 19 | //Input for upper triangle 20 | for (int row = 0; row < numberOfColumns; row++) 21 | for (int column = 0; column <= row; column++) 22 | cin >> bananaMatrix[row][column]; 23 | //Input for lower triangle 24 | int shiftedPosition = 1; 25 | for (int row = numberOfColumns; row < (numberOfColumns * 2) - 1; row++) 26 | { 27 | for (int column = shiftedPosition; column < numberOfColumns; column++) 28 | cin >> bananaMatrix[row][column]; 29 | shiftedPosition++; 30 | } 31 | maxBanana[0][0] = bananaMatrix[0][0]; 32 | for (int row = 1; row < numberOfColumns; row++) 33 | { 34 | for (int column = 0; column <= row; column++) 35 | if (column == 0)//Caution for negative indexes. 36 | maxBanana[row][column] = maxBanana[row - 1][column] + 37 | bananaMatrix[row][column]; 38 | else 39 | maxBanana[row][column] = max(maxBanana[row - 1][column], maxBanana[row 40 | - 1][column - 1]) + bananaMatrix[row][column]; 41 | } 42 | //Memoizing the lower triangle to store the max value 43 | shiftedPosition = 1; 44 | for (int row = numberOfColumns; row < (numberOfColumns * 2) - 1; row++) 45 | { 46 | for (int column = shiftedPosition; column < numberOfColumns; column++) 47 | maxBanana[row][column] = max(maxBanana[row - 1][column], maxBanana[row - 48 | 1][column - 1]) + bananaMatrix[row][column]; 49 | shiftedPosition++; 50 | } 51 | cout <>carrotMatrix[row][column];"; 55 | } --------------------------------------------------------------------------------