要在 Array 中插入的最小 0 使得没有元素与其索引相同
给定一个数组A = [A 0 , A 1 , A 2 , . . ., 一个N-1 ] .执行以下操作:
- 值与其位置相同的索引的总数。
- 在每个步骤中,在其中一个位置插入0 。
- 重复直到不再存在值与索引相同的元素。
任务是找到所需的最小插入次数,使得没有元素与其索引相同,即对于每个 i (0 ≤ i < N),A[i] ≠ i。
例子:
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
方法:该问题的解决方案是基于在使用数组遍历插入一些 0 后找到特定元素的位置。请按照以下步骤操作:
- 创建一个变量来存储添加的零的数量。
- 遍历数组并在每次迭代中:
- 检查(索引号+之前添加的零个数)是否等于数组值。
- 如果相等,则将结果变量值加一。
- 如果 2 个索引与其各自的元素匹配,则首先将最左边的元素转换为 0。
- 返回结果。
按照下图理解问题
插图:
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.
下面是上述方法的实现:
C++
// C++ code for the above approach
#include
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
2
时间复杂度: 在 )
辅助空间: O(1)