i have a problem with an exam test. I have two arrays: int[] a, int[] b, with random values. E1 return true if there are two elements of b greater then each element of a. How do I write "there are two elements of b greater than each element of a"? I can write "there is ONE elements of b greater than each element of a" in this way:
public static boolean e1(int[] a, int[] b){
boolean ris = false;
boolean ogniA = true;
int i = 0;
if(a != null && a.length != 0){
while(ogniA && i<a.length){
boolean es1= false;
boolean es2 = false;
int j = 0;
if(b!= null && b.length != 0){
while(!es1 && j < b.length){
if(j%2!=0){
es1 = a[i] < b[j];
}
j++;
}ris = es1 ;
}
i++;
}
}
return ris;
I need something like this. Thanks for help.
So I would solve this problem, by dividing it into smaller problems. 1st you need to find the 2 biggest values in the first array. I presume this should be abstract and work for every array
Code (using the example from Andronicus):
"E1 return true if there are two elements of b greater then each element of a"
Basically you need the second greater value of array B and the greater value of the array A
int[] a = {3, 5, 7, 9};
int[] b = {2, 4, 3, 10, 11};
int secondGreatestOfB = IntStream.of(b).boxed()
.sorted(Comparator.reverseOrder())
.limit(2)
.sorted(Comparator.naturalOrder())
.limit(1)
.findFirst().orElse(0);
System.out.println(areTheTwoBiggestNumbersOfArrayBbiggerThanAllValuesFromArrayA(a, secondGreatestOfB));
private static boolean areTheTwoBiggestNumbersOfArrayBbiggerThanAllValuesFromArrayA(int[] a, int secondGreatestOfArrayB) {
return secondGreatestOfArrayB > IntStream.of(a).max().orElse(0);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.