Given an array arr[] of size N, where arr[i] is the time required to visit ith city, the task is to find the minimum total time required to visit all N cities by two persons such that none of them meet in any of the cities.
Examples:
Input: arr[] = {2, 8, 3}
Output: 16
Explanation:
Visiting cities in below given order will take minimum time:
First person: 2nd city ? 1st city ? 3rd city
Second person: 1st city ? 3rd city ? 2nd city.Input: arr[]={1, 10, 6, 7, 5}
Output: 29
Approach: The given problem can be solved based on the following observations:
- Suppose ith city takes the longest time T to visit and the total time to visit all cities by one person is the sum of all array elements, say sum.
- If the 1st person visits the ith city, then in T time, the second person will visit other cities in that time, if possible.
- If the value T is at most (sum – T), then both people can visit the place individually in sum time.
- Otherwise, the 2nd person will have to wait to visit the ith city. Then, the total time required will be 2 * T as the 2nd person will be able to visit the ith city only if the first person comes out.
- Therefore, from the above observations, the answer will be the maximum of 2 * T and sum.
Therefore, from the above observations, find the sum of the array elements/a> and find the maximum element present in the array and print the maximum among twice the maximum element and the sum.
Below is the implementation of the above approach:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the minimum time// to visit all the cities such that// both the person never meetsvoid minimumTime(int* arr, int n){Â Â Â Â // Initialize sum as 0Â Â Â Â int sum = 0;Â
    // Find the maximum element    int T = *max_element(arr, arr + n);Â
    // Traverse the array    for (int i = 0; i < n; i++) {Â
        // Increment sum by arr[i]        sum += arr[i];    }Â
    // Print maximum of 2*T and sum    cout << max(2 * T, sum);}Â
// Driver Codeint main(){Â Â Â Â int arr[] = { 2, 8, 3 };Â Â Â Â int N = sizeof(arr) / sizeof(arr[0]);Â
    // Function Call    minimumTime(arr, N);Â
    return 0;} |
Java
// Java program for the above approach import java.util.*;class GFG{Â Â Â Â Â Â Â // Function to find the minimum time// to visit all the cities such that// both the person never meetsstatic void minimumTime(int[] arr, int n){Â Â Â Â Â Â Â // Initialize sum as 0Â Â Â Â int sum = 0;Â
    // Find the maximum element    int T = Arrays.stream(arr).max().getAsInt(); Â
    // Traverse the array    for (int i = 0; i < n; i++)     {Â
        // Increment sum by arr[i]        sum += arr[i];    }Â
    // Print maximum of 2*T and sum    System.out.println(Math.max(2 * T, sum));}   // Driver codepublic static void main(String[] args){    int arr[] = { 2, 8, 3 };    int N = arr.length;Â
    // Function Call    minimumTime(arr, N);}}Â
// This code is contributed by sanjoy_62 |
Python3
# Python3 program for the above approachÂ
# Function to find the minimum time# to visit all the cities such that# both the person never meetsdef minimumTime(arr, n):Â Â Â Â Â # Initialize sum as 0Â Â Â Â sum = 0Â
    # Find the maximum element    T = max(arr)Â
    # Traverse the array    for i in range(n):               # Increment sum by arr[i]        sum += arr[i]Â
    # Print maximum of 2*T and sum    print(max(2 * T, sum))Â
# Driver Codeif __name__ == '__main__':Â Â Â Â arr = [2, 8, 3]Â Â Â Â N = len(arr)Â
    # Function Call    minimumTime(arr, N)Â
    # This code is contributed by mohit kumar 29 |
C#
// C# program for the above approach using System;using System.Linq;class GFG{Â Â Â Â Â Â Â // Function to find the minimum time// to visit all the cities such that// both the person never meetsstatic void minimumTime(int[] arr, int n){Â Â Â Â Â Â Â // Initialize sum as 0Â Â Â Â int sum = 0;Â
    // Find the maximum element    int T = arr.Min(); Â
    // Traverse the array    for (int i = 0; i < n; i++)     {Â
        // Increment sum by arr[i]        sum += arr[i];    }Â
    // Print maximum of 2*T and sum    Console.WriteLine(Math.Max(2 * T, sum));}   // Driver codepublic static void Main(String[] args){    int []arr = { 2, 8, 3 };    int N = arr.Length;Â
    // Function Call    minimumTime(arr, N);}}Â
// This code is contributed by 29AjayKumar |
Javascript
<script>// javascript program for the above approach Â
    // Function to find the minimum time    // to visit all the cities such that    // both the person never meets    function minimumTime(arr , n) {Â
        // Initialize sum as 0        var sum = 0;Â
        // Find the maximum element        var T =Math.max(...arr);Â
        // Traverse the array        for (i = 0; i < n; i++) {Â
            // Increment sum by arr[i]            sum += arr[i];        }Â
        // Print maximum of 2*T and sum        document.write(Math.max(2 * T, sum));    }Â
    // Driver code    var arr = [ 2, 8, 3 ];    var N = arr.length;Â
    // Function Call    minimumTime(arr, N);Â
// This code is contributed by todaysgaurav</script> |
16
Â
Time Complexity: O(N)
Auxiliary Space: O(1)
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

… [Trackback]
[…] Read More Info here to that Topic: geeksforgeeks.org/minimize-total-time-taken-by-two-persons-to-visit-n-cities-such-that-none-of-them-meet/ […]
… [Trackback]
[…] Read More to that Topic: geeksforgeeks.org/minimize-total-time-taken-by-two-persons-to-visit-n-cities-such-that-none-of-them-meet/ […]