-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp104.java
53 lines (39 loc) · 1.16 KB
/
p104.java
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
/*Given an unsorted array arr[] of size N having both negative and positive integers. The task is place all negative element at the end of array without changing the order of positive element and negative element.
Example 1:
Input :
N = 8
arr[] = {1, -1, 3, 2, -7, -5, 11, 6 }
Output :
1 3 2 11 6 -1 -7 -5
Example 2:
Input :
N=8
arr[] = {-5, 7, -3, -4, 9, 10, -1, 11}
Output :
7 9 10 11 -5 -3 -4 -1
Your Task:
You don't need to read input or print anything. Your task is to complete the function segregateElements() which takes the array arr[] and its size N as inputs and store the answer in the array arr[] itself.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(N)
Constraints:
1 ≤ N ≤ 105
-105 ≤ arr[] ≤ 105*/
class Solution {
public void segregateElements(int arr[], int n){
int temp[]=new int[n];
int i=0;
for(int j=0;j<n;j++){
if(arr[j]>=0){
temp[i++]=arr[j];
}
}
for(int j=0;j<n;j++){
if(arr[j]<0){
temp[i++]=arr[j];
}
}
for(int k=0;k<n;k++){
arr[k]=temp[k];
}
}
}