Posts

Showing posts with the label Array

Plus One

  Given a   non-empty   array of digits representing a non-negative integer, increment one to the integer. The digits are stored such that the most significant digit is at the head of the list, and each element in the array contains a single digit. You may assume the integer does not contain any leading zero, except the number 0 itself.   Example 1: Input: digits = [1,2,3] Output: [1,2,4] Explanation: The array represents the integer 123. Example 2: Input: digits = [4,3,2,1] Output: [4,3,2,2] Explanation: The array represents the integer 4321. Example 3: Input: digits = [0] Output: [1]   Constraints: 1 <= digits.length <= 100 0 <= digits[i] <= 9 Solution: Algorithm: 1) Add 1 to the nth element in the array. If the sum is greater than 9 then take the ten's element (save it to 'carry' ) and move to the (n-1)th element.  2) Continue step 1 till we don't have a carry. Time Complexity:  O(n) Space Complexity: O(1)

Remove Element from Unsorted Array - Easy

  Given an array   nums   and a value   val , remove all instances of that value   in-place   and return the new length. Do not allocate extra space for another array, you must do this by  modifying the input array  in-place  with O(1) extra memory. The order of elements can be changed. It doesn't matter what you leave beyond the new length. Example 1: Given nums = [3,2,2,3] , val = 3 , Your function should return length = 2 , with the first two elements of nums being 2 . It doesn't matter what you leave beyond the returned length. Example 2: Given nums = [0,1,2,2,3,0,4,2] , val = 2 , Your function should return length = 5 , with the first five elements of nums containing  0 , 1 , 3 , 0 , and  4 . Note that the order of those five elements can be arbitrary. It doesn't matter what values are set beyond the returned length. Clarification: Confused why the returned value is an integer but your answer is an array? Note t...

Remove Duplicates - Easy

Given a sorted array  nums , remove the duplicates  in-place  such that each element appear only  once  and return the new length. Do not allocate extra space for another array, you must do this by  modifying the input array  in-place  with O(1) extra memory. Example 1: Given nums = [1,1,2] , Your function should return length = 2 , with the first two elements of nums being 1 and 2 respectively. It doesn't matter what you leave beyond the returned length. Example 2: Given nums = [0,0,1,1,1,2,2,3,3,4] , Your function should return length = 5 , with the first five elements of nums being modified to  0 , 1 , 2 , 3 , and  4 respectively. It doesn't matter what values are set beyond the returned length. Clarification: Confused why the returned value is an integer but your answer is an array? Note that the input array is passed in by  reference , which means modification to the input array will be known to the caller as we...

Two Sum

Given an array of integers, return   indices   of the two numbers such that they add up to a specific target. You may assume that each input would have  exactly  one solution, and you may not use the  same  element twice. Example: Given nums = [2, 7, 11, 15], target = 9, Because nums[ 0 ] + nums[ 1 ] = 2 + 7 = 9, return [ 0 , 1 ]. Solution: Since for each element 'x' present in the array, we will have to check whether a number 'y' exists in the array such that sum of two numbers is target and return indices of the number.  We can also say that, for any element 'x' present in the array , find a number 'target-x' which is also present in the array. One approach to the problem is to scan the complete array for finding number 'target-x' for each element 'x'.  Another approach is to use dictionaries. For each element 'x' in the array, check whether the dictionary contains element 'target-x'. So here, first we will scan the...