Advanced Java Programming - Old Questions
Question Answer Details
4. An array is called balanced if it's even numbered elements (a[0], a[2], etc.) are even and its odd numbered elements (a[1], a[3],etc.) are Odd. Write a function named is Balanced that accepts an array of integers and returns 1 if the array is balanced otherwise it returns 0. [5]
Answer
AI Generated Answer
AI is thinking...
Official Answer
public static int Balanced(int[] array){
for(int i=0; i<array.length; i++){
if(i%2==0){
if(array[i]%2 != 0){
return -1;
}
}
else if(i%2 != 0){
if(array[i]%2 == 0){
return -1;
}
}
}
return 1;
}