Official

A - Not Overflow Editorial by en_translator


Just as instructed in the Problem Statement, receive the input as an integer and check if it is between \(-2^{31}\) and \(2^{31}-1\) (inclusive). Note that any integer greater than \(2^{31}\), and even \(2^{31}\) too, does not fit in \(32\)-bit signed integer type.

Sample code in C++:

#include <bits/stdc++.h>
using namespace std;

int main(void) {
	long long n;
	long long m = (long long)1 << 31;
	//long long m = 1 << 31; cannot be used, because the right hand side is evaluated as (int)1<<31, causing an overflow (resulting in -(2^31)), and then coverted to a long long type and assigned to m.
	//long long m = 2147483648; is OK.
	cin >> n;
	if ((-m <= n) && (n < m))cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

Sample code in Python:

n=int(input())
if -2**31<=n and n<2**31:
    print("Yes")
else: 
    print("No")

posted:
last update: