Questions tagged [array]
A competition to solve a particular problem through the usage and manipulation of arrays.
839 questions
17
votes
18
answers
1k
views
Order a list by the difference of its elements
Task
Given an unsorted list of integers, order it in such a way that the absolute difference of every two adjacent elements will always be equal to 1:
\$|dx| = 1\$
There will be guaranteed one or more ...
8
votes
6
answers
942
views
Counting the sequences
I was thinking when SQL can be less verbose than traditional programming languages and came up with this challenge.
Input
Natural number N
Generating data
We ...
11
votes
11
answers
784
views
Find a linear function such that if f(0), f(1), f(2), ⋯ are indices of the elements of the list, the sum must equal to its slope
Challenge:
Find integers \$a\$ and \$b\$ defining linear function \$f(x) = ax + b\$ with \$a > 0, b \ge 0\$ such that the sum of the elements of the input list \$l\$ located at the values of the ...
17
votes
22
answers
2k
views
Find the first relabeling
I had this problem come up for me recently and I thought it would make a fun code-golf.
You will receive as input a list of positive integers. e.g.
...
16
votes
16
answers
1k
views
Activate the Laser Gates
In a secret lab corridor, there are emitters and gates: A number represents a laser emitter with a certain frequency, a 0 represents an inactive gate. If two ...
13
votes
9
answers
2k
views
Pennies to Dollars
Over on Puzzling a couple years ago, Hermant Agarwal proposed the following question:
In a certain country the following coins are in circulation: 1 cent, 2 cents, 5 cents, 10 cents, 20 cents, 50 ...
18
votes
5
answers
1k
views
Hide from the "left-hand wall" robot
You need to hide in a maze from a robot which methodically follows the left hand wall rule. That is, it enters, immediately turns left, and will always move keeping its left hand on the wall. It only ...
15
votes
12
answers
781
views
Slender sub-lists
A somewhat interesting task came up for me recently which I thought would make a fun code-golf.
Given a number \$n\$ and a list of positive integers less than or equal to \$n\$, we call a sublist ...
5
votes
5
answers
364
views
Calculate the Squadrat "yard"
Write a program/function to calculate the squadrat "yard" from a collection of squadrats.
Squadrats is a platform that gamifies outdoor activities by recording which large (~1.6 km square) ...
8
votes
12
answers
679
views
The Array Walker (part 2)
The Array Walker (part 2)
Input: A non-empty array of integers (without nesting1). This can have negative values!
Output: A boolean value2 indicating whether we can reach the end of the array when ...
9
votes
4
answers
663
views
Triangular Transposition Cipher
A text that can be arranged triangularly in some fashion can be read back in some other fashion effectively enciphering it.
Narrowing down a set of plausible triangular numberings allows to ...
8
votes
23
answers
947
views
Can we escape from array?
Can we escape from the array?
Input: A non-empty array of non-negative integers (without nesting1).
Output: A boolean value2 indicating whether we can reach the end of the array when starting from the ...
13
votes
7
answers
980
views
Traverse the list
Update 2025-06-04: Thanks for the submissions everyone! It seems like a lot of the solutions are quite slow / timeout for the 100 length test case, so I don't think any will be able to do the 10k. ...
10
votes
16
answers
881
views
Maximum contiguous subarray average
Take an array of integers and an integer K. For all contiguous subarrays of length at least K, return the maximum average value (sum of all elements in the subarray divided by length of subarray, ...
10
votes
6
answers
840
views
Optimal Assembly Jump Distance
In some forms of assembly, there are separate "short jump" and "long jump" instructions. However, these jumps can themselves have different lengths, which can affect the distance ...
7
votes
8
answers
974
views
Implement PHMINPOSUW (Packed Horizontal Word Minimum)
PHMINPOSUW (Packed Horizontal Word Minimum) is a specialized x86 instruction that finds the minimum value and location of a 16-bit unsigned integer in an 8-element vector packed into a 128-bit ...
4
votes
1
answer
287
views
Find the factor to use for shortest simple Brainfuck data initialization loop
I like writing answers using Brainfuck, since this language basically simulates a Turing machine, and it is pretty challenging to find the shortest answer for any problem, since there are a lot of ...
12
votes
9
answers
1k
views
All possible swaps for the permutation
Given a permutation of {1,2,...,n} named A, do each swap of form swap A[i], A[j] exactly once, where ...
13
votes
9
answers
863
views
Output the symmetric inverse semigroup
The symmetric inverse semigroup is a very important object in the study of semigroups, for a number of reasons, but most obviously due to the Wagner-Preston theorem. In brief, for any set \$X\$, the ...
7
votes
4
answers
1k
views
Tips for golfing in array languages
We already have a few language-specific tips threads for languages which fall under the array-language umbrella, but there are many golfing tricks which are more or less universal among these ...
15
votes
14
answers
2k
views
Max Island Area
You are given a two-dimensional array consisting of 0s and 1s. The number of rows and columns can vary. For example, 10 by 20.
0 represents water.
1 represents land.
Your task is to find the area of ...
12
votes
13
answers
1k
views
Determine the area of biggest rectangle containing exactly one "X"
Your input is a rectangular 2D char array, such as:
.X.......X
..........
.....X....
..X.......
........X.
.X........
..X.....X.
X.........
....X.....
Your goal is ...
11
votes
13
answers
1k
views
Longest bitonic subarray
Solve this problem in the fewest number of bytes of code possible.
We have a data variable that contains user input data.
Data is a list of integers.
Write the code that finds the longest bitonic ...
12
votes
22
answers
1k
views
Find all unique quintuplets in an array that sum to a given target
Inspiration: Leetcode's [3Sum] link
Problem
Given an array nums of n (not necessarily distinct) integers, and given a target ...
37
votes
17
answers
2k
views
In what order will the downloads complete?
Background
Recently, I was installing updates on my pc with Pacman (I use arch btw) and noticed the order it downloaded files was like this
Start with the largest 4 downloads
When a download ...
21
votes
17
answers
2k
views
Building Skyscrapers
You work as an architect, and you've been assigned the task of reviewing your coworker's skyscraper design. In your line of work, a skyscraper is just a tower of blocks. Nothing more, nothing less.
...
9
votes
48
answers
2k
views
Calculate sum of self-exponentation
Given an array of positive integers, calculate the sum of each number raised to its own power. For example, given 4,6,7, the code needs to return \$4^4+6^6+7^7=...
14
votes
5
answers
539
views
Generate a subgroup of a free group
In group theory, the free group with \$n\$ generators can be obtained by taking \$n\$ distinct symbols (let's call them \$a, b, c ...\$ etc), along with their inverses \$ a^{-1},b^{-1},c^{-1} ...\$ . ...
28
votes
16
answers
2k
views
Is it a cartesian product?
The cartesian product of two multisets \$A\$ and \$B\$ is the multiset of all ordered pairs consisting of an element of \$A\$ and an element of \$B\$. For example, the cartesian product of \$\{1, 2, 7,...
28
votes
23
answers
2k
views
Weave two lists, cycling if necessary
Consider two lists, A and B. To weave them together, we take the first element of A, then the first element of B, then the next element of A, the next element of B, and so on, ending with the last ...
24
votes
19
answers
2k
views
Emulate Jelly's tie-scan
The golfing language Jelly has a built-in ƭ called "tie", that cycles through a list of functions every time it's called. For example, ...
1
vote
7
answers
205
views
Interlaced array [duplicate]
You are given an array A of length N and an array B of length ...
16
votes
15
answers
1k
views
Reconstruct a list of strings from its prefixes
emanresu A posted a challenge to reconstruct an ordered list of integers from its unordered set of unordered prefixes. Many users (beginning with xnor's answer) noticed that summing each unordered ...
18
votes
18
answers
1k
views
Reconstruct a list from its prefixes
A list \$[a_1,a_2,a_3 \cdots a_n]\$ can be uniquely represented as an unordered list of its prefixes - \$ [[a_1],[a_1, a_2], [a_1,a_2,a_3] \cdots [a_1,a_2,a_3 \cdots a_n]] \$. This can be in any order ...
12
votes
7
answers
2k
views
Contract a tensor
Introduction
Tensor contraction is an operation that can be performed on a tensor. It is a generalization of the idea of the trace of a matrix. For example, if we have a rank-2 tensor (a matrix) and ...
20
votes
12
answers
2k
views
Search the deepest depths of an array
Given a ragged array, find the length of the largest subarray that contains the depth of that subarray. The base array layer has a depth of 0.
Here is an example:
...
26
votes
23
answers
2k
views
Remove falsy rows and columns
Your challenge is to, given a matrix of nonnegative integers, remove all rows and columns that contain a 0.
For example, with this matrix:
...
20
votes
27
answers
3k
views
What and where is the max?
This small challenge derives from my annoyance at discovering that numpy doesn't have a built in function for it.
Input
An n by m matrix of integers.
Output
The maximum value in the matrix and the 2d ...
7
votes
4
answers
449
views
Busy Letters & Evil CEOs
Imagine you are given an array/string containing \$5\$ letters of the English alphabet, with each letter having any value from A to Z (inclusive).
Every day, each letter will perform a job, which can ...
25
votes
15
answers
4k
views
"Perfect" an array
Given an n non-negative integers, how many numbers must be changed so that each number x appears x times? You cannot add or remove elements, only replace them with other numbers.
For example:
...
10
votes
6
answers
1k
views
Unnecessary Fluff
Following the great advice (what do you mean it's not advice?!) on Adding unnecessary fluff we can devise the following task:
Take a list of positive integers and a positive integer \$m\$ as input.
...
5
votes
4
answers
316
views
Is it stuck in a counting loop? [duplicate]
Given a list of non-negative integers the function \$f\$ replaces every integer with the number of identical integers preceding it (not necessarily contiguously). So
...
11
votes
8
answers
1k
views
Night hike partitioning
We want to go on a night hike with the youth group, but of course not everyone has their torch, even though we told them we planned to split up. What options are there for group formation if n teens ...
16
votes
7
answers
1k
views
One-dimensional map colouring
It is a well-known fact that you can colour a two-dimensional map with four colours in such a way that two countries with a common border always have different colours. There have already been enough ...
19
votes
24
answers
3k
views
Just another traffic jam!
One lane is closed, the vehicles have to thread their way in according to the zip-lock system: One car from one lane, the next car from the other lane, always taking turns.
Input: Two vehicle queues, ...
16
votes
16
answers
4k
views
Which skill to train?
Story (skip, if you prefer the naked task): You need five skills for an imaginary sport: Speed, strength, endurance, accuracy and tactics. If you achieve a score in each of these disciplines, you can ...
7
votes
2
answers
365
views
How many twisted corners?
Your challenge is to output the number of twisted corners given a 2x2 Rubik's Cube scramble.
For the purpose of this challenge, twisted corners are defined as corners where the colour of the top/...
14
votes
9
answers
2k
views
Seat gangs as far as possible
Imagine there are \$n\$ people \$\{a_1, a_2, \ldots, a_n\}\$ who enter a room in order and sit down in \$n\$ seats, arranged in a row. Each of these people belong to some gang, indicated by an integer ...
30
votes
20
answers
4k
views
How many umbrellas to cover the beach?
You work at a beach. In the afternoon, the sun gets quite hot and beachgoers want to be shaded. So you put out umbrellas. When you put out umbrellas you want to shade the entire beach, with as few ...
16
votes
9
answers
2k
views
Transpose a multidimensional array
Transposition is an operation on 2-dimensional arrays that flips every element across the main diagonal:
...