📌  相关文章
📜  使用给定的操作检查 1 到 N 的给定排列是否可行

📅  最后修改于: 2021-09-04 11:23:01             🧑  作者: Mango

给定一个大小为N的数组arr[] ,检查该数组是否在以下约束下构建的任务:

  1. 该数组只能包含从 1 到 N 的数字。
  2. 我们必须按顺序构建数组。这意味着首先我们放置 1,然后是 2,依此类推直到 N。
  3. 如果数组为空,那么我们可以在任何位置放置一个数字。
  4. 如果它不为空,那么我们可以将下一个元素放在前一个元素的下一个位置。如果下一个位置超出数组大小或已经填满,那么我们可以选择任何未被占用的位置并放置数字。

例子:

方法:

  1. 首先,将每个数组元素的索引存储在map 中
  2. 将下一个元素的位置存储在‘next’ 中。最初,由于数组为空,因此next包含1的位置。
  3. 迭代[1, N]并检查当前元素是否存在于下一个索引处。如果不是,则返回-1。
  4. 在每次迭代中,将当前位置标记为已访问并更新一个值的可能索引的下一个。如果当前索引的下一个索引(i + 1)未被访问,则更新 (i + 1)旁边的索引。否则,如果下一个可能的索引超出数组索引范围,则存储地图中下一个元素的位置,因为它可以放置在地图中当前索引之前的任何索引处。
  5. 在完全遍历数组时,返回 true,因为所有索引都已放置在各自的下一个索引处。

下面是上述方法的实现。

C++
// C++ program to Check If we
// can build the given array
// under given constraints.
 
#include 
using namespace std;
 
// Function return true if we
// can build the array
bool CheckArray(int a[], int n)
{
    int i, f = 0, next;
 
    // First one is to keep position
    // of each element
    // Second one is for marking
    // the current position
    map pos, vis;
 
    for (i = 0; i < n; i++) {
        pos[a[i]] = i;
    }
 
    // Initially next contains
    // the position of 1.
    next = pos[1];
 
    for (i = 1; i <= n; i++) {
        // Mark the current
        // position.
        vis[next] = 1;
 
        // If the element is not
        // present at that position
        // then it is impossible
        // to build
        if (i != a[next]) {
            return false;
        }
 
        // Updating the next
        if (next + 1 == n
            || vis[next + 1]) {
 
            // If the next position is
            // out of array size or next
            // position is not empty then
            // we use the map to find the
            // position of the next element
            next = pos[i + 1];
        }
        else
            // Else just increment it
            next++;
    }
 
    return true;
}
 
// Driver code
int main()
{
 
    int arr[] = { 2, 3, 4, 5, 1 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    if (CheckArray(arr, N)) {
        cout << "YES" << endl;
    }
    else {
        cout << "NO" << endl;
    }
 
    return 0;
}


Java
// Java program to check If we
// can build the given array
// under given constraints.
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function return true if we
// can build the array
static boolean CheckArray(int[] a, int n)
{
    int i, f = 0, next;
 
    // First one is to keep position
    // of each element
    // Second one is for marking
    // the current position
    HashMap pos = new HashMap();
    HashMap vis = new HashMap();
    vis.put(0, 0);
 
    for(i = 0; i < n; i++)
    {
        pos.put(a[i], i);
    }
 
    // Initially next contains
    // the position of 1.
    next = pos.getOrDefault(1, 0);
 
    for(i = 1; i <= n; i++)
    {
         
        // Mark the current
        // position.
        vis.put(next, 1);
 
        // If the element is not
        // present at that position
        // then it is impossible
        // to build
        if (i != a[next])
        {
            return false;
        }
 
        // Updating the next
        if (next + 1 == n ||
            vis.getOrDefault(next + 1, 0) != 0)
        {
             
            // If the next position is
            // out of array size or next
            // position is not empty then
            // we use the map to find the
            // position of the next element
            next = pos.getOrDefault(i + 1, 0);
        }
        else
         
            // Else just increment it
            next++;
    }
    return true;
}
 
// Driver code
public static void main(String[] args)
{
    int[] arr = { 2, 3, 4, 5, 1 };
    int N = arr.length;
 
    if (CheckArray(arr, N) == true)
    {
        System.out.println("YES");
    }
    else
    {
        System.out.println("NO");
    }
}
}
 
// This code is contributed by akhilsaini


Python3
# Python3 program to check if we
# can build the given array
# under given constraints.
 
# Function return true if we
# can build the array
def CheckArray(a, n):
     
    f = 0
 
    # First one is to keep position
    # of each element
    # Second one is for marking
    # the current position
    pos = {}
    vis = {}
     
    for i in range(n):
        pos[a[i]] = i
 
    # Initially next contains
    # the position of 1.
    next = pos[1]
     
    for i in range(1, n + 1):
         
        # Mark the current
        # position.
        vis[next] = 1
 
        # If the element is not
        # present at that position
        # then it is impossible
        # to build
        if (i != a[next]):
            return False
 
        # Updating the next
        if (next + 1 == n or
           (next + 1 in vis and
                        vis[next + 1])):
             
            # If the next position is
            # out of array size or next
            # position is not empty then
            # we use the map to find the
            # position of the next element
            if(i + 1 in pos):
                next = pos[i + 1]
        else:
             
            # Else just increment it
            next += 1
 
    return True
 
# Driver code
arr = [ 2, 3, 4, 5, 1 ]
N = len(arr)
 
if (CheckArray(arr, N)):
    print('YES')
else:
    print('NO')
 
# This code is contributed by yatinagg


C#
// C# program to check If we
// can build the given array
// under given constraints.
using System;
using System.Collections;
 
class GFG{
 
// Function return true if we
// can build the array
static bool CheckArray(int[] a, int n)
{
    int i, next;
 
    // First one is to keep position
    // of each element
    // Second one is for marking
    // the current position
    Hashtable pos = new Hashtable();
      Hashtable vis = new Hashtable();
 
    for(i = 0; i < n; i++)
    {
        pos.Add(a[i], i);
    }
 
    // Initially next contains
    // the position of 1.
      if (pos.Contains(1))
        next = (int)pos[1];
      else
        next = 0; 
 
    for(i = 1; i <= n; i++)
    {
         
        // Mark the current
        // position.
        vis.Add(next, 1);
 
        // If the element is not
        // present at that position
        // then it is impossible
        // to build
        if (i != a[next])
        {
            return false;
        }
 
        // Updating the next
        if (next + 1 == n ||
           (vis.Contains(next + 1) &&
                (int)vis[next + 1] != 0))
        {
             
            // If the next position is
            // out of array size or next
            // position is not empty then
            // we use the map to find the
            // position of the next element
              if (pos.Contains(i + 1))
                next = (int)pos[i + 1];
              else
                next = 0; 
        }
        else
         
            // Else just increment it
            next++;
    }
    return true;
}
 
// Driver code
static public void Main()
{
    int[] arr = { 2, 3, 4, 5, 1 };
    int N = arr.Length;
 
    if (CheckArray(arr, N) == true)
    {
        Console.WriteLine("YES");
    }
    else
    {
        Console.WriteLine("NO");
    }
}
}
 
// This code is contributed by akhilsaini


Javascript


输出:
YES

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live