✅Easy C++ Solution | 2 Approaches | Intuition & Approach
Approach 1\n# Intuition\n Describe your first thoughts on how to solve this problem. \nThe question is about flipping the random index which is done using rand(
## Approach 1\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nThe question is about flipping the random index which is done using rand() function. It is highly recommended to do the following question as pre-requisite : https://leetcode.com/problems/random-pick-index/\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nrand() function will return random value from [0,RAND_MAX] which we will adjust in our range using the modulo.\n\nIf tackle any repeated value from rand(), we will use a set and if we have found it earlier we will generate a new value. \n\nWe will generate random value for row and column seperately.\n\nSee the code for implementation\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nSolution() -> O(1)\nflip() -> O(m*n) in worst case\nreset() -> O(m*n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(m*n)\n\n# Code\n```\nclass Solution {\n    int m,n;\n    set<pair<int,int>>s;\npublic:\n    Solution(int m, int n) {\n        this -> m = m;\n        this -> n = n;\n        s.clear();\n    }\n    \n    vector<int> flip() {\n        int r = rand()%m;\n        int c = rand()%n;\n\n        while(s.count({r,c}) > 0){\n            r = rand()%m;\n            c = rand()%n;\n        }\n\n        s.insert({r,c});\n        return {r,c};\n    }\n    \n    void reset() {\n        s.clear();\n    }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(m, n);\n * vector<int> param_1 = obj->flip();\n * obj->reset();\n */\n```\n\n## Approach 2\n# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nHere random values can be gererated equally if we traverse row wise or column wise. Here I have done it using row wise traversal.\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nIntialise i = 0, j = 0.\nNow when flip is called move in the next column in the same row. However, if we reached out of the column, move to next row, and set column to 0. Similarly, if we reach out of row, then set row to 0.\n\n\nSince we have adjusted the iterators in flip function only no need to alter reset function.\nReason? : If we reset the iterators to 0,0 we are defeating the purpose of rand() function from which we were generating indexes with equal probability. So after reset is called we will not alter the iterators so probability will remain eqally likeable.\n\nSee the code for implementation\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nSolution() -> O(1)\nflip() -> O(1) \nreset() -> O(1)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\nO(m*n)\n\n# Code\n```\nclass Solution {\n    int i,j;\n    int m,n;\npublic:\n    Solution(int m, int n) {\n        i = 0;\n        j = 0;\n        this -> m = m;\n        this -> n = n;\n    }\n    \n    vector<int> flip() {\n        vector<int> ans =  {i,j};\n        j = (j + 1);\n        if(j == n){\n            j = 0;\n            i += 1;\n            if(i == m){\n                i = 0;\n            }\n        }\n        return ans;\n    }\n    \n    void reset() {\n        \n    }\n};\n\n/**\n * Your Solution object will be instantiated and called as such:\n * Solution* obj = new Solution(m, n);\n * vector<int> param_1 = obj->flip();\n * obj->reset();\n */\n```
['Math', 'Ordered Set', 'Randomized', 'C++']