Aman has made a To-Do list: a list of all the pending tasks that he has to complete. The list is in the increasing order of time taken to finish the tasks. Aman begins from the starting of the list.
Given an array of size N denoting the list of the pending tasks. Each task is an integer between 1 and N. The tasks numbered from 1 to 7 are classified as top priority tasks. Aman needs to know the minimum number of tasks he has to complete in order to get all the top priority tasks accomplished. Can you help him to do so?
Two Samples are given as if input array is
1 5 2 4 9 3 6 7 8
Output is
8
And,
10 9 8 7 6 5 4 3 2 1
Output is
10
I could only code till counting for all 1 to 7 exist, here's what I coded
#include <iostream>
using namespace std;
int main()
{
int n, count = 0;
cin >> n;
int arr[n];
for (int i = 0; i < n; i )
{
cin >> arr[i];
}
for (int i = 0; i < n; i )
{
if (arr[i] <= 7)
{
count ;
}
}
cout << count << endl;
return 0;
}
I understood the logic behind the question, but I can't put it into code, like what should I do to count for minimum tasks, in second sample 10 9 8 came before all "important tasks" so he needs to do them till he gets to 1-7, and in first 8th came at last but 1-7 including 9 was done so minimum was 8.
CodePudding user response:
Something like this, loop though the tasks counting the priority ones. When the number of priority tasks reaches 7, stop the loop and print out the number of tasks seen so far.
int tasks = 0, pri_tasks = 0;
for (; tasks < n && pri_tasks < 7; tasks )
{
if (arr[tasks] <= 7)
pri_tasks;
}
if (pri_tasks == 7)
cout << tasks << '\n';