📌  相关文章
📜  计算除以所有其他元素之和的数组元素

📅  最后修改于: 2022-05-13 01:57:47.219000             🧑  作者: Mango

计算除以所有其他元素之和的数组元素

给定一个数组arr[] ,任务是计算数组中除以所有其他元素之和的元素的数量。

例子:

朴素方法:从 0 到 N 运行两个循环,计算除当前元素之外的所有元素的总和,如果该元素除以该总和,则增加计数。
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count
// of the required numbers
int countNum(int N, int arr[])
{
    // To store the count of required numbers
    int count = 0;
    for (int i = 0; i < N; i++) {
 
        // Initialize sum to 0
        int sum = 0;
        for (int j = 0; j < N; j++) {
 
            // If current element and the
            // chosen element are same
            if (i == j)
                continue;
 
            // Add all other numbers of array
            else
                sum += arr[j];
        }
 
        // If sum is divisible by the chosen element
        if (sum % arr[i] == 0)
            count++;
    }
 
    // Return the count
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 10, 4, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countNum(n, arr);
 
    return 0;
}


Java
// Java implementation of the approach
class GFG
{
     
// Function to return the count
// of the required numbers
static int countNum(int N, int arr[])
{
    // To store the count of required numbers
    int count = 0;
    for (int i = 0; i < N; i++)
    {
 
        // Initialize sum to 0
        int sum = 0;
        for (int j = 0; j < N; j++)
        {
 
            // If current element and the
            // chosen element are same
            if (i == j)
                continue;
 
            // Add all other numbers of array
            else
                sum += arr[j];
        }
 
        // If sum is divisible by the chosen element
        if (sum % arr[i] == 0)
            count++;
    }
 
    // Return the count
    return count;
}
 
// Driver code
public static void main(String[] args)
{
    int arr[] = { 3, 10, 4, 6, 7 };
    int n = arr.length;
    System.out.println(countNum(n, arr));
}
}
 
// This code is contributed by Code_Mech


Python3
# Python3 implementation of the approach
 
# Function to return the count
# of the required numbers
def countNum(N, arr):
 
    # To store the count of
    # required numbers
    count = 0
 
    for i in range(N):
 
        # Initialize sum to 0
        Sum = 0
        for j in range(N):
 
            # If current element and the
            # chosen element are same
            if (i == j):
                continue
 
            # Add all other numbers of array
            else:
                Sum += arr[j]
 
        # If Sum is divisible by the
        # chosen element
        if (Sum % arr[i] == 0):
            count += 1
     
    # Return the count
    return count
 
# Driver code
arr = [3, 10, 4, 6, 7]
n = len(arr)
print(countNum(n, arr))
 
# This code is contributed
# by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
// Function to return the count
// of the required numbers
static int countNum(int N, int []arr)
{
    // To store the count of required numbers
    int count = 0;
    for (int i = 0; i < N; i++)
    {
 
        // Initialize sum to 0
        int sum = 0;
        for (int j = 0; j < N; j++)
        {
 
            // If current element and the
            // chosen element are same
            if (i == j)
                continue;
 
            // Add all other numbers of array
            else
                sum += arr[j];
        }
 
        // If sum is divisible by the chosen element
        if (sum % arr[i] == 0)
            count++;
    }
 
    // Return the count
    return count;
}
 
// Driver code
public static void Main()
{
    int []arr = { 3, 10, 4, 6, 7 };
    int n = arr.Length;
    Console.WriteLine(countNum(n, arr));
}
}
 
// This code is contributed by inder_verma..


PHP


Javascript


C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count
// of the required numbers
int countNum(int N, int arr[])
{
    // Initialize sum and count to 0
    int sum = 0, count = 0;
 
    // Calculate sum of all
    // the array elements
    for (int i = 0; i < N; i++)
        sum += arr[i];
 
    for (int i = 0; i < N; i++)
 
        // If current element satisfies the condition
        if ((sum - arr[i]) % arr[i] == 0)
            count++;
 
    // Return the count of required elements
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 10, 4, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countNum(n, arr);
 
    return 0;
}


Java
// Java implementation of the approach
 
class GFG
{
 
    // Function to return the count
    // of the required numbers
    static int countNum(int N, int arr[])
    {
        // Initialize sum and count to 0
        int sum = 0, count = 0;
 
        // Calculate sum of all
        // the array elements
        for (int i = 0; i < N; i++)
        {
            sum += arr[i];
        }
         
        // If current element satisfies the condition
        for (int i = 0; i < N; i++)
        {
            if ((sum - arr[i]) % arr[i] == 0)
            {
                count++;
            }
        }
 
        // Return the count of required elements
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {3, 10, 4, 6, 7};
        int n = arr.length;
        System.out.println(countNum(n, arr));
    }
}
 
