Skip to main content

Questions tagged [array]

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

Filter by
Sorted by
Tagged with
8 votes
45 answers
1k views

Calculate sum of self-exponentation

Given an array of positive integers, calculate the sum of each number raised to its own power. For example, given 4,6,7, the code needs to return \$4^4+6^6+7^7=...
Andy Liu's user avatar
  • 141
14 votes
5 answers
378 views

Generate a subgroup of a free group

In group theory, the free group with \$n\$ generators can be obtained by taking \$n\$ distinct symbols (let's call them \$a, b, c ...\$ etc), along with their inverses \$ a^{-1},b^{-1},c^{-1} ...\$ . ...
emanresu A's user avatar
  • 41.6k
26 votes
15 answers
2k views

Is it a cartesian product?

The cartesian product of two multisets \$A\$ and \$B\$ is the multiset of all ordered pairs consisting of an element of \$A\$ and an element of \$B\$. For example, the cartesian product of \$\{1, 2, 7,...
emanresu A's user avatar
  • 41.6k
28 votes
23 answers
2k views

Weave two lists, cycling if necessary

Consider two lists, A and B. To weave them together, we take the first element of A, then the first element of B, then the next element of A, the next element of B, and so on, ending with the last ...
DLosc's user avatar
  • 38.8k
21 votes
16 answers
2k 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, ...
emanresu A's user avatar
  • 41.6k
1 vote
7 answers
125 views

Interlaced array [duplicate]

You are given an array A of length N and an array B of length ...
Daniil Tutubalin's user avatar
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 ...
Greg Martin's user avatar
  • 15.9k
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 ...
emanresu A's user avatar
  • 41.6k
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 ...
Tbw's user avatar
  • 2,123
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: ...
Larry Bagel's user avatar
  • 4,219
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: ...
emanresu A's user avatar
  • 41.6k
20 votes
27 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 ...
Simd's user avatar
  • 3,153
7 votes
4 answers
403 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 ...
Trivaxy's user avatar
  • 487
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: ...
Larry Bagel's user avatar
  • 4,219
10 votes
6 answers
955 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. ...
Command Master's user avatar
6 votes
4 answers
248 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 ...
Wheat Wizard's user avatar
  • 99.6k
11 votes
8 answers
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 ...
Philippos's user avatar
  • 2,660
16 votes
8 answers
967 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 ...
Philippos's user avatar
  • 2,660
19 votes
24 answers
3k 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, ...
Philippos's user avatar
  • 2,660
16 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 ...
Philippos's user avatar
  • 2,660
7 votes
2 answers
324 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/...
math scat's user avatar
  • 9,438
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 ...
97.100.97.109's user avatar
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 ...
Wheat Wizard's user avatar
  • 99.6k
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: ...
emanresu A's user avatar
  • 41.6k
3 votes
7 answers
851 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 ...
Wheat Wizard's user avatar
  • 99.6k
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 ...
emanresu A's user avatar
  • 41.6k
1 vote
2 answers
562 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 \$...
Simd's user avatar
  • 3,153
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 ...
Simd's user avatar
  • 3,153
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 ...
bsoelch's user avatar
  • 6,035
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 (...
math scat's user avatar
  • 9,438
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 ...
Aitzaz Imtiaz's user avatar
1 vote
0 answers
199 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 ...
BlazeRod11's user avatar
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 \...
bigyihsuan's user avatar
  • 10.4k
17 votes
4 answers
666 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 ...
caird coinheringaahin g's user avatar
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 ...
mousetail's user avatar
  • 13k
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 ...
Simd's user avatar
  • 3,153
16 votes
6 answers
933 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 ...
lesobrod's user avatar
  • 3,423
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....
lesobrod's user avatar
  • 3,423
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 ...
solid.py's user avatar
  • 1,597
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}^...
emirps's user avatar
  • 1,807
14 votes
13 answers
2k 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 ...
Yousername's user avatar
  • 4,060
6 votes
2 answers
400 views

Shortening this Code to process nested tuple even further without the use of max()

I have the code: ...
Dark Programmer's user avatar
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 ...
Wheat Wizard's user avatar
  • 99.6k
30 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 ...
mousetail's user avatar
  • 13k
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 ...
chunes's user avatar
  • 26.3k
14 votes
10 answers
814 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 ...
lesobrod's user avatar
  • 3,423
12 votes
42 answers
3k 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 (...
Aitzaz Imtiaz's user avatar
7 votes
29 answers
626 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 ...
Aitzaz Imtiaz's user avatar
10 votes
3 answers
634 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 ...
lesobrod's user avatar
  • 3,423
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 ...
Aitzaz Imtiaz's user avatar

1
2 3 4 5
17