Example 1: Input: num[] = {3, 2, 5, 1, 7}; Output: {2, 1, 3, 0, 4} Explanation: Number of Elements smaller than num[0] = 3 are (1 and 2) = 2 Number of Elements smaller than num[1] = 2 are (1) = 1 Number of Elements smaller than num[2] = 5 are (1, 2 and 3) = 3 Number of Elements smaller than num[3] = 1 are () = 0 Number of Elements smaller than num[4] = 7 are (1, 2, 3 and 5) = 4 Example 2: Input: num[] = {3, 3, 3, 3} Output: {0, 0, 0, 0}
Example 3:
Input: num[] = {3, 2, 2, 5, 1, 7}
Output: {3, 1, 1, 4, 0, 5}
Note: We are using a C++ vector in our solution instead of an array because vectors are dynamic in nature and provide more functionality. (alert-passed)
Approach 1: Brute Force Solution.
/*C++ program for how many number of elements smaller than current number.*/ #include<iostream> #include<vector> using namespace std; vector<int> smallerNumber(vector<int> &num){ vector<int> ans; int count; for(int i = 0; i < num.size(); i++){ count = 0; for(int j = 0; j < num.size(); j++){ if(num[i] > num[j]) count++; } ans.push_back(count); } return ans; } int main(){ vector<int> num = {3, 2, 2, 5, 1, 7};
vector<int> result = smallerNumber(num); for(int i = 0; i < result.size(); i++) cout<<result[i]<<" "; return 0; }
3 1 1 4 0 5- Time Complexity: Running two nested loops from 0 to n will take n x n units of time so the time complexity will be O(n^2).
- Space Complexity: No extra space is required to perform any operation except the answer vector that is used to store the counts but we cannot consider this for calculating space complexity so overall space complexity will be O(1).
Approach 2: Using Hash Map and Sorting.
Given num = [3, 2, 2, 5, 1, 7]
copy = [1, 2, 2, 3, 5, 7]
mp[copy[5]] = mp[7] = 5
mp[copy[4]] = mp[5] = 4
mp[copy[3]] = mp[3] = 3
mp[copy[2]] = mp[2] = 1(overwritten 2 to 1)
mp[copy[0]] = mp[1] = 0
num[0] = mp[num[0]] = mp[3] = 3
num[1] = mp[num[1]] = mp[2] = 1
num[2] = mp[num[2]] = mp[2] = 1
num[3] = mp[num[3]] = mp[5] = 4
num[4] = mp[num[4]] = mp[1] = 0
num[5] = mp[num[5]] = mp[7] = 5
num = [3, 1, 1, 4, 0, 5]
/*C++ Optimized program for how many number of elements smaller than current number.*/ #include<bits/stdc++.h> using namespace std; vector<int> smallerNumber(vector<int> &num){ unordered_map<int, int> mp; //make a copy vector vector<int> copy = num; //sort the copy vector in ascending order sort(copy.begin(), copy.end()); //store elements corresponding to their index for(int i = num.size() - 1; i >= 0; i--){ mp[copy[i]] = i; } //store map value back to num array for(int i = 0; i < num.size(); i++){ num[i] = mp[num[i]]; } return num; } int main(){ vector<int> num = {3, 2, 2, 5, 1, 7}; vector<int> result = smallerNumber(num); for(int i = 0; i < result.size(); i++) cout<<result[i]<<" "; return 0; }
3 1 1 4 0 5- Time Complexity: Sorting the copied array will take O(nlogn) time, and running two separate loops for n elements will take 2O(n) time so the overall time complexity of our solution is O(nlogn).
- Space Complexity: As we are using a new copy vector and unordered map for storing the element so overall space complexity is O(n).


