Decimal equivalent of concatenation of absolute difference of floor and rounded-off values of array elements as a Binary String
Given an array arr[] consisting of N floating-point numbers, the task is to print the decimal representation of the binary array constructed from the absolute difference between the floor and round-off value for each array element.
Examples:
Input: arr[] = {1.2, 2.6, 4.2, 6.9, 3.1, 21.6, 91.2}
Output: 42
Explanation:
Below is the image to illustrate the above example:
Input: arr[] = {5.7, 2.8, 1.9, 5.6, 2.2}
Output: 30
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say result as 0 that stores the resultant numbers formed.
- Initialize a variable, say power as 0 that keeps the power of 2 added in each step.
- Traverse the given array arr[] from the end and perform the following steps:
- Initialize a variable, say bit that stores the absolute difference between the round-off value and the floor value of each array element.
- If the value of the absolute difference is 1, then multiply the digit with the proper power of 2 and add it to the variable result.
- Increment the value of power by 1.
- After completing the above steps, print the value of the result as the required decimal equivalent of the binary representation.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int findDecimal( float arr[], int N)
{
int bit, power = 0, result = 0;
for ( int i = N - 1; i >= 0; i--) {
bit = abs ( floor (arr[i])
- round(arr[i]));
if (bit)
result += pow (2, power);
power++;
}
cout << result;
}
int main()
{
float arr[] = { 1.2, 2.6, 4.2, 6.9,
3.1, 21.6, 91.2 };
int N = sizeof (arr) / sizeof (arr[0]);
findDecimal(arr, N);
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG
{
static void findDecimal( double arr[], int N)
{
int bit, power = 0 , result = 0 ;
for ( int i = N - 1 ; i >= 0 ; i--)
{
bit = Math.abs(( int )Math.floor(arr[i])
- ( int )Math.round(arr[i]));
if (bit != 0 )
result += Math.pow( 2 , power);
power++;
}
System.out.print(result);
}
public static void main(String[] args)
{
double arr[] = { 1.2 , 2.6 , 4.2 , 6.9 ,
3.1 , 21.6 , 91.2 };
int N = arr.length;
findDecimal(arr, N);
}
}
|
Python3
def findDecimal(arr, N):
power = 0 ;
result = 0 ;
for i in range (N - 1 , - 1 , - 1 ):
bit = abs ( int (arr[i]) - round (arr[i]));
if (bit):
result + = pow ( 2 , power);
power + = 1 ;
print (result);
arr = [ 1.2 , 2.6 , 4.2 , 6.9 , 3.1 , 21.6 , 91.2 ];
N = len (arr)
findDecimal(arr, N);
|
C#
using System;
class GFG{
static void findDecimal( double [] arr, int N)
{
int bit, power = 0, result = 0;
for ( int i = N - 1; i >= 0; i--)
{
bit = Math.Abs(( int )Math.Floor(arr[i]) -
( int )Math.Round(arr[i]));
if (bit != 0)
result += ( int )Math.Pow(2, power);
power++;
}
Console.WriteLine(result);
}
public static void Main()
{
double [] arr = { 1.2, 2.6, 4.2, 6.9,
3.1, 21.6, 91.2 };
int N = arr.Length;
findDecimal(arr, N);
}
}
|
Javascript
<script>
function findDecimal(arr, N) {
let bit, power = 0, result = 0;
for (let i = N - 1; i >= 0; i--) {
bit = Math.abs(Math.floor(arr[i])
- Math.round(arr[i]));
if (bit != 0)
result += Math.pow(2, power);
power++;
}
document.write(result);
}
let arr = [1.2, 2.6, 4.2, 6.9,
3.1, 21.6, 91.2];
let N = arr.length;
findDecimal(arr, N);
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
12 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...