theash2473
theash2473
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 11/15/2024 in #java-help
Adding character/s to a string has two different variations.
If you add two characters to a string you get an int as an output but when you add one character it works. Please explain. Here is an example with output for reference.
String str = "eeksforGeeks";

// Inserting at the beginning
String str2 ='[' +'['+ str;

// Print and display the above string
System.out.println(str2);
str2='['+str;
System.out.println(str2);
String str = "eeksforGeeks";

// Inserting at the beginning
String str2 ='[' +'['+ str;

// Print and display the above string
System.out.println(str2);
str2='['+str;
System.out.println(str2);
Output-: 182eeksforGeeks [eeksforGeeks
14 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/27/2024 in #java-help
I am getting TLE in Leetcode on question 29. Please help me optimize.
I am using the most efficient code that I could understand. Please suggest how to optimize it for the test case where the dividend is int_max and divisor is -1.
public int divide(int dividend, int divisor) {
// if(di(1<<31))
// if ((dividend < 0) ^ (divisor < 0)) {
// negative = 1;
// dividend = Math.abs(dividend);
// divisor = Math.abs(divisor);
// } else if ((dividend < 0) && (divisor < 0)) {
// dividend = Math.abs(dividend);
// divisor = Math.abs(divisor);
// }
// while (dividend >= divisor) {
// dividend -= divisor;
// answer++;
// }
// if (negative == 1) {
// answer *= -1;
// }
// ;
// return answer;
int answer = 0, negative = 0;
if ((dividend < 0) ^ (divisor < 0))
negative = 1;
if ((dividend == (1 << 31)) && (divisor == -1))
answer = ((1 << 31) - 1);
else {
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
while (dividend > divisor) {
int temp_divisor = divisor, counter = 0;
for (; dividend >= temp_divisor; counter++) {
temp_divisor = temp_divisor << 1;
}
counter--;
temp_divisor = temp_divisor >> 1;
dividend -= temp_divisor;
answer += (Math.pow((int) 2, (int) counter));
System.out.println(temp_divisor);
System.out.println(dividend);
System.out.println(answer);
System.out.println(counter + "\n");
}
}
if (negative == 1) {
answer *= -1;
}
return answer;
}
public int divide(int dividend, int divisor) {
// if(di(1<<31))
// if ((dividend < 0) ^ (divisor < 0)) {
// negative = 1;
// dividend = Math.abs(dividend);
// divisor = Math.abs(divisor);
// } else if ((dividend < 0) && (divisor < 0)) {
// dividend = Math.abs(dividend);
// divisor = Math.abs(divisor);
// }
// while (dividend >= divisor) {
// dividend -= divisor;
// answer++;
// }
// if (negative == 1) {
// answer *= -1;
// }
// ;
// return answer;
int answer = 0, negative = 0;
if ((dividend < 0) ^ (divisor < 0))
negative = 1;
if ((dividend == (1 << 31)) && (divisor == -1))
answer = ((1 << 31) - 1);
else {
dividend = Math.abs(dividend);
divisor = Math.abs(divisor);
while (dividend > divisor) {
int temp_divisor = divisor, counter = 0;
for (; dividend >= temp_divisor; counter++) {
temp_divisor = temp_divisor << 1;
}
counter--;
temp_divisor = temp_divisor >> 1;
dividend -= temp_divisor;
answer += (Math.pow((int) 2, (int) counter));
System.out.println(temp_divisor);
System.out.println(dividend);
System.out.println(answer);
System.out.println(counter + "\n");
}
}
if (negative == 1) {
answer *= -1;
}
return answer;
}
12 replies
JCHJava Community | Help. Code. Learn.
Created by theash2473 on 10/18/2024 in #java-help
In the bitwise compliment operator there is confusion. Please help if you can.
For the 2’s complement, if we take 8 bits for a binary representation, in the case of decimal 200, binary is 11001000 2’s complement is also 11001000 In the procedure to calculate mentioned in the link https://www.rit.edu/academicsuccesscenter/sites/rit.edu.academicsuccesscenter/files/documents/math-handouts/DM3_TwosComplement_BP_9_22_14.pdf, it should be 00111000 The confusion must be because of the 8th bit being a 1.
17 replies