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"                                                     
                                                     
Solve this question to get points.

Given an array A of size N of integers. Your task is to find the sum of minimum and maximum element in the array.

Full Problem: https://practice.geeksforgeeks.org/problems/max-min/1
Must Do-DSA Topics: Day 11

Know your sorting algorithm: Ever wondered how sort() function we use in C++/Java or sorted() in Python work internally? Here is a list of all the inbuilt sorting algorithms of different programming languages and the algorithm they use internally. Check it out now: https://bit.ly/3CqpEGs

Comparator function of qsort() in C: Standard C library provides qsort() that can be used for sorting an array. As the name suggests, the function uses QuickSort algorithm to sort the given array. The key point about qsort() is comparator function comparator. Know more about Comparator function here: https://bit.ly/3g7vDbR

Array.sort() in Java with examples: Array class is a class containing static methods that are used with arrays in order to search, sort, compare, insert elements, or return a string representation of an array. Know more about them here: https://bit.ly/3yBFQDt

You can find everything you need to know here: https://bit.ly/3CiEoHr
πŸ‘8❀3πŸ”₯1
Problem Of The Day
"Print Diagonally"

Solve the problem to win points

Give a N * N square matrix A, return all the elements of its anti-diagonals from top to bottom.

Your Task:
You don't need to read input or print anything. Your task is to complete the function downwardDigonal() which takes an integer N and a 2D matrix A[ ][ ] as input parameters and returns the list of all elements of its anti-diagonals from top to bottom.

Find the complete problem and submit your answers here : https://practice.geeksforgeeks.org/problems/print-diagonally4331/1
πŸ‘6πŸ‘4❀1
Must Do-DSA Topics : Day 12

Know your sorting Algorithm | Set 2 : Learn about C++’s Sorting Weapon, Introsort. 
What is Introsort? 
Simply putting, it is the best sorting algorithm around. It is a hybrid sorting algorithm, which means that it uses more than one sorting algorithms as a routine. 
Checkout more here :
https://bit.ly/3eDblXf

Collections.sort() in Java with Examples : java.util.Collections.sort() method is present in java.util.Collections class. It is used to sort the elements present in the specified list of Collection in ascending order.
Checkout more here : https://bit.ly/3CGPVQX

std::sort() in C++ STL :
C++ STL provides a similar function sort (like qsort in C) that sorts a vector or array (items with random access)

It generally takes two parameters, the first one being the point of the array/vector from where the sorting needs to begin.
Checkout more : https://bit.ly/3MMlOfG

You can find everything you need to know here : https://bit.ly/3CiEoHr
πŸ‘3
Problem Of The Day
"Hamiltonian Path"

Solve the problem to win points

A Hamiltonian path, is a path in an undirected graph that visits each vertex exactly once. Given an undirected graph, the task is to check if a Hamiltonian path is present in it or not.

Example 1:

Input:
N = 4, M = 4
Edges[][]= { {1,2}, {2,3}, {3,4}, {2,4} }
Output:
1
Explanation:
There is a hamiltonian path:
1 -> 2 -> 3 -> 4

Your task:
You don't need to read input or print anything. Your task is to complete the function check() which takes the N (the number of vertices), M (Number of edges) and the list of Edges[][] (where Edges[i] denotes the undirected Edge between vertices Edge[i][0] and Edges[i][1] ) as input parameter and returns true (boolean value) if the graph contains Hamiltonian path,otherwise returns false.


Expected Time Complexity: O(N!).
Expected Auxiliary Space: O(N+M).

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/hamiltonian-path2522/1
πŸ‘4
Must Do DSA Topics day 13
Matrix
What is Matrix in Data Structure?

A matrix represents a collection of numbers arranged in an order of rows and columns. It is necessary to enclose the elements of a matrix in parentheses/brackets.

Rotate elements in a Matrix : The idea is to use loops similar to the program for printing a matrix in spiral form. One by one rotate all rings of elements, starting from the outermost. To rotate a ring, we need to do following.
Move elements of top row.
Move elements of last column.
Move elements of bottom row.
Move elements of first column.
Learn more : https://bit.ly/3MHLE4e

Inplace rotate square matrix by 90 degrees | Set 1:
Given a square matrix, turn it by 90 degrees in an anti-clockwise direction without using any extra space
Learn more : https://bit.ly/3gmaV8c

Rotate a matrix by 90 degree without using any extra space | Set 2 : Given a square matrix, turn it by 90 degrees in anti-clockwise direction without using any extra space.
Learn More : https://bit.ly/3D65suZ
❀3πŸ‘2
Problem Of The Day
"Number Formation"

