You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In this problem we have: (A + (B - 1)) / B = C and we want to find A so this becomes: A = C * B - (B - 1)
voidsolve(Scannerin) {
longb = in.nextLong(), c = in.nextLong();
longa = c * b - (b - 1);
System.out.println(a);
}
Problem D: Symmetric Order
Just implement what's required in the statement.
voidsolve(Scannerin) {
intn = in.nextInt();
List<String> left = newArraylist<>(), right = newArrayList<>();
for(inti = 0; i < n; ++i) {
Stringcur = in.next();
if(i % 2 == 0) left.add(cur);
elseright.add(cur);
}
Collections.reverse(right);
left.addAll(right);
// print left
}
Problem E: Speed limit
Again, Just implement what's required in the statement.
voidsolve(Scannerin) {
intn = in.nextInt();
longans = 0;
intprev = 0;
for (i = 0; i < n; ++i) {
ints = in.nextInt(), t = in.nextInt();
ans += s * (t - p);
p = t;
}
System.out.println(ans);
}
Problem F: Odd man out
This problem asks to print the number that occured an odd number of times, you can simply keep a counter in a Map
or an arr and see which one occurs an odd number of times, and the statment guarentees that there is just one.
This is also a standard problem, so if we assume that there are no repeated characters, the answer is going to be N!.
But since allowed characters are allowed, we need to account for them, so the answer becomes N! / MUL{0, 26}(F[i]!)
where F[i] is the frequency of character i
One other note here: the answer can get pretty huge so you must use big integers, a good idea is to use Java's bulting BigInteger or some language
that supports it out of the box like python or ruby. in C/C++ you have to write it yourself link for inspiration
voidsolve(Scannerin) {
Strings = in.next();
int[] f = newint[26];
for(inti = 0; i < s.length(); ++i) {
f[s.charAt(i) - 'A']++;
}
BigIntegerdown = BigInteger.valueOf(1);
for (inti = 0; i < 26; i++) {
BigIntegerfactored = factorial(f[i]);
down = down.multiply(factored);
}
BigIntegerresult = factorial(s.length()).divide(down);
System.out.println(result);
}
Problem I: Birds on a wire
In this problem you need to add as much birds on that wire while respecting the rules mentionned in the statement.
voidsolve(Scannerin) {
longl = sc.nextLong();
longd = sc.nextLong();
longn = sc.nextLong();
ArrayList<Long> sol = newArrayList<>();
for (inti = 0; i < (int) n; i++) {
sol.add(sc.nextLong());
}
sol.add(6 - d); // account for the postssol.add(l + d - 6);
Collections.sort(sol);
intans = 0;
for (inti = 0; i < n + 1; i++) {
ans += (sol.get(i + 1) - sol.get(i) - d) / d; // how many birds can you put is the distance between two already - the min distance over d
}
System.out.println(ans);