why did we used unordered instead of ordered, what is the relation between the keys and the values in the way of ranks and marks
what if we used the ordered map
Problem of the day
Ninja recently appeared for a class test. The test was attended by ‘N’ students and the marks of each student was given in an array ‘A’.
Ninja knows that he got a rank ‘K’ among the ‘N’ students but forgot the marks achieved by him. The ranks are given according to the better marks scored by the students.
Tell the marks that must have been scored by Ninja to get a rank ‘K’.
Example :N = 5
K = 3
A = [ 2, 5, 4, 4, 5 ]
Rank 1 and Rank 2 students get marks = 5.
Rank 3 and Rank 4 students get marks = 4.
Rank 5 student gets marks = 2.
So, Ninja must have scored 4 marks in order to achieve Rank = 3.
The first line contains an integer 'T' which denotes the number of test cases to be run. Then the test cases follow.
The first line of each test case contains an integer ‘N’.
The second line of each test case contains an integer ‘K’.
The third line contains ‘N’ space separated integers denoting the elements of array ‘A’.
Output format :
For each test case, print one integer denoting the score Ninja must have scored.
Print the output of each test case in a new line.
Note :
You don’t need to print anything. It has already been taken care of. Just implement the given function.
1 <= T <= 10
1 <= N <= 10^4
1 <= K <= N
0 <= A[i] <= 100
Time Limit: 1 sec
2
3
1
3 4 4
4
3
1 1 2 2
4
1
For test case 1 we have,
To get a Rank 1, Ninja must have scored maximum marks in the class.
Hence, Ninja scored 4 marks.
For test case 2 we have,
Students with marks = 2 will achieve ranks 1 and 2 respectively.
Students with marks = 1 will achieve ranks 3 and 4 respectively.
So, Ninja scored 1 mark.
3
2
1
20 9
2
1
9 17
7
2
9 6 0 2 20 10 5
20
17
10
Think about sorting the marks.
Approach :
Algorithm :
O(N*logN) , where ‘N’ is the length of the array ‘A’.
Since we are sorting the array, the overall time complexity is O(N*logN).
O(1)
Constant extra space is required. Hence, the overall Space Complexity is O(1).
Interview problems
why unordered?
why did we used unordered instead of ordered, what is the relation between the keys and the values in the way of ranks and marks
what if we used the ordered map
Interview problems
Easy C++ Solution
TC=O(nlogn)
Sc=n
#include <bits/stdc++.h>
int classTest(int n, vector<int> &a, int k) {
// Write your code here.
sort(a.rbegin(),a.rend());
unordered_map<int,int>m;
int c=1;
for (int i = 0; i < a.size(); i++) {
m[c] = a[i];
c++;
}
return m[k];
}Interview problems
Code+Approach
#include <bits/stdc++.h>
int classTest(int n, vector<int> &arr, int k) {
sort(arr.begin(),arr.end());
return arr[n-k];
}
First sort all elements then we need to return n-k element.
For eg. to find score of rank 3 student having total 5 students , we will return arr[5-3] which is arr[2] position.
Interview problems
Easy Solution Alongwith Approach
CODE :
#include <bits/stdc++.h>
int classTest(int n, vector<int> &a, int k) {
// Write your code here.
sort(a.begin(),a.end());
int size=a.size();
int ans= a[size-k];
return ans;
}
Firstly, we'll sort the given array in increasing order using the sort function. Thereafter, we will create an ans integer variable in which we will store the answer which will be a[a.size()-k]. After this we will return the answer.
Interview problems
best solution.
#include <bits/stdc++.h>
int classTest(int n, vector<int> &a, int k){
sort(a.begin() , a.end());
for(int j = 0 ; j<a.size() ; j++){
if(k == 1){
return *max_element(a.begin(),a.end());
}
else{
int p = n - k;
return (a[p]);
}
}
}
TC: O(logn)
SC: O(1)
Interview problems
C++ BASIC || 2 APPROCHES
APR1:
#include <bits/stdc++.h> int classTest(int n, vector<int> &a, int k) { sort(a.begin(),a.end()); int count =0; for(int i=n-1;i>=0;i--) { count =count+1; if(count==k) { return a[i]; } } }
APR2:
#include <bits/stdc++.h> int classTest(int n, vector<int> &a, int k) { sort(a.begin(),a.end()); return a[n-k]; }
/*PLEASE UPVOTE IF FOUND USEFUL*/
Interview problems
2 lines of code c++
#include <bits/stdc++.h>
int classTest(int n, vector<int> &a, int k) {
// Write your code here.
sort(a.rbegin(),a.rend());
return a[k-1];
}
Interview problems
Easy code
int classTest(int n, vector<int> &a, int k) {
// Write your code here.
sort(a.begin(),a.end());
return a[n-k];
}
Interview problems
easy code
int ans =0;
int rank=0;
sort(a.begin(),a.end());
for(int i=0;i<n;i++){
ans = i-k+1;
rank =a[ans];
}
return rank;
Interview problems
3 line solution in c++
int classTest(int n, vector<int> &a, int k) { sort(a.begin(),a.end()); reverse(a.begin(),a.end()); return a[k-1]; }