Find elements in given Array that are a factor of sum of remaining elements
Last Updated :
10 Feb, 2022
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:
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.
C++
// c++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find element
vector<int> Factor(vector<int> A)
{
// Sum of all element
int s = 0;
for (int i = 0; i < A.size(); i++)
{
s += A[i];
}
// Subtract each element from sum
vector<int> p;
for (int i : A)
p.push_back(s - i);
// Divide corresponding element
// from array p and l
vector<int> q;
for (int i = 0; i < A.size(); i++)
q.push_back(p[i] / A[i]);
// Check sum is divisible by
// corresponding element or not
vector<int> z;
for (int i = 0; i < q.size(); i++)
{
// First we divided element now multiple
// to check perfect divisibility of element
if (q[i] * A[i] == p[i])
z.push_back(A[i]);
}
return z;
}
// Driver code
int main()
{
vector<int> A = {2, 4, 6, 8, 10, 12, 14};
// Calling function
vector<int> b = Factor(A);
// Print required array
for (auto i : b)
{
cout << i << " ";
}
}
// This code is contributed by amreshkumar3.
Java
// Java program for the above approach
import java.util.ArrayList;
class GFG{
// Function to find element
static ArrayList<Integer> Factor(int[] A)
{
// Sum of all element
int s = 0;
for(int i = 0; i < A.length; i++)
{
s += A[i];
}
// Subtract each element from sum
ArrayList<Integer> p = new ArrayList<>();
for(int i : A)
p.add(s - i);
// Divide corresponding element
// from array p and l
ArrayList<Integer> q = new ArrayList<Integer>();
for(int i = 0; i < A.length; i++)
q.add((int) Math.floor(p.get(i) / A[i]));
// Check sum is divisible by
// corresponding element or not
ArrayList<Integer> z = new ArrayList<Integer>();
for(int i = 0; i < q.size(); i++)
{
// First we divided element now multiple
// to check perfect divisibility of element
if (q.get(i) * A[i] == p.get(i))
z.add(A[i]);
}
// If no such element found return -1
if (z.size() == 0)
return new ArrayList<Integer>();
return z;
}
// Driver code
public static void main(String args[])
{
int[] A = { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
ArrayList<Integer> b = Factor(A);
// Print required array
System.out.println(b);
}
}
// This code is contributed by gfgking
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
# corresponding 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)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG{
// Function to find element
static List<int> Factor(int[] A)
{
// Sum of all element
int s = 0;
for(int i = 0; i < A.Length; i++)
{
s += A[i];
}
// Subtract each element from sum
List<int> p = new List<int>();
foreach(int i in A)
p.Add(s - i);
// Divide corresponding element
// from array p and l
List<int> q = new List<int>();
for(int i = 0; i < A.Length; i++)
q.Add((int) Math.Floor((double)p[i] / A[i]));
// Check sum is divisible by
// corresponding element or not
List<int> z = new List<int>();
for(int i = 0; i < q.Count; i++)
{
// First we divided element now multiple
// to check perfect divisibility of element
if (q[i] * A[i] == p[i])
z.Add(A[i]);
}
// If no such element found return -1
if (z.Count == 0)
return new List<int>();
return z;
}
// Driver code
public static void Main(String []args)
{
int[] A = { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
List<int> b = Factor(A);
// Print required array
foreach(int i in b)
Console.Write(i+", ");
}
}
// This code is contributed by 29AjayKumar
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
// corresponding 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>
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.
C++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find sum of all elements of an array
int sum(vector<int>& A)
{
int res = 0;
for (auto it : A)
res += it;
return res;
}
// Function to find element
vector<int> Factor(vector<int>& A)
{
// Sum of all element
int s = sum(A);
vector<int> z;
// Loop to find the factors of sum.
for (int i = 0; i < A.size(); ++i) {
// a is sum of remaining elements.
int a = s - A[i];
// b is integer value or factor of b.
int b = a / A[i];
// Check the divisibility
if (b * A[i] == a)
z.push_back(A[i]);
}
// If no element found return -1
if (z.size() == 0)
return { -1 };
return z;
}
// Drive Code
int main()
{
vector<int> A = { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
vector<int> b = Factor(A);
// Print resultant element
for (auto it : b)
cout << it << " ";
return 0;
}
// This code is contributed by rakeshsahni
Java
// Java program for the above approach
import java.util.*;
class GFG
{
// Function to find sum of all elements of an array
static int sum(int[] A)
{
int res = 0;
for (int i = 0; i < A.length; i++) {
res += A[i];
}
return res;
}
// Function to find element
static ArrayList<Integer> Factor(int[] A)
{
// Sum of all element
int s = sum(A);
ArrayList<Integer> z = new ArrayList<Integer>();
// Loop to find the factors of sum.
for (int i = 0; i < A.length; ++i) {
// a is sum of remaining elements.
int a = s - A[i];
// b is integer value or factor of b.
int b = a / A[i];
// Check the divisibility
if (b * A[i] == a){
z.add(A[i]);
}
}
// If no element found return -1
if (z.size() == 0){
ArrayList<Integer> l1 = new ArrayList<Integer>();
l1.add(-1);
return l1;
}
return z;
}
// Drive Code
public static void main (String[] args)
{
int A[] = new int[] { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
ArrayList<Integer> b = Factor(A);
// Print resultant element
System.out.println(b);
}
}
// This code is contributed by Shubham Singh
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)
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG {
// Function to find sum of all elements of an array
static int sum(List<int> A)
{
int res = 0;
foreach(int it in A) res += it;
return res;
}
// Function to find element
static List<int> Factor(List<int> A)
{
// Sum of all element
int s = sum(A);
List<int> z = new List<int>();
// Loop to find the factors of sum.
for (int i = 0; i < A.Count; ++i) {
// a is sum of remaining elements.
int a = s - A[i];
// b is integer value or factor of b.
int b = a / A[i];
// Check the divisibility
if (b * A[i] == a)
z.Add(A[i]);
}
// If no element found return -1
if (z.Count == 0)
return new List<int>() { -1 };
return z;
}
// Drive Code
public static void Main()
{
List<int> A
= new List<int>() { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
List<int> b = Factor(A);
// Print resultant element
Console.Write("[ ");
int it;
for (it = 0; it < b.Count - 1; it++) {
Console.Write(b[it] + ", ");
}
Console.Write(b[it] + " ]");
}
}
// This code is contributed by ukasp.
JavaScript
<script>
// JavaScript program for the above approach
// Function to find sum of all elements of an array
function sum(A)
{
var res = 0;
for (var it of A){
res += parseInt(it);}
return res;
}
// Function to find element
function Factor(A)
{
// Sum of all element
var s = sum(A);
var z =[];
// Loop to find the factors of sum.
for (var i = 0; i < A.length; ++i) {
// a is sum of remaining elements.
var a = s - A[i];
// b is integer value or factor of b.
var b = parseInt(a / A[i]);
// Check the divisibility
if (b * A[i] == a)
z.push(A[i]);
}
// If no element found return -1
if (z.length == 0)
return [ -1 ];
return z;
}
// Drive Code
A = [ 2, 4, 6, 8, 10, 12, 14 ];
// Calling function
b = Factor(A);
// Print resultant element
for (var it of b)
document.write(it + " ");
//This code is contributed by Shubham Singh
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Sum of array elements which are prime factors of a given number Given an array arr[] of size N and a positive integer K, the task is to find the sum of all array elements which are prime factors of K. Examples: Input: arr[] = {1, 2, 3, 5, 6, 7, 15}, K = 35Output: 12Explanation: From the given array, 5 and 7 are prime factors of 35. Therefore, required sum = 5 +
8 min read
Check if the array has an element which is equal to sum of all the remaining elements Given an array of N elements, the task is to check if the array has an element that is equal to the sum of all the remaining elements. Examples: Input: a[] = {5, 1, 2, 2} Output: Yes we can write 5=(1+2+2) Input: a[] = {2, 1, 2, 4, 3} Output: No Approach: Suppose that the total elements in the array
10 min read
Minimize the sum of MEX by removing all elements of array Given an array of integers arr[] of size N. You can perform the following operation N times: Pick any index i, and remove arr[i] from the array and add MEX(arr[]) i.e., Minimum Excluded of the array arr[] to your total score. Your task is to minimize the total score. Examples: Input: N = 8, arr[] =
7 min read
Find prime factors of Array elements whose sum of exponents is divisible by K Given an array arr[] of N positive integers and an integer K., The task is to create a set of prime numbers such that the sum of all the powers of prime numbers in the prime factorization of all the array elements is divisible by K. Examples: Input: arr[] = {1, 2, 3}, K = 1 Output: {2, 3} Explanatio
12 min read
Element equal to the sum of all the remaining elements Given an array of N positive elements. The task is to find an element which is equal to the sum of all elements of array except itself. Examples: Input: arr[] = {1, 2, 3, 6} Output: 6 6 is the element which is equal to the sum of all remaining elements i.e. 1 + 2+ 3 = 6 Input: arr[] = {2, 2, 2, 2} O
4 min read
Replace every element of the array by sum of all other elements Given an array of size N, the task is to find the encrypted array. The encrypted array is obtained by replacing each element of the original array with the sum of the remaining array elements i.e. For every i, arr[i] = sumOfArrayElements - arr[i] Examples: Input: arr[] = {5, 1, 3, 2, 4} Output: 10 1
5 min read
Sum of element whose prime factors are present in array Given an array arr[] of non-negative integers where 2 ? arr[i] ? 106. The task is to find the sum of all those elements from the array whose prime factors are present in the same array. Examples: Input: arr[] = {2, 3, 10} Output: 5 Factor of 2 is 2 which is present in the array Factor of 3 is 3, als
10 min read
Find an element in array such that sum of left array is equal to sum of right array Given, an array of size n. Find an element that divides the array into two sub-arrays with equal sums. Examples: Input: 1 4 2 5 0Output: 2Explanation: If 2 is the partition, subarrays are : [1, 4] and [5] Input: 2 3 4 1 4 5Output: 1Explanation: If 1 is the partition, Subarrays are : [2, 3, 4] and [4
15+ min read
Sum of multiples of Array elements within a given range [L, R] Given an array arr[] of positive integers and two integers L and R, the task is to find the sum of all multiples of the array elements in the range [L, R]. Examples: Input: arr[] = {2, 7, 3, 8}, L = 7, R = 20 Output: 197 Explanation: In the range 7 to 20: Sum of multiples of 2: 8 + 10 + 12 + 14 + 16
7 min read
Find element in array that divides all array elements Given an array of n non-negative integers. Find such element in the array, that all array elements are divisible by it. Examples : Input : arr[] = {2, 2, 4}Output : 2 Input : arr[] = {2, 1, 3, 1, 6}Output : 1 Input: arr[] = {2, 3, 5}Output : -1 Brute Force Approach: The brute force approach to solve
7 min read