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
"Burst Balloons"

Solve the problem to win points

You are given N balloons, indexed from 0 to n - 1. Each balloon is painted with a number on it represented by an array arr. You are asked to brust all the balloons.

If you brust the ith  balloon,, you will get arr[ i - 1 ] * arr[ i ] * arr[ i + 1] coins. If i - 1, or i + 1 goes out of bounds of the array, consider it as if there is a balloon with a 1 painted on it.
Return the maximum coins you can collect by brusting the balloons wisely.

Example 1:
Input:
N = 4
arr[ ] = {3, 1, 5, 8}
Output: 167
Explanation: 
arr[ ] = {3, 1, 5, 8}  - - > {3, 5, 8} - - > {3, 8} - - > { 8} - -> { }
coins = 3 *1 *5,      +      3*5*8     +   1*3*8   +  1*8*1   = 167

Example 2:
Input:
N = 2
arr[ ] = {1, 10}
Output: 20

Your Task:
You don't need to read input or print anything. Your task is to complete the function maxCoins() which takes the array of integers arr and N as parameters and returns the maximum coin you can collect.

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

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/burst-balloons/1

Powered by hirist.com
πŸ‘1
Must Do DSA Topics Day 70
Heap
Print all nodes less than a value x in a Min Heap : Given a binary min heap and a value x, print all the binary heap nodes having value less than the given value x.
Learn more : https://bit.ly/3hLMF06

Median of Stream of Running Integers using STL : Given that integers are being read from a data stream. Find the median of all the elements read so far starting from the first integer till the last integer.
Learn more : https://bit.ly/3viNyAl

Largest triplet product in a stream : Given a stream of integers represented as arr[]. For each index i from 0 to n-1, print the multiplication of largest, second largest, third largest element of the subarray arr[0…i]. If i < 2 print -1.
Learn more : https://bit.ly/3VnXoLY

Get access to all the topics of heap: https://www.geeksforgeeks.org/heap-data-structure/
Problem Of The Day
"Wine Buying and Selling"

Solve the problem to win points

Given an array, Arr[] of size N represents N house built along a straight line with equal distance between adjacent houses. Each house has a certain number of wine and they want to buy/sell those wines to other houses. Transporting one bottle of wine from one house to an adjacent house results in one unit of work. The task is to find the minimum number of work is required to fulfill all the demands of those N houses.

if arr[i] < 0, then ith house wants to sell arr[i] number of a wine bottle to other houses.

if arr[i] > 0, then ith house wants to buy arr[i] number of a wine bottle from other houses.

Note: One have to print the minimum number such that, all the house can buy/sell wine to each other.
It is guaranteed that sum of all the elements of the array will be 0.

Example 1:
Input: N = 5, Arr[] = {5, -4, 1, -3, 1}
Output: 9
Explanation:
1th house can sell 4 wine bottles to 0th house,
total work needed 4*(1-0) = 4, new arr[] = 1,0,1,-3,1
now 3rd house can sell wine to 0th, 2th and 4th.
so total work needed = 1*(3-0)+1*(3-2)+1*(4-3) = 5
So total work will be 4+5 = 9

Example 2:
Input: N = 5,
Arr[]={-1000, -1000, -1000, 1000, 1000, 1000}
Output: 9000
Explanation:
0th house sell 1000 wine bottles to 3rd house,
total work needed 1000*(3-0) = 3000.
1st house sell 1000 wine bottles to 4th house,
total work needed 3000 + 1000*(3-0) = 6000.
2nd house sell 1000 wine bottles to 5th house,
total work needed 6000 + 1000*(3-0) = 9000.
So total work will be 9000 unit.

Your Task:
You don't need to read input or print anything. Complete the function wineSelling() which takes the array Arr[] and its size N as input parameters and returns an integer as output.

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

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/wine-buying-and-selling/1

Powered by hirist.com
πŸ‘1
Must Do DSA Topics Day 71
Graph
Introduction to Graphs : A Graph is a non-linear data structure consisting of vertices and edges. The vertices are sometimes also referred to as nodes and the edges are lines or arcs that connect any two nodes in the graph.
Learn more : https://bit.ly/3vhejp8

Graph and its representations : Graphs are used to represent many real-life applications: Graphs are used to represent networks. The networks may include paths in a city or telephone network or circuit network.
Learn more : https://bit.ly/3WER0kv

Types of Graphs with Examples : Any graph which contains some parallel edges but doesn’t contain any self-loop is called a multigraph.
Learn more : https://bit.ly/3GfOc8h
πŸ‘2
Job Alert πŸ’ΌπŸ”Š
Micro Focus is hiring for Software Development Engineer - Java in Bangalore.

Interested candidates can apply and share the job opportunity with friends.πŸ˜ƒπŸ€Œ

Click on the following link for more details: https://practice.geeksforgeeks.org/jobs/microfocus-sdejava
Problem Of The Day
"Missing number in matrix"

Solve the problem to win points

Given a matrix of size n x n such that it has only one 0, Find the positive number (greater than zero) to be placed in place of the 0 such that sum of the numbers in every row, column and two diagonals become equal. If no such number exists, return -1.

Note: Diagonals should be only of the form matrix[i][i] and matrix[i][n-i-1]. n is always greater than 1.

Example 1:
Input: matrix = {{5, 5}, {5, 0}}
Output: 5
Explanation: The matrix is
5 5
5 0
Therefore If we place 5 instead of 0, all
the element of matrix will become 5.
Therefore row 5+5=10, column 5+5=10 and
diagonal 5+5=10, all are equal.

Example 2:
Input: matrix = {{1, 2, 0}, {3, 1, 2},
{2, 3, 1}}
Output: -1
Explanation: It is not possible to insert
an element in place of 0 so that the
condition is satisfied.thus result is -1.

Your Task:
You don't need to read or print anyhting. Your task is to complete the function MissingNo() which takes the matrix as input parameter and returns the number which should be placed in place of 0 such that the condition gets satisfied. If not possible return -1.

Expected Time Complexity: O(n * n)
Expected Space Complexity: O(2 * n)

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/missing-number-in-matrix5316/1

Powered by hirist.com
πŸ‘3
Must Do DSA Topics Day 72
Graph
Applications of Depth First Search : Depth-first search (DFS) is an algorithm (or technique) for traversing a graph. DFS uses a stack data structure for the traversal. A graph can have more than one DFS traversal.
Learn more : https://bit.ly/3vfC9S1

Applications of Breadth First Traversal : We have earlier discussed Breadth First Traversal Algorithm for Graphs. We have also discussed Applications of Depth First Traversal. In this article, applications of Breadth First Search are discussed.
Learn more : https://bit.ly/3gjipte

Iterative Depth First Search : Depth First Traversal (or Search) for a graph is similar to Depth First Traversal (DFS) of a tree. The only catch here is, unlike trees, graphs may contain cycles, so a node might be visited twice.
Learn more : https://bit.ly/3Gh2mWt
πŸ‘1
Job Alert πŸ’ΌπŸ”Š
LinuXbean is hiring for PHP Developer in Indore.

Interested candidates can apply and share the job opportunity with friends.

Click on the following link for more details: https://practice.geeksforgeeks.org/jobs/PHP-DeveloperLinuxBean
πŸ‘1
Problem Of The Day
"Akku and Binary Numbers"

Solve the problem to win points

Akku likes binary numbers and she likes playing with these numbers. Her teacher once gave her a question.For given value of L and R, Akku have to find the count of number X, which have only three-set bits in it's binary representation such that "L ≀ X ≀ R".Akku is genius, she solved the problem easily. Can you do it ??

Example 1:
Input:
L = 11 and R = 19
Output: 4
Explanation:
There are 4 such numbers with 3 set bits in
range 11 to 19.
11 -> 1011
13 -> 1101
14 -> 1110
19 -> 10011

Example 2:
Input:
L = 25 and R = 29
Output: 3
Explanation:
There are 3 such numbers with 3 set bits in
range 25 to 29.
25 -> 11001
26 -> 11010
28 -> 11100

Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the integer L and R as input parameters and returns the count of number X, which have only three-set bits in its binary representation such that "L ≀ X ≀ R".

Expected Time Complexity: O(633) + O(log(R-L))
Expected Auxiliary Space: O(633)

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/akku-and-binary-numbers0902/1

Powered by hirist.com
πŸ‘1
Must Do DSA Topics Day 73
Graph
Detect Cycle in a Directed Graph : Given the root of a Directed graph, The task is to check whether the graph contains a cycle if yes then return true, return false otherwise.
Learn more : https://bit.ly/3VpGyw8

Detect cycle in an undirected graph : Given an undirected graph, The task is to check if there is a cycle in the given graph.
Learn more : https://bit.ly/3FUgJib

