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.
Examples:
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.
Naive Method:
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.
C++
#include <bits/stdc++.h>
using namespace std;
void find(vector<string> list1, vector<string> list2)
{
vector<string> res;
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[i] == list2[sum - i])
res.push_back(list1[i]);
if (res.size() > 0)
break ;
}
for ( int i = 0; i < res.size(); i++)
cout << res[i] << " " ;
}
int main()
{
vector<string> list1;
list1.push_back( "GeeksforGeeks" );
list1.push_back( "Udemy" );
list1.push_back( "Coursera" );
list1.push_back( "edX" );
vector<string> list2;
list2.push_back( "Codecademy" );
list2.push_back( "Khan Academy" );
list2.push_back( "GeeksforGeeks" );
find(list1, list2);
return 0;
}
|
Java
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);
}
}
|
Python3
def find(list1, list2):
res = []
max_possible_sum = len (list1) + len (list2) - 2
for sum in range (max_possible_sum + 1 ):
for i in range ( sum + 1 ):
if (i < len (list1) and
( sum - i) < len (list2) and
list1[i] = = list2[ sum - i]):
res.append(list1[i])
if ( len (res) > 0 ):
break
for i in range ( len (res)):
print (res[i], end = " " )
list1 = []
list1.append( "GeeksforGeeks" )
list1.append( "Udemy" )
list1.append( "Coursera" )
list1.append( "edX" )
list2 = []
list2.append( "Codecademy" )
list2.append( "Khan Academy" )
list2.append( "GeeksforGeeks" )
find(list1, list2)
|
C#
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);
}
}
|
Javascript
<script>
function find(list1, list2)
{
let res = [];
let max_possible_sum = list1.length + list2.length - 2;
for (let sum = 0; sum <= max_possible_sum ; sum++)
{
for (let i = 0; i <= sum; i++)
if (i < list1.length &&
(sum - i) < list2.length &&
list1[i] == list2[sum - i])
res.push(list1[i]);
if (res.length > 0)
break ;
}
for (let i = 0; i < res.length; i++)
document.write(res[i]+ " " );
}
let list1 = [];
list1.push( "GeeksforGeeks" );
list1.push( "Udemy" );
list1.push( "Coursera" );
list1.push( "edX" );
let list2= [];
list2.push( "Codecademy" );
list2.push( "Khan Academy" );
list2.push( "GeeksforGeeks" );
find(list1, list2);
</script>
|
Output:
GeeksforGeeks
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.
Using Hash:
- 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.
C++
#include <bits/stdc++.h>
using namespace std;
void find(vector<string> list1, vector<string> list2)
{
unordered_map<string, int > map;
for ( int i = 0; i < list1.size(); i++)
map[list1[i]] = i;
vector<string> res;
int minsum = INT_MAX;
for ( int j = 0; j < list2.size(); j++)
{
if (map.count(list2[j]))
{
int sum = j + map[list2[j]];
if (sum < minsum)
{
minsum = sum;
res.clear();
res.push_back(list2[j]);
}
else if (sum == minsum)
res.push_back(list2[j]);
}
}
for ( int i = 0; i < res.size(); i++)
cout << res[i] << " " ;
}
int main()
{
vector<string> list1;
list1.push_back( "GeeksforGeeks" );
list1.push_back( "Udemy" );
list1.push_back( "Coursera" );
list1.push_back( "edX" );
vector<string> list2;
list2.push_back( "Codecademy" );
list2.push_back( "Khan Academy" );
list2.push_back( "GeeksforGeeks" );
find(list1, list2);
return 0;
}
|
Java
import java.util.*;
class GFG
{
static void find(Vector<String> list1, Vector<String> list2)
{
Map<String, Integer> map = new HashMap<>();
for ( int i = 0 ; i < list1.size(); i++)
map.put(list1.get(i), i);
Vector<String> res = new Vector<String>();
int minsum = Integer.MAX_VALUE;
for ( int j = 0 ; j < list2.size(); j++)
{
if (map.containsKey(list2.get(j)))
{
int sum = j + map.get(list2.get(j));
if (sum < minsum)
{
minsum = sum;
res.clear();
res.add(list2.get(j));
}
else if (sum == minsum)
res.add(list2.get(j));
}
}
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<String>();
list1.add( "GeeksforGeeks" );
list1.add( "Udemy" );
list1.add( "Coursera" );
list1.add( "edX" );
Vector<String> list2 = new Vector<String>();
list2.add( "Codecademy" );
list2.add( "Khan Academy" );
list2.add( "GeeksforGeeks" );
find(list1, list2);
}
}
|
Python3
import sys
def find(list1, list2):
Map = {}
for i in range ( len (list1)):
Map [list1[i]] = i
res = []
minsum = sys.maxsize
for j in range ( len (list2)):
if list2[j] in Map :
Sum = j + Map [list2[j]]
if ( Sum < minsum):
minsum = Sum
res.clear()
res.append(list2[j])
else if ( Sum = = minsum):
res.append(list2[j])
print ( * res, sep = " " )
list1 = []
list1.append( "GeeksforGeeks" )
list1.append( "Udemy" )
list1.append( "Coursera" )
list1.append( "edX" )
list2 = []
list2.append( "Codecademy" )
list2.append( "Khan Academy" )
list2.append( "GeeksforGeeks" )
find(list1, list2)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static void find(List<String> list1, List<String> list2)
{
Dictionary<String, int > map = new Dictionary<String, int >();
for ( int i = 0; i < list1.Count; i++)
map.Add(list1[i], i);
List<String> res = new List<String>();
int minsum = int .MaxValue;
for ( int j = 0; j < list2.Count; j++)
{
if (map.ContainsKey(list2[j]))
{
int sum = j + map[list2[j]];
if (sum < minsum)
{
minsum = sum;
res.Clear();
res.Add(list2[j]);
}
else if (sum == minsum)
res.Add(list2[j]);
}
}
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);
}
}
|
Javascript
<script>
function find(list1, list2)
{
let map = new Map();
for (let i = 0; i < list1.length; i++)
map.set(list1[i], i);
let res = [];
let minsum = Number.MAX_VALUE;
for (let j = 0; j < list2.length; j++)
{
if (map.has(list2[j]))
{
let sum = j + map.get(list2[j]);
if (sum < minsum)
{
minsum = sum;
res = [];
res.push(list2[j]);
}
else if (sum == minsum)
res.push(list2[j]);
}
}
for (let i = 0; i < res.length; i++)
document.write(res[i] + " " );
}
let list1 = [];
list1.push( "GeeksforGeeks" );
list1.push( "Udemy" );
list1.push( "Coursera" );
list1.push( "edX" );
let list2 = [];
list2.push( "Codecademy" );
list2.push( "Khan Academy" );
list2.push( "GeeksforGeeks" );
find(list1, list2);
</script>
|
Output:
GeeksforGeeks
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 write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. 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.