
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ code to find position of students after coding contest
Suppose we have an array A with n elements. In a coding contest, in total n students will participate, and before the start, every one of them has some positive rating (integer). A[i] represents the rating of ith student. After the contest ends, every student will end up with some positive integer position. We are expecting the students will take places according to their ratings. If student A has rating strictly lower than student B, A will get strictly greater position than B. We have to find the position at the end of the contest.
So, if the input is like A = [3, 5, 3, 4, 5], then the output will be [4, 1, 4, 3, 1] because 2nd and 5th students share the first position with highest rating, 4th student is next with third position, 1st and 3rd students are the last sharing fourth position.
Steps
To solve this, we will follow these steps −
n := size of A for initialize i := 0, when i < n, update (increase i by 1), do: d := 1 for initialize j := 0, when j < n, update (increase j by 1), do: if A[j] > A[i], then: (increase d by 1) cout << d << ", "
Example
Let us see the following implementation to get better understanding −
#include <bits/stdc++.h> using namespace std; void solve(vector<int> A){ int n = A.size(); for (int i = 0; i < n; i++){ int d = 1; for (int j = 0; j < n; j++){ if (A[j] > A[i]) d++; } cout << d << ", "; } } int main(){ vector<int> A = { 3, 5, 3, 4, 5 }; solve(A); }
Input
{ 3, 5, 3, 4, 5 }
Output
4, 1, 4, 3, 1,
- Related Articles
- C++ code to find winner of math contest
- C++ code to find different winner and non-winner counts on a contest
- C++ code to find minimal tiredness after meeting
- C++ code to find xth element after removing numbers
- C++ code to find tree height after n days
- C++ code to find minimum stones after all operations
- C++ code to find minimum k to get more votes from students
- C++ code to find money after buying and selling shares
- C++ code to find corrected text after double vowel removal
- C++ code to find final number after min max removal game
- C++ program to find maximum distance between two rival students after x swaps
- C++ code to get minimum sum of cards after discarding
- Position of robot after given movements in C++
- C++ code to count steps to reach final position by robot
- Output Contest Matches in C++
