Skip to content

Instantly share code, notes, and snippets.

@fahied
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fahied/52ccc50fc4fc08d95fd0 to your computer and use it in GitHub Desktop.
Save fahied/52ccc50fc4fc08d95fd0 to your computer and use it in GitHub Desktop.
Given two sorted arrays of integers, find the pair of indices (one into each array) which identify elements which sum to a given integer
import java.util.ArrayList;
public class SearchMe {
static int[] intArrayA = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20};
static int[] intArrayB = {3,4,9,14};
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
pairSum(intArrayA, intArrayB,14);
}
public static ArrayList<String> pairSum(int[] x, int []y, int sum) {
int lenX = x.length;
int lenY = y.length;
int pointer1;
int pointer2;
ArrayList<String> storage = new ArrayList<String>();
for (int i = 0; i < lenX; i++) {
pointer1 = x[i];
for (int k = lenY - 1; k >= 0; k--) {
pointer2 = y[k];
if (pointer1 + pointer2 == sum) {
i++;
String pair = Integer.toString(pointer1) + " and " + Integer.toString(pointer2) ;
System.out.println(pair);
storage.add(pair);
}
}
}
return storage;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment