Skip to content

Commit d8190f5

Browse files
Create 17.1 Inserting In Array.java
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"); } } }
1 parent bc8456a commit d8190f5

File tree

1 file changed

+82
-0
lines changed

1 file changed

+82
-0
lines changed

17.1 Inserting In Array.java

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
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.
3+
4+
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.
5+
6+
Output: 3 6 5 7 9 8 7
7+
8+
Input Format
9+
10+
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.
11+
12+
Constraints
13+
14+
N>2 && N<20
15+
16+
Output Format
17+
18+
The desired sequence of students after inserting students having the average height of two students with odd height standing in consecutive in between them.
19+
20+
Sample Input 0
21+
22+
10
23+
4 7 9 8 5 7 6 5 2 4
24+
Sample Output 0
25+
26+
4 7 8 9 8 5 6 7 6 5 2 4
27+
Sample Input 1
28+
29+
1
30+
Sample Output 1
31+
32+
Invalid Input
33+
*/
34+
35+
import java.io.*;
36+
import java.util.*;
37+
import java.text.*;
38+
import java.math.*;
39+
import java.util.regex.*;
40+
41+
public class Solution {
42+
public static void main(String args[] ) throws Exception {
43+
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
44+
Scanner sc = new Scanner(System.in);
45+
46+
int n = sc.nextInt();
47+
if(n>2 && n<20)
48+
{
49+
int []students = new int[50];
50+
Arrays.fill(students,-1);
51+
for(int i=0;i<n;i++)
52+
{
53+
students[i] = sc.nextInt();
54+
}
55+
56+
for(int i=1,j=0;i<n && j<n;i++,j++)
57+
{
58+
if(students[j]%2!=0 && students[i]%2!=0)
59+
{
60+
int avg = (students[i]+students[j])/2;
61+
int k=n;
62+
while(k>i)
63+
{
64+
students[k] = students[k-1];
65+
k--;
66+
}
67+
students[k] = avg;
68+
n++;
69+
j++;
70+
i++;
71+
}
72+
}
73+
for(int i=0;i<n;i++)
74+
System.out.print(students[i]+" ");
75+
76+
}
77+
else
78+
{
79+
System.out.print("Invalid Input");
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)