Skip to main content

Data Structures - Arrays

1) Move all Negative numbers to the beginning of the Array
Move all the Negative elements to the beginning of the given Array. The solution is to take two pointers slowPointer, fastPointer. SlowPointer starting at position 0 and fastPointer starting at position 1. The required condition is to have all negatives at the beginning, so keep slowPointer at beginning as long as the element is positive and move it only incase the element is negative. For the fastPointer move it as long as the number is negative and stop when the number is positive and do a comparison with the slowPointer, if slowPointer is positive and fastPointer is negative swap the elements at both the positions and move both the pointers. When the Iteration is completed all the negative elements get moved to the beginning and all the positive elements to the end.
private static void moveNegatives(int[]nums) {
if(nums==null || nums.length==0) {
return;
}
int i = 0;
int j = 1;
while(i<nums.length-1 && j<nums.length-1) {
if(nums[j]<0 && nums[i]>=0) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
i++;
j++;
} else if(nums[i]<0) {
i++;
} else if(nums[j]>0) {
j++;
}
}
}

Rotate an Image represented as a 2D matrix in place To rotate an image represented as a 2D Matrix in place we first need to transpose the matrix(move row to column and column to row), after the transpose is completed then reverse the positions of elements for each row which makes the complete 2D array rotation by 90 degrees.
class Solution {
public void rotate(int[][] matrix) {
if(matrix==null || matrix.length==0) {
return;
}
int n = matrix.length;
for(int i=0;i<n;i++) {
for(int j=i;j<n;j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i=0;i<n;i++) {
for(int j=0;j<n/2;j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[i][n-1-j];
matrix[i][n-1-j] = temp;
}
}
}
}

Comments

Popular posts from this blog

Running Multiple Operating Systems(Windows and Ubuntu Linux) on the same machine

VMWare Player is a freely downloadable VMWare. Download VMWare player software and install it on your windows OS download an image of the Ubuntu Linux Desktop version called Ubuntu from http://www.ubuntu.com/getubuntu/download that in iso image format. Then download VMWare configuration bundle that contains a list of files, extract those file to some folder like C:\OS\. Then edit the file" os.vmx file and give the path of the .iso image in that file in the line like below. ide1:0.fileName = C:\OS\ubuntu-8.10-desktop-i386.iso" Now open the file os.vmx file using the vmware player, that will open the Ubuntu OS. You will get a list of options in that select the option install Ubuntu without changing your current configuration of the system Now that will start the Ubuntu OS in a window inside your windows OS. Now you have a browser and all the applications inside the Ubuntu OS, you can start working on that. Double click on this window/expand it to show in full screen. To switch ...