The Wayback Machine - https://web.archive.org/web/20211223003822/https://www.geeksforgeeks.org/find-elements-in-given-array-that-are-a-factor-of-sum-of-remaining-elements/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Find elements in given Array that are a factor of sum of remaining elements

  • Last Updated : 22 Dec, 2021

Given an array A[] of size N, the task is to find the elements in the array which are factors of the sum of the remaining element. So just select an element from an array and take the sum of the remaining elements and check whether the sum is perfectly divisible by the selected element or not. If it is divisible then return the element.

Examples:

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

Input: A[] = {2, 4, 6, 8, 10, 12, 14}
Output: [2, 4, 8, 14]
Explanation: 
1. Take sum for remaining element except selected one.
2. For element 2, sum of remaining element is 4+6+8+10+12+14=54 
3. Similarly for complete array: [54, 52, 50, 48, 46, 44, 42]
3. 54/2, 52/4, 48/8, 42/14 are perfectly divisible so resultant elements are [2, 4, 8, 14]



Input: A[]= {3, 6, 8, 10, 7, 15}
Output: [7]

 

Naive Approach: Take the sum of all elements from the array. Now subtract each element one by one from sum and append it to new array p[]. Divide each sum by the corresponding index element from a given array and append it to new array q[ ]. Multiply the corresponding element from array A[] and array q[] and compare it with similar indexed elements from array p[]. If they are equal then append it to a new array z[ ]. If no such element is found return -1.

Below is the implementation of the above approach.

Python3




# Python program for the above approach
 
# Function to find element
def Factor(A):
   
    # Sum of all element
    s = sum(A)
     
    # Subtract each element from sum
    p =[]
    for i in A:
        p.append(s-i)
 
    # Divide corresponding element
    # from array p and l
    q =[]
    for i in range(len(A)):
        q.append(p[i]//A[i])
 
    # Check sum is divisible by
    # corrsponding element or not
    z =[]
    for i in range(len(q)):
       
          # First we divided element now multiple
        # to check perfect divisibility of element
        if q[i]*A[i]== p[i]:
            z.append(A[i])
             
    # If no such element found return -1
    if len(z)== 0:
      return -1
    return z
 
A = [2, 4, 6, 8, 10, 12, 14]
 
# Calling function
b = Factor(A)
 
# Print required array
print(b)

Javascript




  <script>
      // JavaScript code for the above approach
 
      // Function to find element
      function Factor(A) {
 
          // Sum of all element
          let s = 0;
 
          for (let i = 0; i < A.length; i++) {
              s += A[i]
          }
 
          // Subtract each element from sum
          p = []
          for (i of A)
              p.push(s - i)
 
          // Divide corresponding element
          // from array p and l
          q = []
          for (i = 0; i < A.length; i++)
              q.push(Math.floor(p[i] / A[i]))
 
          // Check sum is divisible by
          // corrsponding element or not
          z = []
          for (let i = 0; i < q.length; i++) {
 
              // First we divided element now multiple
              // to check perfect divisibility of element
              if (q[i] * A[i] == p[i])
                  z.push(A[i])
          }
          // If no such element found return -1
          if (z.length == 0)
              return -1
          return z
      }
      A = [2, 4, 6, 8, 10, 12, 14]
 
      // Calling function
      b = Factor(A)
 
      // Print required array
      document.write(b)
 
// This code is contributed by Potta Lokesh
  </script>

 
 

Output
[2, 4, 8, 14]

 

Time Complexity: O(N)
Auxiliary Space: O(N)

 

Efficient Approach: In this approach, there is no need to use multiple loops and multiple arrays. so space complexity and time complexity will be decreased. In this, all the subtraction, division, multiplication operation are performed in a single loop. Follow the steps below to solve the problem:

 

  • Initialize the variable s as the sum of the array A[].
  • Initialize the array z[] to store the result.
  • Iterate over the range [0, len(A)) using the variables i and perform the following tasks:
    • Initialize the variable a as s-l[i], b as a/A[i].
    • If b*A[i] equals a then append A[i] into z[].
  • After performing the above steps, print -1 if the resultant array is empty, else print the elements of the array z[] as the answer.

 

Below is the implementation of the above approach.

 

Python3




# Python program for the above approach
 
# Function to find element
def Factor(A):
 
    # Sum of all element
    s = sum(A)
    z = []
 
    # Loop to find the factors of sum.
    for i in range(len(A)):
 
        # a is sum of remaining elements.
        a = s-A[i]
 
        # b is integer value or factor of b.
        b = a//A[i]
 
        # Check the divisibility
        if b * A[i] == a:
            z.append(A[i])
 
    # If no element found return -1
    if len(z) == 0:
        return -1
 
    return z
 
 
A = [2, 4, 6, 8, 10, 12, 14]
 
# Calling function
b = Factor(A)
 
# Print resultant element
print(b)
Output
[2, 4, 8, 14]

Time Complexity: O(N)
Auxiliary Space: O(1)




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!