Minimize the non-zero elements in the Array by given operation
Last Updated :
10 Nov, 2021
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:
Operation 1: arr[0] -> arr[1], arr[] = {0, 1, 1, 0, 0, 1}
Operation 2: arr[2] -> arr[1], arr[] = {0, 2, 0, 0, 0, 1}
Count of non-zero elements = 2
Input: arr[] = { 1, 0, 1, 1, 1, 0, 1 }
Output: 3
Explanation:
Operation 1: arr[2] -> arr[3], arr[] = {1, 0, 0, 2, 1, 0, 1}
Operation 2: arr[4] -> arr[3], arr[] = {1, 0, 0, 3, 0, 0, 1}
Count of non-zero elements = 3
Approach: The idea is to use greedy algorithms to choose greedily at each step. The key observation in the problem is there can be only three possibilities of three consecutive indices i, j and k, i.e.
- Both the end index have non-zero element.
- Any one of the index have non-zero element.
In the above two cases, we can always add the values to the middle element and subtract to the adjacent elements. Similarly, we can greedily choose the operations and update the elements of the array to minimize the non-zero elements.
Below is the implementation of the above approach:
C++
// C++ implementation to minimize the
// non-zero elements in the array
#include <bits/stdc++.h>
using namespace std;
// Function to minimize the non-zero
// elements in the given array
int minOccupiedPosition(int A[], int n)
{
// To store the min pos needed
int minPos = 0;
// Loop to iterate over the elements
// of the given array
for (int i = 0; i < n; ++i) {
// If current position A[i] is occupied
// the we can place A[i], A[i+1] and A[i+2]
// elements together at A[i+1] if exists.
if (A[i] > 0) {
++minPos;
i += 2;
}
}
return minPos;
}
// Driver Code
int main()
{
int A[] = { 8, 0, 7, 0, 0, 6 };
int n = sizeof(A) / sizeof(A[0]);
// Function Call
cout << minOccupiedPosition(A, n);
return 0;
}
Java
// Java implementation to minimize the
// non-zero elements in the array
class GFG{
// Function to minimize the non-zero
// elements in the given array
static int minOccupiedPosition(int A[], int n)
{
// To store the min pos needed
int minPos = 0;
// Loop to iterate over the elements
// of the given array
for (int i = 0; i < n; ++i)
{
// If current position A[i] is occupied
// the we can place A[i], A[i+1] and A[i+2]
// elements together at A[i+1] if exists.
if (A[i] > 0) {
++minPos;
i += 2;
}
}
return minPos;
}
// Driver Code
public static void main(String[] args)
{
int A[] = { 8, 0, 7, 0, 0, 6 };
int n = A.length;
// Function Call
System.out.print(minOccupiedPosition(A, n));
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 implementation to minimize the
# non-zero elements in the array
# Function to minimize the non-zero
# elements in the given array
def minOccupiedPosition(A, n):
# To store the min pos needed
minPos = 0
# Loop to iterate over the elements
# of the given array
i = 0
while i < n:
# If current position A[i] is
# occupied the we can place A[i],
# A[i+1] and A[i+2] elements
# together at A[i+1] if exists.
if(A[i] > 0):
minPos += 1
i += 2
i += 1
return minPos
# Driver Code
if __name__ == '__main__':
A = [ 8, 0, 7, 0, 0, 6 ]
n = len(A)
# Function Call
print(minOccupiedPosition(A, n))
# This code is contributed by Shivam Singh
C#
// C# implementation to minimize the
// non-zero elements in the array
using System;
class GFG {
// Function to minimize the non-zero
// elements in the given array
static int minOccupiedPosition(int[] A, int n)
{
// To store the min pos needed
int minPos = 0;
// Loop to iterate over the elements
// of the given array
for(int i = 0; i < n; ++i)
{
// If current position A[i] is occupied
// the we can place A[i], A[i+1] and A[i+2]
// elements together at A[i+1] if exists.
if (A[i] > 0)
{
++minPos;
i += 2;
}
}
return minPos;
}
// Driver code
static void Main()
{
int[] A = { 8, 0, 7, 0, 0, 6 };
int n = A.Length;
// Function Call
Console.WriteLine(minOccupiedPosition(A, n));
}
}
// This code is contributed by divyeshrabadiya07
JavaScript
<script>
// Javascript implementation to minimize the
// non-zero elements in the array
// Function to minimize the non-zero
// elements in the given array
function minOccupiedPosition( A, n)
{
// To store the min pos needed
var minPos = 0;
// Loop to iterate over the elements
// of the given array
for (var i = 0; i < n; ++i) {
// If current position A[i] is occupied
// the we can place A[i], A[i+1] and A[i+2]
// elements together at A[i+1] if exists.
if (A[i] > 0) {
++minPos;
i += 2;
}
}
return minPos;
}
var A = [ 8, 0, 7, 0, 0, 6 ];
var n = A.length;
// Function Call
document.write(minOccupiedPosition(A, n));
// This code is contributed by SoumikMondal
</script>
Time Complexity: O(N).
Auxiliary Space: O(1)
Similar Reads
Minimize steps to make Array elements equal by using giving operations Given an arr[] of length N. Then the task is to find minimum steps to make all the elements of the given array equal. Two types of operations can be performed as given below: Choose a prefix of size K, where arr[K] = max element in that chosen sub-array and convert all elements equal to max. Choose
8 min read
Minimize operations to convert Array elements to 0s 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
6 min read
Minimum no. of operations required to make all Array Elements Zero Given an array of N elements and each element is either 1 or 0. You need to make all the elements of the array equal to 0 by performing the below operations: If an element is 1, You can change it's value equal to 0 then, if the next consecutive element is 1, it will automatically get converted to 0.
12 min read
Minimize the number of operations to make all the elements equal with given conditions Given an array arr[]. The task is to minimize the number of operations required to make all the elements in arr[] equal. It is allowed to replace any element in arr[] with any other element almost once. Find the minimum number of operations required to do so, in one operation take any suffix of arr[
7 min read
Minimum operations of given type required to empty given array Given an array arr[] of size N, the task is to find the total count of operations required to remove all the array elements such that if the first element of the array is the smallest element, then remove that element, otherwise move the first element to the end of the array. Examples: Input: A[] =
14 min read