How to Build Logic for iMocha Java Questions
1. Understand the Problem Clearly
- Read the problem twice
- Identify what is being calculated (sum, count, condition)
- Highlight keywords like 'every', 'each', 'if', 'for all types', etc.
2. Identify Input & Output
- Note what the input values represent (N, K, Array A[])
- Know what your function must return or print
3. Extract Rules or Conditions
- Understand offers, discount rules, ranges
- Break complex conditions into simple ones
4. Plan Loops & Logic
- Use for-loops to process arrays
- Use if-conditions or integer division for conditions like 'every K items'
5. Use Dry Run (Manual Calculation)
- Try 1 small example input on paper
- Validate your logic before coding
6. Write Clean Code
- Use meaningful variable names
- Break into steps: sum, condition, output
- Avoid unnecessary complexity
Example: Free Candy
- Loop through array -> A[i] / K for each type
- Sum all A[i] -> total / 10
- Return total extra candies