Minimum Index Sum for Common Elements of Two Lists |  ; Function to print common Strings with minimum index sum ; resultant list ; iterating over sum in ascending order ; iterating over one list and check index ( Corresponding to given sum ) in other list ; put common Strings in resultant list ; if common String found then break as we are considering index sums in increasing order . ; print the resultant list ; Driver code ; Creating list1 ; Creating list2
import java . util . * ; class GFG { static void find ( Vector < String > list1 , Vector < String > list2 ) { Vector < String > res = new Vector < > ( ) ; int max_possible_sum = list1 . size ( ) + list2 . size ( ) - 2 ; for ( int sum = 0 ; sum <= max_possible_sum ; sum ++ ) { for ( int i = 0 ; i <= sum ; i ++ ) if ( i < list1 . size ( ) && ( sum - i ) < list2 . size ( ) && list1 . get ( i ) == list2 . get ( sum - i ) ) res . add ( list1 . get ( i ) ) ; if ( res . size ( ) > 0 ) break ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { Vector < String > list1 = new Vector < > ( ) ; list1 . add ( " GeeksforGeeks " ) ; list1 . add ( " Udemy " ) ; list1 . add ( " Coursera " ) ; list1 . add ( " edX " ) ; Vector < String > list2 = new Vector < > ( ) ; list2 . add ( " Codecademy " ) ; list2 . add ( " Khan ▁ Academy " ) ; list2 . add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }