📌  相关文章
📜  使数组的所有元素与给定操作相等

📅  最后修改于: 2021-06-26 02:11:13             🧑  作者: Mango

给定一个由n个整数和一个整数k组成的数组arr [] 。任务是使arr []的所有元素与给定的操作相等。在一次操作中,可以将x≤k的任何非负数(可以是浮点值)添加到数组的任何元素,并且k将更新为k = k – x 。打印可能是其他打印
例子:

方法:由于任务是使数组的所有元素相等,并且加法的总和必须恰好是k 。只有一个单一的值可以使所有这些元素相等,即(sum(arr)+ k)/ n 。如果数组中的某个元素已经大于该值,则答案不存在,否则打印“是”
下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function that returns true if all the elements
// of the array can be made equal
// with the given operation
bool isPossible(int n, int k, int arr[])
{
 
    // To store the sum of the array elements
    // and the maximum element from the array
    int sum = arr[0], maxVal = arr[0];
 
    for (int i = 1; i < n; i++) {
        sum += arr[i];
        maxVal = max(maxVal, arr[i]);
    }
 
    if ((float)maxVal > (float)(sum + k) / n)
        return false;
 
    return true;
}
 
// Driver code
int main()
{
    int k = 8;
    int arr[] = { 1, 2, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    if (isPossible(n, k, arr))
        cout << "Yes";
    else
        cout << "No";
 
    return 0;
}


Java
//Java implementation of the approach
import java.io.*;
 
class GFG
{
     
// Function that returns true if all
// the elements of the array can be
// made equal with the given operation
static boolean isPossible(int n, int k, int arr[])
{
 
    // To store the sum of the array elements
    // and the maximum element from the array
    int sum = arr[0];
    int maxVal = arr[0];
 
    for (int i = 1; i < n; i++)
    {
        sum += arr[i];
        maxVal = Math.max(maxVal, arr[i]);
    }
 
    if ((float)maxVal > (float)(sum + k) / n)
        return false;
 
    return true;
}
 
    // Driver code
    public static void main (String[] args)
    {
     
        int k = 8;
        int arr[] = { 1, 2, 3, 4 };
        int n = arr.length;
 
        if (isPossible(n, k, arr))
            System.out.println ("Yes");
        else
            System.out.println( "No");
    }
}
 
// This code is contributed by @Tushil.


Python3
# Python 3 implementation of the approach
 
# Function that returns true if all
# the elements of the array can be
# made equal with the given operation
def isPossible(n, k, arr):
     
    # To store the sum of the array elements
    # and the maximum element from the array
    sum = arr[0]
    maxVal = arr[0];
 
    for i in range(1, n):
        sum += arr[i]
        maxVal = max(maxVal, arr[i])
 
 
    if (int(maxVal)> int((sum + k) / n)):
        return False
 
    return True
 
# Driver code
if __name__ == '__main__':
    k = 8
    arr = [1, 2, 3, 4]
    n = len(arr)
 
    if (isPossible(n, k, arr)):
        print("Yes")
    else:
        print("No")
 
 
# This code is contributed by
# Surendra_Gangwar


C#
// C# implementation of the approach
using System;
 
class GFG
{
     
    // Function that returns true if all
    // the elements of the array can be
    // made equal with the given operation
    static bool isPossible(int n,
                        int k, int []arr)
    {
     
        // To store the sum of the array elements
        // and the maximum element from the array
        int sum = arr[0];
        int maxVal = arr[0];
         
        for (int i = 1; i < n; i++)
        {
            sum += arr[i];
            maxVal = Math.Max(maxVal, arr[i]);
        }
     
        if ((float)maxVal > (float)(sum + k) / n)
            return false;
     
        return true;
    }
     
    // Driver code
    public static void Main()
    {
         
        int k = 8;
        int []arr = { 1, 2, 3, 4 };
        int n = arr.Length;
     
        if (isPossible(n, k, arr))
            Console.WriteLine("Yes");
        else
            Console.WriteLine( "No");
    }
}
 
// This code is contributed by Ryuga


PHP
 (float)($sum + $k) / $n)
        return false;
 
    return true;
}
 
    // Driver code
    $k = 8;
    $arr = array( 1, 2, 3, 4 );
    $n = sizeof($arr) / sizeof($arr[0]);
 
    if (isPossible($n, $k, $arr))
        echo "Yes";
    else
        echo "No";
 
# This code is contributed by akt_miit.
?>


Javascript


输出:
Yes

时间复杂度: O(n)

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。