Questions tagged [array]

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

Filter by
Sorted by
Tagged with
109 votes
18 answers
6k views

Xorting an array

Conceptually, this challenge is really simple. You're given a list of non-negative integers ai. If possible, find a non-negative integer ...
Martin Ender's user avatar
101 votes
51 answers
8k views

2048-like array shift

Assume we want to shift an array like it is done in the 2048 game: if we have two equal consecutive elements in array, merge them into twice the value element. Shift must return a new array, where ...
greenwolf's user avatar
  • 1,051
68 votes
61 answers
7k views

N-dimensional N^N array filled with N

In: Enough memory and a positive integer N Out: N-dimensional N^N array filled with N, where N^N means N terms of N-by-N-by-N-by... Examples: 1: [1] ...
Adám's user avatar
  • 29.9k
68 votes
69 answers
10k views

Lossy Sorting (Implement Dropsort)

Dropsort, designed by David Morgan-Mar, is an example of a linear-time "sorting algorithm" that produces a list that is, in fact, sorted, but contains only some of the original elements. Any ...
SuperJedi224's user avatar
  • 11.9k
67 votes
62 answers
4k views

Count trailing truths

Inspired by, and in memory of, my dear friend and colleague, Dan Baronet, 1956 – 2016. R.I.P. He found the shortest possible APL solution to this task: Task Given a Boolean list, count the number of ...
Adám's user avatar
  • 29.9k
64 votes
51 answers
6k views

The forbidden built-in

In the standard loopholes, the following is forbidden: Claiming that your answer is written in "MyOwnLanguage", where the command x means "read a sequence of ...
Leaky Nun's user avatar
  • 49.6k
59 votes
75 answers
5k views

Be the First 1 (leave only the first Truthy)

Intro Every year, Dyalog Ltd. holds a student competition. The challenge there is to write good APL code. This is a language agnostic code-golf edition of this year's eighth problem. I have explicit ...
Adám's user avatar
  • 29.9k
59 votes
57 answers
6k views

Increment an Array

Given a nonempty array of positive integers, "increment" it once as follows: If all the array elements are equal, append a 1 to the end of the array. For example: ...
Calvin's Hobbies's user avatar
59 votes
18 answers
4k views

There are two new sheriffs in town – Identifying DJMcMego pairs!

We have some new sheriffs moderators in town, Mego and DJMcMayhem. We need a challenge to properly honour them for their new positions, so there we go. Here's something that has caught my attention ...
Mr. Xcoder's user avatar
  • 42.3k
53 votes
11 answers
3k views

Surrounded Countries

Countries own a series of territories on a 1D world. Each country is uniquely identified by a number. Ownership of the territories can be represented by a list as follows: ...
Sisyphus's user avatar
  • 14.7k
52 votes
46 answers
5k views

Detect heat waves

Background The Royal Netherlands Meteorological Institute defines a heat wave* as a series of at least 5 consecutive days of ≥25°C weather (“summery weather”), such that at least 3 of those days are ≥...
Lynn's user avatar
  • 68.1k
51 votes
31 answers
9k views

Covering a Skyline with brush strokes

Given a non-negative integer skyline height list, answer how many uninterrupted 1-unit-high horizontal brush strokes are needed to cover it. [1,3,2,1,2,1,5,3,3,4,2]...
Adám's user avatar
  • 29.9k
50 votes
61 answers
3k views

Cover up zeroes in a list

Inspired by this SO question As input you will be given a non-empty list of integers, where the first value is guaranteed to be non-zero. To construct the output, walk from the start of the list, ...
FryAmTheEggman's user avatar
50 votes
41 answers
3k views

Moving modest minimum

Inspired by a question over at Stack Overflow. The title here is entirely my fault. The challenge Given a list of positive integers containing at least two entries, replace each number by the ...
Luis Mendo's user avatar
  • 104k
49 votes
67 answers
7k views

Find the first duplicated element

Given an array a that contains only numbers in the range from 1 to a.length, find the first duplicate number for which the second occurrence has the minimal index. In other words, if there are more ...
Jeanderson Candido's user avatar
49 votes
40 answers
3k views

At least h with at least h

Input A list of nonnegative integers. Output The largest nonnegative integer h such that at least h of the numbers in the ...
Zgarb's user avatar
  • 42.5k
49 votes
38 answers
5k views

Duck, Duck, Josephus

Given an Integer array: Start from the first number Jump forward n positions where n is the value of the current position Delete the current position, making what was the next position the current ...
workoverflow's user avatar
  • 1,035
48 votes
85 answers
8k views

Implementing a stack

I can't believe we don't have this already.. It's one of the most important data-structures in programming, yet still simple enough to implement it in a code-golf: Challenge Your task is to ...
ბიმო's user avatar
  • 16.7k
48 votes
84 answers
6k views

The shortest way to find one unique value when all other values are the same

In this question at Code Review they tried to find the fastest way to return the unique element in an array where all the elements are the same except one. But what is the shortest code that ...
Charlie's user avatar
  • 12.8k
48 votes
40 answers
10k views

Weapons of Math Instruction

Last time when I tried to come up with something easy that wasn't a duplicate, it ended up being way too hard.. So hopefully this time it's indeed something newcomers can try as well. Input: An ...
Kevin Cruijssen's user avatar
48 votes
25 answers
6k views

First code golf decathlon [closed]

Tasks All competitors try to solve the following list of 10 tasks: math Read a positive integer n from input and return the sum of the cubes of the first n non-negative integers. For input ...
Dennis's user avatar
  • 210k
48 votes
20 answers
6k views

Into how many pieces can you cut this string?

Consider a piece of string (as in "rope", not as in "a bunch of characters"), which is folded back and forth on the real line. We can describe the shape of the string with a list of points it passes ...
Martin Ender's user avatar
48 votes
16 answers
6k views

Zigzagify a Matrix

As part of its compression algorithm, the JPEG standard unrolls a matrix into a vector along antidiagonals of alternating direction: Your task is to take a matrix (not necessarily square) and return ...
Martin Ender's user avatar
47 votes
69 answers
8k views

Am I an insignificant array?

An insignificant array is an array of positive integers, where the absolute differences between consecutive elements are all smaller than or equal to 1. For example, the following array is ...
user avatar
47 votes
30 answers
3k views

Sever-sort an array

Challenge Given a non-empty array of integers, e.g.: [5, 2, 7, 6, 4, 1, 3] First sever it into arrays where no item is larger than the previous (i.e. non-...
ETHproductions's user avatar
45 votes
23 answers
7k views

Unflatten an Array

This challenge was inspired by a question on Mathematica.SE. Say you've got a nested list/array of some arbitrary structure (the lists at each level don't necessarily have the same length). For ...
Martin Ender's user avatar
44 votes
26 answers
5k views

Remove every N-th N

The task In this challenge, your input is a non-empty list of positive integers, given in the native format of your language. Your output is that same list, in the same format, with some elements ...
Zgarb's user avatar
  • 42.5k
44 votes
16 answers
5k views

Implement the "Lazy Sort"

I'm supposed to sort a list of numbers, but I'm super lazy. It's really hard to figure how to swap all the numbers around until all of them are in increasing order, so I came up with my own algorithm ...
DJMcMayhem's user avatar
  • 58.9k
43 votes
44 answers
4k views

An Array of Challenges #1: Alternating Arrays

Alternating Arrays An alternating array is a list of any length in which two (not necessarily different) values are alternating. That is to say, all even-indexed items are equal, and all odd-indexed ...
FlipTack's user avatar
  • 14.6k
43 votes
56 answers
7k views

In Search of a Soulmate

Given a nonempty finite list of integers, output a truthy value if there are exactly two equal entries and all other entries are distinct, and a falsey value otherwise. Examples ...
flawr's user avatar
  • 43.7k
42 votes
39 answers
3k views

Hot moo shuffle: milk an array

Background Milking the deck is the name given to the following card shuffling method: 'Pinch' the deck to remove the top and bottom cards simultaneously. (With lots of imagination, this action ...
Dingus's user avatar
  • 10.9k
42 votes
48 answers
6k views

Is it true? Ask Jelly!

Background Inspired by Octave's (and, by extension, MATL's) very convenient interpretation of truthy/falsy matrices, Jelly got the Ȧ (Octave-style all) atom. Ȧ takes an array as input and returns 1 ...
Dennis's user avatar
  • 210k
42 votes
68 answers
4k views

Difference of the square of the sum

Find the difference between the square of the sums and sum of the squares. This is the mathematical representation: \$\left(\sum n\right)^2-\sum n^2\$ Your program/method should take two inputs, ...
george's user avatar
  • 1,792
42 votes
45 answers
4k views

Skip like a rabbit!

Given a list of non-negative integers in any reasonable format, iterate over it, skipping as many elements as every integer you step on says. Here is a worked example: ...
Erik the Outgolfer's user avatar
42 votes
17 answers
5k views

Recursive Stalin Sort [duplicate]

There is a "sorting" algorithm often called Stalin Sort, where instead of sorting an array, you just remove any items that are out of order. In this challenge, you'll implement part of a ...
Rydwolf Programs's user avatar
41 votes
67 answers
4k views

Consolidate an Array

The task is simple: consolidate an array of ints. Consolidating this array consists of the following: All instances of 0 need to be moved to the end of the array. There should be no 0s between the ...
Addison Crump's user avatar
41 votes
34 answers
13k views

Cheating a multiple choice test

Introduction While studying, I tried to come up with several ways to cheat a multiple choice test. It basically is a compressed version of the multiple choice answers. The method goes as following: ...
Adnan's user avatar
  • 44.3k
41 votes
62 answers
9k views

Remove leading and trailing zeroes

Given a non-empty list/array containing only non-negative integers like this: [0, 0, 0, 8, 1, 4, 3, 5, 6, 4, 1, 2, 0, 0, 0, 0] Output the list with trailing and ...
Lamaro's user avatar
  • 939
41 votes
33 answers
4k views

Remove surrounding zeroes of a 2d array

This is a 2-dimensional version of this question. Given a non-empty 2-dimensional array/matrix containing only non-negative integers: $$ \begin{bmatrix} {\color{Red}0} & {\color{Red}0} & {\...
alephalpha's user avatar
  • 46.5k
40 votes
40 answers
5k views

Is there a bump?

Given a list of positive integers determine if there is an element that is either greater than its two neighbors or less than its two neighbors (a "bump"). To be clear a bump can never be the first ...
Wheat Wizard's user avatar
  • 96.5k
40 votes
32 answers
3k views

Print a physical multiplication table

Rectangles have this nice property - an \$n \times m\$ rectangle consists of exactly \$n \times m\$ characters! A.. more interesting property is that the rectangles can be aligned nicely in a ...
dzaima's user avatar
  • 20.2k
40 votes
17 answers
1k views

An Array of Challenges #2: Separate a Nested Array

Note: This is #2 in a series of array challenges. For the previous challenge, click here. Separating Nested Lists To separate values in a nested list, flatten it, and then wrap each value so it is at ...
FlipTack's user avatar
  • 14.6k
40 votes
16 answers
2k views

Zero sum covers

Introduction Consider a nonempty list L of integers. A zero-sum slice of L is a contiguous subsequence of L whose sum equals 0. For example, [1, -3, 2] is a zero-sum slice of [-2, 4, 1, -3, 2, 2, -1, ...
Zgarb's user avatar
  • 42.5k
40 votes
17 answers
4k views

Look, up in the sky! It's a super duper array!

Inspired by this question from our rivals friends over at Code Review. Definitions A super array is an array where each new element in the array is larger than the sum of all the previous elements. <...
DJMcMayhem's user avatar
  • 58.9k
40 votes
13 answers
5k views

Recreate Minecraft's lighting

Minecraft has a fairly unique lighting system. Each block's light value is either one less than the brightest one surrounding it, or it is a light source itself. Your task is to write a method that ...
Chspsa's user avatar
  • 691
39 votes
77 answers
6k views

P Pr Pre Pref Prefi Prefix Prefixe Prefixes

Given some finite list, return a list of all its prefixes, including an empty list, in ascending order of their length. (Basically implementing the Haskell function ...
flawr's user avatar
  • 43.7k
39 votes
53 answers
11k views

Determine the depth of an array

A simple challenge for your Monday evening (well, or Tuesday morning in the other half of the world...) You're given as input a nested, potentially ragged array of positive integers: ...
Martin Ender's user avatar
39 votes
54 answers
4k views

Boustrophedonise

Related but very different. A boustrophedon is a text where every other line of writing is flipped or reversed, with reversed letters. In this challenge, we will just reverse every other line, but ...
Adám's user avatar
  • 29.9k
39 votes
58 answers
3k views

Fold a List in Half

We are going to fold a list of integers. The procedure to do so is as follows, If the list is of even length, make a list of half of its length where the nth item of the new list is the sum of the ...
Wheat Wizard's user avatar
  • 96.5k
39 votes
22 answers
1k views

Array alignment addition

Introduction Consider two non-empty integer arrays, say A = [0 3 2 2 8 4] and B = [7 8 7 2]. To perform alignment addition on them, we do the following: Repeat each array enough times to have total ...
Zgarb's user avatar
  • 42.5k

1
2 3 4 5
16