Skip to content
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.
This repository has been archived by the owner on Oct 4, 2022. It is now read-only.

Explain how you solved the coding problem #1760

Open
jaybamroliya opened this issue Oct 17, 2021 · 1 comment
Open

Explain how you solved the coding problem #1760

jaybamroliya opened this issue Oct 17, 2021 · 1 comment

Comments

@jaybamroliya
Copy link

how you solved the coding problem and material you may have referred.

Replace issue_no in the above line, with the issue related to this PR.

Code

CPP
paste_your_code_here
// Special Array With X Elements Greater Than or Equal X
// Aryan Shinde
//
// Approach:
// In this problem, my approach was to sort the given array first and then apllied binary search,
// which compares the numbers with the iteration count and decreases untill their difference doesnt
// get equal to the ierating element, if found, return that or simply return -1 if not found

// Time complexity: O(nlogn)
// Space complexity: O(1)

#include
#include
#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
int specialArray(vector &nums)
{
sort(nums.begin(), nums.end());
for (int i = 0; i <= nums.size(); i++)
{
int start = 0;
int end = nums.size() - 1;
while (start <= end)
{
int mid = start + (end - start) / 2;
if (nums[mid] >= i)
{
end = mid - 1;
}
else if (nums[mid] < i)
{
start = mid + 1;
}
}
if (nums.size() - start == i)
{
return i;
}
}
return -1;
}
};
int main()
{
Solution s;
vector nums = {0, 4, 3, 0, 4};
if (s.specialArray(nums) != -1)
cout << s.specialArray(nums) << endl;
else
{
cout << "element not found";
}
return -1;
}

@jaybamroliya
Copy link
Author

Please assign me @abhpd

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant