/* LittleElephantAndDouble (SRM 597, D2, 250 from Topcoder). James S. Plank Wed Dec 4 11:34:32 EST 2019 This is an O(n) solution that divides each element by two until it is no longer divisible by two. It checks each element with A[0]. */ #include #include #include using namespace std; class LittleElephantAndDouble { public: string getAnswer(vector &A); }; string LittleElephantAndDouble::getAnswer(vector &A) { size_t i; for (i = 0; i < A.size(); i++) { while ((A[i] & 0x1) == 0) A[i] >>= 1; /* I'm using bit arithmetic instead of div and mod. */ if (A[i] != A[0]) return "NO"; } return "YES"; }