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.

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.

Comments

Popular posts from this blog

Design Patterns using Java

According to Gang of Four(GOF) any software is classified in to one of the three categories. I read so many books about design patterns which provide a lot of information about Design Patterns in a language neutral way or related to a particular programming language. I am trying to complement the great books by providing the precise and concise information that is required in the day to day programming of a Java Developer. Any software can be classified into one of the three categories -Framework, Toolkit, Application. Framework - Framework defines a set of steps to create an application. Framework provides design reuse. Toolkit - Toolkit provides some utility functions to an existing application. Toolkit provides code reuse. Application - Application is some thing that is specific to the project, and is not useful outside the context of the current application. Gang of Four divided the Design Patterns in to 3 types based on their usage. There are 3 types of Gang of Fo