Solved

rotate array rotateLeft3

Posted on 2015-02-12
13
98 Views
Last Modified: 2015-02-17
Hi,

I am working  below challenge

http://codingbat.com/prob/p185139

public int[] rotateLeft3(int[] nums) {
  int num1=nums[0];
  int num2=nums[1];
  int num3=nums[2]
  
  
}

Open in new window


i kind of started but not sure on the approach to follow. Please advise
0
Comment
Question by:gudii9
  • 5
  • 4
  • 3
  • +1
13 Comments
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 40606595
int array[]={nums[1],nums[2],nums[0]};
  return array;
0
 
LVL 7

Author Comment

by:gudii9
ID: 40606598
rotate left means what. Is it is not same as reversing?
i thought it shoud be like

int array[]={nums[2],nums[1],nums[0]};
  return array;

please advise
0
 
LVL 84

Expert Comment

by:ozo
ID: 40606613
The word rotate is being used in this sense: http://en.wikipedia.org/wiki/Circular_shift#Example
0
 
LVL 26

Accepted Solution

by:
dpearson earned 200 total points
ID: 40606742
Looks closely at the first example:
rotateLeft3({1, 2, 3}) → {2, 3, 1}

to see how the numbers move.  Each goes one left and the first number goes to the end.

The solution ozo posted is perfectly fine here.  But if you want to think about this problem a bit more you might want to consider a solution like this which works for any length array (>= 1):

public int[] rotateLeft3(int[] nums) {
  int len = nums.length ;
  int first = nums[0] ;
  for (int i = 0 ; i < len-1 ; i++) {
    nums[i] = nums[i+1] ;
  }
  nums[len-1] = first ;
  return nums ;
}

Open in new window


Can you see how it moves everything left and then moves the first value to the end?

Doug
0
 
LVL 3

Assisted Solution

by:Nadia Sobnom
Nadia Sobnom earned 100 total points
ID: 40607319
The following solution does this without any extra variables :)
public int[] rotateLeft3(int[] nums) {
  nums[0]^=nums[1];
  nums[1]^=nums[0];
  nums[0]^=nums[1];
  nums[1]^=nums[2];
  nums[2]^=nums[1];
  nums[1]^=nums[2];
  return nums;
}

Open in new window


Let me try to explain how it works.
We can rotate the whole array by two steps
1. swap nums[0] and nums[1]
2. swap nums[1] and nums[2]

If you can see the XOR swap algorithm, two numbers a and b can be swapped by the following
a ^= b;
b ^= a;
a ^= b;

Open in new window

That is exactly what I've done in first three lines to swap first two elements, similarly last three lines are swapping last two elements.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40608085
nums[0]^=nums[1];
  nums[1]^=nums[0];
  nums[0]^=nums[1];
  nums[1]^=nums[2];
  nums[2]^=nums[1];
  nums[1]^=nums[2];

Open in new window


can you please elaborate on this.

How ^ works. I have not used that much earlier.

Please advise

public int[] rotateLeft3(int[] nums) {
  int len = nums.length ;
  int first = nums[0] ;
  for (int i = 0 ; i < len-1 ; i++) {
    nums[i] = nums[i+1] ;
  }
  nums[len-1] = first ;
  return nums ;
}

Open in new window

above also passed all tests
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 84

Expert Comment

by:ozo
ID: 40608105
0
 
LVL 7

Author Comment

by:gudii9
ID: 40608152
basicaly it is circular movement(first is kept at last and other than placed at index subtracted by 1 to orignial) right?
0
 
LVL 84

Expert Comment

by:ozo
ID: 40609452
Yes, assuming that indexes are numbered left to right.
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40611208
The XOR (^) solution is clever - but not one you should be focusing on when learning the basics of the language.  You could go for many many years as a professional Java developer and never see that pattern again.

You should focus on loops, variable assignments etc. over the behavior of binary operators like XOR.  I think Nadia was just having some fun and pointing out there are many possible solutions here.

Did you figure out how this works and why it would work for rotating arrays of any length >= 1?

public int[] rotateLeft3(int[] nums) {
  int len = nums.length ;
  int first = nums[0] ;
  for (int i = 0 ; i < len-1 ; i++) {
    nums[i] = nums[i+1] ;
  }
  nums[len-1] = first ;
  return nums ;
}

Open in new window


Doug
0
 
LVL 84

Expert Comment

by:ozo
ID: 40611290
http://en.wikipedia.org/wiki/XOR_swap_algorithm#Reasons_for_avoidance_in_practice explains several reasons not to use the xor solution.
An additional reason in this application is that it uses 3*(n-1) operations instead of n+1 operations using a temporary.
0
 
LVL 7

Author Comment

by:gudii9
ID: 40612864
Did you figure out how this works and why it would work for rotating arrays of any length >= 1?

yes I think  it is circular movement(first is kept at last and other than placed at index subtracted by 1 to orignial) right

public int[] rotateLeft3(int[] nums) {
  int len = nums.length ;//find lengh of nums array
  int first = nums[0] ;assign fist element of arrya to first int variable
  for (int i = 0 ; i < len-1 ; i++) {//loop through till the end of the array lenth
    nums[i] = nums[i+1] ;//assign right hand side element to left hand side element except first i think??
  }
  nums[len-1] = first ;//assign first to last elemt now
  return nums ; return the nums array which is modified
}

Open in new window


Please correct me in case if i made some mistake
0
 
LVL 26

Expert Comment

by:dpearson
ID: 40613302
Yep that's it exactly.

For these problems it's often good to solve the specific challenge they give you and also think about the wider set of similar problems.

Sounds like you've mastered this one.

Doug
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
network + 7 73
How Complex Is This Java Course ? 9 63
wordlen challenge 3 49
JDeveloper 12c for 32 bit 4 36
Navigation is an important part of web design from a usability perspective. But it is often a pain when it comes to a developer’s perspective. By navigation, it often means menuing. This is less theory and more practical of how to get a specific gro…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now