Stretch Each Number Into Two Halves
Updated
Problem statement
You are given a list of integers nums. Stretch it: replace every number x with two numbers, its two halves, so the result is twice as long. The halves of each number stay next to each other, in the same order as the numbers in nums.
The two halves of x are found like this:
- The first half is
xdivided by 2 with the fraction dropped, which rounds toward zero. For example, 7 gives 3 and -7 gives -3. - The second half is the rest:
xminus the first half.
So an even number splits into two equal halves, and for an odd number the half nearer zero comes first: 7 becomes 3, 4 and -7 becomes -3, -4. The two halves always add up to x.
Function
stretchIntoHalves(nums: int[]) → int[]Examples
Example 1
nums = [14,0,-5]return = [7,7,0,0,-2,-3]14 splits into 7 and 7. 0 splits into 0 and 0. -5 is odd: the half nearer zero, -2, comes first, then the rest, -5 - (-2) = -3. The answer is [7,7,0,0,-2,-3].
Example 2
nums = [5,-1,8]return = [2,3,0,-1,4,4]5 is odd: the half nearer zero is 2, then 5 - 2 = 3. -1 is odd: the half nearer zero is 0, then -1 - 0 = -1. 8 splits into 4 and 4. The answer is [2,3,0,-1,4,4].
Example 3
nums = [1000000,-999999]return = [500000,500000,-499999,-500000]1,000,000 splits into 500,000 and 500,000. For -999,999 the half nearer zero is -499,999, and the rest is -500,000. The answer is [500000,500000,-499999,-500000].
Constraints
0 <= nums.length <= 1000-106 <= nums[i] <= 106
More DocuSign Onsite Interview questions
- Count Binary SubstringsONSITE INTERVIEW · Seen Sep 2026
- Two Positions That Add Up to a TargetPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Compress Consecutive Integer RangesONSITE INTERVIEW · Seen Aug 2026
- JSON Schema Object ValidatorONSITE INTERVIEW · Seen Aug 2026
- Most Transactions Kept Without Going NegativeONSITE INTERVIEW · Seen May 2025
- Three-Part Splits With a Light MiddleONSITE INTERVIEW · Seen May 2025
- Combine Two Sorted Lists Into OneONSITE INTERVIEW · Seen Apr 2022
- Exact Palindrome CheckPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
- Paired Words That Share a SubstringONSITE INTERVIEW · Seen Apr 2022
- Reverse the Characters of a TextPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2022
Similar questions at other companies
- Plus OneDruva · ONSITE INTERVIEW
- Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
- Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
- Maximum Product of Three IntegersPelatro · ONSITE INTERVIEW
- Exact-Sixty Dice Game ValueJane Street · ONSITE INTERVIEWJane Street interview questions
- Find the Missing Number from 1 to nHyperVerge · ONSITE INTERVIEW
- Alternating Index Square SumAccenture · ONSITE INTERVIEWAccenture interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- Add One to a Number Represented as DigitsOracle · ONSITE INTERVIEWOracle interview questions
- Streaming Entropy, Part 1: Batch EntropyOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions