Click here to Skip to main content
15,890,579 members
Articles / Programming Languages / Java
Tip/Trick

Store Credit Solution - Google Code Jam

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
3 Mar 2016CPOL1 min read 10.7K   1  
Store Credit solution

Following is my solution to the problem Store credit found in the practise program list of Google Code jam.

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

Sample

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

Solution

Java
import java.util.Scanner;
import java.util.StringTokenizer;

public class StoreCredit {
    public static Scanner sc = new Scanner(System.in);
    
    public static void main(String[] args) {
        
        int N, item, credit;
        String price;
        StringTokenizer tkn;
        int[] list;
        String[] result ;
        
        N = sc.nextInt();        //Number of cases
        result = new String[N];
        
        for(int k=0; k<N; k++){
            
            credit = sc.nextInt();
            item = sc.nextInt();        //number items in the list
            list = new int[item];
            sc.nextLine();                // consumes newline left-overs
            
            try {
                
                price = sc.nextLine();        //prices of the items as a string
                
                tkn = new StringTokenizer(price, " ");    //breaks the string into tokens
                
                for(int x=0; x<item; x++){                         // converts the tokens into
                    list[x] = Integer.parseInt(tkn.nextToken());   // integers and puts them
                }                                                  // into a list
                
            } catch (Exception e) {
                println("\tMissing elements...");
            }
            
            myLabel:{
                for(int x=0; x<item; x++){
                    for(int y=x+1; y<item; y++){
                        if( (list[x]+list[y])==credit ){
                            result[k] = (x+1)+" "+(y+1);
                            break myLabel;
                        }else {
                            result[k] = "No match found...";
                        }
                    }
                }
            }            
        }
        
        println("\n");
        for(int i=0; i<N; i++){
            println("Case #"+(i+1)+": "+result[i]);        // prints the output
        }
    }
    
                            // Custom methods //
    public static void print(Object obj){ System.out.print(obj); }
    public static void println(Object obj){ System.out.println(obj); }
}

Output

Case #1: 2 3
Case #2: 1 4
Case #3: 4 5
Case #4: 29 46
Case #5: 11 56
Case #6: 4 5
Case #7: 40 46
Case #8: 16 35
Case #9: 55 74
Case #10: 7 9

License

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


Written By
Bangladesh Bangladesh
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
-- There are no messages in this forum --