Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

Find A Pair With The Given Sum In An Array

class Main
{
    // Naive method to find a pair in an array with a given sum
    public static void findPair(int[] nums, int target)
    {
        // consider each element except the last
        for (int i = 0; i < nums.length - 1; i++)
        {
            // start from the i'th element until the last element
            for (int j = i + 1; j < nums.length; j++)
            {
                // if the desired sum is found, print it
                if (nums[i] + nums[j] == target)
                {
                    System.out.println("Pair found (" + nums[i] + "," + nums[j] + ")");
                    return;
                }
            }
        }
 
        // we reach here if the pair is not found
        System.out.println("Pair not found");
    }
 
    public static void main (String[] args)
    {
        int[] nums = { 8, 7, 2, 5, 3, 1 };
        int target = 10;
 
        findPair(nums, target);
    }
}
Source by www.techiedelight.com #
 
PREVIOUS NEXT
Tagged: #Find #A #Pair #With #The #Given #Sum #In #An #Array
ADD COMMENT
Topic
Name
8+4 =