Skip to main content
We’ve updated our Terms of Service. A new AI Addendum clarifies how Stack Overflow utilizes AI interactions.

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
1 vote
2 answers
312 views

Input You are given 2 positive integers, n, q, followed by q queries. the queries can be of two forms: 0 a b: add the line a*x + b. a and b are integers between -...
3RR0R404's user avatar
  • 115
11 votes
16 answers
1k views

Given an array of integers A, the task is to output another array B of the same length so that B[i] is the maximum over A for every index that is not i. That is \$B[i] = \max_{i' \ne i} A[i']\$. ...
Simd's user avatar
  • 3,167
3 votes
1 answer
231 views

Consider an array A of integers of length n. The k-max subarray sum asks us to find up to \$k \leq 3\$ (contiguous) non overlapping subarrays of A with maximum sum. If A is all negative then this ...
Simd's user avatar
  • 3,167
9 votes
1 answer
424 views

Earlier I asked about this problem on stackoverflow (link), but now I also want to see the golfiest solutions Problem: Given an array of arbitrary numbers, what is the longest subarray that is ...
user23274861's user avatar
7 votes
4 answers
858 views

Related. Given a number \$n\$, Euler's totient function, \$\varphi(n)\$ is the number of integers up to \$n\$ which are coprime to \$n\$. That is, no number bigger than \$1\$ divides both of them. For ...
Command Master's user avatar
21 votes
11 answers
2k views

Given a number \$n\$, we have its sum of divisors, \$\sigma(n)\ = \sum_{d | n} {d}\$, that is, the sum of all numbers which divide \$n\$ (including \$1\$ and \$n\$). For example, \$\sigma(28) = 1 + 2 +...
Command Master's user avatar
10 votes
2 answers
550 views

Given two strings \$A\$ and \$B\$ with edit (Levenshtein) distance \$x\$, find a third string with edit distance \$a\$ to \$A\$ and edit distance \$b\$ to \$B\$ so that \$a+b=x\$ and \$a=int(x/2)\$ (...
Simd's user avatar
  • 3,167
10 votes
3 answers
397 views

Suppose we want to encode a large integer \$x\$ as a list of words in such a way that the decoder can recover \$x\$ regardless of the order in which the words are received. Using lists of length \$k\$ ...
Karl's user avatar
  • 871
12 votes
17 answers
2k views

Consider a sorted array of positive floating point numbers such as: input = [0.22, 2.88, 6.35, 7.17, 9.15] For each integer \$i\$ from 1 up to the last value in ...
Simd's user avatar
  • 3,167
13 votes
14 answers
789 views

Given a permutation, we can define its high-water marks as the indices in which its cumulative maximum increases, or, equivalently, indices with values bigger than all previous values. For example, ...
Command Master's user avatar
8 votes
3 answers
441 views

It can be easily proven using Hall's marriage theorem that given fixed \$n\$ and \$k<n/2\$, there is an injective (one-to-one) function from all \$n\$-bit strings with \$k\$ ones to \$n\$-bit ...
Parcly Taxel's user avatar
  • 4,749
7 votes
3 answers
495 views

Write a program that takes an undirected graph and finds the minimum cut, i.e., the set of edges that, if removed, would disconnect the graph into two or more connected components. The program should ...
user avatar
12 votes
11 answers
837 views

You are given a matrix of size m x n where each cell can contain either 1 or 0. You need to find the largest square submatrix that contains only 1's. The output should be the area of the largest ...
user avatar
9 votes
9 answers
923 views

Let's solve the same task as in this challenge but faster! Input: a non-empty string containing letters a-z Output: the length of a longest (contiguous) substring in which all letters are different ...
anatolyg's user avatar
  • 14.1k
12 votes
10 answers
1k views

Introduction Our goal is to efficiently find the maximum of a large amount of (redundant) data. We define the outer product of vectors \$A\$ and \$B\$ as a matrix containing the products of all ...
Sebastian's user avatar
  • 221
10 votes
6 answers
818 views

Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details. You successfully route the laser into the sensor, but nothing happens. "What?" Frustrated, you flip the ...
Bubbler's user avatar
  • 79.3k
12 votes
3 answers
569 views

I struggle to easily encode big numbers in ><>. If only there was a program that could find the best way for me? What is ><> ...
mousetail's user avatar
  • 14.4k
10 votes
9 answers
465 views

Given an integer n >= 1 as input, output a sample from the discrete triangular distribution over the integers k, for ...
user1502040's user avatar
  • 4,192
8 votes
1 answer
570 views

Special String We call a binary string \$S\$ of length \$N\$ special if : substring \$S[0:i+1]\$ is lexicographically strictly smaller than substring \$S[i+1:N]\$ for \$ 0\leq i\leq N-2\$, Note: \$ S[...
cheems's user avatar
  • 619
1 vote
1 answer
296 views

I have recently been on a quest to create really really slow sorting algorithms that make Bogosort seem like it is the best. The task is simple: Sort an array of integers in as long average time as ...
Eknoma's user avatar
  • 27
18 votes
3 answers
744 views

Problem A fact you may have noticed about factorials is that as \$n\$ gets larger \$n!\$ will have an increasing number of \$0\$s at the end of it's base \$10\$ representation. In fact this is true ...
Wheat Wizard's user avatar
  • 103k
2 votes
1 answer
1k views

The event scheduler is a recurrent theme in event-driven architectures: something triggers an event that needs to be checked in the future one or more times. I has particular use in trading where you ...
user avatar
10 votes
3 answers
650 views

Consider all arrays of \$\ell\$ non-negative integers in the range \$0,\dots,m\$. Consider all such arrays whose sum is exactly \$s\$. We can list those in lexicographic order and assign an ...
user avatar
9 votes
3 answers
391 views

Much harder than Can this pattern be made with dominoes? Challenge A grid of width \$w\$ and height \$h\$ is given, filled with 1s and 0s. You can place a domino somewhere on the grid only if both ...
Bubbler's user avatar
  • 79.3k
14 votes
10 answers
3k views

The purpose of this challenge is to solve the original first Project Euler problem, but as the title suggests in constant time (with respect to the size of the interval). Find the sum of all the ...
N3buchadnezzar's user avatar
12 votes
0 answers
310 views

Background Hamiltonian circuit problem is a decision problem which asks whether a given graph has a Hamiltonian circuit, i.e. a cycle that visits every vertex exactly once. This problem is one of the ...
Bubbler's user avatar
  • 79.3k
16 votes
6 answers
1k views

The input for the continuous knapsack problem is as follows. For each item 1...n we are given a positive weight and profit. Overall we are also given a positive capacity which is the maximum weight ...
user avatar
19 votes
8 answers
2k views

Given an integer \$ n \$ \$ (n \ge 1) \$, return/output the total number of set bits between \$ 1 \$ and \$ n \$ inclusive. To make the problem more interesting, your solution must run with a time ...
dingledooper's user avatar
  • 23.4k
12 votes
5 answers
609 views

Given a sequence of integers and an integer N, output the number of contiguous subsequences that contain at least N distinct ...
user101295's user avatar
12 votes
6 answers
1k views

Given a non-empty sequence of non-negative integers, output any permutation of it in which no two adjacent elements have a sum that is divisible by 3. If it is impossible, either crash the program, ...
user101295's user avatar
28 votes
3 answers
2k views

Given the following Python "reference implementation" of non-terminating FizzBuzz: ...
Retr0id's user avatar
  • 479
16 votes
4 answers
1k views

The edit distance between two strings is the minimum number of single character insertions, deletions and substitutions needed to transform one string into the other. This task is simply to write ...
user avatar
16 votes
12 answers
770 views

Working on something in probability theory, I stumbled across another combinatorical exercise. These are always fun to solve, searching for intelligent approaches. Of course, one can use brute force ...
Kenji's user avatar
  • 161
-4 votes
2 answers
349 views

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 ...
iirelu's user avatar
  • 15
-6 votes
1 answer
580 views

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 of ...
Bip's user avatar
  • 531
1 vote
0 answers
109 views

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). ...
Vishal Sharma's user avatar
18 votes
1 answer
788 views

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 ...
Lopsy's user avatar
  • 331
3 votes
6 answers
490 views

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 ...
Rushabh Mehta's user avatar
11 votes
1 answer
765 views

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 ...
Rushabh Mehta's user avatar
11 votes
6 answers
792 views

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. ...
user avatar
18 votes
20 answers
3k views

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. ...
user avatar
15 votes
4 answers
560 views

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 ...
user avatar
11 votes
6 answers
617 views

Challenge Let's imagine an N-tuple of integers between 0 and M inclusive, and let's call it ...
Bubbler's user avatar
  • 79.3k
13 votes
15 answers
1k views

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 ...
user avatar
13 votes
14 answers
2k views

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 ...
user avatar
8 votes
9 answers
628 views

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 ...
fred russell's user avatar
9 votes
6 answers
972 views

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 ...
Stewie Griffin's user avatar
3 votes
0 answers
228 views

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 ...
katana_0's user avatar
  • 207
8 votes
4 answers
490 views

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 ...
SEJPM's user avatar
  • 3,463
69 votes
6 answers
5k views

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 the ...
Calvin's Hobbies's user avatar