# Am I an insignificant array?

An insignificant array is an array of positive integers, where the absolute differences between consecutive elements are all smaller than or equal to 1.

For example, the following array is insignificant:

[1, 2, 3, 4, 3, 4, 5, 5, 5, 4]


Because the corresponding (absolute) differences are:

[1, 1, 1, 1, 1, 1, 0, 0, 1]


Which are all smaller than or equal to 1.

Your task is to determine whether a given array of integers is insignificant.

• You may assume that the array always contains at least two elements.
• Standard input and output rules apply. You may take input (and output) in any reasonable format.
• Default Loopholes are forbidden.
• The truthy / falsy values have to be distinct and consistent.
• This is , so shortest answer in bytes wins.

# Test cases

Input -> Output

[1, 2, 3, 4, 3, 4, 5, 5, 5, 4] -> true
[1, 2, 3, 4, 5, 6, 7, 8, 9, 8] -> true
[3, 3, 3, 3, 3, 3, 3]          -> true
[3, 4, 4, 4, 3, 3, 3, 4, 4, 4] -> true
[1, 2, 3, 4]                   -> true
[5, 4, 3, 2]                   -> true
[1, 3, 5, 7, 9, 7, 5, 3, 1]    -> false
[1, 1, 1, 2, 3, 4, 5, 6, 19]   -> false
[3, 4, 5, 6, 7, 8, 7, 5]       -> false
[1, 2, 4, 10, 18, 10, 100]     -> false
[10, 20, 30, 30, 30]           -> false


I used the values true and false.

• Do the truthy/falsy values actually have to be truthy/falsy in our language of choice, or can we use any two distinct and consistent values? Sep 20, 2017 at 17:36
• @MartinEnder Any two distinct and consistent values. P.S Sorry for the late response
– user70974
Sep 20, 2017 at 18:04
• The text says you'll be given an array of integers, but that only arrays of positive integers can be insignificant. Should we be prepared for an array of negative integers? Sep 23, 2017 at 12:50

# Ruby, 3635 33 bytes

->b,*a{a.all?{|x|(b-(b=x))**2<2}}


Takes the input array as individual arguments.

-1 byte thanks to MegaTom, -2 bytes thanks to Jordan.

Try it online!

• ->a{b,=a;a.all?{|x|(b-(b=x))**2<2}} is 35 bytes. Sep 21, 2017 at 18:55
• @MegaTom right you are! Sep 21, 2017 at 18:59
• ->b,*a{a.all?{|x|(b-(b=x))**2<2}} is 33 bytes. Nov 1, 2017 at 20:27
• @Jordan Not sure if that works - how are you passing the array into it? Passing the array like in the TIO just gives true for all test cases. Nov 1, 2017 at 22:29
• No array, just pass the input as individual arguments. Nov 1, 2017 at 22:35

# K (ngn/k), 15 13 bytes

-2 bytes from @ngn's improvements

&/2>#'!'1_-':


Try it online!

• -': subtract each value in the (implicit) input from its predecessor
• 1_ drop the first result
• #'!' take the absolute value (literally, take the count of the range of each value; e.g. 1 -> ,0, -2 -> -2 -1)
• &/2> are all the absolute values smaller than 2?
• here's an idea: abs:{#'!'x}
– ngn
Mar 27, 2021 at 5:56
• Makes the literal translation a bit more... nuanced, but saves some bytes! Thanks! Mar 27, 2021 at 16:04

## JavaScript (ES6), 37 36 bytes

(a,u)=>!a.some(e=>(e-=(u=e))>1|e<-1)


Edit: Saved 1 byte by stealing @Arnauld's trick.

• You could use currying: a=>u=>!a.some(e=>(e-=(u=e))>1|e<-1) Sep 24, 2017 at 10:30

# Perl 5, 38 + 2 (-ap) = 40 bytes

$_&&=abs$i-$F[-1]<2while($i=pop@F)&&@F


Try it online!

• I don't think -p is needed to be a +1. Does it? Sep 20, 2017 at 20:12

# Swift 4, 52 bytes

{!zip($0.dropFirst(),$0).map(-).contains{1<abs($0)}}  Test suite: let isInsignificant: (_ array: [Int]) -> Bool = {!zip($0.dropFirst(),$0).map(-).contains{1<abs($0)}}

