-1

Alex is a student at a collage and will now enroll in the summer semester, for which he needs to take paper documents to the faculty. All students bring their documents on one of the N days (marked from 1 to N) that are provided for it. From the enrollments of previous years, for each of the N days it is known what is the expected number of students who will take the documents that day. Alex will bring the documents on the day when the least number of students are expected to bring documents on that day, and if there are more such days - then on the first possible day. Print on which of the days Alex will bring the documents and how many students are expected to go to the college on that day.

Input In the first row is the number N (1 ≤ N ≤ 30), the number of days provided for carrying documents. The next row contains N integers in the range 1 to 1000, the number of students expected to attend college on each of the N days, starting with day one

Output On one line, print the sequence number of the day Alex chooses, followed by the number of students expected to attend college on that day.

Examples

#1

Input 3 30 20 40 Output 2 20

#2

Input 4 20 30 40 20 Output 1 20

I can find the min number,but im now sure on how to get the position of the array and choose the first one if values repeat

int arr[1000],n,i,min;
    cin>>n;
    for (int i = 0; i < n; ++i)
    cin>>arr[i];
    min = arr[0];
    for (i = 0; i < n; i++)
    {
        if (min > arr[i])
            min = arr[i];
    }
        cout<<min;
Alrk
  • 9
  • 4
  • your code already does consider only the first max value in case they do repeat. THe index of `arr[i]` is `i`. – 463035818_is_not_an_ai Feb 23 '23 at 08:54
  • Remember the array starts at position 0 not 1. So the first element in the array is arr[0]. – akira hinoshiro Feb 23 '23 at 08:57
  • @akirahinoshiro OP does start with element `arr[0]` – 463035818_is_not_an_ai Feb 23 '23 at 08:59
  • Side note: As you select index 0 as initial `min` anyway you can start your loop at `1` – note that it is good practice in C++ to limit the scope of variables as much as possible – you did so for `i` in first loop (hiding the previous `i` outside the loop), you didn't do for the second loop. – Aconcagua Feb 23 '23 at 09:00
  • @463035818_is_not_a_number the 2 given examples state the position of the elements starting by 1. It is in the text not the code. – akira hinoshiro Feb 23 '23 at 09:01
  • @akirahinoshiro The code of the question fills an array starting at position 0 with n values – this corresponds to an implicit index translation from 1-based to 0-based. Then it's totally correct starting iteration at index 0. On outputting, though, you need to translate the index in the array (0-based) back to 1-based as requested – simply by adding 1... – Aconcagua Feb 23 '23 at 09:09
  • Side note: About [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice)... – Aconcagua Feb 23 '23 at 09:10

1 Answers1

1

The task requires you to output the day on which the minimum number of students attend – which is equivalent to the index of the minimum in the array (by the way: why an array of size 1000 if at most 30 days are to be considered anyway?).

However you lose this information by only storing the minimum value. Instead you can simply store the index and compare relying on that one:

size_t min = 0;
for(size_t i = 1; i < n; ++i) // note: can start at 1 as 0 is tested already
{
   if(arr[i] < arr[min])
   {
       min = i;
   }
}

Now after the loop has terminated you can simply output min + 1 (note: index translation, C++ is zero-based, desired output is one-based).

Aconcagua
  • 24,880
  • 4
  • 34
  • 59