Is this the most efficient way to initialize an array - java
I am doing an exercise, which requires to create 20 long arrays with 10^18 numbers in 10^5 cells. Every time I am always using the same array simply overwriting it in this way:
array = new long [b];
The program stops working at around 19th iteration, because of overused provided resources by the 'Hackerrank' website. What are the ways to make this code more efficient? I was thinking that maybe I am not deleting previously used array and it overuses system parameters. Is there a better way to do this? This is done in www.hackerrank.com and it only shows that "Runtime time" error occurs, but doesn't show which one exactly. It runs fine without printing the answer, so it probably requires to use slightly less resources and I think most resources are required for array.
My code:
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int b;
long n;
long k;
long []array;
int count;
long sum = 0;
for(int i = 0; i < t; i++){
n = sc.nextLong();
k = sc.nextLong();
b = sc.nextInt();
count = 0;
sum = 0;
array = new long [b];
for (int j = 0; j < b; j++){
array[b - 1 - j]=j+1;
sum +=(j+1);
}
while (true){
while (sum < n && count < b && k > b){
if ((sum + (k - count - array[count]) )< n) {
sum += k - count - array[count];
array[count]+=k - count - array[count];
}
else {
System.out.println (n - sum);
array[count]+= n - sum;
sum += n - sum;
}
if (array[count] == k - count) count++;
}
if (sum == n) {
for (int x = 0; x < b; x++){
System.out.print (array[x]);
if (x < b - 1) System.out.print(" ");
else System.out.print("\n");
}
break;
}
else {
System.out.println ("-1");
break;
}
}
}
}
}
Input:
20
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
999999995000050000 10000000000000 100000
All cases work separately.
The output is:
10000000000000 9999999999999 9999999999998 9999999999997 9999999999996 9999999999995 9999999999994 9999999999993 9999999999992 9999999999991 9999999999990 9999999999989 9999999999988 9999999999987 9999999999986 9999999999985 9999999999984 9999999999983 9999999999982 9999999999981 9999999999980 9999999999979 9999999999978 9999999999977 9999999999976 9999999999975 9999999999974 9999999999973 9999999999972 9999999999971 9999999999970 9999999999969 9999999999968 9999999999967 9999999999966 9999999999965 9999999999964 9999999999963 9999999999962 9999999999961 9999999999960 9999999999959 9999999999958 9999999999957 9999999999956 9999999999955 9999999999954 9999999999953 9999999999952 9999999999951 9999999999950 9999999999949 9999999999948 9999999999947 9999999999946 9999999999945 9999999999944 9999999999943 9999999999942 9999999999941 9999999999940 9999999999939 9999999999938 9999999999937 9999999999936 9999999999935 9999999999934 9999999999933 9999999999932 9999999999931 9999999999930 9999999999929 9999999999928 9999999999927 9999999999926 9999999999925 9999999999924 9999999999923 9999999999922 9999999999921 9999999999920 9999999999919 9999999999918 9999999999917 9999999999916 9999999999915 9999999999914 9999999999913 9999999999912 9999999999911 9999999999910 9999999999909 9999999999908 9999999999907 9999999999906 9999999999905 9999999999904 9999999999903 9999999999902 9999999999901 9999999999900 9999999999899 9999999999898 9999999999897 9999999999896 9999999999895 9999999999894 9999999999893 9999999999892 9999999999891 9999999999890 9999999999889 9999999999888 9999999999887 9999999999886 9999999999885 9999999999884 9999999999883 9999999999882 9999999999881 9999999999880 9999999999879 9999999999878 9999999999877 9999999999876 9999999999875 9999999999874 9999999999873 9999999999872 9999999999871 9999999999870 9999999999869 9999999999868 9999999999867 9999999999866 9999999999865 9999999999864 9999999999863 9999999999862 9999999999861 9999999999860 9999999999859 9999999999858 9999999999857 9999999999856 9999999999855 9999999999854 9999999999853 9999999999852 9999999999851 9999999999850 9999999999849 9999999999848 9999999999847 9999999999846 9999999999845 9999999999844 9999999999843 9999999999842 9999999999841 9999999999840 9999999999839 9999999999838 9999999999837 9999999999836 9999999999835 9999999999834 9999999999833 9999999999832 9999999999831 9999999999830 9999999999829 9999999999828 9999999999827 9999999999826 9999999999825 9999999999824 9999999999823 9999999999822 9999999999821 9999999999820 9999999999819 9999999999818 9999999999817 9999999999816 9999999999815 9999999999814 9999999999813 9999999999812 9999999999811 9999999999810 9999999999809 9999999999808 9999999999807 9999999999806 9999999999805 9999999999804 9999999999803 9999999999802 9999999999801 9999999999800 9999999999799 9999999999798 9999999999797 9999999999796 9999999999190 9999999999189 9999999999188 9999999999187 9999999999186 9999999999185 9999999999184 9999999999183 9999999999182 9999999999181 9999999999180 9999999999179 9999999999178 9999999999177 9999999999176 9999999999175 9999999999174 9999999999173 9999999999172 9999999999171 9999999999170 9999999999169 9999999999168 9999999999167 9999999999166 9999999999165 9999999999164 9999999999163 9999999999162 9999999999161 9999999999160 9999999999159 9999999999158 9999999999157 9999999999156 9999999999155 9999999999154 9999999999153 9999999999152 9999999999151 9999999999150 9999999999149 9999999999148 9999999999147 9999999999146 9999999999145 9999999999144 9999999999143 9999999999142 9999999999141 9999999999140 9999999999139 9999999999138 9999999999137 9999999999136 9999999999135 9999999999134 9999999999133 9999999999132 9999999999131 9999999999130 9999999999129 9999999999128 9999999999127 9999999999126 9999999999125 9999999999124 9999999999123 9999999999122 9999999999121 9999999999120 9999999999119 9999999999118 9999999999117 9999999999116 9999999999115 9999999999114 9999999999113 9999999999112 9999999999111 9999999999110 9999999999109 9999999999108 9999999999107 9999999999106 9999999999105 9999999999104 9999999999103 9999999999102 9999999999101 9999999999100 9999999999099 9999999999098 9999999999097 9999999999096 9999999999095 9999999999094 9999999999093 9999999999092 9999999999091 9999999999090 9999999999089 9999999999088 9999999999087 9999999999086 9999999999085 9999999999084 9999999999083 9999999999082 9999999999081 9999999999080 9999999999079 9999999999078 9999999999077 9999999999076 9999999999075 9999999999074 9999999999073 9999999999072 9999999999071 9999999999070 9999999999069 9999999999068 9999999999067 9999999999066 9999999999065 9999999999064 9999999999063 9999999999062 9999999999061 9999999999060 9999999999059 9999999999058 9999999999057 9999999999056 9999999999055 9999999999054 9999999999053 9999999999052 9999999999051 9999999999050 9999999999049 9999999999048 9999999999047 9999999999046 9999999999045 9999999999044 9999999999043 9999999999042 9999999999041 9999999999040 9999999999039 9999999999038 9999999999037 9999999999036 9999999999035 9999999999034 9999999999033 9999999999032 9999999999031 9999999999030 9999999999029 9999999999028 9999999999027 9999999999026 9999999999025 9999999999024 9999999999023 9999999999022 9999999999021 9999999999020 9999999999019 9999999999018 9999999999017 9999999999016 9999999999015 9999999999014 9999999999013 9999999999012 9999999999011 9999999999010 9999999999009 9999999999008 9999999999007 9999999999006 9999999999005 9999999999004 9999999999003 9999999999002 9999999999001 9999999999000 9999999998999 9999999998998 9999999998997 9999999998996 9999999998995 9999999998994 9999999998993 9999999998992 9999999998991 9999999998990 9999999998989 9999999998988 9999999998987 9999999998986 9999999998985 9999999998984 9999999998983 9999999998982 9999999998981 9999999998980 9999999998979 9999999998978 9999999998977 9999999998976 9999999998975 9999999998974 9999999998973 9999999998972 9999999998971 9999999998970 9999999998969 9999999998968 9999999998967 9999999998966 9999999998965 9999999998964 9999999998963 9999999998962 9999999998961 9999999998960 9999999998959 9999999998958 9999999998957 9999999998956 9999999998955 9999999998954 9999999998953 9999999998952 9999999998951 9999999998950 9999999998949 9999999998948 9999999998947 9999999998946 9999999998945 9999999998944 9999999998943 9999999998942 9999999998941 9999999998940 9999999998939 9999999998938 9999999998937 9999999998936 9999999998935 9999999998934 9999999998933 9999999998932 9999999998931 9999999998930 9999999998929 9999999998928 9999999998927 9999999998926 9999999998925 9999999998924 9999999998923 9999999998922 9999999998921 9999999998920 9999999998919 9999999998918 9999999998917 9999999998916 9999999998915 9999999998914 9999999998913 9999999998912 9999999998911 9999999998910 9999999998909 9999999998908 9999999998907 9999999998906 9999999998905 9999999998904 9999999998903 9999999998902 9999999998901 9999999998900 9999999998899 9999999998898 9999999998897 9999999998896 9999999998895 9999999998894 9999999998893 9999999998892 9999999998891 9999999998890 9999999998889 9999999998888 9999999998887 9999999998886 9999999998885 9999999998884 9999999998883 9999999998882 9999999998881 9999999998880 9999999998879 9999999998878 9999999998877 9999999998876 9999999998875 9999999998874 9999999998873 9999999998872 9999999998871 9999999998870 9999999998869 9999999998868 9999999998867 9999999998866 9999999998865 9999999998864 9999999998863 9999999998862 9999999998861 9999999998860 9999999998859 9999999998858 9999999998857 9999999998856 9999999998855 9999999998854 9999999998853 9999999998852 9999999998851 9999999998850 9999999998849 9999999998848 9999999998847 9999999998846 9999999998845 9999999998844 9999999998843 9999999998842 9999999998841 9999999998840 9999999998839 9999999998838 9999999998837 9999999998836 9999999998835 9999999998834 9999999998833 9999999998832 9999999998831 9999999998830 9999999998829 9999999998828 9999999998827 9999999998826 9999999998825 9999999998824 9999999998823 9999999998822 9999999998821 9999999998820 9999999998819 9999999998818 9999999998817 9999999998816 9999999998815 9999999998814 9999999998813 9999999998812 9999999998811 9999999998810 9999999998809 9999999998808 9999999998807 9999999998806 9999999998805 9999999998804 9999999998803 9999999998802 9999999998801 9999999998800 9999999998799 9999999998798 9999999998797 9999999998796 9999999998795 9999999998794 9999999998793 9999999998792 9999999998791 9999999998790 9999999998789 9999999998788 9999999998787 9999999998786 9999999998785 9999999998784 9999999998783 9999999998782 9999999998781 9999999998780 9999999998779 9999999998778 9999999998777 9999999998776 9999999998775 9999999998774 9999999998773 9999999998772 9999999998771 9999999998770 9999999998769 9999999998768 9999999998767 9999999998766 9999999998765 9999999998764 9999999998763 9999999998762 9999999998761 9999999998760 9999999998759 9999999998758 9999999998757 9999999998756 9999999998755 9999999998754 9999999998753 9999999998752 9999999998751 9999999998750 9999999998749 9999999998748 9999999998747 9999999998746 9999999998745 9999999998744 9999999998743 9999999998742 9999999998741 9999999998740 9999999998739 9999999998738 9999999998737 9999999998736 9999999998735 9999999998734 9999999998733 9999999998732 9999999998731 9999999998730 9999999998729 9999999998728 9999999998727 9999999998726 9999999998725 9999999998724 9999999998723 9999999998722 9999999998721 9999999998720 9999999998719 9999999998718 9999999998717 9999999998716 9999999998715 9999999998714 9999999998713 9999999998712 9999999998711 9999999998710 9999999998709 9999999998708 9999999998707 9999999998706 9999999998705 9999999998704 9999999998703 9999999998702 9999999998701 9999999998700 9999999998699 9999999998698 9999999998697 9999999998696 9999999998695 9999999998694 9999999998693 9999999998692 9999999998691 9999999998690 9999999998689 9999999998688 9999999998687 9999999998686 9999999998685 9999999998684 9999999998683 9999999998682 9999999998681 9999999998680 9999999998679 9999999998678 9999999998677 9999999998676 9999999998675 9999999998674 9999999998673 9999999998672 9999999998671 9999999998670 9999999998669 9999999998668 9999999998667 9999999998666 9999999998665 9999999998664 9999999998663 9999999998662 9999999998661 9999999998660 9999999998659 9999999998658 9999999998657 9999999998656 9999999998655 9999999998654 9999999998653 9999999998652 9999999998651 9999999998650 9999999998649 9999999998648 9999999998647 9999999998646 9999999998645 9999999998644 9999999998643 9999999998642 9999999998641 9999999998640 9999999998639 9999999998638 9999999998637 9999999998636 9999999998635 9999999998634 9999999998633 9999999998632 9999999998631 9999999998630 9999999998629 9999999998628 9999999998627 9999999998626 9999999998625 9999999998624 9999999998623 9999999998622 9999999998621 9999999998620 9999999998619 9999999998618 9999999998617 9999999998616 9999999998615 9999999998614 9999999998613 9999999998612 9999999998611 9999999998610 9999999998609 9999999998608 9999999998607 9999999998606 9999999998605 9999999998604 9999999998603 9999999998602 9999999998601 9999999998600 9999999998599 9999999998598 9999999998597 9999999998596 9999999998595 9999999998594 9999999998593 9999999998592 9999999998591 9999999998590 9999999998589 9999999998588 9999999998587 9999999998586 9999999998585 9999999998584 9999999998583 9999999998582 9999999998581 9999999998580 9999999998579 9999999998578 9999999998577 9999999998576 9999999998575 9999999998574 9999999998573 9999999998572 9999999998571 9999999998570 9999999998569 9999999998568 9999999998567 9999999998566 9999999998565 9999999998564 9999999998563 9999999998562 9999999998561 9999999998560 9999999998559 9999999998558 9999999998557 9999999998556 9999999998555 9999999998554 9999999998553 9999999998552 9999999998551 9999999998550 9999999998549 9999999998548 9999999998547 9999999998546 9999999998545 9999999998544 9999999998543 9999999998542 9999999998541 9999999998540 9999999998539 9999999998538 9999999998537 9999999998536 9999999998535 9999999998534 9999999998533 9999999998532 9999999998531 9999999998530 9999999998529 9999999998528 9999999998527 9999999998526 9999999998525 9999999998524 9999999998523 9999999998522 9999999998521 9999999998520 9999999998519 9999999998518 9999999998517 9999999998516 9999999998515 9999999998514 9999999998513 9999999998512 9999999998511 9999999998510 9999999998509 9999999998508 9999999998507 9999999998506 9999999998505 9999999998504 9999999998503 9999999998502 9999999998501 9999999998500 9999999998499 9999999998498 9999999998497 9999999998496 9999999998495 9999999998494 9999999998493 9999999998492 9999999998491 9999999998490 9999999998489 9999999998488 9999999998487 9999999998486 9999999998485 9999999998484 9999999998483 9999999998482 9999999998481 9999999998480 9999999998479 9999999998478 9999999998477 9999999998476 9999999998475 9999999998474 9999999998473 9999999998472 9999999998471 9999999998470 9999999998469 9999999998468 9999999998467 9999999998466 9999999998465 9999999998464 9999999998463 9999999998462 9999999998461 9999999998460 9999999998459 9999999998458 9999999998457 9999999998456 9999999998455 9999999998454 9999999998453 9999999998452 9999999998451 9999999998450 9999999998449 9999999998448 9999999998447 9999999998446 9999999998445 9999999998444 9999999998443 9999999998442 9999999998441 9999999998440 9999999998439 9999999998438 9999999998437 9999999998436 9999999998435 9999999998434 9999999998433 9999999998432 9999999998431 9999999998430 9999999998429 9999999998428 9999999998427 9999999998426 9999999998425 9999999998424 9999999998423 9999999998422 9999999998421 9999999998420 9999999998419 9999999998418 9999999998417 9999999998416 9999999998415 9999999998414 9999999998413 9999999998412 9999999998411 9999999998410 9999999998409 9999999998408 9999999998407 9999999998406 9999999998405 9999999998404 9999999998403 9999999998402 9999999998401 9999999998400 9999999998399 9999999998398 9999999998397 9999999998396 9999999998395 9999999998394 9999999998393 9999999998392 9999999998391 9999999998390 9999999998389 9999999998388 9999999998387 9999999998386 9999999998385 9999999998384 9999999998383 9999999998382 9999999998381 9999999998380 9999999998379 9999999998378 9999999998377 9999999998376 9999999998375 9999999998374 9999999998373 9999999998372 9999999998371 9999999998370 9999999998369 9999999998368 9999999998367 9999999998366 9999999998365 9999999998364 9999999998363 9999999998362 9999999998361 9999999998360 9999999998359 9999999998358 9999999998357 9999999998356 9999999998355 9999999998354 9999999998353 9999999998352 9999999998351 9999999998350 9999999998349 9999999998348 9999999998347 9999999998346 9999999998345 9999999998344 9999999998343 9999999998342 9999999998341 9999999998340 9999999998339 9999999998338 9999999998337 9999999998336 9999999998335 9999999998334 9999999998333 9999999998332 9999999998331 9999999998330 9999999998329 9999999998328 9999999998327 9999999998326 9999999998325 9999999998324 9999999998323 9999999998322 9999999998321 9999999998320 9999999998319 9999999998318 9999999998317 9999999998316 9999999998315 9999999998314 9999999998313 9999999998312 9999999998311 9999999998310 9999999998309 9999999998308 9999999998307 9999999998306 9999999998305 9999999998304 9999999998303 9999999998302 9999999998301 9999999998300 9999999998299 9999999998298 9999999998297 9999999998296 9999999998295 9999999998294 9999999998293 9999999998292 9999999998291 9999999998290 9999999998289 9999999998288 9999999998287 9999999998286 9999999998285 9999999998284 9999999998283 9999999998282 9999999998281 9999999998280 9999999998279 9999999998278 9999999998277 9999999998276 9999999998275 9999999998274 9999999998273 9999999998272 9999999998271 9999999998270 9999999998269 9999999998268 9999999998267 9999999998266 9999999998265 9999999998264 9999999998263 9999999998262 9999999998261 9999999998260 9999999998259 9999999998258 9999999998257 9999999998256 9999999998255 9999999998254 9999999998253 9999999998252 9999999998251 9999999998250 9999999998249 9999999998248 9999999998247 9999999998246 9999999998245 9999999998244 9999999998243 9999999998242 9999999998241 9999999998240 9999999998239 9999999998238 9999999998237 9999999998236 9999999998235 9999999998234 9999999998233 9999999998232 9999999998231 9999999998230 9999999998229 9999999998228 9999999998227 9999999998226 9999999998225 9999999998224 9999999998223 9999999998222 9999999998221 9999999998220 9999999998219 9999999998218 9999999998217 9999999998216 9999999998215 9999999998214 9999999998213 9999999998212 9999999998211 9999999998210 9999999998209 9999999998208 9999999998207 9999999998206 9999999998205 9999999998204 9999999998203 9999999998202 9999999998201 9999999998200 9999999998199 9999999998198 9999999998197 9999999998196 9999999998195 9999999998194 9999999998193 9999999998192 9999999998191 9999999998190 9999999998189 9999999998188 9999999998187 9999999998186 9999999998185 9999999998184 9999999998183 9999999998182 9999999998181 9999999998180 9999999998179 9999999998178 9999999998177 9999999998176 9999999998175 9999999998174 9999999998173 9999999998172 9999999998171 9999999998170 9999999998169 9999999998168 9999999998167 9999999998166 9999999998165 9999999998164 9999999998163 9999999998162 9999999998161 9999999998160 9999999998159 9999999998158 9999999998157 9999999998156 9999999998155 9999999998154 9999999998153 9999999998152 9999999998151 9999999998150 9999999998149 9999999998148 9999999998147 9999999998146 9999999998145 9999999998144 9999999998143 9999999998142 9999999998141 9999999998140 9999999998139 9999999998138 9999999998137 9999999998136 9999999998135 9999999998134 9999999998133 9999999998132 9999999998131 9999999998130 9999999998129 9999999998128 9999999998127 9999999998126 9999999998125 9999999998124 9999999998123 9999999998122 9999999998121 9999999998120 9999999998119 9999999998118 9999999998117 9999999998116 9999999998115 9999999998114 9999999998113 9999999998112 9999999998111 9999999998110 9999999998109 9999999998108 9999999998107 9999999998106 9999999998105 9999999998104 9999999998103 9999999998102 9999999998101 9999999998100 9999999998099 9999999998098 9999999998097 9999999998096 9999999998095 9999999998094 9999999998093 9999999998092 9999999998091 9999999998090 9999999998089 9999999998088 9999999998087 9999999998086 9999999998085 9999999998084 9999999998083 9999999998082 9999999998081 9999999998080 9999999998079 9999999998078 9999999998077 9999999998076 9999999998075 9999999998074 9999999998073 9999999998072 9999999998071 9999999998070 9999999998069 9999999998068 9999999998067 9999999998066 9999999998065 9999999998064 9999999998063 9999999998062 9999999998061 9999999998060 9999999998059 9999999998058 9999999998057 9999999998056 9999999998055 9999999998054 9999999998053 9999999998052 9999999998051 9999999998050 9999999998049 9999999998048 9999999998047 9999999998046 9999999998045 9999999998044 9999999998043 9999999998042 9999999998041 9999999998040 9999999998039 9999999998038 9999999998037 9999999998036 9999999998035 9999999998034 9999999998033 9999999998032 9999999998031 9999999998030 9999999998029 9999999998028 9999999998027 9999999998026 9999999998025 9999999998024 9999999998023 9999999998022 9999999998021 9999999998020 9999999998019 9999999998018 9999999998017 9999999998016 9999999998015 9999999998014 9999999998013 9999999998012 9999999998011 9999999998010 9999999998009 9999999998008 9999999998007 9999999998006 9999999998005 9999999998004 9999999998003 9999999998002 9999999998001 9999999998000 9999999997999 9999999997998 9999999997997 9999999997996 9999999997995 9999999997994 9999999997993 9999999997992 9999999997991 9999999997990 9999999997989 9999999997988 9999999997987 9999999997986 9999999997985 9999999997984 9999999997983 9999999997982 9999999997981 9999999997980 9999999997979 9999999997978 9999999997977 9999999997976 9999999997975 9999999997974 9999999997973 9999999997972 9999999997971 9999999997970 9999999997969 9999999997968 9999999997967 9999999997966 9999999997965 9999999997964 9999999997963 9999999997962 9999999997961 9999999997960 9999999997959 9999999997958 9999999997957 9999999997956 9999999997955 9999999997954 9999999997953 9999999997952 9999999997951 9999999997950 9999999997949 9999999997948 9999999997947 9999999997946 9999999997945 9999999997944 9999999997943 9999999997942 9999999997941 9999999997940 9999999997939 9999999997938 9999999997937 9999999997936 9999999997935 9999999997934 9999999997933 9999999997932 9999999997931 9999999997930 9999999997929 9999999997928 9999999997927 9999999997926 9999999997925 9999999997924 9999999997923 9999999997922 9999999997921 9999999997920 9999999997919 9999999997918 9999999997917 9999999997916 9999999997915 9999999997914 9999999997913 9999999997912 9999999997911 9999999997910 9999999997909 9999999997908 9999999997907 9999999997906 9999999997905 9999999997904 9999999997903 9999999997902 9999999997901 9999999997900 9999999997899 9999999997898 9999999997897 9999999997896 9999999997895 9999999997894 9999999997893 9999999997892 9999999997891 9999999997890 9999999997889 9999999997888 9999999997887 9999999997886 9999999997885 9999999997884 9999999997883 9999999997882 9999999997881 9999999997880 9999999997879 9999999997878 9999999997877 9999999997876 9999999997875 9999999997874 9999999997873 9999999997872 9999999997871 9999999997870 9999999997869 9999999997868 9999999997867 9999999997866 9999999997865 9999999997864 9999999997863 9999999997862 9999999997861 9999999997860 9999999997859 9999999997858 9999999997857 9999999997856 9999999997855 9999999997854 9999999997853 9999999997852 9999999997851 9999999997850 9999999997849 9999999997848 9999999997847 9999999997846 9999999997845 9999999997844 9999999997843 9999999997842 9999999997841 9999999997840 9999999997839 9999999997838 9999999997837 9999999997836 9999999997835 9999999997834 9999999997833 9999999997832 9999999997831 9999999997830 9999999997829 9999999997828 9999999997827 9999999997826 9999999997825 9999999997824 9999999997823 9999999997822 9999999997821 9999999997820 9999999997819 9999999997818 9999999997817 9999999997816 9999999997815 9999999997814 9999999997813 9999999997812 9999999997811 9999999997810 9999999997809 9999999997808 9999999997807 9999999997806 9999999997805 9999999997804 9999999997803 9999999997802 9999999997801 9999999997800 9999999997799 9999999997798 9999999997797 9999999997796 9999999997795 9999999997794 9999999997793 9999999997792 9999999997791 9999999997790 9999999997789 9999999997788 9999999997787 9999999997786 9999999997785 9999999997784 9999999997783 9999999997782 9999999997781 9999999997780 9999999997779 9999999997778 9999999997777 9999999997776 9999999997775 9999999997774 9999999997773 9999999997772 9999999997771 9999999997770 9999999997769 9999999997768 9999999997767 9999999997766 9999999997765 9999999997764 9999999997763 9999999997762 9999999997761 9999999997760 9999999997759 9999999997758 9999999997757 9999999997756 9999999997755 9999999997754 9999999997753 9999999997752 9999999997751 9999999997750 9999999997749 9999999997748 9999999997747 9999999997746 9999999997745 9999999997744 9999999997743 9999999997742 9999999997741 9999999997740 9999999997739 9999999997738 9999999997737 9999999997736 9999999997735 9999999997734 9999999997733 9999999997732 9999999997731 9999999997730 9999999997729 9999999997728 9999999997727 9999999997726 9999999997725 9999999997724 9999999997723 9999999997722 9999999997721 9999999997720 9999999997719 9999999997718 9999999997717 9999999997716 9999999997715 9999999997714 9999999997713 9999999997712 9999999997711 9999999997710 9999999997709 9999999997708 9999999997707 9999999997706 9999999997705 9999999997704 9999999997703 9999999997702 9999999997701 9999999997700 9999999997699 9999999997698 9999999997697 9999999997696 9999999997695 9999999997694 9999999997693 9999999997692 9999999997691 9999999997690 9999999997689 9999999997688 9999999997687 9999999997686 9999999997685 9999999997684 9999999997683 9999999997682 9999999997681 9999999997680 9999999997679 9999999997678 9999999997677 9999999997676 9999999997675 9999999997674 9999999997673 9999999997672 9999999997671 9999999997670 9999999997669 9999999997668 9999999997667 9999999997666 9999999997665 9999999997664 9999999997663 9999999997662 9999999997661 99999999{-truncated-}
You are reusing the same array pointer. But not really re using the exact same memory space. Its not really the best practice in the case you mentioned.
You can create the array (alloccate memory) only once in the beginning of your code and re use it in all further cases.
import java.io.*;
import java.util.*;
import java.util.stream.*;
public class Solution {
public static void main(String[] args) {
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
int b;
long n;
long k;
long []array;
array = new long [100005]; // biggest size that can be possible
int count;
long sum = 0;
for(int i = 0; i < t; i++){
n = sc.nextLong();
k = sc.nextLong();
b = sc.nextInt();
count = 0;
sum = 0;
//no need to create it again and again, just create it once but create it as the biggest size
for (int j = 0; j < b; j++){
array[b - 1 - j]=j+1;
sum +=(j+1);
}
while (true){
while (sum < n && count < b && k > b){
if ((sum + (k - count - array[count]) )< n) {
sum += k - count - array[count];
array[count]+=k - count - array[count];
}
else {
System.out.println (n - sum);
array[count]+= n - sum;
sum += n - sum;
}
if (array[count] == k - count) count++;
}
if (sum == n) {
for (int x = 0; x < b; x++){
System.out.print (array[x]);
if (x < b - 1) System.out.print(" ");
else System.out.print("\n");
}
break;
}
else {
System.out.println ("-1");
break;
}
}
}
}
}
Related
EhCache does not release the space consumed for persistent storage into disk, even after all keys are expired
It is observed that EhCache is not releasing the space consumed in the disk, even after the removal of all keys by itself, due to timeToLiveExpiration expiration (after 60 seconds in my case). However, EhCache is not so keen on releasing the disk space consumed while storing them into the disk. Can some experts please comment, on what is wrong with my configuration (Java Based) Note: This might be in some way related to the below question, but answers there yet. Ehcache doen't flush to disk when process is killed Here is my code. import org.ehcache.Cache; import org.ehcache.PersistentCacheManager; import org.ehcache.config.builders.CacheConfigurationBuilder; import org.ehcache.config.builders.CacheManagerBuilder; import org.ehcache.config.builders.ExpiryPolicyBuilder; import org.ehcache.config.builders.ResourcePoolsBuilder; import org.ehcache.config.units.EntryUnit; import org.ehcache.config.units.MemoryUnit; import java.time.Duration; import java.util.ArrayList; public class CacheHelper { private static PersistentCacheManager cacheManager; public static Cache<String, ArrayList> getDataStore() { if (cacheManager == null) { cacheManager = CacheManagerBuilder.newCacheManagerBuilder() .with(CacheManagerBuilder.persistence(System.getProperty("user.dir"))) .withCache("datastore", CacheConfigurationBuilder .newCacheConfigurationBuilder(String.class, ArrayList.class, ResourcePoolsBuilder.newResourcePoolsBuilder() .heap(40, EntryUnit.ENTRIES) .disk(500, MemoryUnit.MB, true) ) .withExpiry(ExpiryPolicyBuilder.timeToLiveExpiration(Duration.ofSeconds(60))) ) .build(true); } return cacheManager.getCache("datastore", String.class, ArrayList.class); } } Here is how I tested the above class import org.apache.commons.io.FileUtils; import java.io.File; import java.util.ArrayList; public class TestingCacheClass { public static void main(String[] args) throws InterruptedException { for (int i = 1; i <= 100; i++) { ArrayList<PlayerInfo> team = new ArrayList<>(); for (int j = 1; j < 100000; j++) { team.add(new PlayerInfo("Player is this with age " + String.valueOf(j), 80000)); } CacheHelper.getDataStore().put("Team_" + i, team); } System.out.println("All inserts are done, Disk size : " + FileUtils.sizeOfDirectory(new File("file")) / 1048576 + " MB"); Thread.sleep(60000);//waits for 60 seconds so that all keys in cache expires //retrieval of caches for (int k = 1; k <= 100; k++) { if (CacheHelper.getDataStore().containsKey("Team_" + k)) { //never reaches here as keys expire ArrayList<PlayerInfo> contents = CacheHelper.getDataStore().get("Team_" + k); System.out.println(contents.size() + " key got from cache, ms. Disk size : " + FileUtils.sizeOfDirectory(new File("file")) / 1048576 + " MB"); } else System.out.println("Team_" + k + " is no more existent"); //always reaches here as every key gets expired. } while (true) { Thread.sleep(10000); System.out.println(System.currentTimeMillis() + " ms. Disk size : " + FileUtils.sizeOfDirectory(new File("file")) / 1048576 + " MB"); //above statement prints - Disk size : 404 MB } } } Here is the output console All inserts are done, Disk size : 404 MB Team_1 is no more existent Team_2 is no more existent Team_3 is no more existent . . . Team_98 is no more existent Team_99 is no more existent Team_100 is no more existent 1645189622984 ms. Disk size : 404 MB (the memory doesn't get free, that is the issue) 1645189632991 ms. Disk size : 404 MB 1645189643002 ms. Disk size : 404 MB 1645189653009 ms. Disk size : 404 MB 1645189663012 ms. Disk size : 404 MB 1645189673021 ms. Disk size : 404 MB Additional details Ehcache maven dependency version: 3.8.1 Java version: 1.8.0_291
Get all perfect squares in a given range
I have 2 numbers M and N of range, I want to count all perfect squares in this range and that perfect square should have below property. lets say the perfect square has digits n1,n2,n3,n4, n5 etc then the digits should be like this: n1 < n2 > n3 < n4 > n5 etc Example: Input : M = 40, N = 70 Output : 1 Explanation: The perfect squares in the range 40 to 70 are 49 and 64. But only 49 has the proerty where 4 < 9. But for 64, 6 < 4 is false. So output is 1. Constraints: 1 <= M <= N <= 1011. This was asked during an interview and I tried the below code that works for some input and fails for some hidden test cases which I don't know. I have taken code references from this link to find out perfect squares first. static int perfectSquares(long M, long N) { int total = 0; // Getting the very first number long number = (long) Math.ceil(Math.sqrt(M)); // First number's square long n2 = number * number; // Next number is at the difference of number = (number * 2) + 1; // While the perfect squares // are from the range while ((n2 >= M && n2 <= N)) { /** Logic to check if digits of form d1 < d2 > d3 < d4 > d5 etc **/ long n = n2; String s = n + ""; int a = s.charAt(0) - '0'; boolean isless = true; boolean valid = true; for (int i = 1; i < s.length(); i++) { int b = s.charAt(i) - '0'; if (isless && a > b) { valid = false; break; } if (!isless && a < b) { valid = false; break; } isless = !isless; a = b; } // Count the valid the perfect square if (valid) { System.out.println("sq:" + n2); total++; } // Get the next perfect square n2 = n2 + number; // Next odd number to be added number += 2; } return total; } Is there any bug in my code or otherwise can we reduce the time complexity of this code?
Issue is with the checks for "valid" where simply using >= and <= will fix it. I ran your code against the full range (1..100000000000) and found an issue early with the series result (your code prints the valid numbers (i got rid of the newline)): 1 4 9 16 25 36 49 121 144 196 361 441 484 576 676 784 1225 1444 1600 1849 1936 2209 2304 2401 2500 2601 2704 2809 2916 3600 3844 3969 4624 4900 5625 5929 6724 6889 7744 8836 14161 15376 16384 17161 18496 19044 19881 25281 26244 28561 29241 29584 35344 36481 49284 57121 58081 58564 66564 67081 68121 69696 77284 110224 110889 111556 120409 121104 121801 122500 131769 133956 140625 144400 150544 154449 160000 160801 161604 162409 164836 165649 172225 173889 174724 180625 182329 184900 186624 190969 191844 193600 198916 220900 230400 232324 240100 250000 260100 270400 273529 275625 277729 280900 291600 294849 295936 330625 332929 342225 351649 352836 360000 361201 362404 363609 364816 374544 375769 381924 384400 390625 396900 442225 443556 444889 452929 455625 462400 471969 481636 484416 485809 490000 491401 492804 495616 553536 562500 565504 571536 580644 586756 592900 660969 665856 672400 680625 683929 688900 692224 693889 695556 774400 777924 786769 883600 894916 896809 1119364 1227664 1229881 1304164 1406596 1418481 1503076 1515361 1527696 1535121 1615441 1623076 1628176 1633284 1666681 1669264 1726596 1745041 1747684 1758276 1766241 1844164 1855044 1907161 1937664 1999396 2208196 2214144 2217121 2223081 2226064 2307361 2316484 2328676 2427364 2446096 2515396 2534464 2637376 2768896 2802276 2819041 2849344 2859481 2917264 2937796 2979076 2989441 3316041 3319684 3334276 3504384 3519376 3538161 3644281 3709476 3717184 3748096 3767481 3837681 3849444 3857296 3888784 3916441 3924361 3944196 3956121 3968064 4528384 4713241 4717584 4726276 4748041 4778596 4879681 4937284 4946176 4955076 4968441 4977361 4999696 5612161 5626384 5645376 5669161 5803281 5827396 5866084 5900041 5934096 5958481 6615184 6625476 6646084 6713281 6718464 6859161 6906384 6922161 6948496 6959044 7706176 7717284 7907344 7918596 7958041 8856576 8868484 8916196 8928144 8934121 8946081 9979281 11022400 11042329 11055625 11088900 11122225 11155600 11175649 11182336 11195716 12040900 12061729 12075625 12110400 12180100 12250000 13010449 13053769 13140625 13176900 13191424 13271449 13293316 13344409 13351716 13373649 13395600 14032516 14040009 14062500 14070001 14092516 14152644 14182756 14197824 14250625 14265729 14386849 14394436 14440000 14462809 14470416 14485636 15054400 15186609 15194404 15233409 15241216 15264649 15342889 15350724 15444900 15460624 15499969 15570916 15586704 15594601 16000000 16080100 16120225 16160400 16184529 16240900 16281225 16353936 16370116 16394401 16483600 16564900 16662724 16670889 17073424 17164449 17172736 17222500 17230801 17255716 17272336 17280649 17355556 17363889 17372224 17388900 17472400 17673616 17690436 17698849 17774656 17791524 18011536 18062500 18164644 18190225 18232900 18275625 18292729 18352656 18361225 18386944 18455616 18472804 18481401 18490000 18662400 18774889 18783556 18792225 18887716 19061956 19096900 19140625 19184400 19254544 19333609 19342404 19351201 19360000 19465744 19492225 19562929 19580625 19686969 19695844 19775809 19784704 19793601 19891600 22033636 22052416 22061809 22071204 22080601 22090000 22155849 22231225 22240656 23011209 23020804 23030401 23040000 23164969 23193856 23232400 23261329 23280625 23299929 24000201 24010000 24176889 24186724 24255625 24373969 24383844 24462916 24472809 24482704 24492601 25000000 25010001 25020004 25030009 25150225 25180324 25230529 25250625 25270729 25351225 25381444 25441936 25472209 25482304 25492401 26010000 26020201 26030404 26040609 26050816 26081449 26132544 26142769 26193924 26265625 26450449 26460736 26481316 26491609 26553409 26563716 26594649 26697889 27040000 27050401 27060804 27071209 27081616 27164944 27352900 27373824 27394756 27562500 27583504 27772900 28090000 28111204 28121809 28132416 28153636 28355625 28376929 28440889 28451556 28462225 28590409 28686736 28793956 28890625 29030544 29084449 29160000 29170801 29181604 29192409 29343889 29354724 29452329 29484900 29560969 29571844 29593600 29680704 29691601 33062500 33085504 33131536 33292900 33350625 33373729 34140649 34152336 34175716 34222500 34292736 34374769 35010889 35022724 35081929 35164900 35283600 35354916 35366809 35390601 35450116 35473936 35485849 35581225 36000000 36120100 36180225 36240400 36360900 36481600 36590401 37063744 37161216 37173409 37185604 37197801 37222201 37454400 37576900 37662769 37773316 37785609 37797904 38031889 38130625 38192400 38241856 38365636 38390416 38440000 38452401 38464804 38588944 38663524 38775529 39050001 39062500 39250225 39262756 39287824 39350529 39375625 39463524 39664804 39690000 39891856 44142736 44182609 44195904 44222500 44262409 44275716 44355600 44462224 44488900 45050944 45091225 45131524 45252529 45292900 45360225 45481536 45562500 46022656 46076944 46144849 46185616 46240000 46253601 46280809 46294416 46553329 46580625 47141956 47196900 47265625 47361924 47444544 47554816 47582404 47692836 47775744 48052624 48121969 48163600 48260809 48274704 48330304 48385936 48441600 48580900 48790225 49000000 49140100 49252324 49280400 49350625 49491225 49561600 49660209 49885969 55011889 55130625 55160329 55353600 55442916 55472704 55591936 56220004 56250000 56280004 56550400 56595529 57032704 57062916 57153600 57183844 57198969 57274624 57350329 57380625 57486724 57577744 57790404 58033924 58064400 58140625 58277956 58461316 58476609 58491904 58583716 58675600 58782889 59043856 59243809 59274601 59290000 59351616 59382436 59397849 59474944 59675625 59783824 66096900 66161956 66373609 66471409 66487716 66585600 66683556 67141636 67174416 67190809 67240000 67272804 67354849 67683529 68062500 68095504 68161536 68260644 68392900 68442529 68475625 68591524 68690944 68773849 68790436 68890000 69155856 69222400 69272329 69388900 69455556 69472225 69555600 69672409 69772609 77000625 77440000 77492809 77792400 78021889 78251716 78340201 78375609 78393316 78552769 78676900 79174404 79192201 79263409 79281216 79691329 79780624 79887844 88021924 88284816 88341201 88360000 88472836 88491649 88585744 88792929 89075844 89264704 89283601 89340304 89491600 89680900 89775625 99022401 99042304 99062209 99121936 99141849 99241444 99460729 110418064 110439081 110502144 110544196 110649361 110838784 111746041 111767184 111809476 111915241 111957561 120209296 120428676 120516484 120538441 120736144 120758121 120802081 120824064 121308196 121418361 121528576 121837444 122412096 122722084 122744241 130439241 130507776 130622041 130919364 131308681 131423296 131744484 131767441 131813361 131928196 132434064 132503121 132526144 132756484 132848676 133726096 133888041 133911184 133957476 140209281 140612164 140707044 140944384 141419664 141824281 142229476 142301041 143328784 143544361 143712144 143736121 143808064 144504441 144528484 150528361 150749284 151807041 151979584 152226244 152819044 152868496 153958464 154405476 154778481 154803364 155725441 160326244 160757041 161518681 161849284 162333081 162766564 162919696 163609681 163635264 164506276 164557584 164814244 165611161 166616464 166978084 170537481 170668096 170877184 170929476 171819664 172423161 172607044 172712164 173659684 173923344 174609796 174927076 175801081 177715561 177848896 177902244 180311184 180338041 180526096 181225444 181548676 181656484 181737361 181926144 182547121 182628196 182817441 182844484 183819364 183846481 184715281 185558884 185613376 185804161 186978276 187909264 187936681 188815081 190219264 190826596 192959881 193738561 193822084 193877776 193989184 194937444 195608196 195748081 196616484 196728676 220879044 220938496 221444161 222427396 222636241 222666084 222845184 230402041 230766481 231709284 232227121 232318564 232806564 232837081 232989696 233417284 233845264 240002064 240529081 240622144 240839361 241118784 241429444 242549476 242705241 242767561 242923396 243609664 244547044 250304041 250525584 250557241 250778896 250937281 251603044 251666496 251825161 252206161 253637476 253701184 253733041 253956096 254849296 255616144 255648121 255712081 255744064 260209161 260435044 261113281 261436561 261727684 263445361 263607696 263705121 264517696 264745441 264908176 266636241 270306481 270339364 271524484 271557441 271623361 272514064 272547081 272613121 272646144 272712196 272877361 273439296 273505444 274929561 275858881 276756496 276823044 276956164 277855561 277922241 277955584 281434176 281769796 281937681 282946041 282979684 283518244 283619281 284968161 285914281 286828096 287709444 287777296 290225296 291419041 291658084 293711044 293848164 294534244 294877584 294946276 296666176 296735076 296838441 296907361 297838564 298978681 330439684 330803344 331749796 332734081 333756361 333829441 333939076 340107364 340845444 340919296 341436484 341547361 341806144 341917081 342546064 342657121 342768196 343657444 343768681 344436481 344622096 344919184 350513284 351225081 351900081 352538176 352613284 353778481 353966596 354418276 354606561 355624164 360544144 360658081 361304064 361418121 361836484 361988676 362445444 362559681 363436096 363703041 363817476 364504464 365536161 365727376 370216081 370909081 371949796 372837481 373301041 373339684 373958244 375623161 376709281 376903396 377758096 380601081 380718144 381108484 381655296 382515364 383337241 383415561 383807281 383846464 384905161 385768881 385808164 386869561 386948241 387735481 388957284 390418081 390813361 391723264 392515344 393546244 394777161 396766561 396846241 440118441 440412196 440538121 440622081 441336064 441504144 441924484 442513296 443439364 444408561 444619396 450755361 450967696 451945081 452668176 453306681 454627684 454926241 460617444 460703296 461218576 461304484 461433361 461648196 461734144 461777121 461906064 462637081 462723121 462766144 464747364 465739561 465955396 466948881 471845284 473889361 474629796 474847681 477947044 480004281 480223396 480749476 480837184 481407481 482329444 482417296 482768784 482856676 483516121 483604081 483648064 484528144 484616196 484836361 484924441 484968484 491908041 492218596 492307344 493817284 493906176 494439696 494706564 495908361 496888681 496933264 497825344 550559296 551216484 551733121 551827081 552626064 552767121 552814144 552908196 554979364 555639184 555827776 555922084 555969241 560316241 560647684 562733284 562828176 562923076 563635081 564727696 566868481 570302161 570779881 570827664 571879396 572214241 572549184 574848576 574944484 575616064 577729296 577825444 577969681 580424464 581726161 581967376 582305161 582546496 582836164 583657281 583802244 584624041 586705284 586802176 590927481 591511041 591559684 592338244 593312164 593507044 593604496 593848161 594433161 594969664 596629476 596727184 598878784 661209796 661724176 661827076 663526081 663629121 664402176 664505284 664917796 666724041 671224464 671535396 672313041 672935481 673869681 674648676 674856484 674908441 676624144 676728196 676988361 677977444 680114241 680218561 681627664 682202161 682829161 683404164 684816561 685706596 685968481 686859264 687803076 690323076 690428176 690533284 691216681 692426596 692847684 693848281 694744164 694955044 695957161 770506564 770839696 771117361 771228441 771506176 771617284 772339681 773507344 773618596 776848384 780755364 782656576 782768484 782824441 782936361 783328144 784448064 784504081 784616121 788823396 790959376 791634496 791747044 792929281 793605241 793999684 795747681 796707076 796989361 880427584 880546276 881555481 883516176 883635076 884408121 884527081 885538564 885717121 886729284 890306244 891559881 891739044 891858496 892336384 894548281 894847396 895745041 990738576 990927441 991557121 991746064 992817081 993447361 993636484 993888676 994519296 994645444 995907364 996728041 996917476 You should see it straight away: 144. So making the conditional changes as described yields: 1 4 9 16 25 36 49 121 196 361 484 576 676 784 1849 1936 2304 2401 2601 2704 2809 2916 3969 4624 5625 5929 6724 14161 15376 16384 17161 18496 25281 28561 29241 29584 36481 49284 57121 58081 58564 67081 68121 69696 120409 121801 131769 140625 160801 161604 162409 164836 165649 174724 180625 182329 190969 198916 232324 273529 275625 294849 295936 351649 352836 361201 362404 363609 364816 375769 381924 390625 452929 471969 481636 485809 491401 492804 495616 571536 586756 680625 683929 786769 894916 896809 1304164 1406596 1418481 1503076 1515361 1527696 1535121 1623076 1628176 1726596 1745041 1747684 1758276 1907161 2307361 2316484 2328676 2427364 2515396 2637376 2819041 2859481 2917264 2979076 3504384 3519376 3538161 3709476 3717184 3748096 3767481 3837681 3857296 3924361 3956121 3968064 4528384 4713241 4717584 4726276 4748041 4879681 4937284 4946176 5612161 5626384 5645376 5803281 5827396 5934096 5958481 6713281 6718464 6859161 6906384 6948496 7918596 7958041 8916196 8934121 8946081 12061729 12075625 13053769 13140625 13191424 14032516 14092516 14182756 14197824 14250625 14265729 14386849 15241216 15264649 15350724 15460624 16184529 16353936 17073424 17172736 17230801 17280649 17673616 17690436 18275625 18292729 18352656 18472804 18481401 19061956 19140625 19342404 19351201 19562929 19580625 19686969 19784704 19793601 23020804 23030401 23164969 23193856 23261329 23280625 24186724 24373969 25180324 25230529 25250625 25270729 25482304 25492401 26020201 26030404 26040609 26050816 26142769 26193924 26265625 26460736 26481316 26491609 26563716 26594649 27050401 27060804 27071209 27081616 27373824 27394756 27583504 28121809 28132416 28153636 28376929 28590409 28686736 28793956 29170801 29181604 29192409 29354724 29452329 29560969 29680704 29691601 34140649 34175716 34292736 34374769 35081929 35354916 35390601 35473936 35485849 36590401 37161216 37173409 37185604 37197801 38130625 38241856 38365636 38390416 38452401 38464804 39262756 39287824 39350529 39375625 39463524 39891856 45131524 45252529 45481536 46185616 46253601 46280809 46580625 47141956 47265625 47361924 47582404 47692836 48052624 48121969 48260809 48274704 48385936 49252324 49350625 57032704 57062916 57198969 57274624 57350329 57380625 57486724 58140625 58461316 58491904 58583716 59043856 59243809 59274601 59351616 59382436 59397849 59675625 59783824 67141636 67190809 67272804 67354849 67683529 68161536 68475625 68591524 68790436 69272329 69672409 78251716 78340201 78375609 79263409 79281216 79691329 79780624 89264704 89283601 89340304 120209296 120428676 120516484 120758121 120802081 120824064 121308196 121418361 121528576 130439241 130919364 131308681 131423296 131928196 132434064 132503121 132756484 132848676 140209281 140612164 141824281 142301041 143736121 143808064 150528361 150749284 151807041 151979584 152868496 153958464 160757041 161518681 161849284 162919696 163609681 163635264 164506276 170537481 170929476 172423161 172712164 173659684 174609796 174927076 175801081 180526096 181548676 181656484 181737361 182547121 182628196 183819364 183846481 184715281 185804161 186978276 187909264 190219264 190826596 193738561 193989184 195608196 195748081 196728676 230402041 231709284 232318564 232806564 232837081 232989696 240529081 240839361 242549476 242705241 242767561 250304041 250937281 251825161 253637476 253956096 254849296 260209161 261436561 261727684 263607696 263705121 264517696 264908176 270306481 272514064 272547081 272613121 272712196 273439296 274929561 276756496 276956164 281434176 281769796 281937681 282946041 282979684 283619281 284968161 285914281 286828096 291419041 291658084 293848164 294946276 296735076 296907361 297838564 298978681 340107364 340919296 341436484 341547361 341917081 342546064 342657121 342768196 343768681 350513284 352538176 352613284 354606561 360658081 361304064 361418121 361836484 363436096 363703041 363817476 365727376 370216081 370909081 371949796 372837481 375623161 376709281 380601081 382515364 383807281 383846464 384905161 385808164 386869561 386948241 390418081 391723264 396846241 450967696 451945081 454627684 454926241 460703296 461218576 461648196 461906064 462637081 462723121 464747364 465739561 471845284 474629796 474847681 480749476 480837184 481407481 482417296 482768784 483516121 483604081 483648064 484616196 484836361 484968484 491908041 493817284 493906176 494706564 495908361 560316241 560647684 562828176 562923076 563635081 564727696 570302161 571879396 572549184 574848576 575616064 581726161 581967376 582305161 582546496 582836164 583657281 584624041 586705284 586802176 590927481 593848161 596727184 671535396 672313041 672935481 673869681 674648676 674856484 676728196 680218561 682829161 683404164 684816561 685706596 685968481 686859264 687803076 690323076 690428176 692426596 692847684 693848281 695957161 782656576 782768484 782936361 784504081 784616121 790959376 792929281 793605241 795747681 796707076 796989361 891858496 894548281 894847396 895745041 Here's that section of code with the change - I suspect it doesn't need explaining ... but for n1 < n2 then the negation of that check is n1 >= n2 and similarly with n2 > n3 then the negation is n2 <= n3... if (isless && a >= b) { valid = false; break; } if (!isless && a <= b) { valid = false; break; } Well, I didn't check every number but this correction seems promising, agreed?
Personally, I think you may have over-complicated how to get the squares. Consider the following: // square root of lowest perfect square in range int n = (int)(Math.ceil(Math.sqrt(N))); // square root of highest perfect square in range int m = (int)(Math.floor(Math.sqrt(M))); for (int i=n;i<=m;i++) { // Calculate the number to test long j = i * i; etc... For more readability, you might also split the order test into its own function. Here is a different implementation that uses math, rather than using strings. /** Test to see if the given number has all digits in increasing order when read from Most Significant Digit to Least Significant Digit */ private boolean isOrdered(long n) { // Preset our current Least Significant Digit so that the first test // won't fail. long last = 10; boolean isLess = false; while (n > 0) { // Get the least significant digit long digit = n % 10; // If the digit is not in order compared to the previous LS digit, then return. // then return false. if (isLess ? digit <= last : digit >= last) return false; isLess = !isLess; // Remember the current LS Digit. last = digit; // Move to the next digit. n = n / 10; } return true; }
Unexpected output of InfluxDB batch write
I am using batch processing to write into InfluxDB and below is my code for doing that. String dbName = "test"; influxDB.query(new Query("CREATE DATABASE " + dbName, dbName)); Stopwatch watch = Stopwatch.createStarted(); influxDB.enableBatch(2000, 100, TimeUnit.MILLISECONDS); for (int j = 0; j < 100000; j++) { Point point = Point.measurement("cpu") .addField("idle", (double) j) .addField("system", 3.0 * j).build(); influxDB.write(dbName, "autogen", point); } influxDB.disableBatch(); System.out.println("Write for " + 100000 + " Points took:" + watch); } Here i am writing 100000 points and which is taking very reasonable time to write, however only few records are written into DB instead of expected 100000 records. select count(idle) from cpu gives me only "89" i am expecting it to be "100000" While select * from cpu gives me following: cpu time idle system 2016-10-06T23:57:41.184Z 8 24 2016-10-06T23:57:41.185Z 196 588 2016-10-06T23:57:41.186Z 436 1308 2016-10-06T23:57:41.187Z 660 1980 2016-10-06T23:57:41.188Z 916 2748 2016-10-06T23:57:41.189Z 1278 3834 2016-10-06T23:57:41.19Z 1405 4215 2016-10-06T23:57:41.191Z 1409 4227 2016-10-06T23:57:41.192Z 1802 5406 2016-10-06T23:57:41.193Z 1999 5997 2016-10-06T23:57:41.456Z 3757 11271 2016-10-06T23:57:41.457Z 3999 11997 2016-10-06T23:57:41.858Z 4826 14478 and so on..... Here my question is why the values of idle are missing, for example, after 8 it should 9, 10, 11, and so on but these values were not persisted and comes directly 196 and then missing in between and then 436. Any idea how to persist all value of loop variable "j" in this situation?
This line influxDB.enableBatch(2000, 100, TimeUnit.MILLISECONDS); says that it will flush input data if there are more than 2000 samples per 100 ms period. Since you are trying to write 100k samples then logically most of them get flushed. Instead, write less samples in a single batch. My recommendation would be to write 5000 samples in a single batch, and make multiple batches until all your data is in the db. // Batch 1 influxDB.enableBatch(5000, 100, TimeUnit.MILLISECONDS); for (int j = 0; j < 5000; j++) { Point point = Point.measurement("cpu") .addField("idle", (double) j) .addField("system", 3.0 * j).build(); influxDB.write(dbName, "autogen", point); } influxDB.disableBatch(); // Batch 2 // ...
Java - why doesn't jstack work in a tight loop
I've noticed that jstack doesn't work when code is in a tight loop. For example in this code: private static void testAllocationNotOnHeap(){ long time = System.currentTimeMillis(); long result =0; for (int j = 0; j < 20_000; j++) { for (int i = 0; i < 100_000_000; i++) { result += new TestObject(5,5).getResult(); } } System.out.println("Allocation not on heap took " + (System.currentTimeMillis()-time) + " with result " + result); } If you run jstack you get this message: Unable to open socket file: target process not responding or HotSpot VM not loaded The -F option can be used when the target process is not responding As soon as you allow the code to 'breathe' e.g. by add a System.out every few thousand iterations it all works fine. Note this is the same if you want to run jconsole or flight recorder from jmc. I'd like to understand why this is and if there's anything I can do about it as I'd like to profile a tight loop.
MongoDB ACKNOWLEDGED write concern faster than UNACKNOWLEDGED?
I've got a very simple test program that performs faster with ACKNOWLEDGED bulk inserts than with UNACKNOWLEDGED. And it's not just a little faster - I'm seeing a factor of nearly 100! My understanding of the difference between these two write concerns is solely that with ACKNOWLEDGED the client waits for confirmation from the server that the operation has been executed (but not necessarily made durable), while with UNACKNOWLEDGED the client only knows that the request made it out onto the wire. So it would seem preposterous that the former could actually perform at a higher speed, yet that's what I'm seeing. I'm using the Java driver (v2.12.0) with Oracle's Java JDK v1.7.0_71, and mongo version 3.0.0 on 64-bit Windows 7. I'm running mongod, completely out-of-the-box (fresh install), no sharding or anything. And before each test I ensure that the collection is empty and has no non-default indexes. I would appreciate any insight into why I'm consistently seeing the opposite of what I'd expect. Thanks. Here's my code: package test; import com.mongodb.BasicDBObject; import com.mongodb.BulkWriteOperation; import com.mongodb.BulkWriteResult; import com.mongodb.DBCollection; import com.mongodb.DBObject; import com.mongodb.MongoClient; import com.mongodb.ServerAddress; import com.mongodb.WriteConcern; import java.util.Arrays; public class Test { private static final int BATCHES = 100; private static final int BATCH_SIZE = 1000; private static final int COUNT = BATCHES * BATCH_SIZE; public static void main(String[] argv) throws Exception { DBCollection coll = new MongoClient(new ServerAddress()).getDB("test").getCollection("test"); for (String wcName : Arrays.asList("UNACKNOWLEDGED", "ACKNOWLEDGED")) { WriteConcern wc = (WriteConcern) WriteConcern.class.getField(wcName).get(null); coll.dropIndexes(); coll.remove(new BasicDBObject()); long start = System.currentTimeMillis(); BulkWriteOperation bulkOp = coll.initializeUnorderedBulkOperation(); for (int i = 1; i < COUNT; i++) { DBObject doc = new BasicDBObject().append("int", i).append("string", Integer.toString(i)); bulkOp.insert(doc); if (i % BATCH_SIZE == 0) { BulkWriteResult results = bulkOp.execute(wc); if (wc == WriteConcern.ACKNOWLEDGED && results.getInsertedCount() != 1000) { throw new RuntimeException("Bogus insert count: " + results.getInsertedCount()); } bulkOp = coll.initializeUnorderedBulkOperation(); } } long time = System.currentTimeMillis() - start; double rate = COUNT / (time / 1000.0); System.out.printf("%s[w=%s,j=%s]: Inserted %d documents in %s # %f/sec\n", wcName, wc.getW(), wc.getJ(), COUNT, duration(time), rate); } } private static String duration(long msec) { return String.format("%d:%02d:%02d.%03d", msec / (60 * 60 * 1000), (msec % (60 * 60 * 1000)) / (60 * 1000), (msec % (60 * 1000)) / 1000, msec % 1000); } } And here's typical output: UNACKNOWLEDGED[w=0,j=false]: Inserted 100000 documents in 0:01:27.025 # 1149.095088/sec ACKNOWLEDGED[w=1,j=false]: Inserted 100000 documents in 0:00:00.927 # 107874.865156/sec EDIT Ran more extensive tests, per request from Markus W. Mahlberg. For these tests, I ran the code with four write concerns: UNACKNOWLEDGED, ACKNOWLEDGED, JOURNALED, and FSYNCED. (I would expect this order to show decreasing speed.) I ran 112 repetitions, each of which performed 100 batches of 1000 inserts under each of the four write concerns, each time into an empty collection with no indexes. Code was identical to original post but with two additional write concerns, and with output to CSV format for easy analysis. Results summary: UNACKNOWLEDGED: 1147.105004 docs/sec avg, std dev 27.88577035 ACKNOWLEDGED: 77539.27653 docs/sec avg, std dev 1567.520303 JOURNALED: 29574.45243 docs/sec avg, std dev 123.9927554 FSYNCED: 29567.02467 docs/sec avg, std dev 147.6150994 The huge inverted performance difference between UNACKNOWLEDGED and ACKNOWLEDGED is what's got me baffled. Here's the raw data if anyone cares for it ("time" is elapsed msec for 100*1000 insertions; "rate" is docs/second): "UNACK time","UNACK rate","ACK time","ACK rate","JRNL time","JRNL rate","FSYNC time","FSYNC rate" 92815,1077.4120562409094,1348,74183.9762611276,3380,29585.798816568047,3378,29603.31557134399 90209,1108.5368422219512,1303,76745.97083653108,3377,29612.081729345577,3375,29629.62962962963 91089,1097.8273995762386,1319,75815.01137225171,3382,29568.30277942046,3413,29299.73630237328 90159,1109.1516099335618,1320,75757.57575757576,3375,29629.62962962963,3377,29612.081729345577 89922,1112.0749093658949,1315,76045.62737642587,3380,29585.798816568047,3376,29620.853080568722 89997,1111.1481493827573,1306,76569.67840735069,3381,29577.048210588586,3379,29594.55460195324 90141,1109.373093264996,1319,75815.01137225171,3386,29533.372711163614,3378,29603.31557134399 89771,1113.9454835080371,1325,75471.69811320755,3387,29524.65308532625,3521,28401.022436807725 89716,1114.6283828971423,1325,75471.69811320755,3379,29594.55460195324,3379,29594.55460195324 90205,1108.5859985588381,1323,75585.78987150417,3377,29612.081729345577,3376,29620.853080568722 90092,1109.976468498868,1328,75301.2048192771,3382,29568.30277942046,3379,29594.55460195324 89822,1113.3129968159249,1322,75642.965204236,3385,29542.097488921714,3383,29559.562518474726 89821,1113.3253916122064,1310,76335.87786259541,3380,29585.798816568047,3383,29559.562518474726 89945,1111.7905386625162,1318,75872.53414264036,3379,29594.55460195324,3379,29594.55460195324 89917,1112.1367483345753,1352,73964.49704142011,3381,29577.048210588586,3377,29612.081729345577 90358,1106.7088691648773,1303,76745.97083653108,3377,29612.081729345577,3380,29585.798816568047 90187,1108.8072560346836,1348,74183.9762611276,3387,29524.65308532625,3395,29455.081001472754 90634,1103.3387029150208,1322,75642.965204236,3384,29550.827423167848,3381,29577.048210588586 90148,1109.2869503483162,1331,75131.48009015778,3389,29507.22927117144,3381,29577.048210588586 89767,1113.9951207013714,1321,75700.22710068131,3380,29585.798816568047,3382,29568.30277942046 89910,1112.2233344455567,1321,75700.22710068131,3381,29577.048210588586,3385,29542.097488921714 89852,1112.9412812180028,1316,75987.84194528875,3381,29577.048210588586,3401,29403.116730373422 89537,1116.8567184515898,1319,75815.01137225171,3380,29585.798816568047,3380,29585.798816568047 89763,1114.0447623185498,1331,75131.48009015778,3380,29585.798816568047,3382,29568.30277942046 90070,1110.2475852115022,1325,75471.69811320755,3383,29559.562518474726,3378,29603.31557134399 89771,1113.9454835080371,1302,76804.91551459293,3389,29507.22927117144,3378,29603.31557134399 90518,1104.7526458825869,1325,75471.69811320755,3383,29559.562518474726,3380,29585.798816568047 90314,1107.2480457071995,1322,75642.965204236,3380,29585.798816568047,3384,29550.827423167848 89874,1112.6688474976079,1329,75244.54477050414,3386,29533.372711163614,3379,29594.55460195324 89954,1111.6793027547415,1318,75872.53414264036,3381,29577.048210588586,3381,29577.048210588586 89903,1112.3099340400208,1325,75471.69811320755,3379,29594.55460195324,3388,29515.9386068477 89842,1113.0651588343983,1314,76103.500761035,3382,29568.30277942046,3377,29612.081729345577 89746,1114.2557885588217,1325,75471.69811320755,3378,29603.31557134399,3385,29542.097488921714 93249,1072.3975592231552,1327,75357.95026375283,3381,29577.048210588586,3377,29612.081729345577 93638,1067.9425019756936,1331,75131.48009015778,3377,29612.081729345577,3392,29481.132075471698 87775,1139.2765593847905,1340,74626.86567164179,3379,29594.55460195324,3378,29603.31557134399 86495,1156.136192843517,1271,78678.20613690009,3375,29629.62962962963,3376,29620.853080568722 85584,1168.442699570013,1276,78369.90595611285,3432,29137.529137529138,3376,29620.853080568722 86648,1154.094728095282,1278,78247.2613458529,3382,29568.30277942046,3411,29316.91586045148 85745,1166.2487608606916,1274,78492.93563579278,3380,29585.798816568047,3363,29735.355337496283 85813,1165.3246011676551,1279,78186.08287724786,3375,29629.62962962963,3376,29620.853080568722 85831,1165.0802157728558,1288,77639.75155279503,3376,29620.853080568722,3377,29612.081729345577 85807,1165.4060857505797,1259,79428.11755361399,3466,28851.702250432772,3375,29629.62962962963 85964,1163.2776511097668,1258,79491.2559618442,3378,29603.31557134399,3378,29603.31557134399 85854,1164.7680946723508,1257,79554.49482895785,3382,29568.30277942046,3375,29629.62962962963 85787,1165.6777833471272,1257,79554.49482895785,3377,29612.081729345577,3377,29612.081729345577 85537,1169.084723569917,1272,78616.35220125786,3377,29612.081729345577,3377,29612.081729345577 85408,1170.8505058074186,1271,78678.20613690009,3375,29629.62962962963,3425,29197.080291970804 85577,1168.5382754712132,1261,79302.14115781126,3378,29603.31557134399,3375,29629.62962962963 85663,1167.365140142185,1261,79302.14115781126,3377,29612.081729345577,3378,29603.31557134399 85812,1165.3381811401669,1273,78554.59544383347,3377,29612.081729345577,3378,29603.31557134399 85783,1165.7321380693145,1273,78554.59544383347,3377,29612.081729345577,3376,29620.853080568722 85682,1167.106276697556,1280,78125.0,3381,29577.048210588586,3376,29620.853080568722 85753,1166.1399601180133,1260,79365.07936507936,3379,29594.55460195324,3377,29612.081729345577 85573,1168.5928972923703,1332,75075.07507507507,3377,29612.081729345577,3377,29612.081729345577 86206,1160.0120641254668,1263,79176.56373713381,3376,29620.853080568722,3383,29559.562518474726 85593,1168.31983923919,1264,79113.92405063291,3380,29585.798816568047,3378,29603.31557134399 85903,1164.1036983574495,1261,79302.14115781126,3378,29603.31557134399,3377,29612.081729345577 85516,1169.3718134618082,1277,78308.53563038372,3375,29629.62962962963,3376,29620.853080568722 85553,1168.8660830128692,1291,77459.3338497289,3490,28653.295128939826,3377,29612.081729345577 85550,1168.907071887785,1293,77339.52049497294,3379,29594.55460195324,3379,29594.55460195324 85610,1168.0878402055835,1298,77041.60246533128,3384,29550.827423167848,3378,29603.31557134399 85522,1169.2897733916418,1267,78926.59826361484,3379,29594.55460195324,3379,29594.55460195324 85595,1168.2925404521293,1276,78369.90595611285,3379,29594.55460195324,3376,29620.853080568722 85451,1170.2613193526115,1286,77760.49766718507,3376,29620.853080568722,3391,29489.82601002654 85792,1165.609847071988,1252,79872.20447284346,3382,29568.30277942046,3376,29620.853080568722 86501,1156.0559993526085,1255,79681.2749003984,3379,29594.55460195324,3379,29594.55460195324 85718,1166.616113301757,1269,78802.20646178094,3382,29568.30277942046,3376,29620.853080568722 85605,1168.156065650371,1265,79051.38339920949,3378,29603.31557134399,3380,29585.798816568047 85398,1170.9876109510762,1274,78492.93563579278,3377,29612.081729345577,3395,29455.081001472754 86370,1157.809424568716,1273,78554.59544383347,3376,29620.853080568722,3376,29620.853080568722 85905,1164.0765962400326,1280,78125.0,3379,29594.55460195324,3379,29594.55460195324 86020,1162.5203441060219,1285,77821.01167315176,3375,29629.62962962963,3376,29620.853080568722 85726,1166.5072440099852,1272,78616.35220125786,3380,29585.798816568047,3380,29585.798816568047 85628,1167.8422945765403,1270,78740.15748031496,3379,29594.55460195324,3376,29620.853080568722 85989,1162.93944574306,1258,79491.2559618442,3376,29620.853080568722,3378,29603.31557134399 85981,1163.047650062223,1276,78369.90595611285,3376,29620.853080568722,3376,29620.853080568722 86558,1155.2947156819703,1269,78802.20646178094,3385,29542.097488921714,3378,29603.31557134399 85745,1166.2487608606916,1293,77339.52049497294,3378,29603.31557134399,3375,29629.62962962963 85544,1168.9890582624148,1266,78988.94154818325,3376,29620.853080568722,3377,29612.081729345577 85536,1169.0983913206135,1268,78864.35331230283,3380,29585.798816568047,3380,29585.798816568047 85477,1169.9053546568082,1278,78247.2613458529,3388,29515.9386068477,3377,29612.081729345577 85434,1170.4941826439124,1253,79808.45969672786,3378,29603.31557134399,3375,29629.62962962963 85609,1168.1014846569872,1276,78369.90595611285,3364,29726.516052318668,3376,29620.853080568722 85740,1166.316771635176,1258,79491.2559618442,3377,29612.081729345577,3377,29612.081729345577 85640,1167.6786548341897,1266,78988.94154818325,3378,29603.31557134399,3377,29612.081729345577 85648,1167.569587147394,1281,78064.012490242,3378,29603.31557134399,3376,29620.853080568722 85697,1166.9019919017,1287,77700.0777000777,3377,29612.081729345577,3378,29603.31557134399 85696,1166.9156086631815,1256,79617.83439490446,3379,29594.55460195324,3376,29620.853080568722 85782,1165.7457275419085,1258,79491.2559618442,3379,29594.55460195324,3379,29594.55460195324 85837,1164.9987767512844,1264,79113.92405063291,3379,29594.55460195324,3376,29620.853080568722 85632,1167.7877428998504,1278,78247.2613458529,3380,29585.798816568047,3459,28910.089621277824 85517,1169.3581393173288,1256,79617.83439490446,3379,29594.55460195324,3380,29585.798816568047 85990,1162.925921618793,1302,76804.91551459293,3380,29585.798816568047,3377,29612.081729345577 86690,1153.535586572846,1281,78064.012490242,3375,29629.62962962963,3381,29577.048210588586 86045,1162.1825788831425,1274,78492.93563579278,3380,29585.798816568047,3383,29559.562518474726 86146,1160.820003250296,1274,78492.93563579278,3382,29568.30277942046,3418,29256.87536571094 86027,1162.4257500552153,1280,78125.0,3382,29568.30277942046,3381,29577.048210588586 85992,1162.8988743138896,1281,78064.012490242,3376,29620.853080568722,3380,29585.798816568047 85857,1164.727395553071,1288,77639.75155279503,3382,29568.30277942046,3376,29620.853080568722 85853,1164.7816616775185,1284,77881.6199376947,3375,29629.62962962963,3374,29638.41138114997 86069,1161.8585088707896,1295,77220.07722007722,3378,29603.31557134399,3378,29603.31557134399 85842,1164.930919596468,1296,77160.49382716049,3378,29603.31557134399,3376,29620.853080568722 86195,1160.160102094089,1301,76863.95080707148,3376,29620.853080568722,3379,29594.55460195324 85523,1169.2761011657683,1305,76628.35249042146,3376,29620.853080568722,3378,29603.31557134399 85752,1166.1535591006625,1275,78431.37254901961,3374,29638.41138114997,3377,29612.081729345577 85441,1170.3982865369085,1286,77760.49766718507,3377,29612.081729345577,3380,29585.798816568047 85566,1168.6884977678048,1265,79051.38339920949,3377,29612.081729345577,3380,29585.798816568047 85523,1169.2761011657683,1267,78926.59826361484,3377,29612.081729345577,3376,29620.853080568722 86152,1160.7391586962578,1285,77821.01167315176,3374,29638.41138114997,3378,29603.31557134399 85684,1167.0790345922226,1272,78616.35220125786,3378,29603.31557134399,3384,29550.827423167848 86252,1159.3934053703103,1271,78678.20613690009,3376,29620.853080568722,3377,29612.081729345577