A – Books Codechef Solution

A – Books Codechef Solution: Hello coders, today we are going to solve A – Books Codechef Solution.

Codechef solutions

You are given a sorted list AA of size NN. You have to make a new list BB such that B[i] is equal to the number of elements strictly greater than A[i] in the list AA. Print the new list.

Input

The first line consists of TT, denoting the number of test cases.

First line of each test case consists of one integer denoting NN, where NN is the size of the list given to you.

Second line of each test case contains the list given to you containing NN elements.

Output

For each test case print the list in a single line and the elements of the list should be separated by spacespace.

Print the answer to eacheach test case in a newnew lineline.

Constraints

  • 1≤T≤1001≤T≤100 , where TT is the number of test cases.
  • 1≤N≤1001≤N≤100, where NN is the number of elements in the list.
  • 1≤A[i]≤10000001≤A[i]≤1000000 ,where A[i]A[i] is the ithith element in the list given to you.

Subtasks

  • 1515 pointspoints: All the elements in the list given to you are distinct.
  • 3535 pointspoints: Original constraints: Elements can be repeated in the list.

Sample Input

2
4
1 2 4 4
5
1 2 2 2 2

Sample Output

3 2 0 0
4 0 0 0 0

Explanation

The explanation for test case 1 of sample input :

The first element in the new list is 3 since the first element in the previous list was 1, and there are three elements which are strictly greater than 1, that is 2, 4 and 4.

The second element in the new list is 2 since the second element in the previous list was 2, and there are two elements which are strictly greater than 2, that is 4 and 4.

The third and fourth element in the new list is 0 since third and fourth element in the previous list was 4 and there are no elements which are strictly greater than them.

A – Books – CodeChef Solution in JAVA

import java.util.*;
import java.lang.*;
import java.io.*;
class Codechef
{
	public static void main (String[] args) throws java.lang.Exception
	{
		// your code goes here
		Scanner sc=new Scanner(System.in);
		int t=sc.nextInt();
		while(t-->0){
		    int n=sc.nextInt();
		    int arr[]=new int[n];
		    for(int i=0;i<n;i++){
		        arr[i]=sc.nextInt();
		    }
		    for(int i=0;i<n;i++){
		         int count=0;
		        for(int j=i+1;j<n;j++){
		            if(arr[i]<arr[j])
		                count++;
		        }
		        System.out.print(count+" ");
		    }
		    System.out.println();
		}
	}
}

A – Books  – CodeChef Solution in CPP

#include <iostream>
using namespace std;
int main() {
	int t;
	cin>>t;
	while(t--){
	    int n;
	    cin>>n;
	    int a[n],b[n];
	    for(int i=0;i<n;i++){
	        cin>>a[i];
	    }
	    int c=0;
	    for(int i=0;i<n;i++){
	        for(int j=0;j<n;j++){
	            if(a[i]<a[j]) c++;
	        }
	        b[i]=c;
	        c=0;
	    }
	    for(int i=0;i<n;i++){
	        cout<<b[i]<<" ";
	    }
	    cout<<endl;
	}
	return 0;
}

A – Books – CodeChef Solution in Python

for i in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    ans=[]
    for j in range(len(l)):
        c=0
        for i in range(len(l)):
            if l[j]<l[i]:
                c=c+1
            else:
                c+=0
        ans.append(str(c))
    x=' '.join(ans)
    print(x)

Disclaimer: The above Problem (A – Books ) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment