Reverser game hackerrank problem

Hello everyone.
Problem statement:
https://www.hackerrank.com/challenges/reverse-game/problem
I am not getting any output. Please help. I found a pattern I have mentioned below the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include<bits/stdc++.h>
using namespace std;

vector<int> newv(vector<int> vect, vector<int> v, int n){
    vector<int>::iterator it;
    it = v.begin();
    vect.assign(v.end(), v.end()+1);
    v.pop_back();

    vect.assign(it, it+1);
    v.erase(it);
    //++it;
    n--;
    if(n>1)
        return newv(vect,v,n);
    else
        return vect;
}

vector<int> alter(vector<int> v, int n){
    vector<int> vect;
    vect = newv(vect, v, n);
    return vect;
}

int main(){
    vector<int> arr{0,1,2,3,4};
    int k,n;
    n =  arr.size();
    cin>>k;
    vector<int> vecto;
    vecto = alter(arr, n);
    cout<<vecto[k];
    return 0;
}


01234
43210
40123
40321
40312

the final one has a pattern
last first 2ndlast 2nd third....and so on
TBH, you need to start learning about debuggers.

$ g++ -g -std=c++11 foo.cpp
$ gdb -q ./a.out
Reading symbols from ./a.out...done.
(gdb) b 33
(gdb) run
Starting program: ./a.out 
3

Program received signal SIGSEGV, Segmentation fault.
__memmove_ssse3_back () at ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:1550
1550	../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S: No such file or directory.
(gdb) bt
#0  __memmove_ssse3_back () at ../sysdeps/x86_64/multiarch/memcpy-ssse3-back.S:1550
#1  0x0000000000402b05 in std::__copy_move<true, true, std::random_access_iterator_tag>::__copy_m<int> (__first=0x6180f4, __last=0x6180f0, __result=0x6180f0) at /usr/include/c++/5/bits/stl_algobase.h:384
#2  0x00000000004028cf in std::__copy_move_a<true, int*, int*> (__first=0x6180f4, __last=0x6180f0, __result=0x6180f0) at /usr/include/c++/5/bits/stl_algobase.h:402
#3  0x00000000004025c1 in std::__copy_move_a2<true, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > > (
    __first=0, __last=0, __result=0) at /usr/include/c++/5/bits/stl_algobase.h:438
#4  0x0000000000401f15 in std::move<__gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >, __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > > > (__first=0, 
    __last=0, __result=0) at /usr/include/c++/5/bits/stl_algobase.h:504
#5  0x00000000004017ef in std::vector<int, std::allocator<int> >::_M_erase (this=0x7fffffffdc40, __position=0) at /usr/include/c++/5/bits/vector.tcc:145
#6  0x000000000040124f in std::vector<int, std::allocator<int> >::erase (this=0x7fffffffdc40, __position=0) at /usr/include/c++/5/bits/stl_vector.h:1147
#7  0x0000000000400c43 in newv (vect=std::vector of length 1, capacity 1 = {...}, v=std::vector of length 0, capacity 1, n=3) at foo.cpp:11
#8  0x0000000000400c8a in newv (vect=std::vector of length 1, capacity 1 = {...}, v=std::vector of length 1, capacity 3 = {...}, n=3) at foo.cpp:15
#9  0x0000000000400c8a in newv (vect=std::vector of length 1, capacity 1 = {...}, v=std::vector of length 3, capacity 5 = {...}, n=4) at foo.cpp:15
#10 0x0000000000400d78 in alter (v=std::vector of length 5, capacity 5 = {...}, n=5) at foo.cpp:22
#11 0x0000000000400edf in main () at foo.cpp:32
(gdb) 

In particular, you need to be VERY careful about your use of recursion.

Also, on the subject of recursion, given your constraints, having 105 copies of your vector, each with 105 elements, and 105 recursive invocations is a BAD place to be.
1 <= N <= 105
1 <= K <= N

If you think about it, you only need ONE copy of the vector.

If you think about it some more, you might realise that you don't need the vector at all.
"At the end of the game, Akhil will ask Akash the final position of any ball numbered . Akash will win the game, if he can answer. Help Akash."
This is meant to be a calculation you can do in your head in a fraction of a second, IF you understand the maths behind the question.

Topic archived. No new replies allowed.