❌

Normal view

There are new articles available, click to refresh the page.
Yesterday β€” 14 April 2025Main stream

67. Add Binary

This problem is like add String number in String.

Description :

Given two binary stringsΒ aΒ andΒ b, returnΒ their sum as a binary string.

Example 1:

Input: a = "11", b = "1"
Output: "100"

Example 2:

Input: a = "1010", b = "1011"
Output: "10101"

Algorithm:

  1. Add numbers from end to start so, initials i =a.length() -1 and j=a.length()-1.
  2. Intialize the carry =0 as the remaining number.
  3. use Stringbuffer for the mutable property in string.
  4. Now Add the continuously,
    • carry += num1.charAt(i) and i– upto i>=0.
    • carry += num2.charAt(j) and j– upto j>=0.
    • stringBuffer. append(carry%10).
    • carry /= 10.
  5. then reverse the stringBuffer by st.reverse().

Code :

int carry =0,i=a.length()-1,j=b.length()-1;
        StringBuffer sb =new StringBuffer();
        while(i>=0 || j>=0 || carry > 0)
        {
        	if(i>=0)
        		carry += a.charAt(i--)-'0';
        	if(j>=0)
        		carry += b.charAt(j--)-'0';
        	
        	sb.append(carry%2);
        	carry /=2;
        	
        }
        return sb.reverse().toString();

Reference :

  1. https://leetcode.com/problems/add-binary/description/

415. Add Strings

LeetCode number 415, String.

This problem is like add binary number in String.

Description:

Given two non-negative integers, num1 and num2 represented as string, return the sum of num1 and num2 as a string.

You must solve the problem without using any built-in library for handling large integers (such as BigInteger). You must also not convert the inputs to integers directly.

Input / Ouput :

Example 1:

Input: num1 = "11", num2 = "123"
Output: "134"

Example 2:

Input: num1 = "456", num2 = "77"
Output: "533"

Example 3:

Input: num1 = "0", num2 = "0"
Output: "0"

Algorithm:

  1. Add numbers from end to start so, initials i =num1.length() -1 and j=num2.length()-1.
  2. Intialize the carry =0 as the remaining number.
  3. use Stringbuffer for the mutable property in string.
  4. Now Add the continuously,
    • carry += num1.charAt(i) and i– upto i>=0.
    • carry += num2.charAt(j) and j– upto j>=0.
    • stringBuffer. append(carry%10).
    • carry /= 10.
  5. then reverse the stringBuffer by st.reverse().

code :

  int i=num1.length()-1, j=num2.length()-1, carry=0;
        StringBuffer sb = new StringBuffer();
        while(i>=0 || j>=0 || carry > 0)
        {
        	// condtion for string out of boundary i.e means i should less then 0
        	if(i>=0)
        		carry += num1.charAt(i--) - '0';
        	
        	if(j>=0)
        		carry += num2.charAt(j--) - '0';
        	sb.append(carry%10);
        	carry /= 10;
        }
        
		return sb.reverse().toString();

Reference :

https://leetcode.com/problems/add-strings

Before yesterdayMain stream

POTD #4 – Search in a sorted Matrix | Geeks For Geeks

24 December 2024 at 06:29

Problem Statement

Geeks For Geeks – https://www.geeksforgeeks.org/problems/search-in-a-matrix-1587115621/1

Given a strictly sorted 2D matrix mat[][] of size n x mΒ anda numberΒ x. Find whether the number x is present in the matrix or not.


Note: In a strictly sorted matrix, each row is sorted in strictly increasing order, andΒ the first element of the ithΒ row (i!=0) is greater than the last element of the (i-1)thΒ row.


Input: mat[][] = [[1, 5, 9], [14, 20, 21], [30, 34, 43]], x = 14
Output: true
Explanation: 14 is present in the matrix, so output is true.

My Approach

Today’s problem is same as yesterday’s problem.


class Solution:
    
    def binary_search(self, arr, x, start, stop):
        if start > stop:
            return False
        mid = (start + stop) // 2
        if start == stop and arr[start] != x:
            return False
        if arr[mid] == x:
            return True
        elif arr[mid] > x:
            return self.binary_search(arr, x, start, mid)
        else:
            return self.binary_search(arr, x, mid+1, stop)
    
    #Function to search a given number in row-column sorted matrix.
    def searchMatrix(self, mat, x): 
    	# code here 
    	length = len(mat[0]) - 1
        for arr in mat:
            result = self.binary_search(arr, x, 0, length)
            if result:
                return True
        return False
    	

❌
❌