PHP Array Change
function arrayChange($a)
{
$min = 0;
for ($k = 0, $c = count($a); $k < $c - 1; $k++) {
if ($a[$k] >= $a[$k + 1]) {
$dif = $a[$k] - $a[$k + 1] + 1;
$a[$k + 1] += $dif;
$min += $dif;
}
}
return $min;
}
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
Python Array Change
def array_change(a: list[int]) -> int:
moves = 0
for k in range(len(a) - 1):
if a[k] >= a[k + 1]:
diff = a[k] - a[k + 1] + 1
a[k + 1] += diff
moves += diff
return moves
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
Rust Array Change
fn array_change(mut a: Vec<i64>) -> i64 {
let mut min = 0;
for k in 0..a.len().saturating_sub(1) {
if a[k] >= a[k + 1] {
let dif = a[k] - a[k + 1] + 1;
a[k + 1] += dif;
min += dif;
}
}
min
}
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
TypeScript Array Change
function arrayChange(a: number[]): number {
let min = 0;
for (let k = 0; k < a.length - 1; k++) {
if (a[k] >= a[k + 1]) {
const dif = a[k] - a[k + 1] + 1;
a[k + 1] += dif;
min += dif;
}
}
return min;
}
This moves left to right and bumps values only when needed so the array becomes strictly increasing.
Bash Array Maximal Adjacement Difference
array_maximal_adjacent_difference() {
local -n _a="$1"
local _dif=0 _i _c=${#_a[@]}
for ((_i = 1; _i < _c - 1; _i++)); do
local _d1=$(( _a[_i] - _a[_i-1] )); (( _d1 < 0 )) && _d1=$(( -_d1 ))
local _d2=$(( _a[_i] - _a[_i+1] )); (( _d2 < 0 )) && _d2=$(( -_d2 ))
(( _d1 > _dif )) && _dif=$_d1
(( _d2 > _dif )) && _dif=$_d2
done
echo "$_dif"
}
This checks the gap between each pair of neighbors and returns the largest difference.
C++ Array Maximal Adjacement Difference
#include <algorithm>
#include <cstdlib>
#include <vector>
long long arrayMaximalAdjacentDifference(const std::vector<int>& a)
{
long long dif = 0;
for (std::size_t i = 1; i + 1 < a.size(); ++i) {
long long left = std::llabs(static_cast<long long>(a[i]) - a[i - 1]);
long long right = std::llabs(static_cast<long long>(a[i]) - a[i + 1]);
dif = std::max({dif, left, right});
}
return dif;
}
This checks the gap between each pair of neighbors and returns the largest difference.
C# Array Maximal Adjacement Difference
static long ArrayMaximalAdjacentDifference(int[] a)
{
long dif = 0;
for (int i = 1; i < a.Length - 1; i++)
{
dif = Math.Max(dif, Math.Max(Math.Abs((long)a[i] - a[i - 1]), Math.Abs((long)a[i] - a[i + 1])));
}
return dif;
}
This checks the gap between each pair of neighbors and returns the largest difference.
Elixir Array Maximal Adjacement Difference
defmodule ArrayMaximalAdjacentDifference do
def array_maximal_adjacent_difference(a) when length(a) < 3, do: 0
def array_maximal_adjacent_difference(a) do
len = length(a)
1..(len - 2)
|> Enum.map(fn i ->
prev = Enum.at(a, i - 1)
cur = Enum.at(a, i)
nxt = Enum.at(a, i + 1)
max(abs(cur - prev), abs(cur - nxt))
end)
|> Enum.max()
end
end
This checks the gap between each pair of neighbors and returns the largest difference.
Erlang Array Maximal Adjacement Difference
-module(array_maximal_adjacent_difference).
-export([array_maximal_adjacent_difference/1]).
array_maximal_adjacent_difference(A) when length(A) < 3 ->
0;
array_maximal_adjacent_difference(A) ->
Triples = lists:zip3(A, tl(A), tl(tl(A))),
lists:max([max(abs(Cur - Prev), abs(Cur - Next)) || {Prev, Cur, Next} <- Triples]).
This checks the gap between each pair of neighbors and returns the largest difference.
Go Array Maximal Adjacement Difference
func abs(n int) int {
if n < 0 {
return -n
}
return n
}
func arrayMaximalAdjacentDifference(a []int) int {
diff := 0
for i := 1; i < len(a)-1; i++ {
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.