Skip to content

princerxj/Java-CP-Template

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

Java Competitive Programming Template

A comprehensive and optimized Java template for competitive programming contests including Codeforces, AtCoder, CodeChef, and other platforms. This template provides fast I/O operations, utility functions, and a clean structure to help you focus on solving problems efficiently.

Features

  • Fast I/O Operations: Custom FastScanner class for lightning-fast input parsing
  • Multiple Test Case Support: Built-in structure for handling multiple test cases
  • Utility Functions: Pre-implemented common functions including:
    • GCD (Greatest Common Divisor) for int and long
    • LCM (Least Common Multiple) for int and long
    • Prime number checking (isPrime)
    • Array sorting utilities
  • String Handling: Optimized nextLine() support for proper string input
  • Clean Structure: Organized code with separate sections for I/O, utilities, and main logic
  • Performance Optimized: Uses BufferedOutputStream and efficient parsing algorithms

Quick Start

1. Clone the Repository

git clone https://github.com/princerxj/Java-CP-Template.git
cd Java-CP-Template

2. Compile the Code

javac Main.java

3. Run with Input

java Main < input.txt

or

java Main
# Then enter your input manually

4. Modify the solve() Method

Replace the example code in the solve() method with your solution:

static void solve() {
    // Your solution code goes here
    int n = in.nextInt();
    long x = in.nextLong();
    String s = in.nextLine();
    
    // Example: Print the sum of n and x
    out.println(n + x);
}

Usage Example

Here's a simple example solving a problem that reads two integers and prints their sum:

Input Format:

3
5 10
2 8
1 1

Solution:

static void solve() {
    int a = in.nextInt();
    int b = in.nextInt();
    out.println(a + b);
}

Output:

15
10
2

Available Utility Functions

The template includes several pre-implemented utility functions in the Utils class:

// GCD and LCM
int g = Utils.gcd(12, 18);        // Returns 6
long l = Utils.lcm(4L, 6L);       // Returns 12

// Prime checking
boolean isPrime = Utils.isPrime(17); // Returns true

// Array sorting
int[] sorted = Utils.sort(new int[]{3, 1, 4, 1, 5});

Template Structure

Java-CP-Template/
├── Main.java          # Main template file
├── LICENSE            # MIT License
└── README.md          # This file

Tips for Competitive Programming

  1. Always flush output: The template automatically flushes output after all test cases
  2. Use appropriate data types: Choose int for values ≤ 10^9, long for larger values
  3. Test with sample inputs: Always verify your solution with provided examples
  4. Handle edge cases: Consider boundary conditions like n=1, n=0, etc.
  5. Time complexity: Ensure your solution runs within time limits (usually 1-2 seconds)

Contributing

Contributions are welcome! If you have ideas for improvements or additional utility functions:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add some amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

Contribution Guidelines

  • Maintain the existing code style and formatting
  • Add comments for complex utility functions
  • Test your additions thoroughly
  • Update this README if you add new features
  • Ensure backward compatibility

License

MIT License

Copyright (c) 2025 Prince Raj

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


Happy Coding! May your solutions be fast and your ratings be high!

For questions or suggestions, feel free to open an issue or contact @princerxj

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages