📌  相关文章
📜  通过从中删除最大 K 值来清空数组

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

通过从中删除最大 K 值来清空数组

给定一个大小为 N 的数组 arr 和一个值 K,任务是通过从中删除最大值 K 来清空该数组。如果数组被清空,则打印删除的值,否则打印 -1。
例子:

方法:

  • 给定的问题可以看作是找到数组 arr 的总和并从 K 中删除该总和。
  • 如果这个总和小于或等于 K,则可以清空数组,输出将是数组的总和。
  • 否则输出将为-1。

下面是上述方法的实现:

C++
// C++ program to empty an Array
// by removing a maximum of K value from it
 
#include 
using namespace std;
 
// Function to Empty an Array by removing
// maximum of K value from it
void emptyArray(long long int n,
                long long int k,
                long long int arr[])
{
    long long int sum = 0, i;
 
    // Find the total sum of the array
    for (i = 0; i < n; i++)
        sum += arr[i];
 
    // Find if sum is less than or equal to K
    if (sum <= k)
        cout << sum << "\n";
    else
        cout << -1 << "\n";
}
 
// Driver code
int main()
{
 
    long long int n = 2, k = 5;
    long long int arr[] = { 2, 2 };
 
    emptyArray(n, k, arr);
 
    n = 4, k = 10;
    long long int arr1[] = { 5, 3, 2, 2 };
 
    emptyArray(n, k, arr1);
 
    return 0;
}


Java
// Java program to empty an Array
// by removing a maximum of K value from it
class GFG
{
     
    // Function to Empty an Array by removing
    // maximum of K value from it
    static void emptyArray(int n, int k, int arr[])
    {
        int sum = 0, i;
     
        // Find the total sum of the array
        for (i = 0; i < n; i++)
            sum += arr[i];
     
        // Find if sum is less than or equal to K
        if (sum <= k)
            System.out.println(sum);
        else
            System.out.println(-1);
    }
     
    // Driver code
    public static void main (String[] args)
    {
        int n = 2;
        int k = 5;
        int arr[] = { 2, 2 };
     
        emptyArray(n, k, arr);
     
        n = 4; k = 10;
        int arr1[] = { 5, 3, 2, 2 };
     
        emptyArray(n, k, arr1);
    }
}
 
// This code is contributed by AnkitRai01


Python3
# Python program to empty an Array
# by removing a maximum of K value from it
 
# Function to Empty an Array by removing
# maximum of K value from it
def emptyArray(n, k, arr) :
 
    sum = 0;
 
    # Find the total sum of the array
    for i in range(n) :
        sum += arr[i];
 
    # Find if sum is less than or equal to K
    if (sum <= k) :
        print(sum);
    else :
        print(-1);
 
# Driver code
if __name__ == "__main__" :
 
    n = 2; k = 5;
    arr = [ 2, 2 ];
 
    emptyArray(n, k, arr);
 
    n = 4; k = 10;
    arr1 = [ 5, 3, 2, 2 ];
 
    emptyArray(n, k, arr1);
 
# This code is contributed by AnkitRai01


C#
// C# program to empty an Array
// by removing a maximum of K value from it
using System;
 
class GFG
{
     
    // Function to Empty an Array by removing
    // maximum of K value from it
    static void emptyArray(int n, int k, int []arr)
    {
        int sum = 0, i;
     
        // Find the total sum of the array
        for (i = 0; i < n; i++)
            sum += arr[i];
     
        // Find if sum is less than or equal to K
        if (sum <= k)
            Console.WriteLine(sum);
        else
            Console.WriteLine(-1);
    }
     
    // Driver code
    public static void Main(String[] args)
    {
        int n = 2;
        int k = 5;
        int []arr = { 2, 2 };
     
        emptyArray(n, k, arr);
     
        n = 4; k = 10;
        int []arr1 = { 5, 3, 2, 2 };
     
        emptyArray(n, k, arr1);
    }
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
4
-1