10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Started on Monday, 29 September 2025, 1:58 PM
State Finished
Completed on Wednesday, 1 October 2025, 5:32 PM
Time taken 2 days 3 hours
Marks 10.00/10.00
Grade 100.00 out of 100.00
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 1/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 1 Correct Mark 1.00 out of 1.00
Assume you have an array of length n initialized with all 0's and are given k update operations.
Each operation is represented as a triplet: [startIndex, endIndex, inc] which increments each element of subarray A[startIndex ...
endIndex] (startIndex and endIndex inclusive) with inc.
Return the modified array after all k operations were executed.
Example:
Input:
132
243
0 2 -2
Output:
-2 0 3 5 3
Explanation:
Initial state:
length = 5, updates = [[1,3,2],[2,4,3],[0,2,-2]]
[0,0,0,0,0]
After applying operation [1,3,2]:
[0,2,2,2,0]
After applying operation [2,4,3]:
[0,2,5,5,3]
After applying operation [0,2,-2]:
[-2,0,3,5,3]
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 2/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
def apply_operations(n, operations):
arr = [0] * (n + 1)
for start, end, inc in operations:
arr[start] += inc
if end + 1 < n:
arr[end + 1] -= inc
for i in range(1, n):
arr[i] += arr[i - 1]
return arr[:n]
n = int(input())
k = int(input())
operations = []
for _ in range(k):
[Link](list(map(int, input().split())))
Input Expected Got
5 -2 0 3 5 3 -2 0 3 5 3
3
1 3 2
2 4 3
0 2 -2
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 3/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 2 Correct Mark 1.00 out of 1.00
Given an array A of sorted integers and another non negative integer k, find if there exists 2 indices i and j such that A[i] - A[j] = k, i != j.
Input Format
1. First line is number of test cases T. Following T lines contain:
2. N, followed by N integers of the array
3. The non-negative integer k
Output format
Print 1 if such a pair exists and 0 if it doesn’t.
Example
Input
Output:
Input
99
Output
For example:
Input Result
1 1
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 4/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Input Result
1 0
99
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
def check_pair_exists(arr, n, k):
i, j = 0, 1
while i < n and j < n:
if i == j:
j += 1
continue
diff = arr[j] - arr[i]
if diff == k:
return 1
elif diff < k:
j += 1
else:
i += 1
return 0
t = int(input().strip())
for _ in range(t):
Input Expected Got
1 1 1
3
1 0 0
3
99
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 5/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 6/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 3 Correct Mark 1.00 out of 1.00
The program must accept N integers and an integer K as the input. The program must print every K integers in descending order as the
output.
Note: If N % K != 0, then sort the final N%K integers in descending order.
Boundary Condition(s):
1 <= N <= 10^4
-99999 <= Array Element Value <= 99999
Input Format:
The first line contains the values of N and K separated by a space.
The second line contains N integers separated by space(s).
Output Format:
The first line contains N integers.
Example Input/Output 1:
Input:
73
48 541 23 68 13 41 6
Output:
541 48 23 68 41 13 6
Explanation:
The first three integers are 48 541 23, after sorting in descending order the integers are 541 48 23.
The second three integers are 68 13 41, after sorting in descending order the integers are 68 41 13.
The last integer is 6.
The integers are 541 48 23 68 41 13 6
Hence the output is 541 48 23 68 41 13 6.
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 7/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
N, K = map(int, input().split())
arr = list(map(int, input().split()))
result = []
for i in range(0, N, K):
chunk = arr[i:i+K]
[Link](reverse=True)
[Link](chunk)
print(' '.join(map(str, result)))
Input Expected Got
7 3 541 48 23 68 41 13 6 541 48 23 68 41 13 6
48 541 23 68 13 41 6
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 8/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 4 Correct Mark 1.00 out of 1.00
An array is monotonic if it is either monotone increasing or monotone decreasing.
An array A is monotone increasing if for all i <= j, A[i] <= A[j]. An array A is monotone decreasing if for all i <= j, A[i] >= A[j].
Write a program if n array is monotonic or not. Print "True" if is monotonic or "False" if it is not. Array can be monotone increasing or
decreasing.
Input Format:
First line n-get number of elements
Next n Lines is the array of elements
Output Format:
True ,if array is monotone increasing or decreasing.
otherwise False is printed
Sample Input1
Sample Output1
True
Sample Input2
Sample Output2
True
Sample Input 3
Sample Output3
False
For example:
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 9/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Input Result
4 True
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
n = int(input())
arr = [int(input()) for _ in range(n)]
inc = dec = True
for i in range(1, n):
if arr[i] < arr[i-1]:
inc = False
if arr[i] > arr[i-1]:
dec = False
print("True" if inc or dec else "False")
Input Expected Got
4 True True
6
4 False False
3
4 False False
1
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 10/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Input Expected Got
4 True True
9
3 False False
2
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 11/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 5 Correct Mark 1.00 out of 1.00
Given two arrays of positive integers, for each element in the second array, find the total number of elements in the first array which are less than or equal to that
element. Store the values determined in an array.
For example, if the first array is [1, 2, 3] and the second array is [2, 4], then there are 2 elements in the first array less than or equal to 2. There are 3 elements in
the first array which are less than or equal to 4. We can store these answers in an array, answer = [2, 3].
Program Description
The program must return an array of m positive integers, one for each maxes[i] representing the total number of elements nums[j] satisfying nums[j] ≤ maxes[i]
where 0 ≤ j < n and 0 ≤ i < m, in the given order.
The program has the following:
nums[nums[0],...nums[n-1]]: first array of positive integers
maxes[maxes[0],...maxes[n-1]]: second array of positive integers
Constraints
· 2 ≤ n, m ≤ 105
· 1 ≤ nums[j] ≤ 109, where 0 ≤ j < n.
· 1 ≤ maxes[i] ≤ 109, where 0 ≤ i < m.
Input Format For Custom Testing
Input from stdin will be processed as follows and passed to the program.
The first line contains an integer n, the number of elements in nums.
The next n lines each contain an integer describing nums[j] where 0 ≤ j < n.
The next line contains an integer m, the number of elements in maxes.
The next m lines each contain an integer describing maxes[i] where 0 ≤ i < m.
Sample Case 0
Sample Input 0
4
1
4
2
4
2
3
5
Sample Output 0
2
4
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 12/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Explanation 0
We are given n = 4, nums = [1, 4, 2, 4], m = 2, and maxes = [3, 5].
1. For maxes[0] = 3, we have 2 elements in nums (nums[0] = 1 and nums[2] = 2) that are ≤ maxes[0].
2. For maxes[1] = 5, we have 4 elements in nums (nums[0] = 1, nums[1] = 4, nums[2] = 2, and nums[3] = 4) that are ≤ maxes[1].
Thus, the program returns the array [2, 4] as the answer.
Sample Case 1
Sample Input 1
5
2
10
5
4
8
4
3
1
7
8
Sample Output 1
1
0
3
4
Explanation 1
We are given, n = 5, nums = [2, 10, 5, 4, 8], m = 4, and maxes = [3, 1, 7, 8].
1. For maxes[0] = 3, we have 1 element in nums (nums[0] = 2) that is ≤ maxes[0].
2. For maxes[1] = 1, there are 0 elements in nums that are ≤ maxes[1].
3. For maxes[2] = 7, we have 3 elements in nums (nums[0] = 2, nums[2] = 5, and nums[3] = 4) that are ≤ maxes[2].
4. For maxes[3] = 8, we have 4 elements in nums (nums[0] = 2, nums[2] = 5, nums[3] = 4, and nums[4] = 8) that are ≤ maxes[3].
Thus, the program returns the array [1, 0, 3, 4] as the answer.
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 13/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
import bisect
n = int(input())
nums = [int(input()) for _ in range(n)]
m = int(input())
maxes = [int(input()) for _ in range(m)]
[Link]()
for x in maxes:
count = bisect.bisect_right(nums, x)
print(count)
Input Expected Got
4 2 2
1 4 4
5 1 1
2 0 0
10 3 3
5 4 4
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 14/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 6 Correct Mark 1.00 out of 1.00
Given a matrix mat where every row is sorted in strictly increasing order, return the smallest common element in all rows.
If there is no common element, return -1.
Example 1:
Input:
45
12345
2 4 5 8 10
3 5 7 9 11
13579
Output:
Constraints:
1 <= [Link], mat[i].length <= 500
1 <= mat[i][j] <= 10^4
mat[i] is sorted in strictly increasing order.
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 15/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
n, m = map(int, input().split())
matrix = [list(map(int, input().split())) for _ in range(n)]
common = set(matrix[0])
for row in matrix[1:]:
common &= set(row)
if common:
print(min(common))
else:
print(-1)
Input Expected Got
4 5 5 5
1 2 3 4 5
2 4 5 8 10
3 5 7 9 11
1 3 5 7 9
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 16/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 7 Correct Mark 1.00 out of 1.00
Program to print all the distinct elements in an array. Distinct elements are nothing but the unique (non-duplicate) elements present in the
given array.
Input Format:
First line take an Integer input from stdin which is array length n.
Second line take n Integers which is inputs of array.
Output Format:
Print the Distinct Elements in Array in single line which is space Separated
Example Input:
Output:
1234
Example Input:
Output:
123
For example:
Input Result
5 1 2 3 4
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 17/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Input Result
6 1 2 3
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
x = int(input())
lst = []
for i in range(x):
y = int(input())
[Link](y)
lst1 = set(lst)
lst1 = list(lst1)
for k in range(len(lst1)):
print(lst1[k], end=" ")
Input Expected Got
5 1 2 3 4 1 2 3 4
1
6 1 2 3 1 2 3
1
Passed all tests!
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 18/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 19/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 8 Correct Mark 1.00 out of 1.00
Determine the factors of a number (i.e., all positive integer values that evenly divide into a number) and then return the pth element of the
list, sorted ascending. If there is no pth element, return 0.
Example
n = 20
p=3
The factors of 20 in ascending order are {1, 2, 4, 5, 10, 20}. Using 1-based indexing, if p = 3, then 4 is returned. If p > 6, 0 would be returned.
Constraints
1 ≤ n ≤ 1015
1 ≤ p ≤ 109
The first line contains an integer n, the number to factor.
The second line contains an integer p, the 1-based index of the factor to return.
Sample Case 0
Sample Input 0
10
Sample Output 0
Explanation 0
Factoring n = 10 results in {1, 2, 5, 10}. Return the p = 3rd factor, 5, as the answer.
Sample Case 1
Sample Input 1
10
Sample Output 1
Explanation 1
Factoring n = 10 results in {1, 2, 5, 10}. There are only 4 factors and p = 5, therefore 0 is returned as the answer.
Sample Case 2
Sample Input 2
Sample Output 2
Explanation 2
Factoring n = 1 results in {1}. The p = 1st factor of 1 is returned as the answer.
For example:
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 20/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Input Result
10 5
10 0
1 1
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
def get_pth_factor(n, p):
factors = []
for i in range(1, int(n ** 0.5) + 1):
if n % i == 0:
[Link](i)
if i != n // i:
[Link](n // i)
[Link]()
if p <= len(factors):
return factors[p - 1]
else:
return 0
n = int(input())
p = int(input())
print(get_pth_factor(n, p))
Input Expected Got
10 5 5
3
10 0 0
5
1 1 1
1
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 21/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 9 Correct Mark 1.00 out of 1.00
Complete the program to count frequency of each element of an array. Frequency of a particular element will be printed
once.
Sample Test Cases
Test Case 1
Input
7
23
45
23
56
45
23
40
Output
23 occurs 3 times
45 occurs 2 times
56 occurs 1 times
40 occurs 1 times
Answer: (penalty regime: 0 %)
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
n = int(input())
arr = [int(input()) for _ in range(n)]
freq = {}
for val in arr:
freq[val] = [Link](val, 0) + 1
printed = set()
for val in arr:
if val not in printed:
print(f"{val} occurs {freq[val]} times")
[Link](val)
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 22/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Input Expected Got
7 23 occurs 3 times 23 occurs 3 times
23 45 occurs 2 times 45 occurs 2 times
45 56 occurs 1 times 56 occurs 1 times
23 40 occurs 1 times 40 occurs 1 times
56
45
23
40
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 23/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
Question 10 Correct Mark 1.00 out of 1.00
Given an integer n, return an list of length n + 1 such that for each i (0 <= i <= n), ans[i] is the number of 1's in the binary representation of i.
Example:
Input: n = 2
Output: [0,1,1]
Explanation:
0 --> 0
1 --> 1
2 --> 10
Example2:
Input: n = 5
Output: [0,1,1,2,1,2]
Explanation:
0 --> 0
1 --> 1
2 --> 10
3 --> 11
4 --> 100
5 --> 101
Note: Complete the given function alone
For example:
Test Result
print(CountingBits(5)) [0, 1, 1, 2, 1, 2]
Answer: (penalty regime: 0 %)
Reset answer
Ace editor not ready. Perhaps reload page?
Falling back to raw text area.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 24/25
10/1/25, 5:32 PM Week-07-Coding-Lists: Attempt review
def CountingBits(n):
return [bin(i).count('1') for i in range(n+1)]
Test Expected Got
print(CountingBits(2)) [0, 1, 1] [0, 1, 1]
print(CountingBits(5)) [0, 1, 1, 2, 1, 2] [0, 1, 1, 2, 1, 2]
Passed all tests!
Correct
Marks for this submission: 1.00/1.00.
[Link]/moodle/mod/quiz/[Link]?attempt=252328&cmid=2549 25/25