Question Link https://leetcode.com/problems/minimum-absolute-difference/
Hello guys,
Yesterday I have given my first coding interview for JIO which was not went well. I performed very bad and did't get the optimised solution even though it was a easy question.
This is what I did at the moment.
//Input: arr[] = [5, 3, 2, 4]
//Output: [[2, 3],[3, 4],[4, 5]]
//Input: arr[] = [2, 4, 7, 13, 17]
//Output: [[2, 4]]
// Min abs diif
// 5 - 3 = 2
// abs to to 1
// Brute f
//
//n2 time
//O n2 space
// i ---n
int main() {
vector<int> nums{2, 5, 7, 13, 17};
vector<int> diffList;
vector<pair<int, int>> pairs;
vector<vector<int>> result;
for(int i = 0; i< nums.size(); i++){
for(int j = i + 1; j< nums.size(); j++){
int diff = abs(nums[i] - nums[j]);
diffList.push_back(diff);
pairs.push_back({nums[i], nums[j]});
}
}
int minDiff = INT_MAX;
for(int d : diffList){
if(d != 0){
minDiff = min(minDiff, d);
}
}
for(auto nums : diffList){
// cout << nums << endl;
}
for(int i = 0; i< diffList.size(); i++){
int value = diffList[i];
if(minDiff == value){
//cout << pairs[i].second << endl;
vector<int> nums;
nums.push_back(pairs[i].first);
nums.push_back(pairs[i].second);
result.push_back(nums);
}
}
// result ready
//cout << result.size();
for(auto nums : result){
cout << nums[0];
cout << nums[1];
}
}My interviwer was a nice guy. He was very supportive but I just ruined my coding interview spent all 40 minutes only for this first asked question.
Mistakes I did
I know you guys laugh on me because it's just a easy question, and it's okay for me I am just a beginner in DSA and way far to go.
Thanks Leetcode community for helping each other.