Maximize sum after K negations
PROBLEM :
Given an array of size n and a number k. We must modify array K number of times. Here modify array means in each operation we can replace any array element arr[i] by -arr[i]. We need to perform this operation in such a way that after K operations, sum of array must be maximum?
Examples:
Input : arr[] = {-2, 0, 5, -1, 2}
K = 4
Output: 10
// Replace (-2) by -(-2), array becomes {2, 0, 5, -1, 2}
// Replace (-1) by -(-1), array becomes {2, 0, 5, 1, 2}
// Replace (0) by -(0), array becomes {2, 0, 5, 1, 2}
// Replace (0) by -(0), array becomes {2, 0, 5, 1, 2}
Input : arr[] = {9, 8, 8, 5}
K = 3
Output: 20
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consist of three lines . The first line of each test case contains an integer N.The second line of each test case contains N space separated integers denoting elements of the array. Third line contains value of k.
Output:
For each test case in a new line print maximum possible sum.
Constraints:
1 = T = 500
1 = N = 1000
Example:
Input:
2
5
1 2 -3 4 5
1
10
5 -2 5 -4 5 -12 5 5 5 20
5
Output:
15
68
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define max 1000
int Maximize_sum(int [],int ,int ) ;
int main()
{
int t,no,i,k ;
int arr[max] ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cin>>k ;
cout<<Maximize_sum(arr,no,k)<<endl ;
}
return 0;
}
int Maximize_sum(int arr[],int no,int k)
{
int i,min ;
while(k--)
{
min=0 ;
for(i=0;i<no;i++)
if(arr[i]<arr[min])
min=i ;
if(arr[min]==0)
break ;
arr[min]=-arr[min] ;
}
int sum=0 ;
for(i=0;i<no;i++)
sum=sum+arr[i] ;
return sum ;
}
---------------------------------------------------------------------------------
Given an array of size n and a number k. We must modify array K number of times. Here modify array means in each operation we can replace any array element arr[i] by -arr[i]. We need to perform this operation in such a way that after K operations, sum of array must be maximum?
Examples:
Input : arr[] = {-2, 0, 5, -1, 2}
K = 4
Output: 10
// Replace (-2) by -(-2), array becomes {2, 0, 5, -1, 2}
// Replace (-1) by -(-1), array becomes {2, 0, 5, 1, 2}
// Replace (0) by -(0), array becomes {2, 0, 5, 1, 2}
// Replace (0) by -(0), array becomes {2, 0, 5, 1, 2}
Input : arr[] = {9, 8, 8, 5}
K = 3
Output: 20
Input:
The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consist of three lines . The first line of each test case contains an integer N.The second line of each test case contains N space separated integers denoting elements of the array. Third line contains value of k.
Output:
For each test case in a new line print maximum possible sum.
Constraints:
1 = T = 500
1 = N = 1000
Example:
Input:
2
5
1 2 -3 4 5
1
10
5 -2 5 -4 5 -12 5 5 5 20
5
Output:
15
68
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define max 1000
int Maximize_sum(int [],int ,int ) ;
int main()
{
int t,no,i,k ;
int arr[max] ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cin>>k ;
cout<<Maximize_sum(arr,no,k)<<endl ;
}
return 0;
}
int Maximize_sum(int arr[],int no,int k)
{
int i,min ;
while(k--)
{
min=0 ;
for(i=0;i<no;i++)
if(arr[i]<arr[min])
min=i ;
if(arr[min]==0)
break ;
arr[min]=-arr[min] ;
}
int sum=0 ;
for(i=0;i<no;i++)
sum=sum+arr[i] ;
return sum ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment