My problem statement: While playing an RPG game, you were assigned to complete one of the hardest quests in this game. There are n monsters you'll need to defeat in this quest. Each monster i is described with two integer numbers - power(i) and bonus(i). To defeat this monster, you'll need at least power(i) experience points. If you try fighting this monster without having enough experience points, you lose immediately. You will also gain bonus(i) experience points if you defeat this monster. You can defeat monsters in any order.
Input: The first line contains an integer, n, denoting the number of monsters. The next line contains an integer, e, denoting your initial experience. Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, power(i), which represents power of the corresponding monster. Each line i of the n subsequent lines (where 0 ≤ i < n) contains an integer, bonus(i), which represents bonus for defeating the corresponding monster.
Sample cases1:
Input
2
123
78
130
10
0
OUTPUT:
2
EXPLANATION: Initial experience level is 123 points. Defeat the first monster having power of 78 and bonus of 10. Experience level is now 123+10=133. Defeat the second monster.
SECOND CASE
INPUT
3
100
101
100
304
100
1
524
OUTPUT:
2
EXPLANATION: Initial experience level is 100 points. Defeat the second monster having power of 100 and bonus of 1. Experience level is now 100+1=101. Defeat the first monster having power of 101 and bonus of 100. Experience level is now 101+100=201. The third monster can't be defeated
THIS IS MY CODE
import java.util.*;
public class Main {
public static int cal(int a[],int e,int n){
HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
for(int i=0;i<n;i++){
if(a[i]<= e && !(map.containsValue(a[i]))){
map.put(1,a[i]);
return i;
}
}
return -1;
}
public static void main(String[] args) {
int n,e,flag=0,count=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number of monster");
n=sc.nextInt();
System.out.println("Enter the experience");
e=sc.nextInt();
int a[]=new int[n];
int b[]=new int[n];
System.out.println("Enter the monster power");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
System.out.println("enter the bonus");
for(int j=0;j<n;j++) {
b[j] = sc.nextInt();
}
while(flag!=-1){
flag=cal(a,e,n);
System.out.println(flag);
if(flag!=-1)
{
e=e+b[flag];
count++;
}
}
System.out.println(count);
}
}
cal function is calling infintely Can you help me out!!!