-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathArray-2
More file actions
131 lines (110 loc) · 4.59 KB
/
Array-2
File metadata and controls
131 lines (110 loc) · 4.59 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
Q1. Given an array arr[] of size n, find the first repeating element. The element should occur more than
once and the index of its first occurrence should be the smallest. If no repeating element exists, print -1.
import java.util.*;
public class FirstRepeatingElement {
public static int findFirstRepeating(int[] arr, int n) {
// Create an empty hash set
Set<Integer> set = new HashSet<>();
// Traverse the input array from left to right
for (int i = 0; i < n; i++) {
// If element is already in hash set, we found our first repeating element
if (set.contains(arr[i])) {
return arr[i];
} else {
// Otherwise, add the element to the hash set
set.add(arr[i]);
}
}
// If no repeating element is found, return -1
return -1;
}
public static void main(String[] args) {
int[] arr = {10, 5, 3, 4, 3, 5, 6};
int n = arr.length;
int firstRepeating = findFirstRepeating(arr, n);
if (firstRepeating == -1) {
System.out.println("No repeating element found");
} else {
System.out.println("First repeating element is " + firstRepeating);
}
}
}
Q2. Given an array of positive and negative numbers, arrange them in an alternate fashion such that
every positive number is followed by a negative and vice-versa maintaining the order of appearance.
The number of positive and negative numbers need not be equal. Begin with a negative number.
If there are more positive numbers, they appear at the end of the array. If there are more negative
numbers, they too appear at the end of the array.
public class AlternatePositiveNegative {
public static void alternate(int[] arr) {
int n = arr.length;
// Find the index of the first negative element
int negIndex = -1;
for (int i = 0; i < n; i++) {
if (arr[i] < 0) {
negIndex = i;
break;
}
}
// Rearrange the array in alternate fashion
int i = 0;
int j = negIndex;
while (i < j && j < n) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i += 2;
j += 1;
}
}
public static void main(String[] args) {
int[] arr = {1, -3, 5, 6, -7, 8, -4, -2};
alternate(arr);
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
Q3. Minimum Platforms
Given arrival and departure times of all trains that reach a railway station. Find the minimum number
of platforms required for the railway station so that no train is kept waiting.
Consider that all the trains arrive on the same day and leave on the same day. Arrival and departure
time can never be the same for a train but we can have arrival time of one train equal to departure time
of the other. At any given instance of time, same platform can not be used for both departure of a train
and arrival of another train. In such cases, we need different platforms.
import java.util.*;
public class MinimumPlatforms {
public static int findMinimumPlatforms(int[] arr, int[] dep, int n) {
// Sort the arrival and departure arrays in non-decreasing order
Arrays.sort(arr);
Arrays.sort(dep);
// Initialize the minimum number of platforms needed and the current number of platforms in use
int minPlatforms = 1;
int currentPlatforms = 1;
// Initialize pointers for the arrival and departure arrays
int i = 1;
int j = 0;
// Traverse the arrival and departure arrays
while (i < n && j < n) {
// If the next train arrives before the previous train departs, we need an additional platform
if (arr[i] <= dep[j]) {
currentPlatforms++;
i++;
}
// If the next train arrives after the previous train departs, we can free up a platform
else {
currentPlatforms--;
j++;
}
// Update the minimum number of platforms needed
minPlatforms = Math.max(minPlatforms, currentPlatforms);
}
return minPlatforms;
}
public static void main(String[] args) {
int[] arr = {900, 940, 950, 1100, 1500, 1800};
int[] dep = {910, 1200, 1120, 1130, 1900, 2000};
int n = arr.length;
int minPlatforms = findMinimumPlatforms(arr, dep, n);
System.out.println("Minimum number of platforms needed is " + minPlatforms);
}
}