Ram and Shyam want to choose a website to learn programming and they both have a list of favorite websites represented by strings.
You need to help them find out their common interest with the least index sum. If there is a choice tie between answers, print all of them with no order requirement. Assume there always exists an answer.
Input : ["GeeksforGeeks", "Udemy", "Coursera", "edX"] ["Codecademy", "Khan Academy", "GeeksforGeeks"] Output : "GeeksforGeeks" Explanation : GeeksforGeeks is the only common website in two lists Input : ["Udemy", "GeeksforGeeks", "Coursera", "edX"] ["GeeksforGeeks", "Udemy", "Khan Academy", "Udacity"] Output : "GeeksforGeeks" "Udemy" Explanation : There are two common websites and index sum of both is same.
The idea is to try all index sums from 0 to sum of sizes. For every sum, check if there are pairs with given sum. Once we find one or more pairs, we print them and return.
Time Complexity : O((l1+l2)2 *x), where l1 and l2 are the lengths of list1 and list2 respectively and x refers to string length.
Auxiliary Space : O(l*x), where x refers to length of resultant list and l is length of maximum size word.
- Traverse over the list1 and create an entry for index each element of list1 in a Hash Table.
- Traverse over list2 and for every element, check if the same element already exists as a key in the map. If so, it means that the element exists in both the lists.
- Find out the sum of indices corresponding to common element in the two lists. If this sum is lesser than the minimum sum obtained till now, update the resultant list.
- If the sum is equal to the minimum sum obtained till now, put an extra entry corresponding to the element in list2 in the resultant list.
- Find common elements in three linked lists
- Remove minimum number of elements such that no common element exist in both array
- Count items common to both the lists but with different prices
- Flip minimum signs of array elements to get minimum sum of positive elements possible
- Common elements in all rows of a given matrix
- Find minimum shift for longest common prefix
- Minimum replacements such that the difference between the index of the same characters is divisible by 3
- Minimum bit changes in Binary Circular array to reach a index
- Find distinct elements common to all rows of a matrix
- Find smallest range containing elements from k lists
- Minimum number of elements to be removed so that pairwise consecutive elements are same
- Count of index pairs with equal elements in an array
- Find the character in first string that is present at minimum index in second string
- Difference between Inverted Index and Forward Index
- Sum of all minimum frequency elements in Matrix
Time Complexity : O(l1+l2), where l1 and l2 are the lengths of list1 and list2 respectively.
Auxiliary Space : O(l1*x), where x refers to length of resultant list and l is length of maximum size word.
This article is contributed by Aakash Pal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.