Daily Archives: October 28, 2021
C# || 3Sum Closest – How To Get 3 Numbers In Array Closest Equal To Target Value Using C#
The following is a module with functions which demonstrates how to get 3 numbers in an array closest equal to target value using C#.
1. 3 Sum Closest – Problem Statement
Given an integer array nums of length n and an integer target, find three integers in nums such that the sum is closest to target.
Return the sum of the three integers.
You may assume that each input would have exactly one solution.
Example 1:
Input: nums = [-1,2,1,-4], target = 1
Output: 2
Explanation: The sum that is closest to the target is 2. (-1 + 2 + 1 = 2).
Example 2:
Input: nums = [0,0,0], target = 1
Output: 0
2. 3 Sum Closest – Solution
The following is a solution which demonstrates how to get 3 numbers in an array closest equal to target value.
This solution uses the two pointer technique to find combinations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 28, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to get 3 numbers closest to target value // ============================================================================ public class Solution { public int ThreeSumClosest(int[] nums, int target) { var result = 0; // Keep track of the solution closest to the target var closestDifference = int.MaxValue; // Sort the array Array.Sort(nums); // Loop through numbers for (int index = 0; index < nums.Length; ++index) { // Skip duplicates if (index > 0 && nums[index] == nums[index - 1]) { continue; } // Find 3 sum combination closest to the target value var startIndex = index + 1; var endIndex = nums.Length - 1; while (startIndex < endIndex) { // Get the sum var sum = nums[index] + nums[startIndex] + nums[endIndex]; // Determine how close the sum is to the target value var difference = Math.Abs(target - sum); if (difference < closestDifference) { closestDifference = difference; result = sum; } // An exact match is found, exit if (difference == 0) { break; // Sum is less than target } else if (sum < target) { ++startIndex; // Sum is greater than target } else { --endIndex; } } // An exact match is found, exit if (closestDifference == 0) { break; } } return result; } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output for the example cases:
2
0
C# || 3Sum – How To Get All Triplet Combinations In Array Equal To Target Value Using C#
The following is a module with functions which demonstrates how to get all triplet combinations in an array equal to a target value using C#.
1. 3 Sum – Problem Statement
Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0.
Notice that the solution set must not contain duplicate triplets.
Example 1:
Input: nums = [-1,0,1,2,-1,-4]
Output: [[-1,-1,2],[-1,0,1]]
Example 2:
Input: nums = []
Output: []
Example 3:
Input: nums = [0]
Output: []
2. 3 Sum – Solution
The following is a solution which demonstrates how to get all triplet combinations in an array equal to a target value.
This solution uses the two pointer technique to find combinations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 28, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to get 3 numbers equal to target value // ============================================================================ public class Solution { public IList<IList<int>> ThreeSum(int[] nums) { var result = new List<IList<int>>(); // Sort the array Array.Sort(nums); // Set the target value var target = 0; // Loop through numbers for (int index = 0; index < nums.Length; ++index) { // Skip duplicates if (index > 0 && nums[index] == nums[index - 1]) { continue; } // Find all 3 sum combinations that equal the target value var startIndex = index + 1; var endIndex = nums.Length - 1; while (startIndex < endIndex) { // Get the sum var sum = nums[index] + nums[startIndex] + nums[endIndex]; // Sum equals target if (sum == target) { // Add values to results when a target is found var valueStart = nums[startIndex]; var valueEnd = nums[endIndex]; result.Add(new List<int>{nums[index], valueStart, valueEnd}); // Advance past duplicate items to the 'next' values // at the start and end of the array while (startIndex < endIndex && valueStart == nums[startIndex]) { ++startIndex; } while (startIndex < endIndex && valueEnd == nums[endIndex]) { --endIndex; } // Sum is less than target } else if (sum < target) { ++startIndex; // Sum is greater than target } else { --endIndex; } } } return result; } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output for the example cases:
[[-1,-1,2],[-1,0,1]]
[]
[]
C# || Two Sum II – How To Get Two Numbers In Sorted Array Equal To Target Value Using C#
The following is a module with functions which demonstrates how to get two numbers in a sorted array equal to target value using C#.
1. Two Sum II – Problem Statement
Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 <= numbers.length.
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Example 1:
Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
Example 2:
Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
Example 3:
Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].
2. Two Sum II – Solution
The following is a solution which demonstrates how to get two numbers in sorted array equal to target value.
In this solution, Binary Search is used to find the two numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 28, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to get two numbers equal to target value // ============================================================================ public class Solution { public int[] TwoSum(int[] numbers, int target) { var result = new int[2]; var lo = 0; var hi = numbers.Length - 1; while (lo < hi) { var m = lo + (hi - lo) / 2; var sum = numbers[lo] + numbers[hi]; if (sum == target) { result[0] = lo + 1; result[1] = hi + 1; break; } else if (sum < target) { // Determine if mid is less than the remaining value // Increase lo to equal mid if so if (numbers[m] < target - numbers[hi]) { lo = m + 1; } else { lo = lo + 1; } } else { // Determine if mid is greater than the remaining value // Decrease hi to equal mid if so if (numbers[m] > target - numbers[lo]) { hi = m - 1; } else { hi = hi - 1; } } } return result; } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output for the example cases:
[1,2]
[1,3]
[1,2]
C# || Two Sum – How To Get Two Numbers In Array Equal To Target Value Using C#
The following is a module with functions which demonstrates how to get two numbers in array equal to target value using C#.
1. Two Sum – Problem Statement
Given an array of integers nums and an integer target, return indices of the two numbers such that they add up to target.
You may assume that each input would have exactly one solution, and you may not use the same element twice.
You can return the answer in any order.
Example 1:
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Output: Because nums[0] + nums[1] == 9, we return [0, 1].
Example 2:
Input: nums = [3,2,4], target = 6
Output: [1,2]
Example 3:
Input: nums = [3,3], target = 6
Output: [0,1]
2. Two Sum – Solution
The following is a solution which demonstrates how to get two numbers in array equal to target value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
// ============================================================================ // Author: Kenneth Perkins // Date: Oct 28, 2021 // Taken From: http://programmingnotes.org/ // File: Solution.cs // Description: Demonstrates how to get two numbers equal to target value // ============================================================================ public class Solution { public int[] TwoSum(int[] nums, int target) { var result = new int[2]; // Use a map dictionary to keep track of the numbers already seen var seen = new Dictionary<int, int>(); // Go through numbers for (int index = 0; index < nums.Length; ++index) { // Subtract the target from the current array index. // The result of this operation is the second number // that is needed to equal the target value int remaining = target - nums[index]; // Check if we have 'seen' this remaining value before if (seen.ContainsKey(remaining)) { result[0] = seen[remaining]; result[1] = index; break; } // Save the number and index seen[nums[index]] = index; } return result; } }// http://programmingnotes.org/ |
QUICK NOTES:
The highlighted lines are sections of interest to look out for.
The code is heavily commented, so no further insight is necessary. If you have any questions, feel free to leave a comment below.
Once compiled, you should get this as your output for the example cases:
[0,1]
[1,2]
[0,1]