From 36496b6a6094aec3c500591ddd68ab2d88b1518c Mon Sep 17 00:00:00 2001 From: hacreating <68935517+hacreating@users.noreply.github.com> Date: Mon, 31 Oct 2022 02:08:15 +0530 Subject: [PATCH] Create TrailingZeroes.java Most of the students know how to write a program to find a trailing zero , but when there is a twist come in a question or problem statement like find the trailing zeros in Binary Representation of a input number then everyone losses the hope . but now I come up with a super optimized solution for such problems with having O(1) Time Complexity (Constant time complexity ) which is an achievement in it self . example input :- 168 ; output :- 3 example input :- 128 ; output :- 7 --- Leetcode/TrailingZeroes.java | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 Leetcode/TrailingZeroes.java diff --git a/Leetcode/TrailingZeroes.java b/Leetcode/TrailingZeroes.java new file mode 100644 index 0000000..7815749 --- /dev/null +++ b/Leetcode/TrailingZeroes.java @@ -0,0 +1,11 @@ +import java.util.Scanner; +public class TrailingZeroes{ + static int numberOfTz(int n){ + return (int)(Math.log10((n&n-1)^n)/Math.log10(2)); + } + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + System.out.println(numberOfTz(n)); + } +}