Posts

Showing posts from August, 2022

Plus one Easy Python solution

 class Solution:     def plusOne(self, digits: List[int]) -> List[int]:         s=''         for i in digits:             s+=str(i)         s=int(s)+1         digits.clear()         digits=[int(i) for i in str(s)]         return digits

Search insert position in python using binary search

 class Solution:     def searchInsert(self, nums: List[int], target: int) -> int:         res=0         l=0         r=len(nums)         while(l<r):             m=(l+r)//2             if nums[m]==target:                 res=m                 break             elif target>nums[l]:                 l+=1                 res=l             else:                 r-=1         return res

Remove element easy leetcode solution

 class Solution:     def removeElement(self, nums: List[int], val: int) -> int:         k=0         j=0         for i in range(len(nums)):             if val!=nums[i]:                 k+=1                 nums[j]=nums[i]                 j+=1                          return k

Left rotation in array interview preparation

  #!/bin/python3 import   math import   os import   random import   re import   sys # # Complete the 'rotLeft' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts following parameters: #  1. INTEGER_ARRAY a #  2. INTEGER d # def   rotLeft ( a ,   d ):      # Write your code here      temp = []      i = 0      while   i < d :          temp . append ( a [ i ])          i = i + 1      i = 0      while   d < len ( a ):          a [ i ] = a [ d ]          i = i + 1          d = d + 1      a [:] = a [: i ] + temp        return   a   if  __ name__  ==  '__main__' :      fptr  =  open ( os . environ [ 'OUTPUT_PATH' ],   'w' )      first_multiple_input  =  input () . rstrip () . split ()      n  =  int ( first_multiple_input [ 0 ])      d  =  int ( first_multiple_input [ 1 ])      a  =  list ( map ( int ,   input () . rstrip () . split ()))      result  =  rotLeft ( a ,   d )      fptr . write ( ' ' . join