📌  相关文章
📜  通过将任何数组元素 arr[i] 更改为 (-1)*arr[i] – 1 任意次数来最大化数组乘积

📅  最后修改于: 2021-10-25 11:19:32             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是通过将每个数组元素arr[i]更改为(-1)*arr[i] – 1任意次数来修改数组元素,使得数组最大化。

例子:

方法:根据以下观察可以解决给定的问题:

  • 为了使乘积最大,元素的绝对值应该随着运算减少负元素的绝对值而变高。因此,给定的操作应该只应用于正值。
  • 如果元素个数为奇数,则对绝对值最高的元素进行运算,因为奇数个负数的乘积为负。因此,将乘积的一个正数更改为正和最大值。
  • 对负数执行操作会减小其绝对值。因此,找到绝对值最大的元素并对其进行一次操作,因为这样会使乘积减少最少。

请按照以下步骤解决问题:

  • 遍历给定的数组arr[]并对每个元素arr[i] ,检查arr[i] ≥ 0是否。如果发现为真,则更新arr[i] = (-1)*arr[i] – 1 。否则,继续。
  • 遍历数组后,检查N的值是否为奇数。如果发现为真,则找到绝对值最大的元素并对其进行一次操作。
  • 完成上述步骤后,打印修改后的数组arr[]

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to find array with maximum
// product by changing array
// elements to (-1)arr[i] - 1
void findArrayWithMaxProduct(int arr[],
                             int N)
{
    // Traverse the array
    for (int i = 0; i < N; i++)
 
        // Applying the operation on
        // all the positive elements
        if (arr[i] >= 0) {
            arr[i] = -arr[i] - 1;
        }
 
    if (N % 2 == 1) {
 
        // Stores maximum element in array
        int max_element = -1;
 
        // Stores index of maximum element
        int index = -1;
 
        for (int i = 0; i < N; i++)
 
            // Check if current element
            // is greater than the
            // maximum element
            if (abs(arr[i]) > max_element) {
 
                max_element = abs(arr[i]);
 
                // Find index of the
                // maximum element
                index = i;
            }
 
        // Perform the operation on the
        // maximum element
        arr[index] = -arr[index] - 1;
    }
 
    // Print the elements of the array
    for (int i = 0; i < N; i++)
        printf("%d ", arr[i]);
}
 
// Driver Code
int main()
{
    int arr[] = { -3, 0, 1 };
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    findArrayWithMaxProduct(arr, N);
 
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG
{
 
  // Function to find array with maximum
  // product by changing array
  // elements to (-1)arr[i] - 1
  static void findArrayWithMaxProduct(int arr[], int N)
  {
 
    // Traverse the array
    for (int i = 0; i < N; i++)
 
      // Applying the operation on
      // all the positive elements
      if (arr[i] >= 0)
      {
        arr[i] = -arr[i] - 1;
      }
 
    if (N % 2 == 1)
    {
 
      // Stores maximum element in array
      int max_element = -1;
 
      // Stores index of maximum element
      int index = -1;
 
      for (int i = 0; i < N; i++)
 
        // Check if current element
        // is greater than the
        // maximum element
        if (Math.abs(arr[i]) > max_element) {
 
          max_element = Math.abs(arr[i]);
 
          // Find index of the
          // maximum element
          index = i;
        }
 
      // Perform the operation on the
      // maximum element
      arr[index] = -arr[index] - 1;
    }
 
    // Print the elements of the array
    for (int i = 0; i < N; i++)
      System.out.print(arr[i] + " ");
  }
 
  // Driver Code
  public static void main(String[] args)
  {
    int arr[] = { -3, 0, 1 };
    int N = arr.length;
 
    // Function Call
    findArrayWithMaxProduct(arr, N);
  }
}
 
// This code is contributed by jithin.


Python3
# Python3 program for the above approach
 
# Function to find array with maximum
# product by changing array
# elements to (-1)arr[i] - 1
def findArrayWithMaxProduct(arr, N):
   
    # Traverse the array
    for i in range(N):
 
        # Applying the operation on
        # all the positive elements
        if (arr[i] >= 0):
            arr[i] = -arr[i] - 1
    if (N % 2 == 1):
 
        # Stores maximum element in array
        max_element = -1
 
        # Stores index of maximum element
        index = -1
        for i in range(N):
 
            # Check if current element
            # is greater than the
            # maximum element
            if (abs(arr[i]) > max_element):
                max_element = abs(arr[i])
 
                # Find index of the
                # maximum element
                index = i
 
        # Perform the operation on the
        # maximum element
        arr[index] = -arr[index] - 1
         
    # Prthe elements of the array
    for i in arr:
        print(i, end = " ")
 
# Driver Code
if __name__ == '__main__':
    arr = [-3, 0, 1]
    N = len(arr)
 
    # Function Call
    findArrayWithMaxProduct(arr, N)
 
    # This code is contributed by mohit kumar 29.


C#
// C# program for the above approach
using System;
class GFG
{
 
  // Function to find array with maximum
  // product by changing array
  // elements to (-1)arr[i] - 1
  static void findArrayWithMaxProduct(int[] arr, int N)
  {
 
    // Traverse the array
    for (int i = 0; i < N; i++)
 
      // Applying the operation on
      // all the positive elements
      if (arr[i] >= 0)
      {
        arr[i] = -arr[i] - 1;
      }
 
    if (N % 2 == 1)
    {
 
      // Stores maximum element in array
      int max_element = -1;
 
      // Stores index of maximum element
      int index = -1;
      for (int i = 0; i < N; i++)
 
        // Check if current element
        // is greater than the
        // maximum element
        if (Math.Abs(arr[i]) > max_element) {
 
          max_element = Math.Abs(arr[i]);
 
          // Find index of the
          // maximum element
          index = i;
        }
 
      // Perform the operation on the
      // maximum element
      arr[index] = -arr[index] - 1;
    }
 
    // Print the elements of the array
    for (int i = 0; i < N; i++)
      Console.Write(arr[i] + " ");
  }
 
// Driver Code
static public void Main()
{
    int[] arr = { -3, 0, 1 };
    int N = arr.Length;
 
    // Function Call
    findArrayWithMaxProduct(arr, N);
}
}
 
// This code is contributed by sanjoy_62.


Javascript


输出:
2 -1 -2

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程