Created
December 10, 2018 02:46
-
-
Save thmain/7b952f1b552f64544774e73b4bae6370 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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