# Questions tagged [array]

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

807 questions
Filter by
Sorted by
Tagged with
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 ...
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: ...
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 ...
2k views

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 \...
8k views

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 ...
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 ...
1k 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: ...
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 ...
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 ...
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 ...
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 ...
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: ...
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 ...
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 ...
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 ...
621 views

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 ...
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 ...
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 ...
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 ...
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 ...
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. ...
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: ...
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 "...
533 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 ...
368 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 ...
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 ...
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 ...
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-...
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 ...
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? ...
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: ...
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 ...
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, ...
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 ...
1k views

907 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. ...
2k views

### Extend a matrix in all directions

Given a multidimensional, rectangular array and a list of dimensions, such as: [ [1, 2], [3, 4] ] [3, 4] Your challenge is to extend the matrix to the ...
1k views

### Move to Right and left

Task Your task is to take an array of numbers as input, and produce a new one where each number has been shifted both right and left, leaving 0s if no number fills ...
3k views

### Shortest Code to Find the Smallest Missing Positive Integer

Description Given an unsorted array of integers, find the smallest positive integer that does not appear in the array. Your task is to write the shortest code possible to solve this problem. Input A ...
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 ...
2k 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, ...
239 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 ...
3k views

### Print a Block-Diagonal Matrix

Here is a simple, bite-sized (byte-sized?) code golf: given a non-empty list of positive integers less than 10, print a block-diagonal matrix, where the list specifies the size of the blocks, in order....
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 ...
530 views

### Sort musical pitches

Given an unordered list of musical pitches, write the shortest program/function (scored in bytes) to sort the list from lowest pitch to highest. Pitches will be given in scientific pitch notation, ...
953 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 ...
I've been posting relatively hard challenges recently, so here goes an easy one. Task Given an array $A$ and a number $n$, calculate the sum of all numbers of $A$ multiplied by $n$, except the ...