# HackerEarth Transaction problem solution

In this HackerEarth Transaction, problem-solution BOB (Bank of Byteland) is the only bank in the land of byte and. Hence millions of transactions take place every day. You are currently the manager in charge of BOB. Being bored one day you took all the transactions of that day and asked your friend Codemonk to ask Q queries on the data. Codemonk being an exceptionally good coder gave you two numbers M and N. He then asks you to tell him Nth transaction having cost more than or equal to M maintaining the order of transaction if it exists else print 1. Can you answer all of his queries?

## HackerEarth Transaction problem solution.

`#include <bits/stdc++.h>#define sflld(n) scanf("%lld",&n)#define sfulld(n) scanf("%llu",&n)#define sfd(n) scanf("%d",&n)#define sfld(n) scanf("%ld",&n)#define sfs(n) scanf("%s",&n)#define ll long long#define ull unsigned long long int#define pflld(n) printf("%lld\n",n)#define pfd(n) printf("%d\n",n)#define pfld(n) printf("%ld\n",n)#define lt 2*idx#define rt 2*idx+1#define f first#define s secondusing namespace std;int arr[100050];struct node{    vector<ll>v;}st[4000000];void build(ll idx,ll a,ll b){    if(a==b)    {        st[idx].v.push_back(arr[a]);        return;    }    int m=(a+b)>>1;    build(lt,a,m);    build(rt,m+1,b);    int sz1=st[lt].v.size();    int sz2=st[rt].v.size();    int i=0,j=0;    while(i<sz1&&j<sz2)    {        if(st[lt].v[i]<=st[rt].v[j])        {            st[idx].v.push_back(st[lt].v[i]);            i++;        }        else        {            st[idx].v.push_back(st[rt].v[j]);            j++;        }    }    while(i<sz1)    {       st[idx].v.push_back(st[lt].v[i]);            i++;    }    while(j<sz2)    {        st[idx].v.push_back(st[rt].v[j]);        j++;    }}ll solve(ll cnt,ll idx,ll val){    if(st[idx].v.size()==1)        return st[idx].v[0];    ll lcnt=(st[lt].v.end()-lower_bound(st[lt].v.begin(),st[lt].v.end(),val));    ll rcnt=(st[rt].v.end()-lower_bound(st[rt].v.begin(),st[rt].v.end(),val));    if(lcnt+rcnt<cnt)        return -1;    if(lcnt<cnt)        return solve(cnt-lcnt,rt,val);    else        return solve(cnt,lt,val);}int main(){    int n,q,l,k,ct=0,i;    sfd(n);    sfd(q);    for(i=1;i<=n;i++)    {        sfd(arr[i]);    }    build(1,1,n);    while(q--)    {       ct=0;       sfd(l);       sfd(k);       pflld(solve(k,1,l));    }    return 0;}`

### Second solution

`#include <bits/stdc++.h>using namespace std;struct node{    int l, r, sz;} seg[100000*25];int nxt;int build(int begin, int end){    int root=nxt++;    if(begin==end)        return root;    int mid=(begin+end)/2;    seg[root].l=build(begin, mid);    seg[root].r=build(mid+1, end);    return root;}int update(int root, int begin, int end, int x, int v){    if(x<begin || end<x)        return root;    seg[nxt]=seg[root];    root=nxt++;    if(begin==end)        seg[root].sz+=v;    else    {        int mid=(begin+end)/2;        seg[root].l=update(seg[root].l, begin, mid, x, v);        seg[root].r=update(seg[root].r, mid+1, end, x, v);        seg[root].sz=seg[seg[root].l].sz+seg[seg[root].r].sz;    }    return root;}int query(int r1, int begin, int end, int k){    if(k>seg[r1].sz)        return -1;    while(begin!=end)    {        int mid=(begin+end)/2;        int lsz=seg[seg[r1].l].sz;        if(k<=lsz)            r1=seg[r1].l, end=mid;        else            r1=seg[r1].r, begin=mid+1, k-=lsz;    }    return begin;}int N, Q;int A[100001];pair<int, int> B[100001];int tree[100001];int main(){    scanf("%d%d", &N, &Q);    assert(1<=N && N<=100000);    assert(1<=Q && Q<=100000);    for(int i=1; i<=N; i++)    {        scanf("%d", A+i);        assert(1<=A[i] && A[i]<=100000);        B[i]=make_pair(-A[i], i);    }    sort(B+1, B+1+N);    tree[0]=build(1, N);    for(int i=1; i<=N; i++)        tree[i]=update(tree[i-1], 1, N, B[i].second, 1);    while(Q--)    {        int a, b;        scanf("%d%d", &a, &b);        assert(1<=a && a<=100000);        assert(1<=b && b<=100000);        int idx=lower_bound(B+1, B+1+N, make_pair(-a+1, -1))-B-1;        int x=query(tree[idx], 1, N, b);        if(x!=-1)            x=A[x];        printf("%d\n", x);    }    return 0;}`