Garākie 1 pēc kārtas binārajā attēlojumā
Dots skaitlis n Uzdevums ir atrast garākās kārtas garumu 1s sēriju tās binārajā attēlojumā.
Piemēri:
Ievade: n = 14
Izvade: 3
Paskaidrojums: 14 binārais attēlojums ir 111 0.Ievade: n = 222
Izvade: 4
Paskaidrojums: 222 binārais attēlojums ir 110 1111 0.
Satura rādītājs
- [Naīvā pieeja] Iteratīvais laiks O(1) un telpa O(1)
- [Efektīva pieeja] Izmantojot bitu manipulācijas O(1) laiks un O(1) telpa
- [Cita pieeja] Virkņu konvertēšanas izmantošana
[Naīvā pieeja] Iteratīvais laiks O(1) un telpa O(1)
C++ #include using namespace std ; int maxConsecutiveOne ( int n ){ int count = 0 ; int maxi = 0 ; // traverse and check if bit set increment the count for ( int i = 0 ; i < 32 ; i ++ ){ if ( n & ( 1 < < i )){ count ++ ; } else { maxi = max ( maxi count ); count = 0 ; } } return maxi ; } int main () { int n = 14 ; cout < < maxConsecutiveOne ( n ) < < 'n' ; return 0 ; }
Java public class GFG { static int maxConsecutiveOne ( int n ) { int count = 0 ; int maxi = 0 ; // traverse and check if bit set increment the count for ( int i = 0 ; i < 32 ; i ++ ) { if (( n & ( 1 < < i )) != 0 ) { count ++ ; } else { maxi = Math . max ( maxi count ); count = 0 ; } } return maxi ; } public static void main ( String [] args ) { int n = 14 ; System . out . println ( maxConsecutiveOne ( n )); } }
Python def maxConsecutiveOne ( n ): count = 0 maxi = 0 # traverse and check if bit set increment the count for i in range ( 32 ): if n & ( 1 < < i ): count += 1 else : maxi = max ( maxi count ) count = 0 return maxi if __name__ == '__main__' : n = 14 print ( maxConsecutiveOne ( n ))
C# using System ; class GFG { static int MaxConsecutiveOne ( int n ) { int count = 0 ; int maxi = 0 ; // traverse and check if bit set increment the count for ( int i = 0 ; i < 32 ; i ++ ) { if (( n & ( 1 < < i )) != 0 ) { count ++ ; } else { maxi = Math . Max ( maxi count ); count = 0 ; } } return maxi ; } static void Main () { int n = 14 ; Console . WriteLine ( MaxConsecutiveOne ( n )); } }
JavaScript function maxConsecutiveOne ( n ) { let count = 0 ; let maxi = 0 ; // traverse and check if bit set increment the count for ( let i = 0 ; i < 32 ; i ++ ) { if ( n & ( 1 < < i )) { count ++ ; } else { maxi = Math . max ( maxi count ); count = 0 ; } } return maxi ; } // Driver code let n = 14 ; console . log ( maxConsecutiveOne ( n ));
Izvade
3
[Efektīva pieeja] Izmantojot bitu manipulācijas O(1) laiks un O(1) telpa
Ideja ir balstīta uz koncepciju, ka UN bitu secības ar a pa kreisi nobīdīts par 1 versija pati par sevi efektīvi noņem beigas 1 no katras secības secības 1s .
Tātad operācija n = (n & (n < < 1)) samazina katras secības garumu 1s ar vienu binārajā attēlojumā n . Ja mēs turpinām veikt šo darbību cilpā, mēs nonākam pie n = 0. Iterāciju skaits, kas nepieciešams, lai sasniegtu faktiski ir garākās secīgās secības garums 1s .
Ilustrācija:
Lai īstenotu iepriekš minēto pieeju, veiciet tālāk norādītās darbības.
- Izveidojiet mainīgo skaitu, kas inicializēts ar vērtību .
- Palaidiet kamēr cilpa līdz n nav 0.
- Katrā iterācijā veiciet darbību n = (n & (n < < 1))
- Palieliniet skaitu par vienu.
- Atdeves skaits
#include using namespace std ; int maxConsecutiveOnes ( int x ) { // Initialize result int count = 0 ; // Count the number of iterations to // reach x = 0. while ( x != 0 ) { // This operation reduces length // of every sequence of 1s by one. x = ( x & ( x < < 1 )); count ++ ; } return count ; } int main () { // Function Call cout < < maxConsecutiveOnes ( 14 ) < < endl ; return 0 ; }
Java class GFG { private static int maxConsecutiveOnes ( int x ) { // Initialize result int count = 0 ; // Count the number of iterations to // reach x = 0. while ( x != 0 ) { // This operation reduces length // of every sequence of 1s by one. x = ( x & ( x < < 1 )); count ++ ; } return count ; } public static void main ( String strings [] ) { System . out . println ( maxConsecutiveOnes ( 14 )); } }
Python def maxConsecutiveOnes ( x ): # Initialize result count = 0 # Count the number of iterations to # reach x = 0. while ( x != 0 ): # This operation reduces length # of every sequence of 1s by one. x = ( x & ( x < < 1 )) count = count + 1 return count if __name__ == '__main__' : print ( maxConsecutiveOnes ( 14 )) # by Anant Agarwal.
C# using System ; class GFG { // Function to find length of the // longest consecutive 1s in binary // representation of a number private static int maxConsecutiveOnes ( int x ) { // Initialize result int count = 0 ; // Count the number of iterations // to reach x = 0. while ( x != 0 ) { // This operation reduces length // of every sequence of 1s by one. x = ( x & ( x < < 1 )); count ++ ; } return count ; } // Driver code public static void Main () { Console . WriteLine ( maxConsecutiveOnes ( 14 )); } } // This code is contributed by Nitin Mittal.
JavaScript function maxConsecutiveOnes ( x ) { // Initialize result let count = 0 ; // Count the number of iterations to reach x = 0 while ( x !== 0 ) { // This operation reduces length of // every sequence of 1s by one x = ( x & ( x < < 1 )); count ++ ; } return count ; } // Driver code console . log ( maxConsecutiveOnes ( 14 ));
PHP // PHP program to find length function maxConsecutiveOnes ( $n ) { // Initialize result $count = 0 ; // Count the number of // iterations to reach x = 0. while ( $n != 0 ) { // This operation reduces // length of every sequence // of 1s by one. $n = ( $n & ( $n < < 1 )); $count ++ ; } return $count ; } echo maxConsecutiveOnes ( 14 ) ' n ' ; ?>
Izvade
3
Laika sarežģītība: O(1)
Palīgtelpa: O(1)
[Cita pieeja] Virkņu konvertēšanas izmantošana
Mēs inicializējam divus mainīgos max_len un cur_len uz 0. Pēc tam atkārtojam katru veselā skaitļa n bitu. Ja vismazāk nozīmīgais bits (LSB) ir 1, mēs palielinām cur_len, lai uzskaitītu pašreizējo secīgo 1 s. Ja LSB ir 0, tas pārtrauc pašreizējo secību, tāpēc mēs atjauninām max_len, ja cur_len ir lielāks, un atiestatām cur_len uz 0. Pēc katra bita pārbaudes mēs nobīdām pa labi n par 1, lai pārietu uz nākamo bitu. Visbeidzot pēc cilpas beigām mēs veicam pēdējo max_len atjauninājumu, ja pēdējais cur_len ir lielāks, un atgriežam max_len kā garākās secīgo 1 s secības garumu.
C++ #include #include #include using namespace std ; int maxConsecutiveOnes ( int n ){ string binary = bitset < 32 > ( n ). to_string (); int count = 0 ; int maxCount = 0 ; // Loop through the binary string to // find the longest consecutive 1s for ( int i = 0 ; i < binary . size (); i ++ ) { if ( binary [ i ] == '1' ) { count ++ ; if ( count > maxCount ) { maxCount = count ; } } else { count = 0 ; } } // Print the result return maxCount ; } int main () { int n = 14 ; cout < < maxConsecutiveOnes ( n ) < < 'n' ; return 0 ; }
Java import java.util.* ; public class Main { static int maxConsecutiveOnes ( int n ) { String binary = String . format ( '%32s' Integer . toBinaryString ( n )). replace ( ' ' '0' ); int count = 0 ; int maxCount = 0 ; // Loop through the binary string to // find the longest consecutive 1s for ( int i = 0 ; i < binary . length (); i ++ ) { if ( binary . charAt ( i ) == '1' ) { count ++ ; if ( count > maxCount ) { maxCount = count ; } } else { count = 0 ; } } // Return the result return maxCount ; } public static void main ( String [] args ) { int n = 14 ; System . out . println ( maxConsecutiveOnes ( n )); } }
Python def maxConsecutiveOnes ( n ): binary = format ( n '032b' ) count = 0 maxCount = 0 # Loop through the binary string to # find the longest consecutive 1s for bit in binary : if bit == '1' : count += 1 if count > maxCount : maxCount = count else : count = 0 # Return the result return maxCount if __name__ == '__main__' : n = 14 print ( maxConsecutiveOnes ( n ))
C# using System ; class GFG { static int MaxConsecutiveOnes ( int n ) { string binary = Convert . ToString ( n 2 ). PadLeft ( 32 '0' ); int count = 0 ; int maxCount = 0 ; // Loop through the binary string to // find the longest consecutive 1s foreach ( char bit in binary ) { if ( bit == '1' ) { count ++ ; if ( count > maxCount ) maxCount = count ; } else { count = 0 ; } } // Return the result return maxCount ; } static void Main () { int n = 14 ; Console . WriteLine ( MaxConsecutiveOnes ( n )); } }
JavaScript function maxConsecutiveOnes ( n ) { let binary = n . toString ( 2 ). padStart ( 32 '0' ); let count = 0 ; let maxCount = 0 ; // Loop through the binary string to // find the longest consecutive 1s for ( let i = 0 ; i < binary . length ; i ++ ) { if ( binary [ i ] === '1' ) { count ++ ; if ( count > maxCount ) { maxCount = count ; } } else { count = 0 ; } } // Return the result return maxCount ; } // Driver code let n = 14 ; console . log ( maxConsecutiveOnes ( n ));
Izvade
3