Haskell Array Maximal Adjacement Difference
arrayMaximalAdjacentDifference :: [Int] -> Int
arrayMaximalAdjacentDifference a
| length a < 3 = 0
| otherwise = maximum (zipWith3 middle a (tail a) (drop 2 a))
where
middle p c n = max (abs (c - p)) (abs (c - n))
This checks the gap between each pair of neighbors and returns the largest difference.
Java Array Maximal Adjacement Difference
public class Solution {
public static int arrayMaximalAdjacentDifference(int[] a) {
int dif = 0;
for (int i = 1; i < a.length - 1; i++) {
dif = Math.max(dif, Math.max(Math.abs(a[i] - a[i - 1]), Math.abs(a[i] - a[i + 1])));
}
return dif;
}
}
This checks the gap between each pair of neighbors and returns the largest difference.
Lisp Array Maximal Adjacement Difference
(defun array-maximal-adjacent-difference (a)
(let* ((vec (coerce a 'vector))
(c (length vec))
(dif 0))
(loop for i from 1 below (1- c)
do (setf dif (max dif
(abs (- (aref vec i) (aref vec (1- i))))
(abs (- (aref vec i) (aref vec (1+ i)))))))
dif))
This checks the gap between each pair of neighbors and returns the largest difference.
PHP Array Maximal Adjacement Difference
function arrayMaximalAdjacentDifference($a)
{
$dif = 0;
for ($i = 1, $c = count($a); $i < $c - 1; $i++) {
$dif = max($dif, abs($a[$i] - $a[$i - 1]), abs($a[$i] - $a[$i + 1]));
}
return $dif;
}
This checks the gap between each pair of neighbors and returns the largest difference.
Python Array Maximal Adjacement Difference
def array_maximal_adjacent_difference(a: list[int]) -> int:
diff = 0
for i in range(1, len(a) - 1):
diff = max(diff, abs(a[i] - a[i - 1]), abs(a[i] - a[i + 1]))
return diff
This checks the gap between each pair of neighbors and returns the largest difference.
Rust Array Maximal Adjacement Difference
fn array_maximal_adjacent_difference(a: &[i64]) -> i64 {
let mut dif = 0;
for i in 1..a.len().saturating_sub(1) {
dif = dif.max((a[i] - a[i - 1]).abs()).max((a[i] - a[i + 1]).abs());
}
dif
}
This checks the gap between each pair of neighbors and returns the largest difference.
TypeScript Array Maximal Adjacement Difference
function arrayMaximalAdjacentDifference(a: number[]): number {
let dif = 0;
for (let i = 1; i < a.length - 1; i++) {
dif = Math.max(dif, Math.abs(a[i] - a[i - 1]), Math.abs(a[i] - a[i + 1]));
}
return dif;
}
This checks the gap between each pair of neighbors and returns the largest difference.
Variable
NAME="John"
echo $NAME
echo "$NAME"
echo "${NAME}
This shows the common ways to read a Bash variable. Quoting is usually the safe default because it keeps spaces and special characters from breaking your output.
Condition
if [[ -z "$string" ]]; then
echo "String is empty"
elif [[ -n "$string" ]]; then
echo "String is not empty"
fi
This is the standard Bash string check. -z means the value is empty, and -n means it has content.
Bash Binary Gap
binary_gap() {
local _n=$1
local _bin=""
if (( _n == 0 )); then
_bin="0"
else
local _x=$_n
while (( _x > 0 )); do
_bin="$(( _x % 2 ))$_bin"
_x=$(( _x / 2 ))
done
fi
_bin="${_bin#"${_bin%%[!0]*}"}"
_bin="${_bin%"${_bin##*[!0]}"}"
local _gap=0 _len=0
local -a _zeros
IFS='1' read -ra _zeros <<< "$_bin"
local _z
for _z in "${_zeros[@]}"; do
_len=${#_z}
(( _len > _gap )) && _gap=$_len
done
echo "$_gap"
}
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.
C++ Binary Gap
#include <algorithm>
int binaryGap(int n)
{
int gap = 0;
int run = -1; // -1 means no leading '1' has been seen yet
while (n > 0) {
if (n & 1) {
if (run >= 0) {
gap = std::max(gap, run);
}
run = 0;
} else if (run >= 0) {
++run;
}
n >>= 1;
}
return gap;
}
This turns the number into binary, ignores zeroes outside the edges, and finds the longest run of zeroes between 1s.