Count Smaller elements

QUESTION

Write a program for count number of smaller elements on right of each element in an array. Given an array arr[ ] of unsorted integers, construct another array countSmaller[ ] such that countSmaller[i] contains count of smaller elements on right side of each element arr[i] in array.\n\nNote: Duplicate elements are possible in array.\n\nInput:\n\nThe first line of input contains an integer T denoting the number of test cases.\nThe first line of each test case is N,N is the size of array.\nThe second line of each test case contains N input arr[i].\n\nOutput:\n\nPrint the count Smaller array.\n\nConstraints:\n\n1<=T<=100\n1<=N<=200\n1<=C[i]<=500.

“TESTCASE_1”: “2\n7\n12 1 2 3 0 11 4\n5\n1 2 3 4 5\n###—###SEPERATOR—###—\n6 1 1 1 0 1 0\n0 0 0 0 0”, “TESTCASE_2”: “2\n7\n1 7 5 9 10 13\n5\n2 4 6 8 10\n###—###SEPERATOR—###—\n0 2 0 1 1 1 0\n0 0”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include <iostream>
using namespace std;
int main()
{
	int t,i,j,k,n,x,count;
  	cin>>t;
  	for(i=0;i<t;i++){
    	cin>>n;
      	int a[n];
      	int b[n];
      	for(j=0;j<n;j++){
        	cin>>a[j];
        }
      	
      for(k=0;k<n-1;k++){
        count=0;
      	for(x=k+1;x<n;x++){
        	if(a[x]<a[k]){
            	count++;
            }
        }
        b[k]=count;
      }
      
      b[n-1]=0;
  	for(k=0;k<n;k++){
      	if(k!=n-1)
    	cout<<b[k]<<" ";
      	else
          cout<<b[k];
    }
      cout<<endl;
    }
  
	return 0;
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.