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 of Subarrays"

Solve the problem to win points

Given an array of N positive integers Arr1, Arr2 ............ Arrn. The value of each contiguous subarray of given array is the maximum element present in that subarray. The task is to return the number of subarrays having value strictly greater than K.

Example:
Input:
N = 3, K = 2
Arr[] = {3, 2, 1}
Output: 3

Explanation: The subarrays having value
strictly greater than K are: [3], [3, 2]
and [3, 2, 1]. Thus there are 3 such
subarrays.

Your Task:
Complete the function countSubarray() which takes an array arr, two integers n, k, as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs.

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

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/count-of-subarrays5922/1
πŸ‘3
Must Do DSA Topics day 28
Linked Lists

Deletion in Linked List : We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.
Let us formulate the problem statement to understand the deletion process.
Learn more : http://bit.ly/3GkLZc9

Delete a Linked List node at a given position : Given a singly linked list and a position, delete a linked list node at the given position.
Example: Input: position = 1, Linked List = 8->2->3->1->7
Output: Linked List = 8->3->1->7

Input: position = 0, Linked List = 8->2->3->1->7
Output: Linked List = 2->3->1->7
Learn more : http://bit.ly/3Xb9Ctp

Write a function to delete a Linked List : Algorithm For C/C++: Iterate through the linked list and delete all the nodes one by one. The main point here is not to access the next of the current pointer if the current pointer is deleted.
Learn more : http://bit.ly/3hOzrPU
πŸ‘5
Problem Of The Day
"Number Of Open Doors"

Solve the problem to win points

Consider a long alley with a N number of doors on one side. All the doors are closed initially. You move to and fro in the alley changing the states of the doors as follows: you open a door that is already closed and you close a door that is already opened. You start at one end go on altering the state of the doors till you reach the other end and then you come back and start altering the states of the doors again.
In the first go, you alter the states of doors numbered 1, 2, 3, , n.
In the second go, you alter the states of doors numbered 2, 4, 6
In the third go, you alter the states of doors numbered 3, 6, 9
You continue this till the Nth go in which you alter the state of the door numbered N.
You have to find the number of open doors at the end of the procedure.
 
Example :
Input: N = 2 Output: 1 Explanation:
Initially all doors are closed. After 1st go, all doors will be opened. After 2nd go second door will be closed. So, Only 1st door will remain Open.


Your Task:
You don't need to read input or print anything. Your task is to complete the function noOfOpenDoors() which takes an Integer N as input and returns the answer.

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

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/number-of-open-doors1552/1
πŸ‘2
Problem Of The Day

"Unique partitions"
Solve the problem to win points

Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers.

Note: The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order.

Example :

Input: n = 3
Output: 3 2 1 1 1 1
Explanation: For n = 3,
{3}, {2, 1} and {1, 1, 1}.

Your Task:
You don't need to read or print anything. Your task is to complete the function UniquePartitions() which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order.


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

Solve the problem and submit your answers here: https://practice.geeksforgeeks.org/problems/unique-partitions1041/1
Must Do DSA Topics day 29
Linked Lists

Find Length of a Linked List (Iterative and Recursive) : Write a function to count the number of nodes in a given singly linked list

Examples:
Input: 2->4->1->9->5->3->6
Output: 7
Learn more : http://bit.ly/3VgKHDh

Search an element in a Linked List (Iterative and Recursive) : Given a linked list and a key β€˜Xβ€˜ in, the task is to check if X is present in the linked list or not.

Examples:
Input: 14->21->11->30->10, X = 14
Output: Yes
Explanation: 14 is present in the linked list.
Learn more : http://bit.ly/3OkJmch

Write a function to get Nth node in a Linked List : Write a GetNth() function that takes a linked list and an integer index and returns the data value stored in the node at that index position.
Learn more : http://bit.ly/3gmWOzH
Must Do DSA Topics day 30
Linked Lists

Program for Nth node from the end of a Linked List : Given a Linked List and a number N, write a function that returns the value at the Nth node from the end of the Linked List.
Learn more : http://bit.ly/3i13Ev6

