description stringlengths 171 4k | code stringlengths 94 3.98k | normalized_code stringlengths 57 4.99k |
|---|---|---|
Given a collection of Intervals, the task is to merge all of the overlapping Intervals.
Example 1:
Input:
Intervals = {{1,3},{2,4},{6,8},{9,10}}
Output: {{1, 4}, {6, 8}, {9, 10}}
Explanation: Given intervals: [1,3],[2,4]
[6,8],[9,10], we have only two overlapping
intervals here,[1,3] and [2,4]. Therefore
we will merge these two and return [1,4],
[6,8], [9,10].
Example 2:
Input:
Intervals = {{6,8},{1,9},{2,4},{4,7}}
Output: {{1, 9}}
Your Task:
Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging.
Expected Time Complexity: O(N*Log(N)).
Expected Auxiliary Space: O(Log(N)) or O(N).
Constraints:
1 β€ N β€ 100
0 β€ x β€ y β€ 1000 | class Solution:
def overlappedInterval(self, Intervals):
Intervals.sort(key=lambda c: c[0])
i = 0
while i < len(Intervals):
if i + 1 < len(Intervals) and Intervals[i][1] >= Intervals[i + 1][0]:
if Intervals[i][1] < Intervals[i + 1][1]:
Intervals[i][1] = Intervals[i + 1][1]
Intervals.remove(Intervals[i + 1])
else:
i += 1
return Intervals | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER FUNC_CALL VAR VAR VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER IF VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR NUMBER VAR BIN_OP VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR NUMBER RETURN VAR |
Given a collection of Intervals, the task is to merge all of the overlapping Intervals.
Example 1:
Input:
Intervals = {{1,3},{2,4},{6,8},{9,10}}
Output: {{1, 4}, {6, 8}, {9, 10}}
Explanation: Given intervals: [1,3],[2,4]
[6,8],[9,10], we have only two overlapping
intervals here,[1,3] and [2,4]. Therefore
we will merge these two and return [1,4],
[6,8], [9,10].
Example 2:
Input:
Intervals = {{6,8},{1,9},{2,4},{4,7}}
Output: {{1, 9}}
Your Task:
Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging.
Expected Time Complexity: O(N*Log(N)).
Expected Auxiliary Space: O(Log(N)) or O(N).
Constraints:
1 β€ N β€ 100
0 β€ x β€ y β€ 1000 | (1, 9), (2, 4), (4, 7), (6, 8)
class Solution:
def overlappedInterval(self, Intervals):
Intervals.sort()
answer = []
for index, element in enumerate(Intervals):
if index == 0 or answer[-1][1] < element[0]:
answer.append(element)
else:
answer[-1][1] = max(element[1], answer[-1][1])
return answer | EXPR NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER NUMBER CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER NUMBER FUNC_CALL VAR VAR NUMBER VAR NUMBER NUMBER RETURN VAR |
Given a collection of Intervals, the task is to merge all of the overlapping Intervals.
Example 1:
Input:
Intervals = {{1,3},{2,4},{6,8},{9,10}}
Output: {{1, 4}, {6, 8}, {9, 10}}
Explanation: Given intervals: [1,3],[2,4]
[6,8],[9,10], we have only two overlapping
intervals here,[1,3] and [2,4]. Therefore
we will merge these two and return [1,4],
[6,8], [9,10].
Example 2:
Input:
Intervals = {{6,8},{1,9},{2,4},{4,7}}
Output: {{1, 9}}
Your Task:
Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging.
Expected Time Complexity: O(N*Log(N)).
Expected Auxiliary Space: O(Log(N)) or O(N).
Constraints:
1 β€ N β€ 100
0 β€ x β€ y β€ 1000 | class Solution:
def overlappedInterval(self, intervals):
intervals.sort()
a, b = intervals[0]
ans = []
for i in intervals[1:]:
if a <= i[0] <= b:
b = max(b, i[1])
else:
ans.append([a, b])
a, b = i
ans.append([a, b])
return ans | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR LIST FOR VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR RETURN VAR |
Given a collection of Intervals, the task is to merge all of the overlapping Intervals.
Example 1:
Input:
Intervals = {{1,3},{2,4},{6,8},{9,10}}
Output: {{1, 4}, {6, 8}, {9, 10}}
Explanation: Given intervals: [1,3],[2,4]
[6,8],[9,10], we have only two overlapping
intervals here,[1,3] and [2,4]. Therefore
we will merge these two and return [1,4],
[6,8], [9,10].
Example 2:
Input:
Intervals = {{6,8},{1,9},{2,4},{4,7}}
Output: {{1, 9}}
Your Task:
Complete the function overlappedInterval() that takes the list N intervals as input parameters and returns sorted list of intervals after merging.
Expected Time Complexity: O(N*Log(N)).
Expected Auxiliary Space: O(Log(N)) or O(N).
Constraints:
1 β€ N β€ 100
0 β€ x β€ y β€ 1000 | class Solution:
def overlappedInterval(self, Intervals):
Intervals.sort()
res = []
for i in Intervals:
if res == []:
res.append(i)
elif i[0] > res[-1][1]:
res.append(i)
elif i[0] < res[-1][1]:
if i[1] > res[-1][1]:
res[-1][1] = i[1]
elif i[0] == res[-1][1]:
res[-1][1] = i[1]
return res | CLASS_DEF FUNC_DEF EXPR FUNC_CALL VAR ASSIGN VAR LIST FOR VAR VAR IF VAR LIST EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR IF VAR NUMBER VAR NUMBER NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER IF VAR NUMBER VAR NUMBER NUMBER ASSIGN VAR NUMBER NUMBER VAR NUMBER RETURN VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | n = int(input())
lst = list(map(int, input().strip().split()))
ans = [[lst[0]]]
head = [lst[0]]
added = False
for i in range(1, n):
t = lst[i]
if head[len(head) - 1] > t:
ans.append([t])
head.append(t)
else:
l, r = 0, len(head) - 1
while r - l > 1:
mm = l + int((r - l) / 2)
if head[mm] < t:
r = mm
else:
l = mm
if head[l] < t:
ans[l].append(t)
head[l] = t
else:
ans[r].append(t)
head[r] = t
for ls in ans:
print(" ".join(str(x) for x in ls)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER ASSIGN VAR LIST VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR VAR IF VAR BIN_OP FUNC_CALL VAR VAR NUMBER VAR EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | def readInt():
return int(input())
def readInts():
return [int(i) for i in input().split()]
n = readInt()
a = readInts()
nexts = [-1] * n
heads = [-1] * n
lasts = [-1] * n
cnt = 0
for i in range(n):
value = a[i]
left = -1
right = cnt
while right - left > 1:
mid = (right + left) // 2
if a[lasts[mid]] < value:
right = mid
else:
left = mid
if right == cnt:
heads[right] = i
cnt += 1
else:
last = lasts[right]
nexts[last] = i
lasts[right] = i
for i in range(cnt):
index = heads[i]
while index != -1:
print(a[index], end=" ")
index = nexts[index]
print() | FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR WHILE VAR NUMBER EXPR FUNC_CALL VAR VAR VAR STRING ASSIGN VAR VAR VAR EXPR FUNC_CALL VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | ans = []
n = int(input())
arr = list(map(int, input().split()))
for x in arr:
lo, hi = 0, len(ans)
while lo < hi:
mid = (lo + hi) // 2
if ans[mid][-1] < x:
hi = mid
else:
lo = mid + 1
if lo == len(ans):
ans.append([x])
else:
ans[lo].append(x)
for line in ans:
print(" ".join([str(i) for i in line])) | ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | input()
numbers = [int(i) for i in input().split()]
runs = [[numbers[0]]]
for n in numbers[1:]:
if n < runs[-1][-1]:
runs.append([n])
continue
lo = 0
hi = len(runs) - 1
valid = -1
while lo <= hi:
to_search = (lo + hi) // 2
if n > runs[to_search][-1]:
valid = to_search
hi = to_search - 1
else:
lo = to_search + 1
runs[valid].append(n)
for r in runs:
print(" ".join(str(n) for n in r)) | EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST LIST VAR NUMBER FOR VAR VAR NUMBER IF VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR LIST VAR ASSIGN VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | n = int(input())
a = []
arr = list(map(int, input().split()))
for i in range(n):
if len(a) == 0:
a.append([arr[i]])
continue
el = arr[i]
l, r = 0, len(a) - 1
while r - l >= 1:
m = (l + r) // 2
if el > a[m][-1]:
r = m
else:
l = m + 1
if el < a[l][-1]:
a.append([el])
else:
a[l].append(el)
for x in a:
for el in x:
print(el, end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER WHILE BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR VAR NUMBER EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING EXPR FUNC_CALL VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | n = int(input())
a = list(map(int, input().split()))
ans = []
for i in range(n):
l = 0
r = len(ans)
while l < r:
mid = l + (r - l) // 2
if ans[mid][-1] < a[i]:
r = mid
else:
l = mid + 1
if l == len(ans):
ans.append([a[i]])
else:
ans[l].append(a[i])
for x in ans:
print(*x) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | n = int(input())
arr = list(map(int, input().split()))
ans = [0] * 1
ans[0] = []
ans[0].append(arr[0])
for i in range(1, n, 1):
lo, hi = 0, len(ans)
idx = -1
while lo <= hi:
mid = (lo + hi) // 2
if lo != len(ans) and ans[mid][len(ans[mid]) - 1] < arr[i]:
idx = mid
hi = mid - 1
else:
lo = mid + 1
if len(ans) == hi:
ans.append([])
ans[hi].append(arr[i])
else:
ans[idx].append(arr[i])
for i in range(len(ans)):
for j in range(len(ans[i])):
print(ans[i][j], end=" ")
print() | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR BIN_OP LIST NUMBER NUMBER ASSIGN VAR NUMBER LIST EXPR FUNC_CALL VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR NUMBER ASSIGN VAR VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR VAR VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR LIST EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR STRING EXPR FUNC_CALL VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | class CodeforcesTask847BSolution:
def __init__(self):
self.result = ""
self.n = 0
self.sequence = []
def read_input(self):
self.n = int(input())
self.sequence = [int(x) for x in input().split(" ")]
def process_task(self):
mx = 2000001
ans = [[] for x in range(mx)]
crf = [0] * mx
cnt = 0
id = 0
for x in range(self.n):
if not x:
ans[cnt].append(self.sequence[x])
crf[cnt] = self.sequence[x]
else:
if self.sequence[x] <= crf[cnt]:
cnt += 1
id = cnt
else:
l = 0
r = cnt
while l < r:
mid = (l + r) // 2
if crf[mid] >= self.sequence[x]:
l = mid + 1
else:
r = mid
id = r
ans[id].append(self.sequence[x])
crf[id] = self.sequence[x]
self.result = "\n".join(
[" ".join([str(x) for x in row]) for row in ans[: cnt + 1]]
)
def get_result(self):
return self.result
def __starting_point():
Solution = CodeforcesTask847BSolution()
Solution.read_input()
Solution.process_task()
print(Solution.get_result())
__starting_point() | CLASS_DEF FUNC_DEF ASSIGN VAR STRING ASSIGN VAR NUMBER ASSIGN VAR LIST FUNC_DEF ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR LIST VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR IF VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR FUNC_CALL STRING FUNC_CALL STRING FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR NUMBER FUNC_DEF RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR |
Ivan has an array consisting of n different integers. He decided to reorder all elements in increasing order. Ivan loves merge sort so he decided to represent his array with one or several increasing sequences which he then plans to merge into one sorted array.
Ivan represent his array with increasing sequences with help of the following algorithm.
While there is at least one unused number in array Ivan repeats the following procedure: iterate through array from the left to the right; Ivan only looks at unused numbers on current iteration; if current number is the first unused number on this iteration or this number is greater than previous unused number on current iteration, then Ivan marks the number as used and writes it down.
For example, if Ivan's array looks like [1, 3, 2, 5, 4] then he will perform two iterations. On first iteration Ivan will use and write numbers [1, 3, 5], and on second one β [2, 4].
Write a program which helps Ivan and finds representation of the given array with one or several increasing sequences in accordance with algorithm described above.
-----Input-----
The first line contains a single integer n (1 β€ n β€ 2Β·10^5) β the number of elements in Ivan's array.
The second line contains a sequence consisting of distinct integers a_1, a_2, ..., a_{n} (1 β€ a_{i} β€ 10^9) β Ivan's array.
-----Output-----
Print representation of the given array in the form of one or more increasing sequences in accordance with the algorithm described above. Each sequence must be printed on a new line.
-----Examples-----
Input
5
1 3 2 5 4
Output
1 3 5
2 4
Input
4
4 3 2 1
Output
4
3
2
1
Input
4
10 30 50 101
Output
10 30 50 101 | n = int(input())
a = list(map(int, input().split()))
groups = []
groupTail = []
for elem in a:
if not groups:
groups.append([elem])
groupTail.append(elem)
else:
l = 0
r = len(groups)
while l <= r:
m = (l + r) // 2
if m == len(groups):
break
if l == r:
break
if groupTail[m] < elem:
r = m
else:
l = m + 1
if m == len(groups):
groups.append([elem])
groupTail.append(elem)
else:
groups[m].append(elem)
groupTail[m] = elem
for line in groups:
print(" ".join(map(str, line))) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR VAR IF VAR EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR WHILE VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR FUNC_CALL VAR VAR IF VAR VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR EXPR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR FUNC_CALL STRING FUNC_CALL VAR VAR VAR |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
def constructLowerArray(self, arr, n):
ans = [0] * n
m_arr = []
for i in range(n):
m_arr.append((arr[i], i))
self.mergesort(m_arr, ans, 0, n - 1)
return ans
def mergesort(self, arr, ans, l, r):
if l < r:
mid = (l + r) // 2
self.mergesort(arr, ans, l, mid)
self.mergesort(arr, ans, mid + 1, r)
self.merge(arr, ans, l, mid, r)
def merge(self, arr, ans, l, mid, r):
temp = []
i = l
j = mid + 1
while i <= mid and j <= r:
if arr[i][0] > arr[j][0]:
ans[arr[i][1]] += r - j + 1
temp.append(arr[i])
i += 1
else:
temp.append(arr[j])
j += 1
while i <= mid:
temp.append(arr[i])
i += 1
while j <= r:
temp.append(arr[j])
j += 1
k = 0
for i in range(l, r + 1):
arr[i] = temp[k]
k += 1 | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
def merge(self, left, right):
i, j = 0, 0
cnt = 0
m, n = len(left), len(right)
sorted_arr = []
while i < m and j < n:
if left[i][1] > right[j][1]:
cnt += 1
sorted_arr.append(right[j])
j += 1
else:
sorted_arr.append(left[i])
self.ans[left[i][0]] += cnt
i += 1
while i < m:
sorted_arr.append(left[i])
self.ans[left[i][0]] += cnt
i += 1
while j < n:
sorted_arr.append(right[j])
cnt += 1
j += 1
return sorted_arr
def split(self, nums):
if len(nums) == 1:
return nums
mid = len(nums) // 2
left, right = self.split(nums[:mid]), self.split(nums[mid:])
return self.merge(left, right)
def constructLowerArray(self, arr, n):
self.ans = [(0) for _ in range(n)]
nums = []
for i, num in enumerate(arr):
nums.append((i, num))
sorted_arr = self.split(nums)
return self.ans | CLASS_DEF FUNC_DEF ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
def constructLowerArray(self, arr, n):
ans = [(0) for i in range(len(arr))]
for i in range(len(arr)):
arr[i] = [arr[i], i]
self.mergesort(arr, 0, len(arr) - 1, ans)
return ans
def mergesort(self, arr, left, right, ans):
if left < right:
mid = left + (right - left) // 2
self.mergesort(arr, left, mid, ans)
self.mergesort(arr, mid + 1, right, ans)
self.merge(arr, left, mid, right, ans)
return
def merge(self, arr, left, mid, right, ans):
temp = [[i, 0] for i in range(right - left + 1)]
i = left
j = mid + 1
k = 0
while i <= mid and j <= right:
if arr[i][0] <= arr[j][0]:
temp[k] = arr[j]
j += 1
k += 1
else:
temp[k] = arr[i]
ans[arr[i][1]] += right - j + 1
i += 1
k += 1
while i <= mid:
temp[k] = arr[i]
k += 1
i += 1
while j <= right:
temp[k] = arr[j]
j += 1
k += 1
for i in range(left, right + 1):
arr[i] = temp[i - left] | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_DEF ASSIGN VAR LIST VAR NUMBER VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class SegmentTree:
def __init__(self, n):
self.N = n
self.BIT = [0] * (2 * n)
def update(self, idx):
n = self.N
A = self.BIT
idx += n
A[idx] += 1
while idx > 1:
A[idx >> 1] = A[idx] + A[idx ^ 1]
idx >>= 1
def query(self, l, r):
n = self.N
A = self.BIT
l += n
r += n
res = 0
while l <= r:
if l & 1:
res += A[l]
l += 1
if r & 1 == 0:
res += A[r]
r -= 1
l >>= 1
r >>= 1
return res
class Solution:
def constructLowerArray(self, A, N):
tree = SegmentTree(N)
mp = {}
for i in A:
mp[i] = 1
P = []
ans = []
c = -1
for i in sorted(mp):
c += 1
mp[i] = c
for i in A:
P.append(mp[i])
for i in range(N - 1, -1, -1):
ans.append(tree.query(0, P[i] - 1))
tree.update(P[i])
return ans[::-1] | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP LIST NUMBER BIN_OP NUMBER VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR NUMBER WHILE VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER BIN_OP VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR VAR VAR ASSIGN VAR NUMBER WHILE VAR VAR IF BIN_OP VAR NUMBER VAR VAR VAR VAR NUMBER IF BIN_OP VAR NUMBER NUMBER VAR VAR VAR VAR NUMBER VAR NUMBER VAR NUMBER RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR DICT FOR VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR LIST ASSIGN VAR LIST ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR VAR VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR VAR FOR VAR FUNC_CALL VAR BIN_OP VAR NUMBER NUMBER NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR NUMBER BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR RETURN VAR NUMBER |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
def constructLowerArray(self, arr, n):
self.answer = [0] * n
self.divide([[num, i] for i, num in enumerate(arr)])
return self.answer
def divide(self, numbers):
return (
numbers
if len(numbers) == 1
else self.conqure(
self.divide(numbers[: len(numbers) // 2]),
self.divide(numbers[len(numbers) // 2 :]),
)
)
def conqure(self, numbers1, numbers2):
numbers = []
n = len(numbers1)
m = len(numbers2)
i = j = 0
while i < n and j < m:
if numbers1[i][0] <= numbers2[j][0]:
numbers.append(numbers1[i])
self.answer[numbers1[i][1]] += j
i += 1
else:
numbers.append(numbers2[j])
j += 1
while i < n:
numbers.append(numbers1[i])
self.answer[numbers1[i][1]] += m
i += 1
while j < m:
numbers.append(numbers2[j])
j += 1
return numbers | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF RETURN FUNC_CALL VAR VAR NUMBER VAR FUNC_CALL VAR FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_CALL VAR VAR BIN_OP FUNC_CALL VAR VAR NUMBER FUNC_DEF ASSIGN VAR LIST ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
d = dict()
def merge(self, arr, l, m, r):
new = [0] * (r - l + 1)
i = l
j = m + 1
k = 0
while i <= m and j <= r:
if arr[i][0] > arr[j][0]:
self.d[arr[i][1]] += r - j + 1
new[k] = arr[i]
i += 1
k += 1
else:
new[k] = arr[j]
k += 1
j += 1
while i <= m:
new[k] = arr[i]
k += 1
i += 1
while j <= r:
new[k] = arr[j]
k += 1
j += 1
i = 0
k = l
for i in range(len(new)):
arr[k] = new[i]
k += 1
def mergeSort(self, arr, l, r):
if l < r:
m = int((l + r) / 2)
self.mergeSort(arr, l, m)
self.mergeSort(arr, m + 1, r)
self.merge(arr, l, m, r)
def constructLowerArray(self, arr, n):
l = 0
r = n - 1
A = []
for i in range(n):
A.append([arr[i], i])
for i in range(n):
self.d[A[i][1]] = 0
self.mergeSort(A, l, r)
res = [0] * n
for i in range(n):
res[i] = self.d[i]
return res | CLASS_DEF ASSIGN VAR FUNC_CALL VAR FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR NUMBER NUMBER EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR RETURN VAR |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
def merge(self, arr, l, m, r, ans):
temp = [[0, 0]] * (r - l + 1)
i = l
j = m + 1
k = 0
while i <= m and j <= r:
if arr[i][0] <= arr[j][0]:
temp[k] = arr[j]
k += 1
j += 1
else:
ans[arr[i][1]] += r - j + 1
temp[k] = arr[i]
k += 1
i += 1
while i <= m:
temp[k] = arr[i]
k += 1
i += 1
while j <= r:
temp[k] = arr[j]
k += 1
j += 1
for val in range(l, r + 1):
arr[val] = temp[val - l]
def divide(self, arr, l, r, ans):
if l < r:
mid = l + (r - l) // 2
self.divide(arr, l, mid, ans)
self.divide(arr, mid + 1, r, ans)
self.merge(arr, l, mid, r, ans)
else:
return
def constructLowerArray(self, arr, n):
if n <= 1:
return [0]
ans = [0] * n
li = []
for i in range(n):
li.append([arr[i], i])
self.divide(li, 0, n - 1, ans)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST LIST NUMBER NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER VAR VAR VAR NUMBER BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN FUNC_DEF IF VAR NUMBER RETURN LIST NUMBER ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER VAR RETURN VAR |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
def constructLowerArray(self, arr, n):
self.ans = [0] * n
nums = []
for i, num in enumerate(arr):
nums.append([i, arr[i]])
self.sort(nums)
return self.ans
def sort(self, nums):
if len(nums) == 1:
return nums
mid = len(nums) // 2
left = self.sort(nums[:mid])
right = self.sort(nums[mid:])
return self.merge(left, right)
def merge(self, left, right):
res = []
l, r = 0, 0
cnt = 0
while l < len(left) and r < len(right):
if left[l][1] > right[r][1]:
res.append(right[r])
cnt += 1
r += 1
else:
res.append(left[l])
self.ans[left[l][0]] += cnt
l += 1
while l < len(left):
res.append(left[l])
self.ans[left[l][0]] += cnt
l += 1
while r < len(right):
res.append(right[r])
r += 1
return res | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR LIST FOR VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR LIST VAR VAR VAR EXPR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF FUNC_CALL VAR VAR NUMBER RETURN VAR ASSIGN VAR BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR NUMBER NUMBER ASSIGN VAR NUMBER WHILE VAR FUNC_CALL VAR VAR VAR FUNC_CALL VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR NUMBER VAR VAR NUMBER WHILE VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER RETURN VAR |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
def constructLowerArray(self, arr, n):
marr = []
temp = []
for i in range(0, n):
marr.append((arr[i], i))
temp.append((arr[i], i))
count = [0] * n
def inv(marr, temp, start, end):
if start >= end:
return
mid = (start + end) // 2
inv(marr, temp, start, mid)
inv(marr, temp, mid + 1, end)
merge(marr, temp, start, mid, end)
def merge(marr, temp, start, mid, end):
k = start
i = start
j = mid + 1
while i <= mid and j <= end:
if marr[i][0] > marr[j][0]:
idx = marr[i][1]
count[idx] += end - j + 1
temp[k] = marr[i]
i += 1
k += 1
else:
temp[k] = marr[j]
j += 1
k += 1
while i <= mid:
temp[k] = marr[i]
k += 1
i += 1
while j <= end:
temp[k] = marr[j]
k += 1
j += 1
for v in range(start, end + 1):
marr[v] = temp[v]
inv(marr, temp, 0, n - 1)
return count | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER WHILE VAR VAR VAR VAR IF VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR VAR VAR NUMBER VAR VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FOR VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class FenwickTree:
def __init__(self, n):
self.tree = [0] * (n + 1)
def update(self, index, delta=0):
index += 1
while 0 < index < len(self.tree):
self.tree[index] += delta
index += index & -index
def getSumQuery(self, index):
output = 0
index += 1
while index > 0:
output += self.tree[index]
index -= index & -index
return output
class Solution:
def constructLowerArray(self, nums, n):
d = {e: i for i, e in enumerate(sorted(set(nums)))}
indices = [d[num] for num in nums]
tree = FenwickTree(len(indices))
result = []
for val in indices[::-1]:
tree.update(val, 1)
result.append(tree.getSumQuery(val - 1))
return result[::-1] | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP LIST NUMBER BIN_OP VAR NUMBER FUNC_DEF NUMBER VAR NUMBER WHILE NUMBER VAR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR VAR FUNC_DEF ASSIGN VAR NUMBER VAR NUMBER WHILE VAR NUMBER VAR VAR VAR VAR BIN_OP VAR VAR RETURN VAR CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR LIST FOR VAR VAR NUMBER EXPR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP VAR NUMBER RETURN VAR NUMBER |
Given an array Arr of size N containing positive integers. Count number of smaller elements on right side of each array element.
Example 1:
Input:
N = 7
Arr[] = {12, 1, 2, 3, 0, 11, 4}
Output: 6 1 1 1 0 1 0
Explanation: There are 6 elements right
after 12. There are 1 element right after
1. And so on.
Example 2:
Input:
N = 5
Arr[] = {1, 2, 3, 4, 5}
Output: 0 0 0 0 0
Explanation: There are 0 elements right
after 1. There are 0 elements right after
2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function constructLowerArray() which takes the array of integers arr and n as parameters and returns an array of integers denoting the answer.
Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)
Constraints:
1 β€ N β€ 10^{6}
0 β€ Arr_{i }β€ 10^{8} | class Solution:
def constructLowerArray(self, arr, n):
n = len(arr)
result = [0] * n
indices = [i for i in range(n)]
def mergesort(low, high):
if low >= high:
return
mid = low + (high - low) // 2
mergesort(low, mid)
mergesort(mid + 1, high)
merge(low, mid, high)
def merge(low, mid, high):
nonlocal indices
nonlocal result
left = low
right = mid + 1
temp = []
while left <= mid and right <= high:
if arr[indices[left]] <= arr[indices[right]]:
temp.append(indices[left])
result[indices[left]] += right - (mid + 1)
left += 1
else:
temp.append(indices[right])
right += 1
while left <= mid:
temp.append(indices[left])
result[indices[left]] += right - (mid + 1)
left += 1
while right <= high:
temp.append(indices[right])
right += 1
indices[low : high + 1] = temp
mergesort(0, n - 1)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP LIST NUMBER VAR ASSIGN VAR VAR VAR FUNC_CALL VAR VAR FUNC_DEF IF VAR VAR RETURN ASSIGN VAR BIN_OP VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR LIST WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR VAR BIN_OP VAR BIN_OP VAR NUMBER VAR NUMBER WHILE VAR VAR EXPR FUNC_CALL VAR VAR VAR VAR NUMBER ASSIGN VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
temp = head
l = []
while temp != None:
l.append(temp.data)
temp = temp.next
l.sort()
head = temp = None
for i in l:
n = Node(i)
if head == None:
head = temp = n
else:
temp.next = n
temp = n
return head | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR NONE FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Node:
def __init__(self, data):
self.data = data
self.next = None
class Solution:
def mergeSort(self, head):
if head.next == None:
return head
mid = self.findMid(head)
head2 = mid.next
mid.next = None
newhead1 = self.mergeSort(head)
newhead2 = self.mergeSort(head2)
finalhead = self.merge(newhead1, newhead2)
return finalhead
def merge(self, head1, head2):
merged = Node(-1)
temp = merged
while head1 != None and head2 != None:
if head1.data < head2.data:
temp.next = head1
head1 = head1.next
else:
temp.next = head2
head2 = head2.next
temp = temp.next
while head1 != None:
temp.next = head1
head1 = head1.next
temp = temp.next
while head2 != None:
temp.next = head2
head2 = head2.next
temp = temp.next
return merged.next
def findMid(self, head):
slow = head
fast = head.next
while fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
return slow | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
s = head
ll = []
while s:
ll.append(s.data)
s = s.next
ll.sort()
s = head
while s:
s.data = ll.pop(0)
s = s.next
return head | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
b = []
tmp = head
while tmp:
b.append(tmp.data)
tmp = tmp.next
tmp = head
b.sort()
for i in b:
tmp.data = i
tmp = tmp.next
return head | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def findMid(self, head):
slow = head
fast = head
while fast.next != None and fast.next.next != None:
slow = slow.next
fast = fast.next.next
return slow
def merge(self, l, r):
if l == None:
return r
if r == None:
return l
dummy = Node(-1)
temp = dummy
while l != None and r != None:
if l.data < r.data:
temp.next = l
l = l.next
else:
temp.next = r
r = r.next
temp = temp.next
while l != None:
temp.next = l
temp = temp.next
l = l.next
while r != None:
temp.next = r
temp = temp.next
r = r.next
return dummy.next
def mergeSort(self, head):
if head == None or head.next == None:
return head
middle = self.findMid(head)
left = head
right = middle.next
middle.next = None
left = self.mergeSort(left)
right = self.mergeSort(right)
res = self.merge(left, right)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if head is None:
return None
if head.next is None:
return head
split_linked_list_heads = self.split_give_two_linked_list(head)
left_sorted_linked_list = self.mergeSort(split_linked_list_heads[0])
right_sorted_linked_list = self.mergeSort(split_linked_list_heads[1])
return self.merge_sorted_linked_list(
left_sorted_linked_list, right_sorted_linked_list
)
def merge_sorted_linked_list(self, head_1, head_2):
if head_1.data <= head_2.data:
new_head = Node(head_1.data)
curr_head_1 = head_1.next
curr_head_2 = head_2
else:
new_head = Node(head_2.data)
curr_head_1 = head_1
curr_head_2 = head_2.next
curr = new_head
while curr_head_1 is not None or curr_head_2 is not None:
if curr_head_1 is None:
curr.next = Node(curr_head_2.data)
curr_head_2 = curr_head_2.next
elif curr_head_2 is None:
curr.next = Node(curr_head_1.data)
curr_head_1 = curr_head_1.next
elif curr_head_1.data <= curr_head_2.data:
curr.next = Node(curr_head_1.data)
curr_head_1 = curr_head_1.next
else:
curr.next = Node(curr_head_2.data)
curr_head_2 = curr_head_2.next
curr = curr.next
return new_head
def get_count(self, head):
count = 0
curr = head
while curr is not None:
count = count + 1
curr = curr.next
return count
def split_give_two_linked_list(self, head):
count = self.get_count(head)
if count % 2 == 0:
half_count = count // 2 - 1
else:
half_count = count // 2
curr = head
while half_count > 0:
curr = curr.next
half_count = half_count - 1
if curr is not None:
new_head = curr.next
curr.next = None
else:
new_head = None
return [head, new_head] | CLASS_DEF FUNC_DEF IF VAR NONE RETURN NONE IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR WHILE VAR NUMBER ASSIGN VAR VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE RETURN LIST VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
arr = []
while head:
arr.append(head.data)
head = head.next
temp = new = Node(0)
for i in sorted(arr):
temp.next = Node(i)
temp = temp.next
return new.next | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def getmid(self, head):
c = 0
cur = head
while head != None:
c += 1
head = head.next
if c % 2 == 0:
i = c // 2
else:
i = c // 2 + 1
for j in range(1, i):
cur = cur.next
return cur
def SortMerge(self, a, b):
if a == None:
return b
if b == None:
return a
if a.data <= b.data:
result = a
result.next = self.SortMerge(a.next, b)
else:
result = b
result.next = self.SortMerge(a, b.next)
return result
def mergeSort(self, head):
cur = head
if head == None or head.next == None:
return head
middle = self.getmid(head)
nextmid = middle.next
middle.next = None
left = self.mergeSort(head)
right = self.mergeSort(nextmid)
sorted_list = self.SortMerge(left, right)
return sorted_list | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NUMBER ASSIGN VAR VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER FOR VAR FUNC_CALL VAR NUMBER VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Node:
def __init__(self, data):
self.data = data
self.next = None
class Solution:
def mergeSort(self, head):
temp = head
lst = []
while temp:
lst.append(temp.data)
temp = temp.next
res1 = Node(0)
res2 = res1
lst.sort()
for num in lst:
res1.next = Node(num)
res1 = res1.next
return res2.next | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def merge(self, head1, head2):
if head1 == None:
return head2
elif head2 == None:
return head1
temp = Node(-1)
top = temp
while head1 != None and head2 != None:
if head1.data < head2.data:
temp.next = head1
head1 = head1.next
else:
temp.next = head2
head2 = head2.next
temp = temp.next
if head1 != None:
temp.next = head1
if head2 != None:
temp.next = head2
return top.next
def getMid(self, head):
fast = head
slow = head
while fast.next != None and fast.next.next != None:
slow = slow.next
fast = fast.next.next
return slow
def mergeSort(self, head):
if head == None or head.next == None:
return head
mid = self.getMid(head)
right = mid
mid = mid.next
right.next = None
left = self.mergeSort(head)
right = self.mergeSort(mid)
return self.merge(left, right) | CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def findMid(self, head):
if not head or not head.next:
return head
slow = head
fast = head
while fast and fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
return slow
def sort(self, head, count):
if head.next == None:
return head
else:
mid = self.findMid(head)
l = head
l2 = mid.next
mid.next = None
l = self.sort(l, count + 1)
l2 = self.sort(l2, count + 1)
dummy = Node(-1)
curr = dummy
while l and l2:
if l.data <= l2.data:
curr.next = l
l = l.next
curr = curr.next
else:
curr.next = l2
l2 = l2.next
curr = curr.next
while l:
curr.next = l
l = l.next
curr = curr.next
while l2:
curr.next = l2
l2 = l2.next
curr = curr.next
curr.next = None
return dummy.next
def mergeSort(self, head):
head = self.sort(head, 0)
return head | CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR NUMBER RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if head == None or head.next == None:
return head
count = 0
temp = head
while temp:
count += 1
temp = temp.next
current = head
arr = []
for i in range(count):
arr.append(current.data)
current = current.next
arr.sort()
head1 = None
tail = None
for i in range(count):
node = Node(arr[i])
if head1 == None:
head1 = node
tail = node
else:
tail.next = node
tail = node
return head1 | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if head is None or head.next is None:
return head
slow = head
fast = head
prev = None
while fast and fast.next:
prev = slow
slow = slow.next
fast = fast.next.next
prev.next = None
ll1 = head
ll2 = slow
ll1 = self.mergeSort(ll1)
ll2 = self.mergeSort(ll2)
flag = Node(None)
head = flag
while ll1 and ll2:
if ll1.data <= ll2.data:
head.next = ll1
head = head.next
ll1 = ll1.next
head.next = None
else:
head.next = ll2
head = head.next
ll2 = ll2.next
head.next = None
if ll1:
head.next = ll1
elif ll2:
head.next = ll2
return flag.next | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR NONE ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if head is None or head.next is None:
return head
mid = self.getMid(head)
new_mid = mid.next
mid.next = None
left = self.mergeSort(head)
right = self.mergeSort(new_mid)
res = self.merge(left, right)
return res
def merge(self, left, right):
dummy_node = Node(0)
prev = dummy_node
while True:
if left is None:
prev.next = right
break
if right is None:
prev.next = left
break
if left.data <= right.data:
prev.next = left
left = left.next
prev = prev.next
else:
prev.next = right
right = right.next
prev = prev.next
return dummy_node.next
def getMid(self, head):
slow, fast = head, head
while fast.next is not None and fast.next.next is not None:
slow = slow.next
fast = fast.next.next
return slow | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE NUMBER IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if not head or head.next == None:
return head
middle = self.get_middle(head)
nexttomiddle = middle.next
middle.next = None
left = self.mergeSort(head)
right = self.mergeSort(nexttomiddle)
Sortedlist = self.Sort_two(left, right)
return Sortedlist
def get_middle(self, head):
if head == None:
return head
slow = head
fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
return slow
def Sort_two(self, a, b):
result = None
if a is None:
return b
if b is None:
return a
if a.data <= b.data:
result = a
result.next = self.Sort_two(a.next, b)
else:
result = b
result.next = self.Sort_two(a, b.next)
return result | CLASS_DEF FUNC_DEF IF VAR VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
dummy = Node(-1)
res = dummy
st = []
curr = head
while curr:
st.append(curr.data)
curr = curr.next
st.sort()
for i in range(len(st)):
res.next = Node(st[i])
res = res.next
return dummy.next | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def get_middle(self, head):
if head == None or head.next == None:
return head
f, s = head, head
while f.next != None and f.next.next != None:
s, f = s.next, f.next.next
return s
def merge_list(self, head1, head2):
v = 0
if head1 is None and head2 is None:
return None
elif head1 is None:
v = head2.data - 1
elif head2 is None:
v = head1.data - 1
else:
v = min(head1.data, head2.data) - 1
head = Node(v)
c = head
while head1 != None and head2 != None:
if head1.data < head2.data:
c.next = head1
head1 = head1.next
else:
c.next = head2
head2 = head2.next
c = c.next
if head1 != None:
c.next = head1
else:
c.next = head2
return head.next
def print_list(self, head):
p = head
while p is not None:
print(p.data, end="-> ")
p = p.next
print("")
def mergeSort(self, head):
if head == None or head.next == None:
return head
mid = self.get_middle(head)
t = mid.next
mid.next = None
after_mid = self.mergeSort(t)
before_mid = self.mergeSort(head)
merged = self.merge_list(before_mid, after_mid)
return merged | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NUMBER IF VAR NONE VAR NONE RETURN NONE IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER IF VAR NONE ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP FUNC_CALL VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR STRING ASSIGN VAR VAR EXPR FUNC_CALL VAR STRING FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergee(self, arr, mid, l, r):
n1 = mid - l + 1
n2 = r - mid
left = []
right = []
for i in range(n1):
left.append(arr[l + i])
for i in range(n2):
right.append(arr[mid + 1 + i])
i = 0
j = 0
k = l
while i < n1 and j < n2:
if left[i] <= right[j]:
arr[k] = left[i]
i += 1
else:
arr[k] = right[j]
j += 1
k += 1
while i < n1:
arr[k] = left[i]
i += 1
k += 1
while j < n2:
arr[k] = right[j]
j += 1
k += 1
def merge(self, arr, l, r):
if l < r:
mid = (l + r) // 2
self.merge(arr, l, mid)
self.merge(arr, mid + 1, r)
self.mergee(arr, mid, l, r)
def mergeSort(self, head):
p = head
l = []
while p:
l.append(p.data)
p = p.next
self.merge(l, 0, len(l) - 1)
l3 = Node(-1)
c = l3
for i in l:
c.next = Node(i)
c = c.next
return l3.next | CLASS_DEF FUNC_DEF ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR BIN_OP VAR VAR ASSIGN VAR LIST ASSIGN VAR LIST FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR VAR FOR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR VAR VAR IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER WHILE VAR VAR ASSIGN VAR VAR VAR VAR VAR NUMBER VAR NUMBER FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR VAR VAR EXPR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR EXPR FUNC_CALL VAR VAR VAR VAR VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if head is None or head.next is None:
return head
temp = head
slow, fast = head, head
while fast and fast.next:
temp = slow
slow = slow.next
fast = fast.next.next
mid = slow
temp.next = None
left = self.mergeSort(head)
right = self.mergeSort(mid)
return self.merge(left, right)
def merge(self, left, right):
dummy = Node(0)
tail = dummy
while left and right:
if left.data <= right.data:
tail.next = left
left = left.next
else:
tail.next = right
right = right.next
tail = tail.next
if left:
tail.next = left
if right:
tail.next = right
return dummy.next | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
arr = []
cur = head
prev = None
while cur is not None:
prev = cur
cur = cur.next
prev.next = None
arr.append(prev)
return self.LinkedListMergeSort(0, len(arr) - 1, arr)
def LinkedListMergeSort(self, l, h, arr):
if l < h:
m = (l + h) // 2
arr[l] = self.LinkedListMergeSort(l, m, arr)
arr[h] = self.LinkedListMergeSort(m + 1, h, arr)
arr[l] = arr[h] = self.merge(arr[l], arr[h], arr)
return arr[l]
def merge(self, temp1, temp2, arr):
head = Node(0)
temp = head
while temp1 is not None and temp2 is not None:
if temp1.data < temp2.data:
temp.next = temp1
temp1 = temp1.next
else:
temp.next = temp2
temp2 = temp2.next
temp = temp.next
if temp1 is None:
temp.next = temp2
else:
temp.next = temp1
temp = head
head = head.next
del temp
return head | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE EXPR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR NUMBER BIN_OP FUNC_CALL VAR VAR NUMBER VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR VAR FUNC_CALL VAR BIN_OP VAR NUMBER VAR VAR ASSIGN VAR VAR VAR VAR FUNC_CALL VAR VAR VAR VAR VAR VAR RETURN VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
def getMiddle(head):
if head is None:
return None
slow = head
fast = head
while fast.next is not None and fast.next.next is not None:
slow = slow.next
fast = fast.next.next
return slow
def mergeLists(left, right):
dummy = Node(0)
tail = dummy
while left and right:
if left.data <= right.data:
tail.next = left
left = left.next
else:
tail.next = right
right = right.next
tail = tail.next
tail.next = left or right
return dummy.next
if head is None or head.next is None:
return head
mid = getMiddle(head)
left = head
right = mid.next
mid.next = None
left = self.mergeSort(left)
right = self.mergeSort(right)
return mergeLists(left, right) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR NONE RETURN NONE ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR RETURN VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | def middle(head):
if head.next.next == None:
return head
return head
slow, fast = head, head
while fast and fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
return slow
def merge(l, r):
result, tresult = None, None
while l or r:
if l == None:
if tresult == None:
return r
else:
result.next = r
return tresult
elif r == None:
if tresult == None:
return l
else:
result.next = l
return tresult
elif l.data >= r.data:
if result is None:
result = r
tresult = result
else:
result.next = r
result = result.next
r = r.next
else:
if result is None:
result = l
tresult = result
else:
result.next = l
result = result.next
l = l.next
result.next = None
return tresult
class Solution:
def mergeSort(self, head):
if head == None or head.next == None:
return head
mid = middle(head)
temp = mid.next
mid_next = temp
mid.next = None
l = self.mergeSort(head)
r = self.mergeSort(mid_next)
return merge(l, r) | FUNC_DEF IF VAR NONE RETURN VAR RETURN VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR NONE NONE WHILE VAR VAR IF VAR NONE IF VAR NONE RETURN VAR ASSIGN VAR VAR RETURN VAR IF VAR NONE IF VAR NONE RETURN VAR ASSIGN VAR VAR RETURN VAR IF VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN VAR CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | def sortedMerge(head1, head2):
if head1.data > head2.data:
p = head2
end = head2
store = head1
else:
p = head1
end = head1
store = head2
while True:
if p.next == None:
break
if p.next.data < store.data:
p = p.next
else:
temp = p.next
p.next = store
store = temp
p = p.next
if p.next == None:
break
p.next = store
return end
def mergesortt(arr, l, r):
if l < r:
m = (l + r) // 2
head1 = mergesortt(arr, l, m)
head2 = mergesortt(arr, m + 1, r)
head123 = sortedMerge(head1, head2)
return head123
else:
return arr[l]
class Solution:
def mergeSort(self, head):
arr = []
temp = head
r = 0
while temp != None:
arr.append(temp)
nextt = temp.next
temp.next = None
temp = nextt
r += 1
headd = mergesortt(arr, 0, r - 1)
return headd | FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE NUMBER IF VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR BIN_OP VAR NUMBER VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN VAR VAR CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER BIN_OP VAR NUMBER RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def merge(self, cur, right):
if cur == None:
return right
if right == None:
return cur
dummy = Node(-1)
temp = dummy
while cur and right:
if cur.data < right.data:
temp.next = cur
temp = cur
cur = cur.next
else:
temp.next = right
temp = right
right = right.next
while right:
temp.next = right
temp = right
right = right.next
while cur:
temp.next = cur
temp = cur
cur = cur.next
return dummy.next
def find_middle(self, head):
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def mergeSort(self, head):
if head == None or head.next == None:
return head
mid = self.find_middle(head)
cur = head
right = mid.next
mid.next = None
cur = self.mergeSort(cur)
right = self.mergeSort(right)
ans = self.merge(cur, right)
return ans | CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
a = []
while head is not None:
a.append(head.data)
head = head.next
a.sort()
a.append("x")
head = None
tail = None
for ele in a:
if ele != "x":
if head is None:
newnode = Node(ele)
head = newnode
tail = newnode
else:
newnode = Node(ele)
tail.next = newnode
tail = newnode
return head | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR EXPR FUNC_CALL VAR STRING ASSIGN VAR NONE ASSIGN VAR NONE FOR VAR VAR IF VAR STRING IF VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | def sortedMerge(head1, head2):
temp = Node(-1)
curr = temp
while head1 and head2:
if head1.data <= head2.data:
curr.next = Node(head1.data)
head1 = head1.next
curr = curr.next
else:
curr.next = Node(head2.data)
curr = curr.next
head2 = head2.next
while head1:
curr.next = Node(head1.data)
head1 = head1.next
curr = curr.next
while head2:
curr.next = Node(head2.data)
curr = curr.next
head2 = head2.next
return temp.next
def middle(head):
if head == None:
return head
slow, fast = head, head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
return slow
class Solution:
def mergeSort(self, head):
if head == None or head.next == None:
return head
mid = middle(head)
head2 = mid.next
mid.next = None
nhead1 = self.mergeSort(head)
nhead2 = self.mergeSort(head2)
fhead = sortedMerge(nhead1, nhead2)
return fhead | FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def Findmid(self, head):
slow = head
fast = head
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def merge(self, l, r):
ans = Node(0)
temp = ans
if l is None:
return r
if r is None:
return l
while l and r:
if l.data <= r.data:
temp.next = l
temp = l
l = l.next
else:
temp.next = r
temp = r
r = r.next
if l:
while l:
temp.next = l
temp = l
l = l.next
if r:
while r:
temp.next = r
temp = r
r = r.next
return ans.next
def mergeSort(self, head):
if head is None or head.next is None:
return head
temp = head
fast = head
slow = head
while fast and fast.next:
temp = slow
slow = slow.next
fast = fast.next.next
temp.next = None
l = self.mergeSort(head)
r = self.mergeSort(slow)
res = self.merge(l, r)
return res | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if head is None or head.next is None:
return head
slow = head
fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
mid = slow
next_mid = slow.next
slow.next = None
left = self.mergeSort(head)
right = self.mergeSort(next_mid)
sorted_ll = self.sorte(left, right)
return sorted_ll
def sorte(self, left, right):
result = None
if left == right or left == None:
return right
if right == None:
return left
if left.data <= right.data:
result = left
result.next = self.sorte(left.next, right)
else:
result = right
result.next = self.sorte(left, right.next)
return result | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE IF VAR VAR VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
curr = head
if head == None or head.next == None:
return head
mid = Solution().find_mid(head)
left = head
right = mid.next
mid.next = None
head1 = Solution().mergeSort(left)
head2 = Solution().mergeSort(right)
return Solution().merge(head1, head2)
def find_mid(self, head):
if head and head.next:
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
if fast and fast.next:
return slow.next
else:
return slow
def merge(self, head1, head2):
curr1 = head1
curr2 = head2
head = None
curr = None
while curr1 and curr2:
if curr1.data < curr2.data:
node = curr1
curr1 = curr1.next
else:
node = curr2
curr2 = curr2.next
if head == None:
head = node
else:
curr.next = node
curr = node
if curr1:
if head == None:
head = curr1
else:
curr.next = curr1
if curr2:
if head == None:
head = curr2
else:
curr.next = curr2
return head | CLASS_DEF FUNC_DEF ASSIGN VAR VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR VAR RETURN FUNC_CALL FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR RETURN VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR IF VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
def midd(head):
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def merge(head1, head2):
dummy = Node(-1)
dummy_tail = dummy
curr1 = head1
curr2 = head2
while curr1 and curr2:
if curr1.data < curr2.data:
dummy_tail.next = curr1
dummy_tail = curr1
curr1 = curr1.next
else:
dummy_tail.next = curr2
dummy_tail = curr2
curr2 = curr2.next
if curr1:
dummy_tail.next = curr1
if curr2:
dummy_tail.next = curr2
return dummy.next
def merge_sort(head):
if head is None or head.next is None:
return head
mid = midd(head)
left = head
right = mid.next
mid.next = None
left = merge_sort(left)
right = merge_sort(right)
res = merge(left, right)
return res
return merge_sort(head) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if head == None or head.next == None:
return head
def findmid(node):
s = node
f = node.next
while f != None and f.next != None:
s = s.next
f = f.next.next
return s
def merge(le, ri):
ans = Node(None)
tmp = ans
while le != None and ri != None:
if le.data < ri.data:
tmp.next = le
tmp = le
le = le.next
else:
tmp.next = ri
tmp = ri
ri = ri.next
while le != None:
tmp.next = le
tmp = le
le = le.next
while ri != None:
tmp.next = ri
tmp = ri
ri = ri.next
ans = ans.next
return ans
mid = findmid(head)
left = head
right = mid.next
mid.next = None
leftsort = self.mergeSort(left)
rightsort = self.mergeSort(right)
res = merge(leftsort, rightsort)
return res | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NONE ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
l = []
while head:
l.append(head.data)
head = head.next
l.sort()
for i in l:
print(i, end=" ") | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR FOR VAR VAR EXPR FUNC_CALL VAR VAR STRING |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def midOfList(self, head):
if head is None:
return
slow = fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
return slow
def mergeSortedList(self, left, right):
if not left:
return right
if not right:
return left
dummy = Node(-1)
temp = dummy
while left and right:
if left.data >= right.data:
temp.next = right
right = right.next
temp = temp.next
else:
temp.next = left
left = left.next
temp = temp.next
if left:
temp.next = left
if right:
temp.next = right
return dummy.next
def mergeSort(self, head):
if head is None or head.next is None:
return head
mid = self.midOfList(head)
nextMid = mid.next
mid.next = None
left = self.mergeSort(head)
right = self.mergeSort(nextMid)
res = self.mergeSortedList(left, right)
return res | CLASS_DEF FUNC_DEF IF VAR NONE RETURN ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def findMiddle(self, head):
if head == None or head.next == None:
return head
tur = head
hare = head
while hare and hare.next and hare.next.next:
tur = tur.next
hare = hare.next.next
return tur
def merge(self, h1, h2):
dum = Node(-1)
curr = dum
while h1 and h2:
if h1.data <= h2.data:
curr.next = h1
h1 = h1.next
else:
curr.next = h2
h2 = h2.next
curr = curr.next
while h1:
curr.next = h1
h1 = h1.next
curr = curr.next
while h2:
curr.next = h2
h2 = h2.next
curr = curr.next
return dum.next
def solveSort(self, head):
if head == None or head.next == None:
return head
middle = self.findMiddle(head)
nxt = middle.next
middle.next = None
l = self.solveSort(head)
r = self.solveSort(nxt)
return self.merge(l, r)
def mergeSort(self, head):
return self.solveSort(head) | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
tmp = []
curr = prev = head
while curr:
tmp.append(curr.data)
curr = curr.next
tmp.sort()
i = 0
while prev:
prev.data = tmp[i]
i = i + 1
prev = prev.next
return head | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR VAR WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR NUMBER WHILE VAR ASSIGN VAR VAR VAR ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def findmid(self, head):
slow = head
fast = head.next
while fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
return slow
def merge(self, head1, head2):
if head1 == None and head2 == None:
return None
else:
temp = dum = Node(0)
lo = head1
hi = head2
while lo and hi:
if lo.data <= hi.data:
dum.next = lo
lo = lo.next
elif lo.data > hi.data:
dum.next = hi
hi = hi.next
dum = dum.next
if lo:
dum.next = lo
if hi:
dum.next = hi
return temp.next
def mergeSort(self, head):
if head == None or head.next == None:
return head
left = head
mid = self.findmid(left)
right = mid.next
mid.next = None
left = self.mergeSort(left)
right = self.mergeSort(right)
return self.merge(left, right) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN NONE ASSIGN VAR VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Node:
def __init__(self, data):
self.data = data
self.next = None
class Solution:
def mergeSort(self, head):
l = []
a = head
while a is not None:
l += [a.data]
a = a.next
l = sorted(l)
head = None
head = Node(l[0])
n = head
for i in range(1, len(l)):
no = Node(l[i])
no.next = None
n.next = no
n = n.next
return head | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR NONE CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE VAR LIST VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def middle(self, temp):
hare, tort = temp, temp
while hare and hare.next and hare.next.next:
hare = hare.next.next
tort = tort.next
return tort
def sort(self, l, r):
merged = Node(-1)
temp = merged
while l and r:
if l.data < r.data:
temp.next = l
l = l.next
else:
temp.next = r
r = r.next
temp = temp.next
while l:
temp.next = l
l = l.next
temp = temp.next
while r:
temp.next = r
r = r.next
temp = temp.next
return merged.next
def mergeSort(self, head):
if not head or not head.next:
return head
mid = self.middle(head)
nxt = mid.next
mid.next = None
return self.sort(self.mergeSort(head), self.mergeSort(nxt)) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
def sort(head):
n = 0
flag = False
mid = None
tail = head
while tail:
tail = tail.next
if flag:
mid = mid.next if mid else head
flag = not flag
n += 1
if n == 1:
return head
if n % 2 != 0:
mid = mid.next
n1 = head
n2 = mid.next
mid.next = None
n1 = sort(n1)
n2 = sort(n2)
n3 = None
n3_t = None
while n1 and n2:
if n1.data > n2.data:
n1, n2 = n2, n1
node = n1
n1 = n1.next
node.next = None
if n3:
n3_t.next = node
n3_t = n3_t.next
else:
n3 = node
n3_t = n3
if n1 == None:
n1 = n2
n2 = None
if n2 == None:
if n3_t:
n3_t.next = n1
else:
n3 = n1
return n3
return sort(head) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NONE ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR NUMBER IF VAR NUMBER RETURN VAR IF BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR NONE ASSIGN VAR NONE WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE IF VAR NONE IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def getMid(self, head):
s = head
f = head.next
while f and f.next:
s = s.next
f = f.next.next
return s
def merge(self, left, right):
head = Node(-1)
tail = head
while left and right:
if left.data < right.data:
tail.next = Node(left.data)
left = left.next
elif left.data >= right.data:
tail.next = Node(right.data)
right = right.next
tail = tail.next
if left is None:
tail.next = right
if right is None:
tail.next = left
return head.next
def mergeSort(self, head):
if not head or not head.next:
return head
mid = self.getMid(head)
left = head
right = mid.next
mid.next = None
left = self.mergeSort(left)
right = self.mergeSort(right)
result = self.merge(left, right)
return result | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def getSize(self, head):
count = 0
curr_node = head
while curr_node:
count += 1
curr_node = curr_node.next
return count
def splitList(self, source):
if source == None or source.next == None:
frontRef = source
backRef = None
else:
size = self.getSize(source)
length = (size + 1) // 2
frontRef = source
while length > 1:
length -= 1
frontRef = frontRef.next
backRef = frontRef.next
frontRef.next = None
return [source, backRef]
def mergeList(self, head_a, head_b):
result = LinkedList()
if head_a == None:
return head_b
if head_b == None:
return head_a
if head_a.data <= head_b.data:
result.head = head_a
result.head.next = self.mergeList(head_a.next, head_b)
else:
result.head = head_b
result.head.next = self.mergeList(head_a, head_b.next)
return result.head
def mergeSort(self, head):
a = LinkedList()
b = LinkedList()
if head == None or head.next == None:
return head
node_list = self.splitList(head)
a.head = node_list[0]
b.head = node_list[1]
a.head = self.mergeSort(a.head)
b.head = self.mergeSort(b.head)
return self.mergeList(a.head, b.head) | CLASS_DEF FUNC_DEF ASSIGN VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR BIN_OP BIN_OP VAR NUMBER NUMBER ASSIGN VAR VAR WHILE VAR NUMBER VAR NUMBER ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE RETURN LIST VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def merge(self, head1, head2):
newhead = Node(None)
t = newhead
t1 = head1
t2 = head2
while t1 is not None and t2 is not None:
if t1.data < t2.data:
t.next = t1
t1 = t1.next
t = t.next
else:
t.next = t2
t2 = t2.next
t = t.next
while t1 is not None:
t.next = t1
t1 = t1.next
t = t.next
while t2 is not None:
t.next = t2
t2 = t2.next
t = t.next
return newhead.next
def getMid(self, head):
s = head
f = head
p = None
while f is not None and f.next is not None:
p = s
s = s.next
f = f.next.next
return s if f is not None else p
def mergeSort(self, head):
head = self.merge_sort(head)
return head
def merge_sort(self, head):
if head is None or head.next is None:
return head
else:
mid = self.getMid(head)
midNext = mid.next
mid.next = None
head1 = self.merge_sort(head)
head2 = self.merge_sort(midNext)
newHead = self.merge(head1, head2)
return newHead | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR NONE VAR VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def get_mid(self, head):
try:
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
except Exception as e:
print("get_mid: ", e)
def merge(self, head1, head2):
try:
temp1 = head1
temp2 = head2
new_head = new_tail = None
while temp1 and temp2:
if temp1.data <= temp2.data:
if new_head is None:
new_head = new_tail = temp1
else:
new_tail.next = temp1
new_tail = temp1
temp1 = temp1.next
else:
if new_head is None:
new_head = new_tail = temp2
else:
new_tail.next = temp2
new_tail = temp2
temp2 = temp2.next
if temp1:
new_tail.next = temp1
new_tail = temp1
if temp2:
new_tail.next = temp2
new_tail = temp2
return new_head
except Exception as e:
print("merge: ", e)
def mergeSort(self, head):
try:
if head is None or head.next is None:
return head
else:
mid = self.get_mid(head)
left_head = head
right_head = mid.next
mid.next = None
left_head = self.mergeSort(left_head)
right_head = self.mergeSort(right_head)
head = self.merge(left_head, right_head)
return head
except Exception as e:
print("mergeSort: ", e) | CLASS_DEF FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR STRING VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR NONE WHILE VAR VAR IF VAR VAR IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR STRING VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR VAR EXPR FUNC_CALL VAR STRING VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def merge(self, left, right):
dummy = Node(-1)
curr = dummy
while left != None and right != None:
if left.data < right.data:
curr.next = left
curr = left
left = left.next
else:
curr.next = right
curr = right
right = right.next
if left == None:
curr.next = right
if right == None:
curr.next = left
return dummy.next
def mergeSort(self, head):
if head == None or head.next == None:
return head
left = head
slow = head
fast = head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
mid = slow
right = mid.next
mid.next = None
left = self.mergeSort(left)
right = self.mergeSort(right)
ans = self.merge(left, right)
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR NONE VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR IF VAR NONE ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | def merge(head1, head2):
if head2 == None:
return head1
tmp = temp = None
while head1 and head2:
if head1.data <= head2.data:
if not tmp:
temp = head1
tmp = head1
else:
tmp.next = head1
tmp = tmp.next
head1 = head1.next
else:
if not tmp:
temp = head2
tmp = head2
else:
tmp.next = head2
tmp = tmp.next
head2 = head2.next
while head1:
tmp.next = head1
tmp = tmp.next
head1 = head1.next
while head2:
tmp.next = head2
tmp = tmp.next
head2 = head2.next
return temp
def mergesort(head):
if head:
if not head.next:
return head
slow = fast = head
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
nxt = slow.next
slow.next = None
l = mergesort(head)
r = mergesort(nxt)
head = merge(l, r)
return head
return None
class Solution:
def mergeSort(self, head):
return mergesort(head) | FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR NONE WHILE VAR VAR IF VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR IF VAR RETURN VAR ASSIGN VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR RETURN NONE CLASS_DEF FUNC_DEF RETURN FUNC_CALL VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
res = []
while head:
res.append(head.data)
head = head.next
res.sort()
ans = Node(res[0])
curr = ans
for i in range(1, len(res)):
val = Node(res[i])
curr.next = val
curr = val
return ans | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR NUMBER ASSIGN VAR VAR FOR VAR FUNC_CALL VAR NUMBER FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def get_mid(self, head):
slow, fast = head, head.next
while fast != None and fast.next != None:
slow = slow.next
fast = fast.next.next
return slow
def merge(self, head1, head2):
if head1 == None and head2 == None:
return head1
elif head1 == None and head2 != None:
return head2
elif head1 != None and head2 == None:
return head1
new_head = None
itr1, itr2 = head1, head2
if head1.data < head2.data:
new_head = head1
itr1 = itr1.next
else:
new_head = head2
itr2 = itr2.next
temp = new_head
while itr1 and itr2:
if itr1.data < itr2.data:
temp.next = itr1
itr1 = itr1.next
else:
temp.next = itr2
itr2 = itr2.next
temp = temp.next
while itr1:
temp.next = itr1
itr1 = itr1.next
temp = temp.next
while itr2:
temp.next = itr2
itr2 = itr2.next
temp = temp.next
return new_head
def mergeSort(self, head):
if head == None or head.next == None:
return head
left = head
temp = self.get_mid(head)
right = temp.next
temp.next = None
left = self.mergeSort(left)
right = self.mergeSort(right)
head = self.merge(left, right)
return head | CLASS_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR NONE ASSIGN VAR VAR VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if not head or not head.next:
return head
left = head
mid = self.getMid(head)
right = mid.next
mid.next = None
l = self.mergeSort(left)
r = self.mergeSort(right)
return self.merge(l, r)
def getMid(self, head):
slow, fast = head, head.next
while fast and fast.next:
slow = slow.next
fast = fast.next.next
return slow
def merge(self, l, r):
dummy = Node(0)
tail = dummy
while l and r:
if l.data <= r.data:
tail.next = l
l = l.next
else:
tail.next = r
r = r.next
tail = tail.next
if l:
tail.next = l
if r:
tail.next = r
return dummy.next | CLASS_DEF FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def merging(self, a, b):
if a == None:
return b
if b == None:
return a
result = None
if a.data > b.data:
result = b
result.next = self.merging(a, b.next)
else:
result = a
result.next = self.merging(a.next, b)
return result
def mergeSort(self, head):
if head == None or head.next == None:
return head
middle = self.middleOne(head)
second = middle.next
middle.next = None
left = self.mergeSort(head)
right = self.mergeSort(second)
new = self.merging(left, right)
return new
def middleOne(self, head):
fast = head
slow = head
while fast.next != None and fast.next.next != None:
slow = slow.next
fast = fast.next.next
return slow | CLASS_DEF FUNC_DEF IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR ASSIGN VAR NONE IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
def merge(temp1, temp2):
if not temp1:
return temp2
if not temp2:
return temp1
temp = Node(0)
h = temp
while temp1 and temp2:
if temp1.data <= temp2.data:
temp.next = temp1
temp = temp.next
temp1 = temp1.next
else:
temp.next = temp2
temp = temp.next
temp2 = temp2.next
if temp1:
temp.next = temp1
elif temp2:
temp.next = temp2
h = h.next
return h
def mergesort(head):
if not head or not head.next:
return head
fast = head
slow = head
while fast and fast.next:
temp = slow
slow = slow.next
fast = fast.next.next
temp.next = None
left = mergesort(head)
right = mergesort(slow)
return merge(left, right)
return mergesort(head) | CLASS_DEF FUNC_DEF FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR RETURN FUNC_CALL VAR VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if not head:
return head
if not head.next:
return head
slow = head
fast = head
while fast and fast.next and fast.next.next:
fast = fast.next.next
slow = slow.next
temp = slow.next
slow.next = None
l = self.mergeSort(head)
r = self.mergeSort(temp)
return self.merge(l, r)
def merge(self, l1, l2):
if not l1:
return l2
elif not l2:
return l1
if l1.data <= l2.data:
l1.next = self.merge(l1.next, l2)
return l1
else:
l2.next = self.merge(l1, l2.next)
return l2 | CLASS_DEF FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR RETURN FUNC_CALL VAR VAR VAR FUNC_DEF IF VAR RETURN VAR IF VAR RETURN VAR IF VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
lst = []
while head:
lst.append(head.data)
head = head.next
lst.sort()
a = Node(0)
tmp = a
for i in lst:
tmp.next = Node(i)
tmp = tmp.next
return a.next | CLASS_DEF FUNC_DEF ASSIGN VAR LIST WHILE VAR EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR FOR VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
a = []
t = head
while t != None:
a.append(t.data)
t = t.next
a.sort()
t = head
i = 0
while t != None:
t.data = a[i]
i += 1
t = t.next
return head | CLASS_DEF FUNC_DEF ASSIGN VAR LIST ASSIGN VAR VAR WHILE VAR NONE EXPR FUNC_CALL VAR VAR ASSIGN VAR VAR EXPR FUNC_CALL VAR ASSIGN VAR VAR ASSIGN VAR NUMBER WHILE VAR NONE ASSIGN VAR VAR VAR VAR NUMBER ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
if head == None or head.next == None:
return head
middle = self.getmiddle(head)
nextmiddle = middle.next
middle.next = None
left = self.mergeSort(head)
right = self.mergeSort(nextmiddle)
sortedlist = self.merge(left, right)
return sortedlist
def merge(self, a, b):
result = None
if a == None:
return b
if b == None:
return a
if a.data <= b.data:
result = a
result.next = self.merge(a.next, b)
else:
result = b
result.next = self.merge(a, b.next)
return result
def getmiddle(self, head):
if head == None:
return head
slow = head
fast = head
while fast.next != None and fast.next.next != None:
slow = slow.next
fast = fast.next.next
return slow | CLASS_DEF FUNC_DEF IF VAR NONE VAR NONE RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR NONE IF VAR NONE RETURN VAR IF VAR NONE RETURN VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF IF VAR NONE RETURN VAR ASSIGN VAR VAR ASSIGN VAR VAR WHILE VAR NONE VAR NONE ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR |
Given Pointer/Reference to the head of the linked list, the task is to Sort the given linked list using Merge Sort.
Note: If the length of linked list is odd, then the extra node should go in the first list while splitting.
Example 1:
Input:
N = 5
value[] = {3,5,2,4,1}
Output: 1 2 3 4 5
Explanation: After sorting the given
linked list, the resultant matrix
will be 1->2->3->4->5.
Example 2:
Input:
N = 3
value[] = {9,15,0}
Output: 0 9 15
Explanation: After sorting the given
linked list , resultant will be
0->9->15.
Your Task:
For C++ and Python: The task is to complete the function mergeSort() which sort the linked list using merge sort function.
For Java: The task is to complete the function mergeSort() and return the node which can be used to print the sorted linked list.
Expected Time Complexity: O(N*Log(N))
Expected Auxiliary Space: O(N)
Constraints:
1 <= N <= 10^{5} | class Solution:
def mergeSort(self, head):
def findmid(node):
slow, fast = node, node
while fast.next and fast.next.next:
slow = slow.next
fast = fast.next.next
return slow
def merge(start):
if not start or not start.next:
return start
middle = findmid(start)
mid = middle.next
middle.next = None
left = merge(start)
right = merge(mid)
result = mergelists(left, right)
return result
def mergelists(left, right):
res = Node(0)
dummy = res
if not left:
return right
if not right:
return left
while left and right:
if left.data <= right.data:
res.next = left
left = left.next
else:
res.next = right
right = right.next
res = res.next
if left:
res.next = left
if right:
res.next = right
return dummy.next
return merge(head) | CLASS_DEF FUNC_DEF FUNC_DEF ASSIGN VAR VAR VAR VAR WHILE VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR RETURN VAR FUNC_DEF IF VAR VAR RETURN VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR VAR ASSIGN VAR NONE ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR VAR RETURN VAR FUNC_DEF ASSIGN VAR FUNC_CALL VAR NUMBER ASSIGN VAR VAR IF VAR RETURN VAR IF VAR RETURN VAR WHILE VAR VAR IF VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR IF VAR ASSIGN VAR VAR RETURN VAR RETURN FUNC_CALL VAR VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for _ in range(t):
number = int(input())
list_number = list(map(int, input().split()))
set_numbers = list(set(list_number))
l = len(set_numbers)
if number == 1:
print(0)
elif l == 1:
print(0)
elif len(list_number) == l:
print(-1)
else:
list_number.sort()
last = 0
max_freq = 0
for i in range(0, number - 1):
freq = 0
if list_number[i] != list_number[i + 1]:
freq = i + 1 - last
last = i + 1
if max_freq < freq:
max_freq = freq
if number - last > max_freq:
max_freq = number - last
print(number + 1 - max_freq) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR NUMBER BIN_OP VAR NUMBER ASSIGN VAR NUMBER IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER IF VAR VAR ASSIGN VAR VAR IF BIN_OP VAR VAR VAR ASSIGN VAR BIN_OP VAR VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | tc = int(input())
for _ in range(tc):
n = int(input())
a = [int(x) for x in input().split()]
m = 1
c = 1
a.sort()
for i in range(n):
if a[i] == a[i - 1]:
c += 1
m = max(m, c)
else:
c = 1
if n == m:
print(0)
continue
if m == 1:
print(-1)
continue
print(n - m + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | for _ in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
freq = {}
for i in a:
if i in freq:
freq[i] += 1
else:
freq[i] = 1
if len(freq) == 1:
print(0)
continue
if len(freq) == n:
print(-1)
continue
ans = float("inf")
for i in freq:
if freq[i] > 1:
ans = min(n - freq[i] + 1, ans)
print(ans) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR VAR NUMBER VAR EXPR FUNC_CALL VAR VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | for i in range(int(input())):
n = int(input())
arr = list(map(int, input().split()))
d = {}
for num in arr:
if num in d:
d[num] += 1
else:
d[num] = 1
max_freq = max(d.values())
if n == 1 or max_freq == n:
print(0)
elif max_freq >= 2:
print(n - max_freq + 1)
else:
print(-1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | import sys
def execute(n, values):
if n == 1:
return 0
count_dict = dict()
max_count = -1
for val in values:
if val in count_dict:
count_dict[val] += 1
else:
count_dict[val] = 1
if count_dict[val] > max_count:
max_count = count_dict[val]
if max_count == 1:
return -1
if len(count_dict.keys()) == 1:
return 0
total_sum = n - max_count - 1 + 2
return int(total_sum)
t = int(input())
while t:
t -= 1
n = int(input())
values = list(map(int, input().split()))
print(execute(n, values)) | IMPORT FUNC_DEF IF VAR NUMBER RETURN NUMBER ASSIGN VAR FUNC_CALL VAR ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER RETURN NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER RETURN NUMBER ASSIGN VAR BIN_OP BIN_OP BIN_OP VAR VAR NUMBER NUMBER RETURN FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR FUNC_CALL VAR VAR VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | T = int(input())
for _ in range(T):
N = int(input())
LL = list(map(int, input().split()))
freqDict = dict()
for item in LL:
if item in freqDict:
freqDict[item] += 1
else:
freqDict[item] = 1
maxFrq = max(list(freqDict.values()))
if maxFrq == 1:
if len(list(freqDict.keys())) == 1:
print("0")
else:
print("-1")
elif len(list(freqDict.keys())) == 1:
print("0")
else:
minOper = N - maxFrq + 1
print(minOper) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR STRING IF FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
while t:
t -= 1
n = int(input())
elems = [int(i) for i in input().split(" ")]
freq = {}
for i in range(len(elems)):
if elems[i] not in freq:
freq[elems[i]] = 0
freq[elems[i]] += 1
maxf = max(list(freq.values()))
minops = n - maxf + 1
if minops == 1:
print(0)
elif minops == n:
print(-1)
else:
print(minops) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for x in range(t):
n = int(input())
a = list(map(int, input().split()))
a.sort()
dic = {}
if n == 1:
print("0")
else:
max_total = 0
max_no = 0
for i in range(n):
if a[i] not in dic:
dic[a[i]] = 1
else:
dic[a[i]] = dic[a[i]] + 1
if dic[a[i]] > max_total:
max_total = dic[a[i]]
max_no = a[i]
if max_total == 1:
print("-1")
elif max_total == n:
print("0")
else:
print(n - max_total + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR EXPR FUNC_CALL VAR ASSIGN VAR DICT IF VAR NUMBER EXPR FUNC_CALL VAR STRING ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER ASSIGN VAR VAR VAR BIN_OP VAR VAR VAR NUMBER IF VAR VAR VAR VAR ASSIGN VAR VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR STRING IF VAR VAR EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
while t != 0:
try:
n = int(input())
a = list(map(int, input().split(" ")))
m = 1
x = 1
a.sort()
for i in range(n):
if a[i] == a[i - 1]:
x = x + 1
m = max(m, x)
else:
x = 1
if n == m:
print("0")
continue
if m == 1:
print("-1")
continue
print(n - m + 1)
except EOFError:
break
t = t - 1 | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR WHILE VAR NUMBER 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 EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER ASSIGN VAR BIN_OP VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR STRING EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER VAR ASSIGN VAR BIN_OP VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split(" ")))
if n == 1:
print(0)
else:
dic = {}
for item in a:
if item in dic:
dic[item] += 1
else:
dic[item] = 1
maximum = 1
for item in dic:
if dic[item] > maximum:
maximum = dic[item]
if maximum == 1:
print(-1)
elif maximum == n:
print(0)
else:
print(n - maximum + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR STRING IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for _ in range(t):
n = int(input())
a = list(map(int, input().split()))
if n == 1:
print(0)
else:
m = len(set(a))
if m == n:
print(-1)
else:
d = {}
for x in a:
if x in d:
d[x] += 1
else:
d[x] = 1
p = max(d.values())
if p == n:
print(0)
else:
res = n - p + 1
print(res) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | nbListe = int(input())
for _ in range(nbListe):
nbNombre = int(input())
l = list(map(int, input().split()))
k = list(set(l))
d = {}
d = dict.fromkeys(k, 0)
for j in l:
d[j] += 1
max = 0
for i in d:
if max < d[i]:
max = d[i]
if max == nbNombre:
print(0)
elif max == 1 and len(d.keys()) == nbNombre:
print(-1)
else:
print(nbNombre - max + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR ASSIGN VAR DICT ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR VAR VAR NUMBER ASSIGN VAR NUMBER FOR VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | for _ in range(int(input())):
n = int(input())
arr = [int(x) for x in input().split(" ")]
d1 = {}
for i in arr:
if i not in d1:
d1[i] = 1
else:
d1[i] += 1
m1 = max(d1.values())
if m1 == n:
print(0)
elif m1 < 2:
print(-1)
else:
print(n - m1 + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR STRING ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | for t in range(int(input())):
n = int(input())
di = {}
val = 1
li = list(map(int, input().split()))
if len(li) < 2:
print(0)
else:
for i in li:
val = i
if val in di:
di[val] += 1
else:
di[val] = 1
if len(di) == n:
print(-1)
elif len(di) == 1:
print(0)
else:
maxi = max(di.values())
print(n - maxi + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR VAR ASSIGN VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for i in range(t):
n = int(input())
d = {}
m = 0
l = input().split(" ")
for j in l:
if j in d:
d[j] += 1
if d[j] >= 2 and d[j] > m:
m = d[j]
else:
d[j] = 1
if len(d.keys()) == 1:
print(0)
elif m == 0:
print(-1)
else:
print(int(n - (m - 2) - 1)) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL FUNC_CALL VAR STRING FOR VAR VAR IF VAR VAR VAR VAR NUMBER IF VAR VAR NUMBER VAR VAR VAR ASSIGN VAR VAR VAR ASSIGN VAR VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR FUNC_CALL VAR BIN_OP BIN_OP VAR BIN_OP VAR NUMBER NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for _ in range(t):
n = int(input())
a = [int(i) for i in input().split()]
maxCount = 1
m = {}
for x in a:
if x not in m:
m[x] = 1
else:
m[x] += 1
for freq in m.values():
maxCount = max(maxCount, freq)
ans = -1
if n == 1 or maxCount == n:
ans = 0
elif maxCount > 1:
ans = n - maxCount + 1
print(ans) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR DICT FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER FOR VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER VAR VAR ASSIGN VAR NUMBER IF VAR NUMBER ASSIGN VAR BIN_OP BIN_OP VAR VAR NUMBER EXPR FUNC_CALL VAR VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | import sys
from sys import maxsize
def get_ints():
return map(int, sys.stdin.readline().strip().split())
def get_list():
return list(map(int, sys.stdin.readline().strip().split()))
def get_list_string():
return list(map(str, sys.stdin.readline().strip().split()))
def get_string():
return sys.stdin.readline().strip()
def get_int():
return int(sys.stdin.readline().strip())
def get_print_int(x):
sys.stdout.write(str(x) + "\n")
def get_print(x):
sys.stdout.write(x + "\n")
def get_print_int_same(x):
sys.stdout.write(str(x) + " ")
def get_print_same(x):
sys.stdout.write(x + " ")
def solve():
for _ in range(get_int()):
n = get_int()
arr = get_list()
d = dict()
for i in range(n):
if arr[i] not in d:
d[arr[i]] = 1
else:
d[arr[i]] += 1
if len(d) == 1:
get_print_int(0)
elif len(d) == n:
get_print_int(-1)
else:
get_print_int(n - 1 - (max(d.values()) - 2))
solve() | IMPORT FUNC_DEF RETURN FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL FUNC_CALL VAR FUNC_DEF RETURN FUNC_CALL VAR FUNC_CALL FUNC_CALL VAR FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP FUNC_CALL VAR VAR STRING FUNC_DEF EXPR FUNC_CALL VAR BIN_OP VAR STRING FUNC_DEF FOR VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER IF FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR NUMBER BIN_OP FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | T = int(input())
for test in range(T):
N = int(input())
A = [int(x) for x in input().split()]
if N == 0:
print(-1)
elif N == 1:
print(0)
elif len(set(A)) == 1:
print(0)
else:
mx = 1
c = 1
A.sort()
for i in range(N):
if A[i] == A[i - 1]:
c += 1
mx = max(mx, c)
else:
c = 1
if mx == N:
print(0)
elif mx == 1:
print(-1)
else:
print(N - mx + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR VAR VAR FUNC_CALL FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER EXPR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR BIN_OP VAR NUMBER VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR VAR ASSIGN VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | for t in range(int(input())):
n = int(input())
a = list(map(int, input().split()))
d = dict()
if n == 1:
print(0)
continue
for i in range(n):
if not a[i] in d:
d[a[i]] = 0
d[a[i]] += 1
max_freq = 0
max_freq_key = 1
for i in d.keys():
if d[i] >= max_freq:
max_freq_key = i
max_freq = d[i]
if max_freq == 1:
print(-1)
else:
if max_freq == n:
print(0)
continue
print(n - max_freq + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR VAR IF VAR VAR VAR ASSIGN VAR VAR VAR NUMBER VAR VAR VAR NUMBER ASSIGN VAR NUMBER ASSIGN VAR NUMBER FOR VAR FUNC_CALL VAR IF VAR VAR VAR ASSIGN VAR VAR ASSIGN VAR VAR VAR IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR VAR EXPR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR BIN_OP BIN_OP VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | for a in range(int(input())):
n = int(input())
l = list(map(int, input().split()))
if len(set(l)) == 1:
print(0)
elif len(set(l)) == len(l):
print(-1)
else:
d = {}
m = 0
c = d.fromkeys(l, 0)
for i in l:
if i in c:
c[i] += 1
m = max(c.values())
print(len(l) - m + 1) | FOR VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL FUNC_CALL VAR IF FUNC_CALL VAR FUNC_CALL VAR VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF FUNC_CALL VAR FUNC_CALL VAR VAR FUNC_CALL VAR VAR EXPR FUNC_CALL VAR NUMBER ASSIGN VAR DICT ASSIGN VAR NUMBER ASSIGN VAR FUNC_CALL VAR VAR NUMBER FOR VAR VAR IF VAR VAR VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR EXPR FUNC_CALL VAR BIN_OP BIN_OP FUNC_CALL VAR VAR VAR NUMBER |
You are given a positive integer N and an array A of size N. There are N lists L_{1}, L_{2} \ldots L_{N}. Initially, L_{i} = [A_{i}].
You can perform the following operation any number of times as long as there are at least 2 lists:
Select 2 (non-empty) lists L_{i} and L_{j} (i \neq j)
Append L_{j} to L_{i} and remove the list L_{j}. Note that this means L_{j} cannot be chosen in any future operation.
Find the minimum number of operations required to obtain a set of lists that satisfies the following conditions:
The first element and last element of each list are equal.
The first element of all the lists is the same.
Print -1 if it is not possible to achieve this via any sequence of operations.
------ Input Format ------
- The first line of 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 an integer N.
- The second line of each test case contains N space-separated integers A_{1}, A_{2}, \ldots, A_{N}.
------ Output Format ------
For each test case, print a single line containing one integer: the minimum number of operations required to obtain an array of lists that satisfies the given conditions.
Print -1 if it is impossible to achieve such an array of lists.
------ Constraints ------
$1 β€ T β€ 10^{5}$
$1 β€ N β€ 2 \cdot 10^{5}$
$1 β€ A_{i} β€ N$
- Sum of $N$ over all test cases doesn't exceed $2 \cdot 10^{5}$
------ subtasks ------
Subtask 1(100 points): Original constraints
----- Sample Input 1 ------
3
1
1
2
1 2
3
1 1 2
----- Sample Output 1 ------
0
-1
2
----- explanation 1 ------
Test case $1$: There is only one list $[1]$, and it trivially satisfies the condition so no operations are required.
Test case $2$: There are only $2$ ways to do an operation - either take list $[1]$ and append it to list $[2]$ or take list $[2]$ and append it to list $[1]$. In both cases, it is not possible to satisfy both given conditions at the same time. Hence, the answer is $-1$.
Test case $3$: Here is one possible order of operations:
- Select the $3$rd list $[2]$ and append it to the $1$st list $[1]$.
- Then, select the $2$nd list $[1]$ and append it to the $1$st list $[1, 2]$.
Finally, we are left with the single list $[1, 2, 1]$ which satisfies the given conditions. It can be verified that it is impossible to do this using less than $2$ operations. | t = int(input())
for _ in range(t):
n = int(input())
z = input().split()
l = dict()
for i in z:
if i not in l:
l[i] = 1
else:
l[i] += 1
j = max(list(l.values()))
x = 0
sum = 0
if n == 1 or len(list(l.values())) == 1:
print(0)
elif j == 1:
print(-1)
else:
for i in list(l.values()):
if i != j or x > 0:
sum += i
elif x == 0:
x += 1
print(sum + 1) | ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FOR VAR FUNC_CALL VAR VAR ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR FUNC_CALL FUNC_CALL VAR ASSIGN VAR FUNC_CALL VAR FOR VAR VAR IF VAR VAR ASSIGN VAR VAR NUMBER VAR VAR NUMBER ASSIGN VAR FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR ASSIGN VAR NUMBER ASSIGN VAR NUMBER IF VAR NUMBER FUNC_CALL VAR FUNC_CALL VAR FUNC_CALL VAR NUMBER EXPR FUNC_CALL VAR NUMBER IF VAR NUMBER EXPR FUNC_CALL VAR NUMBER FOR VAR FUNC_CALL VAR FUNC_CALL VAR IF VAR VAR VAR NUMBER VAR VAR IF VAR NUMBER VAR NUMBER EXPR FUNC_CALL VAR BIN_OP VAR NUMBER |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.