Solution In Java-8:

From JavaWorld:

Java 8 introduced Java developers to functional programming with lambda expressions. This Java release effectively notified developers that it’s no longer sufficient to think about Java programming only from the imperative, object-oriented perspective. A Java developer must also be able to think and code using the declarative functional paradigm.

Before understanding the Java-8 solution, let’s try to understand Java 8 functional interfaces which it provides in general:

Function: A function that accepts one argument type T and produces a result R. BiFunction: A function that accepts two arguments of type T and type U and produces a result R. Supplier: A function that doesn’t accept any argument but produces a result R. Consumer: A function that accepts one argument of type T and doesn’t produce any result. Predicate: A function that accepts one argument type T and produces a Boolean. Operator: A function that accepts arguments of type T and produces the same type result.

After understanding the functional interfaces, let's understand how to use them to solve the problem. Function and BiFunction are used to achieve the solution.

To start with we implement class Pair to use which takes two arguments of type A and type B. And there are three methods in class JumpingMario:

loopWithIndex

This method accepts the number of times to iterate and then the block of function(Function<Integer, T>) to execute which takes in Integer and returns type T. We use integer stream to get a range from 1 to n, and then foreach we execute the function passed. sliding

This method is directly available as part of collections in Scala, but need to be implemented in case of Java. We implement it by taking a Vector<A> c and sliding window length int n parameters and then produce Vector<Vector<A>> result. The implementation is straight forward, where we create a Vector<Vector<A>> of size n-1and then iterate through the input vector and extract each element Vector<A> using the subList method. foldLeft

This method as well is available as part of collections in Scala, but need to be implemented in case of Java. The method takes in Stream<A> a, B b(Used for initial value for foldLeft), BiFunction<A, B, B> biFunction

as arguments and produces a result of type B by applying biFunction on each element A of the Stream and another argument of type B. We iterate through the stream, by converting that into a list (Another Approach: Stream can also be iterated through forEach) using the Collectors.toList, and then apply the biFunction to produce the result B.

Now let's get into the main method where we read the inputs and then compute the result. The biFunction implemented in the main method takes Pair<Integer, Integer> as two arguments and produces Pair<Integer, Integer>, and this will be passed as input to the foldLeft method, which has the logic of computing the result as a Pair<Integer, Integer>. The loopWithIndex method needs us to pass the implementation for Function<Integer, T> to be applied in this case we will be providing a Function<Integer, String> implementation, where the result of the function is used to print the solution forEach iteration.

We use sliding method to compute the sliding-window two-sized vectors, which are mapped as a Pair<Integer, Integer>, as the window size is two. Then we use foldLeft by passing the biFunction using which we compute the resulting pair which contains the number of high-jumps and low-jumps respectively and return the result in an expected manner.