Header Ads Widget

Annual Day | Tech Mahindra coding question

 Annual Day :-

On the eve of the annual day, a competition is held among N students in which each student gains some points based on their performance. All students were asked to stand in a queue in an increasing order based on the points they scored. The clever students stood in a random order so that the students with lower points are not noticed.
You will be given a points array where points[i] tells the teacher the number of points that the ith student earned. Your task is to help the teacher to find all pairs such that for all (0<=i<j<N), the points scored by the ith student is greater than that of the jth student, i.e. points[i]>points[j] where i<j and return the total count of such pairs.

Input Specification:
input1: An integer value N(1<=N<=1000) denoting the number of students in the class.
input2: An integer array of length N representing the points scored where input2[i] = points scored by ith student and input2[i]<=1000(0<=i<N)

Output Specification:
Return the total number of pairs that satisfy the constraints given in the problem statement

Example 1:-
input1: 5
input2: {1,1,3,6,2}

Output:-
2

Explanation:-
 The pairs of points satisfying the given scenario are:
(3,2) and (6,2).
Hence, 2 is returned as the output.
C++ Code:-
#include<bits/stdc++.h>
using namespace std;

int Student(int input1,int input2[])
{
int count=0;
for(int i=0;i<input1;i++)
{
for(int j=i+1;j<input1;j++)
{
if(input2[i]>input2[j])
count++;
}
}
return count;
}

int main()
{
int n;
cin>>n;
int arr[n];
for(int i=0;i<n;i++)
cin>>arr[i];
cout<<Student(n,arr);
return 0;
}
Output:-
5
1 1 3 6 2

2

Wipro :-

Infytq :-

Key Points;-

Hackerrank:-


C-tutorial:-

See more:-