The Stack Overflow podcast is back! Listen to an interview with our new CEO.

Questions tagged [restricted-complexity]

Challenges with a spec which requires all answers to meet certain time complexity restrictions. This could be specific ("Your answer must be O(n^2) where n is the number of items in the input"), or at the level of complexity classes ("Your answer must be polynomial in the number of items in the input").

Filter by
Sorted by
Tagged with
-3
votes
2answers
191 views

A really inefficient calculator [closed]

Challenge: Add two numbers. In O(n^m) time, where n and m are the two numbers given. Sleeping, or your language equivalent, is not allowed. Input: Two integers, separated by a space. Output: The sum ...
-5
votes
1answer
347 views

Write the shortest O(n^2) sorting algorithm [closed]

Write a sorting algorithm for a list of integers that has an average performance of O(n^2). This is code-golf, so the shortest code in bytes wins. RULES: The characters wasted for the declaration ...
1
vote
0answers
87 views

Finding row wise sum of transpose of hv-convex binary matrix [closed]

I'm stuck on a problem involving the Gale-Ryser Theorem. The problem's input gives me the row-wise sum of an hv-convex binary matrix(n*m). ...
10
votes
0answers
249 views

Solve Subset-Sum in polynomial time (…if P = NP)

Shocking news: Dr. Mad J Scientist has released a proof of P = NP to the world. But the proof is nonconstructive, and she's keeping the algorithm to herself. Worry not. Without even looking at her ...
3
votes
6answers
369 views

Let's Play some ProSet! [duplicate]

ProSet is a classic card game that is played normally with 63 cards. One card has 6 colored dots on it, like below The rest of the cards are missing some of these 6 dots, but each card has at least 1 ...
11
votes
1answer
661 views

Polite Near-Sighted Drunk Bot on a Minefield

As the title may suggest, this problem is semi-inspired by the Polite Near-Sighted Drunk Bot by @N.P. Our poor bot is placed on a cartesian grid at the origin, and after each minute, it moves 1 unit ...
10
votes
6answers
664 views

Find the sum of closest distances

For this task your code should take two sorted arrays of integers X and Y as input. It should compute the sum of the absolute distances between each integer in X and its closest number in Y. ...
18
votes
20answers
3k views

Give a permutation with no two consecutive integers next to each other

Challenge Given an integer n ≥ 4, output a permutation of the integers [0, n-1] with the property that no two consecutive integers (integers with absolute difference 1) are next to each other. ...
13
votes
4answers
400 views

Generalized Gray codes

Input: An array I of k positive integers. The integers will be no larger than 100 and k ≤ 100. Output: Your code must output all possible arrays O of non-negative integers of length k with the ...
10
votes
5answers
396 views

Circular Limited Sums

Challenge Let's imagine an N-tuple of integers between 0 and M inclusive, and let's call it ...
12
votes
15answers
1k views

Binning in time

The task in this challenge is to put elements of an array into time bins. The input will be a non-decreasing array of positive integers representing the time of events, and an integer which represents ...
12
votes
14answers
1k views

Put an array into bins

In this simple challenge you are given an input array L of non-negative integers and a number of bins b greater than 0 but no ...
8
votes
9answers
463 views

Make sets disjoint without emptying them

Suppose you have a set of sets of integers. It's possible that some of the sets will overlap (i.e. sharing elements). You could get rid of the overlaps by deleting elements from the sets, but then ...
9
votes
6answers
597 views

Output all distinct permutations of a vector

Challenge: Output all distinct permutations of a, potentially long, list of positive integers. You may assume that the vector has less than 1,000 numbers when testing, but the process should in ...
4
votes
0answers
194 views

Those annoying grasshoppers [closed]

The problem #6 of IMO 2009 reads: Let a 1, a 2, a 3, ..., a n, be distinct positive integers and let T be a set of n-1positive integers not containing a 1+a 2+a 3+...+a n, A grasshopper is to ...
7
votes
3answers
363 views

Quickly Prove Me Wrong!

Introduction This is the evolution of this previous challenge which was about checking satisfieability of normal formulae in conjunctive normal form (CNF). However, this problem is NP-complete and ...
65
votes
5answers
5k views

Powerprogramming: O(1^N), O(N^1), O(2^N), O(N^2) all in one

Write a program (or function) that exhibits four common big O time complexities depending on how it is run. In any form it takes in a positive integer N which you may assume is less than 231. When ...
24
votes
2answers
929 views

Write an Incident tokeniser

Background Incident is a fairly unusual programming language, in that its list of tokens is not predetermined, but rather inferred from the input. As such, tokenising an Incident program can be ...
10
votes
3answers
499 views

Find the shortest pangrams from a word list

