Minimum 0s to be inserted in Array such that no element is same as its index
Last Updated :
15 Mar, 2022
Given an array A = [A0, A1, A2, . . ., AN-1]. Perform the following operation:
- Total count of indices with value same as their positions.
- At each step, insert 0 at one of such positions.
- Repeat till no more elements exist whose value is same as the index.
The task is to find the minimum number of insertions required such that no element is same as its index i.e. for each i (0 ≤ i < N), A[i] ≠i.
Examples:
Input: A = {4, 3, 5}
Output: 0
Explanation: Here, no insertion of 0 is required because for each index, A[index] ≠index.
Input: A = {7, 2, 2, 4, 5, 8}
Output: 2
Explanation: Here, insertion of 0 is required at index 2 and 4. After insertion the array becomes :
Array = 7 2 0 2 0 4 5 8
Index = 0 1 2 3 4 5 6 7
Approach: The solution to the problem is based on finding the position of a particular element after inserting some 0s using array traversal. Follow the steps mentioned below:
- Create a variable to store the number of zero added.
- Traverse through the array and in each iteration:
- Check whether the (index number+ number of zero added before) is equal to array value or not.
- If that are equal, Increment the result variable value by one.
- If 2 indices are matching with their respective elements then first convert the leftmost element to 0.
- Return the result.
Follow the illustration below to understand the problem
Illustration:
Consider an array of length N = 6
A = {7, 2, 2, 4, 5, 8}
Index = 0 1 2 3 4 5
From above representation it is clear that, value at index 2 is 2.
- So, 1st insertion is required at any index less than or equal to 2 (i.e index 1 and index 2) to satisfy the given condition.
- After Insertion at index 2 the array becomes
A[] = 7 2 0 2 4 5 8
Index = 0 1 2 3 4 5 6
- Now, A[4] = 4 and A[5] = 5, So one more insertion required at index 4
A[] = 7 2 0 2 4 5 8
Index = 0 1 2 3 4 5 6
- After Insertion at index 4 the array becomes
A[] = 7 2 0 2 0 4 5 8
Index = 0 1 2 3 4 5 6 7
Now, this array satisfy the condition for each 0 ≤ index < N, A[index] ≠index.
So, Output for the given array is 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int minimum_Insertion( int A[], int N)
{
int zero_added = 0;
for ( int i = 0; i < N; i++) {
if (i + zero_added == A[i]) {
zero_added++;
}
}
return zero_added;
}
int main()
{
int A[] = { 7, 2, 2, 4, 5, 8 };
int N = 6;
cout << minimum_Insertion(A, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static int minimum_Insertion( int A[], int N)
{
int zero_added = 0 ;
for ( int i = 0 ; i < N; i++) {
if (i + zero_added == A[i]) {
zero_added++;
}
}
return zero_added;
}
public static void main(String[] args)
{
int A[] = new int [] { 7 , 2 , 2 , 4 , 5 , 8 };
int N = 6 ;
System.out.print(minimum_Insertion(A, N));
}
}
|
Python3
def minimum_Insertion(A, N):
zero_added = 0
for i in range (N):
if (i + zero_added = = A[i]):
zero_added + = 1
return zero_added
A = [ 7 , 2 , 2 , 4 , 5 , 8 ]
N = 6
print (minimum_Insertion(A, N))
|
C#
using System;
public class GFG
{
public static int minimum_Insertion( int [] A, int N)
{
int zero_added = 0;
for ( int i = 0; i < N; i++) {
if (i + zero_added == A[i]) {
zero_added++;
}
}
return zero_added;
}
public static void Main(String []args)
{
int [] A = new int [] { 7, 2, 2, 4, 5, 8 };
int N = 6;
Console.WriteLine(minimum_Insertion(A, N));
}
}
|
Javascript
<script>
function minimum_Insertion(A, N)
{
let zero_added = 0;
for (let i = 0; i < N; i++) {
if (i + zero_added == A[i]) {
zero_added++;
}
}
return zero_added;
}
let A = [7, 2, 2, 4, 5, 8];
let N = 6;
document.write(minimum_Insertion(A, N));
</script>
|
Time Complexity: O( N )
Auxiliary Space: O( 1 )
Share your thoughts in the comments
Please Login to comment...