Monday, November 18, 2024
Google search engine
HomeData Modelling & AIMaximize array product by changing any array element arr to (-1)*arr –...

Maximize array product by changing any array element arr[i] to (-1)*arr[i] – 1 any number of times

Given an array arr[] consisting of N integers, the task is to modify the array elements by changing each array element arr[i] to (-1)*arr[i] – 1 any number of times such that the product of the array is maximized.

Examples:

Input: arr[] = {-3, 0, 1}
Output: 2 -1 -2
Explanation:
Performing the following operations on the array elements maximizes the product:
Applying the operation on arr[0] once, arr[0] = (- 1) * (- 3) – 1 = 2.
Applying the operation on arr[1] once, arr[1] = 0 – 1 = -1.
Applying the operation on arr[2] once, arr[2] = -1 – 1 = -2.
The modified array is {2, -1, -2} with product 4, which is maximum possible.

Input: arr[] = {-9, 2, 0, 1, -5, 3, 16}
Output: -9 -3 -1 -2 -5 -4 16

Approach: The given problem can be solved based on the following observations:

  • For the product to be maximum, the absolute value of elements should be higher as the operation decreases the absolute value of negative elements. Therefore, the given operations should only be applied to positive values.
  • If the number of elements is odd, then perform an operation on the highest absolute valued element as the product of an odd number of negative numbers is negative. Therefore, change one positive number for the product to be positive and maximum.
  • Perform the operation on a negative number decreases its absolute value. Therefore, find the element with the greatest absolute value and apply the operation on it once as that would decrease the product by the least.

Follow the steps below to solve the problem:

  • Traverse the given array arr[] and for each element arr[i], check if arr[i] ? 0 or not. If found to be true, then update arr[i] = (-1)*arr[i] – 1. Otherwise, continue.
  • After the array traversal, check if the value of N is odd or not. If found to be true, then find the element with the greatest absolute value and apply the operation to it once.
  • After completing the above steps, print the modified array arr[].

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <bits/stdc++.h>
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
         
    # Print 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




<script>
      // JavaScript program for the above approach
      // Function to find array with maximum
      // product by changing array
      // elements to (-1)arr[i] - 1
      function findArrayWithMaxProduct(arr, N)
      {
       
        // Traverse the array
        for (var 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
          var max_element = -1;
 
          // Stores index of maximum element
          var index = -1;
          for (var 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 (var i = 0; i < N; i++) document.write(arr[i] + " ");
      }
 
      // Driver Code
      var arr = [-3, 0, 1];
      var N = arr.length;
 
      // Function Call
      findArrayWithMaxProduct(arr, N);
       
      // This code is contributed by rdtank.
    </script>


Output: 

2 -1 -2

 

Time Complexity: O(N)
Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

RELATED ARTICLES

Most Popular

Recent Comments