### thelili018's blog

By thelili018, history, 4 years ago, ,

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);
}


}

• -6

 » 4 years ago, # | ← Rev. 3 →   +8 deleted
•  » » 4 years ago, # ^ |   0 Is that why it fails on test 5? It's a time-complexity issue?
•  » » » 4 years ago, # ^ | ← Rev. 2 →   +5 For test 5 answer is 1 but your code is giving output as 2. Test case 5 is22 1You can put both groups in one taxi so answer is one.
•  » » » » 4 years ago, # ^ |   0 Thank you so much! That really helped!
•  » » 4 years ago, # ^ |   0 You cannot solve it in O(1) just because you have to input N numbers...
 » 2 months ago, # |   0 import java.util.Scanner; import java.util.Arrays; public class Solution{ //what is wrong in this code ,this code is not working on testcase 37 where 3 values are given 3 3 2 // this code will produce 2 but the actual answer should be 3.(but how it can produce 3 beacuse there //are total 8 children so we can divide it into 4 and 4 ,so we need 2 taxies only and also we have to //return minimum no of taxies not maximum.) //please anyone can explain this `public static void main(String[] args){ Scanner s = new Scanner(System.in); int n = s.nextInt(); int a[] = new int[n]; int sum = 0; for(int i = 0;i