My code is giving correct output for case [1,2], 1 when i compiling in my ide.. but showing wrong answer for this case?


  • 1
    P
       int tmp[1000001];
        void rotate(vector<int> nums, int k) {
        
            int n = nums.size();
            k = k%n;
    
    		for (int i = 0; i < n - k; i++) {
    			tmp[i] = nums[i];
    			//	cout << tmp[i];
    		}
    		int p = n - k;
    		for (int i = n - k; i < n; i++) {
    			nums[i - p] = nums[i];
    			//cout << i-p<<"----"<<nums[i - p]<<endl;
    		}
    		
    		int j = 0;
    		for (int i = n - 1 - p + 1; i < n; i++) {
    			nums[i] = tmp[j++];
    		}
    
    		for (int i = 0; i < n; i++) {
    			cout << nums[i] << " ";
    		}
    		cout << endl;
    	
    	return;
        }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.