Problem Solving Sheet
Problem Solving Sheet
Four Elements
Four Elements
Message decoding
https://practice.geeksforgeeks.org/problems/find-triplets-with-zero-sum/1?page=1&category[]=two-pointer-algorithm&sortB
https://practice.geeksforgeeks.org/problems/reverse-a-string-with-spaces-intact5213/1?page=1&category[]=two-pointer-algo
https://practice.geeksforgeeks.org/problems/reverse-a-string-with-spaces-intact5213/1?page=1&category[]=two-pointer-algo
https://practice.geeksforgeeks.org/problems/binary-array-sorting-1587115620/1?page=1&category[]=two-pointer-algorithm&
https://practice.geeksforgeeks.org/problems/binary-array-sorting-1587115620/1?page=1&category[]=two-pointer-algorithm&
https://practice.geeksforgeeks.org/problems/find-pairs-with-given-sum-in-doubly-linked-list/1?page=2&category[]=two-point
https://practice.geeksforgeeks.org/problems/find-pairs-with-given-sum-in-doubly-linked-list/1?page=2&category[]=two-point
https://practice.geeksforgeeks.org/problems/delete-middle-of-linked-list/1?page=1&category[]=two-pointer-algorithm&sortB
https://practice.geeksforgeeks.org/problems/delete-middle-of-linked-list/1?page=1&category[]=two-pointer-algorithm&sortB
https://practice.geeksforgeeks.org/problems/detect-loop-in-linked-list/1?page=1&category[]=two-pointer-algorithm&sortBy=
https://practice.geeksforgeeks.org/problems/detect-loop-in-linked-list/1?page=1&category[]=two-pointer-algorithm&sortBy=
https://practice.geeksforgeeks.org/problems/d6e88f06b273a60ae83992314ac514f71841a27d/1?page=1&category[]=two-po
https://practice.geeksforgeeks.org/problems/count-the-triplets4615/1?page=1&category[]=two-pointer-algorithm&sortBy=di
https://practice.geeksforgeeks.org/problems/count-the-triplets4615/1?page=1&category[]=two-pointer-algorithm&sortBy=di
https://practice.geeksforgeeks.org/problems/four-elements2452/1?page=1&category[]=two-pointer-algorithm&sortBy=diffic
https://practice.geeksforgeeks.org/problems/four-elements2452/1?page=1&category[]=two-pointer-algorithm&sortBy=diffic
https://practice.geeksforgeeks.org/problems/ishaan-loves-chocolates2156/1?page=1&status[]=unsolved&category[]=two-poi
https://practice.geeksforgeeks.org/problems/sort-the-half-sorted2157/1?page=1&status[]=unsolved&category[]=two-pointer
https://practice.geeksforgeeks.org/problems/message-decoding4305/1?page=1&status[]=unsolved&category[]=two-pointer-
https://practice.geeksforgeeks.org/problems/length-of-longest-subarray0440/1?page=1&status[]=unsolved&category[]=two-
https://practice.geeksforgeeks.org/problems/length-of-longest-subarray0440/1?page=1&status[]=unsolved&category[]=two-
https://practice.geeksforgeeks.org/problems/maximum-sum-of-subarray-less-than-or-equal-to-x4033/1?page=1&status[]=un
https://practice.geeksforgeeks.org/problems/pair-with-given-sum-in-a-sorted-array4940/1?page=1&status[]=unsolved&categ
https://practice.geeksforgeeks.org/problems/pair-with-given-sum-in-a-sorted-array4940/1?page=1&status[]=unsolved&categ
https://practice.geeksforgeeks.org/problems/special-array-reversal2328/1?page=1&status[]=unsolved&category[]=two-point
https://practice.geeksforgeeks.org/problems/triplet-sum-in-array-1587115621/1?page=1&status[]=unsolved&category[]=two
https://practice.geeksforgeeks.org/problems/triplet-sum-in-array-1587115621/1?page=1&status[]=unsolved&category[]=two
https://practice.geeksforgeeks.org/problems/number-of-subarrays-with-maximum-values-in-given-range5949/1?page=1&sta
Solution
Use hashmap and fill all the elements and then, get all the duplets summations and check if -c is present
in that hashmap, where equation is a+b-c
Use for loop for external iteration of i, intialize j and k as i+1 and n-1 respectively and check if sum is
greater or lesser and update k and j accordingly
declare i and j at 0 and n-1 respectively, when there is space i++ or j--, if not space swap the elements,
and increase and decrease i and j respectively
declare a new string add all the characters without spaces along with array with index of spaces, then
form a result string and iterate reversly, check for the index of spaces and add spaces accordingly
declare i and j at 0 and n-1, swap if i is 1 and j =0, swap and i++,j----- if both are zero then i++ and if j=1
and i=0 then j--
count total zeros in array and add all the zeros till count, now start with count till n and add all ones
declare start and tail node at head and tail for linkedlist, iterate through linkedlist, if sum is less tail =
tail.prev, if greater change the head accordingly
declare hashmap and store all the values in it, find if i-target is present in the linkedlist by iterating it
Get the length of the linkedlist and find the middle element of list. Iterate till the middle of the list and
to prev.next = middle.next.next
Make a fast and slow pointers nodes. Fast=fast.next.next and slow=slow.next, find the prev and delete
the element when fast is at end, slow will be at middle
Make a fast and slow pointers node. Fast=fast.next.next and slow=slow.next, when slow==fast break the
loop, when fast and slow pointers meet at the location that is the starting point of the loop
Use hashset, if node is not present, fill the hashset, if present break it.
declare three pointers to 0 and start to calculate difference min and max between all the pointers and
find minimum from diffrenece from them, lastly we need to add one.
put one pointer at 0 index and two other pointers at last and second last index. Run the loop till j<k
where k = i-1 and j =0, if summation of them is i break, if i>sum then increase j and if less than decrease
k;
use hashmap and fill all the elements then sum of duplets must contains in the hashmap then we found
the triplet
use hashmap and store all the elements (X-A) where a+b+c+d = X, and then check all the elements if
a+b+c contains in the hashmap
fix first two elements and find other elements by creating two pointers, here fixing first two elements
mean writing the two nested for loops, writing while loop inside the nested loops, increasing and
decreasing pointer accroding to sum
At last, only chocolate with less tastiness level will remail at last, so minimum in the array will remain, in
two pointers approach decrease or increase the pointer which is bigger
find the breaking point where array is divided and store least element to the new array.
declare a string of hello, and find it every character is equal and increase the counter if equal, if counter
is 5 then return true
run a for loop and count when number is positive, reset the count when number is negative and find the
max count
Declare i and j at 0th index, increase j till number is negative, subtract to find the length, and equate the i
and j at negative position,
declare i and j at 0th index and increase j till sum exceeds X, when it does subtract element is ith position
and increase i then calculate the max element.
declare two pointer at beginning and end of the array and count if pair is equal to the sum
store all the elements in the array of hashmap and find if sum-i is present in the hashmap
declare two pointer at 0 and n-1 swap when both are not special character otherwise when special
increase and decrease j respectively,
similar question like count triplets problem at a15 cell, have three pointers first at I, j and k at second
last and last element respectively, in loop fix one pointer at last element. And write while loop for i and j
use hashmap to store all the elements and try to find X-(a+b) in the hashmap, if exists then true
initialize two pointers at 0th positiion and increase j and and run while loop till j<n, when in range
window size is j-i+1 when greater than range than reset the window size and i=j+1; and start again.
Difficuilty
Basic
Basic
Basic
Basic
Basic
Basic
Easy
Easy
Easy
Easy
Easy
Easy
Easy
Easy
Easy
Easy
Easy
Basic
Basic
Basic
Easy
Easy
Easy
Easy
Easy
Easy
Medium
Medium
Medium
Title
https://practice.geeksforgeeks.org/problems/max-sum-subarray-of-size-k5313/1?page=1&status[]=unsolved&category[]=slidi
https://practice.geeksforgeeks.org/problems/subarray-with-given-sum-1587115621/1?page=1&status[]=unsolved&category[
https://practice.geeksforgeeks.org/problems/count-distinct-elements-in-every-window/1?page=1&status[]=unsolved&catego
https://practice.geeksforgeeks.org/problems/subarray-with-0-sum-1587115621/1?page=1&status[]=unsolved&category[]=slid
Solution Difficuilty
declare two pointer at starting position and increase j when window is smaller and when
window becomes the K fetch the maximum sum, do i++ when and sum-=a[i] when
moving window forward. Basic
declare two pointers at 1 because it is one based indexing, increase second pointer till
along with that do the sum, now, subtract the first element till sum is less than given Sum
S, and check for maximum sum meanwhile Easy
declare two pointer at starting position and increase j and along with that add element to
the hashmap with frequency, if window size is k then count the size of the hashmap, if
freq is 1 then remove it it greater than one then subtract it Easy
sum all the elements and put it into the set, if it contains the sum then return true, the
reason behind this logic is if we encounter same sum twice then there must me minus
value present in the array that subract the whole sum Easy
Title
Preorder Traversal
Mirror Tree
Link
https://practice.geeksforgeeks.org/problems/preorder-traversal/1?page=1&difficulty[]=-1&category[]=Tree&sortBy=difficulty
https://practice.geeksforgeeks.org/problems/preorder-traversal-iterative/1
https://practice.geeksforgeeks.org/problems/height-of-heap5025/1?page=1&difficulty[]=-1&category[]=Tree&sortBy=difficult
https://practice.geeksforgeeks.org/problems/size-of-binary-tree/1?page=1&difficulty[]=-1&category[]=Tree&sortBy=difficulty
https://practice.geeksforgeeks.org/problems/postorder-traversal/1?page=1&difficulty[]=-1&category[]=Tree&sortBy=difficult
https://practice.geeksforgeeks.org/problems/inorder-traversal/1?page=1&difficulty[]=-1&category[]=Tree&sortBy=difficulty
https://practice.geeksforgeeks.org/problems/minimum-element-in-bst/1?page=1&difficulty[]=-1&status[]=unsolved&categor
https://practice.geeksforgeeks.org/problems/sum-of-binary-tree/1?page=1&difficulty[]=-1&status[]=unsolved&category[]=Tre
https://practice.geeksforgeeks.org/problems/count-non-leaf-nodes-in-tree/1?page=1&difficulty[]=-1&status[]=unsolved&cate
https://practice.geeksforgeeks.org/problems/maximum-gcd-of-siblings-of-a-binary-tree/1?page=1&difficulty[]=0&curated[]=6
https://practice.geeksforgeeks.org/problems/check-for-bst/1?page=1&difficulty[]=0&curated[]=6&sortBy=difficulty
https://practice.geeksforgeeks.org/problems/mirror-tree/1?page=1&difficulty[]=0&curated[]=6&sortBy=difficulty
Solution
In preorder first print the root node and then print left and right node, so we will write a recursive
function that prints the node first and checks if left or right are not null, if they are not null then again it
will consider current node as the root node and procees further
Idea is easy, declare a stack, and push the root to the stack, while stack is not empty, print the first node
and pop the first node to the stack, push right and left node of that popped node
Height of the heap is totalNumberofLevels-1, if we see the pattern so it is 1,2,4,8,16,32, i=i*2
Iterative preorder, and count the elements
print the left node then right and lastly print the root node,
print the left node then root and then right node
last left node data will be the minimum data. Because in binary tree left is lower than root and right is
bigger than root, is left==null break, that is the base case of the scenario
use preorder or any order traversal and keep adding the elements
use preorder or any order traversal and check is left or right is not null
get the gcd function, sort the 2d arraylist and then only check max gcd when two first nodes are same
otherwise don’t , for the gcd function, get ma min value and run the while loop till min is not zero and
check is n1 and n2 both divides the min number
if this is the bst then inorder traversal will provide the sorted answer, fill all the elements with the array
and check that is sorted or not
Use stack, fill the first node and then run stack till stack is empty, get the current node and swap the
nodes then pop from the stack. Push into the stack whether right or left if they are not null
Difficuilty
Basic
Medium
Basic
Basic
Basic
Basic
Basic
Basic
Basic
Easy
Easy
Easy