Find the maximum possible summation of differences of consecutive elements

Array A contains the elements, A1,A2,…,AN. And array B contains the elements, B1,B2,…,BN. There is a relationship between Ai and Bi: any element Ai lies between 1 and Bi. Let the cost S of an array A be defined as: S=N∑i=2|Ai−Ai−1| Given B, find and print the largest possible value of S. The problem can … Read more

Create a matrix where if an element in an M×N matrix is 0, its entire row and column are set to 0

How is my implementation? Are there improvements I can make to the variable names or any edge cases I might have missed? public class ZeroMatrix { public static void main(String[] args) { int[][] matrix = new int[][]{ {1, 0, 2}, {3, 4, 1} }; System.out.println(Arrays.deepToString(setZeroes(matrix))); } private static int[][] setZeroes(int[][] matrix) { boolean[] rowsWithZero = … Read more

Base64 string ↔ float array

I need to convert f32 arrays with a fix length to base64 representation and back. My current code looks like this. It works, but it feels way too complicated. How can I improve it? extern crate base64; fn read_float_vec_from_byte_vec(v: Vec<u8>) -> Result<Vec<f32>, String> { match v.len() % 4 { 0 => { use std::ptr; … Read more