Editorial for FINDX

PROBLEM LINK:CodeChef: Practical coding for everyone

Practice

Author: targer_5star
Tester: targer_5star
Editorialist: targer_5star

DIFFICULTY:

CAKEWALK.

PREREQUISITES:

Arrays,Pointers.

PROBLEM:

It’s an Emergency in the chefland. some prisoners smached the chefland’s prison and tried to escape from it.But got Caught.

Now the jailor is asking the prisoners that who smached the walls. But no one is answering. Jailor has got the clue that the prisoner who smached the wall is standing between the prisoners whose numbers are 3 and 5.

So you are given an array of size N Containing the Number of prisoner’s and Every Ni value consist of the prisoner Number.

Your job is to find the Prisoner Number. who is standing between the prisoner Number 3 and 5. and print that number.

If no one is standing between 3 and 5 OR there is no prisoner whose number is 3 or 5 then print -1.

QUICK EXPLANATION:

Keep Two Pointers while traversing the array and move both the pointes with the difference of one index.
And keep checking the pointers if the pointer x-> points to value 3 and the pointer x+2-> points to value 5 then print the value x+1->Value.
Else if no such positions exists with the value 3 and 5 then print the value -1.

EXPLANATION:

Your job is to find the Prisoner Number. who is standing between the prisoner Number 3 and 5. and print that number.

If no one is standing between 3 and 5 OR there is no prisoner whose number is 3 or 5 then print -1.

so to solve this question:-

Keep Two Pointers while traversing the array and move both the pointes with the difference of one index.

And keep checking the pointers if the pointer x-> points to value 3 and the pointer x+2-> points to value 5 then print the value x+1->Value.
Else if no such positions exists with the value 3 and 5 then print the value -1.

SOLUTIONS:

Setter's Solution

//Prem se bolo Radhe Radhe

#include <bits/stdc++.h>

const long long SZ = 4e3 + 7;

const long long inf = 1e18;

const long long MOD = 1e9 + 7;

const long long mod = 1e9 + 7;

long long opnmbr = 1;

#define ll long long

#define ld long double

#define pb push_back

#define mp make_pair

#define eb emplace_back

#define pll pair<ll, ll>

#define vi vector

#define vs vector

#define vpl vector

#define qi queue

#define si set

#define map map<ll, ll>

#define umap unordered_map<ll, ll>

#define fi first

#define se second

#define sz(x) (ll)x.size()

#define all(c) (c).begin(), (c).end()

#define allr(c) (c).rbegin(), (c).rend()

#define Max(a,b) ((a > b) ? a : b)

#define Min(a,b) ((a < b) ? a : b)

#define ci(X) ll X; cin>>X

#define cii(X, Y) ll X, Y; cin>>X>>Y

#define ciii(X, Y, Z) ll X, Y, Z; cin>>X>>Y>>Z

#define ciiii(W, X, Y, Z) ll W, X, Y, Z; cin>>W>>X>>Y>>Z

#define co cout<<

#define in cin>>

#define Jivan_ka_asli_aadhar_to_prem_hai_na ll ___T; cin>>___T; while (___T-- > 0)

#define Code_Wode_mai_kya_rakha_hai ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define inf 1e18

#define ed endl

#define abhi(i, n) for(ll i = 0; i < (n); i++)

#define abhi1(i, n) for(ll i = 1; i <= (n); i++)

#define abhi2(i, l, r) for(ll i = l; i < (r); i++)

#define rabhi(i, l, r) for(ll i = l; i >= (r); i–)

#define ms0(X) memset((X), 0, sizeof((X)))

#define ms1(X, V) memset((X), 1, sizeof((X)))

#define ms2(X, V) memset((X), 1, sizeof((X)))

#define flv(X, V) fill(all((X)), V)

#define gcd(a,b) __gcd(a,b)

using namespace std;

ll prmod(ll a, ll b)

