@@ -0,0 +1,37 @@
package Algorithms.Implementation.EqualizetheArray;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int[] k = new int[n];
for(int i = 0; i < n; i ++){
k[i] = scanner.nextInt();
}
System.out.println(minDeletions(k));
}

public static int minDeletions(int[] a) {
int max = 1;
Map<Integer, Integer> nums = new HashMap<>();
for (int i : a)
if (!nums.containsKey(i))
nums.put(i, 1);
else {
nums.put(i, nums.get(i) + 1);
if (max < nums.get(i))
max = nums.get(i);
}
return a.length - max;
}
}
@@ -0,0 +1,22 @@
package Algorithms.Implementation.ExtraLongFactorials;

import java.math.BigInteger;
import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
int n = (new Scanner(System.in)).nextInt();
BigInteger factorial = new BigInteger("1");
while (n>1){
factorial = factorial.multiply(BigInteger.valueOf(n));
n--;
}
System.out.println(factorial);
}
}
@@ -0,0 +1,40 @@
package Algorithms.Implementation.FairRations;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
int B[] = new int[N];
int sum = 0;
for(int B_i=0; B_i < N; B_i++){
B[B_i] = in.nextInt();
sum+=B[B_i];
}

int count = 0;
if(sum % 2 == 1){
System.out.println("NO");
}
else{
for(int i = 0; i<N; i++){
if(B[i] % 2 != 0){
B[i] = B[i] + 1;
B[i+1] = B[i+1] + 1;
count+=2;
}



}
System.out.println(count);
}
}
}
@@ -0,0 +1,28 @@
package Algorithms.Implementation.FindDidgits;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
int n = in.nextInt();

int r = n;
int count = 0;
while(r > 0){
if(r % 10 != 0 && n % (r % 10) == 0) count++;
r = r / 10;
}
System.out.println(count);
}

}
}
@@ -0,0 +1,100 @@
package Algorithms.Implementation.FlatlandSpaceStations;

import java.util.LinkedList;
import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {

Scanner in = new Scanner(System.in);
LinkedList<Integer> q=new LinkedList<Integer>();
int n = in.nextInt();
int m = in.nextInt();
int c[] = new int[m];
for(int i=0;i<n;i++){
q.add(i);



}
// System.out.println(q);
for(int c_i=0; c_i < m; c_i++){
c[c_i] = in.nextInt();
// System.out.println(c[c_i]);
int index=q.indexOf(c[c_i]); q.set(index,-1);
}
if(n==m){
System.out.println("0");
}
// System.out.println((11/2));
else{
int max=0;int min;
//System.out.println(q);
int size=q.size();
int flag=0;
int previous=(-2);
// System.out.println(size);
for(int i=0;i<size;i++){
int k=q.poll();
// System.out.println(i+" "+k);
//System.out.println(k);
if(i==(size-1)&& k!=(-1)){
int max2=Math.abs(i-previous);
if(max2>max){
max=max2;
}

}
else if(i!=0 &&previous==(-2) && k==(-1)){

previous=i;
flag=1;
max=i;
}
else if(i==0 && k==(-1) && previous!=(-1)){
previous=0;
// System.out.println(i+" "+"hit1");
flag=1;
}
else if(i!=0 && flag==1 && k==(-1) && previous!=(-1)){
int pattern=((i+previous)/2);
if(pattern!=previous){
int min1=Math.abs(pattern-previous);
int min2=Math.abs(pattern-i);
// System.out.println(min1+" "+min2+" minimiumhit");
if(min1<=min2){
min=min1;
}
else{
min=min2;
}
if(min>max){
max=min;}
}
previous=i;
//System.out.println(i+" "+"hit2"+max);
}
else if(i!=0 && flag==0 && k==(-1)&& previous!=(-1)){
flag=1;
previous=i;
// System.out.println(i+" "+"hit3");

}
// System.out.println(max);
}

System.out.println(max);
}





}
}
@@ -0,0 +1,33 @@
package Algorithms.Implementation.HappyLadybugs;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
}
/* Python
def calculate(n, b):
if b.count("_") == 0 and compare(b):
return "NO"
for a in b:
if a != "_" and b.count(a) == 1:
return "NO"
return "YES"
def compare(b):
if len(b) == 1:
return True
for x in set(b):
if x * b.count(x) != b[b.index(x):b.index(x) + b.count(x)]:
return True
return False
for _ in range(int(input())):
n = int(input())
b = input()
print(calculate(n, b))
*/
@@ -0,0 +1,25 @@
package Algorithms.Implementation.JumpingOnTheClouds;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int c[] = new int[n];
for(int i=0; i < n; i++){
c[i] = in.nextInt();
}
int count = -1;
for (int i = 0; i < n; i++, count++) {
if (i<n-2 && c[i+2]==0) i++;
}
System.out.println(count);
}
}
@@ -0,0 +1,31 @@
package Algorithms.Implementation.LibraryFine;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int aDay = sc.nextInt();
int aMonth = sc.nextInt();
int aYear = sc.nextInt();
int eDay = sc.nextInt();
int eMonth = sc.nextInt();
int eYear = sc.nextInt();
int fine = 0;
if(aYear == eYear) {
if(aMonth == eMonth) {
if(aDay > eDay) {
fine = 15 * (aDay - eDay);
}
} else if(aMonth > eMonth) fine = 500 * (aMonth - eMonth);
} else if(aYear > eYear) fine = 10000 * (aYear - eYear);
System.out.println(fine);
sc.close();
}
}
@@ -0,0 +1,42 @@
package Algorithms.Implementation.LisasWorkbook;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner sn = new Scanner(System.in);
int n = sn.nextInt();
int k = sn.nextInt();
int [] m = new int[n];
int j=2;int x=0; int y=0;
int count=0 ;

for(int i=0 ; i<n ; i++){
m[i] = sn.nextInt();
}

for(int i=0 ; i<n ; i++){
//System.out.println("x:"+x);
//System.out.print("|||j:"+j);
x = x+((j-2)/k)+1;
//System.out.println("check x:"+x);
// if((j != 0)&&(j%k == 0))
// x--;
for(j=1 ; j<=m[i] ; j++){
y = x+(j-1)/k;
if(j==y)
count++;
}
}
System.out.println(count);


/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
}
}
@@ -0,0 +1,35 @@
package Algorithms.Implementation.ManasaandStones;

import java.util.Scanner;
import java.util.TreeSet;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner stdin = new Scanner(System.in);
int t = stdin.nextInt();
for (int t_i = 0; t_i < t; t_i++) {
int n = stdin.nextInt();
int a = stdin.nextInt();
int b = stdin.nextInt();

TreeSet<Integer> finalStones = new TreeSet<Integer>();

for (int i = 0; i < n; i++) {
int sum = (i * a) + ((n - 1 - i) * b);
finalStones.add(sum);
}

for (Integer e : finalStones) {
System.out.print(e + " ");
}
System.out.println();
}
}
}

@@ -0,0 +1,32 @@
package Algorithms.Implementation.MinimumDistances;

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<Integer, Integer> hm = new HashMap<>();
int minDist= Integer.MAX_VALUE;
int n = in.nextInt();
int[] arr = new int[n];
for(int i=0; i < n; i++){
arr[i] = in.nextInt();
if(hm.containsKey(arr[i])) {
int x=hm.get(arr[i]);
int dist = i - x;
if(dist < minDist) minDist = dist;
}
else hm.put(arr[i],i);
}
if(minDist == Integer.MAX_VALUE) minDist=-1;
System.out.println(minDist);
}
}
@@ -0,0 +1,43 @@
package Algorithms.Implementation.ModifiedKaprekarNumbers;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int p = scan.nextInt();
int q = scan.nextInt();
scan.close();

boolean foundKaprekar = false;
for (int num = p; num <= q; num++) {
if (isKaprekar(num)) {
foundKaprekar = true;
}
}
if (!foundKaprekar) {
System.out.println("INVALID RANGE");
}
}

private static boolean isKaprekar(int num) {
long squared = (long) num * num;
String str = String.valueOf(squared);
String left = str.substring(0, str.length() / 2);
String right = str.substring(str.length() / 2);
int numL = (left.isEmpty()) ? 0 : Integer.parseInt(left);
int numR = (right.isEmpty()) ? 0 : Integer.parseInt(right);
if (numL + numR == num) {
System.out.print(num + " ");
return true;
} else {
return false;
}
}
}
@@ -0,0 +1,39 @@
package Algorithms.Implementation.NonDivisibleSubSet;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k=in.nextInt();
int count=0;
int a[]=new int[n];
for(int i=0; i < n; i++){
a[i] = in.nextInt();
}
int b[]=new int[k+1];

for(int i=0;i<n;i++)
{
b[a[i]%k]=b[a[i]%k]+1;
}
int cond=(k%2==0)?k/2:(k/2)+1;
for(int j=0;j<cond;j++)
{
if(b[0]!=0&&j==0)
count++;
else
count+=(b[j]>b[k-j])?b[j]:b[k-j];
}
if(k%2==0)
count+=1;
System.out.println(count);
}
}
@@ -0,0 +1,51 @@
package Algorithms.Implementation.OrganizingContainersofBalls;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int q = in.nextInt();
for(int a0 = 0; a0 < q; a0++){
int n = in.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i=0; i<n; i++)
{
for(int j=0; j<n; j++)
{
int x = in.nextInt();
a[i] += x;
b[j] += x;
}
}
String pts = "Possible";
for(int i=0;i<n;i++)
{
int j=0;
for(j=i;j<n;j++)
{
if(a[i] == b[j])
{
int temp = b[j];
b[j] = b[i];
b[i] = temp;
break;
}
}
if(j==n)
{
pts = "Impossible";
break;
}
}
System.out.println(pts);
}
}
}
@@ -0,0 +1,77 @@
package Algorithms.Implementation.QueensAttackII;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int rQueen = in.nextInt();
int cQueen = in.nextInt();

int up,left,right,down,uld,urd,dld,drd;
//set default from queen to each side
up = n-rQueen;
down = rQueen-1;
left = cQueen-1;
right = n-cQueen;
uld=up<left?up:left; //uoper left diagonal
urd=up<right?up:right; //upper right diagonal
dld=down<left?down:left;//down left diagonal
drd=down<right?down:right;//down right diagonal

for(int a0 = 0; a0 < k; a0++){
int rObstacle = in.nextInt();
int cObstacle = in.nextInt();
// your code goes here
if(cObstacle==cQueen){
//vertical
int path = Math.abs(rObstacle-rQueen)-1;
if(rQueen<rObstacle){

up=up<path?up:path;
}else{
down=down<path?down:path;
}
}else if(rObstacle==rQueen){
//horizontal
int path = Math.abs(cObstacle-cQueen)-1;
if(cQueen<cObstacle){
right = right<path?right:path;
}else{
left=left<path?left:path;
}
}else{
int path = Math.abs(cObstacle-cQueen)-1;
int path2= Math.abs(rObstacle-rQueen)-1;

if(path==path2){
if(cQueen<cObstacle && rQueen<rObstacle){

//urd
urd=urd<path?urd:path;
}else if(cQueen>cObstacle && rQueen<rObstacle){
//uld
uld=uld<path?uld:path;
}else if(cQueen<cObstacle && rQueen>rObstacle){
//drd
drd=drd<path?drd:path;
}else{
dld=dld<path?dld:path;
}
}
}
}

int total = up+down+left+right+urd+uld+drd+dld;
System.out.println(total);

}
}
@@ -0,0 +1,30 @@
package Algorithms.Implementation.RepeatedString;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
long n = in.nextLong();
long count =0;
for(char c : s.toCharArray())
if(c == 'a')
count++;

long factor = (n/s.length());
long rem = (n%s.length());
count = factor*count ;
for(int i=0;i<rem;i++)
if(s.charAt(i)=='a')
count++;
System.out.println(count);

}
}
@@ -0,0 +1,47 @@
package Algorithms.Implementation.ServiceLane;

import java.util.ArrayList;
import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner in = new Scanner(System.in);
int n = in.nextInt(); //length of freeway
int t = in.nextInt(); //number of testcases
ArrayList<Integer> arr = new ArrayList<Integer>();
in.nextLine();
String str = in.nextLine();
Scanner inStr = new Scanner(str);

while(inStr.hasNextInt())
{

arr.add(inStr.nextInt());

}