Solve the problem to win points

Given three integers x, y, and z, the task is to find the sum of all the numbers formed by
having 4 at most x times, having 5 at most y times, and having 6 at most z times as a digit.

Note: Output the sum modulo 109+7.

Example 1:

Input: X = 1, Y = 1, Z = 1
Output: 3675
Explanation: 4 + 5 + 6 + 45 + 54 + 56
+ 65 + 46 + 64 + 456 + 465
+ 546 + 564 + 645 + 654 = 3675

Your Task:
You don't need to read input or print anything. Complete the function getSum() which takes X, Y and Z as input parameters and returns the integer value

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/number-formation3506/1
πŸ”₯4πŸ‘3
Must Do DSA Topics day 14

Matrix

Rotate a Matrix by 180 degree : Given a square matrix, the task is that we turn it by 180 degrees in an anti-clockwise direction without using any extra space.
Learn more here : https://bit.ly/3DbeBTa

Rotate each ring of matrix anticlockwise by K elements : Given a matrix of order M*N and a value K, the task is to rotate each ring of the matrix anticlockwise by K elements. If in any ring elements are less than and equal K then don’t rotate it.
Learn more here : https://bit.ly/3EXCXRq

Turn an image by 90 degree : Given an image, how will you turn it by 90 degrees? A vague question. Minimize the browser and try your solution before going further. An image can be treated as 2D matrix which can be stored in a buffer. We are provided with matrix dimensions and it’s base address.
Learn more here : https://bit.ly/3COqddo
❀4πŸ‘2
Must Do DSA Topics day 15
Matrix

Check if all rows of a matrix are circular rotations of each other : Given a matrix of n*n size, the task is to find whether all rows are circular rotations of each other or not.
Learn more here : https://bit.ly/3eQhwHv


Sort the given matrix : Given a n x n matrix. The problem is to sort the given matrix in strict order. Here strict order means that the matrix is sorted in a way such that all elements in a row are sorted in increasing order and for row β€˜i’, where 1 <= i <= n-1, the first element of row β€˜i’ is greater than or equal to the last element of row β€˜i-1’.
Learn more here : https://bit.ly/3sfdNpX


Find the row with maximum number of 1s : Given a boolean 2D array, where each row is sorted. Find the row with the maximum number of 1s.
Example:
Input matrix
0 1 1 1
0 0 1 1
1 1 1 1 // this row has maximum 1s
0 0 0 0
Output: 2
Learn more here : https://bit.ly/3TI0YjS
πŸ‘3
Problem Of The Day
Filling Bucket
Solve the problem to win points
Given a Bucket having a capacity of N litres and the task is to determine that by how many ways you can fill it using two bottles of capacity of 1 Litre and 2 Litre only. Find the answer modulo 108.

Example 1:

Input:
3
Output:
3
Explanation:
Let O denote filling by 1 litre bottle and
T denote filling by 2 litre bottle.
So for N = 3, we have :
{OOO,TO,OT}. Thus there are 3 total ways.

Your Task:
You don't need to read input or print anything. Your task is to complete the function fillingBucket() which takes an Integer N as input and returns the number of total ways possible.

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

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/filling-bucket0529/1
πŸ‘4
Must Do DSA Topics, day 16
Matrix

Find median in row wise sorted matrix :
We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.

Examples:
Input: 1 3 5
2 6 9
3 6 9
Output: Median is 5
If we put all the values in a sorted
array A[] = 1 2 3 3 5 6 6 9 9)

Input: 1 3 4
2 5 6
7 8 9
Output: Median is 5
Learn more here : https://bit.ly/3VY4YP1

Matrix Multiplication | Recursive :
Given two matrices A and B. The task is to multiply matrix A and matrix B recursively. If matrix A and matrix B are not multiplicative compatible, then generate output β€œNot Possible”.
Learn more : https://bit.ly/3VY2EYa

Program to multiply two matrices :
Given two matrices, the task is to multiply them. Matrices can either be square or rectangular.
Learn more here : https://bit.ly/3gG47Cg
πŸ‘8
Problem of the day
"The Smurfs"

Solve the problem to win points

