You are viewing a single comment's thread. Return to all comments →
Neatest code to get this done!!!
you deserve my respect sir........ AWESOME
MASHA ALLAH...good logic...simple nd awesome...thanx,4 sharing this..
You didn't actually rotated the array, printing the required answer is not the solution. The rotated array might be of some use later in the actual program and the problem is designed to practice the array rotation. Hope you try to understand.
I understand that. But in this question i was required just to print the rotated form so i did the same. If i was to use the array later on, it wouldn't have been tough to declare a new array and start storing in it using a counter variable. So i kept it simple here just to pass all the test cases :)
"A left rotation operation on an array of size n shifts each of the array's elements d unit to the left."
That opening problem statement alone means that you need to shift an existing array; not just print the array in the shifted order.
I do understand d=d%n but in this instance it isn't necessary, question's constraints says 1<d<n
Sometimes coders are extra-careful i guess !
You're over-complicating things with two loops and C arrays in C++.
int n, d;
cin >> n >> d;
vector<int> v((istream_iterator<int>(cin)), istream_iterator<int>());
for (int i = 0; i < n; ++i) cout << v[(i + d) % n] << " ";
Using 2 loops is not overcomplicating. Overcomplicating is your "code". I would be angry for my emloyee if he writes such code like yours. Looking at 2 loops everybody gets in mere second what is going on. Looking at your code one needs to waste much more time figuring out what is going on there.
please explain the logic thanks in advance
Thanks.Its a smart way to solve the question
as neat as it can get! so satisfying!
but this is just printing data as required . if u check array then no rotation had occured