A pangram is a string that contains every letter a-z of the English alphabet, case-insensitive. (It's OK if the pangram contains ...
8
votes
16answers
2k views

Check if 2 arrays contain the same element

Write a program which will take for input 2 integer arrays and return a truthy value if there exists an element which is present in both of arrays, or a falsy one otherwise. The obvious solution to ...
11
votes
22answers
965 views

Find the positive divisors!

Definition A number is positive if it is greater than zero. A number (A) is the divisor of another number (B) if ...
11
votes
9answers
1k views

A classic sorting code-golf question

This is a code-golf question. Input A list of non-negative integers in whatever format is the most convenient. Output The same list in sorted order in whatever format is the most convenient. ...
13
votes
11answers
3k views

Solve the Secretary Problem

The Secretary Problem is a famous problem described as thus: You need a new secretary You have N applicants that you can interview one at a time You are able to score each applicant after the ...
21
votes
3answers
394 views

Permutation Square Root

In math, a permutation σ of order n is a bijective function from the integers 1...n to itself. This list: 2 1 4 3 represents the permutation σ such that σ(1) = 2,...
19
votes
11answers
616 views

Maximise the squared difference

Consider a permutation of the integer values from 1 to N. E.g. this example for N = 4: <...
7
votes
3answers
451 views

Convolve integers in subquadratic time

An linear discrete convolution is an operation that turns two vectors of numbers into a third vector of numbers by multiplying elements inside-out. Formally, for two vectors ...
6
votes
3answers
451 views

Symmetric boolean functions as Zhegalkin polynomials

Let \$\mathbb{B} = \mathbb{Z}_2 = \{0, 1\}\$ be the set of booleans. A symmetric boolean function in \$n\$ arguments is a function \$f_S : \mathbb{B}^n \to \mathbb{B}\$ that checks if the number of ...
19
votes
3answers
408 views

One goes up, the other comes down

Introduction In this challenge, your task is to decide whether a given sequence of numbers can be separated into two subsequences, one of which is increasing, and the other decreasing. As an example, ...
7
votes
2answers
589 views

Crack a Vigenère Cipher

A Vigenère Cipher is encrypted by repeating a keyword to be the length of the plaintext, and summing each character in that text with the corresponding letter in the plaintext modulo 26. (using A=0,B=...
12
votes
2answers
444 views

Pair Capacitors

Capacitors are notorious for being manufactured with high tolerances. This is acceptable in many cases, but some times a capacity with tight tolerances is required. A common strategy to get a capacity ...
12
votes
3answers
347 views

Books on a Shelf

I have some books and a bookshelf. I would like to put as many books on the shelf as possible but I have a rule. All dimensions of the books (height, width and depth) should form a non-increasing ...
15
votes
6answers
1k views

Real-time string matching

Task The task is to golf a real-time exact string matching algorithm of your choice. Input Two lines of text supplied on standard input, separated by a new line. The first line contains the "...
14
votes
4answers
1k views

Find the maximum of ax+b

You are given a list of (a,b), and a list of x. Compute the maximum ax+b for each x. You can assume a, b and x are non-negative integers. Your program or function must run in expected (to the ...
45
votes
1answer
4k views

Longest common substring in linear time

This challenge is about writing code to solve the following problem. Given two strings A and B, your code should output the start and end indices of a substring of A with the following properties. ...
21
votes
2answers
1k views

Book Stack Sort

When stacking books you usually want to put the largest ones at the bottom and the smallest ones at the top. However, my latent OCD makes me feel very uneasy if I've got two books where one is shorter ...
13
votes
15answers
3k views

Pick the longest stick

You are a young programming geek living with your 2 other best friends. Every week, one of you has to do all the chores of the house and you decide whose turn it is by picking a stick. The one who ...
7
votes
1answer
741 views

Approximate square root from algorithm time complexity

There have been a few square root challenges lately, but I think this one is different. Challenge Find an algorithm such that the worst case time complexity is proportional to ...
7
votes
4answers
3k views

Quickly find length of n-th term of the look-and-say sequence

The look and say sequence is an example of a run length encoding sequence. To get the next term of the sequence one groups the sequence into runs of the same number, each group in the next term then ...
5
votes
13answers
862 views

Generate the list [1, 0, -1, 0, 1, 0, -1, 0, …] [closed]

In the spirit of a question on math.se: given an integer n, return the integer in position n of the sequence [1, 0, -1, 0, 1, 0, ...
5
votes
8answers
1k views

Return the first N primes

With a twist; your algorithm has to be less complex than O(X2) where X is the Nth prime. Your solution post should include the character count and the theoretical complexity in terms of X (or N, which ...
6
votes
6answers
411 views

Minimum of last k

Write a function that takes two arguments: a number k and a list l, and that returns another list ...
7
votes
0answers
766 views

Stable positive/negative separation [closed]

From SO. You are given an array with positive and negative integers. Write a function to change the elements order in the array such that negative integers are at the beginning, the positive integers ...