Skip to content
Related Articles

Related Articles

Minimum 0s to be inserted in Array such that no element is same as its index

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 15 Mar, 2022
View Discussion
Improve Article
Save Article

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++




// C++ code for the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to count the
// No. of zeroes added
int minimum_Insertion(int A[], int N)
{
    // Variable to store the result
    int zero_added = 0;
 
    // Traverse through array and
    // Check whether array value is
    // Equal to index or not
    for (int i = 0; i < N; i++) {
 
        // If A[index] = index,
        // Increment the count of
        // Variable zero_added by 1.
        if (i + zero_added == A[i]) {
            zero_added++;
        }
    }
    return zero_added;
}
 
// Driver code
int main()
{
    int A[] = { 7, 2, 2, 4, 5, 8 };
    int N = 6;
 
    // Display the minimum number
    // of zero insertion required
    cout << minimum_Insertion(A, N);
    return 0;
}


Java




// JAVA code for the above approach
import java.util.*;
class GFG
{
 
  // Function to count the
  // No. of zeroes added
  public static int minimum_Insertion(int A[], int N)
  {
 
    // Variable to store the result
    int zero_added = 0;
 
    // Traverse through array and
    // Check whether array value is
    // Equal to index or not
    for (int i = 0; i < N; i++) {
 
      // If A[index] = index,
      // Increment the count of
      // Variable zero_added by 1.
      if (i + zero_added == A[i]) {
        zero_added++;
      }
    }
    return zero_added;
  }
 
  // Driver code
  public static void main(String[] args)
  {
    int A[] = new int[] { 7, 2, 2, 4, 5, 8 };
    int N = 6;
 
    // Display the minimum number
    // of zero insertion required
    System.out.print(minimum_Insertion(A, N));
  }
}
 
// This code is contributed by Taranpreet


Python3




# Python code for the above approach
 
# Function to count the
# No. of zeroes added
def minimum_Insertion(A, N):
         
    # Variable to store the result
    zero_added = 0
 
    # Traverse through array and
    # Check whether array value is
    # Equal to index or not
    for i in range(N):
 
        # If A[index] = index,
        # Increment the count of
        # Variable zero_added by 1.
        if (i + zero_added == A[i]):
            zero_added += 1
 
    return zero_added
 
# driver code
A = [7, 2, 2, 4, 5, 8]
N = 6
 
# Display the minimum number
# of zero insertion required
print(minimum_Insertion(A, N))
 
# This code is contributed by ShinjanPatra


C#




// C# program to implement
// the above approach
using System;
 
public class GFG
{
 
  // Function to count the
  // No. of zeroes added
  public static int minimum_Insertion(int[] A, int N)
  {
 
    // Variable to store the result
    int zero_added = 0;
 
    // Traverse through array and
    // Check whether array value is
    // Equal to index or not
    for (int i = 0; i < N; i++) {
 
      // If A[index] = index,
      // Increment the count of
      // Variable zero_added by 1.
      if (i + zero_added == A[i]) {
        zero_added++;
      }
    }
    return zero_added;
  }
 
  // Driver Code
  public static void Main(String []args)
  {
    int[] A = new int[] { 7, 2, 2, 4, 5, 8 };
    int N = 6;
 
    // Display the minimum number
    // of zero insertion required
    Console.WriteLine(minimum_Insertion(A, N));
  }
}
 
// This code is contributed by code_hunt.


Javascript




<script>
       // JavaScript code for the above approach
 
       // Function to count the
       // No. of zeroes added
       function minimum_Insertion(A, N)
       {
        
           // Variable to store the result
           let zero_added = 0;
 
           // Traverse through array and
           // Check whether array value is
           // Equal to index or not
           for (let i = 0; i < N; i++) {
 
               // If A[index] = index,
               // Increment the count of
               // Variable zero_added by 1.
               if (i + zero_added == A[i]) {
                   zero_added++;
               }
           }
           return zero_added;
       }
 
       // Driver code
       let A = [7, 2, 2, 4, 5, 8];
       let N = 6;
 
       // Display the minimum number
       // of zero insertion required
       document.write(minimum_Insertion(A, N));
 
    // This code is contributed by Potta Lokesh
   </script>


 
 

Output

2

 

Time Complexity: O( N )
Auxiliary Space: O( 1 )

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!