I am currently working on Hackerranks practice questions and encountered their compareTriplets practice. In this, we are required to take in two arrays and compare each element to the other. If one is bigger than the other we reward a point to the bigger one. For example, in an array of [5, 6, 7] and another array of [3, 6, 10] we would return a [1,1] since each side had one number greater than the other. So far I have tested this exact function and rather than returning a [1,1] I return a [3,0] I have no idea why it is not working and would like to know why the method is only adding to the aArray points.
import java.io.*;
import java.util.*;
public class Solution {
static int[] compareTriplets(int[] a, int[] b) {
int aPoints = 0;
int bPoints = 0;
for(int i = 0; i < a.length; i++){
if(a[i] > b[i]){
aPoints++;
}
if(a[i] < b[i]){
bPoints++;
}
}
int[] newArray = {aPoints, bPoints};
return newArray;
}
public static void main(String[] args) throws IOException {
Scanner scan = new Scanner(System.in);
int []aArray = new int[3];
for(int j = 0; j < aArray.length; j++){
aArray[j] = scan.nextInt();
}
System.out.println(Arrays.toString(aArray));
int []bArray = new int[3];
for(int c = 0; c < bArray.length; c++){
aArray[c] = scan.nextInt();
}
System.out.println(Arrays.toString(aArray));
int[] fArray = compareTriplets(aArray, bArray);
System.out.println(Arrays.toString(fArray));
}
}
Aucun commentaire:
Enregistrer un commentaire