Minimize 0 by optimally replacing non zero elements with -1
Given an integer N, denoting the size of a circular array containing non-zero elements and an array arr[] of size M having the indices of zeroes in the circular array. In each moment, elements adjacent to 0 (except -1) change to 0. The task is to find the minimum number of 0s that will be present in the circular array if we optimally replace one non-zero element with -1 in each moment.
Examples:
Input: N = 10, M = 3, arr[] = {2, 5, 7}
Output: 7
Explanation:
During first iteration, indices 2, 5, and 7 have value 0. Choose index 1 and make curr[1] = -1.
During second iteration, indices 2, 3, 4, 5, 6, 7, and 8 have 0. Choose index 9 to make curr[9] = -1.
During third iteration, no more elements can be made zero. So finally number of 0’s is 7.
Input: N = 6, M = 2, arr[] = {2, 5}
Output: 5
Approach: This can be solved using the following idea:
The key observation is that we need place -1 s in non-zero segments. The optimal idea is to prioritize the longer segments because the loss of non-zero elements will last longer than shorter segments and will change more number of non-zero elements to 0.
- If there are x elements in a segment and the process last y moments, then there will be x-2*y elements at the end which are not 0. Simultaneously, every moment we can make at least one element -1, which indicates that if x−2*y>0, we have an opportunity to get one element -1.
Follow the below steps to implement the idea:
- First sort the array arr[].
- Make an array to store the difference between consecutive elements.
- Sort the newly generated array in decreasing order.
- Initialize a variable res to store the number of non-zero elements
- Traverse the new array and count the new non-zero elements and
- If the number of elements between two consecutive elements of arrays is 1 then make res = res+1.
- Otherwise, make res = res+x-1.
- Return N – res.
Below is the implementation for the above approach.
C++
#include <bits/stdc++.h>
using namespace std;
void findStability( int n, int m, vector< int >& arr)
{
sort(arr.begin(), arr.end());
arr.push_back(arr[0]);
vector< int > dist;
for ( int i = 1; i <= m; i++) {
int curr = arr[i] - arr[i - 1] - 1;
if (curr < 0) {
curr += n;
}
dist.push_back(curr);
}
sort(dist.begin(), dist.end(), greater< int >());
int res = 0, curr = 0;
for ( int i : dist) {
int x = i;
x -= curr;
if (x == 1) {
res++;
curr++;
}
else if (x > 1) {
res += x - 1;
curr += 4;
}
}
cout << n - res << '\n' ;
}
int main()
{
int N = 10, M = 3;
vector< int > arr = { 3, 6, 8 };
findStability(N, M, arr);
return 0;
}
|
Java
import java.io.*;
import java.util.*;
class GFG {
static void findStability( int n, int m,
List<Integer> arr)
{
Collections.sort(arr);
arr.add(arr.get( 0 ));
List<Integer> dist = new ArrayList<>();
for ( int i = 1 ; i <= m; i++) {
int curr = arr.get(i) - arr.get(i - 1 ) - 1 ;
if (curr < 0 ) {
curr += n;
}
dist.add(curr);
}
Collections.sort(dist, Collections.reverseOrder());
int res = 0 , curr = 0 ;
for ( int i : dist) {
int x = i;
x -= curr;
if (x == 1 ) {
res++;
curr++;
}
else if (x > 1 ) {
res += x - 1 ;
curr += 4 ;
}
}
System.out.println(n - res);
}
public static void main(String[] args)
{
int N = 10 , M = 3 ;
List<Integer> arr
= new ArrayList<>(Arrays.asList( 3 , 6 , 8 ));
findStability(N, M, arr);
}
}
|
C#
using System;
using System.Collections.Generic;
public class GFG{
static void findStability( int n, int m,
List< int > arr)
{
arr.Sort();
arr.Add(arr[0]);
List< int > dist = new List< int >();
int curr;
for ( int i = 1; i <= m; i++) {
curr = arr[i] - arr[i - 1] - 1;
if (curr < 0) {
curr += n;
}
dist.Add(curr);
}
dist.Sort((x, y) => y.CompareTo(x));
int res = 0;
curr = 0;
foreach ( var i in dist) {
int x = i;
x -= curr;
if (x == 1) {
res++;
curr++;
}
else if (x > 1) {
res += x - 1;
curr += 4;
}
}
Console.WriteLine(n - res);
}
static public void Main (){
int N = 10, M = 3;
List< int > arr
= new List< int >{3, 6, 8};
findStability(N, M, arr);
}
}
|
Javascript
function findStability( n, m, arr)
{
arr.sort();
arr.push(arr[0]);
let dist= new Array();
for (let i = 1; i <= m; i++) {
let curr = arr[i] - arr[i - 1] - 1;
if (curr < 0) {
curr += n;
}
dist.push(curr);
}
dist.sort();
dist.reverse();
let res = 0, curr = 0;
for (let i of dist) {
let x = i;
x -= curr;
if (x == 1) {
res++;
curr++;
}
else if (x > 1) {
res += x - 1;
curr += 4;
}
}
document.write(n - res );
}
let N = 10, M = 3;
let arr = [ 3, 6, 8 ];
findStability(N, M, arr);
|
Python3
import sys
def findStability(n, m, arr):
arr.sort()
arr.append(arr[ 0 ])
dist = []
for i in range ( 1 , m + 1 ):
curr = arr[i] - arr[i - 1 ] - 1
if curr < 0 :
curr + = n
dist.append(curr)
dist.sort(reverse = True )
res = 0
curr = 0
for i in dist:
x = i
x - = curr
if x = = 1 :
res + = 1
curr + = 1
elif x > 1 :
res + = x - 1
curr + = 4
print (n - res)
if __name__ = = "__main__" :
N = 10
M = 3
arr = [ 3 , 6 , 8 ]
findStability(N, M, arr)
|
Time Complexity: O(N * logN)
Auxiliary Space: O(N)
Related Articles:
Last Updated :
13 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...