Search This Blog

Showing posts with label Problem Solving. Show all posts
Showing posts with label Problem Solving. Show all posts
Tuesday, 26 December 2023

Solve Gas Station Problem using Computer

0 comments

Sample Input:

Sample Input:

Case 1:
int A[] = {2,1,3};
int B[] = {1,3,2};

Case 2:
int A[] = {2,2,2};
int B[] = {1,3,2};

Case 3:
int A[] = {1,2,2};
int B[] = {2,1,2};

Case 4:
int A[] = {1,2,2};
int B[] = {2,3,2};

Case 5:
int A[] = {1,2,3};
int B[] = {2,3,2};

Program in C:

#include <stdio.h>

int main()
{
    int A[] = {1,2,3};
    int B[] = {2,3,2};
    int i,j,k,s=0,t;
    int n=3;
    for(i=0; i<n; i++)
    {
        t=0;
        for(j=0; j<n; j++)
        {
           k=(i+j)%n;
           t = t + A[k];
           if(t<B[k])
            break;
           t = t - B[k];
        }
        if(j==n)
        {
            s=i+1;
        printf("The right gas station to begin with is %d",s);
        break;
        }
        
    }
    if(s==0)
        printf("There is no station to start with ...");
    return 0;
}
Continue reading →