import java.util.Scanner;
public class Taxi2 {
public static void main (String[] args) {
int n, x, count;
int[] a;
Scanner sc = new Scanner(System.in);
n = sc.nextInt();
a = new int[4];
for(int i = 0; i < n; i++) {
x = sc.nextInt();
switch(x) {
case 1: a[0]++; break;
case 2: a[1]++; break;
case 3: a[2]++; break;
case 4: a[3]++; break;
}
}
count = a[3];
if(a[2] != 0 && a[0] != 0) {
x = (a[2] >= a[0]) ? a[0] : a[2];
for(int i = 0; i < x; i++) {
a[2]--;
a[0]--;
count++;
}
}
count += a[2];
count += a[1] / 2;
a[1] = a[1] % 2;
if(a[1] != 0 && a[0] >= 2) {
a[1]--;
a[0] = a[0] - 2;
count++;
}
else
count += a[1];
count += a[0] / 4;
if(a[0] % 4 != 0)
count++;
System.out.println(count);
}
}
deleted
Is that why it fails on test 5? It's a time-complexity issue?
For test 5 answer is 1 but your code is giving output as 2.
Test case 5 is
2
2 1
You can put both groups in one taxi so answer is one.
You cannot solve it in O(1) just because you have to input N numbers...