📌  相关文章
📜  通过用任何正整数取模替换任何 K 个元素来最大化数组总和

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

通过用任何正整数取模替换任何 K 个元素来最大化数组总和

给定一个正整数数组arr[]和一个数字K 。任务是通过用任何小于arr[i]的正整数取模来替换数组的任何K个元素来最大化数组的总和,即(arr[i] = arr[i]%X 其中 X ≤ arr[i])。

例子:

方法:对于每个元素arr[i] 在数组arr[]中,使用(arr[i]/2 +1)对其进行模块化,这将在操作后给出arr[i]的最高可能值。以下是解决问题的步骤

  • 对数组arr[]进行排序。
  • 使用变量i遍历范围[0, K)并执行以下任务:
    • 对于每个元素arr[i],(arr[i]/2 +1)对其进行模块化并更新结果。
  • 求更新数组的总和并输出。

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find the maximum possible sum
int find(int arr[], int K, int N)
{
    // Sorting the array
    sort(arr, arr + N);
    int sum = 0;
 
    // Loop to take update K
    for (int i = 0; i < K; i++) {
 
        // Smallest number in array
        arr[i] %= (arr[i] / 2) + 1;
    }
 
    // Loop to find sum
    for (int i = 0; i < N; i++) {
        sum += arr[i];
    }
    return sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 5, 7, 18, 12, 11, 3 };
    int K = 4;
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << find(arr, K, N);
    return 0;
}


Java
// Java program for the above approach
import java.util.Arrays;
class GFG {
 
  // Function to find the maximum possible sum
  static int find(int arr[], int K, int N) {
    // Sorting the array
    Arrays.sort(arr);
    int sum = 0;
 
    // Loop to take update K
    for (int i = 0; i < K; i++) {
 
      // Smallest number in array
      arr[i] %= (arr[i] / 2) + 1;
    }
 
    // Loop to find sum
    for (int i = 0; i < N; i++) {
      sum += arr[i];
    }
    return sum;
  }
 
  // Driver Code
  public static void main(String args[]) {
    int arr[] = { 5, 7, 18, 12, 11, 3 };
    int K = 4;
    int N = arr.length;
 
    System.out.println(find(arr, K, N));
  }
}
 
// This code is contributed by saurabh_jaiswal.


Python3
# python3 program for the above approach
 
# Function to find the maximum possible sum
def find(arr, K, N):
 
    # Sorting the array
    arr.sort()
    sum = 0
 
    # Loop to take update K
    for i in range(0, K):
 
        # Smallest number in array
        arr[i] %= (arr[i] // 2) + 1
 
    # Loop to find sum
    for i in range(0, N):
        sum += arr[i]
 
    return sum
 
# Driver Code
if __name__ == "__main__":
 
    arr = [5, 7, 18, 12, 11, 3]
    K = 4
    N = len(arr)
 
    print(find(arr, K, N))
 
# This code is contributed by rakeshsahni


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to find the maximum possible sum
  static int find(int[] arr, int K, int N)
  {
    // Sorting the array
    Array.Sort(arr);
    int sum = 0;
 
    // Loop to take update K
    for (int i = 0; i < K; i++) {
 
      // Smallest number in array
      arr[i] %= (arr[i] / 2) + 1;
    }
 
    // Loop to find sum
    for (int i = 0; i < N; i++) {
      sum += arr[i];
    }
    return sum;
  }
 
  // Driver Code
  public static void Main()
  {
    int[] arr = { 5, 7, 18, 12, 11, 3 };
    int K = 4;
    int N = arr.Length;
 
    Console.Write(find(arr, K, N));
  }
}
 
// This code is contributed by ukasp.


Javascript



输出
41

时间复杂度: O(N * logN) 其中 N 是数组的大小
辅助空间: O(1)