All Questions
Tagged with programming-challenge performance
629 questions
9
votes
2
answers
1k
views
Return all valid expressions with specific target
The problem is stated as: Given a string that contains only digits 0-9 and a target value, return all expressions that are created by adding some binary operators (+, -, or *) between the digits so ...
8
votes
4
answers
4k
views
Reversing a string - two approaches
Test Case:
Input- "I'm hungry!"
Output- "!yrgnuh m'I"
Approach 1: In this approach, I used a empty string and bind it with the input string reversely.
...
8
votes
3
answers
5k
views
Project Euler -problem 1
Find the sum of all the multiples of 3 or 5 below 1000.
https://projecteuler.net/problem=1
Is there any hack to do modulo operation in better way?
...
8
votes
3
answers
281
views
Refactor the code which performs "cross-product", "reduce", "product of list" and "sum of list"
I have come up with a sequence of steps to find the maximum product of y positive numbers which add up to x. But the program is ...
8
votes
3
answers
2k
views
Leetcode 125. Valid Palindrome (better performance?)
Problem statement
Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignoring cases.
For example,
"A man, a plan, a canal: Panama" is a ...
8
votes
1
answer
3k
views
Project Euler 407: Is there any more optimal way to solve this idempotent equation (modulo n ring)?
Project Euler problem 407:
If we calculate a2 mod 6 for 0 ≤ a ≤ 5 we get: 0, 1, 4, 3, 4, 1.
The largest value of a such that a2 mod 6 = a is 4.
Let's call M(n) the largest value of a < n ...
8
votes
4
answers
476
views
Calculate sum of multiple series within array
The exercise is as follows:
Player A will write list \$ a \$ consisting of \$ n \$ numbers. Player B will take a look at the paper. After that the A player will ask player B \$q\$ questions. Each ...
8
votes
3
answers
3k
views
Two sets came to an intersection
Challenge:
Print set intersections.
Specifications:
Your program should accept as its first argument a path to a filename.
Each line in the file is a test case.
Each test case contain two ...
8
votes
2
answers
2k
views
Word-Ladder solver in Python 3
Follow-up to Python program to find a word ladder transforming "four" to "five"
I'm trying to write a program to solve a word ladder puzzle to brush up on my Python. For those who ...
8
votes
2
answers
355
views
The only thing better than being unique
is being first and unique.
Challenge:
Write a program which finds the first non-repeated character in a string.
Specifications:
The first argument is a path to a file.
The file contains ...
8
votes
1
answer
2k
views
Advent of Code 2018 day 1 part 2: find the first repeated number after some increases and decreases
Here is the problem:
Part Two
You notice that the device repeats the same frequency change list over and over. To calibrate the device, you need to find
the first frequency it reaches twice.
For ...
8
votes
2
answers
749
views
Sum of prime factors of binomial coefficients over 9000!\${}\$
The problem
I have lately been working Project Euler: 231:
The prime factorisation of binomial coefficients
The binomial coefficient \$ ^{10}C_3 = 120 \$.
\$ 120 = 2^3 × 3 × 5 = 2 × 2 ×...
8
votes
2
answers
3k
views
Make Depth First Search program more time efficient
This is a problem from programming contest. The original problem can be found here on hackerearth.com.
In the first line there is one integer T denoting the number of test
cases. Description of T ...
8
votes
1
answer
1k
views
Project Euler Problem 33 - digit canceling fractions
Problem:
The fraction \$(\dfrac{49}{98})\$ is a curious fraction, as an inexperienced
mathematician in attempting to simplify it may incorrectly believe
that \$(\dfrac{49}{98}) = (\dfrac{4}{8}...
8
votes
2
answers
457
views
(Codewars Kata) Memoised Log Cutting
(I am not sure of the official CS name for the problem in question, so I just gave it the name of the kata).
Problem
CLEAR CUTTER'S NEEDS YOUR HELP!
The logging company Clear Cutter's ...
8
votes
3
answers
1k
views
Word Pattern challenge in LeetCode
I solved this problem from LeetCode:
Given a pattern and a string str, find if str follows the same
pattern.
Here follow means a full match, such that there is a bijection between
a letter ...
8
votes
1
answer
2k
views
Codewars: N-dimensional Von Neumann Neighborhood in a matrix
Task:
For creating this challenge on Codewars I need a very performant
function that calculates Von Neumann Neighborhood in a N-dimensional
array. This function will be called about 2000 times
The ...
8
votes
1
answer
2k
views
"Dungeon Game" solution
The demons had captured the princess (P) and imprisoned her in the
bottom-right corner of a dungeon. The dungeon consists of M x N rooms
laid out in a 2D grid. Our valiant knight (K) was initially ...
7
votes
4
answers
1k
views
Project Euler #14 - Longest Collatz sequence
I'm attempting some Project Euler problems in an attempt to learn Python. While I can get the correct answer quite easily, my programs are quite slow. Project Euler, if doing it correctly, specifies ...
7
votes
2
answers
527
views
Optimizing this inefficient TicTacToe configuration parser
On a programming contest I came upon this question:
Given a partially played 3 × 3 tic-tac-toe configuration, write a program to determine which player will have a better chance of winning if the ...
7
votes
3
answers
3k
views
Efficiency of Project Euler problem 35
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, ...
7
votes
5
answers
1k
views
Project Euler #12 - first triangle number with more than 500 divisors
I tried to solve Project Euler 12 with a first very naive solution by myself. It took nearly 30 minutes to run until it found the solution. Then I made a change in the function ...
7
votes
6
answers
5k
views
Improving efficiency for two sum
I am trying to solve this problem:
Given an array of integers, find two numbers such that they add up to a specific target number.
and this is my strategy:
For each ...
7
votes
4
answers
839
views
Finding the sum of all the multiples of 3 or 5 below 1000, using list comprehension
I'm trying to compare my own implementation with another solution of Project Euler problem #1, which uses a list comprehension:
...
7
votes
3
answers
451
views
Modified Sieve of Eratosthenes has very long runtime
I am slowly moving through Project Euler, and have reached problem #10:
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
I did a naive ...
7
votes
3
answers
435
views
Keeping Linq's readability (e.g. over goto statements) without sacrificing performance
I'm doing Project Euler problem 5 as a kata, focussing on TDD and code readability. The challenge is:
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to ...
7
votes
5
answers
1k
views
Project Euler #35 - Circular primes solution is slow
This is my code for project euler #35 (in Python):
The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
There are thirteen such ...
7
votes
2
answers
3k
views
Prime factorization for integers with up to 9-digit long prime factors
I've become interested prime factorization since solving Project Euler problem 3 (finding the largest prime factor of 600851475143). Learning here that initializing lists with many elements to later ...
7
votes
5
answers
3k
views
CodeEval Challenge: Remove specified characters from a string
Question
Write a program which removes specific characters from a string.
The first argument is a path to a file. The file contains the source
strings and the characters that need to be ...
7
votes
2
answers
2k
views
Arranging numbers in a circle such that the sums of neighbors and sums of diametric opposites are prime
I need to create a circle with numbers from 1 to n , however it is necessary to respect some rules:
All numbers from 1 to n are used in the circle only once.
The sum of two consecutive numbers ...
7
votes
3
answers
2k
views
Project Euler problem 44: sums and differences of pentagonal numbers
This is my code for solving the pentagonal number problem on Project Euler:
Pentagonal numbers are generated by the formula, \$P_n=n\dfrac{3n−1}{2}\$.
The
first ten pentagonal numbers are:
...
7
votes
4
answers
15k
views
Sum of all paths between all pairs of nodes in a tree
I'm trying to solve a competitive programming problem. It basically gives a undirected graph (tree-like: no multiple paths between two nodes...) and asks for the sum of all possible paths between any ...
7
votes
4
answers
15k
views
Finding the Pythagorean triplet that sums to 1000
Project Euler problem 9 says:
A Pythagorean triplet is a set of three natural numbers, a < b < c,
for which,
a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.
There ...
7
votes
2
answers
6k
views
Sherlock and The Beast
I have recently written the program for the Sherlock and The Beast' HackerRank challenge. That's working fine, but the problem is that it takes too much time if a big number is given as a input. I ...
7
votes
2
answers
1k
views
Find good numbers in a given range
This is the Good Numbers problem at Code Chef:
A number is called a square-free number if there does not exist a number greater than 1, whose square divides the number. For example, 8 is not a ...
7
votes
2
answers
12k
views
Python program to solve the "skyline problem"
This is a Leetcode problem -
A city's skyline is the outer contour of the silhouette formed by all
the buildings in that city when viewed from a distance. Now suppose
you are given the ...
7
votes
3
answers
8k
views
Find K biggest numbers in the array
I was trying to implement method number 2, from this article.
Method 2 (Use temporary array) K largest elements from arr[0..n-1]
Store the first k elements in a temporary array temp[0..k-1]....
7
votes
2
answers
2k
views
Lychrel calculator (Project Euler 55)
If we take 47, reverse and add, 47 + 74 = 121, which is palindromic.
Not all numbers produce palindromes so quickly. For example,
...
7
votes
2
answers
2k
views
HackerRank: ACM ICPC Team
I made a solution to this problem from HackerRank :
You are given a list of N people who are attending ACM-ICPC World
Finals. Each of them are either well versed in a topic or they are
not. ...
7
votes
3
answers
3k
views
Size of the largest connected component in a grid in Python
Given an R x C grid of 1s and 0s (or True and False values), I need a function that can find the size of the largest connected ...
7
votes
2
answers
7k
views
Implementing a stack and using it to print alternate elements in reverse
Challenge:
Implement a stack interface.
Specifications:
The interface should have ‘push’ and ‘pop’ functions.
Your task is to ‘push’ a series of integers and then ‘pop’ and print every ...
7
votes
3
answers
581
views
Project Euler #645 -- speed up Monte-Carlo simulation in Python
I am trying to solve Q645. While the logic used for my code seems to be appropriate, the code itself is way too slow for the large number required in this question. May I ask for suggestion(s) to ...
7
votes
2
answers
3k
views
Deepest pit of an array
Following is based on a problem description from a Codility test, as a task in an interview.
DeepestPit - problem description
A non-empty zero-indexed array B ...
7
votes
3
answers
10k
views
Solution to Hackerrank challenge "Sherlock and Queries"
First, here is the full challenge description:
Watson gives to Sherlock an array: \$A_1, A_2, ..., A_N\$. He also gives to
Sherlock two other arrays: \$B_1, B_2, ..., B_M\$ and \$C_1, C_2, ..., ...
7
votes
1
answer
1k
views
Optimizing string match algorithm
I was trying to solve this problem. I thought of any possible relation between the number of matches among subsequent substrings, but could not find one. So, the probably naive solution that I ...
7
votes
1
answer
540
views
Check consistency of a list of statements, with fuzzy rhyme matching
I made a program today that solves the following problem on a programming competition site (open.kattis.com): Check if all statements with the following format X is Y or X not Y are consistent. X and ...
7
votes
1
answer
688
views
I CAN HAS EULR 14, PLZ?
A few days ago I found myself learning lolcode. It turns out it's (almost) a real language and capable of handling (pseudo-)serious problems. So, let's try some (pseudo-)serious code this time.
...
7
votes
1
answer
312
views
Eight Directions Crossword
I solved a problem which you can read about here, but it gives me "time limit exceeded" even though it runs in 4 seconds. I really don't understand why.
I don't think my code can be any simpler, ...
7
votes
1
answer
3k
views
Solving systems of linear equations
I am working on the following problem (http://www.enigmagroup.org/missions/programming/9/).
You have a 6x6 table where the first 5 columns and rows are comprised of different types of shapes, 5 total....
7
votes
2
answers
572
views
Search and Replace (FreeCodeCamp intermediate algorithm scripting)
I completed this challenge Search and Replace and passed the tests:
Perform a search and replace on the sentence using the arguments provided and return the new sentence.
First argument is the ...