সিকোয়েনশিয়াল ডিজিট সংখ্যা
একটি সংখ্যার সিকোয়েনশিয়াল ডিজিট থাকলে তার প্রতিটি ডিজিট আগের ডিজিট থেকে এক বড় হবে।
সমস্যা
আমাদের একটি জাভাস্ক্রিপ্ট ফাংশন লিখতে হবে যা ঠিক দুটি এলিমেন্ট ধারণকারী একটি অ্যারে (arr) নেবে যা একটি রেঞ্জ নির্দেশ করে। ফাংশনটি সেই রেঞ্জের (সীমাসহ) সব ইন্টিজার যাদের সিকোয়েনশিয়াল ডিজিট আছে সেগুলোর সর্ট করা অ্যারে রিটার্ন করবে।
উদাহরণস্বরূপ, ইনপুট যদি হয় –
const arr = [1000, 13000];
তবে আউটপুট হবে –
const output = [1234, 2345, 3456, 4567, 5678, 6789, 12345];
উদাহরণ
কোডটি নিচের মতো –
const arr = [1000, 13000]; const sequentialDigits = ([low, high] = [1, 1]) => { const findCount = (num) => { let count = 0; while(num > 0){ count += 1; num = Math.floor(num / 10) }; return count; }; const helper = (count, start) => { let res = start; while(count > 1 && start < 9){ res = res * 10 + start + 1; start += 1; count -= 1; }; if(count > 1){ return 0; }; return res; }; const count1 = findCount(low); const count2 = findCount(high); const res = []; for(let i = count1; i <= count2; i++){ for(let start = 1; start <= 8; start++){ const num = helper(i, start); if(num >= low && num <= high){ res.push(num); }; }; }; return res; }; console.log(sequentialDigits(arr));আউটপুট
কনসোলে আউটপুট হবে –
[ 1234, 2345, 3456, 4567, 5678, 6789, 12345 ]