description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() operations = [] for i in range(n - 1): if a[i] != a[i + 1]: operations.append(i + 1) if a[-1] == "1": operations.append(n) if b[-1] == "1": operations.append(n) for i in reversed(range(1, n)): if b[i] != b[i - 1]: operations.append(i) print(len(operations), end=" ") print(*operations)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
import sys input = sys.stdin.readline T = int(input()) for t in range(T): N = int(input()) A = input()[:-1] B = input()[:-1] answer = [] el0 = A[0] for i in range(N - 1, -1, -1): if el0 == B[i]: answer.append(1) answer.append(i + 1) if i == N - 1: el0 = "0" if A[i] == "1" else "1" elif (N - i) % 2: el0 = "0" if A[N - 1 - (N - i - 1) // 2] == "1" else "1" else: el0 = A[(N - i) // 2] print(len(answer), " ".join(map(str, answer)))
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 NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR STRING STRING STRING IF BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER STRING STRING STRING ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) s = [int(item) for item in list(input().rstrip())] t = [int(item) for item in list(input().rstrip())] srev = [] for item in s: srev.append(1 - item) ans = [] state = 1 for_idx = n - 1 rev_idx = 0 for i in range(n): ti = t[n - 1 - i] if state: si = s[for_idx] if ti == si: for_idx -= 1 continue if ti == s[rev_idx]: ans.append(1) ans.append(n - i) rev_idx += 1 else: si = srev[rev_idx] if ti == si: rev_idx += 1 continue if ti == srev[for_idx]: ans.append(1) ans.append(n - i) for_idx -= 1 state = 1 - state if state: if s[rev_idx] != t[0]: ans.append(1) elif srev[for_idx] != t[0]: ans.append(1) if not ans: print(0) else: print(len(ans), *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 VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR VAR NUMBER IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR IF VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
import sys sys.setrecursionlimit(10**5) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def SI(): return sys.stdin.readline()[:-1] for _ in range(II()): n = II() a = SI() b = SI() ans = [] l = 0 r = n - 1 rev = False for i in range(n - 1, 0, -1): if rev: if a[l] != b[i]: l += 1 continue if a[r] != b[i]: ans.append(1) ans.append(i + 1) r -= 1 rev = False else: if a[r] == b[i]: r -= 1 continue if a[l] == b[i]: ans.append(1) ans.append(i + 1) l += 1 rev = True if rev ^ (a[r] != b[0]): ans.append(1) print(len(ans), *ans)
IMPORT EXPR FUNC_CALL VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR STRING FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
import sys def solve(ls1, ls2, debug=0): n = len(ls1) result = [] for i in range(n): if i % 2: j = -1 - i // 2 x = ls1[j] ^ 1 else: j = i // 2 x = ls1[j] y = ls2[-i - 1] if debug: print(f"i: {i}, j: {j}, x: {x}, y: {y}") if x == y: result.append(1) result.append(n - i) return result def main(istr, ostr): t = int(istr.readline()) for _ in range(t): n = int(istr.readline()) ls1 = list(map(int, istr.readline().strip())) ls2 = list(map(int, istr.readline().strip())) result = solve(ls1, ls2) print(len(result), *result, file=ostr) main(sys.stdin, sys.stdout)
IMPORT FUNC_DEF NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR EXPR FUNC_CALL VAR STRING VAR STRING VAR STRING VAR STRING VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR 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 ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
import sys input = sys.stdin.readline t = int(input()) for _ in range(t): n = int(input()) a = list(map(int, input().strip())) b = list(map(int, input().strip())) ans1, ans2 = [], [] if a.count(1) > n // 2: for i in range(n): a[i] = 1 * (a[i] == 0) a.reverse() ans1.append(n) for i in range(n): if a[i] == 1: if i != 0: ans1.append(i) ans1.append(i + 1) if b.count(1) > n // 2: for i in range(n): b[i] = 1 * (b[i] == 0) b.reverse() ans2.append(n) for i in range(n): if b[i] == 1: if i != 0: ans2.append(i) ans2.append(i + 1) print(len(ans1) + len(ans2), *ans1, *ans2[::-1])
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 FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST LIST IF FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() i = 1 s = a[0] ans = [] while i < n: if a[i] == s[-1]: i += 1 continue elif a[i] == "1" and s[-1] == "0": s = "1" ans.append(i) else: s = "0" ans.append(i) i += 1 if s[-1] == "1": ans.append(n) i = 1 s = b[0] ans2 = [] while i < n: if b[i] == s[-1]: i += 1 continue elif b[i] == "1" and s[-1] == "0": s = "1" ans2.append(i) else: s = "0" ans2.append(i) i += 1 if s[-1] == "1": ans2.append(n) l = ans + ans2[::-1] print(len(l), *l)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR STRING VAR NUMBER STRING ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
for _ in range(int(input())): n = int(input()) s = input() b = input() A = [] for i in range(n - 1): if s[i] != s[i + 1]: A.append(i + 1) if s[-1] == "1": A.append(n) A1 = [] for i in range(n - 1): if b[i] != b[i + 1]: A1.append(i + 1) if b[-1] == "1": A1.append(n) A += A1[::-1] print(len(A), *A)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
t = int(input()) for _ in range(t): n = int(input()) a = input() b = input() ans = [] cur = a[0] inv = 0 in0 = 0 arr = [0] * n ar1 = [i for i in range(1, (n + 1) // 2 + 1)] ar2 = [i for i in range(n, (n + 1) // 2, -1)] na = [] for i in range(len(ar2)): na.append(ar1[i] - 1) na.append(ar2[i] - 1) if n % 2 == 1: na.append(ar1[-1] - 1) cnt = 0 for i in range(n - 1, -1, -1): tmp = a[na[cnt]] if cnt % 2 == 1: cur = "0" if tmp == "1" else "1" else: cur = tmp if b[i] == cur: ans.append(1) ans.append(i + 1) else: ans.append(i + 1) inv += 1 cnt += 1 print(len(ans), end=" ") for i in ans: print(i, end=" ") print()
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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR STRING STRING STRING ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
for tt in range(int(input())): n = int(input()) b = input() a = input() ans = [] for i in range(n - 1): if b[i] != b[i + 1]: ans.append(i + 1) cur = int(b[-1]) for i in range(n - 1, -1, -1): if cur != int(a[i]): ans.append(i + 1) cur ^= 1 print(len(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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
import sys LI = lambda: list(map(int, sys.stdin.readline().strip("\n").split())) MI = lambda: map(int, sys.stdin.readline().strip("\n").split()) SI = lambda: sys.stdin.readline().strip("\n") II = lambda: int(sys.stdin.readline().strip("\n")) for _ in range(II()): n, a, b = II(), SI() + "0", SI() + "0" one, two = [], [] for i in range(n): if a[i + 1] != a[i]: one.append(i + 1) if b[i + 1] != b[i]: two.append(i + 1) ans = one + two[::-1] print(len(ans), *ans)
IMPORT ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP FUNC_CALL VAR STRING ASSIGN VAR VAR LIST LIST FOR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() ans = [] arr = [] for i in range(n - 1): if a[i] != a[i + 1]: ans.append(i + 1) for i in range(n - 1): if b[i] != b[i + 1]: arr.append(i + 1) if a[n - 1] != b[n - 1]: ans.append(n) for i in range(len(arr) - 1, -1, -1): ans.append(arr[i]) print(len(ans), *ans, sep=" ")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR STRING
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
for _ in range(int(input())): n = int(input()) a = input() b = input() ans = [] sign = -1 for ansi in range(n, 0, -1): if (a[(n + ansi * sign) // 2] == b[ansi - 1]) ^ (sign > 0): ans.append(1) ans.append(ansi) sign = -sign print(len(ans), *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 ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR NUMBER NUMBER IF BIN_OP VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
This is the hard version of the problem. The difference between the versions is the constraint on $n$ and the required number of operations. You can make hacks only if all versions of the problem are solved. There are two binary strings $a$ and $b$ of length $n$ (a binary string is a string consisting of symbols $0$ and $1$). In an operation, you select a prefix of $a$, and simultaneously invert the bits in the prefix ($0$ changes to $1$ and $1$ changes to $0$) and reverse the order of the bits in the prefix. For example, if $a=001011$ and you select the prefix of length $3$, it becomes $011011$. Then if you select the entire string, it becomes $001001$. Your task is to transform the string $a$ into $b$ in at most $2n$ operations. It can be proved that it is always possible. -----Input----- The first line contains a single integer $t$ ($1\le t\le 1000$)  — the number of test cases. Next $3t$ lines contain descriptions of test cases. The first line of each test case contains a single integer $n$ ($1\le n\le 10^5$)  — the length of the binary strings. The next two lines contain two binary strings $a$ and $b$ of length $n$. It is guaranteed that the sum of $n$ across all test cases does not exceed $10^5$. -----Output----- For each test case, output an integer $k$ ($0\le k\le 2n$), followed by $k$ integers $p_1,\ldots,p_k$ ($1\le p_i\le n$). Here $k$ is the number of operations you use and $p_i$ is the length of the prefix you flip in the $i$-th operation. -----Example----- Input 5 2 01 10 5 01011 11100 2 01 01 10 0110011011 1000110100 1 0 1 Output 3 1 2 1 6 5 2 5 3 1 2 0 9 4 1 2 10 4 1 2 1 5 1 1 -----Note----- In the first test case, we have $01\to 11\to 00\to 10$. In the second test case, we have $01011\to 00101\to 11101\to 01000\to 10100\to 00100\to 11100$. In the third test case, the strings are already the same. Another solution is to flip the prefix of length $2$, which will leave $a$ unchanged.
import sys inp = sys.stdin.read().split() ii = 0 t = int(inp[ii]) ii += 1 for _ in range(t): toprint = [] n = int(inp[ii]) ii += 1 c = list(map(int, inp[ii])) ii += 1 b = list(map(int, inp[ii])) ii += 1 count = 0 for i in range(n - 1, -1, -1): if count % 2 == 0: lead = c[count // 2] else: lead = 1 - c[-count // 2] y = b[i] if i == 0: if lead == y: pass else: toprint.append(1) else: if lead == y: toprint.append(1) toprint.append(i + 1) count += 1 print(len(toprint), " ".join(list(map(str, toprint))))
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR IF VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): i, j, ans = 0, 0, 0 state = {} while j < N: state[fruits[j]] = 1 + state.get(fruits[j], 0) while len(state) > 2: state[fruits[i]] = -1 + state.get(fruits[i]) if state[fruits[i]] == 0: state.pop(fruits[i]) i += 1 ans = max(ans, j - i + 1) j += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR DICT WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): start, end, count, ans = 0, 0, 0, 0 cnt = [0] * (max(fruits) + 1) while end < N: if cnt[fruits[end]] == 0: count += 1 cnt[fruits[end]] += 1 end += 1 while start < end and count > 2: cnt[fruits[start]] -= 1 if cnt[fruits[start]] == 0: count -= 1 start += 1 ans = max(ans, end - start) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): dic = {} cnt = 0 ans = 0 j = 0 for i in range(N): el = fruits[i] if el not in dic: dic[el] = 1 cnt += 1 while len(dic) > 2 and j < i: dic[fruits[j]] -= 1 cnt -= 1 if dic[fruits[j]] == 0: del dic[fruits[j]] j += 1 else: dic[el] += 1 cnt += 1 ans = max(ans, cnt) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, n, fruits): hashmap = {} l, r = 0, 0 res = 0 while r < n: while len(hashmap) >= 2 and fruits[r] not in hashmap: hashmap[fruits[l]] -= 1 if hashmap[fruits[l]] == 0: del hashmap[fruits[l]] l += 1 hashmap[fruits[r]] = 1 + hashmap.get(fruits[r], 0) res = max(res, r - l + 1) r += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): i, j = 0, 0 d = {} maxi = 1 while j < N: d[fruits[j]] = d.get(fruits[j], 0) + 1 if len(d) <= 2: maxi = max(maxi, j - i + 1) j += 1 else: while len(d) > 2: d[fruits[i]] -= 1 if d[fruits[i]] == 0: d.pop(fruits[i]) i += 1 if len(d) == 2: maxi = max(maxi, j - i + 1) j += 1 return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): fruitsCnt = dict() maxFruits = 0 startIdx = 0 for endIdx in range(len(fruits)): if fruits[endIdx] in fruitsCnt: fruitsCnt[fruits[endIdx]] += 1 else: fruitsCnt[fruits[endIdx]] = 1 while startIdx < endIdx and len(fruitsCnt) > 2: fruitsCnt[fruits[startIdx]] -= 1 if fruitsCnt[fruits[startIdx]] == 0: fruitsCnt.pop(fruits[startIdx], None) startIdx += 1 maxFruits = max(maxFruits, endIdx - startIdx + 1) return maxFruits
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NONE VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): res = 0 r = 0 d = dict() for i in range(N): d[fruits[i]] = d.get(fruits[i], 0) + 1 while len(d) > 2: d[fruits[r]] = d.get(fruits[r], 0) - 1 if d[fruits[r]] == 0: del d[fruits[r]] r += 1 if len(d) <= 2: ans = i - r + 1 res = max(res, ans) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): k = 2 type = {} i = 0 j = 0 max_fruits = 0 while j < N: fruit = fruits[j] type[fruit] = type.get(fruit, 0) + 1 if len(type) > k: max_fruits = max(max_fruits, j - i) while len(type) > k: type[fruits[i]] = type[fruits[i]] - 1 if type[fruits[i]] == 0: del type[fruits[i]] i += 1 j += 1 max_fruits = max(max_fruits, j - i) return max_fruits
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): basket = {} max_picked = 0 left = 0 for right in range(len(fruits)): basket[fruits[right]] = basket.get(fruits[right], 0) + 1 while len(basket) > 2: basket[fruits[left]] -= 1 if basket[fruits[left]] == 0: del basket[fruits[left]] left += 1 max_picked = max(max_picked, right - left + 1) return max_picked
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): first = fruits[0] second = -1 i = 0 j = 1 ans = 1 while j < N: if fruits[j] != first and fruits[j] != second: if second == -1: second = fruits[j] else: i = j while fruits[i - 1] == fruits[j - 1]: i -= 1 if first != fruits[i]: first = fruits[j] else: second = fruits[j] ans = max(ans, j - i + 1) j += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): pt1, pt2 = 0, 0 hash_map = {} ct = 0 no_fruits = 0 max_fruits = 0 while pt1 <= pt2 and pt2 < N: if fruits[pt2] in hash_map: hash_map[fruits[pt2]] += 1 else: ct += 1 hash_map[fruits[pt2]] = 1 no_fruits += 1 while ct > 2: hash_map[fruits[pt1]] -= 1 no_fruits -= 1 if hash_map[fruits[pt1]] == 0: del hash_map[fruits[pt1]] ct -= 1 pt1 += 1 max_fruits = max(max_fruits, no_fruits) pt2 += 1 return max_fruits
CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, N, fruits): fp, lp, mp = 0, 0, {} ans = 0 while lp < N: if fruits[lp] not in mp and len(mp) == 2: while len(mp) == 2 and fp < lp: mp[fruits[fp]] -= 1 if mp[fruits[fp]] == 0: mp.pop(fruits[fp]) fp += 1 if fruits[lp] in mp: mp[fruits[lp]] += 1 else: mp[fruits[lp]] = 1 ans = max(ans, lp - fp + 1) lp += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER NUMBER DICT ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, n, fruits): hm = {} l = 0 r = 0 k = 2 ans = 0 while r < n: if fruits[r] in hm: hm[fruits[r]] += 1 else: hm[fruits[r]] = 1 if len(hm) > k: hm[fruits[l]] -= 1 if hm[fruits[l]] == 0: del hm[fruits[l]] l += 1 ans = max(r - l + 1, ans) r += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR NUMBER RETURN VAR
You are visiting a farm that has a single row of fruit trees arranged from left to right.The trees are represented by an integer array fruits of size N, where fruits[i] is the type of fruit the ith tree produces. You want to collect as much fruit as possible.However,the owner has some strict rules that you must follow : You only have two baskets , and each basket can only hold a single type of fruit. There is no limit on the amount of fruit each basket can hold. Starting from any tree of your choice,you must pick exactly one fruit from every tree (including the start tree) while moving to the right. The picked fruits must fit in one of the baskets. Once you reach a tree with fruit that cannot fit in your baskets, you must stop. Given the integer array fruits , return the maximum number of fruits you can pick. Example 1: Input: N = 3 fruits [ ] = { 2, 1, 2 } Output: 3 Explanation: We can pick from all three trees. Example 2: Input: N = 6 fruits [ ] = { 0, 1, 2, 2, 2, 2 } Output: 5 Explanation: It's optimal to pick from trees(0-indexed) [1,2,3,4,5]. Your Task: You don't need to read input or print anything. Your task is to complete function totalFruits() which takes the integer array fruits and it's size N as input parameters and returns the maximum number of fruits the you can pick. Expected Time Complexity: O(N). Expected Auxiliary Space: O(1). Constraints: 1 ≤ N ≤ 10^{5} 0 ≤ fruits_{i} < N
class Solution: def sumSubarrayMins(self, n, a): d = {} f = 0 for i in range(n): if len(d) == 2: f = 1 break elif a[i] not in d.keys(): d[a[i]] = 1 else: d[a[i]] += 1 if f == 0: i += 1 j = 0 s = sum(d.values()) ans = s while i < n: if a[i] in d.keys(): d[a[i]] += 1 s += 1 else: while j < i and len(d) == 2: d[a[j]] -= 1 s -= 1 if d[a[j]] == 0: del d[a[j]] j += 1 d[a[i]] = 1 s += 1 i += 1 ans = max(ans, s) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: averages = [] first = True sum_avg = 0 for i in range(len(arr) - (k - 1)): if first: for j in range(k): sum_avg += arr[i + j] first = False else: sum_avg -= arr[i - 1] sum_avg += arr[i + (k - 1)] averages.append(sum_avg / k) averages.sort(reverse=True) i = 0 while i < len(averages) and averages[i] >= threshold: i += 1 return i
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER IF VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: threshold *= k window = 0 ans = 0 for r, num in enumerate(arr): window += num if r >= k: window -= arr[r - k] if r >= k - 1 and window >= threshold: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF VAR VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, a: List[int], k: int, threshold: int) -> int: lo, sum_of_win, cnt, target = -1, 0, 0, k * threshold for hi, v in enumerate(a): sum_of_win += v if hi - lo == k: if sum_of_win >= target: cnt += 1 lo += 1 sum_of_win -= a[lo] return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER NUMBER NUMBER BIN_OP VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: res = 0 s = sum(arr[: k - 1]) t = threshold * k for i in range(k - 1, len(arr)): s += arr[i] if s >= t: res += 1 s -= arr[i + 1 - k] return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: goal = k * threshold cSums = [(0) for i in arr] c = 0 for i in range(len(arr)): if i == 0: cSums[0] = arr[0] else: cSums[i] = cSums[i - 1] + arr[i] if i == k - 1: if cSums[i] >= goal: c += 1 elif i >= k: if cSums[i] - cSums[i - k] >= goal: c += 1 print(cSums) return c
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: minVal = k * threshold beg = 0 end = k - 1 total = 0 currSum = 0 for i in range(beg, end + 1): currSum += arr[i] while end < len(arr): if beg != 0: currSum = currSum - arr[beg - 1] + arr[end] if currSum >= minVal: total += 1 beg += 1 end += 1 return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: if len(arr) < k: return 0 prevAverage = 0 counter = 0 for i in range(len(arr) - k + 1): if i == 0: average = sum(arr[i : i + k]) / k else: average = (prevAverage * k - arr[i - 1] + arr[i + k - 1]) / k if average >= threshold: counter += 1 prevAverage = average return counter
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: sm = threshold * k ct = 0 tot = 0 for i in range(len(arr)): tot += arr[i] if i < k - 1: continue if i > k - 1: tot -= arr[i - k] if tot >= sm: ct += 1 return ct
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: s = l = 0 for r in range(k): s += arr[r] ans = 0 while r < len(arr) - 1: if s >= threshold * k: ans += 1 s -= arr[l] l += 1 r += 1 s += arr[r] ans += s >= threshold * k return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: if len(arr) < k: return None k_sum = sum([arr[i] for i in range(0, k)]) count = 0 p1, p2 = 0, k if k_sum / k >= threshold: count += 1 while p2 < len(arr): k_sum = k_sum - arr[p1] + arr[p2] if k_sum / k >= threshold: count += 1 p1 += 1 p2 += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NONE ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: l = 0 r = k - 1 total = sum(arr[0:k]) if total / k >= threshold: count = 1 else: count = 0 while r < len(arr) - 1: total -= arr[l] l += 1 r += 1 total += arr[r] if total / k >= threshold: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: l, r = 0, k - 1 temp = 0 for i in range(l, r + 1): temp += arr[i] res = 0 if temp < k * threshold else 1 while r < len(arr) - 1: temp += arr[r + 1] - arr[l] r += 1 l += 1 if temp >= k * threshold: res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR NUMBER NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: l, r = 0, k - 1 res = 0 s = sum(arr[:k]) t = threshold * k while r < len(arr): if s >= t: res += 1 if r + 1 < len(arr): s = s - arr[l] + arr[r + 1] l += 1 r += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: target = k * threshold s, ans = 0, 0 for i, num in enumerate(arr): if i < k: s += num continue if s >= target: ans += 1 s = s - arr[i - k] + arr[i] if s >= target: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: tmp = [sum(arr[:k])] for i in range(k, len(arr)): temp = tmp[-1] temp = temp - arr[i - k] + arr[i] tmp.append(temp) print(tmp) count = 0 for el in tmp: if el / k >= threshold: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: cur = 0 res = 0 for i in range(k): cur += arr[i] if cur / k >= threshold: res += 1 for i in range(k, len(arr)): cur = cur + arr[i] - arr[i - k] if cur / k >= threshold: res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: temp = sum(arr[:k]) count = 0 if temp / k < threshold else 1 for i in range(k, len(arr)): temp = temp - arr[i - k] + arr[i] if temp / k >= threshold: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: n = len(arr) a = [0] * (n + 1) for i in range(1, n + 1): a[i] = arr[i - 1] + a[i - 1] cnt = 0 for i in range(k, n + 1): res = a[i] - a[i - k] if res / k >= threshold: cnt += 1 return cnt
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: if len(arr) < k: return 0 bar = k * threshold total = 0 window = sum(arr[:k]) if window >= bar: total += 1 for i in range(k, len(arr)): window -= arr[i - k] window += arr[i] if window >= bar: total += 1 return total
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def __init__(self): self.counter = 0 self.threshold = 0 self.k = 0 def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: self.k = k self.threshold = threshold interval = sum(arr[:k]) self._count_if_slice_sum_passes_threshold(interval) left = 0 while left + k < len(arr): interval -= arr[left] left += 1 right = left + k - 1 interval += arr[right] self._count_if_slice_sum_passes_threshold(interval) return self.counter def _count_if_slice_sum_passes_threshold(self, slice): if slice / self.k >= self.threshold: self.counter += 1
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF IF BIN_OP VAR VAR VAR VAR NUMBER
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: if len(arr) < k: return 0 store = [0] * len(arr) i = 0 j = 0 ans = 0 while i < len(arr): if i - j + 1 == k: if j == 0: store[i] = store[i - 1] + arr[i] else: store[i] = store[i - 1] + arr[i] - arr[j - 1] if store[i] // k >= threshold: ans = ans + 1 j = j + 1 i = i + 1 else: if i == 0: store[i] = arr[i] else: store[i] = arr[i] + store[i - 1] i = i + 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: rep = 0 target = k * threshold cur = sum(arr[:k]) if cur >= target: rep += 1 l, r = 0, k while r < len(arr): cur -= arr[l] cur += arr[r] l += 1 r += 1 if cur >= target: rep += 1 return rep
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: s = 0 ret = 0 for i, num in enumerate(arr): if i - k < 0: s += num if s >= threshold * k and i == k - 1: ret += 1 print(i) continue s -= arr[i - k] s += num if s >= threshold * k: ret += 1 print(i) return ret
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR IF VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: start = 0 end = k count = 0 sumInit = sum(arr[start:end]) if sumInit / k >= threshold: count += 1 while end < len(arr): sumInit = sumInit - arr[start] + arr[end] if sumInit / k >= threshold: count += 1 start += 1 end += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: s = threshold * k s1 = sum(arr[:k]) n = len(arr) c = 0 if s1 >= s: c += 1 for i in range(k, n): s1 -= arr[i - k] s1 += arr[i] if s1 >= s: c += 1 return c
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: ans, sm = 0, 0 q = deque() for v in arr: sm += v q.append(v) if len(q) > k: sm -= q.popleft() if len(q) == k: ans += sm / k >= threshold return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: start = 0 count = 0 threshold = threshold * k avg = sum(arr[start:k]) i = k while i <= len(arr): if start != 0: avg = avg - arr[start - 1] + arr[i - 1] if avg >= threshold: count += 1 start += 1 i = start + k else: start += 1 i += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: size = len(arr) avg = sum(arr[:k]) / k count = 1 if avg >= threshold else 0 end = k while end < size: prevVal = arr[end - k] / k nextVal = arr[end] / k avg += nextVal - prevVal count += 1 if avg >= threshold else 0 end += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr, k, threshold) -> int: s = 0 sub = arr[0:k] subSum = sum(sub) avg = subSum / k if avg >= threshold: s += 1 i = k j = 0 while i < len(arr): subSum += arr[i] subSum -= arr[j] j += 1 i += 1 avg = subSum / k if avg >= threshold: s += 1 return s
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, a: List[int], k: int, threshold: int) -> int: prefixSum = [0] for i in a: prefixSum.append(i + prefixSum[-1]) return sum( prefixSum[i + k] - prefixSum[i] >= k * threshold for i in range(len(a) - k + 1) )
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: start = 0 s = 0 count = 0 for end in range(len(arr)): s = s + arr[end] if end - start + 1 == k: if s / k >= threshold: count += 1 s -= arr[start] start += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF BIN_OP BIN_OP VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: ans = 0 n = len(arr) start = 0 end = 0 presum = [0] for num in arr: presum.append(presum[-1] + num) for i in range(0, n - k + 1): if (presum[i + k] - presum[i]) / k >= threshold: ans += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: answer = 0 initial_sum = sum(arr[:k]) for i in range(len(arr) - k + 1): if i == 0: if initial_sum / k >= threshold: answer += 1 else: initial_sum = initial_sum - arr[i - 1] + arr[i + k - 1] if initial_sum / k >= threshold: answer += 1 return answer
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: if not arr or len(arr) < k: return 0 summ = [0] res = 0 for p, v in enumerate(arr): i = p + 1 summ.append(summ[p] + v) if i - k >= 0: if (summ[i] - summ[i - k]) / k >= threshold: res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR LIST NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: threshold *= k n = len(arr) l = 0 tmp_sum = 0 count = 0 for i in range(k): tmp_sum += arr[i] if tmp_sum >= threshold: count += 1 for l in range(n - k): tmp_sum -= arr[l] r = l + k tmp_sum += arr[r] if tmp_sum >= threshold: count += 1 return count
CLASS_DEF FUNC_DEF VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: answer = 0 avg = average(arr[0:k]) prev_el = arr[0] if avg >= threshold: answer += 1 for i in range(k, len(arr)): new_avg = (avg * k - prev_el + arr[i]) / k if new_avg >= threshold: answer += 1 avg = new_avg prev_el = arr[i - k + 1] return answer def average(l): return sum(l) / len(l)
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR VAR FUNC_DEF RETURN BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: i = 0 j = i + k s = sum(arr[i:j]) c = 0 while j <= len(arr): if s // k >= threshold: c += 1 s -= arr[i] if j < len(arr): s += arr[j] j += 1 i += 1 return c
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR VAR VAR IF VAR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: l = len(arr) ans = 0 x = 0 running_sum = sum(arr[0:k]) if running_sum / k >= threshold: ans += 1 while k + x < l: running_sum += arr[k + x] - arr[x] if running_sum / k >= threshold: ans += 1 x += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR IF BIN_OP VAR VAR VAR VAR NUMBER WHILE BIN_OP VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: total = sum(arr[:k]) result = 0 for i in range(k, len(arr) + 1): avg = total / k if avg >= threshold: result += 1 if i < len(arr): left = arr[i - k] right = arr[i] total -= left total += right return result
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: c = 0 a = 0 f = 0 for i in range(len(arr)): a = a + 1 c = c + arr[i] if a >= k: if c / k >= threshold: f = f + 1 c = c - arr[i - a + 1] a = a - 1 return f
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: ans = 0 total = 0 if len(arr) < k: return 0 else: for i in range(k): total += arr[i] if float(total / k) >= threshold: ans += 1 i = k while i < len(arr): total += arr[i] - arr[i - k] if float(total / k) >= threshold: ans += 1 i += 1 return ans
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF FUNC_CALL VAR BIN_OP VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: presum = [0] for num in arr: presum.append(presum[-1] + num) res = 0 i = 0 while i + k < len(presum): if presum[i + k] - presum[i] >= threshold * k: res += 1 i += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR LIST NUMBER FOR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, A, k, target): target *= k s = 0 for i in range(k): s += A[i] res = 1 if s >= target else 0 for i in range(len(A) - k): s += -A[i] + A[i + k] res += 1 if s >= target else 0 return res
CLASS_DEF FUNC_DEF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER NUMBER RETURN VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: i, j = 0, k - 1 total = sum(arr[i : j + 1]) target = threshold * k res = 0 if total >= target: res += 1 while j < len(arr) - 1: total -= arr[i] i += 1 j += 1 total += arr[j] if total >= target: res += 1 return res
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR IF VAR VAR VAR NUMBER RETURN VAR VAR
Given an array of integers arr and two integers k and threshold. Return the number of sub-arrays of size k and average greater than or equal to threshold.   Example 1: Input: arr = [2,2,2,2,5,5,5,8], k = 3, threshold = 4 Output: 3 Explanation: Sub-arrays [2,5,5],[5,5,5] and [5,5,8] have averages 4, 5 and 6 respectively. All other sub-arrays of size 3 have averages less than 4 (the threshold). Example 2: Input: arr = [1,1,1,1,1], k = 1, threshold = 0 Output: 5 Example 3: Input: arr = [11,13,17,23,29,31,7,5,2,3], k = 3, threshold = 5 Output: 6 Explanation: The first 6 sub-arrays of size 3 have averages greater than 5. Note that averages are not integers. Example 4: Input: arr = [7,7,7,7,7,7,7], k = 7, threshold = 7 Output: 1 Example 5: Input: arr = [4,4,4,4], k = 4, threshold = 1 Output: 1   Constraints: 1 <= arr.length <= 10^5 1 <= arr[i] <= 10^4 1 <= k <= arr.length 0 <= threshold <= 10^4
class Solution: def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int: dp = [0] * len(arr) dp[0] = arr[0] for i in range(1, len(arr)): dp[i] = dp[i - 1] + arr[i] cn = 0 for i in range(k - 1, len(arr)): if i == k - 1: if dp[i] // k >= threshold: cn += 1 elif (dp[i] - dp[i - k]) // k >= threshold: cn += 1 return cn
CLASS_DEF FUNC_DEF VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR VAR VAR NUMBER IF BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER RETURN VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
def CountSubarrays(arr, n): freq = {i: (0) for i in arr} d = len(list(freq.keys())) count, res, i = 0, 0, 0 for j in range(n): freq[arr[j]] += 1 if freq[arr[j]] == 1: count += 1 if count == d: res += n - j while i <= j: freq[arr[i]] -= 1 if freq[arr[i]] == 0: count -= 1 i += 1 if count == d: res += n - j else: break return res class Solution: def countDistinctSubarray(self, arr, n): return CountSubarrays(arr, n)
FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR RETURN VAR CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): set1 = set(arr) distinct_count = len(set1) start, end = 0, 0 count = 0 res = 0 d = {} for i in range(0, n): if arr[i] in set1: d[arr[i]] = 1 + d.get(arr[i], 0) if len(d) == distinct_count: while len(d) == distinct_count: res += n - i d[arr[start]] -= 1 if d[arr[start]] == 0: d.pop(arr[start]) start += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): k = set(arr) req = len(k) mp = {} ans = 0 i = 0 j = 0 while j < n: if arr[j] not in mp: mp[arr[j]] = 1 else: mp[arr[j]] += 1 while i <= j and len(mp) == req: ans += n - j mp[arr[i]] -= 1 if mp[arr[i]] == 0: del mp[arr[i]] i += 1 j += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, l, n): d = {} for x in l: d[x] = 0 k = len(d) i = 0 j = 0 ans = 0 count = 0 while i < len(l): if d[l[i]] == 0: ans += 1 d[l[i]] += 1 while ans == k and j <= i: count += len(l) - i d[l[j]] -= 1 if d[l[j]] == 0: ans -= 1 j += 1 i += 1 return count t = int(input()) for _ in range(0, t): n = int(input()) a = list(map(int, input().split())) ob = Solution() print(ob.countDistinctSubarray(a, n))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def distinct(self, arr, start, end): d = dict() c = 0 for i in range(start, end): if arr[i] not in d: c += 1 d[arr[i]] = 1 return c def countDistinctSubarray(self, arr, n): res = 0 c = self.distinct(arr, 0, n) d = dict() start = 0 i = 0 while start < n: if arr[i] not in d: d[arr[i]] = 1 else: d[arr[i]] += 1 if len(d) == c: res += n - i while len(d) == c: if d[arr[start]] == 1: del d[arr[start]] else: d[arr[start]] -= 1 if len(d) == c: res += n - i start += 1 if len(d) < c and i == n - 1: break if i < n - 1: i += 1 return res
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): def subs(arr, n, maxdistinct): hm = {} dist_cnt = 0 start, end = 0, 0 subs = 0 while end < n: hm[arr[end]] = 1 + hm.get(arr[end], 0) if hm[arr[end]] == 1: dist_cnt += 1 while dist_cnt > maxdistinct: hm[arr[start]] -= 1 if hm[arr[start]] == 0: dist_cnt -= 1 start += 1 subs += end - start + 1 end += 1 return subs s = set(arr) k = len(s) atmostk = subs(arr, n, k) atmostk_1 = subs(arr, n, k - 1) return atmostk - atmostk_1
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN BIN_OP VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): total_ele = len(set(arr)) count = {} right = 0 window = 0 ans = 0 for i in range(n): while right < n and window < total_ele: if arr[right] in count: count[arr[right]] += 1 else: count[arr[right]] = 1 if count[arr[right]] == 1: window += 1 right += 1 if window == total_ele: ans += n - right + 1 count[arr[i]] -= 1 if count[arr[i]] == 0: window -= 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def atMostK(self, arr, n, k): count = 0 left = 0 right = 0 map = {} while right < n: if arr[right] not in map: map[arr[right]] = 0 map[arr[right]] += 1 while len(map) > k: if arr[left] not in map: map[arr[left]] = 0 map[arr[left]] -= 1 if map[arr[left]] == 0: del map[arr[left]] left += 1 count += right - left + 1 right += 1 return count def countDistinctSubarray(self, arr, n): d = dict() k = 0 for i in arr: if d.get(i) == None: k = k + 1 d[i] = True return self.atMostK(arr, n, k) - self.atMostK(arr, n, k - 1)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR NUMBER RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): t = len(set(arr)) ans = 0 left = 0 d = {} for i in range(n): if arr[i] not in d: d[arr[i]] = 1 else: d[arr[i]] += 1 while len(d) == t: ans += n - i d[arr[left]] -= 1 if d[arr[left]] == 0: del d[arr[left]] left += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): count = {} for i in range(len(arr)): count[arr[i]] = 1 + count.get(arr[i], 0) distinct = len(count) res = {} l = 0 ans = 0 for r in range(n): res[arr[r]] = 1 + res.get(arr[r], 0) while len(res) == distinct: ans += n - r res[arr[l]] -= 1 if res[arr[l]] == 0: del res[arr[l]] l += 1 return ans t = int(input()) for _ in range(0, t): n = int(input()) a = list(map(int, input().split())) ob = Solution() print(ob.countDistinctSubarray(a, n))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, a, n): d = {} k = 0 for i in range(n): if a[i] not in d.keys(): d[a[i]] = 1 k += 1 ans = 0 i = 0 d = {} while i in range(n): if a[i] not in d.keys(): d[a[i]] = 1 else: d[a[i]] += 1 if len(d) == k: j = 0 cnt = 1 while d[a[j]] != 1: cnt += 1 d[a[j]] -= 1 j += 1 ans += cnt break i += 1 i = i + 1 while i < n: d[a[i]] += 1 while d[a[j]] != 1: cnt += 1 d[a[j]] -= 1 j += 1 ans += cnt i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE VAR FUNC_CALL VAR VAR IF VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def solve(self, arr, k, n): d = {} count, l, r = 0, 0, 0 while r < n: if arr[r] not in d: d[arr[r]] = 0 d[arr[r]] += 1 while len(d) > k: if arr[l] not in d: d[arr[l]] = 0 d[arr[l]] -= 1 if d[arr[l]] <= 0: del d[arr[l]] l += 1 count += r - l + 1 r += 1 return count def countDistinctSubarray(self, arr, n): d = {} for i in arr: d[i] = d.get(i, 0) + 1 k = len(d) return self.solve(arr, k, n) - self.solve(arr, k - 1, n)
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN BIN_OP FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): ans, cnt, start, k, di = 0, 0, 0, len(set(arr)), dict() for i in range(n): while start < n and cnt < k: di[arr[start]] = di.get(arr[start], 0) + 1 if di[arr[start]] == 1: cnt += 1 start += 1 if cnt == k: ans += n - start + 1 di[arr[i]] -= 1 cnt -= 1 if di[arr[i]] == 0 else 0 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR NUMBER NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER NUMBER NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): uniq, d = len(set(arr)), {} start = c = tot = 0 for i in range(n): if arr[i] not in d: d[arr[i]], c = 1, c + 1 else: d[arr[i]] += 1 if c == uniq: tot += n - i break while start < n: c -= d[arr[start]] == 1 d[arr[start]] -= 1 start += 1 while c < uniq: i += 1 if i >= n: break if arr[i] not in d or d[arr[i]] == 0: d[arr[i]], c = 1, c + 1 else: d[arr[i]] += 1 tot += n - i if c == uniq else 0 return tot t = int(input()) for _ in range(0, t): n = int(input()) a = list(map(int, input().split())) ob = Solution() print(ob.countDistinctSubarray(a, n))
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR DICT ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): d = {} for i in range(n): d[arr[i]] = 1 p = len(d) d1 = {} h = 0 res = 0 l = 0 for i in range(n): d1[arr[i]] = d1.get(arr[i], 0) + 1 if arr[i] in d and d[arr[i]] == d1[arr[i]]: h += 1 while p == h: d1[arr[l]] -= 1 if arr[l] in d and d[arr[i]] > d1[arr[l]]: h -= 1 res += n - i l += 1 return res t = int(input()) for _ in range(0, t): n = int(input()) a = list(map(int, input().split())) ob = Solution() print(ob.countDistinctSubarray(a, n))
CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): distinctEle = len(set(arr)) i = 0 j = 0 ans = 0 dic = {} currDistinctEle = 0 cnt = 0 while j < n: while i < n and currDistinctEle == distinctEle: ans += n - j + 1 dic[arr[i]] -= 1 if dic[arr[i]] == 0: del dic[arr[i]] currDistinctEle -= 1 i += 1 if arr[j] not in dic: dic[arr[j]] = 1 currDistinctEle += 1 else: dic[arr[j]] += 1 j += 1 while i < n and currDistinctEle == distinctEle: ans += n - j + 1 dic[arr[i]] -= 1 if dic[arr[i]] == 0: del dic[arr[i]] currDistinctEle -= 1 i += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR WHILE VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): s = set() for x in arr: s.add(x) i = -1 j = -1 count = 0 d = {} while True: f1 = False f2 = False while i < len(arr) - 1: f1 = True i += 1 try: d[arr[i]] += 1 except: d[arr[i]] = 1 if len(d) == len(s): count += len(arr) - i break while j < i: j += 1 f2 = True if d[arr[j]] == 1: d.pop(arr[j]) else: d[arr[j]] -= 1 if len(d) == len(s): count += len(arr) - i else: break if f1 == False and f2 == False: return count
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): k = 0 hashmap = {} for i in range(n): curr = arr[i] if curr in hashmap: hashmap[curr] += 1 else: k += 1 hashmap[curr] = 1 def fun(k): left = dist = ans = 0 hashmap = {} for right in range(len(arr)): curr = arr[right] if curr in hashmap: hashmap[curr] += 1 else: hashmap[curr] = 1 dist += 1 while left <= right and dist > k: disc = arr[left] hashmap[disc] -= 1 left += 1 if hashmap[disc] == 0: del hashmap[disc] dist -= 1 ans += right - left + 1 return ans ans = fun(k) - fun(k - 1) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR NUMBER WHILE VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): tot = len(set(arr)) fp, sp, mp = 0, 0, {} ans = 0 while sp < n: if arr[sp] in mp: mp[arr[sp]] += 1 else: mp[arr[sp]] = 1 while len(mp) == tot and fp <= sp: ans += n - sp mp[arr[fp]] -= 1 if mp[arr[fp]] == 0: mp.pop(arr[fp]) fp += 1 sp += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER DICT ASSIGN VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): uniq = 0 dt, ds = {}, {} for i in range(n): if dt.get(arr[i], 0) == 0: uniq += 1 dt[arr[i]] = 1 start = end = ans = 0 cnt = 0 for i in range(n): if ds.get(arr[i], 0) == 0: cnt += 1 ds[arr[i]] = ds.get(arr[i], 0) + 1 if cnt == uniq: end = i break while end < n: if start < n and ds[arr[start]] >= dt[arr[start]] and cnt == uniq: if ds[arr[start]] == dt[arr[start]]: cnt -= 1 ans += n - end ds[arr[start]] -= 1 start += 1 else: end += 1 if end < n: ds[arr[end]] = ds.get(arr[end], 0) + 1 if ds[arr[end]] == dt[arr[end]]: cnt += 1 return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR DICT DICT FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR VAR VAR VAR VAR VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): vis = dict() for i in range(n): vis[arr[i]] = 1 k = len(vis) vid = dict() ans = 0 right = 0 window = 0 for left in range(n): while right < n and window < k: if arr[right] in vid.keys(): vid[arr[right]] += 1 else: vid[arr[right]] = 1 if vid[arr[right]] == 1: window += 1 right += 1 if window == k: ans += n - right + 1 vid[arr[left]] -= 1 if vid[arr[left]] == 0: window -= 1 return ans t = int(input()) for _ in range(0, t): n = int(input()) a = list(map(int, input().split())) ob = Solution() print(ob.countDistinctSubarray(a, n))
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): k = len(set(arr)) has = {} i = -1 j = -1 ans = 0 while True: f1 = False f2 = False while i < n - 1: f1 = True i += 1 has[arr[i]] = has.get(arr[i], 0) + 1 if len(has) == k: ans += n - i break while j < i: f2 = True j += 1 if has.get(arr[j]) == 1: has.pop(arr[j]) else: has[arr[j]] -= 1 if len(has) == k: ans += n - i else: break if f1 == False and f2 == False: break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR NUMBER VAR NUMBER IF FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, str, n): x = len(set(str)) st = 0 d = {} ans = 99999999 l = 0 c = 0 r = False z = 0 for i in range(n): if str[i] in d: d[str[i]] += 1 else: d[str[i]] = 1 l += 1 if not r and l == x: c += (z + 1) * (n - i) r = True while st < i and d.get(str[st], 0) > 1: d[str[st]] -= 1 st += 1 if l == x: c += n - i else: z += 1 if l == x: ans = min(ans, i - st + 1) return c
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def check_distinct(self, subarray_freq, unique_elem_hm): for key in unique_elem_hm: if subarray_freq[key] == 0: return False return True def countDistinctSubarray(self, nums, n): unique_elem_hm = {} for i in range(n): unique_elem_hm[nums[i]] = unique_elem_hm.get(nums[i], 0) + 1 ans = 0 unique_elem_count = 0 subarray_freq = {} for key in unique_elem_hm: subarray_freq[key] = 0 unique_elem_count += 1 for i in range(unique_elem_count): subarray_freq[nums[i]] += 1 l = 0 r = unique_elem_count - 1 while r < n and not self.check_distinct(subarray_freq, unique_elem_hm): r += 1 subarray_freq[nums[r]] += 1 ans += n - r while r < n: removed_elem = nums[l] l += 1 subarray_freq[removed_elem] -= 1 while r < n and subarray_freq[removed_elem] == 0: r += 1 if r < n: subarray_freq[nums[r]] += 1 ans += n - r return ans
CLASS_DEF FUNC_DEF FOR VAR VAR IF VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR VAR WHILE VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): e = 0 d = {} for i in range(n): if arr[i] not in d: d[arr[i]] = 1 e += 1 d = {} i = 0 j = n - 1 while i < n: if arr[i] in d: d[arr[i]] += 1 else: d[arr[i]] = 1 if len(d) == e: r = i break i += 1 i = 0 c = 0 while r < n: if len(d) == e: c += n - r d[arr[i]] -= 1 if d[arr[i]] == 0: del d[arr[i]] i += 1 else: r += 1 if r < n and arr[r] not in d: d[arr[r]] = 1 elif r < n: d[arr[r]] += 1 return c t = int(input()) for _ in range(0, t): n = int(input()) a = list(map(int, input().split())) ob = Solution() print(ob.countDistinctSubarray(a, n))
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER 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 EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): a = [0] * 100000 k = 0 for i in range(n): if a[arr[i]] == 0: a[arr[i]] += 1 k += 1 else: a[arr[i]] += 1 a = [0] * 1000000 value = 0 z = 0 res = 0 value = 0 for i in range(n): if a[arr[i]] == 0: a[arr[i]] += 1 z += 1 else: a[arr[i]] += 1 if z == k: while value <= i: if a[arr[value]] >= 1: res += n - i a[arr[value]] -= 1 if a[arr[value]] == 0: z -= 1 break value += 1 else: break value += 1 if i == n - 1: break return res
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR NUMBER VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR WHILE VAR VAR IF VAR VAR VAR NUMBER VAR BIN_OP VAR VAR VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER VAR NUMBER IF VAR BIN_OP VAR NUMBER RETURN VAR
Given an array arr[] of n integers. Count the total number of sub-array having total distinct elements same as that of total distinct elements of the original array. Example 1: Input: N=5 arr[] = {2, 1, 3, 2, 3} Output: 5 Explanation: Total distinct elements in array is 3 Total sub-arrays that satisfy the condition are: Subarray from index 0 to 2 Subarray from index 0 to 3 Subarray from index 0 to 4 Subarray from index 1 to 3 Subarray from index 1 to 4 Example 2: Input: N=5 arr[] = {2, 4, 4, 2, 4} Output: 9 Your Task: Since, this is a function problem. You don't need to take any input, as it is already accomplished by the driver code. You just need to complete the function countDistinctSubarray() that takes array arr and integer n as parameters and returns the desired result. Expected Time Complexity: O(N). Expected Auxiliary Space: O(N). Constraints: 1 ≤ N ≤ 10^{4}
class Solution: def countDistinctSubarray(self, arr, n): hash_arr = {} k = 0 for num in arr: if num not in hash_arr: k += 1 hash_arr[num] = 1 else: hash_arr[num] += 1 hash_arr = {} match_cnt = 0 i = -1 j = -1 ans = 0 while True: f1 = False f2 = False while j < n - 1 and match_cnt < k: j += 1 if arr[j] not in hash_arr: hash_arr[arr[j]] = 1 match_cnt += 1 else: hash_arr[arr[j]] += 1 f1 = True while i < j and match_cnt == k: ans += 1 + (n - 1 - j) i += 1 hash_arr[arr[i]] -= 1 if hash_arr[arr[i]] == 0: hash_arr.pop(arr[i]) match_cnt -= 1 f2 = True if f1 == False and f2 == False: break return ans
CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR BIN_OP NUMBER BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR