Integers K, M and a non-empty array A consisting of N integers, not bigger than M, are given.
The leader of the array is a value that occurs in more than half of the elements of the array, and the segment of the array is a sequence of consecutive elements of the array.
You can modify A by choosing exactly one segment of length K and increasing by 1 every element within that segment.
The goal is to find all of the numbers that may become a leader after performing exactly one array modification as described above.
Write a function:
function solution(K, M, A);
that, given integers K and M and an array A consisting of N integers, returns an array of all numbers that can become a leader, after increasing by 1 every element of exactly one segment of A of length K. The returned array should be sorted in ascending order, and if there is no number that can become a leader, you should return an empty array. Moreover, if there are multiple ways of choosing a segment to turn some number into a leader, then this particular number should appear in an output array only once.
For example, given integers K = 3, M = 5 and the following array A:
A[0] = 2 A[1] = 1 A[2] = 3 A[3] = 1 A[4] = 2 A[5] = 2 A[6] = 3the function should return [2, 3]. If we choose segment A[1], A[2], A[3] then we get the following array A:
A[0] = 2 A[1] = 2 A[2] = 4 A[3] = 2 A[4] = 2 A[5] = 2 A[6] = 3and 2 is the leader of this array. If we choose A[3], A[4], A[5] then A will appear as follows:
A[0] = 2 A[1] = 1 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = 3 A[6] = 3and 3 will be the leader.
And, for example, given integers K = 4, M = 2 and the following array:
A[0] = 1 A[1] = 2 A[2] = 2 A[3] = 1 A[4] = 2the function should return [2, 3], because choosing a segment A[0], A[1], A[2], A[3] and A[1], A[2], A[3], A[4] turns 2 and 3 into the leaders, respectively.
Write an efficient algorithm for the following assumptions:
- N and M are integers within the range [1..100,000];
- K is an integer within the range [1..N];
- each element of array A is an integer within the range [1..M].
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies =
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = [];
A.forEach(el => tallies[])
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = [];
A.forEach(el => tallies[el] = tallies[el])
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = [];
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = [];
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = [];
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = [];
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = [];
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = [];
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies)
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// given a segment K long, there's a limited number of cases
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// given a segment K long, there's a limited number of cases
let k;
for ()
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
// make a copy
A
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// given a segment K long, there's a limited number of cases
let k;
for ()
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
// make a copy
A = [...A];
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// given a segment K long, there's a limited number of cases
let k;
for ()
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for ()
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < len; k++)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let answers = [];
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let answers = [];
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let answers = [];
for (k)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let answers = [];
for (k = 1; )
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let answers = [];
for (k = 0; )
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let answers = [];
for (k = 0; ; k++)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; ; k++)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k; k++)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
// ready for next one
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
// ready for next one
A[]
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
// ready for next one
A[k]--;
A[k + K]
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
if (findLeader(A))
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
if ()
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if ()
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers = [];
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers.push(leader);
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers.push(leader);
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[.push(]leader);
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return Object.keys(answers).sort();
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return Object.keys(answers).sort();
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return Object.keys(answers).sort();
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, last;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return Object.keys(answers).sort();
}
// testing
SyntaxError: Identifier 'last' has already been declared at solutionWrapper (/tmp/exec.js:413:28) at Promise.resolve.then (/tmp/exec.js:439: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:3
SyntaxError: Identifier 'last' has already been declared at solutionWrapper (/tmp/exec.js:413:28) at Promise.resolve.then (/tmp/exec.js:439: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:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k, ;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return Object.keys(answers).sort();
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return Object.keys(answers).sort();
}
// testing
Invalid result type, integer expected, 'string' found
Invalid result type, integer expected, 'string' found
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return Object.keys(answers).sor
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
Object.keys(answers);
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
let ans = Object.keys(answers);
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
let ans = Object.keys(answers);
answers[0] =
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
let ans = Object.keys(answers).map(a => Number(a)));
answers[0] =
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
let ans = Object.keys(answers).map(a => Number(a));
answers[0] =
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 &&
answers[0] =
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
}
answers[0] =
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
{ans[0], ans[1]} = an
}
answers[0] =
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
return
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
{ans[0], ans[1]} = ans;
}
return ans
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
{ans[0], ans[1]} = ans;
}
return ans
}
// testing
solution.js:43 {ans[0], ans[1]} = ans; ^ SyntaxError: Unexpected token = at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:403:29) at Promise.resolve.then (/tmp/exec.js:437:34) 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:3
solution.js:43 {ans[0], ans[1]} = ans; ^ SyntaxError: Unexpected token = at createScript (vm.js:80:10) at Object.runInNewContext (vm.js:135:10) at getSolution (/tmp/exec.js:403:29) at Promise.resolve.then (/tmp/exec.js:437:34) 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:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
let t = ans[0];
ans[0] = ans[1];
{ans[0], ans[1]} = ans;
}
return ans
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
let t = ans[0];
ans[0] = ans[1];
ans[1] = t;
}
return ans
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
let t = ans[0];
ans[0] = ans[1];
ans[1] = t;
}
return ans
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
let t = ans[0];
ans[0] = ans[1];
ans[1] = t;
}
return ans
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
let t = ans[0];
ans[0] = ans[1];
ans[1] = t;
}
return ans
}
// testing
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// examine the array A and return whatever value is a leader, or nUll if none
function findLeader(A) {
let tallies = {};
A.forEach(el => tallies[el] = (tallies[el] || 0) + 1);
// now search for someone with more than A.length/2
let half = A.length / 2;
for (el in tallies) {
if (tallies[el] > half)
return el;
}
return null;
}
function solution(K, M, A) {
// write your code in JavaScript (Node.js 8.9.4)
// make a copy
A = [...A];
// given a segment K long, there's a limited number of cases
let k;
for (k = 0; k < K; k++)
A[k]++;
// so we'll try all combinations
let last = A.length - K;
let answers ={};
for (k = 0; k <= last; k++) {
let leader = findLeader(A);
if (leader !== null)
answers[leader] = true;
// ready for next one
A[k]--;
A[k + K]++;
}
let ans = Object.keys(answers).map(a => Number(a));
if (ans.length >= 2 && ans[0] > ans[1]) {
let t = ans[0];
ans[0] = ans[1];
ans[1] = t;
}
return ans
}
// testing
The following issues have been detected: timeout errors.
medium tests (N = 10000, M = 100)
running time: 2.892 sec., time limit: 0.352 sec.
medium tests(N >= 20000, M=30000)
Killed. Hard limit reached: 6.000 sec.