神刀安全网

Rearrange array in alternating positive & negative items with O(1) extra space | Set 2

Given an array of positive and negative numbers, arrange them in an alternate fashion such that every positive number is followed by negative and vice-versa. Order of elements in output doesn’t matter. Extra positive or negative elements should be moved to end.

Examples

Input : arr[] = {-2, 3, 4, -1} Output : arr[] = {-2, 3, -1, 4} OR {-1, 3, -2, 4} OR ..  Input : arr[] = {-2, 3, 1} Output : arr[] = {-2, 3, 1} OR {-2, 1, 3}   Input :  arr[] = {-5, 3, 4, 5, -6, -2, 8, 9, -1, -4} Output : arr[] = {-5, 3, -2, 5, -6, 4, -4, 9, -1, 8}          OR ..

We strongly recommend you to minimize your browser and try this yourself first.

We have already discussed a O(n 2 ) solution that maintains the order of appearance in the arrayhere. If we are allowed to change order of appearance, we can solve this problem in O(n) time and O(1) space.

The idea is to process the array and shift all negative values to the end in O(n) time. After all negative values are shifted to the end, we can easily rearrange array in alternating positive & negative items. We basically swap next positive element at even position from next negative element in this step.

Following is C++ implementation of above idea.

// C++ program to rearrange array in alternating // positive & negative items with O(1) extra space #include <bits/stdc++.h> using namespace std;  // Function to rearrange positive and negative // integers in alternate fashion. The below // solution doesn't maintain original order of // elements void rearrange(int arr[], int n) {     int i = -1, j = n;      // shift all negative values to the end     while (i < j)     {         while (arr[++i] > 0);         while (arr[--j] < 0);         if (i < j)             swap(arr[i], arr[j]);     }      // i has index of leftmost negative element     if (i == 0 || i == n)         return;      // start with first positive element at index 0      // Rearrange array in alternating positive &     // negative items     int k = 0;     while (k < n && i < n)     {      // swap next positive element at even position      // from next negative element.         swap(arr[k], arr[i]);         i = i + 1;         k = k + 2;     } }  // Utility function to print an array void printArray(int arr[], int n) {     for (int i = 0; i < n; i++)       cout << arr[i] << " ";     cout << endl; }  // Driver code int main() {     int arr[] = {2, 3, -4, -1, 6, -9};      int n = sizeof(arr)/sizeof(arr[0]);      cout << "Given array is /n";     printArray(arr, n);      rearrange(arr, n);      cout << "Rearranged array is /n";     printArray(arr, n);      return 0; }

Output:

Given array is  2 3 -4 -1 6 -9  Rearranged array is  -1 3 -4 2 -9 6

This article is contributed by Aditya Goel . Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above

转载本站任何文章请注明:转载至神刀安全网,谢谢神刀安全网 » Rearrange array in alternating positive & negative items with O(1) extra space | Set 2

分享到:更多 ()

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址