Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 6046746

Browse files
author
Ram swaroop
committed
consecutive elements : initial commit
1 parent 1ef3296 commit 6046746

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package me.ramswaroop.arrays;
2+
3+
/**
4+
* Created by IntelliJ IDEA.
5+
*
6+
* @author: ramswaroop
7+
* @date: 8/28/15
8+
* @time: 10:32 AM
9+
*/
10+
public class ConsecutiveElements {
11+
12+
/**
13+
*
14+
* @param a
15+
* @return
16+
*/
17+
public static boolean areConsecutiveElements(int[] a) {
18+
int min = a[0], max = a[0];
19+
int[] visitedArray = new int[a.length];
20+
21+
for (int i = 1; i < a.length; i++) {
22+
if (a[i] < min) {
23+
min = a[i];
24+
}
25+
if (a[i] > max) {
26+
max = a[i];
27+
}
28+
}
29+
30+
if (a.length != max - min + 1) {
31+
return false;
32+
}
33+
34+
for (int i = 0; i < a.length; i++) {
35+
if (visitedArray[a[i] - min] == 0) {
36+
visitedArray[a[i] - min] = a[i];
37+
} else {
38+
return false;
39+
}
40+
}
41+
42+
return true;
43+
}
44+
45+
public static void main(String a[]) {
46+
System.out.println(areConsecutiveElements(new int[]{5, 4, 3, 2, 1}));
47+
System.out.println(areConsecutiveElements(new int[]{67, 68, 69, 72, 70, 71}));
48+
System.out.println(areConsecutiveElements(new int[]{67, 68, 69, 72, 70, 71, 70}));
49+
System.out.println(areConsecutiveElements(new int[]{8, 5, 2, 4, 3, 1}));
50+
}
51+
}

0 commit comments

Comments
 (0)