From 7ac07265bbdeb5462eb2976923bf3864075c8f5b Mon Sep 17 00:00:00 2001 From: M-Manas-s Date: Fri, 1 Oct 2021 09:14:58 +0530 Subject: [PATCH 1/2] 1356. Sort Integers by The Number of 1 Bits --- ..._Sort Integers by The Number of 1 Bits.cpp | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Algorithms/Easy/1356_SortIntegersbyTheNumberof1Bits/1356_Sort Integers by The Number of 1 Bits.cpp diff --git a/Algorithms/Easy/1356_SortIntegersbyTheNumberof1Bits/1356_Sort Integers by The Number of 1 Bits.cpp b/Algorithms/Easy/1356_SortIntegersbyTheNumberof1Bits/1356_Sort Integers by The Number of 1 Bits.cpp new file mode 100644 index 0000000..08d6b8c --- /dev/null +++ b/Algorithms/Easy/1356_SortIntegersbyTheNumberof1Bits/1356_Sort Integers by The Number of 1 Bits.cpp @@ -0,0 +1,31 @@ +#include +using namespace std; + +class Solution { +public: + + static bool cmp(int a,int b) + { + int bitsa = 0; + int bitsb = 0; + int ta=a,tb=b; + while(a!=0) + { + if ( a%2==1 ) + bitsa++; + a/=2; + } + while(b!=0) + { + if ( b%2==1 ) + bitsb++; + b/=2; + } + return ( bitsa!=bitsb ? bitsa sortByBits(vector& arr) { + sort(arr.begin(),arr.end(),cmp); + return arr; + } +}; \ No newline at end of file From 9f21c371b636816d1d5081c8c62a61a19971afd2 Mon Sep 17 00:00:00 2001 From: Manas Tiwari <46261625+M-Manas-s@users.noreply.github.com> Date: Fri, 1 Oct 2021 09:20:43 +0530 Subject: [PATCH 2/2] Edit --- ..._Sort Integers by The Number of 1 Bits.cpp | 24 +++++++++---------- 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/Algorithms/Easy/1356_SortIntegersbyTheNumberof1Bits/1356_Sort Integers by The Number of 1 Bits.cpp b/Algorithms/Easy/1356_SortIntegersbyTheNumberof1Bits/1356_Sort Integers by The Number of 1 Bits.cpp index 08d6b8c..e488d28 100644 --- a/Algorithms/Easy/1356_SortIntegersbyTheNumberof1Bits/1356_Sort Integers by The Number of 1 Bits.cpp +++ b/Algorithms/Easy/1356_SortIntegersbyTheNumberof1Bits/1356_Sort Integers by The Number of 1 Bits.cpp @@ -4,28 +4,28 @@ using namespace std; class Solution { public: - static bool cmp(int a,int b) + static bool cmp ( int a, int b ) { int bitsa = 0; int bitsb = 0; - int ta=a,tb=b; - while(a!=0) + int ta = a, tb = b; + while( a != 0 ) { - if ( a%2==1 ) + if ( a%2 == 1 ) bitsa++; - a/=2; + a /= 2; } - while(b!=0) + while( b != 0 ) { - if ( b%2==1 ) + if ( b%2 == 1 ) bitsb++; - b/=2; + b /= 2; } - return ( bitsa!=bitsb ? bitsa sortByBits(vector& arr) { - sort(arr.begin(),arr.end(),cmp); + vector sortByBits ( vector& arr ) { + sort( arr.begin(), arr.end(), cmp ); return arr; } -}; \ No newline at end of file +};