Gasoline Introduction Codechef Solution

Hello Programmers In this post, you will know how to solve the Gasoline Introduction Codechef Solution. The Problem Code: BEGGASOL

Ezoicreport this adGasoline Introduction Codechef Solution
Gasoline Introduction Codechef Solution

One more thing to add, don’t directly look for the solutions, first try to solve the problems of Codechef by yourself. If you find any difficulty after trying several times, then you can look for 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

Ezoicreport this adGasoline 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 Solutions 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 Solutions 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)
Ezoicreport this ad

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

Note:- I compile all programs, if there is any case program is not working and showing an error please let me know in the comment section. If you are using adblocker, please disable adblocker because some functions of the site may not work correctly.

Next: Average Flex Codechef Solution

Sharing Is Caring

Leave a Comment

Ezoicreport this ad