📌  相关文章
📜  在翻转给定数组中最多 K 个元素的符号后找到子序列的最大和

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

在翻转给定数组中最多 K 个元素的符号后找到子序列的最大和

给定一个数组arr ,任务是在翻转最多K个元素的符号后找到子序列的最大和。

例子:

方法:按照以下步骤解决问题:

  • 对数组进行排序
  • 将变量sum初始化为0,存储子序列的最大和
  • 迭代数组并转换为负数 元素正数和递减k直到k > 0
  • 遍历数组,只添加正数 要求和的值
  • 返回答案总和

下面是上述方法的实现:

C++
// C++ implementation for the above approach
 
#include 
using namespace std;
 
// Function to calculate
// the max sum of subsequence
int maxSubseq(int arr[], int N, int K)
{
    // Variable to store the max sum
    int sum = 0;
 
    // Sort the array
    sort(arr, arr + N);
 
    // Iterate over the array
    for (int i = 0; i < N; i++) {
        if (K == 0)
            break;
 
        if (arr[i] < 0) {
 
            // Flip sign
            arr[i] = -arr[i];
 
            // Decrement k
            K--;
        }
    }
 
    // Traverse over the array
    for (int i = 0; i < N; i++)
 
        // Add only positive elements
        if (arr[i] > 0)
            sum += arr[i];
 
    // Return the max sum
    return sum;
}
 
// Driver Code
int main()
{
 
    // Given array
    int arr[] = { 6, -10, -1, 0, -4, 2 };
 
    // Variable to store number
    // of flips are allowed
    int K = 2;
    int N = sizeof(arr)
            / sizeof(arr[0]);
 
    // Function call to find
    // the maximum sum of subsequence
    cout << maxSubseq(arr, N, K);
    return 0;
}


Java
// Java implementation for the above approach
import java.util.*;
 
class GFG
{
 
  // Function to calculate
  // the max sum of subsequence
  static int maxSubseq(int arr[], int N, int K)
  {
 
    // Variable to store the max sum
    int sum = 0;
 
    // Sort the array
    Arrays.sort(arr);
 
    // Iterate over the array
    for (int i = 0; i < N; i++) {
      if (K == 0)
        break;
 
      if (arr[i] < 0) {
 
        // Flip sign
        arr[i] = -arr[i];
 
        // Decrement k
        K--;
      }
    }
 
    // Traverse over the array
    for (int i = 0; i < N; i++)
 
      // Add only positive elements
      if (arr[i] > 0)
        sum += arr[i];
 
    // Return the max sum
    return sum;
  }
 
  // Driver Code
  public static void main(String []args)
  {
 
    // Given array
    int []arr = { 6, -10, -1, 0, -4, 2 };
 
    // Variable to store number
    // of flips are allowed
    int K = 2;
    int N = arr.length;
 
    // Function call to find
    // the maximum sum of subsequence
    System.out.println(maxSubseq(arr, N, K));
  }
}
 
// This code is contributed by ipg2016107.


Python3
# Python 3 implementation for the above approach
 
# Function to calculate
# the max sum of subsequence
def maxSubseq(arr, N, K):
 
    # Variable to store the max sum
    sum = 0
 
    # Sort the array
    arr.sort()
 
    # Iterate over the array
    for i in range(N):
        if (K == 0):
            break
 
        if (arr[i] < 0):
 
            # Flip sign
            arr[i] = -arr[i]
 
            # Decrement k
            K -= 1
 
    # Traverse over the array
    for i in range(N):
 
        # Add only positive elements
        if (arr[i] > 0):
            sum += arr[i]
 
    # Return the max sum
    return sum
 
# Driver Code
if __name__ == "__main__":
 
    # Given array
    arr = [6, -10, -1, 0, -4, 2]
 
    # Variable to store number
    # of flips are allowed
    K = 2
    N = len(arr)
 
    # Function call to find
    # the maximum sum of subsequence
    print(maxSubseq(arr, N, K))
 
    # This code is contributed by ukasp.


C#
// C++ implementation for the above approach
using System;
 
class GFG
{
 
// Function to calculate
// the max sum of subsequence
static int maxSubseq(int []arr, int N, int K)
{
   
    // Variable to store the max sum
    int sum = 0;
 
    // Sort the array
    Array.Sort(arr);
 
    // Iterate over the array
    for (int i = 0; i < N; i++) {
        if (K == 0)
            break;
 
        if (arr[i] < 0) {
 
            // Flip sign
            arr[i] = -arr[i];
 
            // Decrement k
            K--;
        }
    }
 
    // Traverse over the array
    for (int i = 0; i < N; i++)
 
        // Add only positive elements
        if (arr[i] > 0)
            sum += arr[i];
 
    // Return the max sum
    return sum;
}
 
// Driver Code
public static void Main(string[] args)
    {
 
    // Given array
    int []arr = { 6, -10, -1, 0, -4, 2 };
 
    // Variable to store number
    // of flips are allowed
    int K = 2;
    int N = arr.Length;
 
    // Function call to find
    // the maximum sum of subsequence
    Console.Write(maxSubseq(arr, N, K));
}
}
 
// This code is contributed by shivanisinghss2110


Javascript



输出:
22

时间复杂度: O(N)
辅助空间: O(1)