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++ 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;
} |
// 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 |
# 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]
# Print maximum 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
|
// 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 |
<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)