-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA1693_CF.java
More file actions
119 lines (93 loc) · 3.17 KB
/
Copy pathA1693_CF.java
File metadata and controls
119 lines (93 loc) · 3.17 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.Scanner;
public class A1693_CF {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0){
int n = scn.nextInt();
long arr[]=new long[n];
for(int i=0;i<n;i++)arr[i]=scn.nextLong();
long pref[]=new long[n];
pref[0]=arr[0];
for(int i=1;i<n;i++)pref[i]=pref[i-1]+arr[i];
boolean ans=true;
if(pref[n-1]!=0) ans=false;
for(int i=0;i<n;i++){
if(pref[i]<0){
ans=false;
break;
}
}
int firstzero=-1;
for(int i=0;i<n;i++){
if(pref[i]==0){
firstzero=i;
break;
}
}
if(firstzero!=-1){
for(int i=firstzero;i<n;i++){
if(pref[i]!=0){
ans=false;
break;
}
}
}
System.out.println(ans?"YES":"NO");
}
}
}
/*
-----------------------------------------------
CF A1693 – Prefix Sum Validation Notes
-----------------------------------------------
Problem Type:
- Prefix Sum
- Balance / Validation Problem (similar to bracket sequence check)
Core Idea:
We are given an array and must verify whether it represents a valid
process based on prefix sums.
Definitions:
pref[i] = sum of elements from index 0 to i
-----------------------------------------------
Conditions for a valid array:
-----------------------------------------------
1) Prefix sum should NEVER become negative
- pref[i] < 0 means we tried to remove something
that was never added
- Immediately invalid
2) Total sum MUST be zero
- pref[n-1] represents sum of entire array
- If pref[n-1] != 0, then some operation was left unfinished
3) Once prefix sum becomes zero, it must remain zero
- After the process finishes, no more operations are allowed
- If any pref[j] != 0 after reaching zero, it is invalid
-----------------------------------------------
Why pref[n-1] == 0 is mandatory:
-----------------------------------------------
- Prefix >= 0 ensures balance never goes below zero
- Final sum = 0 ensures balance returns to zero
- Without this check, unfinished sequences would pass
Example Invalid:
[1, 1] -> pref = [1, 2] (never negative, but never finishes)
Example Valid:
[2, -1, -1] -> pref = [2, 1, 0]
-----------------------------------------------
Implementation Strategy:
-----------------------------------------------
- Build prefix sum array
- Check for negative prefix
- Find first index where pref[i] == 0
- Ensure all following prefixes are also zero
Time Complexity:
- O(n) per test case
Space Complexity:
- O(n) for prefix array (can be optimized to O(1))
-----------------------------------------------
Key Takeaway:
-----------------------------------------------
This is a balance validation problem.
Every addition must be matched by a removal,
and once balance reaches zero, it must stay zero.
-----------------------------------------------
*/