Skip to content

Detect If Two Integers Have Opposite Signs

In this question, input two numbers and detect if they have opposite signs. We solve using bitwise XOR operator

Gopi Gorantala
Gopi Gorantala
2 min read

In this lesson, we detect if two integers have different signs. Ignore the values of the inputs for this problem and focus on the signs.

Introduction

In this question, input two numbers and detect if they have opposite signs.

Problem statement

We need to write a program to detect if two input integers have opposite signs.

Input: a = 100, b = -1 

Output: "Signs are opposite"
Input: a = 100, b = 501 

Output: "Signs are not opposite."

Concept

We have already learned about representing/finding a positive/negative number in the NOT lesson.

Two rules:

If the leading bit on the left side is 0, then it is a positive number.
If the leading bit on the left side is 1, then it is a negative number.

Solution

The XOR rule says the output will be 1 only when two input values are opposite (0 and 1, or 1 and 0).

The above concept clearly says if the leading left-MSB (left-most significant bit) is 1, then it’s negative.

The following four examples will clearly explain these concepts.

  • Consider two numbers whose left MSBs are both 1, and the XOR of them is 0.
  • If two numbers both have left MSBs of 0, then the XOR of them is 0.
  • Now, when the left MSB of two inputs is different, then the XOR yields to 1, which is a negative number.

So, when we perform (x^y) < 0, we get the correct answer.

Java

class DetectIfTwoIntegersHaveOppositeSigns {
    private static String oppositeSigns(int x, int y) {
        return (x ^ y) < 0 ? "Signs are opposite" : "Signs are not opposite";
    }

    public static void main(String[] args) {
        int x = 100, y = -1;
        System.out.println("For inputs " + x + ", " + y + " : " + oppositeSigns(x, y));

        int z = 100, p = 501;
        System.out.println("For inputs " + z + ", " + p + " : " + oppositeSigns(z, p));
    }
}

Python

def oppositeSigns(x,y):
  return "signs are opposite" if (x ^ y)< 0 else "signs are not opposite"

x=100
y=-1
print("For inputs ",x,"," ,y ,":" ,oppositeSigns(x,y))

z=100
p=501
print("For inputs ",z,"," ,p ,":" ,oppositeSigns(z,p))

JavaScript

const helper = (a, b) => {
    return (a ^ b) < 0 ? 'Signs are opposite' : 'Signs are not opposite';
}

const x = 100, y = -1;
console.log (`For inputs ${x}, ${y} :  ${helper (x, y)}`);

const z = 100, p = 501;
console.log (`For inputs ${z}, ${p} :  ${helper (z, p)}`);

C++

#include <iostream>
#include <string>

using namespace std;

string oppositeSign(int x, int y) {
    return (x ^ y) < 0 ? "Signs are opposite" : "Signs are not opposite";
}

int main() {
    int x = 100, y = -1;
    cout << "For inputs " << x << " , " << y << " : " << oppositeSign(x, y) << endl;
    int z = 100, p = 501;
    cout << "For inputs " << z << " , " << p << " : " << oppositeSign(z, p) << endl;
    return 0;
}

TypeScript

export const helper = (a: number, b: number): string => {
    return (a ^ b) < 0 ? 'Signs are opposite' : 'Signs are not opposite';
}

const x: number = 100;
const y: number = -1;
console.log(`For inputs ${x}, ${y} :  ${helper(x, y)}`);

const z: number = 100;
const p: number = 501;
console.log(`For inputs ${z}, ${p} :  ${helper(z, p)}`);

Complexity analysis

Time Complexity: We are not running a loop or scaling the inputs. The inputs never change. So, the operation takes a single unit of time, which is O(1).

Space Complexity: We didn’t create an extra memory for this. So, space complexity is O(1).

Coding Interview QuestionsData Structures and AlgorithmsBit Manipulation

Gopi Gorantala Twitter

Gopi is an engineering leader with 12+ of experience in full-stack development—a specialist in Java technology stack. He worked for multiple startups, the European govt, and FAANG in India and Europe.

Comments


Related Posts

Members Public

Leetcode 217: Contains Duplicate

This question marks the first problem when working on duplicate data, either integers or strings etc. Companies that have asked this in their coding interview are Amazon, Apple, Netflix, Google, Microsoft, Adobe, Facebook, and many more top tech companies. Problem statement Given an integer array nums, return true if any

Leetcode 217: Contains Duplicate
Members Public

Leetcode 121: Best Time To Buy and Sell Stock

The Best time to buy and sell stock problem is a classic problem that can be solved using the Greedy approach. This is one of the most popular questions asked in such interviews. Companies that have asked this in their coding interview are Facebook, Amazon, Apple, Netflix, Google, Microsoft, Adobe,

Leetcode 121: Best Time To Buy and Sell Stock
Members Public

Bit Manipulation Course Overview

Overview In this course, you will learn how to solve problems using bit manipulation, a powerful technique that can be used to optimize your algorithmic and problem-solving skills. This is one of the most important/critical topics when someone starts preparing for coding interviews for FAANG companies. To kick things