Skip to content

Commit 25d02c6

Browse files
committed
valid perfect square
1 parent 24a655a commit 25d02c6

File tree

3 files changed

+31
-2
lines changed

3 files changed

+31
-2
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
| S.No. | Topic | Status |
66
|---|--------------|-----|
77
|01. | [Array](https://github.com/geeky01adarsh/DSA-Interview-Questions/tree/main/Arrays) | 24/28 |
8-
|02. | [Searching](/Searching)|3/38|
8+
|02. | [Searching](/Searching)|4/38|

Searching/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
|1|[Square Root](https://leetcode.com/problems/sqrtx/)|[Solution](sqrt.cpp)|
77
|2| [First Bad Version](https://leetcode.com/problems/first-bad-version/)|[Solution](first_bad_version.cpp)|
88
|3| [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/)|[Solution](two_sum_ii_input_array_is_sorted.cpp)|
9-
|4| [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/)|Pending|
9+
|4| [Valid Perfect Square](https://leetcode.com/problems/valid-perfect-square/)|[Solution](valid_perfect_square.cpp)|
1010
|5| [Arranging Coins(Easy)](https://leetcode.com/problems/arranging-coins/)|Pending|
1111
|6| [Find Smallest Letter Greater Than Target](https://leetcode.com/problems/find-smallest-letter-greater-than-target/)|Pending|
1212
|7| [Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/)|Pending|

Searching/valid_perfect_square.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
bool isPerfectSquare(int num)
5+
{
6+
int low = 1, high = num;
7+
while (low <= high)
8+
{
9+
long long mid = low + (high - low) / 2;
10+
if ((mid * mid) == num)
11+
return 1;
12+
else if (mid * mid > num)
13+
high = mid - 1;
14+
else
15+
low = mid + 1;
16+
}
17+
return 0;
18+
}
19+
20+
int main(){
21+
int t;
22+
cin>>t;
23+
while(t--){
24+
int n;
25+
cin>>n;
26+
cout<<isPerfectSquare(n)<<endl;
27+
}
28+
return 0;
29+
}

0 commit comments

Comments
 (0)