diff --git a/src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java b/src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java new file mode 100644 index 0000000000..8b696881a3 --- /dev/null +++ b/src/main/java/com/ossez/codebank/interview/KayakCountUpDown.java @@ -0,0 +1,58 @@ +package com.ossez.codebank.interview; + +import java.util.ArrayList; +import java.util.List; + +/** + * + * @author YuCheng + * + */ +public class KayakCountUpDown { + + static int minNumber = 0; + static int maxNumber = 0; + int tmpN = 0; + List retList = new ArrayList(); + + public List countUp(int start, int end) { + maxNumber = end; + tmpN = start; + moveUp(0); + retList.add(end); + return retList; + + } + + public List countUpDown(int start, int end) { + minNumber = start; + maxNumber = end; + tmpN = start; + + moveUp(0); + retList.add(end); + + moveDown(1); + return retList; + + } + + private void moveUp(int n) { + retList.add(tmpN); + tmpN++; + if (tmpN != maxNumber) { + moveUp(tmpN + 1); + } + + } + + private void moveDown(int n) { + tmpN = (maxNumber - n ); + retList.add(tmpN); + + if (tmpN != minNumber) { + moveDown(n + 1); + } + } + +}