#C6899. Longest Even Length Substring with Equal Half Sums
Longest Even Length Substring with Equal Half Sums
Longest Even Length Substring with Equal Half Sums
You are given a string s
consisting only of digits. Your task is to find the length of the longest contiguous even-length substring such that the sum of the first half of the substring is equal to the sum of the second half.
Formally, for any even length L, if the substring s[i...i+L-1]
is divided into two halves s[i...i+L/2-1]
and s[i+L/2...i+L-1]
, then the condition that must be met is:
$$\sum_{j=i}^{i+L/2-1} s_j = \sum_{j=i+L/2}^{i+L-1} s_j$$
If no such substring exists, return 0
.
inputFormat
The input consists of a single line containing the string s
composed only of digits.
outputFormat
Output a single integer representing the length of the longest even-length substring that can be split into two halves with equal sum. If there is no valid substring, output 0
.
123123
6