Find the middle of a given linked list : Given a singly linked list, find the middle of the linked list. For example, if the given linked list is 1->2->3->4->5 then the output should be 3.
If there are even nodes, then there would be two middle nodes, we need to print the second middle element. For example, if the given linked list is 1->2->3->4->5->6 then the output should be 4.
Learn more : http://bit.ly/3gkj4u0

Write a function that counts the number of times a given int occurs in a Linked List : Given a singly linked list and a key, count the number of occurrences of the given key in the linked list. For example, if the given linked list is 1->2->1->2->1->3->1 and the given key is 1, then the output should be 4.
Learn more : http://bit.ly/3AsAo6P
πŸ‘4
Problem Of The Day
"Magic Triplets"

Solve the problem to win points

Given an array of size n, a triplet (a[i], a[j], a[k]) is called a Magic Triplet if a[i] < a[j] < a[k] and i < j < k. Count the number of magic triplets in a given array.

Example 1:
Input: arr = [3, 2, 1]
Output: 0

Explanation: There is no magic triplet.

Example 2:
Input: arr = [1, 2, 3, 4]
Output: 4

Explanation: Fours magic triplets are
(1, 2, 3), (1, 2, 4), (1, 3, 4) and
(2, 3, 4).


Your Task:
You don't need to read or print anything. Your task is to complete the function countTriplets() which takes the array nums[] as input parameter and returns the number of magic triplets in the array.

Expected Time Complexity: O(N2)
Expected Space Complexity: O(1)

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/magic-triplets4003/1
πŸ₯°2
Must Do DSA Topics day 31
Linked Lists

Introduction to Circular Linked List :
What is Circular linked list?
The circular linked list is a linked list where all nodes are connected to form a circle. In a circular linked list, the first node and the last node are connected to each other which forms a circle. There is no NULL at the end.
Learn more :http://bit.ly/3EPGEZh

Traversal of Circular Linked List : In a conventional linked list, we traverse the list from the head node and stop the traversal when we reach NULL. In a circular linked list, we stop traversal when we reach the first node again.
Learn more : http://bit.ly/3gn4GRP

Split a Circular Linked List into two halves : 1) Store the mid and last pointers of the circular linked list using tortoise and hare algorithm.
2) Make the second half circular.
3) Make the first half circular.
4) Set head (or start) pointers of the two linked lists.
Learn more : http://bit.ly/3GyNBPE
πŸ‘1
Must Do DSA Topics day 32
Stack
Introduction to Stack : It is a linear data structure that follows a particular order in which the operations are performed.
Learn more : http://bit.ly/3OvOz14

Stack in C++ STL : Stacks are a type of container adaptors with LIFO(Last In First Out) type of working, where a new element is added at one end (top) and an element is removed from that end only. Stack uses an encapsulated object of either vector or deque (by default) or list (sequential container class) as its underlying container, providing a specific set of member functions to access its elements.
Learn more : http://bit.ly/3Xn777C

Stack Class in Java : Java Collection framework provides a Stack class that models and implements a Stack data structure. The class is based on the basic principle of last-in-first-out. In addition to the basic push and pop operations, the class provides three more functions of empty, search, and peek.
Learn more : http://bit.ly/3gnDq5R
πŸ‘1
Problem Of The Day
"Maximum Sum LCM"

Solve the problem to win points

Given a positive number n. You need to write a program to find the maximum sum of distinct numbers such that the LCM of all these numbers is equal to n.

Example 1:
Input: n = 2
Output: 3
Explanation: The distinct numbers you can have are
just 1 and 2 and their sum is equal to 3.

Example 2:
Input: n = 5
Output: 6
Explanation: The distinct numbers you can have
are just 1 and 5 and their sum is equal to 6.

Your Task:
You dont need to read input or print anything. Complete the function maxSumLCM() which takes n as input parameter and returns the maximum sum of distinct numbers such that the LCM of all these numbers is equal to n.

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

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/maximum-sum-lcm3025/1
Must Do DSA Topics day 33
Stack
Stack in Python : A stack is a linear data structure that stores items in a Last-In/First-Out (LIFO) or First-In/Last-Out (FILO) manner. In stack, a new element is added at one end and an element is removed from that end only. The insert and delete operations are often called push and pop.
Learn more : http://bit.ly/3tUgG09

