Admin – Learn2Code https://learn2code.musitrature.com Learn to code : A programmers resource Mon, 13 Apr 2020 13:30:52 +0000 en-US hourly 1 https://wordpress.org/?v=6.5.3 https://learn2code.musitrature.com/wp-content/uploads/2020/03/favicon-2.ico Admin – Learn2Code https://learn2code.musitrature.com 32 32 174224216 Codeforces Angry Students – 1287A solution https://learn2code.musitrature.com/codeforces-angry-students-1287a-solution/?utm_source=rss&utm_medium=rss&utm_campaign=codeforces-angry-students-1287a-solution Sun, 29 Mar 2020 09:43:46 +0000 https://learn2code.musitrature.com/?p=308 Read More »Codeforces Angry Students – 1287A solution]]> This problem has denoted a row of students by a string. Where the string contains 2 char ‘A’ and ‘P’ denoting Angry and patients students respectively.

Now, the main point every angry student in a row throws a snowball every minute to its next student. Simply put, if a row is like this :

As mentioned before each minute any angry students ‘A’ will throw snow to the next student and if it was patient ‘P‘, will make him/her angry.e.g. ‘P’ to ‘A’. Exactly like this:

1st minute:

2nd minute:

3rd minute:

After 3rd minute:

As you can see we have to find the substring containing the longest sequence of ‘P’ because the longest consecutive ‘P’ can be changed to a sequence of ‘A’.But you can do that only if:

  • The string must have ‘P’ and ‘A’ both characters.
  • The longest sequence of ‘P’ starts with ‘A’. So, each iteration we can change the next ‘P’ to ‘A’.
#include<bits/stdc++.h>
using namespace std;
int main() {
    int tc,n,cnt,tmp_cnt;
    cin >> tc;
    while(tc--) {
        cnt = tmp_cnt = 0;
        cin >> n;
        char stu_row[n];
        cin >> stu_row;
        for(int i = 0; i<strlen(stu_row); i++) {
            if(stu_row[i] == 'A') {
                for(int j = i+1; j<=strlen(stu_row); j++) {
                    if(stu_row[j] == 'P')
                        ++tmp_cnt;
                    else if(stu_row[j] == 'A') {
                        break;
                    }
                }
                if(tmp_cnt > cnt) {
                    cnt = tmp_cnt;
                }
                tmp_cnt = 0;
            }
        }
        cout << cnt << endl;
    }
}

]]>
308
Codeforces 1220 – A. Cards solution https://learn2code.musitrature.com/codeforces-1220-a-cards-solution/?utm_source=rss&utm_medium=rss&utm_campaign=codeforces-1220-a-cards-solution Wed, 25 Mar 2020 09:51:36 +0000 https://learn2code.musitrature.com/?p=302 Read More »Codeforces 1220 – A. Cards solution]]>

This problem is very simple. First You have to input an integer n. Then input a randomized string of n consisting of these five English lowercase letters: ‘z’, ‘e’, ‘r’, ‘o’ and ‘n’.

Now the problem said you have to sort/restore the randomized/shuffled string and It is guaranteed that it is possible to rearrange the letters in such a way that they form a sequence of words, each being either “zero” which corresponds to the digit 0 or “one” which corresponds to the digit 1.

Here is where the problem gets tricky. If you think hard and try to find each “one” and “zero”s from the rearranged string you may need to make a complicated code and possibly a timeout error.

Instead, we can solve this pretty easily. If you analyse carefully: Out of the five characters that formed the string, between the two words “zero” and “one” z,r are unique to the word “zero” and “n” is unique to the word “one” and occurs only once.

So, If we count the number of “z” or “r” we can get the total number of “zero” in the string.
Similarly, If we count “n” we will get the total occurrence of “one”.

Now that we have found the total number of “one” and “zeros” we have to print the maximum binary number possible of them. Which is easy. Print all the ‘1’ first then “0”

#include<bits/stdc++.h>
using namespace std;
int main() {
    int n,one = 0, zero = 0;
    cin >> n;
    char str[n];
    cin >> str;
    for(int i = 0; i<strlen(str); i++) {
        if(str[i] == 'z')
            ++zero;
        else if(str[i] == 'n')
            ++one;
    }
    while(one--) cout << "1 ";
    while(zero--) cout << "0 ";
}

]]>
302
Uri 1024 – Encryption solution https://learn2code.musitrature.com/uri-1024-encryption-solution-c-cpp/?utm_source=rss&utm_medium=rss&utm_campaign=uri-1024-encryption-solution-c-cpp Sat, 21 Mar 2020 11:47:29 +0000 https://learn2code.musitrature.com/?p=286 Read More »Uri 1024 – Encryption solution]]> This is a basic string manipulation problem to create a basic encryption program.

First of all, we see from the given inputs that inputs can be a sentence aka. more than one word in an input. So, we can’t use scanf, cin or any other formatted input functions of c/c++ as those will only take input until space is encountered. Instead, we can use getline function.

The first point of the problem is we have to right shift every alphabets 3 positions to the right. Ex: ‘a’ will be ‘d’, ‘b’ will be ‘e’ etc. This is very easy.

All the characters in c have their own integer value. The characters used in C are stored in a table called the ASCII table. And a specific character’s int value is actually the position of that character in the ASCII table. So, we can easily shift to any number of positions by adding that number to that character. Ex: ‘a’ + 3 = ‘d’.

Now, the second point is to reverse the string. Which is very easy. We can use the built-in C++ library function reverse().

Again we have to shift the modified string like before. But we have to only do that with the latter half of the string. Use this formula if the string length is even then half is length/2 else length/2 + 1.

Then do shifting to the truncated/half of the string. This time left shift by 1. So, like the previous step instead of adding 3 just subtract the char by one this time. Remember this time all characters must be shifted, not only alphabets, unlike the first step.
That is all.

#include<iostream>
#include<string>
#include<algorithm>
#include<stdio.h>
using namespace std;
int main() {
    int t_case;
    string x;
    cin >> t_case;
    getchar();
    while(t_case--) {
        getline(cin,x);
        for(int i = 0; i<x.length(); i++) {
            if((x[i]>='A'&& x[i]<='Z')||(x[i]>='a' && x[i]<='z')) {
                x[i] += 3;
            }
        }
        reverse(x.begin(),x.end());
        if(x.length()%2!=0) {
            for(int i = (x.length()-1)/2; i < x.length(); i++) {
                x[i] -=1;
            }
        } else {
            for(int i = x.length()/2; i < x.length(); i++) {
                x[i] -=1;
            }
        }
        cout << x << endl;
    }

    return 0;
}

]]>
286
Program to check neon number https://learn2code.musitrature.com/c-program-to-check-neon-number/?utm_source=rss&utm_medium=rss&utm_campaign=c-program-to-check-neon-number Wed, 18 Mar 2020 11:55:02 +0000 https://learn2code.musitrature.com/?p=262 Read More »Program to check neon number]]> Neon Numbers are numbers that are the same as the sum of all digits of its squared number.
e.g: x2 = a+b+c…+n where (a,b,c) are all digits of x2.

For example, 9 is a neon number. Because



While 4 isn’t a neon number because of 42 = 16.And the sum of its digits is 1 + 6 = 7.And 7 not equals 4.

#include<iostream>
using namespace std;
int main(){
int inputNumber,square,rem = 0;
cin >> inputNumber;
square = inputNumber * inputNumber;
int sum = 0;

while(square!=0){
 rem = square % 10;
 sum += rem;
 square /= 10;
}

if(inputNumber == sum )
 cout << inputNumber << " is a neon number." << endl;
else
 cout << inputNumber << " is not a neon number." << endl;
}

So, you can see that first, we squared inputNumber and then put that in int variable square. Then we used the algorithm of the sum of digits of a number. Now if the sum is the same then the number will be a neon number or else not.

Checking neon numbers in a specific range is easy. We just have to use a nested loop and then call the above snippet of code we used to check neon numbers. The only change in the above code will be the inputNumber will be the index of the first loop.

#include<iostream>
using namespace std;
int main() {
    int a,b,inputNumber,square;

    cin >> a >> b;
    for(a; a<=b; a++) {
        square = a * a;
        int sum = 0,rem = 0;

        while(square!=0) {
            rem = square % 10;
            sum += rem;
            square /= 10;
        }
        if(a == sum )
            cout << a << " " << endl;
    }
}
]]>
262
URI|1036 Bhaskara’s Formula solution https://learn2code.musitrature.com/uri1036-bhaskaras-formula-solution/?utm_source=rss&utm_medium=rss&utm_campaign=uri1036-bhaskaras-formula-solution Sat, 14 Mar 2020 17:12:56 +0000 http://learn2code.musitrature.com/?p=232 Read More »URI|1036 Bhaskara’s Formula solution]]>

Well, the problem says it all. You need to calculate the root of Bhaskara,s Formula.

Don’t know Bhaskara’s Formula? It is actually Algebra’s Quadratic Formula.

The Quadratic/ Bhaskaras formula is : ax2 + bx + c .

And Quadratic Formula’s root is :

So, That’s it. We have to just print the positive and negative value of the quadratic formula.

#include<sdtio.h>
#include<math.h>
int main(){
 double a,b,c;
 scanf("%lf %lf %lf",&a,&b,&c);
 if(a> 0 && (pow(b,2)-(4*a*c))>0){
   double r1 = (-b + sqrt(pow(b,2) - (4*a*c)) ) / (2*a);
   double r2 =  (-b - sqrt(pow(b,2) - (4*a*c)) ) / (2*a);
   printf("R1 = %.5lf\nR2 = %.5lf\n",r1,r2);
 }else{
   printf("Impossivel calcular\n");
 } 
 return 0;
 } 

]]>
232
Codeforces 266b solution: Queue at the School https://learn2code.musitrature.com/codeforces-266b-solution-queue-at-school/?utm_source=rss&utm_medium=rss&utm_campaign=codeforces-266b-solution-queue-at-school Sat, 14 Mar 2020 09:32:32 +0000 http://learn2code.musitrature.com/?p=210 Read More »Codeforces 266b solution: Queue at the School]]> problem link

This problem’s main point is each second a boy will let every girl swap positions if a girl is behind the boy. This happens for all the girls and boys who meet the same condition.

5 1

BGBGB

Here, if you look at sample input you will see a single string containing only ‘B’ or ‘G’ letters denoting boy and girl respectively, where the sequence of the whole string represents the line containing boys and girls int the line/queue at a certain time.

The first integer represents the number of children ( in this case string size ) and the 2nd integer represents seconds that will pass.

  • First, we will take two integers as asked in the problem. (number of children and passable time).
  •  After inputting them we will declare a string whose length will be the 1st inputted integer to create the line of students as a string.
  • Then, we will call a loop until we pass all iterations of the second Input integer so that we can do operations of swapping boys and girls for the given seconds. (line 8 of code )
  • Now we will loop through all the index of the input string ( boys, girls queue representation ) to check if current_index = ‘B’ and next_index = ‘G’.
  • If the previous comparison is correct we will swap current_index and next_index values. ( Boys exchanging position with girls if the girl is behind ). (line 10 – 12 of code)
  • Then increment the loop iterator by 2 because we had done work with two indexes and we don,t need to do anything u til next second. (line 13 of code)
  • Else if the comparison didn’t match we will increment the loop iterator only by one so we can check the next index.
  • After the looping process is done just print the string to show the changed output.


#include<bits/stdc++.h>
int main() {
int x,y;
scanf("%d%d",&x,&y);
char str[x],tmp;
scanf(" %s",str);
int len = strlen(str);
while(y--) {
   for(int i = 0; i<len-1;) {
      if(str[i]=='B' && str[i+1]=='G') {
         tmp = str[i];
         str[i] = str[i+1];
         str[i+1] = tmp;
         i+=2;
      }else{
         i++;
          }
       }
    }
printf("%s\n",str);
return 0;
}
]]>
210