fork download
  1. class Solution {
  2. public:
  3.  
  4. int OR(int i, int j, vector<vector<int>> &prefix)
  5. {
  6. int k = 0;
  7.  
  8. for (int u = 0; u <= 30; u++)
  9. {
  10. int one;
  11. if (i == 0)
  12. one = prefix[u][j];
  13. else
  14. one = prefix[u][j] - prefix[u][i - 1];
  15.  
  16. if (one >= 1)
  17. {
  18. k = k + (1 << u);
  19. }
  20. }
  21.  
  22. return k;
  23. }
  24.  
  25. int minimumSubarrayLength(vector<int>& nums, int k)
  26. {
  27. int n = nums.size();
  28.  
  29. vector<vector<int>> bit(31, vector<int>(n));
  30.  
  31. for (int j = 30; j >= 0; j--)
  32. {
  33. for (int i = 0; i < n; i++)
  34. {
  35. bit[j][i] = (nums[i] >> j) & 1;
  36. }
  37. }
  38.  
  39. vector<vector<int>> prefix(31, vector<int>(n));
  40.  
  41. for (int j = 30; j >= 0; j--)
  42. {
  43. for (int i = 0; i < n; i++)
  44. {
  45. if (i == 0)
  46. prefix[j][i] = bit[j][i];
  47. else
  48. prefix[j][i] = prefix[j][i - 1] + bit[j][i];
  49. }
  50. }
  51.  
  52. // from here the exact previous Concept Building optimization code of two pointer template is added
  53. // one thing above whole condition bitwise OR condition is added extra with respect to question the two pointer template is exact it is
  54.  
  55. // 'i' pointer we rename the variable it as left
  56. // 'j' pointer we rename the variable it as right
  57.  
  58. int minLength = INT_MAX;
  59. int windowSum = 0;
  60. int left = 0;
  61.  
  62. for (int right = 0; right < n; ++right)
  63. {
  64. windowSum = OR(left, right, prefix);
  65.  
  66. while (windowSum >= k && left <= right)
  67. {
  68. // in the whole algorithm this loop will only go from left = 0 till left = n-1
  69. // so only total O(N) extra operations.
  70.  
  71. minLength = min(minLength, right - left + 1);
  72.  
  73. windowSum = OR(left + 1, right, prefix);
  74. ++left;
  75. }
  76.  
  77. if (left <= right)
  78. {
  79.  
  80. }
  81. else
  82. {
  83. right = left;
  84. }
  85. }
  86.  
  87. if (minLength == INT_MAX)
  88. {
  89. return -1;
  90. }
  91.  
  92. return minLength;
  93. }
  94. };
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.cpp:4:26: error: ‘vector’ has not been declared
     int OR(int i, int j, vector<vector<int>> &prefix)
                          ^~~~~~
prog.cpp:4:32: error: expected ‘,’ or ‘...’ before ‘<’ token
     int OR(int i, int j, vector<vector<int>> &prefix)
                                ^
prog.cpp:25:31: error: ‘vector’ has not been declared
     int minimumSubarrayLength(vector<int>& nums, int k)
                               ^~~~~~
prog.cpp:25:37: error: expected ‘,’ or ‘...’ before ‘<’ token
     int minimumSubarrayLength(vector<int>& nums, int k)
                                     ^
prog.cpp: In member function ‘int Solution::OR(int, int, int)’:
prog.cpp:12:23: error: ‘prefix’ was not declared in this scope
                 one = prefix[u][j];
                       ^~~~~~
prog.cpp:14:23: error: ‘prefix’ was not declared in this scope
                 one = prefix[u][j] - prefix[u][i - 1];
                       ^~~~~~
prog.cpp: In member function ‘int Solution::minimumSubarrayLength(int)’:
prog.cpp:27:17: error: ‘nums’ was not declared in this scope
         int n = nums.size();
                 ^~~~
prog.cpp:27:17: note: suggested alternative: ‘enum’
         int n = nums.size();
                 ^~~~
                 enum
prog.cpp:29:9: error: ‘vector’ was not declared in this scope
         vector<vector<int>> bit(31, vector<int>(n));
         ^~~~~~
prog.cpp:29:23: error: expected primary-expression before ‘int’
         vector<vector<int>> bit(31, vector<int>(n));
                       ^~~
prog.cpp:35:17: error: ‘bit’ was not declared in this scope
                 bit[j][i] = (nums[i] >> j) & 1;
                 ^~~
prog.cpp:39:23: error: expected primary-expression before ‘int’
         vector<vector<int>> prefix(31, vector<int>(n));
                       ^~~
prog.cpp:46:21: error: ‘prefix’ was not declared in this scope
                     prefix[j][i] = bit[j][i];
                     ^~~~~~
prog.cpp:46:36: error: ‘bit’ was not declared in this scope
                     prefix[j][i] = bit[j][i];
                                    ^~~
prog.cpp:48:21: error: ‘prefix’ was not declared in this scope
                     prefix[j][i] = prefix[j][i - 1] + bit[j][i];
                     ^~~~~~
prog.cpp:48:55: error: ‘bit’ was not declared in this scope
                     prefix[j][i] = prefix[j][i - 1] + bit[j][i];
                                                       ^~~
prog.cpp:58:25: error: ‘INT_MAX’ was not declared in this scope
         int minLength = INT_MAX;
                         ^~~~~~~
prog.cpp:58:25: note: ‘INT_MAX’ is defined in header ‘<climits>’; did you forget to ‘#include <climits>’?
prog.cpp:1:1:
+#include <climits>
 class Solution {
prog.cpp:58:25:
         int minLength = INT_MAX;
                         ^~~~~~~
prog.cpp:64:41: error: ‘prefix’ was not declared in this scope
             windowSum = OR(left, right, prefix);
                                         ^~~~~~
prog.cpp:66:33: error: ‘k’ was not declared in this scope
             while (windowSum >= k && left <= right)
                                 ^
prog.cpp:71:29: error: ‘min’ was not declared in this scope
                 minLength = min(minLength, right - left + 1);
                             ^~~
stdout
Standard output is empty