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
Returning just greater array in JavaScript
We need to write a JavaScript function that takes an array of positive integers, joins them to form a single number, adds 1 to that number, and returns the result as an array of digits.
Problem Statement
Given an array of positive integers, we need to:
- Join the digits to form a single number
- Add 1 to that number
- Return the result as an array of individual digits
For example, if the input array is [6, 7, 3, 9], it represents the number 6739. Adding 1 gives us 6740, which should be returned as [6, 7, 4, 0].
Example Input and Output
Input:
const arr = [6, 7, 3, 9];
Expected Output:
[6, 7, 4, 0]
Explanation: The array [6, 7, 3, 9] represents 6739. Adding 1 gives 6740, which becomes [6, 7, 4, 0].
Simple Solution
Here's a straightforward approach using string manipulation:
const arr = [6, 7, 3, 9];
const justGreater = (arr = []) => {
// Validate input
if (!arr.length || !arr.every(v => v >= 0 && v <= 9)) {
return null;
}
// Join digits to form number, add 1, convert back to array
const number = arr.join('');
const incremented = (parseInt(number) + 1).toString();
return incremented.split('').map(digit => parseInt(digit));
};
console.log(justGreater(arr));
console.log(justGreater([9, 9, 9])); // Edge case: carries over
console.log(justGreater([1, 2, 3])); // Simple case
[6, 7, 4, 0] [1, 0, 0, 0] [1, 2, 4]
Handling Large Numbers
For very large numbers that exceed JavaScript's safe integer limit, we can use BigInt:
const justGreaterBigInt = (arr = []) => {
if (!arr.length || !arr.every(v => v >= 0 && v <= 9)) {
return null;
}
// Use BigInt for large numbers
const number = BigInt(arr.join(''));
const incremented = (number + 1n).toString();
return incremented.split('').map(digit => parseInt(digit));
};
// Test with large number
const largeArr = [9, 0, 0, 7, 1, 9, 9, 2, 5, 4, 7, 4, 0, 9, 9, 1];
console.log(justGreaterBigInt(largeArr));
[9, 0, 0, 7, 1, 9, 9, 2, 5, 4, 7, 4, 0, 9, 9, 2]
Manual Addition Algorithm
For educational purposes, here's a manual digit-by-digit addition approach:
const justGreaterManual = (arr = []) => {
if (!arr.length || !arr.every(v => v >= 0 && v <= 9)) {
return null;
}
const result = [...arr]; // Copy array
let carry = 1; // We want to add 1
// Start from rightmost digit
for (let i = result.length - 1; i >= 0 && carry > 0; i--) {
const sum = result[i] + carry;
result[i] = sum % 10;
carry = Math.floor(sum / 10);
}
// If there's still a carry, prepend it
if (carry > 0) {
result.unshift(carry);
}
return result;
};
console.log(justGreaterManual([6, 7, 3, 9]));
console.log(justGreaterManual([9, 9, 9])); // Should become [1, 0, 0, 0]
console.log(justGreaterManual([0])); // Should become [1]
[6, 7, 4, 0] [1, 0, 0, 0] [1]
Comparison of Approaches
| Method | Simplicity | Large Numbers | Performance |
|---|---|---|---|
| String Conversion | High | Limited by Number.MAX_SAFE_INTEGER | Good |
| BigInt | High | Unlimited | Good |
| Manual Addition | Low | Unlimited | Best |
Conclusion
The string conversion approach with BigInt is the most practical solution for incrementing array-represented numbers. For performance-critical applications with very large numbers, the manual addition algorithm provides optimal efficiency.
