Stone Wall

You are going to build a stone wall. The wall should be straight and N meters long, and its thickness should be constant; however, it should have different heights in different places. The height of the wall is specified by a zero-indexed array H of N positive integers. H[I] is the height of the wall from I to I+1 meters to the right of its left end. In particular, H[0] is the height of the wall’s left end and H[N−1] is the height of the wall’s right end.

Number of distinct absolute values of sorted array elements

function solution(A) { // write your code in JavaScript (Node.js 4.0.0) var count=0;l=0,r=A.length-1; while(l <= r){ count++; if(Math.abs(A[l]) === Math.abs(A[r])){ l++; r–; } else if(Math.abs(A[l]) > Math.abs(A[r])){ l++; } else if(Math.abs(A[l]) < Math.abs(A[r])){ r–; } } return count; }

Chocolates By Numbers

Two positive integers N and M are given. Integer N represents the number of chocolates arranged in a circle, numbered from 0 to N − 1. You start to eat the chocolates. After eating a chocolate you leave only a wrapper. You begin with eating chocolate number 0. Then you omit the next M − 1 chocolates or wrappers on the circle, and eat the following one. More precisely, if you ate chocolate number X, then you will next eat the chocolate with number (X + M) modulo N (remainder of division).

Calculate how many fish are alive

You are given two non-empty zero-indexed arrays A and B consisting of N integers. Arrays A and B represent N voracious fish in a river, ordered downstream along the flow of the river. The fish are numbered from 0 to N − 1. If P and Q are two fish and P < Q, then fish P is initially upstream of fish Q. Initially, each fish has a unique position.

Compute number of distinct values in an array

Write a function function solution(A); that, given a zero-indexed array A consisting of N integers, returns the number of distinct values in array A. Assume that: N is an integer within the range [0..100,000]; each element of array A is an integer within the range [−1,000,000..1,000,000]. For example, given array A consisting of six elements such that: A[0] = 2 A1 = 1 A[2] = 1 A[3] = 2 A[4] = 3 A[5] = 1