arrays - Decrease run time of this java program -
i have made program input array, , find product of 3 largest numbers of array such that:
the array constitutes of sub-arrays consisting of index increased 1 @ time.
that is, array of 10 elements, find product considering first 3 elements, first 4 elements, first 5 elements , on.
here's code:
import java.io.*; import java.util.arrays; public class monkmulti { public static void main(string args[] ) throws ioexception { bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); int n = integer.parseint(br.readline()); // no. of elements string xs = br.readline(); //accepting elements in string xs=xs+" "; if(n<1||n>100000) //constraint system.exit(0); int i,temp,count=0; for(i=0;i<xs.length();i++) { if(xs.charat(i)==' ') { count++; } } if(count!=n) //checks if no. of elements equal n system.exit(0); int[] x=new int[count]; int k=0; temp=0; for(i=0;i<xs.length();i++) { if(xs.charat(i)==' ') { x[k++]=integer.parseint(xs.substring(temp, i)); temp=i+1; } } count=0; int len=x.length,j; int[] x1=new int[len]; system.arraycopy(x, 0, x1, 0, len); for(i=0;i<len;i++) { if(x[i]<1||x[i]>100000) //constraint each element system.exit(0); } int m1=0,m2=0,m3=0; int max1=x[0],max2=x[0],max3=x[0]; /*code improved below here*/ for(i=2;i<len;i++) { for(j=0;j<=i;j++) { for(k=0;k<i;k++) { if(x1[k]>x1[k+1]) { temp=x1[k]; x1[k]=x1[k+1]; x1[k+1]=temp; } } } system.out.println(x1[i]*x1[i-1]*x1[i-2]); } } }
input:
n=user inputs no. of elements in array
xs= string accepting n elements separated space
output:
product considering first 3 elements
product considering first 4 elements
on..
example:
input:
5
6 2 9 3 8
output:
-1
-1
108
162
432
explanation there 5 integers 6,2,9,3,8
third index, top 3 numbers 6,2 , 9 product 108.
fourth index, top 3 numbers 6,9 , 3 product 162.
fifth index, top 3 numbers 6,9 , 8 product 432.
if understand, need this:
the order o(items * log(subgroups)) -> o(items * log(3)) -> o(items)
public static void product(int[] items, int subgroup) { if (subgroup > 0 && items.length >= subgroup) { // save largest numbers: priorityqueue<integer> maxelements = new priorityqueue<>(subgroup); int product = 1; (int = 0; < items.length; i++) { // queue of largest numbers full: if (maxelements.size() == subgroup) { // minimum of previous largest number lower new number if (maxelements.peek() < items[i]) { // swapping product /= maxelements.poll(); product *= items[i]; maxelements.add(items[i]); } system.out.println(product); } else { product *= items[i]; maxelements.add(items[i]); if (maxelements.size() < subgroup) { //the queue of largest numbers isn't full system.out.println("-1"); } else { // queue of largest numbers full system.out.println(product); } } } } } public static void main(string[] args) { int[] input = {6, 2, 9, 3, 8}; product(input, 3); }
output:
-1 -1 108 162 432
edit: little bit faster:
private static void heap(int[] maxelements) { int candidate = 1; if (maxelements[1] > maxelements[2]) { candidate = 2; } if (maxelements[0] > maxelements[candidate]) { int temp = maxelements[0]; maxelements[0] = maxelements[candidate]; maxelements[candidate] = temp; } } private static int addelement(int k, int[] maxelements, int item) { if (k < 3) { maxelements[k++] = item; if (k < 3) { system.out.println("-1"); } else { heap(maxelements); system.out.println(maxelements[0] * maxelements[1] * maxelements[2]); } } else { if (maxelements[0] < item) { maxelements[0] = item; heap(maxelements); } system.out.println(maxelements[0] * maxelements[1] * maxelements[2]); } return k; } public static void main(string args[]) throws ioexception { bufferedreader br = new bufferedreader(new inputstreamreader(system.in)); int n = integer.parseint(br.readline()); // no. of elements string line = br.readline(); //accepting elements in string int[] maxelements = new int[3]; int k = 0; int item = 0; char c; (int = 0; < line.length(); i++) { c = line.charat(i); if (c == ' ') { k = addelement(k, maxelements, item); item = 0; } else { item = item * 10 + (c - '0'); } } addelement(k, maxelements, item); }
Comments
Post a Comment