import java.util.*;
import java.lang.*;
import java.io.*;

// The main method must be in a class named "Main".
class Main {
    public static void main(String[] args) {
        int n_1 = 22;
        int w_1 = 6;
        int num_1 = 8;
        //====================
        int n_2 = 13;
        int w_2 = 3;
        int num_2 = 6;
        
        System.out.print(solution(n_1, w_1, num_1));
        //System.out.print(solution(n_2, w_2, num_2));
    }
    public static int solution(int n, int w, int num) {
        //System.out.println("--- [ Solution ] ---");
        int answer = 0;
        int rowCount = n / w + (n % w == 0 ? 0 : 1);
        
        int targetR = 0;
        int targetC = 0;
        
        int maxR = rowCount - 1;
        int minC = 0;
        int maxC = w - 1;
        
        int r = 0;
        int c = 0;
        
        boolean isRight = true;
        
        for (int i = 1; i <= n; i++) {
            if(isRight && c > maxC) {
                c -= 1;
                r += 1;
                
                isRight = !isRight;
            }
            else if(c < minC){
                c += 1;
                r += 1;
                
                isRight = !isRight;
            }
            
            if(i == num) {
                targetC = c;
                answer++;
            }
            else if(i > num && c == targetC) {
                answer++;
            }
            
            if(isRight)
                c += 1;
            else
                c -= 1;
        }
        
        return answer;
    }
}

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: