Two pointer approach ... RUNTIMEERROR TWODOGS

PROBLEM CODE : TWODOGS
LINK : Contest Page | CodeChef
solution L

#include <bits/stdc++.h>
using namespace std;
int main()
{ // MAX
    long long int n, k;
    cin >> n >> k;
    unordered_map<long long int, vector<long long int>> tejus;
    unordered_map<long long int, bool> exist;
    for (long long int i = 0; i < n; i++)
    {
        long long int x;
        cin >> x;
        exist[x] = true;
        tejus[x].push_back(i);
    }
    long long int answer = INT_MAX;
    bool isthere = false;
    for (auto it = exist.begin(); it != exist.end(); it++)
    {
        long long int phela = it->first;
        long long int doosra = k - phela;
        if (exist[doosra])
        {
            if (phela == doosra)
            {
                if (tejus[phela].size() > 1)
                {
                    /*
                                        ( for equal phela & doosra )
                    index1 index2 ...             indexsecondlast indexlast
                    index1 , index2
                    index1 , n-indexlast-1
                    n-indexlast-1 , n-indexsecondlast-1        */
                    long long int index1 = tejus[phela][0];
                    long long int index2 = tejus[phela][1];
                    long long int indexsecondlast = tejus[phela][tejus[phela].size() - 2];
                    long long int indexlast = tejus[phela][tejus[phela].size() - 1];
                    long long int temp1 = max(index1, index2);
                    if (answer > temp1)
                    {
                        answer = temp1;
                        isthere = true;
                    }
                    temp1 = max(index1, n - 1 - indexlast);
                    if (answer > temp1)
                    {
                        answer = temp1;
                        isthere = true;
                    }
                    temp1 = max(n - 1 - indexlast, n - 1 - indexsecondlast);
                    if (answer > temp1)
                    {
                        answer = temp1;
                        isthere = true;
                    }
                }
            }
            else
            {
                /*
                    for different phela doosra
                    phelaindex1 ...      phelaindexlast
                    doosraindex1 ....    doosraindexlast
                    phelaindex1 , doosraindex1
                    phelaindex1 , n-1-doosraindexlast
                    n-1-phelaindexlast , doosraindex1
                    n-1-phelaindexlast , n-1-doosraindex1
                */
                long long int phelaindex1 = tejus[phela][0];
                long long int phelaindexlast = tejus[phela][tejus[phela].size() - 1];
                long long int doosraindex1 = tejus[doosra][0];
                long long int doosraindexlast = tejus[doosra][tejus[doosra].size() - 1];
                long long int temp = max(phelaindex1, doosraindex1);
                if (answer > temp)
                {
                    answer = temp;
                    isthere = true;
                }
                temp = max(phelaindex1, n - 1 - doosraindexlast);
                if (answer > temp)
                {
                    answer = temp;
                    isthere = true;
                }
                temp = max(n - 1 - phelaindexlast, doosraindex1);
                if (answer > temp)
                {
                    answer = temp;
                    isthere = true;
                }
                temp = max(n - 1 - phelaindexlast, n - 1 - doosraindex1);
                if (answer > temp)
                {
                    answer = temp;
                    isthere = true;
                }
            }
        }
    }
    if (!isthere)
    {
        cout << "-1\n";
    }
    else
    {
        cout << answer + 1 << "\n";
    }
}

i am getting runtime error…
someone plx help…

https://www.codechef.com/viewsolution/48107118

Out of bounds access on Sample Input:

[simon@simon-laptop][08:27:04]
[~/devel/hackerrank/otherpeoples]>./compile-latest-cpp.sh 
Compiling tejvirus0003-TWODOGS.cpp
+ g++ -std=c++14 tejvirus0003-TWODOGS.cpp -O3 -g3 -Wall -Wextra -Wconversion -DONLINE_JUDGE -D_GLIBCXX_DEBUG -fsanitize=undefined -ftrapv
+ set +x
Successful
[simon@simon-laptop][08:27:19]
[~/devel/hackerrank/otherpeoples]>echo "5 5
2 4 9 2 5" | ./a.out
/usr/include/c++/7/debug/vector:417:
Error: attempt to subscript container with out-of-bounds index 0, but 
container only holds 0 elements.

Objects involved in the operation:
    sequence "this" @ 0x0x5637eec8b240 {
      type = std::__debug::vector<long long, std::allocator<long long> >;
    }
Aborted (core dumped)

This line, according to gdb:

    long long int phelaindex1 = tejus[phela][0];
1 Like

let me see

but , i am accessing the very first index and since exist map shows the value of true to it exists… its still giving the outofbound…
shocking

#include <bits/stdc++.h>
using namespace std;
int main()
{ // MAX
    long long int n, k;
    cin >> n >> k;
    unordered_map<long long int, vector<long long int>> tejus;
    unordered_map<long long int, bool> exist;
    for (long long int i = 0; i < n; i++)
    {
        long long int x;
        cin >> x;
        exist[x] = true;
        tejus[x].push_back(i);
    }
    long long int answer = INT_MAX;
    bool isthere = false;
    for (auto it = exist.begin(); it != exist.end(); it++)
    {
        long long int phela = it->first;
        long long int doosra = k - phela;
        if (exist[doosra])
        {
            if (phela == doosra)
            {
                if (tejus[phela].size() > 1)
                {
                    /*
                                        ( for equal phela & doosra )
                    index1 index2 ...             indexsecondlast indexlast
                    index1 , index2
                    index1 , n-indexlast-1
                    n-indexlast-1 , n-indexsecondlast-1        */
                    long long int index1 = tejus[phela][0];
                    long long int index2 = tejus[phela][1];
                    long long int indexsecondlast = tejus[phela][tejus[phela].size() - 2];
                    long long int indexlast = tejus[phela][tejus[phela].size() - 1];
                    long long int temp1 = max(index1, index2);
                    if (answer > temp1)
                    {
                        answer = temp1;
                        isthere = true;
                    }
                    temp1 = max(index1, n - 1 - indexlast);
                    if (answer > temp1)
                    {
                        answer = temp1;
                        isthere = true;
                    }
                    temp1 = max(n - 1 - indexlast, n - 1 - indexsecondlast);
                    if (answer > temp1)
                    {
                        answer = temp1;
                        isthere = true;
                    }
                }
            }
            else
            {
                /*
                    for different phela doosra
                    phelaindex1 ...      phelaindexlast
                    doosraindex1 ....    doosraindexlast
                    phelaindex1 , doosraindex1
                    phelaindex1 , n-1-doosraindexlast
                    n-1-phelaindexlast , doosraindex1
                    n-1-phelaindexlast , n-1-doosraindex1
                */
                if(tejus[phela].size()>0 && tejus[doosra].size()>0){
                long long int phelaindex1 = tejus[phela][0];
                long long int phelaindexlast = tejus[phela][tejus[phela].size() - 1];
                long long int doosraindex1 = tejus[doosra][0];
                long long int doosraindexlast = tejus[doosra][tejus[doosra].size() - 1];
                long long int temp = max(phelaindex1, doosraindex1);
                if (answer > temp)
                {
                    answer = temp;
                    isthere = true;
                }
                temp = max(phelaindex1, n - 1 - doosraindexlast);
                if (answer > temp)
                {
                    answer = temp;
                    isthere = true;
                }
                temp = max(n - 1 - phelaindexlast, doosraindex1);
                if (answer > temp)
                {
                    answer = temp;
                    isthere = true;
                }
                temp = max(n - 1 - phelaindexlast, n - 1 - doosraindex1);
                if (answer > temp)
                {
                    answer = temp;
                    isthere = true;
                }
            }}
        }
    }
    if (!isthere)
    {
        cout << "-1\n";
    }
    else
    {
        cout << answer + 1 << "\n";
    }
}

now its GIVING wa…
please help in this