Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Finding the missing number in an arithmetic progression sequence in JavaScript
An arithmetic progression (AP) or arithmetic sequence is a sequence of numbers where the difference between consecutive terms is constant. For example, the sequence 5, 7, 9, 11, 13... has a common difference of 2.
When we have an array representing elements of an arithmetic progression with one missing number, we need to find that missing element efficiently. The key is to determine the common difference and locate where the sequence breaks.
Problem Example
Given an array like:
const arr = [7, 13, 19, 31, 37, 43];
The missing number is 25, which should appear between 19 and 31 in the sequence with common difference 6.
Algorithm Approach
The solution works by:
- Comparing differences at the start and end of the array
- Identifying the correct common difference
- Scanning through the array to find the gap
Implementation
const arr = [7, 13, 19, 31, 37, 43];
const findMissingNumber = (arr = []) => {
let {length} = arr;
let diff1 = arr[1] - arr[0];
let diff2 = arr[length - 1] - arr[length - 2];
// Check if the missing number is at the beginning or end
if (diff1 !== diff2) {
if (diff1 == 2 * diff2) {
return arr[0] + diff2;
} else {
return arr[length - 1] - diff1;
}
}
// Scan through the array to find the gap
for (let i = 1; i < length - 2; i++) {
if (arr[i + 1] - arr[i] != diff1) {
return arr[i] + diff1;
}
}
return arr[0];
};
console.log(findMissingNumber(arr));
25
How It Works
The algorithm first calculates the difference between the first two elements (diff1) and the last two elements (diff2). If these differences are not equal, the missing number is likely at the beginning or end of the sequence.
If diff1 equals 2 * diff2, the missing number is at the beginning. If diff2 equals 2 * diff1, the missing number is at the end. Otherwise, the function iterates through the array to find where the normal progression breaks.
Alternative Approach Using Mathematical Formula
const findMissingNumberMath = (arr) => {
const n = arr.length + 1; // Original sequence length
const first = arr[0];
const last = arr[arr.length - 1];
// Calculate what the common difference should be
const commonDiff = (last - first) / (n - 1);
// Find the missing number
for (let i = 0; i < arr.length - 1; i++) {
const expected = first + (i + 1) * commonDiff;
if (Math.abs(arr[i + 1] - expected) > 0.001) {
return expected;
}
}
return first + commonDiff; // Missing at the beginning
};
const testArr = [2, 4, 8, 10, 12];
console.log("Missing number:", findMissingNumberMath(testArr));
Missing number: 6
Conclusion
Finding a missing number in an arithmetic progression requires identifying the common difference and locating the gap in the sequence. Both approaches work efficiently with O(n) time complexity, making them suitable for practical applications.
