GeeksforGeeks
17.4K subscribers
1.83K photos
34 videos
24 files
2.76K links
Official Hub ✅

Your coding haven – where tech meets simplicity.

Discover:
😂 Coding Laughter
📘 Curated Study Material
💎 Exclusive Offers

Utilize every service we provide and make a promise to find better opportunities for yourself! #gfgkarlohojayega!
Download Telegram
Problem Of The Day
“Count the Number of Full Binary Trees”

Solve the problem to win points

Given an array arr[] of n integers, where each integer is greater than 1. The task is to find the number of Full binary tree from the given integers, such that each non-leaf node value is the product of its children value.

Note: Each integer can be used multiple times in a full binary tree. The answer can be large, return answer modulo 1000000007

Example 1:
Input:
n = 4
arr[] = {2, 3, 4, 6}
Output:
7
Explanation:
There are 7 full binary tree with
the given product property.
Four trees with single nodes
2 3 4 6
Three trees with three nodes
4
/ \
2 2 ,
6
/ \
2 3 ,
6
/ \
3 2


Example 2:
Input:
n = 3
arr[] = {2, 4, 5}
Output: 4
Explanation: There are 4 full binary tree
with the given product property.
Three trees with single nodes 2 4 5
One tree with three nodes
4
/ \
2 2


Your Task:
You don't need to read input or print anything. Your task is to complete the function numoffbt() which takes the array arr[]and its size n as inputs and returns the number of Full binary tree.

Expected Time Complexity: O(n. Log(n))
Expected Auxiliary Space: O(n)

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/count-the-number-of-full-binary-trees2525/1
👍2
Must Do DSA Topics day 37
Stack
Convert Infix expression to Postfix expression : The compiler scans the expression either from left to right or from right to left.
Consider the expression: a + b * c + d
The compiler first scans the expression to evaluate the expression b * c, then again scans the expression to add a to it.
Learn more : http://bit.ly/3ASwXqw

Prefix to Infix Conversion : An expression is called the Infix expression if the operator appears in between the operands in the expression. Simply of the form (operand1 operator operand2).
Learn more : http://bit.ly/3ihI71w

Prefix to Postfix Conversion : An expression is called the prefix expression if the operator appears in the expression before the operands. Simply of the form (operator operand1 operand2).
Learn more : http://bit.ly/3ieZTCp
👍2
Must Do DSA Topics day 38
Stack
Reverse a stack using recursion : The idea of the solution is to hold all values in Function Call Stack until the stack becomes empty. When the stack becomes empty, insert all held items one by one at the bottom of the stack.
Learn more : http://bit.ly/3AT5fty

Sort a stack using recursion : The idea of the solution is to hold all values in Function Call Stack until the stack becomes empty. When the stack becomes empty, insert all held items one by one in sorted order. and then print the stack.
Learn more : http://bit.ly/3ELI3P9

Sort a stack using a temporary stack : Given a stack of integers, sort it in ascending order using another temporary stack.
Learn more : http://bit.ly/3VEVYgR
Problem Of The Day
“Reorder List”

Given a singly linked list: A0→A1→...→An-2→An-1, reorder it to: A0→An-1→A1→An-2→A2→An-3→...
For example: Given 1->2->3->4->5 its reorder is 1->5->2->4->3.

Note: It is recommended do this in-place without altering the node's values.

Example 1:
Input:
LinkedList: 1->2->3
Output: 1 3 2
Explanation:
Here n=3, so the correct
order is A0→A2→A1

Example 2:
Input:
Explanation: 1->7->3->4
Output: 1 4 7 3
Explanation:
Here n=4, so the correct
order is A0→A3→A1→A2

Your Task:
The task is to complete the function reorder list() which should reorder the list as required. The reorder list is automatically printed by the driver's code.

Note: Try to solve without using any auxilliary space.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/reorder-list/1
👍1
Must Do DSA Topics day 39
Stack
Merge Overlapping Intervals : A simple approach is to start from the first interval and compare it with all other intervals for overlapping, if it overlaps with any other interval, then remove the other interval from the list and merge the other into the first interval.
Learn more : http://bit.ly/3VFoXkJ

Print ancestors of a given binary tree node without recursion : Given a Binary Tree and a key, write a function that prints all the ancestors of the key in the given binary tree.
Learn more : http://bit.ly/3FbprJP

Reverse a string using stack : The idea is to create an empty stack and push all the characters from the string into it. Then pop each character one by one from the stack and put them back into the input string starting from the 0’th index.
Learn more : http://bit.ly/3insPIq

Get access to more topics of Stack : http://bit.ly/3ubkryx 
👍1
Must Do DSA Topics day 40
Queue
Introduction to Queue in Data Structures and Algorithms : We define a queue to be a list in which all additions to the list are made at one end, and all deletions from the list are made at the other end.  The element which is first pushed into the order, the operation is first performed on that.
Learn more : http://bit.ly/3ENxkUw

Implementations of Queue Data Structure : Similar to Stack, Queue is a linear data structure that follows a particular order in which the operations are performed for storing data. 
Learn more : http://bit.ly/3GSg6b8

Applications, Advantages and Disadvantages of Queue : Similar to stacks, multiple operations can be performed on the queue. When an element is inserted in a queue, then the operation is known as Enqueue and when an element is deleted from the queue, then the operation is known as Dequeue.
Learn more : http://bit.ly/3UethGd
👍1
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/-rearrange-array-alternately-1587115620/1
👍2
Must Do DSA Topics Day 41
Queue
Implement Queue using Stacks : We are given a stack data structure with push and pop operations, the task is to implement a queue using instances of stack data structure and operations on them.
Learn more : http://bit.ly/3udaCzZ

LRU Cache Implementation : We are given the total possible page numbers that can be referred. We are also given a cache (or memory) size (The number of page frames that the cache can hold at a time). The LRU caching scheme is to remove the least recently used frame when the cache is full and a new page is referenced which is not there in the cache.
Learn more : http://bit.ly/3gMkOwz

Implement Stack using Queues : Given a Queue data structure that supports standard operations like enqueue() and dequeue(). The task is to implement a Stack data structure using only instances of Queue and Queue operations allowed on the instances.
Learn more : https://bit.ly/3V2ay24
Must Do DSA Topics Day 42
Queue
How to efficiently implement k Queues in a single array : Create a data structure kQueues that represents k queues. Implementation of kQueues should use only one array, i.e., k queues should use the same array for storing elements.
Learn more : http://bit.ly/3gOFjIL

Implement a stack using single queue : We are given queue data structure, the task is to implement stack using only given queue data structure. We have discussed a solution that uses two queues. In this article, a new solution is discussed that uses only one queue.
Learn more : https://bit.ly/3gJcHRd

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/3UionIh
👍2
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/check-if-it-is-possible-to-convert-one-string-into-another-with-given-constraints4116/1
👍2
Must Do DSA Questions Day 43
Queue
Check if a queue can be sorted into another queue using a stack : Given a Queue consisting of first n natural numbers (in random order). The task is to check whether the given Queue elements can be arranged in increasing order in another Queue using a stack.
Learn more : http://bit.ly/3P2czZN

Breadth First Traversal or BFS for a Graph : Breadth-First Traversal (or Search) for a graph is similar to Breadth-First Traversal of a tree. The only catch here is, that, unlike trees, graphs may contain cycles, so we may come to the same node again.
Learn more : http://bit.ly/3gRtemh

Level Order Tree Traversal : 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 : http://bit.ly/3EUF9HN
Must Do DSA Topics Day 44
Queue
Reverse a path in BST using queue : Take a queue and push all the element till that given key at the end replace node key with queue front element till root, then print inorder of the tree.
Learn more : http://bit.ly/3OUxXjw

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 : http://bit.ly/3B3vcXx

