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
using System ; using System . Collections . Generic ; class GFG { static void find ( List < String > list1 , List < String > list2 ) { List < String > res = new List < String > ( ) ; int max_possible_sum = list1 . Count + list2 . Count - 2 ; for ( int sum = 0 ; sum <= max_possible_sum ; sum ++ ) { for ( int i = 0 ; i <= sum ; i ++ ) if ( i < list1 . Count && ( sum - i ) < list2 . Count && list1 [ i ] == list2 [ sum - i ] ) res . Add ( list1 [ i ] ) ; if ( res . Count > 0 ) break ; } for ( int i = 0 ; i < res . Count ; i ++ ) Console . Write ( res [ i ] + " ▁ " ) ; } public static void Main ( String [ ] args ) { List < String > list1 = new List < String > ( ) ; list1 . Add ( " GeeksforGeeks " ) ; list1 . Add ( " Udemy " ) ; list1 . Add ( " Coursera " ) ; list1 . Add ( " edX " ) ; List < String > list2 = new List < String > ( ) ; list2 . Add ( " Codecademy " ) ; list2 . Add ( " Khan ▁ Academy " ) ; list2 . Add ( " GeeksforGeeks " ) ; find ( list1 , list2 ) ; } }