Pushing elements in priority queue where the key is f [ i ]
p . add ( new Pair ( f [ i ] , s [ i ] ) ) ; } Pair it = p . poll ( ) ; int start = it . second ; int end = it . first ; ans . add ( new Pair ( start , end ) ) ; while ( ! p . isEmpty ( ) ) { Pair itr = p . poll ( ) ; if ( itr . second >= end ) { start = itr . second ; end = itr . first ; ans . add ( new Pair ( start , end ) ) ; } } System . out . println ( "Following Activities should be selected. NEW_LINE"); for ( Pair itr : ans ) { System . out . println ( " Activity ▁ started ▁ at : ▁ " + itr . first + " ▁ and ▁ ends ▁ at ▁ " + itr . second ) ; } }