Skip to content

Instantly share code, notes, and snippets.

@cypher-nullbyte
Created May 14, 2020 18:10
Show Gist options
  • Save cypher-nullbyte/39a229fe823d3488427faa54d6a2a8bf to your computer and use it in GitHub Desktop.
Save cypher-nullbyte/39a229fe823d3488427faa54d6a2a8bf to your computer and use it in GitHub Desktop.
Vpropel VIT | POD | 14/05/2020 | Train routes | 44
Train routes
A train stops on n stations numbered from 0 to n - 1 that form a circle.
We know the distance between all pairs of neighboring stations where distance[i] is the distance between the station number
i and (i + 1) % n.
The train goes along both directions i.e. clockwise and counterclockwise.
Return the shortest distance between the given start and destination stations.
Example:-
Input: distance = [1,2,3,4], start = 0, destination = 3
Output: 4
Explanation: Distance between 0 and 3 is 6 or 4, minimum is 4.
Input format:-
Number of stations(n)
Next n lines – distances
Starting station
Destination station
Output format:-
Minimum distance between start and destination
41 19BCE1082 G O NARENDRA
42 19BCE2643 SANDESH DHUNGANA
43 19BCE0345 ANURAG PANDEY
44 19BCI0016 CHIRANJEET SINGH
45 19BCE1773 PRIYA
46 19BCI0083 NAMIT GUPTA
47 19MIS0308 U A ABHILASH
#include<stdio.h>
int min(int a,int b)
{
return (a<b)?a:b;
}
int Mindist(int arr[],int n,int st,int end)
{
int tot[2]={0,0};
int i=st;
while(i!=end)
{
tot[0]+=arr[i];
i=(i+1)%n;
}
while(i!=st)
{
tot[1]+=arr[i];
i=(i+1)%n;
}
return min(tot[0],tot[1]);
}
int main()
{
int n;
scanf("%d",&n);
int distance[n];
for(int i=0;i<n;i++)
{
scanf("%d",&distance[i]);
}
int start,end;
scanf("%d%d",&start,&end);
printf("%d",Mindist(distance,n,start,end));
return 0;
}
// O(n)
#include<iostream>
#include<algorithm>
int Mindist(int arr[],int n,int st,int end)
{
int tot[2]={0};
int i=st;
while(i!=end)
{
tot[0]+=arr[i];
i=(i+1)%n;
}
while(i!=st)
{
tot[1]+=arr[i];
i=(i+1)%n;
}
return std::min(tot[0],tot[1]);
}
int main()
{
int n;
std::cin>>n;
int distance[n];
for(int i=0;i<n;i++)
{
std::cin>>distance[i];
}
int start,end;
std::cin>>start>>end;
std::cout<<Mindist(distance,n,start,end);
}
import java.util.Scanner;
class MDist
{
private static int min(int a,int b)
{
return (a<b)?a:b;
}
public static int Mindist(int arr[],int n,int st,int end)
{
int tot[]=new int[2];
int i=st;
while(i!=end)
{
tot[0]+=arr[i];
i=(i+1)%n;
}
while(i!=st)
{
tot[1]+=arr[i];
i=(i+1)%n;
}
return min(tot[0],tot[1]);
}
}
public class Main
{
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
int n=s.nextInt();
int stations[]=new int[n];
for(int i=0;i<n;i++)
stations[i]=s.nextInt();
int start=s.nextInt();
int end=s.nextInt();
System.out.print(MDist.Mindist(stations,n,start,end));
}
}
n=int(input())
l=[]
for i in range(n):
l.append(int(input()))
start=int(input())
end=int(input())
tot=[0,0]
i=start
while i!=end:
tot[0]+=l[i]
i=(i+1)%n
while i!=start:
tot[1]+=l[i]
i=(i+1)%n
print(min(tot[0],tot[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment