← leetcode

Find the Distance Value Between Two Arrays

· Easy

LeetCode problem 1385

(i just chose the easiest solution) simply simulate all the cases with brute force!

code link to code

class Solution {
public:
    int findTheDistanceValue(vector<int>& arr1, vector<int>& arr2, int d) {
        int ans = 0;
        for (int num1 : arr1) {
            bool valid = true;
            for (int num2 : arr2) {
                if (abs(num1 - num2) <= d) {
                    valid = false;
                    break;
                }
            }

            if (valid) ++ans;
        }
        return ans;
    }
};

complexity link to complexity

  • Time: O(mn) for m = arr1.size() and n = arr2.size()
  • Space: O(1)

time taken link to time taken

  • 2 minutes and 56 seconds