Open In App

How to Print Fast Output in Competitive Programming using Java?

In Competitive programming, most of the students use C++ as their primary language as it is faster than the other languages(e.g Java, Python) but for a student/professional who use Java as his/her primary language taking Input from input streams and printing fast output is the main difficulty faced during contests on competitive platforms(eg. CodeChef, CodeForces, Spoj, etc).

In this article, there defined the fastest method to print O/P using Java (Mainly in Competitive Programming).

BufferedWriter Class: It writes text to a character-output stream, buffering characters to provide for the efficient writing of single characters, arrays, and strings. It makes the performance fast.



BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));

Methods of BufferedWriter:



Below is the implementation of the problem statement:




// Print fast Output in Competitive Programming using JAVA
import java.io.*;
 
class GFG {
    public static void main(String[] args) throws Exception
    {
        String[] gfg = { "Geeks", "For", "Geeks" };
 
        BufferedWriter output = new BufferedWriter(
            new OutputStreamWriter(System.out));
 
        for (int i = 0; i < gfg.length; i++) {
            output.write(gfg[i] + "\n");
        }
 
        output.flush();
    }
}

Output
Geeks
For
Geeks

 

Article Tags :