jayati

Kth Missing Positive Number

Mar 6th, 2023
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.59 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     int findKthPositive(vector<int>& arr, int k) {
  4.         int n = *max_element(arr.begin(),arr.end());
  5.         vector<int> v(n+k+1,0);
  6.         for(int i=0;i<arr.size();i++)
  7.         {
  8.             v[arr[i]]=1;
  9.         }
  10.         int j=1;
  11.         int i=0;
  12.         int ans=0;
  13.         while(j<n+k+1)
  14.         {
  15.            if(v[j]==0)
  16.            {
  17.                i++;
  18.                if(i==k)
  19.                {
  20.                    ans = j;
  21.                    break;
  22.                }
  23.            }
  24.            j++;
  25.         }
  26.        return ans;
  27.        
  28.     }
  29. };
Advertisement
Add Comment
Please, Sign In to add comment