Find maximum sum of subsequence after flipping signs of at most K elements in given Array
Last Updated :
16 May, 2023
Given an array arr, the task is to find the maximum sum of subsequence after flipping signs of at most K elements.
Examples:
Input: arr = [6, -10, -1, 0, -4, 2], K = 2
Output: 22
Explanation: Maximum sum can be obtained by flipping -10 and -4 to 10 and 4 respectively
Input: arr = [1, 2, 3], K = 3
Output: 6
Approach: Follow the steps below to solve the problem:
- Sort the array
- Initialize a variable sum to 0 to store the maximum sum of subsequence
- Iterate the array and convert negative elements to positive and decrement k until k > 0
- Traverse the array and add only positive values to sum
- Return the answer sum
Below is the implementation of the above approach:
C++
// C++ implementation for the above approach
#include <bits/stdc++.h>
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
<script>
// JavaScript implementation for the above approach
// Function to calculate
// the max sum of subsequence
const maxSubseq = (arr, N, K) => {
// Variable to store the max sum
let sum = 0;
// Sort the array
arr.sort((a, b) => a - b);
// Iterate over the array
for (let 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 (let i = 0; i < N; i++)
// Add only positive elements
if (arr[i] > 0)
sum += arr[i];
// Return the max sum
return sum;
}
// Driver Code
// Given array
let arr = [6, -10, -1, 0, -4, 2];
// Variable to store number
// of flips are allowed
let K = 2;
let N = arr.length
// Function call to find
// the maximum sum of subsequence
document.write(maxSubseq(arr, N, K));
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(N*log2N) since sort() has been called. Here, N is size of input array.
Auxiliary Space: O(1)
Similar Reads
Maximum subarray sum by flipping signs of at most K array elements Given an array arr[] of N integers and an integer K, The task is to find the maximum sub-array sum by flipping signs of at most K array elements. Examples: Input: arr[] = {-6, 2, -1, -1000, 2}, k = 2 Output: 1009 We can flip the signs of -6 and -1000, to get maximum subarray sum as 1009 Input: arr[]
9 min read
Maximum sum subsequence made up of at most K distant elements including the first and last array elements Given an array arr[] of size n, and an integer k. The task is to find the maximum sum of a subsequence that satisfies the following two conditions:The subsequence must include both arr[0] and arr[n - 1].The indices of consecutive elements chosen in the subsequence must differ by at most k.Examples:I
12 min read
Find the Maximum Alternate Subsequence Sum from a given array Given an array arr[] of size n having both positive and negative integer excluding zero. Find the maximum sum of maximum size alternate subsequence of a given sequence that is, in a subsequence sign of each adjacent element is opposite for example if the first one is positive then the second one has
5 min read
Longest alternating subsequence which has maximum sum of elements Given a list of length N with positive and negative integers. The task is to choose the longest alternating subsequence of the given sequence (i.e. the sign of each next element is the opposite of the sign of the current element). Among all such subsequences, we have to choose one which has the maxi
10 min read
Maximum Subarray Sum after inverting at most two elements Given an array arr[] of integer elements, the task is to find maximum possible sub-array sum after changing the signs of at most two elements.Examples: Input: arr[] = {-5, 3, 2, 7, -8, 3, 7, -9, 10, 12, -6} Output: 61 We can get 61 from index 0 to 10 by changing the sign of elements at 4th and 7th i
11 min read
Maximum sum possible by assigning alternate positive and negative sign to elements in a subsequence Given an array arr[] consisting of N positive integers, the task is to find the maximum sum of subsequences from the given array such that elements in the subsequence are assigned positive and negative signs alternately. Subsequence = {a, b, c, d, e, ... }, Sum of the above subsequence = (a - b + c
12 min read