Skip to content

Commit

Permalink
Added solution - LeetHub
Browse files Browse the repository at this point in the history
  • Loading branch information
sinjini05 committed Feb 13, 2023
1 parent db9d606 commit 2587a31
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions Ceil The Floor - GFG/ceil-the-floor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//{ Driver Code Starts

#include <bits/stdc++.h>

using namespace std;

pair<int, int> getFloorAndCeil(int arr[], int n, int x);

int main() {
int t;
cin >> t;
while (t--) {
int n, x;
cin >> n >> x;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
auto ans = getFloorAndCeil(arr, n, x);
cout << ans.first << " " << ans.second << "\n";
}
return 0;
}

// } Driver Code Ends

pair<int, int> getFloorAndCeil(int arr[], int n, int x) {

int c=-1,f=-1;

int c1=0;

for(int i=0;i<n;i++){

if(arr[i]<x) f=max(f,arr[i]);

if(arr[i]>x)

{ if(c1==0){ c=arr[i]; c1++;}

else c=min(c,arr[i]);

}

if(arr[i]==x){

c=arr[i];

f=arr[i];

break;

}

}

pair<int,int>p(f,c);

return p;

}

0 comments on commit 2587a31

Please sign in to comment.