
Annapoorani KadhiravanJAVA Two-Pointer (Manual Binary Addition) This approach simulates binary addition bit by...
Two-Pointer (Manual Binary Addition)
This approach simulates binary addition bit by bit without converting to decimal, making it suitable for large inputs.
Algorithm
class GFG {
public static String addBinary(String x, String y) {
int i = x.length() - 1;
int j = y.length() - 1;
int carry = 0;
StringBuilder result = new StringBuilder();
while (i >= 0 || j >= 0) {
int sum = carry;
if (i >= 0) sum += x.charAt(i--) - '0';
if (j >= 0) sum += y.charAt(j--) - '0';
result.append(sum % 2);
carry = sum / 2;
}
if (carry == 1) {
result.append(1);
}
return result.reverse().toString();
}
public static void main(String[] args) {
String x = "011011";
String y = "1010111";
System.out.println(addBinary(x, y));
}
}
Output
1110010
Key Points:
Built-in methods are simpler but limited by integer range.
Two-pointer approach is preferred for large binary strings.
Manual addition avoids overflow and improves reliability.
StringBuilder improves performance over string concatenation.
The SQL ORDER BY
The ORDER BY keyword is used to sort the result-set in ascending or descending order.
The ORDER BY keyword sorts the result-set in ascending order (ASC) by default.
Example
Sort the products from lowest to highest price:
SELECT * FROM Products
ORDER BY Price;
ORDER BY Syntax
SELECT column1, column2, ...
FROM table_name
ORDER BY column1, column2, ... ASC|DESC;
ORDER BY DESC
To sort the records in descending order, use the DESC keyword.
Example
Sort the products from highest to lowest price:
SELECT * FROM Products
ORDER BY Price DESC;
References :
https://www.w3schools.com/sql/sql_orderby.asp
https://www.geeksforgeeks.org/java/java-program-to-add-two-binary-strings/