-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMoveNegativeToStart.cpp
59 lines (45 loc) · 1.18 KB
/
MoveNegativeToStart.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//////////////////////////////////////CODE STUDIO////////////////////////////////////
// EASY
/*
An array of N elements, rearrange the elemments such that
all negatives are at the beiginning of the array
and all posiitvive are at the end
*/
/*
Brute: Store the negative elements in temp array
and then array the elements in the given array accordingly
by overwriting
Disadvantage: SC : O(n)
Optimal: Take two pointers i and j=-1
iterate in array if arr[i]>=0, set j=i
again iterate in the array
and if(j!=-1 and arr[i]<0)
then swap arr[i] with arr[j]
in this way with the help of j
we will locate all +ve and zero numbers to the end of array
and others to the beginning
without using auxiliary space
*/
/*
SOL:
#include <bits/stdc++.h>
vector<int> separateNegativeAndPositive(vector<int> &nums){
// Write your code here.
int n=nums.size();
int i;
int j=-1;
for(i=0;i<n;i++){
if(nums[i]>=0){
j=i;
break;
}
}
for(i=j+1;i<n;i++){
if(j!=-1 && nums[i]<0){
swap(nums[i],nums[j]);
j++;
}
}
return nums;
}
*/