## Friday, April 11, 2014

### Store Credit Problem

Problem
You receive a credit `C` at a local store and would like to buy two items. You first walk through the store and create a list `L` of all available items. From this list you would like to buy two items that add up to the entire value of the credit. The solution you provide will consist of the two integers indicating the positions of the items in your list (smaller number first).
Input
The first line of input gives the number of cases, NN test cases follow. For each test case there will be:
• One line containing the value C, the amount of credit you have at the store.
• One line containing the value I, the number of items in the store.
• One line containing a space separated list of I integers. Each integer P indicates the price of an item in the store.
• Each test case will have exactly one solution.
Output
For each test case, output one line containing "Case #x: " followed by the indices of the two items whose price adds up to the store credit. The lower index should be output first.

Limits
5 ≤ C ≤ 1000
1 ≤ P ≤ 1000

Small dataset
N = 10
3 ≤ I ≤ 100

Large dataset
N = 50
3 ≤ I ≤ 2000

Sample

Input
`310035 75 252007150 24 79 50 88 345 3882 1 9 4 4 56 90 3`

`Output``Case #1: 2 3Case #2: 1 4Case #3: 4 5`

A Solution [Java] :

import java.io.*;
import java.util.Vector;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class credit {
public static void main(String[] args) throws Exception{
BufferedWriter bw = new BufferedWriter(new FileWriter("d:\\OUTPUTFile.txt"));

int cases=0;
int index=1;
while(index<=cases)
{
int credit =0;

int nItems = 0;

Vector<Integer> vec = new Vector<Integer>();
Pattern p = Pattern.compile("\\d+");
while(m.find()){
}

int first = 0;
int second = 0;
int flag = 1;
for(int i = 0;i<nItems;++i){
if(flag==1){
for(int j = i+1;j<nItems;++j){

if(vec.elementAt(i)+vec.elementAt(j)==credit)
{
first = i;
second = j;
flag = 0;
break;
}
}
}else
break;
}
bw.write("Case #"+index+++": "+(first+1)+" "+(second+1));
bw.newLine();
bw.flush();
}
}
}