• + 0 comments

    Here is my rust submission:

    It is long but this is the only way I was able to understand properly. Try running it, the println! gives very useful output for figuring the problem out.

    fn workbook(n: i32, k: i32, arr: &[i32]) -> i32 {
        let mut special_problems = 0;
        let mut page_no = 0;
    
        for chapter in 0..n {
            let mut problem_count = arr[chapter as usize];
            let mut exitLoop = false;
            let mut start = 0;
            loop {
                let mut problems_in_page: Vec<i32> = Vec::new();
                if (problem_count > k) {
                    problem_count -= k;
    
                    let range = start..start + k;
    
                    problems_in_page = range.collect();
                    start += k;
                } else {
                    let range = start..start + problem_count;
                    problems_in_page = range.collect();
                    exitLoop = true;
                }
    
                for i in &problems_in_page {
                    if page_no == *i {
                        special_problems += 1;
                    }
                }
    
                println!("Chapter: {chapter}, Page: {page_no}, Value: {problems_in_page:?}");
                page_no += 1;
                if exitLoop {
                    break;
                }
            }
        }
    
        special_problems
    }