function solution(A, K) {
const L = A.length - (K % A.length); // to get the rotation length value below array length (since rotation of product of array length gives same array)
const A1 = A.slice(L); // last part of array which need to be get to front after L times rotation
const A2 = A.slice(0, L); // part which rotate L times to right side
const Result = [...A1, ...A2]; // reverse and join both array by spreading
return Result;
}
function solution(A) {
let left = A[0];
let right = A.reduce((acc, curr) => acc + curr) - A[0];
let min = Math.abs(left - right);
for (let i = 1; i < A.length - 1; i++) {
left += A[i];
right -= A[i];
min = Math.min(min, Math.abs(left - right));
}
return min;
}
function solution(X, A) {
let covered = 0;
let tempArray = [];
for (let i = 0; i < A.length; i++) {
if (!tempArray[A[i]]) {
tempArray[A[i]] = true;
covered++;
if (covered === X) return i;
}
}
return -1;
}
function solution(N, A) {
var i;
var j;
var len = A.length;
var lastMax = 0;
var max = 0;
var counters = new Array(N);
for (j = 0; j < N; j++) counters[j] = 0;
var n1 = N + 1;
for (j = 0; j < len; j++) {
if (A[j] < n1) {
i = A[j] - 1;
if (counters[i] < lastMax) counters[i] = lastMax;
counters[i]++;
if (max < counters[i]) max = counters[i];
} else {
lastMax = max;
}
}
for (j = 0; j < N; j++) {
if (counters[j] < lastMax) counters[j] = lastMax;
}
return counters;
}
// simple solution but with perfomance issue : 66%
function solution(N, A) {
let max = 0;
let counters = new Array(N).fill(0);
for (let item of A) {
if (item > N) {
counters.fill(max);
} else {
counters[item - 1] += 1;
max = Math.max(max, counters[item - 1]);
}
}
return counters;
}
function solution(A) {
const array = [...new Set(A.filter((val) => val > 0))].sort((a, b) => a - b);
for (let i = 0; i < array.length; i++) if (array[i] != i + 1) return i + 1;
return array.length + 1;
}
function solution(A) {
let passing = 0;
let west = 0;
const len = A.length;
for (let i = len - 1; i > -1; i--) {
if (A[i] == 0) {
passing += west;
if (passing > 1000000000) return -1;
} else west += 1;
}
return passing;
}
function solution(A, B, K) {
let count = 0;
if (K > B) count = A == 0 ? 1 : 0;
const min = A >= K ? A + (A % K) : K;
const max = B - (B % K);
count = (max - min + K) / K + (A == 0 ? 1 : 0);
return parseInt(count);
}
function solution(S, P, Q) {
let dna = "";
let res = [];
for (let i = 0; i < P.length; i++) {
dna = S.slice(P[i], Q[i] + 1);
if (dna.indexOf("A") !== -1) res.push(1);
else if (dna.indexOf("C") !== -1) res.push(2);
else if (dna.indexOf("G") !== -1) res.push(3);
else res.push(4);
}
return res;
}