Click here to Skip to main content
15,917,645 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Consider yourself as the route planner working for Mumbai road transportation cooperation which has b bus stops. We are aware of the distance between all pairs of adjacent bus stops:

x1 is the distance between the 1st and the 2nd Bus stop,
x2 is the distance between the 2nd and the 3rd Bus stop,

. . .

xb - 1 is the distance between the (b- 1)th and the (b)th Bus stop,

xb is the distance between the bth and the 1st Bus stop.
Consider the buses travel along the circle line in both ways. Find the shortest distance between stops with numbers s and t.

Input Format

The number of Bus stops on the circle line is denoted in the first line containing the number .
The distances between the adjacent Bus stops are denoted in the second line containing b integers x1, x2, ..., xb.
The numbers of Bus stops are denoted in the third line containing two numbers s and t, the value of s & t. Find the shortest distance.
Constraints

b(3 ≤ b ≤ 100)
(1 ≤ xi ≤ 100)
(1 ≤ s, t ≤ b)
Output Format

Find and print the Shortest distances between s & t.

Sample Input :
4
2 3 4 9
1 3
Sample Output 0

5
Sample Input 1

4
5 8 2 100
4 1
Sample Output 1

15

What I have tried:

I HAVE TRIED THE JAV CODE AS:
Java
import java.io.*;
        import java.util.*;
        import java.text.*;
        import java.math.*;
        import java.util.regex.*;

        public class Solution {
            public static void main(String args[] ) throws Exception {
                /* Enter your code here. Read input from STDIN. Print output to STDOUT */
             Scanner s = new Scanner(System.in);
               
              }
                    if(s1>t){
                      for(int d = s1;d<=b;d++){
                      xi[d]++;
                      dist1=xi[d];   
                        
                    }
                         for(int e = s1-1;e>=t;e--){
                      xi[e]++;
                      dist2=xi[e];
                         }
                    if(dist1<dist2){
                      System.out.println(dist1);
                  }else
                  {
                      System.out.println(dist2);
                  }
                  
                    }
                  
              }
            }
        }



WHEN I GO THROUGH THE TEST CASES I DONT KNOW WHERE AM I GOING WRONG...
Posted
Updated 21-Jun-22 4:54am
v2

1 solution

You might want to go through your code and make sure everything is indented properly. Your main method only has a single line of code in it. Everything else is ignored and should cause compile failures.
Java
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

    public class Solution {
        public static void main(String args[] ) throws Exception {
            /* Enter your code here. Read input from STDIN. Print output to STDOUT */
            Scanner s = new Scanner(System.in);
        }

        if(s1>t) {
            for(int d = s1;d<=b;d++) {
                xi[d]++;
                dist1=xi[d];   
            }
            
            for(int e = s1-1;e>=t;e--) {
                xi[e]++;
                dist2=xi[e];
            }
            
            if(dist1<dist2) {
                System.out.println(dist1);
            }
            else {
                System.out.println(dist2);
            }
        }
    }
}     <--- This and the next brace are extras that need to be removed
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900