/* LittleElephantAndDouble (SRM 597, D2, 250 from Topcoder). James S. Plank Wed Dec 4 11:33:40 EST 2019 This is an O(n log n) solution that sorts the vector, and then checks every element with its neighbor. */ #include #include #include #include #include #include #include #include #include #include using namespace std; class LittleElephantAndDouble { public: string getAnswer(vector &A); }; string LittleElephantAndDouble::getAnswer(vector &A) { int i; sort(A.begin(), A.end()); /* Now, for each element, keep doubling it until it is greater than or equal to the next element. If greater, return "NO". If you succeed for all elements, return "YES". */ for (i = 0; i < A.size()-1; i++) { while (A[i] < A[i+1]) A[i] *= 2; if (A[i] != A[i+1]) return "NO"; } return "YES"; }