# Find floor and ceil in an unsorted array

Here given code implementation process.

``````// C Program
// Find floor and ceil in an unsorted array
#include <stdio.h>

//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("  %d", arr[i]);
}
printf("\n");
}
// Find the floor and ceil value of given element
void floorCeil(int arr[], int size, int element)
{
// Define resultant variable
int floor = -1;
int ceil = -1;
// Define loop control variable
int i = 0;
// Execute loop through by size
for (i = 0; i < size; ++i)
{
if (arr[i] < element)
{
if (floor == -1 || arr[floor] < arr[i])
{
floor = i;
}
}
else if (arr[i] > element)
{
if (ceil == -1 || arr[ceil] > arr[i])
{
ceil = i;
}
}
else
{
// When given element exists in array
ceil = i;
floor = i;
// Terminate loop
// Like break loop
i = size;
}
}
// Display given element
printf("\n Element  : %d", element);
// Display calculated result
if (floor != -1)
{
printf("\n Floor : %d", arr[floor]);
}
else
{
// When floor are not exist
printf("\n Floor : None");
}
if (ceil != -1)
{
printf("\n Ceil  : %d\n", arr[ceil]);
}
else
{
// When ceil are not exist
printf("\n Ceil  : None\n");
}
}
int main(int argc, char const *argv[])
{
int arr[] = {
6 , 9 , 2 , 8 , 0 , 6 , 7 , 4 , 8 , 3
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
printf(" Array elements \n");
printArray(arr, size);
// Test Cases
// element = 1
floorCeil(arr, size, 1);
// element = 5
floorCeil(arr, size, 5);
// element = 9
floorCeil(arr, size, 9);
// element = -1
floorCeil(arr, size, -1);
return 0;
}``````

#### Output

`````` Array elements
6  9  2  8  0  6  7  4  8  3

Element  : 1
Floor : 0
Ceil  : 2

Element  : 5
Floor : 4
Ceil  : 6

Element  : 9
Floor : 9
Ceil  : 9

Element  : -1
Floor : None
Ceil  : 0``````
``````/*
Java Program
Find floor and ceil in an unsorted array
*/
public class FloorAndCeil
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
System.out.print("  " + arr[i]);
}
System.out.print("\n");
}
// Find the floor and ceil value of given element
public void floorCeil(int[] arr, int size, int element)
{
// Define resultant variable
int floor = -1;
int ceil = -1;
// Define loop control variable
int i = 0;
// Execute loop through by size
for (i = 0; i < size; ++i)
{
if (arr[i] < element)
{
if (floor == -1 || arr[floor] < arr[i])
{
floor = i;
}
}
else if (arr[i] > element)
{
if (ceil == -1 || arr[ceil] > arr[i])
{
ceil = i;
}
}
else
{
// When given element exists in array
ceil = i;
floor = i;
// Terminate loop
// Like break loop
i = size;
}
}
// Display given element
System.out.print("\n  Element : " + element);
// Display calculated result
if (floor != -1)
{
System.out.print("\n  Floor : " + arr[floor]);
}
else
{
// When floor are not exist
System.out.print("\n  Floor : None");
}
if (ceil != -1)
{
System.out.print("\n  Ceil : " + arr[ceil] + "\n");
}
else
{
// When ceil are not exist
System.out.print("\n  Ceil : None\n");
}
}
public static void main(String[] args)
{
int[] arr =
{
6 , 9 , 2 , 8 , 0 , 6 , 7 , 4 , 8 , 3
};
// Get the size
int size = arr.length;
// Display array elements
System.out.print(" Array elements \n");
// Test Cases
// element = 1
// element = 5
// element = 9
// element = -1
}
}``````

#### Output

`````` Array elements
6  9  2  8  0  6  7  4  8  3

Element : 1
Floor : 0
Ceil : 2

Element : 5
Floor : 4
Ceil : 6

Element : 9
Floor : 9
Ceil : 9

Element : -1
Floor : None
Ceil : 0``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ Program
Find floor and ceil in an unsorted array
*/
class FloorAndCeil
{
public:
//Display elements of given array
void printArray(int arr[], int size)
{
for (int i = 0; i < size; ++i)
{
cout << "  " << arr[i];
}
cout << "\n";
}
// Find the floor and ceil value of given element
void floorCeil(int arr[], int size, int element)
{
// Define resultant variable
int floor = -1;
int ceil = -1;
// Define loop control variable
int i = 0;
// Execute loop through by size
for (i = 0; i < size; ++i)
{
if (arr[i] < element)
{
if (floor == -1 || arr[floor] < arr[i])
{
floor = i;
}
}
else if (arr[i] > element)
{
if (ceil == -1 || arr[ceil] > arr[i])
{
ceil = i;
}
}
else
{
// When given element exists in array
ceil = i;
floor = i;
// Terminate loop
// Like break loop
i = size;
}
}
// Display given element
cout << "\n  Element : " << element;
// Display calculated result
if (floor != -1)
{
cout << "\n  Floor : " << arr[floor];
}
else
{
// When floor are not exist
cout << "\n  Floor : None";
}
if (ceil != -1)
{
cout << "\n  Ceil : " << arr[ceil] << "\n";
}
else
{
// When ceil are not exist
cout << "\n  Ceil : None\n";
}
}
};
int main()
{
int arr[] = {
6 , 9 , 2 , 8 , 0 , 6 , 7 , 4 , 8 , 3
};
// Get the size
int size = sizeof(arr) / sizeof(arr[0]);
// Display array elements
cout << " Array elements \n";
// Test Cases
// element = 1
// element = 5
// element = 9
// element = -1
return 0;
}``````

#### Output

`````` Array elements
6  9  2  8  0  6  7  4  8  3

Element : 1
Floor : 0
Ceil : 2

Element : 5
Floor : 4
Ceil : 6

Element : 9
Floor : 9
Ceil : 9

Element : -1
Floor : None
Ceil : 0``````
``````// Include namespace system
using System;
/*
C# Program
Find floor and ceil in an unsorted array
*/
public class FloorAndCeil
{
//Display elements of given array
public void printArray(int[] arr, int size)
{
for (int i = 0; i < size; ++i)
{
Console.Write("  " + arr[i]);
}
Console.Write("\n");
}
// Find the floor and ceil value of given element
public void floorCeil(int[] arr, int size, int element)
{
// Define resultant variable
int floor = -1;
int ceil = -1;
// Define loop control variable
int i = 0;
// Execute loop through by size
for (i = 0; i < size; ++i)
{
if (arr[i] < element)
{
if (floor == -1 || arr[floor] < arr[i])
{
floor = i;
}
}
else if (arr[i] > element)
{
if (ceil == -1 || arr[ceil] > arr[i])
{
ceil = i;
}
}
else
{
// When given element exists in array
ceil = i;
floor = i;
// Terminate loop
// Like break loop
i = size;
}
}
// Display given element
Console.Write("\n  Element : " + element);
// Display calculated result
if (floor != -1)
{
Console.Write("\n  Floor : " + arr[floor]);
}
else
{
// When floor are not exist
Console.Write("\n  Floor : None");
}
if (ceil != -1)
{
Console.Write("\n  Ceil : " + arr[ceil] + "\n");
}
else
{
// When ceil are not exist
Console.Write("\n  Ceil : None\n");
}
}
public static void Main(String[] args)
{
int[] arr = {
6 , 9 , 2 , 8 , 0 , 6 , 7 , 4 , 8 , 3
};
// Get the size
int size = arr.Length;
// Display array elements
Console.Write("  Array elements \n");
// Test Cases
// element = 1
// element = 5
// element = 9
// element = -1
}
}``````

#### Output

``````  Array elements
6  9  2  8  0  6  7  4  8  3

Element : 1
Floor : 0
Ceil : 2

Element : 5
Floor : 4
Ceil : 6

Element : 9
Floor : 9
Ceil : 9

