Inti Sets: Hackerank
The input numbers \$ A, B, N \$ can be as large as \$ 10^{12} \$, which
means that a Java int (which is a 32-bit signed integer) is not large
enough to hold all possible values, you should use long instead.

You also do not reduce the sum modulo 1000000007, as required.

The main function can be simplified by calculating the output for each
input line as it is read, this makes the queries array obsolete.

The function sumBetweenRange does not describe what the function actually does.
Something like sumOfIntiNumbersInRange or – using the mathematical term –
sumOfCoprimeNumbersInRange might be a better choice.

The main function would then look like this:

public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

    Scanner console = new Scanner(System.in);

    int queries = Integer.parseInt(console.nextLine());

    for (int i = 0; i &lt; queries; i++) {
        long N = console.nextLong();
        long A = console.nextLong();
        long B = console.nextLong();

        long result = sumOfCoprimeNumbersInRange(A, B, N);
        System.out.println(result);
    }
}


Your method to determine if two numbers have a common factor is
very slow, it does trial division for all numbers between 2 and
no1/2. That can be improved slightly by using sqrt(no1) as the upper bound,
but one can do much better.

An efficient method is the
Euclidean algorithm
which determines the
greatest common divisor
of two integers. Two integers \$ a, b \$ have no common divisor exactly if \$ \gcd(a, b) = 1 \$. 

There are many known Java functions for calculation the 
greatest common divisor, see for example https://rosettacode.org/wiki/Greatest_common_divisor#Java.
Here is a simple (tail-recursive) implementation:

public static long gcd(long a, long b) {
    if (b == 0) {
        return a;
    } else {
        return gcd(b, a % b);
    }
}


Then sumOfCoprimeNumbersInRange() can be implemented as

private static final int MOD = 1000000007;

// Compute the sum (modulo MOD) of all numbers in the range a...b which are coprime to N.
private static long sumOfCoprimeNumbersInRange(long a, long b, long n) {
    long sum = 0;

    for(long i = a; i &lt;= b; i++){
        if (gcd(i, n) == 1) {
            sum = (sum + i) % MOD;
        }
    }
    return sum;
}


This is already much faster than your original implementation:
For \$ N=100000, A=10000, B=90000 \$ I measured on a 
1.2 GHz Intel Core m5 MacBook:


Your code: 3 seconds.
GCD-based solution: 0.165 seconds.


But it may be not fast enough for the full range of numbers up to 
\$ 10^{12} \$. A very elegant and efficient approach using the
Inclusion-Exclusion Principle is explained in


How to count co-prime to n in a range [1,x] where x can be different from n?


on Stack Overflow.
Let us consider \$ N = 300 = 2^2  \cdot 3 \cdot 5^2 \$ as an example. The distinct prime 
factors are \$ 2, 3, 5 \$, and to compute the sum of all numbers in some range which
are coprime to \$ N \$ one proceeds as follows:


Compute the sum of all numbers in the range.
Subtract the sum of all multiples of \$ 2, 3 \$, and \$ 5 \$ in the range.
Add the sum of all multiples of \$ 2 \cdot 3, 2 \cdot 5 \$, and \$ 3 \cdot 5 \$ in the range.
Subtract the sum of all multiples of \$ 2 \cdot 3 \cdot 5  \$ in the range.


Note that apart from the factorization, no trial divisions are required anymore, only
multiplications.

First we need a function to get the distinct prime factors of a given number,
e.g. the function in Finding out the prime factors of a number,
modified slightly to return only distinct prime factors:

// List of all distinct prime factors of n.
static List&lt;Long&gt; primeFactors(long n) {
    List&lt;Long&gt; factors = new ArrayList&lt;Long&gt;();
    long md = 2;
    if (n % md == 0) {
        factors.add(md);
        do {
            n /= md;
        } while (n % md == 0);
    }
    md = 3;
    while (md &lt;= java.lang.Math.sqrt(n) + 1) {
        if (n % md == 0) {
            factors.add(md);
            do {
                n /= md;
            } while (n % md == 0);
        }
        md += 2;
    }
    if (n &gt; 1) {
        factors.add(n);
    }
    return factors;
}


Now the "challenge" is to translate the Inclusion-Exclusion approach to a 
recursive function, and that could look as follows:

// Sum of all numbers 1...k, computed modulo MOD.
private static long sumUpTo(long k) {
    k = k % MOD; // Otherwise the following multiplication can overflow.
    let sum = k * (k+1) / 2;
    return sum % MOD;
}

// Sum of all numbers (modulo MOD) in a...b which are divisible by k, but not
// by any multiple of k with factors in the primes list: 
private static long inclExcl(long a, long b, long k, List&lt;Long&gt; primes) {

    // Sum of all numbers in 1...b which are divisible by k:
    long s1 = sumUpTo(b / k) * k;
    // Sum of all numbers in 1...(a-1) which are divisible by k:
    long s2 = sumUpTo((a-1) / k) * k;

    long sum = (s1 - s2) % MOD;

    for (int idx = 0; idx &lt; primes.size(); idx++) {
        // The magic recursion!
        sum = (sum - inclExcl(a, b, k * primes.get(idx), primes.subList(idx + 1, primes.size()))) % MOD;
    }

    return sum;
}

private static long sumOfCoprimeNumbersInRange(long a, long b, long n) {

    List&lt;Long&gt; primes = primeFactors(n);

    long sum = inclExcl(a, b, 1, primes) % MOD;
    if (sum &lt; 0) {
        sum += MOD;
    }
    return sum;
}


For a (hopefully) better understanding of the implementation, here is how the
inclExcl function is called recursively if \$ N = 300 = 2^2  \cdot 3 \cdot 5^2 \$
as above (showing only the relevant arguments):


k=1, primes=[2, 3, 5]
    k=2, primes=[3, 5]
        k=6, primes=[5]
            k=30, primes=[]
        k=10, primes=[]
    k=3, primes=[5]
        k=15, primes=[]
    k=5, primes=[]


This is again much faster, because we have to iterate and recurse only over the distinct prime factors of \$ N \$ 
(and there can be no more than \$ 11 \$ prime factors if \$ N \le 10^{12} \$).
There is no iteration over the range  \$ A \ldots B \$ anymore.

Benchmark: For \$ N=96996900, A=100, B=96996000 \$:


GCD-based solution: 26 seconds.
Inclusion/exclusion-based solution: 0.181 seconds.