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].
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total <)
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total -)
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount > )
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < )
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount)
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
isLeftDone = tru
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
cons
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('9=======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
9======= 0 1 2 9======= 0 1 9======= 0 1 2
9======= 0 1 9======= 0 1
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
====== 0 1 2 ====== 0 1 ====== 0 1 2
====== 0 1 ====== 0 1
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
====== 0 1 2 ====== 0 1 ====== 0 1 2
====== 0 1 ====== 0 1
function result: []
====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
====== 0 1 2 ====== 0 1 ====== 0 1 2
====== 0 1 ====== 0 1
function result: []
====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
function result: [1]
====== 0 1 2 3 4 5 6 7 8 9
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[3, 5, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
====== 0 1 2 ====== 0 1 ====== 0 1 2
====== 0 1 ====== 0 1
function result: []
====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
function result: [1]
====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[3, 5, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
consol
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
console.log({l})
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
{ l: 7 } ====== 0 1 2 ====== 0 1 ====== 0 1 2
{ l: 5 } ====== 0 1 ====== 0 1
function result: []
{ l: 117 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
function result: [1]
{ l: 351 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[3, 5, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
console.log({l})
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
{ l: 7 } ====== 0 1 2 ====== 0 1 ====== 0 1 2
{ l: 5 } ====== 0 1 ====== 0 1
function result: []
{ l: 117 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
function result: [1]
{ l: 702 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[3, 5, [1, 1, 1,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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
console.log({l})
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
{ l: 7 } ====== 0 1 2 ====== 0 1 ====== 0 1 2
{ l: 5 } ====== 0 1 ====== 0 1
function result: []
{ l: 117 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
function result: [1]
{ l: 1020 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
console.log({l})
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
{ l: 7 } ====== 0 1 2 ====== 0 1 ====== 0 1 2
{ l: 5 } ====== 0 1 ====== 0 1
function result: []
{ l: 117 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
function result: [1]
{ l: 1020 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[3, 5, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
console.log({l})
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
{ l: 7 } ====== 0 1 2 ====== 0 1 ====== 0 1 2
{ l: 5 } ====== 0 1 ====== 0 1
function result: []
{ l: 117 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
function result: [1]
{ l: 1020 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[3, 5,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{
let detailedArr = [];
const l = A.length;
console.log({l})
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
{ l: 7 } ====== 0 1 2 ====== 0 1 ====== 0 1 2
{ l: 5 } ====== 0 1 ====== 0 1
function result: []
{ l: 117 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
function result: [1]
{ l: 921 } ====== 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1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; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
console.log('======')
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
let x =0;
while(true) {
console.log(x++);
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
function result: []
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
function result: [1]
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3,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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[100, 5, [1, 1, 1, 1, 1, 1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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[200, 5, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,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, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[700, 5, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 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 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
[3, 5, [2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2, 2, 1, 3, 1, 2, 2, 3, 2, 1, 3, 1, 2, 2]]
[700, 5, [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1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1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
function solution(K, M, A) {
let detailedArr = [];
const l = A.length;
for (let x = 0; x < A.length; x++) {
detailedArr.push({ index: x, value: A[x] })
}
let resultSet = [];
let newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let x = 0; x < A.length; x++) {
const firstIndex = x;
const lastIndex = x + K - 1;
if (lastIndex < A.length) {
newArray = detailedArr.map(item => {
return { index: item.index, value: item.value }
});
for (let y = firstIndex; y <= lastIndex; y++) {
newArray[y].value++;
}
newArray.sort((a,b) => {
return a.value - b.value;
});
const middleIndex = l % 2 === 0 ? l / 2 : (l - 1) / 2;
const candidateValue = newArray[middleIndex].value;
if (!resultSet.includes(candidateValue) && isLeader(newArray, candidateValue, middleIndex)) {
resultSet.push(candidateValue);
}
}
else {
break;
}
}
resultSet.sort((a,b) => {
return a - b;
})
return resultSet;
}
function isLeader(sortedArr, candidateValue, middleIndex) {
const leaderCountRequirement = sortedArr.length % 2 === 0 ? Math.round(sortedArr.length / 2) + 1: Math.round(sortedArr.length / 2);
let isLeftDone = false;
let isRightDone = false;
let leftIndex = middleIndex - 1;
let rightIndex = middleIndex + 1;
let totalCandidateCount = 1;
const total = sortedArr.length;
while(true) {
const lValue = sortedArr[leftIndex] ? sortedArr[leftIndex].value : '';
const rValue = sortedArr[rightIndex] ? sortedArr[rightIndex].value : '';
if (lValue === candidateValue) {
totalCandidateCount++;
}
else {
isLeftDone = true;
}
if (rValue === candidateValue){
totalCandidateCount++;
}
else {
isRightDone = true;
}
leftIndex--;
rightIndex++;
if (total - totalCandidateCount < totalCandidateCount) {
return true;
}
if (isLeftDone && isRightDone) {
break;
}
if (leftIndex <= 0) {
isLeftDone = true;
}
if (rightIndex >= sortedArr.length - 1) {
isRightDone = true;
}
}
return totalCandidateCount >= leaderCountRequirement;
}
The following issues have been detected: timeout errors.
medium tests (N = 10000, M = 100)
running time: 4.164 sec., time limit: 0.336 sec.
medium tests(N >= 20000, M=30000)
Killed. Hard limit reached: 6.000 sec.