# Tag Info

2

We can simplify this quite a bit. At a high level, note that all you are doing is: Converting each digit of the input to an array of possible neighbors. Now you have an array of neighbor arrays. The answer is simply the cartesian cross product of those neighbor arrays. Sort them. Turn the into back into strings. The cartesian cross-product is a simple ...

5

General remarks This using namespace std; is considered bad practice, see for example Why is “using namespace std” considered bad practice? on Stack Overflow. Consistent indenting and spacing increases the legibility of the code. Use curly braces for if/else blocks even if they consist only of a single statement. Enable all compiler warnings and fix ...

0

the given answers would fail on some cases i.e [1,2,5,3,7,8,6,4] The solution has 2 steps: 1- is it Too chaotic (first loop) 2- move the element to its right position(second loop). function minimumBribes(q) { let min = 0; for(let i = 0; i < q.length; i++){ if(q[i] - (i + 1) > 2 ){ return console.log('Too chaotic'); } ...

7

The algorithm provided by the OP presumes that the array is important and preserves the original array. In addition, the displayed algorithm seeks an "instantaneous answer", as if the routine may be interrogated at any point to get that answer at the point in time. All that is important here is the output - a single number. Any other information used to ...

5

Better complexity can be achieved using a heap. The input array can be organized into a min-heap in $O(n)$ time with negative integers dropped. Then the smallest number can be popped one by one until the target number is found. The complexity of this algorithm is $O(n + klogn)$ where $k$ is the insertion index of the target number among the sorted ...

8

I then tried putting the array into an arraylist, which reduces big-O since each object is "touched" only once, and I can use .Contains which is more efficient than iteration (not sure if that's true; I just sort of remember reading it somewhere). As was mentioned in the comments, for your purpose, there is no significant difference in performance between ...

2

Variable names The few keystrokes you save by not having to type digits or permutations in full is not worth it. Just name em digits and permutations. For the import, just import itertools instead of from itertools import permutations as perm to_int To take a sum, reduce is seldom needed. You can explain this logic simpler like : def to_int(digits): ...

4

From the mathematical point , one number is divisible by 3 if sum of its digits is divisible by 3 and it is valid the commutative property, so 3 + 6 + 2 is equal to 6 + 3 + 2 because you can commute elements in every position and still obtain the same result. So if sum of n digits is divisible by 3, the n! permutations of the number including itself are ...

5

The algorithm can be improved, so that we do less work: Use the fact that multiples of 3 have a sum of digits that's also a multiple of 3. That means we can select all the distinct combinations of digits, and discard any that don't sum to a multiple of 3. Only for those sets not discarded, we can compute the number of permutation of the selected digits. (...

3

Welcome to CodeReview! Whitespace The PEP8 standard, and consequently most Python linting tools, will recommend that you add another linebreak before your function definitions, plus some whitespace around your operators, etc. I won't detail this exhaustively; you're best to use the IDE of your choice - PyCharm is a reasonable one that is helpful for this. ...

3

Even after using reserve, as suggested by @L.F. and @AJNeufeld, there was no considerable speedup and it was still timing out. So I decided to use dynamically allocated arrays instead. I also removed using namespace std, and instead only imported those std functions which are required in the program. #include <iostream> #include <vector> #...

2

I think the important thing to notice here is that you don't actually need a Trie. The partial queries are not just substrings of the names, they are prefixes. It should therefore be faster to just built a dictionary which directly counts how often each prefix occurs. Something as simple as this is sufficient (just tested it, it passes all testcases): from ...

7

Don't use using namespace std;. It is extremely bad practice and will ruin your life. You will have trouble on common identifiers like count, size, etc. See Why is “using namespace std;” considered bad practice? for more information. The input format is extremely awkward, but this seems to be beyond your control, so I'll leave it alone. Instead of using ...

7

The statement V.push_back(temp); can be inefficient, as the vector V may need to be reallocated multiple times. Use std::vector::reserve(N) to ensure sufficient space exists in the vector before reading in the data to avoid multiple reallocations. $3 \le N \le 10^7$ $1 \le \mathrm{arr}[i] \le 10^3$ With $10^7$ pigeons and $10^3$ holes, many ...

6

Things to improve You are looping over the input twice; (1) when splitting the raw input string Console.ReadLine().Trim().Split(' ') (2) when going over the splitted items for (int i = 0; i < arrSize - 1; i++). Try finding a way to go over the raw input in a single pass. You are parsing most items twice, once as arr[i+1] and once as arr[i] in the next ...

2

Constraints 1 <= N <= 100000 0 <= K <= 109 0 <= H[i] <= 109 1 <= Q <= 100000 0 <= l <= r < N My rule of thumb for this kind of challenge is that 109 operations is a limit. So, ignoring the hidden constant of Landau notation, we're looking at something which is $O(N\sqrt N + Q\sqrt Q + Q\sqrt{N} + K)$....

1

$\DeclareMathOperator{\Oh}{O}$Frankly, the code in the HackerRank editor is a large mess. It is promoting crap like #include <bits/stdc++.h> and using namespace std;, and the code contains many problems: int for traversing a std::vector, copying containers around, etc. Not to mention bad practices like i++, explicitly calling fout.close(), not ...

Top 50 recent answers are included