C# Stack : A Stack represents a last-in, first-out collection of objects. It is used when you need last-in, first-out access to items. It is both a generic and non-generic type of collection. The generic stack is defined in System.Collections.Generic namespace whereas non-generic stack is defined under System.
Learn more : http://bit.ly/3EVGa3H

Queue using Stacks : The problem is opposite of this post. 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/3Vo2Qiz
Problem Of The Day
"Longest Bitonic subsequence"

Solve the problem to win points

Given an array of positive integers. Find the maximum length of Bitonic subsequence.
A subsequence of array is called Bitonic if it is first strictly increasing, then strictly decreasing.


Example :
Input: nums = [1, 2, 5, 3, 2]
Output: 5
Explanation: The sequence {1, 2, 5} is
increasing and the sequence {3, 2} is
decreasing so merging both we will get
length 5.

Example :
Input: nums = [1, 11, 2, 10, 4, 5, 2, 1]
Output: 6
Explanation: The bitonic sequence
{1, 2, 10, 4, 2, 1} has length 6.


Your Task:
You don't need to read or print anything. Your task is to complete the function LongestBitonicSequence() which takes the array nums[] as input parameter and returns the maximum length of bitonic subsequence.

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

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/longest-bitonic-subsequence0824/1
πŸ‘2
Must Do DSA Topics day 34
Stack
Design and Implement Special Stack Data Structure | Added Space Optimized Version: Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack.
Learn more: http://bit.ly/3XrfrTP

Implement two Stacks in an Array: Create a data structure twoStacks that represent two stacks. Implementation of two stacks should use only one array, i.e., both stacks should use the same array for storing elements.
Learn more: http://bit.ly/3gtyAUH

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: http://bit.ly/3V2ay24
πŸ‘1
Problem Of The Day
β€œShreyansh and his bits”

Solve the problem to win points

Shreyansh has an integer N. He is really curious about the binary representation of integers. He sees that any given integer has a number of set bits. Now he wants to find out that how many positive integers, strictly less than N, have the same number of set bits as N.
He is a little weak in maths. Help him find the number of integers.
Note : Since N takes large values, brute force won't work.


Example 1:
Input:
N = 8
Output: 3
Explanation:
Binary representation of 8 : 1000
So the integers less than 8 with
same number of set bits are : 4, 2, 1
Hence, the result is 3.


Example 2:
Input:
N = 4
Output: 2
Explanation:
Binary representation of 4 : 100
So the integers less than 4 with
same number of set bits are : 2, 1
So, The result is 2.

Your Task:
You don't need to read input or print anything. Your task is to complete the function count() which takes the integer N as input parameters and returns the required answer.

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

Solve the problem and submit your answers here :
https://practice.geeksforgeeks.org/problems/shreyansh-and-his-bits1420/1
Must Do DSA Topics day 35
Stack
Design a stack with operations on middle element : Please note that we need to find and delete the middle element. Deleting an element from the middle is not O(1) for the array. Also, we may need to move the middle pointer up when we push an element and move down when we pop().
Learn more : http://bit.ly/3EXrgKi

How to efficiently implement k stacks in a single array : Create a data structure kStacks that represents k stacks. Implementation of kStacks should use only one array, i.e., k stacks should use the same array for storing elements.
Learn more : http://bit.ly/3OxgtK0

How to create mergeable stack : We can use a linked list with two pointers, one pointer to the first node (also used as a top when elements are added and removed from the beginning). The other pointer is needed for the last node so that we can quickly link the linked list of s2 at the end of s1.
Learn more : http://bit.ly/3Vp1nZ9
πŸ‘1
Black Friday Sale is LiveπŸ“’πŸ“’

Go big on savings with 25% on all courses plus assured cashbacks!
Grab all you can, till Saturday midnight!✨✨

