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

"Next Greater Element"

Given an array arr[ ] of size n having distinct elements, the task is to find the next greater element for each element of the array in order of their appearance in the array.
Next greater element of an element in the array is the nearest element on the right which is greater than the current element.
If there does not exist next greater of current element, then next greater element for current element is -1. For example, next greater of the last element is always -1.

Example 1:
Input:
n = 4, arr[] = [1 3 2 4]
Output:
3 4 4 -1
Explanation:
In the array, the next larger element
to 1 is 3, 3 is 4, 2 is 4 and for 4?
since it doesn't exist, it is -1.

Example 2:
Input:
n = 5, arr[] = [6 8 0 1 3]
Output:
8 -1 1 3 -1
Explanation:
In the array, the next larger element to
6 is 8, for 8 there is no larger elements
hence it is -1, for 0 it is 1, for 1 it
is 3 and then for 3 there is no larger
element on right and hence -1.

Your Task:
This is a function problem. You only need to complete the function nextLargerElement() that takes list of integers arr[ ] and n as input parameters and returns list of integers of length N denoting the next greater elements for all the corresponding elements in the input array.

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

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

Powered by hirist.com
👍2
Must Do DSA Topics Day 90
Dynamic Programming
What is memoization? A Complete tutorial : In computing, memoization is used to speed up computer programs by eliminating the repetitive computation of results, and by avoiding repeated calls to functions that process the same input.
Learn more : http://bit.ly/3CUaz0P

Introduction to Dynamic Programming : Dynamic Programming is mainly an optimization over plain recursion. Wherever we see a recursive solution that has repeated calls for the same inputs, we can optimize it using Dynamic Programming.
Learn more : http://bit.ly/3w6Beni

Tabulation vs Memoizatation : Prerequisite – Dynamic Programming, How to solve Dynamic Programming problems? There are two different ways to store the values so that the values of a sub-problem can be reused.
Learn more : http://bit.ly/3ZI1F07
🚨Job Alert 🚨

Comviva is hiring for C/Golang Senior Technical Team Lead.

Location - Bangalore
Last date to apply - 31 Jan, 2023.

Apply via the following link and share the opportunity with your friends- https://practice.geeksforgeeks.org/jobs/Comviva-Srtechlead
👍2
Problem Of The Day

"Find the maximum GCD of the siblings of a Binary Tree"

Given a binary tree. The task is to find the maximum GCD of the siblings of this tree. You have to return the value of the node whose two immediate children has the maximum gcd.
If there are multiple such nodes, return the node which has the maximum value.

Siblings: Nodes with the same parent are called siblings.

GCD (Greatest Common Divisor) of two positive integers is the largest positive integer that divides both numbers without a remainder.

Note:
Return 0 if input tree is empty i.e level of tree is -1.
Consider those nodes which have a sibling.
Return 0 if no such pair of siblings found.


Example 1:
Input:
4
/ \
5 2
/ \
3 1
/ \
6 12

Output: 3
Explanation: For the above tree, the maximum
GCD for the siblings is formed for nodes 6 and 12
for the children of node 3.


Example 2:
Input:
1
/ \
3 5
/ \ / \
6 9 4 8

Output: 5
Explanation: For the above tree, the maximum
GCD for the siblings is formed for nodes 4 and 8
for the children of node 5.


Your Task:
You don't need to take input. Just complete the function maxGCD() that takes the root node as a parameter and returns the value of the node whose two immediate children has the maximum gcd.



Expected Time Complexity: O(|Number of nodes|) .
Expected Auxiliary Space: O(1) .

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

Powered by hirist.com
👍3
Must Do DSA Topics Day 91
Dynamic Programming
How to solve a Dynamic Programming Problem? : Dynamic Programming problems are all about the state and its transition. This is the most basic step which must be done very carefully because the state transition depends on the choice of state definition you make.
Learn more : http://bit.ly/3XrPUsW

Digit DP | Introduction : There are many types of problems that ask to count the number of integers ‘x‘ between two integers say ‘a‘ and ‘b‘ such that x satisfies a specific property that can be related to its digits.
Learn more : http://bit.ly/3kbAlXS

Sum over Subsets | Dynamic Programming : Given an array of 2n integers, we need to calculate function F(x) = ∑Ai such that x&i==i for all x. i.e, i is a bitwise subset of x. i will be a bitwise subset of mask x, if x&i==i.
Learn more : http://bit.ly/3kgChhP
Problem Of The Day
"Find the first node of loop in linked list"

