Wednesday, April 16, 2025

Using Google AI Studio for Competitive Programming

Last year, Gemini was estimated to be better than 85% of competition participants in solving competitive programming problems.

This blog post shows how to use Google AI Studio using the latest Gemini 2.5 Pro (Preview 03-25) model to solve a competitive programming problem from onlinejudge.org and then ask it to write an editorial explaining how the problem is solved.

Here are the steps:
  1. Pick a problem. Let's say problem 1595.

  2. Create an image screenshot like this and save it to your computer:

  3. Open Google AI Studio: https://aistudio.google.com/prompts/new_chat

  4. Attach the image (from step 2) and type in this prompt:
    Given a competitive programming problem in the attached image.
    Write a C++ code to solve the problem.
    Make sure to follow the input and output specifications.
    See the sample input and sample output for some examples.
  5. Click Run:

  6. It will run for a few minutes to think and solve the problem.
    You can click "Expand to view model thoughts" to see the details.

  7. After it completes, you should be able to click the copy button for the solution code:


  8. Paste the solution to the quick submit


  9. See the status at https://uhunt.onlinejudge.org/ make sure it is "Accepted":


  10. Now, go back to the Google AI Studio, type in this prompt, then click Run:
    Write one page editorial explaining how you solved the problem.
  11. You should get a good explanation on how to solve the problem 1595:


  12. You can continue the prompt to ask follow up questions if you have.

In the past, I have to dig into forums to find hints on how to solve the problems when I'm stuck, which wasted a lot of my time. Nowdays, we can use Google AI Studio to minimize the time to find hints!

The competitive programming field is going to be even more competitive than ever ...