Minimum time to finish tasks without skipping two consecutive | Java program to find minimum time to finish tasks such that no two consecutive tasks are skipped . ; arr [ ] represents time taken by n given tasks ; Corner Cases ; Initialize value for the case when there is only one task in task list . First task is included ; First task is exluded ; Process remaining n - 1 tasks ; Time taken if current task is included . There are two possibilities ( a ) Previous task is also included ( b ) Previous task is not included ; Time taken when current task is not included . There is only one possibility that previous task is also included . ; Update incl and excl for next iteration ; Return maximum of two values for last task ; Driver code
import java . io . * ; class GFG { static int minTime ( int arr [ ] , int n ) { if ( n <= 0 ) return 0 ; int incl = arr [ 0 ] ; int excl = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int incl_new = arr [ i ] + Math . min ( excl , incl ) ; int excl_new = incl ; incl = incl_new ; excl = excl_new ; } return Math . min ( incl , excl ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 10 , 5 , 2 , 7 , 10 } ; int n1 = arr1 . length ; System . out . println ( minTime ( arr1 , n1 ) ) ; int arr2 [ ] = { 10 , 5 , 7 , 10 } ; int n2 = arr2 . length ; System . out . println ( minTime ( arr2 , n2 ) ) ; int arr3 [ ] = { 10 , 5 , 2 , 4 , 8 , 6 , 7 , 10 } ; int n3 = arr3 . length ; System . out . println ( minTime ( arr3 , n3 ) ) ; } }