-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPartition.java
More file actions
63 lines (46 loc) · 1012 Bytes
/
Partition.java
File metadata and controls
63 lines (46 loc) · 1012 Bytes
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
60
61
62
63
package codility;
/*
* TT question. Scored 20% IIRC
*/
public class Partition {
public int solution(int X, int[] A) {
if( A.length == 0)
return 0;
int sameBeforeCount =0;
int differentAfterCount =0;
int k=1;
for(int i=0; i<A.length/2; i++,k++) {
if(A[i] == X)
++sameBeforeCount;
if(A[A.length-i-1] != X)
++differentAfterCount;
if( sameBeforeCount == differentAfterCount) {
if( A.length %2 ==0) {
return k;
}
else {
int middleElement = A[A.length/2+1];
if(middleElement == X)
k-=1;
else
k+=1;
}
return k;
}
}
if( sameBeforeCount != 0 )
return 0;
else
return A.length;
}
public static void main(String args[]) {
Partition instance = new Partition();
/*int[] A = new int[] {2,2,2,2,2};
int[] A = new int[] {5,5,5,5,5};
int[] A = new int[] {5,5,1,2,3,5};
int[] A = new int[] {5,5,1,7,2,3,5};
*/
int[] A = new int[] {};
System.err.println(instance.solution(5, A));
}
}