{

ll ans = 1;

while (b)

{

    if (b & 1) ans = (ans * a) % MOD;

    b = b / 2;

    a = (a * a) % MOD;

}

return ans;

}

int pri(ll n)

{

int flag=0;

for(ll i = 2; i <= n / 2; ++i)

{

 if(n % i == 0)

 {

    flag = 1;

    break;

  }

}

if(flag==0)

{

  return 1;

}

else

{

   return 0;

}

}

ll lcm(ll a, ll b)

{

 return (a / gcd(a, b)) * b;

}

void Radhe_Radhe()

{

ll n,flag=0,x=0;

cin>>n;

ll a[n];

for(int i=0;i<n;i++)

{

   cin>>a[i];

}

for(int i=0;i<n;i++)

{

   if(a[i]==3 and a[i+2]==5)

   {

       flag=1;

       x=a[i+1];

   }

}

if(flag==1)

{

   cout<<x<<"\n";

}

else

{

   cout<<-1<<"\n";

}

}

int main()

{

   Code_Wode_mai_kya_rakha_hai;

   Jivan_ka_asli_aadhar_to_prem_hai_na

   {

          Radhe_Radhe();

   }

}

Tester's Solution

//Prem se bolo Radhe Radhe

#include <bits/stdc++.h>

const long long SZ = 4e3 + 7;

const long long inf = 1e18;

const long long MOD = 1e9 + 7;

const long long mod = 1e9 + 7;

long long opnmbr = 1;

#define ll long long

#define ld long double

#define pb push_back

#define mp make_pair

#define eb emplace_back

#define pll pair<ll, ll>

#define vi vector

#define vs vector

#define vpl vector

#define qi queue

#define si set

#define map map<ll, ll>

#define umap unordered_map<ll, ll>

#define fi first

#define se second

#define sz(x) (ll)x.size()

#define all(c) (c).begin(), (c).end()

#define allr(c) (c).rbegin(), (c).rend()

#define Max(a,b) ((a > b) ? a : b)

#define Min(a,b) ((a < b) ? a : b)

#define ci(X) ll X; cin>>X

#define cii(X, Y) ll X, Y; cin>>X>>Y

#define ciii(X, Y, Z) ll X, Y, Z; cin>>X>>Y>>Z

#define ciiii(W, X, Y, Z) ll W, X, Y, Z; cin>>W>>X>>Y>>Z

#define co cout<<

#define in cin>>

#define Jivan_ka_asli_aadhar_to_prem_hai_na ll ___T; cin>>___T; while (___T-- > 0)

#define Code_Wode_mai_kya_rakha_hai ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define inf 1e18

#define ed endl

#define abhi(i, n) for(ll i = 0; i < (n); i++)

#define abhi1(i, n) for(ll i = 1; i <= (n); i++)

#define abhi2(i, l, r) for(ll i = l; i < (r); i++)

#define rabhi(i, l, r) for(ll i = l; i >= (r); i–)

#define ms0(X) memset((X), 0, sizeof((X)))

#define ms1(X, V) memset((X), 1, sizeof((X)))

#define ms2(X, V) memset((X), 1, sizeof((X)))

#define flv(X, V) fill(all((X)), V)

#define gcd(a,b) __gcd(a,b)

using namespace std;

ll prmod(ll a, ll b)

{

ll ans = 1;

while (b)

{

    if (b & 1) ans = (ans * a) % MOD;

    b = b / 2;

    a = (a * a) % MOD;

}

return ans;

}

int pri(ll n)

{

int flag=0;

for(ll i = 2; i <= n / 2; ++i)

{

 if(n % i == 0)

 {

    flag = 1;

    break;

  }

}

if(flag==0)

{

  return 1;

}

else

{

   return 0;

}

}

ll lcm(ll a, ll b)

{

 return (a / gcd(a, b)) * b;

}

void Radhe_Radhe()

