MaxCounters

Calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum


Solution:

Solution to Codility's Maximum Counters problem which is from the Codility Lesson 4: Counting Elements and, is solved in Java 8 with 77% performance and correctness scores. The goal here is to calculate the values of counters after applying all alternating operations: increase counter by 1; set value of all counters to current maximum You can find the question of this MaxCounters problem in the Codility website.


package Codility.Lesson4;

import java.util.*;

public class MaxCounters {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int input[] = { 3, 4, 4, 6, 1, 4, 4 };
		int[] result = solution(5, input);
		System.out.println(result);
	}

	public static int[] solution(int N, int[] A) {
		// write your code in Java SE 8
		int[] arr = new int[N];
		int maxCounter = 0;
		int newVal = 0;

		for (int i = 0; i < A.length; i++) {
			if (A[i] == N + 1) {
				for (int a = 0; a < arr.length; a++) {
					arr[a] = maxCounter;
				}
			} else if (A[i] <= N) {
				newVal = arr[A[i] - 1] + 1;
				arr[A[i] - 1] = newVal;
				if (maxCounter < newVal) {
					maxCounter = newVal;
				}
			}
		}
		return arr;
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *