File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ import java .util .Arrays ;
2+ import java .util .Scanner ;
3+
4+ public class Main {
5+ public static void main (String [] args ) {
6+ Scanner sc = new Scanner (System .in );
7+
8+ int t = sc .nextInt ();
9+ for (int tc = 0 ; tc < t ; ++tc ) {
10+ int n = sc .nextInt ();
11+ int x = sc .nextInt ();
12+ int [] a = new int [n ];
13+ for (int i = 0 ; i < a .length ; ++i ) {
14+ a [i ] = sc .nextInt ();
15+ }
16+
17+ System .out .println (solve (a , x ));
18+ }
19+
20+ sc .close ();
21+ }
22+
23+ static int solve (int [] a , int x ) {
24+ int [] minIndices = new int [x ];
25+ Arrays .fill (minIndices , Integer .MAX_VALUE );
26+ minIndices [0 ] = -1 ;
27+
28+ int modWithMinIndex = 0 ;
29+ int modWithSecondMinIndex = -1 ;
30+ int result = -1 ;
31+ int sum = 0 ;
32+ for (int i = 0 ; i < a .length ; ++i ) {
33+ sum = (sum + a [i ]) % x ;
34+
35+ if (sum != modWithMinIndex ) {
36+ result = Math .max (result , i - minIndices [modWithMinIndex ]);
37+
38+ if (modWithSecondMinIndex == -1 ) {
39+ modWithSecondMinIndex = sum ;
40+ }
41+ } else if (modWithSecondMinIndex != -1 ) {
42+ result = Math .max (result , i - minIndices [modWithSecondMinIndex ]);
43+ }
44+
45+ if (minIndices [sum ] == Integer .MAX_VALUE ) {
46+ minIndices [sum ] = i ;
47+ }
48+ }
49+
50+ return result ;
51+ }
52+ }
You can’t perform that action at this time.
0 commit comments