
Problem Statement
In an array arr
, initially, the values followed an arithmetic progression. This means, for every successive pair of elements, the difference between them was constant throughout the array. However, the array that is provided to us has one missing value which was not at the extreme ends (neither the first nor the last element of the array). The task is to identify and return this missing value from the given arithmetic sequence. The challenge thus becomes pinpointing the removed element that disturbs this equal difference sequence.
Examples
Example 1
Input:
arr = [5,7,11,13]
Output:
9
Explanation:
The previous array was [5,7,9,11,13].
Example 2
Input:
arr = [15,13,12]
Output:
14
Explanation:
The previous array was [15,14,13,12].
Constraints
3 <= arr.length <= 1000
0 <= arr[i] <= 105
- The given array is guaranteed to be a valid array.
Approach and Intuition
The key to solving this problem lies in identifying the common difference in the arithmetic sequence formed by the elements of the array. Here’s a strategic way to find the removed element:
- Calculate all possible differences between successive elements in the provided array,
arr
. - Usually, the common difference in a perfect arithmetic sequence is the most occurring difference. In this case, however, because exactly one element (not at the ends) is missing, there might be at most two different differences observed.
- The correct or normal difference is the one that appears most frequently among the calculated differences.
- Using the most frequent (normal) difference, simulate what the next expected value in the sequence should be for each element starting from the first.
- The point at which the actual next value in the array deviates from this expected value using the normal difference is right after where the element is missing.
- Calculate the missing element using the known element just before the deviation and add the normal difference to it.
For example, in an array [5,7,11,13]
, the differences between the elements are 2 and 4. Here, 2
occurs once and 4
occurs twice; thereby making 4
the normal difference. Starting from 5
with a difference of 4
each time, you expect the sequence to be 5, 9, 13
, clearly revealing that 9
is missing after 7
. This method effectively employs the properties of the arithmetic sequence and the pattern of progression to deduce the missing element.
Solutions
- C++
- Java
class Solution {
public:
int findMissingNumber(vector<int> &nums) {
int size = nums.size();
// Calculate common difference
int step = (nums.back() - nums.front()) / size;
int low = 0;
int high = size - 1;
// Binary search implementation
while (low < high) {
int middle = (low + high) / 2;
// Condition to check continuity in elements using the calculated step
if (nums[middle] == nums.front() + middle * step) {
low = middle + 1;
} else {
high = middle;
}
}
// The missing number based on its expected position
return nums.front() + step * low;
}
};
This solution tackles the problem of finding a missing number in an arithmetic sequence using a C++ implementation that leverages binary search for efficiency. The sequence is passed as a vector of integers.
- Start by determining the size of the vector called
nums
. - Compute the common difference of the sequence, which is
(nums.back() - nums.front()) / size
. This calculation uses the first and the last elements of the sequence. - Initialize two pointers,
low
andhigh
, which help in navigating the binary search. Initially,low
is set to 0 andhigh
to size - 1. - Implement a binary search loop that continues until
low
is less thanhigh
. Inside the loop:- Calculate the midpoint (
middle
) as(low + high) / 2
. - Following the arithmetic sequence formula, check if
nums[middle]
is precisely what it should be if there were no number missing (nums.front() + middle * step
). If this condition is true, adjust thelow
pointer tomiddle + 1
, else adjust thehigh
pointer tomiddle
.
- Calculate the midpoint (
- Calculate and return the missing number using
nums.front() + step * low
after the loop completes. This formula estimates the missing number based on its expected position in the sequence.
This approach efficiently pinpoints the location of the missing number in log(n) time complexity due to the binary search mechanism, making it highly suitable for large sequences.
class Solution {
public int findMissing(int sequence[]) {
int size = sequence.length;
int step = (sequence[size - 1] - sequence[0]) / size;
int start = 0;
int end = size - 1;
while (start < end) {
int middle = (start + end) / 2;
if (sequence[middle] == sequence[0] + middle * step) {
start = middle + 1;
} else {
end = middle;
}
}
return sequence[0] + step * start;
}
}
In this Java solution, the task is to find the missing number in an arithmetic progression. The code defines a class Solution
with the method findMissing
which accepts an integer array sequence
. Here's how the solution works step-by-step:
- Calculate the length of the sequence array and store it in
size
. - Compute the common difference
step
of the progression using the formula(sequence[size - 1] - sequence[0]) / size
. - Initialize two pointers:
start
at the beginning of the sequence andend
at the end of the sequence. - Use a binary search approach to determine the missing element. The search is conducted within the boundaries set by
start
andend
:- Calculate the middle index
middle
using the average ofstart
andend
. - If the value at the middle index matches the expected value of an arithmetic sequence
(sequence[0] + middle * step)
:- Move the
start
index tomiddle + 1
.
- Move the
- Otherwise:
- Adjust the
end
tomiddle
.
- Adjust the
- Calculate the middle index
- Exit the while loop once
start
is not less thanend
. - The missing number is found by plugging
start
into the formula for the terms of the arithmetic sequencesequence[0] + step * start
.
The algorithm efficiently locates the missing number by narrowing down the search range using the arithmetic properties of the sequence. The use of binary search ensures that the time complexity is logarithmic, making it suitable for large sequences.
No comments yet.