আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা তিনটি আর্গুমেন্ট নেয়, যথা −
arr --> an array of integers m --> a positive integer n --> a positive integer
আমাদের ফাংশনের কাজ হল এই ধরনের দুটি উপাদান আছে কিনা তা খুঁজে বের করা (এগুলিকে a1 এবং a2 বলি) যেমন −
-
a1 এবং a2 এর মধ্যে পরম পার্থক্য সর্বাধিক m
-
a1 এবং a2 এর সূচকগুলির মধ্যে পরম পার্থক্য সর্বাধিক n
উদাহরণ
নিম্নলিখিত কোড -
const arr = [1, 2, 3, 1, 7, 8];
const findSpecialElements = (arr = [], m, n) => {
const map = arr
.map((el, ind) => ({ el, ind }))
.sort((a, b) => a.el - b.el);
let left = 0;
let right = 1;
while (right < map.length) {
const diff = Math.abs(map[right].el - map[left].el);
const range = Math.abs(map[right].ind - map[left].ind);
if (diff <= n && range <= m){
return true
}else if (diff > n){
left++;
}else if (range > m){
right++;
};
if (left === right){
right++;
};
};
return false;
};
console.log(findSpecialElements(arr, 3, 0)); আউটপুট
নিম্নোক্ত কনসোল আউটপুট -
true