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
C# program to determine if any two integers in array sum to given integer
In this article, we'll learn how to determine if any two integers in an array sum to a given target integer. This is a common programming problem that can be solved using different approaches with varying time complexities.
Problem Statement
Given an array of integers and a target sum, we need to find if there exist any two numbers in the array that add up to the target value.
Using Nested Loops (Brute Force Approach)
The simplest approach is to check every possible pair of numbers using nested loops −
using System;
public class Program {
public static void Main() {
int[] arr = {7, 4, 6, 2};
int target = 8;
Console.WriteLine("Array: [" + string.Join(", ", arr) + "]");
Console.WriteLine("Target sum: " + target);
Console.WriteLine();
bool found = false;
for (int i = 0; i
The output of the above code is −
Array: [7, 4, 6, 2]
Target sum: 8
Found pair: 4 + 6 = 8
Found pair: 6 + 2 = 8
Using HashSet for Optimized Solution
A more efficient approach uses a HashSet to achieve O(n) time complexity by storing visited elements and checking if the complement exists −
using System;
using System.Collections.Generic;
public class Program {
public static void Main() {
int[] arr = {7, 4, 6, 2, 1, 9};
int target = 10;
Console.WriteLine("Array: [" + string.Join(", ", arr) + "]");
Console.WriteLine("Target sum: " + target);
Console.WriteLine();
HashSet seen = new HashSet();
bool found = false;
foreach (int num in arr) {
int complement = target - num;
if (seen.Contains(complement)) {
Console.WriteLine("Found pair: " + complement + " + " + num + " = " + target);
found = true;
}
seen.Add(num);
}
if (!found) {
Console.WriteLine("No pair found that sums to " + target);
}
}
}
The output of the above code is −
Array: [7, 4, 6, 2, 1, 9]
Target sum: 10
Found pair: 4 + 6 = 10
Found pair: 7 + 9 = 10
Found pair: 1 + 9 = 10
Function to Return Boolean Result
Here's a clean implementation that returns true if any pair exists, false otherwise −
using System;
using System.Collections.Generic;
public class Program {
public static bool HasTwoSum(int[] arr, int target) {
HashSet seen = new HashSet();
foreach (int num in arr) {
int complement = target - num;
if (seen.Contains(complement)) {
return true;
}
seen.Add(num);
}
return false;
}
public static void Main() {
int[] arr1 = {2, 7, 11, 15};
int[] arr2 = {3, 2, 4};
int[] arr3 = {3, 3};
Console.WriteLine("Array [2, 7, 11, 15], target 9: " + HasTwoSum(arr1, 9));
Console.WriteLine("Array [3, 2, 4], target 6: " + HasTwoSum(arr2, 6));
Console.WriteLine("Array [3, 3], target 6: " + HasTwoSum(arr3, 6));
Console.WriteLine("Array [2, 7, 11, 15], target 20: " + HasTwoSum(arr1, 20));
}
}
The output of the above code is −
Array [2, 7, 11, 15], target 9: True
Array [3, 2, 4], target 6: True
Array [3, 3], target 6: True
Array [2, 7, 11, 15], target 20: False
Comparison of Approaches
| Approach | Time Complexity | Space Complexity | Description |
|---|---|---|---|
| Nested Loops | O(n²) | O(1) | Check every possible pair |
| HashSet | O(n) | O(n) | Store complements and check existence |
Conclusion
The two-sum problem can be solved efficiently using a HashSet approach in O(n) time complexity. While the brute force method is simpler to understand, the HashSet solution is more scalable for larger datasets by trading space for time efficiency.