A geek once visited a magical island where he found a special creature. He named it as Smurf. He noticed a very strange thing there. The smurfs resembled the primary colors of light. To make it more clear, the primary colors of light are Red(R), Green(G), and Blue(B). He talked to one of the smurfs. The smurfs came to know that he is a good programmer. The smurfs suggested a deal that they will ask him one question and if he is able to answer that question, they will allow the geek to take anything from the island.
The question is as follows:
The smurfs possess a very magical property. When two smurfs of different colors meet with other, they gets converted into a smurf of the third color. How many minimum number of smurfs will be remaining after this transformation? The question looked simple to geek. However, the smurfs put another constraint to make the geek think more. The two smurfs must be adjacent to each other to make the transformation take place. There are n smurfs the colors of which are given in an array a[].

Example 1:

Input: n = 5
a = {'R' , 'G', 'B', 'R', 'B'}
Output: 1
Explaination: First 'R' and 'G' makes 'B'.
Then 'B' and 'R' makes 'G' and that 'G'
with 'B' at index 2 makes 'R', Now the 'R'
and the last 'B' makes 'G'.
Example 2:

Input: n = 2
a = {'R', 'R'}
Output: 2
Explaination: There are two 'R' s. So
they cannot change to anything else.
Your Task:
You do not need to read input or print anything. Your task is to complete the function findMin() which takes n and a as input parameters and retunrs the minimum number of smurfs existing at the end.

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

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/the-smurfs4201/1
πŸ‘4
Problem Of The Day
"Print leaf nodes from preorder traversal of BST"

Solve the problem to win points

Example 1:

Input:
N = 2
arr = {2,1}
Output: {1}
Explaination: 1 is the only leaf node.

Example 2:

Input:
N = 3
Arr = {3, 2, 4}
Output: {2, 4}
Explaination: 2, 4 are the leaf nodes.

Your Task:
You don't need to read input or print anything. Your task is to complete the function leafNodes() which takes the array arr[] and its size N as input parameters and returns the leaf nodes of the tree.


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

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/print-leaf-nodes-from-preorder-traversal-of-bst2657/1
πŸ‘3πŸ‘1
Must Do DSA Topics day 17
Hashing

Introduction : Hashing is a popular technique for storing and retrieving data as fast as possible.Hashing gives optimal results as it performs optimal searches.
Learn more : https://bit.ly/3sAvsZm

Index Mapping (or Trivial Hashing) with negatives allowed : Given a limited range array contains both positive and non-positive numbers, i.e., elements are in the range from -MAX to +MAX. Our task is to search if some number is present in the array or not in O(1) time.
Since range is limited, we can use index mapping (or trivial hashing). We use values as the index in a big array. Therefore we can search and insert elements in O(1) time.
Learn more : https://bit.ly/3N9FJVO

Separate Chaining Collision Handling Technique in Hashing : The idea behind separate chaining is to implement the array as a linked list called a chain. Separate chaining is one of the most popular and commonly used techniques in order to handle collisions.
Learn more : https://bit.ly/3FlpVgQ
πŸ‘5
Problem Of The Day
"Sequence Fun"

Solve the problem to win points

You have a sequence 2,5,16,65,........Given an integer n as input . You have to find the value at nth position in the sequence.


Example 1:

Input: n = 4
Output: 65
Example 2:

Input: n = 10
Output: 9864101


Your Task:

You don't need to read or print anything, Your task is to complete the function NthTerm() which takes n as input parameter and retruns value at nth position of the given sequence modulo 109 + 7.


Expected Time Compelxity:  O(n)
Expected Space Complexity: O(1)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/sequence-fun5018/1
πŸ‘6❀2
Problem Of The Day
"Phone directory"

Solve the problem to win points

Given a list of contacts contact[] of length n where each contact is a string which exist in a phone directory and a query string s. The task is to implement a search query for the phone directory. Run a search query for each prefix p of the query string s (i.e. from index 1 to |s|) that prints all the distinct contacts which have the same prefix as p in lexicographical increasing order. Please refer the explanation part for better understanding.
Note: If there is no match between query and contacts, print "0".

Example 1:

Input:
n = 3
contact[] = {"geeikistest", "geeksforgeeks",
"geeksfortest"}
s = "geeips"
Output:
geeikistest geeksforgeeks geeksfortest
geeikistest geeksforgeeks geeksfortest
geeikistest geeksforgeeks geeksfortest
geeikistest
0
0
Explaination: By running the search query on
contact list for "g" we get: "geeikistest",
"geeksforgeeks" and "geeksfortest".
By running the search query on contact list
for "ge" we get: "geeikistest" "geeksforgeeks"
and "geeksfortest".
By running the search query on contact list
for "gee" we get: "geeikistest" "geeksforgeeks"
and "geeksfortest".
By running the search query on contact list
for "geei" we get: "geeikistest".
No results found for "geeip", so print "0".
No results found for "geeips", so print "0".
Your Task:
Youd do not need to read input or print anything. Your task is to complete the function displayContacts() which takes n, contact[ ] and s as input parameters and returns a list of list of strings for required prefixes. If some prefix has no matching contact return "0" on that list.

