查找给定数组中作为剩余元素总和因子的元素
给定一个大小为N的数组A[] ,任务是找到数组中的元素,这些元素是剩余元素之和的因子。因此,只需从数组中选择一个元素并获取剩余元素的总和,然后检查总和是否可以被所选元素完全整除。如果它是可分的,则返回该元素。
例子:
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]
朴素的方法:从数组中获取所有元素的总和。 现在从 sum 中逐个减去每个元素,并将其附加到新数组p[]。将每个总和除以给定数组中的相应索引元素,并将其附加到新数组q[]。将数组A[]和数组q[]中的相应元素相乘,并将其与数组p[]中类似的索引元素进行比较。如果它们相等,则将其附加到新数组z[ ] 。如果没有找到这样的元素,则返回-1。
下面是上述方法的实现。
C++
// c++ program for the above approach
#include
using namespace std;
// Function to find element
vector Factor(vector 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 p;
for (int i : A)
p.push_back(s - i);
// Divide corresponding element
// from array p and l
vector 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 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 A = {2, 4, 6, 8, 10, 12, 14};
// Calling function
vector 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 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 p = new ArrayList<>();
for(int i : A)
p.add(s - i);
// Divide corresponding element
// from array p and l
ArrayList q = new ArrayList();
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 z = new ArrayList();
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();
return z;
}
// Driver code
public static void main(String args[])
{
int[] A = { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
ArrayList 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 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 p = new List();
foreach(int i in A)
p.Add(s - i);
// Divide corresponding element
// from array p and l
List q = new List();
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 z = new List();
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();
return z;
}
// Driver code
public static void Main(String []args)
{
int[] A = { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
List b = Factor(A);
// Print required array
foreach(int i in b)
Console.Write(i+", ");
}
}
// This code is contributed by 29AjayKumar
Javascript
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find sum of all elements of an array
int sum(vector& A)
{
int res = 0;
for (auto it : A)
res += it;
return res;
}
// Function to find element
vector Factor(vector& A)
{
// Sum of all element
int s = sum(A);
vector 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 A = { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
vector 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 Factor(int[] A)
{
// Sum of all element
int s = sum(A);
ArrayList z = new ArrayList();
// 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 l1 = new ArrayList();
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 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 A)
{
int res = 0;
foreach(int it in A) res += it;
return res;
}
// Function to find element
static List Factor(List A)
{
// Sum of all element
int s = sum(A);
List z = new List();
// 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() { -1 };
return z;
}
// Drive Code
public static void Main()
{
List A
= new List() { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
List 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
[2, 4, 8, 14]
时间复杂度: O(N)
辅助空间: O(N)
高效方法:在这种方法中,不需要使用多个循环和多个数组。所以空间复杂度和时间复杂度会降低。在这种情况下,所有的减法、除法、乘法运算都在一个循环中执行。请按照以下步骤解决问题:
- 将变量s初始化为数组A[] 的和。
- 初始化数组z[]以存储结果。
- 使用变量i遍历范围[0, len(A))并执行以下任务:
- 将变量a初始化为sl[i],将 b初始化为a/A[i]。
- 如果b*A[i]等于a,则将A[i]附加到z[] 中。
- 执行上述步骤后,如果结果数组为空,则打印-1 ,否则打印数组z[]的元素作为答案。
下面是上述方法的实现。
C++
// C++ program for the above approach
#include
using namespace std;
// Function to find sum of all elements of an array
int sum(vector& A)
{
int res = 0;
for (auto it : A)
res += it;
return res;
}
// Function to find element
vector Factor(vector& A)
{
// Sum of all element
int s = sum(A);
vector 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 A = { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
vector 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 Factor(int[] A)
{
// Sum of all element
int s = sum(A);
ArrayList z = new ArrayList();
// 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 l1 = new ArrayList();
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 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 A)
{
int res = 0;
foreach(int it in A) res += it;
return res;
}
// Function to find element
static List Factor(List A)
{
// Sum of all element
int s = sum(A);
List z = new List();
// 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() { -1 };
return z;
}
// Drive Code
public static void Main()
{
List A
= new List() { 2, 4, 6, 8, 10, 12, 14 };
// Calling function
List 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
[2, 4, 8, 14]
时间复杂度: O(N)
辅助空间: O(1)