Skip to content
    SeaCode
    Learning tracksCode ArenaWorkshops & EventsInstructors
    Log inSign up
    Practice problems / Gas station

    Gas station

    Mediumgreedyarray

    Stations sit on a circular road. At station i you get gas[i] fuel, and driving to the next station costs cost[i]. You start with an empty tank. Return the station index from which you can complete the loop, or -1. If an answer exists it is unique.

    Example 1

    Input:  gas = [1,2,3,4,5], cost = [3,4,5,1,2]
    Output: 3

    Example 2

    Input:  gas = [2,3,4], cost = [3,4,3]
    Output: -1

    Constraints

    • 1 ≤ n ≤ 10⁵
    • 0 ≤ gas[i], cost[i] ≤ 10⁴

    Submit checks your solution against 8 tests, including hidden ones.

    /**
     * @param {int[]} gas
     * @param {int[]} cost
     * @return {int}
     */
    function canCompleteCircuit(gas, cost) {
      
    }
    
    Sign in to run
    SeaCode

    Learn to code for real, online or at our branch in front of Cairo University, Giza.

    Learn

    • All courses
    • Interactive tutorials
    • Interview practice problems
    • Mock interview
    • Learning tracks
    • Code Arena
    • Workshops & Events
    • Knowledge library
    • Team projects

    Your account

    • Log in
    • Sign up
    • Learning plan
    • Certificates
    • Site map

    Contact us

    • WhatsApp
    • 010 9094 1104
    • In front of Cairo University, Giza

    © 2026 SeaCode. All rights reserved.