description
stringlengths
171
4k
code
stringlengths
94
3.98k
normalized_code
stringlengths
57
4.99k
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() seg = n // k if s.count("0") % seg != 0 or s.count("1") % seg != 0: print("IMPOSSIBLE") else: for i in range(seg): if i % 2 == 0: for j in range(s.count("0") // seg): print("0", end="") for k in range(s.count("1") // seg): print("1", end="") else: for j in range(s.count("1") // seg): print("1", end="") for k in range(s.count("0") // seg): print("0", end="") print()
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR STRING VAR NUMBER BIN_OP FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
t = int(input()) while t > 0: t -= 1 n, k = map(int, input().split()) a = input() s = n // k z = 0 o = 0 for i in a: if i == "0": z += 1 else: o += 1 if o % s != 0 or z % s != 0: print("IMPOSSIBLE") else: z = z // s o = o // s b = "0" * z + "1" * o k = 0 l = 0 c = "" while s > 0: s -= 1 if l % 2 == 0: c += b else: c += b[::-1] l += 1 print(c)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR NUMBER VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
t = int(input()) while t: t -= 1 n, k = map(int, input().strip().split(" ")) s = input() g = n // k fq = [0] * 2 for i in range(n): fq[ord(s[i]) - ord("0")] += 1 if fq[0] % g != 0 or fq[1] % g != 0: print("IMPOSSIBLE") else: it = n // k f0 = fq[0] // g f1 = fq[1] // g s1 = "" for i in range(f0): s1 += "0" for j in range(f1): s1 += "1" s2 = s1[::-1] ans = "" for i in range(it): if i % 2 == 0: ans += s1 else: ans += s2 print(ans)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING NUMBER IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
t = int(input()) while t: n, k = map(int, input().split()) s = input() ans = "" ans1 = "" finans = "" set_len = int(len(s) / k) count_1 = 0 count_0 = 0 for i in s: if i == "1": count_1 += 1 else: count_0 += 1 if count_1 % set_len == 0: if count_0 % set_len == 0: set_1 = int(count_1 / set_len) set_0 = int(count_0 / set_len) for i in range(set_0): ans += "0" for i in range(set_1): ans += "1" ans1 = ans[::-1] for i in range(set_len): if i % 2 == 0: finans += ans else: finans += ans1 print(finans) else: print("IMPOSSIBLE") else: print("IMPOSSIBLE") t -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR VAR STRING FOR VAR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
try: def solve(): n, k = map(int, input().split()) s = list(map(int, input())) segments = int(n / k) zerocount = 0 onecount = 0 for i in s: if i == 0: zerocount += 1 if i == 1: onecount += 1 if zerocount % segments == 0 and onecount % segments == 0: zerocount = int(zerocount / segments) onecount = int(onecount / segments) l = 0 for l in range(segments): if l % 2 == 0: print("0" * zerocount, end="") print("1" * onecount, end="") else: print("1" * onecount, end="") print("0" * zerocount, end="") print(end="\n") else: print("IMPOSSIBLE") i = int(input()) for j in range(i): solve() except: pass
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING EXPR FUNC_CALL VAR BIN_OP STRING VAR STRING EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() o = s.count("1") z = n - o k = n // k if o % k or z % k: print("IMPOSSIBLE") else: word = "1" * (o // k) + "0" * (z // k) rev = word[::-1] ans = [] for i in range(k): if i & 1: ans.append(word) else: ans.append(rev) print("".join(ans))
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
t = int(input()) for i in range(t): n, k = map(int, input().split()) s = input() l = [] l1 = [] a = n // k if s.count("0") % a != 0 or s.count("1") % a != 0: print("IMPOSSIBLE") else: n0 = s.count("0") // a n1 = s.count("1") // a while n0 != 0: l.append("0") n0 = n0 - 1 while n1 != 0: l.append("1") n1 = n1 - 1 s1 = "".join(l) for k in range(1, a + 1): if k % 2 != 0: l1.append(s1) else: l1.append(s1[::-1]) print("".join(l1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR BIN_OP VAR VAR IF BIN_OP FUNC_CALL VAR STRING VAR NUMBER BIN_OP FUNC_CALL VAR STRING VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR BIN_OP FUNC_CALL VAR STRING VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL STRING VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
t = int(input()) for i in range(t): x = input() x = x.split() n = int(x[0]) k = int(x[1]) s = input() factor = n // k n0 = s.count("0") n1 = s.count("1") if n0 % factor != 0 or n1 % factor != 0: print("IMPOSSIBLE") else: r = "0" * (n0 // factor) + "1" * (n1 // factor) rr = r[-1::-1] p = "" for i in range(factor): if i % 2 == 0: p += r else: p += rr print(p)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR VAR ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
def k_fold_string(string: list, n, k): counts = [string.count("0"), string.count("1")] if counts[0] % (n // k) != 0 and counts[1] % (n // k) != 0: return "IMPOSSIBLE" else: pattern = [ "0" * (counts[0] // (n // k)) + "1" * (counts[1] // (n // k)), "1" * (counts[1] // (n // k)) + "0" * (counts[0] // (n // k)), ] count = 0 ans = "" while count != n // k: if count % 2 == 0: ans += pattern[0] else: ans += pattern[1] count += 1 return ans for _ in range(int(input())): n, k = list(map(int, input().split())) string = input() print(k_fold_string(string, n, k))
FUNC_DEF VAR ASSIGN VAR LIST FUNC_CALL VAR STRING FUNC_CALL VAR STRING IF BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR NUMBER RETURN STRING ASSIGN VAR LIST BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP STRING BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR STRING WHILE VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR 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 VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() a = [0] * 2 for i in s: if i == "0": a[0] += 1 else: a[1] += 1 r = n // k if n < k or a[0] % r != 0 or a[1] % r != 0: print("IMPOSSIBLE") else: k0, k1 = a[0] // r, a[1] // r kstring = ["0"] * k0 + ["1"] * k1 s1 = [] for i in range(r): s1 += kstring kstring.reverse() print("".join(s1))
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP LIST STRING VAR BIN_OP LIST STRING VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
for i in range(int(input())): a, b = map(int, input().split()) c = input() x = c.count("1") y = c.count("0") z = a // b d = x // z e = y // z f = "" h = "" j = 0 if y % z != 0 or x % z != 0: print("IMPOSSIBLE") else: f = "0" * e + "1" * d g = f[::-1] for i in range(z): if j == 0: h = h + f j = 1 continue if j == 1: h = h + g j = 0 print(h)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() a = [int(i) for i in s] zero = a.count(0) one = a.count(1) m = n // k if zero % m != 0 or one % m != 0: print("IMPOSSIBLE") else: ans = "" e = zero // m p = one // m while e: ans += "0" e -= 1 while p: ans += "1" p -= 1 m -= 1 temp = ans[::-1] while m: ans += temp temp = temp[::-1] m -= 1 print(ans)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR STRING VAR NUMBER WHILE VAR VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
def gcd(x, y): if x > y: small = y else: small = x for i in range(1, small + 1): if x % i == 0 and y % i == 0: gcd = i return gcd def pal(s, n): k = 0 for i in range(int(n / 2)): if s[i] == s[n - i]: k += 1 if k == int(n / 2): return True else: return False t = int(input()) while t: n, k = map(int, input().strip().split(" ")) s = input() o = 0 z = 0 j = list() for i in range(1, 50): j.append(2**i) for i in range(n): if s[i] == "1": o += 1 else: z += 1 if o < 1 or z < 1: print(s) else: g = gcd(o, z) x = int(o / g) y = int(z / g) if k % (x + y) != 0: print("IMPOSSIBLE") else: a = "" d = int(k / (x + y)) for i in range(y * d): a = a + "0" for i in range(x * d): a = a + "1" p = k j = 1 b = a while p < n: if j % 2 != 0: a += b[::-1] else: a += b p += k j += 1 print(a) t -= 1
FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR STRING ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
for _ in range(int(input())): n, k = map(int, input().split()) s = input() l = [0, 0] for i in s: if i == "0": l[0] += 1 else: l[1] += 1 a = n // k o = l[0] // a e = l[1] // a if l[0] % a == 0 and l[1] % a == 0: temp = "0" * o + "1" * e ans = "" for i in range(a): if i % 2 == 0: ans = ans + temp else: ans = ans + temp[::-1] print(ans) else: print("IMPOSSIBLE")
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR STRING
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
for _ in range(int(input())): n, k = [int(x) for x in input().split()] s = input() if s.count("0") % (n / k) != 0 or s.count("1") % (n / k) != 0: print("IMPOSSIBLE") else: l = "" f = 0 while True: l += "0" * int(s.count("0") // (n / k)) + "1" * int(s.count("1") // (n / k)) if len(l) >= n: break l += "1" * int(s.count("1") // (n / k)) + "0" * int(s.count("0") // (n / k)) if len(l) >= n: break print(l)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF BIN_OP FUNC_CALL VAR STRING BIN_OP VAR VAR NUMBER BIN_OP FUNC_CALL VAR STRING BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE NUMBER VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR VAR BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR VAR BIN_OP STRING FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING BIN_OP VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
def find(n, k, s): repeat = n // k zero = 0 one = 0 for i in s: if i == "0": zero += 1 else: one += 1 li = [] for i in range(0, k + 1): z = i o = k - i if z * repeat == zero and o * repeat == one: temp = "" str1 = "0" * z + "1" * o str2 = "1" * o + "0" * z for m in range(repeat): if m % 2 == 0: temp += str1 else: temp += str2 li.append(temp) temp = "" for m in range(repeat): if m % 2 == 0: temp += str2 else: temp += str1 li.append(temp) if len(li) == 0: return "IMPOSSIBLE" else: li.sort() return li[0] t = int(input()) for i in range(t): n, k = map(int, input().split()) s = input() print(find(n, k, s))
FUNC_DEF ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR STRING ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR ASSIGN VAR BIN_OP BIN_OP STRING VAR BIN_OP STRING VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN STRING EXPR FUNC_CALL VAR RETURN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
t = int(input()) for _ in range(t): n, k = map(int, input().split()) s = input() count_1 = s.count("1") count_0 = s.count("0") num_of_seg = n // k if count_1 % num_of_seg == 0 and count_0 % num_of_seg == 0: for x in range(num_of_seg): if x % 2 == 0: for y in range(count_0 // num_of_seg): print("0", end="") for y in range(count_1 // num_of_seg): print("1", end="") else: for y in range(count_1 // num_of_seg): print("1", end="") for y in range(count_0 // num_of_seg): print("0", end="") print() else: print("IMPOSSIBLE")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR STRING STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
for i in range(int(input())): n, k = map(int, input().split()) string = input() list1 = [0, 0] for i in range(n): if string[i] == "0": list1[0] += 1 else: list1[1] += 1 pass1 = n // k if list1[0] % pass1 != 0 and list1[1] % pass1 != 0: print("IMPOSSIBLE") elif len(set(list(string))) == 1: print(string) else: string1 = "" string1 += "0" * (list1[0] * k // sum(list1)) string1 += "1" * (list1[1] * k // sum(list1)) output = "" revs = string1[::-1] counter = 0 while len(output) != n: if counter % 2 == 0: output += string1 else: output += revs counter += 1 print(output)
FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING VAR NUMBER NUMBER VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR STRING VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR BIN_OP STRING BIN_OP BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
def kfolded(s, k): n = len(s) if n == 0: return "IMPOSSIBLE" c1 = 0 c2 = 0 seg = n // k for c in s: if c == "0": c1 += 1 else: c2 += 1 if c1 == 0 or c2 == 0: return s if c1 % seg == 0 and c2 % seg == 0 and c1 // seg != 0 and c2 // seg != 0: num1 = c1 // seg num2 = c2 // seg else: return "IMPOSSIBLE" seg1 = ["0"] * num1 seg2 = ["1"] * num2 p1 = "".join(seg1 + seg2) p2 = "".join(seg2 + seg1) if seg == 1: return p1 ans = [] for i in range(seg // 2): ans.append(p1) ans.append(p2) if seg % 2 != 0: ans.append(p1) return ans t = int(input()) for i in range(t): nk = input().strip().split() n = int(nk[0]) k = int(nk[1]) s = input() result = kfolded(s, k) print("".join(result))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER RETURN STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER RETURN VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR RETURN STRING ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR BIN_OP LIST STRING VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR ASSIGN VAR FUNC_CALL STRING BIN_OP VAR VAR IF VAR NUMBER RETURN VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING VAR
Read problems statements in [Hindi], [Mandarin Chinese], [Russian], [Vietnamese], and [Bengali] as well You are given a binary string $S$ with length $N$ and an integer $K$, which is a divisor of $N$. A string is said to be $K$*-foldable* if it can be changed to a string with length $K$ by repeating the following process without any *collisions* (defined below): Select the prefix of the current string $S$ with length $2K$. (Note that as long as the length of $S$ is greater than $K$, this prefix always exists.) For each $i$ ($1 ≀ i ≀ K$), check if the $2K-i+1$-th character of $S$ is equal to the $i$-th character of $S$ ― if they are not equal, there is a collision and the process is invalid. Erase the prefix of $S$ with length $K$. Your goal is to reorder the characters of $S$ (possibly leaving this string unchanged) in such a way that the resulting string $S$ is a $K$-foldable string. Find a way to do that or determine that it is impossible. If there are multiple solutions, find the lexicographically smallest rearranged string which is $K$-foldable. ------ Input ------ The first line of the input contains a single integer $T$ denoting the number of test cases. The description of $T$ test cases follows. The first line of each test case contains two space-separated integers $N$ and $K$. The second line contains a single string $S$ with length $N$. ------ Output ------ For each test case, print a single line containing the smallest rearranged string or the string "IMPOSSIBLE" if it is impossible to rearrange $S$. ------ Constraints ------ $1 ≀ T ≀ 100$ $1 ≀ K ≀ N ≀ 10^{3}$ $N$ is divisible by $K$ $S$ contains only characters '0' and '1' ----- Sample Input 1 ------ 2 8 2 00011101 6 2 100111 ----- Sample Output 1 ------ 01100110 IMPOSSIBLE ----- explanation 1 ------ Example case 1: If the given string "00011101" is rearranged to "01100110", it becomes $2$-foldable: - Initially, $S$ is "01100110". - After the first folding, it becomes "100110". - After the second folding, it becomes "0110". - After the third folding, it becomes "10". This string has length $2$, so we are done. Example case 2: It is impossible to rearrange $S$ into a $2$-foldable string.
def kfold(n, k, s): zero = 0 one = 0 segment_count = n // k for i in s: if i == "0": zero += 1 else: one += 1 if zero % segment_count != 0 or one % segment_count != 0: return "IMPOSSIBLE" zero = zero // segment_count one = one // segment_count ans = "" x = 1 while x <= segment_count: if x % 2 == 1: for _ in range(0, zero): ans = ans + "0" for _ in range(0, one): ans = ans + "1" else: for _ in range(0, one): ans = ans + "1" for _ in range(0, zero): ans = ans + "0" x += 1 return ans t = int(input()) while t: n, k = list(map(int, input().split())) s = input() ans = kfold(n, k, s) print(ans) t -= 1
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR FOR VAR VAR IF VAR STRING VAR NUMBER VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER RETURN STRING ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR STRING ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR BIN_OP VAR STRING VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER
Chef likes toys. His favourite toy is an array of length N. This array contains only integers. He plays with this array every day. His favourite game with this array is Segment Multiplication. In this game, the second player tells the left and right side of a segment and some modulo. The first player should find the multiplication of all the integers in this segment of the array modulo the given modulus. Chef is playing this game. Of course, he is the first player and wants to win all the games. To win any game he should write the correct answer for each segment. Although Chef is very clever, he has no time to play games. Hence he asks you to help him. Write the program that solves this problem. -----Input----- The first line of the input contains an integer N denoting the number of elements in the given array. Next line contains N integers Ai separated with spaces. The third line contains the number of games T. Each of the next T lines contain 3 integers Li, Ri and Mi, the left side of the segment, the right side of segment and the modulo. -----Output----- For each game, output a single line containing the answer for the respective segment. -----Constrdaints----- - 1 ≀ N ≀ 100,000 - 1 ≀ Ai ≀ 100 - 1 ≀ T ≀ 100,000 - 1 ≀ Li ≀ Ri ≀ N - 1 ≀ Mi ≀ 109 -----Example----- Input: 5 1 2 3 4 5 4 1 2 3 2 3 4 1 1 1 1 5 1000000000 Output: 2 2 0 120
def main(): N = int(input()) A = list(map(int, input().strip().split(" "))) primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, ] counting = [([0] * len(primes)) for i in range(len(A))] def div(n): CC = [0] * len(primes) for i in range(len(primes)): while n % primes[i] == 0: CC[i] += 1 n = n // primes[i] if n == 1: return CC return CC for i in range(len(A)): C = div(A[i]) for j in range(len(primes)): if i > 0: counting[i][j] = counting[i - 1][j] + C[j] else: counting[i][j] = C[j] for _ in range(int(input())): L, R, M = list(map(int, input().strip().split(" "))) L -= 1 R -= 1 if L > 0: CC = [(counting[R][j] - counting[L - 1][j]) for j in range(len(primes))] else: CC = [counting[R][j] for j in range(len(primes))] temp = 1 for j in range(len(CC)): temp *= pow(primes[j], CC[j], M) temp %= M print(temp) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING VAR NUMBER VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Chef likes toys. His favourite toy is an array of length N. This array contains only integers. He plays with this array every day. His favourite game with this array is Segment Multiplication. In this game, the second player tells the left and right side of a segment and some modulo. The first player should find the multiplication of all the integers in this segment of the array modulo the given modulus. Chef is playing this game. Of course, he is the first player and wants to win all the games. To win any game he should write the correct answer for each segment. Although Chef is very clever, he has no time to play games. Hence he asks you to help him. Write the program that solves this problem. -----Input----- The first line of the input contains an integer N denoting the number of elements in the given array. Next line contains N integers Ai separated with spaces. The third line contains the number of games T. Each of the next T lines contain 3 integers Li, Ri and Mi, the left side of the segment, the right side of segment and the modulo. -----Output----- For each game, output a single line containing the answer for the respective segment. -----Constrdaints----- - 1 ≀ N ≀ 100,000 - 1 ≀ Ai ≀ 100 - 1 ≀ T ≀ 100,000 - 1 ≀ Li ≀ Ri ≀ N - 1 ≀ Mi ≀ 109 -----Example----- Input: 5 1 2 3 4 5 4 1 2 3 2 3 4 1 1 1 1 5 1000000000 Output: 2 2 0 120
def main(): N = int(input()) a = [int(x) for x in input().split()] p = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, ] c = [[(0) for x in range(25)] for y in range(N)] def fct(n): fact = [0] * 25 for i in range(25): while n % p[i] == 0: fact[i] += 1 n = n // p[i] if n == 1: return fact return fact for i in range(N): hc = fct(a[i]) for j in range(25): if i > 0: c[i][j] = c[i - 1][j] + hc[j] else: c[i][j] = hc[j] for _ in range(int(input())): L, R, M = map(int, input().split()) L = L - 1 R = R - 1 if L > 0: t = [(c[R][i] - c[L - 1][i]) for i in range(25)] else: t = [c[R][i] for i in range(25)] ans = 1 for i in range(25): ans *= pow(p[i], t[i], M) ans %= M print(ans) main()
FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER WHILE BIN_OP VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR IF VAR NUMBER RETURN VAR RETURN VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR
Chef likes toys. His favourite toy is an array of length N. This array contains only integers. He plays with this array every day. His favourite game with this array is Segment Multiplication. In this game, the second player tells the left and right side of a segment and some modulo. The first player should find the multiplication of all the integers in this segment of the array modulo the given modulus. Chef is playing this game. Of course, he is the first player and wants to win all the games. To win any game he should write the correct answer for each segment. Although Chef is very clever, he has no time to play games. Hence he asks you to help him. Write the program that solves this problem. -----Input----- The first line of the input contains an integer N denoting the number of elements in the given array. Next line contains N integers Ai separated with spaces. The third line contains the number of games T. Each of the next T lines contain 3 integers Li, Ri and Mi, the left side of the segment, the right side of segment and the modulo. -----Output----- For each game, output a single line containing the answer for the respective segment. -----Constrdaints----- - 1 ≀ N ≀ 100,000 - 1 ≀ Ai ≀ 100 - 1 ≀ T ≀ 100,000 - 1 ≀ Li ≀ Ri ≀ N - 1 ≀ Mi ≀ 109 -----Example----- Input: 5 1 2 3 4 5 4 1 2 3 2 3 4 1 1 1 1 5 1000000000 Output: 2 2 0 120
from sys import stdin primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, ] def getPrime(n): arr = [0] * 25 for i, p in enumerate(primes): if p > n: break while n % p == 0: arr[i] += 1 n //= p return arr def matAdd(matA, matB): arr = [] for i in range(25): arr.append(matB[i] + matA[i]) return arr def matSub(matA, matB): arr = [] for i in range(25): arr.append(matA[i] - matB[i]) return arr def getPow(arr, M): ans = 1 for i, a in enumerate(arr): if a != 0: ans *= pow(primes[i], a, M) if ans > M: ans %= M return ans % M n = int(input()) arr = list(map(int, input().strip().split())) temp = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] pref = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]] for a in arr: for i, p in enumerate(primes): if p > a: break while a % p == 0: temp[i] += 1 a //= p pref.append(temp.copy()) for po in range(int(input())): a, b, M = map(int, input().strip().split()) ans = getPow([(x - y) for x, y in zip(pref[b], pref[a - 1])], M) print(ans % M)
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER ASSIGN VAR LIST LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR
Chef likes toys. His favourite toy is an array of length N. This array contains only integers. He plays with this array every day. His favourite game with this array is Segment Multiplication. In this game, the second player tells the left and right side of a segment and some modulo. The first player should find the multiplication of all the integers in this segment of the array modulo the given modulus. Chef is playing this game. Of course, he is the first player and wants to win all the games. To win any game he should write the correct answer for each segment. Although Chef is very clever, he has no time to play games. Hence he asks you to help him. Write the program that solves this problem. -----Input----- The first line of the input contains an integer N denoting the number of elements in the given array. Next line contains N integers Ai separated with spaces. The third line contains the number of games T. Each of the next T lines contain 3 integers Li, Ri and Mi, the left side of the segment, the right side of segment and the modulo. -----Output----- For each game, output a single line containing the answer for the respective segment. -----Constrdaints----- - 1 ≀ N ≀ 100,000 - 1 ≀ Ai ≀ 100 - 1 ≀ T ≀ 100,000 - 1 ≀ Li ≀ Ri ≀ N - 1 ≀ Mi ≀ 109 -----Example----- Input: 5 1 2 3 4 5 4 1 2 3 2 3 4 1 1 1 1 5 1000000000 Output: 2 2 0 120
import sys def solve(n, arr, queries): store = { (2): [], (3): [], (5): [], (7): [], (11): [], (13): [], (17): [], (19): [], (23): [], (29): [], (31): [], (37): [], (41): [], (43): [], (47): [], (53): [], (59): [], (61): [], (67): [], (71): [], (73): [], (79): [], (83): [], (89): [], (97): [], } temp = [(0) for i in range(n + 1)] for ele in store: store[ele] = temp[:] for i, ele in enumerate(arr): reqIndex = i + 1 for num in store: count = 0 while ele % num == 0 and ele != 0: count += 1 ele = ele // num store[num][reqIndex] = store[num][reqIndex - 1] + count for ele in queries: l, r, m = ele[0], ele[1], ele[2] if m == 1: sys.stdout.write(str(0) + "\n") continue ans = 1 for ele in store: count = store[ele][r] - store[ele][l - 1] ans *= pow(ele, count, m) ans = ans % m ans = ans % m sys.stdout.write(str(ans) + "\n") n = int(sys.stdin.readline().strip()) arr = list(map(int, sys.stdin.readline().strip().split())) T = int(sys.stdin.readline().strip()) queries = [] while T: l, r, m = map(int, sys.stdin.readline().strip().split()) queries.append([l, r, m]) T -= 1 solve(n, arr, queries)
IMPORT FUNC_DEF ASSIGN VAR DICT NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST LIST ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FOR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR NUMBER STRING ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST WHILE VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR
Chef likes toys. His favourite toy is an array of length N. This array contains only integers. He plays with this array every day. His favourite game with this array is Segment Multiplication. In this game, the second player tells the left and right side of a segment and some modulo. The first player should find the multiplication of all the integers in this segment of the array modulo the given modulus. Chef is playing this game. Of course, he is the first player and wants to win all the games. To win any game he should write the correct answer for each segment. Although Chef is very clever, he has no time to play games. Hence he asks you to help him. Write the program that solves this problem. -----Input----- The first line of the input contains an integer N denoting the number of elements in the given array. Next line contains N integers Ai separated with spaces. The third line contains the number of games T. Each of the next T lines contain 3 integers Li, Ri and Mi, the left side of the segment, the right side of segment and the modulo. -----Output----- For each game, output a single line containing the answer for the respective segment. -----Constrdaints----- - 1 ≀ N ≀ 100,000 - 1 ≀ Ai ≀ 100 - 1 ≀ T ≀ 100,000 - 1 ≀ Li ≀ Ri ≀ N - 1 ≀ Mi ≀ 109 -----Example----- Input: 5 1 2 3 4 5 4 1 2 3 2 3 4 1 1 1 1 5 1000000000 Output: 2 2 0 120
primes = [ 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, ] def getPrimeFactors(num): arr = [0] * 25 for index, i in enumerate(primes): if i > num: break while num % i == 0: arr[index] += 1 num //= i return arr def findPower(arr, mod): ans = 1 for index, i in enumerate(arr): if i != 0: ans *= pow(primes[index], i, mod) if ans > mod: ans %= mod return ans % mod n = int(input()) arr = list(map(int, input().strip().split())) cumarr = [] temp = [0] * 25 cumarr.append(temp.copy()) for i in arr: for index, p in enumerate(primes): if p > i: break while i % p == 0: temp[index] += 1 i //= p cumarr.append(temp.copy()) for x in range(int(input())): l, r, m = map(int, input().strip().split()) ans = findPower([(x - y) for x, y in zip(cumarr[r], cumarr[l - 1])], m) print(ans)
ASSIGN VAR LIST NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR IF VAR VAR VAR VAR RETURN BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP LIST NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR VAR WHILE BIN_OP VAR VAR NUMBER VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution: def isValidSerialization(self, preorder): arr = preorder.split(",") k = len(arr) for _ in range(k): print(arr) if arr == ["#"]: return True newArr = [] i = 0 while i < len(arr): if ( i < len(arr) - 2 and arr[i].isdigit() and arr[i + 1 : i + 3] == ["#", "#"] ): newArr += ["#"] i += 3 else: newArr += [arr[i]] i += 1 arr = newArr return False
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR IF VAR LIST STRING RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER LIST STRING STRING VAR LIST STRING VAR NUMBER VAR LIST VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN NUMBER
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution: def isValidSerialization(self, preorder): if not preorder: return False nodes = preorder.split(",") stack = [0] if nodes[0] != "#" else [] dt = {(0): 2} i = 1 while stack and i < len(nodes): dt[stack[-1]] -= 1 if dt[stack[-1]] == 0: stack.pop() if nodes[i] != "#": stack.append(i) dt[i] = 2 i = i + 1 return not stack and i == len(nodes)
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER STRING LIST NUMBER LIST ASSIGN VAR DICT NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF VAR VAR STRING EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR VAR FUNC_CALL VAR VAR
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution: def isValidSerialization(self, preorder): if not preorder: return True arr = preorder.split(",") s = [] for a in arr: s.append(a) while len(s) >= 3 and s[-1] == "#" and s[-2] == "#" and s[-3] != "#": s.pop() s.pop() s.pop() s.append("#") if s == ["#"]: return True return False
CLASS_DEF FUNC_DEF IF VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING VAR NUMBER STRING VAR NUMBER STRING EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING IF VAR LIST STRING RETURN NUMBER RETURN NUMBER
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution: def isValidSerialization(self, preorder): stack, preorder = [], preorder.split(",") top = -1 for s in preorder: stack.append(s) top += 1 while self.endsWithTwoHashes(stack, top): h, top = stack.pop(), top - 1 h, top = stack.pop(), top - 1 if top < 0: return False stack[-1] = "#" return len(stack) == 1 and stack[0] == "#" def endsWithTwoHashes(self, stack, top): if top < 1: return False if stack[top] == "#" and stack[top - 1] == "#": return True return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR LIST FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR NUMBER STRING RETURN FUNC_CALL VAR VAR NUMBER VAR NUMBER STRING FUNC_DEF IF VAR NUMBER RETURN NUMBER IF VAR VAR STRING VAR BIN_OP VAR NUMBER STRING RETURN NUMBER RETURN NUMBER
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution: def isValidSerialization(self, preorder): new_preorder = preorder for i in range(len(new_preorder) - 1, 0, -1): if (new_preorder[i] != "#" and new_preorder[i] != ",") and ( new_preorder[i - 1] != "#" and new_preorder[i - 1] != "," ): preorder = preorder[:i] + preorder[i + 1 :] print(preorder) num = 0 sharp = 0 for i in range(0, len(preorder)): print((num, sharp)) if sharp > num: return False elif preorder[i] == "#": sharp += 1 if sharp == num + 1 and num == int((len(preorder) / 2 + 1) / 2): return True elif preorder[i] != ",": num += 1 print((num, sharp)) if num != sharp - 1: return False
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR STRING VAR VAR STRING VAR BIN_OP VAR NUMBER STRING VAR BIN_OP VAR NUMBER STRING ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN NUMBER IF VAR VAR STRING VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER RETURN NUMBER IF VAR VAR STRING VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR BIN_OP VAR NUMBER RETURN NUMBER
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution: def isValidSerialization(self, preorder): if len(preorder) < 1: return False stack = [] for s in preorder.split(","): stack.append(False) if s == "#": while len(stack) > 2 and stack[-2]: stack.pop() stack.pop() stack.pop() else: stack.append(True) return stack == [False, True]
CLASS_DEF FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR NUMBER IF VAR STRING WHILE FUNC_CALL VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR NUMBER RETURN VAR LIST NUMBER NUMBER
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution: def isValidSerialization(self, preorder): preorder, first = preorder.split(","), preorder.split(",") def backward(index): if index >= len(preorder) or index < 0: return if ( index + 1 < len(preorder) and preorder[index + 1] == preorder[index] == "#" and index - 1 >= 0 and preorder[index - 1] != "#" ): preorder.pop(index) preorder.pop(index) preorder[index - 1] = "#" backward(index - 2) else: backward(index + 1) backward(0) return ( True if preorder != first and preorder == ["#"] or preorder == first == ["#"] else False )
CLASS_DEF FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR STRING FUNC_DEF IF VAR FUNC_CALL VAR VAR VAR NUMBER RETURN IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR VAR STRING BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER STRING EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR NUMBER RETURN VAR VAR VAR LIST STRING VAR VAR LIST STRING NUMBER NUMBER
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution(object): def isValidSerialization(self, preorder): p = preorder.split(",") slot = 1 for node in p: if slot == 0: return False if node == "#": slot -= 1 else: slot += 1 return slot == 0
CLASS_DEF VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR IF VAR NUMBER RETURN NUMBER IF VAR STRING VAR NUMBER VAR NUMBER RETURN VAR NUMBER
One way to serialize a binary tree is to use pre-order traversal. When we encounter a non-null node, we record the node's value. If it is a null node, we record using a sentinel value such as #. _9_ / \ 3 2 / \ / \ 4 1 # 6 / \ / \ / \ # # # # # # For example, the above binary tree can be serialized to the string "9,3,4,#,#,1,#,#,2,#,6,#,#", where # represents a null node. Given a string of comma separated values, verify whether it is a correct preorder traversal serialization of a binary tree. Find an algorithm without reconstructing the tree. Each comma separated value in the string must be either an integer or a character '#' representing null pointer. You may assume that the input format is always valid, for example it could never contain two consecutive commas such as "1,,3". Example 1: Input: "9,3,4,#,#,1,#,#,2,#,6,#,#" Output: true Example 2: Input: "1,#" Output: false Example 3: Input: "9,#,#,1" Output: false
class Solution: def isValidSerialization(self, preorder): preorder = preorder.split(",") if preorder[0] == "#": return len(preorder) == 1 s = [] curr = preorder[0] on_left = True for i in range(1, len(preorder)): if not curr: return False e = preorder[i] if e != "#": if on_left: s.append(curr) curr = e on_left = True else: if not on_left: curr = s.pop() if len(s) > 0 else None on_left = False return curr is None
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR STRING IF VAR NUMBER STRING RETURN FUNC_CALL VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF VAR RETURN NUMBER ASSIGN VAR VAR VAR IF VAR STRING IF VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR NONE ASSIGN VAR NUMBER RETURN VAR NONE
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai β€” add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. - ai β€” delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. 3. ? s β€” count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer t (1 ≀ t ≀ 100 000) β€” the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci β€” the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≀ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Output 2 1 2 1 1 Input 4 + 200 + 200 - 200 ? 0 Output 1 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000.
from sys import stdin, stdout quests = int(stdin.readline().rstrip()) multi_set = {} p = str.maketrans("0123456789", "0101010101") for i in range(quests): quest, core = stdin.readline().rstrip().split() pattern = int(core.translate(p)) if quest == "+": if pattern in multi_set.keys(): multi_set[pattern] += 1 else: multi_set[pattern] = 1 elif quest == "-": multi_set[pattern] -= 1 elif pattern in multi_set: stdout.write(str(multi_set[pattern]) + "\n") else: stdout.write("0\n")
ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING IF VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR STRING VAR VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR STRING EXPR FUNC_CALL VAR STRING
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai β€” add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. - ai β€” delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. 3. ? s β€” count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer t (1 ≀ t ≀ 100 000) β€” the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci β€” the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≀ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Output 2 1 2 1 1 Input 4 + 200 + 200 - 200 ? 0 Output 1 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000.
t = int(input()) ans = [0] * 2**18 y = str.maketrans("0123456789", "0101010101") for i in range(t): x, s = map(str, input().split()) if x == "+": ans[int(s.translate(y), 2)] += 1 elif x == "-": ans[int(s.translate(y), 2)] -= 1 else: print(ans[int(s.translate(y), 2)])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER IF VAR STRING VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai β€” add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. - ai β€” delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. 3. ? s β€” count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer t (1 ≀ t ≀ 100 000) β€” the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci β€” the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≀ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Output 2 1 2 1 1 Input 4 + 200 + 200 - 200 ? 0 Output 1 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000.
present = {} coded = {} cc = {} def encode(s): a = "" for i in range(len(s)): if int(s[i]) % 2 == 0: a += "0" else: a += "1" x = "" for i in range(18 - len(a)): x += "0" x += a return x def add(num): present[num] = present.get(num, 0) + 1 x = encode(num) cc[num] = x coded[x] = coded.get(x, 0) + 1 def delete(num): present[num] -= 1 coded[cc[num]] -= 1 def query(patt): x = "" for i in range(18 - len(patt)): x += "0" x = x + patt if x in coded: print(coded[x]) else: print(0) t = int(input()) for q in range(t): ty, i = input().split() if ty == "+": add(i) if ty == "-": delete(i) if ty == "?": query(i)
ASSIGN VAR DICT ASSIGN VAR DICT ASSIGN VAR DICT FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF BIN_OP FUNC_CALL VAR VAR VAR NUMBER NUMBER VAR STRING VAR STRING ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR STRING VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FUNC_DEF VAR VAR NUMBER VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP NUMBER FUNC_CALL VAR VAR VAR STRING ASSIGN VAR BIN_OP VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai β€” add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. - ai β€” delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. 3. ? s β€” count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer t (1 ≀ t ≀ 100 000) β€” the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci β€” the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≀ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Output 2 1 2 1 1 Input 4 + 200 + 200 - 200 ? 0 Output 1 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000.
from sys import stdin input() cnt = [0] * 2**18 t = str.maketrans("0123456789", "0101010101") for ch, s in map(str.split, stdin): if ch == "?": print(cnt[int(s, 2)]) else: cnt[int(s.translate(t), 2)] += 1 if ch == "+" else -1
EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR STRING NUMBER NUMBER
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai β€” add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. - ai β€” delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. 3. ? s β€” count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer t (1 ≀ t ≀ 100 000) β€” the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci β€” the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≀ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Output 2 1 2 1 1 Input 4 + 200 + 200 - 200 ? 0 Output 1 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000.
num = int(input()) multiset = [0] * 2**18 t = str.maketrans("0123456789", "0101010101") while num: opr = input().split() if opr[0] == "+": multiset[int(opr[1].translate(t), 2)] += 1 elif opr[0] == "-": multiset[int(opr[1].translate(t), 2)] -= 1 elif opr[0] == "?": print(multiset[int(opr[1], 2)]) num -= 1
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING WHILE VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER STRING VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER STRING EXPR FUNC_CALL VAR VAR FUNC_CALL VAR VAR NUMBER NUMBER VAR NUMBER
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai β€” add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. - ai β€” delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. 3. ? s β€” count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer t (1 ≀ t ≀ 100 000) β€” the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci β€” the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≀ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Output 2 1 2 1 1 Input 4 + 200 + 200 - 200 ? 0 Output 1 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000.
t = int(input()) trantab = str.maketrans("0123456789", "0101010101") a = {} for i in range(t): string = input() oper, number = string.split() pattern = int(number.translate(trantab)) if oper == "+": a[pattern] = a.get(pattern, 0) + 1 elif oper == "-": a[pattern] -= 1 if not a[pattern]: del a[pattern] elif pattern in a: print(a[pattern]) else: print(0)
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR STRING ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER IF VAR STRING VAR VAR NUMBER IF VAR VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER
Today Sonya learned about long integers and invited all her friends to share the fun. Sonya has an initially empty multiset with integers. Friends give her t queries, each of one of the following type: 1. + ai β€” add non-negative integer ai to the multiset. Note, that she has a multiset, thus there may be many occurrences of the same integer. 2. - ai β€” delete a single occurrence of non-negative integer ai from the multiset. It's guaranteed, that there is at least one ai in the multiset. 3. ? s β€” count the number of integers in the multiset (with repetitions) that match some pattern s consisting of 0 and 1. In the pattern, 0 stands for the even digits, while 1 stands for the odd. Integer x matches the pattern s, if the parity of the i-th from the right digit in decimal notation matches the i-th from the right digit of the pattern. If the pattern is shorter than this integer, it's supplemented with 0-s from the left. Similarly, if the integer is shorter than the pattern its decimal notation is supplemented with the 0-s from the left. For example, if the pattern is s = 010, than integers 92, 2212, 50 and 414 match the pattern, while integers 3, 110, 25 and 1030 do not. Input The first line of the input contains an integer t (1 ≀ t ≀ 100 000) β€” the number of operation Sonya has to perform. Next t lines provide the descriptions of the queries in order they appear in the input file. The i-th row starts with a character ci β€” the type of the corresponding operation. If ci is equal to '+' or '-' then it's followed by a space and an integer ai (0 ≀ ai < 1018) given without leading zeroes (unless it's 0). If ci equals '?' then it's followed by a space and a sequence of zeroes and onse, giving the pattern of length no more than 18. It's guaranteed that there will be at least one query of type '?'. It's guaranteed that any time some integer is removed from the multiset, there will be at least one occurrence of this integer in it. Output For each query of the third type print the number of integers matching the given pattern. Each integer is counted as many times, as it appears in the multiset at this moment of time. Examples Input 12 + 1 + 241 ? 1 + 361 - 241 ? 0101 + 101 ? 101 - 101 ? 101 + 4000 ? 0 Output 2 1 2 1 1 Input 4 + 200 + 200 - 200 ? 0 Output 1 Note Consider the integers matching the patterns from the queries of the third type. Queries are numbered in the order they appear in the input. 1. 1 and 241. 2. 361. 3. 101 and 361. 4. 361. 5. 4000.
t = int(input()) dc = [0] * 2**19 tt = str.maketrans("0123456789", "0101010101") for i in range(0, t): st = input().split() if st[0] == "+": dc[int(st[1].translate(tt), 2)] += 1 elif st[0] == "-": dc[int(st[1].translate(tt), 2)] -= 1 else: mu = int(st[1], 2) print(dc[mu])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR STRING STRING FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER STRING VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER STRING VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = map(int, input().split()) A = list(map(int, input().split())) for _ in range(q): d = int(input()) curr_max = max(A[i] for i in range(d)) min_of_maxes = curr_max for i in range(1, n - d + 1): a, b = A[i - 1], A[i + d - 1] if b > curr_max: curr_max = b if curr_max < min_of_maxes: min_of_maxes = curr_max elif a == curr_max: curr_max = max(A[j] for j in range(i, i + d)) if curr_max < min_of_maxes: min_of_maxes = curr_max print(min_of_maxes)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def naive_solve(arr, d): return min(max(arr[i : i + d]) for i in range(len(arr) - d + 1)) def max_last(start, stop, arr): i = start for j in range(start, stop): if arr[j] >= arr[i]: i = j return i def naive_solve2(arr, d): imax = max_last(0, d, arr) res = arr[imax] for i in range(1, len(arr) - d + 1): if imax < i: imax = max_last(i, i + d, arr) else: imax = imax if arr[imax] > arr[i + d - 1] else i + d - 1 res = min(res, arr[imax]) return res parse = lambda: [int(x) for x in input().split()] n, q = parse() arr = parse() for _ in range(q): d = int(input()) print(naive_solve2(arr, d))
FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
stin = lambda: tuple(map(int, input().strip().split())) n, q = stin() lst = stin() def nearest_largers(): lfts = [-1] * n rgts = [n] * n st = list() for i, a in enumerate(lst): while st and lst[st[-1]] < a: rgts[st.pop()] = i if st: if lst[st[-1]] > a: lfts[i] = st[-1] else: lfts[i] = lfts[st[-1]] st.append(i) return lfts, rgts def max_widths(): lrs = nearest_largers() return list(r - l - 1 for l, r in zip(*lrs)) def mins_of_sliding_maxs(): res = [float("inf")] * (n + 1) for a, width in zip(lst, max_widths()): res[width] = min(res[width], a) for width in range(n - 1, 0, -1): res[width] = min(res[width], res[width + 1]) return res mins = mins_of_sliding_maxs() for _ in range(q): print(mins[int(input())])
ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR IF VAR VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR RETURN FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR BIN_OP LIST FUNC_CALL VAR STRING BIN_OP VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = map(int, input().strip().split(" ")) a = list(map(int, input().strip().split(" "))) for _ in range(q): d = int(input().strip()) maxs = max(a[0:d]) res = maxs for i in range(1, n - d + 1): if maxs == a[i - 1]: maxi = max(a[i : i + d]) else: maxi = max(maxs, a[i + d - 1]) maxs = maxi if maxi < res: res = maxi print(res)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def max_c(l): if not l: return None m = l[0] c = 1 for i in l[1:]: if i > m: m = i c = 1 elif i == m: c += 1 return m, c def min_max(n, d, l): m, c = max_c(l[:d]) min_max = m for i in range(n - d): if l[i] == m: c -= 1 newv = l[i + d] if c == 0: m, c = max_c(l[i + 1 : i + d + 1]) elif m == newv: c += 1 elif m < newv: m = newv c = 1 min_max = min(min_max, m) return min_max n, q = map(int, input().strip().split()) seq = list(map(int, input().strip().split())) for _ in range(q): i = int(input().strip()) print(min_max(n, i, seq))
FUNC_DEF IF VAR RETURN NONE ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER RETURN VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, m = map(int, input().split(" ")) numbers = list(map(int, input().split(" "))) for _ in range(m): q = int(input()) comparing_i = 0 answer = max(numbers[:q]) cur_max = answer for n in range(q, len(numbers)): if numbers[n] > cur_max: cur_max = numbers[n] temp = numbers[comparing_i] comparing_i += 1 if temp == cur_max and temp > numbers[n]: cur_max = max(numbers[comparing_i : n + 1]) answer = min(answer, cur_max) print(answer)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def min_max(array, sub_length): curr_max = max(array[0:sub_length]) least_max = curr_max front = 0 end = sub_length while end < len(array): dequeued = array[front] enqueued = array[end] if dequeued == curr_max: curr_max = max(array[front + 1 : end + 1]) elif enqueued > curr_max: curr_max = enqueued if curr_max < least_max: least_max = curr_max front += 1 end += 1 return least_max inputs = [int(i) for i in input().strip().split(" ")] num_queries = inputs[0] array_length = inputs[1] array = [int(i) for i in input().strip().split(" ")] for i in range(num_queries): sub_length = int(input().strip()) print(min_max(array, sub_length))
FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = map(int, input().split()) sequence = list(map(int, input().split())) def debug(ds): for i in ds: print(i, end=" ") print() for _ in range(q): d = int(input()) max_current = max(sequence[0:d]) min_ans = max_current for left in range(1, len(sequence) - d + 1): removed = sequence[left - 1] added = sequence[left + d - 1] if added > max_current: max_current = added elif removed == max_current: max_current = max(sequence[left : left + d]) if min_ans > max_current: min_ans = max_current elif max_current >= added: pass print(min_ans)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = [int(arr) for arr in input().strip().split()] data = [int(data) for data in input().strip().split()] query = [] for i in range(q): query.append(int(input())) for d in query: max_index = [] min_nums = [] for i in range(n): while max_index != [] and data[i] > data[max_index[-1]]: max_index.pop() max_index.append(i) if i >= d - 1: min_nums.append(data[max_index[0]]) if max_index[0] == i + 1 - d: max_index.pop(0) print(min(min_nums))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR WHILE VAR LIST VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER IF VAR NUMBER BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def calMax(l, s, len): m = l[s] for i in range(s + 1, s + len): m = (m, l[i])[l[i] > m] return m [n, q] = list(map(int, input().rstrip().split())) nu = list(map(int, input().rstrip().split())) for i in range(q): d = int(input()) max = calMax(nu, 0, d) min = max mc = 1 for j in range(d, n): en = nu[j] ex = nu[j - d] if en > max: max = en mc = 1 elif en == max: mc += 1 if ex == max: mc -= 1 if not mc: max = calMax(nu, j - d + 1, d) if max < min: min = max mc = 1 print(min)
FUNC_DEF ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR RETURN VAR ASSIGN LIST VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER IF VAR VAR VAR NUMBER IF VAR VAR VAR NUMBER IF VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = map(int, input().split()) A = [int(_) for _ in input().split()] for _ in range(q): d = int(input()) current_max = max(A[:d]) current_min = current_max for i in range(1, n - d + 1): if A[i - 1] == current_max: if A[i + d - 1] != current_max: current_max = max(A[i : i + d]) current_min = min(current_min, current_max) print(current_min)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
import sys num, n = map(int, input().split(" ")) lst = list(map(int, input().strip(" ").split())) for i in range(n): x = int(input()) temp = 0 count = 0 i = 0 minimum = 0 while i < num - x + 1: temp = 0 for j in range(i, x + i): if lst[j] > temp: temp = lst[j] pos = j if i == 0: minimum = temp elif temp < minimum: minimum = temp i = pos + 1 print(minimum)
IMPORT ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
_, Q = map(int, input().split()) values = list(map(int, input().split())) for _ in range(Q): d = int(input()) cmin = cmax = max(values[i] for i in range(d)) for i in range(d, len(values)): if values[i - d] == cmax: cmax = max(values[i] for i in range(i - d + 1, i + 1)) else: cmax = max(cmax, values[i]) cmin = min(cmin, cmax) print(cmin)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = input().strip().split(" ") n, q = [int(n), int(q)] a = list(map(int, input().strip().split(" "))) def findMax(arr): val = arr[0] idx = 0 for i in range(len(arr)): if val < arr[i]: val = arr[i] idx = i return val, idx for q0 in range(q): d = int(input()) if d == 1: print(min(a)) else: minVal = 1000000.0 + 1 i = 0 while i <= len(a) - d: if i + d > len(a): end = len(a) else: end = i + d val, idx = findMax(a[i:end]) idx += i if val < minVal: minVal = val i = idx + 1 print(minVal)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = [int(x) for x in input().strip().split()] arr = [int(x) for x in input().strip().split()] for _ in range(q): d = int(input().strip()) maxes = [] was_first = False for i in range(n - d + 1): if i == 0 or was_first == True: maxes.append(max(arr[i : i + d])) else: maxes.append(max(maxes[-1], arr[i + d - 1])) if maxes[-1] == arr[i]: was_first = True else: was_first = False print(min(maxes))
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def decr_queue_append(max_queue, elem, i): while max_queue and max_queue[-1][0] < elem: max_queue.pop() max_queue.append((elem, i)) n, q = input().strip().split(" ") n, q = int(n), int(q) a = list(map(int, input().strip().split(" "))) for _ in range(q): Min = 1000000 d = int(input()) max_queue = [] for i in range(d - 1): decr_queue_append(max_queue, a[i], i) for i in range(n - d + 1): decr_queue_append(max_queue, a[i + d - 1], i + d - 1) if max_queue[0][1] < i: max_queue.pop(0) Min = min(Min, max_queue[0][0]) print(Min)
FUNC_DEF WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = [int(i) for i in input().split()] arr = [int(i) for i in input().split()] for i in range(q): a = int(input()) b = arr[:a] maxi = max(b) large = maxi mini = maxi front = 0 size = a for j in range(a, n): avail = (front + a) % a deq = b[front] b[avail] = arr[j] if deq == large: large = max(b) mini = min(mini, large) else: large = max(large, arr[j]) mini = min(mini, large) front = (front + 1) % a b = [] print(mini)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR LIST EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def build_affected_area(arr): left = [0] * len(arr) for i in range(1, len(arr)): accu = 0 j = i - 1 while j >= 0: if arr[i] < arr[j]: break accu += left[j] + 1 j -= left[j] + 1 left[i] = accu right = [0] * len(arr) for i in range(len(arr) - 2, -1, -1): accu = 0 j = i + 1 while j < len(arr): if arr[i] < arr[j]: break accu += right[j] + 1 j += right[j] + 1 right[i] = accu return [(left[i] + right[i] + 1) for i in range(len(arr))] def build_reverse_mapping(arr, area): mapping = {} for i in range(len(arr)): if area[i] not in mapping or mapping[area[i]] > arr[i]: mapping[area[i]] = arr[i] tmp = sorted(list(mapping.items())) map_list = [tmp[-1]] for i in range(len(tmp) - 2, -1, -1): if tmp[i][1] >= map_list[-1][1]: continue map_list.append(tmp[i]) map_list.reverse() return map_list def find_from_mapping(mapping, d): def binary_search(start, end, d): if start == end: return mapping[start][1] mid = (start + end) // 2 if mapping[mid][0] >= d: return binary_search(start, mid, d) else: return binary_search(mid + 1, end, d) return binary_search(0, len(mapping) - 1, d) n, q = map(int, input().split()) arr = list(map(int, input().split())) area = build_affected_area(arr[:n]) mapping = build_reverse_mapping(arr, area) for _ in range(q): d = int(input()) print(find_from_mapping(mapping, d))
FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR RETURN BIN_OP BIN_OP VAR VAR VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR RETURN VAR FUNC_DEF FUNC_DEF IF VAR VAR RETURN VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = [int(number) for number in input().split()] mylist = [int(number) for number in input().split()] for step in range(q): minmax = 0 d = int(input()) maxnumber = 0 for i in range(n): if mylist[i] >= maxnumber: maxnumber = mylist[i] elif i >= d: if mylist[i - d] == maxnumber: maxnumber = max(mylist[i - d + 1 : i + 1]) if i >= d - 1: if minmax == 0 or minmax > maxnumber: minmax = maxnumber print(minmax)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
import sys line = sys.stdin.readline().split(" ") n, q = int(line[0]), int(line[1]) line = sys.stdin.readline().split(" ") A = [int(a) for a in line] def findmax(D, m, n): m_val = D[m] m_idx = m for i in range(m + 1, n): if D[i] > m_val: m_val = D[i] m_idx = i return m_val, m_idx for qq in range(q): d = int(sys.stdin.readline()) maxval, maxidx = findmax(A, 0, d) minmax = maxval for i in range(d, n): if i - d == maxidx: maxval, maxidx = findmax(A, maxidx + 1, i + 1) minmax = min(maxval, minmax) elif A[i] > maxval: maxval, maxidx = A[i], i minmax = min(maxval, minmax) elif A[i] == maxval: maxidx = i sys.stdout.write(str(minmax) + "\n")
IMPORT ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR RETURN VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def minmax(sequence, d): maxlist = [] start = 0 end = d if d == 1: return min(sequence) if d == len(sequence): return max(sequence) submax = max(sequence[start:end]) maxlist.append(submax) for _ in range(1, len(sequence) + 1 - d): start = start + 1 end = end + 1 if sequence[start - 1] == submax: submax = max(sequence[start:end]) elif sequence[end - 1] > submax: submax = sequence[end - 1] maxlist.append(submax) return min(maxlist) n, q = input().strip().split(" ") n = int(n) q = int(q) sequence = list(map(int, input().strip().split(" "))) for _ in range(q): d = int(input().strip()) print(minmax(sequence, d))
FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR VAR IF VAR NUMBER RETURN FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def Sol(a, d): if d == len(a): return max(a) maxqueue = [0] for k in range(1, d): while maxqueue and a[k] > a[maxqueue[-1]]: maxqueue.pop() maxqueue.append(k) res = a[maxqueue[0]] for k in range(d, len(a)): if k - maxqueue[0] >= d: maxqueue.pop(0) while maxqueue and a[k] > a[maxqueue[-1]]: maxqueue.pop() maxqueue.append(k) res = min(res, a[maxqueue[0]]) return res N, Q = map(int, input().split()) a = list(map(int, input().split())) for _ in range(Q): d = int(input()) print(Sol(a, d))
FUNC_DEF IF VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = input().split() n, q = [int(n), int(q)] l = list(map(int, input().split())) for q_i in range(q): d = int(input()) temp = [] ma = max(l[:d]) temp.append(ma) for i in range(1, len(l) - d + 1): if l[i - 1] == ma: ma = max(l[i : i + d]) temp.append(ma) elif l[i + d - 1] >= ma: ma = l[i + d - 1] temp.append(ma) m = min(temp) print(m)
ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR LIST FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER IF VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR IF VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def solve(a, q): min_ = 10**6 max_ = -1 ind = None upper = q lower = 0 while upper <= len(a): for i in range(lower, upper): temp = a[i] if temp > max_: ind = i + 1 max_ = temp if max_ < min_: min_ = max_ max_ = -1 upper = ind + q lower = ind return min_ n, q = [int(i) for i in input().split()] a = [int(i) for i in input().split()] for __ in range(q): query = int(input()) print(solve(a, query))
FUNC_DEF ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = map(int, input().strip().split(" ")) arr = list(map(int, input().strip().split(" "))) for _ in range(q): d = int(input().strip()) i = 1 tempMax = max(arr[i - 1 : d]) minm = tempMax while i < len(arr) - d: skipped = arr[i] added = arr[i + d] if added >= tempMax: tempMax = added elif skipped == tempMax: tempMax = max(arr[i + 1 : i + d + 1]) minm = min(minm, tempMax) i += 1 print(minm)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR WHILE VAR BIN_OP FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
n, q = [int(c) for c in input().strip().split(" ")] alist = [int(c) for c in input().strip().split(" ")] for _ in range(q): d = int(input().strip()) dq = [] amin = 10**6 + 1 for i in range(d): while dq and alist[i] > alist[dq[-1]]: dq.pop() dq.append(i) for i in range(d, n): amin = min(amin, alist[dq[0]]) while dq and dq[0] <= i - d: dq.pop(0) while dq and alist[i] > alist[dq[-1]]: dq.pop() dq.append(i) amin = min(amin, alist[dq[0]]) print(amin)
ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR NUMBER BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
import sys class NotEvenAQueue: def __init__(self, size): self.data = [(-sys.maxsize) for _ in range(size)] self.size = size self.maximum = -sys.maxsize def put(self, d, i): if self.data[i % self.size] == self.maximum: self.data[i % self.size] = -sys.maxsize self.maximum = max(self.data) self.data[i % self.size] = d if d > self.maximum: self.maximum = d def get_maximum(self): return self.maximum n, q = map(int, input().strip().split()) a = list(map(int, input().strip().split())) d = [int(x) for x in [input().strip() for _ in range(q)]] for offset in d: q = NotEvenAQueue(offset) m = list() for i in range(offset): q.put(a[i], i) min_m = q.get_maximum() for i in range(offset, n): q.put(a[i], i) if q.get_maximum() < min_m: min_m = q.get_maximum() print(min_m)
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF IF VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_DEF RETURN VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
N, Q = map(int, input().split()) A = list(map(int, input().split())) D = [] for q in range(Q): d = int(input()) min_num = 10**6 + 1 i = 0 max_idx = -1 while i <= N - d: if i > max_idx: max_num = -1 for j in range(i, i + d): if A[j] >= max_num: max_num = A[j] max_idx = j if max_num < min_num: min_num = max_num i += 1 print(min_num)
ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
import sys def find_min(d, A): max_val = 0 for i in range(d): max_val = max(max_val, A[i]) min_of_max = max_val for i in range(d, len(A)): if A[i] > max_val: max_val = max(max_val, A[i]) elif A[i - d] == max_val: max_val = max(A[i - d + 1 : i + 1]) if max_val < min_of_max: min_of_max = max_val return min_of_max def exec_input(fd=sys.stdin): m, n = map(int, fd.readline().strip().split(" ")) A = [int(i) for i in fd.readline().strip().split(" ")] for a0 in range(n): d = int(fd.readline().strip()) print(find_min(d, A)) exec_input()
IMPORT FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR IF VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
str = input() li = str.split() n = int(li[0]) q = int(li[1]) str = input() arr = str.split() for i in range(n): arr[i] = int(arr[i]) li = [] for i in range(q): li.append(int(input())) mx = -4 for i in range(q): d = li[i] j = d mx = max(arr[:j]) mn = mx while j < n: if mx <= arr[j]: mx = arr[j] if mx < mn: mn = mx else: if mx == arr[j - d]: mx = max(arr[j - d + 1 : j + 1]) if mx < mn: mn = mx j += 1 print(mn)
ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
nq = input().split() n = int(nq[0]) q = int(nq[1]) arr = list(map(int, input().rstrip().split())) queries = [] for _ in range(q): queries_item = int(input()) queries.append(queries_item) i = 0 while i < len(queries): d = queries[i] max_ele = max(arr[0:d]) current_min = max_ele if d == 1: current_min = min(arr) elif d == len(arr): current_min = max(arr) else: j = d front = 1 while j < len(arr): if max_ele == arr[front - 1]: max_ele = max(arr[front : j + 1]) if current_min > max_ele: current_min = max_ele elif arr[j] > max_ele: max_ele = arr[j] j += 1 front += 1 print(current_min) i += 1
ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR VAR IF VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def query(n, d, a): minimum = maximum = max(a[:d]) for i in range(1, n - d + 1): if maximum <= a[i + d - 1]: maximum = a[i + d - 1] elif maximum == a[i - 1]: maximum = max(a[i : i + d]) minimum = min(maximum, minimum) return minimum n, q = input().split() n, q = int(n), int(q) a = list(map(int, input().split())) for a0 in range(q): d = int(input()) print(query(n, d, a))
FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
Consider an $n$-integer sequence, $A=\{a_0,a_1,\ldots,a_{n-1}\}$. We perform a query on $\mbox{A}$ by using an integer, $\boldsymbol{d}$, to calculate the result of the following expression: $\underset{0\leq i\leq n-d}{\text{min}}\left(\underset{i\leq j\lt i+d}{\text{max}}a_j\right)$ In other words, if we let $m_i=\text{max}(a_i,a_{i+1},a_{i+2},...,a_{i+d-1})$, then you need to calculate $\text{min}(m_0,m_1,\ldots,m_{n-d})$. Given $\textbf{arr}$ and $\textit{q}$ queries, return a list of answers to each query. Example $arr=[2,3,4,5,6]$ $queries=[2,3]$ The first query uses all of the subarrays of length $2$: $[2,3],[3,4],[4,5],[5,6]$. The maxima of the subarrays are $[3,4,5,6]$. The minimum of these is $3$. The second query uses all of the subarrays of length $3$: $[2,3,4],[3,4,5],[4,5,6]$. The maxima of the subarrays are $[4,5,6]$. The minimum of these is $\begin{array}{c}4\end{array}$. Return $[3,4]$. Function Description Complete the solve function below. solve has the following parameter(s): int arr[n]: an array of integers int queries[q]: the lengths of subarrays to query Returns int[q]: the answers to each query Input Format The first line consists of two space-separated integers, $n$ and $\textit{q}$. The second line consists of $n$ space-separated integers, the elements of $\textbf{arr}$. Each of the $\textit{q}$ subsequent lines contains a single integer denoting the value of $\boldsymbol{d}$ for that query. Constraints $1\leq n\leq10^5$ $0\leq ar r[i]<10^6$ $1\leq q\leq100$ $1\leq d\leq n$ Sample Input 0 5 5 33 11 44 11 55 1 2 3 4 5 Sample Output 0 11 33 44 44 55 Explanation 0 For $\boldsymbol{d}=1$, the answer is $\text{min}(\text{max}(a_0),\text{max}(a_1),\text{max}(a_2),\text{max}(a_3),\text{max}(a_4))=11$. For $\boldsymbol{d}=2$, the answer is $\text{min}(\text{max}(a_0,a_1),\text{max}(a_1,a_2),\text{max}(a_2,a_3),\text{max}(a_3,a_4))=33$. For $\boldsymbol{d}=3$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2),\text{max}(a_1,a_2,a_3),\text{max}(a_2,a_3,a_4))=44$. For $\boldsymbol{d}=4$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3),\text{max}(a_1,a_2,a_3,a_4))=44$. For $\boldsymbol{d}=5$, the answer is $\text{min}(\text{max}(a_0,a_1,a_2,a_3,a_4))=55$. Sample Input 1 5 5 1 2 3 4 5 1 2 3 4 5 Sample Output 1 1 2 3 4 5 Explanation 1 For each query, the "prefix" has the least maximum value among the consecutive subsequences of the same size.
def minOfMax(arr, d): if len(arr) == 0: return 10**5 elif d == 1: return min(arr) else: M = max(arr) idx = arr.index(M) if idx < d and len(arr) - idx - 1 < d: return M elif idx < d: return min(M, minOfMax(arr[idx + 1 :], d)) elif len(arr) - idx - 1 < d: return min(M, minOfMax(arr[:idx], d)) else: return min(M, minOfMax(arr[:idx], d), minOfMax(arr[idx + 1 :], d)) n, q = map(int, input().split(" ")) A = list(map(int, input().split(" "))) for _ in range(q): d = int(input().strip()) print(minOfMax(A, d))
FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN BIN_OP NUMBER NUMBER IF VAR NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR RETURN VAR IF VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): dp = [[(0) for i in range(N + 1)] for j in range(N + 1)] for i in range(1, N + 1): for j in range(1, N + 1): dp[i][j] = ( dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] + mat[i - 1][j - 1] ) res = -100000000.0 for i in range(K, N + 1): for j in range(K, N + 1): res = max( res, dp[i][j] - dp[i - K][j] - dp[i][j - K] + dp[i - K][j - K] ) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): dp = [[(0) for i in range(N + 1)] for j in range(N + 1)] ans = 0 for i, r in enumerate(dp): for j, _ in enumerate(r): if i > 0 and j > 0: dp[i][j] = ( mat[i - 1][j - 1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] ) if i >= k and j >= k: area = dp[i][j] - dp[i - k][j] - dp[i][j - k] + dp[i - k][j - k] ans = max(ans, area) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, n, k): dp = [([0] * (n + 1)) for i in range(n + 1)] maxm = -10000000.0 for i in range(n + 1): for j in range(n + 1): if i == 0 or j == 0: dp[i][j] = 0 else: dp[i][j] = ( mat[i - 1][j - 1] + dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1] ) if i >= k and j >= k: maxm = max( maxm, dp[i][j] - (dp[i - k][j] + dp[i][j - k] - dp[i - k][j - k]), ) return maxm
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, k): if k > N: return stripSum = [[(0) for j in range(N)] for i in range(N)] for j in range(N): sum = 0 for i in range(k): sum += mat[i][j] stripSum[0][j] = sum for i in range(1, N - k + 1): sum += mat[i + k - 1][j] - mat[i - 1][j] stripSum[i][j] = sum max_sum = -1000000000 i_ind = 0 j_ind = 0 for i in range(N - k + 1): sum = 0 for j in range(k): sum += stripSum[i][j] if sum > max_sum: max_sum = sum i_ind = i j_ind = 0 for j in range(1, N - k + 1): sum += stripSum[i][j + k - 1] - stripSum[i][j - 1] if sum > max_sum: max_sum = sum i_ind = i j_ind = j return max_sum
CLASS_DEF FUNC_DEF IF VAR VAR RETURN ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): psa = [[(0) for i in range(N)] for j in range(N)] psa[0][0] = mat[0][0] for i in range(1, N): psa[i][0] = psa[i - 1][0] + mat[i][0] for j in range(1, N): psa[0][j] = psa[0][j - 1] + mat[0][j] for i in range(1, N): for j in range(1, N): psa[i][j] = ( mat[i][j] + psa[i - 1][j] + psa[i][j - 1] - psa[i - 1][j - 1] ) max_sum = float("-inf") for i in range(K - 1, N): for j in range(K - 1, N): tmp = psa[i][j] if i - K >= 0 and j - K >= 0: tmp = tmp - psa[i - K][j] - psa[i][j - K] + psa[i - K][j - K] elif i - K >= 0: tmp = tmp - psa[i - K][j] elif j - K >= 0: tmp = tmp - psa[i][j - K] if tmp > max_sum: max_sum = tmp return max_sum
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
def calsum(mat, n): temp = [[(0) for i in range(n)] for j in range(n)] temp[0][0] = mat[0][0] for i in range(1, n): temp[0][i] = mat[0][i] + temp[0][i - 1] for i in range(1, n): temp[i][0] = temp[i - 1][0] + mat[i][0] for i in range(1, n): for j in range(1, n): temp[i][j] = ( mat[i][j] + temp[i - 1][j] + temp[i][j - 1] - temp[i - 1][j - 1] ) return temp class Solution: def Maximum_Sum(self, mat, n, k): temp = calsum(mat, n) maximum = -999999 for i in range(k - 1, n): for j in range(k - 1, n): total = temp[i][j] if i - k >= 0: total = total - temp[i - k][j] if j - k >= 0: total = total - temp[i][j - k] if i - k >= 0 and j - k >= 0: total = total + temp[i - k][j - k] if total > maximum: maximum = total return maximum if __name__ == "__main__": t = int(input()) for _ in range(t): n = int(input()) matrix = [] for _ in range(n): matrix.append([int(x) for x in input().strip().split()]) k = int(input()) obj = Solution() print(obj.Maximum_Sum(matrix, n, k))
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR IF VAR STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): for i in range(N): for j in range(N): if i != 0: mat[i][j] += mat[i - 1][j] for i in range(N): for j in range(N): if j != 0: mat[i][j] += mat[i][j - 1] dp = [[(0) for i in range(N)] for i in range(N)] top = 0 left = 0 center = 0 ans = 0 for i in range(N): for j in range(N): top = 0 if i - K < 0 else mat[i - K][j] left = 0 if j - K < 0 else mat[i][j - K] center = 0 if i < K or j < K else mat[i - k][j - k] dp[i][j] = mat[i][j] - top - left + center ans = max(ans, dp[i][j]) return ans
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): if K > N: return 0 tl = [] max_sum = 0 i = 0 while i < N: j = 0 while j < N - K + 1: x = i t = sum(mat[i][j : j + K]) tl.append(t) j += 1 i += 1 f = [] for l in range(0, (N - K + 1) * (N - K + 1)): f.append(sum(tl[l : l + (N - K + 1) * K : N - K + 1])) return max(f)
CLASS_DEF FUNC_DEF IF VAR VAR RETURN NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR BIN_OP VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER RETURN FUNC_CALL VAR VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): maxsum = -5 * pow(10, 5) prefix_mat = [[(0) for i in range(N)] for j in range(N)] for i in range(N): for j in range(N): prefix_mat[i][j] = mat[i][j] for i in range(1, N): prefix_mat[i][0] += prefix_mat[i - 1][0] for j in range(1, N): prefix_mat[0][j] += prefix_mat[0][j - 1] for i in range(1, N): for j in range(1, N): prefix_mat[i][j] += ( prefix_mat[i][j - 1] + prefix_mat[i - 1][j] - prefix_mat[i - 1][j - 1] ) for i in range(k - 1, N): for j in range(k - 1, N): rowv, colv, commonval = 0, 0, 0 if j >= k: rowv = prefix_mat[i][j - k] if i >= k: colv = prefix_mat[i - k][j] if i >= k and j >= k: commonval = prefix_mat[i - k][j - k] cursum = prefix_mat[i][j] - rowv - colv + commonval maxsum = max(maxsum, cursum) return maxsum
CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP NUMBER FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR NUMBER NUMBER NUMBER IF VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
import sys class Solution: def Maximum_Sum(self, mat, N, K1): mat1 = [] mat2 = [] for i in range(0, N): k = 0 p = [] for j in range(0, N): k += mat[i][j] p.append(k) mat1.append(p) for i in range(0, N): k = 0 for j in range(0, N): k += mat1[j][i] mat1[j][i] = k ma = -sys.maxsize - 1 for i in range(0, N - K1 + 1): for j in range(0, N - K1 + 1): if i == 0 and j == 0: p1 = mat1[i + (K1 - 1)][j + (K1 - 1)] if p1 > ma: ma = p1 elif i == 0 and j > 0: p1 = mat1[i + (K1 - 1)][j + (K1 - 1)] q1 = mat1[i + (K1 - 1)][j - 1] if p1 - q1 > ma: ma = p1 - q1 elif i > 0 and j == 0: p1 = mat1[i + (K1 - 1)][j + (K1 - 1)] q1 = mat1[i - 1][j + (K1 - 1)] if p1 - q1 > ma: ma = p1 - q1 else: p1 = mat1[i + (K1 - 1)][j + (K1 - 1)] q1 = mat1[i + (K1 - 1)][j - 1] r1 = mat1[i - 1][j + (K1 - 1)] m1 = mat1[i - 1][j - 1] res = p1 - (q1 - m1 + r1) if res > ma: ma = res return ma
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): for i, _ in enumerate(mat): for j, e in enumerate(mat[i]): if j > 0: mat[i][j] += mat[i][j - 1] if i > 0: mat[i][j] += mat[i - 1][j] if i > 0 and j > 0: mat[i][j] -= mat[i - 1][j - 1] ans = 0 for y0 in range(0, N - K + 1): for x0 in range(0, N - K + 1): y, x = y0 + K, x0 + K v = ( area(mat, y - 1, x - 1) + area(mat, y0 - 1, x0 - 1) - area(mat, y - 1, x0 - 1) - area(mat, y0 - 1, x - 1) ) ans = max(ans, v) return ans def area(mat, y, x): if y < 0 or x < 0: return 0 return mat[y][x]
CLASS_DEF FUNC_DEF FOR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): for i in range(N): for j in range(1, N): mat[i][j] += mat[i][j - 1] for i in range(1, N): for j in range(N): mat[i][j] += mat[i - 1][j] res = mat[K - 1][K - 1] for i in range(K - 1, N): for j in range(K - 1, N): if i == K - 1 and j == K - 1: continue temp = mat[i][j] if i > K - 1: temp -= mat[i - K][j] if j > K - 1: temp -= mat[i][j - K] if i > K - 1 and j > K - 1: temp += mat[i - K][j - K] res = max(res, temp) return res
CLASS_DEF FUNC_DEF FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): cp = mat.copy() for i in range(N): for j in range(1, N): cp[i][j] += cp[i][j - 1] for j in range(N): for i in range(1, N): cp[i][j] += cp[i - 1][j] maxi = -1000000000000.0 for i in range(N - K + 1): for j in range(N - K + 1): ro = i + K - 1 co = j + K - 1 sumi = cp[ro][co] if j > 0: sumi -= cp[ro][j - 1] if i > 0: sumi -= cp[i - 1][co] if i > 0 and j > 0: sumi += cp[i - 1][j - 1] maxi = max(sumi, maxi) return maxi
CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
def calsum(mat, n): temp = [[(0) for i in range(n)] for j in range(n)] temp[0][0] = mat[0][0] for i in range(1, n): temp[0][i] = mat[0][i] + temp[0][i - 1] for i in range(1, n): temp[i][0] = temp[i - 1][0] + mat[i][0] for i in range(1, n): for j in range(1, n): temp[i][j] = ( mat[i][j] + temp[i - 1][j] + temp[i][j - 1] - temp[i - 1][j - 1] ) return temp class Solution: def Maximum_Sum(self, mat, n, k): temp = calsum(mat, n) maximum = -999999 for i in range(k - 1, n): for j in range(k - 1, n): total = temp[i][j] if i - k >= 0: total = total - temp[i - k][j] if j - k >= 0: total = total - temp[i][j - k] if i - k >= 0 and j - k >= 0: total = total + temp[i - k][j - k] if total > maximum: maximum = total return maximum
FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
def window(arr, l): m = 0 mx = -float("inf") s = 0 for i in range(l): m += arr[i] mx = max(mx, m) for i in range(l, len(arr)): m -= arr[s] m += arr[i] mx = max(mx, m) s += 1 return mx class Solution: def Maximum_Sum(self, mat, N, k): m = len(mat) n = len(mat[0]) temp = [0] * n for i in range(k): for j in range(n): temp[j] += mat[i][j] mx = -float("inf") mx = max(mx, window(temp, k)) s = 0 for i in range(k, m): for j in range(n): temp[j] -= mat[s][j] temp[j] += mat[i][j] s += 1 mx = max(mx, window(temp, k)) return mx
FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
import sys class Solution: def Maximum_Sum(self, mat, N, K): dp = [[(0) for i in range(N)] for i in range(N)] for i in range(N): for j in range(N): dp[i][j] = mat[i][j] if i > 0: dp[i][j] += dp[i - 1][j] if j > 0: dp[i][j] += dp[i][j - 1] if i > 0 and j > 0: dp[i][j] -= dp[i - 1][j - 1] ans = 0 for i in range(K - 1, N): for j in range(K - 1, N): t = dp[i][j] if i > K - 1: t -= dp[i - K][j] if j > K - 1: t -= dp[i][j - K] if i > K - 1 and j > K - 1: t += dp[i - K][j - K] ans = max(ans, t) return ans
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, n, k): prefix_sum = [[(0) for i in range(n)] for j in range(n)] res = [[float("-inf") for j in range(n)] for i in range(n)] max_sum = float("-inf") for i in range(n): for j in range(n): if i == 0 and j == 0: prefix_sum[i][j] = mat[i][j] if i == 0 and j > 0: prefix_sum[i][j] = prefix_sum[i][j - 1] + mat[i][j] if j == 0 and i > 0: prefix_sum[i][j] = prefix_sum[i - 1][j] + mat[i][j] if i > 0 and j > 0: prefix_sum[i][j] = ( prefix_sum[i - 1][j] + prefix_sum[i][j - 1] - prefix_sum[i - 1][j - 1] + mat[i][j] ) for i in range(n): for j in range(n): if i - k < -1 or j - k < -1: continue if i - k == -1 and j - k == -1: res[i][j] = prefix_sum[i][j] max_sum = max(max_sum, res[i][j]) if i - k == -1 and j - k >= 0: res[i][j] = prefix_sum[i][j] - prefix_sum[i][j - k] max_sum = max(max_sum, res[i][j]) if i - k >= 0 and j - k == -1: res[i][j] = prefix_sum[i][j] - prefix_sum[i - k][j] max_sum = max(max_sum, res[i][j]) if i - k >= 0 and j - k >= 0: res[i][j] = ( prefix_sum[i][j] - prefix_sum[i - k][j] - prefix_sum[i][j - k] + prefix_sum[i - k][j - k] ) max_sum = max(max_sum, res[i][j]) return max_sum
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, n, k): def get(x, y): if x < 0 or y < 0: return 0 return mat[x][y] def calc(x, y): up = get(x - 1, y) left = get(x, y - 1) diag = get(x - 1, y - 1) return up + left - diag for i in range(n): for j in range(n): mat[i][j] += calc(i, j) res = -float("inf") for i in range(k - 1, n): for j in range(k - 1, n): cur = mat[i][j] if j != k - 1: cur -= mat[i][j - k] if i != k - 1: cur -= mat[i - k][j] if i != k - 1 and j != k - 1: cur += mat[i - k][j - k] res = max(cur, res) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER RETURN BIN_OP BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): max_sum = 0 s = [[(0) for i in range(n + 1)] for j in range(n + 1)] for i in range(n): for j in range(n): s[i + 1][j + 1] = mat[i][j] s[i + 1][j + 1] += s[i][j + 1] s[i + 1][j + 1] += s[i + 1][j] s[i + 1][j + 1] -= s[i][j] if i >= k - 1 and j >= k - 1: max_sum = max( s[i + 1][j + 1] - s[i + 1][j - k + 1] - s[i - k + 1][j + 1] + s[i - k + 1][j - k + 1], max_sum, ) return max_sum
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR IF VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): def f(temp, k, N): l = 0 h = 0 su = 0 maxi = -1000000000.0 while h < k: su += temp[h] h += 1 maxi = max(maxi, su) while h < N: su += temp[h] - temp[l] h += 1 l += 1 maxi = max(maxi, su) return maxi temp = [0] * N for i in range(k): for j in range(N): temp[j] += mat[i][j] l = 0 h = K maxi = -1000000000.0 maxi = max(maxi, f(temp, K, N)) while h < N: for i in range(N): temp[i] += mat[h][i] - mat[l][i] maxi = max(maxi, f(temp, K, N)) h += 1 l += 1 return maxi
CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR WHILE VAR VAR FOR VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
import sys class Solution: def Maximum_Sum(self, mat, N, K): sum_arr = [[(0) for _ in range(N)] for _ in range(N)] for i in range(N): for j in range(N): if i > 0 and j > 0: sum_arr[i][j] = ( sum_arr[i - 1][j] + sum_arr[i][j - 1] + mat[i][j] - sum_arr[i - 1][j - 1] ) elif i > 0: sum_arr[i][j] = sum_arr[i - 1][j] + mat[i][j] elif j > 0: sum_arr[i][j] = sum_arr[i][j - 1] + mat[i][j] else: sum_arr[i][j] = mat[i][j] max_value = 1 - sys.maxsize for i in range(N): for j in range(N): k_sum = sum_arr[i][j] if i - K >= 0: k_sum -= sum_arr[i - K][j] if j - K >= 0: k_sum -= sum_arr[i][j - K] if i - K >= 0 and j - K >= 0: k_sum += sum_arr[i - K][j - K] if k_sum > max_value: max_value = k_sum return max_value
IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR BIN_OP BIN_OP BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP NUMBER VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, n, k): def get(x, y): if x < 0 or y < 0: return 0 return mat[x][y] def calc(x, y, d, m=1): return ( get(x, y) + get(x - d, y) * m + get(x, y - d) * m - get(x - d, y - d) * m ) for i in range(n): for j in range(n): mat[i][j] = calc(i, j, 1) res = -float("inf") for i in range(k - 1, n): for j in range(k - 1, n): cur = calc(i, j, k, -1) res = max(cur, res) return res
CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NUMBER VAR NUMBER RETURN NUMBER RETURN VAR VAR VAR FUNC_DEF NUMBER RETURN BIN_OP BIN_OP BIN_OP FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR BIN_OP VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): ans = -55664651 for i in range(N): for j in range(N): if j - 1 >= 0: mat[i][j] += mat[i][j - 1] if i - 1 >= 0: mat[i][j] += mat[i - 1][j] if j - 1 >= 0 and i - 1 >= 0: mat[i][j] -= mat[i - 1][j - 1] for i in range(k - 1, N): for j in range(k - 1, N): local = mat[i][j] if i - k >= 0: local -= mat[i - k][j] if j - k >= 0: local -= mat[i][j - k] if i - k >= 0 and j - k >= 0: local += mat[i - k][j - k] ans = max(ans, local) return ans
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR VAR BIN_OP VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR IF BIN_OP VAR NUMBER NUMBER BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR VAR IF BIN_OP VAR VAR NUMBER VAR VAR VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER BIN_OP VAR VAR NUMBER VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, mat, N, K): gmax = None l1 = [0] * (len(mat[0]) + 1) sumgrid = [] for i in range(len(mat) + 1): sumgrid.append(l1[:]) for i in range(len(mat)): sum1 = 0 for j in range(len(mat[i])): sum1 = sum1 + mat[i][j] sumgrid[i + 1][j + 1] = sumgrid[i][j + 1] + sum1 for i in range(1, len(sumgrid)): for j in range(1, len(sumgrid)): idif = i - k jdif = j - k if idif >= 0 and jdif >= 0: sum1 = ( sumgrid[i][j] + sumgrid[idif][jdif] - sumgrid[idif][j] - sumgrid[i][jdif] ) if gmax == None: gmax = sum1 elif sum1 > gmax: gmax = sum1 else: pass else: pass return gmax
CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR BIN_OP LIST NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER BIN_OP VAR VAR BIN_OP VAR NUMBER VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR RETURN VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
class Solution: def Maximum_Sum(self, m, n, k): store = [[(0) for i in range(n)] for i in range(n - k + 1)] for j in range(n): s = 0 for i in range(k): s += m[i][j] store[0][j] = s for i in range(1, n - k + 1): s = s - m[i - 1][j] + m[i + k - 1][j] store[i][j] = s a = [] for i in range(n - k + 1): s = 0 for j in range(k): s += store[i][j] a.append(s) for j in range(1, n - k + 1): s = s - store[i][j - 1] + store[i][j + k - 1] a.append(s) return max(a)
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR
In Geekland there is a grid of coins of size N x N. You have to find the maximum sum of coins in any sub-grid of size K x K. Note: Coins of the negative denomination are also possible at Geekland. Example 1: Input: N = 5, K = 3 mat[[]] = {1, 1, 1, 1, 1} {2, 2, 2, 2, 2} {3, 8, 6, 7, 3} {4, 4, 4, 4, 4} {5, 5, 5, 5, 5} Output: 48 Explanation: {8, 6, 7} {4, 4, 4} {5, 5, 5} has the maximum sum Example 2: Input: N = 1, K = 1 mat[[]] = {{4}} Output: 4 Your Task: You don't need to read input or print anything. Complete the function Maximum_Sum() which takes the matrix mat[], size of Matrix N, and value K as input parameters and returns the maximum sum. Expected Time Complexity: O(N^{2}) Expected Auxiliary Space: O(N^{2}) Constraints: 1 ≀ K ≀ N ≀ 10^{3} -5*10^{5} ≀ mat[i][j] ≀ 5*10^{5}
from itertools import * class Solution: def Maximum_Sum(self, a, n, t): dp = [[(0) for _ in range(n + 1)] for _ in range(n + 1)] res = float("-inf") for i, j in product(range(1, n + 1), range(1, n + 1)): dp[i][j] += a[i - 1][j - 1] dp[i][j] += dp[i - 1][j] dp[i][j] += dp[i][j - 1] dp[i][j] -= dp[i - 1][j - 1] if i >= t and j >= t: res = max( res, dp[i][j] - dp[i - t][j] - dp[i][j - t] + dp[i - t][j - t] ) return res
CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR