Questions tagged [array]

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

Filter by
Sorted by
Tagged with
23
votes
31answers
1k views
+100

Even sum subarrays

Given an array of integers, count the number of contiguous subarrays with an even sum. You may assume that the array is non-empty, and contains only non-negative integers. This is code-golf, so the ...
14
votes
9answers
1k views

Convert Regex to Mask

From my CMC. Given a regex and a non-empty printable ASCII text, return one bit per character in the text, indicating the positions of beginnings of non-overlapping matches, and also the positions of ...
2
votes
2answers
87 views

Tips for parsing Standard Input in JavaScript

I've been doing quite a few code golf recently and I realised that a lot of times, I have to do something involving list. Most of the times lists are space-separated or comma-separated values in the ...
14
votes
22answers
2k views

Ways to add 1 to lists of lists

Adapted from this StackOverflow question In this challenge you will take a list of lists of integers, e.g. A = [[1,2],[3,4],[5],[]] And an additional single ...
26
votes
18answers
2k views

Floating Point indexOf

Given a sorted array of unique positive integers \$A\$ \$(A_0<A_1<\cdots<A_{n-1})\$, and an integer \$t\$, where \$A_0\le t\le A_{n-1}\$. Output the value \$i\$ such that: If \$t \in A\$, \$...
21
votes
59answers
4k views

Swap every two elements in a list

Challenge Let's have a list L of n elements. The task is to swap every two elements in this list. Constrains the list ...
19
votes
13answers
1k views

Compute matrix blockiness

Consider a non-empty binary matrix M and a natural number n. For the purposes of this challenge, M is said to have blockiness n if it can be built using adjacent square blocks of size n, where each ...
14
votes
20answers
1k views

Rotate N Values in a List

Similar to this. Input a list and rotate N numbers in it given their indices. The following example uses 1-indexing, but feel free to use either. In other words, take a list of indices and a list of ...
16
votes
10answers
755 views

Remove submatrices

Given two integer matrices a and b, your challenge is to replace any occurences of b in <...
20
votes
9answers
2k views

Bird's Algorithm for Computing Determinants

There is a division-free algorithm for computing determinants published by R.S.Bird in 2011 that uses only matrix multiplications. Given a \$n×n\$ matrix \$X\$, the matrix \$Y=μ(X)\$ is another \$n×n\$...
22
votes
17answers
2k views

Double trace of a square matrix

Inspired by a question (now closed) at Stack Overflow. Given a square matrix, let its double trace be defined as the sum of the entries from its main diagonal and its anti-diagonal. These are marked ...
25
votes
42answers
2k views

Swap Two Values in a List

Introduction: Although we have a lot of challenges where swapping two items in a list is a subtask, like Single swaps of an array; Swap to Sort an Array; \$n\$ swaps into a nop; etc., we don't have ...
8
votes
2answers
578 views

Follow the border

Inspired by Is this Flow Free puzzle trivial? by @Bubbler. Lengthy chunks of this challenge are borrowed from there. This may be one step of a solution for the linked challenge, depending on chosen ...
14
votes
10answers
904 views

Revert Flow Free puzzle

Inspired by Is this Flow Free puzzle trivial? by @Bubbler. Lengthy chunks of this challenge are borrowed from there. This may be one step of a solution for the linked challenge, depending on chosen ...
41
votes
17answers
5k views

Recursive Stalin Sort

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 ...
14
votes
2answers
543 views

Is this Flow Free puzzle trivial?

Background Flow Free is a series of puzzle games whose objective is to connect all the same-colored pairs of dots on the grid. In this challenge, we consider the original game on a rectangular grid (...
5
votes
6answers
182 views

"Fold" the array [duplicate]

An example will easily explain this to you. Suppose an array [1,2,3,4,5] Step 1 1 2 3 4 5 ----+---- Step 2 ...
11
votes
15answers
2k views

Minimizing flights of stairs climbed

Challenge Given input as a list/array of natural numbers that each represent the floor of a class a student has to go to, sort the numbers in a way such that the MAD (mean average deviation) number of ...
13
votes
11answers
3k views

Simulate my pill box

I have a small pill box of \$n\$ slots. (Think of it as a linear one like the image below, but not necessarily having 7 slots.) Each slot has at most one pill. Every day, I take one of two actions: ...
9
votes
9answers
444 views

\$n\$ swaps into a nop

Given \$n, m\ge 1\$. There are \$n\$ balls. Do \$m\$ moves, each choose a ball (maybe also the first one), and exchange with the one currently on the first place. At the end, the order of balls should ...
21
votes
24answers
3k views

Apply gravity to this matrix

This was inspired by this question. Given an \$m\times n\$ matrix of \$0\$'s and \$1\$'s, apply "gravity" to it. This means to drop down all the \$1\$'s as if they were affected by gravity. ...
10
votes
7answers
2k views

Game of Life, but on a 4-8-8 tiling

Background The 4-8-8 tiling looks like this: For the purpose of this challenge, we take the orientation of the tiling as exactly shown above. In plain English words, we take the tiling so that it can ...
7
votes
4answers
257 views

So is that maximum?

Given a map from arrays of integers of same size to single integers, determine if there is a set of indices such that the output is equal to the maximum of the elements at those indices for every ...
4
votes
0answers
120 views

How many fill buckets to blank? [duplicate]

The fill bucket is a tool available in many drawing tools which replaces a contiguous chunk of one color with a another color. We are going to define a fill bucket which operates on lists of non-...
12
votes
2answers
253 views

Draw constraint system #18762389

Background Page 219 of A New Kind of Science (a book by Stephen Wolfram, the creator of Mathematica) shows an interesting 2D pattern generated by constraints. The relevant section in the book starts ...
24
votes
31answers
3k views

"-rot" transform

Background -rot transform (read as "minus-rot transform") is a sequence transformation I just invented. This transform is done by viewing the sequence as a stack in Forth or Factor (first ...
19
votes
20answers
2k views

Hunt for discount

Story My local pizza delivery introduced new discount. You get 50% discount from every second item on your order. But being greedy capitalists, they forgot to mention that they will rearrange items ...
23
votes
17answers
2k views

Show me the trajectory

Given a list of integers find the "trajectory" that results from indefinitely moving the instructed steps to the right (left if negative), wrapping if necessary, starting at the first ...
9
votes
7answers
738 views

Reverse cadaddadadaddddaddddddr [closed]

The Lisp language has a family of functions car, cdr, cadr, etc for accessing arrays. For ...
23
votes
26answers
1k views

Split into sublists of specified sizes

05AB1E has the £ builtin which, given a list of integers b and a string s, splits ...
18
votes
19answers
5k views

What's my PIN number?

My PIN number is 1077, but that's too difficult to remember. I know from muscle memory that it's a digit, followed by a different digit, then followed by two of the same digit, which is different to ...
21
votes
18answers
4k views

Build me a room

Related A room, in the context of this challenge, is a multidimensional array where the elements on the "outside" are 1, to represent the walls, and all ...
6
votes
6answers
497 views

Group binary shapes together

Consider the following binary matrix: $$\begin{matrix} 1 & 0 & 1 \\ 1 & 0 & 0 \\ 0 & 1 & 1 \end{matrix}$$ The (1-indexed) co-ordinates of the \$1\$s here are \$(1,1), (1,3), (2,...
22
votes
22answers
3k views

Longest Zero Sum Sub-array

Given an array of integers. Find out its longest sub-array (contiguous subsequence) whose sum is 0. The sub-array for output may be an empty array. Input Input an array of integers. Output Output the ...
6
votes
6answers
377 views

Blinking Lighthouses – Founder's Day Upgrade

To celebrate the island's next Founder's Day, colored filters (fuchsia, chartreuse, and aquamarine*) have been installed in every lighthouse. All lighthouses begin operation with their fuchsia filter,...
5
votes
3answers
149 views

Is this an ordinal transform? [duplicate]

Related: What's my telephone number? which asks to calculate the terms of A000085, the number of possible ordinal transforms of length n. Background Ordinal transform is a transformation on an integer ...
23
votes
8answers
2k views

Blinking Lighthouses

Lighthouses on an island have blink codes that let ships know which lighthouse they are seeing. Input should be strings of S and L. Here is a (Python) example of codes for 4 lighthouses as a list of ...
29
votes
26answers
2k views

Recursive Z-matrix

Originally from a CMC I proposed for the last BMG event Challenge Given a non-negative integer \$n\$, create a 2D array of size \$2^n × 2^n\$ which is generated in the following manner: Divide the ...
14
votes
10answers
1k views

How many bead arrangements on the abacus?

Your toy in this challenge is a special abacus of 4 rows and 8 positions per row. There's one bead on the first row, 2 beads on the 2nd row, 3 beads on the 3rd row and 4 beads on the 4th row. Beads on ...
1
vote
1answer
175 views

No of subsets of a given array such that their product is in the form of p1*p2*p3 [closed]

Given an array \$A\$ of size \$n\$. You have to find the number of subsets such that their product is in the form of \$p_1 \times p_2 \times p_3 \dots\$ where \$p_1, p_2, p_3, \dots\$ are prime ...
29
votes
40answers
3k views

Is this a Permutation of 1..n

Input a non-empty array with \$n\$ positive integers. Test if the input array contains every integer in \$1\cdots n\$. In case you prefer 0-indexed numbers, you may choose to input an array of non-...
1
vote
0answers
54 views

Reverse a sentence word by word [duplicate]

The setting "Well I actually wrote the sql library they, the maintainers actually.." "Right, but do you have any experience working with django?" " ...no I don't use ...
30
votes
12answers
2k views

Is it a checkered tiling?

Background A checkered tiling of a rectangular grid is a tiling using some polyominoes, where each region can be colored either black or white so that no two polyominoes sharing an edge has the same ...
14
votes
23answers
2k views

What's missing (aka the vanilla multiset difference challenge)

Write a program which takes two arrays of positive integers as input. One of them (your choice which) will have one item missing compared to the other. Your program must figure out what is missing. ...
27
votes
22answers
1k views

Consecutive Distance Rating

We'll call the consecutive distance rating of an integer sequence the sum of the distances between consecutive integers. Consider 2 9 3 6 8 1. ...
13
votes
7answers
1k views

A Cat’s Game to Claim

Intro You like cats. Naturally, you like cat’s games in tic-tac-toe. So, you’ve come up with a little party trick. You ask someone what square on the board they want you to make a move in. And you ask ...
18
votes
8answers
1k views

Generalised multi-dimensional chess knight's moves

Multi-dimensional chess is an extension of normal chess that is played on an 8x8x8x8... "board". In normal 2D chess, a knight's move is a movement by a vector of \$ \begin{bmatrix} \pm 2 \\ \...
31
votes
48answers
2k views

Split a list at the second occurrence of the first element

Why should you golf in Haskell? Find out here. In that answer, Zgarb defines a task: Let's define a function f that splits a list at the second occurrence of the ...
17
votes
14answers
991 views

Smallest slice of array containing a number of items

Take this array: [1, 2, 6, 4, 4, 1, 0, 0, 2, 0, 4, 1, 4, 2, 4, 8, 1, 2, 4] There are quite a few places where ...
26
votes
21answers
3k views

I'm Lazy*: Top-left align my text

* and don't have a word processor with top-left align support :D Take several lines of input, with at least four unique characters of your choice including newline and space. The input can also be ...

1
2 3 4 5
14