Free Puzzle Questions with Answer using Java, C#, JavaScript and PHP

Java Quiz - Check For Middle Number

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package checkformiddlenumber;

/**
 *
 * @author Bereket
 */
public class CheckForMiddleNumber {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int[] sampleData = {3, 2, 1, 4, 1};
        
        int result = CheckArray(sampleData);
        
        System.out.println(String.valueOf(result));
    }
    
    
    //HOW TO CHECK IF ARRAY IS CENTERED
    private static int CheckArray(int[] givenArray){
        
        //checks if the given array is empty or not
        if(givenArray == null || givenArray.length == 0){
            return 0;
        }
        else if((givenArray.length%2) == 0){
            return 0;
        }
        else{
			//get middle index of the given array
            int middleIndex = givenArray.length/2; 
			//get middle value from the given array usng the middle index found above
            int middleElement = givenArray[middleIndex]; 
            
            for(int i=0; i < givenArray.length; i++){
                //check if current element is lessthan the middle element while 'i' 
				//is not the middle index
                if((givenArray[i] <= middleElement) && i != middleIndex){
                    return 0;
                }
            }
            
            //if we got here, it means the array is CENTERED
            return 1;
        }
    }
    
}