Answer to Question #254562 in Java | JSP | JSF for Shwetha T P

Question #254562
Maximillian is the chief commander of the Great Greek Army and he is leading his forces into a crucial war with Spain. If all the enemy soldiers stand in a straight line incrementally marked starting from position 1, and a particular soldier at position i dies, the soldiers at position 21 and 21+1 die as well. This happens in a cascading manner and so, a major part of troops can be killed by just killing one person. By retrospection, Maximillian realize that this also means that if the soldier marked 1 (standing at the head of the troops) is killed and then the entire army is Input Specification: input1: N, number of soldiers in the enemy camp input2: K, number of soldiers to be killed input3: An array of soldiers numbered between 1 to N who will be killed sequentially in the mentioned order 9 10 Output Specification: Return an array of numbers that belong to soldiers who are alive at the end (in increasing order). If all are dead, then return (0) 12 13 15 16
1
Expert's answer
2021-10-22T00:49:18-0400
import java.io.*;
import java.util.Scanner;
class Main {
 
    static int soldiers(int N, int K)
    {
        if (N == 1)
            return 1;
        else
            return (soldiers(N - 1, K) + K - 1) % N + 1;
    }
    public static void main(String[] args)
    {
        Scanner input=new Scanner(System.in);
        System.out.println("Number of soldiers in the enemy camp")
        int N = input.nextInt();
        System.out.println("Number of soldiers to be killed")
        int K = input.nextInt();
        System.out.println(soldiers(N, K));
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS