Count smaller elements present in the array for each array element
Last Updated :
12 Apr, 2023
Given an array arr[] consisting of N integers, the task is for each array element, say arr[i], is to find the number of array elements that are smaller than arr[i].
Examples:
Input: arr[] = {3, 4, 1, 1, 2}
Output: 3 4 0 0 2
Explanation:
The elements which are smaller than arr[0](= 3) are {1, 1, 2}. Hence, the count is 3.
The elements which are smaller than arr[1](= 4) are {1, 1, 2, 3}. Hence, the count is 4.
The elements arr[2](= 1) and arr[3](= 1) are the smallest possible. Hence, the count is 0.
The elements which are smaller than arr[4](= 2) are {1, 1}. Hence, the count is 2.
Input: arr[] = {1, 2, 3, 4}
Output: 0 1 2 3
Naive Approach: The simplest approach is to traverse the array and for each array element, count the number of array elements that are smaller than them and print the counts obtained.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void smallerNumbers( int arr[], int N)
{
for ( int i = 0; i < N; i++) {
int count = 0;
for ( int j = 0; j < N; j++) {
if (arr[j] < arr[i]) {
count++;
}
}
cout << count << " " ;
}
}
int main()
{
int arr[] = { 3, 4, 1, 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
smallerNumbers(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void smallerNumbers( int arr[], int N)
{
for ( int i = 0 ; i < N; i++)
{
int count = 0 ;
for ( int j = 0 ; j < N; j++)
{
if (arr[j] < arr[i])
{
count++;
}
}
System.out.print(count + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 3 , 4 , 1 , 1 , 2 };
int N = arr.length;
smallerNumbers(arr, N);
}
}
|
Python3
def smallerNumbers(arr, N):
for i in range (N):
count = 0
for j in range (N):
if (arr[j] < arr[i]):
count + = 1
print (count, end = " " )
if __name__ = = "__main__" :
arr = [ 3 , 4 , 1 , 1 , 2 ]
N = len (arr)
smallerNumbers(arr, N)
|
C#
using System;
public class GFG
{
static void smallerNumbers( int [] arr, int N)
{
for ( int i = 0; i < N; i++)
{
int count = 0;
for ( int j = 0; j < N; j++)
{
if (arr[j] < arr[i])
{
count++;
}
}
Console.Write(count + " " );
}
}
static public void Main()
{
int [] arr = { 3, 4, 1, 1, 2 };
int N = arr.Length;
smallerNumbers(arr, N);
}
}
|
Javascript
<script>
function smallerNumbers(arr, N)
{
var i;
for (i = 0; i < N; i++) {
var count = 0;
for (j = 0; j < N; j++) {
if (arr[j] < arr[i]) {
count += 1;
}
}
document.write(count + " " );
}
}
var arr = [3, 4, 1, 1, 2]
var N = arr.length;
smallerNumbers(arr, N);
</script>
|
Time Complexity: O(N2)
Auxiliary Space: O(1)
Efficient Approach: The above approach can be optimized by using Hashing. Follow the steps below to solve the problem:
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void smallerNumbers( int arr[], int N)
{
int hash[100000] = { 0 };
for ( int i = 0; i < N; i++)
hash[arr[i]]++;
int sum = 0;
for ( int i = 1; i < 100000; i++) {
hash[i] += hash[i - 1];
}
for ( int i = 0; i < N; i++) {
if (arr[i] == 0) {
cout << "0" ;
continue ;
}
cout << hash[arr[i] - 1]
<< " " ;
}
}
int main()
{
int arr[] = { 3, 4, 1, 1, 2 };
int N = sizeof (arr) / sizeof (arr[0]);
smallerNumbers(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG{
static void smallerNumbers( int arr[], int N)
{
int hash[] = new int [ 100000 ];
for ( int i = 0 ; i < N; i++)
hash[arr[i]]++;
int sum = 0 ;
for ( int i = 1 ; i < 100000 ; i++)
{
hash[i] += hash[i - 1 ];
}
for ( int i = 0 ; i < N; i++)
{
if (arr[i] == 0 )
{
System.out.println( "0" );
continue ;
}
System.out.print(hash[arr[i] - 1 ] + " " );
}
}
public static void main(String[] args)
{
int arr[] = { 3 , 4 , 1 , 1 , 2 };
int N = arr.length;
smallerNumbers(arr, N);
}
}
|
Python3
def smallerNumbers(arr, N):
hash = [ 0 ] * 100000
for i in range (N):
hash [arr[i]] + = 1
sum = 0
for i in range ( 1 , 100000 ):
hash [i] + = hash [i - 1 ]
for i in range (N):
if (arr[i] = = 0 ):
print ( "0" )
continue
print ( hash [arr[i] - 1 ], end = " " )
if __name__ = = "__main__" :
arr = [ 3 , 4 , 1 , 1 , 2 ]
N = len (arr)
smallerNumbers(arr, N)
|
C#
using System;
class GFG{
static void smallerNumbers( int []arr, int N)
{
int []hash = new int [100000];
for ( int i = 0; i < N; i++)
hash[arr[i]]++;
for ( int i = 1; i < 100000; i++)
{
hash[i] += hash[i - 1];
}
for ( int i = 0; i < N; i++)
{
if (arr[i] == 0)
{
Console.WriteLine( "0" );
continue ;
}
Console.Write(hash[arr[i] - 1] + " " );
}
}
public static void Main( string [] args)
{
int []arr = { 3, 4, 1, 1, 2 };
int N = arr.Length;
smallerNumbers(arr, N);
}
}
|
Javascript
<script>
function smallerNumbers(arr, N)
{
let hash = new Array(100000);
hash.fill(0);
for (let i = 0; i < N; i++)
hash[arr[i]]++;
let sum = 0;
for (let i = 1; i < 100000; i++)
{
hash[i] += hash[i - 1];
}
for (let i = 0; i < N; i++)
{
if (arr[i] == 0)
{
document.write( "0" + "</br>" );
continue ;
}
document.write(hash[arr[i] - 1] + " " );
}
}
let arr = [ 3, 4, 1, 1, 2 ];
let N = arr.length;
smallerNumbers(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N), where N is the maximum value of the array elements
Share your thoughts in the comments
Please Login to comment...