From c62e2f0a26265c34ef10e0e4993f2b585645c5e0 Mon Sep 17 00:00:00 2001 From: chayan das <110921638+Chayandas07@users.noreply.github.com> Date: Thu, 7 Nov 2024 00:32:08 +0530 Subject: [PATCH] Create 3011. Find if Array Can Be Sorted --- 3011. Find if Array Can Be Sorted | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 3011. Find if Array Can Be Sorted diff --git a/3011. Find if Array Can Be Sorted b/3011. Find if Array Can Be Sorted new file mode 100644 index 0000000..5e97360 --- /dev/null +++ b/3011. Find if Array Can Be Sorted @@ -0,0 +1,17 @@ +class Solution { +public: + bool canSortArray(vector& nums) { + + vector compareArray = nums; + sort(compareArray.begin(), compareArray.end()); + + stable_sort(nums.begin(), nums.end(), [&](int a, int b) -> bool { + if(__builtin_popcount(a) == __builtin_popcount(b)){ + return a < b; + } + return false; + }); + + return compareArray == nums; + } +};