description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: flipped = deque() count = 0 flips = 0 for i in range(len(A)): if i > 0: x = flipped.popleft() if i >= K else 0 count -= x if (A[i] + count) % 2 == 0: count += 1 flipped.append(True) flips += 1 else: flipped.append(False) return flips if not any(list(flipped)[1:]) else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER VAR VAR IF BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: win = collections.deque() ans = 0 for i, x in enumerate(A): while len(win) > 0 and win[0] < i - K + 1: win.popleft() if x ^ len(win) % 2 == 0: if i + K - 1 <= len(A) - 1: ans += 1 win.append(i) else: return -1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR IF BIN_OP VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A, K: int) -> int: flipped, res = 0, 0 length = len(A) isFlipped = [0] * length for i, v in enumerate(A): if i >= K: flipped ^= isFlipped[i - K] if flipped == v: if i + K > length: return -1 isFlipped[i] = 1 flipped ^= 1 res += 1 return res
CLASS_DEF FUNC_DEF VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: count = 0 flippings = [(False) for _ in A] flipped = False for i in range(len(A)): if flippings[i]: flipped = not flipped if not flipped and A[i] == 0 or flipped and A[i] == 1: count += 1 flipped = not flipped if i + K > len(A): return -1 if i + K < len(flippings): flippings[i + K] = True return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: i = 0 c = 0 q = collections.deque() sign = 0 for i in range(len(A)): if q and i == q[0]: sign = 1 - sign q.popleft() if A[i] != sign: continue else: if i + K > len(A): return -1 q.append(i + K) sign = 1 - sign c += 1 return c
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP NUMBER VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: interval_status = [0] * (len(A) + 1) counter = 0 prev = 0 for i in range(len(A)): prev ^= interval_status[i] curr_status = prev ^ A[i] if curr_status == 0: if i + K <= len(A): interval_status[i + K] ^= 1 counter += 1 prev ^= 1 else: return -1 return counter
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], k: int) -> int: q = collections.deque() res = 0 for i in range(len(A)): if len(q) % 2 == 0: if A[i] == 0: res += 1 q.append(i + k - 1) elif A[i] == 1: res += 1 q.append(i + k - 1) if q and q[0] == i: q.popleft() if q and q[-1] >= len(A): return -1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: n = len(A) flip_count = 0 hint = [0] * n min_flip = 0 for i in range(n): if i >= K and hint[i - K] == 1: flip_count -= 1 if flip_count % 2 == A[i]: if i + K > n: return -1 min_flip += 1 flip_count += 1 hint[i] = 1 return min_flip
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: count = 0 stack = [] for i in range(len(A)): if stack and i == stack[0]: stack.pop(0) if A[i] == len(stack) % 2: if i + K > len(A): return -1 count += 1 stack.append(i + K) return count
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: start = 0 count = 0 res = 0 for end, a in enumerate(A): if count ^ A[end] == 0: A[end] -= 2 res += 1 count ^= 1 if end >= start + K - 1: if A[start] < 0: count ^= 1 A[start] += 2 start += 1 return -1 if any([(a < 0) for a in A]) else res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: "List[int]", K: "int") -> "int": idx = 0 num_digits = len(A) A_bitrep = int("1" + "".join([str(i) for i in A]), 2) K_bitrep = 2**K - 1 num_flips = 0 while A_bitrep > K_bitrep: if A_bitrep & 1 == 0: num_flips += 1 A_bitrep ^= K_bitrep A_bitrep >>= 1 return num_flips if A_bitrep == K_bitrep else -1
CLASS_DEF FUNC_DEF STRING STRING ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR VAR NUMBER STRING
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: N = len(A) hint = [0] * N counter = 0 flip = 0 for i, x in enumerate(A): flip = (flip + hint[i]) % 2 if x == 1 and flip == 1 or x == 0 and flip == 0: counter += 1 if i + K > N: return -1 flip = 1 - flip if i + K < N: hint[i + K] = 1 return counter
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: l = len(A) rem = l % K prev = 1 change_list = [[] for i in range(K)] for ind, num in enumerate(A): if prev != num: prev = num change_list[ind % K].append(ind) if num == 0: change_list[rem].append(l) num_flips = 0 for r, mini_list in enumerate(change_list): mini_list_len = len(mini_list) if mini_list_len % 2 == 1: return -1 for i in range(mini_list_len // 2): first = mini_list.pop(0) second = mini_list.pop(0) num_flips += (second - first) // K return num_flips def oldMinKBitFlips(self, A: List[int], K: int) -> int: l = len(A) prev = 1 change_list = [] for ind, num in enumerate(A): if prev != num: prev = num change_list.append(ind) if len(change_list) == 0: return 0 num_flips = 0 while len(change_list) > 1: first = change_list[0] if first + K >= l: return -1 change_list = change_list[1:] left_insertion_pt = bisect.bisect_left(change_list, first + K) if left_insertion_pt >= len(change_list): change_list.append(first + K) elif change_list[left_insertion_pt] == first + K: del change_list[left_insertion_pt] else: change_list.insert(left_insertion_pt, first + K) num_flips += 1 if len(change_list) > 0: first = change_list[0] q, r = divmod(l - first, K) if r == 0: return num_flips + q else: return -1 return num_flips
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR BIN_OP BIN_OP VAR VAR VAR RETURN VAR VAR FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF BIN_OP VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN BIN_OP VAR VAR RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A, K): cur = res = 0 for i in range(len(A)): if i >= K and A[i - K] == 2: cur -= 1 if cur % 2 == A[i]: if i + K > len(A): return -1 A[i] = 2 cur, res = cur + 1, res + 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: n = len(A) D = [0] * n D[0] = A[0] for i in range(n - 1): D[i + 1] = A[i + 1] ^ A[i] D.append(0) cnt = 0 if D[0] == 0: cnt += 1 D[0] ^= 1 D[K] ^= 1 for i in range(1, n - K + 1): if D[i] == 1: cnt += 1 D[i] ^= 1 D[i + K] ^= 1 D.pop() if D == [1] + [0] * (n - 1): return cnt else: return -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER NUMBER VAR NUMBER VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR BIN_OP LIST NUMBER BIN_OP LIST NUMBER BIN_OP VAR NUMBER RETURN VAR RETURN NUMBER VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: flips = 0 carry = 10 result = 0 valid = True for i, num in enumerate(A): if A[i] & 1 == flips & 1: result += 1 flips += 1 if i < len(A) - K + 1: A[i + K - 1] += carry else: valid = False if A[i] > 1: A[i] -= carry flips -= 1 return result if valid else -1
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR NUMBER VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: flip_pos = 0 flip_inds = [] for i in range(len(A)): while flip_pos < len(flip_inds) and i - K + 1 > flip_inds[flip_pos]: flip_pos += 1 cur_bit = A[i] ^ ((len(flip_inds) - flip_pos) % 2 != 0) if i > len(A) - K: if cur_bit == 0: return -1 elif not cur_bit: flip_inds.append(i) return len(flip_inds)
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: tot = 0 flips = [] mask = 0 for i in range(len(A) - (K - 1)): if flips and i == flips[0]: flips.pop(0) mask ^= 1 if A[i] == mask: flips.append(i + K) tot += 1 mask ^= 1 for i in range(len(A) - K, len(A)): if flips and i == flips[0]: flips.pop(0) mask ^= 1 if A[i] == mask: return -1 return tot
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER IF VAR VAR VAR RETURN NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: res = 0 flip = 0 for i in range(len(A)): if flip % 2 != 0: if A[i] % 10: res += 1 flip += 1 if i + K - 1 >= len(A): return -1 A[i + K - 1] += 10 elif not A[i] % 10: res += 1 flip += 1 if i + K - 1 >= len(A): return -1 A[i + K - 1] += 10 if flip > 0 and A[i] > 1: flip -= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR RETURN NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: N = len(A) flips = [] res, total = 0, 0 for i, v in enumerate(A): if i < N - K + 1: if v == 1: if total & 1: total += 1 res += 1 flips.append(1) else: flips.append(0) elif total & 1: flips.append(0) else: total += 1 res += 1 flips.append(1) else: if v == 0: if not total & 1: return -1 if v == 1: if total & 1: return -1 if i >= K - 1: total -= flips[i - K + 1] return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER IF VAR NUMBER IF BIN_OP VAR NUMBER RETURN NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR
In an array A containing only 0s and 1s, a K-bit flip consists of choosing a (contiguous) subarray of length K and simultaneously changing every 0 in the subarray to 1, and every 1 in the subarray to 0. Return the minimum number of K-bit flips required so that there is no 0 in the array.  If it is not possible, return -1.   Example 1: Input: A = [0,1,0], K = 1 Output: 2 Explanation: Flip A[0], then flip A[2]. Example 2: Input: A = [1,1,0], K = 2 Output: -1 Explanation: No matter how we flip subarrays of size 2, we can't make the array become [1,1,1]. Example 3: Input: A = [0,0,0,1,0,1,1,0], K = 3 Output: 3 Explanation: Flip A[0],A[1],A[2]: A becomes [1,1,1,1,0,1,1,0] Flip A[4],A[5],A[6]: A becomes [1,1,1,1,1,0,0,0] Flip A[5],A[6],A[7]: A becomes [1,1,1,1,1,1,1,1]   Note: 1 <= A.length <= 30000 1 <= K <= A.length
class Solution: def minKBitFlips(self, A: List[int], K: int) -> int: res = 0 flip = 0 na = [0] * len(A) for i, x in enumerate(A): flip ^= na[i] if flip ^ x == 0: res += 1 flip ^= 1 if i + K > len(A): return -1 if i + K < len(A): na[i + K] ^= 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR RETURN NUMBER IF BIN_OP VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR VAR
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
class Solution: def findDuplicate(self, nums): left, right = 0, len(nums) - 1 mid = (left + right) // 2 while right - left > 1: count = 0 for num in nums: if mid < num <= right: count += 1 if count > right - mid: left = mid else: right = mid mid = (left + right) // 2 return right
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
class Solution: def findDuplicate(self, nums): if len(nums) == 0: return None slow = fast = nums[0] while True: slow = nums[slow] fast = nums[nums[fast]] if slow == fast: break fast = nums[0] while slow != fast: slow = nums[slow] fast = nums[fast] return slow
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NONE ASSIGN VAR VAR VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
class Solution: def findDuplicate(self, nums): low, high = 1, len(nums) - 1 while low < high: mid = (low + high) // 2 count = 0 for i in nums: if i <= mid: count += 1 if count <= mid: low = mid + 1 else: high = mid return low
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
class Solution: def findDuplicate(self, nums): fast, slow = nums[0], nums[0] while True: slow = nums[slow] fast = nums[fast] fast = nums[fast] if slow == fast: break ptr1 = nums[0] ptr2 = fast while ptr1 != ptr2: ptr1 = nums[ptr1] ptr2 = nums[ptr2] return ptr1
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR RETURN VAR
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
class Solution: def findDuplicate(self, nums): st = set() for i in range(len(nums)): n = nums[i] if n not in st: st.add(n) else: return n
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
class Solution: def findDuplicate(self, nums): n = len(nums) - 1 a = 1 b = n while a < b: m = (a + b) // 2 lCount = 0 hCount = 0 for k in nums: if a <= k <= m: lCount += 1 elif m < k <= b: hCount += 1 if lCount > m - a + 1: b = m else: a = m + 1 return a
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
class Solution: def findDuplicate(self, nums): for n in nums: if n < 0: i = -n else: i = n ind = i - 1 if nums[ind] < 0: return i else: nums[ind] = -nums[ind] return extra
CLASS_DEF FUNC_DEF FOR VAR VAR IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR VAR VAR RETURN VAR
Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), prove that at least one duplicate number must exist. Assume that there is only one duplicate number, find the duplicate one. Example 1: Input: [1,3,4,2,2] Output: 2 Example 2: Input: [3,1,3,4,2] Output: 3 Note: You must not modify the array (assume the array is read only). You must use only constant, O(1) extra space. Your runtime complexity should be less than O(n2). There is only one duplicate number in the array, but it could be repeated more than once.
class Solution: def findDuplicate(self, nums): nums.sort() i = 0 while i < len(nums): if nums[i] == nums[i + 1]: return nums[i] i += 1
CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR VAR VAR NUMBER
This is the easy version of the problem. The only difference is that in this version $q = 1$. You are given an array of integers $a_1, a_2, \ldots, a_n$. The cost of a subsegment of the array $[l, r]$, $1 \leq l \leq r \leq n$, is the value $f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r)$, where $\operatorname{sum}(l, r) = a_l + a_{l+1} + \ldots + a_r$, and $\operatorname{xor}(l, r) = a_l \oplus a_{l+1} \oplus \ldots \oplus a_r$ ($\oplus$ stands for bitwise XOR ). You will have $q = 1$ query. Each query is given by a pair of numbers $L_i$, $R_i$, where $1 \leq L_i \leq R_i \leq n$. You need to find the subsegment $[l, r]$, $L_i \leq l \leq r \leq R_i$, with maximum value $f(l, r)$. If there are several answers, then among them you need to find a subsegment with the minimum length, that is, the minimum value of $r - l + 1$. -----Input----- Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \leq n \leq 10^5$, $q = 1$) — the length of the array and the number of queries. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^9$) — array elements. $i$-th of the next $q$ lines of each test case contains two integers $L_i$ and $R_i$ ($1 \leq L_i \leq R_i \leq n$) — the boundaries in which we need to find the segment. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that $L_1 = 1$ and $R_1 = n$. -----Output----- For each test case print $q$ pairs of numbers $L_i \leq l \leq r \leq R_i$ such that the value $f(l, r)$ is maximum and among such the length $r - l + 1$ is minimum. If there are several correct answers, print any of them. -----Examples----- Input 6 1 1 0 1 1 2 1 5 10 1 2 3 1 0 2 4 1 3 4 1 0 12 8 3 1 4 5 1 21 32 32 32 10 1 5 7 1 0 1 0 1 0 1 0 1 7 Output 1 1 1 1 1 1 2 3 2 3 2 4 -----Note----- In the first test case, $f(1, 1) = 0 - 0 = 0$. In the second test case, $f(1, 1) = 5 - 5 = 0$, $f(2, 2) = 10 - 10 = 0$. Note that $f(1, 2) = (10 + 5) - (10 \oplus 5) = 0$, but we need to find a subsegment with the minimum length among the maximum values of $f(l, r)$. So, only segments $[1, 1]$ and $[2, 2]$ are the correct answers. In the fourth test case, $f(2, 3) = (12 + 8) - (12 \oplus 8) = 16$. There are two correct answers in the fifth test case, since $f(2, 3) = f(3, 4)$ and their lengths are equal.
test = int(input()) for testtest in range(test): n, q = map(int, input().split()) L = list(map(int, input().split())) l, r = map(int, input().split()) s = 0 x = 0 S = [0] * n X = [0] * n for i in range(n): s += L[i] x ^= L[i] S[i] = s X[i] = x S += [0] X += [0] Ans = [0, n - 1] for i in range(0, n): a = i b = n - 1 while a < b: c = (a + b) // 2 if S[c] - S[i - 1] - (X[c] ^ X[i - 1]) == s - x: b = c continue else: a = c + 1 continue if a - i < Ans[1] - Ans[0]: Ans = [i, a] if S[n - 1] - S[i] - (X[n - 1] ^ X[i]) != s - x: break print(Ans[0] + 1, Ans[1] + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR LIST NUMBER VAR LIST NUMBER ASSIGN VAR LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR LIST VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER
This is the easy version of the problem. The only difference is that in this version $q = 1$. You are given an array of integers $a_1, a_2, \ldots, a_n$. The cost of a subsegment of the array $[l, r]$, $1 \leq l \leq r \leq n$, is the value $f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r)$, where $\operatorname{sum}(l, r) = a_l + a_{l+1} + \ldots + a_r$, and $\operatorname{xor}(l, r) = a_l \oplus a_{l+1} \oplus \ldots \oplus a_r$ ($\oplus$ stands for bitwise XOR ). You will have $q = 1$ query. Each query is given by a pair of numbers $L_i$, $R_i$, where $1 \leq L_i \leq R_i \leq n$. You need to find the subsegment $[l, r]$, $L_i \leq l \leq r \leq R_i$, with maximum value $f(l, r)$. If there are several answers, then among them you need to find a subsegment with the minimum length, that is, the minimum value of $r - l + 1$. -----Input----- Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \leq n \leq 10^5$, $q = 1$) — the length of the array and the number of queries. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^9$) — array elements. $i$-th of the next $q$ lines of each test case contains two integers $L_i$ and $R_i$ ($1 \leq L_i \leq R_i \leq n$) — the boundaries in which we need to find the segment. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that $L_1 = 1$ and $R_1 = n$. -----Output----- For each test case print $q$ pairs of numbers $L_i \leq l \leq r \leq R_i$ such that the value $f(l, r)$ is maximum and among such the length $r - l + 1$ is minimum. If there are several correct answers, print any of them. -----Examples----- Input 6 1 1 0 1 1 2 1 5 10 1 2 3 1 0 2 4 1 3 4 1 0 12 8 3 1 4 5 1 21 32 32 32 10 1 5 7 1 0 1 0 1 0 1 0 1 7 Output 1 1 1 1 1 1 2 3 2 3 2 4 -----Note----- In the first test case, $f(1, 1) = 0 - 0 = 0$. In the second test case, $f(1, 1) = 5 - 5 = 0$, $f(2, 2) = 10 - 10 = 0$. Note that $f(1, 2) = (10 + 5) - (10 \oplus 5) = 0$, but we need to find a subsegment with the minimum length among the maximum values of $f(l, r)$. So, only segments $[1, 1]$ and $[2, 2]$ are the correct answers. In the fourth test case, $f(2, 3) = (12 + 8) - (12 \oplus 8) = 16$. There are two correct answers in the fifth test case, since $f(2, 3) = f(3, 4)$ and their lengths are equal.
t = int(input()) for _ in range(t): n, q = map(int, input().split()) vals = list(map(int, input().split())) L, R = map(int, input().split()) L -= 1 R -= 1 s = 0 xr = 0 for i in range(L, R + 1): s += vals[i] xr ^= vals[i] f = s - xr l, r = L, L ml, mr = L, R curs, curxr = 0, 0 while r <= R: curs += vals[r] curxr ^= vals[r] if curs - curxr == f: while l < r and curs - vals[l] - (curxr ^ vals[l]) == f: curs -= vals[l] curxr ^= vals[l] l += 1 if mr - ml > r - l: mr, ml = r, l r += 1 print(ml + 1, mr + 1)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR WHILE VAR VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER
This is the easy version of the problem. The only difference is that in this version $q = 1$. You are given an array of integers $a_1, a_2, \ldots, a_n$. The cost of a subsegment of the array $[l, r]$, $1 \leq l \leq r \leq n$, is the value $f(l, r) = \operatorname{sum}(l, r) - \operatorname{xor}(l, r)$, where $\operatorname{sum}(l, r) = a_l + a_{l+1} + \ldots + a_r$, and $\operatorname{xor}(l, r) = a_l \oplus a_{l+1} \oplus \ldots \oplus a_r$ ($\oplus$ stands for bitwise XOR ). You will have $q = 1$ query. Each query is given by a pair of numbers $L_i$, $R_i$, where $1 \leq L_i \leq R_i \leq n$. You need to find the subsegment $[l, r]$, $L_i \leq l \leq r \leq R_i$, with maximum value $f(l, r)$. If there are several answers, then among them you need to find a subsegment with the minimum length, that is, the minimum value of $r - l + 1$. -----Input----- Each test consists of multiple test cases. The first line contains an integer $t$ ($1 \leq t \leq 10^4$) — the number of test cases. The description of test cases follows. The first line of each test case contains two integers $n$ and $q$ ($1 \leq n \leq 10^5$, $q = 1$) — the length of the array and the number of queries. The second line of each test case contains $n$ integers $a_1, a_2, \ldots, a_n$ ($0 \leq a_i \leq 10^9$) — array elements. $i$-th of the next $q$ lines of each test case contains two integers $L_i$ and $R_i$ ($1 \leq L_i \leq R_i \leq n$) — the boundaries in which we need to find the segment. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. It is guaranteed that $L_1 = 1$ and $R_1 = n$. -----Output----- For each test case print $q$ pairs of numbers $L_i \leq l \leq r \leq R_i$ such that the value $f(l, r)$ is maximum and among such the length $r - l + 1$ is minimum. If there are several correct answers, print any of them. -----Examples----- Input 6 1 1 0 1 1 2 1 5 10 1 2 3 1 0 2 4 1 3 4 1 0 12 8 3 1 4 5 1 21 32 32 32 10 1 5 7 1 0 1 0 1 0 1 0 1 7 Output 1 1 1 1 1 1 2 3 2 3 2 4 -----Note----- In the first test case, $f(1, 1) = 0 - 0 = 0$. In the second test case, $f(1, 1) = 5 - 5 = 0$, $f(2, 2) = 10 - 10 = 0$. Note that $f(1, 2) = (10 + 5) - (10 \oplus 5) = 0$, but we need to find a subsegment with the minimum length among the maximum values of $f(l, r)$. So, only segments $[1, 1]$ and $[2, 2]$ are the correct answers. In the fourth test case, $f(2, 3) = (12 + 8) - (12 \oplus 8) = 16$. There are two correct answers in the fifth test case, since $f(2, 3) = f(3, 4)$ and their lengths are equal.
def compute(prefixS, prefixXor, l, r): if l == 0: return prefixS[r] - prefixXor[r] return prefixS[r] - prefixS[l - 1] - (prefixXor[r] ^ prefixXor[l - 1]) t = int(input()) while t: n, q = list(map(int, input().split())) nums = list(map(int, input().split())) l, r = list(map(int, input().split())) l -= 1 r -= 1 prefixS = nums[:] prefixXor = nums[:] for i in range(1, n): prefixS[i] += prefixS[i - 1] prefixXor[i] ^= prefixXor[i - 1] maxSum = prefixS[n - 1] - prefixXor[n - 1] start, end = 1, n - 1 while start <= end: mid = (start + end) // 2 i, j = 0, mid - 1 flag = 0 while j < n: temp = compute(prefixS, prefixXor, i, j) if temp == maxSum: flag = 1 break j += 1 i += 1 if flag: l, r = i, j end = mid - 1 else: start = mid + 1 print(l + 1, " ", r + 1) t -= 1
FUNC_DEF IF VAR NUMBER RETURN BIN_OP VAR VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR NUMBER VAR NUMBER VAR NUMBER IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER STRING BIN_OP VAR NUMBER VAR NUMBER
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1. Input The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1". Output Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. Examples Input 3 6 101010 000100 100000 Output 3 Input 2 3 111 000 Output -1 Note In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
n, m = map(int, input().split()) memo = [[int(2000000000.0) for j in range(m)] for i in range(n)] for i in range(n): s = input() if s.find("1") == -1: print(-1) exit() memo[i][0] = s[::-1].find("1") + 1 memo[i][m - 1] = s.find("1") + 1 for j in range(m): if s[j] == "1": memo[i][j] = 0 else: memo[i][j] = min(memo[i][j], memo[i][j - 1] + 1) for j in range(m - 2, -1, -1): memo[i][j] = min(memo[i][j], memo[i][j + 1] + 1) print(min([sum([memo[i][j] for i in range(n)]) for j in range(m)]))
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR IF FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR NUMBER STRING NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR STRING NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1. Input The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1". Output Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. Examples Input 3 6 101010 000100 100000 Output 3 Input 2 3 111 000 Output -1 Note In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
x = int(input().split(" ")[0]) y = [] for i in range(x): y.append(input()) for x in y: if "1" not in x: print("-1") exit() s = "00010100" s = "000100110" def row_cost_cal(string): arrL = [""] * len(string) initPosL = 0 if string[0] != "1": initPosL = -(len(string) - string.rindex("1")) for i in range(len(string)): if string[i] == "1": arrL[i] = 0 initPosL = i else: arrL[i] = i - initPosL arrR = [""] * len(string) initPosR = 0 if string[-1] != "1": initPosR = len(string) + string.index("1") for i in range(len(string) - 1, -1, -1): if string[i] == "1": arrR[i] = 0 initPosR = i else: arrR[i] = initPosR - i arrMin = [min(arrL[i], arrR[i]) for i in range(len(string))] return arrMin arrCost = [row_cost_cal(string) for string in y] colCost = [sum(x) for x in zip(*arrCost)] print(min(colCost))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR IF STRING VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING FUNC_DEF ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST STRING FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1. Input The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1". Output Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. Examples Input 3 6 101010 000100 100000 Output 3 Input 2 3 111 000 Output -1 Note In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
def calc_shift_cost(row): starts = [i for i, x in enumerate(row) if x] for start in starts: d = 2 pos = start + 1 if pos == len(row): pos = 0 while row[pos] != 1: if row[pos]: row[pos] = min(row[pos], d) else: row[pos] = d d += 1 pos += 1 if pos == len(row): pos = 0 d = 2 pos = start - 1 if pos == -1: pos = len(row) - 1 while row[pos] != 1: if row[pos]: row[pos] = min(row[pos], d) else: row[pos] = d d += 1 pos -= 1 if pos == -1: pos = len(row) - 1 for x in range(len(row)): row[x] -= 1 class CodeforcesTask229ASolution: def __init__(self): self.result = "" self.n_m = [] self.board = [] def read_input(self): self.n_m = [int(x) for x in input().split(" ")] for x in range(self.n_m[0]): self.board.append([int(x) for x in input()]) def process_task(self): for row in self.board: if sum(row): calc_shift_cost(row) else: self.result = "-1" break if not self.result: costs = [] for x in range(self.n_m[1]): ss = 0 for y in range(self.n_m[0]): ss += self.board[y][x] costs.append(ss) self.result = str(min(costs)) def get_result(self): return self.result Solution = CodeforcesTask229ASolution() Solution.read_input() Solution.process_task() print(Solution.get_result())
FUNC_DEF ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR FOR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR LIST ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_DEF FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING IF VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF RETURN VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1. Input The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1". Output Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. Examples Input 3 6 101010 000100 100000 Output 3 Input 2 3 111 000 Output -1 Note In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
def calculate_row_distance(row, rows_len): init_i = row.index(1) dist = 0 dists = [(-1) for i in range(rows_len)] i = init_i for x in range(rows_len): if i == rows_len: i = 0 if row[i] == 1: dist = 0 dists[i] = 0 elif dists[i] == -1 or dists[i] > dist: dists[i] = dist elif dists[i] < dist: dist = dists[i] dist += 1 i += 1 dist = 0 i = init_i for x in range(rows_len): if i == -1: i = rows_len - 1 if row[i] == 1: dist = 0 dists[i] = 0 elif dists[i] == -1 or dists[i] > dist: dists[i] = dist elif dists[i] < dist: dist = dists[i] dist += 1 i -= 1 return dists def init(): cols_len, rows_len = map(lambda x: int(x), input().split(" ")) cols_distance = [(0) for i in range(rows_len)] for r in range(cols_len): row = [int(i) for i in input()] if 1 not in row: print("-1") return dists = calculate_row_distance(row, rows_len) for c in range(rows_len): cols_distance[c] += dists[c] cols_distance.sort() print(cols_distance[0]) init()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR IF NUMBER VAR EXPR FUNC_CALL VAR STRING RETURN ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1. Input The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1". Output Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. Examples Input 3 6 101010 000100 100000 Output 3 Input 2 3 111 000 Output -1 Note In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
import sys n, m = map(int, input().split()) mp = ["" for i in range(n)] f = [[(0) for j in range(m)] for i in range(n)] for i in range(n): mp[i] = input() if mp[i].find("1") == -1: print(-1) sys.exit() tmp = mp[i][::-1].find("1") + 1 for j in range(m): if mp[i][j] == "1": f[i][j] = 0 else: f[i][j] = f[i][j - 1] + 1 if j > 0 else tmp tmp = mp[i].find("1") + 1 for j in range(m - 1, -1, -1): f[i][j] = min(f[i][j], f[i][j + 1] + 1 if j + 1 < m else tmp) ans = int(1000000000.0) for j in range(m): sm = 0 for i in range(n): sm += f[i][j] ans = min(ans, sm) print(ans)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR STRING VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR STRING NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER STRING NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR STRING ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR STRING NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1. Input The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1". Output Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. Examples Input 3 6 101010 000100 100000 Output 3 Input 2 3 111 000 Output -1 Note In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
def binS(a, x, n): l, r = -1, n while l + 1 < r: m = (l + r) // 2 if a[m] >= x: r = m else: l = m return min(abs(a[r] - x), abs(a[r - 1] - x)) def main(): n, m = [int(i) for i in input().split()] q = [0] * m for i in range(n): s = input() * 3 a = [] ln = 0 for j in range(m * 3): if s[j] == "1": a += [j] ln += 1 if ln == 0: q = [-1] break for x in range(m, m * 2): q[x - m] += binS(a, x, ln) print(min(q)) main()
FUNC_DEF ASSIGN VAR VAR NUMBER VAR WHILE BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR NUMBER VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR STRING VAR LIST VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given a table consisting of n rows and m columns. Each cell of the table contains a number, 0 or 1. In one move we can choose some row of the table and cyclically shift its values either one cell to the left, or one cell to the right. To cyclically shift a table row one cell to the right means to move the value of each cell, except for the last one, to the right neighboring cell, and to move the value of the last cell to the first cell. A cyclical shift of a row to the left is performed similarly, but in the other direction. For example, if we cyclically shift a row "00110" one cell to the right, we get a row "00011", but if we shift a row "00110" one cell to the left, we get a row "01100". Determine the minimum number of moves needed to make some table column consist only of numbers 1. Input The first line contains two space-separated integers: n (1 ≤ n ≤ 100) — the number of rows in the table and m (1 ≤ m ≤ 104) — the number of columns in the table. Then n lines follow, each of them contains m characters "0" or "1": the j-th character of the i-th line describes the contents of the cell in the i-th row and in the j-th column of the table. It is guaranteed that the description of the table contains no other characters besides "0" and "1". Output Print a single number: the minimum number of moves needed to get only numbers 1 in some column of the table. If this is impossible, print -1. Examples Input 3 6 101010 000100 100000 Output 3 Input 2 3 111 000 Output -1 Note In the first sample one way to achieve the goal with the least number of moves is as follows: cyclically shift the second row to the right once, then shift the third row to the left twice. Then the table column before the last one will contain only 1s. In the second sample one can't shift the rows to get a column containing only 1s.
G = [[], [1], [1, 1], [1, 2, 1], [1, 2, 2, 1], [1, 2, 3, 2, 1]] def g(k): global G if k < 6: return G[k] return list(range(1, (k + 1) // 2 + 1)) + list(range(k // 2, 0, -1)) def f(): n, m = map(int, input().split()) s, p = [0] * m, [0] * m for i in range(n): q = [j for j, x in enumerate(input()) if x == "1"] if not q: return -1 c = q.pop(0) p[c], a = 0, c + 1 for b in q: p[b] = 0 if b > a: p[a:b] = g(b - a) a = b + 1 if c + m > a: t = g(c + m - a) p[a:], p[:c] = t[: m - a], t[m - a :] for j, x in enumerate(p): s[j] += x return min(s) print(f())
ASSIGN VAR LIST LIST LIST NUMBER LIST NUMBER NUMBER LIST NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER LIST NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF IF VAR NUMBER RETURN VAR VAR RETURN BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR BIN_OP LIST NUMBER VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
s = input() n = len(s) ans = 0 X = [] for x in range(n): k = 1 while x + 2 * k < n and (s[x] != s[x + k] or s[x] != s[x + 2 * k]): k += 1 if x + 2 * k < n: X += [(x, x + 2 * k)] for i in range(len(X) - 2, -1, -1): X[i] = X[i][0], min(X[i][1], X[i + 1][1]) I = 0 if len(X): for l in range(n): if X[I][0] < l: while I < len(X) and X[I][0] < l: I += 1 if I == len(X): break ans += max(0, n - X[I][1]) print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR LIST VAR BIN_OP VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR NUMBER FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
from sys import stdin s = stdin.readline().strip() x = -1 ans = 0 for i in range(len(s)): for j in range(1, 10): if i - 2 * j >= 0 and s[i] == s[i - j] and s[i - j] == s[i - 2 * j]: if i - 2 * j > x: ans += (i - 2 * j - x) * (len(s) - i) x = i - 2 * j print(ans)
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP NUMBER VAR VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
X = [ [], ["0", "1"], ["00", "01", "10", "11"], ["001", "010", "011", "100", "101", "110"], ["0010", "0011", "0100", "0101", "0110", "1001", "1010", "1011", "1100", "1101"], [ "00100", "00101", "00110", "01001", "01011", "01100", "01101", "10010", "10011", "10100", "10110", "11001", "11010", "11011", ], [ "001001", "001011", "001100", "001101", "010010", "010011", "010110", "011001", "011010", "011011", "100100", "100101", "100110", "101001", "101100", "101101", "110010", "110011", "110100", "110110", ], [ "0010011", "0011001", "0011010", "0011011", "0100101", "0101100", "0101101", "0110011", "1001100", "1010010", "1010011", "1011010", "1100100", "1100101", "1100110", "1101100", ], ["00110011", "01011010", "01100110", "10011001", "10100101", "11001100"], ] s = input() N = len(s) ans = (N - 1) * (N - 2) // 2 for i in range(N): for j in range(i + 3, min(i + 9, N + 1)): if s[i:j] in X[j - i]: ans -= 1 print(ans)
ASSIGN VAR LIST LIST LIST STRING STRING LIST STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING STRING LIST STRING STRING STRING STRING STRING STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
def good(l, r): for x in range(l, r): for k in range(1, 10): if x + (k << 1) <= r: if s[x] == s[x + k] and s[x + k] == s[x + (k << 1)]: return False else: break return True s = input() n = len(s) answer = 0 for l in range(n): r = l while good(l, r): r += 1 if r >= n: break answer += n - r print(answer)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
s = input() n = len(s) l = 0 ans = 0 for i in range(n): for j in range(i - 1, l, -1): if 2 * j - i < l: break if s[i] == s[j] == s[j + j - i]: ans += (2 * j - i - l + 1) * (n - i) l = 2 * j - i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
import sys data_string = input() total_seq = 0 current_end_seq = 0 step = 0 while step < len(data_string) - 1: step += 1 total_seq = total_seq + current_end_seq min_d = 1 while step - 2 * min_d + 1 > current_end_seq: if ( data_string[step] == data_string[step - min_d] == data_string[step - 2 * min_d] ): current_end_seq = step - 2 * min_d + 1 break min_d += 1 print(total_seq + current_end_seq)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
import sys S = sys.stdin.readline() S = S.strip() n = len(S) ans = 0 def check(i, j): if j - i < 3: return False for x in range(i, j): for k in range(1, j - i): if x + 2 * k >= j: break if S[x] == S[x + k] == S[x + 2 * k]: return True return False for i in range(n): for j in range(i + 1, min(i + 100, n + 1)): if check(i, j): ans += n - j + 1 break print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN NUMBER RETURN NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
s = input() def pri(l, r): k = 1 while r - 2 * k >= l: if s[r] != s[r - k] or s[r - k] != s[r - 2 * k]: k += 1 continue return False return True ans = 0 for i in range(len(s)): j = i while j < len(s) and pri(i, j): j += 1 ans += len(s) - j print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
s = input() len = len(s) r = len ans = 0 for l in range(len)[::-1]: r = min(r, l + 9) k = 1 while l + 2 * k < r: if s[l] == s[l + k] and s[l] == s[l + 2 * k]: r = min(r, l + 2 * k) break k += 1 ans += len - r print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string s. A binary string consists only of zeros and ones. Let n be the length of s. Rash needs to find the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Find this number of pairs for Rash. Input The first line contains the string s (1 ≤ |s| ≤ 300 000), consisting of zeros and ones. Output Output one integer: the number of such pairs of integers l, r that 1 ≤ l ≤ r ≤ n and there is at least one pair of integers x, k such that 1 ≤ x, k ≤ n, l ≤ x < x + 2k ≤ r, and s_x = s_{x+k} = s_{x+2k}. Examples Input 010101 Output 3 Input 11001100 Output 0 Note In the first example, there are three l, r pairs we need to count: 1, 6; 2, 6; and 1, 5. In the second example, there are no values x, k for the initial string, so the answer is 0.
def parse(c, n): l = [-1] * n for x in c: se = set(x) for i in range(len(x) - 1): for j in range(i + 1, len(x)): k = x[j] - x[i] if x[i] + k + k >= n: break if x[i] + k + k in se: l[x[i] + k + k] = x[i] break res = 0 prex = -1 for i in range(n): if l[i] <= prex: continue res = res + (l[i] - prex) * (n - i) prex = l[i] return res s = input() one = [i for i in range(len(s)) if s[i] == "1"] zero = [i for i in range(len(s)) if s[i] == "0"] ans = parse((one, zero), len(s)) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().split())) ans, j = n, 0 index = dict() for i in range(n): if a[i] in index.keys(): j = max(j, index[a[i]]) index[a[i]] = i + 1 else: index[a[i]] = i + 1 ans = min(ans, 2 * min(j, n - i - 1) + max(j, n - i - 1)) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
ans = "" for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) l, r = 0, 0 maxLen = 0 maxL, maxR = -1, -1 candi = [] st = set() while r < n: while r < n and arr[r] not in st: st.add(arr[r]) r += 1 if maxLen <= r - l: maxLen = r - l maxL = l maxR = r - 1 candi.append([maxLen, maxL, maxR]) while l <= r and r < n and arr[r] in st: st.remove(arr[l]) l += 1 res = float("inf") for cand in candi: if cand[0] == maxLen: res = min( res, 2 * cand[1] + (n - cand[2] - 1), cand[1] + 2 * (n - cand[2] - 1) ) ans += str(res) + "\n" print(ans)
ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR WHILE VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER BIN_OP VAR NUMBER BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for _ in range(int(input())): N = int(input()) A = [int(i) for i in input().split()] dict = {} ans = 4 * N front = 0 back = 0 while back < N: if A[back] in dict: for i in range(front, dict[A[back]]): if A[i] in dict: del dict[A[i]] ans = min(2 * min(front, N - back) + max(front, N - back), ans) front = dict[A[back]] + 1 dict[A[back]] = back back += 1 else: dict[A[back]] = back back += 1 ans = min(2 * min(front, N - back) + max(front, N - back), ans) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
def gcd(a, b): if b == 0: return a return gcd(a % b, a) def solve(arr, n): a = {} temp, ans, j = n - 1, n, 0 i = 0 while i < n: temp = n - i - 1 if arr[i] in a: j = max(a[arr[i]], j) else: j = max(0, j) a[arr[i]] = 1 + i ans = min(ans, min(j, temp) + j + temp) i += 1 return ans def main(): t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) print(solve(arr, n)) main()
FUNC_DEF IF VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
result = [] t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().strip().split())) mapIndex = dict() st, ed = 0, n - 1 res = [] for i in range(n): if arr[i] not in mapIndex: mapIndex[arr[i]] = i ed = i else: ind = mapIndex[arr[i]] e = n - 1 - ed res.append(min(2 * st + e, st + 2 * e)) st = ind + 1 while ind >= 0 and arr[ind] in mapIndex: if mapIndex[arr[ind]] == ind: del mapIndex[arr[ind]] ind -= 1 mapIndex[arr[i]] = i e = n - 1 - ed res.append(min(2 * st + e, st + 2 * e)) result.append(str(min(res))) print("\n".join(result))
ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
T = int(input()) def int_arr_input(): return [int(x) for x in input().split()] def compute_next_idx(A, N): next_idx = [(N - 1) for i in range(N)] i = j = 0 cnt = dict() while j < N: j_key = str(A[j]) prev = cnt.get(j_key, 0) cnt[j_key] = prev + 1 if prev == 1: while i <= j: next_idx[i] = j - 1 i_key = str(A[i]) cur = cnt[i_key] cnt[i_key] = cur - 1 i += 1 if cur == 2: break j += 1 return next_idx while T: T -= 1 N = int(input()) A = int_arr_input() next_idx = compute_next_idx(A, N) ret = 1000000000.0 for i in range(N): j = next_idx[i] m1 = 2 * i + N - j - 1 m2 = i + 2 * (N - j - 1) ret = min(ret, min(m1, m2)) print(ret)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
t = int(input()) def solve(ar): mp = {} n = len(ar) ans = float("inf") i, j = 0, 0 while j < n: if ar[j] not in mp or mp[ar[j]] == 0: mp[ar[j]] = 1 elif mp[ar[j]] == 1: while ar[i] != ar[j]: mp[ar[i]] -= 1 i += 1 i += 1 ans = min(ans, 2 * i + n - j - 1, 2 * (n - j - 1) + i) j += 1 return ans for _ in range(t): n = int(input()) x = list(map(int, input().split())) print(solve(x))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) mp = {} j = 0 ans = 10000000000 for i in range(0, n): if a[i] in mp: mp[a[i]] += 1 else: mp[a[i]] = 1 while mp[a[i]] > 1: mp[a[j]] -= 1 j += 1 x = j y = n - i - 1 ans = min(ans, 2 * x + y, x + y * 2) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
def fun(i, j, n): left = i right = n - j - 1 if left == right: return 3 * left elif left > right: return 2 * right + left else: return 2 * left + right for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} j = 0 ans = 1000000000 for i in range(len(a)): if a[i] not in d.keys(): d[a[i]] = i else: d[a[i]] = i while a[j] != a[i]: del d[a[j]] j += 1 j += 1 ans = min(fun(j, i, n), ans) print(ans)
FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN BIN_OP NUMBER VAR IF VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
t = int(input()) while t: a = int(input()) b = list(map(int, input().split(" "))) s, check, index = len(b) - 1, 0, a p = 0 local = {} while p < a: k = s - p c = local[b[p]] if b[p] in local.keys() else 0 check = max(c, check) local[b[p]] = p + 1 mk = k + check + min(k, check) index = min(index, mk) p += 1 print(index) t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
t = int(input()) for _ in range(t): n = int(input()) l1 = [int(i) for i in input().split()] d1 = {} for i in l1: d1[i] = -1 j = 0 ans = -1 for i in range(n): while j < n and d1[l1[j]] == -1: d1[l1[j]] = 1 j += 1 if ans == -1: ans = min(i * 2 + (n - j), (n - j) * 2 + i) else: ans = min(ans, i * 2 + (n - j), (n - j) * 2 + i) d1[l1[i]] = -1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
import sys from itertools import islice for s in islice(sys.stdin, 2, None, 2): a = s.split() n = len(a) k = 0 r = 200000.0 d = {} for i, x in enumerate(a): m = i - d.get(x, -1) d[x] = i if m <= k: k = m else: k += 1 r = min(r, n - k + min(i - k + 1, n - i - 1)) print(r)
IMPORT FOR VAR FUNC_CALL VAR VAR NUMBER NONE NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for _ in range(int(input())): s = int(input()) inp = list(map(int, input().split())) dic = {} j = 0 res = 10**10 for i in range(s): if inp[i] not in dic.keys(): dic[inp[i]] = 1 else: dic[inp[i]] += 1 while dic[inp[i]] > 1: dic[inp[j]] -= 1 j += 1 res = min(res, 2 * min(j, s - i - 1) + max(j, s - i - 1)) print(res)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for _ in range(int(input())): n = int(input()) nums = list(map(int, input().split())) unique, maxUnique, ind, s, queue = 0, 0, 0, set(), [] ans = float("inf") for i in range(n): if nums[i] not in s: unique += 1 else: if unique >= maxUnique: maxUnique, ind = unique, i - unique ans = min(ans, 2 * min(ind, n - i) + max(ind, n - i)) while queue[0] != nums[i]: s.remove(queue.pop(0)) queue.pop(0) unique = len(queue) + 1 queue.append(nums[i]) s.add(nums[i]) if unique >= maxUnique: ans = min(ans, n - unique) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR LIST ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
def help(a): map = dict() n = len(a) res = n j = 0 for i in range(n): r = n - i - 1 temp = 0 if a[i] in map: temp = map[a[i]] j = max(j, temp) map[a[i]] = 1 + i res = min(res, min(j, r) + j + r) return res for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] print(help(a))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
t = int(input()) for i in range(t): n = int(input()) ar = list(map(int, input().strip().split()))[:n] dic = {} r = n - 1 ans = n j = 0 for i in range(n): r = n - i - 1 if ar[i] in dic: x = dic[ar[i]] else: x = 0 j = max(x, j) dic[ar[i]] = 1 + i ans = min(ans, min(j, r) + j + r) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for _ in range(int(input())): n = int(input()) a = list(map(int, input().split())) dic = {} sample = [] max_l = start = 0 for i, key in enumerate(a): if key in dic and start <= dic[key]: start = dic[key] + 1 elif i - start + 1 > max_l: max_l = i - start + 1 sample = [[start, i]] elif i - start + 1 == max_l: sample.append([start, i]) dic[key] = i ans = 10**9 for i, j in sample: ans = min(ans, min(i, n - j - 1) * 2 + max(i, n - j - 1)) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR LIST LIST VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
import sys from itertools import islice for s in islice(sys.stdin, 2, None, 2): a = s.split() n = len(a) d = {} r = 200000.0 k = 0 for i, x in enumerate(map(int, a), 1): k = min(i - d.get(x, 0), k + 1) d[x] = i r = min(r, n - k + min(i - k, n - i)) print(r)
IMPORT FOR VAR FUNC_CALL VAR VAR NUMBER NONE NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) a = {arr[0]} i = 0 j = 1 ans = float("inf") while j < n: if arr[j] in a: while arr[j] in a: a.remove(arr[i]) i += 1 a.add(arr[j]) ans = min(ans, max(i, n - j - 1) + 2 * min(i, n - j - 1)) j += 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING WHILE VAR VAR IF VAR VAR VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
import sys def printArr(a): s = "" for i in a: s += str(i) + " " print(s[0:-1]) sys.setrecursionlimit(10**5) def topop(l, h, n): x = n - h - 1 if x < l: return 2 * x + l else: return 2 * l + x t = int(input()) for _ in range(t): n = int(input()) a = [int(_) for _ in input().split()] l = 0 h = 0 t = 0 d = dict() for i in range(n): x = a[i] if x in d and d[x] >= t: y = d[x] if topop(l, h, n) > topop(t, y, n): l = t h = y if topop(l, h, n) > topop(y + 1, i, n): l = y + 1 h = i if topop(l, h, n) > topop(y, i - 1, n): l = y h = i - 1 if topop(l, h, n) > topop(t, i - 1, n): l = t h = i - 1 t = y + 1 d[x] = i if topop(t, n - 1, n) < topop(l, h, n): print(topop(t, n - 1, n)) else: print(topop(l, h, n))
IMPORT FUNC_DEF ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
test = int(input()) while test: n = int(input()) a = [int(x) for x in input().split()] f = dict() i = j = 0 ans = 1000000.0 while j < n: if a[j] in f.keys(): f[a[j]] += 1 else: f[a[j]] = 1 if f[a[j]] > 1: while a[i] != a[j]: f[a[i]] -= 1 i += 1 f[a[j]] -= 1 i += 1 ans = min(ans, 2 * i + n - j - 1, 2 * (n - j - 1) + i) j += 1 test -= 1 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) dic = {} for i in range(n): dic[arr[i]] = 0 l = 0 h = n - 1 ans = n for i in range(n): temp = 0 h = n - 1 - i l = max(dic[arr[i]], l) dic[arr[i]] = 1 + i ans = min(ans, min(l, h) + l + h) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) ans = n start = 0 x = 0 d = {arr[0]: 1} for i in range(1, n): d[arr[i]] = d.get(arr[i], 0) + 1 if d[arr[i]] > 1: x = start y = n - i temp1 = 2 * x + y temp2 = 2 * y + x ans = min(ans, temp1, temp2) while d[arr[i]] > 1: d[arr[start]] -= 1 start += 1 x = start y = 0 temp1 = 2 * x + y temp2 = 2 * y + x ans = min(ans, temp1, temp2) print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for _ in range(int(input())): n = int(input()) lis = list(map(int, input().split())) coll = {} for i in lis: if i not in coll: coll[i] = 0 p1 = 0 p2 = 0 ans = n - 1 l, r = -1, -1 while p2 < n: if coll[lis[p2]] == 0: coll[lis[p2]] = 1 p2 += 1 else: while coll[lis[p2]] != 0: coll[lis[p1]] -= 1 p1 += 1 coll[lis[p2]] = 1 p2 += 1 le = p1 - 0 ri = n - p2 ans = min(ans, min(2 * le + ri, le + 2 * ri)) print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR VAR BIN_OP VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
def func(): n = int(input()) arr = list(map(int, input().split())) dic = {} i = j = 0 ans = n while i < n: r = n - 1 - i present = dic.get(arr[i], 0) j = max(j, present) dic[arr[i]] = i + 1 ans = min(min(j, r) + j + r, ans) i += 1 print(ans) tc = int(input()) while tc > 0: func() tc -= 1
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
def solve(n, arr): i = 0 seen = set() maxLen = 0 left = right = -1 res = float("inf") for j in range(n): while arr[j] in seen: seen.remove(arr[i]) i += 1 seen.add(arr[j]) if j - i + 1 >= maxLen: maxLen = j - i + 1 left = i right = j removeLeftFirst = left * 2 + n - right - 1 removeRightFirst = (n - right - 1) * 2 + left res = min(res, removeLeftFirst, removeRightFirst) if left == 0: return n - right - 1 if right == n - 1: return left return res for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) print(solve(n, arr))
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
for i in range(int(input())): n = int(input()) l = list(map(int, input().split())) l1 = [] s = {} start = -1 mx = 0 l1 = [] for i in range(n): if l[i] in s and s[l[i]] > start: start = s[l[i]] s[l[i]] = i if i == n - 1: l1.append([start + 1, i]) else: l1.append([start + 1, i]) s[l[i]] = i if l.count(l[0]) == n and n != 1: print(n - 1) elif l1: l2 = [] for i in l1: a = i[0] b = n - i[1] - 1 if b > a: a, b = b, a l2.append(b * 2 + a) print(min(l2)) else: print(0)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR LIST BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER
You are given an array A of N integers. You must perform some (possibly zero) operations to make the elements of A distinct. In one operation, you can either: Remove one element from the beginning of the array A and append any positive integer to the end. Or remove one element from the end of the array A and prepend any positive integer to the beginning. Find the minimum number of operations required to make all the elements of the array distinct. ------ Input Format ------ - The first line contains a single integer T — the number of test cases. Then the test cases follow. - The first line of each test case contains an integer N — the size of the array A. - The second line of each test case contains N space-separated integers A_{1}, A_{2}, \dots, A_{N} denoting the array A. ------ Output Format ------ For each test case, output the minimum number of operations required to make all the elements distinct. ------ Constraints ------ $1 ≤ T ≤ 10^{3}$ $1 ≤ N ≤ 2 \cdot 10^{5}$ - The sum of $N$ over all test cases does not exceed $2 \cdot 10^{5}$ $1 ≤ A_{i} ≤ 10^{9}$ ----- Sample Input 1 ------ 3 8 1 3 3 5 1 2 4 1 4 1 2 3 4 5 10 20 10 20 30 ----- Sample Output 1 ------ 4 0 2 ----- explanation 1 ------ Test case $1$: Initially $A = [1, 3, 3, 5, 1, 2, 4, 1]$. Consider the following sequence of operations. - $[1, 3, 3, 5, 1, 2, 4, \underline{1}] \xrightarrow[\mathrm{prepend} \, 6]{\mathrm{remove} \, 1} [\boldsymbol 6, 1, 3, 3, 5, 1, 2, 4]$ - $[\underline{6}, 1, 3, 3, 5, 1, 2, 4] \xrightarrow[\mathrm{append} \, 7]{\mathrm{remove} \, 6} [1, 3, 3, 5, 1, 2, 4, \boldsymbol 7]$ - $[\underline{1}, 3, 3, 5, 1, 2, 4, 7] \xrightarrow[\mathrm{append} \, 8]{\mathrm{remove} \, 1} [3, 3, 5, 1, 2, 4, 7, \boldsymbol 8]$ - $[\underline{3}, 3, 5, 1, 2, 4, 7, 8] \xrightarrow[\mathrm{append} \, 9]{\mathrm{remove} \, 3} [3, 5, 1, 2, 4, 7, 8, \boldsymbol 9]$ All the elements of $A$ are now distinct. It can be shown that it is not possible in less than $4$ operations. Test case $2$: No operations are required since all the elements of $A$ are already distinct.
inf = 10**6 for tcase in range(int(input())): n = int(input()) a = list(map(int, input().split())) d = {} l = 0 ans = inf for i, x in enumerate(a): if x in d: v = d[x] while l <= v: del d[a[l]] l += 1 d[x] = i r = n - 1 - i nop = 2 * min(l, r) + max(l, r) ans = min(ans, nop) print(ans)
ASSIGN VAR BIN_OP NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
def get_arr(v): ans = [] while v != 0: ans.append(v % 2) v //= 2 return ans[::-1] def check_arr(arr): for i in range(len(arr)): for di in range(1, (len(arr) - i) // 2 + 1): if i + 2 * di >= len(arr): continue if arr[i] == arr[i + di] == arr[i + 2 * di]: return True return False s = input() ans = len(s) * (len(s) + 1) // 2 for i in range(len(s)): for j in range(i + 1, min(i + 10, len(s) + 1)): if not check_arr(s[i:j]): ans -= 1 print(ans)
FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR NUMBER FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP NUMBER VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
s = input() n = len(s) ans = 0 l = 0 for i in range(0, n): for j in range(i - 1, l, -1): if 2 * j - i < l: break if s[i] == s[j] == s[j + j - i]: ans += (2 * j - i - l + 1) * (n - i) l = 2 * j - i + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER IF BIN_OP BIN_OP NUMBER VAR VAR VAR IF VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR VAR VAR NUMBER BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
import sys input = sys.stdin.readline s = list(input().rstrip()) n = len(s) for i in range(n): s[i] = int(s[i]) ans = n * (n - 1) // 2 if n >= 2: ans -= n - 1 for d in range(3, 13): for i in range(n): if i + d > n: break s1 = s[i : i + d] flg = False for k in range(1, 5): for st in range(d): if st + 2 * k >= d: break if s1[st] == s1[st + k] == s1[st + 2 * k]: flg = True break if not flg: ans -= 1 print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP NUMBER VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR NUMBER IF VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
s = input() cur, ans = -1, 0 for i in range(len(s)): for j in range(cur + 1, i - 1): if (i + j) % 2 == 0 and s[i] == s[j] and s[i] == s[(i + j) // 2]: cur = j ans += cur + 1 print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR VAR NUMBER NUMBER VAR VAR VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
s = input() n = len(s) sml = n * (n + 1) // 2 for i in range(8): sml -= max(n - i, 0) good3 = set() good4 = set() good5 = set() good6 = set() good7 = set() for i in range(n - 2): if s[i] == s[i + 1] == s[i + 2]: good3.add(i) sml += 1 for i in range(n - 3): if i in good3 or i + 1 in good3: good4.add(i) sml += 1 for i in range(n - 4): if i in good4 or i + 1 in good4 or s[i] == s[i + 2] == s[i + 4]: good5.add(i) sml += 1 for i in range(n - 5): if i in good5 or i + 1 in good5: good6.add(i) sml += 1 for i in range(n - 6): if i in good6 or i + 1 in good6 or s[i] == s[i + 3] == s[i + 6]: good7.add(i) sml += 1 for i in range(n - 7): if i in good7 or i + 1 in good7: sml += 1 print(sml)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
def parse(c, n): l = [-1] * n for x in c: se = set(x) for i in range(len(x) - 1): for j in range(i + 1, len(x)): k = x[j] - x[i] if k > 20: break if x[i] + k + k >= n: break if x[i] + k + k in se: l[x[i] + k + k] = x[i] break res = 0 prex = -1 for i in range(n): if l[i] <= prex: continue res = res + (l[i] - prex) * (n - i) prex = l[i] return res s = input() one = [i for i in range(len(s)) if s[i] == "1"] zero = [i for i in range(len(s)) if s[i] == "0"] ans = parse((one, zero), len(s)) print(ans)
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
s = input() le = len(s) m = [le] * (le + 1) ans = 0 for i in range(le - 1, -1, -1): m[i] = m[i + 1] k = 1 while k * 2 + i < m[i]: if s[i] == s[i + k] and s[i] == s[i + 2 * k]: m[i] = i + 2 * k k += 1 ans += le - m[i] print(ans)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP BIN_OP VAR NUMBER VAR VAR VAR IF VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP NUMBER VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP NUMBER VAR VAR NUMBER VAR BIN_OP VAR VAR VAR EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
import sys input = sys.stdin.readline S = input().strip() L = len(S) ANS1 = [0] * (L + 10) ANS2 = [0] * (L + 10) ANS3 = [0] * (L + 10) for i in range(L - 2): if S[i] == S[i + 1] == S[i + 2]: ANS1[i] = 1 for i in range(L - 4): if S[i] == S[i + 2] == S[i + 4]: ANS2[i] = 1 for i in range(L - 6): if S[i] == S[i + 3] == S[i + 6]: ANS3[i] = 1 SCORE = 0 for i in range(L): if ANS1[i] == 1: SCORE += max(0, L - i - 2) elif ANS1[i + 1] == 1: SCORE += max(0, L - i - 3) elif ANS1[i + 2] == 1: SCORE += max(0, L - i - 4) elif ANS2[i] == 1: SCORE += max(0, L - i - 4) elif ANS2[i + 1] == 1: SCORE += max(0, L - i - 5) elif ANS1[i + 3] == 1: SCORE += max(0, L - i - 5) elif ANS1[i + 4] == 1: SCORE += max(0, L - i - 6) elif ANS2[i + 2] == 1: SCORE += max(0, L - i - 6) elif ANS3[i] == 1: SCORE += max(0, L - i - 6) elif ANS1[i + 5] == 1: SCORE += max(0, L - i - 7) elif ANS2[i + 3] == 1: SCORE += max(0, L - i - 7) elif ANS3[i + 1] == 1: SCORE += max(0, L - i - 7) else: SCORE += max(0, L - i - 8) print(SCORE)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Toad Rash has a binary string $s$. A binary string consists only of zeros and ones. Let $n$ be the length of $s$. Rash needs to find the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. Find this number of pairs for Rash. -----Input----- The first line contains the string $s$ ($1 \leq |s| \leq 300\,000$), consisting of zeros and ones. -----Output----- Output one integer: the number of such pairs of integers $l$, $r$ that $1 \leq l \leq r \leq n$ and there is at least one pair of integers $x$, $k$ such that $1 \leq x, k \leq n$, $l \leq x < x + 2k \leq r$, and $s_x = s_{x+k} = s_{x+2k}$. -----Examples----- Input 010101 Output 3 Input 11001100 Output 0 -----Note----- In the first example, there are three $l$, $r$ pairs we need to count: $1$, $6$; $2$, $6$; and $1$, $5$. In the second example, there are no values $x$, $k$ for the initial string, so the answer is $0$.
def func(st, en): for i in range(st, en + 1): for j in range(i + 1, en + 1): if (j - i + 1) % 2 == 1: if s[(i + j) // 2] == s[i] == s[j]: return False return True s = input().strip() c = 0 n = len(s) for i in range(2, 9): for j in range(len(s)): if i + j < len(s): if func(j, j + i): c += 1 print(n * (n + 1) // 2 - c - n - n + 1)
FUNC_DEF FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER NUMBER IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR NUMBER
You are given an array $a$ consisting of $n$ integers. For each $i$ ($1 \le i \le n$) the following inequality is true: $-2 \le a_i \le 2$. You can remove any number (possibly $0$) of elements from the beginning of the array and any number (possibly $0$) of elements from the end of the array. You are allowed to delete the whole array. You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them. The product of elements of an empty array (array of length $0$) should be assumed to be $1$. -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. Then the descriptions of the input test cases follow. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the length of array $a$. The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($|a_i| \le 2$) — elements of array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two non-negative numbers $x$ and $y$ ($0 \le x + y \le n$) — such that the product (multiplication) of the array numbers, after removing $x$ elements from the beginning and $y$ elements from the end, is maximal. If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be $1$. -----Examples----- Input 5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2 Output 0 2 3 0 2 0 0 1 1 0 -----Note----- In the first case, the maximal value of the product is $2$. Thus, we can either delete the first three elements (obtain array $[2]$), or the last two and one first element (also obtain array $[2]$), or the last two elements (obtain array $[1, 2]$). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2". In the second case, the maximum value of the product is $1$. Then we can remove all elements from the array because the value of the product on the empty array will be $1$. So the answer is "3 0", but there are other possible answers. In the third case, we can remove the first two elements of the array. Then we get the array: $[-2, 2, -1]$. The product of the elements of the resulting array is $(-2) \cdot 2 \cdot (-1) = 4$. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
for _ in range(int(input())): n = int(input()) a = [int(x) for x in input().split()] best = 1, 0, n, 0 best_pow_neg = 1, 0, 0 best_pow_pos = 1, 0, 0 prefix_pow = 0 prefix_sign = 1 for i in range(n): if a[i] == 0: best_pow_neg = 1, 0, i + 1 best_pow_pos = 1, 0, i + 1 prefix_pow = 0 prefix_sign = 1 continue prefix_pow += abs(a[i]) == 2 prefix_sign *= a[i] // abs(a[i]) if prefix_sign == -1: cur = ( prefix_sign * best_pow_neg[0], prefix_pow - best_pow_neg[1], best_pow_neg[2], n - i - 1, ) else: cur = ( prefix_sign * best_pow_pos[0], prefix_pow - best_pow_pos[1], best_pow_pos[2], n - i - 1, ) best = max(best, cur) if prefix_sign == 1: best_pow_pos = min(best_pow_pos, (prefix_sign, prefix_pow, i + 1)) if best_pow_neg[0] == 1 or prefix_pow < best_pow_neg[1]: best_pow_neg = prefix_sign, prefix_pow, i + 1 print(best[2], best[3])
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER VAR NUMBER
You are given an array $a$ consisting of $n$ integers. For each $i$ ($1 \le i \le n$) the following inequality is true: $-2 \le a_i \le 2$. You can remove any number (possibly $0$) of elements from the beginning of the array and any number (possibly $0$) of elements from the end of the array. You are allowed to delete the whole array. You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them. The product of elements of an empty array (array of length $0$) should be assumed to be $1$. -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. Then the descriptions of the input test cases follow. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the length of array $a$. The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($|a_i| \le 2$) — elements of array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two non-negative numbers $x$ and $y$ ($0 \le x + y \le n$) — such that the product (multiplication) of the array numbers, after removing $x$ elements from the beginning and $y$ elements from the end, is maximal. If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be $1$. -----Examples----- Input 5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2 Output 0 2 3 0 2 0 0 1 1 0 -----Note----- In the first case, the maximal value of the product is $2$. Thus, we can either delete the first three elements (obtain array $[2]$), or the last two and one first element (also obtain array $[2]$), or the last two elements (obtain array $[1, 2]$). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2". In the second case, the maximum value of the product is $1$. Then we can remove all elements from the array because the value of the product on the empty array will be $1$. So the answer is "3 0", but there are other possible answers. In the third case, we can remove the first two elements of the array. Then we get the array: $[-2, 2, -1]$. The product of the elements of the resulting array is $(-2) \cdot 2 \cdot (-1) = 4$. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
def main(): for _ in range(int(input())): n = int(input()) arr = list(map(int, input().split())) l_ind, r_ind = 0, n res = 0 pl = 0 pr = 0 nl = None nv = None temp = 1 c = 0 for i, el in enumerate(arr): if el > 0: temp *= 1 elif el < 0: temp *= -1 else: temp = 0 if abs(el) == 2: c += 1 if temp > 0: if c > res: res = c l_ind = pl r_ind = i elif temp < 0: if nv == None: nl = i nv = c elif c - nv > res: res = c - nv l_ind = nl + 1 r_ind = i else: pl = i + 1 pr = 0 nl = None nv = None temp = 1 c = 0 a, b, v = l_ind, r_ind, res if v == 0: print(n, 0) else: print(a, n - b - 1) main()
FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR NUMBER IF VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR NONE ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. For each $i$ ($1 \le i \le n$) the following inequality is true: $-2 \le a_i \le 2$. You can remove any number (possibly $0$) of elements from the beginning of the array and any number (possibly $0$) of elements from the end of the array. You are allowed to delete the whole array. You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them. The product of elements of an empty array (array of length $0$) should be assumed to be $1$. -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. Then the descriptions of the input test cases follow. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the length of array $a$. The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($|a_i| \le 2$) — elements of array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two non-negative numbers $x$ and $y$ ($0 \le x + y \le n$) — such that the product (multiplication) of the array numbers, after removing $x$ elements from the beginning and $y$ elements from the end, is maximal. If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be $1$. -----Examples----- Input 5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2 Output 0 2 3 0 2 0 0 1 1 0 -----Note----- In the first case, the maximal value of the product is $2$. Thus, we can either delete the first three elements (obtain array $[2]$), or the last two and one first element (also obtain array $[2]$), or the last two elements (obtain array $[1, 2]$). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2". In the second case, the maximum value of the product is $1$. Then we can remove all elements from the array because the value of the product on the empty array will be $1$. So the answer is "3 0", but there are other possible answers. In the third case, we can remove the first two elements of the array. Then we get the array: $[-2, 2, -1]$. The product of the elements of the resulting array is $(-2) \cdot 2 \cdot (-1) = 4$. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
t = int(input()) for _ in range(t): n = int(input()) numbers = [int(num) for num in input().split()] max_pow_2 = 0 remove_from_start = n remove_from_end = 0 idx = 0 j = 0 pow_2 = 0 sign = True while idx < n: if numbers[idx] == 0: idx += 1 j = idx pow_2 = 0 sign = True continue if idx > 0 and numbers[idx - 1] != 0: if numbers[idx - 1] < 0: sign = not sign if abs(numbers[idx - 1]) == 2: pow_2 -= 1 if sign and pow_2 > max_pow_2: max_pow_2 = pow_2 remove_from_start = idx remove_from_end = n - j while j < n and numbers[j] != 0: if numbers[j] < 0: sign = not sign if abs(numbers[j]) == 2: pow_2 += 1 if sign and pow_2 > max_pow_2: max_pow_2 = pow_2 remove_from_start = idx remove_from_end = n - j - 1 j += 1 idx += 1 print(remove_from_start, remove_from_end)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. For each $i$ ($1 \le i \le n$) the following inequality is true: $-2 \le a_i \le 2$. You can remove any number (possibly $0$) of elements from the beginning of the array and any number (possibly $0$) of elements from the end of the array. You are allowed to delete the whole array. You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them. The product of elements of an empty array (array of length $0$) should be assumed to be $1$. -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. Then the descriptions of the input test cases follow. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the length of array $a$. The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($|a_i| \le 2$) — elements of array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two non-negative numbers $x$ and $y$ ($0 \le x + y \le n$) — such that the product (multiplication) of the array numbers, after removing $x$ elements from the beginning and $y$ elements from the end, is maximal. If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be $1$. -----Examples----- Input 5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2 Output 0 2 3 0 2 0 0 1 1 0 -----Note----- In the first case, the maximal value of the product is $2$. Thus, we can either delete the first three elements (obtain array $[2]$), or the last two and one first element (also obtain array $[2]$), or the last two elements (obtain array $[1, 2]$). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2". In the second case, the maximum value of the product is $1$. Then we can remove all elements from the array because the value of the product on the empty array will be $1$. So the answer is "3 0", but there are other possible answers. In the third case, we can remove the first two elements of the array. Then we get the array: $[-2, 2, -1]$. The product of the elements of the resulting array is $(-2) \cdot 2 \cdot (-1) = 4$. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
def count(nums): total = {(-2): 0, (-1): 0, (1): 0, (2): 0} for i in nums: total[i] += 1 return total def solution(): n = int(input()) a = [int(i) for i in input().split()] ans = 0 x, y = n, 0 zero_pos = [-1] + [i for i in range(n) if a[i] == 0] + [n] for i in range(len(zero_pos) - 1): l, r = zero_pos[i] + 1, zero_pos[i + 1] - 1 c = count(a[l : r + 1]) if (c[-1] + c[-2]) % 2 == 0 and c[2] + c[-2] > ans: ans = c[2] + c[-2] x, y = l, n - r - 1 elif (c[-1] + c[-2]) % 2 != 0: leftpos, rightpos = l, r while a[leftpos] > 0: leftpos += 1 while a[rightpos] > 0: rightpos -= 1 c = count(a[l:rightpos]) if c[2] + c[-2] > ans: ans = c[2] + c[-2] x, y = l, n - rightpos c = count(a[leftpos + 1 : r + 1]) if c[2] + c[-2] > ans: ans = c[2] + c[-2] x, y = leftpos + 1, n - r - 1 print(x, y) t = int(input()) for _ in range(t): solution()
FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER LIST VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR NUMBER VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR WHILE VAR VAR NUMBER VAR NUMBER WHILE VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
You are given an array $a$ consisting of $n$ integers. For each $i$ ($1 \le i \le n$) the following inequality is true: $-2 \le a_i \le 2$. You can remove any number (possibly $0$) of elements from the beginning of the array and any number (possibly $0$) of elements from the end of the array. You are allowed to delete the whole array. You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them. The product of elements of an empty array (array of length $0$) should be assumed to be $1$. -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. Then the descriptions of the input test cases follow. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the length of array $a$. The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($|a_i| \le 2$) — elements of array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two non-negative numbers $x$ and $y$ ($0 \le x + y \le n$) — such that the product (multiplication) of the array numbers, after removing $x$ elements from the beginning and $y$ elements from the end, is maximal. If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be $1$. -----Examples----- Input 5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2 Output 0 2 3 0 2 0 0 1 1 0 -----Note----- In the first case, the maximal value of the product is $2$. Thus, we can either delete the first three elements (obtain array $[2]$), or the last two and one first element (also obtain array $[2]$), or the last two elements (obtain array $[1, 2]$). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2". In the second case, the maximum value of the product is $1$. Then we can remove all elements from the array because the value of the product on the empty array will be $1$. So the answer is "3 0", but there are other possible answers. In the third case, we can remove the first two elements of the array. Then we get the array: $[-2, 2, -1]$. The product of the elements of the resulting array is $(-2) \cdot 2 \cdot (-1) = 4$. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
for _ in range(int(input())): n = int(input()) nums = list(map(int, input().split())) ans = 0 ansl, ansr = n, 0 l = -1 for i in range(n + 1): if i == n or nums[i] == 0: count = 0 left = -1 right = -1 neg = 1 zr = 0 zl = 0 for j in range(l + 1, i): if nums[j] < 0: neg *= -1 right = j zr = 0 if abs(nums[j]) == 2: count += 1 zr += 1 if left == -1: zl += 1 if nums[j] < 0 and left == -1: left = j if neg == -1: if count - zl > count - zr: right = i count -= zl else: left = l count -= zr else: left = l right = i if ans < count: ans = count ansl = left + 1 ansr = n - right l = i print(ansl, ansr)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER IF BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR
You are given an array $a$ consisting of $n$ integers. For each $i$ ($1 \le i \le n$) the following inequality is true: $-2 \le a_i \le 2$. You can remove any number (possibly $0$) of elements from the beginning of the array and any number (possibly $0$) of elements from the end of the array. You are allowed to delete the whole array. You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them. The product of elements of an empty array (array of length $0$) should be assumed to be $1$. -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. Then the descriptions of the input test cases follow. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the length of array $a$. The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($|a_i| \le 2$) — elements of array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two non-negative numbers $x$ and $y$ ($0 \le x + y \le n$) — such that the product (multiplication) of the array numbers, after removing $x$ elements from the beginning and $y$ elements from the end, is maximal. If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be $1$. -----Examples----- Input 5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2 Output 0 2 3 0 2 0 0 1 1 0 -----Note----- In the first case, the maximal value of the product is $2$. Thus, we can either delete the first three elements (obtain array $[2]$), or the last two and one first element (also obtain array $[2]$), or the last two elements (obtain array $[1, 2]$). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2". In the second case, the maximum value of the product is $1$. Then we can remove all elements from the array because the value of the product on the empty array will be $1$. So the answer is "3 0", but there are other possible answers. In the third case, we can remove the first two elements of the array. Then we get the array: $[-2, 2, -1]$. The product of the elements of the resulting array is $(-2) \cdot 2 \cdot (-1) = 4$. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
tests = int(input()) for _ in range(tests): num_len = int(input()) n = num_len all_num = input() a = [int(i) for i in all_num.split(" ")] a = [1] + a + [0] m = 0 lef = 0 rig = num_len num_len = len(a) op = [0] * num_len ex = [0] * num_len j = 0 l = 0 for i in range(1, num_len): if a[i] < 0: op[i] = op[i - 1] + 1 else: op[i] = op[i - 1] if a[i] == 2 or a[i] == -2: ex[i] = ex[i - 1] + 1 else: ex[i] = ex[i - 1] if a[i] == 0: for j in range(l + 1, i): if (op[j] - op[l]) % 2 == 0 and ex[j] - ex[l] > m: lef = l rig = n - j m = ex[j] - ex[l] if (op[i - 1] - op[j - 1]) % 2 == 0 and ex[i - 1] - ex[j - 1] > m: lef = j - 1 rig = n - i + 1 m = ex[i - 1] - ex[j - 1] l = i print("{} {}".format(lef, rig))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP LIST NUMBER VAR LIST NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP VAR VAR VAR VAR NUMBER NUMBER BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR IF BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR VAR
You are given an array $a$ consisting of $n$ integers. For each $i$ ($1 \le i \le n$) the following inequality is true: $-2 \le a_i \le 2$. You can remove any number (possibly $0$) of elements from the beginning of the array and any number (possibly $0$) of elements from the end of the array. You are allowed to delete the whole array. You need to answer the question: how many elements should be removed from the beginning of the array, and how many elements should be removed from the end of the array, so that the result will be an array whose product (multiplication) of elements is maximal. If there is more than one way to get an array with the maximum product of elements on it, you are allowed to output any of them. The product of elements of an empty array (array of length $0$) should be assumed to be $1$. -----Input----- The first line of input data contains an integer $t$ ($1 \le t \le 10^4$) —the number of test cases in the test. Then the descriptions of the input test cases follow. The first line of each test case description contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$) —the length of array $a$. The next line contains $n$ integers $a_1, a_2, \dots, a_n$ ($|a_i| \le 2$) — elements of array $a$. It is guaranteed that the sum of $n$ over all test cases does not exceed $2 \cdot 10^5$. -----Output----- For each test case, output two non-negative numbers $x$ and $y$ ($0 \le x + y \le n$) — such that the product (multiplication) of the array numbers, after removing $x$ elements from the beginning and $y$ elements from the end, is maximal. If there is more than one way to get the maximal product, it is allowed to output any of them. Consider the product of numbers on empty array to be $1$. -----Examples----- Input 5 4 1 2 -1 2 3 1 1 -2 5 2 0 -2 2 -1 3 -2 -1 -1 3 -1 -2 -2 Output 0 2 3 0 2 0 0 1 1 0 -----Note----- In the first case, the maximal value of the product is $2$. Thus, we can either delete the first three elements (obtain array $[2]$), or the last two and one first element (also obtain array $[2]$), or the last two elements (obtain array $[1, 2]$). Thus, in the first case, the answers fit: "3 0", or "1 2", or "0 2". In the second case, the maximum value of the product is $1$. Then we can remove all elements from the array because the value of the product on the empty array will be $1$. So the answer is "3 0", but there are other possible answers. In the third case, we can remove the first two elements of the array. Then we get the array: $[-2, 2, -1]$. The product of the elements of the resulting array is $(-2) \cdot 2 \cdot (-1) = 4$. This value is the maximum possible value that can be obtained. Thus, for this case the answer is: "2 0".
t = int(input()) for i in range(t): _ = int(input()) nums = list(map(int, input().split())) start = 0 end = 0 count_2 = 0 neg = 1 max_prod = 0 max_range = [0, 0] while start <= end and start < len(nums): if end == len(nums) or nums[end] == 0: if start == end: count_2 = 0 start = end = end + 1 continue count_2 -= abs(nums[start]) // 2 neg *= nums[start] // abs(nums[start]) start += 1 if neg * count_2 > max_prod: max_prod = count_2 max_range = [start, end] else: count_2 += abs(nums[end]) // 2 neg *= nums[end] // abs(nums[end]) end += 1 if neg * count_2 > max_prod: max_prod = count_2 max_range = [start, end] print(max_range[0], len(nums) - max_range[1])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER NUMBER WHILE VAR VAR VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR VAR NUMBER
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction [Image] whose denominator is no more than n. Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that the value $|\frac{x}{y} - \frac{a}{b}|$ is as minimal as possible. If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator. -----Input----- A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 10^5). -----Output----- Print the required fraction in the format "a/b" (without quotes). -----Examples----- Input 3 7 6 Output 2/5 Input 7 2 4 Output 7/2
def s(): [x, y, n] = list(map(int, input().split())) res = 0, 1 for b in range(1, n + 1): a = int(round(b * x / y, 0) + 0.1) if abs(x * res[1] - y * res[0]) * b > abs(x * b - y * (a - 1)) * res[1]: res = a - 1, b if abs(x * res[1] - y * res[0]) * b > abs(x * b - y * a) * res[1]: res = a, b print(*res, sep="/") s()
FUNC_DEF ASSIGN LIST VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER NUMBER IF BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction [Image] whose denominator is no more than n. Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that the value $|\frac{x}{y} - \frac{a}{b}|$ is as minimal as possible. If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator. -----Input----- A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 10^5). -----Output----- Print the required fraction in the format "a/b" (without quotes). -----Examples----- Input 3 7 6 Output 2/5 Input 7 2 4 Output 7/2
x, y, n = map(int, input().split()) best, mn, eps = [0, 0], 10**9, 1e-15 for i in range(1, n + 1): m1 = i * x // y m2 = (i * x + y - 1) // y a = abs(x / y - m1 / i) b = abs(x / y - m2 / i) if a < mn - eps: mn = a best = [m1, i] if b < mn - eps: mn = b best = [m2, i] print(*best, sep="/")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR VAR LIST NUMBER NUMBER BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR IF VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR STRING
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction [Image] whose denominator is no more than n. Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that the value $|\frac{x}{y} - \frac{a}{b}|$ is as minimal as possible. If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator. -----Input----- A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 10^5). -----Output----- Print the required fraction in the format "a/b" (without quotes). -----Examples----- Input 3 7 6 Output 2/5 Input 7 2 4 Output 7/2
class Fraction: def __init__(self, x, y): self.x = x self.y = y def __sub__(self, a): return Fraction(self.x * a.y - a.x * self.y, self.y * a.y) def __lt__(self, a): return self.x * a.y < self.y * a.x def __abs__(self): if self.x < 0: return Fraction(-self.x, self.y) return self def __str__(self): return str(self.x) + "/" + str(self.y) x, y, n = list(map(int, input().split())) f = Fraction(x, y) dif = None for i in range(1, n + 1): m = x * i // y for d in [m, m + 1]: g = Fraction(d, i) if dif is None or abs(f - g) < dif: dif = abs(f - g) ans = g print(ans)
CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF RETURN BIN_OP VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR NUMBER RETURN FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF RETURN BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR FOR VAR LIST VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE FUNC_CALL VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction [Image] whose denominator is no more than n. Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that the value $|\frac{x}{y} - \frac{a}{b}|$ is as minimal as possible. If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator. -----Input----- A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 10^5). -----Output----- Print the required fraction in the format "a/b" (without quotes). -----Examples----- Input 3 7 6 Output 2/5 Input 7 2 4 Output 7/2
def gcd(a, b): return gcd(b % a, a) if a else b x, y, n = map(int, input().split()) z = gcd(x, y) x, y = x // z, y // z if n < y: d = [min(i, y - i) for i in (x * i % y for i in range(0, n + 1))] for i in range(n - 1, 0, -1): if d[i] < d[n] and n * d[i] <= i * d[n]: n = i print(str(x * n // y + int(2 * (x * n % y) > y)) + "/" + str(n)) else: print(str(x) + "/" + str(y))
FUNC_DEF RETURN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP NUMBER BIN_OP BIN_OP VAR VAR VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction [Image] whose denominator is no more than n. Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that the value $|\frac{x}{y} - \frac{a}{b}|$ is as minimal as possible. If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator. -----Input----- A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 10^5). -----Output----- Print the required fraction in the format "a/b" (without quotes). -----Examples----- Input 3 7 6 Output 2/5 Input 7 2 4 Output 7/2
x, y, n = map(int, input().split()) ansNum = -1 ansDenom = -1 for b in range(1, n + 1): a = x * b // y if x * b % y * 2 > y: a += 1 if ansNum == -1 or abs(x / y - ansNum / ansDenom) > abs(x / y - a / b): ansNum = a ansDenom = b print(ansNum, ansDenom, sep="/")
ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF BIN_OP BIN_OP BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER IF VAR NUMBER FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR STRING
You are given three positive integers x, y, n. Your task is to find the nearest fraction to fraction [Image] whose denominator is no more than n. Formally, you should find such pair of integers a, b (1 ≤ b ≤ n; 0 ≤ a) that the value $|\frac{x}{y} - \frac{a}{b}|$ is as minimal as possible. If there are multiple "nearest" fractions, choose the one with the minimum denominator. If there are multiple "nearest" fractions with the minimum denominator, choose the one with the minimum numerator. -----Input----- A single line contains three integers x, y, n (1 ≤ x, y, n ≤ 10^5). -----Output----- Print the required fraction in the format "a/b" (without quotes). -----Examples----- Input 3 7 6 Output 2/5 Input 7 2 4 Output 7/2
import sys s = input() all = s.split() ans = "lol" n = int(all[2]) x = float(all[0]) y = float(all[1]) a = 0 b = 1 dif = x / y for i in range(1, n + 1): na = int(x * i / y) if dif > abs(x * i - na * y) / (y * i): a = na b = i dif = abs(x * i - na * y) / (y * i) na = na + 1 if dif > abs(x * i - na * y) / (y * i): a = na b = i dif = abs(x * i - na * y) / (y * i) ans = str(a) + "/" + str(b) print(ans)
IMPORT ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
You are given an array $a_1, a_2 \dots a_n$. Calculate the number of tuples $(i, j, k, l)$ such that: $1 \le i < j < k < l \le n$; $a_i = a_k$ and $a_j = a_l$; -----Input----- The first line contains a single integer $t$ ($1 \le t \le 100$) — the number of test cases. The first line of each test case contains a single integer $n$ ($4 \le n \le 3000$) — the size of the array $a$. The second line of each test case contains $n$ integers $a_1, a_2, \dots, a_n$ ($1 \le a_i \le n$) — the array $a$. It's guaranteed that the sum of $n$ in one test doesn't exceed $3000$. -----Output----- For each test case, print the number of described tuples. -----Example----- Input 2 5 2 2 2 2 2 6 1 3 3 1 2 3 Output 5 2 -----Note----- In the first test case, for any four indices $i < j < k < l$ are valid, so the answer is the number of tuples. In the second test case, there are $2$ valid tuples: $(1, 2, 4, 6)$: $a_1 = a_4$ and $a_2 = a_6$; $(1, 3, 4, 6)$: $a_1 = a_4$ and $a_3 = a_6$.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) arr = list(map(int, input().split())) m = max(arr) look = [[(0) for _ in range(m + 1)] for _ in range(n)] for i in range(len(arr)): for j in range(m + 1): look[i][j] = look[i - 1][j] x = arr[i] look[i][x] += 1 ans = 0 for i in range(1, n - 2): for j in range(i + 1, n - 1): x = arr[j] t = look[i - 1][x] y = arr[i] v = look[-1][y] - look[j][y] ans += t * v print(ans)
IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR