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

📅  最后修改于: 2021-05-17 01:34:18             🧑  作者: 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.


输出:
2 -1 -2

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