Skip to content

Instantly share code, notes, and snippets.

@thmain
Created December 10, 2018 02:46
Show Gist options
  • Save thmain/7b952f1b552f64544774e73b4bae6370 to your computer and use it in GitHub Desktop.
Save thmain/7b952f1b552f64544774e73b4bae6370 to your computer and use it in GitHub Desktop.
public class ThreeSmallestElement {
public static void leastThreeElements(int [] arrA){
if(arrA.length<3){
System.out.println("Invalid Input, array size is less than 3");
}
int first=Integer.MAX_VALUE;
int second=Integer.MAX_VALUE;
int third = Integer.MAX_VALUE;
for (int i = 0; i <arrA.length ; i++) {
int current = arrA[i];
if(first>current){
third = second;
second = first;
first = current;
}else if(second>current){
third = second;
second = current;
}else if(third>current){
third=current;
}
}
System.out.println("least three elements are: " + first + " " + second + " " + third);
}
public static void main(String[] args) {
int [] arrA = new int [] { 6, 8, 1, 9, 2, 10};
leastThreeElements(arrA);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment