# Sept lunchtime div 2 PAIRSUM2

this code gives TLE for subtask 2. please help me how this is happening and how i can avoid thesepackage sept_Lunchtime;

import java.util.*;

public class pairSum2 {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

int t = sc.nextInt();
while (t-- > 0) {

int n = sc.nextInt();
int quer = sc.nextInt();
int b[] = new int[n];
for (int i = 1; i <= n - 1; i++)
b[i] = sc.nextInt();

for (int i = 1; i <= quer; i++) {
int p=0, q=0;
if (sc.hasNext()) {
p = sc.nextInt();
q = sc.nextInt();
}

int x = Integer.max(p, q);
int y = Integer.min(p, q);

if ((x - y) % 2 == 0) { // odd difference
System.out.println("UNKNOWN");

} else {
long sum = 0;
int sign = 0;
for (int k = x - 1; k >= y; k--) {
if (sign % 2 == 0)
sum = sum + b[k];
else
sum = sum - b[k];

sign++;
// System.out.print(sum + " ");
}

System.out.println(sum);
}

}
}

}


}

Try to optimize the snippet by pre processing your data.

You Time Complexity - O (N * Q)
Go for linear time. Think!

just preprocess it as u do it in prefix sum

Hope this helps