// This code has been contributed by 29AjayKumar


Python3
# Python3 implementation of the approach
 
# Function to return the count
# of the required numbers
def countNum(N, arr):
     
    # Initialize Sum and count to 0
    Sum, count = 0, 0
 
    # Calculate Sum of all the
    # array elements
    for i in range(N):
        Sum += arr[i]
 
    for i in range(N):
 
        # If current element satisfies
        # the condition
        if ((Sum - arr[i]) % arr[i] == 0):
            count += 1
 
    # Return the count of required
    # elements
    return count
 
# Driver code
arr = [ 3, 10, 4, 6, 7 ]
n = len(arr)
print(countNum(n, arr))
 
# This code is contributed
# by Mohit Kumar


C#
// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the count
    // of the required numbers
    static int countNum(int N, int []arr)
    {
        // Initialize sum and count to 0
        int sum = 0, count = 0;
 
        // Calculate sum of all
        // the array elements
        for (int i = 0; i < N; i++)
        {
            sum += arr[i];
        }
         
        // If current element satisfies the condition
        for (int i = 0; i < N; i++)
        {
            if ((sum - arr[i]) % arr[i] == 0)
            {
                count++;
            }
        }
 
        // Return the count of required elements
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int []arr = {3, 10, 4, 6, 7};
        int n = arr.Length;
        Console.WriteLine(countNum(n, arr));
    }
}
 
/* This code contributed by PrinciRaj1992 */


PHP


Javascript


输出:
3

时间复杂度: O(N 2 )

高效方法:从 0 到 N 运行一个循环,计算所有元素的总和。现在运行另一个从 0 到 N 的循环,如果(sum – arr[i]) % arr[i] = 0然后增加计数。
下面是上述方法的实现:

C++

// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the count
// of the required numbers
int countNum(int N, int arr[])
{
    // Initialize sum and count to 0
    int sum = 0, count = 0;
 
    // Calculate sum of all
    // the array elements
    for (int i = 0; i < N; i++)
        sum += arr[i];
 
    for (int i = 0; i < N; i++)
 
        // If current element satisfies the condition
        if ((sum - arr[i]) % arr[i] == 0)
            count++;
 
    // Return the count of required elements
    return count;
}
 
// Driver code
int main()
{
    int arr[] = { 3, 10, 4, 6, 7 };
    int n = sizeof(arr) / sizeof(arr[0]);
    cout << countNum(n, arr);
 
    return 0;
}

Java

// Java implementation of the approach
 
class GFG
{
 
    // Function to return the count
    // of the required numbers
    static int countNum(int N, int arr[])
    {
        // Initialize sum and count to 0
        int sum = 0, count = 0;
 
        // Calculate sum of all
        // the array elements
        for (int i = 0; i < N; i++)
        {
            sum += arr[i];
        }
         
        // If current element satisfies the condition
        for (int i = 0; i < N; i++)
        {
            if ((sum - arr[i]) % arr[i] == 0)
            {
                count++;
            }
        }
 
        // Return the count of required elements
        return count;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = {3, 10, 4, 6, 7};
        int n = arr.length;
        System.out.println(countNum(n, arr));
    }
}
 
// This code has been contributed by 29AjayKumar

Python3

# Python3 implementation of the approach
 
# Function to return the count
# of the required numbers
def countNum(N, arr):
     
    # Initialize Sum and count to 0
    Sum, count = 0, 0
 
    # Calculate Sum of all the
    # array elements
    for i in range(N):
        Sum += arr[i]
 
    for i in range(N):
 
        # If current element satisfies
        # the condition
        if ((Sum - arr[i]) % arr[i] == 0):
            count += 1
 
    # Return the count of required
    # elements
    return count
 
# Driver code
arr = [ 3, 10, 4, 6, 7 ]
n = len(arr)
print(countNum(n, arr))
 
# This code is contributed
# by Mohit Kumar

C#

// C# implementation of the approach
using System;
 
class GFG
{
 
    // Function to return the count
    // of the required numbers
    static int countNum(int N, int []arr)
    {
        // Initialize sum and count to 0
        int sum = 0, count = 0;
 
        // Calculate sum of all
        // the array elements
        for (int i = 0; i < N; i++)
        {
            sum += arr[i];
        }
         
        // If current element satisfies the condition
        for (int i = 0; i < N; i++)
        {
            if ((sum - arr[i]) % arr[i] == 0)
            {
                count++;
            }
        }
 
        // Return the count of required elements
        return count;
    }
 
    // Driver code
    public static void Main()
    {
        int []arr = {3, 10, 4, 6, 7};
        int n = arr.Length;
        Console.WriteLine(countNum(n, arr));
    }
}
 
/* This code contributed by PrinciRaj1992 */

PHP


Javascript


输出:
3

时间复杂度: O(N)