A guy with a mental problem | Practice | GeeksforGeeks

A guy has to reach his home and does not want to be late. He takes train to reach home. He has a mental illness, so he always switches train at every station.
For eg: If he starts with train A then at station 2 he will switch his train to train B and so on. Similarly, if he starts with train B then he will switch to train A at station 2 and so on. Please help him to find the minimum total time he would take to reach his home.
 
Example 1:
Input:
N = 3
A[] = {2, 1, 2}
B[] = {3, 2, 1}
Output:
5
Explanation:
If he chose train A initially, then time to
reach home will be : 2 + 2 + 2 = 6 
If he Chose train B initially, then time to
reach home will be : 3 + 1 + 1 = 5
5 is minimum hence the answer.
 
Your Task:  
You don't need to read input or print anything. Your task is to complete the function minTime() which takes the array A[], B[] and its size N as inputs and returns the minimum total time in seconds to reach home.

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 ≤ N ≤ 106
1 ≤ Ai, Bi ≤ 106
0
arvind16yadav2 weeks ago
/*JAVA CODE*/
/*Time : 0.7 / 7.3  (Ten time faster)*/
public long minTime(long a[], long b[], long n) {       // Your code goes here               long time_a = 0;       long time_b = 0;              // time for time_a i.e, start with a       for(long i = 0; i<n; i+=2){           time_a += a[(int)i];       }              for(long i=1; i<n; i+=2){           time_a += b[(int)i];       }              // time for time_b i.e,  start with b       for(long i = 0; i<n; i+=2){           time_b += b[(int)i];       }              for(long i=1; i<n; i+=2){           time_b += a[(int)i];       }              return (time_a < time_b) ? time_a : time_b;   }
0
divyabhatia5191 month ago
JAVA Solution :-
 
public long minTime(long a[], long b[], long n) {       // Your code goes here        long a1 = 0 , b1 = 0;       for(int i = 0; i < n; i++){           if(i % 2 == 0){               a1 += a[i];               b1 += b[i];           }else{               a1 += b[i];               b1 += a[i];           }       }       return Math.min(a1 , b1);          }
0
princejee20191 month ago
C++ || EASY TO UNDERSTAND
long long minTime(long long a[], long long b[], long long n){       long long ans =0,ans2=0;       for(int i =0;i<n;i++){           if(i%2==0) ans+=a[i];           else ans+=b[i];       }       for(int i =0;i<n;i++){           if(i%2!=0) ans2+=a[i];           else ans2+=b[i];       }       long long mi = min(ans,ans2);       return mi;   }
0
hasnainraza1998hr2 months ago
C++
long long minTime(long long a[], long long b[], long long n)   {       long long ATime=0;       long long BTime=0;       for(int i=0;i<n;i++){          if(i%2==0){              ATime += a[i];              BTime += b[i];          }          else{              ATime += b[i];              BTime += a[i];          }       }       return min(ATime,BTime);   }
0
mananpushkar3 months ago
long long minTime(long long a[], long long b[], long long n)   {       long long sum1=0;       long long sum2=0;       sum1+=a[0];       sum2+=b[0];       for(long long int i=1;i<n;i++)       {           if(i%2==0)           {           sum1+=a[i];           sum2+=b[i];           }           else           {           sum1+=b[i];           sum2+=a[i];           }       }       if(sum1<sum2)       return sum1;       else       return sum2;   }
 
0
lasthoneybadger3 months ago
// Your code goes here 
        long a1 = 0 , b1 = 0;
        for(int i = 0; i < n; i++){
            if(i % 2 == 0){
                a1 += a[i];
                b1 += b[i];
            }else{
                a1 += b[i];
                b1 += a[i];
            }
        }
        return Math.min(a1 , b1);
0
mayank20214 months ago
C++ : in one for Looplong long minTime(long long a[], long long b[], long long n)   {              long long sum_first=0, sum_second=0;              for(int i=0; i<n; )       {                      sum_first = sum_first + a[i];           sum_second=sum_second + b[i];                      if(i+1<n)           {               sum_first = sum_first + b[i+1];           sum_second=sum_second + a[i+1];           }                      i=i+2;                             }              //cout<<sum_first<<" "<<sum_second<<endl;              return (sum_first< sum_second)? sum_first:sum_second;   }
0
user_i9s04 months ago
 // Your code goes here       long long s=0,t=0,u=0,v=0;       for(long long i=0;i<n;i=i+2){           s=s+a[i];           u=u+b[i];       }       for(long long j=1;j<n;j=j+2){           t=t+b[j];           v=v+a[j];       }       long long total1=s+t;       long long total2=u+v;       if(total1>total2){           return total2;       }       return total1;
0
debujjal46 months ago
long long sum1=0;
       long long sum2=0;
       for(int i=0;i<n;i++){
           if(i%2==0){
               sum1+=a[i];
               sum2+=b[i];
           }else{
               sum1+=b[i];
               sum2+=a[i];
           }
       }
       if(sum1<sum2){
           return sum1;
       }else{
           return sum2;
       }
0
sagarmish12346 months ago
C++ 0.4 solution (ternary magic)
long long minTime(long long a[], long long b[], long long n)
    {
        long long A=0,B=0;
        int flag=1;
        for(int i=0;i<n;i++){
            A+= flag?a[i]:b[i];
            B+= flag?b[i]:a[i];
            flag = flag?0:1;
        }
        return min(A,B);
        // Your code goes here  
    }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.