Data structures and Algorithms Problem Solving #01
Today I started to write about a new topic, Data Structures and Algorithms. This series of stories will ease you to help your technical interviews in future. The problem is everyone has knowledge about different technical subjects, but they have not a problem-solving skill which is definitely checking on an interview nowadays. The only way you have to get in success is by practising these problems day by day. So for that, My next stories will help you.
This set of stories will sacrifice to increase your knowledge of Data structures and Algorithms. Different problems have different solutions in different programming languages. I will well explain a solution in one way. You can suggest any other better solution. However, You also should refresh your knowledge with algorithms everyday even you are in a high-level profession.
I will write the answer in Java. And I will explain what has happened in the code snippet. You can understand the scenario easily and convert it into any language you prefer. The most important thing is understanding the solution and the problem very well. Let’s go to the problem with the difficulty level is easy.
Problem
- A left rotation operation on an array shifts each of the array’s elements 1 unit to the left.
- For example, if 3 left rotations are performed on the array [1,2,3,4,5], then the array would become [4,5,1,2,3].
- Note that the lowest index item moves to the highest index in a rotation. This is called a circular array.
- Given an array of integers and a number, perform left rotations on the array. An Example has given below.
Sample Input:
5 3
[1,2,3,4,5]
Sample Output
[4,5,1,2,3]
Problem Explanation
When we perform d=3 left rotations, the array undergoes the following sequence of changes:
[1,2,3,4,5] -> [2,3,4,5,1] -> [3,4,5,1,2] -> [4,5,1,2,3]
Solution
// Complete the rotateLeft function below.
static int[] rotateLeft(int[] a, int d) {
for(int j=0;j<d;j++){
int first = a[0];
for(int i=0;i<a.length-1;i++){
a[i] = a[i+1];
}
a[a.length-1] = first;
}
return a;
}
Explanation
The method, rotateLeft has the following parameter(s):
- int a[n]: the array to rotate
- int d: the number of rotations
Here I implemented two for loops, the first one is to iterate the number of rotations. the second one is for executing a single rotation. Before start, the single rotation, need to memorize the first element of the array. After starting the single rotation, in every iteration, replace each element with its next element. Then finally the last element replaces with the first element which we stored at the beginning. I will show how the first iteration works below.
Example : First Iteration
input - a[] = [1,2,3,4,5]
first = 1
when i = 0
a[] = [2,2,3,4,5]
when i = 1
a[] = [2,3,3,4,5]
when i = 2
a[] = [2,3,4,4,5]
when i = 3
a[] = [2,3,4,5,5]
Finally - first = a[4]
a[] = [2,3,4,5,1]
This flow will continue until complete the first for loop i.e; the number of rotations.
Conclusion
This story solved one interview question you face as a developer in your next interview. Please feel free to ask any question you will face in the response section below. Also, ask any other related questions you have.
Happy Coding !!!!
Found this post useful? Kindly tap the 👏 button below! :)