Solve the problem to win points

Given a singly linked list of N nodes. Find the first node of the loop if the linked list has a loop. If a loop is present return the node data of the first node of the loop else return -1.

Example 1:
Input:

Output: 3
Explanation:
We can see that there exists a loop
in the given linked list and the first
node of the loop is 3.


Example 2:
Input:

Output: -1
Explanation: No loop exists in the above
linked list.So the output is -1.

Your Task:
The task is to complete the function findFirstNode() which contains reference to the head as only argument. This function should return the value of the first node of the loop if the linked list contains a loop, else return -1.

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

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

Powered by hirist.com
Must Do DSA Topics Day 92
Dynamic Programming
Fibonacci numbers : The Fibonacci numbers are the numbers in the following integer sequence.
Learn more : http://bit.ly/3XF4cqk

nth Catalan Number : Catalan numbers are defined as a mathematical sequence that consists of positive integers, which can be used to find the number of possibilities of various combinations.
Learn more : https://bit.ly/3kez3ve

Binomial Coefficient : A binomial coefficient C(n, k) also gives the number of ways, disregarding order, that k objects can be chosen from among n objects more formally, the number of k-element subsets (or k-combinations) of a n-element set.
Learn more : http://bit.ly/3HcDmQM

Get access to all the topics of Dynamic Programming: https://www.geeksforgeeks.org/dynamic-programming/?ref=gcse
Hey Geeks! Job-A-Thon 16 is here with exciting opportunities.😃🙌

This time, don't miss the chance to get your dream job.🎊

Apply now- https://practice.geeksforgeeks.org/contest/job-a-thon-16-hiring-challenge

Also, as an amazing human, you should share this with your friends and others!!🤌
👍1
🚨Job Alert 🚨

MangoApps is hiring WPF Engineer.

Experience - 2-6 Years
Location - Pune

Apply via the following link and share the opportunity with your friends - https://practice.geeksforgeeks.org/jobs/MangoApps-wpfengineer
👍2
Problem Of The Day

"Carpet into Box"

Solve the problem to win points

There is a carpet of a size a*b [length * breadth]. You are given a box of size c*d. The task is, one has to fit the carpet in the box in a minimum number of moves.

In one move, you can either decrease the length or the breadth of the carpet by half (floor value of its half).

Note: One can even turn the carpet by 90 degrees any number of times, wont be counted as a move.

Example 1:
Input:
A = 8, B = 13
C = 6, D = 10
Output:
Minimum number of moves: 1
Explanation:
Fold the carpet by breadth, 13/2 i.e. 6,
so now carpet is 6*8 and can fit fine.

Example 2:
Input:
A = 4, B = 8
C = 3, D = 10
Output:
Minimum number of moves: 1
Explanation: Fold the carpet by length , 4/2 i.e. 2,
so now carpet is 2*8 and can fit fine.

Your Task:
You don't need to read input or print anything. You only need to complete the function carpetBox() that takes an integer A, B, C and D as input and returns an integer denoting the minimum numbers of moves required to fit the carpet into the box.

Expected Time Complexity: O( max( log(a), log(b) ) ) .
Expected Auxiliary Space: O(1) .

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

Powered by hirist.com
👍4
Must Do DSA Topics Day 93
Trie
Trie Delete : During delete operation we delete the key in bottom up manner using recursion.
Learn more : https://www.geeksforgeeks.org/trie-delete/

Trie data structure : Trie data structure is defined as a Tree based data structure that is used for storing some collection of strings and performing efficient search operations on them.
Learn more : https://www.geeksforgeeks.org/introduction-to-trie-data-structure-and-algorithm-tutorials/

Displaying content of Trie : Trie is an efficient information retrieval data structure. In our previous post on trie we have discussed about basics of trie and how to insert and search a key in trie.
Learn more : https://www.geeksforgeeks.org/trie-display-content/
Problem Of The Day

"Maximum Weight Node"

Solve the problem to win points

Given a maze with N cells. Each cell may have multiple entry points but not more than one exit (i.e entry/exit points are unidirectional doors like valves).
You are given an array Edge[] of N integers, where Edge[i] contains the cell index that can be reached from cell i in one step. Edge[i] is -1 if the ith cell doesn't have an exit.
The task is to find the cell with maximum weight (The weight of a cell is the sum of cell indexes of all cells pointing to that cell). If there are multiple cells with the maximum weight return the cell with highest index.

Note: The cells are indexed with an integer value from 0 to N-1. If there is no cell pointing to the ith cell then the weight of the i'th cell is zero.

Example 1:
Input:
N = 4
Edge[] = {2, 0, -1, 2}
Output: 2
Explanation:
1 -> 0 -> 2 <- 3
weight of 0th cell = 1
weight of 1st cell = 0
(because there is no cell pointing to the 1st cell)
weight of 2nd cell = 0+3 = 3
weight of 3rd cell = 0
There is only one cell which has maximum weight
(i.e 2) So, cell 2 is the output.

Example 2:
Input:
N = 1
Edge[] = {-1}
Output: 0
Explanation:
weight of 0th cell is 0.
There is only one cell so
cell 0 has maximum weight.

Your task:
You dont need to read input or print anything. Your task is to complete the function maxWeightCell() which takes the integer N denoting the number of cells and the array Edge[] as input parameters and returns the cell which has maximum weight. If there are multiple answers then return the cell with highest index.

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

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

Powered by hirist.com
Must Do DSA Topics Day 94
Trie
Auto-complete feature using Trie : We are given a Trie with a set of strings stored in it. Now the user types in a prefix of his search query, we need to give him all recommendations to auto-complete his query based on the strings stored in the Trie.
Learn more : http://bit.ly/3XG6J3o

Minimum Word Break : Given a string s, break s such that every substring of the partition can be found in the dictionary. Return the minimum break needed.
Learn more : http://bit.ly/3wen7MX

Sorting array of strings (or words) using Trie : Given an array of strings, print them in alphabetical (dictionary) order. If there are duplicates in input array, we need to print them only once.
Learn more : http://bit.ly/3Wqwtjp

Get access to all the topics of Trie : https://www.geeksforgeeks.org/trie-insert-and-search/
Problem Of The Day
"Minimum X (xor) A"

Solve the problem to win points

Given two integers A and B, the task is to find an integer X such that (X XOR A) is minimum possible and the count of set bit in X is equal to the count of set bits in B.

Example 1:
Input:
A = 3, B = 5
Output: 3
Explanation:
Binary(A) = Binary(3) = 011
Binary(B) = Binary(5) = 101
The XOR will be minimum when x = 3
i.e. (3 XOR 3) = 0 and the number
of set bits in 3 is equal
to the number of set bits in 5.

Example 2:
Input:
A = 7, B = 12
Output: 6
Explanation:
(7)2= 111
(12)2= 1100
The XOR will be minimum when x = 6
i.e. (6 XOR 7) = 1 and the number
of set bits in 6 is equal to the
number of set bits in 12.
Your task :
You don't need to read input or print anything. Your task is to complete the function minVal() that takes integer A and B as input and returns the value of X according to the question.

Expected Time Complexity : O(log MAX(A,B))
Expected Auxiliary Space : O(1)

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

Powered by hirist.com
👍3
Problem Of The Day
"GCD Array"

Solve the problem to win points

You are given an array, arr of length N, and also a single integer K . Your task is to split the array arr into K non-overlapping, non-empty subarrays. For each of the subarrays, you calculate the sum of the elements in it. Let us denote these sums as S1, S2, S3, ..., SK. Where Si denotes the sum of the elements in the ith subarray from left.

Let G = GCD( S1, S2 ,S3 , ..., SK).

Find the maximum value of G that can be obtained.
The array may contain duplicate elements.

Example 1:
Input:
N = 5
K = 4
arr[] = {6, 7, 5, 27, 3}
Output: 3
Explanation:
Since K = 4, you have to split the array into 4 subarrays.
For optimal splitting, split the array into
4 subarrays as follows: [[6], [7, 5], [27], [3]]
Therefore, S1 = 6, S2 = 7 + 5 = 12, S3 = 27, S4 = 3
Hence, G = GCD(S1, S2, S3, S4) = GCD(6, 12, 27, 3) = 3
It can be shown that 3 is the maximum value of G that can be obtained.
Thus, the answer is 3.

Example 2:
Input:
N = 3
K = 2
arr[] = {1, 4, 5}
Output: 5
Explanation:
Since K = 2, you have to split the array into 2 subarrays.
For optimal splitting, split the array into
2 subarrays as follows: [[1, 4], [5]]
Therefore, S1 = 1 + 4 = 5, S2 = 5
Hence, G = GCD(S1, S2) = GCD(5,5) = 5
It can be shown that 5 is the maximum value of G that can be obtained.
Thus, the answer is 5.

