Must Do DSA Topics Day 61
Binary Search Tree
Introduction to Binary Search Tree β Data Structure and Algorithm Tutorials : Binary Search Tree is a node-based binary tree data structure which has the following properties:
-The left subtree of a node contains only nodes with keys lesser than the nodeβs key.
-The right subtree of a node contains only nodes with keys greater than the nodeβs key.
Learn more : https://bit.ly/3Bw91JW
Applications of BST : Binary Search Tree, is a node-based binary tree data structure which has the following properties:
-The left subtree of a node contains only nodes with keys lesser than the nodeβs key.
-The right subtree of a node contains only nodes with keys greater than the nodeβs key.
Learn more : https://bit.ly/3BWMfLx
Application, Advantages and Disadvantages of Binary Search Tree : Searching in BST involves the comparison of the key values.
Learn more : https://bit.ly/3hvrPlv
Binary Search Tree
Introduction to Binary Search Tree β Data Structure and Algorithm Tutorials : Binary Search Tree is a node-based binary tree data structure which has the following properties:
-The left subtree of a node contains only nodes with keys lesser than the nodeβs key.
-The right subtree of a node contains only nodes with keys greater than the nodeβs key.
Learn more : https://bit.ly/3Bw91JW
Applications of BST : Binary Search Tree, is a node-based binary tree data structure which has the following properties:
-The left subtree of a node contains only nodes with keys lesser than the nodeβs key.
-The right subtree of a node contains only nodes with keys greater than the nodeβs key.
Learn more : https://bit.ly/3BWMfLx
Application, Advantages and Disadvantages of Binary Search Tree : Searching in BST involves the comparison of the key values.
Learn more : https://bit.ly/3hvrPlv
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/balanced-string1627/1
"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 and submit your answer here : https://practice.geeksforgeeks.org/problems/balanced-string1627/1
practice.geeksforgeeks.org
Balanced string | Practice | GeeksforGeeks
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
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
π2
Must Do DSA Topics Day 62
Binary Search Tree
Insertion in Binary Search Tree : A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node.
Learn more : https://bit.ly/3BC13yK
Deletion in Binary Search Tree : We have discussed BST search and insert operations. In this post, the delete operation is discussed. When we delete a node, three possibilities arise.
Learn more : https://bit.ly/3BCdRFA
Convert a normal BST to Balanced BST : Given a BST (Binary Search Tree) that may be unbalanced, convert it into a balanced BST that has minimum possible height.
Learn more : https://bit.ly/3G41C7n
Binary Search Tree
Insertion in Binary Search Tree : A new key is always inserted at the leaf by maintaining the property of the binary search tree. We start searching for a key from the root until we hit a leaf node.
Learn more : https://bit.ly/3BC13yK
Deletion in Binary Search Tree : We have discussed BST search and insert operations. In this post, the delete operation is discussed. When we delete a node, three possibilities arise.
Learn more : https://bit.ly/3BCdRFA
Convert a normal BST to Balanced BST : Given a BST (Binary Search Tree) that may be unbalanced, convert it into a balanced BST that has minimum possible height.
Learn more : https://bit.ly/3G41C7n
π3
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/hopscotch4857/1
Powered by Hirist.com
"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 and submit your answer here : https://practice.geeksforgeeks.org/problems/hopscotch4857/1
Powered by Hirist.com
practice.geeksforgeeks.org
2D Hopscotch | Practice | GeeksforGeeks
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 h
Mehak starts the game from tile (i,j) while Avantika and Aakriti direct h
π1
Must Do DSA Topics Day 63
Binary Search Tree
Iterative searching in Binary Search Tree : Given a binary search tree and a key. Check the given key exists in BST or not without recursion.
Learn more : https://bit.ly/3uWDoVY
A program to check if a binary tree is BST or not: The idea is to for each node, check if max value in left subtree is smaller than the node and min value in right subtree greater than the node.
Learn more : https://bit.ly/3uVFhCr
Binary Tree to Binary Search Tree Conversion : Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of Binary Tree.
Learn more : https://bit.ly/3BI7eBA
Binary Search Tree
Iterative searching in Binary Search Tree : Given a binary search tree and a key. Check the given key exists in BST or not without recursion.
Learn more : https://bit.ly/3uWDoVY
A program to check if a binary tree is BST or not: The idea is to for each node, check if max value in left subtree is smaller than the node and min value in right subtree greater than the node.
Learn more : https://bit.ly/3uVFhCr
Binary Tree to Binary Search Tree Conversion : Given a Binary Tree, convert it to a Binary Search Tree. The conversion must be done in such a way that keeps the original structure of Binary Tree.
Learn more : https://bit.ly/3BI7eBA
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/break-a-number5913/1
Powered by hirist.com
"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 and submit your answer here : https://practice.geeksforgeeks.org/problems/break-a-number5913/1
Powered by hirist.com
www.geeksforgeeks.org
Break a number | Practice | GeeksforGeeks
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
Example 1:
Input:
N
π1
Must Do DSA Topics Day 64
Binary Search Tree
Sorted Linked List to Balanced BST : Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Learn more : https://bit.ly/3PxEssQ
Transform a BST to greater sum tree : Given a BST, transform it into a greater sum tree where each node contains sum of all nodes greater than that node.
Learn more : https://bit.ly/3C5QhRR
Construct all possible BSTs for keys 1 to N : In this article, first count of possible BST (Binary Search Trees)s is discussed, then the construction of all possible BSTs.
Learn more : https://bit.ly/3hFtO6O
Get access to all the topics of Binary Search Tree : https://www.geeksforgeeks.org/binary-search-tree-data-structure/?ref=gcse
Binary Search Tree
Sorted Linked List to Balanced BST : Given a Singly Linked List which has data members sorted in ascending order. Construct a Balanced Binary Search Tree which has same data members as the given Linked List.
Learn more : https://bit.ly/3PxEssQ
Transform a BST to greater sum tree : Given a BST, transform it into a greater sum tree where each node contains sum of all nodes greater than that node.
Learn more : https://bit.ly/3C5QhRR
Construct all possible BSTs for keys 1 to N : In this article, first count of possible BST (Binary Search Trees)s is discussed, then the construction of all possible BSTs.
Learn more : https://bit.ly/3hFtO6O
Get access to all the topics of Binary Search Tree : https://www.geeksforgeeks.org/binary-search-tree-data-structure/?ref=gcse
Job Alert π
Twerlo is hiring for Fullstack (Angular). Eligible candidates can apply. Also, share with your friends who might be looking for the opportunity.
Location - Banglore
Last date to apply - Dec 21, 2022
Get more details about the job here : https://practice.geeksforgeeks.org/jobs/Ttwerlo-aangularfullstack
Twerlo is hiring for Fullstack (Angular). Eligible candidates can apply. Also, share with your friends who might be looking for the opportunity.
Location - Banglore
Last date to apply - Dec 21, 2022
Get more details about the job here : https://practice.geeksforgeeks.org/jobs/Ttwerlo-aangularfullstack
Problem Of The Day
"A Game of LCM"
Solve the problem to win points
Given an integer N. Find maximum LCM (Least Common Multiple) that can be obtained from four numbers less than or equal to N.
Note: Duplicate numbers can be used.
Example 1:
Input:
N = 4
Output: 12
Explanation:
The four numbers can be [4,4,3,2] or
[4,4,4,3], etc. It can be shown that 12 is
the maximum LCM of four numbers that can
be obtained from numbers less than or equal
to 4.
Example 2:
Input:
N = 5
Output: 60
Explanation:
The four numbers can be [5,5,4,3] or
[5,4,3,2], etc. 60 is the maximum that can
be obtained.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxGcd() which takes N as the input parameter and returns the maximum LCM that can be obtained from four numbers less than or equal to N.
Expected Time Complexity: O( Log2(max(N)) )
Expected Auxillary Space: O(1)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/a-game-of-lcm2531/1
Hey Geeks! You can also share the approach you used to solve the problem in the comments.π
Powered by Hirist.com
"A Game of LCM"
Solve the problem to win points
Given an integer N. Find maximum LCM (Least Common Multiple) that can be obtained from four numbers less than or equal to N.
Note: Duplicate numbers can be used.
Example 1:
Input:
N = 4
Output: 12
Explanation:
The four numbers can be [4,4,3,2] or
[4,4,4,3], etc. It can be shown that 12 is
the maximum LCM of four numbers that can
be obtained from numbers less than or equal
to 4.
Example 2:
Input:
N = 5
Output: 60
Explanation:
The four numbers can be [5,5,4,3] or
[5,4,3,2], etc. 60 is the maximum that can
be obtained.
Your Task:
You don't need to read input or print anything. Your task is to complete the function maxGcd() which takes N as the input parameter and returns the maximum LCM that can be obtained from four numbers less than or equal to N.
Expected Time Complexity: O( Log2(max(N)) )
Expected Auxillary Space: O(1)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/a-game-of-lcm2531/1
Hey Geeks! You can also share the approach you used to solve the problem in the comments.π
Powered by Hirist.com
practice.geeksforgeeks.org
A Game of LCM | Practice | GeeksforGeeks
Given an integer N. Find maximum LCM (Least Common Multiple) that can be obtained from four numbers less than or equal to N.
Note: Duplicate numbers can be used.
Example 1:
Input:
N = 4
Output: 12
Explanation:
The four num
Note: Duplicate numbers can be used.
Example 1:
Input:
N = 4
Output: 12
Explanation:
The four num
Must Do DSA Topics Day 65
Heap
Binary Heap : A Binary Heap is a Complete Binary Tree. A binary heap is typically represented as an array.
Learn more : https://bit.ly/3BKlHwD
Time Complexity of building a heap : Consider the following algorithm for building a Heap of an input array A.
Learn more : https://bit.ly/3jd2yx9
Applications of Heap Data Structure : Heap Data Structure is generally taught with Heapsort. Heapsort algorithm has limited uses because Quicksort is better in practice.
Learn more : https://bit.ly/3G3qWKI
Heap
Binary Heap : A Binary Heap is a Complete Binary Tree. A binary heap is typically represented as an array.
Learn more : https://bit.ly/3BKlHwD
Time Complexity of building a heap : Consider the following algorithm for building a Heap of an input array A.
Learn more : https://bit.ly/3jd2yx9
Applications of Heap Data Structure : Heap Data Structure is generally taught with Heapsort. Heapsort algorithm has limited uses because Quicksort is better in practice.
Learn more : https://bit.ly/3G3qWKI
π1
Job Alert π
Hubilo is hiring for Backend.
Candidates should have 6-8 years of experience. Do apply and share with your friends who might be interested in the position.
Get more details of the job: https://practice.geeksforgeeks.org/jobs/-TalentXO-Backend
Hubilo is hiring for Backend.
Candidates should have 6-8 years of experience. Do apply and share with your friends who might be interested in the position.
Get more details of the job: https://practice.geeksforgeeks.org/jobs/-TalentXO-Backend
Problem Of The Day
"Alex Travelling"
Solve the problem to win points
Alex is very fond of traveling. There are n cities, labeled from 1 to n. You are also given flights, a list of travel flights as directed weighted edges flights[i] = (ui,vi,wi) where ui is the source node, vi is the target node, and wi is the price it takes for a person to travel from source to target.
Currently, Alex is in k'th city and wants to visit one city of his choice. Return the minimum money Alex should have so that he can visit any city of his choice from k'th city. If there is a city that has no path from k'th city, which means Alex can't visit that city, return -1.
Alex always takes the optimal path. He can any city via another city by taking multiple flights.
Example 1:
Input:
n: 4
k: 2
flights size: 3
flights: [[2,1,1],[2,3,1],[3,4,1]]
Output:
2
Explanation:
to visit 1 from 2 takes cost 1
to visit 2 from 2 takes cost 0
to visit 3 from 2 takes cost 1
to visit 4 from 2 takes cost 2,
2->3->4
So if Alex wants to visit city 4
from 2, he needs 2 units of money
Example 2:
Input:
n: 4
k: 3
flights size: 3
flights: [[2,1,1],[2,3,1],[3,4,1]]
Output: -1
Explanation:
There is no direct or indirect path
to visit city 2 and 1 from city 3
Your Task:
You don't need to print or input anything. Complete the function minimumCost() which takes a flights array, an integer n and an integer k as the input parameters and returns an integer, denoting the minimum money Alex should have so that he can visit any city of his choice from k'th city.
Expected Time Complexity: O((V+E) log V), here V is number of cities and E is number of flights.
Expected Auxiliary Space: O(V+E), here V is number of cities and E is number of flights.
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/alex-travelling/1
Powered by hirist.com
"Alex Travelling"
Solve the problem to win points
Alex is very fond of traveling. There are n cities, labeled from 1 to n. You are also given flights, a list of travel flights as directed weighted edges flights[i] = (ui,vi,wi) where ui is the source node, vi is the target node, and wi is the price it takes for a person to travel from source to target.
Currently, Alex is in k'th city and wants to visit one city of his choice. Return the minimum money Alex should have so that he can visit any city of his choice from k'th city. If there is a city that has no path from k'th city, which means Alex can't visit that city, return -1.
Alex always takes the optimal path. He can any city via another city by taking multiple flights.
Example 1:
Input:
n: 4
k: 2
flights size: 3
flights: [[2,1,1],[2,3,1],[3,4,1]]
Output:
2
Explanation:
to visit 1 from 2 takes cost 1
to visit 2 from 2 takes cost 0
to visit 3 from 2 takes cost 1
to visit 4 from 2 takes cost 2,
2->3->4
So if Alex wants to visit city 4
from 2, he needs 2 units of money
Example 2:
Input:
n: 4
k: 3
flights size: 3
flights: [[2,1,1],[2,3,1],[3,4,1]]
Output: -1
Explanation:
There is no direct or indirect path
to visit city 2 and 1 from city 3
Your Task:
You don't need to print or input anything. Complete the function minimumCost() which takes a flights array, an integer n and an integer k as the input parameters and returns an integer, denoting the minimum money Alex should have so that he can visit any city of his choice from k'th city.
Expected Time Complexity: O((V+E) log V), here V is number of cities and E is number of flights.
Expected Auxiliary Space: O(V+E), here V is number of cities and E is number of flights.
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/alex-travelling/1
Powered by hirist.com
practice.geeksforgeeks.org
Alex Travelling | Practice | GeeksforGeeks
Alex is very fond of traveling. There are n cities, labeled from 1 to n. You are also given flights, a list of travel flights as directed weighted edges flights[i] = (ui,vi,wi) where ui is the source node, vi is the tar
Must Do DSA Topics Day 66
Heap
Binomial Heap : The main application of Binary Heap is as implement a priority queue. Binomial Heap is an extension of Binary Heap that provides faster union or merge operation with other operations provided by Binary Heap.
Learn more : https://bit.ly/3BKyWxB
Fibonacci Heap : Heaps are mainly used for implementing priority queue. We have discussed the below heaps in previous posts.
Learn more : https://bit.ly/3Wa2YTA
Leftist Heap : K-ary heap when used in the implementation of priority queue allows faster decrease key operation as compared to binary heap ( O(log2n)) for binary heap vs O(logkn) for K-ary heap).
Learn more : https://bit.ly/3HMClzQ
Heap
Binomial Heap : The main application of Binary Heap is as implement a priority queue. Binomial Heap is an extension of Binary Heap that provides faster union or merge operation with other operations provided by Binary Heap.
Learn more : https://bit.ly/3BKyWxB
Fibonacci Heap : Heaps are mainly used for implementing priority queue. We have discussed the below heaps in previous posts.
Learn more : https://bit.ly/3Wa2YTA
Leftist Heap : K-ary heap when used in the implementation of priority queue allows faster decrease key operation as compared to binary heap ( O(log2n)) for binary heap vs O(logkn) for K-ary heap).
Learn more : https://bit.ly/3HMClzQ
Problem Of The Day
"A difference of values and indexes"
Solve the problem to win points
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/a-difference-of-values-and-indexes0302/1
Powered by hirist.com
"A difference of values and indexes"
Solve the problem to win points
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr[ ] = {1, 3, -1}
Output: 5
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 3 - (-1) | + | 1 - 2 | = 5
Example 2:
Input :
n = 4
arr[ ] = {5, 9, 2, 6}
Output: 8
Explanation:
Maximum difference comes from indexes
1, 2 i.e | 9 - 2 | + | 1 - 2 | = 8
Your Task:
This is a function problem. The input is already taken care of by the driver code. You only need to complete the function maxDistance() that takes an array (arr), sizeOfArray (n), and return the maximum difference as given in the question. The driver code takes care of the printing.
Expected Time Complexity: O(n).
Expected Auxiliary Space: O(1).
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/a-difference-of-values-and-indexes0302/1
Powered by hirist.com
practice.geeksforgeeks.org
A difference of values and indexes | Practice | GeeksforGeeks
Given an unsorted array arr[ ] of size n, you need to find the maximum difference of absolute values of elements and indexes, i.e., for i <= j, calculate maximum of | arr[ i ] - arr[ j ] | + | i - j |.
Example 1:
Input :
n = 3
arr
Example 1:
Input :
n = 3
arr
π3
Must Do DSA Topics Day 67
Heap
Tournament Tree (Winner Tree) and Binary Heap : Given a team of N players. How many minimum games are required to find the second-best player? We can use adversary arguments based on the tournament tree (Binary Heap).
Learn more : https://bit.ly/3HPWJ30
Check if a given Binary Tree is Heap : Given a binary tree, check if it has heap property or not, Binary tree needs to fulfill the following two conditions for being a heap β It should be a complete tree (i.e. all levels except the last should be full).
Every nodeβs value should be greater than or equal to its child node (considering max-heap).
Learn more : https://bit.ly/3WdkB4H
How to check if a given array represents a Binary Heap? : A Simple Solution is to first check root if itβs greater than all of its descendants. Then check for children of the root. Time complexity of this solution is O(n2)
Learn more : https://bit.ly/3FzaOPu
Heap
Tournament Tree (Winner Tree) and Binary Heap : Given a team of N players. How many minimum games are required to find the second-best player? We can use adversary arguments based on the tournament tree (Binary Heap).
Learn more : https://bit.ly/3HPWJ30
Check if a given Binary Tree is Heap : Given a binary tree, check if it has heap property or not, Binary tree needs to fulfill the following two conditions for being a heap β It should be a complete tree (i.e. all levels except the last should be full).
Every nodeβs value should be greater than or equal to its child node (considering max-heap).
Learn more : https://bit.ly/3WdkB4H
How to check if a given array represents a Binary Heap? : A Simple Solution is to first check root if itβs greater than all of its descendants. Then check for children of the root. Time complexity of this solution is O(n2)
Learn more : https://bit.ly/3FzaOPu
π1
Problem Of The Day
"Absolute List Sorting"
Solve the problem to win points
Given a linked list of N nodes, sorted in ascending order based on the absolute values of its data,i.e. negative values are considered as positive ones. Sort the linked list in ascending order according to the actual values, and consider negative numbers as negative and positive numbers as positive.
Example 1:
Input:
List: 1, -2, -3, 4, -5
Output:
List: -5, -3, -2, 1, 4
Explanation:
Actual sorted order of {1, -2, -3, 4, -5}
is {-5, -3, -2, 1, 4}
Example 2:
Input:
List: 5, -10
Output:
List: -10, 5
Explanation:
Actual sorted order of {5, -10}
is {5, 10}
Your Task:
You don't need to read or print anyhting. Your Task is to comple the function sortList() which takes the head of the Linked List as input parameter and sort the list in ascending order and return the head pointer of the sorted list.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/absolute-list-sorting/1
Powered by hirist.com
"Absolute List Sorting"
Solve the problem to win points
Given a linked list of N nodes, sorted in ascending order based on the absolute values of its data,i.e. negative values are considered as positive ones. Sort the linked list in ascending order according to the actual values, and consider negative numbers as negative and positive numbers as positive.
Example 1:
Input:
List: 1, -2, -3, 4, -5
Output:
List: -5, -3, -2, 1, 4
Explanation:
Actual sorted order of {1, -2, -3, 4, -5}
is {-5, -3, -2, 1, 4}
Example 2:
Input:
List: 5, -10
Output:
List: -10, 5
Explanation:
Actual sorted order of {5, -10}
is {5, 10}
Your Task:
You don't need to read or print anyhting. Your Task is to comple the function sortList() which takes the head of the Linked List as input parameter and sort the list in ascending order and return the head pointer of the sorted list.
Expected Time Complexity: O(N)
Expected Space Complexity: O(1)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/absolute-list-sorting/1
Powered by hirist.com
practice.geeksforgeeks.org
Absolute List Sorting | Practice | GeeksforGeeks
Given a linked list of N nodes, sorted in ascending order based on the absolute values of its data,i.e. negative values are considered as positive ones. Sort the linked list in ascending order according to the actual values, and consider negativ
π3
Must Do DSA Topics Day 68
Heap
Merge Sort Tree for Range Order Statistics : The key idea is to build a Segment Tree with a vector at every node and the vector contains all the elements of the sub-range in a sorted order.
Learn more : https://bit.ly/3VgGxuH
Sort numbers stored on different machines : Given N machines. Each machine contains some numbers in sorted form. But the amount of numbers, each machine has is not fixed. Output the numbers from all the machine in sorted non-decreasing form.
Learn more : https://bit.ly/3VdLswu
Smallest Derangement of Sequence : A derangement of S is as any permutation of S such that no two elements in S and its permutation occur at the same position.
Learn more : https://bit.ly/3FEWEwe
Heap
Merge Sort Tree for Range Order Statistics : The key idea is to build a Segment Tree with a vector at every node and the vector contains all the elements of the sub-range in a sorted order.
Learn more : https://bit.ly/3VgGxuH
Sort numbers stored on different machines : Given N machines. Each machine contains some numbers in sorted form. But the amount of numbers, each machine has is not fixed. Output the numbers from all the machine in sorted non-decreasing form.
Learn more : https://bit.ly/3VdLswu
Smallest Derangement of Sequence : A derangement of S is as any permutation of S such that no two elements in S and its permutation occur at the same position.
Learn more : https://bit.ly/3FEWEwe
Problem Of The Day
"Zero Sum Subarrays"
Solve the problem to win points
You are given an array arr[] of size n. Find the total count of sub-arrays having their sum equal to 0.
Example 1:
Input:
n = 6
arr[] = {0,0,5,5,0,0}
Output: 6
Explanation: The 6 subarrays are
[0], [0], [0], [0], [0,0], and [0,0].
Example 2:
Input:
n = 10
arr[] = {6,-1,-3,4,-2,2,4,6,-12,-7}
Output: 4
Explanation: The 4 subarrays are [-1 -3 4]
[-2 2], [2 4 6 -12], and [-1 -3 4 -2 2]
Your Task:
You don't need to read input or print anything. Complete the function findSubarray() that takes the array arr and its size n as input parameters and returns the total number of sub-arrays with 0 sum.
Expected Time Complexity: O(n*log(n))
Expected Auxilliary Space: O(n)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/zero-sum-subarrays1825/1
Powered by hirist.com
"Zero Sum Subarrays"
Solve the problem to win points
You are given an array arr[] of size n. Find the total count of sub-arrays having their sum equal to 0.
Example 1:
Input:
n = 6
arr[] = {0,0,5,5,0,0}
Output: 6
Explanation: The 6 subarrays are
[0], [0], [0], [0], [0,0], and [0,0].
Example 2:
Input:
n = 10
arr[] = {6,-1,-3,4,-2,2,4,6,-12,-7}
Output: 4
Explanation: The 4 subarrays are [-1 -3 4]
[-2 2], [2 4 6 -12], and [-1 -3 4 -2 2]
Your Task:
You don't need to read input or print anything. Complete the function findSubarray() that takes the array arr and its size n as input parameters and returns the total number of sub-arrays with 0 sum.
Expected Time Complexity: O(n*log(n))
Expected Auxilliary Space: O(n)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/zero-sum-subarrays1825/1
Powered by hirist.com
practice.geeksforgeeks.org
Zero Sum Subarrays | Practice | GeeksforGeeks
You are given an array arr[] of size n. Find the total count of sub-arrays having their sum equal to 0.
Example 1:
Input:
n = 6
arr[] = {0,0,5,5,0,0}
Output: 6
Explanation: The 6 subarrays are
[0], [0], [0], [0], [0,0], and [0,0]
Example 1:
Input:
n = 6
arr[] = {0,0,5,5,0,0}
Output: 6
Explanation: The 6 subarrays are
[0], [0], [0], [0], [0,0], and [0,0]
π1
Must Do DSA Topics Day 69
Heap
Maximum distinct elements after removing k elements: Given an array arr[] containing n elements. The problem is to find the maximum number of distinct elements (non-repeating) after removing k elements from the array.
Learn more : https://bit.ly/3GaXd29
Maximum difference between two subsets of m elements : Given an array of n integers and a number m, find the maximum possible difference between two sets of m elements chosen from given array.
Learn more : https://bit.ly/3FNPSEt
Height of a complete binary tree (or Heap) with N nodes : Consider a Binary Heap of size N. We need to find the height of it.
Learn more : https://bit.ly/3hE9lQ9
Heap
Maximum distinct elements after removing k elements: Given an array arr[] containing n elements. The problem is to find the maximum number of distinct elements (non-repeating) after removing k elements from the array.
Learn more : https://bit.ly/3GaXd29
Maximum difference between two subsets of m elements : Given an array of n integers and a number m, find the maximum possible difference between two sets of m elements chosen from given array.
Learn more : https://bit.ly/3FNPSEt
Height of a complete binary tree (or Heap) with N nodes : Consider a Binary Heap of size N. We need to find the height of it.
Learn more : https://bit.ly/3hE9lQ9