Find the sum of all array elements that are equidistant from two consecutive powers of 2
Given an array arr[] consisting of N integers, the task is to find the sum of array elements that are equidistant from the two consecutive powers of 2.
Examples:
Input: arr[] = {10, 24, 17, 3, 8}
Output: 27
Explanation:
Following array elements are equidistant from two consecutive powers of 2:
- arr[1] (= 24) is equally separated from 24 and 25.
- arr[3] (= 3) is equally separated from 21 and 22.
Therefore, the sum of 24 and 3 is 27.
Input: arr[] = {17, 5, 6, 35}
Output: 6
Approach: Follow the steps below to solve the problem:
- Initialize a variable, say res, that stores the sum of array elements.
- Traverse the given array arr[] and perform the following steps:
- Find the value of log2(arr[i]) and store it in a variable, say power.
- Find the value of 2(power) and 2(power + 1) and store them in variables, say LesserValue and LargerValue, respectively.
- If the value of (arr[i] – LesserValue) equal to (LargerValue – arr[i]), then increment the value of res by arr[i].
- After completing the above steps, print the value of res as the resultant sum.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int FindSum( int arr[], int N)
{
int res = 0;
for ( int i = 0; i < N; i++) {
int power = log2(arr[i]);
int LesserValue = pow (2, power);
int LargerValue = pow (2, power + 1);
if ((arr[i] - LesserValue)
== (LargerValue - arr[i])) {
res += arr[i];
}
}
return res;
}
int main()
{
int arr[] = { 10, 24, 17, 3, 8 };
int N = sizeof (arr) / sizeof (arr[0]);
cout << FindSum(arr, N);
return 0;
}
|
Java
class GFG {
static int FindSum( int [] arr, int N)
{
int res = 0 ;
for ( int i = 0 ; i < N; i++) {
int power
= ( int )(Math.log(arr[i]) / Math.log( 2 ));
int LesserValue = ( int )Math.pow( 2 , power);
int LargerValue = ( int )Math.pow( 2 , power + 1 );
if ((arr[i] - LesserValue)
== (LargerValue - arr[i])) {
res += arr[i];
}
}
return res;
}
public static void main(String[] args)
{
int [] arr = { 10 , 24 , 17 , 3 , 8 };
int N = arr.length;
System.out.println(FindSum(arr, N));
}
}
|
Python3
from math import log2
def FindSum(arr, N):
res = 0
for i in range (N):
power = int (log2(arr[i]))
LesserValue = pow ( 2 , power)
LargerValue = pow ( 2 , power + 1 )
if ((arr[i] - LesserValue) = =
(LargerValue - arr[i])):
res + = arr[i]
return res
if __name__ = = '__main__' :
arr = [ 10 , 24 , 17 , 3 , 8 ]
N = len (arr)
print (FindSum(arr, N))
|
C#
using System;
class GFG{
static int FindSum( int [] arr, int N)
{
int res = 0;
for ( int i = 0; i < N; i++) {
int power = ( int )(Math.Log(arr[i]) / Math.Log(2));
int LesserValue = ( int )Math.Pow(2, power);
int LargerValue = ( int )Math.Pow(2, power + 1);
if ((arr[i] - LesserValue)
== (LargerValue - arr[i])) {
res += arr[i];
}
}
return res;
}
public static void Main()
{
int [] arr= { 10, 24, 17, 3, 8 };
int N = arr.Length;
Console.WriteLine(FindSum(arr, N));
}
}
|
Javascript
<script>
function FindSum(arr, N)
{
let res = 0;
for (let i = 0; i < N; i++)
{
let power = Math.floor(
Math.log2(arr[i]));
let LesserValue = Math.pow(2, power);
let LargerValue = Math.pow(2, power + 1);
if ((arr[i] - LesserValue) ==
(LargerValue - arr[i]))
{
res += arr[i];
}
}
return res;
}
let arr = [ 10, 24, 17, 3, 8 ];
let N = arr.length;
document.write(FindSum(arr, N));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(1)
Last Updated :
09 Nov, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...