GeeksforGeeks
19.1K subscribers
1.66K photos
34 videos
24 files
2.63K 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
🚨Job Alert🚨

Akash Educational Services is hiring Senior Salesforce Developer in Gurgaon and Bengalore.

Interesting candidates can apply via the following link and share the opportunity with your friends - https://practice.geeksforgeeks.org/jobs/AkashByjus_salesforcedev
Problem Of The Day
"Count Lucky Permutations"

Solve the problem to win points

You are given an array arr[ ] of integers having N elements and a non-weighted undirected graph having N nodes and M edges. The details of each edge in the graph is given to you in the form of list of list.
Your task is to find the number of lucky permutations of the given array.

An array permutation is said to be lucky if for every node Vi [1 < i < N-1] in the array there exists an edge between the nodes Vi and Vi+1 in the given graph.

Example 1:
Input:
N = 3, M = 2
arr = {1, 2, 3}
graph = {{3, 1}, {1, 2}}
Output:
2
Explanation:
All possible permutations of the
array are as follows-
{1,2,3}: There is an edge between 1 and
2 in the graph but not betwen 2 and 3.

{2,1,3}: There is an edge between (2,1)
and (1,3) in the graph.

{3,1,2}: There is an edge between (3,1)
and (1,2) in the graph.

Out of the 3 possible permutations,
2 are lucky. Therefore, answer is 2.

Example 2:
Input:
n = 2, m = 1
arr = {1, 1}
graph = {{1, 2}}
Output :
0
Explanation:
There is no lucky permutation in the
given graph.

Your Task:
You don't need to read input or print anything. Your task is to complete the function luckyPermutations() which takes the two integers N and M, an array arr[ ] and a list of lists named graph of size M as input parameters and returns the count of lucky permutations.


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

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

Powered by hirist.com
Must Do DSA Topics Day 89
Backtracking
Generate a combination of minimum coins that sums to a given value : Given an array arr[] of size N representing the available denominations and an integer X. The task is to find any combination of the minimum number of coins of the available denominations such that the sum of the coins is X.
Learn more : http://bit.ly/3IHlLS7

Generate all possible permutations of a Number divisible by N : Given a numerical string S, the task is to print all the permutations of the string which are divisible by N.
Learn more : http://bit.ly/3Xtkx1a

Print all possible ways to write N as sum of two or more positive integers : Given an integer N, the task is to print all the possible ways in which N can be written as the sum of two or more positive integers.
Learn more : http://bit.ly/3wjcKrj

Get access to all the topics of Backtracking : https://www.geeksforgeeks.org/category/dsa/algorithm/backtracking/
👍1
Problem Of The Day
"Maximum Number of Toys"

Solve the problem to win points

You are given N toys in a shop .
The cost of each toy is represented by an array A[]. You are given Q queries, For ith query, You have a C amount of money which you can use to purchase the toys. Also there are K broken toys and you won't purchase them. The task is to calculate the maximum number of toys you can purchase using the C amount of money.

Note: 1 based indexing is used. Each query is treated independently.
Query definition: The first element represents an integer C where C=Queries[i][0].
The second element represents an integer K, where K = Queries[i][1].
The next K integers represent the indices of broken toys which are Queries[i][j] ,j>1

Example 1:
Input:
N = 5
A[] = {8, 6, 9, 2, 5}
Q = 2
Query[][] = {{12,2,3,4},{30,0}}
Output:
2 5
Explanation:
Query 1: C = 12, K = 2 ,
Indices of Broken toys is {3,4}
Indices of Available toys are {1,2,5}
If we purchase the toys 2 and 5 ,
then cost = A[2]+A[5]= 6+5 = 11,
Therefore,We purchase the 2 toys
using 11 amount of money.
Query 2: C = 30, K = 0
There is no broken toy.
We can purchase all toys,
cost = A[1]+A[2]+A[3]+A[4]+A[5]= 30
Therefore,We purchase the 5 toys
using 30 amount of money.

Example 2:
Input:
N = 2
A[] = {3,3}
Q = 1
Query[][] = {{1,0}}
Output:
0
Explanation:
Query 1: C = 1, K = 0 ,
There is no broken toy.
We have not enough amount to purchase
any toy.

Your Task:
You don't need to read input or print anything. Your task is to complete the function maximumToys() which takes the integer N and array A[ ], integer Q and 2D array Queries[][] as input parameters and returns the array of answers of each query.

Expected Time Complexity: O(NLogMx + Q*K*LogMx + Q*(LogMx)2)
Expected Auxiliary Space: O(Mx)
Where Mx is the maximum element present in the array A[i].

Solve and submit your answer here : https://practice.geeksforgeeks.org/problems/maximum-number-of-toys/1

Powered by hirist.com
👍5
MangoApps is hiring for DevOps Engineer in Pune.

Interested candidates can apply via the following link and also share the opportunity with friends- https://practice.geeksforgeeks.org/jobs/MangoApps-Devops
👍1
Problem Of The Day
"Count the Substring"

Solve the problem to win points

Given a binary string S consists only of 0s and 1s. The task is to calculate the number of substrings that have more 1s than 0s.

Example 1:
Input:
S = "011"
Output: 4
Explanation: There are 4 substring which
has more 1s than 0s. i.e "011","1","11" and "1"

Example 2:
Input:
S = "0000"
Output: 0
Explanation: There is no substring
which has more 1s than 0s
Your Task:
You dont need to read input or print anything. Complete the function countSubstring() which takes the string S as input parameter and returns the number of substring which has more 1s than 0s.

Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)

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

Powered by hirist.com
👍1
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