ArraySumTwoNumbers.java
This program was created to gather the sum of two numbers. We call function solve with parameters sums and k.
We then check to see if it contains only one element and then we basically go through the array and find two numbers that add up to k. It would return true if found or false if not.
printArray simply formats the array elements and prints them out. The formats are customly tailored.
The code has been modified slightly and now uploaded to the site.
Code Snippet
/*
Author: ENTPRESTIGIOUS
*/
import java.util.Arrays;
public class ArraySumTwoNumbers {
public static void main(String[] args){
int [] arr1 = {3, 2, 4};
int key = 6;
boolean result = solve(arr1, key);
printArray(arr1);
System.out.println(result);
}
public static boolean solve(int[] nums, int k){
// int total = 0;
// int store = 0;
Arrays.sort(nums);
if (nums.length == 1){
return false;
}
// String print = String.format("%d", nums);
// System.out.print(print + " ");
for (int i = 0; i < nums.length - 1; i++){
if (nums[i] + nums[i + 1] == k){
return true;
}
}
return false;
}
public static void printArray(int[] nums){
for (int i = 0; i < nums.length; i++){
int store = nums[i];
String print = String.format("%d", store);
System.out.print(print + "\t");
}
}
}