-
Notifications
You must be signed in to change notification settings - Fork 2
/
17.1 Inserting In Array.java
82 lines (64 loc) · 2.47 KB
/
17.1 Inserting In 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
70
71
72
73
74
75
76
77
78
79
80
81
82
/*
Consider a physical trainer is arranging the students for physical training in a school. There are N students standing in a row for physical training. He wants to insert a student with average height between the two students who are having odd height standing in consecutive. Write a program for a physical trainer so that he can get the desired sequence of students.
Example: if the 5 students are having values: 3 6 5 9 7 then 5 and 9 are the students with odd height so the student with height 7 (average of 5 and 9) should be inserted between 5 and 9. Similarly, 9 and 7 are the 2 students with odd heights so student with height 8 should be inserted between 9 and 7.
Output: 3 6 5 7 9 8 7
Input Format
The first line will be containing one Integer representing a number of students N. The second line will contain N integers representing the heights of the students.
Constraints
N>2 && N<20
Output Format
The desired sequence of students after inserting students having the average height of two students with odd height standing in consecutive in between them.
Sample Input 0
10
4 7 9 8 5 7 6 5 2 4
Sample Output 0
4 7 8 9 8 5 6 7 6 5 2 4
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 n = sc.nextInt();
if(n>2 && n<20)
{
int []students = new int[50];
Arrays.fill(students,-1);
for(int i=0;i<n;i++)
{
students[i] = sc.nextInt();
}
for(int i=1,j=0;i<n && j<n;i++,j++)
{
if(students[j]%2!=0 && students[i]%2!=0)
{
int avg = (students[i]+students[j])/2;
int k=n;
while(k>i)
{
students[k] = students[k-1];
k--;
}
students[k] = avg;
n++;
j++;
i++;
}
}
for(int i=0;i<n;i++)
System.out.print(students[i]+" ");
}
else
{
System.out.print("Invalid Input");
}
}
}