Header Ads Widget

Top contestants | Hackerearth Problem solution

    Top contestants | Hackerearth Problem solution:-

 You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Ai are distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Aiare distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Aiare distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Aiare distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Aiare distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Aiare distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Aiare distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Aiare distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.You are given that 

N candidates participated in a contest. For each candidate, you are given Ai, representing the points scored by the ith candidate in the contest. You have to print the indexes of the top K candidates of the contest.

Note: AiAjij i.e, all Aiare distinct.


Input format

  • First line: Two space-separated integers N and K
  • Next line: N space-separated integers. The ithinteger denotes the points scored by the ithcandidate in the contest.

Output format

  • Print K space-separated integers, denoting the indexes of the top K candidates.

Constraints

1N105

1KN

1Ai109

Sample Input
5 3
25 5 17 10 45
Sample Output
5 1 3
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

The points scored by candidates are 25,5,17,10,45. The points scored by top 3 candidates of the contest are 45,25,17. Indexes of those points are 5,1,3 respectively.

10

Top contestants | Hackerearth Problem solutionCode:-

  Here I am going to give you two solution first one is on the basis of C language and second one is on the basis of c++ language which you can submit in c++14 and c++17 also

Solution 1 ( C language):-

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define pcx putchar_unlocked
#define gcx getchar_unlocked
typedef long int lint;
static inline lint getli () {
lint n =0;
int c = gcx();
while(c<'0' || c>'9') c = gcx();
while(c>='0' && c<='9') {
n = n * 10 + c-'0';
c = gcx();
} return n;
}
static inline void putli (lint n, char lc) {
if (!n) {
pcx('0'); if(lc) pcx(lc); return;
}
char s[24]; lint rdi =0;
while (n) {
s[rdi++] = '0' + n % 10;
n /= 10;
}
while (rdi) pcx(s[--rdi]);
if(lc) pcx(lc);
}
typedef struct {
int points;
int index;
} pi_t;
int cmp(const void *p, const void *q) { // DSC order
return (((pi_t*)p)->points < ((pi_t*)q)->points);
}
int main () {
lint N = getli();
lint K = getli();
pi_t *PI = malloc ((N +1) * sizeof(pi_t));
PI[0].points = 0;
PI[0].index = 0;
for (lint ai=1; ai<=N; ) {
PI[ai].index = ai;
PI[ai++].points = getli();
}
qsort (PI, N+1, sizeof(pi_t), cmp);
for (lint ai=0; ai<K; )
putli (PI[ai++].index, ' ');
return 0;
}

Solution 2 ( C++ language):-

 This solution is based on the c++ language and you can submit ib c++14 and c++17 also.
In this solution first three lines of the main function is only for the decreasing the time of execution of the program..
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);

This is your choice that you want to use this or not but in some cases the code may take more time in execution and that time we need it .

#include <bits/stdc++.h>
using namespace std;
void init(){
cin.tie(0);
cin.sync_with_stdio(0);
}
int main()
{
init();
int n, k;
cin>> n >> k;
pair<int, int> a[n];
for(int i = 0; i < n; i++) {
     cin>> a[i].first;
     a[i].second = i + 1;
}
sort(a, a + n );
for(int i = n-1; i >= n - k; i--)
cout<< a[i].second << " ";
return 0;
}

Keywords:- 

  ,top contestants hackerearth solution python,
,capital of hills hackerearth solutions,
,the number line problem hackerearth solution,
,minimum money hackerearth solution,
,bank transaction hackerearth solution,
,an equilateral triangle hackerearth solution,
,hackerearth python solutions,
,capitals and cities hackerearth solution,

Recommended Post:

Companies interview:-

Full C course:-    

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-


Post a Comment

0 Comments