From 3c9d6ed0baaea6738e42f25f111bc8e38fdfbf65 Mon Sep 17 00:00:00 2001 From: Viveka Date: Mon, 24 Aug 2015 23:21:44 +0530 Subject: [PATCH] Update to handle an edge case Added an update to handle the case when array size is not a multiple of 3. Without this the program will run into an infinite loop! --- solutions/java/ConvertArray.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/solutions/java/ConvertArray.java b/solutions/java/ConvertArray.java index c3ce1ab..422b188 100644 --- a/solutions/java/ConvertArray.java +++ b/solutions/java/ConvertArray.java @@ -8,6 +8,11 @@ public int getOriginalIndex(int currentIndex, int length) { } public int[] convert(int[] input) { + // When array size is less than 3, returns an error. + if (input.length % 3 != 0 ) { + System.out.println("Error! Array cannot be divided into three equal parts"); + return; + } for (int i = 0; i < input.length; ++i) { int originalIndex = getOriginalIndex(i, input.length/3); while (originalIndex < i) {