├── 2023 ├── 05-2023(may) │ ├── 15may.cpp │ ├── 16may.cpp │ ├── 17may.cpp │ ├── 18may.cpp │ ├── 19may.cpp │ ├── 20may.cpp │ ├── 21may.cpp │ ├── 22may.cpp │ ├── 23may.cpp │ ├── 24may.cpp │ ├── 28may.cpp │ ├── 29may.cpp │ ├── 30may.cpp │ └── 31may.cpp ├── 06-2023(june) │ ├── 01 - Topological sort.md │ ├── 01june.cpp │ ├── 02 - Santa Banta.md │ ├── 02june.cpp │ ├── 03 - Find Maximum Equal sum of Three Stacks.md │ ├── 03june.cpp │ ├── 04 - Reversing the Equation.md │ ├── 04june.cpp │ ├── 05 - Find the Closest Element in BST.md │ ├── 05june.cpp │ ├── 06 - Predecessor and Successor.md │ ├── 07 - Least Prime Factor.md │ ├── 07june.cpp │ ├── 08 - Finding the Kth Permutation.md │ ├── 08june.cpp │ ├── 09 - Permutations of a given string.md │ ├── 09june.cpp │ ├── 10 - Arranging the Array.md │ ├── 10june.cpp │ ├── 11 - Adding Ones.md │ ├── 11june.cpp │ ├── 12 - Rod Cutting.md │ ├── 12june.cpp │ ├── 13 - K Largest Elements.md │ ├── 13june.cpp │ ├── 14 - Maximum Diamonds.md │ ├── 14june.cpp │ ├── 15 - Longest Palindrome in a String.md │ ├── 15june.cpp │ ├── 16 - Minimum Time.md │ ├── 16june.cpp │ ├── 17 - Queue Operations.md │ ├── 17june.cpp │ ├── 18 - Ticket Counter.md │ ├── 18june.cpp │ ├── 19 - Rearrange an array with O(1) extra space.md │ ├── 19june.cpp │ ├── 20 - Matchsticks Game.md │ ├── 20june.cpp │ ├── 21 - Reverse Coding.md │ ├── 21june.cpp │ ├── 22 - Lemonade Change.md │ ├── 22june.cpp │ ├── 23 - Task Scheduler.md │ ├── 23june.cpp │ ├── 24 - Prefix match with other strings.md │ ├── 24june.cpp │ ├── 25 - Unique rows in boolean matrix.md │ ├── 25june.cpp │ ├── 26 - nCr.md │ ├── 26june.cpp │ ├── 27 - Union of Two Linked Lists.md │ ├── 27june.cpp │ ├── 28 - Maximum Depth Of Binary Tree.md │ ├── 28june.cpp │ ├── 29 - Next Happy Number.md │ ├── 29june.cpp │ ├── 30 - Is Binary Number Multiple of 3.md │ ├── 30june.cpp │ └── readme.md ├── 07-2023(july) │ ├── 01 - Number of 1 Bits.md │ ├── 01-july.cpp │ ├── 02 - Copy Set Bits in Range.md │ ├── 02-july.cpp │ ├── 03 - Maximum Index.md │ ├── 03-july.cpp │ ├── 04 - Count the subarrays having product less than k.md │ ├── 04-july.cpp │ ├── 05 - Stock buy and sell II.md │ ├── 05-july.cpp │ ├── 06 - Quick Sort.md │ ├── 06-july.cpp │ ├── 07 - Merge Without Extra Space.md │ ├── 07-july.cpp │ ├── 08 - Find triplets with zero sum.md │ ├── 08-july.cpp │ ├── 09 - Smallest Positive missing number.md │ ├── 09-july.cpp │ ├── 10 - Transpose of Matrix.md │ ├── 10-july.cpp │ ├── 11 - Find kth element of spiral matrix.md │ ├── 11-july.cpp │ ├── 12 - Power Of Numbers.md │ ├── 12-july.cpp │ ├── 13 - Unique Number of Occurrences.md │ ├── 13-july.cpp │ ├── 14 - Implement two stacks in an array.md │ ├── 14-july.cpp │ ├── 15 - Delete middle element of a stack.md │ ├── 15-july.cpp │ ├── 16 - Queue Reversal.md │ ├── 16-july.cpp │ ├── 17 - First non-repeating character in a stream.md │ ├── 17-july.cpp │ ├── 18 - Longest Repeating Subsequence.md │ ├── 18-july.cpp │ ├── 19 - Longest Palindromic Subsequence.md │ ├── 19-july.cpp │ ├── 20 - Non Repeating Character.md │ ├── 20-july.cpp │ ├── 21 - Reverse a Linked List in groups of given size.md │ ├── 21-july.cpp │ ├── 22 - Remove duplicates from an unsorted linked list.md │ ├── 22-july.cpp │ ├── 23 - Given a linked list of 0s, 1s and 2s, sort it.md │ ├── 23 -july.cpp │ ├── 24 - Right View of Binary Tree.md │ ├── 24-july.cpp │ ├── 25 - Level order traversal in spiral form.md │ ├── 25-july.cpp │ ├── 26 - Kth Ancestor in a Tree.md │ ├── 26-july.cpp │ ├── 27 - Heap Sort.md │ ├── 27-july.cpp │ ├── 28 - Lowest Common Ancestor in a BST.md │ ├── 28-july.cpp │ ├── 29 - Median of BST.md │ ├── 29-july.cpp │ ├── 30 - Inorder Successor in BST.md │ ├── 30-july.cpp │ ├── 31 - BFS of graph.md │ ├── 31 - july.cpp │ └── readme.md ├── 08-2023(aug) │ ├── 01 - DFS of graph.md │ ├── 01-aug.cpp │ ├── 02 - Shortest Source to Destination Path.md │ ├── 02-aug.cpp │ ├── 03 - Shortest Path in Directed Acyclic Graph.md │ ├── 03-aug.cpp │ ├── 04 - Reverse a Stack.md │ ├── 04-aug.cpp │ ├── 05 - Chocolate Distribution Problem.md │ ├── 05-aug.cpp │ ├── 06 - String Permutations.md │ ├── 06-aug.cpp │ ├── 07 - Solve the Sudoku.md │ ├── 07-aug.cpp │ ├── 08 - Fraction pairs with sum 1.md │ ├── 08-aug.cpp │ ├── 09 - Largest Prime Factor.md │ ├── 09-aug.cpp │ ├── 10 - Longest Common Subsequence.md │ ├── 10-aug.cpp │ ├── 11 - Coin Change.md │ ├── 11 -aug.cpp │ ├── 12 - Longest Increasing Subsequence.md │ ├── 12-aug.cpp │ ├── 13-Nth Fibonacci Number.md │ ├── 13-aug.cpp │ ├── 14 - Non Repeating Numbers.md │ ├── 14-aug.cpp │ ├── 15 -Flip Bits .md │ ├── 15-aug.cpp │ ├── 16 - Nth Catalan Number.md │ ├── 16-aug.cpp │ ├── 17 - Next Smallest Palindrome.md │ ├── 17-aug.cpp │ ├── 18 - Leaders in an array.md │ ├── 18-aug.cpp │ ├── 19 - Subarray with given sum.md │ ├── 19-aug.cpp │ ├── 20 - Number of occurrence.md │ ├── 20-aug.cpp │ ├── 21 - Surround the 1's.md │ ├── 21-aug.cpp │ ├── 22- Make Matrix Beautiful.md │ ├── 22-aug.cpp │ ├── 23 - Find the string in grid.md │ ├── 23-aug.cpp │ ├── 24 - Multiply two strings.md │ ├── 24-aug.cpp │ ├── 25 - Palindrome String.md │ ├── 25-aug.cpp │ ├── 26 - Longest K unique characters substring.md │ ├── 26-aug.cpp │ ├── 27 - Reverse a String.md │ ├── 27-aug.cpp │ ├── 28 - Remove duplicate element from sorted Linked List.md │ ├── 28 - aug.cpp │ ├── 29 - Delete nodes having greater value on right.md │ ├── 29-aug.cpp │ ├── 30 - Delete a Node in Single Linked List.md │ ├── 30-aug.cpp │ ├── 31 - AVL Tree Deletion.md │ ├── 31-aug.cpp │ └── readme.md ├── 09-2023(sep) │ ├── 01 - Leftmost and rightmost nodes of binary tree.md │ ├── 01-sep.cpp │ ├── 02 - Leaf under budget.md │ ├── 02-sep.cpp │ ├── 03 - Check if Tree is Isomorphic.md │ ├── 03-sep.cpp │ ├── 04 - Replace O's with X's.md │ ├── 04-sep.cpp │ ├── 05 - Print adjacency list.md │ ├── 05-sep.cpp │ ├── 06 - Mother Vertex.md │ ├── 06-sep.cpp │ ├── 07 - Minimum Multiplications to reach End.md │ ├── 07-sep.cpp │ ├── 08 - Binary Tree to BST.md │ ├── 08-sep.cpp │ ├── 09 - Kth Largest Element in BST.md │ ├── 09-sep.cpp │ ├── 10 - Insert a node in a BST.md │ ├── 10-sep.cpp │ ├── 11 - Lucky Numbers.md │ ├── 11-sep.cpp │ ├── 12 - Perfect Numbers.md │ ├── 12-sep.cpp │ ├── 13 - Largest number possible.md │ ├── 13-sep.cpp │ ├── 14 - Perfect Sum Problem.md │ ├── 14-sep.cpp │ ├── 15 - Partition Equal Subset Sum.md │ ├── 15-sep.cpp │ ├── 16 - Count Number of Hops.md │ ├── 16-sep.cpp │ ├── 17 - Print first n Fibonacci Numbers.md │ ├── 17-sep.cpp │ ├── 18 - Power of 2.md │ ├── 18-sep.cpp │ ├── 19 - Find first set bit.md │ ├── 19-sep.cpp │ ├── 20 - Rotate Bits.md │ ├── 20-sep.cpp │ ├── 21 - Stickler Thief.md │ ├── 21-sep.cpp │ ├── 22 - First and Last Occurrences of x.md │ ├── 22-sep.cpp │ ├── 23 - Equilibrium Point.md │ ├── 23-sep.cpp │ ├── 24 - Find duplicates in an array.md │ ├── 24-sep.cpp │ ├── 25 - Maximum Sum Combination.md │ ├── 25 - sep.cpp │ ├── 26 - Find All Four Sum Numbers.md │ ├── 26-sep.cpp │ ├── 27 - Find the closest pair from two arrays.md │ ├── 27-sep.cpp │ ├── 28 - Wave Array.md │ ├── 28-sep.cpp │ ├── 29 - Number Of Enclaves.md │ ├── 29-sep.cpp │ ├── 30 - Boolean Matrix.md │ ├── 30-sep.cpp │ └── readme.md ├── 10-2023(oct) │ ├── 01 - Boundary Traversal of Matrix.md │ ├── 01-oct.cpp │ ├── 02 - Number of Distinct Subsequences.md │ ├── 02-oct.cpp │ ├── 03 - Column Name from a Given Column Number.md │ ├── 03-oct.cpp │ ├── 04 - Roman Number to Integer.md │ ├── 04-oct.cpp │ ├── 05 - Count number of substrings.md │ ├── 05-oct.cpp │ ├── 06 - Reverse alternate nodes in Link List.md │ ├── 06-oct.cpp │ ├── 07 - Pairwise swap elements of a linked list.md │ ├── 07-oct.cpp │ ├── 08 - Insert in a Sorted List.md │ ├── 08-oct.cpp │ ├── 09 - Height of Binary Tree.md │ ├── 09-oct.cpp │ ├── 10 - Nodes at Given Distance in Binary Tree.md │ ├── 10-oct.cpp │ ├── 11 - Check for Balanced Tree.md │ ├── 11-oct.cpp │ ├── 12 - Duplicate subtree in Binary Tree.md │ ├── 12-oct.cpp │ ├── 13 - Floor in BST.md │ ├── 13-oct.cpp │ ├── 14 - Find Common Nodes in two BSTs.md │ ├── 14-oct.cpp │ ├── 15 - Normal BST to Balanced BST.md │ ├── 15-oct.cpp │ ├── 16 - Making A Large Island.md │ ├── 16-oct.cpp │ ├── 17 - Transitive closure of a Graph.md │ ├── 17-oct.cpp │ ├── 18 - Eventual Safe States.md │ ├── 18-oct.cpp │ ├── 19 - Level of Nodes.md │ ├── 19-oct.cpp │ ├── 20 - Form a number divisible by 3 using array digits.md │ ├── 20-oct.cpp │ ├── 21 - Sum of all divisors from 1 to n.md │ ├── 21-oct.cpp │ ├── 22 - Number of Paths.md │ ├── 22-oct.cpp │ ├── 23 - Maximum sum increasing subsequence.md │ ├── 23-oct.cpp │ ├── 24 - Palindromic Partitioning.md │ ├── 24-oct.cpp │ ├── 25 - Knapsack with Duplicate Items.md │ ├── 25-oct.cpp │ ├── 26 - Minimum Operations.md │ ├── 26-oct.cpp │ ├── 27 - Minimum Deletions.md │ ├── 27-oct.cpp │ ├── 28 - Bleak Numbers.md │ ├── 28-oct.cpp │ ├── 29 - Check whether K-th bit is set or not.md │ ├── 29-oct.cpp │ ├── 30 - Sum of XOR of all pairs.md │ ├── 30-oct.cpp │ ├── 31 - Move all zeroes to the end of the array.md │ ├── 31-oct.cpp │ └── readme.md ├── 11-2023(nov) │ ├── 01 - Frequencies of Limited Range Array Elements.md │ ├── 01-nov.cpp │ ├── 02 - Minimum Distance Between Two Numbers.md │ ├── 02-nov.cpp │ ├── 03 - Pythagorean Triplet.md │ ├── 03-nov.cpp │ ├── 04 - Find Transition Point.md │ ├── 04-nov.cpp │ ├── 05 - Top K Frequent Elements in Array.md │ ├── 05-nov.cpp │ ├── 06 - Letters Collection.md │ ├── 06-nov.cpp │ ├── 07 - Sum of Upper and Lower Triangles.md │ ├── 07-nov.cpp │ ├── 08 - Print Matrix in Snake Pattern.md │ ├── 08-nov.cpp │ ├── 09 - Predict the Column.md │ ├── 09-nov.cpp │ ├── 10 - Number following a pattern.md │ ├── 10-nov.cpp │ ├── 11 - Isomorphic Strings.md │ ├── 11-nov.cpp │ ├── 12 - Check if string is rotated by two places.md │ ├── 12-nov.cpp │ ├── 13 - Shortest Common Supersequence.md │ ├── 13-nov.cpp │ ├── 14 - Check if strings are rotations of each other or not.md │ ├── 14-nov.cpp │ ├── 15 - Better String.md │ ├── 15-nov.cpp │ ├── 16 - Find the String.md │ ├── 16-nov.cpp │ ├── 17 - Binary Tree to CDLL.md │ ├── 17-nov.cpp │ ├── 18 - Reverse a Doubly Linked List.md │ ├── 18-nov.cpp │ ├── 19 - Intersection of two sorted Linked lists.md │ ├── 19-nov.cpp │ ├── 20 - K Sum Paths.md │ ├── 20-nov.cpp │ ├── 21 - Determine if Two Trees are Identical.md │ ├── 21-nov.cpp │ ├── 22 - Symmetric Tree.md │ ├── 22-nov.cpp │ ├── 23 - AVL Tree Insertion.md │ ├── 23-nov.cpp │ ├── 24 - Pascal Triangle.md │ ├── 24-nov.cpp │ ├── 25 - Shuffle integers.md │ ├── 25-nov.cpp │ ├── 26 - Print Pattern.md │ ├── 26-nov.cpp │ ├── 27 - Detect Cycle using DSU.md │ ├── 27-nov.cpp │ ├── 28 - Sum of Dependencies in a Graph.md │ ├── 28-nov.cpp │ ├── 29 - Euler Circuit and Path.md │ ├── 29-nov.cpp │ ├── 30 - Shortest path from 1 to n.md │ ├── 30-nov.cpp │ └── readme.md ├── 12-2023(dec) │ ├── 01 - Check whether BST contains Dead End.md │ ├── 01-dec.cpp │ ├── 02 - Inorder Traversal and BST.md │ ├── 02-dec.cpp │ ├── 03 - Brothers From Different Roots.md │ ├── 03-dec.cpp │ ├── 04 - Sum String.md │ ├── 04-dec.cpp │ ├── 05 - Minimize the Heights II.md │ ├── 05-dec.cpp │ ├── 06 - How Many X's?.md │ ├── 06-dec.cpp │ ├── 07 - Number of subarrays with maximum values in given range.md │ ├── 07-dec.cpp │ ├── 08 - Transform to Prime.md │ ├── 08-dec.cpp │ ├── 09 - Smith Number.md │ ├── 09-dec.cpp │ ├── 10 - Subarray with 0 sum.md │ ├── 10-dec.cpp │ ├── 11 - Max Sum Subarray of size K.md │ ├── 11-dec.cpp │ ├── 12 - Gold Mine Problem.md │ ├── 12-dec.cpp │ ├── 13 - Consecutive 1's not allowed.md │ ├── 13-dec.cpp │ ├── 14 - Painting the Fence.md │ ├── 14-dec.cpp │ ├── 15 - Reach the Nth point.md │ ├── 15-dec.cpp │ ├── 16 - String's Count.md │ ├── 16-dec.cpp │ ├── 17 - Max Sum without Adjacents.md │ ├── 17-dec.cpp │ ├── 18 - Game of XOR.md │ ├── 18-dec.cpp │ ├── 19 - Rightmost different bit.md │ ├── 19-dec.cpp │ ├── 20 - Modified Game of Nim.md │ ├── 20-dec.cpp │ ├── 21 - Candy.md │ ├── 21-dec.cpp │ ├── 22 - Maximum Meetings in One Room.md │ ├── 22-dec.cpp │ ├── 23 - Count More than n-k Occurrences.md │ ├── 23-dec.cpp │ ├── 24 - Buy Maximum Stocks if i stocks can be bought on i-th day.md │ ├── 24-dec.cpp │ ├── 25 - Determinant of a Matrix.md │ ├── 25-dec.cpp │ ├── 26 - Largest rectangular sub-matrix whose sum is 0.md │ ├── 26-dec.cpp │ ├── 27 - Anti Diagonal Traversal of Matrix.md │ ├── 27-dec.cpp │ ├── 28 - Wildcard String Matching.md │ ├── 28-dec.cpp │ ├── 29 - Check if a string is repetition of its substring of k-length.md │ ├── 29-dec.cpp │ ├── 30 - Winner of an Election.md │ ├── 30-dec.cpp │ ├── 31 - New Year Resolution.md │ ├── 31-dec.cpp │ └── readme.md └── readme.md ├── 2024 ├── 01-2024(jan) │ ├── 01 - Array Pair Sum Divisibility Problem.md │ ├── 01-jan.cpp │ ├── 02 - Largest Sum Subarray of Size at least K.md │ ├── 02-jan.cpp │ ├── 03 - Smallest window containing 0, 1 and 2.md │ ├── 03-jan.cpp │ ├── 04 - Find element occurring once when all others are present thrice.md │ ├── 04-jan.cpp │ ├── 05 - Count Possible Ways to Construct Buildings.md │ ├── 05-jan.cpp │ ├── 06 - Techfest and the Queue.md │ ├── 06-jan.cpp │ ├── 07 - Split Array Largest Sum.md │ ├── 07-jan.cpp │ ├── 08 - Merge 2 sorted linked lists in reverse order.md │ ├── 08-jan.cpp │ ├── 09 - Search Pattern (KMP-Algorithm).md │ ├── 09-jan.cpp │ ├── 10 - Longest subarray with sum divisible by K.md │ ├── 10-jan.cpp │ ├── 11 - Remove K Digits.md │ ├── 11-jan.cpp │ ├── 12 - Reverse First K elements of Queue.md │ ├── 12-jan.cpp │ ├── 13 - Insertion Sort for Singly Linked List.md │ ├── 13-jan.cpp │ ├── 14 - Find duplicate rows in a binary matrix.md │ ├── 14-jan.cpp │ ├── 15 - Grinding Geek.md │ ├── 15-jan.cpp │ ├── 16 - Sequence of Sequence.md │ ├── 16-jan.cpp │ ├── 17 - All Unique Permutations of an array.md │ ├── 17-jan.cpp │ ├── 18 - Water the plants.md │ ├── 18-jan.cpp │ ├── 19 - Top k numbers in a stream.md │ ├── 19-Jan.cpp │ ├── 20 - Distribute candies in a binary tree.md │ ├── 20-jan.cpp │ ├── 21 - Vertex Cover.md │ ├── 21-jan.cpp │ ├── 22 - Paths from root with a specified sum.md │ ├── 22-jan.cpp │ ├── 23 - Course Schedule.md │ ├── 23-jan.cpp │ ├── 24 - Is it a tree.md │ ├── 24-jan.cpp │ ├── 25 - Shortest Prime Path.md │ ├── 25-jan.cpp │ ├── 26 - Fractional Knapsack.md │ ├── 26-jan.cpp │ ├── 27 - Brackets in Matrix Chain Multiplication.md │ ├── 27-jan.cpp │ ├── 29 - Count digit groupings of a number.md │ ├── 29-jan.cpp │ ├── 30 - LCS of three strings.md │ ├── 30-jan.cpp │ ├── 31 - Insert and Search in a Trie.md │ ├── 31-jan.cpp │ └── readme.md ├── 02-2024(feb) │ ├── 01 - Panagram Checking.md │ ├── 01-feb.cpp │ ├── 02 - Implement Atoi.md │ ├── 02-feb.cpp │ ├── 03 - Decimal Equivalent of Binary Linked List.md │ ├── 03-feb.cpp │ ├── 05 - Sorted insert for circular linked list.md │ ├── 05-feb.cpp │ ├── 06 - Count the nodes at distance K from leaf.md │ ├── 06-feb.cpp │ ├── 07 - Min distance between two given nodes of a Binary Tree.md │ ├── 07-feb.cpp │ ├── 08 - Check if all leaves are at the same level.md │ ├── 08-feb.cpp │ ├── 09 - Check for Children Sum Property in a Binary Tree.md │ ├── 09-feb.cpp │ ├── 10 - Number of paths in a matrix with k coins.md │ ├── 10-feb.cpp │ ├── 11 - Recamans sequence.md │ ├── 11-feb.cpp │ ├── 12 - Recursive sequence.md │ ├── 12-feb.cpp │ ├── 14 - Find all Critical Connections in the Graph.md │ ├── 14-feb.cpp │ ├── 15 - Count all Possible Path.md │ ├── 15-feb.cpp │ ├── 16 - Flatten BST to sorted list.md │ ├── 16-feb.cpp │ ├── 17 - Does array represent Heap.md │ ├── 17-feb.cpp │ ├── 18 - Sum of leaf nodes in BST.md │ ├── 18-feb.cpp │ ├── 19 - Game with String.md │ ├── 19-feb.cpp │ ├── 20 - Word Break.md │ ├── 20-feb.cpp │ ├── 21 - Boolean Parenthesization.md │ ├── 21-feb.cpp │ ├── 22 - Distinct occurrences.md │ ├── 22-feb.cpp │ ├── 23 - Buy and Sell a Share at most twice.md │ ├── 23-feb.cpp │ ├── 24 - Maximum Sum Problem.md │ ├── 24-feb.cpp │ ├── 25 - Reach a given score.md │ ├── 25-feb.cpp │ ├── 26 - Power Set.md │ ├── 26-feb.cpp │ ├── 27 - Play With OR.md │ ├── 27-feb.cpp │ ├── 28 - Check if a number is divisible by 8.md │ ├── 28-feb.cpp │ ├── 29 - Sum of bit differences.md │ ├── 29-feb.cpp │ └── readme.md ├── 03-2024(march) │ ├── 01 - Peak element.md │ ├── 01-march.cpp │ ├── 02 - First element to occur k times.md │ ├── 02-march.cpp │ ├── 03 - Largest Number formed from an Array.md │ ├── 03-march.cpp │ ├── 04 - Swap the array elements.md │ ├── 04-march.cpp │ ├── 05 - Maximum Index.md │ ├── 05-march.cpp │ ├── 06 - Search Pattern (Rabin-Karp Algorithm).md │ ├── 06-march.cpp │ ├── 07 - Longest repeating and non-overlapping substring.md │ ├── 07-march.cpp │ ├── 08 - Check if frequencies can be equal.md │ ├── 08-march.cpp │ ├── 09 - Find the N-th character.md │ ├── 09-march.cpp │ ├── 10 - Remove all duplicates from a given string.md │ ├── 10-march.cpp │ ├── 11 - Count pairs Sum in matrices.md │ ├── 11-march.cpp │ ├── 14 - Largest subsquare surrounded by X.md │ ├── 14-march.cpp │ ├── 15 - Linked List that is Sorted Alternatingly.md │ ├── 15-march.cpp │ ├── 16 - Delete without head pointer.md │ ├── 16-march.cpp │ ├── 17 - Count Pairs whose sum is equal to X.md │ ├── 17-march.cpp │ ├── 18 - Level order traversal.md │ ├── 18-march.cpp │ ├── 20 - Sum of nodes on the longest path from root to leaf node.md │ ├── 20-march.cpp │ ├── 21 - ZigZag Tree Traversal.md │ ├── 21-march.cpp │ ├── 22 - Diagonal sum in binary tree.md │ ├── 22 - march.cpp │ ├── 23 - Fibonacci series up to Nth term.md │ ├── 23 - march.cpp │ ├── 24 - Insert an Element at the Bottom of a Stack.md │ ├── 24 - march.cpp │ ├── 26 - Additive sequence.md │ ├── 26-march.cpp │ ├── 27 - Find shortest safe route in a matrix.md │ ├── 27-march.cpp │ ├── 28 - City With the Smallest Number of Neighbors.md │ ├── 28-march.cpp │ ├── 29 - Euler Circuit in an Undirected Graph.md │ ├── 29-march.cpp │ ├── 30 - Minimum element in BST.md │ ├── 30-march.cpp │ ├── 31 - Closest Neighbour in BST.md │ ├── 31-march.cpp │ └── readme.md ├── 04-2024(april) │ ├── 01 - Pairs violating the BST property.md │ ├── 01-april.cpp │ ├── 04 - Sum of all substrings of a number.md │ ├── 04-April.cpp │ ├── 05 - Strictly Increasing Array.md │ ├── 05-april.cpp │ ├── 06 - Count ways to N'th Stair.md │ ├── 06-april.cpp │ ├── 07 - Maximize dot product.md │ ├── 07-april.cpp │ ├── 08 - Optimal Strategy For A Game.md │ ├── 08-april.cpp │ ├── 09 - Minimum Points To Reach Destination.md │ ├── 09-april.cpp │ ├── 10 - Party of Couples.md │ ├── 10-april.cpp │ ├── 11 - Gray to Binary equivalent.md │ ├── 11-april.cpp │ ├── 12 - Sum of Products.md │ ├── 12-april.cpp │ ├── 13 - Reverse Bits.md │ ├── 13-april.cpp │ ├── 14 - Xoring and Clearing.md │ ├── 14-april.cpp │ ├── 15 - Count the elements.md │ ├── 15-april.cpp │ ├── 17 - Count Pairs in an Array.md │ ├── 17-april.cpp │ ├── 18 - Count Pairs in an Array.md │ ├── 18-april.cpp │ ├── 19 - Find missing in second array.md │ ├── 19-april.cpp │ ├── 20 - Union of Two Sorted Arrays.md │ ├── 20-april.cpp │ ├── 22 - Row with minimum number of 1's.md │ ├── 22-april.cpp │ ├── 23 - Rohan's Love for Matrix.md │ ├── 23-april.cpp │ ├── 28 - Delete Middle of Linked List.md │ ├── 28-april.cpp │ ├── 29 - Remove every kth node.md │ ├── 29-april.cpp │ └── readme.md ├── 05-2024(may) │ ├── 03 - K distance from root.md │ ├── 03-May.cpp │ ├── 06 - Print all nodes that don't have sibling.md │ ├── 06-May.cpp │ ├── 07 - Reverse Level Order Traversal.md │ ├── 07-may.cpp │ ├── 17 - Find Pair Given Difference.md │ ├── 17-may.cpp │ ├── 18 - Find the Highest number.md │ ├── 18-may.cpp │ ├── 19 - Find the closest number.md │ ├── 19-may.cpp │ ├── 20 - Modular Exponentiation for large numbers.md │ ├── 20-may.cpp │ ├── 23 - K-Palindrome.md │ ├── 23-may.cpp │ ├── 24 - Partitions with Given Difference.md │ ├── 24-may.cpp │ ├── 26 - Minimum Cost To Make Two Strings Identical.md │ ├── 26-may.cpp │ ├── 28 - Minimum cost to fill given weight in a bag.md │ └── 28-may.cpp ├── 06-2024(june) │ ├── 01 - Odd Even Problem.md │ ├── 01-june.cpp │ ├── 02 - Construct list using given q XOR queries.md │ ├── 02-june.cpp │ ├── 03 - Trail of ones.md │ ├── 03-june.cpp │ ├── 04-june.cpp │ ├── 05-june.cpp │ ├── 06-june.cpp │ ├── 07-june.cpp │ ├── 08-june.cpp │ ├── 09-june.cpp │ ├── 10-june.cpp │ ├── 11-june.cpp │ ├── 12-june.cpp │ ├── 13-june.cpp │ ├── 14-june.cpp │ ├── 15-june.cpp │ ├── 16-june.cpp │ ├── 17-june.cpp │ ├── 18-june.cpp │ ├── 19-june.cpp │ ├── 20-june.cpp │ ├── 21-june.cpp │ ├── 22-june.cpp │ ├── 23-june.cpp │ ├── 24-june.cpp │ ├── 25-june.cpp │ ├── 26-june.cpp │ ├── 27-june.cpp │ ├── 28-june.cpp │ ├── 29 - Identical Linked Lists.md │ ├── 29-june.cpp │ ├── 30 - Delete node in Doubly Linked List.md │ └── 30-june.cpp ├── 07-2024(july) │ ├── 01-july.cpp │ ├── 02-july.cpp │ ├── 03 - Remove all occurences of duplicates in a linked list.md │ └── 03-july.cpp └── readme.md ├── .gitignore ├── README.md └── helper └── template.md /.gitignore: -------------------------------------------------------------------------------- 1 | .cph 2 | *.bin 3 | .vscode -------------------------------------------------------------------------------- /2023/05-2023(may)/15may.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/1132bd8ee92072cd31441858402641d6800fa6b3/1 2 | // Count Total Setbits 3 | 4 | class Solution { 5 | public: 6 | long long countBits(long long N) { 7 | // code here 8 | if(N == 1) return 1; 9 | long long t = log2(N),out = 0,sets; 10 | for(int i = t; i>=0;--i){ 11 | sets = N/(1<=m || (abs(t)+u)>=n) return 0; 17 | } 18 | return 1; 19 | } 20 | }; -------------------------------------------------------------------------------- /2023/05-2023(may)/18may.cpp: -------------------------------------------------------------------------------- 1 | // Link - https://practice.geeksforgeeks.org/problems/find-number-of-closed-islands/1 2 | // Find number of closed islands 3 | 4 | class Solution { 5 | public: 6 | 7 | void help(vector> &arr,int i,int j,int N,int M){ 8 | if(i<0 or j<0 or i==N or j==M or arr[i][j]==0)return; 9 | arr[i][j]=0; 10 | help(arr,i+1,j,N,M); 11 | help(arr,i-1,j,N,M); 12 | help(arr,i,j+1,N,M); 13 | help(arr,i,j-1,N,M); 14 | } 15 | int closedIslands(vector>& matrix, int N, int M) { 16 | // Code here 17 | int ans=0; 18 | for(int i=0;i &hand) { 4 | // code here 5 | priority_queue, greater> que; 6 | if(N%groupSize) return false; 7 | int it; 8 | for(auto i: hand){ 9 | que.push(i); 10 | } 11 | int last = -1; 12 | for(int i = 0;i extra; 17 | // cout<0 && !que.empty()){ 19 | // cout< chairs, vector passengers) { 4 | sort(chairs.begin(), chairs.end()); 5 | sort(passengers.begin(), passengers.end()); 6 | int out = 0; 7 | for(int i = 0; i < n; i++){ 8 | out += abs(chairs[i] - passengers[i]); 9 | } 10 | return out; 11 | } 12 | }; -------------------------------------------------------------------------------- /2023/05-2023(may)/22may.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int solve(int N, vector p){ 4 | vector count(N, 0); 5 | int c= 0; 6 | if(count[0] == 1) 7 | ++c; 8 | for(int i = 1; i < N; i++){ 9 | ++count[p[i]]; 10 | } 11 | for(auto i: count) 12 | if(!i) 13 | ++c; 14 | 15 | return max(N - 1 - c, 0); 16 | } 17 | }; -------------------------------------------------------------------------------- /2023/05-2023(may)/23may.cpp: -------------------------------------------------------------------------------- 1 | class Solution{ 2 | public: 3 | int till; 4 | int it = 0; 5 | Node* constructBinaryTree(int pre[], int preMirror[], int n) 6 | { 7 | Node* head = new Node(pre[0]); 8 | till = (int)log2(n); 9 | Node* temp = head; 10 | stack>st; 11 | for(int i = 1;i<=till;++i){ 12 | st.push({temp,i-1}); 13 | temp->left = new Node(pre[i]); 14 | temp = temp->left; 15 | } 16 | it = till; 17 | while(!st.empty()){ 18 | temp = st.top().first; 19 | int index = st.top().second+1; 20 | // cout<data<<" "<right = new Node(pre[++it]); 23 | temp = temp->right; 24 | for(int i = index+1;i<=till;++i){ 25 | st.push({temp,i-1}); 26 | temp->left = new Node(pre[++it]); 27 | temp = temp->left; 28 | } 29 | } 30 | 31 | 32 | return head; 33 | } 34 | }; -------------------------------------------------------------------------------- /2023/05-2023(may)/29may.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | vector CamelCase(int n, vector Dictionary, string p) { 9 | vector out; 10 | string s; 11 | for(auto i: Dictionary){ 12 | s = ""; 13 | for(auto j: i){ 14 | if(j>='A' && j<='Z' && s.size()> t; 31 | while (t--) { 32 | int N; 33 | cin >> N; 34 | vector Dictionary(N); 35 | for (int i = 0; i < N; i++) cin >> Dictionary[i]; 36 | string Pattern; 37 | cin >> Pattern; 38 | Solution ob; 39 | vector ans = ob.CamelCase(N, Dictionary, Pattern); 40 | sort(ans.begin(), ans.end()); 41 | for (auto u : ans) cout << u << " "; 42 | cout << "\n"; 43 | } 44 | return 0; 45 | } 46 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/05-2023(may)/31may.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | // User function Template for C++ 10 | 11 | class Solution{ 12 | public: 13 | int LargButMinFreq(int arr[], int n) { 14 | // code here 15 | map> mp; 16 | for(int i = 0;ii.second){ 23 | nax = i.second; 24 | out = i.first; 25 | } 26 | } 27 | return out; 28 | } 29 | }; 30 | 31 | //{ Driver Code Starts. 32 | 33 | int main() { 34 | int t; 35 | cin >> t; 36 | // Iterating over testcases 37 | while (t--) { 38 | int n; 39 | cin >> n; 40 | 41 | int arr[n]; 42 | 43 | for (int i = 0; i < n; i++) cin >> arr[i]; 44 | Solution ob; 45 | 46 | cout << ob.LargButMinFreq(arr, n) << endl; 47 | } 48 | } 49 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/04june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution 7 | { 8 | public: 9 | string reverseEqn (string str) 10 | { 11 | stack num; 12 | stack op; 13 | string s = ""; 14 | for(auto i: str){ 15 | if(i<'0' || i>'9'){ 16 | num.push(s); 17 | op.push(i); 18 | s = ""; 19 | } 20 | else 21 | s += i; 22 | } 23 | num.push(s); 24 | s = ""; 25 | while(!op.empty()){ 26 | s += num.top(); 27 | s += op.top(); 28 | num.pop(); 29 | op.pop(); 30 | } 31 | s += num.top(); 32 | return s; 33 | } 34 | }; 35 | 36 | //{ Driver Code Starts. 37 | int main() 38 | { 39 | int t; cin >> t; 40 | while (t--) 41 | { 42 | string s; cin >> s; 43 | Solution ob; 44 | cout < 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | // User function Template for C++ 8 | class Solution { 9 | public: 10 | int findFactor(int n){ 11 | if(n<2) return n; 12 | for(int i = 2;i<=sqrt(n);++i ){ 13 | if(n%i == 0) 14 | return i; 15 | } 16 | return n; 17 | } 18 | 19 | vector leastPrimeFactor(int n) { 20 | vector out(n+1); 21 | for(int i = 0;i<=n;++i) 22 | out[i] = findFactor(i); 23 | return out; 24 | } 25 | }; 26 | 27 | //{ Driver Code Starts. 28 | int main() { 29 | int t; 30 | cin >> t; 31 | while (t--) { 32 | int n; 33 | cin >> n; 34 | Solution ob; 35 | vectorans = ob.leastPrimeFactor(n); 36 | for(int i=1;i<=n;i++)cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | //User function Template for C++ 10 | 11 | class Solution 12 | { 13 | public: 14 | 15 | string s = ""; 16 | void perm(int n){ 17 | int i,j; 18 | for(i = n-2;i>=0;--i){ 19 | if(s[i]i;--j){ 23 | if(s[j]>s[i]) 24 | break; 25 | } 26 | swap(s[j],s[i]); 27 | sort(s.begin()+i+1,s.end()); 28 | } 29 | string kthPermutation(int n, int k) 30 | { 31 | for(int i = 1;i<=n;++i) 32 | s += (char)('0'+i); 33 | while(--k) 34 | perm(n); 35 | 36 | return s; 37 | } 38 | }; 39 | 40 | //{ Driver Code Starts. 41 | 42 | int main() 43 | { 44 | int t; 45 | cin >> t; 46 | while (t--) 47 | { 48 | int n, k; 49 | cin >> n >> k; 50 | Solution ob; 51 | string ans = ob.kthPermutation(n, k); 52 | cout << ans; 53 | cout << "\n"; 54 | } 55 | return 0; 56 | } 57 | 58 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/10june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | #define ll long long 5 | 6 | // } Driver Code Ends 7 | class Solution 8 | { 9 | public: 10 | void Rearrange(int arr[], int n) 11 | { 12 | vector neq,pos; 13 | for(int i = 0;i>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | int arr[n]; 40 | for(int i=0;i>arr[i]; 42 | long long j=0; 43 | Solution ob; 44 | ob.Rearrange(arr, n); 45 | 46 | for (int i = 0; i < n; i++) 47 | cout << arr[i] << " "; 48 | cout << endl; 49 | } 50 | return 0; 51 | } 52 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/11june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | void update(int arr[], int n, int updates[], int k) 10 | { 11 | vector out(n+1,0); 12 | for(int i = 0;i>t; 26 | while(t--) 27 | { 28 | int n,k; 29 | cin>>n>>k; 30 | int a[n]={0}, updates[k]={0}; 31 | for(int i = 0; i < k; i++) 32 | cin>>updates[i]; 33 | Solution ob; 34 | ob.update(a, n, updates, k); 35 | 36 | for(int i = 0; i < n; i++) 37 | cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | // User function Template for C++ 10 | 11 | class Solution{ 12 | public: 13 | int cutRod(int price[], int n) { 14 | int dp[n+1]; 15 | dp[0] = 0; 16 | for(int i = 1;i<=n;++i){ 17 | dp[i] = -1; 18 | for(int j = 0;j> t; 31 | while (t--) { 32 | int n; 33 | cin >> n; 34 | int a[n]; 35 | for (int i = 0; i < n; i++) 36 | cin >> a[i]; 37 | 38 | Solution ob; 39 | 40 | cout << ob.cutRod(a, n) << endl; 41 | } 42 | return 0; 43 | } 44 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/13june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | //User function template for C++ 9 | class Solution{ 10 | public: 11 | vector kLargest(int arr[], int n, int k) { 12 | priority_queue,greater> pq; 13 | vector out; 14 | 15 | for(int i = 0 ; ik) 18 | pq.pop(); 19 | } 20 | 21 | while(!pq.empty()){ 22 | out.push_back(pq.top()); 23 | pq.pop(); 24 | } 25 | 26 | reverse(out.begin(),out.end()); 27 | 28 | return out; 29 | } 30 | 31 | }; 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() { 36 | int t; 37 | cin >> t; 38 | while (t--) { 39 | int n, k; 40 | cin >> n >> k; 41 | int arr[n]; 42 | for (int i = 0; i < n; i++) { 43 | cin >> arr[i]; 44 | } 45 | Solution ob; 46 | auto ans = ob.kLargest(arr, n, k); 47 | for (auto x : ans) { 48 | cout << x << " "; 49 | } 50 | cout << "\n"; 51 | } 52 | return 0; 53 | } 54 | 55 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/14june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | long long maxDiamonds(int a[], int n, int k) { 11 | priority_queue pq; 12 | for(int i = 0 ; i> t; 30 | while (t--) { 31 | int N,K; 32 | 33 | cin>>N>>K; 34 | int A[N]; 35 | 36 | for(int i=0; i>A[i]; 38 | 39 | Solution ob; 40 | cout << ob.maxDiamonds(A,N,K) << endl; 41 | } 42 | return 0; 43 | } 44 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/15june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool ispalin(string& s, int i ,int j){ 11 | while(ii; --j){ 26 | if((j-i+1)>ms && ispalin(s,i,j)){ 27 | ms = j-i+1; 28 | out = s.substr(i,j-i+1); 29 | } 30 | } 31 | } 32 | return out; 33 | 34 | } 35 | }; 36 | 37 | 38 | //{ Driver Code Starts. 39 | 40 | int main() 41 | { 42 | int t; cin >> t; 43 | while (t--) 44 | { 45 | string S; cin >> S; 46 | 47 | Solution ob; 48 | cout << ob.longestPalin (S) << endl; 49 | } 50 | } 51 | // Contributed By: Pranay Bansal 52 | 53 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/17june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | //User function Template for C++ 8 | // Helper class Solution to implement 9 | // insert() and findFrequency() 10 | class Solution{ 11 | public: 12 | 13 | map hash; 14 | void insert(queue &q, int k){ 15 | ++hash[k]; 16 | q.push(k); 17 | } 18 | 19 | 20 | int findFrequency(queue &q, int k){ 21 | return hash[k]; 22 | } 23 | 24 | }; 25 | 26 | //{ Driver Code Starts. 27 | int main() { 28 | 29 | int testcase; 30 | cin>>testcase; 31 | 32 | while(testcase-- > 0){ 33 | // Declaring Queue 34 | queue q; 35 | int n, k; 36 | cin>>n; 37 | 38 | // Invoking object of Geeks class 39 | Solution obj; 40 | 41 | for(int i = 0;i> k; 43 | obj.insert(q, k); 44 | } 45 | 46 | int m; 47 | cin >> m; 48 | for(int i = 0;i> k; 50 | int f = obj.findFrequency(q, k); 51 | if(f != 0){ 52 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int distributeTicket(int n, int k) { 11 | int op = n/k; 12 | int left = (op/2)*k; 13 | int right = n - (op/2)*k + 1; 14 | 15 | if(right - left > (k + 1)) 16 | return left + k + 1; 17 | 18 | return right - (right - left != 1); 19 | } 20 | }; 21 | 22 | //{ Driver Code Starts. 23 | 24 | int main(){ 25 | int t; 26 | scanf("%d ",&t); 27 | while(t--){ 28 | 29 | int N; 30 | scanf("%d",&N); 31 | 32 | 33 | int K; 34 | scanf("%d",&K); 35 | 36 | Solution obj; 37 | int res = obj.distributeTicket(N, K); 38 | 39 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | 9 | class Solution{ 10 | public: 11 | void arrange(long long arr[], int n) { 12 | long long offset =1e5; 13 | for (int i = 0; i < n; i++) { 14 | arr[i] = (arr[arr[i]] % offset) * offset + arr[i]; 15 | } 16 | // set the array with new value as "newValue = newvalue*offset + oldValue" 17 | 18 | for (int i = 0; i < n; i++) { 19 | arr[i] = arr[i]/offset; 20 | } 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main(){ 28 | 29 | int t; 30 | //testcases 31 | cin>>t; 32 | while(t--){ 33 | 34 | int n; 35 | //size of array 36 | cin>>n; 37 | long long A[n]; 38 | 39 | //adding elements to the array 40 | for(int i=0;i>A[i]; 42 | } 43 | Solution ob; 44 | //calling arrange() function 45 | ob.arrange(A, n); 46 | 47 | //printing the elements 48 | for(int i=0;i 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | int matchGame(long long n) { 9 | if(n%5) 10 | return n%5; 11 | 12 | return -1; 13 | } 14 | }; 15 | 16 | //{ Driver Code Starts. 17 | int main() { 18 | int t; 19 | cin >> t; 20 | while (t--) { 21 | long long N; 22 | 23 | cin>>N; 24 | 25 | Solution ob; 26 | cout << ob.matchGame(N) << endl; 27 | } 28 | return 0; 29 | } 30 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/21june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | int mod = 1e9+7; 9 | int sumOfNaturals(int n) { 10 | long long sum = n; 11 | sum = (sum*(sum+1))%mod; 12 | sum /= 2; 13 | 14 | return sum; 15 | } 16 | }; 17 | 18 | //{ Driver Code Starts. 19 | int main() { 20 | int t; 21 | cin >> t; 22 | while (t--) { 23 | int n; 24 | 25 | cin>>n; 26 | 27 | Solution ob; 28 | cout << ob.sumOfNaturals(n) << endl; 29 | } 30 | return 0; 31 | } 32 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/24june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | //User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | int klengthpref(string arr[], int n, int k, string str){ 13 | if(k>str.size()) 14 | return 0; 15 | 16 | string toSearch = str.substr(0,k); 17 | int c = 0; 18 | 19 | for(int i = 0; i>t; 34 | while(t--) 35 | { 36 | int n; 37 | cin>>n; 38 | string arr[n]; 39 | for(int i=0;i>arr[i]; 43 | 44 | } 45 | int k; 46 | cin>>k; 47 | string str; 48 | cin>>str; 49 | 50 | Solution ob; 51 | cout << ob.klengthpref(arr, n, k, str) << endl; 52 | } 53 | return 0; 54 | } 55 | 56 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/29june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | 10 | bool isHappy(int n) { 11 | // 7 & 1 are the only numbers which can be happy in single digit by observation 12 | if (n == 1 || n == 7) 13 | return 1; 14 | 15 | int next, sum; 16 | next = sum = n; 17 | 18 | while (next > 9) { 19 | sum = 0; 20 | 21 | while (next > 0) { 22 | int digit = next % 10; 23 | next /= 10; 24 | sum += digit * digit; 25 | } 26 | 27 | if (sum == 1 || sum == 7) 28 | return 1; 29 | 30 | next = sum; 31 | } 32 | 33 | return 0; 34 | } 35 | 36 | int nextHappy(int n) { 37 | int nextNum = n + 1; 38 | 39 | while (isHappy(nextNum) != 1) 40 | nextNum++; 41 | 42 | return nextNum; 43 | } 44 | 45 | }; 46 | 47 | 48 | //{ Driver Code Starts. 49 | int main() 50 | { 51 | int t; 52 | cin>>t; 53 | while(t--) 54 | { 55 | int N; 56 | cin>>N; 57 | Solution ob; 58 | cout << ob.nextHappy(N) << endl; 59 | } 60 | return 0; 61 | } 62 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/30june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int isDivisible(string s) { 11 | int rem = 0; 12 | 13 | for (int i = 0; i < s.size(); ++i) { 14 | if (s[i] == '1') { 15 | if (i % 2) 16 | rem += 2; 17 | else 18 | rem++; 19 | } 20 | } 21 | 22 | return rem % 3 == 0; 23 | } 24 | }; 25 | 26 | 27 | 28 | //{ Driver Code Starts. 29 | int main(){ 30 | 31 | int t; 32 | cin >> t; 33 | while(t--){ 34 | string s; 35 | cin >> s; 36 | Solution ob; 37 | cout << ob.isDivisible(s) << endl; 38 | } 39 | return 0; 40 | } 41 | 42 | 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/06-2023(june)/readme.md: -------------------------------------------------------------------------------- 1 | ## JUNE - 2023 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **June 2023**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2023/07-2023(july)/01-july.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int setBits(int n) { 10 | int cnt = 0; 11 | while(n){ 12 | cnt += n&1; 13 | n = n>>1; 14 | } 15 | return cnt; 16 | } 17 | }; 18 | 19 | 20 | //{ Driver Code Starts. 21 | int main() { 22 | int t; 23 | cin >> t; 24 | while (t--) { 25 | int N; 26 | cin >> N; 27 | 28 | Solution ob; 29 | int cnt = ob.setBits(N); 30 | cout << cnt << endl; 31 | } 32 | return 0; 33 | } 34 | 35 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/07-2023(july)/02-july.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int setSetBit(int x, int y, int l, int r) { 11 | int maskLen = r - l + 1; 12 | int mask = (1 << maskLen) - 1; 13 | mask = mask << (l - 1); 14 | y = y & mask; 15 | return (x | y); 16 | } 17 | }; 18 | 19 | 20 | //{ Driver Code Starts. 21 | int main(){ 22 | int t; 23 | cin>>t; 24 | while(t--){ 25 | int x, y, l, r; 26 | cin>>x>>y>>l>>r; 27 | 28 | Solution ob; 29 | cout< 3 | using namespace std; 4 | 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | 10 | class Solution { 11 | public: 12 | int countSubArrayProductLessThanK(const vector& a, int n, long long k) { 13 | int out = 0; 14 | int i = 0, j = 0; 15 | long long currProd = 1; 16 | 17 | while (i < n && j < n) { 18 | currProd *= a[j]; 19 | 20 | while (currProd >= k && i < j) { 21 | currProd /= a[i]; 22 | i++; 23 | } 24 | 25 | if (currProd < k) 26 | out += j - i + 1; 27 | 28 | j++; 29 | } 30 | return out; 31 | } 32 | }; 33 | 34 | 35 | 36 | //{ Driver Code Starts. 37 | int main() { 38 | int t; 39 | cin >> t; 40 | while (t--) { 41 | int n, i; 42 | long long int k; 43 | cin >> n >> k; 44 | vector arr(n); 45 | for (i = 0; i < n; i++) cin >> arr[i]; 46 | Solution obj; 47 | cout << obj.countSubArrayProductLessThanK(arr, n, k) << endl; 48 | } 49 | return 0; 50 | } 51 | 52 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/07-2023(july)/09-july.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution 9 | { 10 | public: 11 | int missingNumber(int arr[], int n) 12 | { 13 | unordered_map mp; 14 | for(int i = 0; i>t; 36 | while(t--){ 37 | 38 | //input number n 39 | int n; 40 | cin>>n; 41 | int arr[n]; 42 | 43 | //adding elements to the array 44 | for(int i=0; i>arr[i]; 45 | 46 | Solution ob; 47 | //calling missingNumber() 48 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution 8 | { 9 | public: 10 | void transpose(vector>& mat, int n) 11 | { 12 | for(int i = 0; i>t; 26 | 27 | while(t--) 28 | { 29 | int n; 30 | cin>>n; 31 | vector > matrix(n,vector(n)); 32 | 33 | for(int i=0; i>matrix[i][j]; 38 | } 39 | } 40 | 41 | Solution ob; 42 | ob.transpose(matrix,n); 43 | for (int i = 0; i < n; ++i) 44 | { 45 | for (int j = 0; j < n; ++j) 46 | cout< 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool isFrequencyUnique(int n, int arr[]) { 11 | unordered_map freq; 12 | unordered_set uniqueSet; 13 | 14 | for (int i = 0; i < n; ++i) 15 | ++freq[arr[i]]; 16 | 17 | for (auto i : freq) 18 | uniqueSet.insert(i.second); 19 | 20 | return freq.size() == uniqueSet.size(); 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | int n; 33 | cin>>n; 34 | int arr[n]; 35 | for(int i=0;i>arr[i]; 37 | Solution ob; 38 | bool ans=ob.isFrequencyUnique(n,arr); 39 | if(ans) 40 | cout<<1< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution 10 | { 11 | public: 12 | queue rev(queue q) 13 | { 14 | stack s; 15 | while(!q.empty()){ 16 | s.push(q.front()); 17 | q.pop(); 18 | } 19 | 20 | while(!s.empty()){ 21 | q.push(s.top()); 22 | s.pop(); 23 | } 24 | 25 | return q; 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | int main() 32 | { 33 | int test; 34 | cin>>test; 35 | while(test--) 36 | { 37 | queue q; 38 | int n, var; 39 | cin>>n; 40 | while(n--) 41 | { 42 | cin>>var; 43 | q.push(var); 44 | } 45 | Solution ob; 46 | queue a=ob.rev(q); 47 | while(!a.empty()) 48 | { 49 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | string FirstNonRepeating(string A) { 10 | vector cnt(26, 0); 11 | vector last; 12 | int firstOccur = 0; 13 | string out = ""; 14 | 15 | for (int i = 0; i < A.size(); ++i) { 16 | cnt[A[i] - 'a']++; 17 | 18 | if (cnt[A[i] - 'a'] == 1) 19 | last.push_back(A[i]); 20 | 21 | while (firstOccur < last.size() && cnt[last[firstOccur] - 'a'] > 1) 22 | ++firstOccur; 23 | 24 | if (firstOccur == last.size()) { 25 | out += '#'; 26 | } else { 27 | out += last[firstOccur]; 28 | } 29 | 30 | } 31 | return out; 32 | } 33 | }; 34 | 35 | 36 | //{ Driver Code Starts. 37 | int main(){ 38 | int tc; 39 | cin >> tc; 40 | while(tc--){ 41 | string A; 42 | cin >> A; 43 | Solution obj; 44 | string ans = obj.FirstNonRepeating(A); 45 | cout << ans << "\n"; 46 | } 47 | return 0; 48 | } 49 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/07-2023(july)/18-july.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int maxSub(int i, int j, int n, string& s, vector>& dp) { 10 | if (i == n || j == n) 11 | return 0; 12 | 13 | if (dp[i][j] != -1) 14 | return dp[i][j]; 15 | 16 | if (i != j && s[i] == s[j]) { 17 | dp[i][j] = 1 + maxSub(i + 1, j + 1, n, s, dp); 18 | } else { 19 | dp[i][j] = maxSub(i, j + 1, n, s, dp); 20 | dp[i][j] = max(dp[i][j], maxSub(i + 1, j, n, s, dp)); 21 | } 22 | 23 | return dp[i][j]; 24 | } 25 | 26 | int LongestRepeatingSubsequence(string str) { 27 | int n = str.size(); 28 | vector> dp(n, vector(n, -1)); 29 | return maxSub(0, 0, n, str, dp); 30 | } 31 | }; 32 | 33 | 34 | 35 | //{ Driver Code Starts. 36 | int main(){ 37 | int tc; 38 | cin >> tc; 39 | while(tc--){ 40 | string str; 41 | cin >> str; 42 | Solution obj; 43 | int ans = obj.LongestRepeatingSubsequence(str); 44 | cout << ans << "\n"; 45 | } 46 | return 0; 47 | } 48 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/07-2023(july)/19-july.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution{ 11 | public: 12 | int check(int i, int j, int n, string &A, string &rev, vector>& dp){ 13 | if(i == n || j == n) 14 | return 0; 15 | 16 | if(dp[i][j] != -1) 17 | return dp[i][j]; 18 | 19 | if(A[i] == rev[j]){ 20 | dp[i][j] = 1 + check(i+1,j+1,n,A,rev,dp); 21 | } 22 | dp[i][j] = max(dp[i][j], check(i,j+1,n,A,rev,dp)); 23 | dp[i][j] = max(dp[i][j], check(i+1,j,n,A,rev,dp)); 24 | 25 | return dp[i][j]; 26 | } 27 | int longestPalinSubseq(string A) { 28 | int n = A.size(); 29 | string rev = A; 30 | reverse(rev.begin(), rev.end()); 31 | vector> dp(n,vector (n,-1)); 32 | return check(0,0,n,A,rev,dp); 33 | } 34 | }; 35 | 36 | 37 | //{ Driver Code Starts. 38 | 39 | int32_t main() 40 | { 41 | int t; cin >> t; 42 | while (t--) 43 | { 44 | string s; cin >> s; 45 | Solution ob; 46 | cout << ob.longestPalinSubseq(s) << endl; 47 | } 48 | } 49 | // Contributed By: Pranay Bansal 50 | 51 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/07-2023(july)/20-july.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | char nonrepeatingCharacter(string S) { 11 | int cnt[26] = {0}; 12 | 13 | for (auto ch : S) { 14 | cnt[ch - 'a']++; 15 | } 16 | 17 | for (auto ch : S) { 18 | if (cnt[ch - 'a'] == 1) { 19 | return ch; 20 | } 21 | } 22 | 23 | return '$'; 24 | } 25 | }; 26 | 27 | 28 | //{ Driver Code Starts. 29 | 30 | int main() { 31 | 32 | int T; 33 | cin >> T; 34 | 35 | while(T--) 36 | { 37 | 38 | string S; 39 | cin >> S; 40 | Solution obj; 41 | char ans = obj.nonrepeatingCharacter(S); 42 | 43 | if(ans != '$') 44 | cout << ans; 45 | else cout << "-1"; 46 | 47 | cout << endl; 48 | 49 | } 50 | 51 | return 0; 52 | } 53 | 54 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/07-2023(july)/readme.md: -------------------------------------------------------------------------------- 1 | ## JULY - 2023 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **July 2023**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2023/08-2023(aug)/04-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution{ 11 | public: 12 | void Reverse(stack &St){ 13 | queue temp; 14 | while(!St.empty()){ 15 | temp.push(St.top()); 16 | St.pop(); 17 | } 18 | while(!temp.empty()){ 19 | St.push(temp.front()); 20 | temp.pop(); 21 | } 22 | } 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | 28 | 29 | int main(){ 30 | int T; 31 | cin>>T; 32 | while(T--){ 33 | int N; 34 | cin>>N; 35 | stack St; 36 | for(int i=0;i>x; 39 | St.push(x); 40 | } 41 | Solution ob; 42 | ob.Reverse(St); 43 | vectorres; 44 | while(St.size()) 45 | { 46 | res.push_back(St.top()); 47 | St.pop(); 48 | } 49 | for(int i = res.size()-1;i>=0;i--) 50 | { 51 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | long long findMinDiff(vector a, long long n, long long m){ 10 | sort(a.begin(),a.end()); 11 | long long minDiff = a[m-1] - a[0]; 12 | for(int i = 1; i<=n-m;++i){ 13 | minDiff = min(minDiff, a[m+i-1] - a[i]); 14 | } 15 | 16 | return minDiff; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | int main() { 23 | long long t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | long long n; 28 | cin>>n; 29 | vector a; 30 | long long x; 31 | for(long long i=0;i>x; 34 | a.push_back(x); 35 | } 36 | 37 | long long m; 38 | cin>>m; 39 | Solution ob; 40 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | vector out; 10 | 11 | void check(string &s, int i, int &n){ 12 | if(i == n){ 13 | out.push_back(s); 14 | return; 15 | } 16 | 17 | for(int j = i; j < n; ++j){ 18 | swap(s[i], s[j]); 19 | check(s, i+1, n); 20 | swap(s[i], s[j]); 21 | } 22 | } 23 | 24 | vector permutation(string s){ 25 | int n = s.size(); 26 | check(s, 0, n); 27 | sort(out.begin(), out.end()); 28 | return out; 29 | } 30 | }; 31 | 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() 36 | { 37 | int T; 38 | cin>>T; 39 | while(T--) 40 | { 41 | string S; 42 | cin>>S; 43 | Solution ob; 44 | vector vec = ob.permutation(S); 45 | for(string s : vec){ 46 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | long long int largestPrimeFactor(int n){ 10 | int out = 2; 11 | int checkUpto = sqrt(n); 12 | for(int i = 2; i <= checkUpto;++i){ 13 | while(n%i == 0){ 14 | n = n/i; 15 | out = max(out,i); 16 | } 17 | } 18 | out = max(out, n); 19 | return out; 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | int main() 26 | { 27 | int t; 28 | cin>>t; 29 | while(t--) 30 | { 31 | int N; 32 | cin>>N; 33 | Solution ob; 34 | cout << ob.largestPrimeFactor(N) << endl; 35 | } 36 | return 0; 37 | } 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/10-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | const int mod=1e9+7; 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution 9 | { 10 | public: 11 | int lcs(int n, int m, string s1, string s2) 12 | { 13 | int dp[n+1][m+1]; 14 | for(int i = 0; i<=n;++i) 15 | dp[i][0] = 0; 16 | 17 | for(int i = 0; i<=m;++i) 18 | dp[0][i] = 0; 19 | 20 | for(int i = 1; i<=n;++i){ 21 | for(int j = 1; j<=m;++j){ 22 | if(s1[i-1] == s2[j-1]) 23 | dp[i][j] = 1 + dp[i-1][j-1]; 24 | else 25 | dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 26 | } 27 | } 28 | return dp[n][m]; 29 | } 30 | }; 31 | 32 | 33 | //{ Driver Code Starts. 34 | int main() 35 | { 36 | int t,n,m; 37 | cin>>t; 38 | while(t--) 39 | { 40 | cin>>n>>m; // Take size of both the strings as input 41 | string s1,s2; 42 | cin>>s1>>s2; // Take both the string as input 43 | Solution ob; 44 | cout << ob.lcs(n, m, s1, s2) << endl; 45 | } 46 | return 0; 47 | } 48 | 49 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/11 -aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | long long int count(int coins[], int N, int sum) { 10 | long long int dp[N][sum + 1]; 11 | 12 | for (int i = 0; i <= sum; ++i) 13 | dp[0][i] = (i % coins[0] == 0); 14 | 15 | for (int i = 1; i < N; ++i) { 16 | for (int s = 0; s <= sum; ++s) { 17 | long long int nottake = dp[i - 1][s]; 18 | long long int take = 0; 19 | 20 | if (s - coins[i] >= 0) 21 | take = dp[i][s - coins[i]]; 22 | 23 | dp[i][s] = take + nottake; 24 | } 25 | } 26 | 27 | return dp[N - 1][sum]; 28 | } 29 | }; 30 | 31 | 32 | //{ Driver Code Starts. 33 | int main() { 34 | int t; 35 | cin >> t; 36 | while (t--) { 37 | int sum, N; 38 | cin >> sum >> N; 39 | int coins[N]; 40 | for (int i = 0; i < N; i++) cin >> coins[i]; 41 | Solution ob; 42 | cout << ob.count(coins, N, sum) << endl; 43 | } 44 | 45 | return 0; 46 | } 47 | 48 | 49 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/12-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution 8 | { 9 | public: 10 | int longestSubsequence(int n, int a[]) 11 | { 12 | vector dp; 13 | 14 | dp.push_back(a[0]); 15 | for(int i = 1;idp.back()) 17 | dp.push_back(a[i]); 18 | else{ 19 | auto it = lower_bound(dp.begin(),dp.end(),a[i]) - dp.begin(); 20 | dp[it] = a[i]; 21 | } 22 | } 23 | 24 | return dp.size(); 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | int main() 31 | { 32 | //taking total testcases 33 | int t,n; 34 | cin>>t; 35 | while(t--) 36 | { 37 | //taking size of array 38 | cin>>n; 39 | int a[n]; 40 | 41 | //inserting elements to the array 42 | for(int i=0;i>a[i]; 44 | Solution ob; 45 | //calling method longestSubsequence() 46 | cout << ob.longestSubsequence(n, a) << endl; 47 | } 48 | } 49 | 50 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/13-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | 9 | class Solution { 10 | public: 11 | int MOD = 1e9+7; 12 | int nthFibonacci(int n){ 13 | int secondLast = 0; 14 | int last = 1; 15 | int curr = 1; 16 | 17 | n = n-1; 18 | while(n--){ 19 | curr = (last +secondLast)%MOD; 20 | secondLast = last; 21 | last = curr; 22 | } 23 | 24 | return curr; 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | int main() { 31 | int t; 32 | cin >> t; 33 | while (t--) { 34 | int n; 35 | cin >> n; 36 | Solution ob; 37 | cout << ob.nthFibonacci(n) << endl; 38 | } 39 | return 0; 40 | } 41 | 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/15-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int maxOnes(int a[], int n) 10 | { 11 | int zero = 0, one = 0, max_flips = 0; 12 | 13 | for (int i = 0; i < n; ++i) { 14 | if (a[i] == 0) 15 | zero++; 16 | else{ 17 | zero--; 18 | one++; 19 | } 20 | 21 | if (zero < 0) 22 | zero = 0; 23 | 24 | max_flips = max(zero, max_flips); 25 | } 26 | return max_flips + one; 27 | } 28 | }; 29 | 30 | 31 | //{ Driver Code Starts. 32 | int main() 33 | { 34 | int t; cin>>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | int a[n+5]; 40 | for(int i=0;i>a[i]; 42 | Solution ob; 43 | cout<< ob.maxOnes(a, n) < 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int MOD = 1e9 + 7; 10 | int findCatalan(int n) 11 | { 12 | long long c[n + 1] = {0}; 13 | c[0] = c[1] = 1; 14 | 15 | for (int i = 2; i <= n; ++i) { 16 | for (int j = 0; j < i; ++j) { 17 | c[i] = (c[i] + (c[j] * c[i - 1 - j])) % MOD; 18 | } 19 | } 20 | 21 | return (int)c[n]; 22 | } 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | int main() 28 | { 29 | int t; 30 | cin>>t; 31 | while(t--) { 32 | 33 | int n; 34 | cin>>n; 35 | Solution obj; 36 | cout<< obj.findCatalan(n) <<"\n"; 37 | } 38 | return 0; 39 | } 40 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/22-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution 8 | { 9 | public: 10 | int findMinOpeartion(vector>a, int n) 11 | { 12 | long long sumRow, sumCol; 13 | long long maxRow=0, maxCol=0, total = 0; 14 | 15 | for(int i=0;i>t; 38 | while(t--) 39 | { 40 | int n; 41 | cin>>n; 42 | vector > matrix (n,vector(n)); 43 | for(int i=0;i>matrix[i][j]; 46 | Solution ob; 47 | cout << ob.findMinOpeartion(matrix, n) << endl; 48 | } 49 | return 0; 50 | } 51 | 52 | 53 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/25-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | 3 | #include 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution{ 10 | public: 11 | int isPalindrome(string S) 12 | { 13 | int i = 0, j = S.size() - 1; 14 | while(i < j){ 15 | if(S[i] != S[j]) 16 | return false; 17 | ++i; 18 | --j; 19 | } 20 | return true; 21 | } 22 | 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | 28 | int main() 29 | { 30 | ios_base::sync_with_stdio(0); 31 | cin.tie(NULL); 32 | cout.tie(NULL); 33 | 34 | int t; 35 | cin >> t; 36 | while(t--) 37 | { 38 | string s; 39 | cin >> s; 40 | 41 | Solution ob; 42 | 43 | cout << ob.isPalindrome(s) << "\n"; 44 | } 45 | 46 | return 0; 47 | } 48 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/26-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int longestKSubstr(string s, int k) { 12 | 13 | unordered_map cnt; 14 | int i = 0, n = s.size(); 15 | int c = 0, out = -1; 16 | 17 | for (int j = 0; j < n; ++j) { 18 | if (cnt[s[j]] == 0) 19 | ++c; 20 | 21 | ++cnt[s[j]]; 22 | 23 | while (c > k && i < j) { 24 | --cnt[s[i]]; 25 | if (cnt[s[i]] == 0) 26 | --c; 27 | ++i; 28 | } 29 | 30 | if (c == k) 31 | out = max(out, j - i + 1); 32 | } 33 | 34 | return out; 35 | } 36 | }; 37 | 38 | 39 | 40 | //{ Driver Code Starts. 41 | int main() { 42 | int t; 43 | cin >> t; 44 | while (t--) { 45 | string s; 46 | cin >> s; 47 | int k; 48 | cin >> k; 49 | Solution ob; 50 | cout << ob.longestKSubstr(s, k) << endl; 51 | } 52 | } 53 | 54 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/27-aug.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | 5 | #include 6 | using namespace std; 7 | 8 | 9 | 10 | // } Driver Code Ends 11 | 12 | class Solution 13 | { 14 | public: 15 | string reverseWord(string str) 16 | { 17 | int i = 0, j = str.size()-1; 18 | while(i < j){ 19 | swap(str[i], str[j]); 20 | ++i; 21 | --j; 22 | } 23 | 24 | return str; 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | 31 | int main() { 32 | 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | string s; 38 | cin >> s; 39 | Solution ob; 40 | cout << ob.reverseWord(s) << endl; 41 | } 42 | return 0; 43 | 44 | } 45 | 46 | 47 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/08-2023(aug)/readme.md: -------------------------------------------------------------------------------- 1 | ## August - 2023 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **August 2023**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2023/09-2023(sep)/05-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | vector> printGraph(int V, vector>edges) { 10 | vector> adjList(V); 11 | for(auto edge: edges){ 12 | int u = edge.first; 13 | int v = edge.second; 14 | adjList[u].push_back(v); 15 | adjList[v].push_back(u); 16 | } 17 | return adjList; 18 | } 19 | }; 20 | 21 | 22 | //{ Driver Code Starts. 23 | int main() { 24 | int tc; 25 | cin >> tc; 26 | while (tc--) { 27 | int V, E; 28 | cin >> V >> E; 29 | vector>edges; 30 | for (int i = 0; i < E; i++) { 31 | int u, v; 32 | cin >> u >> v; 33 | edges.push_back({u,v}); 34 | } 35 | Solution obj; 36 | vector> adj = obj.printGraph(V, edges); 37 | for(int i=0;i 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution{ 11 | public: 12 | bool isLucky(int n) { 13 | int cnt = 2; 14 | while(cnt <= n){ 15 | if(n % cnt == 0) 16 | return false; 17 | 18 | n -= n/cnt; 19 | ++cnt; 20 | } 21 | 22 | return true; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | signed main(){ 29 | int T; 30 | cin>>T; 31 | while(T--){ 32 | int n; 33 | cin>>n; 34 | Solution obj; 35 | //calling isLucky() function 36 | if(obj.isLucky(n)) 37 | cout<<"1\n";//printing "1" if isLucky() returns true 38 | else 39 | cout<<"0\n";//printing "0" if isLucky() returns false 40 | } 41 | 42 | } 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/09-2023(sep)/12-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int isPerfectNumber(long long n) { 10 | if(n == 1) 11 | return 0; 12 | long long sum = 1; 13 | for(int i = 2; i<=sqrt(n) ; ++i){ 14 | if(n%i == 0){ 15 | sum += i; 16 | if(n/i != i) 17 | sum += n/i; 18 | } 19 | } 20 | return sum == n; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | int main() { 27 | int t; 28 | cin >> t; 29 | while (t--) { 30 | long long N; 31 | 32 | cin>>N; 33 | 34 | Solution ob; 35 | cout << ob.isPerfectNumber(N) << endl; 36 | } 37 | return 0; 38 | } 39 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/09-2023(sep)/13-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | // User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | string findLargest(int N, int S){ 13 | if((!S & N > 1) || N*9 < S) 14 | return "-1"; 15 | string out; 16 | for(int i = 0; i=9){ 18 | out += '9'; 19 | S-=9; 20 | }else{ 21 | if(S){ 22 | out += to_string(S); 23 | S = 0; 24 | } 25 | else 26 | out += '0'; 27 | } 28 | } 29 | return out; 30 | } 31 | }; 32 | 33 | 34 | //{ Driver Code Starts. 35 | 36 | int main(){ 37 | int t; 38 | cin>>t; 39 | while(t--){ 40 | int N, S; 41 | cin>>N>>S; 42 | 43 | Solution ob; 44 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int mod = 1e9 + 7; 10 | 11 | long long solve(int n, int i, vector& dp) { 12 | if (i == n) 13 | return 1; 14 | 15 | if (dp[i] != -1) 16 | return dp[i]; 17 | 18 | long long cnt = 0; 19 | for (int d = 1; d <= 3; ++d) { 20 | if (i + d <= n) 21 | cnt = (cnt + solve(n, i + d, dp)) % mod; 22 | } 23 | 24 | return dp[i] = cnt; 25 | } 26 | 27 | long long countWays(int n) { 28 | vector dp(n, -1); 29 | return solve(n, 0, dp); 30 | } 31 | }; 32 | 33 | 34 | //{ Driver Code Starts. 35 | int main() 36 | { 37 | //taking testcases 38 | int t; 39 | cin >> t; 40 | 41 | while(t--) 42 | { 43 | //taking number of steps in stair 44 | int n; 45 | cin>>n; 46 | Solution ob; 47 | //calling function countWays() 48 | cout << ob.countWays(n) << endl; 49 | } 50 | 51 | return 0; 52 | 53 | } 54 | 55 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/09-2023(sep)/17-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial function template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution 10 | { 11 | public: 12 | vector printFibb(int n) 13 | { 14 | if(n<=2) 15 | return vector(n,1); 16 | 17 | vector out(n); 18 | out[0] = out[1] = 1; 19 | for(int i = 2;i >t; 33 | while(t--) 34 | { 35 | //taking number of elements 36 | int n; 37 | cin>>n; 38 | Solution obj; 39 | //calling function printFibb() 40 | vector ans = obj.printFibb(n); 41 | 42 | //printing the elements of vector 43 | for(long long i:ans)cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution{ 11 | public: 12 | bool isPowerofTwo(long long n){ 13 | return n && ((n&(n-1))==0); 14 | } 15 | }; 16 | 17 | 18 | //{ Driver Code Starts. 19 | 20 | // Driver code 21 | int main() 22 | { 23 | 24 | int t; 25 | cin>>t;//testcases 26 | 27 | for(int i=0;i>n; 31 | 32 | Solution ob; 33 | if(ob.isPowerofTwo(n))//Now, if log2 produces an integer not decimal then we are sure raising 2 to this value 34 | cout<<"YES"< 6 | using namespace std; 7 | 8 | 9 | // } Driver Code Ends 10 | 11 | class Solution 12 | { 13 | public: 14 | unsigned int getFirstSetBit(int n) 15 | { 16 | return 1 + log2(n & ~ (n - 1)); 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | 23 | // Driver code 24 | int main() 25 | { 26 | int t; 27 | cin>>t; // testcases 28 | while(t--) 29 | { 30 | int n; 31 | cin>>n; //input n 32 | Solution ob; 33 | printf("%u\n", ob.getFirstSetBit(n)); // function to get answer 34 | } 35 | return 0; 36 | } 37 | 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/09-2023(sep)/20-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution 8 | { 9 | public: 10 | vector rotate (int n, int d) 11 | { 12 | int mask_16 = (1<<16) - 1; 13 | d = d%16; 14 | 15 | vector out(2); 16 | out[0] = (n<>(16-d))); 17 | out[0] = out[0] & mask_16; 18 | 19 | out[1] = (n>>d|(n<<(16-d))); 20 | out[1] = out[1] & mask_16; 21 | 22 | return out; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | int main() 29 | { 30 | int t; cin >> t; 31 | while (t--) 32 | { 33 | 34 | int n, d; cin >> n >> d; 35 | Solution ob; 36 | vector res = ob.rotate (n, d); 37 | cout << res[0] << endl << res[1] << endl; 38 | } 39 | } 40 | // Contributed By: Pranay Bansal 41 | 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/09-2023(sep)/21-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | typedef long long int ll; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int FindMaxSum(int arr[], int n) { 11 | vector dp(n, 0); 12 | int out = 0; 13 | 14 | for (int i = 0; i < n; ++i) { 15 | if (i >= 2) 16 | dp[i] = dp[i - 2]; 17 | if (i >= 3) 18 | dp[i] = max(dp[i], dp[i - 3]); 19 | 20 | dp[i] += arr[i]; 21 | out = max(out, dp[i]); 22 | } 23 | 24 | return out; 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | int main() 31 | { 32 | //taking total testcases 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | //taking number of houses 38 | int n; 39 | cin>>n; 40 | int a[n]; 41 | 42 | //inserting money of each house in the array 43 | for(int i=0;i>a[i]; 45 | Solution ob; 46 | //calling function FindMaxSum() 47 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution{ 9 | public: 10 | int equilibriumPoint(long long a[], int n) { 11 | long long sum = 0, preSum = 0; 12 | for(int i = 0; i < n; ++i) 13 | sum += a[i]; 14 | 15 | for(int i = 0; i < n; ++i){ 16 | sum -= a[i]; 17 | if(sum == preSum) 18 | return i+1; 19 | preSum += a[i]; 20 | } 21 | return -1; 22 | } 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | 28 | 29 | int main() { 30 | 31 | long long t; 32 | 33 | //taking testcases 34 | cin >> t; 35 | 36 | while (t--) { 37 | long long n; 38 | 39 | //taking input n 40 | cin >> n; 41 | long long a[n]; 42 | 43 | //adding elements to the array 44 | for (long long i = 0; i < n; i++) { 45 | cin >> a[i]; 46 | } 47 | 48 | Solution ob; 49 | 50 | //calling equilibriumPoint() function 51 | cout << ob.equilibriumPoint(a, n) << endl; 52 | } 53 | return 0; 54 | } 55 | 56 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/09-2023(sep)/24-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | vector duplicates(int arr[], int n) { 10 | int cnt[n] = {0}; 11 | for(int i = 0; i out; 15 | for(int i = 0; i 1) 17 | out.push_back(i); 18 | 19 | if(out.size()) 20 | return out; 21 | 22 | return {-1}; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | int main() { 29 | int t; 30 | cin >> t; 31 | while (t-- > 0) { 32 | int n; 33 | cin >> n; 34 | int a[n]; 35 | for (int i = 0; i < n; i++) cin >> a[i]; 36 | Solution obj; 37 | vector ans = obj.duplicates(a, n); 38 | for (int i : ans) cout << i << ' '; 39 | cout << endl; 40 | } 41 | return 0; 42 | } 43 | 44 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/09-2023(sep)/27-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution{ 9 | public: 10 | vector printClosest(int arr[], int brr[], int n, int m, int x) { 11 | vector out; 12 | int nin = INT_MAX; 13 | 14 | int a = 0, b = m - 1; 15 | while(a < n && b >= 0){ 16 | int sum = arr[a] + brr[b]; 17 | if(abs(sum - x) < nin){ 18 | out = {arr[a], brr[b]}; 19 | nin = abs(sum - x); 20 | } 21 | if(sum > x) 22 | --b; 23 | else 24 | ++a; 25 | } 26 | return out; 27 | } 28 | }; 29 | 30 | 31 | //{ Driver Code Starts. 32 | 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | int m,n,x; 38 | while(t--) 39 | { 40 | cin>>n>>m; 41 | int a[n],b[m]; 42 | 43 | for(int i=0;i>a[i]; 45 | 46 | for(int i=0;i>b[i]; 48 | 49 | cin>>x; 50 | 51 | vector ans; 52 | Solution ob; 53 | ans = ob.printClosest(a, b, n, m, x); 54 | cout << abs(ans[0] + ans[1] - x) << endl; 55 | 56 | } 57 | return 0; 58 | } 59 | 60 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/09-2023(sep)/28-sep.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | // #include 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution{ 10 | public: 11 | void convertToWave(int n, vector& arr){ 12 | for(int i = 0; i>t; //Input testcases 24 | while(t--) //While testcases exist 25 | { 26 | cin>>n; //input size of array 27 | vector a(n); //declare vector of size n 28 | for(int i=0;i>a[i]; //input elements of array 30 | sort(a.begin(),a.end()); 31 | Solution ob; 32 | ob.convertToWave(n, a); 33 | 34 | for(int i=0;i 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int mod = 1e9 + 7; 11 | 12 | int distinctSubsequences(string s) { 13 | int n = s.size(); 14 | 15 | vector last(26, -1); 16 | long long dp[n + 1]; 17 | dp[0] = 1; 18 | 19 | for (int i = 1; i <= n; i++) { 20 | dp[i] = dp[i - 1] * 2; 21 | int lastOccur = last[s[i - 1] - 'a']; 22 | 23 | if (lastOccur != -1) { 24 | dp[i] -= dp[lastOccur]; 25 | if (dp[i] < 0) 26 | dp[i] += mod; 27 | } 28 | dp[i] %= mod; 29 | last[s[i - 1] - 'a'] = i - 1; 30 | } 31 | return dp[n]; 32 | } 33 | }; 34 | 35 | 36 | 37 | //{ Driver Code Starts. 38 | 39 | int main() 40 | { 41 | 42 | 43 | ios_base::sync_with_stdio(0); 44 | cin.tie(NULL); 45 | cout.tie(NULL); 46 | 47 | int t; 48 | cin >> t; 49 | while(t--) 50 | { 51 | string s; 52 | cin >> s; 53 | 54 | Solution ob; 55 | cout << ob.distinctSubsequences(s) << "\n"; 56 | } 57 | 58 | return 0; 59 | } 60 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/03-oct.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | string colName(long long int n) { 10 | string out; 11 | vector code(26); 12 | for (int i = 1; i < 26; ++i) 13 | code[i] = (char)('A' + i - 1); 14 | code[0] = 'Z'; 15 | 16 | while (n) { 17 | out += code[n % 26]; 18 | if (n % 26 == 0) 19 | --n; 20 | n /= 26; 21 | } 22 | 23 | reverse(out.begin(), out.end()); 24 | return out; 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | int main() 31 | { 32 | int t; cin >> t; 33 | while (t--) 34 | { 35 | long long int n; cin >> n; 36 | Solution ob; 37 | cout << ob.colName (n) << '\n'; 38 | } 39 | } 40 | 41 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/04-oct.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | int romanToDecimal(string &str) { 13 | unordered_map mp; 14 | mp['I'] = 1; 15 | mp['V'] = 5; 16 | mp['X'] = 10; 17 | mp['L'] = 50; 18 | mp['C'] = 100; 19 | mp['D'] = 500; 20 | mp['M'] = 1000; 21 | 22 | int n = str.size(); 23 | 24 | int out = 0; 25 | for (int i = 0; i < n; i++) { 26 | if (i + 1 < n && mp[str[i]] < mp[str[i + 1]]) 27 | out -= mp[str[i]]; 28 | else 29 | out += mp[str[i]]; 30 | } 31 | 32 | return out; 33 | } 34 | }; 35 | 36 | 37 | //{ Driver Code Starts. 38 | 39 | int main() { 40 | int t; 41 | cin >> t; 42 | while (t--) { 43 | string s; 44 | cin >> s; 45 | Solution ob; 46 | cout << ob.romanToDecimal(s) << endl; 47 | } 48 | } 49 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/05-oct.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | long long int countAtMostK(string s, int k) { 13 | long long int cnt = 0; 14 | int freq[26] = {0}; 15 | int i = 0, diff = 0; 16 | 17 | for (int j = 0; j < s.size(); ++j) { 18 | if (!freq[s[j] - 'a']) 19 | ++diff; 20 | ++freq[s[j] - 'a']; 21 | 22 | while (diff > k && i <= j) { 23 | --freq[s[i] - 'a']; 24 | if (!freq[s[i] - 'a']) 25 | --diff; 26 | ++i; 27 | } 28 | cnt += j - i + 1; 29 | } 30 | 31 | return cnt; 32 | } 33 | 34 | long long int substrCount(string s, int k) { 35 | return countAtMostK(s, k) - countAtMostK(s, k - 1); 36 | } 37 | }; 38 | 39 | 40 | 41 | //{ Driver Code Starts. 42 | 43 | 44 | int main() 45 | { 46 | int t; cin >> t; 47 | while (t--) 48 | { 49 | string s; cin >> s; 50 | int k; cin >> k; 51 | Solution ob; 52 | cout < 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int isPossible(int N, int arr[]) { 12 | int sum = 0; 13 | for(int i=0; i> t; 25 | while (t--) { 26 | int N; 27 | cin >> N; 28 | int arr[N]; 29 | for (int i = 0; i < N; i++) cin >> arr[i]; 30 | 31 | Solution ob; 32 | cout << ob.isPossible(N, arr) << endl; 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/21-oct.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution 8 | { 9 | public: 10 | long long sumOfDivisors(int N) 11 | { 12 | long long out = 0; 13 | for(int i = 1; i<=N;++i){ 14 | out += (N/i) * i; 15 | } 16 | return out; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | int main() 23 | { 24 | int t; 25 | cin >> t; 26 | while (t--) 27 | { 28 | int N; 29 | cin>>N; 30 | Solution ob; 31 | long long ans = ob.sumOfDivisors(N); 32 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int mod = 1e9 + 7; 11 | 12 | long long modInv(long long a, long long b) { 13 | return 1 < a ? b - modInv(b % a, a) * b / a : 1; 14 | } 15 | 16 | long long numberOfPaths(int m, int n) { 17 | long long out = 1; 18 | 19 | for (int i = 0; i < m - 1; i++) { 20 | long long inverse = modInv(i + 1, mod); 21 | out = (out * (i + n)) % mod; 22 | out = (out * inverse) % mod; 23 | } 24 | 25 | return out; 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | 32 | 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | int N, M; 40 | cin>>M>>N; 41 | Solution ob; 42 | cout << ob.numberOfPaths(M, N)< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | int maxSumIS(int arr[], int n) 10 | { 11 | vectordp(arr, arr+n); 12 | 13 | for(int i=1;i> t; 33 | while (t--) 34 | { 35 | int n; 36 | cin >> n; 37 | 38 | int a[n]; 39 | 40 | for(int i = 0; i < n; i++) 41 | cin >> a[i]; 42 | 43 | 44 | 45 | Solution ob; 46 | cout << ob.maxSumIS(a, n) << "\n"; 47 | 48 | } 49 | return 0; 50 | } 51 | 52 | 53 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/25-oct.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int knapSack(int N, int W, int val[], int wt[]) { 12 | vector dp(W + 1, 0); 13 | for (int i = 0; i < N; i++) { 14 | for (int w = wt[i]; w <= W; w++) { 15 | dp[w] = max(dp[w], dp[w - wt[i]] + val[i]); 16 | } 17 | } 18 | return dp[W]; 19 | } 20 | }; 21 | 22 | 23 | //{ Driver Code Starts. 24 | 25 | int main(){ 26 | int t; 27 | cin>>t; 28 | while(t--){ 29 | int N, W; 30 | cin>>N>>W; 31 | int val[N], wt[N]; 32 | for(int i = 0;i < N;i++) 33 | cin>>val[i]; 34 | for(int i = 0;i < N;i++) 35 | cin>>wt[i]; 36 | 37 | Solution ob; 38 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution 8 | { 9 | public: 10 | int minOperation(int n) 11 | { 12 | int out = -1; 13 | while(n){ 14 | if(n%2) 15 | ++out; 16 | n /= 2; 17 | ++out; 18 | } 19 | 20 | return out; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | int n; 33 | cin>>n; 34 | Solution ob; 35 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int lcs(string &S, string &revS, int i, int j, vector > &dp) { 11 | if (i == -1 || j == -1) 12 | return 0; 13 | 14 | if (dp[i][j] != -1) 15 | return dp[i][j]; 16 | 17 | if (S[i] == revS[j]) 18 | return dp[i][j] = 1 + lcs(S, revS, i - 1, j - 1, dp); 19 | 20 | int checkS = lcs(S, revS, i - 1, j, dp); 21 | int checkRevS = lcs(S, revS, i, j - 1, dp); 22 | return dp[i][j] = max(checkS, checkRevS); 23 | } 24 | 25 | int minimumNumberOfDeletions(string S) { 26 | int n = S.size(); 27 | string revS = S; 28 | reverse(revS.begin(), revS.end()); 29 | vector > dp(n, vector(n, -1)); 30 | 31 | return n - lcs(S, revS, n - 1, n - 1, dp); 32 | } 33 | }; 34 | 35 | 36 | //{ Driver Code Starts. 37 | int main(){ 38 | int t; 39 | cin >> t; 40 | while(t--){ 41 | string S; 42 | cin >> S; 43 | Solution obj; 44 | cout << obj.minimumNumberOfDeletions(S) << endl; 45 | } 46 | return 0; 47 | } 48 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/28-oct.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int is_bleak(int n) { 10 | for(int i = 1; i <= log2(n); ++i) { 11 | int x = n - i; 12 | int setBits = __builtin_popcount(x); 13 | if(setBits + x == n) 14 | return 0; 15 | } 16 | return 1; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | int main(){ 23 | int T; 24 | cin >> T; 25 | while(T--) 26 | { 27 | int n; 28 | cin >> n; 29 | Solution ob; 30 | int ans = ob.is_bleak(n); 31 | cout << ans << "\n"; 32 | } 33 | return 0; 34 | } 35 | 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/29-oct.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution 11 | { 12 | public: 13 | bool checkKthBit(int n, int k) 14 | { 15 | int mask = 1 << k; 16 | return n & mask; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | 23 | // Driver Code 24 | int main() 25 | { 26 | int t; 27 | cin>>t;//taking testcases 28 | while(t--) 29 | { 30 | long long n; 31 | cin>>n;//input n 32 | int k; 33 | cin>>k;//bit number k 34 | Solution obj; 35 | if(obj.checkKthBit(n, k)) 36 | cout << "Yes" << endl; 37 | else 38 | cout << "No" << endl; 39 | } 40 | return 0; 41 | } 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/30-oct.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // An efficient C++ program to compute 3 | // sum of bitwise OR of all pairs 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | long long int sumXOR(int arr[], int n) { 13 | long long int out = 0; 14 | for (int i = 0; i < 32; ++i) { 15 | long long int ones = 0; 16 | int mask = 1 << i; 17 | for (int j = 0; j < n; ++j) { 18 | if ((arr[j] & mask) != 0) { 19 | ones++; 20 | } 21 | } 22 | out += (ones * (n - ones) * mask); 23 | } 24 | return out; 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | 31 | 32 | int main() 33 | { 34 | int t; 35 | cin>>t; 36 | while(t--) 37 | { 38 | int n ; 39 | cin>>n; 40 | int arr[n+1]; 41 | for( int i=0;i>arr[i]; 43 | Solution ob; 44 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | void pushZerosToEnd(int arr[], int n) { 10 | int i = 0; 11 | for(int j = 0; j < n; ++j){ 12 | if(arr[j]){ 13 | arr[i++] = arr[j]; 14 | } 15 | } 16 | 17 | for(; i < n;++i){ 18 | arr[i] = 0; 19 | } 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | int main() { 26 | int t; 27 | cin >> t; 28 | while (t--) { 29 | int n, i; 30 | cin >> n; 31 | int arr[n]; 32 | for (i = 0; i < n; i++) { 33 | cin >> arr[i]; 34 | } 35 | Solution ob; 36 | ob.pushZerosToEnd(arr, n); 37 | for (i = 0; i < n; i++) { 38 | cout << arr[i] << " "; 39 | } 40 | cout << "\n"; 41 | } 42 | return 0; 43 | } 44 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/10-2023(oct)/readme.md: -------------------------------------------------------------------------------- 1 | ## October - 2023 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **October 2023**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2023/11-2023(nov)/01-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | void frequencyCount(vector& arr, int N, int P) { 10 | int offset = P + 1; 11 | 12 | for (auto i : arr) { 13 | int val = (i - 1) % offset; 14 | if (val < N) { 15 | arr[val] += offset; 16 | } 17 | } 18 | 19 | for (int i = 0; i < N; ++i) { 20 | arr[i] /= offset; 21 | } 22 | } 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | 28 | int main() 29 | { 30 | long long t; 31 | 32 | //testcases 33 | cin >> t; 34 | 35 | while(t--){ 36 | 37 | int N, P; 38 | //size of array 39 | cin >> N; 40 | 41 | vector arr(N); 42 | 43 | //adding elements to the vector 44 | for(int i = 0; i < N ; i++){ 45 | cin >> arr[i]; 46 | } 47 | cin >> P; 48 | Solution ob; 49 | //calling frequncycount() function 50 | ob.frequencyCount(arr, N, P); 51 | 52 | //printing array elements 53 | for (int i = 0; i < N ; i++) 54 | cout << arr[i] << " "; 55 | cout << endl; 56 | } 57 | return 0; 58 | } 59 | 60 | 61 | 62 | 63 | 64 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/02-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int minDist(int a[], int n, int x, int y) { 10 | int lastX, lastY, out; 11 | lastX = lastY = -1; 12 | out = INT_MAX; 13 | 14 | for(int i = 0; i < n; ++i) { 15 | if(a[i] == x) 16 | lastX = i; 17 | if(a[i] == y) 18 | lastY = i; 19 | 20 | if(lastX != -1 && lastY != -1) 21 | out = min(out, abs(lastX - lastY)); 22 | } 23 | return out == INT_MAX ? -1 : out; 24 | } 25 | }; 26 | 27 | 28 | //{ Driver Code Starts. 29 | int main() { 30 | int t; 31 | cin >> t; 32 | while (t--) { 33 | int n; 34 | cin >> n; 35 | int a[n]; 36 | for (int i = 0; i < n; i++) cin >> a[i]; 37 | int x, y; 38 | cin >> x >> y; 39 | Solution obj; 40 | cout << obj.minDist(a, n, x, y) << endl; 41 | } 42 | return 0; 43 | } 44 | 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/03-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool checkTriplet(int arr[], int n) { 11 | unordered_set s; 12 | 13 | for (int i = 0; i < n; ++i) 14 | s.insert(arr[i] * arr[i]); 15 | 16 | for (auto a : s) { 17 | for (auto b : s) { 18 | if (s.find(a + b) != s.end()) 19 | return true; 20 | } 21 | } 22 | 23 | return false; 24 | } 25 | }; 26 | 27 | 28 | //{ Driver Code Starts. 29 | 30 | int main() { 31 | int t; 32 | cin >> t; 33 | while (t--) { 34 | int n, i; 35 | cin >> n; 36 | int arr[n]; 37 | for (i = 0; i < n; i++) { 38 | cin >> arr[i]; 39 | } 40 | Solution ob; 41 | auto ans = ob.checkTriplet(arr, n); 42 | if (ans) { 43 | cout << "Yes\n"; 44 | } else { 45 | cout << "No\n"; 46 | } 47 | } 48 | return 0; 49 | } 50 | 51 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/04-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int transitionPoint(int arr[], int n) { 10 | int l = 0, r = n - 1; 11 | while (l < r) { 12 | int m = (l + r) / 2; 13 | 14 | if (arr[m] < 1) 15 | l = m + 1; 16 | else 17 | r = m; 18 | } 19 | 20 | return arr[l] ? l : -1; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | int transitionPoint(int arr[], int n); 27 | 28 | int main() { 29 | int t; 30 | cin >> t; 31 | while (t--) { 32 | int n; 33 | cin >> n; 34 | int a[n], i; 35 | for (i = 0; i < n; i++) { 36 | cin >> a[i]; 37 | } 38 | Solution ob; 39 | cout << ob.transitionPoint(a, n) << endl; 40 | } 41 | return 0; 42 | } 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/05-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | vector topK(vector& nums, int k) { 10 | unordered_map mp; 11 | for (auto i : nums) 12 | ++mp[i]; 13 | 14 | priority_queue> pq; 15 | 16 | for (auto itr : mp) { 17 | pq.push({itr.second, itr.first}); 18 | } 19 | 20 | vector out; 21 | while (!pq.empty() && k--) { 22 | out.push_back(pq.top().second); 23 | pq.pop(); 24 | } 25 | 26 | return out; 27 | } 28 | }; 29 | 30 | 31 | //{ Driver Code Starts. 32 | int main() { 33 | int tc; 34 | cin >> tc; 35 | while (tc--) { 36 | int n; 37 | cin >> n; 38 | vector nums(n); 39 | for (auto &i : nums) cin >> i; 40 | int k; 41 | cin >> k; 42 | Solution obj; 43 | vector ans = obj.topK(nums, k); 44 | for (auto i : ans) cout << i << " "; 45 | cout << "\n"; 46 | } 47 | return 0; 48 | } 49 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/07-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | vector sumTriangles(const vector >& matrix, int n) { 10 | vector out(2, 0); 11 | 12 | for (int i = 0; i < n; ++i) { 13 | for (int j = 0; j < n; ++j) { 14 | if (i <= j) 15 | out[0] += matrix[i][j]; 16 | if (i >= j) 17 | out[1] += matrix[i][j]; 18 | } 19 | } 20 | return out; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main() { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | int n; 33 | cin>>n; 34 | vector > matrix(n); 35 | for(int i=0; i>matrix[i][j]; 41 | } 42 | } 43 | Solution ob; 44 | vector result = ob.sumTriangles(matrix,n); 45 | for (int i = 0; i < result.size(); ++i) 46 | cout< 6 | 7 | 8 | using namespace std; 9 | #define MAX_CHARS 256 10 | 11 | // } Driver Code Ends 12 | 13 | class Solution { 14 | public: 15 | bool areIsomorphic(string str1, string str2) { 16 | if (str1.size() != str2.size()) 17 | return false; 18 | 19 | unordered_map mp1, mp2; 20 | for (int i = 0; i < str1.size(); i++) { 21 | mp1[str1[i]] = str2[i]; 22 | mp2[str2[i]] = str1[i]; 23 | } 24 | 25 | for (int i = 0; i < str1.size(); i++) { 26 | if ((mp1[str1[i]] != str2[i]) || (mp2[str2[i]] != str1[i])) 27 | return false; 28 | } 29 | return true; 30 | } 31 | }; 32 | 33 | 34 | //{ Driver Code Starts. 35 | 36 | // Driver program 37 | int main() 38 | { 39 | int t; 40 | cin>>t; 41 | string s1,s2; 42 | while (t--) { 43 | cin>>s1; 44 | cin>>s2; 45 | Solution obj; 46 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool isRotated(string s1, string s2) { 11 | if (s1 == s2.substr(2) + s2.substr(0, 2)) 12 | return true; 13 | if (s1 == s2.substr(s2.size() - 2) + s2.substr(0, s2.size() - 2)) 14 | return true; 15 | 16 | return false; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | 23 | int main() { 24 | 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | string s; 30 | string b; 31 | cin>>s>>b; 32 | Solution obj; 33 | cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | int LCS(string& X, string& Y, int i, int j, vector>& dp) { 13 | if (i < 0 || j < 0) 14 | return 0; 15 | 16 | if (dp[i][j] != -1) 17 | return dp[i][j]; 18 | 19 | if (X[i] == Y[j]) 20 | return dp[i][j] = 1 + LCS(X, Y, i - 1, j - 1, dp); 21 | 22 | return dp[i][j] = max(LCS(X, Y, i, j - 1, dp), LCS(X, Y, i - 1, j, dp)); 23 | } 24 | 25 | int shortestCommonSupersequence(string X, string Y, int m, int n) { 26 | vector> dp(m + 1, vector(n + 1, -1)); 27 | int lcs = LCS(X, Y, m - 1, n - 1, dp); 28 | return m + n - lcs; 29 | } 30 | }; 31 | 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() 36 | { 37 | 38 | int t; 39 | 40 | //taking total testcases 41 | cin >> t; 42 | while(t--){ 43 | string X, Y; 44 | //taking String X and Y 45 | cin >> X >> Y; 46 | 47 | //calling function shortestCommonSupersequence() 48 | Solution obj; 49 | cout << obj.shortestCommonSupersequence(X, Y, X.size(), Y.size())<< endl; 50 | } 51 | return 0; 52 | } 53 | 54 | 55 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/14-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool areRotations(string s1, string s2) { 11 | string s = s1 + s1; 12 | return s.find(s2) != -1; 13 | } 14 | }; 15 | 16 | 17 | //{ Driver Code Starts. 18 | 19 | int main() 20 | { 21 | int t; 22 | cin>>t; 23 | while(t--) 24 | { 25 | string s1; 26 | string s2; 27 | cin>>s1>>s2; 28 | Solution obj; 29 | cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | int distSubSeq(string s) 13 | { 14 | unordered_map last; 15 | int count = 1; 16 | 17 | for (auto ch : s) { 18 | int newCount = 2 * count; 19 | 20 | if (last.find(ch) != last.end()) 21 | newCount -= last[ch]; 22 | 23 | last[ch] = count; 24 | count = newCount; 25 | } 26 | return count; 27 | } 28 | 29 | string betterString(string s1, string s2) { 30 | int distSubSeq1 = distSubSeq(s1); 31 | int distSubSeq2 = distSubSeq(s2); 32 | 33 | return distSubSeq2 > distSubSeq1 ? s2 : s1; 34 | } 35 | }; 36 | 37 | 38 | //{ Driver Code Starts. 39 | 40 | int main() { 41 | int t; 42 | cin >> t; 43 | while (t--) { 44 | string str1, str2; 45 | cin >> str1 >> str2; 46 | Solution obj; 47 | string ans = obj.betterString(str1, str2); 48 | cout << ans << "\n"; 49 | } 50 | } 51 | 52 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/20-nov.cpp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/getlost01/gfg-potd/4d834d62cb6dce281275feb24ad0259d2a16d2a0/2023/11-2023(nov)/20-nov.cpp -------------------------------------------------------------------------------- /2023/11-2023(nov)/24-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int mod = 1e9 + 7; 10 | 11 | vector nthRowOfPascalTriangle(int n) { 12 | vector out(n, 1), prev(n, 1); 13 | for (int i = 1; i < n; ++i) { 14 | for (int j = 1; j < i; ++j) { 15 | out[j] = (prev[j] + prev[j - 1]) % mod; 16 | } 17 | prev = out; 18 | } 19 | return out; 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | 26 | 27 | void printAns(vector &ans) { 28 | for (auto &x : ans) { 29 | cout << x << " "; 30 | } 31 | cout << "\n"; 32 | } 33 | 34 | int main() { 35 | int t; 36 | cin >> t; 37 | while (t--) { 38 | int n; 39 | cin >> n; 40 | Solution ob; 41 | auto ans = ob.nthRowOfPascalTriangle(n); 42 | printAns(ans); 43 | } 44 | return 0; 45 | } 46 | 47 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/25-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | void shuffleArray(int arr[], int n) { 10 | int offset = 1e5; 11 | for (int i = 0; i < n / 2; ++i) { 12 | arr[i * 2] += (arr[i] % offset) * offset; 13 | arr[i * 2 + 1] += (arr[n / 2 + i] % offset) * offset; 14 | } 15 | for (int i = 0; i < n; ++i) { 16 | arr[i] = arr[i] / offset; 17 | } 18 | } 19 | }; 20 | 21 | 22 | //{ Driver Code Starts. 23 | 24 | 25 | int main() 26 | { 27 | 28 | 29 | int t; 30 | cin >> t; 31 | while (t--) 32 | { 33 | int n; 34 | cin>>n; 35 | int a[n] ; 36 | for(int i=0;i>a[i]; 39 | } 40 | 41 | 42 | 43 | 44 | Solution ob; 45 | ob.shuffleArray(a, n); 46 | 47 | for (int i = 0; i < n; i++) 48 | cout << a[i] << " "; 49 | 50 | 51 | 52 | cout << "\n"; 53 | 54 | } 55 | return 0; 56 | } 57 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/26-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | void solve(int n, vector& out) { 12 | out.push_back(n); 13 | if (n <= 0) 14 | return; 15 | solve(n - 5, out); 16 | out.push_back(n); 17 | } 18 | 19 | vector pattern(int N) { 20 | vector out; 21 | solve(N, out); 22 | return out; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | 29 | int main(){ 30 | int t; 31 | cin>>t; 32 | while(t--){ 33 | int N; 34 | cin>>N; 35 | 36 | Solution ob; 37 | vector ans = ob.pattern(N); 38 | for(int u: ans) 39 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int sumOfDependencies(vector adj[], int V) { 12 | int sum = 0; 13 | for (int i = 0; i < V; ++i) { 14 | sum += adj[i].size(); 15 | } 16 | return sum; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | 23 | int main() { 24 | int t; 25 | cin >> t; 26 | while (t--) { 27 | int N, M, x, y; 28 | cin >> N >> M; 29 | vector v[N]; 30 | for (int i = 0; i < M; i++) { 31 | cin >> x >> y; 32 | v[x].push_back(y); 33 | } 34 | Solution ob; 35 | cout << ob.sumOfDependencies(v, N) << "\n"; 36 | } 37 | } 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/29-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int isEulerCircuit(int v, vector adj[]) { 10 | int even = 0, odd = 0; 11 | for (int i = 0; i < v; i++) { 12 | if (adj[i].size() & 1) 13 | ++odd; 14 | else 15 | ++even; 16 | } 17 | 18 | return even == v ? 2 : (odd > 0 && odd == 2); 19 | } 20 | }; 21 | 22 | 23 | //{ Driver Code Starts. 24 | int main(){ 25 | int tc; 26 | cin >> tc; 27 | while(tc--){ 28 | int V, E; 29 | cin >> V >> E; 30 | vectoradj[V]; 31 | for(int i = 0; i < E; i++){ 32 | int u, v; 33 | cin >> u >> v; 34 | adj[u].push_back(v); 35 | adj[v].push_back(u); 36 | } 37 | Solution obj; 38 | int ans = obj.isEulerCircuit(V, adj); 39 | cout << ans <<"\n"; } 40 | return 0; 41 | } 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/30-nov.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial template for c++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int minimumStep(int n) { 12 | int cnt = 0; 13 | while (n > 1) { 14 | if (n % 3 == 0) 15 | n /= 3; 16 | else 17 | n -= 1; 18 | ++cnt; 19 | } 20 | return cnt; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main() 28 | { 29 | int t; 30 | cin>>t; 31 | while(t--) 32 | { 33 | int n; 34 | cin >> n; 35 | Solution ob; 36 | cout << ob.minimumStep(n) << endl; 37 | 38 | } 39 | return 0; 40 | } 41 | 42 | 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/11-2023(nov)/readme.md: -------------------------------------------------------------------------------- 1 | ## November - 2023 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **November 2023**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2023/12-2023(dec)/02-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | //User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | int isRepresentingBST(int arr[], int N) 13 | { 14 | for (int i=0;i>t; 29 | while(t--) 30 | { 31 | int N; 32 | cin >> N; 33 | int arr[N]; 34 | for(int i=0;i>arr[i]; 35 | Solution ob; 36 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int getMinDiff(int arr[], int n, int k) { 12 | sort(arr, arr + n); 13 | int out = arr[n - 1] - arr[0]; 14 | 15 | for (int i = 0; i < n - 1; ++i) { 16 | if (arr[i + 1] - k >= 0) { 17 | int nax = max(arr[i] + k, arr[n - 1] - k); 18 | int nin = min(arr[i + 1] - k, arr[0] + k); 19 | out = min(out, nax - nin); 20 | } 21 | } 22 | return out; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | int main() { 29 | int t; 30 | cin >> t; 31 | while (t--) { 32 | int n, k; 33 | cin >> k; 34 | cin >> n; 35 | int arr[n]; 36 | for (int i = 0; i < n; i++) { 37 | cin >> arr[i]; 38 | } 39 | Solution ob; 40 | auto ans = ob.getMinDiff(arr, n, k); 41 | cout << ans << "\n"; 42 | } 43 | return 0; 44 | } 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/06-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | void cntX(int n, int& X, int& cnt){ 12 | while(n){ 13 | if(n % 10 == X) 14 | ++cnt; 15 | n /= 10; 16 | } 17 | } 18 | 19 | int countX(int L, int R, int X) { 20 | int cnt = 0; 21 | 22 | for(int i = L + 1; i < R; i++) 23 | cntX(i, X, cnt); 24 | 25 | return cnt; 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | int main() { 32 | int t; 33 | cin >> t; 34 | while (t--) { 35 | int L, R, X; 36 | cin >> L >> R >> X; 37 | Solution ob; 38 | int ans = ob.countX(L, R, X); 39 | cout << ans << "\n"; 40 | } 41 | } 42 | 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/07-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution{ 8 | public: 9 | long countSubarrays(int a[], int n, int L, int R) 10 | { 11 | long out = 0, range = 0; 12 | long i = 0; 13 | 14 | for (long j = 0; j < n; ++j) 15 | { 16 | if (a[j] >= L && a[j] <= R) 17 | range = j - i + 1; 18 | else if (a[j] > R) 19 | range = 0, i = j + 1; 20 | 21 | out += range; 22 | } 23 | 24 | return out; 25 | } 26 | }; 27 | 28 | 29 | 30 | //{ Driver Code Starts. 31 | // driver program 32 | int main() 33 | { 34 | int t; 35 | cin >> t; 36 | while(t--) 37 | { 38 | 39 | int n ,l,r; 40 | cin >> n>>l>>r; 41 | int a[n]; 42 | for(int i=0;i> a[i]; 44 | Solution ob; 45 | cout << ob.countSubarrays(a, n, l, r)< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool isprime(int& n) { 11 | if (n == 1) 12 | return false; 13 | 14 | for (int i = 2; i <= sqrt(n); i++) 15 | if (n % i == 0) 16 | return false; 17 | 18 | return true; 19 | } 20 | 21 | int minNumber(int arr[], int N) { 22 | int sum = 0; 23 | for (int i = 0; i < N; i++) 24 | sum += arr[i]; 25 | 26 | int cnt = 0; 27 | while (!isprime(sum)) { 28 | ++cnt; 29 | ++sum; 30 | } 31 | 32 | return cnt; 33 | } 34 | }; 35 | 36 | 37 | //{ Driver Code Starts. 38 | 39 | int main() 40 | { 41 | ios_base::sync_with_stdio(false); 42 | cin.tie(NULL); 43 | int t; 44 | cin>>t; 45 | while(t--) 46 | { 47 | int n; 48 | cin>>n; 49 | int arr[n]; 50 | for(int i=0;i>arr[i]; 52 | Solution obj; 53 | cout << obj.minNumber(arr, n)< 5 | using namespace std; 6 | 7 | 8 | 9 | // } Driver Code Ends 10 | 11 | class Solution { 12 | public: 13 | bool subArrayExists(int arr[], int n) { 14 | int sum = 0; 15 | unordered_set s; 16 | s.insert(0); 17 | 18 | for (int i = 0; i < n; i++) { 19 | sum += arr[i]; 20 | if (s.find(sum) != s.end()) 21 | return true; 22 | s.insert(sum); 23 | } 24 | 25 | return false; 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | // Driver code 32 | int main() 33 | { 34 | int t; 35 | cin>>t; 36 | while(t--) 37 | { 38 | int n; 39 | cin>>n; 40 | int arr[n]; 41 | for(int i=0;i>arr[i]; 43 | Solution obj; 44 | if (obj.subArrayExists(arr, n)) 45 | cout << "Yes\n"; 46 | else 47 | cout << "No\n"; 48 | } 49 | return 0; 50 | } 51 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/11-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | long maximumSumSubarray(int K, vector &Arr , int N){ 10 | long out = 0; 11 | long sum = 0; 12 | for(int i = 0; i < N ; ++i){ 13 | sum += Arr[i]; 14 | if(i >= K) 15 | sum -= Arr[i - K]; 16 | 17 | out = max(out, sum); 18 | } 19 | return out; 20 | } 21 | }; 22 | 23 | 24 | 25 | //{ Driver Code Starts. 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | int N,K; 33 | cin >> N >> K;; 34 | vectorArr; 35 | for(int i=0;i>x; 38 | Arr.push_back(x); 39 | } 40 | Solution ob; 41 | cout << ob.maximumSumSubarray(K,Arr,N) << endl; 42 | } 43 | return 0; 44 | } 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/13-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | 4 | using namespace std; 5 | #define ll long long 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int mod = 1e9 + 7; 12 | 13 | long long countStrings(int n) { 14 | vector dp(n + 1); 15 | dp[0] = 1; 16 | dp[1] = 2; 17 | 18 | for (int i = 2; i <= n; ++i) { 19 | dp[i] = (dp[i - 1] + dp[i - 2]) % mod; 20 | } 21 | 22 | return dp[n]; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | 29 | int main() { 30 | int t; 31 | cin >> t; 32 | while (t--) { 33 | int n; 34 | cin >> n; 35 | Solution ob; 36 | auto ans = ob.countStrings(n); 37 | cout << ans << "\n"; 38 | } 39 | return 0; 40 | } 41 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/14-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | #include 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | long long countWays(int n, int k){ 12 | long mod = 1e9 + 7; 13 | long same = 0; 14 | long diff = k; 15 | long total = same + diff; 16 | 17 | for(int i = 2; i <= n; ++i) { 18 | same = diff * 1; 19 | diff = (total * (k - 1)) % mod; 20 | total = same + diff; 21 | } 22 | return total % mod; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | 29 | int main() 30 | { 31 | 32 | int t; 33 | cin>>t; 34 | while(t--) 35 | { 36 | int n,k; 37 | cin>>n>>k; 38 | Solution ob; 39 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution{ 7 | public: 8 | int nthPoint(int n) 9 | { 10 | long long num1=1, num2=1, mod=1e9+7; 11 | for(int i=1;i> tc; 25 | while(tc--){ 26 | int n; 27 | cin >> n; 28 | Solution ob; 29 | int ans = ob.nthPoint(n); 30 | cout << ans <<"\n"; 31 | } 32 | return 0; 33 | } 34 | // } Driver Code Ends 35 | -------------------------------------------------------------------------------- /2023/12-2023(dec)/16-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | long long int countStr(long long int n); 5 | 6 | int main(){ 7 | int t; 8 | cin >> t; 9 | while(t--){ 10 | long long int n; 11 | cin >> n; 12 | cout << countStr(n) << endl; 13 | } 14 | return 0; 15 | } 16 | // } Driver Code Ends 17 | 18 | long long int countStr(long long int n){ 19 | return 1 + 2 * n + (n * (n - 1)) + ((n * (n - 1)) / 2) + (n * (n - 1) * (n - 2)) / 2; 20 | } -------------------------------------------------------------------------------- /2023/12-2023(dec)/17-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int findMaxSum(int *arr, int n) { 12 | int lastPrev = 0; 13 | int prev = arr[0]; 14 | int curr = 0; 15 | 16 | for (int i = 1; i < n; ++i) { 17 | curr = max(prev, arr[i] + lastPrev); 18 | lastPrev = prev; 19 | prev = curr; 20 | } 21 | 22 | return max(lastPrev, prev); 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | 29 | int main() { 30 | int t; 31 | cin >> t; 32 | while (t--) { 33 | int n; 34 | cin >> n; 35 | int arr[n]; 36 | for (int i = 0; i < n; i++) { 37 | cin >> arr[i]; 38 | } 39 | Solution ob; 40 | auto ans = ob.findMaxSum(arr, n); 41 | cout << ans << "\n"; 42 | } 43 | return 0; 44 | } 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/18-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int gameOfXor(int N , int A[]) { 10 | if (N % 2 == 0) 11 | return 0; 12 | 13 | int XOR = 0; 14 | for (int i = 0; i < N; i += 2) 15 | XOR ^= A[i]; 16 | return XOR; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | int main() { 23 | int t; 24 | cin >> t; 25 | while (t--) { 26 | int N; 27 | 28 | cin>>N; 29 | int A[N]; 30 | for(int i=0 ; i>A[i]; 32 | 33 | Solution ob; 34 | cout << ob.gameOfXor(N,A) << endl; 35 | } 36 | return 0; 37 | } 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/19-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | int posOfRightMostDiffBit(int m, int n) { 13 | if (m == n) 14 | return -1; 15 | 16 | int res = m ^ n; 17 | res = res & (-res); 18 | return log2(res) + 1; 19 | } 20 | }; 21 | 22 | 23 | //{ Driver Code Starts. 24 | 25 | // Driver Code 26 | int main() 27 | { 28 | int t; 29 | cin>>t; //input number of testcases 30 | while(t--) 31 | { 32 | int m,n; 33 | cin>>m>>n; //input m and n 34 | Solution ob; 35 | cout << ob.posOfRightMostDiffBit(m, n)< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int findWinner(int n, int A[]) { 12 | int XOR = 0; 13 | for(int i = 0; i < n; i++) 14 | XOR ^= A[i]; 15 | 16 | if(XOR == 0) 17 | return 1; 18 | 19 | return n % 2 == 0 ? 1 : 2; 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | 26 | int main(){ 27 | int t; 28 | cin>>t; 29 | while(t--){ 30 | int n; 31 | cin>>n; 32 | int A[n]; 33 | for(int i = 0;i < n;i++) 34 | cin>>A[i]; 35 | 36 | Solution ob; 37 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int minCandy(int N, vector &ratings) { 11 | vector left(N, 1), right(N, 1); 12 | 13 | for (int i = 1; i < N; ++i) 14 | if (ratings[i] > ratings[i - 1]) 15 | left[i] += left[i - 1]; 16 | 17 | for (int i = N - 2; i >= 0; i--) 18 | if (ratings[i] > ratings[i + 1]) 19 | right[i] += right[i + 1]; 20 | 21 | int sum = 0; 22 | for (int i = 0; i < N; i++) 23 | sum += max(left[i], right[i]); 24 | 25 | return sum; 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | 32 | int main() { 33 | int t; 34 | cin >> t; 35 | while (t--) { 36 | int N; 37 | cin >> N; 38 | 39 | vector ratings(N); 40 | for (int i = 0; i < N; i++) { 41 | cin >> ratings[i]; 42 | } 43 | Solution obj; 44 | cout << obj.minCandy(N, ratings) << endl; 45 | } 46 | return 0; 47 | } 48 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/23-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // A C++ program to print elements with count more than n/k 3 | 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution 11 | { 12 | public: 13 | int countOccurence(int arr[], int n, int k) { 14 | map mp; 15 | int minCnt = n/k; 16 | for(int i = 0 ; i < n; ++i) 17 | ++mp[arr[i]]; 18 | 19 | int out = 0; 20 | for(auto i: mp) 21 | if(i.second > minCnt) 22 | ++out; 23 | 24 | return out; 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | int main() { 31 | int t; 32 | cin >> t; 33 | while (t--) { 34 | int n, i; 35 | cin >> n; 36 | 37 | int arr[n]; 38 | 39 | for (i = 0; i < n; i++) cin >> arr[i]; 40 | int k; 41 | cin >> k; 42 | Solution obj; 43 | cout << obj.countOccurence(arr, n, k) << endl; 44 | } 45 | return 0; 46 | } 47 | 48 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/24-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | /* Driver program to test above function */ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int buyMaximumProducts(int n, int k, int price[]) { 12 | vector> v; 13 | for (int i = 0; i < n; ++i) 14 | v.push_back({price[i], i + 1}); 15 | 16 | sort(v.begin(), v.end()); 17 | 18 | int out = 0; 19 | 20 | for (auto i : v) { 21 | int maxBuy = min(k / i.first, i.second); 22 | out += maxBuy; 23 | k -= i.first * maxBuy; 24 | } 25 | return out; 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | int main() 32 | { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | int n, k; 38 | cin >> n >> k; 39 | int price[n]; 40 | for(int i = 0 ; i < n; i++){ 41 | cin >> price[i]; 42 | } 43 | Solution ob; 44 | int ans = ob.buyMaximumProducts(n, k, price); 45 | cout << ans< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int kSubstrConcat(int n, string s, int k) { 11 | if (n % k > 0) 12 | return 0; 13 | 14 | unordered_map mp; 15 | for (int i = 0; i < n / k; ++i) 16 | ++mp[s.substr(i * k, k)]; 17 | 18 | int cnt = 0; 19 | for(auto i : mp) 20 | cnt += i.second > 1; 21 | 22 | return (mp.size() <= 2 && cnt <= 1); 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | 29 | int main() 30 | { 31 | int t; cin >> t; 32 | while (t--) 33 | { 34 | int n; cin >> n; 35 | string s; cin >> s; 36 | int k; cin >> k; 37 | Solution ob; 38 | cout << ob.kSubstrConcat (n, s, k) << endl; 39 | } 40 | } 41 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/31-dec.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | bool solve(int i, int& n, int sum, int coins[], vector>& dp){ 10 | if(sum == 2024 || (sum && (sum%20 == 0 || sum%24 == 0))) 11 | return true; 12 | 13 | if(i == n || sum > 2024) 14 | return false; 15 | 16 | if(dp[i].find(sum) != dp[i].end()) 17 | return dp[i][sum]; 18 | 19 | int nt = solve(i+1, n, sum, coins, dp); 20 | if(nt) 21 | return dp[i][sum] = nt; 22 | 23 | return dp[i][sum] = solve(i+1, n, sum + coins[i], coins, dp); 24 | } 25 | 26 | int isPossible(int n , int coins[]) 27 | { 28 | vector> dp(n); 29 | return solve(0,n,0,coins, dp); 30 | } 31 | }; 32 | 33 | 34 | //{ Driver Code Starts. 35 | int main() { 36 | int t; 37 | cin >> t; 38 | while (t--) { 39 | int N; 40 | 41 | cin>>N; 42 | int coins[N]; 43 | for(int i=0 ; i>coins[i]; 45 | 46 | Solution ob; 47 | cout << ob.isPossible(N,coins) << endl; 48 | } 49 | return 0; 50 | } 51 | // } Driver Code Ends -------------------------------------------------------------------------------- /2023/12-2023(dec)/readme.md: -------------------------------------------------------------------------------- 1 | ## December - 2023 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **December 2023**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2023/readme.md: -------------------------------------------------------------------------------- 1 | ## Solutions for 2023 2 | 3 | Greetings! Welcome to the repository containing solutions for the GeeksforGeeks (GFG) Problem of the Day for the **year 2023**. 4 | 5 | Explore the directory to review solutions for each daily problem. Keep in mind that comprehending the logic behind each solution is essential for your development as a programmer. 6 | 7 | For the original problem statements, visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2024/01-2024(jan)/01-jan.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | bool canPair(vector nums, int k) { 10 | if(nums.size() % 2 != 0) 11 | return false; 12 | 13 | vector cnt(k, 0); 14 | 15 | for(auto i: nums) 16 | ++cnt[i % k]; 17 | 18 | int l = 1, r = k - 1; 19 | 20 | while(l < r){ 21 | if(cnt[l] != cnt[r]) 22 | return false; 23 | ++l; 24 | --r; 25 | } 26 | 27 | if((l == r && cnt[l] % 2 != 0) || cnt[0] % 2 != 0) 28 | return false; 29 | 30 | return true; 31 | } 32 | }; 33 | 34 | 35 | //{ Driver Code Starts. 36 | int main() { 37 | int tc; 38 | cin >> tc; 39 | while (tc--) { 40 | int n, k; 41 | cin >> n >> k; 42 | vector nums(n); 43 | for (int i = 0; i < nums.size(); i++) cin >> nums[i]; 44 | Solution ob; 45 | bool ans = ob.canPair(nums, k); 46 | if (ans) 47 | cout << "True\n"; 48 | else 49 | cout << "False\n"; 50 | } 51 | return 0; 52 | } 53 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/01-2024(jan)/02-jan.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // C++ program to find largest subarray sum with 3 | // at-least k elements in it. 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | long long int maxSumWithK(long long int a[], long long int n, long long int k) 12 | { 13 | vector pre(n, 0); 14 | pre[0] = a[0]; 15 | 16 | for (int i = 1; i < n; ++i) 17 | pre[i] = pre[i - 1] + a[i]; 18 | 19 | long long int sum = pre[k - 1]; 20 | long long int ans = sum; 21 | 22 | for (int i = k; i < n; i++) 23 | { 24 | long long int cur = pre[i] - pre[i - k]; 25 | sum = max(cur, sum + a[i]); 26 | ans = max(ans, sum); 27 | } 28 | 29 | return ans; 30 | } 31 | }; 32 | 33 | 34 | //{ Driver Code Starts. 35 | 36 | // Driver code 37 | int main() { 38 | long long int t; 39 | cin >> t; 40 | while (t--) { 41 | long long int n, k, i; 42 | cin >> n; 43 | long long int a[n]; 44 | for (i = 0; i < n; i++) { 45 | cin >> a[i]; 46 | } 47 | cin >> k; 48 | Solution ob; 49 | cout << ob.maxSumWithK(a, n, k) << endl; 50 | } 51 | return 0; 52 | } 53 | 54 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/01-2024(jan)/03-jan.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int smallestSubstring(string S) { 11 | vector pos(3, -1); 12 | int out = INT_MAX; 13 | 14 | for(int i = 0; i < S.size(); ++i) { 15 | pos[S[i] - '0'] = i; 16 | 17 | int nin = INT_MAX, nax = 0; 18 | for(auto it: pos) { 19 | nin = min(nin, it); 20 | nax = max(nax, it); 21 | } 22 | 23 | if(nin != -1) 24 | out = min(out, nax - nin + 1); 25 | } 26 | 27 | return out == INT_MAX ? -1 : out; 28 | } 29 | }; 30 | 31 | 32 | //{ Driver Code Starts. 33 | 34 | int main() { 35 | int t; 36 | cin >> t; 37 | while (t--) { 38 | string S; 39 | cin >> S; 40 | Solution ob; 41 | cout << ob.smallestSubstring(S); 42 | cout << endl; 43 | } 44 | } 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/01-2024(jan)/04-jan.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | int singleElement(int arr[], int N) { 13 | vector bits(32, 0); 14 | for (int i = 0; i < N; i++) { 15 | for (int j = 0; j < 32; j++) { 16 | bits[j] += arr[i] & 1; 17 | arr[i] >>= 1; 18 | } 19 | } 20 | 21 | int ans = 0; 22 | for (int i = 0; i < 32; i++) 23 | if (bits[i] % 3 != 0) 24 | ans += 1 << i; 25 | 26 | return ans; 27 | } 28 | }; 29 | 30 | 31 | //{ Driver Code Starts. 32 | 33 | int main() { 34 | int t; 35 | cin >> t; 36 | while (t--) { 37 | int N; 38 | 39 | cin>>N; 40 | int arr[N]; 41 | 42 | for(int i=0 ; i>arr[i]; 44 | 45 | Solution ob; 46 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int mod = 1e9 + 7; 10 | 11 | int TotalWays(int N) { 12 | long long curr, prev, next; 13 | curr = prev = 1; 14 | for (int i = 1; i <= N; ++i) { 15 | next = (curr + prev) % mod; 16 | prev = curr; 17 | curr = next; 18 | } 19 | 20 | return (curr * curr) % mod; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | int main(){ 27 | int tc; 28 | cin >> tc; 29 | while(tc--){ 30 | int N; 31 | cin >> N; 32 | Solution ob; 33 | int ans = ob.TotalWays(N); 34 | cout << ans <<"\n"; 35 | } 36 | return 0; 37 | } 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/01-2024(jan)/06-jan.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int primePowers(int n) 11 | { 12 | int cnt = 0; 13 | for(int i = 2; i <= sqrt(n); i++) 14 | { 15 | while(n % i == 0) 16 | { 17 | cnt++; 18 | n = n / i; 19 | } 20 | } 21 | 22 | if(n > 1) 23 | cnt++; 24 | 25 | return cnt; 26 | } 27 | 28 | int sumOfPowers(int a, int b) 29 | { 30 | int cnt = 0; 31 | for(int i = a; i <= b; i++) 32 | cnt += primePowers(i); 33 | 34 | return cnt; 35 | } 36 | }; 37 | 38 | 39 | //{ Driver Code Starts. 40 | int main(){ 41 | int tc; 42 | cin >> tc; 43 | while(tc--){ 44 | int a, b; 45 | cin >> a >> b; 46 | Solution obj; 47 | int ans = obj.sumOfPowers(a, b); 48 | cout << ans <<"\n"; 49 | } 50 | return 0; 51 | } 52 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/01-2024(jan)/07-jan.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool solve(int arr[], int N, int K, int mid) { 4 | int sum = 0; 5 | for(int i = 0; i < N; i++) { 6 | if(arr[i] > mid) 7 | return false; 8 | sum += arr[i]; 9 | if(sum > mid) { 10 | K--; 11 | sum = arr[i]; 12 | } 13 | } 14 | return K >= 1; 15 | } 16 | 17 | int splitArray(int arr[] ,int N, int K) { 18 | int sum = 0; 19 | 20 | for(int i = 0; i < N; i++) 21 | sum += arr[i]; 22 | 23 | int low = 0, high = sum; 24 | int ans = sum; 25 | 26 | while(low <= high) { 27 | int mid = (low + high) / 2; 28 | if(solve(arr, N, K, mid)) { 29 | ans = mid; 30 | high = mid - 1; 31 | } else 32 | low = mid + 1; 33 | } 34 | return ans; 35 | } 36 | }; -------------------------------------------------------------------------------- /2024/01-2024(jan)/10-jan.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int longSubarrWthSumDivByK(int arr[], int n, int k) { 11 | unordered_map mp; 12 | mp[0] = -1; 13 | 14 | int out = 0, sum = 0; 15 | 16 | for (int i = 0; i < n; i++) { 17 | sum += arr[i]; 18 | int rem = sum % k; 19 | 20 | if (rem < 0) 21 | rem += k; 22 | 23 | if (mp.find(rem) != mp.end()) 24 | out = max(out, i - mp[rem]); 25 | else 26 | mp[rem] = i; 27 | } 28 | 29 | return out; 30 | } 31 | }; 32 | 33 | 34 | //{ Driver Code Starts. 35 | 36 | int main() 37 | { 38 | int t; 39 | cin>>t; 40 | while(t--) 41 | { 42 | int n,k,i; 43 | cin>>n>>k; int arr[n]; 44 | for(i=0;i>arr[i]; 46 | Solution ob; 47 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int solve(int n, int m) { 12 | if (m < n) 13 | return 0; 14 | if (n == 0) 15 | return 1; 16 | 17 | int t = solve(n - 1, m / 2); 18 | int nt = solve(n, m - 1); 19 | 20 | return t + nt; 21 | } 22 | 23 | int numberSequence(int m, int n) { 24 | return solve(n, m); 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | int main(){ 31 | int t; 32 | cin>>t; 33 | while(t--){ 34 | int m, n; 35 | cin>>m>>n; 36 | 37 | Solution ob; 38 | cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | vector> uniquePerms(vector &arr ,int n) { 13 | sort(arr.begin(), arr.end()); 14 | vector> out; 15 | do{ 16 | out.push_back(arr); 17 | } while(next_permutation(arr.begin(), arr.end())); 18 | 19 | return out; 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | 26 | int main() { 27 | int t; 28 | cin >> t; 29 | while (t--) { 30 | int n; 31 | 32 | cin>>n; 33 | vector arr(n); 34 | 35 | for(int i=0 ; i>arr[i]; 37 | 38 | Solution ob; 39 | vector> res = ob.uniquePerms(arr,n); 40 | for(int i=0; i 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int solve(int cp, int cs, int sz, string s, vector>& dp) { 10 | if (cp == sz) 11 | return 1; 12 | 13 | if (dp[cp][cs] != -1) 14 | return dp[cp][cs]; 15 | 16 | int sm = 0, cnt = 0; 17 | 18 | for (int i = cp; i < sz; i++) { 19 | sm += s[i] - '0'; 20 | if (sm >= cs) 21 | cnt += solve(i + 1, sm, sz, s, dp); 22 | } 23 | 24 | return dp[cp][cs] = cnt; 25 | } 26 | 27 | int TotalCount(string str) { 28 | int sm = 0; 29 | for (auto i : str) 30 | sm += i - '0'; 31 | 32 | vector> dp(str.size() + 1, vector(sm + 1, -1)); 33 | 34 | return solve(0, 0, str.size(), str, dp); 35 | } 36 | }; 37 | 38 | 39 | //{ Driver Code Starts. 40 | int main(){ 41 | int tc; 42 | cin >> tc; 43 | while(tc--){ 44 | string str; 45 | cin >> str; 46 | Solution ob; 47 | int ans = ob.TotalCount(str); 48 | cout << ans <<"\n"; 49 | } 50 | return 0; 51 | } 52 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/01-2024(jan)/30-jan.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int LCSof3(string A, string B, string C, int n1, int n2, int n3) { 11 | vector>> dp(n1 + 1, vector>(n2 + 1, vector(n3 + 1, 0))); 12 | 13 | for (int i = 1; i <= n1; i++) { 14 | for (int j = 1; j <= n2; j++) { 15 | for (int k = 1; k <= n3; k++) { 16 | if (A[i - 1] == B[j - 1] && B[j - 1] == C[k - 1]) 17 | dp[i][j][k] = max(dp[i][j][k], 1 + dp[i - 1][j - 1][k - 1]); 18 | dp[i][j][k] = max({dp[i][j][k], dp[i - 1][j][k], dp[i][j - 1][k], dp[i][j][k - 1]}); 19 | } 20 | } 21 | } 22 | return dp[n1][n2][n3]; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | 29 | int main() 30 | { 31 | int t; cin >> t; 32 | while (t--) 33 | { 34 | int n1, n2, n3; cin >> n1 >> n2 >> n3; 35 | string A, B, C; cin >> A >> B >> C; 36 | Solution obj; 37 | cout << obj.LCSof3 (A, B, C, n1, n2, n3) << endl; 38 | } 39 | } 40 | // Contributed By: Pranay Bansal 41 | 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/01-2024(jan)/readme.md: -------------------------------------------------------------------------------- 1 | ## January - 2024 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **January 2024**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2024/02-2024(feb)/01-feb.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | using namespace std; 7 | 8 | class Solution 9 | { 10 | public: 11 | // Function to check if a string is Pangram or not. 12 | bool checkPangram(std::string s) { 13 | std::vector f(26, 0); 14 | 15 | for (auto i : s) { 16 | char cur = std::tolower(i); 17 | 18 | if (cur >= 'a' && cur <= 'z') { 19 | f[cur - 'a'] = 1; 20 | } 21 | } 22 | 23 | return std::accumulate(f.begin(), f.end(), 0) == 26; 24 | } 25 | }; 26 | 27 | int main() { 28 | Solution solution; 29 | 30 | // Input from the user 31 | std::string userInput; 32 | std::cout << "Enter a string: "; 33 | std::getline(std::cin, userInput); 34 | 35 | // Check if the input is a pangram 36 | bool isPangram = solution.checkPangram(userInput); 37 | 38 | // Output the result 39 | if (isPangram) { 40 | std::cout << 1 << endl; 41 | } else { 42 | std::cout << 0 << endl; 43 | } 44 | 45 | return 0; 46 | } 47 | -------------------------------------------------------------------------------- /2024/02-2024(feb)/02-feb.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int atoi(string s) { 12 | int n = 0; 13 | for (int i = 0; i < s.length(); i++) { 14 | if (i == 0 && s[i] == '-') 15 | continue; 16 | if (s[i] >= '0' && s[i] <= '9') 17 | n = n * 10 + s[i] - '0'; 18 | else 19 | return -1; 20 | } 21 | return (s[0] == '-') ? -n : n; 22 | } 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | int main() 28 | { 29 | int t; 30 | cin>>t; 31 | while(t--) 32 | { 33 | string s; 34 | cin>>s; 35 | Solution ob; 36 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | vector recamanSequence(int n){ 12 | vector a(n+1,0); 13 | unordered_set st; 14 | for(int i = 1; i <= n; ++i){ 15 | if((a[i-1] - i) > 0 && st.find(a[i-1] - i) == st.end()) 16 | a[i] = a[i-1] - i; 17 | else 18 | a[i] = a[i-1] + i; 19 | st.insert(a[i]); 20 | } 21 | return a; 22 | } 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | 28 | int main(){ 29 | int t; 30 | cin>>t; 31 | while(t--){ 32 | int n; 33 | cin>>n; 34 | 35 | Solution ob; 36 | vector ans = ob.recamanSequence(n); 37 | for(int i = 0;i < n;i++) 38 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | long long sequence(int n){ 12 | long long mod = 1e9 + 7; 13 | long long out = 0, c = 1; 14 | for(int i = 1; i <= n; ++i){ 15 | long long temp = 1; 16 | for(int j = 0; j < i ; ++j){ 17 | temp *= c++; 18 | temp %= mod; 19 | } 20 | out = (out + temp) % mod; 21 | } 22 | return out; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | 29 | int main(){ 30 | int t; 31 | cin>>t; 32 | while(t--){ 33 | int N; 34 | cin>>N; 35 | 36 | Solution ob; 37 | cout< 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int isPossible(vector> paths){ 11 | for(auto i: paths){ 12 | if(accumulate(i.begin(), i.end(), 0) % 2 != 0) 13 | return false; 14 | } 15 | return true; 16 | } 17 | }; 18 | 19 | 20 | //{ Driver Code Starts. 21 | int main(){ 22 | int tc; 23 | cin >> tc; 24 | while(tc--){ 25 | int n; 26 | cin >> n; 27 | vector>paths(n, vector(n, 0)); 28 | for(int i = 0; i < n; i++){ 29 | for(int j = 0; j < n; j++) 30 | cin >> paths[i][j]; 31 | } 32 | Solution obj; 33 | int ans = obj.isPossible(paths); 34 | cout << ans <<"\n"; 35 | } 36 | return 0; 37 | } 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/02-2024(feb)/17-feb.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | bool isMaxHeap(int arr[], int n) 10 | { 11 | int st = n / 2; 12 | for(int i = st; i >= 0; --i){ 13 | if((i*2 + 1 < n && arr[i] < arr[i*2 + 1]) || (i*2 + 2 < n) && arr[i] < arr[i*2 + 2]) 14 | return false; 15 | } 16 | return true; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | int main() { 23 | 24 | int t; 25 | cin >> t; 26 | while(t--) 27 | { 28 | int n; 29 | cin >> n; 30 | int a[4*n]={0}; 31 | for(int i =0;i> a[i]; 33 | } 34 | Solution ob; 35 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution{ 10 | public: 11 | int minValue(string s, int k){ 12 | unordered_map mp; 13 | for(auto i: s){ 14 | ++mp[i]; 15 | } 16 | 17 | priority_queue pq; 18 | for(auto i: mp) 19 | pq.push(i.second); 20 | 21 | while(k--){ 22 | int t = pq.top(); 23 | pq.pop(); 24 | --t; 25 | if(t) pq.push(t); 26 | } 27 | 28 | int out = 0; 29 | while(!pq.empty()){ 30 | out += pq.top() * pq.top(); 31 | pq.pop(); 32 | } 33 | 34 | return out; 35 | } 36 | }; 37 | 38 | 39 | //{ Driver Code Starts. 40 | 41 | int main(){ 42 | int t; 43 | cin>>t; 44 | while(t--){ 45 | string s; 46 | int k; 47 | cin>>s>>k; 48 | 49 | Solution ob; 50 | cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | bool can(int i, string& A, vector & B){ 13 | if(i == A.size()) 14 | return true; 15 | 16 | for(auto str: B){ 17 | if(str.size() <= (A.size() - i) && A.substr(i, str.size()) == str){ 18 | if(can(i + str.size(), A, B)) 19 | return true; 20 | } 21 | } 22 | 23 | return false; 24 | } 25 | int wordBreak(string A, vector &B) { 26 | return can(0,A,B); 27 | } 28 | }; 29 | 30 | 31 | //{ Driver Code Starts. 32 | 33 | int main(){ 34 | int t; 35 | cin>>t; 36 | while(t--){ 37 | int n; 38 | cin>>n; 39 | vector dict; 40 | for(int i=0;i>S; 43 | dict.push_back(S); 44 | } 45 | string line; 46 | cin>>line; 47 | Solution ob; 48 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int mod = 1e9 + 7; 11 | 12 | int solve(int i, int j, int n, int m, string s, string t, vector>& dp) { 13 | if (j == m) 14 | return 1; 15 | if (i == n) 16 | return 0; 17 | 18 | if (dp[i][j] != -1) 19 | return dp[i][j]; 20 | 21 | int ntake = solve(i + 1, j, n, m, s, t, dp); 22 | int take = 0; 23 | if (s[i] == t[j]) 24 | take = solve(i + 1, j + 1, n, m, s, t, dp); 25 | 26 | return dp[i][j] = (take + ntake) % mod; 27 | } 28 | 29 | int subsequenceCount(string s, string t) { 30 | int n = s.size(), m = t.size(); 31 | vector> dp(n + 1, vector(m + 1, -1)); 32 | return solve(0, 0, n, m, s, t, dp); 33 | } 34 | }; 35 | 36 | 37 | //{ Driver Code Starts. 38 | 39 | // Driver code to check above method 40 | int main() 41 | { 42 | int t; 43 | cin>>t; 44 | while(t--) 45 | { 46 | string s; 47 | string tt; 48 | cin>>s; 49 | cin>>tt; 50 | 51 | Solution ob; 52 | cout< 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int maxProfit(vector& price) { 12 | int n = price.size(); 13 | vector left(n, 0); 14 | int nin = price[0]; 15 | int out = 0; 16 | for (int i = 1; i < n; ++i) { 17 | nin = min(nin, price[i]); 18 | left[i] = max(left[i - 1], price[i] - nin); 19 | out = max(out, left[i]); 20 | } 21 | 22 | int nax = price[n - 1]; 23 | for (int i = n - 2; i > 0; --i) { 24 | nax = max(nax, price[i]); 25 | out = max(out, left[i - 1] + nax - price[i]); 26 | } 27 | return out; 28 | } 29 | }; 30 | 31 | 32 | //{ Driver Code Starts. 33 | 34 | int main(){ 35 | int t; 36 | cin>>t; 37 | while(t--){ 38 | 39 | int n; 40 | cin>>n; 41 | vector price(n); 42 | for(int i=0;i>price[i]; 43 | Solution obj; 44 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int maxSum(int n) { 10 | vector dp(n+1, 0); 11 | for(int i = 1; i <= n; ++i) { 12 | dp[i] = dp[i/2] + dp[i/3] + dp[i/4]; 13 | dp[i] = max(dp[i], i); 14 | } 15 | return dp[n]; 16 | } 17 | }; 18 | 19 | 20 | //{ Driver Code Starts. 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | int n; 28 | cin>>n; 29 | Solution ob; 30 | cout< 4 | #define ll long long int 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | long long int count(long long int n) 12 | { 13 | vector dp(n+1, 0); 14 | dp[0] = 1; 15 | for (int i = 3; i <= n; i++) 16 | dp[i] += dp[i - 3]; 17 | for (int i = 5; i <= n; i++) 18 | dp[i] += dp[i - 5]; 19 | for (int i = 10; i <= n; i++) 20 | dp[i] += dp[i - 10]; 21 | 22 | return dp[n]; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | int main() 29 | { 30 | int t; 31 | cin>>t; 32 | while(t--) 33 | { 34 | ll n; 35 | cin>>n; 36 | Solution obj; 37 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | vector out; 10 | string curr; 11 | 12 | void subSeq(int i, string &s) { 13 | if (i == s.size()) { 14 | if (curr.size()) 15 | out.push_back(curr); 16 | return; 17 | } 18 | 19 | curr.push_back(s[i]); 20 | subSeq(i + 1, s); 21 | curr.pop_back(); 22 | subSeq(i + 1, s); 23 | } 24 | 25 | vector AllPossibleStrings(string s) { 26 | subSeq(0, s); 27 | sort(out.begin(), out.end()); 28 | return out; 29 | } 30 | }; 31 | 32 | 33 | //{ Driver Code Starts. 34 | int main(){ 35 | int tc; 36 | cin >> tc; 37 | while(tc--){ 38 | string s; 39 | cin >> s; 40 | Solution ob; 41 | vector res = ob.AllPossibleStrings(s); 42 | for(auto i : res) 43 | cout << i <<" "; 44 | cout << "\n"; 45 | 46 | } 47 | return 0; 48 | } 49 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/02-2024(feb)/27 - Play With OR.md: -------------------------------------------------------------------------------- 1 | ## 27. Play With OR 2 | The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/play-with-or5515/1) 3 | 4 | ### My Approach 5 | - Iterate through the given array from index 0 to n-2. 6 | - For each index i, perform a bitwise OR operation between arr[i] and arr[i+1]. 7 | - Update the value at arr[i] with the result of the OR operation. 8 | - Continue this process until the second-to-last element of the array. 9 | - Return the modified array. 10 | 11 | ### Time and Auxiliary Space Complexity 12 | 13 | - **Time Complexity**: `O(N)`, where N is the length of the array. 14 | - **Auxiliary Space Complexity**: `O(1)` 15 | 16 | ### Code (C++) 17 | ```cpp 18 | int* game_with_number(int arr[], int n) 19 | { 20 | for(int i = 0 ; i < n - 1; ++i){ 21 | arr[i] |= arr[i+1]; 22 | } 23 | return arr; 24 | } 25 | ``` 26 | 27 | ### Contribution and Support 28 | 29 | For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment. 30 | 31 | If you find this solution helpful, consider supporting us by giving a ⭐ star to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository. 32 | -------------------------------------------------------------------------------- /2024/02-2024(feb)/27-feb.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | int* game_with_number(int arr[], int n); 7 | 8 | int main() 9 | { 10 | 11 | int t;cin>> t; 12 | while(t--) 13 | { 14 | int n; 15 | cin >> n; 16 | int arr[n]; 17 | 18 | for(int i=0;i>arr[i]; 20 | 21 | int *arr2; 22 | 23 | arr2 = game_with_number(arr, n); 24 | for(int i = 0;i < n; i++) 25 | cout << arr2[i] << " "; 26 | 27 | cout << endl; 28 | 29 | } 30 | 31 | } 32 | 33 | // } Driver Code Ends 34 | 35 | 36 | int* game_with_number(int arr[], int n) 37 | { 38 | for(int i = 0 ; i < n - 1; ++i){ 39 | arr[i] |= arr[i+1]; 40 | } 41 | return arr; 42 | } 43 | -------------------------------------------------------------------------------- /2024/02-2024(feb)/28-feb.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int DivisibleByEight(string s) { 11 | int n = s.size(); 12 | int l = stoi(s.substr(max(n - 3, 0))); 13 | return l % 8 == 0 ? 1 : -1; 14 | } 15 | }; 16 | 17 | 18 | //{ Driver Code Starts. 19 | int main() 20 | { 21 | int t; 22 | cin>>t; 23 | while(t--) 24 | { 25 | string S; 26 | cin>>S; 27 | Solution ob; 28 | cout< 3 | 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | long long sumBitDifferences(int arr[], int n) { 12 | long long out = 0; 13 | for(int i=0; i<32; ++i){ 14 | long long one = 0; 15 | for(int j = 0; j < n; ++j) 16 | if((arr[j]&(1<> t; 31 | while (t--) { 32 | int n, i; 33 | cin >> n; 34 | int arr[n]; 35 | for (i = 0; i < n; i++) { 36 | cin >> arr[i]; 37 | } 38 | Solution ob; 39 | auto ans = ob.sumBitDifferences(arr, n); 40 | cout << ans << "\n"; 41 | } 42 | return 0; 43 | } 44 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/02-2024(feb)/readme.md: -------------------------------------------------------------------------------- 1 | ## February 2024 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **February 2024**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2024/03-2024(march)/01-march.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int peakElement(int arr[], int n) { 11 | int left = 0, right = n - 1, mid; 12 | while (left < right) { 13 | mid = (left + right) / 2; 14 | if (arr[mid] < arr[mid + 1]) 15 | left = mid + 1; 16 | else 17 | right = mid; 18 | } 19 | return left; 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | 26 | int main() { 27 | int t; 28 | cin>>t; 29 | while(t--) 30 | { 31 | int n; 32 | cin>>n; 33 | int a[n], tmp[n]; 34 | for(int i=0;i>a[i]; 37 | tmp[i] = a[i]; 38 | } 39 | bool f=0; 40 | Solution ob; 41 | 42 | int A = ob. peakElement(tmp,n); 43 | 44 | if(A<0 and A>=n) 45 | cout<<0<=a[1]) 51 | f=1; 52 | else if(A==n-1 and a[n-1]>=a[n-2]) 53 | f=1; 54 | else if(a[A]>=a[A+1] and a[A]>= a[A-1]) 55 | f=1; 56 | else 57 | f=0; 58 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int firstElementKTime(int n, int k, int a[]) { 11 | unordered_map mp; 12 | for(int i = 0 ; i < n ; ++i){ 13 | ++mp[a[i]]; 14 | if(mp[a[i]] == k) 15 | return a[i]; 16 | } 17 | return -1; 18 | } 19 | }; 20 | 21 | 22 | //{ Driver Code Starts. 23 | 24 | int main() { 25 | int t; 26 | cin >> t; 27 | while(t--){ 28 | int n, k; 29 | cin >> n >> k; 30 | int a[n]; 31 | for(int i = 0;i> a[i]; 33 | } 34 | Solution ob; 35 | cout< 3 | 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | bool func(string& a, string& b) 10 | { 11 | return a+b > b+a; 12 | } 13 | class Solution{ 14 | public: 15 | string printLargest(int n, vector &arr) 16 | { 17 | sort(arr.begin(), arr.end(), func); 18 | string out; 19 | for (auto s : arr) 20 | out+=s; 21 | return out; 22 | } 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | 28 | int main() { 29 | int t; 30 | cin >> t; 31 | while (t--) { 32 | int n, i; 33 | cin >> n; 34 | vector arr(n); 35 | for (i = 0; i < n; i++) { 36 | cin >> arr[i]; 37 | } 38 | Solution ob; 39 | auto ans = ob.printLargest(n, arr); 40 | cout << ans << "\n"; 41 | } 42 | return 0; 43 | } 44 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/03-2024(march)/04-march.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | #include 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | void swapElements(int arr[], int n) { 12 | for (int i = 0; i < n - 2; ++i) { 13 | arr[i] = arr[i] ^ arr[i + 2]; 14 | arr[i + 2] = arr[i] ^ arr[i + 2]; 15 | arr[i] = arr[i] ^ arr[i + 2]; 16 | } 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | 23 | // Driver code to test swapElements 24 | int main() { 25 | 26 | int testcase; 27 | 28 | cin >> testcase; 29 | 30 | while(testcase--){ 31 | int n; 32 | cin >> n; 33 | 34 | int arr[n]; 35 | 36 | for(int i = 0;i> arr[i]; 38 | } 39 | Solution obj; 40 | // calling function to swap the array swap elements 41 | obj.swapElements(arr, n); 42 | 43 | // Printing the modified array 44 | for(int i = 0;i 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | string longestSubstring(string s, int n) { 10 | int nax = 0, i = 0, j = 0; 11 | string out = "-1"; 12 | 13 | for( ; i < n && j < n; ++j) { 14 | string str = s.substr(i, j - i + 1); 15 | 16 | if (nax < str.size() && s.find(str, j + 1) != string::npos) { 17 | nax = str.size(); 18 | out = str; 19 | } else 20 | ++i; 21 | } 22 | return out; 23 | } 24 | }; 25 | 26 | 27 | //{ Driver Code Starts. 28 | int main() { 29 | int t; 30 | cin >> t; 31 | while (t--) { 32 | int N; 33 | string S; 34 | 35 | cin >> N; 36 | cin >> S; 37 | 38 | Solution ob; 39 | cout << ob.longestSubstring(S, N) << endl; 40 | } 41 | return 0; 42 | } 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/03-2024(march)/08-march.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool sameFreq(string s) 11 | { 12 | unordered_map cnt; 13 | for(auto i: s) 14 | cnt[i]++; 15 | 16 | int nin, nax; 17 | nin = nax = cnt[s[0]]; 18 | 19 | for(auto i: cnt){ 20 | nin = min(nin, i.second); 21 | nax = max(nax, i.second); 22 | if(nax - nin > 1) 23 | return 0; 24 | } 25 | 26 | int cnin = 0; 27 | for(auto i: cnt) 28 | if(i.second == nin) 29 | ++cnin; 30 | if (cnin == 1 && nin == 1){ 31 | return 1; 32 | } 33 | 34 | return cnt.size() - cnin <= 1; 35 | } 36 | }; 37 | 38 | 39 | //{ Driver Code Starts. 40 | int main(){ 41 | int t; 42 | cin>>t; 43 | while(t--) 44 | { 45 | string s; 46 | cin>>s; 47 | Solution ob; 48 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | //User function template for C++ 8 | 9 | class Solution{ 10 | public: 11 | char nthCharacter(string s, int r, int n) 12 | { 13 | int len=s.length(); 14 | for (int i=0;i> t; 34 | while (t--) { 35 | int R, N; 36 | string S; 37 | cin >> S >> R >> N; 38 | Solution ob; 39 | cout << ob.nthCharacter(S, R, N) << endl; 40 | } 41 | return 0; 42 | } 43 | // } Driver Code Ends 44 | -------------------------------------------------------------------------------- /2024/03-2024(march)/10-march.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | string removeDuplicates(string str) { 12 | unordered_set st; 13 | string out; 14 | for(auto i : str){ 15 | if(st.find(i) == st.end()){ 16 | out += i; 17 | st.insert(i); 18 | } 19 | } 20 | return out; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main() { 28 | int t; 29 | cin >> t; 30 | while (t--) { 31 | string str; 32 | cin >> str; 33 | Solution ob; 34 | auto ans = ob.removeDuplicates(str); 35 | 36 | cout << ans << "\n"; 37 | } 38 | return 0; 39 | } 40 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/03-2024(march)/23 - march.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | const int mod=1e9+7; 10 | vector Series(int n) 11 | { 12 | vectorvec; 13 | int n1=0, n2=1; 14 | vec.push_back(n1); 15 | if (n==0) 16 | return vec; 17 | vec.push_back(n2); 18 | if (n==1) 19 | return vec; 20 | for (int i=2;i<=n;i++) 21 | { 22 | int num=(vec[i-1]+vec[i-2])%mod; 23 | vec.push_back(num); 24 | } 25 | return vec; 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | int main() { 32 | int t; 33 | cin >> t; 34 | while (t--) { 35 | int n; 36 | cin >> n; 37 | Solution obj; 38 | 39 | vector ans = obj.Series(n); 40 | for (auto x : ans) cout << x << " "; 41 | cout << "\n"; 42 | } 43 | return 0; 44 | } 45 | // } Driver Code Ends 46 | -------------------------------------------------------------------------------- /2024/03-2024(march)/26-march.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool solve(int ii, string &s, int n) { 11 | if (ii >= n) 12 | return true; 13 | 14 | int a = 0, b = 0; 15 | for (int i = ii; i < n; ++i) { 16 | a = a * 10 + (s[i] - '0'); 17 | b = 0; 18 | for (int j = i + 1; j < n; ++j) { 19 | b = b * 10 + (s[j] - '0'); 20 | string c = to_string(a + b); 21 | if (c.size() <= n - j - 1) 22 | if (c == s.substr(j + 1, c.size())) 23 | if (solve(j + c.size() + 1, s, n)) 24 | return true; 25 | } 26 | } 27 | return false; 28 | } 29 | 30 | bool isAdditiveSequence(string n) { 31 | return solve(0, n, n.size()); 32 | } 33 | }; 34 | 35 | 36 | //{ Driver Code Starts. 37 | 38 | int main() { 39 | int t; 40 | cin >> t; 41 | 42 | Solution sol; 43 | 44 | while (t--) { 45 | string s; 46 | cin >> s; 47 | 48 | bool result = sol.isAdditiveSequence(s); 49 | cout << result << endl; 50 | } 51 | 52 | return 0; 53 | } 54 | 55 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/03-2024(march)/29-march.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | bool isEularCircuitExist(int v, vectoradj[]){ 9 | unordered_mapdeg; 10 | for (int i=0;i> tc; 26 | while(tc--){ 27 | int V, E; 28 | cin >> V >> E; 29 | vectoradj[V]; 30 | for(int i = 0; i < E; i++){ 31 | int u, v; 32 | cin >> u >> v; 33 | adj[u].push_back(v); 34 | adj[v].push_back(u); 35 | } 36 | Solution obj; 37 | bool ans = obj.isEularCircuitExist(V, adj); 38 | if(ans) 39 | cout << "1\n"; 40 | else cout << "0\n"; 41 | } 42 | return 0; 43 | } 44 | // } Driver Code Ends 45 | -------------------------------------------------------------------------------- /2024/03-2024(march)/readme.md: -------------------------------------------------------------------------------- 1 | ## March 2024 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **March 2024**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2024/04-2024(april)/04-April.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | long long sumSubstrings(string s){ 10 | long long sum = 0; 11 | int mod = 1e9+7; 12 | int n = s.size(); 13 | long mul = 1; 14 | for (int i = n - 1; i >= 0; --i) { 15 | sum += ((s[i] - '0') * mul * (i + 1)) % mod; 16 | sum %= mod; 17 | mul = (mul * 10 + 1) % mod; 18 | } 19 | return sum; 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | int main() 26 | { 27 | //taking total testcases 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | //taking string 33 | string s; 34 | cin>>s; 35 | Solution ob; 36 | //calling sumSubstrings() function 37 | cout << ob.sumSubstrings(s) << endl; 38 | 39 | } 40 | return 0; 41 | } 42 | 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/04-2024(april)/05-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int lis(vector& nums){ 11 | int n = nums.size(); 12 | vector dp(n,1); 13 | for(int i=1;inums[j] && (nums[i]-nums[j])>=(i-j)) 16 | dp[i] = max(1+dp[j],dp[i]); 17 | 18 | int out = 1; 19 | for(auto i: dp) 20 | out = max(out, i); 21 | 22 | return out; 23 | } 24 | int min_operations(vector& nums) { 25 | return nums.size() - lis(nums); 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | int main() { 32 | int tc; 33 | cin >> tc; 34 | while (tc--) { 35 | int n; 36 | cin >> n; 37 | vector nums(n); 38 | for (int i = 0; i < n; i++) 39 | cin >> nums[i]; 40 | Solution ob; 41 | int ans = ob.min_operations(nums); 42 | cout << ans << "\n"; 43 | } 44 | return 0; 45 | } 46 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/04-2024(april)/06 - Count ways to N'th Stair.md: -------------------------------------------------------------------------------- 1 | ## 06. Count ways to N'th Stair 2 | The problem can be found at the following link: [Question Link](https://www.geeksforgeeks.org/problems/count-ways-to-nth-stairorder-does-not-matter1322/1) 3 | 4 | ### My Approach 5 | To count the ways to the N'th stair, we observe that for each step, we can either take 1 step or 2 steps. This forms a Fibonacci sequence, where the N'th element represents the number of ways to reach the N'th stair. So, our approach is to return (N/2) + 1. 6 | 7 | ### Time and Auxiliary Space Complexity 8 | 9 | - **Time Complexity**: O(1) 10 | - **Auxiliary Space Complexity**: O(1) 11 | 12 | ### Code (C++) 13 | ```cpp 14 | class Solution { 15 | public: 16 | long long countWays(int n) { 17 | return n / 2 + 1; 18 | } 19 | }; 20 | ``` 21 | 22 | ### Contribution and Support 23 | 24 | For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment. 25 | 26 | If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository. -------------------------------------------------------------------------------- /2024/04-2024(april)/06-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | long long countWays(int n) { 11 | return n / 2 + 1; 12 | } 13 | }; 14 | 15 | 16 | //{ Driver Code Starts. 17 | int main() { 18 | // taking count of testcases 19 | int t; 20 | cin >> t; 21 | 22 | while (t--) { 23 | // taking stair count 24 | int n; 25 | cin >> n; 26 | Solution ob; 27 | cout << ob.countWays(n) << endl; // Print the output from our pre computed array 28 | } 29 | return 0; 30 | } 31 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/04-2024(april)/07-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | int maxDotProduct(int n, int m, int a[], int b[]) 10 | { 11 | vector> dp(n + 1, vector(m + 1, 0)); 12 | 13 | for (int j = 1; j <= m; ++j) 14 | dp[0][j] = INT_MIN; 15 | 16 | for (int i = 1; i <= n; ++i) 17 | for (int j = 1; j <= m; ++j) 18 | dp[i][j] = max(dp[i - 1][j], dp[i - 1][j - 1] + a[i - 1] * b[j - 1]); 19 | 20 | return dp[n][m]; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | int main() 27 | { 28 | 29 | 30 | int t; 31 | cin >> t; 32 | while (t--) 33 | { 34 | int n, m; 35 | cin >> n >> m; 36 | 37 | int a[n], b[m]; 38 | 39 | for(int i = 0; i < n; i++) 40 | cin >> a[i]; 41 | 42 | for(int i = 0; i < m; i++) 43 | cin >> b[i]; 44 | 45 | 46 | 47 | Solution ob; 48 | cout << ob.maxDotProduct(n, m, a, b) << "\n"; 49 | 50 | } 51 | return 0; 52 | } 53 | 54 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/04-2024(april)/08-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | vector> dp; 10 | 11 | long long solve(int arr[], int i, int j, int& n) { 12 | if (i >= n || j < 0 || j - i + 1 <= 0) 13 | return 0; 14 | 15 | if (dp[i][j] != -1) 16 | return dp[i][j]; 17 | 18 | long long d1, d2; 19 | d1 = arr[i] + min(solve(arr, i + 2, j, n), solve(arr, i + 1, j - 1, n)); 20 | d2 = arr[j] + min(solve(arr, i + 1, j - 1, n), solve(arr, i, j - 2, n)); 21 | return dp[i][j] = max(d1, d2); 22 | } 23 | 24 | long long maximumAmount(int n, int arr[]) { 25 | dp = vector>(1001, vector(1001, -1)); 26 | return solve(arr, 0, n - 1, n); 27 | } 28 | }; 29 | 30 | 31 | //{ Driver Code Starts. 32 | int main() 33 | { 34 | 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | int n; 40 | cin>>n; 41 | int a[n]; 42 | for(int i=0;i>a[i]; 44 | Solution ob; 45 | cout<< ob.maximumAmount(n,a)< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | //User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | int findSingle(int n, int arr[]) 13 | { 14 | int ans=0; 15 | for (int i=0;i>t; 29 | while(t--) 30 | { 31 | int N, X; 32 | cin >> N; 33 | int arr[N]; 34 | for(int i = 0; i < N; i++){ 35 | cin >> arr[i]; 36 | } 37 | 38 | Solution ob; 39 | cout << ob.findSingle(N, arr) << endl; 40 | } 41 | return 0; 42 | } 43 | // } Driver Code Ends 44 | -------------------------------------------------------------------------------- /2024/04-2024(april)/11-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | int grayToBinary(int n) 13 | { 14 | int ans = 0; 15 | while(n > 0) 16 | { 17 | ans = ans ^ n; 18 | n = n >> 1; 19 | } 20 | return ans; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | 27 | // Driver code 28 | int main() 29 | { 30 | int t,n; 31 | cin>>t;//testcases 32 | while(t--) 33 | { 34 | cin>>n;//initializing n 35 | 36 | Solution ob; 37 | //calling function grayToBinary() 38 | cout<< ob.grayToBinary(n)< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | long long pairAndSum(int n, long long arr[]) { 12 | long long out = 0; 13 | for (int i = 0; i < 32; ++i) { 14 | long long cnt = 0; 15 | for (int j = 0; j < n; ++j) 16 | if (arr[j] & (1 << i)) 17 | cnt++; 18 | long long pairs = (cnt * (cnt - 1) / 2); 19 | out += pairs * (1 << i); 20 | } 21 | return out; 22 | } 23 | }; 24 | 25 | 26 | //{ Driver Code Starts. 27 | int main() { 28 | int t; 29 | cin >> t; 30 | while (t--) { 31 | int N; 32 | cin >> N; 33 | long long Arr[N]; 34 | for (int i = 0; i < N; i++) { 35 | cin >> Arr[i]; 36 | } 37 | Solution ob; 38 | cout << ob.pairAndSum(N, Arr) << endl; 39 | } 40 | return 0; 41 | } 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/04-2024(april)/13-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | long long reversedBits(long long x) { 10 | long long out = 0; 11 | for(int i = 0; i < 31; ++i) 12 | out += x & 1, 13 | out <<= 1, 14 | x >>= 1; 15 | 16 | return out; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | int main() { 23 | int t; 24 | cin >> t; 25 | while (t--) { 26 | long long X; 27 | 28 | cin>>X; 29 | 30 | Solution ob; 31 | cout << ob.reversedBits(X) << endl; 32 | } 33 | return 0; 34 | } 35 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/04-2024(april)/14-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | // User function Template for C++ 10 | 11 | class Solution { 12 | public: 13 | void printArr(int n, int arr[]) 14 | { 15 | // Your code for printing array here 16 | for (int i=0;i> t; 42 | while (t--) { 43 | int n; 44 | cin >> n; 45 | int arr[n]; 46 | for (int i = 0; i < n; i++) { 47 | cin >> arr[i]; 48 | } 49 | Solution obj; 50 | obj.xor1ToN(n, arr); 51 | obj.printArr(n, arr); 52 | obj.setToZero(n, arr); 53 | obj.printArr(n, arr); 54 | } 55 | return 0; 56 | } 57 | 58 | // } Driver Code Ends 59 | -------------------------------------------------------------------------------- /2024/04-2024(april)/17-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | #include 8 | #include 9 | 10 | using namespace std; 11 | using namespace __gnu_pbds; 12 | typedef tree, rb_tree_tag, tree_order_statistics_node_update> orderedMultiSet; 13 | 14 | class Solution { 15 | public: 16 | int countPairs(int arr[], int n) { 17 | orderedMultiSet st; 18 | st.insert(arr[n - 1] * (n - 1)); 19 | int ans = 0; 20 | 21 | for (int i = n - 2; i >= 0; --i) { 22 | st.insert(arr[i] * i); 23 | ans += st.order_of_key(arr[i] * i); 24 | } 25 | 26 | return ans; 27 | } 28 | }; 29 | 30 | 31 | //{ Driver Code Starts. 32 | // Driver code 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | int n; 40 | cin>>n; 41 | int a[n]; 42 | for(int i=0;i>a[i]; 44 | Solution ob; 45 | cout< 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | vector twoRepeated(int arr[], int n) { 13 | vector out; 14 | for (int i = 0; i < n + 2; ++i) { 15 | if (arr[abs(arr[i])] > 0) 16 | arr[abs(arr[i])] = -arr[abs(arr[i])]; 17 | else 18 | out.push_back(abs(arr[i])); 19 | } 20 | return out; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main() 28 | { 29 | int t,n; 30 | cin>>t; 31 | 32 | while(t--) 33 | { 34 | cin>>n; 35 | 36 | int a[n+2]; 37 | 38 | for(int i=0;i>a[i]; 40 | 41 | Solution obj; 42 | vector res; 43 | res = obj.twoRepeated(a, n); 44 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | 7 | class Solution { 8 | public: 9 | vector findMissing(int a[], int b[], int n, int m) 10 | { 11 | unordered_set s; 12 | vector out; 13 | for(int i = 0; i < m; ++i) 14 | s.insert(b[i]); 15 | 16 | for(int i = 0; i < n; ++i) 17 | if(!s.count(a[i])) 18 | out.push_back(a[i]); 19 | 20 | return out; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main() 28 | { 29 | 30 | 31 | int t; 32 | cin >> t; 33 | while (t--) 34 | { 35 | int n; 36 | cin>>n; 37 | int m; 38 | cin>>m; 39 | int a[n]; 40 | int b[m]; 41 | for(int i=0;i>a[i]; 43 | for(int i=0;i> b[i]; 45 | vector ans; 46 | 47 | 48 | Solution ob; 49 | ans=ob.findMissing(a,b,n,m); 50 | for(int i=0;i 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | int minRow(int n, int m, vector> a) { 12 | int out = 0; 13 | for(int i = 0; i < n; ++i){ 14 | for(int j = 1; j < m; ++j) 15 | a[i][j] += a[i][j-1]; 16 | if(a[out].back() > a[i].back()) 17 | out = i; 18 | } 19 | return out + 1; 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | 26 | int main() { 27 | int t; 28 | cin >> t; 29 | while (t--) { 30 | int N, M; 31 | cin >> N >> M; 32 | vector> A(N, vector(M)); 33 | for (int i = 0; i < N; i++) { 34 | for (int j = 0; j < M; j++) { 35 | cin >> A[i][j]; 36 | } 37 | } 38 | Solution ob; 39 | cout << ob.minRow(N, M, A) << "\n"; 40 | } 41 | } 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/04-2024(april)/23-april.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | #include 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | // User function Template for C++ 9 | class Solution { 10 | public: 11 | int firstElement(int n) 12 | { 13 | int prev1=0, prev2=1; 14 | for (int i=2;i<=n;i++) 15 | { 16 | int curr=(prev1+prev2)%1000000007; 17 | prev1=prev2; 18 | prev2=curr; 19 | } 20 | return prev2; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | int main() { 27 | int t; 28 | cin >> t; 29 | while (t--) { 30 | int n; 31 | cin >> n; 32 | Solution ob; 33 | cout << ob.firstElement(n) << endl; 34 | } 35 | return 0; 36 | } 37 | 38 | // } Driver Code Ends 39 | -------------------------------------------------------------------------------- /2024/04-2024(april)/readme.md: -------------------------------------------------------------------------------- 1 | ## April 2024 2 | 3 | Welcome! to the directory that have the solutions for the GeeksforGeeks (GFG) Problem of the Day for the month of **April 2024**. 4 | 5 | Feel free to navigate through the directory and examine the solutions for each problem. Remember that understanding the logic behind each solution is crucial for your growth as a programmer. 6 | 7 | To access the original problem statements, please visit the **GFG [Problem of the Day](https://practice.geeksforgeeks.org/problem-of-the-day)** page. -------------------------------------------------------------------------------- /2024/05-2024(may)/18-may.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | int findPeakElement(vector& a) 9 | { 10 | int size=a.size(); 11 | int low=0, high=size-1; 12 | while (low<=high) 13 | { 14 | int mid=(low+high)/2; 15 | if (a[mid]>a[mid-1] && a[mid]>a[mid+1]) 16 | return a[mid]; 17 | else if (a[mid]>a[mid-1] && a[mid]> T; 31 | while(T--) 32 | { 33 | int n; 34 | cin >> n; 35 | vectora(n); 36 | for(int i = 0; i < n; i++) 37 | cin>>a[i]; 38 | Solution ob; 39 | int ans = ob.findPeakElement(a); 40 | cout << ans << "\n"; 41 | } 42 | return 0; 43 | } 44 | 45 | 46 | // } Driver Code Ends 47 | -------------------------------------------------------------------------------- /2024/05-2024(may)/20-may.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution 7 | { 8 | public: 9 | long long int PowMod(long long int x,long long int n,long long int M) 10 | { 11 | long long int ans=1; 12 | x%=M; 13 | while (n>0) 14 | { 15 | if (n%2==1) 16 | ans=(ans*x)%M; 17 | n>>=1; 18 | x=(x*x)%M; 19 | } 20 | return ans; 21 | } 22 | }; 23 | 24 | //{ Driver Code Starts. 25 | int main(){ 26 | int T; 27 | cin >> T; 28 | while(T--) 29 | { 30 | long long int x, n, m; 31 | cin >> x >> n >> m; 32 | Solution ob; 33 | long long int ans = ob.PowMod(x, n, m); 34 | cout << ans <<"\n"; 35 | } 36 | return 0; 37 | } 38 | 39 | // } Driver Code Ends 40 | -------------------------------------------------------------------------------- /2024/06-2024(june)/01-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | string oddEven(string s) { 11 | vector c(26, 0); 12 | for(int i = 0; i < s.size(); ++i) 13 | ++c[s[i] - 'a']; 14 | 15 | int cnt = 0; 16 | for(int i = 0; i < 26; ++i) 17 | if(c[i] && c[i]%2 == (i+1)%2) 18 | ++cnt; 19 | 20 | return cnt%2?"ODD":"EVEN"; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main() { 28 | int t; 29 | scanf("%d ", &t); 30 | while (t--) { 31 | 32 | string s; 33 | getline(cin, s); 34 | 35 | Solution obj; 36 | string res = obj.oddEven(s); 37 | 38 | cout << res << "\n"; 39 | } 40 | } 41 | 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/06-2024(june)/03-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int numberOfConsecutiveOnes(int n) { 11 | int mod = 1e9+7; 12 | long out = 1; 13 | int x = 0, y = 1; 14 | for(int i = 3; i <= n ;++i){ 15 | out = (out*2 + x + y)%mod; 16 | int z = x; 17 | x = y; 18 | y = (x + z)%mod; 19 | } 20 | return out; 21 | } 22 | }; 23 | 24 | 25 | //{ Driver Code Starts. 26 | int main() { 27 | int t; 28 | cin >> t; 29 | while (t--) { 30 | int N; 31 | cin >> N; 32 | Solution ob; 33 | cout << ob.numberOfConsecutiveOnes(N) << endl; 34 | } 35 | return 0; 36 | } 37 | 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/06-2024(june)/04-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | 3 | #include 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution { 10 | public: 11 | string binaryNextNumber(string s) { 12 | int n = s.size(); 13 | int i = n - 1; 14 | while(i >= 0 && s[i] == '1') 15 | s[i--] = '0'; 16 | 17 | if(i == -1) 18 | s = "1" + s; 19 | else 20 | s[i] = '1'; 21 | 22 | i = 0; 23 | while(s[i] == '0') 24 | ++i; 25 | 26 | return s.substr(i); 27 | } 28 | }; 29 | 30 | 31 | //{ Driver Code Starts. 32 | int main() { 33 | int t; 34 | cin >> t; 35 | while (t--) { 36 | string s; 37 | cin >> s; 38 | Solution ob; 39 | cout << ob.binaryNextNumber(s); 40 | cout << "\n"; 41 | } 42 | 43 | return 0; 44 | } 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /2024/06-2024(june)/05-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | class Solution { 8 | 9 | public: 10 | int findSwapValues(int a[], int n, int b[], int m) 11 | { 12 | set st; 13 | int suma = 0; 14 | int sumb = 0; 15 | for(int i = 0 ; i < n ; i++ ) 16 | { 17 | suma += a[i]; 18 | st.insert(a[i]); 19 | } 20 | 21 | for(int i = 0 ; i < m ; i++ ) 22 | sumb += b[i]; 23 | 24 | if((sumb-suma)%2 != 0) return -1; 25 | 26 | 27 | for(int i = 0 ; i < m ; i++ ) 28 | if(st.find((suma-sumb)/2+b[i]) != st.end()) 29 | return 1; 30 | 31 | return -1; 32 | } 33 | }; 34 | 35 | //{ Driver Code Starts. 36 | 37 | int main() { 38 | 39 | int t; 40 | cin >> t; 41 | while (t--) { 42 | int n, m; 43 | cin >> n >> m; 44 | int a[n]; 45 | int b[m]; 46 | for (int i = 0; i < n; i++) 47 | cin >> a[i]; 48 | for (int i = 0; i < m; i++) 49 | cin >> b[i]; 50 | 51 | Solution ob; 52 | cout << ob.findSwapValues(a, n, b, m); 53 | cout << "\n"; 54 | } 55 | return 0; 56 | } 57 | 58 | // } Driver Code Ends 59 | -------------------------------------------------------------------------------- /2024/06-2024(june)/06-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | /*You are required to complete this method*/ 8 | class Solution { 9 | public: 10 | long long max_sum(int a[], int n) 11 | { 12 | long long sum = 0, org = 0; 13 | for(int i=0; i=0; i--) 20 | { 21 | org += sum - (1LL * a[i] * n); 22 | ans = max(ans, org); 23 | } 24 | return ans; 25 | } 26 | }; 27 | 28 | 29 | //{ Driver Code Starts. 30 | int main() { 31 | int T; 32 | cin >> T; 33 | while (T--) { 34 | int N; 35 | cin >> N; 36 | int A[N]; 37 | for (int i = 0; i < N; i++) { 38 | cin >> A[i]; 39 | } 40 | Solution ob; 41 | cout << ob.max_sum(A, N) << endl; 42 | /*keeping track of the total sum of the array*/ 43 | } 44 | } 45 | 46 | // } Driver Code Ends 47 | -------------------------------------------------------------------------------- /2024/06-2024(june)/08-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | class Solution { 8 | public: 9 | int findExtra(int n, int arr1[], int arr2[]) 10 | { 11 | int left = 0, right = n-1; 12 | 13 | while(left < right) 14 | { 15 | int mid = left + (right - left) / 2; 16 | if(arr1[mid] == arr2[mid]) 17 | left = mid + 1; 18 | else right = mid; 19 | } 20 | 21 | if(arr1[n-1] != arr2[n-2]) 22 | return n-1; 23 | 24 | return left; 25 | } 26 | }; 27 | 28 | //{ Driver Code Starts. 29 | int main() { 30 | int t; 31 | cin >> t; 32 | while (t--) { 33 | int n; 34 | cin >> n; 35 | int arr1[n], arr2[n - 1]; 36 | for (int i = 0; i < n; i++) { 37 | cin >> arr1[i]; 38 | } 39 | for (int i = 0; i < n - 1; i++) { 40 | cin >> arr2[i]; 41 | } 42 | Solution obj; 43 | cout << obj.findExtra(n, arr1, arr2) << endl; 44 | } 45 | } 46 | // } Driver Code Ends 47 | -------------------------------------------------------------------------------- /2024/06-2024(june)/10-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | // User function template for C++ 9 | class Solution { 10 | public: 11 | 12 | void matchPairs(int n, char nuts[], char bolts[]) 13 | { 14 | sort(nuts, nuts+n); 15 | sort(bolts, bolts+n); 16 | } 17 | }; 18 | 19 | //{ Driver Code Starts. 20 | 21 | int main() { 22 | int t; 23 | cin >> t; 24 | while (t--) { 25 | int n; 26 | cin >> n; 27 | char nuts[n], bolts[n]; 28 | for (int i = 0; i < n; i++) { 29 | cin >> nuts[i]; 30 | } 31 | for (int i = 0; i < n; i++) { 32 | cin >> bolts[i]; 33 | } 34 | Solution ob; 35 | ob.matchPairs(n, nuts, bolts); 36 | for (int i = 0; i < n; i++) { 37 | cout << nuts[i] << " "; 38 | } 39 | cout << "\n"; 40 | for (int i = 0; i < n; i++) { 41 | cout << bolts[i] << " "; 42 | } 43 | cout << "\n"; 44 | } 45 | return 0; 46 | } 47 | 48 | // } Driver Code Ends 49 | -------------------------------------------------------------------------------- /2024/06-2024(june)/12-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | bool func(int num) 11 | { 12 | while (num) 13 | { 14 | if (num%10==4) 15 | return true; 16 | num/=10; 17 | } 18 | return false; 19 | } 20 | int countNumberswith4(int n) 21 | { 22 | int ans=0; 23 | for (int i=1;i<=n;i++) 24 | { 25 | if (func(i)) 26 | ans++; 27 | } 28 | return ans; 29 | } 30 | }; 31 | 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() { 36 | int t; 37 | scanf("%d ", &t); 38 | while (t--) { 39 | 40 | int n; 41 | scanf("%d", &n); 42 | 43 | Solution obj; 44 | int res = obj.countNumberswith4(n); 45 | 46 | cout << res << endl; 47 | } 48 | } 49 | 50 | // } Driver Code Ends 51 | -------------------------------------------------------------------------------- /2024/06-2024(june)/13-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | int mod=1e9+7; 11 | int padovanSequence(int n) 12 | { 13 | vector p(n+1,1); 14 | for(int i=3;i<=n;i++) 15 | p[i]=(p[i-2]+p[i-3])%mod; 16 | return p[n]; 17 | } 18 | }; 19 | 20 | 21 | //{ Driver Code Starts. 22 | int main() { 23 | int t; 24 | cin >> t; 25 | while (t--) { 26 | int n; 27 | cin >> n; 28 | Solution ob; 29 | cout << ob.padovanSequence(n) << endl; 30 | } 31 | return 0; 32 | } 33 | // } Driver Code Ends 34 | -------------------------------------------------------------------------------- /2024/06-2024(june)/14-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | #include 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | // User function Template for C++ 9 | class Solution { 10 | public: 11 | int func (int num) 12 | { 13 | int ans=0; 14 | while (num) 15 | { 16 | ans+=(num%10)*(num%10)*(num%10); 17 | num/=10; 18 | } 19 | return ans; 20 | } 21 | string armstrongNumber(int n) 22 | { 23 | if (func(n)==n) 24 | return "Yes"; 25 | else return "No"; 26 | } 27 | }; 28 | 29 | 30 | //{ Driver Code Starts. 31 | int main() { 32 | int t; 33 | cin >> t; 34 | while (t--) { 35 | int n; 36 | cin >> n; 37 | Solution ob; 38 | cout << ob.armstrongNumber(n) << endl; 39 | } 40 | return 0; 41 | } 42 | 43 | // } Driver Code Ends 44 | -------------------------------------------------------------------------------- /2024/06-2024(june)/18-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | // User function Template for C++ 10 | 11 | class Solution { 12 | public: 13 | int rectanglesInCircle(int r) 14 | { 15 | int limit = 4*r*r; 16 | int numRects = 0; 17 | int tempRects = -1; 18 | int diff = -1; 19 | for(int l = 1; ; ++l) 20 | { 21 | diff = limit - l*l; 22 | if(diff <= 0) 23 | break; 24 | tempRects = (int)pow(diff, 0.5); 25 | if(tempRects == 0) 26 | break; 27 | numRects += tempRects; 28 | } 29 | return numRects; 30 | } 31 | }; 32 | 33 | 34 | //{ Driver Code Starts. 35 | int main() { 36 | int t; 37 | cin >> t; 38 | while (t--) { 39 | int n; 40 | cin >> n; 41 | Solution ob; 42 | int ans = ob.rectanglesInCircle(n); 43 | cout << ans << "\n"; 44 | } 45 | } 46 | // } Driver Code Ends 47 | -------------------------------------------------------------------------------- /2024/06-2024(june)/19-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | double maxVolume(double P, double A) 11 | { 12 | double discriminant = P * P - 24 * A; 13 | if (discriminant < 0) 14 | return -1; 15 | double length = (P - sqrt(discriminant)) / 12; 16 | double height = P / 4 - 2 * length; 17 | double volume = length * length * height; 18 | volume = round(volume * 100) / 100; 19 | return volume; 20 | } 21 | }; 22 | 23 | //{ Driver Code Starts. 24 | int main() { 25 | long long int t; 26 | cin >> t; 27 | while (t--) { 28 | double perimeter, area; 29 | cin >> perimeter >> area; 30 | // if (area == 5 and parameter == 15) { 31 | // cout << "0.46" << endl; 32 | // return 0; 33 | // } 34 | Solution ob; 35 | double ans = ob.maxVolume(perimeter, area); 36 | cout.precision(2); 37 | cout << fixed << ans << "\n"; 38 | } 39 | } 40 | // } Driver Code Ends 41 | -------------------------------------------------------------------------------- /2024/06-2024(june)/22-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | // User function Template for C++ 10 | 11 | class Solution { 12 | public: 13 | long long ExtractNumber(string sentence) 14 | { 15 | long long maxNumber = -1; 16 | stringstream ss(sentence); 17 | string word; 18 | while (ss >> word) 19 | { 20 | if (all_of(word.begin(), word.end(), ::isdigit)) 21 | { 22 | long long num = stoll(word); 23 | if (word.find('9') == string::npos) 24 | { 25 | if (num > maxNumber) 26 | maxNumber = num; 27 | } 28 | } 29 | } 30 | return maxNumber; 31 | } 32 | }; 33 | 34 | //{ Driver Code Starts. 35 | int main() { 36 | 37 | ios_base::sync_with_stdio(0); 38 | cin.tie(NULL); 39 | cout.tie(NULL); 40 | 41 | int t; 42 | string tc; 43 | getline(cin, tc); 44 | t = stoi(tc); 45 | while (t--) { 46 | string s; 47 | getline(cin, s); 48 | 49 | Solution ob; 50 | cout << ob.ExtractNumber(s) << "\n"; 51 | } 52 | 53 | return 0; 54 | } 55 | 56 | // } Driver Code Ends 57 | -------------------------------------------------------------------------------- /2024/06-2024(june)/23-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | // User function template for C++ 8 | class Solution { 9 | public: 10 | 11 | vector bracketNumbers(string str) 12 | { 13 | vector ans; 14 | stack st; 15 | int index = 0; 16 | for(int i=0; i ans = ob.bracketNumbers(s); 52 | 53 | for (auto i : ans) 54 | cout << i << " "; 55 | 56 | cout << "\n"; 57 | } 58 | 59 | return 0; 60 | } 61 | // } Driver Code Ends 62 | -------------------------------------------------------------------------------- /2024/06-2024(june)/24-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | long long sumMatrix(long long n, long long q) 11 | { 12 | if(q>(2*n)) 13 | return 0; 14 | return (n-abs(n+1-q)); 15 | } 16 | }; 17 | 18 | //{ Driver Code Starts. 19 | int main() { 20 | int t; 21 | cin >> t; 22 | while (t--) { 23 | long long n,q; 24 | 25 | cin>>n>>q; 26 | 27 | Solution ob; 28 | cout << ob.sumMatrix(n,q) << endl; 29 | } 30 | return 0; 31 | } 32 | // } Driver Code Ends 33 | -------------------------------------------------------------------------------- /2024/06-2024(june)/27-june.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | bool isToeplitz(vector> &mat); 6 | 7 | int main() { 8 | // your code goes here 9 | int t; 10 | cin >> t; 11 | while (t--) { 12 | int n, m; 13 | cin >> n >> m; 14 | vector> a(n, vector(m, 0)); 15 | 16 | for (int i = 0; i < n; i++) { 17 | for (int j = 0; j < m; j++) 18 | cin >> a[i][j]; 19 | } 20 | 21 | bool b = isToeplitz(a); 22 | 23 | if (b == true) 24 | cout << "true"; 25 | else 26 | cout << "false"; 27 | 28 | cout << endl; 29 | } 30 | return 0; 31 | } 32 | // } Driver Code Ends 33 | 34 | 35 | 36 | bool isToeplitz(vector>& mat) 37 | { 38 | int n = mat.size(); 39 | int m = mat[0].size(); 40 | for(int i=0; i 4 | 5 | ## 01. Question Name 6 | 7 | The problem can be found at the following link: [Question Link](https://practice.geeksforgeeks.org/problems/find-triplets-with-zero-sum/1) 8 | 9 | 10 | ### My Approach 11 | 12 | - step 1 13 | - step 2 14 | - step 3 15 | - ...so on 16 | 17 | ### Time and Auxiliary Space Complexity 18 | 19 | - **Time Complexity** : ..... 20 | - **Auxiliary Space Complexity** : ..... 21 | 22 | ### Code (C++) 23 | ```cpp 24 | 25 | class Solution { 26 | public: 27 | 28 | }; 29 | 30 | ``` 31 | 32 | ### Contribution and Support 33 | 34 | For discussions, questions, or doubts related to this solution, please visit our [discussion section](https://github.com/getlost01/gfg-potd/discussions). We welcome your input and aim to foster a collaborative learning environment. 35 | 36 | If you find this solution helpful, consider supporting us by giving a `⭐ star` to the [getlost01/gfg-potd](https://github.com/getlost01/gfg-potd) repository. 37 | --------------------------------------------------------------------------------