Problem Solving (Basic) Skill Test on Hackerrank Solutions

maximum laptop count c++


Maximum cost of laptop count solution in C++


#include<bits/stdc++.h>
using namespace std;
#define fast ios_base::sync_with_stdio(false);cin.tie(NULL)
#define nl "\n"
#define ll long long
#define M 10001

int main()
{
    //fast
    fast;

    //code starts
    int n;
    cin>>n;
    int a[n], b[n];
    for(int i=0; i<n; i++)
    {
        cin>>a[i];
    }
    cin>>n;
    for(int i=0; i<n; i++)
    {
        string x;
        cin>>x;
        if(x=="legal") b[i]=1;
        else b[i]=0;
    }

    int dc, s=0, sum=0, mx=0;
    cin>>dc;
    for(int i=0;i<n;i++)
    {
        s+=b[i];
        sum+=a[i];
        if(s==dc)
        {
            mx=(sum>mx)?sum:mx;
            s=sum=0;
        }
    }

    cout<<mx<<nl;
    return 0;
}


Maximum cost of laptop count solution in Java


package com.company;

import java.util.Scanner;

public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int i=0,n=in.nextInt();
int a[]=new int[n];
int b[]=new int[n];
for(i=0;i<n;i++)
a[i]=in.nextInt();
n=in.nextInt();
for(i=0;i<n;i++)
b[i]=(in.next().equals("legal"))?1:0;

int dc=in.nextInt(),s=0,sum=0,max=0;
for(i=0;i<n;i++)
{
s+=b[i];
sum+=a[i];
if(s==dc)
{
max=(sum>max)?sum:max;
s=sum=0;
}
}
System.out.println(max);
}
}


Hackerrank solution in c++ is available here at code alap website.

Visit our youtube channel for better understanding. and get contests early solutions. 

youtube Code Alap link click here. youtube

Post a Comment

Previous Post Next Post