Closest Numbers

Sort by

recency

|

620 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can watch the explanation here: https://youtu.be/-sX3IgdQ6Wg

    vector<int> closestNumbers(vector<int> arr) {
        sort(arr.begin(), arr.end());
        int diff = INT_MAX;
        vector<int> result;
        for(int i = 1; i < arr.size(); i++){
            if(arr[i] - arr[i-1] < diff){
                diff = arr[i] - arr[i-1];
                result = {arr[i-1], arr[i]};
            }
            else if(arr[i] - arr[i-1] == diff){
                result.insert(result.end(), {arr[i-1], arr[i]});
            }
        }
        return result;
    }
    
  • + 0 comments

    def closestNumbers(arr):

    arr.sort()
    dc = {}
    min_i = float('inf')
    for i in range(1, len(arr)):
        if abs(arr[i]-arr[i-1]) == min_i:
            dc[min_i] += arr[i-1],arr[i]
        if arr[i]!=arr[i-1] and abs(arr[i]-arr[i-1]) < min_i:
            min_i = abs(arr[i]-arr[i-1])
            dc[min_i]= arr[i-1],arr[i]
    return dc[min_i]
    
  • + 0 comments
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <climits>
    
    using namespace std;
    
    int main() {
        int n;
        cin >> n;
    
        vector<int> arr(n);
        for (int i = 0; i < n; i++) {
            cin >> arr[i];
        }
    
        sort(arr.begin(), arr.end());
    
        int minDiff = INT_MAX;
        vector<pair<int, int>> closestPairs;
    
        for (int i = 0; i < n - 1; i++) {
            int diff = arr[i + 1] - arr[i];
            if (diff < minDiff) {
                minDiff = diff;
                closestPairs.clear();
                closestPairs.push_back({arr[i], arr[i + 1]});
            } else if (diff == minDiff) {
                closestPairs.push_back({arr[i], arr[i + 1]});
            }
        }
    
        for (const auto& pair : closestPairs) {
            cout << pair.first << " " << pair.second << " ";
        }
        
        cout << endl;
    
        return 0;
    }
    
  • + 0 comments

    https://github.com/Achintha444/problem-solving-hackerrank-js/blob/main/90-closest-numbers.js

    solution in js

  • + 0 comments

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.math.BigInteger;

    public class Solution {

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
    
        // Read number of test cases
        int T = Integer.parseInt(br.readLine().trim());
    
        // Process each test case
        for (int t = 0; t < T; t++) {
            String[] input = br.readLine().trim().split("\\s+");
            int a = Integer.parseInt(input[0]);
            int b = Integer.parseInt(input[1]);
            int x = Integer.parseInt(input[2]);
    
            // Calculate a^b using BigInteger for arbitrary precision
            BigInteger ab = power(a, b);
    
            // Find closest multiple of x to ab
            BigInteger closestMultiple = findClosestMultiple(ab, x);
    
            // Append result to StringBuilder
            sb.append(closestMultiple).append("\n");
        }
    
        // Print all results
        System.out.print(sb);
    }
    
    // Function to calculate a^b using BigInteger for arbitrary precision
    private static BigInteger power(int a, int b) {
        BigInteger result = BigInteger.ONE;
        BigInteger base = BigInteger.valueOf(a);
        BigInteger exponent = BigInteger.valueOf(b);
    
        while (exponent.compareTo(BigInteger.ZERO) > 0) {
            if (exponent.testBit(0)) { // If exponent is odd
                result = result.multiply(base);
            }
            base = base.multiply(base);
            exponent = exponent.shiftRight(1); // Divide exponent by 2
        }
    
        return result;
    }
    
    // Function to find closest multiple of x to num using BigInteger
    private static BigInteger findClosestMultiple(BigInteger num, int x) {
        BigInteger quotient = num.divide(BigInteger.valueOf(x));
        BigInteger closestSmaller = quotient.multiply(BigInteger.valueOf(x));
        BigInteger closestLarger = closestSmaller.add(BigInteger.valueOf(x));
    
        // Determine closest multiple
        if (num.subtract(closestSmaller).compareTo(closestLarger.subtract(num)) <= 0) {
            return closestSmaller;
        } else {
            return closestLarger;
        }
    }
    

    }