Skip to content

Commit 6feeee8

Browse files
committed
alternating positive & negative
1 parent 5318f5f commit 6feeee8

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

DSA Crack Sheet/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
- [Best Time to Buy and Sell Stock](https://leetcode.com/problems/best-time-to-buy-and-sell-stock/ "view question") - [Cpp Solution](./solutions/Best%20Time%20to%20Buy%20and%20Sell%20Stock.cpp)
2121
- [Count pairs with given sum](https://practice.geeksforgeeks.org/problems/count-pairs-with-given-sum5022/1# "view question") - [Cpp Solution](./solutions/Count%20pairs%20with%20given%20sum.cpp)
2222
- [Common elements](https://practice.geeksforgeeks.org/problems/common-elements1132/1# "view question") - [Cpp Solution](./solutions/Common%20elements.cpp)
23+
- [Rearrange array in alternating positive & negative items with O(1) extra space](https://www.geeksforgeeks.org/rearrange-array-alternating-positive-negative-items-o1-extra-space/ "view topic") - [Cpp Solution](./solutions/Rearrange%20array%20in%20alternating%20positive%20&%20negative%20items%20with%20O(1)%20extra%20space.cpp)
2324
- []( "view question") - [Cpp Solution](./solutions/.cpp)
2425

2526
<!-- - []( "view question") - [Cpp Solution](./solutions/) -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
Rearrange array in alternating positive & negative items with O(1) extra space
3+
==============================================================================
4+
5+
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 maintaining the order of appearance.
6+
Number of positive and negative numbers need not be equal. If there are more positive numbers they appear at the end of the array. If there are more negative numbers, they too appear in the end of the array.
7+
8+
Examples :
9+
10+
Input: arr[] = {1, 2, 3, -4, -1, 4}
11+
Output: arr[] = {-4, 1, -1, 2, 3, 4}
12+
13+
Input: arr[] = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8}
14+
output: arr[] = {-5, 5, -2, 2, -8, 4, 7, 1, 8, 0}
15+
*/
16+
17+
#include <bits/stdc++.h>
18+
using namespace std;
19+
20+
int main()
21+
{
22+
vector<int> arr = {-5, -2, 5, 2, 4, 7, 1, 8, 0, -8, -1, -2, -3, -4, -5, -6, -4};
23+
int i = 0, n = arr.size();
24+
25+
while (i < n)
26+
{
27+
int next_sign = 0;
28+
while (i < n && ((i % 2 == 0 && arr[i] < 0) || (i % 2 && arr[i] >= 0)))
29+
i++;
30+
if (i >= n)
31+
break;
32+
33+
int j = i + 1;
34+
35+
if (arr[i] < 0)
36+
{
37+
// find next +ve
38+
while (j < n && arr[j] < 0)
39+
j++;
40+
if (j >= n)
41+
break;
42+
}
43+
else
44+
{
45+
// find next -ve
46+
while (j < n && arr[j] >= 0)
47+
j++;
48+
if (j >= n)
49+
break;
50+
}
51+
52+
// rotate right one i to j
53+
int temp = arr[j];
54+
for (int k = j; k >= i + 1; --k)
55+
arr[k] = arr[k - 1];
56+
arr[i] = temp;
57+
}
58+
59+
for (auto &i : arr)
60+
cout << i << " ";
61+
return 0;
62+
}

0 commit comments

Comments
 (0)