Smallest power of 2 which is greater than or equal to sum of array elements

Given an array of N numbers where values of the array represent memory sizes. The memory that is required by the system can only be represented in powers of 2. The task is to return the size of the memory required by the system.
Examples:Â
Â
Input: a[] = {2, 1, 4, 5}
Output: 16
The sum of memory required is 12,
hence the nearest power of 2 is 16.
Input: a[] = {1, 2, 3, 2}
Output: 8
Source: Microsoft Interview
Â
Approach: The problem is a combination of summation of array elements and smallest power of 2 greater than or equal to N. Find the sum of array elements and then find the smallest power of 2 greater than or equal to N.Â
Below is the implementation of the above approach:Â
Â
C++
// C++ implementation of the above approach#include <bits/stdc++.h>using namespace std;Â
// Function to find the nearest power of 2int nextPowerOf2(int n){Â
    // The number    int p = 1;Â
    // If already a power of 2    if (n && !(n & (n - 1)))        return n;Â
    // Find the next power of 2    while (p < n)        p <<= 1;Â
    return p;}Â
// Function to find the memory usedint memoryUsed(int arr[], int n){    // Sum of array    int sum = 0;Â
    // Traverse and find the sum of array    for (int i = 0; i < n; i++)        sum += arr[i];Â
    // Function call to find the nearest power of 2    int nearest = nextPowerOf2(sum);Â
    return nearest;}// Driver Codeint main(){    int arr[] = { 1, 2, 3, 2 };    int n = sizeof(arr) / sizeof(arr[0]);Â
    cout << memoryUsed(arr, n);Â
    // getchar();    return 0;} |
Java
// Java implementation of the above approachÂ
class GFG{    // Function to find the nearest power of 2    static int nextPowerOf2(int n)    {             // The number        int p = 1;             // If already a power of 2        if(n!=0 && ((n&(n-1)) == 0))            return n;             // Find the next power of 2        while (p < n)            p <<= 1;             return p;    }         // Function to find the memory used    static int memoryUsed(int arr[], int n)    {        // Sum of array        int sum = 0;             // Traverse and find the sum of array        for (int i = 0; i < n; i++)            sum += arr[i];             // Function call to find the nearest power of 2        int nearest = nextPowerOf2(sum);             return nearest;    }    // Driver Code    public static void main(String []args)    {        int arr[] = { 1, 2, 3, 2 };        int n = arr.length;             System.out.println(memoryUsed(arr, n));     Â
    }}Â
// This code is contributed // by ihritik |
Python3
# Python3 implementation of the above approach Â
# Function to find the nearest power of 2 def nextPowerOf2(n):         # The number     p = 1         # If already a power of 2     if (n and not(n & (n - 1))):         return n             # Find the next power of 2     while (p < n):         p <<= 1    return pÂ
# Function to find the memory used def memoryUsed(arr, n):         # Sum of array     sum = 0Â
    # Traverse and find the sum of array     for i in range(n):         sum += arr[i] Â
    # Function call to find the nearest    # power of 2     nearest = nextPowerOf2(sum)Â
    return nearestÂ
# Driver Code arr = [1, 2, 3, 2] n = len(arr) print(memoryUsed(arr, n))Â
# This code is contributed by sahishelangia |
C#
// C# implementation of the above approachÂ
using System;class GFG{    // Function to find the nearest power of 2    static int nextPowerOf2(int n)    {             // The number        int p = 1;             // If already a power of 2        if(n!=0 && ((n&(n-1)) == 0))            return n;             // Find the next power of 2        while (p < n)            p <<= 1;             return p;    }         // Function to find the memory used    static int memoryUsed(int []arr, int n)    {        // Sum of array        int sum = 0;             // Traverse and find the sum of array        for (int i = 0; i < n; i++)            sum += arr[i];             // Function call to find the nearest power of 2        int nearest = nextPowerOf2(sum);             return nearest;    }    // Driver Code    public static void Main()    {        int []arr = { 1, 2, 3, 2 };        int n = arr.Length;             Console.WriteLine(memoryUsed(arr, n));     Â
    }}Â
// This code is contributed // by ihritik |
PHP
<?php// PHP implementation of the above approachÂ
// Function to find the nearest power of 2function nextPowerOf2($n){Â
    // The number    $p = 1;Â
    // If already a power of 2    if ($n && !($n & ($n - 1)))        return $n;Â
    // Find the next power of 2    while ($p < $n)        $p <<= 1;Â
    return $p;}Â
// Function to find the memory usedfunction memoryUsed(&$arr, $n){    // Sum of array    $sum = 0;Â
    // Traverse and find the sum of array    for ($i = 0; $i < $n; $i++)        $sum += $arr[$i];Â
    // Function call to find the     // nearest power of 2    $nearest = nextPowerOf2($sum);Â
    return $nearest;}Â
// Driver Code$arr = array(1, 2, 3, 2);$n = sizeof($arr);Â
echo(memoryUsed($arr, $n));Â
// This code is contributed // by Shivi_Aggarwal?> |
Javascript
<script>Â
// Javascript implementation of the above approachÂ
// Function to find the nearest power of 2function nextPowerOf2(n){Â
    // The number    let p = 1;Â
    // If already a power of 2    if (n && !(n & (n - 1)))        return n;Â
    // Find the next power of 2    while (p < n)        p <<= 1;Â
    return p;}Â
// Function to find the memory usedfunction memoryUsed(arr, n){    // Sum of array    let sum = 0;Â
    // Traverse and find the sum of array    for (let i = 0; i < n; i++)        sum += arr[i];Â
    // Function call to find the nearest power of 2    let nearest = nextPowerOf2(sum);Â
    return nearest;}// Driver Codelet arr = [ 1, 2, 3, 2 ];let n = arr.length;Â
document.write(memoryUsed(arr, n));Â
</script> |
8
Â
Time Complexity: O(N), as we are using a loop to traverse N times, where N is the number of elements in the array.
Auxiliary Space: O(1), as we are not using any extra space.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