let testcases: [(input: [Int], expected: Bool)] = [
(input: [1, 2, 3, 4, 3, 4, 5, 5, 5, 4], expected: true),
(input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 8], expected: true),
(input: [3, 3, 3, 3, 3, 3, 3],          expected: true),
(input: [3, 4, 4, 4, 3, 3, 3, 4, 4, 4], expected: true),
(input: [1, 2, 3, 4],                   expected: true ),
(input: [5, 4, 3, 2],                   expected: true ),
(input: [1, 3, 5, 7, 9, 7, 5, 3, 1],    expected: false),
(input: [1, 1, 1, 2, 3, 4, 5, 6, 19],   expected: false),
(input: [3, 4, 5, 6, 7, 8, 7, 5],       expected: false),
(input: [1, 2, 4, 10, 18, 10, 100],     expected: false),
(input: [10, 20, 30, 30, 30],           expected: false),
]

for (caseNumber, testcase) in testcases.enumerated() {
let actual = isInsignificant(testcase.input)
assert(actual == testcase.expected,
"Testcase #\(caseNumber) \(testcase.input) failed. Got \(actual), but expected \(testcase.expected)!")
print("Testcase #\(caseNumber) passed!")
}


# RProgN 2, 8 bytes

{-â2<}á*


## Explained

{-â2<}á*
{    }á     # Fold the input by the function
-          # Subtract, get the difference.
â         # Absolute
2<       # Is less than 2.
*    # Get the product, 1 for truthy, 0 for falsey.


Try it online!

# Common Lisp, 53 bytes

(lambda(a)(every(lambda(x y)(< -2(- x y)2))a(cdr a)))


Try it online!

# Jq 1.5, 42 41 bytes

all(keys[1:][]as$i|.[$i]-.[$i-1]|.*.;.<2)  Explained all( # true if all values from keys[1:][] as$i   # scanning input indices starting at second element
| .[$i]-.[$i-1]      # calculate differences between successive elements
| .*.                # square the difference
; . < 2                # are < 2
)


Sample Run

$jq -Mc 'all(keys[1:][]as$i|.[$i]-.[$i-1]|.*.;.<2)' data.json
true
true
true
true
true
true
false
false
false
false
false

$echo -n 'all(keys[1:][]as$i|.[$i]-.[$i-1]|.*.;.<2)' | wc -c
41


# Excel VBA, 55 Bytes

Anonymous VBE immediate window function that takes input from range [1:1] and outputs to the VBE immediate window

[2:2]="=IfError(If(B1=0,0,ABS(B1-A1)),0)":?[Max(2:2)>1]


# Pushy, 13 bytes

$2d-|v.;Og2<#  Try it online!  \ Implicit: Input on stack$      ;         \ While there are items left on stack:
2d-|            \   Get the absolute difference of the last two integers
v           \   Send this to the second stack
.          \   Pop the last item

O        \ Go to the second stack (list of differences)
g       \ Sort ascendingly (largest item last)
2<#    \ Check if it is smaller than 2 (print 1/0 accordingly)


## F#, 56 bytes

let f a=Seq.forall(fun(x,y)->abs(x-y)<2)(Seq.pairwise a)


L,vbUÑ_€|2ª>


Try it online!

## How it works

L,		; Create a lambda function
; Example argument: 	    ['[1 2 3 4 3 4 5 5 5 4]']
vbU	; Evaluate as list; STACK = [1 2 3 4 3 4 5 5 5 4]
Ñ_	; Deltas;	    STACK = [1 1 1 -1 1 1 0 0 -1]
€|	; Absolute values;  STACK = [1 1 1 1 1 1 0 0 1]
2ª>	; All less than 2;  STACK = [1]


# Desmos, 46 bytes

\left\{max(abs(n[2,...,n.length]-n))<2\right\}


View on Desmos!

This is a rare case where I think using \left\{\right\} is more efficient than not.

# Python 3, 54 bytes

2 bytes thanks to Jonathan Frech.

lambda a:all(-2<a[i]-a[i+1]<2for i in range(len(a)-1))


Try it online!

• abs(a[i]-a[i+1])<2 -> -2<a[i]-a[-~i]<2. Sep 20, 2017 at 15:47

# Clojure, 36 bytes

#(every? #{-1 0 1}(map - %(rest %)))


Try it online!

# 8th, 6649 55 bytes

Code

1 >r ( n:- abs dup 1 > if rdrop 0 >r then ) a:y drop r>


SED (Stack Effect Diagram) is: a -- f

