Find three element from different three arrays such that that a + b + c = sum in Python


Suppose we have three arrays A, B, C and another value called "sum", We have to check whether there are three elements a, b, c such that a + b + c = sum and a, b and c should be under three different arrays.

So, if the input is like A = [2,3,4,5,6], B = [3,4,7,2,3], C = [4,3,5,6,7], sum = 12, then the output will be True as 4+2+6 = 12, and 4, 2, 6 are taken from A, B, C respectively.

To solve this, we will follow these steps −

  • for i in range 0 to size of A, do

    • for j in range 0 to size of B, do

      • for k in range 0 to size of C, do

        • if A[i] + B[j] + C[k] is same as sum, then

          • return True

  • return False

Example

Let us see the following implementation to get better understanding −

 Live Demo

def is_sum_from_three_arr(A, B, C, sum):
   for i in range(0 , len(A)):
      for j in range(0 , len(B)):
         for k in range(0 , len(C)):
            if (A[i] + B[j] + C[k] == sum):
               return True
   return False
A = [2,3,4,5,6]
B = [3,4,7,2,3]
C = [4,3,5,6,7]
sum = 12
print(is_sum_from_three_arr(A, B, C, sum))

Input

[2,3,4,5,6], [3,4,7,2,3], [4,3,5,6,7], 12

Output

True

Updated on: 27-Aug-2020

131 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements