Write a java program to find Nth smallest number without sorting :-

import java.util.Scanner;
public class NthLeastNumber {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
int arr[]=inputArray();
System.out.println("Enter which smallest element you want :");
int n=sc.nextInt();
int sm=nThsmallest(arr,n);
System.out.println(n+" biggest is "+sm);
}
static int nThsmallest(int[] ar,int n) {
for(int i=0;i<ar.length;i++) {
int count=0;
for(int j=0;j<ar.length;j++) {
if(ar[j]<ar[i])
count++;
}
if(count==n-1)
return ar[i];
}
return 0;
}
static int[] inputArray() {
Scanner sc=new Scanner(System.in);
System.out.println("Enter Size of Array : ");
int n=sc.nextInt();
int count=0;
int arr[]=new int[n];
System.out.println("enter value in array : ");
for (int i=0; i<n; i++) {
arr[i]=sc.nextInt();
}
return arr;
}
}


Comments

  1. sorry now i have update because previous has input method present in another class
    so i have update it

    ReplyDelete

Post a Comment

Popular posts from this blog

program to calculate product of each column in matrix