Your Task:
You don't need to read input or print anything. Your task is to complete the function solve() which takes the array arr[] and its size N and an integer K as input parameters and returns the required maximum GCD value.


Expected Time Complexity: O(N * x)
Expected Auxiliary Space: O(x), x is the number of factors of the sum of all elements.

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

Powered by hirist.com
👍2
Problem Of The Day

"Geeks And The String"

Solve the problem to win points

Our geek loves to play with strings, Currently, he is trying to reduce the size of a string by recursively removing all the consecutive duplicate pairs. In other words, He can apply the below operations any number of times.

Remove all the consecutive duplicate pairs and concatenate the remaining string to replace the original string.
Your task is to find the string with minimum length after applying the above operations.

Note: If the string length become zero after applying operations, return "-1" as a string.


Example 1:
Input:
aaabbaaccd
Output:
ad
Explanation:
Remove (aa)abbaaccd =>abbaaccd
Remove a(bb)aaccd => aaaccd
Remove (aa)accd => accd
Remove a(cc)d => ad

Example 2:
Input:
aaaa
Output:
Empty String
Explanation:
Remove (aa)aa => aa
Again removing pair of duplicates then (aa)
will be removed and we will get 'Empty String'.

Your Task:
This is a function problem. You only need to complete the function removePair() that takes a string as a parameter and returns the modified string. Return an empty string if the whole string is deleted.

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

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

Powered by hirist.com
👍3
Must Do DSA Topics Day 95
Segment Tree
Segment Tree introduction : A simple solution is to run a loop from l to r and calculate the sum of elements in the given range. To update a value, simply do arr[i] = x. The first operation takes O(n) time and the second operation takes O(1) time.
Learn more : http://bit.ly/3XwFZTf

Range Minimum Query : We have introduced segment tree with a simple example in the previous post. In this post, Range Minimum Query problem is discussed as another example where Segment Tree can be used.
Learn more : http://bit.ly/3ZXylDd

Persistent Segment Tree : Segment Tree is itself a great data structure that comes into play in many cases. In this post we will introduce the concept of Persistency in this data structure. Persistency, simply means to retain the changes.
Learn more : http://bit.ly/3GZzbGL
Must Do DSA Topics Day 95
Segment Tree
Segment Tree introduction : A simple solution is to run a loop from l to r and calculate the sum of elements in the given range. To update a value, simply do arr[i] = x. The first operation takes O(n) time and the second operation takes O(1) time. 
Learn more : http://bit.ly/3XwFZTf

Range Minimum Query : We have introduced segment tree with a simple example in the previous post. In this post, Range Minimum Query problem is discussed as another example where Segment Tree can be used.
Learn more : http://bit.ly/3ZXylDd

Persistent Segment Tree : Segment Tree is itself a great data structure that comes into play in many cases. In this post we will introduce the concept of Persistency in this data structure. Persistency, simply means to retain the changes.
Learn more : http://bit.ly/3GZzbGL
🚨Job Alert 🚨
Midland Credit Management is hiring for Engineer III in Gurgaon.

Hurry up! The last date to apply is 31st January.

Apply via the following link and share it with your friends - https://practice.geeksforgeeks.org/jobs/mcmencore-SEIII
Problem Of The Day

"Convert an array to reduced form"

Solve the problem to win points

Given an array with N distinct elements, convert the given array to a reduced form where all elements are in range from 0 to N-1. The order of elements is same, i.e., 0 is placed in place of smallest element, 1 is placed for second smallest element, N-1 is placed for largest element.

Note: You don't have to return anything. You just have to change the the given array.

Example 1:
Input:
N = 3
Arr[] = {10, 40, 20}
Output: 0 2 1
Explanation: 10 is the least element so it
is replaced by 0. 40 is the largest
element so it is replaced by 3-1 = 2. And
20 is the 2nd smallest element so it is
replaced by 1.

Example 2:
Input:
N = 5
Arr[] = {5, 10, 40, 30, 20}
Output: 0 1 4 3 2
Explanation: As 5 is smallest element,
it's replaced by 0. Then 10 is 2nd
smallest element, so it's replaced by 1.
Then 20 is 3rd smallest element, so it's
replaced by 2. And so on.
Your Task:
You don't need to read input or print anything. Your task is to complete the function convert() which takes the array of integers arr and n as parameters and make changes in the given array.

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

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/convert-an-array-to-reduced-form1101/1

Powered by hirist.com
👍4