Skip to content

UdaySharmaGitHub/GFG-Problems

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

353 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

GFG Problems β€” DSA Solutions in C++

A curated collection of solved Data Structures & Algorithms problems from GeeksforGeeks, including Problem of the Day (POTD) submissions β€” all implemented in C++.


Table of Contents


About This Repository

This repository contains solutions for GFG problems organized by difficulty level. Each file is a self-contained C++ solution to a single problem. The goal is to:

  • Build a reference library for common DSA patterns
  • Track progress across difficulty levels
  • Help others learn by reading clean, working solutions

Browse by Difficulty

Folder Description
🏫 School Problems Foundational math, strings, I/O
πŸ”΅ Basic Problems Arrays, linked lists, trees, basic patterns
🟒 Easy Problems Sorting, searching, BST, stack, queue
🟑 Medium Problems DP, graphs, advanced trees, sliding window
πŸ”΄ Hard Problems Complex DP, advanced graphs, segment trees

Repository Structure

GFG-Problems/
β”œβ”€β”€ School Problems/      # Foundational math, strings, I/O
β”œβ”€β”€ Basic Problems/       # Arrays, linked lists, trees, basic patterns
β”œβ”€β”€ Easy Problems/        # Sorting, searching, BST, stack, queue
β”œβ”€β”€ Medium Problems/      # DP, graphs, advanced trees, sliding window
β”œβ”€β”€ Hard Problems/        # Complex DP, advanced graphs, segment trees
└── README.md

Folder Breakdown

🏫 School Problems

Count: 80+ problems

Beginner-level problems covering C++ fundamentals, basic math, and string manipulation.

Topic Example Problems
Math & Numbers Armstrong Number, Palindrome of Number, Perfect Number, Simple Interest, GCD, nPr
Strings Reverse A String, Remove Vowels, Lower/Upper Case, Count Camel Case Characters
Arrays & Matrix Matrix Multiplication, Transpose 2D Matrix, Sum of Diagonals, Mean/Median of Array
Patterns Printing Pattern Set 1, Triangle Shrinking Downwards, Pattern of Strings
Linked Lists Print Linked List Elements
Miscellaneous Series of AP/GP, Swap Kth Element, Combinational Logic, Java Switch Case

πŸ“ Path: School Problems


πŸ”΅ Basic Problems

Count: 85+ problems

Covers core data structures and foundational algorithms.

Topic Example Problems
Arrays Missing Number, Binary Array Sorting, Cyclically Rotate Array, Count Pair Sum
Strings Anagram of String, Implement strstr, Merge Two Strings, Print First Letter of Every Word
Linked Lists Linked List Insertion, Insert in Middle, Node at Given Index, Find Sum of Last N Nodes
Trees Inorder/Preorder/Postorder Traversal, Count Leaves, Minimum Element in BST
Stack Implement Stack using Array
Searching Binary Search, First and Last Occurrences of X
Math LCM and GCD, Power of 2, Power of Four, Factorial, Fibonacci
Patterns Pattern 1–13 (multiple star/number patterns)
Miscellaneous Leap Year, Logical Operators, C++ STL Vector

πŸ“ Path: Basic Problems


🟒 Easy Problems

Count: 130+ problems

Introduces classic algorithm patterns and more complex data structures.

Topic Example Problems
Arrays Move Zeroes to End, Rotate Array, Sort 0-1-2, Equilibrium Point, Second Largest
Strings Palindrome String, Panagram Checking, Run Length Encoding, Reverse Words
Linked Lists Detect Loop, Reverse Linked List, Merge Sorted LL, Delete Middle Node
Trees Height of Binary Tree, Left/Right View, Level Order Traversal, Mirror Tree
BST Check for BST, Kth Largest in BST, Print BST Elements in Range
Sorting Bubble Sort, Selection Sort
Stack & Queue Parentheses Checker, Delete Middle of Stack, Two Stacks
Searching Binary Search variants, Floor in Sorted Array, Find Transition Point
Math Juggler Sequence, Padovan Sequence, Nth Fibonacci Number
Matrix Search in Matrix, Print Matrix in Snake Pattern, Unique Rows in Boolean Matrix
Patterns Pattern 2–21 (various shapes)

πŸ“ Path: Easy Problems


🟑 Medium Problems

Count: 160+ problems

Covers advanced techniques such as dynamic programming, greedy algorithms, and complex tree/graph operations.

Topic Example Problems
Dynamic Programming 0/1 Knapsack, Longest Common Substring, Gold Mine, Pascal Triangle, Count Ways to Nth Stair
Greedy Job Sequencing, Minimum Cost of Ropes, N Meetings in One Room, Maximum Tip Calculator
Linked Lists Palindrome LL, Reverse in Groups, Flatten LL, Clone with Random Pointer (see Hard)
Trees Diameter of Binary Tree, Diagonal Sum, Ancestors in BT, Boundary Traversal
BST Lowest Common Ancestor, Pair Sum in BST, Flatten BST, Largest BST
Graphs Shortest Path in Undirected/Weighted Graph
Arrays Kadane's Algorithm, Count Inversions, Majority Element, Find Missing & Repeating
Strings Implement Atoi, Validate IP Address, Smallest Distinct Window
Sliding Window Longest Substring with K Uniques, Subarrays with Atmost K Distinct
Binary Search Allocate Minimum Pages, Minimize Heights II, K-Sized Subarray Maximum
Math Modular Exponentiation, Integral Points Inside Triangle, Prime Pair with Target Sum
Matrix Left Rotate Matrix K Times, Search in Row-Column Sorted Matrix, Largest Square

