Must Do DSA Topics Day 49
Deque
Implementation of Deque using circular array : Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and delete at both ends.
Learn more: http://bit.ly/3Y0Lbzp
Deque interface in Java : Deque interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports the addition or removal of elements from either end of the data structure.
Learn more : http://bit.ly/3Fu9yOE
Deque in Python : Deque (Doubly Ended Queue) in Python is implemented using the module “collections“. Deque is preferred over a list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to a list that provides O(n) time complexity.
Learn more : http://bit.ly/3iJZDLS
Deque
Implementation of Deque using circular array : Deque or Double Ended Queue is a generalized version of the Queue data structure that allows insert and delete at both ends.
Learn more: http://bit.ly/3Y0Lbzp
Deque interface in Java : Deque interface present in java.util package is a subtype of the queue interface. The Deque is related to the double-ended queue that supports the addition or removal of elements from either end of the data structure.
Learn more : http://bit.ly/3Fu9yOE
Deque in Python : Deque (Doubly Ended Queue) in Python is implemented using the module “collections“. Deque is preferred over a list in the cases where we need quicker append and pop operations from both the ends of the container, as deque provides an O(1) time complexity for append and pop operations as compared to a list that provides O(n) time complexity.
Learn more : http://bit.ly/3iJZDLS
Must Do DSA Topics Day 50
Deque
Level order traversal in spiral form | Using Deque : Given a Binary Tree, the task is to print spiral order traversal of the given tree. For below tree, the function should print 1, 2, 3, 4, 5, 6, 7.
Learn more : http://bit.ly/3HfbiN9
Deque descending Iterator() method in Java : The descendingIterator(E e) method of Deque Interface returns an iterator over the elements in this deque in a reverse sequential order. The elements will be returned in order from last(tail) to first(head).
Learn more : http://bit.ly/3Hfbg7Z
Deque iterator() method in Java : The iterator() method of Deque Interface returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a “weakly consistent” iterator.
Learn more : http://bit.ly/3FdFjui
Get access to more topics of deque : https://www.geeksforgeeks.org/tag/deque/
Deque
Level order traversal in spiral form | Using Deque : Given a Binary Tree, the task is to print spiral order traversal of the given tree. For below tree, the function should print 1, 2, 3, 4, 5, 6, 7.
Learn more : http://bit.ly/3HfbiN9
Deque descending Iterator() method in Java : The descendingIterator(E e) method of Deque Interface returns an iterator over the elements in this deque in a reverse sequential order. The elements will be returned in order from last(tail) to first(head).
Learn more : http://bit.ly/3Hfbg7Z
Deque iterator() method in Java : The iterator() method of Deque Interface returns an iterator over the elements in this deque in a proper sequence. The elements will be returned in order from first (head) to last (tail). The returned iterator is a “weakly consistent” iterator.
Learn more : http://bit.ly/3FdFjui
Get access to more topics of deque : https://www.geeksforgeeks.org/tag/deque/
👍1
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/find-k-th-smallest-element-in-bst/1
"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 and submit your answer here : https://practice.geeksforgeeks.org/problems/find-k-th-smallest-element-in-bst/1
www.geeksforgeeks.org
k-th Smallest in BST | Practice | GeeksforGeeks
Given the root of a BST and an integer k, find the kth smallest element in the BST. If there is no kth smallest element present then return -1.
Examples:
Input: root = [20, 8, 22, 4, 12, N, N, N, N, 10, 14], k = 3 Output: 10
Explanation: 10 is the
Examples:
Input: root = [20, 8, 22, 4, 12, N, N, N, N, 10, 14], k = 3 Output: 10
Explanation: 10 is the
👍1
Must Do DSA Topics Day 51
Tree
Handshaking Lemma and Interesting Tree Properties : Handshaking lemma is about an undirected graph. In every finite undirected graph, an even number of vertices will always have an odd degree.
Learn more : http://bit.ly/3VYgCsl
Enumeration of Binary Trees : A Binary Tree is labeled if every node is assigned a label and a Binary Tree is unlabelled if nodes are not assigned any label.
Learn more : http://bit.ly/3BbDQmH
Insertion in a Binary Tree : Given a binary tree and a key, insert the key into the binary tree at the first position available in level order.
Learn more : http://bit.ly/3F65cfn
Tree
Handshaking Lemma and Interesting Tree Properties : Handshaking lemma is about an undirected graph. In every finite undirected graph, an even number of vertices will always have an odd degree.
Learn more : http://bit.ly/3VYgCsl
Enumeration of Binary Trees : A Binary Tree is labeled if every node is assigned a label and a Binary Tree is unlabelled if nodes are not assigned any label.
Learn more : http://bit.ly/3BbDQmH
Insertion in a Binary Tree : Given a binary tree and a key, insert the key into the binary tree at the first position available in level order.
Learn more : http://bit.ly/3F65cfn
❤1👍1
Must Do DSA Topics Day 52
Tree
AVL with duplicate keys : This is to augment AVL tree node to store count together with regular fields like key, left and right pointers.
Learn more : http://bit.ly/3h49s7d
Applications of tree data structure : Unlike Array and Linked List, which are linear data structures, tree is hierarchical (or non-linear) data structure.
Learn more : http://bit.ly/3iJPv64
Applications of Minimum Spanning Tree Problem : Minimum Spanning Tree (MST) problem: Given connected graph G with positive edge weights, find a min weight set of edges that connects all of the vertices.
Learn more : http://bit.ly/3hdMzhs
Tree
AVL with duplicate keys : This is to augment AVL tree node to store count together with regular fields like key, left and right pointers.
Learn more : http://bit.ly/3h49s7d
Applications of tree data structure : Unlike Array and Linked List, which are linear data structures, tree is hierarchical (or non-linear) data structure.
Learn more : http://bit.ly/3iJPv64
Applications of Minimum Spanning Tree Problem : Minimum Spanning Tree (MST) problem: Given connected graph G with positive edge weights, find a min weight set of edges that connects all of the vertices.
Learn more : http://bit.ly/3hdMzhs
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
"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
practice.geeksforgeeks.org
3 Divisors | Practice | GeeksforGeeks
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[
Example 1:
Input:
q = 1
query[
Must Do DSA Topics Day 53
Tree
Tree Traversals : Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways.
Learn more : http://bit.ly/3P6FtYC
Inorder Tree Traversal without Recursion : Using Stack is the obvious way to traverse tree without recursion. Below is an algorithm for traversing binary tree using stack. See this for step wise step execution of the algorithm.
Learn more : http://bit.ly/3VXzKXv
Inorder Tree Traversal without recursion and without stack! : Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree.
Learn more : http://bit.ly/3VSpi3o
Tree
Tree Traversals : Unlike linear data structures (Array, Linked List, Queues, Stacks, etc) which have only one logical way to traverse them, trees can be traversed in different ways.
Learn more : http://bit.ly/3P6FtYC
Inorder Tree Traversal without Recursion : Using Stack is the obvious way to traverse tree without recursion. Below is an algorithm for traversing binary tree using stack. See this for step wise step execution of the algorithm.
Learn more : http://bit.ly/3VXzKXv
Inorder Tree Traversal without recursion and without stack! : Using Morris Traversal, we can traverse the tree without using stack and recursion. The idea of Morris Traversal is based on Threaded Binary Tree.
Learn more : http://bit.ly/3VSpi3o
❤1
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/black-and-white-1587115620/1
"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 and submit your answer here : https://practice.geeksforgeeks.org/problems/black-and-white-1587115620/1
www.geeksforgeeks.org
Black and White | Practice | GeeksforGeeks
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
Note:
The knights have to be placed on different squares. A knight can move two
👍3
Must Do DSA Topics Day 54
Tree
Print Postorder traversal from given Inorder and Preorder traversals : We can print postorder traversal without constructing the tree. The idea is, root is always the first item in preorder traversal and it must be the last item in postorder traversal.
Learn more : http://bit.ly/3iEid8g
Find postorder traversal of BST from preorder traversal : An efficient approach is to find postorder traversal without constructing the tree. The idea is to traverse the given preorder array and maintain a range in which current element should lie.
Learn more : http://bit.ly/3VXXSZY
Find all possible binary trees with given Inorder Traversal : Given an array that represents Inorder Traversal, find all possible Binary Trees with the given Inorder traversal and print their preorder traversals.
Learn more : http://bit.ly/3iMlPVS
Tree
Print Postorder traversal from given Inorder and Preorder traversals : We can print postorder traversal without constructing the tree. The idea is, root is always the first item in preorder traversal and it must be the last item in postorder traversal.
Learn more : http://bit.ly/3iEid8g
Find postorder traversal of BST from preorder traversal : An efficient approach is to find postorder traversal without constructing the tree. The idea is to traverse the given preorder array and maintain a range in which current element should lie.
Learn more : http://bit.ly/3VXXSZY
Find all possible binary trees with given Inorder Traversal : Given an array that represents Inorder Traversal, find all possible Binary Trees with the given Inorder traversal and print their preorder traversals.
Learn more : http://bit.ly/3iMlPVS
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/build-the-smallest2841/1
“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 and submit your answer here : https://practice.geeksforgeeks.org/problems/build-the-smallest2841/1
practice.geeksforgeeks.org
Build the smallest | Practice | GeeksforGeeks
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 c
Note: num will not c
👍2
Must Do DSA Topics Day 55
Tree
Find n-th node of inorder traversal : We do simple Inorder Traversal. While doing the traversal, we keep track of count of nodes visited so far. When count becomes n, we print the node.
Learn more : http://bit.ly/3YdwUQ8
Find n-th node in Postorder traversal of a Binary Tree : Given a Binary tree and a number N, write a program to find the N-th node in the Postorder traversal of the given Binary tree.
Learn more : http://bit.ly/3FgMYb5
Level order traversal line by line : Given the root of the Binary Tree. The task is to print the Level order traversal of a tree is breadth first traversal for the tree.
Learn more : https://bit.ly/3EUF9HN
Tree
Find n-th node of inorder traversal : We do simple Inorder Traversal. While doing the traversal, we keep track of count of nodes visited so far. When count becomes n, we print the node.
Learn more : http://bit.ly/3YdwUQ8
Find n-th node in Postorder traversal of a Binary Tree : Given a Binary tree and a number N, write a program to find the N-th node in the Postorder traversal of the given Binary tree.
Learn more : http://bit.ly/3FgMYb5
Level order traversal line by line : Given the root of the Binary Tree. The task is to print the Level order traversal of a tree is breadth first traversal for the tree.
Learn more : https://bit.ly/3EUF9HN
👍1
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/array-pair-sum-divisibility-problem3257/1
“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 and submit your answer here : https://practice.geeksforgeeks.org/problems/array-pair-sum-divisibility-problem3257/1
practice.geeksforgeeks.org
Array Pair Sum Divisibility Problem | Practice | GeeksforGeeks
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
Explana
Example 1 :
Input : arr = [9, 5, 7, 3], k = 6
Output: True
Explana
👍4
Must Do DSA Topics Day 56
Tree
Reverse Level Order Traversal : The idea is to print the last level first, then the second last level, and so on. Like Level order traversal, every level is printed from left to right.
Learn more : https://bit.ly/3uKMwwI
Reverse tree path : Given a tree and node data, the task to reverse the path to that particular Node.
Learn more : https://bit.ly/3YaV8u9
Perfect Binary Tree Specific Level Order Traversal : In standard Level Order Traversal, we enqueue root into a queue 1st, then we dequeue ONE node from queue, process (print) it, enqueue its children into queue.
Learn more : https://bit.ly/3uKM4yw
Tree
Reverse Level Order Traversal : The idea is to print the last level first, then the second last level, and so on. Like Level order traversal, every level is printed from left to right.
Learn more : https://bit.ly/3uKMwwI
Reverse tree path : Given a tree and node data, the task to reverse the path to that particular Node.
Learn more : https://bit.ly/3YaV8u9
Perfect Binary Tree Specific Level Order Traversal : In standard Level Order Traversal, we enqueue root into a queue 1st, then we dequeue ONE node from queue, process (print) it, enqueue its children into queue.
Learn more : https://bit.ly/3uKM4yw
🔥1
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/articulation-point-1/1
“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 and submit your answer here : https://practice.geeksforgeeks.org/problems/articulation-point-1/1
practice.geeksforgeeks.org
Articulation Point - I | Practice | GeeksforGeeks
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 i
Must Do DSA Topics Day 57
Tree
Postorder traversal of Binary Tree without recursion and without stack : Given a binary tree, perform postorder traversal. We have discussed the below methods for postorder traversal.
1) Recursive Postorder Traversal.
2) Postorder traversal using Stack.
2) Postorder traversal using two Stacks.
Learn more : https://bit.ly/3FiUiTK
Diagonal Traversal of Binary Tree : Consider lines with a slope of -1 that cross through nodes. Print all diagonal elements in a binary tree that belong to the same line, given a binary tree.
Learn more : https://bit.ly/3VHo4s9
Iterative diagonal traversal of binary tree : Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree belonging to the same line.
Learn more : https://bit.ly/3FIOSCF
Tree
Postorder traversal of Binary Tree without recursion and without stack : Given a binary tree, perform postorder traversal. We have discussed the below methods for postorder traversal.
1) Recursive Postorder Traversal.
2) Postorder traversal using Stack.
2) Postorder traversal using two Stacks.
Learn more : https://bit.ly/3FiUiTK
Diagonal Traversal of Binary Tree : Consider lines with a slope of -1 that cross through nodes. Print all diagonal elements in a binary tree that belong to the same line, given a binary tree.
Learn more : https://bit.ly/3VHo4s9
Iterative diagonal traversal of binary tree : Consider lines of slope -1 passing between nodes. Given a Binary Tree, print all diagonal elements in a binary tree belonging to the same line.
Learn more : https://bit.ly/3FIOSCF
👍1🔥1
Must Do DSA Topics Day 58
Tree
Calculate depth of a full Binary tree from Preorder : Given preorder of a binary tree, calculate its depth(or height) [starting from depth 0]. The preorder is given as a string with two possible characters.
Learn more : https://bit.ly/3VHDBIt
Number of Binary Trees for given Preorder Sequence length : Count the number of Binary Tree possible for a given Preorder Sequence length n. In Preorder traversal, we process the root node first, then traverse the left child node and then right child node.
Learn more : https://bit.ly/3Fkt3rU
Modify a binary tree to get Preorder traversal using right pointers only : Given a binary tree. Modify it in such a way that after modification you can have a preorder traversal of it using only the right pointers. During modification, you can use right as well as left pointers.
Learn more : https://bit.ly/3Yi8BAD
Tree
Calculate depth of a full Binary tree from Preorder : Given preorder of a binary tree, calculate its depth(or height) [starting from depth 0]. The preorder is given as a string with two possible characters.
Learn more : https://bit.ly/3VHDBIt
Number of Binary Trees for given Preorder Sequence length : Count the number of Binary Tree possible for a given Preorder Sequence length n. In Preorder traversal, we process the root node first, then traverse the left child node and then right child node.
Learn more : https://bit.ly/3Fkt3rU
Modify a binary tree to get Preorder traversal using right pointers only : Given a binary tree. Modify it in such a way that after modification you can have a preorder traversal of it using only the right pointers. During modification, you can use right as well as left pointers.
Learn more : https://bit.ly/3Yi8BAD
👍1
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/f04fd67b26b4828b6180715d8b1700426b637247/1
“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 and submit your answer here : https://practice.geeksforgeeks.org/problems/f04fd67b26b4828b6180715d8b1700426b637247/1
practice.geeksforgeeks.org
Split Array Largest Sum | Practice | GeeksforGeeks
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,
Example 1:
Input:
N = 4,
👍2
Must Do DSA Topics Day 59
Tree
Construct Tree from given Inorder and Preorder traversals : In a Preorder sequence, the leftmost element is the root of the tree. So we know ‘A’ is the root for given sequences.
Learn more : https://bit.ly/3VSVcNL
Construct a tree from Inorder and Level order traversals : Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is an example to illustrate the problem.
Learn more : https://bit.ly/3uHLCRU
Construct Complete Binary Tree from its Linked List Representation : Given Linked List Representation of Complete Binary Tree, construct the Binary tree. A complete binary tree can be represented in an array in the following approach.
Learn more : https://bit.ly/3Ygp5ZT
Tree
Construct Tree from given Inorder and Preorder traversals : In a Preorder sequence, the leftmost element is the root of the tree. So we know ‘A’ is the root for given sequences.
Learn more : https://bit.ly/3VSVcNL
Construct a tree from Inorder and Level order traversals : Given inorder and level-order traversals of a Binary Tree, construct the Binary Tree. Following is an example to illustrate the problem.
Learn more : https://bit.ly/3uHLCRU
Construct Complete Binary Tree from its Linked List Representation : Given Linked List Representation of Complete Binary Tree, construct the Binary tree. A complete binary tree can be represented in an array in the following approach.
Learn more : https://bit.ly/3Ygp5ZT
Must Do DSA Topics Day 60
Tree
Check for Children Sum Property in a Binary Tree : Given a binary tree, you need to check whether sum of all covered elements is equal to sum of all uncovered elements or not.
Learn more : bit.ly/3UYtv4K
Check if a given Binary Tree is SumTree : Write a function that returns true if the given Binary Tree is SumTree else false. A SumTree is a Binary Tree where the value of a node is equal to the sum of the nodes present in its left subtree and right subtree.
Learn more : bit.ly/3VRYv7W
Check sum of Covered and Uncovered nodes of Binary Tree : Given a binary tree, the task is to check for every node, its value is equal to the sum of values of its immediate left and right child. For NULL values, consider the value to be 0.
Learn more : bit.ly/3hmNjRI
Get access to more topics of Tree : https://www.geeksforgeeks.org/binary-tree-data-structure/
Tree
Check for Children Sum Property in a Binary Tree : Given a binary tree, you need to check whether sum of all covered elements is equal to sum of all uncovered elements or not.
Learn more : bit.ly/3UYtv4K
Check if a given Binary Tree is SumTree : Write a function that returns true if the given Binary Tree is SumTree else false. A SumTree is a Binary Tree where the value of a node is equal to the sum of the nodes present in its left subtree and right subtree.
Learn more : bit.ly/3VRYv7W
Check sum of Covered and Uncovered nodes of Binary Tree : Given a binary tree, the task is to check for every node, its value is equal to the sum of values of its immediate left and right child. For NULL values, consider the value to be 0.
Learn more : bit.ly/3hmNjRI
Get access to more topics of Tree : https://www.geeksforgeeks.org/binary-tree-data-structure/
Problem Of The Day
"Complement"
Solve the problem to win points
You are given a binary string str. In a single operation, you can choose two indices L and R such that 1 ≤ L ≤ R ≤ N and complement the characters between L and R i.e strL, strL+1, …, strR. By complement, we mean change character 0 to 1 and vice-versa.
You task is to perform ATMOST one operation such that in final string number of 1s is maximised. If there is no need to completement, i.e., string contains all 1s, return -1. Else, return the two values denoting L and R. If there are multiple solutions, return the lexicographically smallest pair of L and R.
Example 1:
Input:
N = 3
str = "111"
Output: -1
Explanation: As all characters are '1',
so don't need to complement any.
Example 2:
Input:
N = 2
str = "01"
Output: 1 1
Explanation: After complementing [1, 1]
the string becomes "11".
Your Task:
You don't need to read input or print anything. Complete the function findRange() which takes the string str and the size of the string, n, as input parameters and returns an array of length 2 or 1 representing the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/complement3911/1
"Complement"
Solve the problem to win points
You are given a binary string str. In a single operation, you can choose two indices L and R such that 1 ≤ L ≤ R ≤ N and complement the characters between L and R i.e strL, strL+1, …, strR. By complement, we mean change character 0 to 1 and vice-versa.
You task is to perform ATMOST one operation such that in final string number of 1s is maximised. If there is no need to completement, i.e., string contains all 1s, return -1. Else, return the two values denoting L and R. If there are multiple solutions, return the lexicographically smallest pair of L and R.
Example 1:
Input:
N = 3
str = "111"
Output: -1
Explanation: As all characters are '1',
so don't need to complement any.
Example 2:
Input:
N = 2
str = "01"
Output: 1 1
Explanation: After complementing [1, 1]
the string becomes "11".
Your Task:
You don't need to read input or print anything. Complete the function findRange() which takes the string str and the size of the string, n, as input parameters and returns an array of length 2 or 1 representing the answer. You don't to print answer or take inputs.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/complement3911/1
practice.geeksforgeeks.org
Complement | Practice | GeeksforGeeks
You are given a binary string str. In a single operation, you can choose two indices L and R such that 1 ≤ L ≤ R ≤ N and complement the characters between L and R i.e strL, strL+1, …, strR. By complement, we mean change character 0 to
👍4👏1