Editorial for CHEFW

PROBLEM LINK:Contest Page | CodeChef

Practice

Author: harsh_beniwal
Tester: harsh_beniwal
Editorialist: harsh_beniwal

DIFFICULTY:

EASY.

PREREQUISITES:

Math,Arrays.

PROBLEM:

Chef is giving a 60 Minutes coding contest.

He has Given an Array of N questions.Each ith element represents the minutes required to solve that question.

Find the maximum number of question chef will be able to solve in given 60 minutes.

QUICK EXPLANATION:

Sort the Array in Ascending order and traverse the loop till the total sum of time is less than or equal to 60 minutes.
And simultaneously increment the counter after iterating on every element.

EXPLANATION:

We have to simply Sort the Array in Ascending order.
You can use any sorting algorithms or STL to sort the array in ascending Order.

After sorting the array iterate over every element and add that element to the Sum variable. Iterate the loop till sum is less than or equal to 60.

simultaneously increment the counter after iterating on every element.

SOLUTIONS:

Setter's Solution

#include<bits/stdc++.h>

using namespace std;

int main()

{

int t;

cin>>t;

while(t--)

{

    int n,s=0;

    cin>>n;

    int a[n];

    for(int i=0;i<n;i++)

    {

        cin>>a[i];

    }

    sort(a,a+n);

    int i=0,ans=0;

    while(s<=60 && i<n)

    {

        s+=a[i];

        if(s<=60)

        {

            ans++;

        }

        i++;

    }

    cout<<ans<<"\n";

}

}

Tester's Solution

#include<bits/stdc++.h>

using namespace std;

int main()

{

int t;

cin>>t;

while(t--)

{

    int n,s=0;

    cin>>n;

    int a[n];

    for(int i=0;i<n;i++)

    {

        cin>>a[i];

    }

    sort(a,a+n);

    int i=0,ans=0;

    while(s<=60 && i<n)

    {

        s+=a[i];

        if(s<=60)

        {

            ans++;

        }

        i++;

    }

    cout<<ans<<"\n";

}

}

Editorialist's Solution

#include<bits/stdc++.h>

using namespace std;

int main()

{

int t;

cin>>t;

while(t--)

{

    int n,s=0;

    cin>>n;

    int a[n];

    for(int i=0;i<n;i++)

    {

        cin>>a[i];

    }

    sort(a,a+n);

    int i=0,ans=0;

    while(s<=60 && i<n)

    {

        s+=a[i];

        if(s<=60)

        {

            ans++;

        }

        i++;

    }

    cout<<ans<<"\n";

}

}