BruteForce Faster than 100% | No Formula | Fooling Leetcode Judge | Static | Accepted
No Formula, No Nothing, Plain brute force with a twist.  \n\nSo my first idea to run a bruteforce gave me TLE. I tried to think but did not get any clever solut
No Formula, No Nothing, Plain brute force with a twist.  \n\nSo my first idea to run a bruteforce gave me TLE. I tried to think but did not get any clever solutions, so being lazy to think i fooled leetcode judge like this.\n\nPrecomputed all the binary strings using a static array. This array will only be computed once and will be used commonly by all the testcases and the bruteforce solution passes.\n\n```\nclass Solution {\n\nstatic String arr[] = new String[100001];\n\npublic void build() {\n\tfor(int i=1; i<=100000; i++) {\n\t\tarr[i] = Integer.toBinaryString(i);\n\t}\n}\n\nint mod = 1000000007;\n\npublic int getVal(int n) {\n\tint val = 0;\n\tfor(int i=1; i<=n; i++) {\n\t\tfor(int j=0; j<arr[i].length(); j++) {\n\t\t\tint x = arr[i].charAt(j)-\'0\';\n\t\t\tval = (val*2 + x)%mod;\n\t\t}\n\t}\n\treturn val;\n}\n\npublic int concatenatedBinary(int n) {\n\tif(arr[1]==null) {\n\t\tbuild();\n\t}\n\treturn getVal(n);\n}\n}\n```\n\nUpdate : Faster than 100%\n\nNow along with computing binary string I added 3 line modification to calculate the answers as well. This makes the solution faster than 100% of the submissions, taking run time from 1600 ms to 24 ms\n\n![image](https://assets.leetcode.com/users/images/dcd46e72-933a-4e57-ad9c-cefbe6ef4e7e_1663896534.6957574.png)\n\nCode \n\nclass Solution {\n    \n    static String arr[] = new String[100001];\n    \n    static int ans[] = new int[100001];\n    \n    public void build() {\n        for(int i=1; i<=100000; i++) {\n            arr[i] = Integer.toBinaryString(i);\n        }\n    }\n    \n    int mod = 1000000007;\n    \n    public int getVal(int n) {\n        int val = 0;\n        for(int i=1; i<=n; i++) {\n            for(int j=0; j<arr[i].length(); j++) {\n                int x = arr[i].charAt(j)-\'0\';\n                val = (val*2 + x)%mod;\n            }\n            ans[i] = val;\n        }\n        return val;\n    }\n    \n    public int concatenatedBinary(int n) {\n        if(arr[1]==null) {\n            build();\n            getVal(100000);\n        }\n        return ans[n];\n    }\n}\n
[]