Element : -1
Floor : None
Ceil : 0``````
``````<?php
/*
Php Program
Find floor and ceil in an unsorted array
*/
class FloorAndCeil
{
//Display elements of given array
public	function printArray( & \$arr, \$size)
{
for (\$i = 0; \$i < \$size; ++\$i)
{
echo "  ". \$arr[\$i];
}
echo "\n";
}
// Find the floor and ceil value of given element
public	function floorCeil( & \$arr, \$size, \$element)
{
// Define resultant variable
\$floor = -1;
\$ceil = -1;
// Define loop control variable
\$i = 0;
// Execute loop through by size
for (\$i = 0; \$i < \$size; ++\$i)
{
if (\$arr[\$i] < \$element)
{
if (\$floor == -1 || \$arr[\$floor] < \$arr[\$i])
{
\$floor = \$i;
}
}
else if (\$arr[\$i] > \$element)
{
if (\$ceil == -1 || \$arr[\$ceil] > \$arr[\$i])
{
\$ceil = \$i;
}
}
else
{
// When given element exists in array
\$ceil = \$i;
\$floor = \$i;
// Terminate loop
// Like break loop
\$i = \$size;
}
}
// Display given element
echo "\n  Element : ". \$element;
// Display calculated result
if (\$floor != -1)
{
echo "\n  Floor : ". \$arr[\$floor];
}
else
{
// When floor are not exist
echo "\n  Floor : None";
}
if (\$ceil != -1)
{
echo "\n  Ceil : ". \$arr[\$ceil] ."\n";
}
else
{
// When ceil are not exist
echo "\n  Ceil : None\n";
}
}
}

function main()
{
\$arr = array(6, 9, 2, 8, 0, 6, 7, 4, 8, 3);
// Get the size
\$size = count(\$arr);
// Display array elements
echo " Array elements \n";
// Test Cases
// element = 1
// element = 5
// element = 9
// element = -1
}
main();``````

#### Output

`````` Array elements
6  9  2  8  0  6  7  4  8  3

Element : 1
Floor : 0
Ceil : 2

Element : 5
Floor : 4
Ceil : 6

Element : 9
Floor : 9
Ceil : 9

Element : -1
Floor : None
Ceil : 0``````
``````/*
Node Js Program
Find floor and ceil in an unsorted array
*/
class FloorAndCeil
{
//Display elements of given array
printArray(arr, size)
{
for (var i = 0; i < size; ++i)
{
process.stdout.write("  " + arr[i]);
}
process.stdout.write("\n");
}
// Find the floor and ceil value of given element
floorCeil(arr, size, element)
{
// Define resultant variable
var floor = -1;
var ceil = -1;
// Define loop control variable
var i = 0;
// Execute loop through by size
for (i = 0; i < size; ++i)
{
if (arr[i] < element)
{
if (floor == -1 || arr[floor] < arr[i])
{
floor = i;
}
}
else if (arr[i] > element)
{
if (ceil == -1 || arr[ceil] > arr[i])
{
ceil = i;
}
}
else
{
// When given element exists in array
ceil = i;
floor = i;
// Terminate loop
// Like break loop
i = size;
}
}
// Display given element
process.stdout.write("\n  Element : " + element);
// Display calculated result
if (floor != -1)
{
process.stdout.write("\n  Floor : " + arr[floor]);
}
else
{
// When floor are not exist
process.stdout.write("\n  Floor : None");
}
if (ceil != -1)
{
process.stdout.write("\n  Ceil : " + arr[ceil] + "\n");
}
else
{
// When ceil are not exist
process.stdout.write("\n  Ceil : None\n");
}
}
}

function main()
{
var arr = [6, 9, 2, 8, 0, 6, 7, 4, 8, 3];
// Get the size
var size = arr.length;
// Display array elements
process.stdout.write(" Array elements \n");
// Test Cases
// element = 1
// element = 5
// element = 9
// element = -1
}
main();``````

#### Output

`````` Array elements
6  9  2  8  0  6  7  4  8  3

Element : 1
Floor : 0
Ceil : 2

Element : 5
Floor : 4
Ceil : 6

Element : 9
Floor : 9
Ceil : 9

Element : -1
Floor : None
Ceil : 0``````
``````#  Python 3 Program
#  Find floor and ceil in an unsorted array

class FloorAndCeil :
# Display elements of given array
def printArray(self, arr, size) :
i = 0
while (i < size) :
print("  ", arr[i], end = "")
i += 1

print(end = "\n")

#  Find the floor and ceil value of given element
def floorCeil(self, arr, size, element) :
#  Define resultant variable
floor = -1
ceil = -1
#  Define loop control variable
i = 0
#  Execute loop through by size
while (i < size) :
if (arr[i] < element) :
if (floor == -1 or arr[floor] < arr[i]) :
floor = i

elif(arr[i] > element) :
if (ceil == -1 or arr[ceil] > arr[i]) :
ceil = i

else :
#  When given element exists in array
ceil = i
floor = i
#  Terminate loop
#  Like break loop
i = size

i += 1

