Skip to main content

Questions tagged [programming-challenge]

Use this tag when the code is a solution to a programming challenge. Always include a sufficient description of the problem to be solved - while a link to the challenge is welcome, the review request needs to be complete when the challenge site is unavailable.

Filter by
Sorted by
Tagged with
61 votes
10 answers
4k views

Using separate functions for Project Euler 1

I started programming with Java and C++, so I'm used to having a 'main' function that calls other functions that do the actual work. At university I was always told that doing actual computation in ...
Basil's user avatar
  • 871
59 votes
9 answers
14k views

Project Euler problem 1 in Python - Multiples of 3 and 5

I'd like suggestions for optimizing this brute force solution to problem 1. The algorithm currently checks every integer between 3 and 1000. I'd like to cut as many unnecessary calls to ...
Robert S Ciaccio's user avatar
41 votes
1 answer
2k views

Finding minimum scalar product using ST Monad

Inspired by a Stack Overflow question, I decided to practice my Haskell by taking a crack at the Google Code Jam's Minimum Scalar Product problem: Given two vectors \$\mathbf{v_1}=(x_1,x_2,\ldots,...
rampion's user avatar
  • 563
40 votes
9 answers
9k views

The FizzBuzz challenge in Java 8 written in a short, readable and interesting way

I decided to take on the FizzBuzz challenge with as twist that I would use Java 8 concepts to make it a bit modular, yet still let it be a short, readable and understandable program. This in contrary ...
skiwi's user avatar
  • 10.6k
35 votes
2 answers
1k views

LOL'ing-Up Project Euler One

It's been a while since I last wrote some lolcode, so I felt like tackling Project Euler #1. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum ...
Mathieu Guindon's user avatar
33 votes
7 answers
6k views

Project Euler #7 10001st prime

I decided to start working on the Euler project exercises. I'm doing fine for now but the seventh problem is running quite slow and I can't think of anything to make it work faster. It takes around ...
Denis's user avatar
  • 8,508
32 votes
1 answer
8k views

100 gunmen in a circle kill next person

I am very happy because I solved this problem with very little code: ...
Caridorc's user avatar
  • 27.5k
32 votes
8 answers
5k views

Project Euler Problem 45

As a self-teaching Python beginner for almost 4 months, I have mostly been doing online challenges including Project Euler problems. Problem 45 asks: Triangle, pentagonal, and hexagonal numbers ...
ifsoMarcus's user avatar
32 votes
7 answers
8k views

100 gunmen in circle kill next person

The question is here. 100 people are standing in a circle with gun in their hands. 1 kills 2, 3 kills 4, 5 kills 6 and so on till we are left with only one person. Who will be the last person alive....
Koray Tugay's user avatar
  • 1,547
32 votes
6 answers
3k views

Aliens at the train

I solved this problem on SPOJ: The aliens have arrived to Earth and everything is in harmony, the two races can live together. However, one specific Female Alien does not want to see humans on ...
Nitin Labhishetty's user avatar
30 votes
6 answers
72k views

Checking for balanced brackets in Python

I'm solving HackerRank "Stacks: Balanced Brackets" in Python. A bracket is considered to be any one of the following characters: (, ...
vinibrsl's user avatar
  • 647
30 votes
3 answers
27k views

Perm-Missing-Elem: 100% functional score, but only 60% performance

I cannot figure out why I didn't get 100% success from the Codility's Perm-Missing-Element test even I solve with \$O(n)\$. I will appreciate any advice/answers. You can see the problem, my code, and ...
quartaela's user avatar
  • 505
29 votes
4 answers
6k views

Project Euler 001 in C++

I come from a C background and have now just begun C++. This also means that I am not focussing on the algorithm, but more on C++ idioms. Description: If we list all the natural numbers below 10 ...
FromTheStackAndBack's user avatar
29 votes
6 answers
4k views

DailyProgrammer 284: Wandering Fingers

Description Software like Swype and SwiftKey lets smartphone users enter text by dragging their finger over the on-screen keyboard, rather than tapping on each letter. You'll be given ...
px06's user avatar
  • 393
27 votes
7 answers
8k views

Trapping Rain Water

This task is taken from Leetcode: Given n non-negative integers representing an elevation map where the width of each bar is 1, ...
thadeuszlay's user avatar
  • 3,993
27 votes
6 answers
119k views

Project Euler #1: Multiples of 3 and 5

Challenge Description: If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of ...
MTHL's user avatar
  • 535
27 votes
5 answers
6k views

Tape Equilibrium

I picked the first test (Tape Equilibrium) from Codility here. Question: A non-empty zero-indexed array A consisting of N integers is given. Array A represents numbers on a tape. Any integer P, ...
Sabarish's user avatar
  • 541
26 votes
2 answers
6k views

Printing 1,000,000 numbers in 1 sec. in Python

Here's a fairly simple task from CSES Problem Set - Permutations 1070 that reads: A permutation of integers 1,2, …, n is called beautiful if there are no adjacent elements whose difference is 1. ...
Bor's user avatar
  • 507
26 votes
3 answers
2k views

Diamond in the rough! Finding Gems in the rocks

Recently a question was asked that solved a hackerrank problem: Counting gems amongst the rocks The description of the challenge is: John has discovered various rocks. Each rock is composed of ...
rolfl's user avatar
  • 97.4k
26 votes
4 answers
1k views

Generalized Project Euler 1: A sledgehammer to crack a nut

The problem Project Euler 1 is one of the most asked questions on site. However I wanted to solve the more general problem of division. Multiples of a list If we list all the natural numbers ...
N3buchadnezzar's user avatar
26 votes
3 answers
2k views

Project Euler Problem 2 in Clojure

I am in the process of learning Clojure. I am fairly new to functional programming and would like to know if my code smells or if there are any performance implications with my approach. ...
Jeremy's user avatar
  • 539
25 votes
8 answers
6k views

Writing nested for loops to produce certain output

My task: Write nested for loops to produce the following output with each line 48 characters wide: ...
JavaBeginner's user avatar
25 votes
3 answers
9k views

Leetcode: Valid parentheses

Leetcode: Valid parentheses Given a string containing just the characters (, ), {, ...
Gilad's user avatar
  • 5,311
25 votes
2 answers
2k views

Speed up solution to Project Euler problem 75

I wrote this code for Project Euler problem 75, which asks how many integers ≤ 1500000 exist, where the integer is a perimeter length that can be divided into three integer-length sides of a right ...
user2312505's user avatar
25 votes
4 answers
4k views

x86-64 Assembly - Sum of multiples of 3 or 5

I'm trying to learn some basic x86 assembly and so I've begun solving Project Euler problems. I was hoping for some critique of my code that, hopefully, includes either the efficiency of the ...
sdasdadas's user avatar
  • 557
25 votes
4 answers
3k views

Project Euler #1 Sum of all the multiples of 3 or 5 below 1000

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Find the sum of all the multiples of 3 or 5 below 1000. My ...
JaDogg's user avatar
  • 4,531
25 votes
3 answers
4k views

What does the Bob say?

Description: Bob is a lackadaisical teenager. In conversation, his responses are very limited. Bob answers 'Sure.' if you ask him a question. He answers 'Whoa, chill out!' if you yell ...
CodeYogi's user avatar
  • 5,107
25 votes
5 answers
2k views

Count distinct primes, discarding palindromes, in under 2 seconds

Problem Statement Generate as many distinct primes P such that reverse (P) is also prime and is not equal to P. Output: Print per line one integer( ≤ 1015 ). Don't print more than 106 ...
Akash Rana's user avatar
24 votes
7 answers
14k views

Finding pairs of Amicable Numbers, up to 10000

I have this program to find pairs of amicable numbers in C: ...
Rahul's user avatar
  • 427
24 votes
12 answers
4k views

Project Euler 8: Maximum product of 13 adjacent digits

Problem 8 on Project Euler which asks Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the product? This is my solution in Visual C#. ...
Paras's user avatar
  • 585
23 votes
8 answers
3k views

Area of the triangle generated by the hands of a clock

Problem explanation: Let’s suppose we have a clock that has an hour hand 3 units long, a minute hand 4 units long, and a second hand 5 units long. The hour hand moves once every hour, the minute hand ...
Xavi Reyes's user avatar
23 votes
3 answers
3k views

Project Euler #1 in PHP

The code below works & as far as I can tell the result is correct. Would you please review and let me know what I could have done better? I tried to use variables as much as possible, that way it ...
Phrancis's user avatar
  • 20.4k
23 votes
5 answers
18k views

Finding the 10001st prime

I'm solving Project Euler problem 7, which says: By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13. What is the 10001st prime number? Here's the ...
Nicholas Hassan's user avatar
23 votes
2 answers
4k views

Ruby Koans' Greed Task

Having coded in Java and C# for quite some years, I'm currently learning Ruby. I'm working my way through the Ruby Koans tutorial. At some point, you are to implement a method that calculates the game-...
Felix Alcala's user avatar
21 votes
4 answers
3k views

Project Euler #2 (Even Fibonacci numbers) in Swift

I figured working through Project Euler problems in Swift would be a good way to learn any tips or tricks. For example, tuples are something I'm not used to using, but they proved useful here. Using ...
nhgrif's user avatar
  • 25.3k
21 votes
6 answers
4k views

Find the longest word's length

The challenge is simple: Return the length of the longest word in the provided sentence. The solution is just as simple: ...
Mast's user avatar
  • 13.4k
20 votes
5 answers
3k views

Print the N longest lines in a file

I have implemented the "Find the N longest lines in a file" problem from CodeEval quoted below. I got a full 100 score and 182ms execution time of their data set on the site so I consider the code ...
Emily L.'s user avatar
  • 16.6k
19 votes
6 answers
7k views

Determining if the kangaroos will land in the same position

Problem There are two kangaroos on an x-axis ready to jump in the positive direction (i.e, toward positive infinity). The first kangaroo starts at location x1 and moves at a rate of v1 meters ...
amallard's user avatar
  • 365
19 votes
4 answers
6k views

Project Euler #1 in C++

I'm learning C++ and currently doing Project Euler challenges. The first challenge is the following. If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. ...
o2640110's user avatar
  • 375
19 votes
9 answers
2k views

Repeatedly eliminate a substring

I'm trying to pass a programming challenge where we are to replace all instances of a substring in a string until there are none left. So an input like ...
Seth Kitchen's user avatar
19 votes
2 answers
596 views

Count the occurence of nucleobases in DNA string

Inspired by this meta question I decided to take a look at Rosalind. Their first challenge seemed easy enough: An example of a length 21 DNA string (whose alphabet contains the symbols 'A', 'C', 'G', ...
Mast's user avatar
  • 13.4k
19 votes
2 answers
3k views

Project Euler "Even Fibonacci numbers" in Java 8

I'm looking for general advice on my code on the following problem: Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms ...
skiwi's user avatar
  • 10.6k
19 votes
2 answers
2k views

"ACM ICPC Team" challenge on Hackerrank... Easy?

This challenge took me awhile to figure out, and it's characterized as "easy," so I'm sorta wondering what I missed. Problem Statement You are given a list of N people who are attending ACM-...
Robert Harvey's user avatar
18 votes
8 answers
5k views

Project Euler Problem #4 - Palindromic number

The problem is as follows: A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99. Find the largest palindrome made ...
IvenBach's user avatar
  • 3,487
18 votes
5 answers
3k views

Flipping coins performance

This is the Flipping coins problem on CodeChef: There are \$N\$ coins kept on the table, numbered from \$0\$ to \$N - 1\$. Initially, each coin is kept tails up. You have to perform two types of ...
Ukiyo's user avatar
  • 283
18 votes
4 answers
2k views

O(N) vs O(N * N) for Word Frequency Algorithm

I am trying to come up with a better solution to the following problem: Statement You are supposed to write a function which takes the contents of a document in string format and also a whole ...
ng-hacker-319's user avatar
17 votes
7 answers
12k views

Hacker Rank: Array left rotation

This code is to solve the Hacker Rank problem array left rotation. A left rotation operation on an array of size n shifts each of the array's elements 1 unit to the left. For example, if 2 left ...
Ramakrishna's user avatar
17 votes
7 answers
4k views

Project Euler #3 - largest prime factor

I was going through the Project Euler problem #3 and made a program to solve it. The problem is as follows: The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the ...
user2040026's user avatar
17 votes
4 answers
1k views

Duplicate like a weapon, arrays like heaven

Challenge: Find the duplicated entry. Specifications: Your program should accept as its first argument a path to a filename. Each line in this file is one test case. Each line begins ...
Legato's user avatar
  • 9,829
17 votes
2 answers
2k views

Yandex programming contest: Alarms

I've tried to solve this challenge about 4 hours during the contest, but all my attempts exceeded the time limit. I tried to solve it with min-heap, but can't pass all the tests. How it can be solved ...
maksadbek's user avatar
  • 281

1
2 3 4 5
68