Block Swap Algorithm or Array Rotation in Java26 Sept 2024 | 6 min read It is very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to discuss block swap algorithm and array rotation in Java with different approaches and logic. Also, we will create Java programs for the same. Array Rotation ExampleInput: int arr[] = {7, 9, 8, 0, 5, 1, 6, 4}, s = 8, d = 2 Output: result[] = {8, 0, 5, 1, 6, 4, 7, 9} Explanation: s is the size of the input array. d = 2 means we have to rotate the array 2 times. When we rotate the first time, we get {9, 8, 0, 5, 1, 6, 4, 7}. Now, we rotate the array a second time, and we get the following. Let's see another example. Input: int arr[] = {6, 8, 7, 9, 0, 5, 1, 3, 2, 4}, s = 10, d = 5 Output: result[] = {5, 1, 2, 3, 4, 6, 8, 7, 9, 0} Explanation: After rotating the array 5 times, we get the following: {5, 1, 2, 3, 4, 6, 8, 7, 9, 0}, which is our answer. Block Swap AlgorithmIn this section, we have used block swap algorithm to rotate array. Observe the algorithm: Step 1: Divide the input array into two sub-arrays with div as division point. Let them be A = arr[0 ... div - 1] and B = arr[div ... n - 1]. Step 2: Until the size of A and B are equal, follow the following steps:
Step 3: When the A and B sizes are equal, swap them. Approach: Using RecursionThe following program uses the above-mentioned algorithm. FileName: RotateArray.java Output: The input array: 7 9 8 0 5 1 6 4 The new array after rotating 2 times, we get 8 0 5 1 6 4 7 9 The input array: 6 8 7 9 0 5 1 3 2 4 The new array after rotating 5 times, we get 5 1 3 2 4 6 8 7 9 0 |
In this program, we will create a singly linked list and delete a node from the middle of the list. To accomplish this task, we will calculate the size of the list and then divide it by 2 to get the mid-point of the list....
5 min read
How do you find duplicate characters in a string? Following program demonstrate it. File: DuplicateCharFinder .java import java.util.HashMap; import java.util.Map; import java.util.Set; public class DuplicateCharFinder { public void findIt(String str) { Map<Character, Integer> baseMap = new HashMap<Character, Integer>(); char[] charArray = str.toCharArray(); for (Character ch : charArray) { if (baseMap.containsKey(ch)) { baseMap.put(ch, baseMap.get(ch) + 1); } else...
1 min read
Program to print the elements of an array present on even position In this program, we need to print the element which is present on even position. Even positioned element can be found by traversing the array and incrementing the value of i by 2. In the...
1 min read
? In Java programming, we often required to generate random numbers while we develop applications. Many applications have the feature to generate numbers randomly, such as to verify the user many applications use the OTP. The best example of random numbers is dice. Because when we throw...
7 min read
It is a problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to solve...
4 min read
Java Program to transpose matrix Converting rows of a matrix into columns and columns of a matrix into row is called transpose of a matrix. Let's see a simple example to transpose a matrix of 3 rows and 3 columns. public class MatrixTransposeExample{ public static void main(String args[]){ //creating a matrix int...
3 min read
In this program, the given ternary tree will be converted into a corresponding doubly linked list. The ternary tree is a hierarchical data structure in which each node can have at most three children. This can be accomplished by traversing the ternary tree in a pre-order...
6 min read
Program to copy all elements of one array into another array In this program, we need to copy all the elements of one array into another. This can be accomplished by looping through the first array and store the elements of the first array into the...
2 min read
In this program, we need to calculate the sum of elements in each row and each column of the given matrix. Above diagram shows the sum of elements of each row and each column of a matrix. Algorithm STEP 1: START STEP 2: DEFINE rows, cols, sumRow, sumCol STEP 3:...
3 min read
In this program, we will create a doubly linked list and delete a node from the beginning of the list. If the list is empty, print the message "List is empty". If the list is not empty, we will make the head to point to...
6 min read
We request you to subscribe our newsletter for upcoming updates.
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India