📌  相关文章
📜  找到 K 使得将数组中大于 K 的所有元素更改为 K 将使数组总和为 N

📅  最后修改于: 2021-09-07 02:11:20             🧑  作者: Mango

给定一个数组arr[]和一个整数N ,任务是从给定的数组中找到一个数字K ,如果arr 中大于K的所有元素都更改为K则结果数组中所有元素的总和将是N 。如果不可能,则打印-1

例子:

方法:思路是用排序来解决上述问题。

  • 首先,按升序对数组进行排序。
  • 然后遍历数组和每个元素arr[i]
  • 假设其后的所有元素都已改为arr[i]并计算总和。
  • 如果它等于N则返回arr[i]
  • 如果到达数组的末尾,则打印-1

下面是上述方法的实现:

CPP
// C++ implementation of the approach
  
#include 
using namespace std;
  
// Function to return K such that changing
// all elements greater than K to K will
// make array sum N otherwise return -1
int findK(int arr[], int size, int N)
{
  
    // Sorting the array in increasing order
    sort(arr, arr + size);
    int temp_sum = 0;
  
    // Loop through all the elements of the array
    for (int i = 0; i < size; i++) {
        temp_sum += arr[i];
  
        // Checking if sum of array equals N
        if (N - temp_sum == arr[i] * (size - i - 1)) {
            return arr[i];
        }
    }
    return -1;
}
  
// Driver code
int main()
{
    int arr[] = { 3, 1, 10, 4, 8 };
    int size = sizeof(arr) / sizeof(int);
    int N = 16;
  
    cout << findK(arr, size, N);
  
    return 0;
}


Java
// Java implementation of the approach 
import java.util.*;
  
class GFG 
{
      
    // Function to return K such that changing 
    // all elements greater than K to K will 
    // make array sum N otherwise return -1 
    static int findK(int arr[], int size, int N) 
    { 
      
        // Sorting the array in increasing order 
        Arrays.sort(arr); 
        int temp_sum = 0; 
      
        // Loop through all the elements of the array 
        for (int i = 0; i < size; i++)
        { 
            temp_sum += arr[i]; 
      
            // Checking if sum of array equals N 
            if (N - temp_sum == arr[i] * (size - i - 1))
            { 
                return arr[i]; 
            } 
        } 
        return -1; 
    } 
      
    // Driver code 
    public static void main (String[] args)
    { 
        int []arr = { 3, 1, 10, 4, 8 }; 
        int size = arr.length; 
        int N = 16; 
      
        System.out.print(findK(arr, size, N)); 
    } 
}
  
// This code is contributed by AnkitRai01


Python
# Python3 implementation of the approach
  
# Function to return K such that changing
# all elements greater than K to K will
# make array sum N otherwise return -1
def findK(arr, size, N):
  
    # Sorting the array in increasing order
    arr = sorted(arr)
    temp_sum = 0
  
    # Loop through all the elements of the array
    for i in range(size):
        temp_sum += arr[i]
  
        # Checking if sum of array equals N
        if (N - temp_sum == arr[i] * (size - i - 1)):
            return arr[i]
    return -1
  
# Driver code
arr = [3, 1, 10, 4, 8]
size = len(arr)
N = 16
  
print(findK(arr, size, N))
  
# This code is contributed by mohit kumar 29


C#
// C# implementation of the above approach
using System;
  
class GFG 
{
      
    // Function to return K such that changing 
    // all elements greater than K to K will 
    // make array sum N otherwise return -1 
    static int findK(int []arr, int size, int N) 
    { 
      
        // Sorting the array in increasing order 
        Array.Sort(arr); 
        int temp_sum = 0; 
      
        // Loop through all the elements of the array 
        for (int i = 0; i < size; i++)
        { 
            temp_sum += arr[i]; 
      
            // Checking if sum of array equals N 
            if (N - temp_sum == arr[i] * (size - i - 1))
            { 
                return arr[i]; 
            } 
        } 
        return -1; 
    } 
      
    // Driver code 
    public static void Main()
    { 
        int []arr = { 3, 1, 10, 4, 8 }; 
        int size = arr.Length; 
        int N = 16; 
      
        Console.Write(findK(arr, size, N)); 
    } 
}
  
// This code is contributed by AnkitRai01


输出:
4

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live