📌  相关文章
📜  通过将数组的前缀乘以-1来最大化数组的总和

📅  最后修改于: 2021-06-26 00:59:20             🧑  作者: Mango

给定元素数组“ arr”,任务是在执行以下操作后使该数组的元素总和最大化:
您可以使用’arr’的任何前缀,并将前缀的每个元素乘以’-1’。
在第一行中,打印最大化的总和,然后在下一行中,打印选择了前缀序列的索引。

例子:

Input: arr = {1, -2, -3, 4}
Output: 10
2 1 3 2
Flip the prefix till 2nd element then the sequence is -1  2 -3  4
Flip the prefix till 1st element then the sequence is  1  2 -3  4
Flip the prefix till 3rd element then the sequence is -1 -2  3  4
Flip the prefix till 2nd element then the sequence is  1  2  3  4
And, the final maximised sum is 10

Input: arr = {1, 2, 3, 4}
Output: 10
As, all the elements are already positive.

方法:最大总和将始终为\small \sum \left | A_i \right |因为数组的所有数字都可以通过给定操作从负数更改为正数。

  • 如果从索引“ i”开始的元素为负,则从左向右遍历该数组,然后选择“ i”作为前缀数组的结束索引,并将每个元素乘以“ -1”。
  • 由于上一步中的操作,索引“ i”之前的数组中的所有元素都必须为负数。因此,采用以索引“ i-1”结尾的前缀数组,然后再次应用相同的操作将所有元素更改为正数。
  • 重复上述步骤,直到遍历完整的数组,并在最后打印所有元素的总和以及所选前缀数组的所有结尾索引。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include
using namespace std;
  
void maxSum(int *a, int n)
{
      
    vector l; 
      
    // To store sum
    int s = 0;
  
    // To store ending indices 
    // of the chosen prefix array vect
    for (int i = 0; i < n; i++)
    {
  
        // Adding the absolute 
        // value of a[i]
        s += abs(a[i]);
        if (a[i] >= 0)
            continue;
  
        // If i == 0 then there is no index 
        // to be flipped in (i-1) position
        if (i == 0)
            l.push_back(i + 1);
        else
        {
            l.push_back(i + 1);
            l.push_back(i);
        }
              
    }
  
          
    // print the maximised sum
    cout << s << endl;
  
    // print the ending indices 
    // of the chosen prefix arrays
    for (int i = 0; i < l.size(); i++)
    cout << l[i] << " ";
  
}
  
// Driver Code    
int main()
{
    int n = 4;
    int a[] = {1, -2, -3, 4};
    maxSum(a, n);
} 
  
// This code is contributed by
// Surendra_Gangwar


Java
// Java implementation of the approach
import java.util.*;
  
class GFG 
{
      
static void maxSum(int []a, int n)
{
    Vector l = new Vector();
      
    // To store sum
    int s = 0;
  
    // To store ending indices 
    // of the chosen prefix array vect
    for (int i = 0; i < n; i++)
    {
  
        // Adding the absolute 
        // value of a[i]
        s += Math.abs(a[i]);
        if (a[i] >= 0)
            continue;
  
        // If i == 0 then there is no index 
        // to be flipped in (i-1) position
        if (i == 0)
            l.add(i + 1);
        else
        {
            l.add(i + 1);
            l.add(i);
        }
    }
  
    // print the maximised sum
    System.out.println(s);
  
    // print the ending indices 
    // of the chosen prefix arrays
    for (int i = 0; i < l.size(); i++)
    System.out.print(l.get(i) + " ");
}
  
// Driver Code 
public static void main(String[] args) 
{
    int n = 4;
    int a[] = {1, -2, -3, 4};
    maxSum(a, n);
}
}
  
// This code is contributed by 29AjayKumar


Python3
# Python implementation of the approach
def maxSum(arr, n):
    # To store sum
    s = 0
  
    # To store ending indices 
    # of the chosen prefix arrays
    l = []
    for i in range(len(a)):
  
        # Adding the absolute 
        # value of a[i]
        s += abs(a[i])
        if (a[i] >= 0):
            continue
  
        # If i == 0 then there is 
        # no index to be flipped 
        # in (i-1) position
        if (i == 0):
            l.append(i + 1)
        else:
            l.append(i + 1)
            l.append(i)
  
    # print the 
    # maximised sum
    print(s)
  
    # print the ending indices 
    # of the chosen prefix arrays
    print(*l)
      
n = 4
a = [1, -2, -3, 4]
maxSum(a, n)


C#
// C# implementation of the approach
using System;
using System.Collections.Generic; 
  
class GFG 
{
      
static void maxSum(int []a, int n)
{
    List l = new List();
      
    // To store sum
    int s = 0;
  
    // To store ending indices 
    // of the chosen prefix array vect
    for (int i = 0; i < n; i++)
    {
  
        // Adding the absolute 
        // value of a[i]
        s += Math.Abs(a[i]);
        if (a[i] >= 0)
            continue;
  
        // If i == 0 then there is no index 
        // to be flipped in (i-1) position
        if (i == 0)
            l.Add(i + 1);
        else
        {
            l.Add(i + 1);
            l.Add(i);
        }
    }
  
    // print the maximised sum
    Console.WriteLine(s);
  
    // print the ending indices 
    // of the chosen prefix arrays
    for (int i = 0; i < l.Count; i++)
    Console.Write(l[i] + " ");
}
  
// Driver Code 
public static void Main(String[] args) 
{
    int n = 4;
    int []a = {1, -2, -3, 4};
    maxSum(a, n);
}
}
  
// This code is contributed by PrinciRaj1992


PHP
= 0)
            continue;
  
        // If i == 0 then there is 
        // no index to be flipped 
        // in (i-1) position
        if ($i == 0)
            array_push($l, $i + 1);
        else
        {
            array_push($l, $i + 1);
            array_push($l, $i);
        }
    }
  
    // print the 
    // maximised sum
    echo $s . "\n";
  
    // print the ending indices 
    // of the chosen prefix arrays
    for($i = 0; $i < count($l); $i++)
    echo $l[$i] . " ";
}
  
// Driver Code
$n = 4;
$a = array(1, -2, -3, 4);
maxSum($a, $n);
  
// This code is contributed by mits
?>


输出:
10
2 1 3 2

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。