📌  相关文章
📜  一种排列,其中每个元素表示它之前或之后的元素数量

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

一种排列,其中每个元素表示它之前或之后的元素数量

给定一个包含n 个元素的数组。任务是检查给定数组的排列是否存在,以便每个元素指示其之前或之后存在的元素的数量。如果存在则打印“Yes”,否则打印“No”。
例子 :

Input : arr[] = {1, 3, 3, 2}
Output : Yes
{3, 1, 2, 3} is a permutation in which each element 
indicate number of element present before or after it.
There is one more permutation {3, 2, 1, 3}

Input : arr[] = {4, 1, 2, 3, 0}
Output : Yes
There are two permutations {0, 1, 2, 3, 4} or
{4, 3, 2, 1, 0}

这个想法是使用散列。请注意,对于数组中的每个索引 i,arr[i] 可以具有值 i 或 n – i。我们遍历给定的数组并找到数组中每个元素的频率。现在,对于每个索引 i,检查值 i 和 ni 的可用性并相应地减少频率。请注意,值为 i 的项目可以转到索引 i 或 ni-1。类似地,值为 ni-1 的项目可以转到索引 i 或 ni-1。
下面是这种方法的实现:

C++
// C++ program to check if array permutation
// exists such that each element indicates
// either number of elements before or after
// it.
#include 
using namespace std;
 
// Check if array permutation exist such that
// each element indicate either number of
// elements before or after it.
bool check(int arr[], int n)
{
    map freq;
 
    // Finding the frequency of each number.
    for (int i = 0; i < n; i++)
        freq[arr[i]]++;
 
    for (int i = 0; i < n; i++)
    {
        // Try to find number of element before
        // the current index.
        if (freq[i])
            freq[i]--;
 
        // Try to find number of element after
        // the current index.
        else if (freq[n-i-1])
            freq[n-i-1]--;
 
        // If no such number find, return false.
        else
            return false;
    }
 
    return true;
}
 
// Driven Program
int main()
{
    int arr[] = {1, 3, 3, 2};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    check(arr, n)? (cout << "Yes" << endl) :
                   (cout << "No" << endl);
 
    return 0;
}


Java
// Java program to check if array permutation
// exists such that each element indicates
// either number of elements before or after
// it.
import java.io.*;
class GFG
{
    // Check if array permutation exist such that
    // each element indicate either number of
    // elements before or after it.
    public static boolean check(int arr[])
    {
        int n = arr.length;
        int[] freq = new int[n];
  
        // Finding the frequency of each number.
        for (int i = 0; i < n; i++)
            freq[arr[i]]++;
  
        for (int i = 0; i < n; i++)
        {
            // Try to find number of element before
            // the current index.
            if (freq[i]!= 0)
                 freq[i]--;
  
            // Try to find number of element after
            // the current index.
            else if (freq[n-i-1]!= 0)
                    freq[n-i-1]--;
  
            // If no such number find, return false.
            else
                return false;
        }
  
    return true;
    }
     
    //Driver program
    public static void main (String[] args)
    {
         
        int arr[] = {1, 3, 3, 2};
        boolean bool = check(arr);
        if(bool)
            System.out.println("Yes");
        else
            System.out.println("No");
    }
}


Python3
# Python program to check if array permutation
# exists such that each element indicates
# either number of elements before or after
# it.
 
# Check if array permutation exist such that
# each element indicate either number of
# elements before or after it.
def check(arr):
 
    n = len(arr);
    freq = [0] * n;
 
    # Finding the frequency of each number.
    for i in range(n):
        freq[arr[i]] += 1;
 
    for i in range(n):
         
        # Try to find number of element before
        # the current index.
        if (freq[i] != 0):
            freq[i] -= 1;
 
        # Try to find number of element after
        # the current index.
        else if (freq[n - i - 1] != 0):
                freq[n - i - 1] -= 1;
 
        # If no such number find, return false.
        else:
            return False;
 
    return True;
     
# Driver program
if __name__ == '__main__':
 
    arr = [1, 3, 3, 2];
    bool = check(arr);
    if(bool):
        print("Yes");
    else:
        print("No");
 
# This code is contributed by 29AjayKumar


C#
// C# program to check if array permutation
// exists such that each element indicates
// either number of elements before or after it.
using System;
 
class GFG
{
    // Check if array permutation exist such that
    // each element indicate either number of
    // elements before or after it.
    public static bool check(int []arr)
    {
        int n = arr.Length;
        int []freq = new int[n];
 
        // Finding the frequency of each number.
        for (int i = 0; i < n; i++)
            freq[arr[i]]++;
 
        for (int i = 0; i < n; i++)
        {
            // Try to find number of element
            // before the current index.
            if (freq[i]!= 0)
                freq[i]--;
 
            // Try to find number of element
            // after the current index.
            else if (freq[n-i-1] != 0)
                    freq[n-i-1]--;
 
            // If no such number find, return false.
            else
                return false;
        }
 
    return true;
    }
     
    //Driver program
    public static void Main ()
    {
        int []arr = {1, 3, 3, 2};
        bool boo = check(arr);
        if(boo)
            Console.Write("Yes");
        else
            Console.Write("No");
    }
}
 
// This code is contributed by nitin mittal.


Javascript


输出:

Yes