
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Floor of every element in same array in C++
In this problem, we are given an array arr[] of integer elements. Our task is to create a program to find the Floor of every element in the same array. If the floor of an element exists, we will print the floor otherwise print -1.
Floor of an element in array is the closest element which is smaller than or equal to the element in the array.
Let’s take an example to understand the problem
Input: arr[] = {3, 1, 5 ,7, 8, 2} Output: 2 -1 3 5 7 1
Solution Approach
An approach to solve the problem is by using nested loops. One to loop for each element of the array and the inner one for finding the floor of the element in the array.
Another approach to solve the problem is using an extra array to store the sorted array. Then loop over that original array and find the floor of the element in the sorted array using a binary seach algorithm.
Example
Program to illustrate the working of our solution
#include <bits/stdc++.h> using namespace std; void printFloorEle(int arr[], int n){ vector<int> sortedArr(arr, arr + n); sort(sortedArr.begin(), sortedArr.end()); for (int i = 0; i < n; i++) { if (arr[i] == sortedArr[0]) { if (arr[i] == sortedArr[1]) cout<<arr[i]; else cout<<-1; cout<<"\t"; continue; } auto iterator = lower_bound(sortedArr.begin(),sortedArr.end(), arr[i]); if (iterator != sortedArr.end() && *(iterator + 1) == arr[i]) cout<<arr[i]<<"\t"; else cout<<*(iterator - 1)<<"\t"; } } int main(){ int arr[] = { 3, 1, 5 ,7, 8, 2 }; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The Floor of every element of the given array is "; printFloorEle(arr, n); return 0; }
Output
The Floor of every element of the given array is 2 -1 3 5 7 1
- Related Articles
- Divide a scalar value into every element of a masked Array and return the floor value in NumPy
- Divide every element of a masked Array into a scalar value and return the floor value in NumPy
- Maximum possible XOR of every element in an array with another array in C++
- Find closest value for every element in array in C++
- Divide every element of one array by other array elements in C++ Program
- Find last element after deleting every second element in array of n integers in C++
- Return the floor of a specific array element in Numpy
- Find closest greater value for every element in array in C++
- Find closest smaller value for every element in array in C++
- Maximum number by concatenating every element in a rotation of an array in C++
- Find maximum sum taking every Kth element in the array in C++
- Floor in a Sorted Array in C++
- Maximum distance between two occurrences of same element in array in C
- Finding sum of every nth element of array in JavaScript
- Find the element that appears once in an array where every other element appears twice in C++
