Java merge same numbers via multiplication in an array with Recursive -


i wanna write method merges same values within array via multiplication. wanna recursive. sequence of numbers should merged, if there similar numbers withing it. example if have numbers 1, 2, 5, 5, 4, should become "1 2 25 4" or 5, 5, 5, 6 becomes ”125 6”.

can help?

this approach utilizes divide , conquer, , should run in o(n log n).

package edu.gwu.seas.cs6212.hw3;  import java.util.arraylist; import java.util.list; import java.util.map; import java.util.iterator; import java.util.map.entry; import java.util.treemap;  public class tester {     public static void main(string [] args){         system.out.println("merging 1, 2, 5, 5, 4: ");         int [] originalarray = {1, 2, 5, 5, 4};         int [] mergedarray = mergesequences(originalarray);         stringbuilder sb = new stringbuilder();         for(int element : mergedarray){             sb.append(element);             sb.append(",");         }         sb.deletecharat(sb.length()-1); //remove final comma         system.out.println(sb.tostring());         sb.delete(0, sb.length());         system.out.println("merging 5, 5, 5, 6: ");         int [] originalarray2 = {5, 5, 5, 6};         int [] mergedarray2 = mergesequences(originalarray2);         for(int element : mergedarray2){             sb.append(element);             sb.append(",");         }         sb.deletecharat(sb.length()-1); //remove final comma         system.out.println(sb.tostring());     }      private static int [] mergesequences(int [] originalarray){          map<integer,integer> indiciesmap = mergedivideandconquer(originalarray, 0, originalarray.length -1, new treemap<integer,integer>());         int indexcounter = 0;         list<integer> mergedlist = new arraylist<integer>();         iterator<entry<integer, integer>> = indiciesmap.entryset().iterator();         if(it.hasnext()){             while(it.hasnext()){                 entry<integer,integer> firstentry = it.next();                 int firstsequencebeginindex = firstentry.getkey();                 int firstsequenceendindex = firstentry.getvalue();                 while(indexcounter < firstsequencebeginindex){                     mergedlist.add(originalarray[indexcounter]);                     indexcounter++;                 }                 //now we've reached first entry                 int multiplicativesum = 1;                 while(indexcounter <= firstsequenceendindex){                     multiplicativesum *= originalarray[indexcounter];                     indexcounter++;                 }                 mergedlist.add(multiplicativesum);             }             //add remaining elements             while(indexcounter < originalarray.length){                 mergedlist.add(originalarray[indexcounter++]);             }          } else{             for(int element : originalarray){                 mergedlist.add(element);             }         }         return mergedlist.stream().maptoint(i -> i).toarray();     }      private static map<integer,integer> findcrossingarray(final int [] originalarray, final int i, final int midpoint,              final int j, map<integer,integer> indiciesmap){         int leftindexofsequence = -1;         for(int leftcounter = midpoint; leftcounter >= i; leftcounter--){             if(originalarray[leftcounter] == originalarray[midpoint]){                 leftindexofsequence = leftcounter;             } else{                 break;             }         }         int rightindexofsequence = midpoint;         for(int rightcounter = midpoint + 1; rightcounter <= j; rightcounter++){             if(originalarray[rightcounter] == originalarray[midpoint]){                 rightindexofsequence = rightcounter;             } else{                 break;             }         }         if(leftindexofsequence != -1 && leftindexofsequence != rightindexofsequence){             indiciesmap.put(leftindexofsequence, rightindexofsequence);         }         return indiciesmap;     }      private static map<integer,integer> mergedivideandconquer(final int [] originalarray, final int i, final int j, map<integer,integer> indiciesmap){         //base case         if(j == i){             return indiciesmap;         }         //divide recursively smaller subproblems         int midpoint = math.floordiv((i + j),2);         map<integer,integer> left = mergedivideandconquer(originalarray, i, midpoint, indiciesmap);         map<integer,integer> right = mergedivideandconquer(originalarray, midpoint + 1, j, indiciesmap);         //combine subsolutions         return findcrossingarray(originalarray, i, midpoint, j, indiciesmap);      } } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -