Amazon Interview Question
Country: United States
package CareerCup;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
public class Solution {
public static void main(String[] args) {
int[] A = { 11, 2, 3, 14, 5 };
Map<Integer, Set> pairs = new HashMap<Integer, Set>();
for (int i : A) {
pairs.put(i, new HashSet<Integer>());
}
for (int i = 0; i < A.length; i++) {
for (int j = A.length - 1; j >= 0; j--) {
if (A[i] <= A[j]){
pairs.get(A[i]).add(-A[j] + A[i]);}
if (pairs.get(A[j]).contains(A[j] - A[i]) && pairs.containsKey(A[i])
&& pairs.containsKey(A[i] - A[j])) {
System.out.println((A[j] + " " + (-A[j] + A[i])));
}
}
}
}
}
i couln'd remove duplicates though :(
public static void Test()
{
int inputArray[] = { 1, 3, 2, 6, 9, 8, 7 };
System.out.print("Input Array : ");
for(int x : inputArray)
System.out.print(x + " ");
System.out.print("\n");
for(int i=0 ; i<inputArray.length; i++)
{
for(int j = i+1; j<inputArray.length; j++ )
{
for(int k=0; k<inputArray.length; k++)
{
if (k != i && k != j)
{
if( inputArray[i] + inputArray[j] == inputArray[k] )
{
System.out.println( inputArray[i] + "+" + inputArray[j] + "=" + inputArray[k] );
}
}
}
}
}
}
public static void Test()
{
int inputArray[] = { 1, 3, 2, 6, 9, 8, 7 };
System.out.print("Input Array : ");
for(int x : inputArray)
System.out.print(x + " ");
System.out.print("\n");
for(int i=0 ; i<inputArray.length; i++)
{
for(int j = i+1; j<inputArray.length; j++ )
{
for(int k=0; k<inputArray.length; k++)
{
if (k != i && k != j)
{
if( inputArray[i] + inputArray[j] == inputArray[k] )
{
System.out.println( inputArray[i] + "+" + inputArray[j] + "=" + inputArray[k] );
}
}
}
}
}
}
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
/*
* Given an array of numbers, print all pair (2) of numbers in the array
* if sum also present in the array
*/
public class ArrayPairSumInArray {
public static void main(String[] args) {
int[] iArrays = {1, 3, 2, 6, 9, 8, 7 };
Arrays.sort( iArrays );
Set<Integer> set = new HashSet<Integer>();
for( int num: iArrays ){
set.add( num );
}
for( int i = 2; i < iArrays.length; i++ ){
for( int j = i - 1; j > 0; j-- ){
if( set.contains(iArrays[i] - iArrays[j]) && (iArrays[i]-iArrays[j]) < iArrays[j]){
System.out.println("[ " + ( iArrays[i] - iArrays[j]) + "+" + iArrays[j] + " ] = " + iArrays[i]);
}
}
}
}
}
public class Resolver {
public static IEnumerable <Tuple<int, int>> GetPairs(IEnumerable<int> numbers) {
var array = numbers.ToArray ();
if (array.Length < 2) // we don't have pairs!!
yield break;
Array.Sort (array);
if (array.Length == 3) {
if (array [0] + array [1] == array [2])
yield return new Tuple <int, int> (array [0], array [1]);
// There are no other possibilities due to the fact that the array is sorted!
yield break;
}
var hSet = new HashSet <int> (array);
var lastPosition = array.Length - 1;
for (var i = 0; i < lastPosition; ++i) {
// break condition!
if ((array [i] < 2 ? 2 : array [i] << 1) > array [lastPosition]) {
yield break; // we found a situation where we would not find the sum in our array anylonger!
}
for (var j = i + 1; j < array.Length; ++j) {
var sum = array [i] + array [j];
if (hSet.Contains (sum)) {
yield return new Tuple <int, int> (array [i], array [j]);
}
}
}
}
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
public class PairsWithSumInArray {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String [] str = br.readLine().split(" ");
int size = str.length;
int [] numbers = new int[size];
for(int i=0; i<size; i++){
numbers[i] = Integer.parseInt(str[i]);
}
List<Triplet> triplets = getAllTriplets(numbers);
Iterator itr = triplets.iterator();
while (itr.hasNext()){
Triplet triplet = (Triplet) itr.next();
System.out.println(triplet+", value("+numbers[triplet.getA()]+", "+numbers[triplet.getB()]+", "+numbers[triplet.getC()]+")");
}
}
public static List<Triplet> getAllTriplets(int [] array){
List<Triplet> triplets = new ArrayList<>();
HashMap<Integer, Integer> hashMap = new HashMap<>();
int size = array.length;
for(int i=0; i<size; i++){
hashMap.put(array[i], i);
}
for(int i=0; i<size-2; i++){
for(int j=i+1; j<size; j++){
int a = array[i];
int b = array[j];
if(hashMap.containsKey(a+b)){
triplets.add(new Triplet(i, j, hashMap.get(a+b)));
}
}
}
return triplets;
}
}
class Triplet{
private int a;
private int b;
private int c;
public Triplet(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int getA() {
return a;
}
public int getB() {
return b;
}
public int getC() {
return c;
}
@Override
public String toString() {
return "Index"+"("+a+","+b+","+c+")";
}
}
- karim.ainine December 06, 2016