Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
You have N warriors and M gems. You can evolve any of your warriors by using X gems.Alternatively, you can kill any of your non-evolved warriors and receive 
Y gems. You cannot Kill any evolved warrior. Find the maximum number of warriors you can evolve.


What I have tried:

I thought of applying Binary search on N warriors , If N/2 warriors can be evolved,check from N/2 to N else , check from 0 to N.
.

I tried the above approach but it didn't work out.
Posted
Updated 9-Jul-23 2:04am
Comments
Member 16046597 9-Jul-23 8:06am    
post the code

As this is off-topic for the QA forum you might like to remove this post and try again in the Alogrithms forum Algorithms Discussion Boards[^]
Quote:
but it didn't work out.
does not help us help you in any way. What did you actually try (post the code) and what actually went wrong.
In the meantime, this is very similar to the problem discussed here WARRIORS - Editorial - editorial - CodeChef Discuss[^] - it might give you some ideas
 
Share this answer
 
v2
Comments
Member 16046597 9-Jul-23 8:08am    
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
long n=sc.nextInt();
long m= sc.nextInt();
long x= sc.nextInt();
long y= sc.nextInt();
int count =0;
evolve(n,m,x,y,count);
}

public static void evolve(long n,long m,long x,long y,long count)
{

if(n<=0)
{
System.out.println(count);
return;
}

long quotient = m/x;
if (quotient <= 0)
{
n--;
m=m+y;
evolve(n,m,x,y,count);
}
else
{
count=count+quotient;
n=n-quotient;
m= m-(x*quotient);
evolve(n,m,x,y,count);
}
} i tried this code but only 5 test case solved out of 9
CHill60 10-Jul-23 4:24am    
If you have a question then use the red "Ask a Question" link at the top of this page
1. Have a look at Data Constraints, Use correct data type to store variables.use long
2.Think of applying Binary search on N warriors , If N/2 warriors can be evolved, check from N/2 to N else , check from 0 to N.
3.Remember You cant evolved a killed warrior.
4.There can be many possible ways to evolve warriors , you have to print the maximum number of warriors which can be evolved.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900