description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | from sys import stdin
input = stdin.readline
T = int(input())
for _ in range(T):
a, b = map(int, input().split())
res = 0
for i in range(1, 35000):
rb = (a - i) // i
rb = min(rb, b) - i
if rb > 0:
res += rb
print(res) | ASSIGN VAR VAR 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 NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | t = int(input())
for lll in range(0, t):
x, y = map(int, input().split())
print(sum(max(0, min(y, x // i - 1) - i) for i in range(1, int(x**0.5) + 1))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | for p in [*open(0)][1:]:
a, b = map(int, p.split())
s = 0
j = 0
while j < b:
j += 1
if a <= j * j + j or b <= j:
break
s += min(a // j - 1, b) - j
print(s) | FOR VAR LIST FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | def db(*arg):
print(*arg)
T = int(input())
ans_ls = [0] * T
for t in range(T):
A, B = map(int, input().split())
ans = 0
for k in range(1, int(A**0.5) + 10):
big_b = A // k - 1
big_b = min(big_b, B)
add = max(0, big_b - k)
ans += add
ans_ls[t] = ans
for ans in ans_ls:
print(ans) | FUNC_DEF EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | import sys
input = sys.stdin.readline
t = int(input())
while t:
t -= 1
x, y = map(int, input().split())
k = 1
ans = 0
while k * (k + 2) <= x:
mx = x // k
ans += max(0, min(y + 1, mx) - (k + 2) + 1)
k += 1
print(ans) | IMPORT ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR VAR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | import sys
def main():
t = int(input())
allAns = []
def findNext(b, x, y):
add = y
change = calcChange(x, b)
while add > 0:
while b + add <= y and calcChange(x, b + add) == change:
b += add
add //= 2
return b + 1
def calcChange(x, b):
return x // (b + 1)
for _ in range(t):
x, y = readIntArr()
ans = 0
turningPoint = int((1 + x) ** 0.5)
turningPoint = min(turningPoint, y)
ans += (0 + turningPoint - 1) * turningPoint // 2
b = turningPoint + 1
while b <= y:
nextB = findNext(b, x, y)
change = calcChange(x, b)
assert change <= b - 1
assert nextB <= y + 1
ans += change * (nextB - b)
b = nextB
allAns.append(ans)
multiLineArrayPrint(allAns)
return
input = lambda: sys.stdin.readline().rstrip("\r\n")
def oneLineArrayPrint(arr):
print(" ".join([str(x) for x in arr]))
def multiLineArrayPrint(arr):
print("\n".join([str(x) for x in arr]))
def multiLineArrayOfArraysPrint(arr):
print("\n".join([" ".join([str(x) for x in y]) for y in arr]))
def readIntArr():
return [int(x) for x in input().split()]
inf = float("inf")
MOD = 10**9 + 7
main() | IMPORT FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR WHILE VAR NUMBER WHILE BIN_OP VAR VAR VAR FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR VAR VAR NUMBER RETURN BIN_OP VAR NUMBER FUNC_DEF RETURN BIN_OP VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP BIN_OP BIN_OP BIN_OP NUMBER VAR NUMBER VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR RETURN ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR FUNC_DEF EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | t = int(input())
for _ in range(t):
x, y = (int(x) for x in input().split())
ans = 0
for i in range(1, int(x**0.5) + 1):
k = min((x - i) // i - i, y - i)
if k <= 0:
break
ans += k
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | T = int(input())
for _ in range(T):
x, y = [int(z) for z in input().split()]
c = 1
result = 0
done = False
while True:
extra = max(min((x - c) // c, y) - c, 0)
result += extra
if extra == 0:
done = True
break
c += 1
print(result) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER ASSIGN VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | p = lambda: list(map(int, input().split()))
for _ in "." * p()[0]:
a, b = p()
s = j = 0
while j < b:
j += 1
if a <= j * j + j or b <= j:
break
s += min(a // j - 1, b) - j
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR BIN_OP STRING FUNC_CALL VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR NUMBER IF VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR BIN_OP FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
A pair of positive integers $(a,b)$ is called special if $\lfloor \frac{a}{b} \rfloor = a mod b$. Here, $\lfloor \frac{a}{b} \rfloor$ is the result of the integer division between $a$ and $b$, while $a mod b$ is its remainder.
You are given two integers $x$ and $y$. Find the number of special pairs $(a,b)$ such that $1\leq a \leq x$ and $1 \leq b \leq y$.
-----Input-----
The first line contains a single integer $t$ ($1 \le t \le 100$) β the number of test cases.
The only line of the description of each test case contains two integers $x$, $y$ ($1 \le x,y \le 10^9$).
-----Output-----
For each test case print the answer on a single line.
-----Examples-----
Input
9
3 4
2 100
4 3
50 3
12 4
69 420
12345 6789
123456 789
12345678 9
Output
1
0
2
3
5
141
53384
160909
36
-----Note-----
In the first test case, the only special pair is $(3, 2)$.
In the second test case, there are no special pairs.
In the third test case, there are two special pairs: $(3, 2)$ and $(4, 3)$. | t = int(input())
for _ in range(t):
def main():
x, y = map(int, input().split())
res = 0
cur = 1
while 1:
end = min(x // cur - 1, y)
if end < cur + 1:
return res
res += end - cur
cur += 1
print(main()) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER RETURN VAR VAR BIN_OP VAR VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
while t > 0:
t -= 1
n, b = [int(i) for i in input().split()]
if int(n % b) == 0:
print(n - int(n / b) + 1)
else:
print(n - int(n / b)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR FUNC_CALL VAR BIN_OP VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | def check(n, m):
ct = 0
if n >= m:
ct = int(n / m)
ct += check(int(n / m) + int(n % m), m)
return ct
def bsearch(n, m):
lo = 1
hi = n
while lo < hi:
mid = int((lo + hi) / 2)
val = mid + check(mid, m)
if val >= n:
hi = mid
else:
lo = mid + 1
return lo
t = int(input())
while t > 0:
n, m = input().split()
n = int(n)
m = int(m)
val = bsearch(n, m)
print(val)
t -= 1 | FUNC_DEF ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR BIN_OP VAR VAR FUNC_CALL VAR BIN_OP VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
while t:
n, b = list(map(int, input().split()))
ans = n - n // b
if b == 1:
print(1)
elif n % b:
print(ans)
else:
print(ans + 1)
t -= 1 | 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 BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | for _ in range(int(input())):
n, b = input().split()
n, b = int(n), int(b)
a = int(n / b)
c = n - a
if n % b == 0:
c += 1
print(c) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL 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 BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
i = 0
while t > i:
n, b = map(int, input().split())
ans = n - n // b
if b == 1:
print(1)
elif not n % b:
print(ans + 1)
else:
print(ans)
i += 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
for j in range(t):
a, b = list(map(int, input().split()))
L = 1
R = a
while R - L > 1:
mid = (L + R) // 2
cookies = mid
crumbles = 0
hours = mid
while cookies > b - 1:
crumbles = cookies % b
hours += cookies // b
cookies = cookies // b + crumbles
if hours < a:
L = mid + 1
else:
R = mid
cookies = L
crumbles = 0
hours = L
while cookies > b - 1:
crumbles = cookies % b
hours += cookies // b
cookies = cookies // b + crumbles
if hours < a:
print(R)
else:
print(L) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
for _ in range(t):
n, b = map(int, input().split())
low = 0
high = n
while low < high:
mid = (low + high) // 2
cook = mid
left = mid
while True:
book = left // b
if book == 0:
break
left = book + left % b
cook += book
if high - low < 3:
for i in range(low, high + 1):
cook = i
left = i
while True:
book = left // b
if book == 0:
break
left = book + left % b
cook += book
ans = i
if cook >= n:
break
break
if cook > n:
ans = mid
high = mid
elif cook < n:
low = mid
if cook == n:
ans = mid
high = mid
print(ans) | 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 NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR IF BIN_OP VAR VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR IF VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
for i in range(t):
n, b = list(map(int, input().split()))
a = n - n // b
if n % b == 0:
a = a + 1
print(a) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | def f():
t = int(input())
while t > 0:
t -= 1
n, b = map(int, input().split())
if b >= n:
print(n)
elif n % b == 0:
ans = (n // b - 1) * (b - 1) + b
print(ans)
else:
ans = ((n - 1) // b - 1) * (b - 1) + b + (n - 1) % b
print(ans)
f() | FUNC_DEF 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 IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP VAR VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | def resck(a, c):
cr = a
net = a
while cr >= c:
new = cr // c
net += new
cr -= new * (c - 1)
return net
for _ in range(int(input())):
n, b = map(int, input().split())
lo = 0
hi = n
while hi - lo > 1:
md = (lo + hi) // 2
if resck(md, b) < n:
lo = md
else:
hi = md
print(hi) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR 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 NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | def count(mid, b):
ans = mid
crumb = mid
while crumb >= b:
ans += crumb // b
crumb = crumb // b + crumb % b
return ans
for _ in range(int(input())):
n, b = map(int, input().split())
low = 1
high = 10**11
while low <= high:
mid = (low + high) // 2
if count(mid, b) >= n:
high = mid - 1
ans = mid
else:
low = mid + 1
print(ans) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR 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 NUMBER ASSIGN VAR BIN_OP NUMBER NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | test = int(input())
while test != 0:
test = test - 1
n, b = map(int, input().split())
pp = n // b
ans = n - pp
if n % b == 0:
ans += 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | for _ in range(int(input())):
n, k = map(int, input().split())
p = n
sum = 0
while n // (k + 1) != 0:
sum += n // (k + 1)
n -= k * (n // (k + 1))
print(p - sum) | 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 VAR ASSIGN VAR NUMBER WHILE BIN_OP VAR BIN_OP VAR NUMBER NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | def cookies(N, B):
ans = N
crumbs = N
while crumbs >= B:
ans += crumbs // B
crumbs = crumbs // B + crumbs % B
return ans
def b_search(N, B):
low = 1
high = N
ans = N
while low < high:
mid = low + (high - low) // 2
qty = cookies(mid, B)
if qty >= N:
high = mid
ans = mid
else:
low = mid + 1
return ans
T = int(input())
for t in range(T):
[N, B] = [int(i) for i in input().split()]
print(b_search(N, B)) | FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN LIST VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
for _ in range(t):
n, b = map(int, input().split())
low = 1
high = n
if b == 1:
print(1)
continue
while low < high and abs(high - low) > 4:
mid = (low + high) // 2
a = mid // b
aa = mid % b
d = mid
while a:
d += a
tt = a + aa
a = tt // b
aa = tt % b
if d >= n:
high = mid
if d < n:
low = mid + 1
for mid in range(low, high + 1):
a = mid // b
aa = mid % b
d = mid
while d < n and a:
d += a
tt = a + aa
a = tt // b
aa = tt % b
if d >= n:
tt = mid
break
print(tt) | 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 NUMBER ASSIGN VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER WHILE VAR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | def finder(k):
eaten = 0
crumbs = 0
while k > 0:
eaten += k
crumbs += k
k = crumbs // b
crumbs %= b
return eaten
def search(N):
lo = 1
hi = N
while lo < hi:
mid = (lo + hi) // 2
total = finder(mid)
if total < n:
lo = mid + 1
else:
hi = mid
print(hi)
for _ in range(int(input())):
n, b = map(int, input().split())
search(n) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
for i in range(t):
n, b = map(int, input().split())
x = n // b
if n % b == 0:
a = n - x + 1
else:
a = n - x
print(a) | 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 BIN_OP VAR VAR IF BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | t = int(input())
for i in range(t):
n, b = map(int, input().split(" "))
if n == b:
print(n)
elif n % b == 0:
print(n - n // b + 1)
else:
print(n - n // b) | 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 STRING IF VAR VAR EXPR FUNC_CALL VAR VAR IF BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR BIN_OP VAR VAR |
Chef is baking delicious cookies today! Since Chef is super hungry, he wants to eat at least $N$ cookies.
Since Chef is a messy eater, he drops a lot of crumbs. Crumbs of $B$ cookies can be put together to make a new cookie!
Given $N$ and $B$, help Chef find out the minimum number of cookies he must initially bake, $A$, to satisfy his hunger.
-----Input:-----
- First line will contain $T$, number of testcases. Then the testcases follow.
- Each testcase contains of two space separated integers $N, B$.
-----Output:-----
For each test case, print a single integer $A$, the minimum number of cookies Chef must bake initially.
-----Constraints-----
- $1 \leq T \leq 1000$
- $1 \leq N \leq 1000000000$
- $2 \leq B \leq 1000000000$
-----Sample Input 1:-----
1
3 2
-----Sample Output 1:-----
2
-----Sample Input 2:-----
1
11 2
-----Sample Output 2:-----
6
-----Explanation 2:-----
Chef initially make 6 cookies. From the crumbs, Chef makes 3 new cookies with no crumbs left over. From the crumbs of the new cookies, Chef makes 1 new cookie and have crumbs left from one cookie. From the new cookie, Chef gets more crumbs. He adds the crumbs and gets one last cookie. After eating that, there are not enough crumbs left to make a new cookie. So a total of 11 cookies are consumed! | def fun(n, b):
if n <= b:
return n
n = n - 1
a = n // b
d = b - 1
r = n % b
return 1 + a * d + r
t = int(input())
l = []
for i in range(t):
n, b = tuple(map(int, input().split()))
l.append(fun(n, b))
for i in l:
print(i) | FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR RETURN BIN_OP BIN_OP NUMBER BIN_OP VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Bob is about to take a hot bath.
There are two taps to fill the bath: a hot water tap and a cold water tap. The cold water's temperature is t1, and the hot water's temperature is t2. The cold water tap can transmit any integer number of water units per second from 0 to x1, inclusive. Similarly, the hot water tap can transmit from 0 to x2 water units per second.
If y1 water units per second flow through the first tap and y2 water units per second flow through the second tap, then the resulting bath water temperature will be:
<image>
Bob wants to open both taps so that the bath water temperature was not less than t0. However, the temperature should be as close as possible to this value. If there are several optimal variants, Bob chooses the one that lets fill the bath in the quickest way possible.
Determine how much each tap should be opened so that Bob was pleased with the result in the end.
Input
You are given five integers t1, t2, x1, x2 and t0 (1 β€ t1 β€ t0 β€ t2 β€ 106, 1 β€ x1, x2 β€ 106).
Output
Print two space-separated integers y1 and y2 (0 β€ y1 β€ x1, 0 β€ y2 β€ x2).
Examples
Input
10 70 100 100 25
Output
99 33
Input
300 500 1000 1000 300
Output
1000 0
Input
143 456 110 117 273
Output
76 54
Note
In the second sample the hot water tap shouldn't be opened, but the cold water tap should be opened at full capacity in order to fill the bath in the quickest way possible. | def gcd(a, b):
if a % b == 0:
return b
else:
return gcd(b, a % b)
[t1, t2, x1, x2, t0] = input().split(" ")
t1 = int(t1)
t2 = int(t2)
x1 = int(x1)
x2 = int(x2)
t0 = int(t0)
a = t2 - t0
b = t0 - t1
if a == 0 and b == 0:
print(x1, x2)
exit(0)
if a == 0:
y1 = 0
y2 = x2
print(y1, y2)
exit(0)
if b == 0:
y1 = x1
y2 = 0
print(y1, y2)
exit(0)
g = gcd(a, b)
a = int(a / g)
b = int(b / g)
if a <= x1 and b <= x2:
mintime = int(x1 / a)
if mintime > int(x2 / b):
mintime = int(x2 / b)
print(mintime * a, mintime * b)
exit(0)
y1 = 1
y2 = 1
miny1 = 1
miny2 = 1
minn = 99999
fg = 0
while y1 <= x1 and y2 <= x2:
if y1 / y2 < a / b:
if minn > a / b - y1 / y2:
minn = a / b - y1 / y2
miny1 = y1
miny2 = y2
fg = 1
y1 = y1 + 1
else:
y2 = y2 + 1
y1 = miny1
y2 = miny2
mintime = int(x1 / y1)
if mintime > int(x2 / y2):
mintime = int(x2 / y2)
if fg == 1:
print(mintime * y1, mintime * y2)
else:
print(0, x2) | FUNC_DEF IF BIN_OP VAR VAR NUMBER RETURN VAR RETURN FUNC_CALL VAR VAR BIN_OP VAR VAR ASSIGN LIST VAR VAR VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER 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 VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF BIN_OP VAR VAR BIN_OP VAR VAR IF VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR BIN_OP VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR FUNC_CALL VAR BIN_OP VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR NUMBER VAR |
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n Γ m multiplication table, where the element on the intersection of the i-th row and j-th column equals iΒ·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all nΒ·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
-----Input-----
The single line contains integers n, m and k (1 β€ n, m β€ 5Β·10^5;Β 1 β€ k β€ nΒ·m).
-----Output-----
Print the k-th largest number in a n Γ m multiplication table.
-----Examples-----
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
-----Note-----
A 2 Γ 3 multiplication table looks like this:
1 2 3
2 4 6 | def works(X, N, M, K):
res = 0
n = 1
div = X / M
while n < div:
res += M
n += 1
while n < N + 1:
res += (X - 1) // n
n += 1
return res
def solve():
N, M, K = [int(s) for s in input().split()]
left = 1
right = K + 1
while right - left > 1:
middle = (left + right) // 2
if works(middle, N, M, K) < K:
left = middle
else:
right = middle
return left
print(solve()) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR VAR WHILE VAR VAR VAR VAR VAR NUMBER WHILE VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR EXPR FUNC_CALL VAR FUNC_CALL VAR |
Bizon the Champion isn't just charming, he also is very smart.
While some of us were learning the multiplication table, Bizon the Champion had fun in his own manner. Bizon the Champion painted an n Γ m multiplication table, where the element on the intersection of the i-th row and j-th column equals iΒ·j (the rows and columns of the table are numbered starting from 1). Then he was asked: what number in the table is the k-th largest number? Bizon the Champion always answered correctly and immediately. Can you repeat his success?
Consider the given multiplication table. If you write out all nΒ·m numbers from the table in the non-decreasing order, then the k-th number you write out is called the k-th largest number.
-----Input-----
The single line contains integers n, m and k (1 β€ n, m β€ 5Β·10^5;Β 1 β€ k β€ nΒ·m).
-----Output-----
Print the k-th largest number in a n Γ m multiplication table.
-----Examples-----
Input
2 2 2
Output
2
Input
2 3 4
Output
3
Input
1 10 5
Output
5
-----Note-----
A 2 Γ 3 multiplication table looks like this:
1 2 3
2 4 6 | from sys import stdin
n, m, k = [int(x) for x in stdin.readline().split()]
be, en = 1, k + 1
while be < en:
mid = be + en + 1 >> 1
be1, cur = (mid + m - 1) // m, 0
for i in range(1, be1):
cur += m
for i in range(be1, n + 1):
cur += (mid - 1) // i
if cur <= k - 1:
be = mid
else:
en = mid - 1
print(be) | ASSIGN VAR VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER ASSIGN VAR VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR VAR VAR FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Concatenation of two integers is obtained as follows: First, convert both integers to strings. Then concatenate both strings into one and convert this concatenated string back to integer.
For example, concatenation of 6 and 7 is CONC(6, 7) = 67, concatenation of 123 and 45 is CONC(123, 45) = 12345.
You are given an array A consisting of N integers. You are also given two integers L and R. Find the number of pairs (i, j) such that 1 β€ i, j β€ N and L β€ CONC(A_{i}, A_{j}) β€ R
Note: Since the size of the input and output is large, please use fast input-output methods.
------ Input Format ------
- The first line will contain T, the number of test cases. Then T test cases follow.
- The first line of each test case contains three integers N, L, R.
- The second line of each test case line contains N integers A_{1}, A_{2},\dots, A_{N}.
------ Output Format ------
For each testcase, output in a single line the number of suitable pairs.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
$1 β€ L β€ R β€ 10^{15}$
$1 β€ A_{i} β€ 10^{7}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 10 52
2 5
3 58 100
4 2 3
4 100 1000
1 10 100 1000
5 28 102
3 2 1 9 10
----- Sample Output 1 ------
3
0
2
11
----- explanation 1 ------
Test case 1:
- $(i = 1, j = 1)$: $CONC(A_{1}, A_{1}) = 22$ and $10 β€ 22 β€ 52$.
- $(i = 1, j = 2)$: $CONC(A_{1}, A_{2}) = 25$ and $10 β€ 25 β€ 52$.
- $(i = 2, j = 1)$: $CONC(A_{2}, A_{1}) = 52$ and $10 β€ 52 β€ 52$.
- $(i = 2, j = 2)$: $CONC(A_{2}, A_{2}) = 55$ and $10 β€ 55$ but $ 55 \nleq 52$.
So there are three suitable pairs.
Test case 2: There is no suitable pair.
Test case 3: The suitable pairs are $(2, 1)$ and $(1, 2)$. | def main():
for _ in range(int(input())):
n, L, R = map(int, input().split())
a = sorted(map(int, input().split()))
c = 0
ls = []
rs = []
l, r = 0, n - 1
for i in range(n - 1, -1, -1):
while l < n and not L <= int(str(a[l]) + str(a[i])):
l += 1
ls.append(l)
for i in range(n):
while r >= 0 and not int(str(a[r]) + str(a[i])) <= R:
r -= 1
rs.append(r)
ls.reverse()
s = 0
for i in range(n):
x = rs[i] - ls[i] + 1
if x > 0:
s += x
print(s)
main() | FUNC_DEF 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR |
Concatenation of two integers is obtained as follows: First, convert both integers to strings. Then concatenate both strings into one and convert this concatenated string back to integer.
For example, concatenation of 6 and 7 is CONC(6, 7) = 67, concatenation of 123 and 45 is CONC(123, 45) = 12345.
You are given an array A consisting of N integers. You are also given two integers L and R. Find the number of pairs (i, j) such that 1 β€ i, j β€ N and L β€ CONC(A_{i}, A_{j}) β€ R
Note: Since the size of the input and output is large, please use fast input-output methods.
------ Input Format ------
- The first line will contain T, the number of test cases. Then T test cases follow.
- The first line of each test case contains three integers N, L, R.
- The second line of each test case line contains N integers A_{1}, A_{2},\dots, A_{N}.
------ Output Format ------
For each testcase, output in a single line the number of suitable pairs.
------ Constraints ------
$1 β€ T β€ 10^{4}$
$1 β€ N β€ 10^{5}$
$1 β€ L β€ R β€ 10^{15}$
$1 β€ A_{i} β€ 10^{7}$
- Sum of $N$ over all test cases does not exceed $10^{6}$.
----- Sample Input 1 ------
4
2 10 52
2 5
3 58 100
4 2 3
4 100 1000
1 10 100 1000
5 28 102
3 2 1 9 10
----- Sample Output 1 ------
3
0
2
11
----- explanation 1 ------
Test case 1:
- $(i = 1, j = 1)$: $CONC(A_{1}, A_{1}) = 22$ and $10 β€ 22 β€ 52$.
- $(i = 1, j = 2)$: $CONC(A_{1}, A_{2}) = 25$ and $10 β€ 25 β€ 52$.
- $(i = 2, j = 1)$: $CONC(A_{2}, A_{1}) = 52$ and $10 β€ 52 β€ 52$.
- $(i = 2, j = 2)$: $CONC(A_{2}, A_{2}) = 55$ and $10 β€ 55$ but $ 55 \nleq 52$.
So there are three suitable pairs.
Test case 2: There is no suitable pair.
Test case 3: The suitable pairs are $(2, 1)$ and $(1, 2)$. | from sys import stdin, stdout
input = stdin.readline
for _ in range(int(input())):
n, L, R = map(int, input().split())
a = list(map(int, input().split()))
a.sort()
s = 0
ls = [0] * n
rs = [0] * n
l, r = 0, n - 1
for i in range(n - 1, -1, -1):
while l < n and L > int(str(a[l]) + str(a[i])):
l += 1
ls[i] = l
for i in range(n):
while r >= 0 and int(str(a[r]) + str(a[i])) > R:
r -= 1
d = r - ls[i] + 1
if d > 0:
s += d
print(s) | ASSIGN 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 FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER WHILE VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR WHILE VAR NUMBER FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR NUMBER IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
pow2 = set()
for i in range(30):
pow2.add("".join(sorted(str(1 << i))))
def reorderedPowerOf2(self, N: int) -> bool:
return "".join(sorted(str(N))) in self.pow2 | CLASS_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR BIN_OP NUMBER VAR FUNC_DEF VAR RETURN FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
def is_power_of_two(c):
return bin(int("".join(c))).count("1") == 1
permutations = itertools.permutations(str(N))
for cand in permutations:
if cand[0] != "0" and is_power_of_two(cand):
return True
return False | CLASS_DEF FUNC_DEF VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL STRING VAR STRING NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER STRING FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
def backtrack(idx, num):
if idx == L:
if num in two_set:
return True
return False
for v in counter:
if idx == 0 and v == "0":
continue
if counter[v] > 0:
counter[v] -= 1
if backtrack(idx + 1, num + v):
return True
counter[v] += 1
return False
perm_set = {N}
ns = str(N)
counter = Counter(ns)
L = len(ns)
two_set = set()
num = 1
while len(str(num)) <= L:
if len(str(num)) == L:
two_set.add(str(num))
num *= 2
return backtrack(0, "") | CLASS_DEF FUNC_DEF VAR FUNC_DEF IF VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER FOR VAR VAR IF VAR NUMBER VAR STRING IF VAR VAR NUMBER VAR VAR NUMBER IF FUNC_CALL VAR BIN_OP VAR NUMBER BIN_OP VAR VAR RETURN NUMBER VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER RETURN FUNC_CALL VAR NUMBER STRING VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
n_hash = {}
n_str = str(N)
for c in n_str:
n_hash[c] = n_hash.get(c, 0) + 1
for i in range(0, 31):
num = pow(2, i)
if len(str(num)) > len(str(N)):
return False
n_num = {}
for c in str(num):
n_num[c] = n_num.get(c, 0) + 1
n_exists = True
for k in n_hash:
if n_num.get(k) and n_num.get(k) == n_hash.get(k):
pass
else:
n_exists = False
break
if n_exists:
return True
return False | CLASS_DEF FUNC_DEF VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER RETURN NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | from itertools import permutations
class Solution:
def reorderedPowerOf2(self, x: int) -> bool:
n = 1
pows = set()
for i in range(31):
pows.add(n)
n <<= 1
for p in permutations(str(x)):
if p[0] == "0":
continue
if int("".join(p)) in pows:
return True
return False | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING IF FUNC_CALL VAR FUNC_CALL STRING VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
maxn = 10**9
twopower = 1
twosets = set()
while twopower <= maxn:
twosets.add(twopower)
twopower *= 2
permutations = self.get_all_permutation(N)
for p in permutations:
if p[0] != "0" and int(p) in twosets:
return True
return False
def get_all_permutation(self, num):
perm = {""}
for n in str(num):
perm = {(p[:i] + n + p[i:]) for p in perm for i in range(len(p) + 1)}
return perm | CLASS_DEF FUNC_DEF VAR ASSIGN VAR BIN_OP NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR IF VAR NUMBER STRING FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR FUNC_DEF ASSIGN VAR STRING FOR VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR VAR VAR VAR VAR VAR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER RETURN VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | from itertools import permutations
class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
Nstr = str(N)
pset = set("".join(p) for p in permutations(Nstr))
bmk = 1
while len(str(bmk)) < len(Nstr):
bmk *= 2
while len(str(bmk)) == len(Nstr):
if str(bmk) in pset:
return True
bmk *= 2
return False | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR VAR NUMBER WHILE FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | import itertools
class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
if N == 1:
return True
digits = self._getDigits(N)
ones = {2, 4, 6, 8} & set(digits)
if not ones:
return False
for o in ones:
digits.remove(o)
if self._canMakePowerOf2(digits, o):
return True
digits.append(o)
return False
def _getDigits(self, N):
digits = []
while N > 0:
digits.append(N % 10)
N //= 10
return digits
def _canMakePowerOf2(self, digits, ones):
for seq in itertools.permutations(digits):
if self._isPowerOf2(seq, ones):
return True
return False
def _isPowerOf2(self, digits, ones):
n = 0
for i, d in enumerate(digits):
if i == 0 and d == 0:
return False
n = 10 * n + d
n = 10 * n + ones
return n & n - 1 == 0 | IMPORT CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP NUMBER NUMBER NUMBER NUMBER FUNC_CALL VAR VAR IF VAR RETURN NUMBER FOR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR RETURN NUMBER VAR FUNC_DEF ASSIGN VAR LIST WHILE VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR ASSIGN VAR BIN_OP BIN_OP NUMBER VAR VAR RETURN BIN_OP VAR BIN_OP VAR NUMBER NUMBER |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
sn = sorted(str(N))
for i in range(32):
pot = 1 << i
spot = sorted(str(pot))
if sn == spot:
return True
return False | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP NUMBER VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
if N == 1:
return True
elif N == 2:
return True
max_v = int("".join(sorted(list(str(N)), reverse=True)))
canidates = 2
while True:
print(canidates)
if len(str(canidates)) == len(str(N)):
canidates_memo = collections.Counter(str(canidates))
N_memo = collections.Counter(str(N))
success = True
for k in canidates_memo:
if k not in N_memo or canidates_memo[k] != N_memo[k]:
success = False
break
if success:
return True
if canidates < max_v:
canidates *= 2
else:
break
return False | CLASS_DEF FUNC_DEF VAR IF VAR NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER IF VAR RETURN NUMBER IF VAR VAR VAR NUMBER RETURN NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
main = list(map(Counter, [str(2**n) for n in range(0, 30)]))
return any(Counter(str(N)) == mape for mape in main)
1892 | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR BIN_OP NUMBER VAR VAR FUNC_CALL VAR NUMBER NUMBER RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, n: int) -> bool:
n_len = len(str(n))
n = Counter(str(n))
p = 1
while len(str(p)) <= n_len:
if len(str(p)) == n_len and Counter(str(p)) == n:
return True
p *= 2
return False | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER VAR NUMBER RETURN NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
pow_sigs = set()
twon = 1
while twon <= 10**9:
sig = [0] * 10
s = str(twon)
for c in s:
sig[int(c)] += 1
pow_sigs.add(tuple(sig))
twon *= 2
n_sig = [0] * 10
s = str(N)
for c in s:
n_sig[int(c)] += 1
if tuple(n_sig) in pow_sigs:
return True
else:
return False | CLASS_DEF FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR BIN_OP NUMBER NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR VAR FOR VAR VAR VAR FUNC_CALL VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | from itertools import permutations
class Solution:
def reorderedPowerOf2(self, N: int) -> bool:
res = False
for n in permutations(str(N)):
if n[0] == "0":
continue
n = int("".join(n))
res = max(res, not n & n - 1)
return res | CLASS_DEF FUNC_DEF VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR NUMBER STRING ASSIGN VAR FUNC_CALL VAR FUNC_CALL STRING VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR BIN_OP VAR NUMBER RETURN VAR VAR |
Starting with a positive integer N, we reorder the digits in any order (including the original order) such that the leading digit is not zero.
Return trueΒ if and only if we can do this in a way such that the resulting number is a power of 2.
Β
Example 1:
Input: 1
Output: true
Example 2:
Input: 10
Output: false
Example 3:
Input: 16
Output: true
Example 4:
Input: 24
Output: false
Example 5:
Input: 46
Output: true
Β
Note:
1 <= N <= 10^9 | class Solution:
def helper(self, level, chars):
if level == len(chars):
temp1 = "".join(chars)
if temp1[0] == "0":
return 0
if temp1 in self.been:
return self.been[temp1]
temp = bin(int(temp1)).count("1")
if temp == 1:
return 1
return 0
seen = set()
for i in range(level, len(chars)):
if chars[i] in seen:
continue
chars[level], chars[i] = chars[i], chars[level]
h = self.helper(level + 1, chars)
if h == 1:
return 1
chars[level], chars[i] = chars[i], chars[level]
seen.add(chars[i])
return 0
def reorderedPowerOf2(self, N: int) -> bool:
N = str(N)
chars = [char for char in N]
seen = set()
self.been = {}
for i in range(0, len(chars)):
if self.helper(i, chars):
return True
return False | CLASS_DEF FUNC_DEF IF VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL STRING VAR IF VAR NUMBER STRING RETURN NUMBER IF VAR VAR RETURN VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR FUNC_CALL VAR VAR STRING IF VAR NUMBER RETURN NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR RETURN NUMBER FUNC_DEF VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR DICT FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Chef has a sequence $A$ consisting of $N$ positive integers.
He is playing a game with this sequence. The goal of the game is to maximize the greatest common divisor of all elements of the sequence. Chef may perform the following operation an arbitrary number of times (including zero):
Choose a positive integer $d$ and a valid index $i$ such that $A_{i}$ is divisible by $d^{2}$.
Divide $A_{i}$ by $d^{2}$.
Choose a valid index $j$ (not necessarily different from $i$) and multiply $A_{j}$ by $d$.
Help Chef and find the maximum GCD of all integers in the sequence he could obtain if he plays optimally!
------ 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 a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum possible value of the greatest common divisor of all elements of the sequence.
------ Constraints ------
$1 β€ T β€ 5$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$ for each valid $i$
----- Sample Input 1 ------
2
2
1 25
5
27 3 15 1024 15
----- Sample Output 1 ------
1
6 | sv = [True] * 101
bpr = [2]
for ap in range(3, 101, 2):
if sv[ap]:
bpr.append(ap)
for ml in range(ap * ap, 101, ap):
sv[ml] = False
for _ in range(int(input())):
n = int(input())
z = input().split()
a = list(map(int, z[:n]))
g = a[0]
for ai in a:
ai %= g
while ai > 0:
g, ai = ai, g % ai
if g == 1:
break
if g > 1:
a = [(ai // g) for ai in a]
for ap in bpr:
mult = []
for ai in a:
m = 0
while 0 == ai % ap:
ai //= ap
m += 1
mult.append(m)
while sum(mult) > n:
mult.sort()
st = 0
en = n - 1
while mult[st] == 0 and mult[en] > 2:
mult[st] += 1
st += 1
mult[en] -= 2
en -= 1
if mult[en] <= 2:
en = n - 1
if mult[st] == 0:
break
g *= ap
mult = [(m - 1) for m in mult]
print(g) | ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER WHILE NUMBER BIN_OP VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR NUMBER VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER VAR VAR NUMBER VAR NUMBER IF VAR VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR |
Read problems statements in Mandarin chinese, Russian and Vietnamese as well.
Chef has a sequence $A$ consisting of $N$ positive integers.
He is playing a game with this sequence. The goal of the game is to maximize the greatest common divisor of all elements of the sequence. Chef may perform the following operation an arbitrary number of times (including zero):
Choose a positive integer $d$ and a valid index $i$ such that $A_{i}$ is divisible by $d^{2}$.
Divide $A_{i}$ by $d^{2}$.
Choose a valid index $j$ (not necessarily different from $i$) and multiply $A_{j}$ by $d$.
Help Chef and find the maximum GCD of all integers in the sequence he could obtain if he plays optimally!
------ 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 a single integer $N$.
The second line contains $N$ space-separated integers $A_{1}, A_{2}, \dots, A_{N}$.
------ Output ------
For each test case, print a single line containing one integer β the maximum possible value of the greatest common divisor of all elements of the sequence.
------ Constraints ------
$1 β€ T β€ 5$
$1 β€ N β€ 10^{5}$
$1 β€ A_{i} β€ 10^{6}$ for each valid $i$
----- Sample Input 1 ------
2
2
1 25
5
27 3 15 1024 15
----- Sample Output 1 ------
1
6 | def findmin(m, arr):
st = -1
en = n - 1
while en - st > 1:
md = (st + en) // 2
if arr[md] < m:
st = md
else:
en = md
return en
sv = [True] * 101
bpr = [2]
for ap in range(3, 101, 2):
if sv[ap]:
bpr.append(ap)
for ml in range(ap * ap, 101, ap):
sv[ml] = False
def facd(a):
rdic = dict()
for ap in bpr:
if 0 == a % ap:
a //= ap
rdic[ap] = 1
while 0 == a % ap:
a //= ap
rdic[ap] += 1
if a < ap * ap:
break
if a > 1:
rdic[a] = 1
return rdic
for _ in range(int(input())):
n = int(input())
z = input().split()
a = list(map(int, z[:n]))
g = a[0]
for ai in a:
ai %= g
while ai > 0:
g, ai = ai, g % ai
if g == 1:
break
if g > 1:
a = [(ai // g) for ai in a]
faclib = [facd(ai) for ai in a]
for ap in bpr:
mult = []
for adic in faclib:
m = 0
if ap in adic:
m = adic[ap]
mult.append(m)
if sum(mult) > n:
mult.sort()
thr = 1
nbt = 0
while True:
nbt += findmin(thr, mult)
nsup = findmin(thr + 2, mult)
surp = (sum(mult[nsup:n]) - thr * (n - nsup)) // 2
if surp < nbt:
break
g *= ap
thr += 1
print(g) | FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR LIST NUMBER FOR VAR FUNC_CALL VAR NUMBER NUMBER NUMBER IF VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR VAR NUMBER VAR ASSIGN VAR VAR NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF NUMBER BIN_OP VAR VAR VAR VAR ASSIGN VAR VAR NUMBER WHILE NUMBER BIN_OP VAR VAR VAR VAR VAR VAR NUMBER IF VAR BIN_OP VAR VAR IF VAR NUMBER ASSIGN VAR VAR NUMBER RETURN VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR NUMBER FOR VAR VAR VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR IF VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR ASSIGN VAR LIST FOR VAR VAR ASSIGN VAR NUMBER IF VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE NUMBER VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def isPalindrome(self, s):
i = 0
j = len(s) - 1
while i < j:
if s[i] != s[j]:
return False
i += 1
j -= 1
return 1
def palindromepair(self, n, arr):
mp = {}
for i in range(n):
s = arr[i]
s = s[::-1]
mp[s] = i
for i in range(n):
for j in range(len(arr[i])):
s1 = arr[i][:j]
s2 = arr[i][j:]
if s1 in mp and self.isPalindrome(s2) and mp[s1] != i:
return 1
if s2 in mp and self.isPalindrome(s1) and mp[s2] != i:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
d = []
for i in arr:
if i[::-1] in d:
return 1
d.append(i)
st1 = set(arr)
st2 = set()
for i in st1:
st2.add(i[::-1])
for s in st1:
n = len(s)
for i in range(1, n):
if s[:i] == s[:i][::-1] and s[i:] in st2:
return 1
if s[n - i :] == s[n - i :][::-1] and s[: n - i] in st2:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST FOR VAR VAR IF VAR NUMBER VAR RETURN NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR VAR VAR VAR NUMBER VAR VAR VAR RETURN NUMBER IF VAR BIN_OP VAR VAR VAR BIN_OP VAR VAR NUMBER VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
def is_palindrome(s):
return s == s[::-1]
words = {}
for i in range(N):
words[arr[i]] = i
for i in range(N):
for j in range(len(arr[i]) + 1):
prefix, suffix = arr[i][:j], arr[i][j:]
if is_palindrome(prefix):
if suffix[::-1] in words and words[suffix[::-1]] != i:
return 1
if is_palindrome(suffix):
if prefix[::-1] in words and words[prefix[::-1]] != i:
return 1
return 0 | CLASS_DEF FUNC_DEF FUNC_DEF RETURN VAR VAR NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NUMBER IF FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | import time
class Solution:
def __init__(self):
self.star_time = time.time()
def palindromepair(self, N, arr):
for i in range(len(arr)):
for e in range(N):
if N > 600 and time.time() - self.star_time > 8:
return 0
j = N - 1 - e
if i != j:
if arr[i] + arr[j] == (arr[i] + arr[j])[::-1]:
return 1
return 0 | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR IF VAR VAR IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
words = {word: i for i, word in enumerate(arr)}
for i, word in enumerate(arr):
if word[::-1] in words and words[word[::-1]] != i:
return 1
if word[::-1][0:-1] in words and words[word[::-1][0:-1]] != i:
return 1
if word[0:-1][::-1] in words and words[word[0:-1][::-1]] != i:
return 1
for j in range(len(word) + 1):
if word[:j] == word[:j][::-1]:
if word[j:][::-1] in words and words[word[j:][::-1]] != i:
return 1
if word[j:] == word[j:][::-1]:
if word[:j][::-1] in words and words[word[:j][::-1]] != i:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NUMBER IF VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR RETURN NUMBER IF VAR NUMBER NUMBER NUMBER VAR VAR VAR NUMBER NUMBER NUMBER VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class TrieNode:
def __init__(self):
self.children = [None] * 26
self.eos = 0
def insert(root, key):
curr = root
for char in key:
index = ord(char) - 97
if not curr.children[index]:
newNode = TrieNode()
curr.children[index] = newNode
curr = curr.children[index]
curr.eos += 1
def search(root, key):
curr = root
for char in key:
index = ord(char) - 97
if not curr.children[index]:
return 0
curr = curr.children[index]
return curr.eos
class Solution:
def palindromepair(self, N, words):
t = TrieNode()
for word in words:
insert(t, word)
for word in words:
for i in range(len(word)):
rightHalf = word[: i + 1][::-1]
newW = word + rightHalf
if rightHalf == word:
if search(t, rightHalf) >= 2:
return 1
elif newW == newW[::-1]:
if search(t, rightHalf):
return 1
wordRev = word[::-1]
for i in range(len(word)):
leftHalf = wordRev[: i + 1]
newW = leftHalf + word
if leftHalf == word:
if search(t, word) >= 2:
return 1
elif newW == newW[::-1]:
if search(t, leftHalf):
return 1
return 0
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
arr = []
for i in range(N):
s = input()
arr.append(s)
ob = Solution()
print(ob.palindromepair(N, arr)) | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR VAR IF VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER 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 ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class TrieNode:
def __init__(self):
self.links = [None] * 26
self.id = -1
self.pos = []
self.isEnd = False
class Trie:
def __init__(self):
self.root = TrieNode()
def isPalindrome(self, s, i, n):
while i < n:
if s[i] != s[n]:
return False
i += 1
n -= 1
return True
def insert(self, word, id):
node = self.root
for i in range(len(word) - 1, -1, -1):
x = ord(word[i]) - ord("a")
if not node.links[x]:
node.links[x] = TrieNode()
if self.isPalindrome(word, 0, i):
node.pos.append(id)
node = node.links[x]
node.id = id
node.pos.append(id)
node.isEnd = True
def Search(self, word, id, result):
node = self.root
for i in range(len(word)):
x = ord(word[i]) - ord("a")
if (
node.id >= 0
and node.id != id
and self.isPalindrome(word, i, len(word) - 1)
):
result.append((id, node.id))
if not node.links[x]:
return
node = node.links[x]
for i in node.pos:
if i == id:
continue
result.append((id, i))
class Solution:
def palindromepair(self, N, arr):
TR = Trie()
for i in range(N):
TR.insert(arr[i], i)
result = []
for i in range(N):
TR.Search(arr[i], i, result)
if len(result) > 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR IF VAR VAR RETURN ASSIGN VAR VAR VAR FOR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Node:
def __init__(self):
self.links = [None for _ in range(26)]
self.flag = False
def is_present(self, ch):
return self.links[ord(ch) - ord("a")] is not None
def insert(self, ch):
self.links[ord(ch) - ord("a")] = Node()
def next(self, ch):
return self.links[ord(ch) - ord("a")]
def set_end(self):
self.flag = True
def end(self):
return self.flag
class Trie:
def __init__(self):
self.root = Node()
def insert(self, word: str) -> None:
node = self.root
for v in word:
if not node.is_present(v):
node.insert(v)
node = node.next(v)
node.set_end()
def search(self, word: str) -> bool:
node = self.root
for v in word:
if not node.is_present(v):
return False
node = node.next(v)
return node.end()
class Solution:
def palindromepair(self, N, arr):
if N == 1:
return 0
if len(set(arr)) != N:
return 1
trie = Trie()
for v in arr:
trie.insert(v)
for word in arr:
s = ""
for i in range(len(word)):
s += word[i]
new_word = word + s[::-1]
if (
new_word == new_word[::-1]
and trie.search(s[::-1])
and s[::-1] != word
):
return 1
s = ""
for i in range(len(word) - 1, -1, -1):
s += word[i]
new_word = s + word
if new_word == new_word[::-1] and trie.search(s) and s != word:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR NONE VAR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER FUNC_DEF RETURN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING NONE FUNC_DEF ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR FUNC_DEF RETURN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER FUNC_DEF RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF VAR ASSIGN VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NONE FUNC_DEF VAR ASSIGN VAR VAR FOR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR FOR VAR VAR ASSIGN VAR STRING FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER VAR RETURN NUMBER ASSIGN VAR STRING FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR IF VAR VAR NUMBER FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
rev_hashMap = {w[::-1]: i for i, w in enumerate(arr)}
for ind, w in enumerate(arr):
for i in range(len(w)):
l, r = w[: i + 1], w[i + 1 :]
if l == l[::-1] and r in rev_hashMap and rev_hashMap[r] != ind:
return 1
if r == r[::-1] and l in rev_hashMap and rev_hashMap[l] != ind:
return 1
return 0
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
arr = []
for i in range(N):
s = input()
arr.append(s)
ob = Solution()
print(ob.palindromepair(N, arr)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER 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 ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
index_dict = {word: i for i, word in enumerate(arr)}
for i, word in enumerate(arr):
for j in range(len(word) + 1):
left_side = word[:j]
right_side = word[j:]
if left_side == left_side[::-1]:
if (
right_side[::-1] in index_dict
and index_dict[right_side[::-1]] != i
):
return 1
if right_side == right_side[::-1]:
if (
left_side[::-1] in index_dict
and index_dict[left_side[::-1]] != i
):
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | def is_palindrome(string):
return string == string[::-1]
class Trie:
def __init__(self):
self.root = {}
def insert(self, word, index):
curr_node = self.root
n = len(word)
for i in range(n - 1, -1, -1):
char = word[i]
if char not in curr_node:
curr_node[char] = {}
if is_palindrome(word[: i + 1]):
curr_node["pal"] = True
curr_node = curr_node[char]
if "$" not in curr_node:
curr_node["$"] = set()
curr_node["$"].add(index)
def search(self, word, index):
curr_node = self.root
n = len(word)
for i in range(n):
char = word[i]
if "$" in curr_node and is_palindrome(word[i:]):
return True
if char not in curr_node:
return False
curr_node = curr_node[char]
if "pal" in curr_node:
return True
elif "$" not in curr_node:
return False
elif len(curr_node["$"]) > 1:
return True
else:
leaf_index = curr_node["$"].pop()
if leaf_index != index:
return True
else:
return False
class Solution:
def is_palindrome(self, string):
return string == string[::-1]
def palindromepair(self, N, arr):
if N <= 1:
return 0
trie = Trie()
for i in range(N):
trie.insert(arr[i], i)
for i in range(N):
if trie.search(arr[i], i):
return 1
return 0 | FUNC_DEF RETURN VAR VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR DICT FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR DICT IF FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR STRING NUMBER ASSIGN VAR VAR VAR IF STRING VAR ASSIGN VAR STRING FUNC_CALL VAR EXPR FUNC_CALL VAR STRING VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF STRING VAR FUNC_CALL VAR VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR IF STRING VAR RETURN NUMBER IF STRING VAR RETURN NUMBER IF FUNC_CALL VAR VAR STRING NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR STRING IF VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF RETURN VAR VAR NUMBER FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, words):
ans = set()
index = {word: i for i, word in enumerate(words)}
for i, word in enumerate(words):
for j in range(len(word) + 1):
left = word[:j]
right = word[j:]
if left == left[::-1]:
if right[::-1] in index and index[right[::-1]] != i:
ans.add((index[right[::-1]], i))
if right == right[::-1]:
if left[::-1] in index and index[left[::-1]] != i:
ans.add((i, index[left[::-1]]))
if len(ans) > 0:
return 1
else:
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class StringTrie:
class Node:
def __init__(self):
self.parent = None
self.childmap = {}
self.terminal = None
self.tcount = 0
def terminal_iter(self):
if self.terminal:
yield self.terminal
for c, child in sorted(self.childmap.items()):
for s in child.terminal_iter():
yield s
def __init__(self):
self.root = StringTrie.Node()
def add(self, s):
node = self.root
for c in s:
new_node = node.childmap.get(c, None)
if new_node is None:
new_node = StringTrie.Node()
node.childmap[c] = new_node
node = new_node
node.terminal = s
node.tcount += 1
def match(self, s):
node = self.root
length = 0
for c in s:
new_node = node.childmap.get(c, None)
if new_node is None:
break
length += 1
node = new_node
return length, node
class Solution:
def palindromepair(self, N, arr):
the_trie = StringTrie()
for s in arr:
the_trie.add(s)
result = 0
for s in arr:
srev = s[::-1]
length, snode = the_trie.match(s[::-1])
if length == len(s):
for tstr in snode.terminal_iter():
if tstr == s and snode.tcount < 2:
continue
tail = tstr[length:]
if tail == tail[::-1]:
return 1
elif length > 0:
node = snode
while node:
tstr = node.terminal
if tstr:
tail = srev[length:]
if tail == tail[::-1]:
return 1
node = node.parent
length -= 1
return 0 | CLASS_DEF CLASS_DEF FUNC_DEF ASSIGN VAR NONE ASSIGN VAR DICT ASSIGN VAR NONE ASSIGN VAR NUMBER FUNC_DEF IF VAR EXPR VAR FOR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR EXPR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NONE IF VAR NONE VAR NUMBER ASSIGN VAR VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
reverse = dict()
for index, word in enumerate(arr):
reverse[word[::-1]] = index
for index, word in enumerate(arr):
for i in range(len(word)):
prefix, suffix = word[:i], word[i:]
if (
suffix in reverse
and self.isPalindrome(prefix)
and reverse[suffix] != index
):
return 1
if (
prefix in reverse
and self.isPalindrome(suffix)
and reverse[prefix] != index
):
return 1
return 0
def isPalindrome(self, word):
return word == word[::-1] | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER FUNC_DEF RETURN VAR VAR NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
if N <= 1:
return 0
d = {}
for i in range(N):
d[arr[i][::-1]] = i
for i in range(N):
x = arr[i]
for j in range(len(x) + 1):
pre = x[:j]
suf = x[j:]
if pre == pre[::-1]:
if suf in d:
if d[suf] != i:
return 1
if suf == suf[::-1]:
if pre in d:
if d[pre] != i:
return 1
return 0 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class TrieNode:
def __init__(self):
self.children = [None] * 26
self.isEndOfWord = False
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, root, word):
def util(root, index, word):
if len(word) == index:
root.isEndOfWord = True
return
char = word[index]
temp = ord(char) - ord("a")
if root.children[temp] != None:
son = root.children[temp]
else:
root.children[temp] = TrieNode()
son = root.children[temp]
util(son, index + 1, word)
util(root, 0, word)
class Solution:
def palindromepair(self, N, arr):
def check(root, st):
i = 0
m = len(st)
while i < m and root != None:
char = st[i]
flag = False
for k in range(26):
if root.children[k] != None:
flag = True
root = root.children[ord(char) - ord("a")]
if root == None and flag == True:
return False
i += 1
if i < m:
temp = st[i - 1 :]
elif root == None and i == m:
temp = st[i:]
elif root != None:
temp = ""
while root != None:
flag = False
for k in range(26):
if root.children[k] != None:
flag = True
temp += chr(ord("a") + k)
root = root.children[k]
break
if flag == False:
break
if len(temp) != 0:
for k in range(0, len(temp) // 2):
if temp[k] != temp[len(temp) - k - 1]:
return False
return True
if N <= 1:
return 0
d = {}
for i in range(N):
d[arr[i][::-1]] = i
for i in range(N):
x = arr[i]
for j in range(len(x) + 1):
pre = x[:j]
suf = x[j:]
if pre == pre[::-1]:
if suf in d:
if d[suf] != i:
return 1
if suf == suf[::-1]:
if pre in d:
if d[pre] != i:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF FUNC_DEF IF FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER RETURN ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NONE ASSIGN VAR NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING IF VAR NONE VAR NUMBER RETURN NUMBER VAR NUMBER IF VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR NONE VAR VAR ASSIGN VAR VAR VAR IF VAR NONE ASSIGN VAR STRING WHILE VAR NONE ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER IF VAR VAR NONE ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR STRING VAR ASSIGN VAR VAR VAR IF VAR NUMBER IF FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR RETURN NUMBER IF VAR VAR NUMBER IF VAR VAR IF VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, n, arr):
if n == 1:
return 0
d = {}
for i in range(n):
d[arr[i]] = i
for i in range(n):
x = arr[i][::-1]
if x in d and d[x] != i:
return 1
for i in range(n):
l = len(arr[i])
for j in range(l - 1):
left = arr[i][0 : j + 1]
right = arr[i][j + 1 : len(arr[i])]
if left == left[::-1] and right[::-1] in d:
return 1
if right == right[::-1] and left[::-1] in d:
return 1
return 0 | CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR IF VAR VAR NUMBER VAR NUMBER VAR RETURN NUMBER IF VAR VAR NUMBER VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
d = {}
for i, j in enumerate(arr):
d[j[::-1]] = i
for i, j in enumerate(arr):
for k in range(len(j)):
left = j[: k + 1]
right = j[k + 1 :]
if left == left[::-1] and right in d and d[right] != i:
return 1
if right == right[::-1] and left in d and d[left] != i:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def __init__(self):
self.dp = []
self.mp = {}
def createPalindromeDp(self, str):
n = len(str)
dp = [([False] * n) for i in range(n)]
for l in range(1, n + 1):
for i in range(0, n - l + 1):
j = i + l - 1
if l == 1:
dp[i][i] = True
elif l == 2:
if str[i] == str[j]:
dp[i][j] = True
elif str[i] == str[j] and dp[i + 1][j - 1] == True:
dp[i][j] = True
return dp
def palindromepair(self, N, arr):
self.dp = []
for str in arr:
self.dp.append(self.createPalindromeDp(str))
if str not in self.mp:
self.mp[str] = 0
self.mp[str] += 1
for index in range(0, len(arr)):
str = arr[index]
l = len(str)
palin = self.dp[index]
for i in range(0, l):
if i == l - 1:
if palin[0][l - 1]:
if (
str[0:l][::-1] in self.mp
and self.mp[str[0 : i + 1][::-1]] > 1
):
return 1
elif str[0:l][::-1] in self.mp:
return 1
else:
if palin[i + 1][l - 1] and str[0 : i + 1][::-1] in self.mp:
return 1
if palin[0][i] and str[i + 1 : l][::-1] in self.mp:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR DICT FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF VAR BIN_OP VAR NUMBER IF VAR NUMBER BIN_OP VAR NUMBER IF VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER BIN_OP VAR NUMBER NUMBER NUMBER RETURN NUMBER IF VAR NUMBER VAR NUMBER VAR RETURN NUMBER IF VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR NUMBER BIN_OP VAR NUMBER NUMBER VAR RETURN NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
ans = []
hs = {wrd[::-1]: i for i, wrd in enumerate(arr)}
for idx, word in enumerate(arr):
for i in range(len(word)):
left, right = word[0 : i + 1], word[i + 1 :]
if (
not len(left) == 0
and left == left[::-1]
and right in hs
and hs[right] != idx
):
ans.append([hs[right], idx])
if right == right[::-1] and left in hs and hs[left] != idx:
ans.append([hs[left], idx])
if len(ans) > 0:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
def isPalindrome(stg, i, j):
while i <= j:
if stg[i] != stg[j]:
return False
i += 1
j -= 1
return True
dic = {}
for i in range(N):
dic[arr[i][::-1]] = i
for idx in range(N):
stg = arr[idx]
if stg in dic and dic[stg] != idx:
return 1
j = len(stg) - 1
for i in range(1, j + 1):
if isPalindrome(stg, i, j) and stg[:i] in dic:
return 1
for idx in range(j - 1, -1, -1):
if isPalindrome(stg, 0, idx) and stg[idx + 1 :] in dic:
return 1
return 0 | CLASS_DEF FUNC_DEF FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
ans = set()
index = {word: i for i, word in enumerate(arr)}
for i, word in enumerate(arr):
for j in range(len(word) + 1):
left = word[:j]
right = word[j:]
if left == left[::-1]:
if right[::-1] in index and index[right[::-1]] != i:
ans.add((index[right[::-1]], i))
if len(ans) > 0:
return 1
if right == right[::-1]:
if left[::-1] in index and index[left[::-1]] != i:
ans.add((i, index[left[::-1]]))
if len(ans) > 0:
return 1
return 0
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
arr = []
for i in range(N):
s = input()
arr.append(s)
ob = Solution()
print(ob.palindromepair(N, arr)) | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER IF VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER RETURN NUMBER RETURN NUMBER 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 ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def ispal(self, word, i, j):
j -= 1
while i < j:
if word[i] != word[j]:
return False
i += 1
j -= 1
return True
def palindromepair(self, N, arr):
mp = dict()
for i in range(N):
mp[arr[i][::-1]] = i
for i in range(N):
if arr[i] in mp and i != mp[arr[i]]:
return 1
for word in arr:
n = len(word)
for j in range(1, n):
if self.ispal(word, 0, j) and word[j:] in mp:
return 1
if self.ispal(word, n - j, n) and word[: n - j] in mp:
return 1
return 0 | CLASS_DEF FUNC_DEF VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR VAR VAR VAR VAR RETURN NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
hashmap = {}
for i, val in enumerate(arr):
hashmap[val] = i
for i, word in enumerate(arr):
for j in range(len(word)):
prefix = word[:j]
suffix = word[j:]
if (
prefix[::-1] in hashmap
and hashmap[prefix[::-1]] != i
and suffix == suffix[::-1]
):
return 1
if (
suffix[::-1] in hashmap
and hashmap[suffix[::-1]] != i
and prefix == prefix[::-1]
):
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER IF VAR NUMBER VAR VAR VAR NUMBER VAR VAR VAR NUMBER RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class TrieNode:
def __init__(self):
self.children = {}
self.end = False
self.ind = -1
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, key, ind):
curr = self.root
for i in key:
if i not in curr.children:
curr.children[i] = TrieNode()
curr = curr.children[i]
curr.end = True
curr.ind = ind
def hey(self, curr, pre, cond):
if not curr:
return False
if curr.end and not cond:
if pre == pre[::-1] and not cond:
return True
for i in curr.children:
if self.hey(curr.children[i], pre + i, False):
return True
return False
def search(self, key, index):
curr = self.root
for ind, i in enumerate(key):
if curr.end:
temp = key[ind:]
if temp == temp[::-1] and curr.ind != index:
return True
if i not in curr.children:
return False
curr = curr.children[i]
cond = False
if curr.end:
if curr.ind == index:
cond = True
return self.hey(curr, "", cond)
class Solution:
def palindromepair(self, N, arr):
if N == 1:
return 0
tree = Trie()
ans = 0
for ind, i in enumerate(arr):
tree.insert(i[::-1], ind)
for ind, i in enumerate(arr):
if tree.search(i, ind):
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_DEF IF VAR RETURN NUMBER IF VAR VAR IF VAR VAR NUMBER VAR RETURN NUMBER FOR VAR VAR IF FUNC_CALL VAR VAR VAR BIN_OP VAR VAR NUMBER RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR FOR VAR VAR FUNC_CALL VAR VAR IF VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR RETURN NUMBER IF VAR VAR RETURN NUMBER ASSIGN VAR VAR VAR ASSIGN VAR NUMBER IF VAR IF VAR VAR ASSIGN VAR NUMBER RETURN FUNC_CALL VAR VAR STRING VAR CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER VAR FOR VAR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | def palindrome(S, l, r):
while l < r:
if S[l] != S[r]:
return 0
l += 1
r -= 1
return 1
class TrieNode:
def __init__(self):
self.chars = [None] * 26
self.idx = -1
self.indices = []
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, s, ix):
curr = self.root
for i in range(len(s) - 1, -1, -1):
if palindrome(s, 0, i):
curr.indices.append(ix)
o = ord(s[i]) - ord("a")
if curr.chars[o] is None:
curr.chars[o] = TrieNode()
curr = curr.chars[o]
curr.idx = ix
curr.indices.append(ix)
def search(self, s, ix):
curr = self.root
n = len(s)
for i in range(n):
if curr.idx >= 0 and curr.idx != ix and palindrome(s, i, n - 1):
return 1
o = ord(s[i]) - ord("a")
if curr.chars[o] is None:
return 0
curr = curr.chars[o]
if curr:
for idx in curr.indices:
if ix != idx:
return 1
return 0
class Solution:
def palindromepair(self, N, A):
ans = 0
trie = Trie()
for i in range(N):
trie.insert(A[i], i)
for i in range(N):
ans |= trie.search(A[i], i)
return ans | FUNC_DEF WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER ASSIGN VAR LIST CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR NUMBER NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR NONE ASSIGN VAR VAR FUNC_CALL VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR VAR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR NONE RETURN NUMBER ASSIGN VAR VAR VAR IF VAR FOR VAR VAR IF VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR VAR RETURN VAR |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
reverse = {arr[i][::-1]: i for i in range(len(arr))}
for i in range(0, N):
for j in range(len(arr[i])):
left, right = arr[i][0 : j + 1], arr[i][j + 1 :]
if left == left[::-1] and right in reverse and reverse[right] != i:
return 1
if right == right[::-1] and left in reverse and reverse[left] != i:
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR NUMBER VAR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR NUMBER BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER IF VAR VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def palindromepair(self, N, arr):
d = {}
for i in range(N):
if arr[i] in d:
if arr[i] == arr[i][::-1]:
return 1
d[arr[i]] = i
def isPalindrome(string):
n = len(string)
if n == 1 or n == 0:
return True
l, h = 0, n - 1
while l <= h:
if string[l] != string[h]:
return False
l += 1
h -= 1
return True
for word in arr:
for i in range(len(word)):
left = word[0:i]
right = word[i:]
if isPalindrome(left):
temp = right[::-1]
if temp in d and d[word] != d[temp]:
return 1
if isPalindrome(right):
temp = left[::-1]
if left[::-1] in d and d[word] != d[temp]:
return 1
return 0
if __name__ == "__main__":
t = int(input())
for _ in range(t):
N = int(input())
arr = []
for i in range(N):
s = input()
arr.append(s)
ob = Solution()
print(ob.palindromepair(N, arr)) | CLASS_DEF FUNC_DEF ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR IF VAR VAR VAR VAR NUMBER RETURN NUMBER ASSIGN VAR VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER RETURN NUMBER ASSIGN VAR VAR NUMBER BIN_OP VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FOR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR ASSIGN VAR VAR VAR IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR RETURN NUMBER IF FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER IF VAR NUMBER VAR VAR VAR VAR VAR RETURN NUMBER RETURN NUMBER 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 ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class TrieNode:
def __init__(self):
self.links = [None] * 26
self.endOfWord = False
self.ind = set()
class Trie:
def __init__(self):
self.root = TrieNode()
def isPalindrome(self, key):
n = len(key)
for i in range(n // 2):
if key[i] != key[n - 1 - i]:
return 0
return 1
def insert(self, key, ind):
curr = self.root
n = len(key)
for i in range(n):
ele = key[i]
if not curr.links[ord(ele) - ord("a")]:
curr.links[ord(ele) - ord("a")] = TrieNode()
curr = curr.links[ord(ele) - ord("a")]
curr.endOfWord = True
curr.ind.add(ind)
def search(self, key, ind):
ll = len(key)
curr = self.root
for j in range(ll - 1, -1, -1):
if not curr.links[ord(key[j]) - ord("a")]:
return False
curr = curr.links[ord(key[j]) - ord("a")]
if (
curr.endOfWord
and (ind not in curr.ind or len(curr.ind) > 1)
and self.isPalindrome(key[:j])
):
return True
return False
class Solution:
def palindromepair(self, N, arr):
if N == 1:
return 0
t = Trie()
for i in range(N):
t.insert(arr[i], i)
for i in range(N):
if t.search(arr[i], i):
return 1
s = Trie()
for i in range(N):
s.insert(arr[i][::-1], i)
for i in range(N):
if s.search(arr[i][::-1], i):
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NONE NUMBER ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER IF VAR VAR VAR BIN_OP BIN_OP VAR NUMBER VAR RETURN NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING FUNC_CALL VAR ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR FUNC_CALL VAR STRING ASSIGN VAR NUMBER EXPR FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER IF VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING RETURN NUMBER ASSIGN VAR VAR BIN_OP FUNC_CALL VAR VAR VAR FUNC_CALL VAR STRING IF VAR VAR VAR FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR VAR RETURN NUMBER RETURN NUMBER CLASS_DEF FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR VAR RETURN NUMBER ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR VAR NUMBER VAR RETURN NUMBER RETURN NUMBER |
Given an array of strings arr[] of size N, find if there exists 2 strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.
Example 1:
Input:
N = 6
arr[] = {"geekf", "geeks", "or","keeg", "abc",
"bc"}
Output: 1
Explanation: There is a pair "geekf"
and "keeg".
Example 2:
Input:
N = 5
arr[] = {"abc", "xyxcba", "geekst", "or", "bc"}
Output: 1
Explanation: There is a pair "abc"
and "xyxcba".
Your Task:
You don't need to read input or print anything. Your task is to complete the function palindromepair() which takes the array arr[], its size N and returns true if palindrome pair exists and returns false otherwise.
The driver code itself prints 1 if returned value is true and prints 0 if returned value is false.
Expected Time Complexity: O(N*l^{2}) where l = length of longest string in arr[]
Expected Auxiliary Space: O(N*l^{2}) where l = length of longest string in arr[]
Constraints:
1 β€ N β€ 2*10^{4}
1 β€ |arr[i]| β€ 10 | class Solution:
def ispalin(self, word):
p1, p2 = 0, len(word) - 1
while p1 < p2:
if word[p1] != word[p2]:
return False
p1 += 1
p2 -= 1
return True
def palindromepair(self, n, words):
tmp = []
ans = []
d = {}
for i in range(n):
if words[i] != "" and self.ispalin(words[i]):
tmp.append(i)
d[words[i][::-1]] = i
for i in range(n):
if words[i] == "":
for j in tmp:
ans.append([i, j])
for j in range(len(words[i])):
s = words[i][:j]
t = words[i][j:]
if s in d and self.ispalin(t) and d[s] != i:
ans.append([i, d[s]])
if t in d and self.ispalin(s) and d[t] != i:
ans.append([d[t], i])
if len(ans):
return 1
return 0 | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE VAR VAR IF VAR VAR VAR VAR RETURN NUMBER VAR NUMBER VAR NUMBER RETURN NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR DICT FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR STRING FOR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF VAR VAR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR IF FUNC_CALL VAR VAR RETURN NUMBER RETURN NUMBER |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | l = int(input())
a = [(int(x) - 1) for x in input().split()]
maxh = max(a)
h = [(0) for x in range(maxh + 1)]
result = 0
prev = maxh + 1
if len(set(a)) > 20000:
for curr in a:
if curr > prev:
h[prev:curr] = [(0) for _ in range(curr - prev)]
result += h[curr]
h[curr] += 1
prev = curr
else:
nonZero = set()
for curr in a:
if curr > prev:
for i in list(nonZero):
if prev <= i < curr:
nonZero.remove(i)
h[i] = 0
result += h[curr]
h[curr] += 1
nonZero.add(curr)
prev = curr
print(result * 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR NUMBER VAR FUNC_CALL VAR BIN_OP VAR VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER VAR VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | N = int(input().strip())
heights = [int(h) for h in input().strip().split()]
count = 0
starts = []
for i in range(len(heights)):
while len(starts) > 0 and heights[i] > starts[-1][0]:
starts.pop()
if len(starts) > 0 and heights[i] == starts[-1][0]:
count += starts[-1][1]
starts[-1][1] += 1
else:
starts.append([heights[i], 1])
print(2 * count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | a = [(0, 0)] * int(input())
c, l = 0, 1
for i in map(int, input().split()):
if a[0][0] < i:
a[0] = i, 1
l = 1
else:
mid, step = 0, 1 << l.bit_length()
while step:
step >>= 1
if mid + step < l and a[mid + step][0] >= i:
mid += step
l = mid + 1
if a[l - 1][0] == i:
c += a[l - 1][1]
a[l - 1] = a[l - 1][0], a[l - 1][1] + 1
else:
a[l] = i, 1
l += 1
print(2 * c) | ASSIGN VAR BIN_OP LIST NUMBER NUMBER FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER NUMBER VAR ASSIGN VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR NUMBER BIN_OP NUMBER FUNC_CALL VAR WHILE VAR VAR NUMBER IF BIN_OP VAR VAR VAR VAR BIN_OP VAR VAR NUMBER VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR BIN_OP VAR NUMBER NUMBER VAR VAR VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER BIN_OP VAR BIN_OP VAR NUMBER NUMBER NUMBER ASSIGN VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | class Stack(object):
def __init__(self):
self.data = []
def push(self, data):
self.data.append(data)
def pop(self):
if not len(self.data):
return None
return self.data.pop()
def peek(self):
if not len(self.data):
return None
return self.data[-1]
def size(self):
return len(self.data)
def horizontal_routes(heights):
stack = Stack()
count = 0
for height in heights:
top_of_stack = stack.peek()
while top_of_stack and top_of_stack[0] < height:
stack.pop()
top_of_stack = stack.peek()
if top_of_stack:
if top_of_stack[0] == height:
top = stack.pop()
count += top[1]
updated = height, top[1] + 1
stack.push(updated)
else:
stack.push((height, 1))
else:
stack.push((height, 1))
return count * 2
number_of_heights = int(input())
heights = (int(x) for x in input().split())
print(horizontal_routes(heights)) | CLASS_DEF VAR FUNC_DEF ASSIGN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN NONE RETURN FUNC_CALL VAR FUNC_DEF IF FUNC_CALL VAR VAR RETURN NONE RETURN VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR IF VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | N = int(input())
heights = [int(x) for x in input().split()]
hstack = []
ret = 0
for hcurr in heights:
dup = 0
while hstack and hstack[-1][0] <= hcurr:
hprev, duprev = hstack.pop()
if hprev == hcurr:
dup += 1 + duprev
hstack.append((hcurr, dup))
ret += dup * 2
print(ret) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR NUMBER NUMBER VAR ASSIGN VAR VAR FUNC_CALL VAR IF VAR VAR VAR BIN_OP NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | import sys
class stack:
def __init__(self):
self.items = []
def isEmpty(self):
return self.items == []
def push(self, item):
self.items.append(item)
def pop(self):
return self.items.pop()
def peek(self):
return self.items[len(self.items) - 1]
def size(self):
return len(self.items)
def __str__(self):
d = ""
for x in self.items:
d += str(x) + ","
return d
class info:
def __init__(self, val, m, count):
self.val = val
self.m = m
self.count = count
def __str__(self):
d = "(" + str(self.val) + "," + str(self.m) + "," + str(self.count) + ")"
return d
n = int(sys.stdin.readline())
x = list(map(int, sys.stdin.readline().split()))
total_count = 0
s = stack()
s.push(info(x[0], 1, 0))
for i in range(1, n):
while s.size() > 0 and x[i] > s.peek().val:
y = s.pop()
total_count += y.count
if s.size() == 0 or x[i] < s.peek().val:
s.push(info(x[i], 1, 0))
elif x[i] == s.peek().val:
top = s.peek()
top.count += top.m
top.m += 1
for obj in s.items:
total_count += obj.count
print(2 * total_count) | IMPORT CLASS_DEF FUNC_DEF ASSIGN VAR LIST FUNC_DEF RETURN VAR LIST FUNC_DEF EXPR FUNC_CALL VAR VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_DEF RETURN VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_DEF ASSIGN VAR STRING FOR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR STRING RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_DEF ASSIGN VAR BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP BIN_OP STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING FUNC_CALL VAR VAR STRING RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF FUNC_CALL VAR NUMBER VAR VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER NUMBER IF VAR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR VAR NUMBER FOR VAR VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | n = int(input())
stack = []
heights = list(map(int, input().strip().split(" ")))
count = 0
for h in heights:
while stack and stack[-1][0] < h:
stack.pop()
if stack and stack[-1][0] == h:
count += stack[-1][1]
stack[-1][1] += 1
else:
stack.append([h, 1])
print(count * 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | class Building(object):
def __init__(self, height, routes):
self.height = height
self.routes = routes
n = int(input())
skyscrapers = [int(x) for x in input().split()]
buildings = []
total_routes = 0
for height in skyscrapers:
while buildings and buildings[-1].height < height:
buildings.pop()
if not buildings or buildings[-1].height > height:
buildings.append(Building(height, 0))
total_routes += buildings[-1].routes
buildings[-1].routes += 1
print(total_routes * 2) | CLASS_DEF VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR VAR WHILE VAR VAR NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | _ = int(input())
st = []
tot = 0
for x in (int(s) for s in input().split()):
while st and st[-1][0] < x:
st.pop()
if st and st[-1][0] == x:
tot += st[-1][1] * 2
st[-1][1] += 1
else:
st.append([x, 1])
print(tot) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR VAR NUMBER NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | st = [[float("inf"), 0]]
input()
res = 0
for h in map(int, input().split()):
while st[-1][0] < h:
st.pop()
if st[-1][0] == h:
res += st[-1][1]
st[-1][1] += 1
else:
st.append([h, 1])
print(res * 2) | ASSIGN VAR LIST LIST FUNC_CALL VAR STRING NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR WHILE VAR NUMBER NUMBER VAR EXPR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | def main():
numSkyscrapers = int(input())
hs = input().split()
heights = list(map(lambda x: int(x), hs))
currentHeight = heights[0]
prevVals = verts = [None] * numSkyscrapers
prevValIndex = 0
counts = [None] * numSkyscrapers
tsum = 0
for y in range(0, len(heights)):
h = heights[y]
while prevValIndex > 0 and prevVals[prevValIndex - 1] < h:
prevValIndex -= 1
if prevValIndex > 0 and h == prevVals[prevValIndex - 1]:
tsum += counts[prevValIndex - 1]
counts[prevValIndex - 1] += 1
else:
prevVals[prevValIndex] = h
counts[prevValIndex] = 1
prevValIndex += 1
print(tsum * 2)
main() | FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NONE VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | N = int(input())
H = list(map(int, input().split(" ")))
stack = []
count = 0
for n in range(0, N):
incr = False
while len(stack) > 0:
h = stack[-1]
if H[n] > h[0]:
stack.pop()
else:
if H[n] == h[0]:
count += h[1]
incr = True
break
if incr:
stack[-1] = H[n], stack[-1][1] + 1
else:
stack.append((H[n], 1))
print(2 * count) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR NUMBER WHILE FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR IF VAR VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR NUMBER IF VAR ASSIGN VAR NUMBER VAR VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR BIN_OP NUMBER VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | num_ss = int(input())
heights = list(reversed(list(map(int, input().split()))))
stack = []
num_paths = 0
for x in range(0, num_ss):
height = heights.pop()
if stack and stack[len(stack) - 1] < height:
old = stack.pop()
c = 1
while stack and stack[len(stack) - 1] < height:
new = stack.pop()
if new == old:
c = c + 1
else:
num_paths = num_paths + c * (c - 1)
old = new
c = 1
num_paths = num_paths + c * (c - 1)
stack.append(height)
while stack:
old = stack.pop()
c = 1
while stack and stack[len(stack) - 1] == old:
c = c + 1
stack.pop()
num_paths = num_paths + c * (c - 1)
print(num_paths) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR IF VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR BIN_OP VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | n = int(input())
arr = [int(x) for x in input().split()]
stack = []
ans = 0
i = 0
while i < n:
if stack:
if arr[i] > stack[-1]:
cur = stack[-1]
cnt = 0
while stack:
if stack[-1] == cur:
stack.pop()
cnt += 1
else:
break
ans += (cnt > 1) * (cnt - 1) * cnt
else:
stack.append(arr[i])
i += 1
else:
stack = [arr[i]]
i += 1
while stack:
cur = stack[-1]
cnt = 0
while stack:
if stack[-1] == cur:
stack.pop()
cnt += 1
else:
break
ans += (cnt > 1) * (cnt - 1) * cnt
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR IF VAR IF VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR LIST VAR VAR VAR NUMBER WHILE VAR ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR IF VAR NUMBER VAR EXPR FUNC_CALL VAR VAR NUMBER VAR BIN_OP BIN_OP VAR NUMBER BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | n = int(input())
hs = map(int, input().split())
curr = []
count = 0
for i in range(n):
h = hs.__next__()
while len(curr) > 0 and h > curr[-1][0]:
curr.pop()
if len(curr) == 0:
curr.append([h, 1])
elif h == curr[-1][0]:
count += curr[-1][1]
curr[-1][1] += 1
else:
curr.append([h, 1])
print(count * 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER IF VAR VAR NUMBER NUMBER VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | def bin_search(arr, low, high, item):
while low <= high:
mid = low + (high - low) // 2
if arr[mid][0] == item:
return mid
elif arr[mid][0] < item:
high = mid - 1
else:
low = mid + 1
return low
def calc(heights):
available = []
last_available = -1
count = 0
for height in heights:
idx = bin_search(available, 0, last_available, height)
if idx > last_available:
new_available = [height, 1]
if len(available) == idx:
available.append(new_available)
else:
available[idx] = new_available
elif available[idx][0] == height:
count += available[idx][1]
available[idx][1] += 1
else:
available[idx] = [height, 1]
last_available = idx
return count * 2
n = input()
print(calc(int(value) for value in input().split())) | FUNC_DEF WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR RETURN VAR IF VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER VAR VAR IF VAR VAR ASSIGN VAR LIST VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR IF VAR VAR NUMBER VAR VAR VAR VAR NUMBER VAR VAR NUMBER NUMBER ASSIGN VAR VAR LIST VAR NUMBER ASSIGN VAR VAR RETURN BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | def routesForward(skyscrapers, size):
count = 0
for i in range(0, size):
for j in range(i + 1, size):
if skyscrapers[j] > skyscrapers[i]:
break
elif skyscrapers[j] == skyscrapers[i]:
count += 1
return count
def routesWithStack(skyscrapers, size):
count = 0
stack = []
for i in range(0, size):
while len(stack) != 0 and skyscrapers[i] > stack[-1][0]:
item = stack.pop()
if item[1] > 1:
count += item[1] * (item[1] - 1)
if len(stack) != 0 and skyscrapers[i] == stack[-1][0]:
stack[-1][1] += 1
else:
stack.append([skyscrapers[i], 1])
for i in stack:
count += i[1] * (i[1] - 1)
return count
numSkyscrapers = int(input())
skyscrapers = list(map(int, input().split()))
print(routesWithStack(skyscrapers, numSkyscrapers)) | FUNC_DEF ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR IF VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR WHILE FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER NUMBER VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER IF FUNC_CALL VAR VAR NUMBER VAR VAR VAR NUMBER NUMBER VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR VAR NUMBER FOR VAR VAR VAR BIN_OP VAR NUMBER BIN_OP VAR NUMBER NUMBER RETURN VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | N = int(input())
heights = list(map(int, input().split(" ")))
total = 0
size = 0
v = [0] * N
m = [0] * N
for i in range(N):
h = heights[i]
while size > 0 and v[size - 1] < h:
size -= 1
if size < 1 or v[size - 1] > h:
v[size] = h
m[size] = 0
size += 1
total += m[size - 1]
m[size - 1] += 1
print(total * 2) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER VAR BIN_OP VAR NUMBER VAR VAR NUMBER IF VAR NUMBER VAR BIN_OP VAR NUMBER VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER VAR NUMBER VAR VAR BIN_OP VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Jim has invented a new flying object called HZ42. HZ42 is like a broom and can only fly horizontally, independent of the environment. One day, Jim started his flight from Dubai's highest skyscraper, traveled some distance and landed on another skyscraper of same height! So much fun! But unfortunately, new skyscrapers have been built recently.
Let us describe the problem in one dimensional space. We have in total $N$ skyscrapers aligned from left to right. The $\boldsymbol{i}$^{th} skyscraper has a height of $h_i$. A flying route can be described as $(i,j)$ with $i\neq j$, which means, Jim starts his HZ42 at the top of the skyscraper $\boldsymbol{i}$ and lands on the skyscraper $j$. Since HZ42 can only fly horizontally, Jim will remain at the height $h_i$ only. Thus the path $(i,j)$ can be valid, only if each of the skyscrapers $i,i+1,\ldots,j-1,j$ is not strictly greater than $h_i$ and if the height of the skyscraper he starts from and arrives on have the same height. Formally, $(i,j)$ is valid iff $\nexists k\in[i,j]:h_k>h_i$ and $h_i=h_j$.
Help Jim in counting the number of valid paths represented by ordered pairs $(i,j)$.
Input Format
The first line contains $N$, the number of skyscrapers. The next line contains $N$ space separated integers representing the heights of the skyscrapers.
Output Format
Print an integer that denotes the number of valid routes.
Constraints
$1\leq N\leq3\cdot10^5$ and no skyscraper will have height greater than $10^{6}$ and less than $\mbox{1}$.
Sample Input #00
6
3 2 1 2 3 3
Sample Output #00
8
Sample Input #01
3
1 1000 1
Sample Output #01
0
Explanation
First testcase: (1, 5), (1, 6) (5, 6) and (2, 4) and the routes in the opposite directions are the only valid routes.
Second testcase: (1, 3) and (3, 1) could have been valid, if there wasn't a big skyscraper with height 1000 between them. | n = int(input())
hs = map(int, input().split())
hhs = sorted(enumerate(hs), key=lambda t: (t[1], t[0]))
prev = list(range(-1, n - 1))
next = list(range(1, n + 1))
firsti, firsth = lasti, lasth = hhs[0]
block_len = 1
s = 0
for i, h in hhs[1:]:
if h == lasth and i == next[lasti]:
block_len += 1
lasti = i
else:
s += block_len * (block_len - 1)
pi, ni = prev[firsti], next[lasti]
if pi >= 0:
next[pi] = ni
if ni < n:
prev[ni] = pi
firsti, firsth = lasti, lasth = i, h
block_len = 1
s += block_len * (block_len - 1)
print(s) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR VAR NUMBER IF VAR VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR IF VAR NUMBER ASSIGN VAR VAR VAR IF VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER VAR BIN_OP VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.