Questions tagged [array]
A competition to solve a particular problem through the usage and manipulation of arrays.
800
questions
16
votes
8
answers
877
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 ...
16
votes
19
answers
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, ...
15
votes
16
answers
4k
views
Which skill to train?
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 ...
7
votes
2
answers
303
views
How many twisted corners?
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/...
14
votes
9
answers
2k
views
Seat gangs as far as possible
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 ...
30
votes
20
answers
4k
views
How many umbrellas to cover the beach?
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 ...
16
votes
9
answers
1k
views
Transpose a multidimensional array
Transposition is an operation on 2-dimensional arrays that flips every element across the main diagonal:
...
3
votes
7
answers
796
views
Output the longest list
In this challenge you will take a non-empty (finite) list of (possibly infinite) lists of positive integers, and your task is to output the longest one. An infinite list is longer than a finite list ...
18
votes
20
answers
2k
views
Complement an infinite list
In this challenge, we define the complement of a list of positive integers as all positive integers not included in that list. For example, the complement of the even numbers ...
1
vote
2
answers
544
views
Compute how many are no larger than each item in an array
Input: an array of length \$n\$ containing integers in the range \$0\$ to \$2n\$.
For each integer \$x\$ in the array, compute the number of integers that occur before \$x\$ that are no larger than \$...
17
votes
17
answers
2k
views
Reconstruct an array by successive insertions
Given an array of letters in the range 'a' to 'o', compute how to construct the array by successively inserting the letters in alphabetical order. You will always start the insertion with a base array ...
23
votes
23
answers
2k
views
Find the largest sum such that no two elements are touching
Inspired by this (off topic) post
Given an array of numbers, find the largest sum over a subarray not containing two adjacent elements
...
16
votes
6
answers
2k
views
What's my score?
The question score on Stack Exchange is the total number of upvotes minus the total number of downvotes a question receives. However, the reputation gained/lost for every upvote/downvote is different (...
24
votes
28
answers
3k
views
Longest Consecutive Sequence
Description
Given a list of positive integers, write the shortest code to find the number count of the longest consecutive sequence of numbers within the list.
Input
A list of positive integers. You ...
1
vote
0
answers
189
views
DP approach gives TLE [closed]
There is a question to basically find the largest sum in an array, such that no two elements are chosen adjacent to each other. The concept is to recursively calculate the sum, while considering and ...
24
votes
38
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 \...
17
votes
4
answers
650
views
Complete the landscape
Carcassonne is a tile-based game, where the objective is to construct Roads, Cities and Monasteries, in order to score points. The game works by players taking turns to draw and place tiles to ...
13
votes
10
answers
1k
views
Adjacent Items Sorting
Given a list, L, of sets of numbers like this:
[[1], [0,2,3], [3,1], [1,2,3]]
Output a single list of numbers such that 2 ...
12
votes
17
answers
2k
views
Compute cumulative means efficiently
Consider a sorted array of positive floating point numbers such as:
input = [0.22, 2.88, 6.35, 7.17, 9.15]
For each integer \$i\$ from 1 up to the last value in ...
16
votes
6
answers
898
views
"Signpost" puzzle from Tatham's collection
Simplified version of "Signpost" puzzle from Simon Tatham's Portable Puzzle Collection.
How the puzzle is created
We pick up randomly positive integer N ...
24
votes
17
answers
1k
views
Counting fading
Background
Being in a philosophical mood, I remembered one old idea. I think it is suitable for CG.
Duplicates are not found, but it's of course just a kind of totalistic cellular automaton or erosion....
17
votes
35
answers
3k
views
The Jaccard Index
The Jaccard index / similarity coefficient, also known as the Tanimoto index / coefficient, is a statistic used for gauging the similarity and diversity of finite sample sets. It was developed by ...
21
votes
29
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}^...
14
votes
13
answers
1k
views
Make a Custom Bayer Matrix
A Bayer matrix is a threshold map used for ordered dithering that gives the illusion of having more shades of color than actually present by using a crosshatch-like pattern.
Bayer matrices are square ...
5
votes
2
answers
374
views
Shortening this Code to process nested tuple even further without the use of max()
I have the code:
...
15
votes
17
answers
2k
views
How long to carry sort?
Carry sort is an \$O(n)\$ "sorting" algorithm. Here's how it works. The algorithm moves left to right along a list. As it traverses a list it "carries" a single item, the largest ...
29
votes
13
answers
3k
views
Is it a valid chemical?
Non-metals typically* have a fixed number of covalent bonds in every chemical they are part of. Given the number of bonds every element requires, output whether it's possible to construct a single ...
29
votes
17
answers
3k
views
Is it traversable?
Imagine that a list of integers describes the heights of some two-dimensional terrain as seen from the side.
Stamina: [ 4 4 4 4 4 4 3 3 3 3 2 2 2 - ]
O
...
13
votes
7
answers
762
views
Guessing on straws
Dedicated to Martin Gardner, taken from his book
Background
In the old days, the Slavs had a divination method for finding out whether a girl would get married. The girl would clutch six straws in her ...
11
votes
41
answers
2k
views
Counting Letters in a String
Write a program or function that takes in a string and outputs a count of each modern English alphabet letter in the string, case-insensitive.
Input: A string consisting of printable ASCII characters (...
5
votes
25
answers
520
views
String with the smallest sum of ASCII values
Description
In this challenge, you need to write a function that takes in a list of strings and returns the smallest string based on their ASCII value. You should assume that the input list contains ...
10
votes
3
answers
612
views
Voronoi-Lloyd ASCII art [closed]
Voronoi diagram is a partition of a plane (or part of plane) into regions close to each of a given set of objects ("seeds").
Here we’ll be dealing with discrete arrays or even rather with ...
27
votes
52
answers
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 ...
12
votes
6
answers
609
views
Number Clusters
Your task is to create a program or function, that when given an input list of nonnegative integers of length \$l \ge 2\$ and a nonnegative integer \$c\$ where \$2 \le c \le l\$, group the list into \$...
7
votes
18
answers
605
views
Find the largest sum of any contiguous subarray of the array [duplicate]
Let's say you are given an integer array. Create a function to determine the largest sum of any array's adjacent subarrays. For instance, the contiguous subarray with the largest sum is ...
11
votes
26
answers
2k
views
Arbitrary Apple Dilemma
I have encountered this type of puzzle, that usually involves apples, several times. It goes as follows:
There is a certain number of apples in a basket. Ava takes a third of the apples. Bob takes a ...
14
votes
8
answers
1k
views
Change the subject
Sometimes I feel like the conversation is going down the wrong path and take it upon myself to steer it in another direction. And this conversation is going wrong real fast, so I need to change the ...
17
votes
16
answers
731
views
Form a subset that is a continuous range
Given a list of lists of positive integers, output a subset of them so their union forms a continuous non-empty range with no numbers missing. For example, consider this input:
...
13
votes
17
answers
2k
views
Remove duplicates from my academic transcript
Consider a list of subject, grade pairs. E.g.
...
8
votes
32
answers
1k
views
Keep elements in sequence that have a letter repeated at least 3 times
Challenge:
Given the input array l with a list of strings, only keep the elements in the sequence that have a letter that's repeated at least 3 times. Like ...
13
votes
35
answers
736
views
Create \$n\$ sublists with the powers of two (1, 2, 4, 8, 16...)
Challenge:
Given the input number n. It should give me nested sublists of n layers with the power of two numbers for each level. ...
18
votes
22
answers
865
views
Generate recursive "arrays" up to a certain depth
Given a positive integer n.
Generate a JSON array (can be a string, or your language's built-in JSON representation as long as we can get valid JSON, (your code ...
13
votes
10
answers
660
views
CGAC2022 Day 8: Fen The Wicked, Part 2
Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details.
Fen is a magician Elf. He can cast spells on an array of numbers to produce another number or array of numbers.
One ...
20
votes
25
answers
1k
views
CGAC2022 Day 7: Fen The Wicked
Part of Code Golf Advent Calendar 2022 event. See the linked meta post for details.
Fen is a magician Elf. He can cast spells on an array of numbers to produce another number or array of numbers.
One ...
11
votes
6
answers
435
views
Conversions Galore!
Imagine that there are \$n\$ different types of objects \$O_1,O_2,O_3,\ldots,O_n\$ and they each have a conversion factor \$k_1,k_2,k_3,\ldots,k_n\$. You can, for any \$1\le i\le n\$, convert \$k_i\$ ...
6
votes
5
answers
897
views
What is the longest trampoline?
In this challenge we are going to consider lists of integers such that for every member \$x\$ at index \$i\$ then the indexes \$i+x\$ and \$i-x\$ have the value \$x+1\$ or are out of bounds for the ...
22
votes
15
answers
2k
views
Construct a diverse array
Given an integer \$ n \ge 1 \$, your task is to output an array \$ a \$ consisting of \$ n^2 + 1 \$ integers, such that all possible pairs of integers \$ (x, y) \$ satisfying \$ 1 \le x, y \le n \$ ...
17
votes
9
answers
1k
views
Longest alternating subsequence
inspired by Longest Increasing Substring
Task
Given a list of non-negative integers, output the length of its longest alternating subsequence.
An alternating sequence is a sequence of numbers which ...
23
votes
45
answers
2k
views
Repeat Values In Array
Given two arrays of non-negative integers \$A = [A_1,A_2,\ldots,A_n]\$ and \$R = [R_1,R_2,\ldots,R_n]\$ which are equal in length, return an array which has the element \$A_1\$ repeated \$R_1\$ times, ...
24
votes
16
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: ...