#  Display given element
print("\n  Element : ", element, end = "")
#  Display calculated result
if (floor != -1) :
print("\n  Floor : ", arr[floor], end = "")
else :
#  When floor are not exist
print("\n  Floor : None", end = "")

if (ceil != -1) :
print("\n  Ceil : ", arr[ceil] )
else :
#  When ceil are not exist
print("\n  Ceil : None")

def main() :
arr = [6, 9, 2, 8, 0, 6, 7, 4, 8, 3]
#  Get the size
size = len(arr)
#  Display array elements
print("  Array elements ")
#  Test Cases
#  element = 1
#  element = 5
#  element = 9
#  element = -1

if __name__ == "__main__": main()``````

#### Output

``````  Array elements
6   9   2   8   0   6   7   4   8   3

Element :  1
Floor :  0
Ceil :  2

Element :  5
Floor :  4
Ceil :  6

Element :  9
Floor :  9
Ceil :  9

Element :  -1
Floor : None
Ceil :  0``````
``````#  Ruby Program
#  Find floor and ceil in an unsorted array

class FloorAndCeil
# Display elements of given array
def printArray(arr, size)
i = 0
while (i < size)
print("  ", arr[i])
i += 1
end

print("\n")
end

#  Find the floor and ceil value of given element
def floorCeil(arr, size, element)
#  Define resultant variable
floor = -1
ceil = -1
#  Define loop control variable
i = 0
#  Execute loop through by size
while (i < size)
if (arr[i] < element)
if (floor == -1 || arr[floor] < arr[i])
floor = i
end

elsif(arr[i] > element)
if (ceil == -1 || arr[ceil] > arr[i])
ceil = i
end

else
#  When given element exists in array
ceil = i
floor = i
#  Terminate loop
#  Like break loop
i = size
end

i += 1
end

#  Display given element
print("\n  Element : ", element)
#  Display calculated result
if (floor != -1)
print("\n  Floor : ", arr[floor])
else
#  When floor are not exist
print("\n  Floor : None")
end

if (ceil != -1)
print("\n  Ceil : ", arr[ceil] ,"\n")
else
#  When ceil are not exist
print("\n  Ceil : None\n")
end

end

end

def main()
arr = [6, 9, 2, 8, 0, 6, 7, 4, 8, 3]
#  Get the size
size = arr.length
#  Display array elements
print(" Array elements \n")
#  Test Cases
#  element = 1
#  element = 5
#  element = 9
#  element = -1
end

main()``````

#### Output

`````` Array elements
6  9  2  8  0  6  7  4  8  3

Element : 1
Floor : 0
Ceil : 2

Element : 5
Floor : 4
Ceil : 6

Element : 9
Floor : 9
Ceil : 9

