TNQT Digital 2 Coding Question

I have tried that method too and you’ll get TLE.

Just click Run button on bottom left, after taking m and n I modified m=2 and n=10e9.
It showed me 0/7 Test cases passed and showed me output for 4 40 public test case, Expected output and my output

Well I passed all 7 Testcases using C++, there isn’t any compiler issue as people are saying.

1 Like

Yeah, there was no compiler problem. Can you tell us the algorithm you used to solve it?

Already told above my friend.

simple traversal and check

  1. Check all prime pairs from 5 with distance of six… Like 5,11,17,23… There will be some point where there will be no more sexy primes.
  2. Repeat above with starting from 7.
  3. Add count no of pairs from step 1 and 2 from 0 till B… There will be some point when the count would no longer increase.
  4. Add count no of pairs from step 1 and 2 from 0 till A-1…
  5. Subtract Step 3 - step 4

this gave TLE to everyone!

1 Like

This 6n+1 or 6n-1 thing is not a concrete logic. Sieve was the right approach to the problem. People who got 7 out of 7 cases passed using sieve were able to do it because the test cases in TCS digital never crosses the limit 10^7.

1 Like

any idea , when the results of the test will be out ?

The same thing happened to me!! Though I have cleared the Digital test.

This is the solution in NodeJS. I hope it helps

process.stdin.resume()
process.stdin.setEncoding(‘utf-8’)

var stdin_input = ‘’

process.stdin.on(‘data’, (input) => {
stdin_input += input
})

process.stdin.on(‘end’, () => {
main(stdin_input)
})

function main(input){
const data = input.split(’ ')
const range1 = parseInt(data[0])
const range2 = parseInt(data[1])

const primesArr = []

for(var i=range1; i<=range2; i++){
    if(i > 1 && isPrime(i)) primesArr.push(parseInt(i))
}

let count = 0
for(var a=0; a<primesArr.length; a++){
    for(var j=a+1; j<primesArr.length; j++){
     if(primesArr[j]-primesArr[a] === 6){
         count++
         break;
     }
    }
}
console.log(count)

function isPrime(num){
    if(num === 2) return true
    for(var i=2; i<num; i++){
        if(num%i===0) return false
    }
    return true
}

}

Thanks