0 votes
I've recently started learning python and I have an assignment which I need help with.

Write python code to check if there exist two elements in the array whose sum is equal to the sum of the entire array Jun 17, 2019 in Python 1,455 views
Hahah! Its an assignment and you should do it by yourself. I can help with working around the logic.
Hahah! Help me out with the logic. That'd be good enough.

## 2 answers to this question.

0 votes
You can find the sun of the array. Then for each element in the array a[i], look for a value that is equal to sum - a[i]. answered Jun 17, 2019 by Viky
0 votes

You can try something like this Harsh:

```def checkPair(a, n):
s = set()
sum = 0
# Find sum of whole array
for i in range(n):
sum += a[i]
# / If sum of array is not
# even than we can not
# divide it into two part
if sum % 2 != 0:
return False
sum = sum / 2
# For each element arr[i], see if
# there is another element with
# value sum - arr[i]
for i in range(n):
val = sum - a[i]
if a[i] not in s:
s.add(a[i])
# If element exist than
# return the pair
if val in s:
print("Pair elements are",
a[i], "and", int(val))
# Driver Code
a = [2, 11, 5, 1, 4, 7]
n = len(a)
if checkPair(a, n) == False:
print("No pair found")
``` answered Jun 17, 2019 by Ayman

0 votes
1 answer

## What is the logic to check if a number is prime or not in python?

n = int (input ('ENTER NUMBER TO ...READ MORE

0 votes
1 answer

## How to compare two JSON objects with the same elements in a different order equal?

Hello @kartik, If you want two objects with ...READ MORE

+1 vote
1 answer

## How to check if a string is null in python

Try this: if cookie and not cookie.isspace(): # the ...READ MORE

0 votes
1 answer

## Is it possible to invert a Numpy boolean array in Python using the tilde operator (~)?

Good question, glad you brought this up. I ...READ MORE

0 votes
2 answers

## how can i randomly select items from a list?

You can also use the random library's ...READ MORE

+1 vote
2 answers

## how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

0 votes
0 answers

## how do i use the enumerate function inside a list?

can you give an example using a ...READ MORE

+5 votes
6 answers

## Lowercase in Python

You can simply the built-in function in ...READ MORE

0 votes
1 answer

## Is arr.__len__() the preferred way to get the length of an array in Python?

my_list = [1,2,3,4,5,6,7] len(my_list) # 7 The same works for ...READ MORE

0 votes
1 answer

## Is there a foreach function in python and is there a way to implement it if there isnt any

Every occurence of "foreach" I've seen (PHP, ...READ MORE