### 技术控

今日:280| 主题:53479

# [其他] Check If Given Array Can be Arranged In Left or Right Positioned Array

113 3

### 立即注册CoLaBug.com会员，免费获得投稿人的专业资料，享用更多功能，玩转个人品牌！

x
Given an array arr[] of size n>4, the task is to check whether the given array can be      arrangedin the form of Left or Right positioned array?
Left or Right Positioned Arraymeans each element in the array is equal to the number of elements to its left or number of elements to its right.
Examples:
1. Input  : arr[] = {1, 3, 3, 2}
2. Output : "YES"
3. This array has one such arrangement {3, 1, 2, 3}.
4. In this arrangement, first element '3' indicates
5. that three numbers are after it, the 2nd element
6. '1' indicates that one number is before it, the
7. 3rd element '2' indicates that two elements are
8. before it.

9. Input : arr[] = {1, 6, 5, 4, 3, 2, 1}
10. Output: "NO"
11. // No such arrangement is possible

12. Input : arr[] = {2, 0, 1, 3}
13. Output: "YES"
14. // Possible arrangement is {0, 1, 2, 3}

15. Input : arr[] = {2, 1, 5, 2, 1, 5}
16. Output: "YES"
17. // Possible arrangement is {5, 1, 2, 2, 1, 5}

A    simple solutionis to generate all possible arrangements (seethis article) and check for the Left or Right Positioned Array condition, if each element in the array satisfies the condition then “YES” else “NO”. Time complexity for this approach is O(n*n! + n), n*n! to generate all arrangements and n for checking the condition using temporary array.
An    efficient solutionfor this problem needs little bit observation and pen-paper work. To satisfy the Left or Right Positioned Array condition all the numbers in the array should either be equal to index, i or (n-1-i) and arr < n. So we create an visited[] array of size n and initialize its element with 0. Then we traverse array and follow given steps :

• If visited[arr] = 0 then make it 1, which checks for the condition that number of elements on the left side of array arr[0]…arr[i-1] is equal to arr.
• Else make visited[n-arr-1] = 1, which checks for the condition that number of elements on the right side of array arr[i+1]…arr[n-1] is equal to arr.
• Now traverse visited[] array and if all the elements of visited[] array become 1 that means arrangement is possible “YES” else “NO”.
1. // C++ program to check if an array can be arranged
2. // to left or right positioned array.
3. #include<bits/stdc++.h>
4. using namespace std;

5. // Function to check Left or Right Positioned
6. // Array.
7. // arr[] is array of n elements
8. // visited[] is boolean array of size n
9. bool leftRight(int arr[],int n)
10. {
11.     // Initially no element is placed at any position
12.     int visited[n] = {0};

13.     // Traverse each element of array
14.     for (int i=0; i<n; i++)
15.     {
16.         // Element must be smaller than n.
17.         if (arr[i] < n)
18.         {
19.             // Place "arr[i]" at position "i"
20.             // or at position n-arr[i]-1
21.             if (visited[arr[i]] == 0)
22.                 visited[arr[i]] = 1;
23.             else
24.                 visited[n-arr[i]-1] = 1;
25.         }
26.     }

27.     // All positions must be occupied
28.     for (int i=0; i<n; i++)
29.         if (visited[i] == 0)
30.             return false;

31.     return true;
32. }

33. // Driver program to test the case
34. int main()
35. {
36.     int arr[] = {2, 1, 5, 2, 1, 5};
37.     int n = sizeof(arr)/sizeof(arr[0]);
38.     if (leftRight(arr, n) == true)
39.         cout << "YES";
40.     else
41.         cout << "NO";
42.     return 0;
43. }

Output:
1. "YES"

Time Complexity : O(n)
Auxiliary Space : O(n)
This article is contributed by          Shashank Mishra ( Gullu )    . If you like GeeksforGeeks and would like to contribute, you can also write an article using    contribute.geeksforgeeks.orgor mail your article to [email protected] See your article appearing on the GeeksforGeeks main page and help other Geeks.

xdagl 发表于 2016-10-18 10:01:28
 我小学十年,中学十二年,我被评为全校最熟悉的面孔,新老师来了都跟我打听学校内幕……

 元芳你怎么看？

meitime 发表于 2016-10-25 10:53:42
 有谁会在时过境迁之后还在那里等你。

• ## 我还在原地等你，你却假装忘记曾来过这里…

【美图心语】青春如酒，成长正酣，所有美好的， [...]

• ## Amazon to acquire Souq, a Middle East cl

Amazon continues its march across the glob [...]

• ## How the UWP Community Toolkit helps Wind

In August 2016, we introduced the open-source [...]

• ## 众筹新规5月份或将出台：终结野蛮生长

日前有消息称，5月份或将出台股权众筹的指导意 [...]

• ## 比便利店更近，比菜市场更精，这家生鲜电商

轻供应链、物流众包、前店后仓、O2O⋯⋯过去两 [...]

• ## ZSL: zk-SNARKs for the Enterprise

The Zerocoin Electric Coin Company’s primary [...]

• ## [视频]当iPhone 7遇上世界最亮手电筒：屏幕

Wicked Lasers推出的Flashtorch强光手电，拥有41 [...]

• ## [视频]BBC受访教授模仿秀：老妈如何应对突

近日韩国釜山国立大学的罗伯特·凯利教授(Robert [...]

• ## 途虎与博世深度合作 将建免费智能检测系统

【亿邦动力网讯】3月24日消息，昨日，汽车养护平 [...]

• ## 首发 | 融资700万 她靠男管家征服经纬投资

。 建立标准服务体系 在小马管家办公 [...]

© 2001-2017 Comsenz Inc. Design: Dean. DiscuzFans.