12,548,514 members (49,237 online)
Technical Blog
Add your own
alternative version

7.3K views
Posted

# Code jam Store Credit solution in Java

, 26 Mar 2013 CPOL
 Rate this:
Please Sign up or sign in to vote.
Store credit solution in Java.

I found this question at codejam. You can find the problem here. So lets get onto the 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, N. N 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

Input:

```3
100
3
5 75 25
200
7
150 24 79 50 88 345 3
8
8
2 1 9 4 4 56 90 3```

Output:

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

My Program:

```import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class StoreCredit {

/**
* @param args
*/
public static Object[] getItems(int items[], int total)
{
ArrayList selectedItems = new ArrayList();
int index = 0;
for (int i = 0; i<items.length; i++)
{
for (int j = i+1; j < items.length; j++)
{
if (items[i] + items[j] == total)
{
selectedItems.add(index, i);
selectedItems.add(index+1,j);
}
}
}
return selectedItems.toArray();
}
public static int[] convertStringToInt(String array[])
{
int intArray[] = new int[array.length];
for (int i = 0; i < array.length; i++) intArray[i] = Integer.parseInt(array[i]);
return intArray;
}
public static void main(String[] args) throws IOException
{
FileInputStream fis = new FileInputStream("StoreCreditLarge.in");
DataInputStream in = new DataInputStream(fis);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String N = br.readLine();
String line = null;
int caseNo = 1;
while((line = br.readLine()) != null )
{
int credits = Integer.parseInt(line);
int totalItems = Integer.parseInt(br.readLine());
String strItems[] = br.readLine().split(" ");
int Items[] = convertStringToInt(strItems);
//System.out.println("Credits: "+credits+
//    " total= "+totalItems+"ItemsLength= "+Items.length);
Object selectedItems[] = getItems(Items, credits);
//System.out.println(selectedItems.length);
System.out.println("Case #"+caseNo+":"+printArray(selectedItems));
caseNo++;
}
}
public static String printArray(Object[] selectedItems) {
// TODO Auto-generated method stub
String result = "";
for (int i=0; i<selectedItems.length; i++) result = result +" "+((int)selectedItems[i]+1);
return result;
}

}```

Output:

## License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

## About the Author

 India
No Biography provided

## You may also be interested in...

 Pro Pro

## Comments and Discussions

 First Prev Next
 slow algorithm Member 990191223-May-14 6:49 Member 9901912 23-May-14 6:49
 Last Visit: 31-Dec-99 18:00     Last Update: 21-Oct-16 23:01 Refresh 1

General    News    Suggestion    Question    Bug    Answer    Joke    Praise    Rant    Admin

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161021.1 | Last Updated 26 Mar 2013
Article Copyright 2013 by ladv02
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid