Skip to content

Instantly share code, notes, and snippets.

@bijay-shrestha
Created July 2, 2021 09:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bijay-shrestha/b7b81b96103f93b5d033928a54be6e47 to your computer and use it in GitHub Desktop.
Save bijay-shrestha/b7b81b96103f93b5d033928a54be6e47 to your computer and use it in GitHub Desktop.
/**
* * Write a function sumIsPower with signature boolean sumIsPower(int[] arr)
* * which outputs true if the sum of the elements in the input array arr is a power of 2, false otherwise.
* *
* * Recall that the powers of 2 are 1, 2, 4, 8, 16, and so on.
* * In general a number is a power of 2 if and only if it is of the form 2^n for some non-negative integer n.
* *
* * You may assume (without verifying in your code) that all elements in the array
* * are positive integers.
* *
* * If the input array arr is null, the return value should be false.
* *
* * Examples: sumIsPower({8,8,8,8}) is true since 8 + 8 + 8 + 8 = 32 = 2^5 .
* * sumIsPower({8,8,8}) is false, since 8 + 8 +8 = 24, not a power of 2.
*/
package com.basic.practice;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class SumIsPower {
public static void main(String[] args) {
int[] arrayOfNumbers = {8, 8, 8, 8};
log.info("Checking if the array {} has the property SumIsPower or not. Return true for Yes and false for No.", arrayOfNumbers);
log.info("Actual Result: {}", sumIsPower(arrayOfNumbers));
}
static boolean sumIsPower(int[] a) {
if (a == null) {
return false;
}
boolean sumIsPower = false;
int len = a.length;
int sum = 0;
for (int i = 0; i < len; i++) {
sum += a[i];
}
int n = sum;
log.info("Sum: {}", sum);
while (n > 1) {
if (n % 2 != 0) {
return false;
}
n = n / 2;
}
return true;
}
}
@sanjmgr
Copy link

sanjmgr commented Aug 16, 2022

static boolean sumIsPower(int[] arr) {
        if (arr == null) return false;

        int sum = 0;
        for (int item : arr) sum += item;

        while (sum > 1) {
            if (sum % 2 != 0) return false;
            sum /= 2;
        }

        return true;
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment