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

Questions tagged [array]

A competition to solve a particular problem through the usage and manipulation of arrays.

Filter by
Sorted by
Tagged with
17 votes
18 answers
1k views

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 ...
Glory2Ukraine's user avatar
8 votes
6 answers
942 views

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 ...
Dr Y Wit's user avatar
  • 519
11 votes
11 answers
784 views

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 ...
Fmbalbuena's user avatar
  • 5,085
17 votes
22 answers
2k views

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. ...
Wheat Wizard's user avatar
  • 103k
16 votes
16 answers
1k views

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 ...
turalson's user avatar
  • 581
13 votes
9 answers
2k views

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 ...
caird coinheringaahing's user avatar
18 votes
5 answers
1k views

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 ...
Steve Bennett's user avatar
15 votes
12 answers
781 views

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 ...
Wheat Wizard's user avatar
  • 103k
5 votes
5 answers
364 views

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) ...
Steve Bennett's user avatar
8 votes
12 answers
679 views

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 ...
QOO-OOKALAN's user avatar
9 votes
4 answers
663 views

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 ...
Domenico's user avatar
  • 2,463
8 votes
23 answers
947 views

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 ...
QOO-OOKALAN's user avatar
13 votes
7 answers
980 views

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. ...
Ted's user avatar
  • 2,417
10 votes
16 answers
881 views

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, ...
Redz's user avatar
  • 629
10 votes
6 answers
840 views

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 ...
mousetail's user avatar
  • 14.4k
7 votes
8 answers
974 views

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 ...
bigyihsuan's user avatar
  • 11.4k
4 votes
1 answer
287 views

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 ...
Weird Glyphs's user avatar
12 votes
9 answers
1k views

Given a permutation of {1,2,...,n} named A, do each swap of form swap A[i], A[j] exactly once, where ...
l4m2's user avatar
  • 32.4k
13 votes
9 answers
863 views

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 ...
caird coinheringaahing's user avatar
7 votes
4 answers
1k views

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 ...
noodle person's user avatar
15 votes
14 answers
2k views

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 ...
Pavel's user avatar
  • 475
12 votes
13 answers
1k views

Your input is a rectangular 2D char array, such as: .X.......X .......... .....X.... ..X....... ........X. .X........ ..X.....X. X......... ....X..... Your goal is ...
Ben Stokman's user avatar
11 votes
13 answers
1k views

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 ...
Pavel's user avatar
  • 475
12 votes
22 answers
1k views

Inspiration: Leetcode's [3Sum] link Problem Given an array nums of n (not necessarily distinct) integers, and given a target ...
CrSb0001's user avatar
  • 879
37 votes
17 answers
2k views

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 ...
mousetail's user avatar
  • 14.4k
21 votes
17 answers
2k views

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. ...
Trivaxy's user avatar
  • 697
9 votes
48 answers
2k views

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=...
Andy Liu's user avatar
  • 251
14 votes
5 answers
539 views

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} ...\$ . ...
emanresu A's user avatar
  • 46.2k
28 votes
16 answers
2k views

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,...
emanresu A's user avatar
  • 46.2k
28 votes
23 answers
2k views

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 ...
DLosc's user avatar
  • 40.7k
24 votes
19 answers
2k views

The golfing language Jelly has a built-in ƭ called "tie", that cycles through a list of functions every time it's called. For example, ...
emanresu A's user avatar
  • 46.2k
1 vote
7 answers
205 views

You are given an array A of length N and an array B of length ...
Daniil Tutubalin's user avatar
16 votes
15 answers
1k views

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 ...
Greg Martin's user avatar
  • 16.7k
18 votes
18 answers
1k views

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 ...
emanresu A's user avatar
  • 46.2k
12 votes
7 answers
2k views

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 ...
Tbw's user avatar
  • 3,023
20 votes
12 answers
2k views

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: ...
Alan Bagel's user avatar
  • 4,269
26 votes
23 answers
2k views

Your challenge is to, given a matrix of nonnegative integers, remove all rows and columns that contain a 0. For example, with this matrix: ...
emanresu A's user avatar
  • 46.2k
20 votes
27 answers
3k views

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 ...
Simd's user avatar
  • 3,167
7 votes
4 answers
449 views

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 ...
Trivaxy's user avatar
  • 697
25 votes
15 answers
4k views

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: ...
Alan Bagel's user avatar
  • 4,269
10 votes
6 answers
1k views

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. ...
Command Master's user avatar
5 votes
4 answers
316 views

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 ...
Wheat Wizard's user avatar
  • 103k
11 votes
8 answers
1k views

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 ...
Philippos's user avatar
  • 2,688
16 votes
7 answers
1k views

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 ...
Philippos's user avatar
  • 2,688
19 votes
24 answers
3k views

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, ...
Philippos's user avatar
  • 2,688
16 votes
16 answers
4k views

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 ...
Philippos's user avatar
  • 2,688
7 votes
2 answers
365 views

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/...
math scat's user avatar
  • 9,528
14 votes
9 answers
2k views

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 ...
97.100.97.109's user avatar
30 votes
20 answers
4k views

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 ...
Wheat Wizard's user avatar
  • 103k
16 votes
9 answers
2k views

Transposition is an operation on 2-dimensional arrays that flips every element across the main diagonal: ...
emanresu A's user avatar
  • 46.2k

1
2 3 4 5
17