My issue
Time limit exceeded…
Here i am using O(n^2) time complexity
how can i reduce my time complexity for this problem
My code
// your code goes here
var count = 0
function processData(input){
var T = Number(input.split("\n")[0])
for(let i = 1; i <= T*2;){
var arr = input.split("\n")[i+1].split(" ").sort((a,b)=>a-b)
var gcd = findGCD(arr)
var count = counting(arr, gcd)
console.log(arr.length - count)
i += 2
}
}
function counting(arr, gcd){
var count = 0
for(let i = 0; i < arr.length; i++){
if(arr[i] == gcd){
count++
}
}
return count
}
function gcd(a, b){
if(a == 0)
return b;
return gcd(b % a, a);
}
function findGCD(arr){
count = 0
let result = 0
for(let i = 0; i < arr.length; i++){
result = gcd(arr[i], result);
if(result == arr[i]){
count++
}
if(result == 1){
return 1
// count--
}
}
return result;
}
process.stdin.resume();
process.stdin.setEncoding("ascii");
_input = "";
process.stdin.on("data", function (input) {
_input += input;
});
process.stdin.on("end", function () {
processData(_input);
});
Learning course: Number theory
Problem Link: Luigi and Uniformity Practice Problem in Introduction to Number theory - CodeChef