Hackearth's Data Structures solutions( Strange Game, Maximize the Earning, Pepper and Contiguous Even Subarray ) :

Problem 10: Strange Game

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()
{
    ios::sync_with_stdio(0); //helps to reduce the compile time a little
cin.tie(0); //
    long long t,n,k;
    cin>>t;
    while(t--)
    {
cin>>n>>k;
     long long ct=0,i,maxi; //ct = count
     vector<long long>a(n,0),b(n,0);
     for(i=0;i<n;i++)
     {
         cin>>a[i];
     }
     for(i=0;i<n;i++)
     {
         cin>>b[i];
     }
     maxi=*max_element(b.begin(),b.end())+1; //max value card bob is having
for(i=0;i<n;i++)
     {
         if(a[i]<maxi)
         {
             ct=ct+(maxi-a[i]); //see note below
         }
     }
     cout<<ct*k<<endl;
    }
}

Here what we have to do is , increase the value of the alice's cards until each of his card value becomes greater than the maximum value of the card bob is having (so that bob has no longer a single card by which he can win a turn and alice victory is assured)

For example : alice's cards are --> 8 9 10 23 4

                         bob's cards are --> 7 9 2 3 13

to ensure alice wins , by the use of algorithm(to increase a number by one using k seconds) his final cards values should look like this --> 14 14 14 23 14

so our final answer will be sum of the total increments of the card values of alice * k

 

 

 

 

Problem 11: Maximize the Earning

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()
{
    ios::sync_with_stdio(0); //helps to reduce the compile time a little
cin.tie(0); //
    long long s,n,r,i,maxi;
    cin>>s;
    while(s--)
    {
        long long ct=0;
        cin>>n>>r;
        vector<long long>v(n,0);
        for(i=0;i<n;i++)
        {
            cin>>v[i];
        }
        maxi=v[0]; //see not below
        for(i=0;i<n;i++)
        {
            if(maxi<v[i])
            {
ct++;
                maxi=v[i];
            }
        }
        cout<<(ct+1)*r<<endl;
    }
}

I used a simple logic here. From front of the buildings, starting from the first building(our eyes at first building) we can only see the next building only if it is bigger than the previous building(our eyes currently stuck on). If there's a bigger building ahead, then our eyes will move and stick to that bigger building and we will compare the next buildings from it until another bigger building comes and we stick our eyes there. We will increase the value of count by one every time our eyes sticks to a new building...counting front building too as our eyes were first stuck there or you can say it is always visible as it is in the front. For those who don't know, this is also called a greedy algorithm approach. Assume our eyes is the maxi variable and  now you can do this problem easily.   

 

 

 

 

Problem 12: Pepper and Contiguous Even Subarray

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,i,n;
    cin>>t;
    while(t--)
    {
        long long ct=0,ans=0; //ct=count
        cin>>n;
        vector<long long>v(n,0);
for(i=0;i<n;i++)
        {
            cin>>v[i];
        }
        for(i=0;i<n;i++)
        {
            if(v[i]%2==0)
            {
                ct++;
                if(ans<ct)
                {
                    ans=ct; //therefore answer will store the max value of ct
                }
            }
            else
            {
                ct=0;
            }
        }
        if(ans==0)
        {
            cout<<"-1"<<endl;
        }
        else
        {
            cout<<ans<<endl;
        }
    }
}

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

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 ) :