divisibilityTest.js

 /*Divisibility Test

Description
Given an array of positive integers of size N and an integer x.
Count the number of elements in the array that are divisible by x.

Input format
First line contains two space separated positive integers n and x,
where n denotes the number of elements in the array. Second line contains
n space separated elements of array.

Output format
For the given array, print the count of numbers that are divisible by x.

Sample input-1
5 5
1 5 6 10 9
Sample output-1
2
Sample input-2
4 7
11 15 22 100
Sample output-2
0
*/

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();
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////

const array = readLine().split(' '); // [ '5', '5' ] green color
//console.log(array);
const noOfIntegers = parseInt(array[0]); // yellow color 5
//console.log(noOfIntegers);
const integer = parseInt(array[1]); // yellow color 5
//console.log(integer);
let count = 0; // intially the count is 0

const array2 = readLine().split(' '); // [ '1', '5', '6', '10', '9' ] green color
//console.log(array2)

for (let i = 0; i < noOfIntegers; i++) //1.(i=0<5 true), 2.(i=1<5 true), 3.(i=2<5 true),
4.(i=3<5 true), 5.(i=4<5 true),6(i=6<5 false exit the loop
{

if (parseInt(array2[i]) % integer == 0)// 1.(1%5=0 false), 2.(5%5=0 true), 3.(6%5=0 false),
4.(10%5=0 true), 5.(9%5=0 false)
{
count++; // 1,2

}

}

console.log(count); // 2

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




Comments