Problem 21. linearsearch1.js ( similar like count++ problem)
/*Question
Given a sequence of integers and a query integer, and query integer is
the first integer
find out how many times the query integer is present
Input:
First line containing a positive integer , say q, denoting the length of seq.
2nd line contains a positive integer say n, denoting the query integer.
Next n line contains one integer each, denoting the elements of the sequence.
Output:
One line containing the number of time query integer is present.
Example Input:
5
67
23
24
67
67
Output:
3
Algorithm:
1.Read the length of the sequence.
2.Read the query number.
3.Run loop till length of the sequence.
4.Read the number from the sequence and check if equal to the query integer.
5.If found increment the count variable.
*/
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();
}
//..............................................................................................................................
let lengthOfSequence = parseInt(readLine()); // 5
let queryInteger = parseInt(readLine()); // 67
let count = 1; // because we have already counted the query integer once.
let i = 1; // intilization of while loop.
while (i < lengthOfSequence)// not <=, because count=1 and we need to check 4 times only
{
let integers = parseInt(readLine());
if (integers == queryInteger) {
count++;
}
i++ // increment of while loop
}
console.log(count);
TERMINAL:
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$ node linearsearch1.js <input.txt
3
Nobins-MacBook-Pro:STOC_MODULE 11SEPT 2022 nobinpunyo$
Comments
Post a Comment