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.
- 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
andlong
- LCM (Least Common Multiple) for
int
andlong
- Prime number checking (
isPrime
) - Array sorting utilities
- GCD (Greatest Common Divisor) for
- 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
git clone https://github.com/princerxj/Java-CP-Template.git
cd Java-CP-Template
javac Main.java
java Main < input.txt
or
java Main
# Then enter your input manually
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);
}
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
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});
Java-CP-Template/
├── Main.java # Main template file
├── LICENSE # MIT License
└── README.md # This file
- Always flush output: The template automatically flushes output after all test cases
- Use appropriate data types: Choose
int
for values ≤ 10^9,long
for larger values - Test with sample inputs: Always verify your solution with provided examples
- Handle edge cases: Consider boundary conditions like n=1, n=0, etc.
- Time complexity: Ensure your solution runs within time limits (usually 1-2 seconds)
Contributions are welcome! If you have ideas for improvements or additional utility functions:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Commit your changes (
git commit -m 'Add some amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
- 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
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