LeetCode 1365: How Many Numbers Are Smaller Than the Current Number Solution

Master LeetCode problem 1365 (How Many Numbers Are Smaller Than the Current Number), a easy challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1365. How Many Numbers Are Smaller Than the Current Number

Problem Explanation

Explanation

  • Algorithmic Idea:
    • We can solve this problem using a simple brute force approach.
    • For each element in the array, we compare it with all other elements to count the numbers smaller than it.
  • Step-by-Step Iterations:
    • Iterate over each element in the array.
    • For each element, compare it with all other elements to count the numbers smaller than it.
    • Store the count for each element in the result array.
  • Time Complexity: O(n^2) where n is the number of elements in the array.
  • Space Complexity: O(n) where n is the number of elements in the array.

Solution Code

class Solution {
    public int[] smallerNumbersThanCurrent(int[] nums) {
        int n = nums.length;
        int[] result = new int[n];
        
        for (int i = 0; i < n; i++) {
            int count = 0;
            for (int j = 0; j < n; j++) {
                if (i != j && nums[j] < nums[i]) {
                    count++;
                }
            }
            result[i] = count;
        }
        
        return result;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1365 (How Many Numbers Are Smaller Than the Current Number)?

This page provides optimized solutions for LeetCode problem 1365 (How Many Numbers Are Smaller Than the Current Number) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1365 (How Many Numbers Are Smaller Than the Current Number)?

The time complexity for LeetCode 1365 (How Many Numbers Are Smaller Than the Current Number) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1365 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1365 in Java, C++, or Python.

Back to LeetCode Solutions