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

Java Quiz - Check Sum Of Given Integer Array

/*
 * 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 checksumofgivenintegerarray;

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

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        int[] sampleData = {1,2,3,4,5,6};
        
        boolean result = CheckGivenIntegerArray(sampleData, 17);
        
        System.out.println(result);
    }
    
    static boolean CheckGivenIntegerArray(int[] givenArray, int givenSum){
        for(int i=0; i < givenArray.length; i++){
            for(int j=0; j < givenArray.length; j++){
                for(int x=0; x < givenArray.length; x++){
                    //make sure each item being added is different item of the array
					if(i != j && j != x && i != x){
						//add three different items in the array
                        int sum = givenArray[i] + givenArray[j] + givenArray[x];
                        //check the sum of three different items of the array is 
						//equal to the given sum value
						if(sum == givenSum){
                            return true;
                        }
                    }
                }
            }
        }
        
        return false;
    }
    
}