Expected Time Complexity: O(|s| * n * max|contact[i]|)
Expected Auxiliary Space: O(n * max|contact[i]|)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/phone-directory4628/1
πŸ”₯6πŸ‘4πŸ₯°2
Problem Of The Day
"Satisfy the equation"

Solve the problem to win points

Given an array A[ ] of N of integers, find the index of values that satisfy A + B = C + D where A,B,C & D are integers values in the array.
Note: As there may be multiple pairs satisfying the equation return lexicographically smallest order of A, B, C and D and return all as -1 if there are no pairs satisfying the equation.

Example 1:

Input:
N = 7
A[] = {3, 4, 7, 1, 2, 9, 8}
Output:
0 2 3 5
Explanation:
A[0] + A[2] = 3+7 = 10
A[3] + A[5] = 1+9 = 10
Example 2:

Input:
N = 4
A[] = {424, 12, 31, 7}
Output:
-1 -1 -1 -1
Explanation:
There are no pairs satisfying the equation.


Your Task:
You don't need to read input or print anything. Your task is to complete the function satisfyEqn() which takes an Integer N and an array A[] of size N as input and returns a vector of 4 integers denoting A, B, C, and D respectively.

Expected Time Complexity: O(N2*logN2)
Expected Auxiliary Space: O(N2)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/satisfy-the-equation5847/1
πŸ‘3
Problem Of The Day
"Median in a row-wise sorted Matrix"

Solve the problem to win points

Given a row wise sorted matrix of size R*C where R and C are always odd, find the median of the matrix.

Example 1:

Input:
R = 3, C = 3
M = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output: 5
Explanation: Sorting matrix elements gives
us {1,2,3,3,5,6,6,9,9}. Hence, 5 is median.


Example 2:

Input:
R = 3, C = 1
M = [[1], [2], [3]]
Output: 2
Explanation: Sorting matrix elements gives
us {1,2,3}. Hence, 2 is median.

Your Task:
You don't need to read input or print anything. Your task is to complete the function median() which takes the integers R and C along with the 2D matrix as input parameters and returns the median of the matrix.

Expected Time Complexity: O(32 * R * log(C))
Expected Auxiliary Space: O(1)

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/median-in-a-row-wise-sorted-matrix1527/1
πŸ‘4πŸ”₯2
Must Do DSA Topics day 18
Hashing

Open Addressing:
Like separate chaining, open addressing is a method for handling collisions. In Open Addressing, all elements are stored in the hash table itself. So at any point, the size of the table must be greater than or equal to the total number of keys.
Learn more here : https://bit.ly/3TTV4g1

Double Hashing : Double hashing is a collision resolving technique in Open Addressed Hash tables. Double hashing uses the idea of applying a second hash function to key when a collision occurs.
Learn more here : https://bit.ly/3NnC72y

Load Factor and Rehashing : Rehashing means hashing again. When the load factor increases to more than its pre-defined value (default value of load factor is 0.75), the complexity increases. The size of the array is increased (doubled) and all the values are hashed again and stored in the new double sized array to maintain a low load factor and low complexity.
Learn more here : https://bit.ly/3SRYtub
πŸ‘3
Problem Of The Day
"Enemy"

Solve the problem to win points

You live in Geek land. Geek land can be seen as a grid of shape N x M.Their are K enemy at K positions. Each enemy blocks the row and column to which it belongs. You have to find the largest continuous area that is not blocked.No two enemies share the same row or the same column.

Example 1:

Input:
N = 2
M = 2
K = 1
enemy[]={{2,2}}
Output:
1
Explanation:
Since only (1,1) cell is free from the enemy hence answer is 1.


Example 2:

Input:
N = 3
M = 3
K = 1
enemy[]={{3,3}}
Output:
4
Explanation:
The cells (1,1),(1,2) ,(2,1) and (2,2) are free hence answer =4.


Your Task:
You don't need to read input or print anything. Your task is to complete the function largestArea() which takes the size of geek land n,m and a 2-D matrix enemy of size k denoting the coordinates of the enemy's and need to return the largest area that is free.

Expected Time Complexity: O(KlogK)
Expected Auxiliary Space: O(K)

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/enemy/1
πŸ‘1