Last active
May 6, 2020 20:03
-
-
Save Lavanyagaur22/4f0c52c68ce5030398c1395c191361b1 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 static ArrayList<Float> final_faces_array(float percent, float[] arr, float comparable_percent) { | |
float len = arr.length; //length of array | |
float min_no = (percent / 100) *len; //number of faces wanted | |
float last_face = 0; | |
float secondlast_face = 0; | |
//maintain an arraylist because if we keep an array we would have to initialize it with the size which can be max = len, but in case | |
//the number of elements are less then array space is wasted. | |
ArrayList<Float> faceslist = new ArrayList<>(); | |
for (int i = 0; i < min_no; i++) | |
faceslist.add(arr[i]); | |
if (faceslist.size() != 0) { | |
last_face = faceslist.get(faceslist.size() - 1); | |
} | |
if (faceslist.size() - 2 >= 0) { | |
secondlast_face = faceslist.get(faceslist.size() - 2); | |
} | |
//checking whether the last element is comparable with the second last element | |
if (secondlast_face != 0 && last_face != 0) { | |
boolean isComparable = check_comparable(last_face, secondlast_face, comparable_percent); | |
if (!isComparable) { | |
faceslist.remove(faceslist.size() - 1); | |
return faceslist; | |
} | |
} | |
for (float i = min_no; i < len; i++) { | |
if (last_face != 0) { | |
float next_face = arr[(int)i]; | |
if (check_comparable(next_face, last_face, comparable_percent)) { | |
faceslist.add(next_face); | |
} | |
} else | |
break; | |
} | |
return faceslist; | |
} | |
private static boolean check_comparable(float last_face, float secondlast_face, float comparable_percent) { | |
if (last_face / secondlast_face <= comparable_percent) | |
return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment