Click here to Skip to main content
15,885,278 members
Please Sign up or sign in to vote.
1.00/5 (4 votes)
See more:
please help me write this program:

Write a function sumIsPower with signatuare 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, ?; in general a number is a power of 2 if and only if it is of the form 2^n for some nonnegative integer n. You may assume (without verifying in your code) that all elements in the array are positive integers. If the input arr is null or of length 0, the return value should be false. Note: x^n means x to the power of n.
Posted
Comments
Jαved 12-Apr-12 6:07am    
It seems to be your Assignment. Try it yourself. if any problem arises post it here.
saifullahiit 12-Apr-12 6:09am    
its not an assignment. its my entry test sample question. please help me out
V. 12-Apr-12 6:54am    
There's not much difference. Mind you, people don't mind answering homework questions, more the fact that homework questions are of the type "plz give code urgentzzz" while a simple google could do the trick. Reading your question I have to admit I also got the feeling you were being lazy. (maybe you are, maybe you're not)
Richard MacCutchan 12-Apr-12 6:57am    
Why? If you cannot figure this out by yourself then you should not pass the test. If we give you the answer and you pass then you have cheated your way in and possibly excluded someone else who did do all the work on their own. Learn to stand on your own two feet and at least make an effort.

I don't normally bite when people are so lazy they can't even be bothered to Google for what is a widely discussed subject[^]

C#
public bool sumIsPower(int[] arr)
{
    if (arr == null || arr.Length == 0)
        return false;

    int total = 0;
    for (int i = 0; i < arr.Length; ++i)
        total += arr[i];

    return (total != 0) && ((total & (total - 1)) == 0); 
} 


...think that should do it tho - had to write one of these out with a pencil for a job interview recently!
 
Share this answer
 
v2
Comments
Pravinjas 12-Apr-12 7:38am    
Hi..please explain more on this return (total != 0) && ((total & (total - 1)) == 0);

I cant understand above line
Dylan Morley 12-Apr-12 8:09am    
Goto the link I provided. Look at the top answer submitted by Greg Hewgill which completely explains this.
Richard MacCutchan 12-Apr-12 8:07am    
Not sure I agree with giving the solution, but I have to admit it's an elegant piece of code.
Dylan Morley 12-Apr-12 8:12am    
Yeah, normally I wouldn't ... just the fact I'd answered something myself recently. Was surprisingly difficult writing out nicely indented code on paper & no IDE helping out..!
It's not difficult - but since this is a test, you really should do the work yourself! So, no code.

It's pretty easy:
0) Check the error conditions they mentioned.
1) Loop though each element in the input, adding them together. You might want to detect overflow and report a problem.
2) Examine the result: deciding if it is a power of two. There are a lot of ways to do this, here are two:
2.1) Loop though the sum, comparing it with a single moving bit 1, 2, 4, 8, 16, ... If you get a match it's a power of two.
2.2) Count the '1' bits in the number. If it is one, then it's a power of two. There are a lot of ways to do that, as well - the MIT HAKEM memo provides a lovely no-loop way to do it!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900