Minimum steps to make the product of the array equal to 1
Given an array arr[] containing N integers. In one step, any element of the array can either be increased or decreased by one. The task is to find minimum steps required such that the product of the array elements becomes 1.
Examples:
Input: arr[] = { -2, 4, 0 }
Output: 5
We can change -2 to -1, 0 to -1 and 4 to 1.
So, a total of 5 steps are required to update the elements
such that the product of the final array is 1.
Input: arr[] = { -1, 1, -1 }
Output: 0
Approach: Follow the steps below to solve the problem:
- The product of the array elements can only be equal to 1 when there are only 1s and -1s in the array and the count of -1s is even.
- Now, all the positive numbers can be reduced to 1 because they are closer to 1 than they are closer to -1.
- Similarly, all the negative numbers can be updated to -1.
- If there are 0s present in the array then they can be reduced to either 1 or -1 according to the situation (the count of -1s must be even).
- If the count of -ve numbers are even then they are always going to yield -1.
- But if there are odd number of -ve numbers then they are going to yield an odd number of -1s. To fix that, there are two possibilities:
- First try to find the count 0s in the array because it will take 1 operation to be -1.
- If there are no zeros in the array then just add 2 in the answer because it will take two steps to make -1 to 1.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int MinStep( int a[], int n)
{
int positive = 0,
negative = 0,
zero = 0;
int step = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == 0) {
zero++;
}
else if (a[i] < 0) {
negative++;
step = step + (-1 - a[i]);
}
else {
positive++;
step = step + (a[i] - 1);
}
}
if (negative % 2 == 0) {
step = step + zero;
}
else {
if (zero > 0) {
step = step + zero;
}
else {
step = step + 2;
}
}
return step;
}
int main()
{
int a[] = { 0, -2, -1, -3, 4 };
int n = sizeof (a) / sizeof (a[0]);
cout << MinStep(a, n);
return 0;
}
|
Java
class GFG {
static int MinStep( int a[], int n)
{
int positive = 0 ,
negative = 0 ,
zero = 0 ;
int step = 0 ;
for ( int i = 0 ; i < n; i++) {
if (a[i] == 0 ) {
zero++;
}
else if (a[i] < 0 ) {
negative++;
step = step + (- 1 - a[i]);
}
else {
positive++;
step = step + (a[i] - 1 );
}
}
if (negative % 2 == 0 ) {
step = step + zero;
}
else {
if (zero > 0 ) {
step = step + zero;
}
else {
step = step + 2 ;
}
}
return step;
}
public static void main(String[] args)
{
int a[] = { 0 , - 2 , - 1 , - 3 , 4 };
int n = a.length;
System.out.println(MinStep(a, n));
}
}
|
Python3
def MinStep(a, n):
positive = 0 ;
negative = 0 ;
zero = 0 ;
step = 0 ;
for i in range (n):
if (a[i] = = 0 ):
zero + = 1 ;
elif (a[i] < 0 ):
negative + = 1 ;
step = step + ( - 1 - a[i]);
else :
positive + = 1 ;
step = step + (a[i] - 1 );
if (negative % 2 = = 0 ):
step = step + zero;
else :
if (zero > 0 ):
step = step + zero;
else :
step = step + 2 ;
return step;
if __name__ = = '__main__' :
a = [ 0 , - 2 , - 1 , - 3 , 4 ];
n = len (a);
print (MinStep(a, n));
|
C#
using System;
class GFG {
static int MinStep( int [] a, int n)
{
int positive = 0,
negative = 0,
zero = 0;
int step = 0;
for ( int i = 0; i < n; i++) {
if (a[i] == 0) {
zero++;
}
else if (a[i] < 0) {
negative++;
step = step + (-1 - a[i]);
}
else {
positive++;
step = step + (a[i] - 1);
}
}
if (negative % 2 == 0) {
step = step + zero;
}
else {
if (zero > 0) {
step = step + zero;
}
else {
step = step + 2;
}
}
return step;
}
static public void Main()
{
int [] a = { 0, -2, -1, -3, 4 };
int n = a.Length;
Console.Write(MinStep(a, n));
}
}
|
Javascript
<script>
function MinStep(a, n)
{
let positive = 0,
negative = 0,
zero = 0;
let step = 0;
for (let i = 0; i < n; i++) {
if (a[i] == 0) {
zero++;
}
else if (a[i] < 0) {
negative++;
step = step + (-1 - a[i]);
}
else {
positive++;
step = step + (a[i] - 1);
}
}
if (negative % 2 == 0) {
step = step + zero;
}
else {
if (zero > 0) {
step = step + zero;
}
else {
step = step + 2;
}
}
return step;
}
let a = [ 0, -2, -1, -3, 4 ];
let n = a.length;
document.write(MinStep(a, n));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
?list=PLM68oyaqFM7Q-sv3gA5xbzfgVkoQ0xDrW
Last Updated :
19 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...