📜  求与两个连续的 2 的幂等距的所有数组元素的总和

📅  最后修改于: 2021-09-06 06:38:34             🧑  作者: Mango

给定一个由N 个整数组成的数组arr[] ,任务是找到与 2 的两个连续幂等距的数组元素之和。

例子:

处理方法:按照以下步骤解决问题:

  • 初始化一个变量,比如res ,它存储数组元素的总和。
  • 遍历给定的数组arr[]并执行以下步骤:
    • 找到log 2 (arr[i]) 的值并将其存储在一个变量中,比如power
    • 2 (幂)2 (幂 + 1)的值 并将它们存储在变量中,分别说LesserValueLargerValue
    • 如果(arr[i] – LesserValue) 的值等于(LargerValue – arr[i]) ,则将res的值增加arr[i]
  • 完成上述步骤后,打印res的值作为结果和。

下面是上述方法的实现:

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


输出:
27

时间复杂度: O(N)
辅助空间: O(1)

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live