for(int a0 = 0; a0 < t; a0++)
{
int x = in.nextInt();
int y = in.nextInt();
int smallest = 10;

for(int i = x; i <= y; i++)
{
if(smallest > arr.get(i))
{
smallest = arr.get(i);
}

}
System.out.println(smallest);
}
}
}
@@ -0,0 +1,22 @@
package Algorithms.Implementation.SherlockAndSquares;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t-- > 0)
{
int a = scan.nextInt();
int b = scan.nextInt();
System.out.println((int) Math.floor(Math.sqrt(b)) - (int) Math.ceil(Math.sqrt(a)) + 1);
}
}
}
@@ -0,0 +1,18 @@
package Algorithms.Implementation.StrangeCounter;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long t = in.nextLong(), n = 2;
while (3 * (n - 1) < t) n = 2 * n;
System.out.println((3 * (n - 1) - t + 1));
}
}
@@ -0,0 +1,26 @@
package Algorithms.Implementation.TaumandBday;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a = 0; a < t; a++){
long b = in.nextLong();
long w = in.nextLong();
long x = in.nextLong();
long y = in.nextLong();
long z = in.nextLong();
x = x>y? ((x-y>z)? y+z : x) : x;
y = y>x? ((y-x>z)? x+z : y) : y;
System.out.println(b*x+w*y);
}
}
}
@@ -0,0 +1,91 @@
package Algorithms.Implementation.TheGridSearch;

import java.util.Scanner;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {
public static void main(String[] args) {
}
}

/* JavaScript
process.stdin.resume();
process.stdin.setEncoding('ascii');
var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;
process.stdin.on('data', function (data) {
input_stdin += data;
});
process.stdin.on('end', function () {
input_stdin_array = input_stdin.split("\n");
main();
});
function readLine() {
return input_stdin_array[input_currentline++];
}
/////////////// ignore above this line ////////////////////
function main() {
var t = parseInt(readLine());
for(var a0 = 0; a0 < t; a0++){
var R_temp = readLine().split(' ');
var R = parseInt(R_temp[0]);
var C = parseInt(R_temp[1]);
var G = [];
for(var G_i = 0; G_i < R; G_i++){
G[G_i] = readLine();
}
var r_temp = readLine().split(' ');
var r = parseInt(r_temp[0]);
var c = parseInt(r_temp[1]);
var P = [];
for(var P_i = 0; P_i < r; P_i++){
P[P_i] = readLine();
}
// -------------- Code below this point -----------
var patt_len = P.length;
var offset = (G[0].length - P[0].length)+1;
var pattern = '';
var regex;
var result;
var i;
var str = '';
// Simply concatenate grid using . as separaor
// make sure u use some kind of separator or
// it won't work, you can use anything but
// numbers here as separator
str = G.join('.');
// Here I build RegEx pattern
for(i = 0; i < patt_len; i++)
{
if(i !== (patt_len - 1)){
pattern += '(' + P[i] + ').{' + offset + '}';
} else {
pattern += '(' + P[i] + ')';
}
}
// And in end we run it on our strig
// and see if we have match or not
regex = new RegExp(pattern);
result = str.match(regex);
console.log(result !== null ? 'YES' : 'NO');
}
}
*/
@@ -0,0 +1,29 @@
package Algorithms.Implementation.TheTimeinWords;

/**
* Solution class
*
* @author Patrick Shinn
* @version 4/20/17
*/
public class Solution {

}
/*
Python code
ByOne=[0,'one','two','three','four','five','six','seven','eight','nine','ten','eleven','twelve',
'thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','ninteen','twenty']
ByQua=["%s o' clock", "quarter past %s", "half past %s", "quarter to %s"]
for i in range(1,10):
ByOne.append('twenty %s'%ByOne[i])
h,m=int(input()),int(input())
hour=ByOne[h] if (m<31) else ByOne[h+1]
if not m%15:
print(ByQua[m//15] % hour)
elif m<30:
print("%s minutes"%ByOne[m]+"s"*(m==1)+ " past %s"%hour)
else:
print("%s minutes"%ByOne[60-m]+"s"*(m==59)+ " to %s"%hour)
*/