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 أيام
أرشيف المشاركات
Problem Of The Day
"k-th smallest element in BST"
Solve the problem to win points
Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space.
Example 1:
Input:
2
/ \
1 3
K = 2
Output: 2
Explanation: 2 is the 2nd smallest element in the BST
Example 2:
Input:
2
/ \
1 3
K = 5
Output: -1
Explanation: There is no 5th smallest element in the BST as the size of BST is 3
Your Task:
You don't need to read input or print anything. Your task is to complete the function KthSmallestElement() which takes the root of the BST and integer K as inputs and return the Kth smallest element in the BST, if no such element exists return -1.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Solve the problem
Problem Of The Day
"Distance of nearest cell having 1"
Solve the problem to win points
Given a binary grid of n*m. Find the distance of the nearest 1 in the grid for each cell.
The distance is calculated as |i1 - i2| + |j1 - j2|, where i1, j1 are the row number and column number of the current cell, and i2, j2 are the row number and column number of the nearest cell having value 1.
Example 1:
Input: grid = {{0,1,1,0},{1,1,0,0},{0,0,1,1}}
Output: {{1,0,0,1},{0,0,1,1},{1,1,0,0}}
Explanation: The grid is-
0 1 1 0
1 1 0 0
0 0 1 1
0's at (0,0), (0,3), (1,2), (1,3), (2,0) and
(2,1) are at a distance of 1 from 1's at (0,1),
(0,2), (0,2), (2,3), (1,0) and (1,1)
respectively.
Example 2:
Input: grid = {{1,0,1},{1,1,0},{1,0,0}}
Output: {{0,1,0},{0,0,1},{0,1,2}}
Explanation: The grid is-
1 0 1
1 1 0
1 0 0
0's at (0,1), (1,2), (2,1) and (2,2) are at a
distance of 1, 1, 1 and 2 from 1's at (0,0),
(0,2), (2,0) and (1,1) respectively.
Yout Task:
You don't need to read or print anything, Your task is to complete the function nearest() which takes the grid as an input parameter and returns a matrix of the same dimensions where the value at index (i, j) in the resultant matrix signifies the minimum distance of 1 in the matrix from grid[i][j].
Expected Time Complexity: O(n*m)
Expected Auxiliary Space: O(n*m)
Solve the problem
Problem Of The Day
“Shortest Path by Removing K walls”
Solve the problem to win points
Given a 2-D binary matrix of size n*m, where 0 represents an empty space while 1 represents a wall you cannot walk through. You are also given an integer k.
You can walk up, down, left, or right. Given that you can remove up to k walls, return the minimum number of steps to walk from the top left corner (0, 0) to the bottom right corner (n-1, m-1).
Note: If there is no way to walk from the top left corner to the bottom right corner, return -1.
Example 1:
Input: n = 3, m = 3, k = 1
mat = {{0, 0, 0},
{0, 0, 1},
{0, 1, 0}}
Output:
4
Explanation:
We can remove any one of the walls and
reach the bottom in 4 moves.
Example 2:
Input:
n = 2, m = 2, k = 0
mat[] = {{0, 1},
{1, 0}}
Output:
-1
Explanation:
There's no way of reaching the bottom
corner without removing any walls.
Your Task:
The task is to complete the function shotestPath() which takes three integers n, m, and k and also a matrix of size n*m as input and returns the minimum number of steps to walk from the top left corner to the bottom right corner.
Constraints:
1 ≤ n,m ≤ 50
0 ≤ k ≤ n*m
Top left and bottom right corners doesn't have 1
Solve the problem
Problem Of The Day
“Alternate Vowel and Consonant String”
Solve the problem to win points
Given a string S of lowercase english characters. Rearrange characters of the given string such that the vowels and consonants occupy alternate positions and the string so formed should be lexicographically (alphabetically) smallest.
Note: Vowels are 'a', 'e', 'i', 'o' and 'u'.
Example 1:
Input:
S = "aeroplane"
Output: alanepero
Explanation: alanepero
The vowels and cosonants are arranged
alternatively with vowels shown in bold.
Also, there's no lexicographically smaller
string possible with required conditions.
Example 2:
Input:
S = "mississippi"
Output: -1
Explanation: The number of vowels is 4
whereas the number of consonants is 7.
Hence, there's no way to arrange the
vowels and consonants alternatively.
Your Task:
You don't need to read input or print anything. Your task is to complete the function rearrange() which takes the string S and its size N as inputs and returns the modified string as stated in the description. If such a modification is not possible, return the string "-1".
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(2*26).
Solve the dsa problem
Problem Of The Day
“Aggressive Cows”
Solve the problem to win points
You are given an array consisting of n integers which denote the position of a stall. You are also given an integer k which denotes the number of aggressive cows. You are given the task of assigning stalls to k cows such that the minimum distance between any two of them is the maximum possible.
The first line of input contains two space-separated integers n and k.
The second line contains n space-separated integers denoting the position of the stalls.
Example 1:
Input:
n=5
k=3
stalls = [1 2 4 8 9]
Output:
3
Explanation:
The first cow can be placed at stalls[0],
the second cow can be placed at stalls[2] and
the third cow can be placed at stalls[3].
The minimum distance between cows, in this case, is 3,
which also is the largest among all possible ways.
Example 2:
Input:
n=5
k=3
stalls = [10 1 2 7 5]
Output:
4
Explanation:
The first cow can be placed at stalls[0],
the second cow can be placed at stalls[1] and
the third cow can be placed at stalls[4].
The minimum distance between cows, in this case, is 4,
which also is the largest among all possible ways.
Your Task:
Complete the function int solve(), which takes integer n, k, and a vector stalls with n integers as input and returns the largest possible minimum distance between cows.
Expected Time Complexity: O(n*log(10^9)).
Expected Auxiliary Space: O(1).
Solve the problem
Problem Of The Day
“Check if it is possible to convert one string into another with given constraints”
Solve the problem to win points
Given two strings S and T, which contains three characters i.e 'A', 'B' and '#' only. Check whether it is possible to convert the first string into another string by performing following operations on string first.
1- A can move towards Left only
2- B can move towards Right only
3- Neither A nor B should cross each other
Note: Moving i'th character towards Left one step means swap i'th with (i-1)'th charecter [ i-1>=0 ]. Moving i'th character towards Right one step means swap i'th with (i+1)'th charecter [ i+1< string's length ].
Example 1:
Input:
S=#A#B#B#
T=A###B#B
Output:
1
Explanation:
A in S is right to the A in T
so A of S can move easily towards
the left because there is no B on
its left positions and for first B
in S is left to the B in T so B
of T can move easily towards the
right because there is no A on its
right positions and it is same for
next B so S can be easily converted
into T.
Example 2:
Input:
S=#A#B#
T=#B#A#
Output:
0
Explanation:
Here first A in S is left to the
A in T and according to the condition,
A cant move towards right,so S cant
be converted into T.
Your Task:
You don't need to read input or print anything. Your task is to complete the function isItPossible() which takes the two strings S, T and their respective lengths M and N as input parameters and returns 1 if S can be converted into T. Otherwise, it returns 0.
Expected Time Complexity: O(M+N) where M is size of string S and N is size of string T.
Expected Auxillary Space: O(1)
Solve the problem
Problem Of The Day
“Rearrange Array Alternately”
Solve the problem to win points
Given a sorted array of positive integers. Your task is to rearrange the array elements alternatively i.e first element should be max value, second should be min value, third should be second max, fourth should be second min and so on.
Note: Modify the original array itself. Do it without using any extra space. You do not have to return anything.
Example 1:
Input:
n = 6
arr[] = {1,2,3,4,5,6}
Output: 6 1 5 2 4 3
Explanation: Max element = 6, min = 1,
second max = 5, second min = 2, and
so on... Modified array is : 6 1 5 2 4 3.
Example 2:
Input:
n = 11
arr[]={10,20,30,40,50,60,70,80,90,100,110}
Output:110 10 100 20 90 30 80 40 70 50 60
Explanation: Max element = 110, min = 10,
second max = 100, second min = 20, and
so on... Modified array is :
110 10 100 20 90 30 80 40 70 50 60.
Your Task:
The task is to complete the function rearrange() which rearranges elements as explained above. Printing of the modified array will be handled by driver code.
Expected Time Complexity: O(N).
Expected Auxiliary Space: O(1).
Solve the problem
Repost from Python full stack development
💥 Hello Guys Good News For You all💥
Welcome to the DATA STRUCTURES section!❤️🔥
Problem Of The Day
“Maximum Sub Array”
Solve the problem to win points
Find out the maximum sub-array of non negative numbers from an array.
The sub-array should be contiguous i.e., a sub-array created by choosing the second and fourth element and skipping the third element is invalid.
Maximum sub-array is defined in terms of the sum of the elements in the sub-array. Sub-array A is greater than sub-array B if sum(A) > sum(B).
Example:
a = [1, 2, 5, -7, 2, 3]
The two sub-arrays are [1, 2, 5] [2, 3].
The answer is [1, 2, 5] as its sum is larger than [2, 3]
NOTE: If there is a tie, then compare with segment's length and return segment which has maximum length.
If there is still a tie, then return the segment with minimum starting index.
If no such subarray is present return "-1"
Example 1:
Input:
n = 3
a[] = {1, 2, 3}
Output: 1 2 3
Explanation: In the given array every
element is non-negative.
Example 2:
Input:
n = 2
a[] = {-1, 2}
Output: 2
Explanation: The only subarray [2] is
the answer.
Your Task:
Complete the function findSubarray() which takes the array a and the size of the array, n, as input parameters and returns an array representing the answer. If there is no subarray return an array of length 1 containing -1 only. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Solve the problem
Introduction to Algorithms, 4th Edition.pdf22.22 MB
