Floor in a Sorted Array
PROBLEM :
Given a sorted array having no duplicates, arr[] and a value, x, find floor of x in given array. Floor of x is the largest element in arr[] such that the element is smaller than or equal to x. If floor exists, then return index of it, else return -1.
Examples:
Input : arr[] = {1, 2, 8, 10, 11, 12, 19}, x = 5
Output : 1
1 is index of 2. 2 is the largest element in arr[]
smaller than 5.
Input : arr[] = {1, 2, 8, 10, 11, 12, 19}, x = 20
Output : 6
19 is the largest element in arr[] smaller than 20.
Input : arr[] = {1, 2, 8, 10, 11, 12, 19}, x = 0
Output : -1
Since floor doesn't exist, output is -1.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N and x, N is the size of array.
The second line of each test case contains N array elements.
Output:
Print index of floor of x if it exists, else print -1
Constraints:
1 = T = 500
1 = N = 1000
0 = X = 1000
1 = arr[i] = 10000
Example:
Input:
3
7 0
1 2 8 10 11 12 19
7 5
1 2 8 10 11 12 19
7 10
1 2 8 10 11 12 19
Output:
-1
1
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int FloorSortedArray(int [],int ,int ) ;
#define max 1000
int main()
{
int t,no,i,k ;
int arr[max] ;
cin>>t ;
while(t--)
{
cin>>no>>k ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cout<<FloorSortedArray(arr,no,k)<<endl ;
}
return 0;
}
int FloorSortedArray(int arr[],int no,int ele)
{
int start,end,ans,mid ;
start=0 ;
end=no-1 ;
ans=-1 ;
while(start<=end)
{
mid=(start+end)/2 ;
if(arr[mid]==ele)
{
ans=mid ;
break ;
}
else if(mid==no-1)
{
if(arr[mid]<end)
ans=mid ;
break ;
}
else if(arr[mid]<ele&&arr[mid+1]>ele)
{
ans=mid ;
break ;
}
else if(arr[mid]>ele)
end=mid-1 ;
else
start=mid+1 ;
}
if(ans==-1&&arr[no-1]<ele)
return no-1 ;
return ans ;
}
---------------------------------------------------------------------------------
Given a sorted array having no duplicates, arr[] and a value, x, find floor of x in given array. Floor of x is the largest element in arr[] such that the element is smaller than or equal to x. If floor exists, then return index of it, else return -1.
Examples:
Input : arr[] = {1, 2, 8, 10, 11, 12, 19}, x = 5
Output : 1
1 is index of 2. 2 is the largest element in arr[]
smaller than 5.
Input : arr[] = {1, 2, 8, 10, 11, 12, 19}, x = 20
Output : 6
19 is the largest element in arr[] smaller than 20.
Input : arr[] = {1, 2, 8, 10, 11, 12, 19}, x = 0
Output : -1
Since floor doesn't exist, output is -1.
Input:
The first line of input contains an integer T denoting the number of test cases.
The first line of each test case is N and x, N is the size of array.
The second line of each test case contains N array elements.
Output:
Print index of floor of x if it exists, else print -1
Constraints:
1 = T = 500
1 = N = 1000
0 = X = 1000
1 = arr[i] = 10000
Example:
Input:
3
7 0
1 2 8 10 11 12 19
7 5
1 2 8 10 11 12 19
7 10
1 2 8 10 11 12 19
Output:
-1
1
3
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
int FloorSortedArray(int [],int ,int ) ;
#define max 1000
int main()
{
int t,no,i,k ;
int arr[max] ;
cin>>t ;
while(t--)
{
cin>>no>>k ;
for(i=0;i<no;i++)
cin>>arr[i] ;
cout<<FloorSortedArray(arr,no,k)<<endl ;
}
return 0;
}
int FloorSortedArray(int arr[],int no,int ele)
{
int start,end,ans,mid ;
start=0 ;
end=no-1 ;
ans=-1 ;
while(start<=end)
{
mid=(start+end)/2 ;
if(arr[mid]==ele)
{
ans=mid ;
break ;
}
else if(mid==no-1)
{
if(arr[mid]<end)
ans=mid ;
break ;
}
else if(arr[mid]<ele&&arr[mid+1]>ele)
{
ans=mid ;
break ;
}
else if(arr[mid]>ele)
end=mid-1 ;
else
start=mid+1 ;
}
if(ans==-1&&arr[no-1]<ele)
return no-1 ;
return ans ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment