Tasks Details
easy
1.
AbsDistinct
Compute number of distinct absolute values of sorted array elements.
Task Score
100%
Correctness
100%
Performance
100%
A non-empty array A consisting of N numbers is given. The array is sorted in non-decreasing order. The absolute distinct count of this array is the number of distinct absolute values among the elements of the array.
For example, consider array A such that:
A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6The absolute distinct count of this array is 5, because there are 5 distinct absolute values among the elements of this array, namely 0, 1, 3, 5 and 6.
Write a function:
function solution(A);
that, given a non-empty array A consisting of N numbers, returns absolute distinct count of array A.
For example, given array A such that:
A[0] = -5 A[1] = -3 A[2] = -1 A[3] = 0 A[4] = 3 A[5] = 6the function should return 5, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647];
- array A is sorted in non-decreasing order.
Copyright 2009–2025 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
Solution
Programming language used JavaScript
Time spent on task 37 minutes
Notes
not defined yet
Code: 08:35:09 UTC,
java,
autosave
Code: 08:42:47 UTC,
js,
autosave
Code: 08:43:09 UTC,
js,
autosave
Code: 08:43:26 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
}
}
}
Code: 08:43:56 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i]
}
}
}
Code: 08:44:06 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
}
Code: 08:44:43 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let
}
Code: 08:44:53 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let
}
Code: 08:45:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let
}
Code: 08:45:31 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
}
Code: 08:46:02 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
while (left >= 0 && right <= A.length)
}
Code: 08:46:32 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && last)
}
}
Code: 08:46:57 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 0;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
}
}
}
Code: 08:47:27 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 & A[left])
}
}
Code: 08:47:41 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
}
}
}
Code: 08:48:11 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
l
}
}
}
Code: 08:48:42 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length)
}
}
Code: 08:49:02 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left])
}
}
Code: 08:49:12 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
}
}
}
Code: 08:50:18 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[left];
left--;
nextVal = A[left];
}
}
}
Code: 08:50:35 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
}
Code: 08:50:49 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
}
}
}
Code: 08:51:15 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
}
}
}
Code: 08:51:25 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
}
Code: 08:52:35 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
ret
}
Code: 08:52:45 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
return total;
}
Code: 08:53:13 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, count')
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
return total;
}
Code: 08:53:26 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count')
if (A[left] !== A[right] && lastVal !== newVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
return total;
}
Code: 08:53:36 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:')
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
return total;
}
Code: 08:53:42 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
return total;
}
Analysis
expand all
Example tests
1.
0.356 s
RUNTIME ERROR,
tested program terminated with exit code 153
stdout:
-> left, right, lastVal, nextVal, count: 3 3 0 0 1 -> left, right, lastVal, nextVal, count: 2 3 0 1 1 -> left, right, lastVal, nextVal, count: 2 4 0 3 2 -> left, right, lastVal, nextVal, count: 1 4 1 3 3 -> left, right, lastVal, nextVal, count: 0 4 3 5 3 -> left, right, lastVal, nextVal, count: 0 5 3 6 4 -> left, right, lastVal, nextVal, count: 0 5 3 6 5 -> left, right, lastVal, nextVal, count: 0 5 3 6 6 -> left, right, lastVal, nextVal, count: 0 5 3 6 7 -> left, right, lastVal, nextVal, count: 0 5 3 6 8 -> left, right, lastVal, nextVal, count: 0 5 3 6 9 -> left, right, lastVal, nextVal, count: 0 5 3 6 10 -> left, right, lastVal, nextVal, count: 0 5 3 6 11 -> left, right, lastVal, nextVal, count: 0 5 3 6 12 -> left, right, lastVal, nextVal, count: 0 5 3 6 13 -> left, right, lastVal, nextVal, count: 0 5 3 6 14 -> left, right, lastVal, nextVal, count: 0 5 3 6 15 -> left, right, lastVal, nextVal, count: 0 5 3 6 16 -> left, right, lastVal, nextVal, count: 0 5 3 6 17 -> left, right, lastVal, nextVal, count: 0 5 3 6 18 -> left, right, lastVal, nextVal, count: 0 5 3 6 19 -> left, right, lastVal, nextVal, count: 0 5 3 6 20 -> left, right, lastVal, nextVal, count: 0 5 3 6 21 -> left, right, lastVal, nextVal, count: 0 5 3 6 22 -> left, right, lastVal, nextVal, count: 0 5 3 6 23 -> left, right, lastVal, nextVal, count: 0 5 3 6 24 -> left, right, lastVal, nextVal, count: 0 5 3 6 25 -> left, right, lastVal, nextVal, count: 0 5 3 6 26 -> left, right, lastVal, nextVal, count: 0 5 3 6 27 -> left, right, lastVal, nextVal, count: 0 5 3 6 28 -> left, right, lastVal, nextVal, count: 0 5 3 6 29 -> left, right, lastVal, nextVal, count: 0 5 3 6 30 -> left, right, lastVal, nextVal, count: 0 5 3 6 31 -> left, right, lastVal, nextVal, count: 0 5 3 6 32 -> left, right, lastVal, nextVal, count: 0 5 3 6 33 -> left, right, lastVal, nextVal, count: 0 5 3 6 34 -> left, right, lastVal, nextVal, count: 0 5 3 6 35 -> left, right, lastVal, nextVal, count: 0 5 3 6 36 -> left, right, lastVal, nextVal, count: 0 5 3 6 37 -> left, right, lastVal, nextVa
Code: 08:58:34 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
}
}
return total;
}
Code: 08:58:44 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return total;
}
Code: 08:58:54 UTC,
js,
verify,
result: Failed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return total;
}
Analysis
expand all
Example tests
1.
0.072 s
RUNTIME ERROR,
tested program terminated with exit code 1
stderr:
ReferenceError: total is not defined at solution (solution.js:52:5) at solutionWrapper (/tmp/exec.js:402:28) at Promise.resolve.then (/tmp/exec.js:428:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3stdout:
-> left, right, lastVal, nextVal, count: 3 3 0 0 1 -> left, right, lastVal, nextVal, count: 2 3 0 1 1 -> left, right, lastVal, nextVal, count: 2 4 0 3 2 -> left, right, lastVal, nextVal, count: 1 4 1 3 3 -> left, right, lastVal, nextVal, count: 0 4 3 5 3 -> left, right, lastVal, nextVal, count: 0 5 3 6 4
Code: 08:59:06 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Analysis
expand all
Example tests
1.
0.072 s
OK
stdout:
-> left, right, lastVal, nextVal, count: 3 3 0 0 1 -> left, right, lastVal, nextVal, count: 2 3 0 1 1 -> left, right, lastVal, nextVal, count: 2 4 0 3 2 -> left, right, lastVal, nextVal, count: 1 4 1 3 3 -> left, right, lastVal, nextVal, count: 0 4 3 5 3 -> left, right, lastVal, nextVal, count: 0 5 3 6 4
Code: 08:59:19 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Code: 08:59:33 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Code: 09:00:03 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Code: 09:00:16 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Code: 09:00:26 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
User test case 1:
[-1, 1]
User test case 2:
[-1, 0, 1]
User test case 3:
[-2147483648, 0, 2147483647]
Analysis
expand all
Example tests
1.
0.072 s
OK
stdout:
-> left, right, lastVal, nextVal, count: 3 3 0 0 1 -> left, right, lastVal, nextVal, count: 2 3 0 1 1 -> left, right, lastVal, nextVal, count: 2 4 0 3 2 -> left, right, lastVal, nextVal, count: 1 4 1 3 3 -> left, right, lastVal, nextVal, count: 0 4 3 5 3 -> left, right, lastVal, nextVal, count: 0 5 3 6 4
expand all
User tests
1.
0.072 s
OK
function result: 1
function result: 1
stdout:
-> left, right, lastVal, nextVal, count: 1 1 1 1 1 -> left, right, lastVal, nextVal, count: 0 1 1 1 1
1.
0.072 s
OK
function result: 2
function result: 2
stdout:
-> left, right, lastVal, nextVal, count: 1 1 0 0 1 -> left, right, lastVal, nextVal, count: 0 1 0 1 1 -> left, right, lastVal, nextVal, count: 0 2 0 1 2
1.
0.072 s
OK
function result: 3
function result: 3
stdout:
-> left, right, lastVal, nextVal, count: 1 1 0 0 1 -> left, right, lastVal, nextVal, count: 0 1 0 2147483648 1 -> left, right, lastVal, nextVal, count: 0 2 0 2147483647 2
Code: 09:00:43 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Code: 09:00:54 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
User test case 1:
[-1, 1]
User test case 2:
[-1, 0, 1]
User test case 3:
[-2147483648, 0, 2147483647]
User test case 4:
[[-2147483648, -2147483647, 0, 2147483647]]
Analysis
expand all
Example tests
1.
0.080 s
OK
stdout:
-> left, right, lastVal, nextVal, count: 3 3 0 0 1 -> left, right, lastVal, nextVal, count: 2 3 0 1 1 -> left, right, lastVal, nextVal, count: 2 4 0 3 2 -> left, right, lastVal, nextVal, count: 1 4 1 3 3 -> left, right, lastVal, nextVal, count: 0 4 3 5 3 -> left, right, lastVal, nextVal, count: 0 5 3 6 4
expand all
User tests
1.
0.080 s
OK
function result: 1
function result: 1
stdout:
-> left, right, lastVal, nextVal, count: 1 1 1 1 1 -> left, right, lastVal, nextVal, count: 0 1 1 1 1
1.
0.084 s
OK
function result: 2
function result: 2
stdout:
-> left, right, lastVal, nextVal, count: 1 1 0 0 1 -> left, right, lastVal, nextVal, count: 0 1 0 1 1 -> left, right, lastVal, nextVal, count: 0 2 0 1 2
1.
0.080 s
OK
function result: 3
function result: 3
stdout:
-> left, right, lastVal, nextVal, count: 1 1 0 0 1 -> left, right, lastVal, nextVal, count: 0 1 0 2147483648 1 -> left, right, lastVal, nextVal, count: 0 2 0 2147483647 2
1.
0.001 s
RUNTIME ERROR,
invalid input, unexpected '[', expecting integer
Code: 09:01:05 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
User test case 1:
[-1, 1]
User test case 2:
[-1, 0, 1]
User test case 3:
[-2147483648, 0, 2147483647]
User test case 4:
[-2147483648, -2147483647, 0, 2147483647]
Analysis
expand all
Example tests
1.
0.072 s
OK
stdout:
-> left, right, lastVal, nextVal, count: 3 3 0 0 1 -> left, right, lastVal, nextVal, count: 2 3 0 1 1 -> left, right, lastVal, nextVal, count: 2 4 0 3 2 -> left, right, lastVal, nextVal, count: 1 4 1 3 3 -> left, right, lastVal, nextVal, count: 0 4 3 5 3 -> left, right, lastVal, nextVal, count: 0 5 3 6 4
expand all
User tests
1.
0.072 s
OK
function result: 1
function result: 1
stdout:
-> left, right, lastVal, nextVal, count: 1 1 1 1 1 -> left, right, lastVal, nextVal, count: 0 1 1 1 1
1.
0.072 s
OK
function result: 2
function result: 2
stdout:
-> left, right, lastVal, nextVal, count: 1 1 0 0 1 -> left, right, lastVal, nextVal, count: 0 1 0 1 1 -> left, right, lastVal, nextVal, count: 0 2 0 1 2
1.
0.072 s
OK
function result: 3
function result: 3
stdout:
-> left, right, lastVal, nextVal, count: 1 1 0 0 1 -> left, right, lastVal, nextVal, count: 0 1 0 2147483648 1 -> left, right, lastVal, nextVal, count: 0 2 0 2147483647 2
1.
0.072 s
OK
function result: 3
function result: 3
stdout:
-> left, right, lastVal, nextVal, count: 2 2 0 0 1 -> left, right, lastVal, nextVal, count: 1 2 0 2147483647 1 -> left, right, lastVal, nextVal, count: 1 3 0 2147483647 2 -> left, right, lastVal, nextVal, count: 0 3 2147483647 2147483648 2
Code: 09:01:14 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Code: 09:01:25 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
console.log('-> left, right, lastVal, nextVal, count:', left, right, lastVal, nextVal, count)
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Code: 09:01:35 UTC,
js,
autosave
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Code: 09:02:22 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
User test case 1:
[-1, 1]
User test case 2:
[-1, 0, 1]
User test case 3:
[-2147483648, 0, 2147483647]
User test case 4:
[-2147483648, -2147483647, 0, 2147483647]
Analysis
expand all
User tests
1.
0.068 s
OK
function result: 1
function result: 1
1.
0.068 s
OK
function result: 2
function result: 2
1.
0.068 s
OK
function result: 3
function result: 3
1.
0.068 s
OK
function result: 3
function result: 3
Code: 09:11:18 UTC,
js,
verify,
result: Passed
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
User test case 1:
[-1, 1]
User test case 2:
[-1, 0, 1]
User test case 3:
[-2147483648, 0, 2147483647]
User test case 4:
[-2147483648, -2147483647, 0, 2147483647]
Analysis
expand all
User tests
1.
0.068 s
OK
function result: 1
function result: 1
1.
0.068 s
OK
function result: 2
function result: 2
1.
0.072 s
OK
function result: 3
function result: 3
1.
0.068 s
OK
function result: 3
function result: 3
Code: 09:11:20 UTC,
js,
final,
score: 
100
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
if (A.length === 1){
return 1;
}
let left = 0;
let right = 0;
for (let i = 0; i < A.length; i++) {
if (A[i] >= 0) {
left = i;
right = i;
break;
} else {
A[i] *= -1;
}
}
let lastVal = A[left];
let nextVal = lastVal;
let count = 1;
while (left >= 0 && right < A.length) {
if (A[left] !== A[right] && lastVal !== nextVal) {
count++;
}
if (left > 0 && A[left] < A[right]) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1 && A[right] < A[left]) {
lastVal = A[right];
right++;
nextVal = A[right];
} else if (left > 0) {
lastVal = A[left];
left--;
nextVal = A[left];
} else if (right < A.length - 1) {
lastVal = A[right];
right++;
nextVal = A[right];
} else {
break;
}
}
return count;
}
Analysis summary
The solution obtained perfect score.
Analysis
Detected time complexity:
O(N) or O(N*log(N))
expand all
Correctness tests
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
4.
0.072 s
OK
5.
0.068 s
OK
6.
0.068 s
OK
7.
0.068 s
OK
8.
0.080 s
OK
9.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
1.
0.068 s
OK
2.
0.068 s
OK
3.
0.068 s
OK
1.
0.068 s
OK
1.
0.068 s
OK