Smallest power of 2 which is greater than or equal to sum of array elements
Last Updated :
14 Jun, 2022
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 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
<?php
// PHP implementation of the above approach
// Function to find the nearest power of 2
function 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 used
function 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 2
function 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 used
function 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 Code
let arr = [ 1, 2, 3, 2 ];
let n = arr.length;
document.write(memoryUsed(arr, n));
</script>
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.
Similar Reads
Size of smallest subarray to be removed to make count of array elements greater and smaller than K equal Given an integer K and an array arr[] consisting of N integers, the task is to find the length of the subarray of smallest possible length to be removed such that the count of array elements smaller than and greater than K in the remaining array are equal. Examples: Input: arr[] = {5, 7, 2, 8, 7, 4,
10 min read
Smallest subarray from a given Array with sum greater than or equal to K | Set 2 Given an array A[] consisting of N positive integers and an integer K, the task is to find the length of the smallest subarray with a sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: arr[] = {3, 1, 7, 1, 2}, K = 11Output: 3Explanation:The smallest subarray with
15+ min read
Smallest subarray with sum greater than or equal to K Given an array A[] consisting of N integers and an integer K, the task is to find the length of the smallest subarray with sum greater than or equal to K. If no such subarray exists, print -1. Examples: Input: A[] = {2, -1, 2}, K = 3 Output: 3 Explanation: Sum of the given array is 3. Hence, the sm
15+ min read
Minimize swap of elements such that one Array has greater sum than other Given two arrays A[] and B[] with size N and M, the task is to find the minimum number of swaps between two arrays (can be any two element of two arrays) required to make the sum of array A[] strictly greater than array B[]. Examples: Input: N = 5, A[] = {1, 5, 4, 6, 2}, M = 7, B[] = {0, 1, 17, 4, 6
15+ min read
Find array elements equal to sum of any subarray of at least size 2 Given an array arr[], the task is to find the elements from the array which are equal to the sum of any sub-array of size greater than 1.Examples: Input: arr[] = {1, 2, 3, 4, 5, 6} Output: 3, 5, 6 Explanation: The elements 3, 5, 6 are equal to sum of subarrays {1, 2},{2, 3} and {1, 2, 3} respectivel
6 min read