How Many Numbers Are Smaller Than the Current Number

Easy
13
69.7% Acceptance

In this lab, you will be given an array of integers nums. For each nums[i], you need to find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i]. Your task is to implement a function smallerNumbersThanCurrent that takes in an array of integers and returns the result in another array.

Make sure to use ESM import/export in your solution.

Example

Input: nums = [8,1,2,2,3]

Output: [4,0,1,1,3]

Explanation:

For nums[0]=8 there exist four smaller numbers than it (1, 2, 2 and 3).

For nums[1]=1 does not exist any smaller number than it.

For nums[2]=2 there exist one smaller number than it (1).

For nums[3]=2 there exist one smaller number than it (1).

For nums[4]=3 there exist three smaller numbers than it (1, 2 and 2).

Constraints

  • 2 <= nums.length <= 500
  • 0 <= nums[i] <= 100

Challenges

  1. Export your function smallerNumbersThanCurrent.
  2. Test your function with the given examples and expect the correct output in each test case.

Evaluation script

import fs from 'fs' import { smallerNumbersThanCurrent } from '/home/damner/code/index.js' const testlog = [] // this first block matches with - Challenge 1 try { if(smallerNumbersThanCurrent !== undefined) { testlog.push({ status: 'pass' }) } else { throw new Error('Function is not exported properly') } } catch(error) { testlog.push({ status: 'error', error: error.message || 'Challenge 1 failed' }) } // this second block matches with - Challenge 2 try { const result1 = smallerNumbersThanCurrent([8,1,2,2,3]) const expected1 = [4,0,1,1,3] const result2 = smallerNumbersThanCurrent([6,5,4,8]) const expected2 = [2,1,0,3] const result3 = smallerNumbersThanCurrent([7,7,7,7]) const expected3 = [0,0,0,0] if(JSON.stringify(result1) === JSON.stringify(expected1) && JSON.stringify(result2) === JSON.stringify(expected2) && JSON.stringify(result3) === JSON.stringify(expected3)) { testlog.push({ status: 'pass' }) } else { throw new Error('Function does not return expected results') } } catch(error) { testlog.push({ status: 'error', error: error.message || 'Challenge 2 failed' }) } fs.writeFileSync('/home/damner/code/.labtests/testlog.json', JSON.stringify(testlog)) fs.writeFileSync(process.env.UNIT_TEST_OUTPUT_FILE, JSON.stringify(testlog.map(result => result.status === 'pass')))

Setting up test environment script

#!/bin/bash set -e 1 mkdir -p /home/damner/code/.labtests cat > /home/damner/code/.labtests/package.json << EOF { "type": "module" } EOF cd /home/damner/code/.labtests mv $TEST_FILE_NAME ./nodecheck.test.js # import puppeteer doesn't work without it npm link puppeteer node nodecheck.test.js 2>&1 | tee evaluationscript.log

Initial file system for user

/** * @param {number[]} nums * @return {number[]} */ export var smallerNumbersThanCurrent = function(nums) { };
{ "name": "codedamn-lab", "type": "module" }
tabs: ['index.js'] terminals: ['yarn install']