Minimum Absolute Difference in an Array

  • + 0 comments

    JavaScript

    'use strict';
    
    const fs = require('fs');
    
    process.stdin.resume();
    process.stdin.setEncoding('utf-8');
    
    let inputString = '';
    let currentLine = 0;
    
    process.stdin.on('data', function(inputStdin) {
        inputString += inputStdin;
    });
    
    process.stdin.on('end', function() {
        inputString = inputString.split('\n');
    
        main();
    });
    
    function readLine() {
        return inputString[currentLine++];
    }
    
    /*
     * Complete the 'minimumAbsoluteDifference' function below.
     *
     * The function is expected to return an INTEGER.
     * The function accepts INTEGER_ARRAY arr as parameter.
     */
    
    function minimumAbsoluteDifference(arr) {
        // Write your code here
    let min = Math.abs(arr[0] - arr[1]);
        
        for(let i=0; i< arr.length-1; i++){
           for(let j=i+1; j< arr.length; j++){
               const abs = Math.abs(arr[i] - arr[j]);
               if(abs < min){
                   min = abs;
               }
           } 
        }
        
        return min;
    }
    
    function main() {
        const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
    
        const n = parseInt(readLine().trim(), 10);
    
        const arr = readLine().replace(/\s+$/g, '').split(' ').map(arrTemp => parseInt(arrTemp, 10));
    
        const result = minimumAbsoluteDifference(arr);
    
        ws.write(result + '\n');
    
        ws.end();
    }