Click on the following link-
https://practice.geeksforgeeks.org/courses?utm_source=telegram&utm_medium=marketingteam_gfgmain&utm_campaign=blackfriday22
Problem Of The Day
"Construct Binary Tree from String with bracket representation"

Solve the problem to win points

Construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the roots value and a pair of parenthesis contains a child binary tree with the same structure. Always start to construct the left child node of the parent first if it exists.


Example 1:
Input: "1(2)(3)"
Output: 2 1 3
Explanation:
1
/ \
2 3
Explanation: first pair of parenthesis contains
left subtree and second one contains the right
subtree. Inorder of above tree is "2 1 3".


Example 2:
Input: "4(2(3)(1))(6(5))"
Output: 3 2 1 4 5 6
Explanation:
4
/ \
2 6
/ \ /
3 1 5


Your Task:
You don't need to read input or print anything. Your task is to complete the function treeFromString() which takes a string str as input parameter and returns the root node of the tree.


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

Solve the problem and submit your answer here: https://practice.geeksforgeeks.org/problems/construct-binary-tree-from-string-with-bracket-representation/1
Problem Of The Day
β€œAdd Binary Strings”

Solve the problem to win points

Given two binary strings A and B consisting of only 0s and 1s. Find the resultant string after adding the two Binary Strings.
Note: The input strings may contain leading zeros but the output string should not have any leading zeros.


Example 1:
Input:
A = "1101", B = "111"
Output: 10100
Explanation:
 1101
+ 111
10100

Example 2:
Input: 
A = "10", B = "01"
Output: 11
Explanation: 
  10
+ 01
  11

Your Task:
You don't need to read input or print anything. Your task is to complete the function addBinary() which takes 2 binary string A and B and returns a binary string denoting the addition of both the strings.


Expected Time Complexity: O(max(|A|, |B|)).
Expected Auxiliary Space: O(max(|A|, |B|)) (for output string).

Solve the problem and submit your answers here : https://practice.geeksforgeeks.org/problems/add-binary-strings3805/1
πŸ‘3
Problem Of The Day
β€œMaximum Sub Array”

Solve the problem to win points

Find out the maximum sub-array of non negative numbers from an array.

The sub-array should be contiguous i.e., a sub-array created by choosing the second and fourth element and skipping the third element is invalid.

Maximum sub-array is defined in terms of the sum of the elements in the sub-array. Sub-array A is greater than sub-array B if sum(A) > sum(B).

Example:
a = [1, 2, 5, -7, 2, 3]
The two sub-arrays are [1, 2, 5] [2, 3].
The answer is [1, 2, 5] as its sum is larger than [2, 3]

NOTE: If there is a tie, then compare with segment's length and return segment which has maximum length.
If there is still a tie, then return the segment with minimum starting index.
If no such subarray is present return "-1"

Example 1:
Input:
n = 3
a[] = {1, 2, 3}
Output: 1 2 3
Explanation: In the given array every
element is non-negative.

Example 2:
Input:
n = 2
a[] = {-1, 2}
Output: 2
Explanation: The only subarray [2] is
the answer.


Your Task:
Complete the function findSubarray() which takes the array a and the size of the array, n, as input parameters and returns an array representing the answer. If there is no subarray return an array of length 1 containing -1 only. You don't to print answer or take inputs.

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

Solve the problem and submit your answers here :
https://practice.geeksforgeeks.org/problems/maximum-sub-array5443/1
πŸ‘4
Must Do DSA Topics day 36
Stack
Design a stack that supports getMin() in O(1) time and O(1) extra space : Design a Data Structure SpecialStack that supports all the stack operations like push(), pop(), isEmpty(), isFull() and an additional operation getMin() which should return minimum element from the SpecialStack.
Learn more : http://bit.ly/3icRJKR

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

How to implement stack using priority queue or heap : In priority queue, we assign priority to the elements that are being pushed. A stack requires elements to be processed in Last in First Out manner. The idea is to associate a count that determines when it was pushed.
Learn more : http://bit.ly/3gHKV7y
πŸ‘2