# [Small Y algorithm]⚡Daily LeetCode clock-in⚡- 43.Sum of Two Numbers II - Input Ordered Array

Keywords: Java Algorithm leetcode #### 📢Preface

 🚀 Algorithmic problems🚀
• 🌲 Typing an algorithmic problem every day is both a learning process and a sharing process😜
• 🌲 Tip: This column uses both C#and Java for problem solving programming languages
• 🌲 To keep learning every day, let's work together to become the algorithmic God🧐!
• 🌲 Today is the 43rd day of punch-in🎈!
 🚀 Algorithmic problems🚀

## 🌲Sample topic: Sum of two numbers II - Input ordered array

Given an integer array number that has been arranged in a non-decreasing order, find out from the array that the sum of two numbers satisfies the sum equal to the target number.

The function should return the subscript values of these two numbers as an array of integers of length 2.
Numbers'subscripts count from 1, so the answer array should satisfy 1 <= answer < answer <= numbers.length.

You can assume that each input corresponds to a unique answer, and that you cannot reuse the same elements.

Example 1:

```Input: numbers = [2,7,11,15], target = 9
Output:[1,2]
Explanation: The sum of 2 and 7 equals the target number 9. Therefore index1 = 1, index2 = 2 .
```

Example 2:

```Input: numbers = [2,3,4], target = 6
Output:[1,3]
```

Example 3:

```Input: numbers = [-1,0], target = -1
Output:[1,2]
```

Tips:

• 2 <= numbers.length <= 3 * 104
• -1000 <= numbers[i] <= 1000
• numbers in non-decreasing order
• -1000 <= target <= 1000
• There is only one valid answer

### 🌻C#Method: Depth-first search

Now that we are solving the minimum depth of a binary tree, let's go through the whole binary tree and determine the depth.

Use depth first search to traverse the entire tree and record the minimum depth.

For each non-leaf node, we only need to calculate the minimum leaf node depth of its left and right subtrees separately.

This translates a big problem into a small one, which can be solved recursively.

Idea Analysis

Code:

```public class Solution {
public int MinDepth(TreeNode root) {
if (root == null) return 0;
else if (root.left == null) return MinDepth(root.right) + 1;
else if (root.right == null) return MinDepth(root.left) + 1;
else return Math.Min(MinDepth(root.left), MinDepth(root.right)) + 1;
}
}
```

results of enforcement

```adopt
Execution time: 272 ms，At all C# 46.32% of users were defeated in submissions
Memory consumption: 50 MB，At all C# 50.00% of users were defeated in submission
```

Complexity analysis

```Time Complexity: O( n ),among n Is the number of nodes in the tree
Spatial Complexity: O( H )，among H Is the height of the tree
```

### 🌻Java Method 1: Binary Search

Idea Analysis

Find two numbers in the array so that their sum is equal to the target value. First fix the first number, then look for the second number, which is equal to the target value minus the difference of the first number.

Using the ordered nature of arrays, the second number can be found by means of binary search.

To avoid repetitive searches, only search to the right of the first number when searching for the second number.

Code:

```class Solution {
public int[] twoSum(int[] numbers, int target) {
for (int i = 0; i < numbers.length; ++i) {
int low = i + 1, high = numbers.length - 1;
while (low <= high) {
int mid = (high - low) / 2 + low;
if (numbers[mid] == target - numbers[i]) {
return new int[]{i + 1, mid + 1};
} else if (numbers[mid] > target - numbers[i]) {
high = mid - 1;
} else {
low = mid + 1;
}
}
}
return new int[]{-1, -1};
}
}
```

results of enforcement

```adopt
Execution time:3 ms，At all Java  26 beats in submission.14%Users
Memory consumption: 38.6 MB，At all Java 52 defeated in submission.15%Users
```

Complexity analysis

```Time Complexity: O( nlog n )
Spatial Complexity: O( 1 )
```

### 🌻Java Method 2: Double Pointer

Idea Analysis Code:

```class Solution {
public int[] twoSum(int[] numbers, int target) {
int low = 0, high = numbers.length - 1;
while (low < high) {
int sum = numbers[low] + numbers[high];
if (sum == target) {
return new int[]{low + 1, high + 1};
} else if (sum < target) {
++low;
} else {
--high;
}
}
return new int[]{-1, -1};
}
}
```

results of enforcement

```adopt
Execution time:0 ms，At all Java  Defeated 100 in submission.00%Users
Memory consumption: 38.4 MB，At all Java 85 Defeated in Submission.91%Users
```

Complexity analysis

```Time Complexity: O(n)
Spatial Complexity: O(1)
```

## 💬summary

• Today is the 43rd day of punching!
• This article uses two programming languages C#and Java to solve problems
• Some methods are also referential, capitalized, and shared as you learn, thank the algorithmic guys again
• That's the end of today's algorithmic sharing. Bye tomorrow! #### 🚀Previous quality article sharing

Posted by Nhoj on Tue, 28 Sep 2021 09:10:40 -0700