Minimize total time taken by two persons to visit N cities such that none of them meet
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++
#include <bits/stdc++.h>
using namespace std;
void minimumTime( int * arr, int n)
{
int sum = 0;
int T = *max_element(arr, arr + n);
for ( int i = 0; i < n; i++) {
sum += arr[i];
}
cout << max(2 * T, sum);
}
int main()
{
int arr[] = { 2, 8, 3 };
int N = sizeof (arr) / sizeof (arr[0]);
minimumTime(arr, N);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void minimumTime( int [] arr, int n)
{
int sum = 0 ;
int T = Arrays.stream(arr).max().getAsInt();
for ( int i = 0 ; i < n; i++)
{
sum += arr[i];
}
System.out.println(Math.max( 2 * T, sum));
}
public static void main(String[] args)
{
int arr[] = { 2 , 8 , 3 };
int N = arr.length;
minimumTime(arr, N);
}
}
|
Python3
def minimumTime(arr, n):
sum = 0
T = max (arr)
for i in range (n):
sum + = arr[i]
print ( max ( 2 * T, sum ))
if __name__ = = '__main__' :
arr = [ 2 , 8 , 3 ]
N = len (arr)
minimumTime(arr, N)
|
C#
using System;
using System.Linq;
class GFG
{
static void minimumTime( int [] arr, int n)
{
int sum = 0;
int T = arr.Min();
for ( int i = 0; i < n; i++)
{
sum += arr[i];
}
Console.WriteLine(Math.Max(2 * T, sum));
}
public static void Main(String[] args)
{
int []arr = { 2, 8, 3 };
int N = arr.Length;
minimumTime(arr, N);
}
}
|
Javascript
<script>
function minimumTime(arr , n) {
var sum = 0;
var T =Math.max(...arr);
for (i = 0; i < n; i++) {
sum += arr[i];
}
document.write(Math.max(2 * T, sum));
}
var arr = [ 2, 8, 3 ];
var N = arr.length;
minimumTime(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
02 Feb, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...