How to Get Minimum Index Sum of Two Lists in C++?

  C & C ++ Interview Q&A

Dear readers, today in this article, the minimum index sum of two lists introduced in C ++?

If the two fences are Amal and Bimal want to choose a restaurant for dinner, now they both have a list of their favorite restaurants represented by wire. We have to help them find their common interest with at least the list index amount. If there is a choice tie between the different answers, then return them all without requiring any sorting.

Therefore, if the input is like [“ABC”, “PQR”, “MNO”, “XYZ”], and [“TUV”, “GHI”, “KLM”, “ABC”], then the output [will be] ” ABC “]

Example for Minimum Index Sum of Two Lists in C++

#include <bits/stdc++.h>
using namespace std;
void print_vector(vector<auto> v){
   cout << "[";
   for(int i = 0; i<v.size(); i++){
      cout << v[i] << ", ";
   }
   cout << "]"<<endl;
}
class MinIndexofTwoProg{
public:
   vector<string> findRestaurant(vector<string>& l1, vector<string>& l2) {
      map<int, vector<string> > mp;
      int least = INT_MAX;
      for (int i = 0; i < l1.size(); i++)
         for (int j = 0; j < l2.size(); j++)
            if (l1[i] == l2[j])
               mp[i + j].push_back(l1[i]);
      vector<string> res;
      auto it = mp.begin();
      res = it->second;
      return res;
   }
};
main(){
   MinIndexofTwoProg ob;
   vector<string> v = {"ABC","PQR","MNO","XYZ"}, v1 = {"TUV","GHI","KLM","ABC"};
   print_vector(ob.findRestaurant(v, v1));
}

Output

[ABC, ]

LEAVE A COMMENT