📌  相关文章
📜  2的最小乘方,大于或等于数组元素的总和

📅  最后修改于: 2021-05-25 07:12:19             🧑  作者: Mango

给定一个N个数字的数组,其中数组的值表示内存大小。系统所需的内存只能以2的幂表示。任务是返回系统所需的内存大小。
例子:

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

资料来源:微软访谈

方法:问题是数组元素的总和与2的最小幂大于或等于N的组合。找到数组元素的总和,然后找到2的最小幂大于或等于N。
下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find the nearest power of 2
int 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 used
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
int 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


Javascript


输出:
8

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。