Showing posts with label Arrays. Show all posts
Showing posts with label Arrays. Show all posts

Tuesday, July 4, 2017

CyclicRotation

import java.util.Arrays;
import java.util.stream.Collectors;

public class CyclicRotation {

     public static void main(String[] args) {
          print(solution(3, 1, 2, 3, 4, 5));
     }

     public static int[] solution(int k, int... a) {
          int l = a.length;
          int[] r = new int[l];
          for (int i = 0; i < l; i++) {
               r[(i + k) % l] = a[i];
          }
          return r;
     }

     public static void print(int... a) {
          System.out.println("[" + Arrays.stream(a).boxed().map(x -> x.toString()).collect(Collectors.joining(",")) + "]");
     }
}


Thursday, June 29, 2017

Calculate Statistics

This task will process a sequence of integer numbers
to determine the following statistics:

    o) minimum value
    o) maximum value
    o) number of elements in the sequence
    o) average value

For example: [6, 9, 15, -2, 92, 11]

    o) minimum value = -2
    o) maximum value = 92
    o) number of elements in the sequence = 6
    o) average value = 21.833333



import java.util.Arrays;
import java.util.IntSummaryStatistics;

public class CalculateStatistics {
    
     public static void main(String[] args) {
          double[] stats = solution(6, 9, 15, -2, 92, 11);
          System.out.println(String.format("Minimum=%f, Maximum=%f, Count=%f, Average=%f", stats[0], stats[1], stats[2], stats[3]));
     }
    
     public static double[] solution(int...numbers){
          double[] stats = new double[4];
          IntSummaryStatistics summaryStatistics = Arrays.stream(numbers).summaryStatistics();
          stats[0] = summaryStatistics.getMin();
          stats[1] = summaryStatistics.getMax();
          stats[2] = summaryStatistics.getCount();
          stats[3] = summaryStatistics.getAverage();
          return stats;
     }
}