Program to Print How Many Numbers Smaller than Current Number

Description

To find numbers smaller than given number, take a loop and compare current
element with other elements, if other element is small increase the count.
 
Example: arr = [5,2,9,4,7]
		 output = [2,0,4,1,3]

C/C++

/* C program to print how many numbers smaller than current number */
//Save it as NumerSmallerThanCurrentNumber.c

#include<stdio.h>
int* countSmallerNumber(int arr[], int n);
int main(){

    int i, j, n;

    printf("Enter the size of array : ");
    scanf("%d",&n);

    
    //Declaring the array
    int arr[n];

    printf("Enter the array elements : ");

    for(i=0;i<n;i++) {

        scanf("%d",&arr[i]);
    }

    printf("The Entered elements are : ");
    for(i=0;i<n;i++) {

        printf("%d ",arr[i]);
    }

    int* listOfElements = countSmallerNumber(arr,n);

    printf("\nThe list of numbers smaller than current number : ");

    for(i=0;i<n;i++) {

        printf("%d ",listOfElements[i]);
    }

    return 0;
}

//Function for count the smaller element than the current element
int* countSmallerNumber(int arr[], int n){
    
    //Take a array for storing the the count of smaller element than the current element
    int* countNumber;
    int i, j;

    for(i=0;i<n;i++) {

        int count=0;

        for(j=0;j<n;j++) {
            
            //Compare current elements with other elements
            if((arr[i]>arr[j]) && (i!=j)) {
                
                //Increase the count, if other element is smaller than the current element
                count++;
            }
        }
        countNumber[i]=count;
    }

    return countNumber;
}
Input:
Enter the size of array : 
5
Enter the array elements : 
5
2
9
4
7

Output:
The Entered elements are : 
5 2 9 4 7 
The list of numbers smaller than current number : 
2 0 4 1 3

Java

/* Java program to print how many numbers smaller than current number */
//Save it as NumerSmallerThanCurrentNumber.java

import java.io.*;
import java.util.Scanner;

public class NumerSmallerThanCurrentNumber {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        
        System.out.println("Enter the size of array : ");
        int n = scanner.nextInt();
        
        
        //Declaring the array
        int arr[] = new int[n];
        
        System.out.println("Enter the array elements : ");

        for(int i=0;i<n;i++) {
            arr[i] = scanner.nextInt();
        }
        
        System.out.println("The Entered elements are : ");
        for(int i=0;i<n;i++) {
            System.out.print(arr[i] + " ");
        }
        
        int listOfElements[] = countSmallerNumber(arr);
        
        System.out.println("\nThe list of numbers smaller than current number : ");
        
        for(int i=0;i<listOfElements.length;i++) {
            
            System.out.print(listOfElements[i]+" ");
        }
    }
    
    //Function for count the smaller element than the current element
    private static int[] countSmallerNumber(int[] arr) {
        
        //Take a array for storing the the count of smaller element than the current element
        int countNumber[] = new int[arr.length];
        
        for(int i=0;i<arr.length;i++) {
            
            int count=0;
            
            for(int j=0;j<arr.length;j++) {
                
                //Compare current elements with other elements
                if((arr[i]>arr[j]) && (i!=j)) {
                    
                    //Increase the count, if other element is smaller than the current element
                    count++;
                }
            }
            countNumber[i]=count;
        }
        
        return countNumber;
    }
}
Input:
Enter the size of array : 
5
Enter the array elements : 
5
2
9
4
7

Output:
The Entered elements are : 
5 2 9 4 7 
The list of numbers smaller than current number : 
2 0 4 1 3

Related Programs

1) Find Smallest element and it’s position of an Array
2) Program for finding Second Largest Element Of Array
3) Program to swap maximum and minimum element of Array
4) Program to Find the smallest missing number
5) Program to Find missing odd number in first n odd number
6) Program to Find Value equal to index value
7) write a Program to find index of two array elements whose sum is equal to given value
8) Program to find maximum and second maximum if elements of array is space separated input
9) Program to make pair of elements alternatively
10) Program to Find K largest elements from array
Share Me

Leave a Reply