πŸ“ Path: Medium Problems


πŸ”΄ Hard Problems

Count: 27+ problems

The most challenging problems involving advanced algorithms and complex optimizations.

Topic Example Problems
Trees Binary Tree to DLL, Burning Tree, Possible Paths in a Tree
DP Edit Distance, Longest Valid Parentheses, Minimum Cost Path, Wildcard Pattern Matching
Graphs Alien Dictionary, Maximum Connected Group
Linked Lists Subtraction in Linked List, Clone LL with Random Pointer
Strings Longest Prefix Suffix, Smallest Window Containing All Characters, The Palindrome Pattern
Arrays Count Reverse Pairs, Count Smaller Elements, Median of 2 Sorted Arrays (Different Sizes)
Others N Queen Problem, Split Array Largest Sum, Meeting Rooms III

πŸ“ Path: Hard Problems


Topics Covered

Data Structure / Algorithm Difficulty Range
Arrays & Strings School β†’ Hard
Linked Lists (Singly, Doubly, Circular) Basic β†’ Hard
Stack & Queue Basic β†’ Medium
Binary Trees & BST Basic β†’ Hard
Graphs Medium β†’ Hard
Dynamic Programming Medium β†’ Hard
Greedy Algorithms Easy β†’ Medium
Sorting & Searching Basic β†’ Medium
Sliding Window & Two Pointers Easy β†’ Medium
Matrix Operations School β†’ Medium
Bit Manipulation Easy β†’ Medium
Math & Number Theory School β†’ Medium

How to Contribute

Contributions are welcome! Follow these steps:

1. Fork & Clone

git clone https://github.com/<your-username>/GFG-Problems.git
cd GFG-Problems

2. Create a Branch

git checkout -b add/<Problem_Name>

3. Add Your Solution

  • Place the file in the correct difficulty folder (School Problems/, Basic Problems/, Easy Problems/, Medium Problems/, or Hard Problems/)
  • Follow the naming convention below
  • Make sure your file compiles without errors

4. Verify It Compiles

g++ -std=c++17 -Wall -o test "Easy Problems/Your_Problem_Name.cpp"

5. Commit and Push

git add "Easy Problems/Your_Problem_Name.cpp"
git commit -m "Add: Your_Problem_Name (Easy)"
git push origin add/<Problem_Name>

6. Open a Pull Request

Open a PR against the main branch. Describe the problem, your approach, and the time/space complexity.


Git Hooks Setup (Optional but Recommended)

This repository includes automatic commit message formatting via git hooks. The hooks will:

  1. prepare-commit-msg β€” Auto-generates commit messages in the format:

    POTD DD-MM-YYYY : problem_name
    

    Example: POTD 07-07-2026 : Two Sum

  2. commit-msg β€” Validates your commit message:

    • Ensures the message starts with POTD or a capital letter
    • Minimum 5 characters long
    • Allows merge/revert commits to bypass validation

Quick Setup

After cloning the repository, run the setup script:

bash setup-hooks.sh

Or manually set up hooks:

# Copy hooks from git_hooks/ to .git/hooks/
cp git_hooks/prepare-commit-msg .git/hooks/
cp git_hooks/commit-msg .git/hooks/

# Make them executable
chmod +x .git/hooks/prepare-commit-msg
chmod +x .git/hooks/commit-msg

Usage

Simply commit as usual β€” the message will be auto-generated:

git add "Easy Problems/Your_Problem_Name.cpp"
git commit
# Message auto-populates: POTD 07-07-2026 : Your_Problem_Name

Note: Git hooks are local and not pushed to GitHub. They need to be set up on each machine after cloning. The setup-hooks.sh script makes this easy!


Naming Convention

File names follow this pattern:

Problem_Name_With_Underscores.cpp

Rules:

  • Use underscores (_) instead of spaces
  • Use Title_Case for each word
  • Match the exact problem name from GFG as closely as possible
  • .cpp extension only β€” do not commit .exe or compiled binaries

Examples:

βœ…  Kadane's_Algorithm.cpp
βœ…  Lowest_Common_Ancestor_in_a_BST.cpp
βœ…  Reverse_a_linked_list.cpp
❌  kadanes algorithm.cpp
❌  solution1.cpp
❌  Kadane's_Algorithm.exe

License

This repository is for educational purposes. All problem statements belong to GeeksforGeeks. Solutions and code in this repository are open for learning and reference.


⭐ If this repository helped you, consider starring it to help others find it!

Releases

No releases published

Packages

 
 
 

Contributors