class SecondSmallestNumber{
public static void main(String args[])
{
int numbers[] = {6,3,37,12,46,5,64,21};
Arrays.sort(numbers);
System.out.println("Smallest Number: "+numbers[0]);
System.out.println("Second Smallest Number: "+numbers[1]);
}
}
Java interview Program to find second smallest number in an integer array without sorting the elements.
class SecondSmallestNumber{
int[] x ={10,11,12,13,14,6,3,-1};
int small=x[0];
for(int i=0;i<x.length;i++)
{
if(x[i]<small)
{
small=x[i];
}
}
int sec_Small=x[0];
for(int i=0;i<x.length;i++)
{
if(x[i]<sec_Small && x[i]!=small)
{
sec_Small=x[i];
}
}
System.out.println("Second Smallest Number: "sec_Small);
}
}
No comments:
Post a Comment