技术控

    今日:114| 主题:49288
收藏本版 (1)
最新软件应用技术尽在掌握

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

[复制链接]
讨厌那个你ヾ 发表于 2016-10-17 20:31:28
76 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.  
  Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
友荐云推荐




上一篇:Studying the Internet Censorship in South Korea
下一篇:Hadoop: Pros And Cons For Enterprise Users
酷辣虫提示酷辣虫禁止发表任何与中华人民共和国法律有抵触的内容!所有内容由用户发布,并不代表酷辣虫的观点,酷辣虫无法对用户发布内容真实性提供任何的保证,请自行验证并承担风险与后果。如您有版权、违规等问题,请通过"联系我们"或"违规举报"告知我们处理。

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

使用道具 举报

邓妮 发表于 2016-10-19 04:09:28
元芳你怎么看?
回复 支持 反对

使用道具 举报

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

使用道具 举报

*滑动验证:
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

我要投稿

推荐阅读

扫码访问 @iTTTTT瑞翔 的微博
回页顶回复上一篇下一篇回列表手机版
手机版/CoLaBug.com ( 粤ICP备05003221号 | 文网文[2010]257号 )|网站地图 酷辣虫

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

返回顶部 返回列表