DataStructure and Algorithms Solutions with Supercool 💯
Открыть в Telegram
DSA with supercool For paid projects:- Email :- supercool7151@gmail.com Follow me on social media https://linktr.ee/codemaking
Больше643
Подписчики
Нет данных24 часа
Нет данных7 дней
Нет данных30 день
Архив постов
```def print_matrix_in_spiral_order(matrix):
row, col = 0, 0
num_rows, num_cols = len(matrix), len(matrix[0])
printed_elements = set()
while len(printed_elements) < num_rows * num_cols:
while col < num_cols and matrix[row][col] not in printed_elements:
print(matrix[row][col])
printed_elements.add(matrix[row][col])
col += 1
while row < num_rows and matrix[row][col - 1] not in printed_elements:
print(matrix[row][col - 1])
printed_elements.add(matrix[row][col - 1])
row += 1
while col > 0 and matrix[row - 1][col - 1] not in printed_elements:
print(matrix[row - 1][col - 1])
printed_elements.add(matrix[row - 1][col - 1])
col -= 1
while row > 0 and matrix[row - 1][col] not in printed_elements:
print(matrix[row - 1][col])
printed_elements.add(matrix[row - 1][col])
row -= 1
matrix = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20]]
print_matrix_in_spiral_order(matrix)```
Reasons you should never visit the Dark Web 🕸🙅♂
Problem Of The Day
"Break a number"
Solve the problem to win points
Given a really large number N, break it into 3 whole numbers such that they sum up to the original number and find the number of ways to do so. Since this number can be very large, return it modulo 109+7.
Example 1:
Input:
N = 2
Output:
6
Explanation:
Possible ways to break the number:
0 + 0 + 2 = 2
0 + 2 + 0 = 2
2 + 0 + 0 = 2
0 + 1 + 1 = 2
1 + 1 + 0 = 2
1 + 0 + 1 = 2
Example 2:
Input:
N = 3
Output:
10
Explanation:
Possible ways to break the number:
0+0+3 = 3
0+3+0 = 3
3+0+0 = 3
0+1+2 = 3
0+2+1 = 3
1+0+2 = 3
1+2+0 = 3
2+0+1 = 3
2+1+0 = 3
1+1+1 = 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function waysToBreakNumber() which takes an integer N and returns the possible ways to break the number in 3 parts.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Solve the problem
Problem Of The Day
"2D Hopscotch"
Solve the problem to win points
Aakriti, Avantika and Mehak are playing 2D Hopscotch. The arena is in the form of a n*m 2D matrix. But the position of the cells is slightly modified as shown below.
Mehak starts the game from tile (i,j) while Avantika and Aakriti direct her. In each turn Mehak will collect all the stones present (1 or 2) steps away from where she is standing. Avantika can direct Mehak to take 1 step and and Aakriti can direct Mehak to take 2 steps.
If the director ty is known to you as ty = 0 being Avantika and 1 being Aakriti, find the number of stones that Mehak will collect.
Example 1:
Input:
n = 3, m = 3
mat = {{5, 9, 7},
{6, 4, 5},
{8, 1, 2}}
ty = 0,
i = 1, j = 1
Output: 31
Explaination:
ty=0, so Avantika is the director.
ie- Mehak will move only one step in
any direction to collect the stones.
(0,1), (1,0), (1,2), (2,1), (2,2), (2,0)
are at a distance of 1 from (1,1).
Adding them 9+6+5+8+1+2=31.
Example 2:
Input:
n = 3, m = 3
mat = {{5, 9, 7},
{6, 4, 5},
{8, 1, 2}}
ty = 1,
i = 1, j = 1
Output: 12
Explaination:
ty=1, so Aakriti is the director.
ie- Mehak can move 2 steps.
(0,0) and (0,2) are the only tiles that
are at a distance of two from (1,1).
Adding them gives 5+7=12.
Your Task:
You do not need to read input or print anything. Your task is to complete the function hopscotch() which takes n, m, mat, ty, i and j as input parameters and returns the number of collected stones.
Expected Time Complexity: O(1)
Expected Auxiliary Space: O(1)
Solve the problem
Problem Of The Day
"Balanced string"
Solve the problem to win points
Given an integer N.Create a string using only lowercase characters from a to z that follows the given rules.
When N is even:
Use N/2 characters from the beginning of a-z and N/2 characters from the ending of a-z.
When N is greater than 26,continue repeating the instructions until length of string becomes N.
When N is odd:
Case 1: If the sum of digits of N is even, Select (N+1)/2 characters from the beginning of a-z and (N-1)/2 characters from the ending of a-z.
Case 2: If the sum of digits of N is odd, Select (N-1)/2 characters from the beginning of a-z and (N+1)/2 characters from the ending of a-z.
When N is greater than 26,continue repeating the instructions until length of string becomes N.
Example 1:
Input:
N=21
Output:
abcdefghijpqrstuvwxyz
Explanation:
Since 21 is odd and sum of digits
of 21 is also odd,we take (21-1)/2=10
characters from the beginning and
(21+1)/2=11 characters from the
end of a-z.
Example 2:
Input:
N=28
Output:
abcdefghijklmnopqrstuvwxyzaz
Explanation:
Since 28>26, we keep repeating
the process until length of string becomes
28.
Your Task:
You don't need to read input or print anything. Your task is to complete the function BalancedString() which takes the integer N as input parameter and returns the string created using given procedures.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Solve the problem
Problem Of The Day
“Split Array Largest Sum”
Solve the problem to win points
Given an array arr[] of N elements and a number K. Split the given array into K subarrays such that the maximum subarray sum achievable out of K subarrays formed is minimum possible. Find that possible subarray sum.
Example 1:
Input:
N = 4, K = 3
arr[] = {1, 2, 3, 4}
Output: 4
Explanation:
Optimal Split is {1, 2}, {3}, {4}.
Maximum sum of all subarrays is 4,
which is minimum possible for 3 splits.
Example 2:
Input:
N = 3, K = 2
A[] = {1, 1, 2}
Output:
2
Explanation:
Splitting the array as {1,1} and {2} is optimal.
This results in a maximum sum subarray of 2.
Your Task:
The task is to complete the function splitArray() which returns the maximum sum subarray after splitting the array into K subarrays such that maximum sum subarray is minimum possible.
Constraints:
1 ≤ N ≤ 105
1 ≤ K ≤ N
1 ≤ arr[i] ≤ 104
Expected Time Complexity: O(N*log(sum(arr))).
Expected Auxiliary Space: O(1).
Solve the problem
Problem Of The Day
“Articulation Point - I”
Solve the problem to win points
Given an undirected connected graph with V vertices and adjacency list adj. You are required to find all the vertices removing which (and edges through it) disconnects the graph into 2 or more components.
Note: Indexing is zero-based i.e nodes numbering from (0 to V-1). There might be loops present in the graph.
Example 1:
Input:
Output:{1,4}
Your Task:
You don't need to read or print anything. Your task is to complete the function articulationPoints() which takes V and adj as input parameters and returns a list containing all the vertices removing which turn the graph into two or more disconnected components in sorted order. If there are no such vertices then returns a list containing -1.
Expected Time Complexity: O(V + E)
Expected Auxiliary Space: O(V)
Solve the problem
Problem Of The Day
“Array Pair Sum Divisibility Problem”
Solve the problem to win points
Given an array of integers and a number k, write a function that returns true if given array can be divided into pairs such that sum of every pair is divisible by k.
Example 1 :
Input : arr = [9, 5, 7, 3], k = 6
Output: True
Explanation: {(9, 3), (5, 7)} is a
possible solution. 9 + 3 = 12 is divisible
by 6 and 7 + 5 = 12 is also divisible by 6.
Example 2:
Input : arr = [2, 4, 1, 3], k = 4
Output: False
Explanation: There is no possible solution.
Your Task:
You don't need to read or print anything. Your task is to complete the function canPair() which takes array and k as input parameter and returns true if array can be divided into pairs such that sum of every pair is divisible by k otherwise returns false.
Expected Time Complexity: O(n)
Expected Space Complexity : O(n)
Solve the problem
Dear Student,
We hope you all enjoyed today's webinar on Playto Labs - IIT National Robotics Competition 2022-23
If you have missed filling out the application form for the competition, please do it here now:
https://tinyurl.com/playto-robotics
Please note that today is the last date for the application and registration will end by midnight. Kindly forward the same link to your team members who will be participating along with you. It is mandatory for all team members to fill out the online application.
Further updates about the competition will be shared shortly after Filling out the application.
We hope to see you and your team's innovative robotics project in the National Level Competition.
All the very best!
Team Playto Labs
Problem Of The Day
“Build the smallest”
Solve the problem to win points
Given a number k and string num of digits (0 to 9) denoting a positive integer. Find a string denoting the lowest integer number possible by removing k digits from num, without changing their order.
Note: num will not contain any leading zero.
Example 1:
Input:
k = 2
num = "143729"
Output: "1329"
Explanation: 1329 is the minimum number
possible after removing '4' and '7'.
Example 2:
Input:
k = 3
num = "10056"
Output: "0"
Explanation: 0 is the minimum number
possible after removing '1' , '5' and '6'.
Your Task:
You dont need to read input or print anything. Complete the function buildLowestNumber() which accepts string num and integer k as input parameters and returns a string denoting the smallest integer possible after removing k digits from num without changing the order.
Expected Time Complexity: O(Length of num)
Expected Auxiliary Space: O(Length of num)
Solve the problem
🔸EXCEL FORMULA CHEATSHEET🔸(VERSION - 5).pdf8.87 KB
Problem Of The Day
"Black and White"
Solve the problem to win points
Given the chessboard dimensions. Find out the number of ways we can place a black and a white Knight on this chessboard such that they cannot attack each other.
Note:
The knights have to be placed on different squares. A knight can move two squares horizontally and one square vertically (L shaped), or two squares vertically and one square horizontally (L shaped). The knights attack each other if one can reach the other in one move.
Example 1:
Input:
N = 2, M = 2
Output: 12
Explanation: There are 12 ways we can place a black and a white Knight on this chessboard such that they cannot attack each other.
Example 2:
Input:
N = 2, M = 3
Output: 26
Explanation: There are 26 ways we can place a black and a white Knight on this chessboard such that they cannot attack each other.
Your Task:
Your task is to complete the function numOfWays() which takes the chessboard dimensions N and M as inputs and returns the number of ways we can place 2 Knights on this chessboard such that they cannot attack each other. Since this number can be very large, return it modulo 109+7.
Expected Time Complexity: O(N*M).
Expected Auxiliary Space: O(1).
Solve the problem
Problem Of The Day
"3 Divisors"
Solve the problem to win points
You are given a list of q queries and for every query, you are given an integer N. The task is to find how many numbers(less than or equal to N) have number of divisors exactly equal to 3.
Example 1:
Input:
q = 1
query[0] = 6
Output:
1
Explanation:
There is only one number 4 which has
exactly three divisors 1, 2 and 4 and
less than equal to 6.
Example 2:
Input:
q = 2
query[0] = 6
query[1] = 10
Output:
1
2
Explanation:
For query 1 it is covered in the
example 1.
query 2: There are two numbers 4 and 9
having exactly 3 divisors and less than
equal to 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function threeDivisors() which takes an integer q and a list of integer of size q as input parameter and returns the list containing the count of the numbers having exactly 3 divisors for each query.
Expected Time Complexity: O(q*N*log(log(N)))
Expected Auxiliary Space: O(N), where N is min(10^6,N)
Solve the problem
Problem Of The Day
"3 Divisors"
Solve the problem to win points
You are given a list of q queries and for every query, you are given an integer N. The task is to find how many numbers(less than or equal to N) have number of divisors exactly equal to 3.
Example 1:
Input:
q = 1
query[0] = 6
Output:
1
Explanation:
There is only one number 4 which has
exactly three divisors 1, 2 and 4 and
less than equal to 6.
Example 2:
Input:
q = 2
query[0] = 6
query[1] = 10
Output:
1
2
Explanation:
For query 1 it is covered in the
example 1.
query 2: There are two numbers 4 and 9
having exactly 3 divisors and less than
equal to 10.
Your Task:
You don't need to read input or print anything. Your task is to complete the function threeDivisors() which takes an integer q and a list of integer of size q as input parameter and returns the list containing the count of the numbers having exactly 3 divisors for each query.
Expected Time Complexity: O(q*N*log(log(N)))
Expected Auxiliary Space: O(N), where N is min(10^6,N)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/3-divisors3942/1
