47
\$\begingroup\$

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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? \$\endgroup\$ Sep 20, 2017 at 17:36
  • 1
    \$\begingroup\$ @MartinEnder Any two distinct and consistent values. P.S Sorry for the late response \$\endgroup\$
    – user70974
    Sep 20, 2017 at 18:04
  • 5
    \$\begingroup\$ 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? \$\endgroup\$
    – Mark S.
    Sep 23, 2017 at 12:50

69 Answers 69

2
\$\begingroup\$

Clojure, 35 bytes

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

How neat is that?

\$\endgroup\$
1
2
\$\begingroup\$

TI-Basic, 6 7 bytes

prod(2>abs(ΔList(Ans

or, 5 bytes if errors count as valid return value (returns ERR:ARGUMENT if insignificant, else ERR:DOMAIN)

augment(sin⁻¹(ΔList(Ans
\$\endgroup\$
2
  • 1
    \$\begingroup\$ This should probably have abs(ΔList(Ans, or else drops by more than 1 (such as in {5,3,1} or in the test case {3,4,5,6,7,8,7,5}) don't get detected. \$\endgroup\$ Sep 24, 2017 at 1:19
  • \$\begingroup\$ @MishaLavrov thanks, you're right! \$\endgroup\$
    – Oki
    Sep 24, 2017 at 20:45
2
\$\begingroup\$

J, 20 19 bytes

i=.[:*/2>[:|2&(-/\)

I defined it as a verb, which adds 3 bytes to the total length.

Explanation

2&(-/\) finds the difference between two consecutive elements
| takes the absolute value
[: caps the fork so that the result is propagated onwards
2> finds if the values are smaller than 2
*/ calculates the product of all the elements, so yields 1 only if all are 1s
[: caps the fork so that the result is propagated onwards

Example:

   i 1 3 5 7 9 7 5 3 1  
0

Try it online

\$\endgroup\$
3
  • \$\begingroup\$ [:*/2>2|@-/\] should work for 6? fewer bytes. \$\endgroup\$
    – cole
    Nov 1, 2017 at 4:27
  • \$\begingroup\$ @cole Thanks, this is much better than mine! \$\endgroup\$ Nov 1, 2017 at 8:49
  • 1
    \$\begingroup\$ [:*/2>&|2-/\] for 13: Try it online! EDIT: woops I see cole beat me to it. I'll leave this here for the TIO. \$\endgroup\$
    – Jonah
    Mar 27, 2021 at 16:56
2
\$\begingroup\$

F#, 56 bytes

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

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?
\$\endgroup\$
2
  • 1
    \$\begingroup\$ here's an idea: abs:{#'!'x} \$\endgroup\$
    – ngn
    Mar 27, 2021 at 5:56
  • \$\begingroup\$ Makes the literal translation a bit more... nuanced, but saves some bytes! Thanks! \$\endgroup\$
    – coltim
    Mar 27, 2021 at 16:04
2
\$\begingroup\$

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!")
}
```
\$\endgroup\$
2
\$\begingroup\$

Julia, 17 bytes

!x=diff(x)⊆-1:1

Attempt This Online!

\$\endgroup\$
2
\$\begingroup\$

Uiua, 8 bytes

≅±.≡/-◫2

Try it!

≅±.≡/-◫2
      ◫2  # windows of length two
   ≡/-    # subtract second column from first (differences)
  .       # duplicate
 ±        # sign (pervasive across array)
≅         # do they match?
\$\endgroup\$
1
\$\begingroup\$

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

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

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ I don't think -p is needed to be a +1. Does it? \$\endgroup\$
    – Stan Strum
    Sep 20, 2017 at 20:12
1
\$\begingroup\$

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!

\$\endgroup\$
1
\$\begingroup\$

Common Lisp, 53 bytes

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

Try it online!

\$\endgroup\$
1
\$\begingroup\$

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
\$\endgroup\$
0
1
\$\begingroup\$

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]
\$\endgroup\$
1
\$\begingroup\$

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)
\$\endgroup\$
1
\$\begingroup\$

Add++, 16 bytes

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]
\$\endgroup\$
1
\$\begingroup\$

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.

\$\endgroup\$
1
1
\$\begingroup\$

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.

\$\endgroup\$
1
1
\$\begingroup\$

Vyxal, 5 4 bytes

¯vṅA

Try it Online!

¯vṅA
¯    # delta (consecutive diff)
 vṅ  # abs(a) ≤ 1
   A # all truthy?

-1 thx to @Sʨɠɠan

\$\endgroup\$
1
  • \$\begingroup\$ ȧ1≤ is just , or in this case, vṅ. \$\endgroup\$
    – naffetS
    Oct 29, 2022 at 1:23
1
\$\begingroup\$

Pyth, 6 bytes

._MI.+

Test suite

Explanation:
._MI.+  | Full code
._MI.+Q | with implicit variables
--------+------------------------------------
    .+Q | Differences of the input
   I    | Is it invariant over:
._M     |  Replace each element with its sign
\$\endgroup\$
1
\$\begingroup\$

Rust, 43 bytes

|a|a.windows(2).all(|s|(s[1]-s[0]).abs()<2)

Try it online!

A fn(&[i8])->bool.

\$\endgroup\$
6
  • \$\begingroup\$ Hmm, could you point me to meta consensus on an external type annotation being allowed? \$\endgroup\$
    – Maya
    Oct 29, 2022 at 12:16
  • \$\begingroup\$ @Maya codegolf.meta.stackexchange.com/questions/25156/… \$\endgroup\$
    – chunes
    Oct 29, 2022 at 17:11
  • \$\begingroup\$ @chunes That seems to be about the case when the examples themselves disambiguate it. \$\endgroup\$
    – Maya
    Oct 30, 2022 at 16:19
  • \$\begingroup\$ @Maya We have a clear answer on that about Java and C#, which IMO applies just as well to Rust codegolf.meta.stackexchange.com/questions/11223. \$\endgroup\$
    – corvus_192
    Oct 31, 2022 at 8:40
  • \$\begingroup\$ @Maya But you're right that for the purpose of documentation, it's better to put the type in the answer. \$\endgroup\$
    – corvus_192
    Oct 31, 2022 at 8:41
1
\$\begingroup\$

Pyt, 5 bytes

₋Å2<Π

Try it online!

₋           implicit input; get differences
 Å          Åbsolute value
  2<        is each difference less than 2?
    Π       product; implicit print
\$\endgroup\$
1
\$\begingroup\$

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.
    (
        ,         # Skip to the next element
        (?=       # 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
        )
    |
        $
    )
)*
$

Regex (ECMAScript), 21 bytes

\b(x+)(xx+,|,xx+)\1\b

Try it online!

Returns a non-match if the array is insignificant, and a match otherwise (i.e., its boolean result is inverted).

If this solution were converted to match=true logic, it would be ^(?!.*\b(x+)(xx+,|,xx+)\1\b) (28 bytes).

           # No anchor; can match starting anywhere
\b         # Assert word boundary, i.e. either the start of the string before
           # an "x", a position after an "x" and before a ",", a position
           # after a "," and before an "x", or the end of the string after an
           # "x".
(x+)       # \1 = any substring of "x"es
(
    xx+,   # 2 or more "x"es followed by ","
|          # or
    ,xx+   # "," followed by 2 or more "x"es
)
\1         # Match what was captured in \1
\b         # Assert word boundary
\$\endgroup\$
1
\$\begingroup\$

Pip, 11 bytes

2>M:BAD_MPg

Attempt This Online!

Explanation

2>M:BAD_MPg
          g  ; List of command-line arguments
        MP   ; Map to each pair of adjacent elements:
     AD      ;  Absolute difference between
    B        ;  the second and
       _     ;  the first
  M:         ; Get the max of that list
2>           ; 1 (truthy) if the max is less than 2; 0 (falsey) otherwise
\$\endgroup\$
1
\$\begingroup\$

Thunno, \$ 7 \log_{256}(96) \approx \$ 5.76 bytes

z[ZA2<P

Attempt This Online!

Outputs 0 for False and 1 for True.

Explanation

z[ZA2<P  # Implicit input
z[       # Differences of the input
  ZA     # Absolute value of each
    2<P  # Are they all less than 2?
         # Implicit output
\$\endgroup\$
1
\$\begingroup\$

Excel, 35 33 bytes

=AND(ABS(DROP(ζ,1)-DROP(ζ,-1))<2)

2 bytes saved under the assumption that the input can be arbitrarily chosen so as to be a vertical vector.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf, and nice answer! \$\endgroup\$ Feb 2, 2023 at 20:15
1
\$\begingroup\$

Nekomata + -e, 4 bytes

∆A2<

Attempt This Online!

∆A2<
∆       Differences
 A      Absolute value
  2<    Is less than 2?

Nekomata + -e, 4 bytes

∆:±=

A port of @Mr. Xcoder's Pyth answer.

Attempt This Online!

∆:±=
∆       Differences
 :      Duplicate
  ±     Sign
   =    Equal?
\$\endgroup\$
1
\$\begingroup\$

Lua, 57 bytes

for i=2,#arg do b=b or 1<(arg[i]-arg[i-1])^2 end print(b)

Try it online!

Outputs false if the array is insignificant, or true if it is not.

\$\endgroup\$
0
\$\begingroup\$

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!

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

Clojure, 36 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

8th, 66 49 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.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.