आसान 0 हल किए गए 0% स्वीकृति

Perfect Square Check

Decide whether an integer is the square of a whole number.

आप क्या सीखेंगे

  • Reuse an integer square root to test squares
  • Handle negative inputs
  • Avoid floating point comparisons

समस्या

अनुवाद दिखाएँ

A perfect square is a number like 0, 1, 4, 9 or 16 that equals k × k for some integer k. Negative numbers are never perfect squares. Check with integer arithmetic so that very large inputs are handled exactly.

Implement perfect_square_check(n).

Input

n: The integer to test. It may be negative.

Output

Return true if n = k × k for some integer k, otherwise false.

सीमाएँ

−10^18 ≤ n ≤ 10^18

उदाहरण

उदाहरण 1
इनपुट:
n = 49

अपेक्षित आउटपुट:
expected = True
इनपुट:
long long n = 49;

अपेक्षित आउटपुट:
bool expected = true;
इनपुट:
long n = 49;

अपेक्षित आउटपुट:
bool expected = true;

व्याख्या

49 = 7 × 7, so the answer is true.

उदाहरण 2
इनपुट:
n = 50

अपेक्षित आउटपुट:
expected = False
इनपुट:
long long n = 50;

अपेक्षित आउटपुट:
bool expected = false;
इनपुट:
long n = 50;

अपेक्षित आउटपुट:
bool expected = false;

व्याख्या

7 × 7 = 49 and 8 × 8 = 64, and 50 falls between them, so the answer is false.

कैसे हल करें

पहले खुद कोशिश करें, फिर एक-एक करके संकेत खोलें।

संकेत 1

Find the integer square root r of n first.

संकेत 2

n is a perfect square exactly when r × r equals n.

संकेत 3

Return false right away for negative numbers.

अपेक्षित जटिलता

O(log n) time, O(1) extra space

संदर्भ

यह चुनौती CodeUtility के लिए अनुकूलित की गई है। ऊपर दिए गए स्टेटमेंट और सीमाओं का पालन करें।

हर कोडर का सबसे अच्छा पास हुआ समाधान। तेज़, कम मेमोरी वाले और बेहतर रेटिंग वाले समाधान ऊपर रहते हैं।

अभी कोई पास हुआ समाधान नहीं है। इसे हल करें और पहला स्थान पाएँ।

दूसरों से सीखें

समुदाय समाधान

अभी तक कोई प्रकाशित समाधान नहीं। पहला साझा करने के लिए इस चुनौती को हल करें।