Open In App
Related Articles

Value of continuous floor function : F(x) = F(floor(x/2)) + x

Improve Article
Improve
Save Article
Save
Like Article
Like

Given an array of positive integers. For every element x of array, we need to find the value of continuous floor function defined as F(x) = F(floor(x/2)) + x, where F(0) = 0. 

Examples :

Input : arr[] = {6, 8}
Output : 10 15

Explanation : F(6) = 6 + F(3)
                   = 6 + 3 + F(1)
                   = 6 + 3 + 1 + F(0)
                   = 10
Similarly F(8) = 15

Basic Approach: For a given value of x, we can calculate F(x) by using simple recursive function as: 

int func(int x)
{
    if (x == 0) 
        return 0;
    return (x + func(floor(x/2)));
}

In this approach if we have n queries then it will take O(x) for every query element
An Efficient Approach is to use memoization We construct an array which holds the value of F(x) for each possible value of x. We compute the value if not already computed. Else we return the value.

Below is the implementation of the above approach:

C++




// C++ program for finding value
// of continuous floor function
#include <bits/stdc++.h>
 
#define max 10000
using namespace std;
 
int dp[max];
 
void initDP()
    {
         for (int i = 0; i < max; i++)
             dp[i] = -1;
    }
 
// function to return value of F(n)
int func(int x)
    {
        if (x == 0)
            return 0;
        if (dp[x] == -1)
            dp[x] = x + func(x / 2);
 
        return dp[x];
    }
 
void printFloor(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            cout << func(arr[i]) << " ";
    }
 
// Driver code
int main()
    {
        // call the initDP() to fill DP array
        initDP();
 
        int arr[] = { 8, 6 };
        int n = sizeof(arr) / sizeof(arr[0]);
 
        printFloor(arr, n);
 
        return 0;
    }


Java




// Java program for finding value
// of continuous floor function
import java.io.*;
class GFG {
    static final int max = 10000;
    static int dp[] = new int[max];
 
    static void initDP()
    {
        for (int i = 0; i < max; i++)
            dp[i] = -1;
    }
 
    // function to return value of F(n)
    static int func(int x)
    {
        if (x == 0)
            return 0;
        if (dp[x] == -1)
            dp[x] = x + func(x / 2);
 
        return dp[x];
    }
 
    static void printFloor(int arr[], int n)
    {
        for (int i = 0; i < n; i++)
            System.out.print(func(arr[i]) + " ");
    }
 
    // Driver code
    public static void main(String[] args)
    {
 
        // call the initDP() to fill DP array
        initDP();
 
        int arr[] = { 8, 6 };
        int n = arr.length;
 
        printFloor(arr, n);
    }
}
 
// This code is contributed by Anant Agarwal.


Python3




# Python3 program for finding value
# of continuous floor function
 
max = 10000
 
dp = [0] * max
 
# function to initialize the DP array
def initDP() :
     
        for i in range(max) :
                dp[i] = -1
     
# function to return value of F(n)
def func(x) :
 
    if (x == 0) :
        return 0
         
    if (dp[x] == -1) :
        dp[x] = x + func(x // 2)
 
    return dp[x]
     
def printFloor(arr, n) :
     
    for i in range(n) :
                 
        print(func(arr[i]), end = " ")
 
# Driver Code
if __name__ == "__main__" :
 
        # call the initDP() to
        # fill DP array        
        initDP()
 
        arr = [8, 6]
        n = len(arr)
 
        printFloor(arr, n)
 
# This code is contributed by Ryuga


C#




// C# program for finding value
// of continuous floor function
using System;
 
class GFG
{
    static int max = 10000;
    static int []dp = new int[max];
     
    static void initDP()
    {
        for (int i = 0; i < max; i++)
            dp[i] = -1;
    }
     
    // function to return value of F(n)
    static int func(int x)
    {
        if (x == 0)
            return 0;
        if (dp[x] == -1)
            dp[x] = x + func(x / 2);
     
        return dp[x];
    }
     
    static void printFloor(int []arr, int n)
    {
        for (int i = 0; i < n; i++)
            Console.Write(func(arr[i]) + " ");
    }
     
    // Driver code
    public static void Main()
    {
         
        // call the initDP() to fill DP array
        initDP();
     
        int []arr = {8, 6};
        int n = arr.Length;
     
        printFloor(arr, n);
    }
}
 
// This code is contributed by nitin mittal


PHP




<?php
// PHP program for finding value
// of continuous floor function
$max = 10000;
 
$dp = array_fill(0, $max, NULL);
 
function initDP()
{
    global $max,$dp;
    for ($i = 0; $i < $max; $i++)
        $dp[$i] = -1;
}
 
// function to return value of F(n)
function func($x)
{
    global $dp;
    if ($x == 0)
        return 0;
    if ($dp[$x] == -1)
        $dp[$x] = $x + func(intval($x / 2));
 
    return $dp[$x];
}
 
function printFloor(&$arr, $n)
{
    for ($i = 0; $i < $n; $i++)
        echo func($arr[$i]) . " ";
}
 
// Driver code
 
// call the initDP() to fill DP array
initDP();
 
$arr = array(8, 6);
$n = sizeof($arr);
 
printFloor($arr, $n);
 
// This code is contributed by ita_c
?>


Javascript




<script>
// Javascript program for finding value
// of continuous floor function
    let max = 10000;
    let dp = Array.from({length: max}, (_, i) => 0);
       
    function initDP()
    {
        for (let i = 0; i < max; i++)
            dp[i] = -1;
    }
       
    // function to return value of F(n)
    function func(x)
    {
        if (x == 0)
            return 0;
        if (dp[x] == -1)
            dp[x] = x + func(Math.floor(x / 2));
       
        return dp[x];
    }
       
    function prletFloor(arr, n)
    {
        for (let i = 0; i < n; i++)
            document.write(func(arr[i]) + " ");
    }
     
// driver function
 
        // call the initDP() to fill DP array
        initDP();
       
        let arr = [8, 6];
        let n = arr.length;
       
        prletFloor(arr, n);
    
   // This code is contributed by code_hunt.
</script>   


Time Complexity: O(n)
Auxiliary Space: O(n)

This article is contributed by Shivam Pradhan (anuj_charm). If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above 


Last Updated : 10 Dec, 2022
Like Article
Save Article
Similar Reads
Related Tutorials