5

First let me point out a few details. Use const intead of let unless you are going to modify the value after initialization. You use it for the ofNumber variable, but there are more that deserve it. But actually there's often no need to define a variable at all if it is used only once. Similarily, storing a return value to a variable and immediately ...


5

You can simplify the code (and maybe accelarate it) by not assigning variables for values that are used once. spreading the nums array is slow (Math.max), see also. Math.max with spreading or the faster alternative (Math.max.apply) for a larger array (somewhere between 120.000 - 130.000 elements) will throw a Range Error, so use a loop for it. reduce is (a ...


4

First thing first, get rid of these ugly a and c. They do not add any value, but only obfuscate the code. def gap(p, q, m): """To generate gap in between two prime numbers""" """p is the difference,q is the lower limit where the list of numbers in between which prime is filtered,m is the upper limit""...


3

Depending on how large your lower and upper limits are, it may be faster to just generate all primes using a Sieve of Eratosthenes implementation. If the limits are beyond what is reasonable to generate all primes for, then primality testing such as Miller-Rabin is significantly faster than trial division. For example, gmpy.is_prime.


3

Code Review Your code is a little hard to read. You should have a blank line after the solution body, to separate it from the mainline code. This code is hard to describe, document, and debug: l = map(int, input().split(' ')) print(solution(*l)) What is l? How many arguments are there? If the wrong number of arguments are given as input, the problem doesn'...


3

I don't think there is a logic error in what you have posted. And even though there are details that would make it nicer (the cycle can for example run from i=1, questionable use of bits/stdc++.h header, long variable scopes and similar), it should do its job. I suggest to think about time complexity of the algorithm. Your brute force approach will have ...


3

RE60K has already critiqued your algorithm, so I'll just leave some minor comments on the code style. These comments won't help you beat the time limit, but they are good habits to get into if you're going to take a coding interview someday, for example. Your class Solution has only public members. Classes with all public members are frequently defined as ...


3

The most basic method of checking the primality of a given integer n is called trial division. This method divides n by each integer from 2 up to the square root of n. Any such integer dividing n evenly establishes n as composite; otherwise it is prime. Integers larger than the square root do not need to be checked because, whenever n=a * b, one of the two ...


2

The most expensive operation is the addToKey x that adds x to all keys in map, because substantially you have to create a new entry key, value + x in your hashmap and delete the old entry key, value. To avoid the need of caching the old entry while iterating over the map, you can distinguish two cases: x > 0, then if you have iterate over a keyset ordered ...


2

I have some suggestions for you. Extract some of the logic to methods. In your code, when the query is insert and get, you have two big blocks of code that are similar; you can extract to a method and reuse the method in both sections. I suggest a method that returns a boolean based on the if condition, so you will be able to set the currValue and currKey ...


2

You recursive solution takes numbers from [0..left] and [right..n-1] where (left+1)+(n-right) <= k So even if there are k ways to select some elements from left and others from right, i.e. (0,k), (1,k-1), ... (k,0). You look at far more a bigger sample space, in worst case, it would be \$O(n^2)\$. I don't think much can be done with this approach.


1

First off I think your code can be adjusted: Booleans to keep track of even or odd size of matrix and even or odd numbered rows allows you to use one set of loops and simply change which set of numbers are printed. To test for odd or even I like (num & 1) if the result is 0 it's even, 1 it's odd. I think using modulus for this is inefficient. Changing ...


Only top voted, non community-wiki answers of a minimum length are eligible