## Interview Question

Java Developers**Team:**4

**Country:**United States

**Interview Type:**Written Test

public static void pairPencils(int[] arr) {

Arrays.parallelSort(arr);

int i = 0;

int j = arr.length / 2;

int count = 0;

while (i < arr.length && j < arr.length) {

if (arr[i] == Integer.MAX_VALUE) {

i++;

continue;

}

if (arr[j] == Integer.MAX_VALUE) {

j++;

continue;

}

if (2 * arr[i] <= arr[j]) {

arr[i] = Integer.MAX_VALUE;

arr[j] = Integer.MAX_VALUE;

count++;

i++;

}

j++;

}

System.out.println(count + " " + (arr.length - 2 * count));

}

public static void pairPencils(int []arr) {

Arrays.parallelSort(arr);

int i = 0;

int j = arr.length / 2;

int count = 0;

while (i < arr.length && j < arr.length) {

if (arr[i] == Integer.MAX_VALUE) {

i++;

continue;

}

if (arr[j] == Integer.MAX_VALUE) {

j++;

continue;

}

if (2 * arr[i] <= arr[j]) {

arr[i] = Integer.MAX_VALUE;

arr[j] = Integer.MAX_VALUE;

count++;

i++;

}

j++;

}

System.out.println(count + " " + (arr.length - 2 * count));

}

```
public static void pairPencils(int []arr) {
Arrays.parallelSort(arr);
int i = 0;
int j = arr.length / 2;
int count = 0;
while (i < arr.length && j < arr.length) {
if (arr[i] == Integer.MAX_VALUE) {
i++;
continue;
}
if (arr[j] == Integer.MAX_VALUE) {
j++;
continue;
}
if (2 * arr[i] <= arr[j]) {
arr[i] = Integer.MAX_VALUE;
arr[j] = Integer.MAX_VALUE;
count++;
i++;
}
j++;
}
System.out.println(count + " " + (arr.length - 2 * count));
}
```

Step 1) Sort the array A of sizes

- s100banerjee June 30, 2019Step 2) Take 2 variables p and q, p =0 and q = 1

Loop:

for A[p] find the smallest A[q] by iteration, Then each of A[p] and A[q], A[q+1],..... ......... make one pair each.

temp = p+1

Loop:

if(A[p] == A[temp])

temp++;

else{

A[p] = A[temp];

break;

}

End Loop:

Loop:

Find next A[q] for the new p.by iteration.

if(q==A.length) return;

End Loop:

End Loop