Knowee
Questions
Features
Study Tools

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.

Question

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.

🧐 Not the exact question you are looking for?Go ask a question

Solution 1

Sure, here is a Python solution for the problem:

def check_sum(arr):
    # Calculate the total sum of the array
    total_sum = sum(arr)
    
    # Create a set to store the elements of the array
    elements = set()
    
    for num in arr:
        # Calculate the target sum
        target_sum = total_sum - num
        
        # If the target sum is divisible by 2, it means there might be two elements whose sum is equal to the sum of the remaining elements
        if target_sum % 2 == 0:
            target_sum //= 2
            
            # If the target sum is in the

This problem has been solved

Solution 2

Sure, here is a step-by-step solution in Python:

  1. First, calculate the sum of all elements in the array. Let's call this total_sum.
total_sum = sum(array)
  1. Then, iterate over the array using two nested loops. The outer loop starts from the first element and the inner loop starts from the next element.
for i in range(len(array)):
    for j in range(i+1, len(array)):
  1. In each iteration, calculate the sum of the two elements. If this sum is equal to half of total_sum, then these two elements satisfy the condition.
        if array[i] + array[j] == total_sum / 2:
            return True
  1. If no such pair of elements is found after checking all pairs, then no such pair exists.
return False

Here is the complete function:

def check_sum(array):
    total_sum = sum(array)
    for i in range(len(array)):
        for j in range(i+1, len(array)):
            if array[i] + array[j] == total_sum / 2:
                return True
    return False

This function returns True if such a pair exists and False otherwise.

This problem has been solved

Similar Questions

Given an array A[] of n numbers and another number x, the task is to check whether or not there exist two elements in A[] whose sum is exactly x. Examples: Input: arr[] = {0, -1, 2, -3, 1}, x= -2Output: YesExplanation: If we calculate the sum of the output,1 + (-3) = -2Input: arr[] = {1, -2, 1, 0, 5}, x = 0Output: No

Given an integer array and another integer element. The task is to find if the given element is present in array or not.

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

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.

Balanced Array Pavani gives Ramya an array of integers. Her challenge is to find an element of the array such that the sum of all elements to the left is equal to the sum of all elements to the right. For instance, given the array=[5,6,8,11], 8 is between two subarrays that sums to 11. You will be given arrays of integers and must determine whether there is an element that meets the criterion. Function DescriptionComplete the program that it should return a string, either ‘YES’ if there is an element meeting the criterion or ‘NO’ otherwise. Input FormatThe first line contains T, the number of test cases.The next T pairs of lines each represent a test case.The first line contains n, the number of elements in the array arr.The second line contains  n space-separated integers arr[i] where 0<=iConstraints1<=T<=01<=n<=1051<=arr[I]<=1040<= I Output FormatFor each test case print YES if there exists an element in the array, such that the sum of the elements on its left is equal to the sum of the elements on its right; otherwise print NO.

1/3

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.