Gasoline Introduction Codechef Solution|Problem Code: BEGGASOL

Gasoline Introduction Codechef Solution: Hello coders, today we are going to solve Gasoline Introduction Codechef Solution.

Codechef solutions

Problem

There are N cars (numbered 11 through NN) on a circular track with length N. For each ii (2≤i≤N), the i-th of them is at a distance i−1 clockwise from car 11, i.e. car 11 needs to travel a distance i−1i−1 clockwise to reach car i. Also, for each valid ii, the i-th car has fi litres of gasoline in it initially.

You are driving car 1 in the clockwise direction. To move one unit of distance in this direction, you need to spend 1 litre of gasoline. When you pass another car (even if you’d run out of gasoline exactly at that point), you steal all its gasoline. Once you do not have any gasoline left, you stop.

What is the total clockwise distance travelled by your car?

Input

  • The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
  • The first line of each test case contains a single integer N.
  • The second line contains N space-separated integers f1,f2,…,fN.

Output

For each test case, print a single line containing one integer ― the total clockwise distance travelled.

Constraints

  • 1≤T≤1001≤T≤100
  • 1≤N≤1001≤N≤100
  • 0≤fi≤1000≤fi≤100 for each valid i

Example

Sample Input 1 

3
5
3 0 0 0 0
5
1 1 1 1 1
5
5 4 3 2 1

Sample Output 1 

3
5
15

Gasoline Introduction  – CodeChef Solution in JAVA

import java.util.Scanner;
public class Main {
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();
    for (int tc = 0; tc < T; ++tc) {
      int N = sc.nextInt();
      int[] f = new int[N];
      for (int i = 0; i < f.length; ++i) {
        f[i] = sc.nextInt();
      }
      System.out.println(solve(f));
    }
    sc.close();
  }
  static int solve(int[] f) {
    int gasoline = 0;
    for (int i = 0; i < f.length; ++i) {
      gasoline += f[i];
      if (gasoline == 0) {
        return i;
      }
      --gasoline;
    }
    return f.length + gasoline;
  }
}

Gasoline Introduction  – CodeChef Solution in CPP

#include <iostream>
using namespace std;
int main() {
	int t;cin>>t;
	while(t--){
	    int n;cin>>n;
	    int a[n];
	    int sum=0;
	    for(int i=0;i<n;i++){
	        cin>>a[i];
	    }
	    int distance=0;
	    for(int i=0;i<n;i++){
	    sum+=a[i]-1;
	    if(sum<0){
	    break;
	    }
	    distance++;
	    }
	    if(sum>0){
	        distance+=sum;
	    }
	    cout<<distance<<endl;
	}
	return 0;
}

Gasoline Introduction  – CodeChef Solution in Python

for _ in range(int(input())):
    a=int(input())
    b=list(map(int,input().split()))
    c=d=b[0]
    for i in range(1,a):
        if(c==0):
            break
        c-=1
        c+=b[i]
        d+=b[i]
    print(d)

Disclaimer: The above Problem (Gasoline Introduction  ) is generated by CodeChef but the solution is provided by  Chase2learn.This tutorial is only for Educational and Learning purpose.

Leave a Comment