HERE I SOLVE /PRACTICE PROGRAMMING EXERCISES FROM VARIOUS sources ENJOY!
full coding bat, without debug prints
public int maxMirror(int[] nums) {
return compute(nums) ;
}
public int[] getReverse(int a[]){
int len = a.length ;
int res[] = new int[len] ;
for(int j = 0 , i = a.length-1; i >= 0 ; i--,j++)
res[j] = a[i] ;
return res ;
}
public int compute(int a[]){
if(a.length == 0) return 0 ;
if (a.length == 1) return 1 ;
int reverse[] = getReverse(a) ;
int maxspan = 1 ;
if(a.length == compareArrays(reverse,a)){
return a.length ;}
for(int sub_size = 2 ; sub_size < a.length;sub_size++ ){
int currentspan = 0 ;
for(int from = 0 ; from <= a.length - sub_size; from++){
int rev_sub[] = makeArray(reverse, from , sub_size) ;
for(int from2 = 0 ; from2 <= a.length - sub_size ; from2++){
int a_sub[] = makeArray(a, from2,sub_size) ;
currentspan = compareArrays(rev_sub,a_sub) ;
if(currentspan > maxspan){
maxspan = currentspan ;}
}
}
}
return maxspan ;
}
public int[] makeArray(int a[], int from, int n){
int len = n ;
int result[] = new int[len] ;
int count = 0 ;
for(int i = from;from <= a.length-n && i < from + n ;i++){
result[count] = a[i] ;
count++ ;
}
return result ;
}
public int compareArrays(int array1[], int array2[]){
for(int i = 0 ; i < array1.length; i++){
if(array1[i] != array2[i]) return 0 ;
}
return array1.length ;
}
Posted by
erjan
max mirror coding bat problem
public class endOtherClass{
public static void main(String args[]){
int test2[] = {1, 2, 7, 8, 1, 7, 2} ;
int test3[] = {1, 2, 1,4} ;
int test4[] ={5, 9, 1, 6, 5, 4, 1, 9, 5} ;
compute(test4) ;
}
public static int[] getReverse(int a[]){
int len = a.length ;
int res[] = new int[len] ;
for(int j = 0 , i = a.length-1; i >= 0 ; i--,j++)
res[j] = a[i] ;
return res ;
}
public static int compute(int a[]){
if(a.length == 0) return 0 ;
if (a.length == 1) return 1 ;
int reverse[] = getReverse(a) ;
int maxspan = 1 ;
if(a.length == compareArrays(reverse,a)){System.out.println("whole array is a mirror!") ; return a.length ;}
for(int sub_size = 2 ; sub_size < a.length;sub_size++ ){
System.out.println("TESTING ALL to ALL of size " + sub_size) ;
int currentspan = 0 ;
for(int from = 0 ; from <= a.length - sub_size; from++){
int rev_sub[] = makeArray(reverse, from , sub_size) ;
System.out.print("new rev_sub compared to all : ") ;
printArray(rev_sub) ;
for(int from2 = 0 ; from2 <= a.length - sub_size ; from2++){
int a_sub[] = makeArray(a, from2,sub_size) ;
System.out.println("\n&&&&&&&&&&&&&&&&&&&&& COMPARING &&&&&&&&&&&&&&&&&&");
currentspan = compareArrays(rev_sub,a_sub) ;
printArray(rev_sub) ;
System.out.print(" to :") ;
printArray(a_sub) ;
System.out.println() ;
if(currentspan > maxspan){ System.out.println("max span got updated!") ; maxspan = currentspan ;}
}
System.out.println() ;
}
System.out.println("\n\n^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^") ;
}
System.out.println("DONE! - max span is " + maxspan) ;
return maxspan ;
}
public static int[] makeArray(int a[], int from, int n){
int len = n ;
int result[] = new int[len] ;
int count = 0 ;
//System.out.println(" start from " + from) ;
for(int i = from;from <= a.length-n && i < from + n ;i++){
//System.out.println("inside make array ,i: " + i) ;
result[count] = a[i] ;
count++ ;
}
return result ;
}
public static int compareArrays(int array1[], int array2[]){
for(int i = 0 ; i < array1.length; i++){
if(array1[i] != array2[i]) return 0 ;
}
return array1.length ;
}
public static void printArray(int []a){
System.out.print("[") ;
for(int i = 0 ; i < a.length;i++)
System.out.print(" " + a[i]) ;
System.out.print(" ]") ;
}
}
Posted by
erjan
Subscribe to:
Comments (Atom)
