Rust Add Border
fn add_border(picture: &[String]) -> Vec<String> {
let width = picture[0].len();
let border = "*".repeat(width + 2);
let mut result = Vec::with_capacity(picture.len() + 2);
result.push(border.clone());
for row in picture {
result.push(format!("*{row}*"));
}
result.push(border);
result
}
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
TypeScript Add Border
function addBorder(picture: string[]): string[] {
const width = picture[0].length;
const bordered = picture.map((row) => `*${row}*`);
const border = "*".repeat(width + 2);
return [border, ...bordered, border];
}
This builds a new grid with a * border around every side. It adds a full top and bottom row, then wraps each existing row from left and right.
Bash Adjacent Elements Product
adjacent_elements_product() {
local -n _arr="$1"
local _max=-9223372036854775808
local _i _c=${#_arr[@]}
for ((_i = 0; _i < _c - 1; _i++)); do
local _p=$(( _arr[_i] * _arr[_i+1] ))
if (( _p > _max )); then _max=$_p; fi
done
echo "$_max"
}
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
C++ Adjacent Elements Product
#include <algorithm>
#include <limits>
#include <vector>
long long adjacentElementsProduct(const std::vector<int>& inputArray)
{
long long max = std::numeric_limits<long long>::min();
for (std::size_t i = 0; i + 1 < inputArray.size(); ++i) {
long long product = static_cast<long long>(inputArray[i]) * inputArray[i + 1];
max = std::max(max, product);
}
return max;
}
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
C# Adjacent Elements Product
static long AdjacentElementsProduct(int[] inputArray)
{
long max = long.MinValue;
for (int i = 0; i < inputArray.Length - 1; i++)
{
max = Math.Max(max, (long)inputArray[i] * inputArray[i + 1]);
}
return max;
}
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Elixir Adjacent Elements Product
defmodule AdjacentElementsProduct do
def adjacent_elements_product(input_array) do
input_array
|> Enum.chunk_every(2, 1, :discard)
|> Enum.map(fn [a, b] -> a * b end)
|> Enum.max()
end
end
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Erlang Adjacent Elements Product
-module(adjacent_elements_product).
-export([adjacent_elements_product/1]).
adjacent_elements_product(InputArray) ->
Pairs = lists:zip(InputArray, tl(InputArray)),
lists:max([X * Y || {X, Y} <- Pairs]).
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Go Adjacent Elements Product
func adjacentElementsProduct(inputArray []int) int {
max := inputArray[0] * inputArray[1]
for i := 0; i < len(inputArray)-1; i++ {
if p := inputArray[i] * inputArray[i+1]; p > max {
max = p
}
}
return max
}
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Haskell Adjacent Elements Product
adjacentElementsProduct :: [Int] -> Int
adjacentElementsProduct inputArray = maximum (zipWith (*) inputArray (tail inputArray))
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.
Java Adjacent Elements Product
public class Solution {
public static int adjacentElementsProduct(int[] inputArray) {
int max = Integer.MIN_VALUE;
for (int i = 0; i < inputArray.length - 1; i++) {
max = Math.max(max, inputArray[i] * inputArray[i + 1]);
}
return max;
}
}
This walks through neighboring values, multiplies each pair, and keeps the biggest product it finds.