I am doing online challenge which has to do the following.
There is a contest going in a village. So in the first line input two numbers N (which is how many people will join the contest) and K (how many of them can go to stage 2).
After that, we input N times the votes for each candidate in both stages.
Example input:
5 3
9 2
3 10
5 6
8 4
6 5
As you can see, we input N=5
, K=3
, which means 5 candidates, so 5 additional lines and 3 of them which go to stage 2.
After we sort the array the candidates with most votes are the ones with 6, 8 and 9. So they're going to stage 2. The winner is the one who has most votes in the stage 2 of them. In this case, 6 has 5 votes which is the most (8 has 4 and 9 has 2) and therefore we output the index of the 6 which is 5.
What I got so far:
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int arr[50],nizabackup[50],n,k,niza2[50],saveindex[50],indexp=0;
cin >> n >> k;
for(int i=0;i<n;i++)
{
cin >> arr[i] >> niza2[i];
nizabackup[i] = arr[i];
}
sort(arr,arr+n);
for(int j=n;j>=k;j--)
{
for(int k=0;k<n;k++)
{
if(arr[j]==nizabackup[k])
{
saveindex[0]=k;
indexp++;
}
}
}
sort(saveindex,saveindex+indexp);
cout << saveindex[indexp];
return 0;
}
I need a hint what to do and also additional question -- why my debugger doesn't read the second for loop?