Dado un entero positivo N , la tarea es verificar si el equivalente binario de ese entero termina en «001» o no.
Escriba “ Sí ” si termina en “001”. De lo contrario, escriba “ No ”.
Ejemplos:
Entrada : N = 9
Salida : Sí
Explicación
Binario de 9 = 1001, que termina en 001
Entrada : N = 5
Salida : No
Binario de 5 = 101, que no termina en 001
Enfoque ingenuo
Encuentre el equivalente binario de N y verifique si 001 es un sufijo de su equivalente binario.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the // above approach #include <bits/stdc++.h> using namespace std; // Function returns true if // s1 is suffix of s2 bool isSuffix(string s1, string s2) { int n1 = s1.length(); int n2 = s2.length(); if (n1 > n2) return false; for (int i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } // Function to check if binary equivalent // of a number ends in "001" or not bool CheckBinaryEquivalent(int N) { // To store the binary // number int B_Number = 0; int cnt = 0; while (N != 0) { int rem = N % 2; int c = pow(10, cnt); B_Number += rem * c; N /= 2; // Count used to store // exponent value cnt++; } string bin = to_string(B_Number); return isSuffix("001", bin); } // Driver code int main() { int N = 9; if (CheckBinaryEquivalent(N)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the above approach class GFG{ // Function returns true if // s1 is suffix of s2 static boolean isSuffix(String s1, String s2) { int n1 = s1.length(); int n2 = s2.length(); if (n1 > n2) return false; for(int i = 0; i < n1; i++) if (s1.charAt(n1 - i - 1) != s2.charAt(n2 - i - 1)) return false; return true; } // Function to check if binary equivalent // of a number ends in "001" or not static boolean CheckBinaryEquivalent(int N) { // To store the binary // number int B_Number = 0; int cnt = 0; while (N != 0) { int rem = N % 2; int c = (int)Math.pow(10, cnt); B_Number += rem * c; N /= 2; // Count used to store // exponent value cnt++; } String bin = Integer.toString(B_Number); return isSuffix("001", bin); } // Driver code public static void main (String[] args) { int N = 9; if (CheckBinaryEquivalent(N)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the # above approach # Function returns true if # s1 is suffix of s2 def isSuffix(s1, s2) : n1 = len(s1); n2 = len(s2); if (n1 > n2) : return False; for i in range(n1) : if (s1[n1 - i - 1] != s2[n2 - i - 1]) : return False; return True; # Function to check if binary equivalent # of a number ends in "001" or not def CheckBinaryEquivalent(N) : # To store the binary # number B_Number = 0; cnt = 0; while (N != 0) : rem = N % 2; c = 10 ** cnt; B_Number += rem * c; N //= 2; # Count used to store # exponent value cnt += 1; bin = str(B_Number); return isSuffix("001", bin); # Driver code if __name__ == "__main__" : N = 9; if (CheckBinaryEquivalent(N)) : print("Yes"); else : print("No"); # This code is contributed by AnkitRai01
C#
// C# implementation of the above approach using System; class GFG{ // Function returns true if // s1 is suffix of s2 static bool isSuffix(string s1, string s2) { int n1 = s1.Length; int n2 = s2.Length; if (n1 > n2) return false; for(int i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } // Function to check if binary equivalent // of a number ends in "001" or not static bool CheckBinaryEquivalent(int N) { // To store the binary // number int B_Number = 0; int cnt = 0; while (N != 0) { int rem = N % 2; int c = (int)Math.Pow(10, cnt); B_Number += rem * c; N /= 2; // Count used to store // exponent value cnt++; } string bin = B_Number.ToString(); return isSuffix("001", bin); } // Driver code public static void Main (string[] args) { int N = 9; if (CheckBinaryEquivalent(N)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by AnkitRai01
Javascript
<script> // javascript implementation of the above approach // Function returns true if // s1 is suffix of s2 function isSuffix( s1, s2) { var n1 = s1.length; var n2 = s2.length; if (n1 > n2) return false; for(var i = 0; i < n1; i++) if (s1[n1 - i - 1] != s2[n2 - i - 1]) return false; return true; } // Function to check if binary equivalent // of a number ends in "001" or not function CheckBinaryEquivalent( N) { // To store the binary // number var B_Number = 0; var cnt = 0; while (N != 0) { var rem = N % 2; var c = Math.pow(10, cnt); B_Number += rem * c; N = Math.floor(N/ 2); // Count used to store // exponent value cnt++; } console.log(B_Number); var bin = B_Number.toString(); return isSuffix("001", bin); } // Driver code var N = 9; if (CheckBinaryEquivalent(N)) document.write("Yes"); else document.write("No"); </script>
Yes
Complejidad temporal: O(N)
Espacio auxiliar: O(1)
Enfoque eficiente
Podemos observar que el equivalente binario de un número termina en “001” solo cuando (N – 1) es divisible por 8 .
Ilustración:
La secuencia 1, 9, 17, 25, 33……. tiene 001 como sufijo en su representación binaria. El término
N de la secuencia anterior se denota por 8 * N + 1. Por lo tanto, el equivalente binario de un número termina en «001» solo cuando (N – 1) % 8 == 0
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above // approach #include <bits/stdc++.h> using namespace std; // Function to check if binary // equivalent of a number ends // in "001" or not bool CheckBinaryEquivalent(int N) { // To check if binary equivalent // of a number ends in // "001" or not return (N - 1) % 8 == 0; } // Driver code int main() { int N = 9; if (CheckBinaryEquivalent(N)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the above approach class GFG{ // Function to check if binary // equivalent of a number ends // in "001" or not static boolean CheckBinaryEquivalent(int N) { // To check if binary equivalent // of a number ends in // "001" or not return (N - 1) % 8 == 0; } // Driver code public static void main (String[] args) { int N = 9; if (CheckBinaryEquivalent(N)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by AnkitRai01
Python3
# Python3 implementation of the above approach # Function to check if binary # equivalent of a number ends # in "001" or not def CheckBinaryEquivalent(N): # To check if binary equivalent # of a number ends in # "001" or not return (N - 1) % 8 == 0; # Driver code if __name__ == "__main__": N = 9; if (CheckBinaryEquivalent(N)): print("Yes"); else : print("No"); # This code is contributed by AnkitRai01
C#
// C# implementation of the above approach using System; class GFG{ // Function to check if binary // equivalent of a number ends // in "001" or not static bool CheckBinaryEquivalent(int N) { // To check if binary equivalent // of a number ends in // "001" or not return (N - 1) % 8 == 0; } // Driver code public static void Main (string[] args) { int N = 9; if (CheckBinaryEquivalent(N)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by AnkitRai01
Javascript
<script> // Javascript implementation of the above // approach // Function to check if binary // equivalent of a number ends // in "001" or not function CheckBinaryEquivalent(N) { // To check if binary equivalent // of a number ends in // "001" or not return (N - 1) % 8 == 0; } // Driver code var N = 9; if (CheckBinaryEquivalent(N)) document.write( "Yes"); else document.write( "No"); </script>
Yes
Complejidad temporal: O(1)
Espacio auxiliar: O(1)
Enfoque bit a bit
si alguno no N tiene (001) 2 al final en su representación binaria, luego N-1 tiene (000) 2 al final en su representación binaria, luego haciendo bit a bit y con 7 (111) 2 le dará (000) 2 como resultado.
tan simple retorno !((N – 1) & 7)
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to check if binary equivalent of a number ends in "001" or not bool CheckBinaryEquivalent(int N) { // N = 9 // N - 1 = 8 => 8 = 1000 // (1000 & 111) == 0 then return true // else return false return !((N - 1) & 7); } // Driver code int main() { int N = 9; if (CheckBinaryEquivalent(N)) cout << "Yes"; else cout << "No"; return 0; }
Java
// Java implementation of the above approach class GFG { // Function to check if binary equivalent of a number // ends in "001" or not static boolean CheckBinaryEquivalent(int N) { // N = 9 // N - 1 = 8 => 8 = 1000 // (1000 & 111) == 0 then return true // else return false if (((N - 1) & 7) > 0) return false; else return true; } // Driver code public static void main(String[] args) { int N = 9; if (CheckBinaryEquivalent(N)) System.out.println("Yes"); else System.out.println("No"); } } // This code is contributed by ajaymakvana
Python3
# Python implementation of the above approach # Function to check if binary equivalent of a number ends in "001" or not def CheckBinaryEquivalent(N): # N = 9 # N - 1 = 8 => 8 = 1000 # (1000 & 111) == 0 then return true # else return false return (not((N - 1) & 7)) N = 9 if (CheckBinaryEquivalent(N)): print("Yes") else: print("No") # This code is contributed by ajaymakvava.
C#
// C# implementation of the above approach using System; class GFG { // Function to check if binary equivalent of a number ends in "001" or not static bool CheckBinaryEquivalent(int N) { // N = 9 // N - 1 = 8 => 8 = 1000 // (1000 & 111) == 0 then return true // else return false if (((N - 1) & 7) > 0) return false; else return true; } // Driver code public static void Main(string[] args) { int N = 9; if (CheckBinaryEquivalent(N)) Console.WriteLine("Yes"); else Console.WriteLine("No"); } } // This code is contributed by ajaymakvana
Javascript
// JavaScript implementation of the above approach // Function to check if binary equivalent of a number ends in "001" or not function CheckBinaryEquivalent(N) { // N = 9 // N - 1 = 8 => 8 = 1000 // (1000 & 111) == 0 then return true // else return false return !((N - 1) & 7); } // Driver code let N = 9; if (CheckBinaryEquivalent(N)) console.log("Yes"); else console.log("No"); // This code is contributed by phasing17
Yes
Time Complexity: O(1) Space COmplexity: O(1)