Questions tagged [array]

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

Filter by
Sorted by
Tagged with
31 votes
20 answers
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\$, \$...
34 votes
25 answers
3k views

The Three 'R's: Reverse, Reorder, Repeat

While doodling around with numbers, I found an interesting permutation you can generate from a list of numbers. If you repeat this same permutation enough times, you will always end up back at the ...
21 votes
31 answers
1k views

Quickly regrouping lists

Grouping takes a list and splits it into new lists of equal adjacent elements. For example [1,1,2,1,1] -> [[1,1],[2],[1,1]] If you then take the length of ...
18 votes
13 answers
1k 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, ...
19 votes
10 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: ...
11 votes
21 answers
1k views

Shuffle a mapping

We define a map as a set of key-value pairs. For this challenge, you need to take each of the values and assign them to a randomly chosen key. You must randomly shuffle the values, and output the ...
20 votes
37 answers
2k views

Implement an argwhere function

The \$\text{argwhere}\$ function takes a list of values and a predicate/boolean function as arguments and returns a list of indices where the predicate function returns true in the input list. For ...
30 votes
83 answers
6k 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 ...
23 votes
62 answers
2k views

Remove odd indices and double the even indices

Your task is simple, just remove the odd indices and double the even indices Example the input is Hello, World! and we get indices ...
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 ...
16 votes
54 answers
5k views

Find the minimum and maximum integers in an array, without using builtins

Challenge Given an array of integers, received from stdin, function arguments, program arguments, or some other method: Output only the minimum and maximum numbers in the array, through a return value,...
51 votes
92 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 ...
20 votes
33 answers
3k views

Zip uneven lists

In this challenge you will take two lists as input and you will zip them. The zip can be defined with this recursive expression: \$ \mathrm{zip}(a,b) = \begin{cases} \left[\,\,\right] & a = \left[...
22 votes
38 answers
3k views

Randomly select from an array

This challenge is rather simple: You are given an array of positive (not including 0) integers, and have to select a random element from this array. But here's the twist: The probability of selecting ...
1 vote
7 answers
110 views

Interlaced array [duplicate]

You are given an array A of length N and an array B of length ...
27 votes
46 answers
3k views

Output a Latin Square

An \$n\times n\$ Latin Square is a grid containing exactly \$n\$ distinct values where the values in each row and column are distinct. For example, $$\begin{matrix} A & B & C \\ C & A &...
12 votes
6 answers
1k 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 ...
18 votes
26 answers
2k 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 ...
17 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 ...
39 votes
67 answers
4k 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-...
33 votes
9 answers
2k views

No co-prime neighbors

Given a list of positive integers, output whether every adjacent pair of integers in it shares a prime factor. In other words, output truthy if and only if no two neighboring integers in the list are ...
43 votes
46 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 ...
21 votes
31 answers
2k views

Cosine similarity of two vectors

The cosine similarity of two vectors \$A\$ and \$B\$ is defined using their dot product and magnitude as: \$\frac{A\cdot B}{\|A\|\|B\|}\$ Or in other terms \$\frac{\sum_{i=1}^nA_iB_i}{\sqrt{\sum_{i=1}^...
24 votes
40 answers
2k views

Diagonalize a vector

Diagonalize a vector into a matrix. Input A vector, list, array, etc. of integers \$\mathbf{v}\$ of length \$n\$. Output A \$n \times n\$ matrix, 2D array, etc. \$A\$ such that for each element \$a_i \...
35 votes
28 answers
8k views

Could you please stop shuffling the deck and play already?

Challenge: Input: A list of distinct positive integers within the range \$[1, \text{list-size}]\$. Output: An integer: the amount of times the list is riffle-shuffled. For a list, this means the list ...
23 votes
23 answers
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 ...
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: ...
24 votes
26 answers
3k views

Diamondize a Matrix

Given a matrix, output a representation of the matrix where the top left element is on top, the anti-diagonal is the central row and the bottom right element is at the bottom. For example, consider ...
18 votes
25 answers
1k views

Trim elements from the ends of a list

The aim is to trim elements in from the ends of a list, depending on if they are in a second list of "falsey" results. For example, if the first list is ...
24 votes
17 answers
2k views

Reconstruct Matrix from its diagonals

Given the diagonals of a matrix, reconstruct the original matrix. The diagonals parallel to the major diagonal (the main diagonals) will be given. Diagonals: ...
19 votes
25 answers
1k views

Remove first encountered elements from a second list

Inspired by this question on Stackoverflow. The task Let's have two lists/arrays of integers: L1 and L2 of equal length. You ...
29 votes
28 answers
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 ...
10 votes
3 answers
623 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 ...
26 votes
31 answers
3k views

Different Way Forward

Given a list of integers produce a Forward Difference at a specified order/depth. For the list of integers: (10, 18, -12, 4, 8, -3, -5, 67, 9, 14) The Forward ...
51 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 ...
25 votes
16 answers
2k views

Multi-dimensional reversal

Given an N-dimensional orthogonal (non-ragged) array of non-negative integers, and an indication of which dimensions to reverse, return the array but reversed along those dimensions. The indication ...
28 votes
63 answers
3k views

Are All the Items the Same?

Given a list of non-negative integers, return whether or not that list is all the same number. Rules Input and output can be taken/given in any reasonable and convenient format Truthy/Falsey values ...
34 votes
36 answers
2k views

Targeted duplicate removal

The task In this challenge, you are given a number and a list. Your task is to remove from the list all occurrences of the given number except the first (leftmost) one, and output the resulting list. ...
22 votes
48 answers
3k views

Split into three!

Given a string, you've to split it into groups of three characters (by padding _ at the end). Related, but still it's different. Sample I/O: ...
11 votes
30 answers
1k views

Multiplicative undelta

Let us define the "multiplicative deltas" of values \$[\; a_0, \cdots a_N, \;]\$ as: $$ [\; a_1 / a_0, \cdots, a_{i+1} / a_i, \cdots, a_N / a_{N-1} \;] $$ The reverse operation - namely "...
6 votes
1 answer
535 views

Conways's Game of Life: Most newborn generating starting pattern

General rules The universe of the Game of Life is an infinite two-dimensional orthogonal grid of square cells, each of which is in one of two possible states, alive or dead. Every cell interacts with ...
7 votes
4 answers
381 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 ...
18 votes
6 answers
1k views

Retaining Water

Your task is to check the volume of water in a pool. Your have to create a program that takes in a input that is represented by an 10 by 10 grid of integers, with ...
28 votes
64 answers
3k views

Create an array with repeated numbers

Challenge Your task in this question is to write a program or a named function which takes a positive integer n (greater than 0) as input via STDIN, ARGV or ...
48 votes
35 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-...
21 votes
43 answers
2k views

Binary Reduce a List By Addition With a Right Bias

Given a list of 2 or more strictly positive integers, sum the first and last half of the list, with the middle element being counted only in the right half if the list has an odd number of elements ...
33 votes
66 answers
4k views

Triangle a number!

We are used to the term "squaring" n to mean calculating n2. We are also used to the term "cubing" n to mean n3. That being said, why couldn't we also triangle a number? How to triangle a number? ...
24 votes
15 answers
3k 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: ...
28 votes
17 answers
2k views

Falsify brief truths

Find the longest run of true in a list of booleans. Return the same list, with all other trues falsified. Input, output A list; any usual format (e.g., a delimited list as a string). Details True and ...
23 votes
19 answers
2k views

Deep Search a List

For this challenge, a list is considered valid if and only if it consists entirely of integers and valid lists (recursive definitions \o/). For this challenge, given a valid list and an integer, ...

1
2 3 4 5
17