-
Notifications
You must be signed in to change notification settings - Fork 0
/
arr03.java
55 lines (47 loc) · 1.14 KB
/
arr03.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
54
55
//question
//given an unsorted arr[] of size n , having both
//negative and positive element
//task:- to segregate element like
//input:8
//arr[] = {1,-1,3,2,-7,-5,11,6}
//o/p=> {1,3,2,11,6,-1,-7,-5}
//Note=> Dont change the order of positive and negative element
package Array;
public class arr03 {
public static void segregatePosNeg(int arr[] , int n) {
//here n is size of array
if(n==0||n==1) {
return;
}
int finalArr[] = new int[n];
int placeHolder = 0;
for(int i=0; i< n; i++) {
if(arr[i] >= 0) {
finalArr[placeHolder] = arr[i];
placeHolder++;
}
}
for(int i=0; i<n; i++) {
if(arr[i] < 0) {
finalArr[placeHolder] = arr[i];
placeHolder++;
}
}
for(int k=0; k<n; k++) {
arr[k] = finalArr[k];
}
}
//function for print Arr
public static void printArr(int arr[]) {
System.out.println("your required array =>");
for(int i=0; i< arr.length; i++) {
System.out.print(arr[i] +" ");
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
int checkArr[] = { 1, -1, 3, 2, -7 , -5 ,11 ,6};
segregatePosNeg(checkArr, 8);
printArr(checkArr);
}
}