{

ll n,flag=0,x=0;

cin>>n;

ll a[n];

for(int i=0;i<n;i++)

{

   cin>>a[i];

}

for(int i=0;i<n;i++)

{

   if(a[i]==3 and a[i+2]==5)

   {

       flag=1;

       x=a[i+1];

   }

}

if(flag==1)

{

   cout<<x<<"\n";

}

else

{

   cout<<-1<<"\n";

}

}

int main()

{

   Code_Wode_mai_kya_rakha_hai;

   Jivan_ka_asli_aadhar_to_prem_hai_na

   {

          Radhe_Radhe();

   }

}

Editorialist's Solution

//Prem se bolo Radhe Radhe

#include <bits/stdc++.h>

const long long SZ = 4e3 + 7;

const long long inf = 1e18;

const long long MOD = 1e9 + 7;

const long long mod = 1e9 + 7;

long long opnmbr = 1;

#define ll long long

#define ld long double

#define pb push_back

#define mp make_pair

#define eb emplace_back

#define pll pair<ll, ll>

#define vi vector

#define vs vector

#define vpl vector

#define qi queue

#define si set

#define map map<ll, ll>

#define umap unordered_map<ll, ll>

#define fi first

#define se second

#define sz(x) (ll)x.size()

#define all(c) (c).begin(), (c).end()

#define allr(c) (c).rbegin(), (c).rend()

#define Max(a,b) ((a > b) ? a : b)

#define Min(a,b) ((a < b) ? a : b)

#define ci(X) ll X; cin>>X

#define cii(X, Y) ll X, Y; cin>>X>>Y

#define ciii(X, Y, Z) ll X, Y, Z; cin>>X>>Y>>Z

#define ciiii(W, X, Y, Z) ll W, X, Y, Z; cin>>W>>X>>Y>>Z

#define co cout<<

#define in cin>>

#define Jivan_ka_asli_aadhar_to_prem_hai_na ll ___T; cin>>___T; while (___T-- > 0)

#define Code_Wode_mai_kya_rakha_hai ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)

#define inf 1e18

#define ed endl

#define abhi(i, n) for(ll i = 0; i < (n); i++)

#define abhi1(i, n) for(ll i = 1; i <= (n); i++)

#define abhi2(i, l, r) for(ll i = l; i < (r); i++)

#define rabhi(i, l, r) for(ll i = l; i >= (r); i–)

#define ms0(X) memset((X), 0, sizeof((X)))

#define ms1(X, V) memset((X), 1, sizeof((X)))

#define ms2(X, V) memset((X), 1, sizeof((X)))

#define flv(X, V) fill(all((X)), V)

#define gcd(a,b) __gcd(a,b)

using namespace std;

ll prmod(ll a, ll b)

{

ll ans = 1;

while (b)

{

    if (b & 1) ans = (ans * a) % MOD;

    b = b / 2;

    a = (a * a) % MOD;

}

return ans;

}

int pri(ll n)

{

int flag=0;

for(ll i = 2; i <= n / 2; ++i)

{

 if(n % i == 0)

 {

    flag = 1;

    break;

  }

}

if(flag==0)

{

  return 1;

}

else

{

   return 0;

}

}

ll lcm(ll a, ll b)

{

 return (a / gcd(a, b)) * b;

}

void Radhe_Radhe()

{

ll n,flag=0,x=0;

cin>>n;

ll a[n];

for(int i=0;i<n;i++)

{

   cin>>a[i];

}

for(int i=0;i<n;i++)

{

   if(a[i]==3 and a[i+2]==5)

   {

       flag=1;

       x=a[i+1];

   }

}

if(flag==1)

{

   cout<<x<<"\n";

}

else

{

   cout<<-1<<"\n";

}

}

int main()

{

   Code_Wode_mai_kya_rakha_hai;

   Jivan_ka_asli_aadhar_to_prem_hai_na

   {

          Radhe_Radhe();

   }

}

1 Like