-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy path17.2 Deleting From Array.java
69 lines (57 loc) · 1.77 KB
/
17.2 Deleting From Array.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Consider Aman is visiting Nehru Zoo. She has seen there are N elephants standing in a row. She wants to remove the elephants having the same height standing in consecutive.Write a program for Aman so that she can get the desired sequence of elephants.
Input Format
The first line will be containing one Integer representing a number of elephants N.
The second line will contain N integers representing the heights of the elephants.
Constraints
N>2 && N<30
Output Format
The desired sequence of elephants after removing elephants having the same height standing in consecutive.
Sample Input 0
12
4 7 9 9 8 5 7 7 6 5 5 5
Sample Output 0
4 7 9 8 5 7 6 5
Sample Input 1
1
Sample Output 1
Invalid Input
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String args[] ) throws Exception {
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
if(size>2 && size<30)
{
int [] elephant = new int[size];
for(int i=0;i<size;i++)
elephant[i]=sc.nextInt();
for(int i=0;i<size-1;i++)
{
if(elephant[i]==elephant[i+1])
{
int j = i+1;
while(j<size-1)
{
elephant[j] = elephant[j+1];
j++;
}
size--;
i--;
}
}
for(int i=0;i<size;i++)
System.out.print(elephant[i]+" ");
}
else
{
System.out.print("Invalid Input");
}
}
}