Introduction
In this problem, we are given an integer array nums.
For every element in the array, we must calculate how many numbers are smaller than the current number.
The result should be stored in another array where:
- Each index contains the count of smaller numbers
- Comparison must be done against every other element
- We cannot count the element itself
This is a beginner-friendly array problem that teaches comparison logic and nested loop thinking.
Problem Statement
Given the array nums, return an array answer such that:
Question Link
Problem Link -: Leetcode 1365
Example
Input
Output
Explanation
- 8 → four smaller numbers → 1,2,2,3
- 1 → no smaller number
- 2 → one smaller number → 1
- 2 → one smaller number → 1
- 3 → three smaller numbers → 1,2,2
Understanding the Problem
We need to check:
For every element:
How many values in the array are smaller than it?
This means:
- Compare one number with all other numbers
- Count valid smaller values
- Store count in answer array
Intuition
The simplest idea is:
- Pick one number
- Compare it with every element
- Count smaller numbers
- Save result
- Repeat for all indices
Since constraints are small:
Brute force works perfectly.
Approach
We use two loops:
- Outer loop → selects current number
- Inner loop → compares with all numbers
If:
Then increase count.
Java Solution
Code Explanation
Variables
Current element index.
Used for comparison.
Stores count of smaller numbers.
Final result array.
Step-by-Step Logic
Step 1
Pick current number using:
Step 2
Compare with every number using:
Step 3
If another value is smaller:
Increase count.
Step 4
Store count.
Step 5
Move to next element.
Dry Run
Input
First Element = 8
Compare with:
| Number | Smaller? |
| 1 | Yes |
| 2 | Yes |
| 2 | Yes |
| 3 | Yes |
Count = 4
Second Element = 1
No smaller number.
Third Element = 2
Smaller number:
Count = 1
Final Answer
Time Complexity
We compare every element with every other element.
Complexity
Because:
- Outer loop = N
- Inner loop = N
Total:
Space Complexity
We only store output array.
Complexity
Better Clean Version (Recommended)
Your logic works, but interviewers usually prefer readable code.
Optimized Approach
Since:
We can use frequency counting.
This gives:
Where:
- N = array size
- K = range of values
Common Mistakes
1. Comparing Same Index
Wrong:
Correct:
2. Forgetting Reset Count
Wrong:
Correct:
after each iteration.
3. Index Out Of Bounds
Always ensure:
Interview Tips
This problem teaches:
- Nested loops
- Array traversal
- Counting logic
- Comparison problems
- Brute force thinking
Interviewers may ask:
Can you optimize it?
Answer:
Use counting sort or prefix frequency.
FAQs
Is this problem easy?
Yes. It is beginner-friendly.
Is brute force accepted?
Yes.
Constraints are small.
Can we optimize?
Yes.
Using counting frequency array.
Is this asked in interviews?
Yes.
Especially for beginners.
Final Thoughts
LeetCode 1365 is a simple array problem that helps build strong fundamentals.
You learn:
- How comparisons work
- How nested loops solve counting problems
- How to convert logic into output arrays




