Hackearth's Data Structures solutions( Takeoff, Help Jarvis! , Pairs Having Similar Elements ) :

Problem 4: Takeoff

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        long long n,p,q,r,i,check,ct=0;
        cin>>n>>p>>q>>r;
        for(i=1;i<=n;i++)
        {
            check=0;
if(i%p==0)
            {
check++;
            }
            if(i%q==0)
            {
check++;
            }
if(i%r==0)
            {
check++;
            }
            if(check==1)
            {
                ct++;
            }
        }
        cout<<ct<<endl;
    }
}

This code is simple. There's no need for any explanation.
 

 

 

 

Problem 5: Help Jarvis!

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    long long t;
    int i,temp;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s; //taking input as a string
        vector<char>v(s.length()); //therefore, vector size = s.length()
        for(i=0;i<s.length();i++)
        {
v[i]=s[i]; //putting the elements of the string in the vector
        }
        sort(v.begin(),v.end()); //sorting vector elements
        temp=v[0]-'0'; //see note below
        for(i=0;i<s.length();i++)
        {
            if(v[i]-'0'!=temp)
            {
                cout<<"NO"<<endl;
                break;
            }
            else
            {
                if(i==s.length()-1)
                {
                    cout<<"YES"<<endl;
                }
            }
            temp++;
        }
    }
}

What i have done here is , first a string of numbers is taken  and then the individual digits of that string is stored in a vector of characters of  same size as that of string and then sorted the vector.

For example -  the input string is 2145

                         all digits will be stored in a vector and after sorting the vector will look like this:

                         index      value

                          0              1

                          1              2

                          2              4

                          3              5           
 

Now i declared a temp variable(outside of for loop) which holds the value of the minimum element in the vector i.e first element (0th index) initially. The temp value will be increased by 1 at the end of the loop using temp++(so that temp values forms a normal counting). Now what we have to check is the next elements in the vector forms a normal counting. For this purpose , we will use the for loop to iterate through the vector. The if condition checks the current value is equal to the temp value(expected value) and if it is not equal than we simply print NO and break out of the loop using break statement. The else loop will execute only when temp (expected value) matches vector element. We can printout YES only when all the elements in the vector matches temp value(expected value). We know at the end the i will be equal to 1 less than the size of vector( as indexing starts from zero) and for that purpose only we used an if statement in the else. Hence YES gets printed when all the elements matches with the temp. 

 

 

 

 

 

Problem 6:  Pairs Having Similar Elements

Solution: (in c++)


( please guys before moving to the solution try it yourself at least 3-4 times , if you really wanna become a good coder)

#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
long count=0,x=0,y=0;
cin>>n;
int a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
}
sort(a,a+n);
for(int i=0;i<n;i++)
{
if(a[i]+1==a[i+1])
{
x++; //counting elements that satisfy our condition
}
else if(a[i]==a[i+1])
{
x++;
//we also need to count the same elements
y++; //for eg:- 4 5 5 here 4 5 and 4 5 both satisfy our condition
}
else
{
if(x!=y) //there may be chance of 5 5 5
{
count+=((x*(x+1))/2); //all possible transitives
}
x=0;
y=0;
}
}
cout<<count;
}

 This code is simple. There's no need for any explanation.

 

 

 

Guys , if you have any queries related to a question or need more explanation for a question , comment below!

Comments

  1. Bruh! Can u explain the else if condition .why have you incremented both x and y

    ReplyDelete

Post a Comment

Popular posts from this blog

Coursera's Algorithmic toolbox assignment solutions( Sum of Two Digits, Maximum Pairwise Product ) :

HackerEarth's basic programming solutions( Seating Arrangement, Zoos, Anagrams ) :

HackerEarth's basic programming solutions( Minimize Cost, Magical Word, Best Index ) :