Detect cycle in a direct graph using colors : Given a directed graph, check whether the graph contains a cycle or not. Your function should return true if the given graph contains at least one cycle, else return false.
Learn more : https://bit.ly/3vidgoO
Problem Of The Day
"Container With Most Water"

Solve the problem to win points

Given N non-negative integers a1,a2,....an where each represents a point at coordinate (i, ai). N vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i,0). Find two lines, which together with x-axis forms a container, such that it contains the most water.

Note : In Case of single verticle line it will not be able to hold water.

Example 1:
Input:
N = 4
a[] = {1,5,4,3}
Output: 6
Explanation: 5 and 3 are distance 2 apart.
So the size of the base = 2. Height of
container = min(5, 3) = 3. So total area
= 3 * 2 = 6.

Example 2:
Input:
N = 5
a[] = {3,1,2,4,5}
Output: 12
Explanation: 5 and 3 are distance 4 apart.
So the size of the base = 4. Height of
container = min(5, 3) = 3. So total area
= 4 * 3 = 12.
Your Task :
You only need to implement the given function maxArea. Do not read input, instead use the arguments given in the function and return the desired output.

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

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/container-with-most-water0535/1

Powered by hirist.com
πŸ‘2
Must Do DSA Topics Day 74
Graph
Dijkstra’s shortest path algorithm : Given a graph and a source vertex in the graph, find the shortest paths from the source to all vertices in the given graph.
Learn more : https://bit.ly/3YRz3kz

Bellman–Ford Algorithm : Given a graph and a source vertex src in the graph, find the shortest paths from src to all vertices in the given graph. The graph may contain negative weight edges.
Learn more : https://bit.ly/3YRYz9C

Floyd Warshall Algorithm : The Floyd Warshall Algorithm is for solving all pairs of shortest-path problems. The problem is to find the shortest distances between every pair of vertices in a given edge-weighted directed Graph.
Learn more : https://bit.ly/3WoTGmQ
πŸ‘1
Problem Of The Day
"Largest subtree sum in a tree"

Solve the problem to win points

Given a binary tree. The task is to find subtree with maximum sum in the tree and return its sum.

Example 1:
Input:
1
/ \
2 3
/ \ / \
4 5 6 7
Output: 28
Explanation:
As all the tree elements are positive,
the largest subtree sum is equal to
sum of all tree elements.

Example 2:
Input:
1
/ \
-2 3
/ \ / \
4 5 -6 2
Output: 7
Explanation:
Subtree with largest sum is :
-2
/ \
4 5
Also, entire tree sum is also 7.

Your Task:
You don't need to read input or print anything. Your task is to complete the function findLargestSubtreeSum() which takes the root of a binary tree and returns an integer.

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

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/largest-subtree-sum-in-a-tree/1

Powered by hirist.com
πŸ‘1
Must Do DSA Topics Day 75
Graph
Prim’s Minimum Spanning Tree (MST) : Prim’s algorithm always starts with a single node and it moves through several adjacent nodes, in order to explore all of the connected edges along the way.
Learn more : https://bit.ly/3Vr7tYD

Kruskal’s Minimum Spanning Tree Algorithm : A Spanning tree is a subset to a connected graph G, where all the edges are connected, i.e, one can traverse to any edge from a particular edge with or without intermediates.
Learn more : https://bit.ly/3I6v39X

Difference between Prim’s and Kruskal’s algorithm for MS : Given a connected and undirected graph, a spanning tree of that graph is a subgraph that is a tree and connects all the vertices together.
Learn more : https://bit.ly/3VtsiTa
Problem Of The Day
"Asteroid Collision"

Solve the problem to win points

We are given an integer array asteroids of size N representing asteroids in a row. For each asteroid, the absolute value represents its size, and the sign represents its direction (positive meaning right, negative meaning left). Each asteroid moves at the same speed.

Find out the state of the asteroids after all collisions. If two asteroids meet, the smaller one will explode. If both are of same size, both will explode. Two asteroids moving in the same direction will never meet.

Example 1:
Input:
N = 3
asteroids[ ] = {3, 5, -3}
Output: {3, 5}
Explanation: The asteroid 5 and -3 collide resulting in 5. The 5 and 3 never collide.

Example 2:
Input:
N = 2
asteroids[ ] = {10, -10}
Output: { }
Explanation: The asteroid -10 and 10 collide exploding each other.

Your Task:
You don't need to read input or print anything. Your task is to complete the function asteroid Collision() which takes the array of integers asteroids and N as parameters and returns the state of asteroids after all collisions.

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

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/asteroid-collision/1

Powered by hirist.com
πŸ‘1
Must Do DSA Topics Day 76
Graph
Topological Sorting : Topological sorting for Directed Acyclic Graph (DAG) is a linear ordering of vertices such that for every directed edge u v, vertex u comes before v in the ordering.
Learn more : http://bit.ly/3IaMcPY

All topological sorts of a Directed Acyclic Graph : In a Directed acyclic graph many a times we can have vertices which are unrelated to each other because of which we can order them in many ways.
Learn more : https://bit.ly/3YTeggK

Kahn’s Algorithm for Topological Sorting : In this article, we will see another way to find the linear ordering of vertices in a directed acyclic graph (DAG).
Learn more : https://bit.ly/3IaMoia
πŸ‘1
Problem Of The Day
"Single valued subtree"

Solve the problem to win points

Given a binary tree, count the number of Single Valued Subtrees. A Single Valued Subtree is one in which all the nodes have same value.

Example 1
Input :
5
/ \
1 5
/ \ \
5 5 5
Output : 4
Explanation :
There are 4 subtrees with single values.

Example 2:
Input:
5
/ \
4 5
/ \ \
4 4 5
Output: 5
Explanation:
There are five subtrees with single values.
Your task :
You don't have to read input or print anything. Your task is to complete the function singlevalued() which takes the root of the tree as input and returns the count of single valued subtrees.

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

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/single-valued-subtree/1

Powered by hirist.com
πŸ‘3
Must Do DSA Topics Day 77
Graph
Find length of the largest region in Boolean Matrix : Consider a matrix, where each cell contains either a β€˜0’ or a β€˜1’, and any cell containing a 1 is called a filled cell.
Learn more : https://bit.ly/3YVVnd4

Count number of trees in a forest : Given n nodes of a forest (collection of trees), find the number of trees in the forest.
Learn more : https://bit.ly/3vrdx8R

A Peterson Graph Problem : Let’s consider a walk W in graph G, which consists of L vertices W1, W2, …, WL. A string S of L letters β€˜A’ – β€˜E’ is realized by walking W if the sequence of letters written along W is equal to S. Vertices can be visited multiple times while walking along W.
Learn more : https://bit.ly/3GsEz6b

Get access to all the topics of Graph : https://www.geeksforgeeks.org/greedy-algorithms/?ref=gcse
Problem Of The Day
"Find minimum number of Laptops required"

Solve the problem to win points

There are N jobs and the start and finish time of the jobs are given in arrays start[] and end[] respectively. Each job requires one laptop and laptops can't be shared. Find the minimum number of laptops required given that you can give your laptop to someone else when you are not doing your job.

Example 1:
Input:
N = 3
start[] = {1, 2, 3}
end[] = {4, 4, 6}
Output:
3
Explanation:
We can clearly see that everyone's supposed to
be doing their job at time 3. So, 3 laptops
will be required at minimum.

Example 2:
Input:
N = 3
start[] = {1, 5, 2}
end[] = {2, 6, 3}
Output :
1
Explanation:
All jobs can be done using 1 laptop only.

Your Task:
You don't need to read input or print anything. Your task is to complete the function minLaptops() which takes an integer N and two arrays start and end denoting starting and ending time of N jobs and returns minimum laptops required.

Expected Time Complexity: O(N*logN)
Expected Auxiliary Space: O(N)

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/af49b143a4ead583e943ca6176fbd7ea55b121ae/1

Powered by hirist.com
Must Do DSA Topics Day 78
Greedy
Activity Selection Problem : If a Greedy Algorithm can solve a problem, then it generally becomes the best method to solve that problem as the Greedy algorithms are in general more efficient than other techniques like Dynamic Programming.
Learn more : https://bit.ly/3G9PXm2

Egyptian Fraction : A fraction is unit fraction if numerator is 1 and denominator is a positive integer, for example 1/3 is a unit fraction. Such a representation is called Egyptian Fraction as it was used by ancient Egyptians.
Learn more : https://bit.ly/3vuHlSg

Job Sequencing Problem : Given an array of jobs where every job has a deadline and associated profit if the job is finished before the deadline.
Learn more : https://bit.ly/3VCmXt0
πŸ‘3