From e5a29fb8f815cb5f0bb79bc78653b19c21fe76c3 Mon Sep 17 00:00:00 2001 From: bmoussa Date: Thu, 21 Jan 2016 17:47:32 +0100 Subject: [PATCH] check consecutive element algorithm --- dp/checkconsecutiveelement.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 dp/checkconsecutiveelement.py diff --git a/dp/checkconsecutiveelement.py b/dp/checkconsecutiveelement.py new file mode 100644 index 0000000..0e2dae7 --- /dev/null +++ b/dp/checkconsecutiveelement.py @@ -0,0 +1,27 @@ +# this program check if the elements of the input array are consecutive or not + +def are_consecutive(input): + + min = 2 ** 31 - 1 + + for i in range(len(input)): + if input[i] < min: + min = input[i] + + for i in range(len(input)): + if abs(input[i]) - min >= len(input): + return False + if input[abs(input[i]) - min] < 0: + return False + input[abs(input[i]) - min] = - input[abs(input[i]) - min] + + for i in range(len(input)): + input[i] = abs(input[i]) + + return True + +input = [76, 78, 76, 77, 73, 74] +print(are_consecutive(input)) # return False + +input = [76, 77, 78, 79, 80] +print(are_consecutive(input)) # return True