deatiled explanation on how to handle negative base and REASON behind it
Basic algo of conversion here if given number is n\n\n1.Devide the number n by -2 \n2.Get remainder and multiply it with 10 here as int is decimal or add \nrema
Basic algo of conversion here if given number is n\n\n1.Devide the number n by -2 \n2.Get remainder and multiply it with 10 here as int is decimal or add \nremainder to string from backward to forward\n3.now make your number n=n/-2 to get next digits and do same steps as above 1 and 2 but with every iteration increase power of 10 by 1 to make a digit\n\nNow here is the catch since base -2 is negative if remainder comes out negative we need to subtract -2 from remainder to make it more towards positive until it comes out to be positive\nWHY??????\nBecause when we devide a number we want our remainder to be lesser and quotient to be greater but according to wikipedia calculation part remainder cant be negative https://en.wikipedia.org/wiki/Negative_base#Calculation\n\nSo what we want \n1.less remainder and positive remainder\n2.greater quotient\n\nTherefore we will subtract -2 from r and add +1 to quotient\nhere is an example in simple terms:\nif we want to devide 20 by 8\n\nand quotient comes 1 so remainder will come \n20/8 ; q=1 r=12\n\nbut we want remainder to be lesser \nso we will subtract 6 from 12 and and 1 to q \nso r=12-8=4\n   q=1+1=2\nand that is what we wanted\n\n\n\n\n\nclass Solution {\n    public String baseNeg2(int n) {\n    //create a destination number\n   \n        int dn = 0;\n          \n        String s = "";\n   //if input is 0 return 0  \n   \n        if(n==0){\n            return "0";\n        }\n       \n        while(n !=0){\n            \n            int r = n% -2;\n                n = n/-2;\n            \n            \n               if(r<0){\n               r = r - (-2);\n               n = n + 1;\n             }\n            \n//add the remainder  \n\n            s  = s + r;\n             }\n           StringBuilder sb = new StringBuilder(s);\n           sb.reverse();\n        return sb.toString();\n       \n    \n    }\n}
['Java']