检查数组是否可以排列在左或右定位数组中
给定一个大小为 n>4 的数组 arr[],任务是检查给定的数组是否可以排列为左定位数组或右定位数组?
左或右定位数组意味着数组中的每个元素都等于其左侧的元素数或右侧的元素数。
例子 :
Input : arr[] = {1, 3, 3, 2}
Output : "YES"
This array has one such arrangement {3, 1, 2, 3}.
In this arrangement, first element '3' indicates
that three numbers are after it, the 2nd element
'1' indicates that one number is before it, the
3rd element '2' indicates that two elements are
before it.
Input : arr[] = {1, 6, 5, 4, 3, 2, 1}
Output: "NO"
// No such arrangement is possible
Input : arr[] = {2, 0, 1, 3}
Output: "YES"
// Possible arrangement is {0, 1, 2, 3}
Input : arr[] = {2, 1, 5, 2, 1, 5}
Output: "YES"
// Possible arrangement is {5, 1, 2, 2, 1, 5}
一个简单的解决方案是生成所有可能的排列(请参阅本文)并检查左或右定位数组条件,如果数组中的每个元素都满足条件,则“是”否则“否”。这种方法的时间复杂度是 O(n*n! + n), n*n!生成所有安排和 n 以使用临时数组检查条件。
这个问题的有效解决方案需要一点点观察和纸笔工作。为了满足左或右定位数组条件,数组中的所有数字都应该等于索引 i 或 (n-1-i) 并且 arr[i] < n。所以我们创建了一个大小为 n 的visited[] 数组并将其元素初始化为0。然后我们遍历数组并按照给定的步骤操作:
- 如果visited[arr[i]] = 0,则将其设为1,这将检查数组arr[0]…arr[i-1]左侧的元素数等于arr[i]的条件。
- Else makevisited[n-arr[i]-1] = 1,它检查数组 arr[i+1]…arr[n-1] 右侧的元素数等于 arr[i ]。
- 现在遍历visited[] 数组,如果visited[] 数组的所有元素都变为1,则表示可以排列“YES”,否则“NO”。
C++
// C++ program to check if an array can be arranged
// to left or right positioned array.
#include
using namespace std;
// Function to check Left or Right Positioned
// Array.
// arr[] is array of n elements
// visited[] is boolean array of size n
bool leftRight(int arr[],int n)
{
// Initially no element is placed at any position
int visited[n] = {0};
// Traverse each element of array
for (int i=0; i
Java
// Java program to check if an array
// can be arranged to left or
// right positioned array.
class GFG {
// Function to check Left or
// Right Positioned Array.
// arr[] is array of n elements
// visited[] is boolean array of size n
static boolean leftRight(int arr[], int n) {
// Initially no element is
// placed at any position
int visited[] = new int[n];
// Traverse each element of array
for (int i = 0; i < n; i++) {
// Element must be smaller than n.
if (arr[i] < n) {
// Place "arr[i]" at position "i"
// or at position n-arr[i]-1
if (visited[arr[i]] == 0)
visited[arr[i]] = 1;
else
visited[n - arr[i] - 1] = 1;
}
}
// All positions must be occupied
for (int i = 0; i < n; i++)
if (visited[i] == 0)
return false;
return true;
}
// Driver code
public static void main(String[] args)
{
int arr[] = {2, 1, 5, 2, 1, 5};
int n = arr.length;
if (leftRight(arr, n) == true)
System.out.print("YES");
else
System.out.print("NO");
}
}
// This code is contributed by Anant Agarwal.
Python3
# Python3 program to check
# if an array can be arranged
# to left or right positioned array.
# Function to check Left
# or Right Positioned
# Array.
# arr[] is array of n elements
# visited[] is boolean array of size n
def leftRight(arr,n):
# Initially no element
# is placed at any position
visited=[]
for i in range(n+1):
visited.append(0)
# Traverse each element of array
for i in range(n):
# Element must be smaller than n.
if (arr[i] < n):
# Place "arr[i]" at position "i"
# or at position n-arr[i]-1
if (visited[arr[i]] == 0):
visited[arr[i]] = 1
else:
visited[n-arr[i]-1] = 1
# All positions must be occupied
for i in range(n):
if (visited[i] == 0):
return False
return True
# Driver code
arr = [2, 1, 5, 2, 1, 5]
n = len(arr)
if (leftRight(arr, n) == True):
print("YES")
else:
print("NO")
# This code is contributed
# by Anant Agarwal.
C#
// C# program to check if an array
// can be arranged to left or
// right positioned array.
using System;
public class GFG {
// Function to check Left or
// Right Positioned Array.
// arr[] is array of n elements
// visited[] is boolean array of size n
static bool leftRight(int []arr, int n) {
// Initially no element is
// placed at any position
int []visited = new int[n];
// Traverse each element of array
for (int i = 0; i < n; i++) {
// Element must be smaller than n.
if (arr[i] < n) {
// Place "arr[i]" at position "i"
// or at position n-arr[i]-1
if (visited[arr[i]] == 0)
visited[arr[i]] = 1;
else
visited[n - arr[i] - 1] = 1;
}
}
// All positions must be occupied
for (int i = 0; i < n; i++)
if (visited[i] == 0)
return false;
return true;
}
// Driver code
public static void Main()
{
int []arr = {2, 1, 5, 2, 1, 5};
int n = arr.Length;
if (leftRight(arr, n) == true)
Console.WriteLine("YES");
else
Console.WriteLine("NO");
}
}
// This code is contributed by PrinciRaj1992
PHP
Javascript
输出 :
"YES"