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++
// C++ Algorithm for the above approach
#include <iostream>
using namespace std;
// Function which returns
// minimum number of operations
// required to convert array
// into target array
int minOperations(int nums[], int N)
{
// ans variable contains number of
// operations processed after traversing
// ith element
int ans = 0;
int i = 0;
// Traversing to get first non-zero element
while (i < N && nums[i] == 0)
i++;
// After getting first non-zero element we will
// apply the given operation
while (i < N - 1) {
if (nums[i] == 0)
ans++;
else
ans += nums[i];
i++;
}
// Returning the ans variable
return ans;
}
// Driver Code
int main()
{
int N = 5;
int nums[] = { 0, 2, 0, 2, 0 };
// Function Call
int ans = minOperations(nums, N);
cout << ans;
return 0;
}
// This code is contributed by Kdheeraj.
Java
// Java algorithm of the above approach
import java.util.*;
class GFG {
// Driver Code
public static void main(String[] args)
{
int N = 5;
int[] nums = { 0, 2, 0, 2, 0 };
// Function Call
int ans = minOperations(nums, N);
System.out.println(ans);
}
// Function which returns
// minimum number of operations
// required to convert array
// into target array
public static int minOperations(int[] nums, int N)
{
// ans variable contains number of
// operations processed after traversing
// ith element
int ans = 0;
int i = 0;
// Traversing to get first non-zero element
while (i < N && nums[i] == 0)
i++;
// After getting first non-zero element we will
// apply the given operation
while (i < N - 1) {
if (nums[i] == 0)
ans++;
else
ans += nums[i];
i++;
}
// Returning the ans variable
return ans;
}
}
Python3
# Python code to implement the above approach
# Function which returns
# minimum number of operations
# required to convert array
# into target array
def minOperations(nums, N) :
# ans variable contains number of
# operations processed after traversing
# ith element
ans = 0
i = 0
# Traversing to get first non-zero element
while (i < N and nums[i] == 0) :
i += 1
# After getting first non-zero element we will
# apply the given operation
while (i < N - 1) :
if (nums[i] == 0) :
ans += 1
else :
ans += nums[i]
i += 1
# Returning the ans variable
return ans
# Driver Code
if __name__ == "__main__":
N = 5
nums = [ 0, 2, 0, 2, 0 ]
# Function Call
ans = minOperations(nums, N)
print(ans)
# This code is contributed by sanjoy_62.
C#
// C# program for above approach:
using System;
class GFG {
// Function which returns
// minimum number of operations
// required to convert array
// into target array
public static int minOperations(int[] nums, int N)
{
// ans variable contains number of
// operations processed after traversing
// ith element
int ans = 0;
int i = 0;
// Traversing to get first non-zero element
while (i < N && nums[i] == 0)
i++;
// After getting first non-zero element we will
// apply the given operation
while (i < N - 1) {
if (nums[i] == 0)
ans++;
else
ans += nums[i];
i++;
}
// Returning the ans variable
return ans;
}
// Driver Code
public static void Main()
{
int N = 5;
int[] nums = { 0, 2, 0, 2, 0 };
// Function Call
int ans = minOperations(nums, N);
Console.Write(ans);
}
}
// This code is contributed by code_hunt.
JavaScript
<script>
// Javascript Algorithm for the above approach
// Function which returns
// minimum number of operations
// required to convert array
// into target array
function minOperations(nums, N)
{
// ans variable contains number of
// operations processed after traversing
// ith element
let ans = 0;
let i = 0;
// Traversing to get first non-zero element
while (i < N && nums[i] == 0)
i++;
// After getting first non-zero element we will
// apply the given operation
while (i < N - 1) {
if (nums[i] == 0)
ans++;
else
ans += nums[i];
i++;
}
// Returning the ans variable
return ans;
}
// Driver Code
let N = 5;
let nums = [ 0, 2, 0, 2, 0 ];
// Function Call
let ans = minOperations(nums, N);
document.write(ans);
// This code is contributed by satwik4409.
</script>
Time Complexity: O(N)
Auxiliary Space: O(1)
Similar Reads
Minimize cost to convert all array elements to 0 Given two integers X and Y and a binary array arr[] of length N whose first and last element is 1, the task is to minimize the cost to convert all array elements to 0, where X and Y represent the cost of converting a subarray of all 1s to 0s and the cost of converting any element to 0 respectively.
12 min read
Minimum cost to equal all elements of array using two operation Given an array arr[] of n positive integers. There are two operations allowed: Operation 1 : Pick any two indexes, increase value at one index by 1 and decrease value at another index by 1. It will cost a. Operation 2 : Pick any index and increase its value by 1. It will cost b. The task is to find
8 min read
Make all Array elements equal to zero in atmost m operations Given an integer array A[] of size N and integer k. For a fixed value p, choose an index i (1 ⤠i ⤠n) and assign A[i] = max(0, A[i] â p), this counts as one operation, and the task is to find the smallest value of p such that all the elements of the array A[] become 0 in at most k operations. Examp
10 min read
Minimize the non-zero elements in the Array by given operation Given an array arr[] of length N, the task is to minimize the count of the number of non-zero elements by adding the value of the current element to any of its adjacent element and subtracting from the current element at most once.Examples: Input: arr[] = { 1, 0, 1, 0, 0, 1 } Output: 2 Explanation:
5 min read
Minimize the non-zero elements in the Array by given operation Given an array arr[] of length N, the task is to minimize the count of the number of non-zero elements by adding the value of the current element to any of its adjacent element and subtracting from the current element at most once.Examples: Input: arr[] = { 1, 0, 1, 0, 0, 1 } Output: 2 Explanation:
5 min read
Minimum operations to convert given string into all 1s or 0s Given a string S containing 0s and 1s of length N with X, the task is to output the minimum operations required to convert S into either all 1s or 0s. The operation is defined as, taking two different indices i and j (1-based indexing), such that they give equal remainder when dividing from X, then
8 min read