Number of siblings of a given Node in n-ary Tree : Given an N-ary tree, find the number of siblings of given node x. Assume that x exists in the given n-ary tree.
Learn more : http://bit.ly/3UkXzHu
👍4
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/aggressive-cows/1
Must Do DSA Topics Day 45
Queue
Reversing a Queue : For reversing the queue one approach could be to store the elements of the queue in a temporary data structure in a manner such that if we re-insert the elements in the queue they would get inserted in reverse order.
Learn more : http://bit.ly/3F1Zzi2

Reversing a queue using recursion : Given a queue, write a recursive function to reverse it.
Standard operations allowed :
enqueue(x) : Add an item x to rear of queue.
dequeue() : Remove an item from front of queue.
empty() : Checks if a queue is empty or not.
Learn more : http://bit.ly/3EVeYAZ

Reversing the first K elements of a Queue : Given an integer k and a queue of integers, The task is to reverse the order of the first k elements of the queue, leaving the other elements in the same relative order.
Learn more : http://bit.ly/3iziTvK
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/alternate-vowel-and-consonant-string2939/1
Must Do DSA Topics Day 46
Queue
Level order traversal in spiral form : Given a Binary Tree, the task is to print the Level order traversal of the Binary Tree in spiral form i.e, alternate order.
Learn more : http://bit.ly/3Fo9wI1

Sliding Window Maximum (Maximum of all subarrays of size k) : Given an array and an integer K, find the maximum for each and every contiguous subarray of size K.
Learn more : Given an array and an integer K, find the maximum for each and every contiguous subarray of size K.
Learn more : http://bit.ly/3H8zGQk

Find the first circular tour that visits all petrol pumps : Given information about N petrol pumps (say arr[]) that are present in a circular path. The information consists of the distance of the next petrol pump from the current one (in arr[i][1]) and the amount of petrol stored in that petrol pump (in arr[i][0]).
Learn more : http://bit.ly/3H8loPK
👍1
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/shortest-path-by-removing-k-walls/1
👍2
Must Do DSA Topics Day 47
Queue
Smallest multiple of a given number made of digits 0 and 9 only : We are given an integer N. We need to write a program to find the least positive integer X made up of only digits 9’s and 0’s, such that, X is a multiple of N.
Learn more : http://bit.ly/3XQclcl

Iterative Method to find Height of Binary Tree : The idea is to traverse level by level. Whenever move down to a level, increment height by 1 (height is initialized as 0). Count number of nodes at each level, stop traversing when the count of nodes at the next level is 0.
Learn more : http://bit.ly/3VRzYzj

Implement PriorityQueue through Comparator in Java : Priority Queue, Comparator Priority Queue is like a regular queue, but each element has a “priority” associated with it. In a priority queue, an element with high priority is served before an element with low priority.
Learn more : http://bit.ly/3XTCsiu
👍1
Must Do DSA Topics Day 48
Queue
First negative integer in every window of size k : Given an array and a positive integer k, find the first negative integer for each window(contiguous subarray) of size k. If a window does not contain a negative integer, then print 0 for that window.
Learn more : http://bit.ly/3uqPBlw

Minimum sum of squares of character counts in a given string after removing k characters : Given a string of lowercase alphabets and a number k, the task is to print the minimum value of the string after removal of ‘k’ characters. The value of a string is defined as the sum of squares of the count of each distinct character.
Learn more : http://bit.ly/3usE5Gt

Queue based approach for first non-repeating character in a stream : Given a stream of characters and we have to find first non repeating character each time a character is inserted to the stream.
Learn more : http://bit.ly/3Fomm9p

Get access to more topics of Queue : http://bit.ly/3P2yqAm
👍1
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 and submit your answer here : https://practice.geeksforgeeks.org/problems/distance-of-nearest-cell-having-1-1587115620/1