Given an array of elements. Find two elements in the array such that their sum is equal to the given element K.Input format :The first line of the input consists of the value of n.The second line of the input consists of the array of elements separated by space.The third line of the input consists of the sum.Output format :The output prints whether the array has a pair of elements with the given sum.Sample test cases :Input 1 :61 4 45 6 10 -816Output 1 :Array has two elements with given sum 16Input 2 :61 4 45 6 10 -860Output 2 :Array doesn't have two element
Question
Given an array of elements. Find two elements in the array such that their sum is equal to the given element K.Input format :The first line of the input consists of the value of n.The second line of the input consists of the array of elements separated by space.The third line of the input consists of the sum.Output format :The output prints whether the array has a pair of elements with the given sum.Sample test cases :Input 1 :61 4 45 6 10 -816Output 1 :Array has two elements with given sum 16Input 2 :61 4 45 6 10 -860Output 2 :Array doesn't have two element
Solution
Here is a step-by-step solution to the problem:
Step 1: First, we need to read the input. The first line of the input gives us the number of elements in the array (n). The second line gives us the array of elements. The third line gives us the sum (K) we are looking for.
Step 2: Next, we initialize an empty hash set. This will help us keep track of the elements we have encountered so far.
Step 3: We then iterate over the array. For each element, we calculate the difference between the sum K and the current element. Let's call this difference "diff".
Step 4: We check if "diff" is present in our hash set. If it is, that means we have found
Similar Questions
Sum of 2 NumbersGiven an array, check if there exist 2 elements of the array such that their sum is equal to the sum of the remaining elements of the array.Input FormatThe first line of input contains T - the number of test cases. It is followed by 2T lines, the first line contains N - the size of the array. The second line contains N integers - elements of the array.Output FormatFor each test case, print "Yes" if such elements exist, and "No" otherwise, separated by a new line.
You are given an integer array and a positive integer K. You have to tell if there exists i,j,k in the given array such that ar[i]+ar[j]+ar[k]=K, i≠j≠k.Input FormatThe first line of input contains T - the number of test cases. Its followed by 2T lines, the first line contains N and K - the size of the array and the number K. The second line contains the elements of the array.Output FormatFor each test case, print "true" if the arrays contains such elements, false otherwise, separated by new line.Constraints30 points1 <= T <= 1003 <= N <= 10070 points1 <= T <= 1003 <= N <= 1000General Constraints-100000 <= A[i] <= 1000000 <= K <= 100000ExampleInput35 601 20 40 100 8012 5412 45 52 65 21 645 234 -100 14 575 -80 1123 155 5 5Outputfalsetruetrue
Given an array of integers and a number K, check if there exist a pair of indices i,j s.t. a[i] + a[j] = K and i!=j.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, first line of each test case contains N - the size of the array and K, and the next line contains N space separated integers - the elements of the array.Output FormatFor each test case, print "True" if such a pair exists, "False" otherwise, separated by newline.Constraints30 points1 <= T <= 1002 <= N <= 100070 points1 <= T <= 3002 <= N <= 10000General Constraints-108 <= K <= 108-108 <= ar[i] <= 108ExampleInput35 -15-30 15 20 10 -102 2010 104 7-4 0 10 -7OutputTrueTrueFalse
Write a Python class to find a pair of elements (indices of the two numbers) from a given array whose sum equals a specific target number.Note: There will be one solution for each input and do not use the same element twice.Sample Test CasesTest Case 1:Expected Output:Enter·comma-separated·numbers:·2,5,1,5,6,0Enter·target·value:·10index1=1,·index2=3Test Case 2:Expected Output:Enter·comma-separated·numbers:·3,4,5,6,2,9Enter·target·value:·9index1=0,·index2=3Test Case 3:Expected Output:Enter·comma-separated·numbers:·9,8,1,4,5Enter·target·value:·4No·valid·indices·found·for·the·target·value.
Given an array, check if there exist 2 elements of the array such that their sum is equal to the sum of the remaining elements of the array.
Upgrade your grade with Knowee
Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.