// Arup Guha
// 5/12/09
// Solution to 2009 AP Computer Science A
// Free Response Section Problem 1: NumberCube

import java.util.*;

public class NumberCube {
	
	/*** This wasn't given, but I am writing an implementation of it so that
	 *   I can test my solution
	 **/
	 
	Random r;
	
	public NumberCube() {
		r = new Random();
	}
	
	public int toss() {
		return r.nextInt(6)+1;
	}
	
	// Solution to part (a)
	public static int[] getCubeTosses(NumberCube cube, int numTosses) {
		
		// Allocate space for the array to be returned.
		int[] ans = new int[numTosses];
		
		// Fill the array.
		for (int i=0; i<numTosses; i++)
			ans[i] = cube.toss();
			
		// Return it.
		return ans;
	}
	
	// Solution to part (b)
	public static int getLongestRun(int[] values) {
		
		// Initialize all of our necessary variables.
		int curMaxIndex = -1;
		int curRunLength = 1;
		int curStartIndex = 0;
		int curValue = values[0];
		int maxRun = 1;
		
		// Loop through each die roll.
		for (int i=1; i<values.length; i++) {
			
			// Our run continues.
			if (values[i] == curValue)
				curRunLength++;
				
			// Reset our max variables for the first seen run.
			if (curRunLength > 1 && curMaxIndex == -1) {
				curMaxIndex = curStartIndex;
				maxRun = curRunLength;
			}
			
			// Or, update this information if this is the longest run we've seen.
			if (curRunLength > maxRun) {
				curMaxIndex = curStartIndex;
				maxRun = curRunLength;
			}
			
			// If there is no match, we have to reset our current variables.
			if (values[i] != curValue) {
				curValue = values[i];
				curStartIndex = i;
				curRunLength = 1;
			}
			
		}
		
		// This is the answer they want.
		return curMaxIndex;
	}
	
	// For testing purposes.
	public static void print(int[] vals) {
		
		for (int i=0; i<vals.length; i++)
			System.out.print(vals[i]+" ");
		System.out.println();
	}
	
	
	// Test these methods.
	public static void main(String[] args) {
		
		// Test for part (b)
		NumberCube myDie = new NumberCube();
		int[] rolls = {1,5,5,4,3,1,2,2,2,2,6,1,3,3,5,5,5,5};
		System.out.println("The longest run for the sample starts at index "+getLongestRun(rolls));
		
		// Test for part (a) and (b)
		rolls = getCubeTosses(myDie, 40);
		print(rolls);
		System.out.println("The longest run for this array starts at index "+getLongestRun(rolls));
	
	}
}