Sunday 22 March 2015

Google Code jam:StoreCredit

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 

Output 
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
Case #1: 2 3
Case #2: 1 4
Ca
se #3: 4 5

package myway;

public class StoreCredit {
 private int totalCredits = 0;
 private int totalItems = 0;
 private int prices[];
 public static int cases = 0;

 public StoreCredit(int totalCredits, int totalItems, int prices[]) {

  this.totalCredits = totalCredits;
  this.totalItems = totalItems;
  this.prices = new int[prices.length];
  int index = 0;
  for (int price : prices) {
   this.prices[index++] = price;
  }
 }

 public int[] getTwoitems() {
  int inum[] = { 1, 2 };
  int max = 0;
 
  for (int i = 0; i < totalItems; i++) {
   for (int j = i + 1; j < totalItems; j++) {
    if (max < prices[i] + prices[j] && totalCredits>=prices[i] + prices[j]) {
      
      inum[0] = i;
      inum[1] = j;
      max = prices[i] + prices[j];
     
    }

   }
  }
  inum[0]++;
  inum[1]++;

  return inum;
 }

 @Override
 public String toString() {
  int inum[] = getTwoitems();

  cases++;
  return "Case #" + cases + ": " + inum[0] + " " + inum[1];
 }

}

package myway;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.util.Scanner;

public class CreditManager {
 private static Scanner s;

 public CreditManager() {
 }

 public static void main(String[] args) {
  File f1;// This class is used for creation of files and directories,
    // file searching, file deletion etc.
  File f2=new File("A-small-output.out");
  
  f1 = new File("A-small-practice.in");
  // Java try block is used to enclose the code that might throw an
  // exception
  try {
   s = new Scanner(f1);
   PrintWriter pw=new PrintWriter(f2);
   int cases = s.nextInt(),tC;// total no. of cases
   tC=cases;
   int tCredit, tItems, tI;// t is used for total/tI is used as
         // temporary variable
   int prices[];
   int index = 0;// for movement in prices array
   int inum[]={1,2};
   int num=1;//like case #num
   
   while (cases != 0) {
    tCredit = s.nextInt();
    tI = tItems = s.nextInt();
    prices = new int[tItems];

    index = 0;// fresh start
    while (tI != 0) {
     prices[index++] = s.nextInt();
     tI--;
    }
    StoreCredit sc = new StoreCredit(tCredit, tItems, prices);
    
    inum= sc.getTwoitems();
    num=tC-cases+1;
    pw.println("Case #" +num + ": " + inum[0] + " " + inum[1]);
    cases--;
   }
   s.close();
   pw.close();

  } catch (FileNotFoundException e) {
   System.out.println("FileNotFoundException ");

  }
  // System.out.println("wel done");

 }

}