Given an array** arr[]** of size **N**, where **arr[i]** is the time required to visit **i ^{th}** 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:16Explanation:

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
**i**city takes the longest time^{th}**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
**1**person visits the^{st}**i**city, then in^{th}**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
**2**person will have to wait to visit the^{nd}**i**. Then, the total time required will be^{th}city**2 * T**as the**2**person will be able to visit the^{nd}**i**city only if the first person comes out.^{th} - 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 meets` `void` `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 Code` `int` `main()` `{` ` ` `int` `arr[] = { 2, 8, 3 };` ` ` `int` `N = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]);` ` ` `// Function Call` ` ` `minimumTime(arr, N);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## 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 meets` `static` `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 code` `public` `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` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach` `# Function to find the minimum time` `# to visit all the cities such that` `# both the person never meets` `def` `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]` ` ` `# Prmaximum of 2*T and sum` ` ` `print` `(` `max` `(` `2` `*` `T, ` `sum` `))` `# Driver Code` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` `arr ` `=` `[` `2` `, ` `8` `, ` `3` `]` ` ` `N ` `=` `len` `(arr)` ` ` `# Function Call` ` ` `minimumTime(arr, N)` ` ` `# This code is contributed by mohit kumar 29` |

*chevron_right*

*filter_none*

## 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 meets` `static` `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 code` `public` `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` |

*chevron_right*

*filter_none*

**Output:**

16

**Time Complexity:** O(N)**Auxiliary Space:** O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.