Related Articles

# Find the sum of all array elements that are equidistant from two consecutive powers of 2

• Last Updated : 19 May, 2021

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:

1. arr[1] (= 24) is equally separated from 24 and 25.
2. 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++

 `// C++ program for the above approach` `#include ``using` `namespace` `std;` `// Function to print the sum of array``// elements that are equidistant from``// two consecutive powers of 2``int` `FindSum(``int` `arr[], ``int` `N)``{``    ``// Stores the resultant sum of the``    ``// array elements``    ``int` `res = 0;` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 0; i < N; i++) {` `        ``// Stores the power of 2 of the``        ``// number arr[i]``        ``int` `power = log2(arr[i]);` `        ``// Stores the number which is``        ``// power of 2 and lesser than``        ``// or equal to arr[i]``        ``int` `LesserValue = ``pow``(2, power);` `        ``// Stores the number which is``        ``// power of 2 and greater than``        ``// or equal to arr[i]``        ``int` `LargerValue = ``pow``(2, power + 1);` `        ``// If arr[i] - LesserValue is the``        ``// same as LargerValue-arr[i]``        ``if` `((arr[i] - LesserValue)``            ``== (LargerValue - arr[i])) {` `            ``// Increment res by arr[i]``            ``res += arr[i];``        ``}``    ``}` `    ``// Return the resultant sum res``    ``return` `res;``}` `// Driver Code``int` `main()``{``    ``int` `arr[] = { 10, 24, 17, 3, 8 };``    ``int` `N = ``sizeof``(arr) / ``sizeof``(arr[0]);``    ``cout << FindSum(arr, N);` `    ``return` `0;``}`

## Java

 `// Java program for the above approach` `class` `GFG {` `    ``// Function to print the sum of array``    ``// elements that are equidistant from``    ``// two consecutive powers of 2``    ``static` `int` `FindSum(``int``[] arr, ``int` `N)``    ``{``        ``// Stores the resultant sum of the``        ``// array elements``        ``int` `res = ``0``;` `        ``// Traverse the array arr[]``        ``for` `(``int` `i = ``0``; i < N; i++) {` `            ``// Stores the power of 2 of the``            ``// number arr[i]``            ``int` `power``                ``= (``int``)(Math.log(arr[i]) / Math.log(``2``));` `            ``// Stores the number which is``            ``// power of 2 and lesser than``            ``// or equal to arr[i]``            ``int` `LesserValue = (``int``)Math.pow(``2``, power);` `            ``// Stores the number which is``            ``// power of 2 and greater than``            ``// or equal to arr[i]``            ``int` `LargerValue = (``int``)Math.pow(``2``, power + ``1``);` `            ``// If arr[i] - LesserValue is the``            ``// same as LargerValue-arr[i]``            ``if` `((arr[i] - LesserValue)``                ``== (LargerValue - arr[i])) {` `                ``// Increment res by arr[i]``                ``res += arr[i];``            ``}``        ``}` `        ``// Return the resultant sum res``        ``return` `res;``    ``}` `    ``// Driver Code``    ``public` `static` `void` `main(String[] args)``    ``{``        ``int``[] arr = { ``10``, ``24``, ``17``, ``3``, ``8` `};``        ``int` `N = arr.length;``        ``System.out.println(FindSum(arr, N));``    ``}``}` `// This code is contributed by ukasp.`

## Python3

 `# Python3 program for the above approach``from` `math ``import` `log2` `# Function to prthe sum of array``# elements that are equidistant from``# two consecutive powers of 2``def` `FindSum(arr, N):``    ` `    ``# Stores the resultant sum of the``    ``# array elements``    ``res ``=` `0` `    ``# Traverse the array arr[]``    ``for` `i ``in` `range``(N):``        ` `        ``# Stores the power of 2 of the``        ``# number arr[i]``        ``power ``=` `int``(log2(arr[i]))` `        ``# Stores the number which is``        ``# power of 2 and lesser than``        ``# or equal to arr[i]``        ``LesserValue ``=` `pow``(``2``, power)` `        ``# Stores the number which is``        ``# power of 2 and greater than``        ``# or equal to arr[i]``        ``LargerValue ``=` `pow``(``2``, power ``+` `1``)` `        ``# If arr[i] - LesserValue is the``        ``# same as LargerValue-arr[i]``        ``if` `((arr[i] ``-` `LesserValue) ``=``=``            ``(LargerValue ``-` `arr[i])):``            ` `            ``# Increment res by arr[i]``            ``res ``+``=` `arr[i]` `    ``# Return the resultant sum res``    ``return` `res` `# Driver Code``if` `__name__ ``=``=` `'__main__'``:``    ` `    ``arr ``=` `[ ``10``, ``24``, ``17``, ``3``, ``8` `]``    ``N ``=` `len``(arr)``    ` `    ``print` `(FindSum(arr, N))` `# This code is contributed by mohit kumar 29`

## C#

 `// C# program for the above approach``using` `System;`` ` `class` `GFG{`` ` `// Function to print the sum of array``// elements that are equidistant from``// two consecutive powers of 2``static` `int` `FindSum(``int``[] arr, ``int` `N)``{``    ``// Stores the resultant sum of the``    ``// array elements``    ``int` `res = 0;`` ` `    ``// Traverse the array arr[]``    ``for` `(``int` `i = 0; i < N; i++) {`` ` `        ``// Stores the power of 2 of the``        ``// number arr[i]``        ``int` `power = (``int``)(Math.Log(arr[i]) / Math.Log(2));`` ` `        ``// Stores the number which is``        ``// power of 2 and lesser than``        ``// or equal to arr[i]``        ``int` `LesserValue = (``int``)Math.Pow(2, power);`` ` `        ``// Stores the number which is``        ``// power of 2 and greater than``        ``// or equal to arr[i]``        ``int` `LargerValue = (``int``)Math.Pow(2, power + 1);`` ` `        ``// If arr[i] - LesserValue is the``        ``// same as LargerValue-arr[i]``        ``if` `((arr[i] - LesserValue)``            ``== (LargerValue - arr[i])) {`` ` `            ``// Increment res by arr[i]``            ``res += arr[i];``        ``}``    ``}`` ` `    ``// Return the resultant sum res``    ``return` `res;``}` ` ` `// Driver Code``public` `static` `void` `Main()``{``   ``int``[] arr= { 10, 24, 17, 3, 8 };``    ``int` `N = arr.Length;``     ``Console.WriteLine(FindSum(arr, N));` `}``}` `// This code is contributed by code_hunt.`

## Javascript

 ``
Output:
`27`

Time Complexity: O(N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.

My Personal Notes arrow_drop_up