📌  相关文章
📜  要在 Array 中插入的最小 0 使得没有元素与其索引相同

📅  最后修改于: 2022-05-13 01:56:08.169000             🧑  作者: Mango

要在 Array 中插入的最小 0 使得没有元素与其索引相同

给定一个数组A = [A 0 , A 1 , A 2 , . . ., 一个N-1 ] .执行以下操作:

  • 值与其位置相同的索引的总数。
  • 在每个步骤中,在其中一个位置插入0
  • 重复直到不再存在值与索引相同的元素。

任务是找到所需的最小插入次数,使得没有元素与其索引相同,即对于每个 i (0 ≤ i < N),A[i] ≠ i。

例子:

方法:该问题的解决方案是基于在使用数组遍历插入一些 0 后找到特定元素的位置。请按照以下步骤操作:

  • 创建一个变量来存储添加的零的数量。
  • 遍历数组并在每次迭代中:
    • 检查(索引号+之前添加的零个数)是否等于数组值。
    • 如果相等,则将结果变量值加一。
  • 如果 2 个索引与其各自的元素匹配,则首先将最左边的元素转换为 0。
  • 返回结果。

按照下图理解问题

插图:

下面是上述方法的实现:

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)