Skip to content

Commit d4edb3b

Browse files
committed
Jan23
1 parent 74eb62b commit d4edb3b

File tree

2 files changed

+67
-16
lines changed

2 files changed

+67
-16
lines changed

src/Jan23/MissingElementInArray.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Design a method to find the missing element in the array.
2+
3+
4+
package Jan23;
5+
import java.util.*;
6+
public class MissingElementInArray {
7+
public static void main(String[] args) {
8+
Scanner scan = new Scanner(System.in);
9+
System.out.println("Enter the size of the array");
10+
int size= scan.nextInt();
11+
int[] a= new int[size];
12+
System.out.println("Enter the elements of the array");
13+
for(int i=0;i<a.length;i++) {
14+
a[i]=scan.nextInt();
15+
}
16+
scan.close();
17+
missing(a);
18+
}
19+
public static void missing(int[] a) {
20+
Arrays.sort(a);
21+
int min=a[0];
22+
int max=a[a.length-1];
23+
for(int i=0;i<a.length&&min<=max;i++) {
24+
if(min!=a[i]) {
25+
System.out.println("The missing element is "+min);
26+
break;
27+
}
28+
else {
29+
min++;
30+
}
31+
}
32+
}
33+
}

src/jan4/rough.java

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
11
package jan4;
2-
2+
import java.util.*;
33
public class rough {
4-
public static void main(String[] args) {
5-
int num=5;
6-
for(int row=1;row<=num;row++) {
7-
for(int col=1;col<=2*num-1;col++) {
8-
if((col+row>num && col-row<=num-1)) {
9-
System.out.print(" * ");
10-
}
11-
else {
12-
System.out.print(" ");
13-
}
14-
}
15-
System.out.println();
16-
}
17-
}
18-
}
4+
5+
public static void missing(int[] a) {
6+
if (a == null || a.length == 0) {
7+
System.out.println("Array is empty or null.");
8+
return;
9+
}
10+
11+
// Sort the array
12+
Arrays.sort(a);
13+
int min = a[0];
14+
int max = a[a.length - 1];
15+
16+
// Check for missing elements in the range [min, max]
17+
for (int j = min; j <= max; j++) {
18+
boolean miss = true; // Reset miss for each j
19+
for (int i = 0; i < a.length; i++) {
20+
if (j == a[i]) {
21+
miss = false; // Found the number, set miss to false
22+
break; // Exit the inner loop
23+
}
24+
}
25+
if (miss) { // If miss is still true, print the missing number
26+
System.out.println("The missing element is " + j);
27+
}
28+
}
29+
}
30+
31+
public static void main(String[] args) {
32+
int[] array = {1, 2, 4, 6, 7, 9};
33+
missing(array);
34+
}
35+
}
36+

0 commit comments

Comments
 (0)