The TwoSum program is a simple yet efficient implementation of the classic Two Sum problem in C. It takes an array of integers and a target sum as input, then finds and returns the indices of the two numbers that add up to the target. If no such numbers exist, it informs the user accordingly.
- Dynamic Memory Allocation: Utilizes
mallocto handle input sizes dynamically. - Input Validation: Ensures that user inputs are valid and handles errors gracefully.
- Output: Provides clear feedback to the user about the results.
- A C compiler (like
gcc) to compile the code. - Basic knowledge of C programming.
-
Clone the repository (or create a new file):
git clone https:/github.com/nerrison/TwoSum.git cd TwoSum -
Compile the code:
gcc -o TwoSum TwoSum.c
-
Run the program:
./TwoSum
- The program will prompt you to enter the number of elements in the array.
- Next, enter the elements of the array, one by one.
- Finally, input the target sum.
- The program will output the indices of the two numbers that add up to the target, or inform you if no such numbers exist.
Enter the number of elements in the array:
5
Enter the elements of the array:
2
7
11
15
Enter the target number:
9
Indices of the numbers that sum to 9: 0 and 1
- TwoSum Function: Contains the logic to find two indices.
- Main Function: Handles user interaction and input validation.
Feel free to fork the repository and make improvements! If you have any suggestions or encounter any issues, please create an issue.
This project is licensed under the MIT License. See the LICENSE file for details.
- Inspired by common algorithms and problems found in coding interviews.
- Thanks to the C community for ongoing support and resources.