Home > Blockchain >  Get the common value between 2 vectors from the reverse order
Get the common value between 2 vectors from the reverse order

Time:10-22

So I need the common value between the 2 vectors from the end or in the reverse order. AND once I find the common value, I don't care whether there exists more common values or not.

Okay, It sounded pretty easy to me also at first, but now I've looked at more vector syntaxes than ever in a single day. So, finally I'm asking for help here.

Example:
Input

v1: 32, 64, 90
v2: 32, 64, 78

Output:

64

First try which I took from python syntax:

    vector<int>::iterator it1 ;
    vector<int>::iterator it2 ;
    
    for(it1 = v1.end() && it2 = v2.end(); it1!=v1.begin() && it2!=v2.begin(); it1-- && it2--){
     if (*it1==*it2) {    //please ignore if the inner syntax is wrong
        res=*it1;        //because I already tried a lot of syntaxes so really confused right now
        break;           //Please look at the for loop
     }
    
    

This, didn't work. So, I thought maybe I'm not iterating 2 vectors correctly. I checked out google for the same and stackoverflow site showed me to use zip which doesn't work on my contest page.

Next thing, I tried, was,

vector<int>::iterator it1 = v1.end();
    vector<int>::iterator it2 = v2.end();
    
    while(it1 != v1.begin() || it2 != v2.begin())
    {
        if (*it1 == *it2){
            cout<<*it1<<" "<<*it2<<endl;  //Output for the same is below
            res=*it1;
            break; 
            
        }
          
        if(it1 != v1.begin())
        {
              it1;
        }
        if(it2 != v2.begin())
        {
              it2;
        }
        
    }
    

It compiles successfully, and outputs,

0 0 

So, Can you please help me with the same? Whether there exists a built-in function that could help me or there's a minor error and I can go ahead by improving that

Thanks in advance.

CodePudding user response:

With reverse iterators, iterating backwards is as simple as iterating forwards. You just need a loop that increments two iterators in parallel and check whether the elements are equal:

#include <iostream>
#include <vector>

int main() {
    std::vector<int> v1{32, 64, 90};
    std::vector<int> v2{32, 64, 78};
    auto it1 = v1.rbegin();
    auto it2 = v2.rbegin();
    auto it_result = v1.rend();
    for (; it1 != v1.rend() && it2 != v2.rend();   it1,  it2){
        if (*it1 == *it2) {
            it_result = it1;
            break;
        }
    }
    if (it_result != v1.rend()) std::cout << *it_result;
}

CodePudding user response:

As suggested in the comments, usage of std::mismatch with the appropriate predicate and usage of reverse iterators could be used:

#include <algorithm>
#include <vector>
#include <iostream>

int main()
{
    std::vector<int> v1 = { 32, 100, 64, 90, 12 };
    std::vector<int> v2 = { 32, 100, 64, 78, 120 };
    auto pr = std::mismatch(v1.rbegin(), v1.rend(), v2.rbegin(), 
                            [&](int n1, int n2) { return n1 != n2; });
    std::cout << *(pr.first) << "\n";
}

Output:

64

Basically we told std::mismatch to skip over non-equal values until you find equal ones. The "mismatch" in this case are the equal values.

  •  Tags:  
  • c
  • Related