Skip to content

Arrays

Introduction

Each problem on the platform includes a direct solution, while others solely outline the approach within a function.


the Approach:

  • convert the given num to binary using the helper function

  • iterate through each digit and check if it is 1

  • and check if the ingap is True then modify the maxGap and make the curr_gap = 0 because we reached ano0ther 1

  • else make the ingap as True cuz we entered the gap

  • increase the curr_gap by 1 until we reach other 1

  • return the max_gap

Solution

Source code in algorithms/arrays/binarygap.py
class Solution:
    def binGap(self,num):
        """
        Args:
            num int : Input
            max_gap int : Output
        """

        bin_num = self.helper(num)
        max_gap = 0
        curr_gap = 0
        ingap = False

        for dig in bin_num:
            if dig == '1':
                if ingap:
                    max_gap = max(max_gap, curr_gap)
                    curr_gap = 0
                else:
                    ingap = True
            else:
                curr_gap +=1
        return max_gap

    # used for converting the int to binary
    def helper(self,num):

            bin_num = ""
            while num > 0:

                dec = num%2
                bin_num = str(dec) + bin_num
                num = num//2

            return bin_num

binGap(num)

Parameters:

Name Type Description Default
num int

Input

required
max_gap int

Output

required
Source code in algorithms/arrays/binarygap.py
def binGap(self,num):
    """
    Args:
        num int : Input
        max_gap int : Output
    """

    bin_num = self.helper(num)
    max_gap = 0
    curr_gap = 0
    ingap = False

    for dig in bin_num:
        if dig == '1':
            if ingap:
                max_gap = max(max_gap, curr_gap)
                curr_gap = 0
            else:
                ingap = True
        else:
            curr_gap +=1
    return max_gap

Given array of integers: we have to find the element in the array we part the array into 2 if target greater than middle then we do the same thing to the right list if target is less than middle then we consider the left list

Solution

Bases: object

Source code in algorithms/arrays/binarysearch.py
class Solution(object):
    def binarysearch(self, nums, target):
        """
        Args:
            nums List[int] : Input array
            target int : element to be searched
            target_idx int : Position of target

        """
        left = 0
        right = len(nums)-1

        while left <=right:
            mid = (left + right)//2
            if target == nums[mid]:
                return mid
            elif target > nums[mid]:
                left = mid+1
            else:
                right = mid-1

        return -1

binarysearch(nums, target)

Parameters:

Name Type Description Default
nums List[int]

Input array

required
target int

element to be searched

required
target_idx int

Position of target

required
Source code in algorithms/arrays/binarysearch.py
def binarysearch(self, nums, target):
    """
    Args:
        nums List[int] : Input array
        target int : element to be searched
        target_idx int : Position of target

    """
    left = 0
    right = len(nums)-1

    while left <=right:
        mid = (left + right)//2
        if target == nums[mid]:
            return mid
        elif target > nums[mid]:
            left = mid+1
        else:
            right = mid-1

    return -1