Infosys - Monster RPG GAME Sample Question Solution

Monster RPG GAME - Solution #Infosys

SOLUTION 

 #include <iostream>

#include <map>
#include <algorithm>
using namespace std ;

void game(map<int,int> &g,int energy )
{
int c = 0;
for (auto & itr : g)
{
if( energy >= itr.first)
{
c++;
energy = energy + itr.second;
}else{
continue ;
}

}
cout << c << endl ;
}

void mapping( int p[], int b[], int n, int energy )
{
int count ;
map<int,int> m ; // mapping the key value pair from the array
int i ;
for ( i = 0 ; i < n ; i++)
{
m.insert({p[i], b[i]});
}
for (auto & itr : m)
{
cout << itr.first << " " << itr.second<< endl ;
}
game(m,energy) ;

}

int main() {
int n ; cin >> n ;
int i ;
int p[n] ; int b[n] ;
int e ;
cin >> e ; // energy i have
for (i = 0 ; i < n ; i++ )
{
cin >> p[i] ;
}
for (i = 0 ; i < n ; i++ )
{
cin >> b[i] ;
}

mapping(p,b,n, e);
return 0 ;

}


Comments

Popular posts from this blog

Hackerrank -- C++ - Variable Sized Array Solution