-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion1.java
More file actions
52 lines (39 loc) · 1.53 KB
/
Copy pathQuestion1.java
File metadata and controls
52 lines (39 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.util.Scanner;
public class Question1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int T =scanner.nextInt(); // No. of test cases
for (int i = 0; i < T; i++) {
int N = scanner.nextInt(); // Size of array
int Q = scanner.nextInt(); // No of queries
int[] A = new int[N]; // Array
// Array input
for (int j = 0; j < N; j++) {
A[j] = scanner.nextInt();
}
int[][] query = new int[Q][4]; // Array for query format (L, R, X, product)
for (int j = 0; j < Q; j++) {
query[j][0] = scanner.nextInt() -1; // Value of L
query[j][1] = scanner.nextInt() -1; // Value of R
query[j][2] = scanner.nextInt(); // Value of X
// Finding and storing product query[][3]
int product = 1;
for (int k = query[j][0]; k <= query[j][1]; k++) {
product *= A[k];
}
query[j][3] = product;
}
// Finding maximum power of X
for (int j = 0; j < Q; j++) {
int power = 0;
int divisor = query[j][2];
int dividend = query[j][3];
while (dividend % Math.pow(divisor, power) == 0) {
power++;
}
// Printing power for each query
System.out.println(power - 1);
}
}
}
}