Element : -1
Floor : None
Ceil : 0
``````
``````/*
Scala Program
Find floor and ceil in an unsorted array
*/
class FloorAndCeil
{
//Display elements of given array
def printArray(arr: Array[Int], size: Int): Unit = {
var i: Int = 0;
while (i < size)
{
print("  " + arr(i));
i += 1;
}
print("\n");
}
// Find the floor and ceil value of given element
def floorCeil(arr: Array[Int], size: Int, element: Int): Unit = {
// Define resultant variable
var floor: Int = -1;
var ceil: Int = -1;
// Define loop control variable
var i: Int = 0;
// Execute loop through by size
while (i < size)
{
if (arr(i) < element)
{
if (floor == -1 || arr(floor) < arr(i))
{
floor = i;
}
}
else if (arr(i) > element)
{
if (ceil == -1 || arr(ceil) > arr(i))
{
ceil = i;
}
}
else
{
// When given element exists in array
ceil = i;
floor = i;
// Terminate loop
// Like break loop
i = size;
}
i += 1;
}
// Display given element
print("\n  Element : " + element);
// Display calculated result
if (floor != -1)
{
print("\n  Floor : " + arr(floor));
}
else
{
// When floor are not exist
print("\n  Floor : None");
}
if (ceil != -1)
{
print("\n  Ceil : " + arr(ceil) + "\n");
}
else
{
// When ceil are not exist
print("\n  Ceil : None\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: FloorAndCeil = new FloorAndCeil();
var arr: Array[Int] = Array(6, 9, 2, 8, 0, 6, 7, 4, 8, 3);
// Get the size
var size: Int = arr.length;
// Display array elements
print(" Array elements \n");
// Test Cases
// element = 1
// element = 5
// element = 9
// element = -1
}
}``````

#### Output

`````` Array elements
6  9  2  8  0  6  7  4  8  3

Element : 1
Floor : 0
Ceil : 2

Element : 5
Floor : 4
Ceil : 6

Element : 9
Floor : 9
Ceil : 9

Element : -1
Floor : None
Ceil : 0``````
``````/*
Swift 4 Program
Find floor and ceil in an unsorted array
*/
class FloorAndCeil
{
//Display elements of given array
func printArray(_ arr: [Int], _ size: Int)
{
var i: Int = 0;
while (i < size)
{
print("  ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
// Find the floor and ceil value of given element
func floorCeil(_ arr: [Int], _ size: Int, _ element: Int)
{
// Define resultant variable
var floor: Int = -1;
var ceil: Int = -1;
// Define loop control variable
var i: Int = 0;
// Execute loop through by size
while (i < size)
{
if (arr[i] < element)
{
if (floor == -1 || arr[floor] < arr[i])
{
floor = i;
}
}
else if (arr[i] > element)
{
if (ceil == -1 || arr[ceil] > arr[i])
{
ceil = i;
}
}
else
{
// When given element exists in array
ceil = i;
floor = i;
// Terminate loop
// Like break loop
i = size;
}
i += 1;
}
// Display given element
print("\n  Element : ", element, terminator: "");
// Display calculated result
if (floor  != -1)
{
print("\n  Floor : ", arr[floor], terminator: "");
}
else
{
// When floor are not exist
print("\n  Floor : None", terminator: "");
}
if (ceil  != -1)
{
print("\n  Ceil : ", arr[ceil]);
}
else
{
// When ceil are not exist
print("\n  Ceil : None");
}
}
}
func main()
{
let arr: [Int] = [6, 9, 2, 8, 0, 6, 7, 4, 8, 3];
// Get the size
let size: Int = arr.count;
// Display array elements
print(" Array elements ");
// Test Cases
// element = 1
// element = 5
// element = 9
// element = -1
}
main();``````

#### Output

`````` Array elements
6   9   2   8   0   6   7   4   8   3

Element :  1
Floor :  0
Ceil :  2

Element :  5
Floor :  4
Ceil :  6

Element :  9
Floor :  9
Ceil :  9

Element :  -1
Floor : None
Ceil :  0``````
``````/*
Kotlin Program
Find floor and ceil in an unsorted array
*/
class FloorAndCeil
{
//Display elements of given array
fun printArray(arr: Array<Int>, size: Int): Unit
{
var i: Int = 0;
while (i<size)
{
print("  " + arr[i]);
i += 1;
}
print("\n");
}
// Find the floor and ceil value of given element
fun floorCeil(arr: Array<Int>, size: Int, element: Int): Unit
{
// Define resultant variable
var floor: Int = -1;
var ceil: Int = -1;
// Define loop control variable
var i: Int = 0;
// Execute loop through by size
while (i<size)
{
if (arr[i] < element)
{
if (floor == -1 || arr[floor]<arr[i])
{
floor = i;
}
}
else if (arr[i] > element)
{
if (ceil == -1 || arr[ceil] > arr[i])
{
ceil = i;
}
}
else
{
// When given element exists in array
ceil = i;
floor = i;
// Terminate loop
// Like break loop
i = size;
}
i += 1;
}
// Display given element
print("\n  Element : " + element);
// Display calculated result
if (floor != -1)
{
print("\n  Floor : " + arr[floor]);
}
else
{
// When floor are not exist
print("\n  Floor : None");
}
if (ceil != -1)
{
print("\n  Ceil : " + arr[ceil] + "\n");
}
else
{
// When ceil are not exist
print("\n  Ceil : None\n");
}
}
}

fun main(args: Array<String>): Unit
{
var arr: Array<Int> = arrayOf(6, 9, 2, 8, 0, 6, 7, 4, 8, 3);
// Get the size
var size: Int = arr.count();
// Display array elements
print(" Array elements \n");
// Test Cases
// element = 1
// element = 5
// element = 9
// element = -1
}``````

#### Output

`````` Array elements
6  9  2  8  0  6  7  4  8  3

Element : 1
Floor : 0
Ceil : 2

Element : 5
Floor : 4
Ceil : 6

Element : 9
Floor : 9
Ceil : 9

Element : -1
Floor : None
Ceil : 0``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.