Explanation

This code leaves 1 on TOS if array is insignificant, otherwise it leaves 0. Here the ungolfed version:

1 >r                                           \ suppose array is insignificant
( n:- n:abs dup 1 n:> if rdrop 0 >r then ) a:y \ compute corresponding (absolute) differences and check if difference is > 1
drop r>                                        \ clean stack and put result on TOS


We could immediately exit from loop with break when array is significant, but in this case we waste 6 bytes.

# Python 3, 49 bytes

lambda l:all(abs(j-i)<=1 for i,j in zip(l,l[1:]))

• Unfortunately, this doesn't work for arrays like [1, 1, 3]. Sep 21, 2017 at 17:07
• Great catch on that edge case!! Updated with another answer. @Dennis Sep 21, 2017 at 18:47

# Pyth, 15 12 bytes

Works pretty simply. T or 10 as a boolean is True

V.+Q=&T<N2)T


Explanation:

V.+Q        In the difference map of the input...
=&T<N2) T = T && the current item is less than 2, and close the loop
T           Print J


Test Suite

Scheme - 89 bytes

(define (f n) (if (not (empty? n)) (and (<= (abs (- (cadr n) (car n))) 1) (f (cdr n))) 1)


# Casio-Basic, 28 bytes

judge(max(abs(⊿list(n)))<2


It's nice that the fx-CP400 has a built-in for cumulative differences. Not so nice that you need to use judge to force it to check whether the resulting value is less than 2.

27 bytes for the function (⊿ is two bytes), +1 to enter n in the argument list.

• If Jelly uses Unicode characters and just count 1 byte, why wouldn't ⊿ also count just 1 byte? Oct 31, 2017 at 9:11
• @LS_ᴅᴇᴠ If by "Unicode" you mean UTF-8, most Jelly programs don't use UTF-8. Nov 1, 2017 at 20:30
• @LS_ᴅᴇᴠ Jelly uses its own code page, as linked in the comment above, which means that every single character used in the language counts as one byte, even if it's two in Unicode. On the Casio ClassPad, that character has a character code above 256, which makes it a two-byte character. Nov 2, 2017 at 1:07
• @Jordan Yes, you are right, I read something more about Jelly and get it! Nov 2, 2017 at 7:26
• It should be measured by how many bytes it takes on the calculator. Which if it's tokenized, could be much less than 26 bytes. Apr 2, 2021 at 14:59

# Pxem, Filename: 40 bytes + Content: 0 bytes = 40 bytes.

• Filename (escaped): ._\001.-.c._.t.w\001.-._.c.m.-\001.x.d.a.t.c.aY.o
• Content: empty

## Usage

Input is from STDIN; output from STDOUT. First positive integer N for input represents number of items in list. Then integers c1, c2, ..., cN represents each item of the list. Each input has to be separated by one or more s.

An output of Y represents truthy, and no output falsey.

### Input example

List [1, 2, 3, 4, 3, 4, 5, 5, 5, 4] is:

10
1 2 3 4 3 4 5 5 5 4


XX.z
.a._\001.-.cXX.z # push N; decrement it
.a._.tXX.z # push c1; heap<-c1
.a.w\001.-XX.z # while N!=0; do N--
.a._.c.m.-\001.xXX.z # push ci; if abs(ci-heap); then
.a.dXX.z # exit
.a.aXX.z # fi
.a.tXX.z # heap=ci
.a.c.aXX.z # done
.aY.oXX.z # putchar o
.a


Try it online!

# Regex (ECMAScript), 27 bytes

^(x(x*)(,(?=\2x?x?\b)|$))*$


Try it online!

Input is a list of positive unary numbers separated by ,. Each one is a string of xs whose length represents the number.

^
(
x(x*)         # \2 = this element - 1
# Assert that either the next element is in the range \2 to \2+2 inclusive,
# or that we've reached the end and there are no more elements.
(
(?=       # look ahead to the next element to make the following assertion:
\2    # subtract \2 from it
x?x?  # subtract any number from 0 to 2 from it
\b    # assert that we matched the exact number
)
|
$) )*$


# PHP, 85 bytes

function a($p){$r=1;foreach($p as$k=>$q)if($p[$k+1]&&abs($p[$k+1]-$q)>1)$r=0;echo$r;}


Try it online!

It can probably be golfed some more, but it's a start.

• Try it online! (link in answer is broken) Apr 2, 2021 at 14:51