-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Floor_of_sorted_array.dart
98 lines (83 loc) · 1.94 KB
/
Floor_of_sorted_array.dart
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
/*
Given a sorted array and a value x, find the floor of x in the array.
The floor of x is the largest element in array smaller than or equal to x.
The function shows the implementation of the function, using binary search.
*/
import 'dart:io';
int findFloor(List arr, int n, int x) {
// Initializing low and high indices
int low = 0;
int high = n - 1;
// Initialinzing floor value
int floor_val = -1;
// Applying binary search
while (low <= high) {
// Finding the mid index
int mid = ((low + high) / 2).floor();
// If the mid index element is equal to the given element
if (arr[mid] == x) {
return arr[mid];
}
// If given element is less than mid index element,
// performing search on the left of mid index
if (x < arr[mid]) {
high = mid - 1;
continue;
}
// If given element is greater than mid index element,
// performing search on the right of mid index
if (x > arr[mid]) {
floor_val = arr[mid];
low = mid + 1;
continue
}
}
return floor_val;
}
main() {
// Taking user input
var array = [];
var n, ele, x;
print('Enter the number of Elements: ');
n = stdin.readLineSync();
n = int.parse(n);
for (int i = 1; i <= n; i++) {
print('Enter Element $i: ');
ele = stdin.readLineSync();
ele = int.parse(ele);
array.add(ele);
}
array.sort();
print('The sorted array entered is: $array');
print('Enter a number X: ');
x = stdin.readLineSync();
x = int.parse(x);
// Calling function to obtain the required results
int floor = findFloor(array, n, x);
print("Floor of $x is : $floor");
}
/*
TIME COMPLEXITY: O(log N)
SPACE COMPLEXITY: O(1)
SAMPLE I/O:
Enter the number of Elements:
7
Enter Element 1:
1
Enter Element 2:
2
Enter Element 3:
8
Enter Element 4:
8
Enter Element 5:
18
Enter Element 6:
27
Enter Element 7:
35
The sorted array entered is: [1, 2, 8, 8, 18, 27, 35]
Enter a number X:
9
Floor of 9 is : 8
*/