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++
// 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>
Time Complexity: O( N )
Auxiliary Space: O( 1 )
Similar Reads
Rearrange given array such that no array element is same as its index Given an array arr[] consisting of N distinct integers, the task is to rearrange the array such that no element is same as its index ( 1-based indexing ). If multiple solutions exist, print any one of them. Examples: Input: arr[] = {4, 2, 3, 1}Output: 3 1 4 2Explanation: The elements at indices {1,
7 min read
Minimum elements to be inserted such that no Subarray has sum 0 Given an array arr[] of N integers such that no element is 0 in that array, the task is to find the minimum number of elements to be inserted such that no subarray of the new array has sum 0. Examples: Input: N = 3, arr[] = {1, -1, 1}Output: 2Explanation: As in the array the sum of first two element
6 min read
Minimize deletion or insertions of Array elements such that arr[i] have frequency as its value Given an array arr[] of length N, the task is to find the minimum number of operations to change the array such that each arr[i] occurs arr[i] times where in each operation either one element can be deleted from the array or one element can be inserted in the array. Examples: Input: N = 4, arr[ ] =
7 min read
Minimum index i such that all the elements from index i to given index are equal Given an array arr[] of integers and an integer pos, the task is to find the minimum index i such that all the elements from index i to index pos are equal. Examples: Input: arr[] = {2, 1, 1, 1, 5, 2}, pos = 3 Output: 1 Elements in index range [1, 3] are all equal to 1. Input: arr[] = {2, 1, 1, 1, 5
12 min read
Minimum array element changes to make its elements 1 to N Suppose you are given an array with N elements with any integer values. You need to find the minimum number of elements of the array which must be changed so that array has all integer values between 1 and N(including 1, N). Examples: Input : arr[] = {1 4 5 3 7} Output : 1 We need to replace 7 with
5 min read