maxdial.js with less dry run

 /*Max Dial

Description
Imagine a series of n dials. They are all set to zero initially.
An input i arrives. The i-th dial is incremented by 1. An input j arrives.
The j-th dial is incremented by 1. You are also given a max value.
Given a sequence of such inputs, find which dial reaches the max value first.

Input format
First line contains n, n >= 0, denoting the number of dials.
Second line contains max, max > 0, denoting the max limit for the dial.
Third line contains m, m >= 0, denoting the number of inputs.
This is followed by m lines each containing a positive integer i, 1 <= i <= n.

Output format
A positive integer i, denoting that ith dial reaches maximum first.
If none reaches maximum limit, then output should be 0.

Sample input
3
2
4
1
2
1
3
Sample output
1
Explanation
First line is 3, i.e. 3 dials. Second line is 2, i.e. max dial limit is 2.
Third line is 4, i.e. 4 inputs arriving. 1,2,1,3 are the inputs.
The dial 1 reaches 2 first. So output is 1
*/

let fs = require("fs");

let data = fs.readFileSync(0, 'utf-8');
let idx = 0;
data = data.split('\n');

function readLine() {
idx++;
return data[idx - 1].trim();
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////

// This part is only for taking the input in array form

let n = parseInt(readLine()); // 3 is the no of dials

let dialsArray = []; // we will push all the dials here

for (let i = 0; i < n; i++) {
dialsArray.push(0);

// console.log(dialsArray);
// [ 0 ] when i=0
// [ 0, 0 ] when i=1
// [ 0, 0, 0 ] when i=2 , all yellow color elements
}

const max = parseInt(readLine()); // 2 is the max limit a dial can be pressed

const m = parseInt(readLine()); // 4 is the total number of inputs

let actualInputs = []; // we will push all the inputs in this array

for (let i = 0; i < m; i++) {

actualInputs.push(parseInt(readLine()));

//OR
// actualInputs[i] = parseInt(readLine());
// console.log(actualInputs)
// [ 1 ] when i=0
// [ 1, 2 ] when i=1
// [ 1, 2, 1 ] when i=2
// [ 1, 2, 1, 3 ] when i=3
}

// From here the actual operations Starts

let ans = 0; // we are assuming that no dials has has been pressed max times, i.e 2 times

for (let i = 0; i < actualInputs.length; i++) {

let currentInputPressed = actualInputs[i];

dialsArray[currentInputPressed - 1]++;

if (dialsArray[currentInputPressed - 1] == max) {
ans = currentInputPressed;

break;
}

}

console.log(ans);

////////////////////////////////////////////////////////////////////////////////////////////

TERMINAL:




Comments