Minimize operations to convert Array elements to 0s
Last Updated :
12 Aug, 2022
Given an array nums[] of length N containing non-negative integers, the task is to convert all elements from index 0 to N-2 to zero, after doing the below operations minimum number of times.
- In one operation select two indices i and j such that i < j and all the elements between i and j has to be non-zero, then set nums[i] = nums[i] – 1 and nums[j] = nums[j] + 1.
Examples:
Input: N = 5, nums[] = [0, 2, 0, 2, 0]
Output: 5
Explanation: We need to do 5 above given operation
to complete the task, and the operations are:
- Select j = 3, k = 4 nums[] becomes: [0, 2, 0, 1, 1]
- Select j = 1, k = 2 nums[] becomes: [0, 1, 1, 1, 1]
- Select j = 1, k = 4 nums[] becomes: [0, 0, 1, 1, 2]
- Select j = 2, k = 4 nums[] becomes: [0, 0, 0, 1, 3]
- Select j = 3, k = 4 nums[] becomes: [0, 0, 0, 0, 4]
Input: N = 3, nums[] = [2, 0, 0]
Output: 3
Explanation: We need to do 3 above given operation
to complete the task, and the operations are:
- Select j = 0, k = 1 nums[] becomes: [1, 1, 0]
- Select j = 0, k = 2 nums[] becomes: [0, 1, 1]
- Select j = 1, k = 2 nums[] becomes: [0, 0, 2]
Approach: The problem can be solved based on the following idea:
- The possible way to convert all elements from 0 to N-2 to 0 is by reducing each element from the left by 1 and adding 1 each time to the last element, until leftmost element become zero and then moving to the next index. So the number of operations will be the sum of elements from 0 to N-2.
- But if there is a 0 present in between then first we have to convert that 0 into a positive number, so that we can perform the operation for the elements present in the left of that 0.
- To convert a 0 to a positive element, 1 operation will take place, so our final answer will be the sum of elements from 0 to N-2 and the number of zeros present after the first positive element.
Follow the steps given below to implement the approach:
- First traverse the array till you get the first non-zero element.
- After that if element is non-zero then update ans += nums[i]
- Else if the element is zero then update ans += 1.
- Traverse the given array till N-2 and calculate the required answer as mentioned above.
- Return the calculated answer.
Below is the implementation of the above approach.
C++
#include <iostream>
using namespace std;
int minOperations( int nums[], int N)
{
int ans = 0;
int i = 0;
while (i < N && nums[i] == 0)
i++;
while (i < N - 1) {
if (nums[i] == 0)
ans++;
else
ans += nums[i];
i++;
}
return ans;
}
int main()
{
int N = 5;
int nums[] = { 0, 2, 0, 2, 0 };
int ans = minOperations(nums, N);
cout << ans;
return 0;
}
|
Java
import java.util.*;
class GFG {
public static void main(String[] args)
{
int N = 5 ;
int [] nums = { 0 , 2 , 0 , 2 , 0 };
int ans = minOperations(nums, N);
System.out.println(ans);
}
public static int minOperations( int [] nums, int N)
{
int ans = 0 ;
int i = 0 ;
while (i < N && nums[i] == 0 )
i++;
while (i < N - 1 ) {
if (nums[i] == 0 )
ans++;
else
ans += nums[i];
i++;
}
return ans;
}
}
|
Python3
def minOperations(nums, N) :
ans = 0
i = 0
while (i < N and nums[i] = = 0 ) :
i + = 1
while (i < N - 1 ) :
if (nums[i] = = 0 ) :
ans + = 1
else :
ans + = nums[i]
i + = 1
return ans
if __name__ = = "__main__" :
N = 5
nums = [ 0 , 2 , 0 , 2 , 0 ]
ans = minOperations(nums, N)
print (ans)
|
C#
using System;
class GFG {
public static int minOperations( int [] nums, int N)
{
int ans = 0;
int i = 0;
while (i < N && nums[i] == 0)
i++;
while (i < N - 1) {
if (nums[i] == 0)
ans++;
else
ans += nums[i];
i++;
}
return ans;
}
public static void Main()
{
int N = 5;
int [] nums = { 0, 2, 0, 2, 0 };
int ans = minOperations(nums, N);
Console.Write(ans);
}
}
|
Javascript
<script>
function minOperations(nums, N)
{
let ans = 0;
let i = 0;
while (i < N && nums[i] == 0)
i++;
while (i < N - 1) {
if (nums[i] == 0)
ans++;
else
ans += nums[i];
i++;
}
return ans;
}
let N = 5;
let nums = [ 0, 2, 0, 2, 0 ];
let ans = minOperations(nums, N);
document.write(ans);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...