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.
Question
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.
Solution
Here is a Python class that solves the problem:
class Solution:
def twoSum(self, nums, target):
h = {}
for i, num in enumerate(nums):
n = target - num
if n not in h:
h[num] = i
else:
return [h[n], i]
This class has a method twoSum that takes a list of numbers and a target number as input. It creates a dictionary h where for each number num in the list, it checks if target - num is in the dictionary. If it is not, it adds num to the dictionary with its index as the value. If it is, it means we have found two numbers whose sum is equal to the target, and it returns their indices.
You can use this class like this:
s = Solution()
print(s.twoSum([2,5,1,5,6,0], 10)) # Output: [1, 3]
print(s.twoSum([3,4,5,6,2,9], 9)) # Output: [0, 3]
print(s.twoSum([9,8,1,4,5], 4)) # Output: None
In the first test case, the numbers at indices 1 and 3 (5 and 5) sum up to 10. In the second test case, the numbers at indices 0 and 3 (3 and 6) sum up to 9. In the third test case, there are no two numbers that sum up to 4, so the method returns None.
Similar Questions
1.1 Two SumGiven an array of integers nums and an integer target, return indices of the two numbers such that theyadd up to target. You may assume that each input would have exactly one solution, and you may not usethe same element twice. You can return the answer in any order.Input: nums = [2, 7, 11, 15], target = 9Output: [0, 1]Explanation: Because nums[0] + nums[1] == 9, so return [0, 1].Input: nums = [3, 2, 4], target = 6Output: [1, 2]Input: nums = [3, 3], target = 6Output: [0, 1]Hints:def twoSum(self, nums: List[int], target: int) -> List[int]:a=[]# Write code here…return a1.2 Contains DuplicateGiven an integer array nums, return true if any value appears at least twice in the array, and return false ifevery element is distinct.Input: nums = [1, 2, 3, 1]Output: trueInput: nums = [1, 2, 3, 4]Output: falseInput: nums = [1, 1, 1, 3, 3, 4, 3, 2, 4, 2]Output: trueHints:
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
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.You may assume that each input would have exactly one solution, and you may not use the same element twice.
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.
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
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.