Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
When the code scales so poorly in the face of large inputs that it cannot complete in a reasonable amount of time, use this instead of the [performance] tag.
0
votes
Find perfect squares between two numbers
Don't repeat yourself (DRY)
rem=square%10;
if(rem!=1 && rem!= 4 && rem!=9 && rem!=0 )
{
break;
…
1
vote
Checking if all possible combinations of any two array elements in an array contains 0-9 atl...
static int winningLotteryTicket(String[] tickets,int n)
// tickets = array of all strings
{
Please don't put comments in between a function declaration and the function body (which starts with …
11
votes
Accepted
Phone number duplication detector
Chomp the new line
scan.nextLine(); // But why?
Nothing to do with performance, but Scanner is skipping nextLine() after using next(), nextInt() or other nextFoo()? explains th …
2
votes
HackerRank Left rotation code using Java 8
String[] nd = scanner.nextLine().split(" ");
int c;
int n = Integer.parseInt(nd[0]);
int d = Integer.parseInt(nd[1]);
First, why declare c here? You don't use it …
4
votes
Project Euler problem 530 - GCD function is inefficient
public static long GCD (long p, long q) {
if (p == 0) return q;
else return GCD (q%p,p);
}
The iterative version of this is
public static long gcd(long a, long b) {
while ( 0 != b ) …
6
votes
Accepted
Sum of all the primes less than or equal to N
Sieve of Eratosthenes
The first possibility would be to implement the Sieve of Eratosthenes. It's one of the more efficient ways to find all primes between 1 and N. But there are other things we ca …
2
votes
Prime generator for SPOJ
public class SPOJ_Prime1 {
private static final long LARGEST_CANDIDATE = 1000000000;
private static final int SEGMENT_SIZE = (int)Math.sqrt(LARGEST_CANDIDATE) + 1;
private static SortedS …
1
vote
Accepted
Kattis "Chasing Subs" custom decryption challenge
Alternative approach
You are repeating a lot of work. Each time you advance a character, you rebuild the matching. But you don't need to do that. Instead create an array that says where to find th …
3
votes
Accepted
Codechef COOKMACH solution, exceeding time limit
if(A%2!=0){
if(A<B){
while(A!=1){
A=(A-1)/2;
count=A;
}
This looks like a bug. Consider the case where A is 5 and B is 8 or mor …
1
vote
Finding b and e such that b to the power of e is closest to a given number
try with resources
Scanner in = new Scanner(System.in);
My IDE complains because this is never closed.
try (Scanner in = new Scanner(System.in)) {
Now it will close the Scanner autom …
0
votes
Programming challenge “Greatest Odd Divisor”
sum = sum + GOD(i);
In Java and most C-based languages, you can write this as
sum += GOD(i);
Which is just a shorter way of writing the same thing. This works for most arithme …
2
votes
Obtaining a target number only using the operations ×2, ×3, and +1
void primitive_calculator(int64_t number)
{
std::vector<int64_t> min_steps(number+1,INT_MAX);
std::list<int64_t>* path=new std::list<int64_t>[number+1];
min_steps[0]=0; min_st …
2
votes
Determining the cardinality of sets defined by recursively following indexes
Remove unused elements
Set<Integer> resultSet = null;
You don't actually read this at any time. So you can delete this line and
resultSet = tempSet;
without any chan …
1
vote
Accepted
"How many numbers exist which are less than or equal to n and are divisible by a, b or c?"
static long FindLCM(long a, long b)
{
return (a * b) / new BigInteger(a+"").gcd(new BigInteger(b+"")).intValue();
}
A simple optimization here would be to convert to BigInteger …
3
votes
How to do modulo thing fast (less execution time)
This algorithm is \$\mathcal{O}(nk)\$ but could be \$\mathcal{O}(n)\$.
It is \$\mathcal{O}(nk)\$ because you reset j k times with j bounded by n. This makes it very much like having two for loops. C …