C++ HackerRank|AND xor OR

  1. Dashboard 
  2.  Data Structures 
  3.  Stacks 
  4.  AND xor OR
    1. #include <iostream>
      #include <stack>
      #include <vector>
      using namespace std;
      
      int main(int argc, char const *argv[])
      {
          int n, max = 0, temp;
          cin >> n;
          vector<int> arr(n);
          stack<int> index;
          for (int i = 0; i < n; ++ i)
          {
              cin >> arr[i];
              while (! index.empty())
              {
                  temp = arr[i] ^ arr[index.top()]; // (((a & b) ^ (a | b)) & (a ^ b)) == a ^ b
                  max = max > temp ? max : temp;
                  if (arr[index.top()] > arr[i])
                  {
                      index.pop();
                  }
                  else
                  {
                      break;
                  }
              }
              index.push(i);
          }
          cout << max << endl;
      
          return 0;
      }


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章