Skip to content

Commit 414b670

Browse files
Create 17.2 Deleting From Array.java
int j = i+1; while(j<size-1) { elephant[j] = elephant[j+1]; j++; } size--; i--;
1 parent d8190f5 commit 414b670

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

17.2 Deleting From Array.java

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
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.
3+
4+
Input Format
5+
6+
The first line will be containing one Integer representing a number of elephants N.
7+
The second line will contain N integers representing the heights of the elephants.
8+
9+
Constraints
10+
11+
N>2 && N<30
12+
13+
Output Format
14+
15+
The desired sequence of elephants after removing elephants having the same height standing in consecutive.
16+
17+
Sample Input 0
18+
19+
12
20+
4 7 9 9 8 5 7 7 6 5 5 5
21+
Sample Output 0
22+
23+
4 7 9 8 5 7 6 5
24+
Sample Input 1
25+
26+
1
27+
Sample Output 1
28+
29+
Invalid Input
30+
*/
31+
import java.io.*;
32+
import java.util.*;
33+
import java.text.*;
34+
import java.math.*;
35+
import java.util.regex.*;
36+
37+
public class Solution {
38+
public static void main(String args[] ) throws Exception {
39+
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
40+
Scanner sc = new Scanner(System.in);
41+
int size = sc.nextInt();
42+
if(size>2 && size<30)
43+
{
44+
int [] elephant = new int[size];
45+
for(int i=0;i<size;i++)
46+
elephant[i]=sc.nextInt();
47+
for(int i=0;i<size-1;i++)
48+
{
49+
if(elephant[i]==elephant[i+1])
50+
{
51+
int j = i+1;
52+
while(j<size-1)
53+
{
54+
elephant[j] = elephant[j+1];
55+
j++;
56+
}
57+
size--;
58+
i--;
59+
}
60+
}
61+
for(int i=0;i<size;i++)
62+
System.out.print(elephant[i]+" ");
63+
}
64+
else
65+
{
66+
System.out.print("Invalid Input");
67+
}
68+
}
69+
}

0 commit comments

Comments
 (0)