├── .github ├── CODEOWNERS ├── ISSUE_TEMPLATE │ ├── Bug Report - Java.yml │ ├── Bug Report - Python3.yml │ ├── Bug Report - README.yaml │ ├── Bug report - C++.yml │ └── config.yml └── pull_request_template.md ├── .gitignore ├── CODE_OF_CONDUCT.md ├── CONTRIBUTING.md ├── LICENSE ├── Problems & Solutions ├── 0-9 │ ├── 2D Hopscotch │ │ └── 16 December 2022 │ │ │ ├── C++ │ │ │ └── 2D Hopscotch.cpp │ │ │ ├── Java │ │ │ └── 2D Hopscotch.java │ │ │ ├── Python │ │ │ └── 2D Hopscotch.py │ │ │ └── README.md │ └── 3 Divisors │ │ └── 08 December 2022 │ │ ├── C++ │ │ └── 3 Divisors.cpp │ │ ├── Java │ │ └── 3 Divisors.java │ │ ├── Python │ │ └── 3 Divisors.py │ │ └── README.md ├── A │ ├── A Game of LCM │ │ └── 18 December 2022 │ │ │ ├── C++ │ │ │ └── A Game of LCM.cpp │ │ │ ├── Java │ │ │ └── A Game of LCM.java │ │ │ ├── Python │ │ │ └── A Game of LCM.py │ │ │ └── README.md │ ├── A difference of values and indexes │ │ └── 20 December 2022 │ │ │ ├── C++ │ │ │ └── A difference of values and indexes.cpp │ │ │ ├── Java │ │ │ └── A difference of values and indexes.java │ │ │ ├── Python │ │ │ └── A difference of values and indexes.py │ │ │ └── README.md │ ├── Absolute List Sorting │ │ └── 21 December 2022 │ │ │ ├── C++ │ │ │ └── Absolute List Sorting.cpp │ │ │ ├── Java │ │ │ └── Absolute List Sorting.java │ │ │ ├── Python │ │ │ └── Absolute List Sorting.py │ │ │ └── README.md │ ├── Absolute difference divisible by K │ │ └── 08 January 2023 │ │ │ ├── C++ │ │ │ └── Absolute difference divisible by K.cpp │ │ │ ├── Java │ │ │ └── Absolute difference divisible by K.java │ │ │ ├── Python │ │ │ └── Absolute difference divisible by K.py │ │ │ └── README.md │ ├── Add Binary Strings │ │ └── 27 November 2022 │ │ │ ├── C++ │ │ │ └── Add Binary Strings.cpp │ │ │ ├── Java │ │ │ └── Add Binary Strings.java │ │ │ ├── Python │ │ │ └── Add Binary Strings.py │ │ │ └── README.md │ ├── Aggressive Cows │ │ └── 03 December 2022 │ │ │ ├── C++ │ │ │ └── Aggressive Cows.cpp │ │ │ ├── Java │ │ │ └── Aggressive Cows.java │ │ │ ├── Python │ │ │ └── Aggressive Cows.py │ │ │ └── README.md │ ├── Akku and Binary Numbers │ │ └── 26 December 2022 │ │ │ ├── C++ │ │ │ └── Akku and Binary Numbers.cpp │ │ │ ├── Java │ │ │ └── Akku and Binary Numbers.java │ │ │ ├── Python │ │ │ └── Akku and Binary Numbers.py │ │ │ └── README.md │ ├── Alex Travelling │ │ └── 19 December 2022 │ │ │ ├── C++ │ │ │ └── Alex Travelling.cpp │ │ │ ├── Java │ │ │ └── Alex Travelling.java │ │ │ ├── Python │ │ │ └── Alex Travelling.py │ │ │ ├── README.md │ │ │ └── assets │ │ │ └── images │ │ │ ├── index.png │ │ │ └── index2.png │ ├── Alternate Vowel and Consonant String │ │ └── 04 December 2022 │ │ │ ├── C++ │ │ │ └── Alternate Vowel and Consonant String.cpp │ │ │ ├── Java │ │ │ └── Alternate Vowel and Consonant String.java │ │ │ ├── Python │ │ │ └── Alternate Vowel and Consonant String.py │ │ │ └── README.md │ ├── Apple Sequences │ │ └── 18 February 2023 │ │ │ ├── Apple Sequences.cpp │ │ │ └── README.md │ ├── Arithmetic Number │ │ └── 13 February 2023 │ │ │ ├── Arithmetic Number.cpp │ │ │ ├── Arithmetic Number.js │ │ │ ├── Arithmetic Number.py │ │ │ └── README.md │ ├── Array Pair Sum Divisibility Problem │ │ └── 11 December 2022 │ │ │ ├── C++ │ │ │ └── Array Pair Sum Divisibility Problem.cpp │ │ │ ├── Java │ │ │ └── Array Pair Sum Divisibility Problem.java │ │ │ ├── Python │ │ │ └── Array Pair Sum Divisibility Problem.py │ │ │ └── README.md │ ├── Array Removals │ │ └── 03 November 2022 │ │ │ ├── C++ │ │ │ └── Array Removals.cpp │ │ │ ├── Java │ │ │ └── Array Removals.java │ │ │ ├── Python │ │ │ └── Array Removals.py │ │ │ └── README.md │ ├── Articulation Point - I │ │ └── 12 December 2022 │ │ │ ├── C++ │ │ │ └── Articulation Point - I.cpp │ │ │ ├── Java │ │ │ └── Articulation Point - I.java │ │ │ ├── Python │ │ │ └── Articulation Point - I.py │ │ │ ├── README.md │ │ │ └── assets │ │ │ ├── index.png │ │ │ ├── index1.png │ │ │ └── index2.png │ ├── Asteroid Collision │ │ └── 29 December 2022 │ │ │ ├── C++ │ │ │ └── Asteroid Collision.cpp │ │ │ ├── Java │ │ │ └── Asteroid Collision.java │ │ │ ├── Python │ │ │ └── Asteroid Collision.py │ │ │ └── README.md │ └── Avoid Explosion │ │ └── 05 March 2023 │ │ ├── Avoid Explosion.cpp │ │ └── README.md ├── B │ ├── BST Downward Traversal │ │ └── 06 February 2023 │ │ │ ├── BST Downward Traversal.cpp │ │ │ ├── BST Downward Traversal.java │ │ │ ├── BST Downward Traversal.py │ │ │ └── README.md │ ├── Balanced string │ │ └── 15 December 2022 │ │ │ ├── C++ │ │ │ └── Balanced string.cpp │ │ │ ├── Java │ │ │ └── Balanced string.java │ │ │ ├── Python │ │ │ └── Balanced string.py │ │ │ └── README.md │ ├── Balloon Everywhere │ │ └── 10 February 2023 │ │ │ ├── Balloon Everywhere.cpp │ │ │ └── README.md │ ├── Base Equivalence │ │ └── 04 November 2022 │ │ │ ├── C++ │ │ │ └── Base Equivalence.cpp │ │ │ ├── Java │ │ │ └── Base Equivalence.java │ │ │ ├── Python │ │ │ └── Base Equivalence.py │ │ │ └── README.md │ ├── Best Node │ │ └── 04 March 2023 │ │ │ ├── Best Node.cpp │ │ │ └── README.md │ ├── Binary matrix having maximum number of 1s │ │ └── 12 March 2023 │ │ │ ├── Binary matrix having maximum number of 1s.cpp │ │ │ └── README.md │ ├── Black and White │ │ └── 09 December 2022 │ │ │ ├── C# │ │ │ └── Black and White.cs │ │ │ ├── C++ │ │ │ └── Black and White.cpp │ │ │ ├── Java │ │ │ └── Black and White.java │ │ │ ├── JavaScript │ │ │ └── Black and White.js │ │ │ ├── Python │ │ │ └── Black and White.py │ │ │ └── README.md │ ├── Break a number │ │ └── 17 December 2022 │ │ │ ├── C++ │ │ │ └── Break a number.cpp │ │ │ ├── Java │ │ │ └── Break a number.java │ │ │ ├── Python │ │ │ └── Break a number.py │ │ │ └── README.md │ ├── Build the smallest │ │ └── 10 December 2022 │ │ │ ├── C++ │ │ │ └── Build the smallest.cpp │ │ │ ├── Java │ │ │ └── Build the smallest.java │ │ │ ├── Python │ │ │ └── Build the smallest.py │ │ │ └── README.md │ └── Burst Balloons │ │ └── 23 December 2022 │ │ ├── C++ │ │ └── Burst Balloons.cpp │ │ ├── Java │ │ └── Burst Balloons.java │ │ ├── Python │ │ └── Burst Balloons.py │ │ └── README.md ├── C │ ├── Carpet into Box │ │ └── 19 January 2023 │ │ │ ├── Carpet into Box.cpp │ │ │ ├── Carpet into Box.java │ │ │ ├── Carpet into Box.py │ │ │ └── README.md │ ├── Case-specific Sorting of Strings │ │ └── 26 January 2023 │ │ │ ├── Case-specific Sorting of Strings.cpp │ │ │ ├── Case-specific Sorting of Strings.java │ │ │ ├── Case-specific Sorting of Strings.js │ │ │ ├── Case-specific Sorting of Strings.py │ │ │ └── README.md │ ├── Check if all levels of two trees are anagrams or not │ │ └── 19 November 2022 │ │ │ ├── C++ │ │ │ └── Check if all levels of two trees are anagrams or not.cpp │ │ │ ├── Java │ │ │ └── Check if all levels of two trees are anagrams or not.java │ │ │ ├── Python │ │ │ └── Check if all levels of two trees are anagrams or not.py │ │ │ └── README.md │ ├── Check if it is possible to convert one string into another with given constraints │ │ └── 02 December 2022 │ │ │ ├── C++ │ │ │ └── Check if it is possible to convert one string into another with given constraints.cpp │ │ │ ├── Java │ │ │ └── Check if it is possible to convert one string into another with given constraints.java │ │ │ ├── Python │ │ │ └── Check if it is possible to convert one string into another with given constraints.py │ │ │ └── README.md │ ├── Complement │ │ └── 14 December 2022 │ │ │ ├── C++ │ │ │ └── Complement.cpp │ │ │ ├── Java │ │ │ └── Complement.java │ │ │ ├── Python │ │ │ └── Complement.py │ │ │ └── README.md │ ├── Connect Nodes at Same Level │ │ └── 22 February 2023 │ │ │ ├── Connect Nodes at Same Level.cpp │ │ │ └── README.md │ ├── Construct Binary Tree from String with bracket representation │ │ └── 26 November 2022 │ │ │ ├── C++ │ │ │ └── Construct Binary Tree from String with bracket representation.cpp │ │ │ ├── Java │ │ │ └── Construct Binary Tree from String with bracket representation.java │ │ │ ├── Python │ │ │ └── Construct Binary Tree from String with bracket representation.py │ │ │ └── README.md │ ├── Container With Most Water │ │ └── 27 December 2022 │ │ │ ├── C++ │ │ │ └── Container With Most Water.cpp │ │ │ ├── Java │ │ │ └── Container With Most Water.java │ │ │ ├── JavaScript │ │ │ └── Container With Most Water.js │ │ │ ├── Python │ │ │ └── Container With Most Water.py │ │ │ └── README.md │ ├── Convert an array to reduced form │ │ └── 24 January 2023 │ │ │ ├── Convert an array to reduced form.cpp │ │ │ ├── Convert an array to reduced form.java │ │ │ ├── Convert an array to reduced form.py │ │ │ └── README.md │ ├── Count Cyclic Paths │ │ └── 20 February 2023 │ │ │ ├── Count Cyclic Paths.cpp │ │ │ └── README.md │ ├── Count Lucky Permutations │ │ └── 13 January 2023 │ │ │ ├── Count Lucky Permutations.cpp │ │ │ ├── Count Lucky Permutations.java │ │ │ ├── Count Lucky Permutations.py │ │ │ └── README.md │ ├── Count even length │ │ └── 01 January 2023 │ │ │ ├── C++ │ │ │ └── Count even length.cpp │ │ │ ├── Java │ │ │ └── Count even length.java │ │ │ ├── Python │ │ │ └── Count even length.py │ │ │ └── README..md │ ├── Count number of free cell │ │ └── 08 February 2023 │ │ │ ├── Count number of free cell.cpp │ │ │ ├── Count number of free cell.java │ │ │ └── README.md │ ├── Count of Subarrays │ │ └── 17 November 2022 │ │ │ ├── C++ │ │ │ └── Count of Subarrays.cpp │ │ │ ├── Java │ │ │ └── Count of Subarrays.java │ │ │ ├── Python │ │ │ └── Count of Subarrays.py │ │ │ └── README.md │ ├── Count the Number of Full Binary Trees │ │ └── 29 November 2022 │ │ │ ├── C++ │ │ │ └── Count the Number of Full Binary Trees.cpp │ │ │ ├── Java │ │ │ └── Count the Number of Full Binary Trees.java │ │ │ ├── Python │ │ │ └── Count the Number of Full Binary Trees.py │ │ │ └── README.md │ ├── Count the substrings │ │ └── 15 January 2023 │ │ │ ├── Count the Substring.cpp │ │ │ ├── Count the Substring.java │ │ │ ├── Count the Substring.py │ │ │ └── README.md │ └── Cutting Rectangles │ │ └── 03 February 2023 │ │ ├── Cutting Rectangles.cpp │ │ └── README.md ├── D │ ├── Distance of nearest cell having 1 │ │ └── 06 December 2022 │ │ │ ├── C# │ │ │ └── Distance of nearest cell having 1.cs │ │ │ ├── C++ │ │ │ └── Distance of nearest cell having 1.cpp │ │ │ ├── Java │ │ │ └── Distance of nearest cell having 1.java │ │ │ ├── Python │ │ │ └── Distance of nearest cell having 1.py │ │ │ └── README.md │ ├── Distinct Coloring │ │ └── 01 February 2023 │ │ │ ├── Distinct Coloring.cpp │ │ │ ├── Distinct Coloring.java │ │ │ ├── Distinct Coloring.py │ │ │ └── README.md │ └── Distinct Difference │ │ └── 19 March 2023 │ │ ├── Distinct Difference.cpp │ │ └── README.md ├── E │ └── Enemy │ │ └── 02 November 2022 │ │ ├── C++ │ │ └── Enemy.cpp │ │ ├── Java │ │ └── Enemy.java │ │ ├── Python │ │ └── Enemy.py │ │ └── README.md ├── F │ ├── Fill the Matrix │ │ └── 21 February 2023 │ │ │ ├── Fill the Matrix.cpp │ │ │ └── README.md │ ├── Fill up buckets │ │ └── 13 November 2022 │ │ │ ├── C++ │ │ │ └── Fill up buckets.cpp │ │ │ ├── Java │ │ │ └── Fill up buckets.java │ │ │ ├── Python │ │ │ └── Fill up buckets.py │ │ │ └── README.md │ ├── Find anagrams in linked list │ │ └── 09 March 2023 │ │ │ ├── Find anagrams in linked list.cpp │ │ │ └── README.md │ ├── Find minimum number of Laptops required │ │ └── 31 December 2022 │ │ │ ├── C++ │ │ │ └── Find minimum number of Laptops required.cpp │ │ │ ├── Java │ │ │ └── Find minimum number of Laptops required.java │ │ │ ├── Python │ │ │ └── Find minimum number of Laptops required.py │ │ │ └── README.md │ ├── Find patterns │ │ └── 14 November 2022 │ │ │ ├── C++ │ │ │ └── Find patterns.cpp │ │ │ ├── Java │ │ │ └── Find patterns.java │ │ │ ├── Python │ │ │ └── Find patterns.py │ │ │ └── README.md │ ├── Find the first node of loop in linked list │ │ └── 18 January 2023 │ │ │ ├── Find the first node of loop in linked list.cpp │ │ │ ├── Find the first node of loop in linked list.java │ │ │ ├── Find the first node of loop in linked list.py │ │ │ └── README.md │ ├── Find the longest string │ │ └── 05 January 2023 │ │ │ ├── C++ │ │ │ └── Find the longest string.cpp │ │ │ ├── Java │ │ │ └── Find the longest string.java │ │ │ ├── Python │ │ │ └── Find the longest string.py │ │ │ └── README.md │ ├── Find the maximum GCD of the siblings of a Binary Tree │ │ └── 17 January 2023 │ │ │ ├── Find the maximum GCD of the siblings of a Binary Tree.cpp │ │ │ ├── Find the maximum GCD of the siblings of a Binary Tree.java │ │ │ ├── Find the maximum GCD of the siblings of a Binary Tree.py │ │ │ └── README.md │ ├── Fixing Two swapped nodes of a BST │ │ └── 19 February 2023 │ │ │ ├── Fixing Two swapped nodes of a BST.cpp │ │ │ └── README.md │ ├── Flattening a Linked List │ │ └── 07 January 2023 │ │ │ ├── C++ │ │ │ └── Flattening a Linked List.cpp │ │ │ ├── Java │ │ │ └── Flattening a Linked List.java │ │ │ ├── Python │ │ │ └── Flattening a Linked List.py │ │ │ └── README.md │ └── Floyd Warshall │ │ └── 07 November 2022 │ │ ├── C++ │ │ └── Floyd Warshall.cpp │ │ ├── Java │ │ └── Floyd Warshall.java │ │ ├── Python │ │ └── Floyd Warshall.py │ │ └── README.md ├── G │ ├── GCD Array │ │ └── 22 January 2023 │ │ │ ├── GCD Array(2).py │ │ │ ├── GCD Array.cpp │ │ │ ├── GCD Array.java │ │ │ ├── GCD Array.py │ │ │ └── README.md │ ├── Geek hates too many 1s │ │ └── 06 March 2023 │ │ │ ├── Geek hates too many 1s.cpp │ │ │ └── README.md │ ├── Geeks And The String │ │ └── 23 January 2023 │ │ │ ├── Geeks And The String.cpp │ │ │ ├── Geeks And The String.java │ │ │ ├── Geeks And The String.py │ │ │ └── README.md │ ├── Geeks Island │ │ └── 15 February 2023 │ │ │ ├── Geeks Island.cpp │ │ │ └── README.md │ ├── Good Stones │ │ └── 16 February 2023 │ │ │ ├── Good Stones.cpp │ │ │ └── README.md │ └── Grouping Of Numbers │ │ └── 05 November 2022 │ │ ├── C++ │ │ └── Grouping Of Numbers.cpp │ │ ├── Java │ │ └── Grouping Of Numbers.java │ │ ├── Python │ │ └── Grouping Of Numbers.py │ │ └── README.md ├── I │ ├── Intersection Point in Y Shaped Linked Lists │ │ └── 05 February 2023 │ │ │ ├── Intersection Point in Y Shaped Linked Lists.cpp │ │ │ ├── Intersection Point in Y Shaped Linked Lists.java │ │ │ ├── Intersection Point in Y Shaped Linked Lists.py │ │ │ └── README.md │ └── Is it Fibonacci │ │ └── 17 February 2023 │ │ ├── Is it Fibonacci.cpp │ │ └── README.md ├── J │ └── Jumping Numbers │ │ └── 10 November 2022 │ │ ├── C++ │ │ └── Jumping number.cpp │ │ ├── Java │ │ └── Jumping Numbers.java │ │ ├── Python │ │ └── Jumping Numbers.py │ │ └── README.md ├── K │ └── k-th smallest element in BST │ │ └── 07 December 2022 │ │ ├── C++ │ │ └── k-th smallest element in BST.cpp │ │ ├── C │ │ └── k-th smallest element in BST.c │ │ ├── Java │ │ └── k-th smallest element in BST.java │ │ ├── Python │ │ └── k-th smallest element in BST.py │ │ └── README.md ├── L │ ├── LCM Triplet │ │ └── 20 November 2022 │ │ │ ├── C++ │ │ │ └── LCM Triplet.cpp │ │ │ ├── Java │ │ │ └── LCM Triplet.java │ │ │ ├── Python │ │ │ └── LCM Triplet.py │ │ │ └── README.md │ ├── Largest Sum Cycle │ │ └── 02 February 2023 │ │ │ ├── Largest Sum Cycle.cpp │ │ │ ├── Largest Sum Cycle.java │ │ │ ├── Largest Sum Cycle.py │ │ │ └── README.md │ ├── Largest subtree sum in a tree │ │ └── 28 December 2022 │ │ │ ├── C++ │ │ │ └── Largest subtree sum in a tree.cpp │ │ │ ├── Java │ │ │ └── Largest subtree sum in a tree.java │ │ │ ├── Python │ │ │ └── Largest subtree sum in a tree.py │ │ │ └── README.md │ ├── Last cell in a Matrix │ │ └── 03 February 2023 │ │ │ ├── Last cell in a Matrix.cpp │ │ │ ├── Last cell in a Matrix.java │ │ │ ├── Last cell in a Matrix.py │ │ │ └── README.md │ ├── Last modified ball │ │ └── 09 January 2023 │ │ │ ├── C++ │ │ │ └── Last modified ball.cpp │ │ │ ├── Java │ │ │ └── Last modified ball.java │ │ │ ├── Python │ │ │ └── Last modified ball.py │ │ │ └── README.md │ ├── Length of the longest subarray with positive product │ │ └── 07 February 2023 │ │ │ ├── Length of the longest subarray with positive product.cpp │ │ │ ├── Length of the longest subarray with positive product.java │ │ │ └── README.md │ ├── Longest Bitonic subsequence │ │ └── 24 November 2022 │ │ │ ├── C++ │ │ │ └── Longest Bitonic subsequence.cpp │ │ │ ├── Java │ │ │ └── Longest Bitonic subsequence.java │ │ │ ├── Python │ │ │ └── Longest Bitonic subsequence.py │ │ │ └── README.md │ └── Longest Perfect Piece │ │ └── 15 November 2022 │ │ ├── C++ │ │ └── Longest Perfect Piece.cpp │ │ ├── Java │ │ └── Longest Perfect Piece.java │ │ ├── Python │ │ └── Longest Perfect Piece.py │ │ └── README.md ├── M │ ├── Magic Triplets │ │ └── 22 November 2022 │ │ │ ├── C++ │ │ │ └── Magic Triplets.cpp │ │ │ ├── Java │ │ │ └── Magic Triplets.java │ │ │ ├── Python │ │ │ └── Magic Triplets.py │ │ │ └── README.md │ ├── Make array elements unique │ │ └── 11 January 2023 │ │ │ ├── C++ │ │ │ └── Make array elements unique.cpp │ │ │ ├── Java │ │ │ └── Make array elements unique.java │ │ │ ├── Python │ │ │ └── Make array elements unique.py │ │ │ └── README.md │ ├── Max Level Sum in Binary Tree │ │ └── 07 March 2023 │ │ │ ├── Max Level Sum in Binary Tree.cpp │ │ │ └── README.md │ ├── Max Sum without Adjacents │ │ └── 04 February 2023 │ │ │ ├── Max Sum without Adjacents.cpp │ │ │ ├── Max Sum without Adjacents.java │ │ │ ├── Max Sum without Adjacents.py │ │ │ └── README.md │ ├── Max min Height │ │ └── 08 March 2023 │ │ │ ├── Max min Height.cpp │ │ │ └── README.md │ ├── Maximum Bipartite Matching │ │ └── 09 February 2023 │ │ │ ├── Maximum Bipartite Matching.cpp │ │ │ ├── Maximum Bipartite Matching.java Maximum Bipartite Matching.java │ │ │ └── README.md │ ├── Maximum Number of Toys │ │ └── 14 January 2023 │ │ │ ├── Maximum Number of Toys.cpp │ │ │ ├── Maximum Number of Toys.java │ │ │ ├── Maximum Number of Toys.py │ │ │ └── README.md │ ├── Maximum Number of coins │ │ └── 14 March 2023 │ │ │ ├── Maximum Number of coins.cpp │ │ │ └── README.md │ ├── Maximum Possible Value │ │ └── 13 March 2023 │ │ │ ├── Maximum Possible Value.cpp │ │ │ └── README.md │ ├── Maximum Profit By Choosing A Subset Of Intervals │ │ └── 04 January 2023 │ │ │ ├── C++ │ │ │ └── Maximum Profit By Choosing A Subset Of Intervals.cpp │ │ │ ├── Java │ │ │ └── Maximum Profit By Choosing A Subset Of Intervals.java │ │ │ ├── Python │ │ │ └── Maximum Profit By Choosing A Subset Of Intervals.py │ │ │ └── README.md │ ├── Maximum Sub Array │ │ └── 28 November 2022 │ │ │ ├── C++ │ │ │ └── Maximum Sub Array.cpp │ │ │ ├── Java │ │ │ └── Maximum Sub Array.java │ │ │ ├── Python │ │ │ └── Maximum Sub Array.py │ │ │ └── README.md │ ├── Maximum Sub-String after at most K changes │ │ └── 12 November 2022 │ │ │ ├── C++ │ │ │ └── Max sub string after at most k changes.cpp │ │ │ ├── Java │ │ │ └── Maximum Sub-String after at most K changes.java │ │ │ ├── Python │ │ │ └── Maximum Sub-String after at most K changes.py │ │ │ └── README.md │ ├── Maximum Sum LCM │ │ └── 23 November 2022 │ │ │ ├── C++ │ │ │ └── Maximum Sum LCM.cpp │ │ │ ├── Java │ │ │ └── Maximum Sum LCM.java │ │ │ ├── Python │ │ │ └── Maximum Sum LCM.py │ │ │ └── README.md │ ├── Maximum Triplet product │ │ └── 10 March 2023 │ │ │ ├── Maximum Triplet product.cpp │ │ │ └── README.md │ ├── Maximum Value │ │ └── 02 January 2023 │ │ │ ├── C++ │ │ │ └── Maximum Value.cpp │ │ │ ├── Java │ │ │ └── Maximum Value.java │ │ │ ├── Python │ │ │ └── Maximum Value.py │ │ │ └── README.md │ ├── Maximum Weight Node │ │ └── 20 January 2023 │ │ │ ├── Maximum Weight Node.cpp │ │ │ ├── Maximum Weight Node.java │ │ │ ├── Maximum Weight Node.py │ │ │ └── README.md │ ├── Median in a row-wise sorted Matrix │ │ └── 01 November 2022 │ │ │ ├── C++ │ │ │ └── Median in a row-wise sorted Matrix.cpp │ │ │ ├── Java │ │ │ └── Median in a row-wise sorted Matrix.java │ │ │ ├── Python │ │ │ └── Median in a row-wise sorted Matrix.py │ │ │ └── README.md │ ├── Median of the Subarrays │ │ └── 18 March 2023 │ │ │ ├── Median of the Subarrays.cpp │ │ │ └── README.md │ ├── Min operations │ │ └── 29 January 2023 │ │ │ ├── Min operations.cpp │ │ │ ├── Min operations.java │ │ │ ├── Min operations.py │ │ │ └── README.md │ ├── Minimize number of Students to be removed │ │ └── 03 January 2023 │ │ │ ├── C++ │ │ │ └── Minimize number of Students to be removed.cpp │ │ │ ├── Java │ │ │ └── Minimize number of Students to be removed.java │ │ │ ├── Python │ │ │ └── Minimize number of Students to be removed.py │ │ │ └── README.md │ ├── Minimize the sum │ │ └── 12 January 2023 │ │ │ ├── .cpp │ │ │ ├── Minimize the sum.java │ │ │ ├── Minimize the sum.py │ │ │ └── README.md │ ├── Minimum Days │ │ └── 11 February 2023 │ │ │ ├── Minimum Days.cpp │ │ │ └── README.md │ ├── Minimum X (xor) A │ │ └── 21 January 2023 │ │ │ ├── Minimum X (xor) A.cpp │ │ │ ├── Minimum X (xor) A.java │ │ │ ├── Minimum X (xor) A.py │ │ │ └── README.md │ ├── Minimum number of Coins │ │ └── 06 November 2022 │ │ │ ├── C++ │ │ │ └── Minimum number of coins.cpp │ │ │ ├── Java │ │ │ └── Minimum number of coins.java │ │ │ ├── Python │ │ │ └── Minimum number of coins.py │ │ │ └── README.md │ ├── Minimum sum partition │ │ └── 09 November 2022 │ │ │ ├── C++ │ │ │ └── Minimum sum partition.cpp │ │ │ ├── Java │ │ │ └── Minimum sum partition.java │ │ │ ├── Python │ │ │ └── Minimum sum partition.py │ │ │ └── README.md │ ├── Minimum times A has to be repeated such that B is a substring of it │ │ └── 31 January 2023 │ │ │ ├── Minimum times A has to be repeated such that B is a substring of it.cpp │ │ │ ├── Minimum times A has to be repeated such that B is a substring of it.java │ │ │ ├── Minimum times A has to be repeated such that B is a substring of it.py │ │ │ └── README.md │ └── Missing number in matrix │ │ └── 25 December 2022 │ │ ├── C++ │ │ └── Missing number in matrix.cpp │ │ ├── Java │ │ └── Missing number in matrix.java │ │ ├── Python │ │ └── Missing number in matrix.py │ │ └── README.md ├── N │ ├── Next Greater Element │ │ └── 16 January 2023 │ │ │ ├── Next Greater Element.cpp │ │ │ ├── Next Greater Element.java │ │ │ ├── Next Greater Element.js │ │ │ ├── Next Greater Element.py │ │ │ └── README.md │ ├── Number Of Open Doors │ │ └── 18 November 2022 │ │ │ ├── C++ │ │ │ └── Number Of Open Doors.cpp │ │ │ ├── Java │ │ │ └── Number Of Open Doors.java │ │ │ ├── JavaScript │ │ │ └── Number Of Open Doors.js │ │ │ ├── Python │ │ │ └── Number Of Open Doors.py │ │ │ └── README.md │ ├── Number of Good Components │ │ └── 26 February 2023 │ │ │ ├── Number of Good Components.cpp │ │ │ └── README.md │ └── Number of Subarrays of 0's │ │ └── 17 March 2023 │ │ ├── Number of Subarrays of 0's.cpp │ │ └── README.md ├── O │ └── Optimal Array │ │ └── 28 February 2023 │ │ ├── Optimal Array.cpp │ │ └── README.md ├── P │ ├── Prime List │ │ └── 12 February 2023 │ │ │ ├── Prime List.cpp │ │ │ └── README.md │ └── Primes Sum │ │ └── 11 November 2022 │ │ ├── C++ │ │ └── Primes sum.cpp │ │ ├── Java │ │ └── Primes sum.java │ │ ├── Python │ │ └── Primes sum.py │ │ └── README.md ├── R │ ├── Rearrange Array Alternately │ │ └── 01 December 2022 │ │ │ ├── C# │ │ │ └── Rearrange Array Alternately.cs │ │ │ ├── C++ │ │ │ └── Rearrange Array Alternately.cpp │ │ │ ├── Java │ │ │ └── Rearrange Array Alternately.java │ │ │ ├── JavaScript │ │ │ └── Rearrange Array Alternately.js │ │ │ ├── Python │ │ │ └── Rearrange Array Alternately.py │ │ │ └── README.md │ ├── Reorder List │ │ └── 30 November 2022 │ │ │ ├── C++ │ │ │ └── Reorder List.cpp │ │ │ ├── Java │ │ │ └── Reorder List.java │ │ │ ├── Python │ │ │ └── Reorder List.py │ │ │ └── README.md │ └── Reverse both parts │ │ └── 27 February 2023 │ │ ├── README.md │ │ └── Reverse both parts.cpp ├── S │ ├── Satisfy the equation │ │ └── 31st October 2022 │ │ │ ├── C++ │ │ │ └── Satisfy the equation.cpp │ │ │ ├── Java │ │ │ └── Satisfy the equation.java │ │ │ ├── Python │ │ │ └── Satisfy the equation.py │ │ │ └── README.md │ ├── Scrambled String │ │ └── 28 January 2023 │ │ │ ├── README.md │ │ │ ├── Scrambled String.cpp │ │ │ ├── Scrambled String.java │ │ │ └── Scrambled String.py │ ├── Second smallest number │ │ └── 16 March 2023 │ │ │ ├── README.md │ │ │ └── Second smallest number.cpp │ ├── Select Nodes │ │ └── 30 January 2023 │ │ │ ├── README.md │ │ │ ├── Select Nodes.cpp │ │ │ ├── Select Nodes.java │ │ │ └── Select Nodes.py │ ├── Shortest Path Using Atmost One Curved Edge │ │ └── 24 February 2023 │ │ │ ├── README.md │ │ │ └── Shortest Path Using Atmost One Curved Edge.cpp │ ├── Shortest Path by Removing K walls │ │ └── 05 December 2022 │ │ │ ├── C++ │ │ │ └── Shortest Path by Removing K walls.cpp │ │ │ ├── Java │ │ │ └── Shortest Path by Removing K walls.java │ │ │ ├── Python │ │ │ └── Shortest Path by Removing K walls.py │ │ │ └── README.md │ ├── Shortest Prime Path │ │ └── 06 January 2023 │ │ │ ├── C++ │ │ │ └── Shortest Prime Path.cpp │ │ │ ├── Java │ │ │ └── Shortest Prime Path.java │ │ │ ├── Python │ │ │ └── Shortest Prime Path.py │ │ │ └── README.md │ ├── Shortest XY distance in Grid │ │ └── 20 March 2023 │ │ │ ├── README.md │ │ │ └── Shortest XY distance in Grid.cpp │ ├── Shreyansh and his bits │ │ └── 25 November 2022 │ │ │ ├── C++ │ │ │ └── Shreyansh and his bits.cpp │ │ │ ├── Java │ │ │ └── Shreyansh and his bits.java │ │ │ ├── Python │ │ │ └── Shreyansh and his bits.py │ │ │ └── README.md │ ├── Single valued subtree │ │ └── 30 December 2022 │ │ │ ├── C++ │ │ │ └── Single valued subtree.cpp │ │ │ ├── Java │ │ │ └── Single valued subtree.java │ │ │ ├── Python │ │ │ └── Single valued subtree.py │ │ │ └── README.md │ ├── Special Palindrome Substrings │ │ └── 15 March 2023 │ │ │ ├── README.md │ │ │ └── Special Palindrome Substrings.cpp │ ├── Split Array Largest Sum │ │ └── 13 December 2022 │ │ │ ├── C++ │ │ │ └── Split Array Largest Sum.cpp │ │ │ ├── Java │ │ │ └── Split Array Largest Sum.java │ │ │ ├── Python │ │ │ └── Split Array Largest Sum.py │ │ │ └── README.md │ └── Sum of Beauty of All Substrings │ │ └── 16 November 2022 │ │ ├── C++ │ │ └── Sum of Beauty of All Substrings.cpp │ │ ├── Java │ │ └── Sum of Beauty of All Substrings.java │ │ ├── Python │ │ └── Sum of Beauty of All Substrings.py │ │ └── README.md ├── T │ ├── Total Decoding Messages │ │ └── 27 January 2023 │ │ │ ├── README.md │ │ │ ├── Total Decoding Messages.cpp │ │ │ ├── Total Decoding Messages.java │ │ │ └── Total Decoding Messages.py │ ├── Transform to Sum Tree │ │ └── 10 January 2023 │ │ │ ├── C++ │ │ │ └── Transform to Sum Tree.cpp │ │ │ ├── Java │ │ │ └── Transform to Sum Tree.java │ │ │ ├── Python │ │ │ └── Transform to Sum Tree.py │ │ │ └── README.md │ ├── Two numbers with odd occurrences │ │ └── 08 November 2022 │ │ │ ├── C++ │ │ │ └── Two numbers with odd occurrences.cpp │ │ │ ├── Java │ │ │ └── Two numbers with odd occurrences.java │ │ │ ├── Python │ │ │ └── Two numbers with odd occurrences.py │ │ │ └── README.md │ └── Type it! │ │ └── 25 January 2023 │ │ ├── README.md │ │ ├── Type it!.cpp │ │ ├── Type it!.java │ │ └── Type it!.py ├── U │ ├── Unique Paths in a Grid │ │ └── 23 February 2023 │ │ │ ├── README.md │ │ │ └── Unique Paths in a Grid.cpp │ ├── Unique partitions │ │ └── 21 November 2022 │ │ │ ├── C++ │ │ │ └── Unique partitions.cpp │ │ │ ├── Java │ │ │ └── Unique partitions.java │ │ │ ├── Python │ │ │ └── Unique partitions.py │ │ │ └── README.md │ └── Update Queries │ │ └── 01 March 2023 │ │ ├── README.md │ │ └── Update Queries.cpp ├── V │ └── Valid Compressed String │ │ └── 25 February 2023 │ │ ├── README.md │ │ └── Valid Compressed String.cpp ├── W │ ├── Walls Coloring II │ │ └── 02 February 2023 │ │ │ ├── README.md │ │ │ └── Walls Coloring II.cpp │ ├── Walls Coloring │ │ └── 14 February 2023 │ │ │ ├── README.md │ │ │ └── Walls Coloring.cpp │ └── Wine Buying and Selling │ │ └── 24 December 2022 │ │ ├── C++ │ │ └── Wine Buying and Selling.cpp │ │ ├── Java │ │ └── Wine Buying and Selling.java │ │ ├── Python │ │ └── Wine Buying and Selling.py │ │ └── README.md ├── Y │ └── Yet another query problem │ │ └── 11 March 2023 │ │ └── Yet another query problem.cpp └── Z │ └── Zero Sum Subarrays │ └── 22 December 2022 │ ├── C# │ └── Zero Sum Subarrays.cs │ ├── C++ │ └── Zero Sum Subarrays.cpp │ ├── Java │ └── Zero Sum Subarrays.java │ ├── JavaScript │ └── Zero Sum Subarrays.js │ ├── Python │ └── Zero Sum Subarrays.py │ └── README.md └── README.md /.github/CODEOWNERS: -------------------------------------------------------------------------------- 1 | # More details are here: https://help.github.com/articles/about-codeowners/ 2 | 3 | # START 4 | 5 | # Global rule: 6 | 7 | # Assigned as a default PR reviewer (review when someone opens a pull request.) 8 | * @imsampro 9 | 10 | # END -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Bug Report - Java.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report - Java 2 | description: If you find a bug, error, or mistake in the Java file, please report it so that the project can be improved. 3 | title: "🪲[Bug]: " 4 | labels: ["bug", "java", "enhancement", "good first issue", "help wanted"] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | Thanks for taking the time to fill out this bug report! 10 | - type: textarea 11 | id: what-happened 12 | attributes: 13 | label: What happened? 14 | description: Also tell us, what did you expect to happen? 15 | placeholder: Tell us what you see! 16 | value: "A bug happened!" 17 | validations: 18 | required: true 19 | - type: dropdown 20 | id: browsers 21 | attributes: 22 | label: What browsers are you seeing the problem on? 23 | multiple: true 24 | options: 25 | - Firefox 26 | - Chrome 27 | - Brave 28 | - Safari 29 | - Microsoft Edge 30 | - type: checkboxes 31 | id: terms 32 | attributes: 33 | label: Pull Request Confirmation 34 | description: By submitting this issue, you agree to fulfil all the checklist requirements above. 35 | options: 36 | - label: I agree to checked all the checklist requirements. 37 | required: true 38 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Bug Report - Python3.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report - Python3 2 | description: If you find a bug, error, or mistake in the python3 file, please report it so that the project can be improved. 3 | title: "🪲[Bug]: " 4 | labels: ["bug", "python3", "enhancement", "good first issue", "help wanted"] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | Thanks for taking the time to fill out this bug report! 10 | - type: textarea 11 | id: what-happened 12 | attributes: 13 | label: What happened? 14 | description: Also tell us, what did you expect to happen? 15 | placeholder: Tell us what you see! 16 | value: "A bug happened!" 17 | validations: 18 | required: true 19 | - type: dropdown 20 | id: browsers 21 | attributes: 22 | label: What browsers are you seeing the problem on? 23 | multiple: true 24 | options: 25 | - Firefox 26 | - Chrome 27 | - Brave 28 | - Safari 29 | - Microsoft Edge 30 | - type: checkboxes 31 | id: terms 32 | attributes: 33 | label: Pull Request Confirmation 34 | description: By submitting this issue, you agree to fulfil all the checklist requirements above. 35 | options: 36 | - label: I agree to checked all the checklist requirements. 37 | required: true 38 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/Bug report - C++.yml: -------------------------------------------------------------------------------- 1 | name: Bug Report - C++ 2 | description: If you find a bug, error, or mistake in the C++ file, please report it so that the project can be improved. 3 | title: "🪲[Bug]: " 4 | labels: ["bug", "c++", "enhancement", "good first issue", "help wanted"] 5 | body: 6 | - type: markdown 7 | attributes: 8 | value: | 9 | Thanks for taking the time to fill out this bug report! 10 | - type: textarea 11 | id: what-happened 12 | attributes: 13 | label: What happened? 14 | description: Also tell us, what did you expect to happen? 15 | placeholder: Tell us what you see! 16 | value: "A bug happened!" 17 | validations: 18 | required: true 19 | - type: dropdown 20 | id: browsers 21 | attributes: 22 | label: What browsers are you seeing the problem on? 23 | multiple: true 24 | options: 25 | - Firefox 26 | - Chrome 27 | - Brave 28 | - Safari 29 | - Microsoft Edge 30 | - type: checkboxes 31 | id: terms 32 | attributes: 33 | label: Pull Request Confirmation 34 | description: By submitting this issue, you agree to fulfil all the checklist requirements above. 35 | options: 36 | - label: I agree to checked all the checklist requirements. 37 | required: true 38 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/config.yml: -------------------------------------------------------------------------------- 1 | blank_issues_enabled: false 2 | links: 3 | - name: Follow AIOCodBase 4 | url: https://github.com/AIOCodeBase 5 | - name: Star this repository 6 | url: https://github.com/AIOCodeBase/GFGPOTD 7 | -------------------------------------------------------------------------------- /.github/pull_request_template.md: -------------------------------------------------------------------------------- 1 | 2 | 3 | ## Fixes Issue 4 | 5 | 6 | Closes # 7 | 8 | 9 | ## CheckList (*Check all the boxes*) 10 | 11 | 12 | 13 | 14 | - [ ] My code follows the code style of this project. 15 | - [ ] My code will not cause a merge conflict. 16 | - [ ] The title of my pull request is a short description of the requested changes. 17 | - [ ] I have named & renamed the file & folders correctly before making this pull request. 18 | - [ ] I have formatted the code accordingly. 19 | - [ ] My code is different than the existing code in this repository (if any). 20 | - [ ] I removed all comments and commented lines from the main code. (class Sotulion{...}) 21 | 22 | ## Screenshots 23 | 24 | 25 | 26 | ## Note to reviewers 27 | 28 | 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AIOCodeBase/GFGPOTD/02d9886c9c7e5381814b0d68f31bf4f67a08b9a6/.gitignore -------------------------------------------------------------------------------- /CODE_OF_CONDUCT.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AIOCodeBase/GFGPOTD/02d9886c9c7e5381814b0d68f31bf4f67a08b9a6/CODE_OF_CONDUCT.md -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 AIOCodeBase Team 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Problems & Solutions/A/A Game of LCM/18 December 2022/C++/A Game of LCM.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 | long long gcd(long long A, long long B) { 12 | if (B == 0) return A; 13 | return gcd(B, A % B); 14 | } 15 | long long lcm(int N){ 16 | if (N == 4) return 12; 17 | long long a = N * (N - 1); 18 | long long b = a; 19 | b *= (N - 2) / gcd(b, N - 2); 20 | b *= (N - 3) / gcd(b, N - 3); 21 | long long c = a, f = 0; 22 | for (int i = N - 2; i >= 1; i--) { 23 | if (gcd(c, i) == 1) { 24 | a *= i; 25 | c *= i; 26 | f++; 27 | } 28 | if (f == 2) break; 29 | } 30 | return max(a, b); 31 | } 32 | long long maxGcd(int N) { 33 | if(N==4)return 12; 34 | return max(lcm(N),lcm(N-1)); 35 | 36 | } 37 | }; 38 | 39 | //{ Driver Code Starts. 40 | 41 | int main() { 42 | int t; 43 | cin >> t; 44 | while (t--) { 45 | int N; 46 | cin >> N; 47 | Solution ob; 48 | cout << ob.maxGcd(N) << "\n"; 49 | } 50 | } 51 | // } Driver Code Ends 52 | -------------------------------------------------------------------------------- /Problems & Solutions/A/A Game of LCM/18 December 2022/Python/A Game of LCM.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def gcd(self,A,B): 3 | if(B==0): 4 | return A 5 | return self.gcd(B,A%B) 6 | def lcm(self,N): 7 | if N==4: 8 | return 12 9 | a=N*(N-1) 10 | b=a 11 | b*=(N-2)//self.gcd(b,N-2) 12 | b*=(N-3)//self.gcd(b,N-3) 13 | c=a 14 | f=0 15 | for i in range(N-2,0,-1): 16 | if self.gcd(c,i)==1: 17 | a*=i 18 | c*=i 19 | f+=1 20 | if f==2: 21 | break 22 | return max(a,b) 23 | def maxGcd(self,N): 24 | if N==4: 25 | return 12 26 | return max(self.lcm(N),self.lcm(N-1)) 27 | 28 | #{ 29 | # Driver Code Starts 30 | #Initial Template for Python 3 31 | 32 | import math 33 | 34 | if __name__=='__main__': 35 | t=int(input()) 36 | for _ in range(t): 37 | N=(int)(input()) 38 | ob=Solution() 39 | print(ob.maxGcd(N)) 40 | # } Driver Code Ends 41 | -------------------------------------------------------------------------------- /Problems & Solutions/A/A Game of LCM/18 December 2022/README.md: -------------------------------------------------------------------------------- 1 | # **A Game of LCM** 2 | 3 | ## **Medium** 4 | 5 | Given an integer N. Find maximum LCM (Least Common Multiple) that can be obtained from four numbers less than or equal to N. 6 | Note: Duplicate numbers can be used. 7 | 8 | **Example 1:** 9 | 10 | ``` 11 | Input: 12 | N = 4 13 | Output: 12 14 | Explanation: 15 | The four numbers can be [4,4,3,2] or 16 | [4,4,4,3], etc. It can be shown that 12 is 17 | the maximum LCM of four numbers that can 18 | be obtained from numbers less than or equal 19 | to 4. 20 | ``` 21 | 22 | **Example 2:** 23 | 24 | ``` 25 | Input: 26 | N = 5 27 | Output: 60 28 | Explanation: 29 | The four numbers can be [5,5,4,3] or 30 | [5,4,3,2], etc. 60 is the maximum that can 31 | be obtained. 32 | ``` 33 | 34 | **Your Task:** 35 | You don't need to read input or print anything. Your task is to complete the function **maxGcd()** which takes N as the input parameter and returns the maximum LCM that can be obtained from four numbers less than or equal to N. 36 | 37 | **Expected Time Complexity:** O( Log2(max(N)) ) 38 | **Expected Auxillary Space:** O(1) 39 | 40 | **Constraints:** 41 | 2 ≤ N ≤ 10^4 42 | 43 | > ### **Problem URL: [A Game of LCM](https://practice.geeksforgeeks.org/problems/a-game-of-lcm2531/1)** 44 | -------------------------------------------------------------------------------- /Problems & Solutions/A/A difference of values and indexes/20 December 2022/C++/A difference of values and indexes.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | 7 | // } Driver Code Ends 8 | class Solution{ 9 | public: 10 | int maxDistance(int arr[], int n) 11 | { 12 | int max1 = INT_MIN, min1 = INT_MAX; 13 | int max2 = INT_MIN, min2 = INT_MAX; 14 | for (int i = 0; i < n; i++) { 15 | max1 = max(max1, arr[i] + i); 16 | min1 = min(min1, arr[i] + i); 17 | max2 = max(max2, arr[i] - i); 18 | min2 = min(min2, arr[i] - i); 19 | } 20 | return max(max1 - min1, max2 - min2); 21 | } 22 | }; 23 | 24 | //{ Driver Code Starts. 25 | int main() 26 | { 27 | int T; 28 | cin>>T; 29 | while(T--){ 30 | int n; 31 | cin>>n; 32 | int arr[n]; 33 | for(int i = 0; i>arr[i]; 35 | Solution obj; 36 | cout< ### **Problem URL: [A difference of values and indexes](https://practice.geeksforgeeks.org/problems/a-difference-of-values-and-indexes0302/1)** 42 | -------------------------------------------------------------------------------- /Problems & Solutions/A/Absolute difference divisible by K/08 January 2023/C++/Absolute difference divisible by K.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | #define ll long long 10 | public: 11 | ll countPairs(int n, int arr[], int k) { 12 | 13 | ll cnt = 0; 14 | 15 | for (ll i = 0; i < n; i++) { 16 | arr[i] = (arr[i] + k) % k; 17 | } 18 | 19 | ll hash[k] = { 0 }; 20 | 21 | for (ll i = 0; i < n; i++) { 22 | hash[arr[i]]++; 23 | } 24 | 25 | for (ll i = 0; i < k; i++) { 26 | cnt += (hash[i] * (hash[i] - 1)) / 2; 27 | } 28 | 29 | return cnt; 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; 41 | 42 | int arr[n]; 43 | for(int i=0; i>arr[i]; 45 | 46 | cin>>k; 47 | 48 | Solution ob; 49 | cout << ob.countPairs(n,arr,k) << endl; 50 | } 51 | return 0; 52 | } 53 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/A/Absolute difference divisible by K/08 January 2023/Java/Absolute difference divisible by K.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for Java 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | class GFG { 8 | public static void main(String args[]) throws IOException { 9 | BufferedReader read = 10 | new BufferedReader(new InputStreamReader(System.in)); 11 | int t = Integer.parseInt(read.readLine()); 12 | while (t-- > 0) { 13 | int n = Integer.parseInt(read.readLine()); 14 | String S[] = read.readLine().split(" "); 15 | int[] arr = new int[n]; 16 | for (int i = 0; i < n; i++) 17 | arr[i] = Integer.parseInt(S[i]); 18 | int k = Integer.parseInt(read.readLine()); 19 | 20 | Solution ob = new Solution(); 21 | System.out.println(ob.countPairs(n, arr, k)); 22 | } 23 | } 24 | } 25 | // } Driver Code Ends 26 | 27 | class Solution { 28 | static long countPairs(int n, int[] arr, int k) { 29 | 30 | long cnt = 0; 31 | 32 | for (int i = 0; i < n; i++) 33 | { 34 | arr[i] = (arr[i] + k) % k; 35 | } 36 | 37 | long hash[] = new long[k]; 38 | Arrays.fill(hash, 0); 39 | 40 | for (int i = 0; i < n; i++) 41 | { 42 | hash[arr[i]]++; 43 | } 44 | 45 | for (int i = 0; i < k; i++) 46 | { 47 | cnt += (hash[i] * (hash[i] - 1)) / 2; 48 | } 49 | 50 | return cnt; 51 | } 52 | } -------------------------------------------------------------------------------- /Problems & Solutions/A/Absolute difference divisible by K/08 January 2023/Python/Absolute difference divisible by K.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countPairs(self, n, arr, k): 3 | 4 | cnt = 0 5 | 6 | for i in range(n): 7 | arr[i] = (arr[i] + k) % k 8 | 9 | hash = [0]*k 10 | 11 | for i in range(n): 12 | hash[arr[i]] += 1 13 | 14 | for i in range(k): 15 | cnt += (hash[i] * (hash[i] - 1)) // 2 16 | 17 | return cnt 18 | 19 | #{ 20 | # Driver Code Starts 21 | #Initial Template for Python 3 22 | 23 | if __name__ == '__main__': 24 | t = int (input ()) 25 | for _ in range (t): 26 | n=int(input()) 27 | arr=list(map(int,input().split())) 28 | k=int(input()) 29 | 30 | ob = Solution() 31 | print(ob.countPairs(n,arr,k)) 32 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/A/Absolute difference divisible by K/08 January 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Absolute difference divisible by K** 2 | 3 | ## **Easy** 4 | 5 | Given an array of integers of size **n** and an integer **k**, find all the pairs in the array whose absolute difference is divisible by k. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | n = 3 12 | arr[] = {3, 7, 11} 13 | k = 4 14 | Output: 15 | 3 16 | Explanation: 17 | (11-3) = 8 is divisible by 4 18 | (11-7) = 4 is divisible by 4 19 | (7-3) = 4 is divisible by 4 20 | ``` 21 | 22 | 23 | **Example 2:** 24 | 25 | ``` 26 | Input: 27 | n = 4 28 | arr[] = {1, 2, 3, 4} 29 | k = 2 30 | Output : 31 | 2 32 | Explanation: 33 | Valid pairs are (1,3), and (2,4). 34 | 35 | ``` 36 | 37 | 38 | **Your Task:** 39 | You don't need to read input or print anything. Your task is to complete the function **countPairs()** which takes integers n, array arr\[ \], integer k as input parameters and returns the number of pairs whose absolute difference is divisible by k. 40 | **Note:** The answer may be large so use 64-bit integer. 41 | 42 | 43 | **Expected Time Complexity:** O(n + k) 44 | **Expected Auxiliary Space:** O(k) 45 | 46 | 47 | **Constraints:** 48 | 2 ≤ n ≤ 10^5 49 | 1 ≤ k,arr\[i\] ≤ 10^5 50 | 51 | > ### **Problem URL: [Absolute difference divisible by K](https://practice.geeksforgeeks.org/problems/e0059183c88ab680b2f73f7d809fb8056fe9dc43/1)** -------------------------------------------------------------------------------- /Problems & Solutions/A/Add Binary Strings/27 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Add Binary Strings** 2 | 3 | ## **Medium** 4 | 5 | Given two binary strings **A** and **B** consisting of only 0s and 1s. Find the resultant string after adding the two Binary Strings. 6 | **Note:** The input strings may contain leading zeros but the output string should not have any leading zeros. 7 | 8 | **Example 1:** 9 | 10 | ``` 11 | Input: 12 | A = "1101", B = "111" 13 | Output: 10100 14 | Explanation: 15 |  1101 16 | + 111 17 | 10100 18 | ``` 19 | 20 | **Example 2:** 21 | 22 | ``` 23 | Input: 24 | A = "10", B = "01" 25 | Output: 11 26 | Explanation: 27 | 10 28 | + 01 29 |   11 30 | ``` 31 | 32 | **Your Task:** 33 | You don't need to read input or print anything. Your task is to complete the function **addBinary()** which takes 2 binary string A and B and **returns** a binary string denoting the addition of both the strings. 34 | 35 | **Expected Time Complexity:** O(max(|A|, |B|)). 36 | **Expected Auxiliary Space:** O(max(|A|, |B|)) (for output string). 37 | 38 | **Constraints:** 39 | 1 ≤ |A|, |B| ≤ 10^6 40 | 41 | > ### **Problem URL: [Add Binary Strings](https://practice.geeksforgeeks.org/problems/add-binary-strings3805/1)** 42 | -------------------------------------------------------------------------------- /Problems & Solutions/A/Aggressive Cows/03 December 2022/Python/Aggressive Cows.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def solve(self, n, k, stalls): 5 | stalls.sort() 6 | 7 | def count(d): 8 | ans, curr = 1, stalls[0] 9 | for i in range(1, n): 10 | if stalls[i] - curr >= d: 11 | ans += 1 12 | curr = stalls[i] 13 | return ans 14 | 15 | l, r = 0, stalls[-1] - stalls[0] 16 | while l < r: 17 | mid = r - (r - l) // 2 18 | if count(mid) >= k: 19 | l = mid 20 | else: 21 | r = mid - 1 22 | return l 23 | 24 | #{ 25 | # Driver Code Starts 26 | #Initial Template for Python 3 27 | 28 | def main(): 29 | 30 | T = int(input()) 31 | 32 | while(T > 0): 33 | n, k = list(map(int, input().split())) 34 | stalls = list(map(int, input().split())) 35 | ob = Solution() 36 | res = ob.solve(n, k, stalls) 37 | print(res) 38 | 39 | T -= 1 40 | 41 | 42 | if __name__ == "__main__": 43 | main() 44 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/A/Akku and Binary Numbers/26 December 2022/C++/Akku and Binary Numbers.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | std::vector v; 9 | 10 | class Solution{ 11 | public: 12 | void precompute() 13 | { 14 | for (int i = 0; i < 63; ++i) 15 | for (int j = i+1; j < 63; ++j) 16 | for (int k = j+1; k < 63; ++k) 17 | { 18 | v.push_back((1LL<>t; 41 | while(t--) 42 | { 43 | long long l,r; 44 | cin>>l>>r; 45 | cout << ob.solve(l, r) << endl; 46 | } 47 | return 0; 48 | } 49 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/A/Akku and Binary Numbers/26 December 2022/Python/Akku and Binary Numbers.py: -------------------------------------------------------------------------------- 1 | from bisect import bisect_left,bisect_right 2 | class Solution: 3 | def __init__(self): 4 | self.p=self.precompute() 5 | def solve (self, L, R): 6 | p=self.p 7 | low=bisect_left(p,L) 8 | high=bisect_right(p,R) 9 | return high-low 10 | def precompute (self): 11 | v=[] 12 | for i in range(63): 13 | for j in range(i+1,63): 14 | for k in range(j+1,63): 15 | v.append((1< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | //User function Template for C++ 9 | 10 | class Solution{ 11 | public: 12 | int appleSequences(int n, int m, string arr){ 13 | // code here 14 | int maxlen = 0; 15 | int end = 0, start = 0; 16 | int count = 0; 17 | 18 | while(end < n) 19 | { 20 | if(arr[end] == 'O') count++; 21 | 22 | while(start < n && count > m) 23 | { 24 | if(arr[start] == 'O') count--; 25 | start++; 26 | } 27 | 28 | maxlen = max(maxlen, end-start+1); 29 | end++; 30 | } 31 | 32 | return maxlen; 33 | } 34 | }; 35 | 36 | //{ Driver Code Starts. 37 | int main() 38 | { 39 | int t; 40 | cin>>t; 41 | while(t--) 42 | { 43 | int N, M; 44 | cin >> N >> M; 45 | string arr; 46 | cin >> arr; 47 | 48 | Solution ob; 49 | cout << ob.appleSequences(N, M, arr) << endl; 50 | } 51 | return 0; 52 | } 53 | // } Driver Code Ends 54 | -------------------------------------------------------------------------------- /Problems & Solutions/A/Apple Sequences/18 February 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Apple Sequences** 2 | 3 | ## **Easy** 4 | 5 | There is a string of size n containing only 'A' and 'O'. 'A' stands for Apple, and 'O' stands for Orange. We have m number of spells, each spell allows us to convert an orange into an apple. 6 | 7 | Find the longest sequence of apples you can make, given a string and the value of m. 8 | 9 | **Example 1:** 10 | 11 | ``` 12 | Input: 13 | N = 5 14 | M = 1 15 | arr[] = 'AAOAO' 16 | Output: 4 17 | Explanation: Changing the orange at 18 | 3rd position into an apple gives 19 | us the maximum possible answer. 20 | ``` 21 | 22 | **Example 2:** 23 | 24 | ``` 25 | Input: 26 | N = 5 27 | M = 1 28 | arr = 'AOOAO' 29 | Output: 2 30 | Explanation: Changing any orange into 31 | an apple will give us a sequence 32 | of length 2. 33 | ``` 34 | 35 | **Your Task:** 36 | You don't need to read input or print anything. Your task is to complete the function appleSequence() which takes the array in the form of a string, its size n, and an integer m as input parameters and returns the largest apple sequences after converting m oranges. 37 | 38 | **Expected Time Complexity:** O(n) 39 | **Expected Space Complexity:** O(1) 40 | 41 | **Constraints:** 42 | 43 | 1 <= m <= n <= $10^6$ 44 | 45 | > ### **Problem URL: [Apple Sequences](https://practice.geeksforgeeks.org/problems/38f100615d0b2efa755e7b07f905e0f8cd2fe5df/1)** 46 | -------------------------------------------------------------------------------- /Problems & Solutions/A/Arithmetic Number/13 February 2023/Arithmetic Number.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 inSequence(int A, int B, int C){ 13 | 14 | if(C==0) return A==B; 15 | 16 | int comm_diff = B-A; 17 | 18 | if(comm_diff%C==0 && comm_diff/C>=0)return 1; 19 | 20 | return 0; 21 | 22 | } 23 | }; 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main(){ 28 | int t; 29 | cin>>t; 30 | while(t--){ 31 | int A, B, C; 32 | cin>>A>>B>>C; 33 | 34 | Solution ob; 35 | cout<= 0: 8 | return 0 9 | if d%C == 0: 10 | return 1 11 | return 0 12 | else: 13 | if C <= 0: 14 | return 0 15 | if d%C == 0: 16 | return 1 17 | return 0 18 | return 0 19 | 20 | #{ 21 | # Driver Code Starts 22 | #Initial Template for Python 3 23 | 24 | if __name__ == '__main__': 25 | t = int(input()) 26 | for _ in range(t): 27 | A, B, C = [int(x) for x in input().split()] 28 | 29 | ob = Solution(); 30 | print(ob.inSequence(A, B, C)) 31 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/A/Arithmetic Number/13 February 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Arithmetic Number** 2 | 3 | ## **Easy** 4 | 5 | Given three integers **'A'** denoting the first term of an arithmetic sequence , **'C'** denoting the common difference of an arithmetic sequence and an integer **'B'**. you need to tell whether 'B' exists in the arithmetic sequence or not. Return 1 if B is present in the sequence. Otherwise, returns 0. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: A = 1, B = 3, C = 2 11 | Output: 1 12 | Explaination: 3 is the second term of the 13 | sequence starting with 1 and having a common 14 | difference 2. 15 | ``` 16 | 17 | **Example 2:** 18 | 19 | ``` 20 | Input: A = 1, B = 2, C = 3 21 | Output: 0 22 | Explaination: 2 is not present in the sequence. 23 | ``` 24 | 25 | **Your Task:** 26 | You do not need to read input or print anything. Your task is to complete the function **inSequence()** which takes A, B and C and returns 1 if B is present in the sequence. Otherwise, returns 0. 27 | 28 | **Expected Time Complexity:** O(1) 29 | **Expected Space Complexity:** O(1) 30 | 31 | **Constraints:** 32 | 33 | $-10^9$ ≤ A, B, C ≤ $10^9$ 34 | 35 | > ### **Problem URL: [Arithmetic Number](https://practice.geeksforgeeks.org/problems/arithmetic-number2815/1)** 36 | -------------------------------------------------------------------------------- /Problems & Solutions/A/Array Pair Sum Divisibility Problem/11 December 2022/Python/Array Pair Sum Divisibility Problem.py: -------------------------------------------------------------------------------- 1 | from collections import defaultdict 2 | 3 | class Solution: 4 | def canPair(self, arr, k): 5 | if (n & 1): 6 | return 0 7 | 8 | freq = defaultdict(lambda: 0) 9 | 10 | for i in range(0, n): 11 | freq[((arr[i] % k) + k) % k] += 1 12 | 13 | for i in range(0, n): 14 | 15 | rem = ((arr[i] % k) + k) % k 16 | 17 | if (2 * rem == k): 18 | 19 | if (freq[rem] % 2 != 0): 20 | return 0 21 | 22 | elif (rem == 0): 23 | if (freq[rem] & 1): 24 | return 0 25 | 26 | elif (freq[rem] != freq[k - rem]): 27 | return 0 28 | return 1 29 | 30 | #{ 31 | # Driver Code Starts 32 | #Initial Template for Python 3 33 | 34 | if __name__ == '__main__': 35 | T=int(input()) 36 | for i in range(T): 37 | n, k = input().split() 38 | n = int(n) 39 | k = int(k) 40 | nums = list(map(int, input().split())) 41 | ob = Solution() 42 | ans = ob.canPair(nums, k) 43 | if(ans): 44 | print("True") 45 | else: 46 | print("False") 47 | # } Driver Code Ends 48 | -------------------------------------------------------------------------------- /Problems & Solutions/A/Array Pair Sum Divisibility Problem/11 December 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Array Pair Sum Divisibility Problem** 2 | 3 | ## **Easy** 4 | 5 | Given an array of integers and a number k, write a function that returns true if given array can be divided into pairs such that sum of every pair is divisible by k. 6 | 7 | **Example 1 :** 8 | 9 | ``` 10 | Input : arr = [9, 5, 7, 3], k = 6 11 | Output: True 12 | Explanation: {(9, 3), (5, 7)} is a 13 | possible solution. 9 + 3 = 12 is divisible 14 | by 6 and 7 + 5 = 12 is also divisible by 6. 15 | 16 | 17 | ``` 18 | 19 | **Example 2:** 20 | 21 | ``` 22 | Input : arr = [2, 4, 1, 3], k = 4 23 | Output: False 24 | Explanation: There is no possible solution. 25 | ``` 26 | 27 | **Your Task:** 28 | You don't need to read or print anything. Your task is to complete the function **canPair()** which takes array and k as input parameter and returns true if array can be divided into pairs such that sum of every pair is divisible by k otherwise returns false. 29 | 30 | **Expected Time Complexity:** O(n) 31 | **Expected Space Complexity :** O(n) 32 | 33 | **Constraints:** 34 | 1 <= length of array <= 10^5 35 | 1 <= elements of array <= 10^5 36 | 1 <= k <= 10^5 37 | 38 | > ### **Problem URL: [Array Pair Sum Divisibility Problem](https://practice.geeksforgeeks.org/problems/array-pair-sum-divisibility-problem3257/1)** 39 | -------------------------------------------------------------------------------- /Problems & Solutions/A/Array Removals/03 November 2022/C++/Array Removals.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 removals(vector& arr, int k){ 14 | //Code here 15 | int n = arr.size(); 16 | sort(arr.begin(),arr.end()); //O(NLOGN) 17 | int ans = n-1; //at max there can be n-1 removals 18 | for(int i=0; i>t; 33 | 34 | while(t--){ 35 | int n,k; 36 | cin>>n>>k; 37 | vector arr(n); 38 | for(int i=0;i>arr[i]; 40 | } 41 | 42 | Solution ob; 43 | int ans = ob.removals(arr,k); 44 | 45 | cout< i: 20 | continue 21 | ans = min(ans, n-(i-lo+1)) 22 | # print("i = ", i, "lo = ", lo, " ans = ", ans) 23 | return ans 24 | 25 | 26 | #{ 27 | # Driver Code Starts 28 | #Initial Template for Python 3 29 | 30 | 31 | if __name__ == '__main__': 32 | tc = int(input()) 33 | while tc > 0: 34 | n, k = list(map(int, input().strip().split())) 35 | arr = list(map(int, input().strip().split())) 36 | ob = Solution() 37 | ans = ob.removals(arr, n, k) 38 | print(ans) 39 | tc -= 1 40 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/A/Articulation Point - I/12 December 2022/assets/index.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AIOCodeBase/GFGPOTD/02d9886c9c7e5381814b0d68f31bf4f67a08b9a6/Problems & Solutions/A/Articulation Point - I/12 December 2022/assets/index.png -------------------------------------------------------------------------------- /Problems & Solutions/A/Articulation Point - I/12 December 2022/assets/index1.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AIOCodeBase/GFGPOTD/02d9886c9c7e5381814b0d68f31bf4f67a08b9a6/Problems & Solutions/A/Articulation Point - I/12 December 2022/assets/index1.png -------------------------------------------------------------------------------- /Problems & Solutions/A/Articulation Point - I/12 December 2022/assets/index2.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AIOCodeBase/GFGPOTD/02d9886c9c7e5381814b0d68f31bf4f67a08b9a6/Problems & Solutions/A/Articulation Point - I/12 December 2022/assets/index2.png -------------------------------------------------------------------------------- /Problems & Solutions/A/Asteroid Collision/29 December 2022/Python/Asteroid Collision.py: -------------------------------------------------------------------------------- 1 | from collections import deque 2 | class Solution: 3 | def asteroidCollision(self, n, a): 4 | st = deque() 5 | 6 | for val in a: 7 | if val > 0: 8 | st.append(val) 9 | else: 10 | while st and st[-1] > 0 and st[-1] < -val: 11 | st.pop() 12 | if st and st[-1] == -val: 13 | st.pop() 14 | elif not st or st[-1] < -val: 15 | st.append(val) 16 | 17 | return st 18 | 19 | #{ 20 | # Driver Code Starts 21 | #Initial Template for Python 3 22 | 23 | if __name__ == '__main__': 24 | t = int(input()) 25 | for _ in range(t): 26 | N = int(input()) 27 | asteroids = list(map(int, input().split())) 28 | ob = Solution() 29 | res = ob.asteroidCollision(N, asteroids) 30 | for val in res: 31 | print(val, end = ' ') 32 | print() 33 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/B/Balanced string/15 December 2022/Python/Balanced string.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def BalancedString(self,N): 3 | ans="" 4 | full="abcdefghijklmnopqrstuvwxyz" 5 | a=N//26 6 | b=N%26 7 | for _ in range(a): 8 | ans=ans+full 9 | if(N%2==0): 10 | for i in range(b//2): 11 | ans=ans+full[i] 12 | for i in range(26-b//2,26): 13 | ans=ans+full[i] 14 | else: 15 | SumOfDigits=0 16 | temp=N 17 | while(temp!=0): 18 | SumOfDigits=SumOfDigits+(temp)%10 19 | temp=temp//10 20 | if(SumOfDigits%2): 21 | for i in range((b-1)//2): 22 | ans=ans+full[i] 23 | for i in range(26-(b+1)//2,26): 24 | ans=ans+full[i] 25 | else: 26 | for i in range((b+1)//2): 27 | ans=ans+full[i] 28 | for i in range(26-(b-1)//2,26): 29 | ans=ans+full[i] 30 | return ans 31 | 32 | 33 | #{ 34 | # Driver Code Starts 35 | #Initial Template for Python 3 36 | 37 | if __name__=='__main__': 38 | t=int(input()) 39 | for _ in range(t): 40 | N=int(input()) 41 | 42 | ob=Solution() 43 | print(ob.BalancedString(N)) 44 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/B/Balloon Everywhere/10 February 2023/Balloon Everywhere.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 maxInstance(string s){ 14 | 15 | unordered_map mp; 16 | int mini = INT_MAX; 17 | 18 | for (int i=0; i>t; 31 | while(t--) 32 | { 33 | string s; 34 | cin>>s; 35 | Solution ob; 36 | cout< 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | // User function Template for C++ 8 | class Solution { 9 | public: 10 | string baseEquiv(int n, int m){ 11 | int x,cnt; 12 | for(int i=2;i<=32;i++){ 13 | x=n;cnt=1; 14 | while(x>=i){ 15 | cnt++; 16 | x=(x/i); 17 | } 18 | if(cnt==m) return "Yes"; 19 | } 20 | return "No"; 21 | } 22 | }; 23 | 24 | //{ Driver Code Starts. 25 | int main() { 26 | int t; 27 | cin >> t; 28 | while (t--) { 29 | int n, m; 30 | cin >> n >> m; 31 | Solution ob; 32 | cout<x): 10 | x*=i 11 | cnt+=1 12 | if cnt>m: 13 | break 14 | if cnt==m: 15 | return "Yes" 16 | return "No" 17 | 18 | 19 | #{ 20 | # Driver Code Starts 21 | #Initial Template for Python 3 22 | if __name__ == '__main__': 23 | T=int(input()) 24 | for i in range(T): 25 | n,m = input().split() 26 | n=int(n) 27 | m=int(m) 28 | ob = Solution(); 29 | print(ob.baseEquiv(n,m)) 30 | 31 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/B/Base Equivalence/04 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Base Equivalence** 2 | 3 | **Medium** 4 | 5 | Given a number (**n**) and no. of digits (**m**) to represent the number, we have to check if we can represent **n** using exactly **m** digits in any **base** from **2** to **32**. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: n = 8, m = 2 11 | Output: Yes  12 | Explanation: Possible in base 3 as 8 in base 3 is 22. 13 | 14 | ``` 15 | 16 | **Example 2:** 17 | 18 | ``` 19 | Input: n = 8, m = 3 20 | Output: No 21 | Explanation: Not possible in any base. 22 | 23 | ``` 24 | 25 | **Your Task:** 26 | You dont need to read input or print anything. Complete the function **baseEquiv()** which takes n and m as input parameter and returns "Yes" if its possible to represent the number else "No" without quotes.. 27 | 28 | **Expected Time Complexity:** O(logN). 29 | **Expected Auxiliary Space:** O(1) 30 | 31 | **Constraints:** 32 | 1 <= n <=109 33 | 1 <= m <=32 34 | 35 | > ### Problem URL: **[Base Equivalence](https://practice.geeksforgeeks.org/problems/base-equivalence1022/1)** 36 | -------------------------------------------------------------------------------- /Problems & Solutions/B/Binary matrix having maximum number of 1s/12 March 2023/Binary matrix having maximum number of 1s.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | 6 | using namespace std; 7 | 8 | 9 | // } Driver Code Ends 10 | //User function Template for C++ 11 | 12 | class Solution { 13 | public: 14 | vector findMaxRow(vector> mat, int N) { 15 | int row = 0; 16 | int OneCnt = 0; 17 | int i = 0; 18 | int j = N-1; 19 | while (i=0){ 20 | if (mat[i][j] == 1){ 21 | row = i; 22 | OneCnt++; 23 | j--; 24 | } 25 | else{ 26 | i++; 27 | } 28 | } 29 | return {row,OneCnt}; 30 | } 31 | }; 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() { 36 | int t; 37 | cin>>t; 38 | while(t--) { 39 | int n; 40 | cin>>n; 41 | vector> arr(n, vector (n)); 42 | for(int i = 0; i < n; i++) 43 | for(int j = 0; j < n; j++) 44 | cin >> arr[i][j]; 45 | Solution obj; 46 | vector ans = obj.findMaxRow(arr, n); 47 | for(int val : ans) { 48 | cout << val << " "; 49 | } 50 | cout << endl; 51 | } 52 | } 53 | // } Driver Code Ends 54 | -------------------------------------------------------------------------------- /Problems & Solutions/B/Black and White/09 December 2022/C++/Black and White.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | 4 | using namespace std; 5 | 6 | long long numOfWays(int n, int m); 7 | 8 | int main() { 9 | // code 10 | int T; 11 | cin >> T; 12 | while (T--) { 13 | int n, m; 14 | cin >> n >> m; 15 | cout << numOfWays(n, m) << endl; 16 | } 17 | return 0; 18 | } 19 | // } Driver Code Ends 20 | 21 | long long numOfWays(int n, int m) 22 | { 23 | static int x_off[] = {-2, -2, -1, 1, 2, 2, 1, -1}; 24 | static int y_off[] = {-1, 1, 2, 2, 1, -1, -2, -2}; 25 | static const long long MOD = (long long)1e9 + 7; 26 | 27 | long long ret = 0; 28 | int x, y; 29 | 30 | for (int i = 0; i < n; ++i) 31 | { 32 | for (int j = 0; j < m; ++j) 33 | { 34 | for (int k = 0; k < 8; ++k) 35 | { 36 | x = i + x_off[k]; 37 | y = j + y_off[k]; 38 | if (x >= 0 && x < n && y >= 0 && y < m) 39 | ++ret; 40 | } 41 | } 42 | } 43 | long long total = n * m; 44 | 45 | total =(total * (total - 1)) % MOD; 46 | 47 | return (total + MOD - ret) % MOD; 48 | 49 | } -------------------------------------------------------------------------------- /Problems & Solutions/B/Black and White/09 December 2022/Python/Black and White.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | 4 | def numOfWays(m,n): 5 | 6 | x_off = [-2,-2,-1, 1, 2, 2, 1, -1] 7 | y_off = [-1, 1, 2, 2, 1,-1, -2, -2] 8 | MOD = 1000000007 9 | 10 | ret = 0 11 | 12 | for i in range(m): 13 | for j in range(n): 14 | for k in range(8): 15 | 16 | x = i + x_off[k] 17 | y = j + y_off[k] 18 | 19 | if x>=0 and x=0 and y 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | 9 | class Solution{ 10 | public: 11 | int waysToBreakNumber(int N) 12 | { 13 | int mod = 1e9+7; 14 | long long int ans = (N+1)%mod; 15 | ans = ((ans%mod)*((N+2)%mod))%mod; 16 | ans/=2; 17 | ans%=mod; 18 | return ans; 19 | } 20 | }; 21 | 22 | //{ Driver Code Starts. 23 | int main() 24 | { 25 | int t; 26 | cin>>t; 27 | while(t--) 28 | { 29 | int N; 30 | cin >> N; 31 | Solution ob; 32 | cout << ob.waysToBreakNumber(N) << endl; 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/B/Break a number/17 December 2022/Java/Break a number.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | 3 | import java.io.*; 4 | import java.util.*; 5 | 6 | class GFG 7 | { 8 | public static void main(String args[])throws IOException 9 | { 10 | BufferedReader read = new BufferedReader(new InputStreamReader(System.in)); 11 | int t = Integer.parseInt(read.readLine()); 12 | while(t-- > 0) 13 | { 14 | int N = Integer.parseInt(read.readLine()); 15 | 16 | Solution ob = new Solution(); 17 | System.out.println(ob.waysToBreakNumber(N)); 18 | } 19 | } 20 | } 21 | // } Driver Code Ends 22 | 23 | class Solution{ 24 | int waysToBreakNumber(int N){ 25 | int mod = 1000000007; 26 | long ans = (N+1)%mod; 27 | ans = ((ans%mod)*((N+2)%mod))%mod; 28 | ans/=2; 29 | ans%=mod; 30 | return (int)ans; 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Problems & Solutions/B/Break a number/17 December 2022/Python/Break a number.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def waysToBreakNumber (self,N): 3 | return (((N+1)*(N+2))//2)%(10**9+7) 4 | 5 | #{ 6 | # Driver Code Starts 7 | #Initial Template for Python 3 8 | 9 | if __name__ == '__main__': 10 | t = int(input()) 11 | for _ in range(t): 12 | n = int(input()) 13 | 14 | ob = Solution() 15 | print(ob.waysToBreakNumber(n)) 16 | 17 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/B/Break a number/17 December 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Break a number** 2 | 3 | ## **Easy** 4 | 5 | Given a really large number N, break it into 3 whole numbers such that they sum up to the original number and find the number of ways to do so. Since this number can be very large, return it modulo 10^9+7. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | N = 2 12 | Output: 13 | 6 14 | Explanation: 15 | Possible ways to break the number: 16 | 0 + 0 + 2 = 2 17 | 0 + 2 + 0 = 2 18 | 2 + 0 + 0 = 2 19 | 0 + 1 + 1 = 2 20 | 1 + 1 + 0 = 2 21 | 1 + 0 + 1 = 2 22 | 23 | ``` 24 | 25 | **Example 2:** 26 | 27 | ``` 28 | Input: 29 | N = 3 30 | Output: 31 | 10 32 | Explanation: 33 | Possible ways to break the number: 34 | 0+0+3 = 3 35 | 0+3+0 = 3 36 | 3+0+0 = 3 37 | 0+1+2 = 3 38 | 0+2+1 = 3 39 | 1+0+2 = 3 40 | 1+2+0 = 3 41 | 2+0+1 = 3 42 | 2+1+0 = 3 43 | 1+1+1 = 3 44 | ``` 45 | 46 | **Your Task:** 47 | 48 | You don't need to read input or print anything. Your task is to complete the function waysToBreakNumber() which takes an integer N and returns the possible ways to break the number in 3 parts. 49 | 50 | **Expected Time Complexity:** O(1) 51 | **Expected Auxiliary Space:** O(1) 52 | 53 | **Constraints:** 54 | 1 <= N <= 10^9 55 | 56 | > ### **Problem URL: [Break a number](https://practice.geeksforgeeks.org/problems/break-a-number5913/1)** 57 | -------------------------------------------------------------------------------- /Problems & Solutions/B/Build the smallest/10 December 2022/C++/Build the smallest.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | string buildLowestNumber(string num, int k); 5 | // Driver program to test above function 6 | int main() 7 | { 8 | int t,n; 9 | string str; 10 | cin>>t; 11 | while(t--) 12 | { 13 | cin>>n; 14 | cin>>str; 15 | cout << buildLowestNumber(str, n)< mystack; 27 | for(char c: num) 28 | { 29 | while(!mystack.empty() && k>0 && mystack.top()>c) 30 | { mystack.pop(); k-=1; } 31 | 32 | if(!mystack.empty() || c!='0') 33 | mystack.push(c); 34 | } 35 | 36 | while(!mystack.empty() && k--) 37 | mystack.pop(); 38 | if(mystack.empty()) 39 | return "0"; 40 | 41 | while(!mystack.empty()) 42 | { 43 | num[n-1] = mystack.top(); 44 | mystack.pop(); 45 | n-=1; 46 | } 47 | return num.substr(n); 48 | } -------------------------------------------------------------------------------- /Problems & Solutions/B/Build the smallest/10 December 2022/Python/Build the smallest.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | def buildLowestNumber(self, num,k): 4 | stack = [] 5 | for n in num: 6 | while(stack and int(stack[-1])>int(n) and k): 7 | k-=1 8 | stack.pop() 9 | stack.append(n) 10 | while(k): 11 | stack.pop() 12 | k-=1 13 | if len(stack)==0: 14 | return "0" 15 | s='' 16 | for i in range(len(stack)): 17 | s+=stack[i] 18 | return str(int(s)) 19 | 20 | #{ 21 | # Driver Code Starts 22 | #Initial Template for Python 3 23 | 24 | if __name__ == '__main__': 25 | 26 | t = int(input()) 27 | 28 | for _ in range(t): 29 | N = int(input()) 30 | S = input() 31 | 32 | solObj = Solution() 33 | 34 | print(solObj.buildLowestNumber(S,N)) 35 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/B/Burst Balloons/23 December 2022/C++/Burst Balloons.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | class Solution { 8 | public: 9 | int maxCoins(int N, vector &arr) 10 | { 11 | arr.insert(arr.begin(), 1); 12 | arr.push_back(1); 13 | vector> dp(arr.size(), vector(arr.size(), 0)); 14 | for (int len = 1; len <= N; ++len) 15 | for (int left = 1; left <= N - len + 1; ++left) 16 | { 17 | int right = left + len - 1; 18 | for (int k = left; k <= right; ++k) 19 | dp[left][right] = max(dp[left][right], arr[left - 1] * arr[k] * arr[right + 1] + dp[left][k - 1] + dp[k + 1][right]); 20 | } 21 | return dp[1][N]; 22 | } 23 | }; 24 | 25 | //{ Driver Code Starts. 26 | 27 | int main() { 28 | int t; 29 | cin >> t; 30 | while (t--) { 31 | int N; 32 | cin>>N; 33 | 34 | vector arr(N); 35 | for(int i=0; i>arr[i]; 37 | 38 | Solution obj; 39 | cout << obj.maxCoins(N, arr) << endl; 40 | } 41 | return 0; 42 | } 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/B/Burst Balloons/23 December 2022/Python/Burst Balloons.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def maxCoins(self, N : int, arr : List[int]) -> int: 5 | arr=[1]+arr 6 | arr.append(1) 7 | dp=[[0]*(len(arr)+1) for _ in range(len(arr))] 8 | for le in range(1,N+1): 9 | for left in range(1,N-le+2): 10 | right=left+le-1 11 | for k in range(left,right+1): 12 | dp[left][right]=max(dp[left][right],arr[left-1]*arr[k]*arr[right+1]+dp[left][k-1]+dp[k+1][right]) 13 | return dp[1][N] 14 | 15 | #{ 16 | # Driver Code Starts 17 | 18 | class IntArray: 19 | def __init__(self) -> None: 20 | pass 21 | def Input(self,n): 22 | arr=[int(i) for i in input().strip().split()]#array input 23 | return arr 24 | def Print(self,arr): 25 | for i in arr: 26 | print(i,end=" ") 27 | print() 28 | 29 | 30 | if __name__=="__main__": 31 | t = int(input()) 32 | for _ in range(t): 33 | 34 | N = int(input()) 35 | 36 | 37 | arr=IntArray().Input(N) 38 | 39 | obj = Solution() 40 | res = obj.maxCoins(N, arr) 41 | 42 | print(res) 43 | 44 | 45 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/C/Carpet into Box/19 January 2023/Carpet into Box.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | import java.util.*; 3 | import java.io.*; 4 | import java.lang.*; 5 | 6 | class Geeks 7 | { 8 | public static void main(String args[]) 9 | { 10 | Scanner sc = new Scanner(System.in); 11 | int t = sc.nextInt(); 12 | 13 | while(t-- > 0) 14 | { 15 | int A =sc.nextInt(); 16 | int B =sc.nextInt(); 17 | int C =sc.nextInt(); 18 | int D =sc.nextInt(); 19 | 20 | System.out.println(new Solution().carpetBox(A,B,C,D)); 21 | } 22 | } 23 | } 24 | 25 | // } Driver Code Ends 26 | 27 | 28 | //User function Template for Java 29 | 30 | 31 | class Solution{ 32 | 33 | int find(int a,int b) 34 | { 35 | int ans=0; 36 | while(a>b) 37 | { 38 | ans++; 39 | a=(a/2); 40 | } 41 | return ans; 42 | } 43 | 44 | int carpetBox(int A, int B, int C, int D) { 45 | 46 | return Math.min(find(A,C)+find(B,D),find(A,D)+find(B,C)); 47 | } 48 | 49 | } 50 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Carpet into Box/19 January 2023/Carpet into Box.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def carpetBox(self, A,B,C,D): 3 | #code here 4 | dp = dict() 5 | return self.solve(A,B,C,D,dp) 6 | 7 | def solve(self,a,b,c,d,dp): 8 | if (a,b) in dp: 9 | return dp[(a,b)] 10 | if max(a,b) <= max(c,d) and min(a,b) <= min(c,d): 11 | return 0 12 | x =10**9 13 | y = 10**9 14 | z = 10**9 15 | if min(a,b) < max(c,d): 16 | if a <= c or a <= d: 17 | x = self.solve(a,b//2,c,d,dp)+1 18 | if b<=c or b<=d: 19 | y = self.solve(a//2,b,c,d,dp)+1 20 | else: 21 | z = min(self.solve(a//2,b,c,d,dp),self.solve(a,b//2,c,d,dp)) + 1 22 | dp[(a,b)] = min(x,y,z) 23 | return dp[(a,b)] 24 | 25 | #{ 26 | # Driver Code Starts 27 | #Initial Template for Python 3 28 | 29 | def main(): 30 | T=int(input()) 31 | while(T>0): 32 | A,B,C,D = map(int, input().split()) 33 | 34 | obj = Solution() 35 | print(obj.carpetBox(A,B,C,D)) 36 | 37 | T-=1 38 | 39 | 40 | if __name__ == "__main__": 41 | main() 42 | 43 | 44 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/C/Case-specific Sorting of Strings/26 January 2023/Case-specific Sorting of Strings.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 | { 13 | public: 14 | string caseSort(string str, int n) 15 | { 16 | 17 | string v = str; 18 | sort(v.begin(), v.end()); 19 | string ans; 20 | int big_index = -1; 21 | 22 | while(v[++big_index] < 97); 23 | 24 | for(int i = 0, small_index = 0; i < n; i++){ 25 | 26 | ans += str[i] >= 97 ? v[big_index++] : v[small_index++]; 27 | } 28 | 29 | return ans; 30 | } 31 | }; 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() 36 | { 37 | int t; 38 | cin>>t; 39 | while(t--) 40 | { 41 | int n; 42 | cin>>n; 43 | string str; 44 | cin>>str; 45 | Solution obj; 46 | cout< ### **Problem URL: [Case-specific Sorting of Strings](https://practice.geeksforgeeks.org/problems/case-specific-sorting-of-strings4845/1)** 44 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Check if it is possible to convert one string into another with given constraints/02 December 2022/Python/Check if it is possible to convert one string into another with given constraints.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def isItPossible(sef, S, T, M, N): 5 | i=0 6 | j=0 7 | if M!=N: 8 | return 0 9 | while i=M and j=N: 17 | return 0 18 | if i>=M and j>=N: 19 | return 1 20 | if S[i]!=T[j]: 21 | return 0 22 | else: 23 | if S[i]=="A" and ij: 26 | return 0 27 | i+=1 28 | j+=1 29 | return 1 30 | 31 | #{ 32 | # Driver Code Starts 33 | #Initial Template for Python 3 34 | 35 | import math 36 | 37 | if __name__=='__main__': 38 | t=int(input()) 39 | for _ in range(t): 40 | S,T=input().split() 41 | ob=Solution() 42 | print(ob.isItPossible(S,T,len(S),len(T))) 43 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/C/Complement/14 December 2022/Python/Complement.py: -------------------------------------------------------------------------------- 1 | from sys import maxsize 2 | class Solution: 3 | 4 | def findRange(self,a, size): 5 | 6 | max_so_far = -maxsize - 1 7 | max_ending_here = 0 8 | start = 0 9 | end = 0 10 | s = 0 11 | ans=[] 12 | allOne=True 13 | for x in a: 14 | if x=='0': 15 | allOne=False 16 | if allOne: 17 | ans.append(-1) 18 | return ans 19 | 20 | for i in range(0,size): 21 | 22 | max_ending_here += -1 if (a[i]=='1') else 1 23 | 24 | if max_so_far < max_ending_here: 25 | max_so_far = max_ending_here 26 | start = s 27 | end = i 28 | 29 | if max_ending_here < 0: 30 | max_ending_here = 0 31 | s = i+1 32 | ans.append(start+1) 33 | ans.append(end+1) 34 | return ans 35 | 36 | #{ 37 | # Driver Code Starts 38 | if __name__ == '__main__': 39 | 40 | 41 | tc=int(input()) 42 | while tc > 0: 43 | n=int(input()) 44 | s=input() 45 | ob = Solution() 46 | ans = ob.findRange(s, n) 47 | if len(ans)==1: 48 | print(ans[0]) 49 | else: 50 | print(str(ans[0])+" "+str(ans[1])) 51 | tc=tc-1 52 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/C/Container With Most Water/27 December 2022/C++/Container With Most Water.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial template for C++ 3 | 4 | #include 5 | using namespace std; 6 | // int maxArea(int A[], int len); 7 | 8 | // } Driver Code Ends 9 | 10 | long long maxArea(long long A[], int len) 11 | { 12 | int l = 0; 13 | int r = len -1; 14 | long long area = 0; 15 | 16 | while (l < r) 17 | { 18 | area = max(area, min(A[l], A[r]) * (r - l)); 19 | 20 | if (A[l] < A[r]) 21 | l += 1; 22 | else 23 | r -= 1; 24 | } 25 | return area; 26 | } 27 | 28 | //{ Driver Code Starts. 29 | 30 | // Driver code 31 | int main() 32 | { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | int n; 38 | cin>>n; 39 | long long arr[n]; 40 | for(int i=0;i>arr[i]; 42 | cout<0){ 15 | int n = Integer.parseInt(br.readLine()); // input size of array 16 | int arr[] = new int[n]; 17 | String inputLine[] = br.readLine().trim().split("\\s+"); 18 | for(int i=0; i 3 | 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | //User function template for C++ 9 | class Solution{ 10 | public: 11 | void convert(int arr[], int n) { 12 | 13 | vector> ans; 14 | 15 | for (int i=0; i> t; 33 | while (t--) { 34 | int n; 35 | cin >> n; 36 | int arr[n]; 37 | for (int i = 0; i < n; i++) { 38 | cin >> arr[i]; 39 | } 40 | Solution ob; 41 | ob.convert(arr, n); 42 | for (int i = 0; i < n; i++) { 43 | cout << arr[i] << " "; 44 | } 45 | cout << "\n"; 46 | } 47 | return 0; 48 | } 49 | 50 | // } Driver Code Ends 51 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Convert an array to reduced form/24 January 2023/Convert an array to reduced form.py: -------------------------------------------------------------------------------- 1 | import numpy as np 2 | class Solution: 3 | def convert(self,arr, n): 4 | indices = np.argsort(arr) 5 | for i in range(n): arr[indices[i]] = i 6 | 7 | #{ 8 | # Driver Code Starts 9 | #Initial Template for Python 3 10 | 11 | if __name__ == '__main__': 12 | tc = int(input()) 13 | while tc > 0: 14 | n = int(input()) 15 | arr = list(map(int, input().strip().split())) 16 | ob = Solution() 17 | ob.convert(arr, n) 18 | for x in arr: 19 | print(x, end=" ") 20 | print() 21 | tc -= 1 22 | 23 | # } Driver Code Ends 24 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Count Cyclic Paths/20 February 2023/Count Cyclic Paths.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 countPaths(int N){ 13 | // code here 14 | if (N==1) return 0; 15 | if (N==2) return 3; 16 | if (N==3) return 6; 17 | 18 | int m = pow(10,9) + 7; 19 | long long int arr[N+1] = {0}; 20 | arr[1]=0; 21 | arr[2]=3; 22 | arr[3]=6; 23 | 24 | for (int i=4; i<=N; i++){ 25 | arr[i] = ( (arr[i-1]*2)%m + (arr[i-2]*3)%m )%m; 26 | } 27 | 28 | return arr[N]; 29 | } 30 | }; 31 | 32 | //{ Driver Code Starts. 33 | int main() 34 | { 35 | int t; 36 | cin>>t; 37 | while(t--) 38 | { 39 | int N; 40 | cin >> N; 41 | 42 | Solution ob; 43 | cout << ob.countPaths(N) << endl; 44 | } 45 | return 0; 46 | } 47 | 48 | // } Driver Code Ends 49 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Count Cyclic Paths/20 February 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Count Cyclic Paths** 2 | 3 | ## **Medium** 4 | 5 | Given a triangular pyramid with its vertices marked as O, A, B and C and a number N, the task is to find the number of ways such that a person starting from the origin O initially, reaches back to the origin in N steps. In a single step, a person can go to any of its adjacent vertices. 6 | 7 | ![img](https://media.geeksforgeeks.org/wp-content/uploads/20200520133822/pyramid1.jpg) 8 | 9 | **Example 1:** 10 | 11 | ``` 12 | Input: 13 | N = 1 14 | Output: 0 15 | Explanation: The minimum length of 16 | a cyclic path is 2. 17 | ``` 18 | 19 | **Example 2:** 20 | 21 | ``` 22 | Input: 23 | N = 2 24 | Output: 3 25 | Explanation: The three paths are : 26 | O-A-O, O-B-O, O-C-O 27 | ``` 28 | 29 | **Your Task:** 30 | You don't need to read input or print anything. Your task is to complete the function countPaths() which takes an integer N as input parameter and returns the number of possible paths. Since the answer may be big, return it modulo (10^9+7). 31 | 32 | **Expected Time Complexity:** O(N) 33 | **Expected Space Complexity:** O(1) 34 | 35 | **Constraints:** 36 | 37 | 1 ≤ N ≤ $10^6$ 38 | 39 | > ### **Problem URL: [Count Cyclic Paths](https://practice.geeksforgeeks.org/problems/aa0000a5f710ce8d41366b714341eef644ec7b82/1)** 40 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Count even length/01 January 2023/C++/Count even length.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution{ 7 | public: 8 | using ll = long long int; 9 | ll mod = 1e9 + 7; 10 | ll modpow(ll a, ll b) 11 | { 12 | ll x = 1%mod; 13 | a %= mod; 14 | while(b) 15 | { 16 | if(b&1) 17 | x = (x*a)%mod; 18 | a = (a*a)%mod; 19 | b >>= 1; 20 | } 21 | return x; 22 | } 23 | ll modinverse(ll a) 24 | { 25 | return modpow(a,mod-2); 26 | } 27 | int compute_value(int n) 28 | { 29 | if(n==1) 30 | return 2; 31 | ll val=2; 32 | for(ll i=1;i> tc; 46 | while(tc--){ 47 | int n; 48 | cin >> n; 49 | Solution ob; 50 | int ans = ob.compute_value(n); 51 | cout << ans <<"\n"; 52 | } 53 | return 0; 54 | } 55 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/C/Count even length/01 January 2023/Java/Count even length.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for Java 3 | 4 | import java.util.*; 5 | import java.lang.*; 6 | import java.io.*; 7 | class GFG 8 | { 9 | public static void main(String[] args) throws IOException 10 | { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | int T = Integer.parseInt(br.readLine().trim()); 13 | while(T-->0) 14 | { 15 | int n = Integer.parseInt(br.readLine().trim()); 16 | Solution ob = new Solution(); 17 | int ans = ob.compute_value(n); 18 | System.out.println(ans); 19 | } 20 | } 21 | } 22 | 23 | // } Driver Code Ends 24 | 25 | class Solution 26 | { 27 | static long mod=1000000007; 28 | public int compute_value(int n) 29 | { 30 | if(n==1) 31 | return 2; 32 | long val=2; 33 | for(long i=1;i0) 48 | { 49 | if((n&1)!=0) 50 | { 51 | res=(res*m)%mod; 52 | } 53 | n=n>>1; 54 | m=(m*m)%mod; 55 | } 56 | return res; 57 | } 58 | } -------------------------------------------------------------------------------- /Problems & Solutions/C/Count even length/01 January 2023/Python/Count even length.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def compute_value(self, n): 3 | mod=10**9+7 4 | if n==1: 5 | return 2 6 | val=2 7 | for i in range(1,n): 8 | val=(val*((2*i)+1)*2)%mod 9 | x=pow(i+1,mod-2,mod) 10 | val=(val*x)%mod 11 | return val 12 | 13 | #{ 14 | # Driver Code Starts 15 | #Initial Template for Python 3 16 | 17 | if __name__ == '__main__': 18 | T=int(input()) 19 | for i in range(T): 20 | n = int(input()) 21 | ob = Solution() 22 | ans = ob.compute_value(n) 23 | print(ans) 24 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/C/Count even length/01 January 2023/README..md: -------------------------------------------------------------------------------- 1 | # **Count even length** 2 | 3 | ## **Medium** 4 | 5 | Given a number n, find count of all binary sequences of length 2n such that sum of first n bits is same as sum of last n bits. 6 | The anwer can be very large. So, you have to return answer modulo 109+7. 7 | 8 | **Example:** 9 | 10 | ``` 11 | Input: n = 2 12 | Output: 6 13 | Explanation: There are 6 sequences of length 14 | 2*n, the sequences are 0101, 0110, 1010, 1001, 15 | 0000 and 1111. 16 | ``` 17 | 18 | **Example:** 19 | 20 | ``` 21 | Input: n = 1 22 | Output: 2 23 | Explanation: There are 2 sequence of length 24 | 2*n, the sequence are 00 and 11. 25 | ``` 26 | 27 | **Your Task:** 28 | You don't need to read or print anyhting. Your task is to complete the function **compute_value()** which takes n as input parameter and returns count of all binary sequence of length 2\*n such that sum of first n bits is same as sum of last n bits modulo 109 + 7. 29 | 30 | **Expected Time Complexity:** O(n \* log(n)) 31 | **Expected Space Complexity:** O(1) 32 | 33 | **Constraints:** 34 | 1 <= n <= 10^5 35 | 36 | > ### **Problem URL: [Count even length](https://practice.geeksforgeeks.org/problems/count-even-length1907/1)** 37 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Count number of free cell/08 February 2023/Count number of free cell.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | 4 | #include 5 | using namespace std; 6 | 7 | 8 | 9 | // } Driver Code Ends 10 | //User function Template for C++ 11 | 12 | 13 | class Solution{ 14 | public: 15 | vector countZero(int n, int k, vector>& arr){ 16 | //Code Here 17 | vector ans; 18 | unordered_set row, col; 19 | 20 | for (auto& a:arr){ 21 | row.insert(a[0]); 22 | col.insert(a[1]); 23 | ans.push_back( (n-row.size()) * (n-col.size()) ); 24 | } 25 | return ans; 26 | } 27 | }; 28 | 29 | //{ Driver Code Starts. 30 | int main(){ 31 | int t; 32 | cin>>t; 33 | while(t--){ 34 | int n,k; 35 | cin>>n>>k; 36 | vector> arr(k,vector(2)); 37 | for(int i=0;i>x>>y; 40 | arr[i][0] = x; 41 | arr[i][1] = y; 42 | } 43 | Solution ob; 44 | vector ans = ob.countZero(n,k,arr); 45 | 46 | for(int i=0;i 3 | 4 | using namespace std; 5 | 6 | #define ll long long 7 | 8 | 9 | // } Driver Code Ends 10 | //User function template for C++ 11 | class Solution{ 12 | public: 13 | // #define ll long long 14 | 15 | ll countSubarray(int arr[], int n, int k) { 16 | ll count=0; 17 | 18 | for(int i=0; ik) 23 | { 24 | count+=(n-j); 25 | break; 26 | } 27 | } 28 | } 29 | return count; 30 | } 31 | }; 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() { 36 | int t; 37 | cin >> t; 38 | while (t--) { 39 | int n, k, i; 40 | cin >> n >> k; 41 | int arr[n]; 42 | for (i = 0; i < n; i++) { 43 | cin >> arr[i]; 44 | } 45 | Solution ob; 46 | auto ans = ob.countSubarray(arr, n, k); 47 | cout << ans << "\n"; 48 | } 49 | return 0; 50 | } 51 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/C/Count of Subarrays/17 November 2022/Python/Count of Subarrays.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | class Solution: 3 | 4 | 5 | def countSubarray(self,arr, n, k): 6 | dp = [0 for i in range(n)] 7 | for i in range(n): 8 | if(arr[i] > k): 9 | dp[i] = n-i 10 | tmp = 0 11 | for i in range(n-1, -1, -1): 12 | if(dp[i] != 0): 13 | tmp = dp[i] 14 | continue 15 | dp[i] = tmp 16 | return sum(dp) 17 | 18 | #{ 19 | # Driver Code Starts 20 | #Initial Template for Python 3 21 | 22 | 23 | # Driver code 24 | if __name__ == "__main__": 25 | tc=int(input()) 26 | while tc > 0: 27 | n, k=map(int, input().strip().split()) 28 | arr=list(map(int, input().strip().split())) 29 | ob = Solution() 30 | ans=ob.countSubarray(arr, n, k) 31 | print(ans) 32 | tc=tc-1 33 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/C/Count of Subarrays/17 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Count of Subarrays** 2 | 3 | ## **Medium** 4 | 5 | Given an array of **N** positive integers **Arr1, Arr2 ............ Arrn**. The value of each contiguous subarray of given array is the **maximum element present in that subarray**. The task is to return the number of subarrays having value strictly greater than **K**. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | N = 3, K = 2 12 | Arr[] = {3, 2, 1} 13 | Output: 3 14 | Explanation: The subarrays having value 15 | strictly greater than K are: [3], [3, 2] 16 | and [3, 2, 1]. Thus there are 3 such 17 | subarrays. 18 | 19 | ``` 20 | 21 | **Example 2:** 22 | 23 | ``` 24 | Input: 25 | N = 4, K = 1 26 | Arr[] = {1, 2, 3, 4} 27 | Output: 9 28 | Explanation: There are 9 subarrays having 29 | value strictly greater than K. 30 | 31 | ``` 32 | 33 | **Your Task:** 34 | Complete the function **countSubarray()** which takes an array **arr,** two integers **n, k,** as input parameters and returns an integer denoting the answer. You don't to print answer or take inputs. 35 | 36 | **Expected Time Complexity:** O(N) 37 | **Expected Auxiliary Space:** O(1) 38 | 39 | **Constraints:** 40 | 41 | - `1 <= N <= 10^5` 42 | - `1 <= Arr\[i\] <= 10^5` 43 | 44 | > ### **Problem URL: [Sum of Beauty of All Substrings](https://practice.geeksforgeeks.org/problems/count-of-subarrays5922/1)** 45 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Count the substrings/15 January 2023/Count the Substring.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 | long long countSubstring(string S){ 11 | int n=S.size(); 12 | long long ans=0; 13 | int sum=0,onesekam=0; 14 | unordered_mapmp; 15 | for(auto it:S){ 16 | if(it=='1')sum++; 17 | else sum--; 18 | if(sum<=0)onesekam++; 19 | mp[sum]++; 20 | } 21 | sum=0; 22 | for(int i=0;i>t; 45 | for(int i=1;i<=t;i++){ 46 | string S; 47 | cin>>S; 48 | Solution obj; 49 | long long ans =obj.countSubstring(S); 50 | cout< ### **Problem URL: [Count the Substring](https://practice.geeksforgeeks.org/problems/f72994353d123b925ff20f0694b662191df03ea2/1)** 42 | -------------------------------------------------------------------------------- /Problems & Solutions/C/Cutting Rectangles/03 February 2023/Cutting Rectangles.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 | vector minimumSquares(long long int L, long long int B) 13 | { 14 | long long int K = __gcd(L,B); 15 | long long int N = L/K * B/K; 16 | return {N,K}; 17 | } 18 | }; 19 | 20 | //{ Driver Code Starts. 21 | 22 | int main(){ 23 | int t; 24 | cin>>t; 25 | while(t--){ 26 | long long int L, B; 27 | cin>>L>>B; 28 | 29 | Solution ob; 30 | vector ans = ob.minimumSquares(L, B); 31 | cout< ### **Problem URL: [Cutting Rectangles](https://practice.geeksforgeeks.org/problems/a7a4da81b20f4a05bbd93f5786fcf7478298f4f5/1)** 35 | -------------------------------------------------------------------------------- /Problems & Solutions/D/Distinct Coloring/01 February 2023/Distinct Coloring.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 | long long int distinctColoring(int N, int r[], int g[], int b[]){ 12 | long long int dp[N][3]; 13 | dp[0][0]=r[0]; 14 | dp[0][1]=g[0]; 15 | dp[0][2]=b[0]; 16 | for(int i=1;i>t; 31 | while(t--) 32 | { 33 | int N; 34 | cin >> N; 35 | int r[N],g[N],b[N]; 36 | for(int i = 0; i < N; i++) 37 | cin >> r[i]; 38 | for(int i = 0; i < N; i++) 39 | cin >> g[i]; 40 | for(int i = 0; i < N; i++) 41 | cin >> b[i]; 42 | 43 | Solution ob; 44 | cout << ob.distinctColoring(N, r, g, b) << endl; 45 | } 46 | return 0; 47 | } 48 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/D/Distinct Coloring/01 February 2023/Distinct Coloring.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def distinctColoring (self, N, r, g, b): 3 | arr=[r,g,b] 4 | for i in range(N-2,-1,-1): 5 | arr[0][i]+=min(arr[1][i+1],arr[2][i+1]) 6 | arr[1][i]+=min(arr[0][i+1],arr[2][i+1]) 7 | arr[2][i]+=min(arr[1][i+1],arr[0][i+1]) 8 | return min(arr[0][0],arr[1][0],arr[2][0]) 9 | 10 | #{ 11 | # Driver Code Starts 12 | #Initial Template for Python 3 13 | 14 | if __name__ == '__main__': 15 | t = int (input ()) 16 | for _ in range (t): 17 | N = int(input()) 18 | r = list(map(int, input().split())) 19 | g = list(map(int, input().split())) 20 | b = list(map(int, input().split())) 21 | 22 | ob = Solution() 23 | print(ob.distinctColoring(N, r, g, b)) 24 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/F/Fill the Matrix/21 February 2023/Fill the Matrix.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 minIteration(int N, int M, int x, int y){ 13 | 14 | int hmax = max(x-1, N-x); 15 | int wmax = max(y-1, M-y); 16 | return (hmax + wmax); 17 | } 18 | }; 19 | 20 | //{ Driver Code Starts. 21 | int main() 22 | { 23 | int t; 24 | cin>>t; 25 | while(t--) 26 | { 27 | int N, M, x, y; 28 | cin >> N >> M; 29 | cin >> x >> y; 30 | 31 | Solution ob; 32 | cout << ob.minIteration(N, M, x, y) << endl; 33 | } 34 | return 0; 35 | } 36 | // } Driver Code Ends 37 | -------------------------------------------------------------------------------- /Problems & Solutions/F/Fill up buckets/13 November 2022/C++/Fill up buckets.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 totalWays(int n, vector a) { 13 | sort(a.begin(),a.end()); 14 | long long m = 1e9 + 7; 15 | long long ans = 1; 16 | for(int i =0;i> tc; 27 | while(tc--){ 28 | int n; 29 | cin >> n; 30 | vectorcapacity(n); 31 | for(int i = 0; i < n; i++) 32 | cin >> capacity[i]; 33 | Solution ob; 34 | int ans = ob.totalWays(n, capacity); 35 | cout << ans << "\n"; 36 | } 37 | return 0; 38 | } 39 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/F/Fill up buckets/13 November 2022/Java/Fill up buckets.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for Java 3 | 4 | import java.util.*; 5 | import java.lang.*; 6 | import java.io.*; 7 | class GFG 8 | { 9 | public static void main(String[] args) throws IOException 10 | { 11 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 12 | int T = Integer.parseInt(br.readLine().trim()); 13 | while(T-->0) 14 | { 15 | int n = Integer.parseInt(br.readLine().trim()); 16 | String s = br.readLine().trim(); 17 | String[] S = s.split(" "); 18 | int[] capacity = new int[n]; 19 | for(int i = 0; i < n; i++) 20 | capacity[i] = Integer.parseInt(S[i]); 21 | Solution ob = new Solution(); 22 | int ans = ob.totalWays(n, capacity); 23 | System.out.println(ans); 24 | } 25 | } 26 | } 27 | 28 | // } Driver Code Ends 29 | 30 | 31 | //User function Template for Java 32 | 33 | class Solution{ 34 | int mod=(int) (1e9 + 7); 35 | public int totalWays(int n, int[] capacity) { 36 | // code here 37 | if(n==1) return capacity[0]; 38 | Arrays.sort(capacity); 39 | long res=1; 40 | for(int i=0;i 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution { 11 | public: 12 | int minLaptops(int N, int start[], int end[]) { 13 | mapmp; 14 | for(int i=0; i> s; 20 | for(auto it: mp) 21 | s.push_back(it); 22 | sort(s.begin(), s.end()); 23 | int c=0,ans=0; 24 | for(pair i:s) 25 | { 26 | c+=i.second; 27 | ans=max(ans,c); 28 | } 29 | return ans; 30 | } 31 | }; 32 | 33 | //{ Driver Code Starts. 34 | 35 | int main() { 36 | int t; 37 | cin >> t; 38 | while (t--) { 39 | int N; 40 | cin >> N; 41 | int start[N], end[N]; 42 | for(int i=0; i>start[i]; 44 | for(int i=0; i>end[i]; 46 | 47 | Solution ob; 48 | cout << ob.minLaptops(N, start, end) << endl; 49 | } 50 | } 51 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/F/Find minimum number of Laptops required/31 December 2022/Python/Find minimum number of Laptops required.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minLaptops(self, N, start, end): 3 | 4 | mp=dict() 5 | 6 | for i in start: 7 | if i in mp: 8 | mp[i] += 1 9 | else: 10 | mp[i] = 1 11 | for i in end: 12 | if i in mp: 13 | mp[i] += -1 14 | else: 15 | mp[i] = -1 16 | 17 | c = ans = 0 18 | for i in sorted(mp): 19 | c += mp[i] 20 | ans = max(ans,c) 21 | return ans 22 | 23 | #{ 24 | # Driver Code Starts 25 | #Initial Template for Python 3 26 | 27 | if __name__ == '__main__': 28 | 29 | T = int(input()) 30 | while T > 0: 31 | N = int(input()) 32 | start = list(map(int,input().split())) 33 | end = list(map(int,input().split())) 34 | 35 | ob = Solution() 36 | print(ob.minLaptops(N, start, end)) 37 | 38 | T -= 1 39 | 40 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/F/Find patterns/14 November 2022/C++/Find patterns.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 numberOfSubsequences(string S, string W){ 13 | int count=0; 14 | int i=0,j=0; 15 | int n=S.size(); 16 | int k=W.size(); 17 | bool lookup[n]={false}; 18 | while(i>t; 39 | while(t--) 40 | { 41 | string S,W; 42 | cin >> S >> W; 43 | Solution ob; 44 | cout << ob.numberOfSubsequences(S,W) << endl; 45 | } 46 | return 0; 47 | } 48 | 49 | 50 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/F/Find patterns/14 November 2022/Python/Find patterns.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | class Solution: 3 | def numberOfSubsequences (self,S,W): 4 | ans=int() 5 | s=[char for char in S] 6 | w=[char for char in W] 7 | i,j=[0]*2 8 | while i ### **Problem URL: [Find patterns](https://practice.geeksforgeeks.org/problems/find-patterns0606/1)** 44 | -------------------------------------------------------------------------------- /Problems & Solutions/F/Floyd Warshall/07 November 2022/Python/Floyd Warshall.py: -------------------------------------------------------------------------------- 1 | #User function template for Python 2 | 3 | class Solution: 4 | def shortest_distance(self, matrix): 5 | #Code here 6 | n=len(matrix) 7 | for k in range(n): 8 | for i in range(n): 9 | for j in range(n): 10 | if(matrix[i][k]== -1 or matrix[k][j] ==-1): 11 | continue 12 | cur=matrix[i][k]+matrix[k][j] 13 | if(matrix[i][j]==-1): 14 | matrix[i][j]=cur 15 | else: 16 | matrix[i][j]=min(matrix[i][j],cur) 17 | 18 | #{ 19 | # Driver Code Starts 20 | #Initial template for Python 21 | 22 | if __name__ == '__main__': 23 | T=int(input()) 24 | for i in range(T): 25 | n = int(input()) 26 | matrix = [] 27 | for _ in range(n): 28 | matrix.append(list(map(int, input().split()))) 29 | obj = Solution() 30 | obj.shortest_distance(matrix) 31 | for _ in range(n): 32 | for __ in range(n): 33 | print(matrix[_][__], end = " ") 34 | print() 35 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/G/Geek hates too many 1s/06 March 2023/Geek hates too many 1s.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 | 12 | public: 13 | int noConseBits(int n) { 14 | 15 | vector v; 16 | while(n){ 17 | v.push_back(n%2); 18 | n/=2; 19 | } 20 | int c=0; 21 | int ans=0; 22 | for(int i=v.size()-1; i>=0; i--){ 23 | if(v[i]){ 24 | c++; 25 | if(c==3){ 26 | c=0; 27 | } 28 | else ans += pow(2,i); 29 | } 30 | else c=0; 31 | } 32 | return ans; 33 | } 34 | }; 35 | 36 | 37 | //{ Driver Code Starts. 38 | 39 | int main() { 40 | 41 | int tt; 42 | cin >> tt; 43 | Solution sol; 44 | while (tt--) { 45 | 46 | int n; 47 | cin >> n; 48 | int ans = sol.noConseBits(n); 49 | cout << ans << '\n'; 50 | } 51 | 52 | return 0; 53 | } 54 | 55 | // } Driver Code Ends 56 | -------------------------------------------------------------------------------- /Problems & Solutions/G/Geeks And The String/23 January 2023/Geeks And The String.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | class Solution { 8 | public: 9 | string removePair(string s) { 10 | 11 | int j = -1; 12 | 13 | for(int i=0; i= 0 && s[j] == s[i]) j--; 16 | else s[++j] = s[i]; 17 | } 18 | 19 | return j < 0 ? "-1" : s.substr(0, j+1); 20 | } 21 | }; 22 | 23 | 24 | //{ Driver Code Starts. 25 | 26 | int main(){ 27 | int t; 28 | cin>>t; 29 | while(t--){ 30 | string s; 31 | cin>>s; 32 | 33 | Solution obj; 34 | string res = obj.removePair(s); 35 | 36 | cout< 4 | using namespace std; 5 | 6 | 7 | // } Driver Code Ends 8 | //User function Template for C++ 9 | class Solution{ 10 | public: 11 | 12 | int solve(int i, int n, vector &dp, vector &arr){ 13 | if (i<0 || i>=n){ 14 | return 1; 15 | } 16 | 17 | if (dp[i] != -1){ 18 | return dp[i]; 19 | } 20 | 21 | dp[i] = 0; 22 | 23 | return dp[i] = solve(i+arr[i], n, dp, arr); 24 | } 25 | 26 | int goodStones(int n,vector &arr){ 27 | vector dp(n, -1); 28 | 29 | for(int i=0; i>t; 44 | while(t--){ 45 | int n; 46 | cin>>n; 47 | vector arr(n); 48 | for(int i=0;i>arr[i]; 50 | } 51 | Solution ob; 52 | cout< 5 | using namespace std; 6 | 7 | // } Driver Code Ends 8 | //User function Template for C++ 9 | 10 | class Solution { 11 | public: 12 | int maxGroupSize(int arr[], int n, int k) { 13 | int fre[k]{} ; 14 | for(int i = 0; i < n; i++) 15 | { 16 | fre[arr[i]%k]++ ; 17 | } 18 | int ans = 0 ; 19 | ans += (fre[0] > 0) ; 20 | for(int i = 1; i < k; i++) 21 | { 22 | if(2*i == k) 23 | { 24 | ans += (fre[i] > 0) ; 25 | break ; 26 | } 27 | if(2*i > k) break ; 28 | ans += max(fre[i], fre[k-i]) ; 29 | } 30 | return ans ; 31 | } 32 | }; 33 | 34 | //{ Driver Code Starts. 35 | int main() { 36 | int t; 37 | cin >> t; 38 | while (t--) { 39 | int N,K; 40 | 41 | cin>>N>>K; 42 | int arr[N]; 43 | 44 | for(int i=0; i>arr[i]; 46 | 47 | Solution ob; 48 | cout << ob.maxGroupSize(arr,N,K) << endl; 49 | } 50 | return 0; 51 | } 52 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/G/Grouping Of Numbers/05 November 2022/Python/Grouping Of Numbers.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def maxGroupSize(self, arr, N, K): 5 | # code here 6 | ns = [0]*K 7 | for e in arr: 8 | ns[e%K] += 1 9 | 10 | ans = ns[0] > 0 11 | 12 | for i in range(1, K//2+1): 13 | 14 | if i*2 == K: 15 | ans += ns[i] > 0 16 | else: 17 | ans += max(ns[i], ns[K-i]) 18 | return ans 19 | 20 | #{ 21 | # Driver Code Starts 22 | #Initial Template for Python 3 23 | 24 | if __name__ == '__main__': 25 | t = int (input ()) 26 | for _ in range (t): 27 | N,K=map(int,input().split()) 28 | arr=list(map(int,input().split())) 29 | 30 | ob = Solution() 31 | print(ob.maxGroupSize(arr,N,K)) 32 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/I/Is it Fibonacci/17 February 2023/Is it Fibonacci.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 solve(int N, int K, vector GeekNum) { 14 | 15 | if (N < K) return GeekNum[N-1]; 16 | 17 | vector dp(N, 0); 18 | long long sum = 0; 19 | 20 | for (int i=0; i> T; 40 | while (T--) { 41 | int N, K; 42 | cin >> N >> K; 43 | 44 | vector GeekNum(K); 45 | 46 | for (int i = 0; i < K; i++) cin >> GeekNum[i]; 47 | 48 | Solution ob; 49 | cout << ob.solve(N, K, GeekNum) << "\n"; 50 | } 51 | return 0; 52 | } 53 | 54 | // } Driver Code Ends 55 | -------------------------------------------------------------------------------- /Problems & Solutions/J/Jumping Numbers/10 November 2022/Python/Jumping Numbers.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def jumpingNums(self, X): 5 | if X<=9: 6 | return X 7 | diff = X 8 | ans = 0 9 | def solve(v): 10 | nonlocal ans,diff 11 | if v>X: 12 | return 13 | if v<= X and v>ans: 14 | ans = v 15 | diff = X-v 16 | rem = v%10 17 | if rem>0: 18 | solve(v*10 + rem-1) 19 | if rem<9: 20 | solve(v*10 + rem+1) 21 | return 22 | for i in range(1,10): 23 | solve(i) 24 | return ans 25 | 26 | #{ 27 | # Driver Code Starts 28 | #Initial Template for Python 3 29 | 30 | if __name__ == '__main__': 31 | t = int (input ()) 32 | for _ in range (t): 33 | X=int(input()) 34 | 35 | ob = Solution() 36 | print(ob.jumpingNums(X)) 37 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/J/Jumping Numbers/10 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Jumping Numbers** 2 | 3 | ## **Medium** 4 | 5 | Given a positive number **X**. Find the largest Jumping Number which is smaller than or equal to X. 6 | **Jumping Number:** A number is called Jumping Number if all adjacent digits in it differ by only 1. All single-digit numbers are considered as Jumping Numbers. For example **7**, **8987** and **4343456** are Jumping numbers but **796, 677** and **89098** are not. 7 | 8 | **Example 1:** 9 | 10 | ``` 11 | Input: 12 | X = 10 13 | Output: 14 | 10 15 | Explanation: 16 | 10 is the largest Jumping Number 17 | possible for X = 10. 18 | ``` 19 | 20 | **Example 2:** 21 | 22 | ``` 23 | Input: 24 | X = 50 25 | Output: 26 | 45 27 | Explanation: 28 | 45 is the largest Jumping Number 29 | possible for X = 50. 30 | ``` 31 | 32 | **Your Task:** 33 | You don't need to read input or print anything. Your task is to complete the function **jumpingNums()** which takes an Integer X as input and returns the largest Jumping Number less than or equal to X. 34 | 35 | **Expected Time Complexity:** O(k), where k is no of jumping numbers 36 | **Expected Auxiliary Space:** O(k), where k is no of jumping numbers 37 | 38 | **Constraints:** 39 | 1 <= X <= 10^9 40 | 41 | > ### **Problem URL: [Jumping Numbers](https://practice.geeksforgeeks.org/problems/jumping-numbers3805/1)** 42 | -------------------------------------------------------------------------------- /Problems & Solutions/K/k-th smallest element in BST/07 December 2022/README.md: -------------------------------------------------------------------------------- 1 | # **k-th smallest element in BST** 2 | 3 | ## **Medium** 4 | 5 | Given a BST and an integer K. Find the Kth Smallest element in the BST using O(1) extra space. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 |   2 12 |   / \ 13 |   1 3 14 | K = 2 15 | Output: 2 16 | Explanation: 2 is the 2nd smallest element in the BST 17 | 18 | ``` 19 | 20 | **Example 2:** 21 | 22 | ``` 23 | Input: 24 | 2 25 |   / \ 26 |   1 3 27 | K = 5 28 | Output: -1 29 | Explanation: There is no 5th smallest element in the BST as the size of BST is 3 30 | ``` 31 | 32 | **Your Task:** 33 | You don't need to read input or print anything. Your task is to complete the function **KthSmallestElement()** which takes the root of the BST and integer K as inputs and return the Kth smallest element in the BST, if no such element exists return -1. 34 | 35 | **Expected Time Complexity:** O(N). 36 | **Expected Auxiliary Space:** O(1). 37 | 38 | **Constraints:** 39 | 1<=Number of nodes<=100000 40 | 41 | > ### **Problem URL: [k-th smallest element in BST](https://practice.geeksforgeeks.org/problems/find-k-th-smallest-element-in-bst/1)** 42 | -------------------------------------------------------------------------------- /Problems & Solutions/L/LCM Triplet/20 November 2022/C++/LCM Triplet.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for C++ 3 | 4 | // Initial Template for C++ 5 | // Back-end complete function Template for C++ 6 | // User function Template for C++ 7 | #include 8 | using namespace std; 9 | 10 | // } Driver Code Ends 11 | // User function Template for C++ 12 | 13 | class Solution { 14 | public: 15 | long long lcmTriplets(long long N) { 16 | if(N<=2) 17 | return N; 18 | else if(N%2==0 && N%3==0) 19 | return (N-1)*(N-2)*(N-3); 20 | else if(N%2==0) 21 | return N*(N-1)*(N-3); 22 | return N*(N-1)*(N-2); 23 | } 24 | }; 25 | 26 | //{ Driver Code Starts. 27 | 28 | int main() { 29 | int t; 30 | cin >> t; 31 | while (t--) { 32 | long long N; 33 | cin >> N; 34 | Solution ob; 35 | cout << ob.lcmTriplets(N) << "\n"; 36 | } 37 | } 38 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/L/LCM Triplet/20 November 2022/Java/LCM Triplet.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for Java 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | class GFG { 8 | public static void main(String args[]) throws IOException { 9 | BufferedReader read = 10 | new BufferedReader(new InputStreamReader(System.in)); 11 | int t = Integer.parseInt(read.readLine()); 12 | while (t-- > 0) { 13 | long N = Long.parseLong(read.readLine()); 14 | Solution ob = new Solution(); 15 | System.out.println(ob.lcmTriplets(N)); 16 | } 17 | } 18 | } 19 | // } Driver Code Ends 20 | 21 | 22 | // User function Template for Java 23 | 24 | class Solution { 25 | long lcmTriplets(long N) { 26 | // code here 27 | if(N == 1){ 28 | return 1; 29 | } 30 | if(N == 2){ 31 | return 2; 32 | } 33 | if(N%2 != 0){ 34 | return N*(N-1)*(N-2); 35 | } 36 | 37 | if(N%3 == 0){ 38 | return (N-1)* (N-2)*(N-3); 39 | } 40 | return Math.max(N*(N-1) * (N-3), (N-1)* (N-2)*(N-3)); 41 | } 42 | } -------------------------------------------------------------------------------- /Problems & Solutions/L/LCM Triplet/20 November 2022/Python/LCM Triplet.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def lcmTriplets(self,N): 5 | if N<3: 6 | return N 7 | if N%2==0: 8 | if N%3==0: 9 | return ((N-2)*(N-1)*(N-3)) 10 | else: 11 | return ((N-3)*(N-1)*(N)) 12 | return ((N-2)*(N-1)*(N)) 13 | 14 | #{ 15 | # Driver Code Starts 16 | #Initial Template for Python 3 17 | 18 | import math 19 | 20 | if __name__=='__main__': 21 | t=int(input()) 22 | for _ in range(t): 23 | N = int(input()) 24 | ob=Solution() 25 | print(ob.lcmTriplets(N)) 26 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/L/LCM Triplet/20 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **LCM Triplet** 2 | 3 | ## **Medium** 4 | 5 | Given a number N.Find the maximum possible LCM that can be attained by any three numbers less than or equal to N. 6 | Note- LCM stands for Lowest Common Multiple. Also, Numbers can be repeated. 7 | 8 | **Example 1:** 9 | 10 | ``` 11 | Input: 12 | N=9 13 | Output: 14 | 504 15 | Explanation: 16 | 504 is the maximum LCM that can be attained 17 | by any triplet of numbers less than or equal 9. 18 | The triplet which has this LCM is {7,8,9}. 19 | ``` 20 | 21 | **Example 2:** 22 | 23 | ``` 24 | Input: 25 | N=7 26 | Output: 27 | 210 28 | Explanation: 29 | 210 is the maximum LCM that can be attained 30 | by any triplet of numbers less than or equal 7. 31 | The triplet which has this LCM is {5,6,7}. 32 | ``` 33 | 34 | **Your Task:** 35 | You don't need to read input or print anything. Your task is to complete the function **lcmTriplets()** that takes a number N as input parameter and returns the maximum LCM that can be attained by any three numbers less than or equal to N. 36 | 37 | **Expected Time Complexity:**O(logN) 38 | **Expected Auxillary Space:**O(logN) 39 | 40 | **Constraints:** 41 | 42 | - ` 1 <= N <= 10^6 ` 43 | 44 | > ### **Problem URL: [LCM Triplet](https://practice.geeksforgeeks.org/problems/lcm-triplet1501/1)** 45 | -------------------------------------------------------------------------------- /Problems & Solutions/L/Largest subtree sum in a tree/28 December 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Largest subtree sum in a tree** 2 | 3 | ## **Hard** 4 | 5 | Given a binary tree. The task is to find subtree with maximum sum in the tree and return its sum. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | 1 12 | / \ 13 | 2 3 14 | / \ / \ 15 | 4 5 6 7 16 | Output: 28 17 | Explanation: 18 | As all the tree elements are positive, 19 | the largest subtree sum is equal to 20 | sum of all tree elements. 21 | ``` 22 | 23 | ``` 24 | 25 | Example 2: 26 | Input: 27 | 1 28 | / \ 29 | -2 3 30 | / \ / \ 31 | 4 5 -6 2 32 | Output: 7 33 | Explanation: 34 | Subtree with largest sum is : 35 | -2 36 | / \ 37 | 4 5 38 | Also, entire tree sum is also 7. 39 | ``` 40 | 41 | **Your Task:** 42 | You don't need to read input or print anything. Your task is to complete the function **findLargestSubtreeSum()** which takes the root of a binary tree and returns an integer. 43 | 44 | **Expected Time Complexity:** O(N) 45 | **Expected Auxiliary Space:** O(N) 46 | 47 | **Constraints:** 48 | 1 <= N <= 10^5 49 | -10^3 <= tree.val <= 10^3 50 | 51 | > ### **Problem URL: [Largest subtree sum in a tree](https://practice.geeksforgeeks.org/problems/largest-subtree-sum-in-a-tree/1)** 52 | -------------------------------------------------------------------------------- /Problems & Solutions/L/Last modified ball/09 January 2023/C++/Last modified ball.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 solve(int N, vector A) { 12 | 13 | for (int i = N - 1; i >= 0; i--) 14 | if (A[i] != 9) 15 | return i + 1; 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 | vector A(N); 28 | 29 | for (int i = 0; i < N; i++) cin >> A[i]; 30 | 31 | Solution ob; 32 | cout << ob.solve(N, A) << "\n"; 33 | } 34 | return 0; 35 | } 36 | 37 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/L/Last modified ball/09 January 2023/Python/Last modified ball.py: -------------------------------------------------------------------------------- 1 | class Solution(): 2 | def solve(self, N, A): 3 | c = 1 4 | for i in range(n-1,-1,-1): 5 | if A[i] != 9: 6 | break 7 | return i+1 8 | 9 | #{ 10 | # Driver Code Starts 11 | #Initial Template for Python 3 12 | 13 | for _ in range(int(input())): 14 | n = int(input()) 15 | array=[int(i) for i in input().split()] 16 | obj = Solution() 17 | print(obj.solve(n, array)) 18 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/L/Longest Bitonic subsequence/24 November 2022/C++/Longest Bitonic subsequence.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution{ 7 | public: 8 | int LongestBitonicSequence(vectornums) 9 | { 10 | int n = nums.size(); 11 | vectorlis(n,1); 12 | for(int i = 1; i < n; i++) 13 | for(int j = 0; j < i; j++) 14 | if(nums[i] > nums[j] and lis[i] < lis[j] + 1) 15 | lis[i] = lis[j] + 1; 16 | 17 | vectorlds(n,1); 18 | for(int i = n-2; i >= 0; i--) 19 | for(int j = n-1; j > i; j--) 20 | if(nums[i] > nums[j] and lds[i] < lds[j] + 1) 21 | lds[i] = lds[j] + 1; 22 | int ans = lis[0] + lds[0] - 1; 23 | for(int i = 1; i < n; i++) 24 | ans = max(ans, lis[i] + lds[i] - 1); 25 | return ans; 26 | } 27 | }; 28 | 29 | //{ Driver Code Starts. 30 | int main(){ 31 | int tc; 32 | cin >> tc; 33 | while(tc--){ 34 | int n; 35 | cin >> n; 36 | vectornums(n); 37 | for(int i = 0; i < n; i++) 38 | cin >> nums[i]; 39 | Solution ob; 40 | int ans = ob.LongestBitonicSequence(nums); 41 | cout << ans <<"\n"; 42 | } 43 | return 0; 44 | } 45 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/L/Longest Bitonic subsequence/24 November 2022/Python/Longest Bitonic subsequence.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def LongestBitonicSequence(self, nums): 5 | n = len(nums) 6 | lis = [1]*n 7 | for i in range(n): 8 | for j in range(i): 9 | if(nums[i] > nums[j] and lis[i] < lis[j] + 1): 10 | lis[i] = lis[j] + 1 11 | 12 | lds = [1]*n 13 | for i in range(n-2,-1,-1): 14 | for j in range(n-1,i,-1): 15 | if(nums[i] > nums[j] and lds[i] < lds[j] + 1): 16 | lds[i] = lds[j] + 1 17 | ans = lis[0] + lds[0] - 1 18 | for i in range(1,n): 19 | ans = max(ans, lis[i] + lds[i] - 1) 20 | return ans 21 | 22 | #{ 23 | # Driver Code Starts 24 | #Initial Template for Python 3 25 | 26 | if __name__ == '__main__': 27 | T=int(input()) 28 | for i in range(T): 29 | n = int(input()) 30 | nums = list(map(int, input().split())) 31 | ob = Solution() 32 | ans = ob.LongestBitonicSequence(nums) 33 | print(ans) 34 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/L/Longest Bitonic subsequence/24 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Longest Bitonic subsequence** 2 | 3 | ## **Medium** 4 | 5 | Given an array of positive integers. Find the maximum length of Bitonic subsequence. 6 | A subsequence of array is called Bitonic if it is first strictly increasing, then strictly decreasing. 7 | 8 | **Example 1:** 9 | 10 | ``` 11 | Input: nums = [1, 2, 5, 3, 2] 12 | Output: 5 13 | Explanation: The sequence {1, 2, 5} is 14 | increasing and the sequence {3, 2} is 15 | decreasing so merging both we will get 16 | length 5. 17 | 18 | ``` 19 | 20 | **Example 2:** 21 | 22 | ``` 23 | Input: nums = [1, 11, 2, 10, 4, 5, 2, 1] 24 | Output: 6 25 | Explanation: The bitonic sequence 26 | {1, 2, 10, 4, 2, 1} has length 6. 27 | 28 | ``` 29 | 30 | **Your Task:** 31 | You don't need to read or print anything. Your task is to complete the function **LongestBitonicSequence()** which takes the array nums\[\] as input parameter and returns the maximum length of bitonic subsequence. 32 | 33 | **Expected Time Complexity:** O(n^2) 34 | **Expected Space Complexity:** O(n) 35 | 36 | **Constraints:** 37 | 1 ≤ length of array ≤ 1000 38 | 1 ≤ arr\[i\] ≤ 1000000 39 | 40 | > ### **Problem URL: [Longest Bitonic subsequence](https://practice.geeksforgeeks.org/problems/longest-bitonic-subsequence0824/1)** 41 | -------------------------------------------------------------------------------- /Problems & Solutions/L/Longest Perfect Piece/15 November 2022/C++/Longest Perfect Piece.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | int longestPerfectPiece(int arr[], int n) { 9 | map mp; 10 | int ans=1; 11 | int l=0; 12 | for(int r=0;rfirst<<" "<first<first-mp.begin()->first>1){ 16 | mp[arr[l]]--; 17 | if(mp[arr[l]]==0){ 18 | mp.erase(arr[l]); 19 | } 20 | l++; 21 | } 22 | 23 | ans=max(ans,r-l+1); 24 | } 25 | return ans; 26 | } 27 | }; 28 | 29 | //{ Driver Code Starts. 30 | int main() { 31 | int t; 32 | cin >> t; 33 | while (t--) { 34 | int N; 35 | 36 | cin>>N; 37 | int arr[N]; 38 | for(int i=0; i>arr[i]; 40 | 41 | Solution ob; 42 | cout << ob.longestPerfectPiece(arr,N) << endl; 43 | } 44 | return 0; 45 | } 46 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/L/Longest Perfect Piece/15 November 2022/Python/Longest Perfect Piece.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def longestPerfectPiece(self, arr, N): 5 | i,j,ans = 0, 0, 1 6 | for k in range(1, N): 7 | if abs(arr[k] - arr[j]) == 1: 8 | if i == j or arr[k] != arr[j-1]: 9 | i = j 10 | j = k 11 | elif arr[k] != arr[j]: 12 | i = j = k 13 | ans = max(ans, k-i+1) 14 | return ans 15 | 16 | #{ 17 | # Driver Code Starts 18 | #Initial Template for Python 3 19 | 20 | if __name__ == '__main__': 21 | t = int (input ()) 22 | for _ in range (t): 23 | N=int(input()) 24 | arr=list(map(int,input().split())) 25 | 26 | ob = Solution() 27 | print(ob.longestPerfectPiece(arr,N)) 28 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Magic Triplets/22 November 2022/C++/Magic Triplets.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution{ 7 | public: 8 | int countTriplets(vectornums){ 9 | int ans = 0; 10 | for(int i = 1 ; i < nums.size()-1 ; i++) 11 | { 12 | int left = 0, right = 0; 13 | for(int j = i-1 ; j >= 0 ; j--) 14 | { 15 | if(nums[i] > nums[j])left++; 16 | } 17 | 18 | for(int j = i + 1 ; j < nums.size() ; j++) 19 | { 20 | if(nums[i] < nums[j])right++; 21 | } 22 | ans+=(left*right); 23 | } 24 | return ans; 25 | } 26 | }; 27 | 28 | //{ Driver Code Starts. 29 | int main(){ 30 | int tc; 31 | cin >> tc; 32 | while(tc--){ 33 | int n; 34 | cin >> n; 35 | vectornums(n); 36 | for(int i = 0; i < n; i++)cin >> nums[i]; 37 | Solution ob; 38 | int ans = ob.countTriplets(nums); 39 | cout << ans << "\n"; 40 | } 41 | return 0; 42 | } 43 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Magic Triplets/22 November 2022/Python/Magic Triplets.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def countTriplets(self, nums): 5 | ans=0 6 | for i in range(len(nums)): 7 | left=0 8 | right=0 9 | for j in range(i-1,-1,-1): 10 | if nums[i]>nums[j]: 11 | left+=1 12 | for j in range(i+1,len(nums)): 13 | if nums[i] ### **Problem URL: [Magic Triplets](https://practice.geeksforgeeks.org/problems/magic-triplets4003/1)** 37 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Make array elements unique/11 January 2023/C++/Make array elements unique.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 | long long int minIncrements(vector arr, int N) { 12 | 13 | long long int ans = 0; 14 | sort(arr.begin(),arr.end()); 15 | for(int i=1; i> t; 31 | while (t--) { 32 | int N; 33 | cin >> N; 34 | vector arr(N); 35 | for (int i = 0; i < N; i++) { 36 | cin >> arr[i]; 37 | } 38 | Solution obj; 39 | cout << obj.minIncrements(arr, N) << endl; 40 | } 41 | } 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Make array elements unique/11 January 2023/Python/Make array elements unique.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minIncrements(self, arr, N): 3 | 4 | ans = 0 5 | arr.sort() 6 | for i in range(1,N): 7 | if(arr[i]<=arr[i-1]): 8 | ans+=(arr[i-1]-arr[i]+1) 9 | arr[i]=arr[i-1]+1 10 | return ans 11 | 12 | #{ 13 | # Driver Code Starts 14 | #Initial Template for Python 3 15 | 16 | if __name__ == '__main__': 17 | 18 | T = int(input()) 19 | while T > 0: 20 | N=int(input()) 21 | arr = [int(i) for i in input().split()] 22 | ob = Solution() 23 | print(ob.minIncrements(arr,N)) 24 | 25 | T -= 1 26 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Max Sum without Adjacents/04 February 2023/Max Sum without Adjacents.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 | // calculate the maximum sum with out adjacent 12 | int findMaxSum(int *arr, int n) { 13 | 14 | int dp[n][2]; 15 | if (n == 1){ 16 | return arr[0]; 17 | } 18 | 19 | dp[0][0] = 0; 20 | dp[0][1] = arr[0]; 21 | 22 | for (int i=1; i> t; 37 | while (t--) { 38 | int n; 39 | cin >> n; 40 | int arr[n]; 41 | for (int i = 0; i < n; i++) { 42 | cin >> arr[i]; 43 | } 44 | Solution ob; 45 | auto ans = ob.findMaxSum(arr, n); 46 | cout << ans << "\n"; 47 | } 48 | return 0; 49 | } 50 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Max Sum without Adjacents/04 February 2023/Max Sum without Adjacents.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | def findMaxSum(self,arr, n): 4 | dp = [0]*n 5 | if n == 1: 6 | return arr[0] 7 | dp[0] = arr[0] 8 | dp[1] = max(arr[0], arr[1]) 9 | for i in range(2, n): 10 | dp[i] = max(dp[i-1], dp[i-2]+arr[i]) 11 | return dp[n-1] 12 | 13 | #{ 14 | # Driver Code Starts 15 | #Initial Template for Python 3 16 | 17 | 18 | 19 | 20 | if __name__ == '__main__': 21 | tc = int(input()) 22 | while tc > 0: 23 | n = int(input()) 24 | arr = list(map(int, input().strip().split())) 25 | ob = Solution() 26 | ans = ob.findMaxSum(arr, n) 27 | print(ans) 28 | tc -= 1 29 | 30 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Max Sum without Adjacents/04 February 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Max Sum without Adjacents** 2 | 3 | ## **Easy** 4 | 5 | Given an array Arr of size N containing positive integers. Find the maximum sum of a subsequence such that no two numbers in the sequence should be adjacent in the array. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | N = 6 12 | Arr[] = {5, 5, 10, 100, 10, 5} 13 | Output: 110 14 | Explanation: If you take indices 0, 3 15 | and 5, then Arr[0]+Arr[3]+Arr[5] = 16 | 5+100+5 = 110. 17 | ``` 18 | 19 | **Example 2:** 20 | 21 | ``` 22 | Input: 23 | N = 4 24 | Arr[] = {3, 2, 7, 10} 25 | Output: 13 26 | Explanation: 3 and 10 forms a non 27 | continuous subsequence with maximum 28 | sum. 29 | ``` 30 | 31 | **Your Task:** 32 | You don't need to read input or print anything. Your task is to complete the function **findMaxSum()** which takes the array of integers arr and n as parameters and returns an integer denoting the answer. It is guaranteed that your answer will always fit in the 32-bit integer. 33 | 34 | **Expected Time Complexity:** O(N) 35 | **Expected Auxiliary Space:** O(1) 36 | 37 | **Constraints:** 38 | 1 ≤ N ≤ $10^6$ 39 | 1 ≤ Arri ≤ $10^7$ 40 | 41 | > ### **Problem URL: [Max Sum without Adjacents](https://practice.geeksforgeeks.org/problems/7a33c749a79327b2889d420dd80342fff33aac6d/1)** 42 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Maximum Number of coins/14 March 2023/Maximum Number of coins.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 | class Solution{ 9 | public: 10 | 11 | int dp[405][405]; 12 | int max_earn(vector&a,int l,int r){ 13 | if(l>r) 14 | return 0; 15 | int ans=INT_MIN; 16 | if(dp[l][r]!=-1)return dp[l][r]; 17 | for(int k=l;k<=r;k++){ 18 | ans=max(ans,max_earn(a,l,k-1)+max_earn(a,k+1,r)+a[k]*(l-1>=0?a[l-1]:1)*(r+1 &a) 24 | { 25 | memset(dp,-1,sizeof(dp)); 26 | return max_earn(a,0,a.size()-1); 27 | } 28 | }; 29 | 30 | //{ Driver Code Starts. 31 | int main(){ 32 | int t; 33 | cin>>t; 34 | while(t--) 35 | { 36 | int n; 37 | cin>>n; 38 | vector a(n); 39 | for(int i=0;i>a[i]; 41 | Solution S; 42 | cout< 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | //User function Template for C++ 8 | class Solution { 9 | public: 10 | long long maxPossibleValue(int N,vector A, vector B) { 11 | long long min=INT_MAX; 12 | long long ans=0; 13 | long long count=0; 14 | for(int i=0;i1) 17 | min=A[i]; 18 | int d = B[i]/2; 19 | ans+=d*2*A[i]; 20 | count+=d*2; 21 | } 22 | if(count%4 !=0) 23 | ans-=min*2; 24 | return ans; 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 | vector A(N), B(N); 36 | for(int i=0;i>A[i]; 38 | } 39 | for(int i=0;i>B[i]; 41 | } 42 | Solution obj; 43 | auto ans = obj.maxPossibleValue(N,A,B); 44 | cout<k: 19 | has[s[left]]-=1 20 | left+=1 21 | else: 22 | res=max(res,right-left+1) 23 | right+=1 24 | return res 25 | 26 | #{ 27 | # Driver Code Starts 28 | #Initial Template for Python 3 29 | 30 | if __name__ == '__main__': 31 | T=int(input()) 32 | for i in range(T): 33 | s = input() 34 | k = int(input()) 35 | ob = Solution() 36 | ans = ob.characterReplacement(s, k) 37 | print(ans) 38 | 39 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Maximum Sum LCM/23 November 2022/C++/Maximum Sum LCM.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 | class Solution { 9 | public: 10 | long long int maxSumLCM(int n) 11 | { 12 | long long int max_sum = 0; 13 | for (int i=1; i*i<=n; i++) 14 | { 15 | if (n%i == 0) 16 | { 17 | max_sum += i; 18 | if (n/i != i) 19 | max_sum += (n/i); 20 | } 21 | } 22 | 23 | return max_sum; 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 | cout< 0) { 11 | int n = Integer.parseInt(read.readLine()); 12 | Solution ob = new Solution(); 13 | 14 | System.out.println(ob.maxSumLCM(n)); 15 | } 16 | } 17 | } 18 | // } Driver Code Ends 19 | 20 | 21 | //User function Template for Java 22 | class Solution { 23 | static long maxSumLCM(int n) { 24 | long sum=0; 25 | for(int i=1;i*i<=n;i++){ 26 | if(n%i==0){ 27 | sum+=i; 28 | } 29 | if(i!=n/i) 30 | if(n%(n/i)==0){ 31 | sum+=(n/i); 32 | } 33 | } 34 | return sum; 35 | } 36 | } -------------------------------------------------------------------------------- /Problems & Solutions/M/Maximum Sum LCM/23 November 2022/Python/Maximum Sum LCM.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | class Solution: 3 | def maxSumLCM (self, n): 4 | max_sum=0 5 | i=1 6 | while i**2<=n: 7 | if n%i==0: 8 | max_sum+=i 9 | if n//i!=i: 10 | max_sum+=n//i 11 | i+=1 12 | return max_sum 13 | 14 | #{ 15 | # Driver Code Starts 16 | #Initial Template for Python 3 17 | if __name__ == '__main__': 18 | t = int (input ()) 19 | for _ in range (t): 20 | n = int(input()) 21 | 22 | ob = Solution() 23 | print(ob.maxSumLCM(n)) 24 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Maximum Sum LCM/23 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Maximum Sum LCM** 2 | 3 | ## **Easy** 4 | 5 | Given a positive number n. You need to write a program to find the maximum sum of distinct numbers such that the LCM of all these numbers is equal to n. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: n = 2 11 | Output: 3  12 | Explanation: The distinct numbers you can have are 13 | just 1 and 2 and their sum is equal to 3. 14 | ``` 15 | 16 | **Example 2:** 17 | 18 | ``` 19 | Input: n = 5 20 | Output: 6 21 | Explanation: The distinct numbers you can have 22 | are just 1 and 5 and their sum is equal to 6. 23 | 24 | ``` 25 | 26 | **Your Task:** 27 | You dont need to read input or print anything. Complete the function **maxSumLCM()** which takes n as input parameter and returns the maximum sum of distinct numbers such that the LCM of all these numbers is equal to n. 28 | 29 | **Expected Time Complexity:** O(sqrt(n)) 30 | **Expected Auxiliary Space:** O(1) 31 | 32 | **Constraints:** 33 | 1<= N <=1000000000 34 | 35 | > ### **Problem URL: [Maximum Sum LCM](https://practice.geeksforgeeks.org/problems/maximum-sum-lcm3025/1)** 36 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Maximum Triplet product/10 March 2023/Maximum Triplet product.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for CPP 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 maxTripletProduct(long long arr[], int n) 14 | { 15 | // if size is less than 3, triplets doesn't exist 16 | if (n < 3) return -1; 17 | // sort an array in ascending order 18 | sort(arr, arr+n); 19 | // return the max of first three elements and first two & last one elements 20 | return max(arr[0]*arr[1]*arr[n-1], arr[n-1]*arr[n-2]*arr[n-3]); 21 | } 22 | }; 23 | 24 | //{ Driver Code Starts. 25 | 26 | int main() 27 | { 28 | int t; 29 | cin>>t; 30 | while(t--) 31 | { 32 | int n,i; 33 | cin>>n; long long arr[n]; 34 | for(i=0;i>arr[i]; 36 | Solution ob; 37 | cout < ### **Problem URL: [Maximum Triplet product](https://practice.geeksforgeeks.org/problems/d54c71dc974b7db3a200eb63f34e3d1cba955d86/1)** 45 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Maximum Weight Node/20 January 2023/Maximum Weight Node.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for C++ 3 | #include 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | 8 | //User function Template for C++ 9 | class Solution 10 | { 11 | public: 12 | int maxWeightCell(int N, vector Edge) 13 | { 14 | vectorans(N,0); 15 | int max=0; 16 | for(int i=0;i=0){ 18 | ans[Edge[i]]+=i; 19 | if(ans[Edge[i]]>ans[max] || Edge[i]>max && ans[Edge[i]]==ans[max])max=Edge[i]; 20 | } 21 | } 22 | return max; 23 | } 24 | }; 25 | 26 | //{ Driver Code Starts. 27 | int main(){ 28 | int tc; 29 | cin >> tc; 30 | while(tc--){ 31 | int N; 32 | cin >> N; 33 | vector Edge(N); 34 | for(int i=0;i>Edge[i]; 36 | } 37 | Solution obj; 38 | int ans=obj.maxWeightCell(N, Edge); 39 | cout<max: 12 | max=matrix[i][c-1] 13 | desired=(r*c+1)//2 14 | while min ### Problem URL: **[Median in a row-wise sorted Matrix](https://practice.geeksforgeeks.org/problems/median-in-a-row-wise-sorted-matrix1527/1)** 43 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Median of the Subarrays/18 March 2023/Median of the Subarrays.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 | class Solution { 9 | public: 10 | 11 | long long solve(int n, vector& A, int m) { 12 | vector mp(2*n+1, 0); 13 | long long cur = n, tot = 0, ans = 0; 14 | mp[cur]++; 15 | for (int i = 0; i < n; i++) { 16 | int x = -1; 17 | if (A[i] >= m) { 18 | x = 1; 19 | } 20 | if (x == -1) { 21 | tot -= mp[cur+x]; 22 | } else { 23 | tot += mp[cur]; 24 | } 25 | cur += x; 26 | ans += tot; 27 | mp[cur]++; 28 | } 29 | return ans; 30 | } 31 | long long countSubarray(int N,vector A,int M) { 32 | // code here 33 | return solve(N, A, M) - solve(N, A, M+1); 34 | } 35 | }; 36 | 37 | //{ Driver Code Starts. 38 | int main() { 39 | int t; 40 | cin >> t; 41 | while (t--) { 42 | int N, M; 43 | cin>>N>>M; 44 | vector A(N); 45 | for(int i=0;i>A[i]; 47 | } 48 | Solution obj; 49 | auto ans = obj.countSubarray(N,A,M); 50 | cout< 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | class Solution { 8 | public: 9 | int solve(int a, int b) { 10 | 11 | if(a == b) return 0; 12 | 13 | if( (a&b) == a || (a&b) == b ) return 1; 14 | 15 | return 2; 16 | } 17 | }; 18 | 19 | 20 | //{ Driver Code Starts. 21 | 22 | int main(){ 23 | int t; 24 | scanf("%d ",&t); 25 | while(t--){ 26 | 27 | int a; 28 | scanf("%d",&a); 29 | 30 | 31 | int b; 32 | scanf("%d",&b); 33 | 34 | Solution obj; 35 | int res = obj.solve(a, b); 36 | 37 | printf("%d\n", res); 38 | 39 | } 40 | } 41 | 42 | // } Driver Code Ends 43 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Min operations/29 January 2023/Min operations.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | import java.io.*; 3 | import java.util.*; 4 | 5 | class GFG { 6 | public static void main(String[] args) throws IOException { 7 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 8 | PrintWriter out=new PrintWriter(System.out); 9 | int t; 10 | t = Integer.parseInt(br.readLine()); 11 | while(t-- > 0){ 12 | 13 | int a; 14 | a = Integer.parseInt(br.readLine()); 15 | 16 | 17 | int b; 18 | b = Integer.parseInt(br.readLine()); 19 | 20 | Solution obj = new Solution(); 21 | int res = obj.solve(a, b); 22 | 23 | out.println(res); 24 | 25 | } 26 | out.close(); 27 | } 28 | } 29 | 30 | // } Driver Code Ends 31 | 32 | class Solution { 33 | public static int solve(int a, int b) { 34 | if(a==b){ 35 | 36 | return 0; 37 | } 38 | int k=a&b; 39 | 40 | if(k!=a && k!=b) 41 | 42 | return 2; 43 | 44 | return 1; 45 | } 46 | } 47 | 48 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Min operations/29 January 2023/Min operations.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def solve(self, a : int, b : int) -> int: 3 | ans = a & b 4 | count =0 5 | 6 | while( a!= b): 7 | x= a 8 | a = a & ans 9 | if( a==b): 10 | count+=1 11 | return count 12 | if( x!=a): 13 | count+=1 14 | 15 | 16 | y=b 17 | b = b & ans 18 | if( a==b ): 19 | count+=1 20 | return count 21 | 22 | if( y !=b): 23 | count+=1 24 | 25 | return count 26 | 27 | #{ 28 | # Driver Code Starts 29 | if __name__=="__main__": 30 | t = int(input()) 31 | for _ in range(t): 32 | 33 | a = int(input()) 34 | 35 | 36 | b = int(input()) 37 | 38 | obj = Solution() 39 | res = obj.solve(a, b) 40 | 41 | print(res) 42 | 43 | 44 | # } Driver Code Ends 45 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Min operations/29 January 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Min operations** 2 | 3 | ## **Easy** 4 | 5 | Given two numbers a and b. In one operation you can pick any non negative integer x and either of a or b. Now if you picked a then replace a with a&x else if you picked b then replace b with b&x. 6 | 7 | Return the minimum number of operation required to make a and b equal. 8 | 9 | Note: Here & represents bitwise AND operation. 10 | 11 | **Example 1:** 12 | 13 | ``` 14 | Input: 15 | a = 5, b = 12 16 | Output: 17 | 2 18 | Explanantion: 19 | In first operation replace 20 | a = a&4 = 4 21 | after that replace 22 | b = b&6 = 4 23 | Hence both are same after applying two 24 | operations. 25 | ``` 26 | 27 | 28 | **Example 2:** 29 | 30 | ``` 31 | Input: 32 | a = 100, b = 100 33 | Output: 34 | 0 35 | Explanation: 36 | Already same. 37 | ``` 38 | 39 | 40 | **Your Task:** 41 | You don't need to read, input, or print anything. Your task is to complete the function solve( ), which takes two integers a and b as input parameters and returns the answer. 42 | 43 | **Expected Time Complexity:** O(1) 44 | **Expected Auxiliary Space:** O(1) 45 | 46 | 47 | **Constraints:** 48 | 0 ≤ a, b ≤ 10^9 49 | 50 | > ### **Problem URL: [Min operations](https://practice.geeksforgeeks.org/problems/5a7e1a52f1b7796238f9efea4c6fda389f26c327/1)** 51 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimize number of Students to be removed/03 January 2023/Python/Minimize number of Students to be removed.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLIS(self, nums): 3 | tails = [0] * len(nums) 4 | size = 0 5 | for x in nums: 6 | i, j = 0, size 7 | while i != j: 8 | m = (i + j) // 2 9 | if tails[m] < x: 10 | i = m + 1 11 | else: 12 | j = m 13 | tails[i] = x 14 | size = max(i + 1, size) 15 | return size 16 | 17 | def removeStudents(self, H, N): 18 | 19 | ans = N - self.lengthOfLIS(H) 20 | return ans 21 | 22 | #{ 23 | # Driver Code Starts 24 | #Initial Template for Python 3 25 | 26 | if __name__ == '__main__': 27 | t = int (input ()) 28 | for _ in range (t): 29 | N=int(input()) 30 | H=list(map(int,input().split())) 31 | 32 | ob = Solution() 33 | print(ob.removeStudents(H,N)) 34 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimize the sum/12 January 2023/.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 minimizeSum(int N, vector arr) { 13 | priority_queue,greater>pq; 14 | for(int i=0;i1){ 19 | int sum = pq.top(); 20 | pq.pop(); 21 | sum += pq.top(); 22 | pq.pop(); 23 | pq.push(sum); 24 | ans += sum; 25 | } 26 | return ans; 27 | } 28 | }; 29 | //{ Driver Code Starts. 30 | 31 | int main() { 32 | int t; 33 | cin >> t; 34 | while (t--) { 35 | int N; 36 | cin >> N; 37 | vector arr(N); 38 | for (int i = 0; i < N; i++) { 39 | cin >> arr[i]; 40 | } 41 | Solution obj; 42 | cout << obj.minimizeSum(N,arr) << endl; 43 | } 44 | } 45 | // } Driver Code Ends 46 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimize the sum/12 January 2023/Minimize the sum.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | 3 | class Solution: 4 | def minimizeSum(self, N, arr): 5 | heapq.heapify(arr) 6 | ans = 0 7 | while len(arr) != 1: 8 | x = heapq.heappop(arr) 9 | y = heapq.heappop(arr) 10 | z = x+y 11 | heapq.heappush(arr, z) 12 | ans += z 13 | return ans 14 | 15 | #{ 16 | # Driver Code Starts 17 | #Initial Template for Python 3 18 | 19 | if __name__ == '__main__': 20 | t = int(input()) 21 | 22 | for _ in range(t): 23 | n = int(input()) 24 | A = [int(x) for x in input().split()] 25 | ob=Solution() 26 | print(ob.minimizeSum(n, A)) 27 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimize the sum/12 January 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Minimize the sum** 2 | 3 | ## **Easy** 4 | 5 | You are given N elements, you can remove any two elements from the list, note their sum, and add the sum to the list. Repeat these steps while there is more than a single element in the list. The task is to minimize the sum of these chosen sums. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | N = 4 12 | arr[] = {1, 4, 7, 10} 13 | 14 | Output: 15 | 39 16 | 17 | Explanation: 18 | Choose 1 and 4, Sum = 1 + 4 = 5. 19 | arr[] = {5, 7, 10} 20 | Choose 5 and 7, Sum = 5 + (5 + 7) = 17. 21 | arr[] = {12, 10} 22 | Choose 12 and 10, Sum = 17 + (12 + 10) = 39. 23 | arr[] = {22} 24 | ``` 25 | 26 | **Example 2:** 27 | 28 | ``` 29 | Input: 30 | N = 5 31 | arr[] = {1, 3, 7, 5, 6} 32 | 33 | Output: 34 | 48 35 | ``` 36 | 37 | **Your Task:** 38 | You don't need to read input or print anything. The task is to complete the function minimizeSum() which takes N as size of arr array and a arr array. Your task is to minimize the sum of these chosen sums and return it. 39 | 40 | **Expected Time Complexity:** O(N*log(N)) 41 | **Expected Auxiliary Space:** O(N) 42 | 43 | **Constraints:** 44 | 1 <= N, arr[i] <= 10^5 45 | 46 | > ### **Problem URL: [Minimize the sum](https://practice.geeksforgeeks.org/problems/86e609332c9ef4f6b8aa79db11a6c0808c4a1bca/1)** 47 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum X (xor) A/21 January 2023/Minimum X (xor) A.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 | 10 | class Solution { 11 | public: 12 | int minVal(int a, int b) { 13 | 14 | int bitcnt = __builtin_popcount(b); 15 | int ans=0; 16 | 17 | for(int i=30; i>=0; i--){ 18 | if( a & (1< 0 ){ 19 | ans += (1< 0){ 25 | 26 | for(int i=0; i<=30; i++){ 27 | if( !(a & (1< 0 ){ 28 | ans += (1<> t; 42 | while (t--) { 43 | int a, b; 44 | cin >> a >> b; 45 | 46 | Solution ob; 47 | cout << ob.minVal(a, b); 48 | 49 | cout << "\n"; 50 | } 51 | 52 | return 0; 53 | } 54 | // } Driver Code Ends 55 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum X (xor) A/21 January 2023/Minimum X (xor) A.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minVal(self, a, b): 3 | n = bin(b)[2:].count("1") 4 | arr = list(bin(a)[2:]) 5 | ans = "" 6 | for i in arr: 7 | if i == "1" and n: 8 | n -= 1 9 | ans += "1" 10 | else: 11 | ans += "0" 12 | i = 1 13 | ans = list(ans) 14 | while n: 15 | if arr[-i] != "1": 16 | ans[-i] = "1" 17 | n -= 1 18 | i += 1 19 | ans = "".join(ans) 20 | return int(ans, 2) 21 | 22 | #{ 23 | # Driver Code Starts 24 | #Initial Template for Python 3 25 | 26 | import atexit 27 | import io 28 | import sys 29 | 30 | _INPUT_LINES = sys.stdin.read().splitlines() 31 | input = iter(_INPUT_LINES).__next__ 32 | _OUTPUT_BUFFER = io.StringIO() 33 | sys.stdout = _OUTPUT_BUFFER 34 | 35 | @atexit.register 36 | 37 | def write(): 38 | sys.__stdout__.write(_OUTPUT_BUFFER.getvalue()) 39 | 40 | if __name__=='__main__': 41 | t = int(input()) 42 | for i in range(t): 43 | a = int(input()) 44 | b = int(input()) 45 | 46 | ob= Solution() 47 | print(ob.minVal(a,b)) 48 | # } Driver Code Ends 49 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum number of Coins/06 November 2022/C++/Minimum number of coins.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 | vector minPartition(int N) 13 | { 14 | // code here 15 | int arr[] = {2000,500,200,100,50,20,10,5,2,1}; 16 | int i=0; 17 | int count =0; 18 | vector ans; 19 | while(i<10 && N>=0){ 20 | if(arr[i]>N){ 21 | i++; 22 | }else{ 23 | ans.push_back(arr[i]); 24 | N = N - arr[i]; 25 | } 26 | } 27 | return ans; 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 | 40 | Solution ob; 41 | vector numbers = ob.minPartition(N); 42 | for(auto u: numbers) 43 | cout< 0){ 13 | int N = Integer.parseInt(in.readLine()); 14 | 15 | Solution ob = new Solution(); 16 | List numbers= new ArrayList(); 17 | numbers = ob.minPartition(N); 18 | for(int i: numbers){ 19 | System.out.print(i + " "); 20 | } 21 | System.out.println(); 22 | } 23 | } 24 | } 25 | // } Driver Code Ends 26 | 27 | 28 | // User function Template for Java 29 | 30 | class Solution{ 31 | static List minPartition(int N) 32 | { 33 | int[] arr = { 1, 2, 5, 10, 20, 50, 100, 200, 500, 2000}; 34 | List ans = new ArrayList(); 35 | for(int i=arr.length-1; i>=0; --i){ 36 | int num = arr[i]; 37 | while(N>=num){ 38 | ans.add(num); 39 | N -= num; 40 | } 41 | } 42 | return ans; 43 | } 44 | } -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum number of Coins/06 November 2022/Python/Minimum number of coins.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def minPartition(self, N): 5 | l = [1,2,5,10,20,50,100,200,500,2000] 6 | res = 0 7 | ans = [] 8 | for i in l[::-1]: 9 | if N>=i: 10 | res += N//i 11 | ans.extend([i]*(N//i)) 12 | N = N%i 13 | return ans 14 | 15 | #{ 16 | # Driver Code Starts 17 | #Initial Template for Python 3 18 | 19 | if __name__ == '__main__': 20 | t = int(input()) 21 | for _ in range(t): 22 | N = int(input()) 23 | 24 | ob = Solution() 25 | arr = ob.minPartition(N) 26 | for i in range(len(arr)): 27 | print(arr[i],end=" ") 28 | print() 29 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum number of Coins/06 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Minimum number of Coins** 2 | 3 | **Medium** 4 | 5 | Given an **infinite supply** of each denomination of Indian currency { **1, 2, 5, 10, 20, 50, 100, 200, 500, 2000** } and a target value **N**. 6 | Find the **minimum** number of coins and/or notes needed to make the change for Rs **N**. You must return the list containing the value of coins required. 7 | 8 | **Example 1:** 9 | 10 | ``` 11 | Input: N = 43 12 | Output: 20 20 2 1 13 | Explaination: 14 | Minimum number of coins and notes needed 15 | to make 43. 16 | ``` 17 | 18 | **Example 2:** 19 | 20 | ``` 21 | Input: N = 1000 22 | Output: 500 500 23 | Explaination: minimum possible notes 24 | is 2 notes of 500. 25 | ``` 26 | 27 | **Your Task:** 28 | You do not need to read input or print anything. Your task is to complete the function **minPartition()** which takes the value N as input parameter and returns a list of integers in decreasing order. 29 | 30 | **Expected Time Complexity:** O(N) 31 | **Expected Auxiliary Space:** O(N) 32 | 33 | **Constraints:** 34 | 1 ≤ N ≤ 10^6 35 | 36 | > ### Problem URL: **[Minimum number of Coins](https://practice.geeksforgeeks.org/problems/-minimum-number-of-coins4426/1)** 37 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum sum partition/09 November 2022/C++/Minimum sum partition.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution{ 7 | 8 | public: 9 | int minDifference(int arr[], int n) 10 | { 11 | int s=0; 12 | for(int i=0;i=arr[i];j--) dp[j]|=dp[j-arr[i]]; 17 | for(int i=s/2;i>=0;i--) if(dp[i])return s-2*i; 18 | return 0; 19 | } 20 | }; 21 | 22 | 23 | //{ Driver Code Starts. 24 | int main() 25 | { 26 | 27 | 28 | int t; 29 | cin >> t; 30 | while (t--) 31 | { 32 | int n; 33 | cin >> n; 34 | 35 | int a[n]; 36 | for(int i = 0; i < n; i++) 37 | cin >> a[i]; 38 | 39 | 40 | 41 | Solution ob; 42 | cout << ob.minDifference(a, n) << "\n"; 43 | 44 | } 45 | return 0; 46 | } 47 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum sum partition/09 November 2022/Python/Minimum sum partition.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | class Solution: 3 | def minDifference(self, arr, n): 4 | s=sum(arr) 5 | dp=[0]*(s//2 +1) 6 | dp[0]=1 7 | for i in range(n): 8 | for j in range(s//2,arr[i]-1,-1): 9 | dp[j]=dp[j] or dp[j-arr[i]] 10 | for i in range(s//2,-1,-1): 11 | if dp[i]:return s-2*i; 12 | return 0 13 | 14 | #{ 15 | # Driver Code Starts 16 | #Initial Template for Python 3 17 | 18 | if __name__ == '__main__': 19 | T=int(input()) 20 | for i in range(T): 21 | N = int(input()) 22 | arr = [int(x) for x in input().split()] 23 | ob = Solution() 24 | ans = ob.minDifference(arr, N) 25 | print(ans) 26 | 27 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum sum partition/09 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Minimum sum partition** 2 | 3 | ## **Hard** 4 | 5 | Given an array **arr** of size **n** containing **non-negative** integers, the task is to divide it into two sets **S1** and **S2** such that the absolute difference between their sums is minimum and find the **minimum** difference 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: N = 4, arr[] = {1, 6, 11, 5} 11 | Output: 1 12 | Explanation: 13 | Subset1 = {1, 5, 6}, sum of Subset1 = 12 14 | Subset2 = {11}, sum of Subset2 = 11 15 | ``` 16 | 17 | **Example 2:** 18 | 19 | ``` 20 | Input: N = 2, arr[] = {1, 4} 21 | Output: 3 22 | Explanation: 23 | Subset1 = {1}, sum of Subset1 = 1 24 | Subset2 = {4}, sum of Subset2 = 4 25 | ``` 26 | 27 | **Your Task:** 28 | You don't need to read input or print anything. Complete the function **`minDifference`()** which takes **N** and array **arr** as input parameters and returns the integer value 29 | 30 | **Expected Time Complexity:** O(N\*|sum of array elements|) 31 | **Expected Auxiliary Space:** O(N\*|sum of array elements|) 32 | 33 | **Constraints:** 34 | 1 ≤ N\*|sum of array elements| ≤ 10^6 35 | 0 < arr\[i\] <= 10^5 36 | 37 | > ### **Problem URL: [Minimum sum partition](https://practice.geeksforgeeks.org/problems/minimum-sum-partition3317/1)** 38 | -------------------------------------------------------------------------------- /Problems & Solutions/M/Minimum times A has to be repeated such that B is a substring of it/31 January 2023/Minimum times A has to be repeated such that B is a substring of it.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | //User function Template for C++ 8 | 9 | class Solution { 10 | public: 11 | int minRepeats(string A, string B) { 12 | string rep=A; 13 | int cnt=1; 14 | while(A.size() 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | 8 | class Solution { 9 | public: 10 | vector nextLargerElement(vector &arr, int n){ 11 | vectorans; 12 | stackst; 13 | for(int i=n-1;i>=0;i--){ 14 | while(!st.empty() && st.top()<=arr[i])st.pop(); 15 | if(st.empty()){ 16 | ans.push_back(-1); 17 | st.push(arr[i]); 18 | } 19 | else{ 20 | ans.push_back(st.top()); 21 | st.push(arr[i]); 22 | } 23 | } 24 | reverse(ans.begin(),ans.end()); 25 | return ans; 26 | } 27 | }; 28 | 29 | //{ Driver Code Starts. 30 | 31 | int main() 32 | { 33 | int t; 34 | cin>>t; 35 | while(t--) 36 | { 37 | 38 | int n; 39 | cin>>n; 40 | vector arr(n); 41 | for(int i=0;i>arr[i]; 43 | 44 | Solution ob; 45 | 46 | vector res = ob.nextLargerElement(arr, n); 47 | for (long long i : res) cout << i << " "; 48 | cout< 3 | using namespace std; 4 | 5 | // } Driver Code Ends 6 | class Solution { 7 | public: 8 | int noOfOpenDoors(long long N) { 9 | return sqrt(N); 10 | } 11 | }; 12 | 13 | //{ Driver Code Starts. 14 | int main() { 15 | int t; 16 | cin >> t; 17 | while (t--) { 18 | long long N; 19 | 20 | cin>>N; 21 | 22 | Solution ob; 23 | cout << ob.noOfOpenDoors(N) << endl; 24 | } 25 | return 0; 26 | } 27 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/N/Number Of Open Doors/18 November 2022/Java/Number Of Open Doors.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for Java 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | 7 | class GFG { 8 | public static void main(String args[]) throws IOException { 9 | BufferedReader read = 10 | new BufferedReader(new InputStreamReader(System.in)); 11 | int t = Integer.parseInt(read.readLine()); 12 | while (t-- > 0) { 13 | Long N = Long.parseLong(read.readLine()); 14 | 15 | Solution ob = new Solution(); 16 | System.out.println(ob.noOfOpenDoors(N)); 17 | } 18 | } 19 | } 20 | // } Driver Code Ends 21 | 22 | 23 | //User function Template for Java 24 | 25 | class Solution { 26 | static int noOfOpenDoors(Long N) { 27 | return (int)Math.sqrt(N); 28 | } 29 | }; -------------------------------------------------------------------------------- /Problems & Solutions/N/Number Of Open Doors/18 November 2022/JavaScript/Number Of Open Doors.js: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | //Initial Template for javascript 3 | 4 | 5 | 'use strict'; 6 | 7 | process.stdin.resume(); 8 | process.stdin.setEncoding('utf-8'); 9 | 10 | let inputString = ''; 11 | let currentLine = 0; 12 | 13 | process.stdin.on('data', inputStdin => { 14 | inputString += inputStdin; 15 | }); 16 | 17 | process.stdin.on('end', _ => { 18 | inputString = inputString.trim().split('\n').map(string => { 19 | return string.trim(); 20 | }); 21 | 22 | main(); 23 | }); 24 | 25 | function readLine() { 26 | return inputString[currentLine++]; 27 | } 28 | 29 | function main() { 30 | let t = parseInt(readLine()); 31 | let i = 0; 32 | for(;i 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | // User function Template for C++ 10 | 11 | long long int no_of_subarrays(int n, vector &arr) { 12 | long ans = 0; 13 | long cnt = 0; 14 | 15 | for(int i = 0; i < n; i++) { 16 | if(arr[i] == 0) { 17 | cnt++; 18 | } else { 19 | ans += ((long)(cnt*(cnt+1))/2); 20 | cnt = 0; 21 | } 22 | } 23 | if(cnt != 0) { 24 | ans += ((long)(cnt*(cnt+1))/2); 25 | } 26 | 27 | return ans; 28 | } 29 | 30 | //{ Driver Code Starts. 31 | 32 | int main() { 33 | 34 | int t; 35 | cin >> t; 36 | while (t--) { 37 | int n; 38 | cin >> n; 39 | vector arr(n); 40 | for (int i = 0; i < n; i++) { 41 | cin >> arr[i]; 42 | } 43 | cout << no_of_subarrays(n, arr) << endl; 44 | } 45 | } 46 | 47 | // } Driver Code Ends 48 | -------------------------------------------------------------------------------- /Problems & Solutions/N/Number of Subarrays of 0's/17 March 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Number of Subarrays of 0's** 2 | 3 | ## **Easy** 4 | 5 | You are given an array arr of length N of 0's and 1's. Find the total number of subarrays of 0's 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | N = 4 12 | arr[] = {0, 0, 1, 0} 13 | Output: 14 | 4 15 | Explanation: 16 | Following are the subarrays of 17 | length 1: {0}, {0}, {0} - 3 18 | length 2: {0, 0} - 1 19 | Total Subarrays: 3 + 1 = 4 20 | ``` 21 | 22 | **Example 2:** 23 | 24 | ``` 25 | Input: 26 | N = 4 27 | arr[] = {0, 0, 0, 0} 28 | Output: 29 | 10 30 | Explanation: 31 | Following are the subarrays of 32 | length 1: {0}, {0}, {0}, {0} - 4 33 | length 2: {0, 0}, {0, 0}, {0, 0} - 3 34 | length 3: {0, 0, 0}, {0, 0, 0} - 2 35 | length 4: {0, 0, 0, 0} - 1 36 | Total Subarrays: 4 + 3 + 2 + 1 = 10 37 | ``` 38 | 39 | **Your Task:** 40 | 41 | Your task is to complete the function no_of_subarrays(), which takes an integer N and an integer array arr as the input parameters and returns an integer denoting the total number of subarrays of 0's. 42 | 43 | **Expected Time Complexity:** O(N) 44 | **Expected Space Complexity:** O(1) 45 | 46 | **Constraints:** 47 | 48 | 1 <= N <= $10^6$ 49 | 0 <= arr[i] <= 1 50 | 51 | > ### **Problem URL: [Number of Subarrays of 0's](https://practice.geeksforgeeks.org/problems/0960a833f70b09c59444ea487f99729929fc8910/1)** 52 | -------------------------------------------------------------------------------- /Problems & Solutions/O/Optimal Array/28 February 2023/Optimal Array.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 | vector optimalArray(int n,vector &a){ 14 | 15 | vector ans; 16 | ans.push_back(0); 17 | 18 | for (int i=1; i>t; 33 | while(t--){ 34 | int n; 35 | cin>>n; 36 | vector a(n); 37 | for(int i=0;i>a[i]; 39 | } 40 | Solution obj; 41 | vector ans=obj.optimalArray(n,a); 42 | for(auto ele:ans){ 43 | cout< 4 | using namespace std; 5 | 6 | // } Driver Code Ends 7 | // User function Template for C++ 8 | class Solution { 9 | public: 10 | bool isprime(int n){ 11 | for(int i=2;i*i<=n;i++){ 12 | if(n%i==0) 13 | return false; 14 | } 15 | return true; 16 | } 17 | string isSumOfTwo(int n){ 18 | for(int i=2;i> t; 31 | while (t--) { 32 | int N; 33 | cin >> N; 34 | Solution ob; 35 | cout << ob.isSumOfTwo(N) << endl; 36 | } 37 | return 0; 38 | } 39 | 40 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/P/Primes Sum/11 November 2022/Python/Primes sum.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | class Solution: 3 | def isSumOfTwo (self, N): 4 | # code here 5 | if N<=2: 6 | return "No" 7 | elif N%2==0: 8 | return "Yes" 9 | else: 10 | k=0 11 | for i in range(2,int((N-2)**0.5)+1): 12 | if(N-2)%i==0: 13 | k+=1 14 | return "Yes" if k==0 else "No" 15 | 16 | #{ 17 | # Driver Code Starts 18 | #Initial Template for Python 3 19 | if __name__ == '__main__': 20 | t = int (input ()) 21 | for _ in range (t): 22 | N = int(input()) 23 | 24 | ob = Solution() 25 | print(ob.isSumOfTwo(N)) 26 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/P/Primes Sum/11 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Primes Sum** 2 | 3 | ## **Easy** 4 | 5 | Given a number **N.** Find if it can be expressed as sum of two prime numbers. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: N = 34 11 | Output: "Yes"  12 | Explanation: 34 can be expressed as 13 | sum of two prime numbers. 14 | 15 | ``` 16 | 17 | **Example 2:** 18 | 19 | ``` 20 | Input: N = 23 21 | Output: "No" 22 | Explanation: 23 cannnot be expressed as 23 | sum of two prime numbers. 24 | 25 | ``` 26 | 27 | **Your Task:** 28 | You dont need to read input or print anything. Complete the function **isSumOfTwo()** which takes N as input parameter and returns "Yes" if can be expressed as sum of two prime numbers. else return "No". 29 | 30 | **Expected Time Complexity:** O(N\*sqrt(N)) 31 | **Expected Auxiliary Space:** O(1) 32 | 33 | **Constraints:** 34 | 1 <= N <= 10^5 35 | 36 | > ### **Problem URL: [Primes Sum](https://practice.geeksforgeeks.org/problems/primes-sum5827/1)** 37 | -------------------------------------------------------------------------------- /Problems & Solutions/R/Rearrange Array Alternately/01 December 2022/Python/Rearrange Array Alternately.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | class Solution: 3 | def rearrange(self,arr, n): 4 | 5 | max_idx = n - 1 6 | min_idx = 0 7 | 8 | max_elem = arr[n-1] + 1 9 | 10 | for i in range(0, n) : 11 | if i % 2 == 0 : 12 | arr[i] += (arr[max_idx] % max_elem ) * max_elem 13 | max_idx -= 1 14 | 15 | else : 16 | arr[i] += (arr[min_idx] % max_elem ) * max_elem 17 | min_idx += 1 18 | 19 | for i in range(0, n) : 20 | arr[i] = arr[i] // max_elem 21 | 22 | #{ 23 | # Driver Code Starts 24 | #Initial Template for Python 3 25 | 26 | import math 27 | 28 | 29 | 30 | 31 | def main(): 32 | T=int(input()) 33 | while(T>0): 34 | 35 | n=int(input()) 36 | 37 | arr=[int(x) for x in input().strip().split()] 38 | 39 | ob=Solution() 40 | ob.rearrange(arr,n) 41 | 42 | for i in arr: 43 | print(i,end=" ") 44 | 45 | print() 46 | 47 | T-=1 48 | 49 | 50 | if __name__ == "__main__": 51 | main() 52 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/R/Reorder List/30 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Reorder List** 2 | 3 | ## **Hard** 4 | 5 | Given a singly linked list*: A*0→*A*1→...→*A\*\*n*-2→*A*n-1, reorder it to: *A*0→*A\*\*n-1*→*A*1→*A\*\*n*-2→*A*2→*A\*\*n*-3→... 6 | For example: Given 1->2->3->4->5 its reorder is 1->5->2->4->3. 7 | 8 | **Note: It is recommended do this in-place without altering the node's values.** 9 | 10 | **Example 1:** 11 | 12 | ``` 13 | Input: 14 | LinkedList: 1->2->3 15 | Output: 1 3 2 16 | Explanation: 17 | Here n=3, so the correct 18 | order is A0→A2→A1 19 | ``` 20 | 21 | **Example 2:** 22 | 23 | ``` 24 | Input: 25 | Explanation: 1->7->3->4 26 | Output: 1 4 7 3 27 | Explanation: 28 | Here n=4, so the correct 29 | order is A0→A3→A1→A2 30 | ``` 31 | 32 | **Your Task:** 33 | The task is to complete the function **reorderList**() which should reorder the list as required. The reorder list is automatically printed by the driver's code. 34 | 35 | **Note**: Try to solve without using any auxilliary space. 36 | 37 | **Expected Time Complexity:** O(N) 38 | **Expected Auxiliary Space:** O(1) 39 | 40 | **Constraints:** 41 | 42 | 1 <= N <= 5\*10^5 43 | 1 <= A\[ i \] <= 10^5 44 | 45 | > ### **Problem URL: [Reorder List](https://practice.geeksforgeeks.org/problems/reorder-list/1)** 46 | -------------------------------------------------------------------------------- /Problems & Solutions/S/Scrambled String/28 January 2023/Scrambled String.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | from functools import lru_cache 3 | class Solution: 4 | @lru_cache(None) 5 | def isScramble(self, s1: str, s2: str): 6 | if Counter(s1) != Counter(s2): 7 | return False 8 | if len(s1) == 1: 9 | return True 10 | for i in range(1, len(s1)): 11 | if (self.isScramble(s1[:i], s2[:i]) and self.isScramble(s1[i:], s2[i:])) or \ 12 | (self.isScramble(s1[:i], s2[-i:]) and self.isScramble(s1[i:], s2[:-i])): 13 | return True 14 | return False 15 | #{ 16 | # Driver Code Starts 17 | #Initial Template for Python 3 18 | 19 | if __name__ == "__main__": 20 | t=int(input()) 21 | for i in range(t): 22 | S1,S2=input().split() 23 | if(Solution().isScramble( S1 , S2)): 24 | print("Yes") 25 | 26 | else: 27 | print("No") 28 | 29 | 30 | # } Driver Code Ends 31 | -------------------------------------------------------------------------------- /Problems & Solutions/S/Second smallest number/16 March 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Second smallest number** 2 | 3 | ## **Medium** 4 | 5 | The task is to find the second smallest number with a given sum of digits as S and the number of digits as D. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | S = 9 12 | D = 2 13 | Output: 14 | 27 15 | Explanation: 16 | 18 is the smallest number possible with sum = 9 17 | and total digits = 2, Whereas the second 18 | smallest is 27. 19 | ``` 20 | 21 | **Example 2:** 22 | 23 | ``` 24 | Input: 25 | S = 16 26 | D = 3 27 | Output: 28 | 178 29 | Explanation: 30 | 169 is the smallest number possible with sum is 31 | 16 and total digits = 3, Whereas the second 32 | smallest is 178. 33 | ``` 34 | 35 | **Your Task:** 36 | 37 | You don't need to read input or print anything. Your task is to complete the function secondSmallest() which takes the two integers S and D respectively and returns a string which is the second smallest number if possible, else return "-1". 38 | 39 | **Expected Time Complexity:** O(D) 40 | **Expected Space Complexity:** O(1) 41 | 42 | **Constraints:** 43 | 44 | 1 ≤ S ≤ $10^5$ 45 | 1 ≤ D ≤ $10^5$ 46 | 47 | > ### **Problem URL: [Second smallest number](https://practice.geeksforgeeks.org/problems/5c2734730cb1e98e3877a0b91f7d680d0efc8acf/1)** 48 | -------------------------------------------------------------------------------- /Problems & Solutions/S/Select Nodes/30 January 2023/README.md: -------------------------------------------------------------------------------- 1 | # **Select Nodes** 2 | 3 | ## **Hard** 4 | 5 | Given **N** nodes of a tree and a list of edges. Find the minimum number of nodes to be selected to light up all the edges of the tree. 6 | An edge lights up when at least one node at the end of the edge is selected. 7 | 8 | **Example 1:** 9 | 10 | ``` 11 | Input: 12 | N = 6 13 | edges[] = {(1,2), (1,3), (2,4), (3,5), (3,6)} 14 | Output: 2 15 | Explanation: Selecting nodes 2 and 3 lights 16 | up all the edges. 17 | ``` 18 | 19 | 20 | **Example 2:** 21 | 22 | ``` 23 | Input: 24 | N = 3 25 | arr[] = {(1,2), (1,3)} 26 | Output: 1 27 | Explanation: Selecting Node 1 28 | lights up all the edges. 29 | ``` 30 | 31 | 32 | **Your Task:** 33 | You don't need to read input or print anything. Your task is to complete the function **countVertex()** which takes the number of nodes N, and the list of edges as input parameters and returns the minimum number of nodes selected. 34 | 35 | 36 | 37 | **Expected Time Complexity:** O(N) 38 | **Expected Auxiliary Space:** O(N) 39 | 40 | 41 | **Constraints:** 42 | 1 ≤ N ≤ 10^5 43 | 1 ≤ edges ≤ N 44 | Given graph is a valid tree. 45 | 46 | > ### **Problem URL: [Select Nodes](https://practice.geeksforgeeks.org/problems/f7bfa137576243795abb0595962d61b632bbad21/1)** 47 | -------------------------------------------------------------------------------- /Problems & Solutions/S/Select Nodes/30 January 2023/Select Nodes.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | 3 | def dfs(self, adj, src, par, val): 4 | for v in adj[src]: 5 | if v!=par: 6 | self.dfs(adj,v,src,val) 7 | 8 | for v in adj[src]: 9 | if v!=par: 10 | val[src][0] += val[v][1]; 11 | val[src][1] += min(val[v][1],val[v][0]) 12 | 13 | def countVertex(self, N, edges): 14 | adj=[[] for i in range(N+1)] 15 | for i in range(N-1): 16 | adj[edges[i][0]].append(edges[i][1]) 17 | adj[edges[i][1]].append(edges[i][0]) 18 | 19 | val=[[] for i in range(N+1)] 20 | for i in range(1,N+1): 21 | val[i] = [0,1] 22 | 23 | self.dfs(adj,1,1,val) 24 | 25 | return min(val[1][0],val[1][1]) 26 | 27 | #{ 28 | # Driver Code Starts 29 | #Initial Template for Python 3 30 | 31 | if __name__ == '__main__': 32 | t = int (input ()) 33 | for _ in range (t): 34 | N = int(input()) 35 | edges=[] 36 | for _ in range(N-1): 37 | arr = list(map(int,input().split())) 38 | edges.append(arr) 39 | 40 | ob = Solution() 41 | print(ob.countVertex(N, edges)) 42 | # } Driver Code Ends 43 | -------------------------------------------------------------------------------- /Problems & Solutions/S/Shreyansh and his bits/25 November 2022/Python/Shreyansh and his bits.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def ncr(self,n,r): 3 | if r>n: 4 | return 0 5 | if r==0 or r==n: 6 | return 1 7 | if self.cache[n][r]!=-1: 8 | return self.cache[n][r] 9 | res=self.ncr(n-1,r-1)+self.ncr(n-1,r) 10 | self.cache[n][r]=res 11 | return res 12 | def count (self, N): 13 | self.cache=[[-1]*(40) for _ in range(40)] 14 | res=0 15 | ones=0 16 | bits=0 17 | while N!=0: 18 | if N&1: 19 | res+=self.ncr(bits,ones+1) 20 | ones+=1 21 | bits+=1 22 | N>>=1 23 | return res 24 | 25 | #{ 26 | # Driver Code Starts 27 | if __name__ == '__main__': 28 | ob = Solution() 29 | t = int (input ()) 30 | for _ in range (t): 31 | N = int(input()) 32 | print(ob.count(N)) 33 | 34 | 35 | 36 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/S/Single valued subtree/30 December 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Single valued subtree** 2 | 3 | ## **Medium** 4 | 5 | Given a binary tree, count the number of Single Valued Subtrees. A Single Valued Subtree is one in which all the nodes have same value. 6 | 7 | **Example 1** 8 | 9 | ``` 10 | Input : 11 | 5 12 | / \ 13 | 1 5 14 | / \ \ 15 | 5 5 5 16 | Output : 4 17 | Explanation : 18 | There are 4 subtrees with single values. 19 | ``` 20 | 21 | **Example 2:** 22 | 23 | ``` 24 | Input: 25 | 5 26 | / \ 27 | 4 5 28 | / \ \ 29 | 4 4 5 30 | Output: 5 31 | Explanation: 32 | There are five subtrees with single values. 33 | ``` 34 | 35 | **Your task :**You don't have to read input or print anything. Your task is to complete the function singlevalued() which takes the root of the tree as input and returns the count of single valued subtrees. 36 | 37 | **Expected Time Complexity :** O(n) 38 | **Expected Auxiliary Space :** O(n) 39 | 40 | **Constraints :** 41 | 1 <= n <= 10^5 42 | 43 | > ### **Problem URL: [Single valued subtree](https://practice.geeksforgeeks.org/problems/single-valued-subtree/1)** 44 | -------------------------------------------------------------------------------- /Problems & Solutions/S/Split Array Largest Sum/13 December 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Split Array Largest Sum** 2 | 3 | ## **Hard** 4 | 5 | Given an array **arr\[\]** of **N** elements and a number **K**. Split the given array into K subarrays such that the maximum subarray sum achievable out of K subarrays formed is minimum possible. Find that possible subarray sum. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | N = 4, K = 3 12 | arr[] = {1, 2, 3, 4} 13 | Output: 4 14 | Explanation: 15 | Optimal Split is {1, 2}, {3}, {4}. 16 | Maximum sum of all subarrays is 4, 17 | which is minimum possible for 3 splits.  18 | ``` 19 | 20 | **Example 2:** 21 | 22 | ``` 23 | Input: 24 | N = 3, K = 2 25 | A[] = {1, 1, 2} 26 | Output: 27 | 2 28 | Explanation: 29 | Splitting the array as {1,1} and {2} is optimal. 30 | This results in a maximum sum subarray of 2. 31 | ``` 32 | 33 | **Your Task:** 34 | The task is to complete the function **splitArray**() which returns the maximum sum subarray after splitting the array into K subarrays such that maximum sum subarray is minimum possible. 35 | 36 | **Constraints:** 37 | 1 ≤ N ≤ 10^5 38 | 1 ≤ K ≤ N 39 | 1 ≤ arr\[i\] ≤ 10^4 40 | 41 | **Expected Time Complexity:** O(N\*log(sum(arr))). 42 | **Expected Auxiliary Space:** O(1). 43 | 44 | > ### **Problem URL: [Split Array Largest Sum](https://practice.geeksforgeeks.org/problems/f04fd67b26b4828b6180715d8b1700426b637247/1)** 45 | -------------------------------------------------------------------------------- /Problems & Solutions/S/Sum of Beauty of All Substrings/16 November 2022/C++/Sum of Beauty of All Substrings.cpp: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | #include 3 | using namespace std; 4 | 5 | 6 | // } Driver Code Ends 7 | // User function Template for C++ 8 | 9 | class Solution { 10 | public: 11 | int beautySum(string s) { 12 | int n = s.length(), sum = 0; 13 | for (int i = 0; i < n - 1; i++) { 14 | vector dp(26, 0); 15 | dp[s[i] - 'a']++; 16 | for (int j = i + 1; j < n; j++) { 17 | dp[s[j] - 'a']++; 18 | int minNum = INT_MAX, maxNum = INT_MIN; 19 | for (int k = 0; k < 26; k++) { 20 | if (dp[k]) minNum = min(minNum, dp[k]); 21 | if (dp[k]) maxNum = max(maxNum, dp[k]); 22 | } 23 | sum = sum + (maxNum - minNum); 24 | } 25 | } 26 | return sum; 27 | } 28 | }; 29 | 30 | //{ Driver Code Starts. 31 | int main() { 32 | int t; 33 | cin >> t; 34 | while (t--) { 35 | string s; 36 | cin >> s; 37 | Solution obj; 38 | cout << obj.beautySum(s) << endl; 39 | } 40 | return 0; 41 | } 42 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/S/Sum of Beauty of All Substrings/16 November 2022/Python/Sum of Beauty of All Substrings.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | 3 | class Solution: 4 | def beautySum(self, s): 5 | result=0 6 | for i in range(len(s)): 7 | S={} 8 | for j in range(i,len(s)): 9 | if s[j] not in S: 10 | S[s[j]]=1 11 | else: 12 | S[s[j]]+=1 13 | mostFrequency,leastFrequency=max(S.values()),min(S.values()) 14 | result+=(mostFrequency-leastFrequency) 15 | return result 16 | 17 | #{ 18 | # Driver Code Starts 19 | #Initial Template for Python 3 20 | 21 | if __name__ == '__main__': 22 | t = int(input()) 23 | for _ in range(t): 24 | s = input() 25 | ob = Solution() 26 | print(ob.beautySum(s)) 27 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/S/Sum of Beauty of All Substrings/16 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Sum of Beauty of All Substrings** 2 | 3 | ## **Medium** 4 | 5 | Given a string **S**, return the sum of **beauty** of all its substrings. 6 | The **beauty** of a string is defined as the difference in frequencies between the most frequent and least frequent characters. 7 | 8 | - For example, the beauty of string "aaac" is 3 - 1 = 2. 9 | 10 | **Example 1:** 11 | 12 | **Input:** 13 | S = "aaac" 14 | **Output:** 15 | 3 16 | **Explanation:** The substrings with non - zero beauty are \["aaac","aac"\] where beauty of "aaac" is 2 and beauty of "aac" is 1.**Example 2:** 17 | 18 | **Input:** 19 | S = "geeksforgeeks" 20 | **Output:** 21 | 62**Your Task:** 22 | You don't need to read input or print anything. Your task is to complete the function **beautySum()** which takes string **S** as input paramters and returns the sum of **beauty** of all its substrings. 23 | 24 | **Expected Time Complexity:** O(|S|^2) 25 | **Expected Auxiliary Space:** O(1) 26 | 27 | **Constraints:** 28 | 1 ≤ |S| ≤ 500 29 | S only contains lower case alphabets. 30 | 31 | > ### **Problem URL: [Sum of Beauty of All Substrings](https://practice.geeksforgeeks.org/problems/sum-of-beauty-of-all-substrings-1662962118/1)** 32 | -------------------------------------------------------------------------------- /Problems & Solutions/T/Total Decoding Messages/27 January 2023/Total Decoding Messages.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def CountWays(self, str): 3 | if str[0] == '0' : 4 | return 0 5 | 6 | n = len(str) 7 | mod = 1000000007 8 | 9 | dp = [0 for i in range(n+1)] 10 | 11 | dp[0] = dp[1] = 1 12 | 13 | for i in range(1,n): 14 | if str[i] == '0' and str[i-1] > '2' : 15 | return 0 16 | 17 | 18 | for i in range(2,n+1): 19 | if str[i-1] > '0' : 20 | dp[i] = dp[i-1] 21 | 22 | if str[i-2] == '1' or (str[i-2] == '2' and str[i-1] < '7') : 23 | dp[i] +=dp[i-2] 24 | 25 | dp[i] %= mod 26 | 27 | return dp[n] 28 | 29 | #{ 30 | # Driver Code Starts 31 | #Initial Template for Python 3 32 | 33 | import sys 34 | sys.setrecursionlimit(10**6) 35 | if __name__ == '__main__': 36 | T=int(input()) 37 | for i in range(T): 38 | str = input() 39 | ob = Solution() 40 | ans = ob.CountWays(str) 41 | print(ans) 42 | 43 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/T/Two numbers with odd occurrences/08 November 2022/Python/Two numbers with odd occurrences.py: -------------------------------------------------------------------------------- 1 | #User function Template for python3 2 | class Solution: 3 | def twoOddNum(self, Arr, N): 4 | dic = {} 5 | for i in range(len(Arr)): 6 | dic[Arr[i]] = dic[Arr[i]] + 1 if Arr[i] in dic else 1 7 | val = list(dic.values()) 8 | ke = list(dic.keys()) 9 | lst = [] 10 | for j in range(len(val)): 11 | if val[j] %2 != 0: 12 | lst.append(ke[j]) 13 | lst.sort() 14 | lst = lst[::-1] 15 | return lst 16 | 17 | 18 | #{ 19 | # Driver Code Starts 20 | #Initial Template for Python 3 21 | 22 | if __name__ == '__main__': 23 | t = int(input()) 24 | for _ in range (t): 25 | N = int(input()) 26 | Arr = input().split() 27 | for itr in range(N): 28 | Arr[itr] = int(Arr[itr]) 29 | ob = Solution(); 30 | ans = ob.twoOddNum(Arr,N) 31 | for i in range(len(ans)): 32 | print(ans[i], end = " ") 33 | print() 34 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/T/Two numbers with odd occurrences/08 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Two numbers with odd occurrences** 2 | 3 | **Medium** 4 | 5 | Given an unsorted array, **Arr**\[\] of size **N** and that contains **even** number of occurrences for all numbers except two numbers. Find the two numbers in **decreasing** order which has **odd** occurrences. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | N = 8 12 | Arr = {4, 2, 4, 5, 2, 3, 3, 1} 13 | Output: {5, 1} 14 | Explaination: 5 and 1 have odd occurrences. 15 | ``` 16 | 17 | 18 | **Example 2:** 19 | 20 | ``` 21 | Input: 22 | N = 6 23 | Arr = {1 7 5 7 5 4 7 4} 24 | Output: {7, 1} 25 | Explaination: 7 and 1 have odd occurrences. 26 | ``` 27 | 28 | 29 | **Your Task:** 30 | You don't need to read input or print anything. Your task is to complete the function **twoOddNum()** which takes the array **Arr\[\]** and its size **N** as input parameters and returns the two numbers in decreasing order which have odd occurrences. 31 | 32 | 33 | **Expected Time Complexity:** O(N) 34 | **Expected Auxiliary Space:** O(1) 35 | 36 | 37 | **Constraints:** 38 | 2 ≤ N ≤ 106 39 | 1 ≤ Arri ≤ 1012 40 | 41 | > ### Problem URL: **[Two numbers with odd occurrences](https://practice.geeksforgeeks.org/problems/two-numbers-with-odd-occurrences5846/1)** -------------------------------------------------------------------------------- /Problems & Solutions/T/Type it!/25 January 2023/Type it!.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 minOperation(string s) { 13 | 14 | int n = s.size(); 15 | int cnt = 1; 16 | 17 | for(int i=0; i> t; 35 | while (t--) { 36 | string s; 37 | cin >> s; 38 | Solution ob; 39 | cout << ob.minOperation(s) << "\n"; 40 | } 41 | return 0; 42 | } 43 | 44 | // } Driver Code Ends 45 | -------------------------------------------------------------------------------- /Problems & Solutions/T/Type it!/25 January 2023/Type it!.java: -------------------------------------------------------------------------------- 1 | //{ Driver Code Starts 2 | // Initial Template for Java 3 | 4 | import java.io.*; 5 | import java.util.*; 6 | class GFG { 7 | public static void main(String args[]) throws IOException { 8 | BufferedReader read = 9 | new BufferedReader(new InputStreamReader(System.in)); 10 | PrintWriter out=new PrintWriter(System.out); 11 | int t = Integer.parseInt(read.readLine()); 12 | 13 | while (t-- > 0) { 14 | String s = read.readLine().trim(); 15 | Solution ob = new Solution(); 16 | int ans = ob.minOperation(s); 17 | out.println(ans); 18 | } 19 | out.close(); 20 | } 21 | } 22 | 23 | // } Driver Code Ends 24 | 25 | 26 | // User function Template for Java 27 | 28 | class Solution { 29 | int minOperation(String s) { 30 | 31 | int ans=s.length(); 32 | 33 | for(int i=0; itarget or num<=0: 10 | return 11 | 12 | if Sum==target: 13 | out.append(res) 14 | return 15 | 16 | res1 = res[:] 17 | res2 = res[:] 18 | res1 = res1+[num] 19 | partitions(num,Sum+num,res1,out) 20 | partitions(num-1,Sum,res2,out) 21 | 22 | return 23 | 24 | Sum = 0 25 | res = [] 26 | out = [] 27 | target = n 28 | partitions(n,Sum,res,out) 29 | return out 30 | 31 | #{ 32 | # Driver Code Starts 33 | #Initial Template for Python 3 34 | 35 | if __name__ == '__main__': 36 | T=int(input()) 37 | for i in range(T): 38 | n= int(input()) 39 | ob = Solution() 40 | ans = ob.UniquePartitions(n) 41 | for a in ans: 42 | for b in a: 43 | print(b, end = " ") 44 | print() 45 | 46 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/U/Unique partitions/21 November 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Unique partitions** 2 | 3 | ## **Medium** 4 | 5 | Given a positive integer **n**, generate all possible unique ways to represent **n** as sum of positive integers. 6 | 7 | **Note:** The generated output is printed without partitions. Each partition must be in decreasing order. Printing of all the partitions is done in reverse sorted order. 8 | 9 | **Example 1:** 10 | 11 | ``` 12 | Input: n = 3 13 | Output: 3 2 1 1 1 1 14 | Explanation: For n = 3, 15 | {3}, {2, 1} and {1, 1, 1}. 16 | ``` 17 | 18 | **Example 2:** 19 | 20 | ``` 21 | Input: n = 4 22 | Output: 4 3 1 2 2 2 1 1 1 1 1 1 23 | Explanation: For n = 4, 24 | {4}, {3, 1}, {2, 2}, {2, 1, 1}, {1, 1, 1, 1}. 25 | ``` 26 | 27 | **Your Task:** 28 | You don't need to read or print anything. Your task is to complete the function **UniquePartitions()** which takes n as input parameter and returns a list of all possible combinations in lexicographically decreasing order. 29 | 30 | **Expected Time Complexity:** O(2^n) 31 | **Expected Space Complexity:** O(n) 32 | 33 | **Constraints:** 34 | 35 | - `1 <= n <= 25` 36 | 37 | > ### **Problem URL: [Unique partitions](https://practice.geeksforgeeks.org/problems/unique-partitions1041/1)** 38 | -------------------------------------------------------------------------------- /Problems & Solutions/W/Walls Coloring/14 February 2023/Walls Coloring.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 minCost(vector> &colors, int N) { 14 | for (int i=1; i> t; 31 | while (t--) { 32 | 33 | int n; 34 | cin >> n; 35 | vector> colors(n, vector(3)); 36 | for (int i = 0; i < n; i++) 37 | cin >> colors[i][0] >> colors[i][1] >> colors[i][2]; 38 | 39 | Solution ob; 40 | cout << ob.minCost(colors, n) << endl; 41 | } 42 | } 43 | // } Driver Code Ends 44 | -------------------------------------------------------------------------------- /Problems & Solutions/W/Wine Buying and Selling/24 December 2022/Python/Wine Buying and Selling.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def wineSelling(self, Arr, N): 3 | buy=[] 4 | sell=[] 5 | for i in range(N): 6 | if Arr[i]>0: 7 | buy.append([Arr[i],i]) 8 | else: 9 | sell.append([Arr[i],i]) 10 | ans=0 11 | i=0 12 | j=0 13 | while i 5 | using namespace std; 6 | 7 | 8 | // } Driver Code Ends 9 | 10 | class Solution{ 11 | public: 12 | long long int findSubarray(vector &arr ,int n) { 13 | 14 | long long int sum=0, counter=0; 15 | 16 | unordered_mapmp; 17 | 18 | for(int i=0;i>t; 43 | while(t--) 44 | { 45 | int n; 46 | cin>>n; //input size of array 47 | 48 | vector arr(n,0); 49 | 50 | for(int i=0;i>arr[i]; //input array elements 52 | Solution ob; 53 | cout << ob.findSubarray(arr,n) << "\n"; 54 | } 55 | return 0; 56 | } 57 | // } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/Z/Zero Sum Subarrays/22 December 2022/Python/Zero Sum Subarrays.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findSubArrays(self,arr,n): 3 | 4 | c=0 5 | hashMap = {} 6 | out = [] 7 | sum1 = 0 8 | 9 | for i in range(n): 10 | sum1 += arr[i] 11 | 12 | if sum1 == 0: 13 | out.append((0, i)) 14 | c+=1 15 | al = [] 16 | 17 | if sum1 in hashMap: 18 | 19 | al = hashMap.get(sum1) 20 | for it in range(len(al)): 21 | out.append((al[it] + 1, i)) 22 | c+=1 23 | 24 | al.append(i) 25 | hashMap[sum1] = al 26 | 27 | return c 28 | 29 | #{ 30 | # Driver Code Starts 31 | #Initial Template for Python 3 32 | 33 | #contributed by RavinderSinghPB 34 | 35 | if __name__=='__main__': 36 | t=int(input()) 37 | for tc in range(t): 38 | 39 | N=int(input()) 40 | A = [int(x) for x in input().strip().split(' ')] 41 | ob = Solution() 42 | print(ob.findSubArrays(A,N)) 43 | 44 | 45 | 46 | # } Driver Code Ends -------------------------------------------------------------------------------- /Problems & Solutions/Z/Zero Sum Subarrays/22 December 2022/README.md: -------------------------------------------------------------------------------- 1 | # **Zero Sum Subarrays** 2 | 3 | ## **Medium** 4 | 5 | You are given an array arr\[\] of size n. Find the total count of sub-arrays having their sum equal to 0. 6 | 7 | **Example 1:** 8 | 9 | ``` 10 | Input: 11 | n = 6 12 | arr[] = {0,0,5,5,0,0} 13 | Output: 6 14 | Explanation: The 6 subarrays are 15 | [0], [0], [0], [0], [0,0], and [0,0]. 16 | ``` 17 | 18 | **Example 2:** 19 | 20 | ``` 21 | Input: 22 | n = 10 23 | arr[] = {6,-1,-3,4,-2,2,4,6,-12,-7} 24 | Output: 4 25 | Explanation: The 4 subarrays are [-1 -3 4] 26 | [-2 2], [2 4 6 -12], and [-1 -3 4 -2 2] 27 | ``` 28 | 29 | **Your Task:** 30 | You don't need to read input or print anything. Complete the function **findSubarray()** that takes the array arr and its size n as input parameters and returns the total number of sub-arrays with 0 sum. 31 | 32 | **Expected Time Complexity**: O(n\*log(n)) 33 | **Expected Auxilliary Space**: O(n) 34 | 35 | **Constraints:** 36 | 1 <= n <= 10^6 37 | -10^9 <= arr\[ i \] <= 10^9 38 | 39 | > ### **Problem URL: [Zero Sum Subarrays](https://practice.geeksforgeeks.org/problems/zero-sum-subarrays1825/1)** 40 | --------------------------------------------------------------------------------