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.Constraints30 points1 <= T <= 1001 <= N <= 1000-106 <= A[i] <= 10670 points1 <= T <= 5001 <= N <= 10000-106 <= A[i] <= 106ExampleInput25-3 5 8 2 -465 -10 8 4 2 -3OutputYesNoExplanationExample 1:Possible values: 8 + (-4) = (-3) + 5 + 2.Example 2:No 2 elements exist whose sum is equal to the sum of the remaining array elements.
Question
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.Constraints30 points1 <= T <= 1001 <= N <= 1000-106 <= A[i] <= 10670 points1 <= T <= 5001 <= N <= 10000-106 <= A[i] <= 106ExampleInput25-3 5 8 2 -465 -10 8 4 2 -3OutputYesNoExplanationExample 1:Possible values: 8 + (-4) = (-3) + 5 + 2.Example 2:No 2 elements exist whose sum is equal to the sum of the remaining array elements.
Solution 1
This problem can be solved using a simple approach. Here are the steps:
-
First, calculate the sum of all elements in the array. Let's call this total sum.
-
Then, iterate through the array. For each element, subtract it from the total sum and divide the result by 2. Let's call this new sum.
-
Now, check if this new sum exists in the array. If it does, then there are two elements whose sum is equal to the sum of the remaining elements. If it doesn't, continue to the next element.
-
If you've gone through all elements and haven't found a match, then no such pair of elements exists.
Here is a Python code snippet that implements this approach:
def check_sum(arr):
total_sum = sum(arr)
for i in arr:
new_sum = (total_sum - i) / 2
if new_sum in arr:
return "Yes"
return "No"
This function takes an array as input and returns "Yes" if there are two elements whose sum is equal to the sum of the remaining elements, and "No" otherwise.
Solution 2
This problem can be solved using a simple approach. Here are the steps:
-
First, calculate the sum of all elements in the array. Let's call this total_sum.
-
Then, iterate over the array. For each element, subtract it from total_sum and divide the result by 2. Let's call this new_sum.
-
Now, check if new_sum exists in the array. If it does, print "Yes" and break the loop. If it doesn't, continue to the next element.
-
If you've iterated over the entire array and haven't found any matching elements, print "No".
Here is a Python code snippet that implements this approach:
T = int(input())
for _ in range(T):
N = int(input())
A = list(map(int, input().split()))
total_sum = sum(A)
found = False
for i in range(N):
new_sum = (total_sum - A[i]) / 2
if new_sum in A:
print("Yes")
found = True
break
if not found:
print("No")
This code first reads the number of test cases. For each test case, it reads the size of the array and the array itself. It then calculates the total sum of the array. It iterates over the array, calculating new_sum for each element and checking if it exists in the array. If it does, it prints "Yes" and breaks the loop. If it doesn't, it continues to the next element. If it has iterated over the entire array and hasn't found any matching elements, it prints "No".
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.
Given an array of integers of size N, print the sum of sum of all subarrays.Input FormatFirst line of input contains T - number of test cases. Its followed by 2T lines, the first line contains N - size of the array and second line contains the elements of the array.Output FormatFor each test case, print the sum of sum of all subarrays, separated by newline.Constraints10 points1 <= T <= 1001 <= N <= 10220 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 10001 <= N <= 104General Constraints-106 <= arr[i] <= 106ExampleInput333 4 521 231 -3 4Output4063ExplanationTest Case 1:[3] + [3,4] + [3,4,5] + [4] + [4,5] + [5] = 40Test Case 2:[1] + [1,2] + [2] = 6Test Case 3:[1] + [1,-3] + [1,-3,4] + [-3] + [-3,4] + [4] = 3
You are given an array of integers. For each element in the array, find the number of smaller elements on the right side and print the total count.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the sum of count of smaller elements on right side of each element in the array, separated by new line.Constraints30 points1 <= N <= 10370 points1 <= N <= 105General Constraints1 <= T <= 100-104 <= A[i] <= 104ExampleInput254 10 54 11 8615 35 25 10 15 12Output410ExplanationTest Case 1Smaller Elements on right side of 4 : 0Smaller Elements on right side of 10 : 1Smaller Elements on right side of 54 : 2Smaller Elements on right side of 11 : 1Smaller Elements on right side of 8 : 0Total Count = 0 + 1 + 2 + 1 + 0 = 4Test Case 2Smaller Elements on right side of 15 : 2Smaller Elements on right side of 35 : 4Smaller Elements on right side of 25 : 3Smaller Elements on right side of 10 : 0Smaller Elements on right side of 15 : 1Smaller Elements on right side of 12 : 0Total Count = 2 + 4 + 3 + 0 + 1 + 0 = 10
Given an array of size 3X+1, where every element occurs three times, except one element, which occurs only once. Find the element that occurs only once.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array (of the form 3X + 1) and second line contains the elements of the array.Output FormatFor each test case, print the number which occurs only once, separated by new line.Constraints30 points1 <= T <= 1001 <= N <= 10370 points1 <= T <= 1001 <= N <= 106ExampleInput2105 7 8 7 7 9 5 9 5 9710 20 20 30 20 30 30Output810
You are given an array of integers. Find the sum of XOR of all pairs formed by the elements of the array.Input FormatThe first line of input contains T - the number of test cases. It's followed by 2T lines, the first line contains N - the size of the array. The second line contains the elements of the array.Output FormatFor each test case, print the sum of XOR of all pairs formed by the elements of the array, separated by a new line.Constraints20 points1 <= T <= 1001 <= N <= 10000 <= A[i] <= 10580 points1 <= T <= 1001 <= N <= 1050 <= A[i] <= 105ExampleInput335 12 854 10 54 11 8615 35 25 10 15 12Output52560680ExplanationTest-Case 1(5 ^ 5) = 0(5 ^ 12) = 9(5 ^ 8) = 13(12 ^ 5) = 9(12 ^ 12) = 0(12 ^ 8) = 4(8 ^ 5) = 13(8 ^ 12) = 4(8 ^ 8) = 0The sum of all the above xor products = 52
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.