maxdial.js with lots of dry run (*** please please please REVISE***)

 /*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 dial inputs

let actualInputs = []; // we will push all the dial 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++) // 0) i=0<4, true go inside the forloop
// 1) i=1<4 , true go inside the forloop
// 2) i=2<4, true go inside the forloop


{

let currentInputPressed = actualInputs[i]; //0) actualInputs[0] = 1
// 1) actualInputs [1] = 2
// 2) actualInputs [2] = 1



dialsArray[currentInputPressed - 1]++; // 0) dialsArray[1-1] => dialsArray[0]++ = 0++
i.e., 1
// 1) dialsArray[2-1] => dialsArray[1]++ = 0++
i.e., 1
// 2) dialsArray[1-1] => dialsArray[0]++ = 1++
i.e., 2 because dialsArray[0] was updated
to 1 in the first iteration.



if (dialsArray[currentInputPressed - 1] == max) // 0) dialsArray[0] is 1 == 2 false don't
go in the if condition and continue
forloop with i++, i.e,. i is 1 now.

// 1) dialsArray[1] is 1 == 2 false don't
go in the if statement and continue
forloop with i++, i.e., i is 2 now

// 2) dialsArray[1] is 2 ==2 true go in the
if condition.


{
ans = currentInputPressed; // 2) ans = currentInputPressed is actualInputs[2]
i.e., 1

break; // 2) now break and completely come out of the
forloop and console the ans

}

}

console.log(ans); // 2) ans = 1

//////////////////////////////////////////////////////////////////////////////////////////////
TERMINAL:


Comments