Skip to main content

Command Palette

Search for a command to run...

Number Complement

Published
1 min read
Number Complement
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

The complement of an integer is the integer you get when you flip all the 0's to 1's and all the 1's to 0's in its binary representation.

  • For example, The integer 5 is "101" in binary and its complement is "010" which is the integer 2.

Given an integer num, return its complement.

LeetCode Problem - 476

class Solution {
    public int findComplement(int num) {
        // Convert the given number to its binary string representation
        String binaryStr = Integer.toBinaryString(num);

        // Use StringBuilder to build the binary complement string
        StringBuilder sb = new StringBuilder();

        // Iterate through each character in the binary string
        for (char ch : binaryStr.toCharArray()) {
            // If the character is '0', append '1' to the StringBuilder
            if (ch == '0') sb.append(1);
            // If the character is '1', append '0' to the StringBuilder
            else sb.append(0);
        }

        // Convert the binary string representation of the complement back to an integer
        int number = Integer.parseInt(sb.toString(), 2);

        // Return the complement as an integer
        return number;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.