Harry
BAN USERLike new challenges always
below performing cycling trough code and carrying the value to next movement. exteral for loop call can be reduced.
int a[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12},{13,14,15,16}};
int matrixSize=4;
public void cyclicRotate(){
for(int i=0;i<matrixSize;i++){
System.out.println();
for(int j=0;j<matrixSize;j++)
System.out.print(" "+a[j][i]);
}
for(int i =0 ; i<matrixSize/2;i++)
Rotate(i,i,-1,true,true);
for(int i=0;i<matrixSize;i++){
System.out.println();
for(int j=0;j<matrixSize;j++)
System.out.print(" "+a[j][i]);
}
}
private void Rotate(int x,int y,int value,boolean Moveforward,boolean horizontalMove){
if(value==-1)
value=a[x][y];
if(horizontalMove)
{
if(Moveforward){
for(int i=x;i<(matrixSize-x-1);i++){
value=swap(value, i+1, y);
}
Rotate(matrixSize-x-1,y,value,true,false);
}
else{
for(int i=x;i>(matrixSize-x-1);i--){
value=swap(value, i-1, y);
}
Rotate(matrixSize-x-1,y,value,false,false);
}
}
else{ // for vertical move Moveforward means move Down here{
if(Moveforward){
for(int i=y;i<matrixSize-y-1;i++){
value=swap(value, x, i+1);
}
Rotate(x,matrixSize-y-1,value,false,true);
}
else{
for(int i=y;i>(matrixSize-y-1);i--){
value=swap(value, x,i-1 );
}
//Rotate(x,matrixSize-y-1,value,false,true);
}
}
}
private int swap(int fromValue, int toX , int toY){
int temp = a[toX][toY];
a[toX][toY]=fromValue;
return temp;
}
we can override the equals method of node and add extra param to check existing hashcode and objection location and next object location matches.. example..
public class Node {
public int value;
public Node(int i){
this.value=i;
}
public Node nextNode;
public boolean equals(Node obj) {
return (super.equals(obj)&& obj.nextNode==nextNode);
}
public class SearchingElement {
public static void main(String[] args){
Node n = new Node(3);
Node n1= new Node(4);
n.nextNode=n1;
Node n2 = new Node(5);
n1.nextNode=n2;
Node n3 = new Node(3);
n2.nextNode=n3;
Node n4 = new Node(6);
n3.nextNode=n4;
Node n5 = new Node(7);
n4.nextNode=n5;
n5.nextNode= n;
Node temp=n;
do{
System.out.println(" "+temp.value);
temp=temp.nextNode;
}while(!temp.equals(n));
}
- Harry January 07, 2016import java.util.LinkedHashMap;
import java.util.Map;
public class SearchingElement {
public static void main(String[] args){
LinkedHashMap<Integer,Integer> lh = new LinkedHashMap<Integer, Integer>();
int[] a ={1,2,3,4,5,6,2,3,2,4,3,2,3};
for(int element:a){
if(lh.get(element)==null){
lh.put(element,1);
}
else
lh.put(element,lh.get(element)+1);
}
//for (Map.Entry<Integer,Integer> e: lh.entrySet()){
for (Map.Entry<Integer, Integer> entry : lh.entrySet()){
if(entry.getValue()%2==0)
System.out.println(entry.getKey());
}
}
}
- Harry January 07, 2016space complexity -- O(2) max in case no palindrome
timecomplexity -- O(2n) maz in case no palindrome
correct me if m wrong above...
public class Palindrome {
//char[] a={'r','a','b','c','p','p','q','x','q','p'};
StringBuffer sb = new StringBuffer("harry");
public static void main(String[] args){
Palindrome pl = new Palindrome();
pl.getPalindrom();
}
public void getPalindrom(){
int pivot =searchPalindrome(sb.length()-1, 0, 0);
for(int i=pivot-1;i>=0;i--)
sb.append(sb.charAt(i));
System.out.println(sb);
}
public int searchPalindrome(int endLoc, int startLoc,int startPivot){
if(sb.charAt(endLoc)==sb.charAt(startLoc)){
if(endLoc==startLoc)
return startPivot;
else
return searchPalindrome(endLoc-1,startLoc+1,startPivot);
}
else{
if(startLoc!=(sb.length()-1))
return searchPalindrome(sb.length()-1,startPivot+1,startPivot+1);
else
return sb.length()-1;
}
}
}
solution with O(n) time and O(1) space complexity.. let me know if m not correct..
public class ArithmaticProg {
public static void main(String[] args){
int[] a={2,5,8,14};
ArithmaticProg ap = new ArithmaticProg();
System.out.println("--"+ap.getMissingElement(a));
}
public int getMissingElement(int[] a){
int missingElement=0,compare;
for (int i=1 ;i<a.length-1 ;i++){
compare = Integer.compare((a[i]-a[i-1]),(a[i+1]-a[i]));
if(compare!=0){
if(compare==1)
missingElement=(a[i]+a[i-1])/2;
else
missingElement=(a[i+1]+a[i])/2;
return missingElement;
}
}
return missingElement;
}
}
not using indexof function which indirectly iterates over the array for the no. of time it is being used... so guess time complexity will be less for below code
- Harry August 06, 2016