Advertisement
maycod23

two_pointers_questions.cpp

Mar 19th, 2022
1,037
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.04 KB | None | 0 0
  1. //boat competition
  2. #include<bits/stdc++.h>
  3. using namespace std;
  4. // int main()
  5. // {
  6. //  int t;
  7. //  cin >> t;
  8. //  while (t--)
  9. //  {
  10. //      int n, i, j, ans = 0, count = 0, k;
  11. //      cin >> n;
  12. //      int a[n];
  13. //      for (i = 0; i < n; i++)
  14. //      {
  15. //          cin >> a[i];
  16. //      }
  17. //      sort(a, a + n);
  18. //      //two pointers ->on sorted array
  19. //      for (k = 2; k <= 100; k++)
  20. //      {
  21. //          count = 0;
  22. //          i = 0; j = n - 1;
  23. //          while (i < j)
  24. //          {
  25. //              if (a[i] + a[j] == k)
  26. //              {
  27. //                  i++; j--;
  28. //                  count++;
  29. //              }
  30. //              else if (a[i] + a[j] > k)
  31. //              {
  32. //                  j--;
  33. //              }
  34. //              else
  35. //              {
  36. //                  i++;
  37. //              }
  38. //          }
  39. //          if (count > ans)
  40. //          {
  41. //              ans = count;
  42. //          }
  43. //      }
  44. //      cout << ans << " " << endl;
  45. //  }
  46. //  return 0;
  47. // }
  48.  
  49.  
  50.  
  51. /////problem->heaters
  52. // class Solution {
  53. // public:
  54.  
  55. //     bool can(vector<int>& houses,int radius,vector<int>& heaters)
  56. //     {
  57. //        int n=houses.size();
  58. //        int m=heaters.size();
  59. //        int l=0,r=0;
  60. //        while(l<n&&r<m)
  61. //        {
  62. //            if(abs(houses[l]-heaters[r])<=radius)
  63. //            {
  64. //                l++;
  65. //            }
  66. //            else
  67. //            {
  68. //                r++;
  69. //            }
  70. //        }
  71. //        if(l==n) return true;//all houses can be covered
  72. //        return false;
  73. //     }
  74. //     int findRadius(vector<int>& houses, vector<int>& heaters)
  75. //     {
  76. //        sort(houses.begin(), houses.end());
  77. //     sort(heaters.begin(), heaters.end());
  78. //        int low=0,high=(int)1e9+1,ans;
  79. //        while(low<=high)
  80. //        {
  81. //            int curr_radius=(low+(high-low)/2);
  82. //            if(can(houses,curr_radius,heaters))
  83. //            {
  84. //                //all houses can be covered
  85. //               ans=curr_radius;
  86. //               high=curr_radius-1;
  87. //            }
  88. //            else
  89. //            {
  90. //               low=curr_radius+1;
  91. //            }
  92. //        }
  93. //         //min curr radius by which all houses should be covered
  94. //         return ans;
  95. //     }
  96. // };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement