Mastering Array Basics in 10-15 Days

Mastering Array Basics in 10-15 Days

Hi All In last 10-15 days I cover all array basics like how to create array , how to insert a element inside array , how to modify a array , how to delete a element from array . Basics concepts of find and Search concept and I solve below problems make above for hasnode

Certainly! Here's a more professional version for your communication on Hasnode:

Hi All,

In the last 10-15 days, I have covered all the basic concepts related to arrays. This includes:

  • Creating arrays

  • Inserting elements into arrays

  • Modifying arrays

  • Deleting elements from arrays

Additionally, I have learned the fundamental concepts of finding and searching within arrays. Below are the problems I have solved during this period:

  1. Array Insertion:

    • I have learned about the traversing technique. For example, if we need to insert an element in the middle of the array, we first create a blank space at the end, then shift the elements from the last position. Once we reach the desired position, we insert our element.

    •     class Solution:
              def ArrInssertion(self,arr,p,num):
                  arr.append("None")
                  for i in range(len(arr)-1,p-1,-1):
                      arr[i]=arr[i-1]
                      if i ==p:
                          arr[i]=num
                          break;
                  return arr
      
          s=Solution()
          print(s.ArrInssertion([1,2,4,8,7,3,9,5,12],4,56))
      
  2. Array Deletion:

    Just like array insertion, once we reach the desired position, we replace the element at that position with the element from the next position. In this case, we do not create any extra space at the end..

    1.  class Solution:
           def DeletionInssertion(self,arr,p):
               # arr.append("None")
               print(arr)
               for i in range(p,len(arr)-1):
                   arr[i]=arr[i+1]
               return arr
      
       s=Solution()
      
       print(s.DeletionInssertion([1,2,4,8,7,3,9,5,12],4))
      
  1. Maximum Element In Array and Min Element In Array:

    1. From this solution, I learned the concept of comparing the first index element with others. First, I store the first element in a variable called max. Then, I compare this max variable with each index. If any index is greater than the max variable, I update the max variable with the current index value.

       class Solution:
           def findLargestElement(self, nums):
               max=nums[0]
               for i in nums:
                   if i>=max:
                       max=i
               return max
      
       s=Solution()
       print(s.maxelement([2,3,14,4,2,1,9,6,0]))
      
    class Solution:
        def findSmallest(self, nums):
            # Your code goes here
                min=nums[0]
                for i in nums:
                    if i<=min:
                        min=i
                return min
    s=Solution()
    print(s.findSmallest([-10,1,5,6,-11]))
  1. Remove Duplicates from Sorted Array .:

    Here I learned that if we encounter a problem like this, we follow an approach called new space creation. This means we create a new array and then push each element from the given array one by one. If the element is already present in the new array, we do not insert it. If it is not present, we insert it.

  1. Largest and Second Largest Element in an Array:

    From this problem, I learned the concept of comparing elements. Here, we initialize two variables: one for the largest value, max, and another for the second largest, sec_max. We compare each element to the max value. If an element is greater than max, we assign the max value to sec_max and the current element to max. If not, we compare the current element with sec_max. If it is greater than sec_max, we update sec_max.

class Solution:
    def secondLargest(self, nums) -> int:
        # Your code goes here
            max=float('-inf')
            sec=float('-inf')
            for i in range (0,len(nums)):
                if nums[i]>max:
                    sec=max
                    max=nums[i]
                elif nums[i]>sec and nums[i]!=max:
                    sec=nums[i]

            return sec



s=Solution()
print(s.secondLargest([2,1,4,2,6,3,7,9,15,21,20]))
  1. Reverse an array:

    1. From this problem, I learned the concept called the two-pointer approach. One variable points to the last index, and another points to the first index. We swap their values, then increase the first index and decrease the last index until the first index is less than the last index.

       class Solution:
           def reverseArray(self, arr):
                   i=0
                   j=len(arr)-1
                   print(arr)
                   while i<j:
                       arr[i],arr[j]=arr[j],arr[i]
                       i +=1
                       j -=1
      
                   return arr
       s=Solution()
       print(s.reverseArray([2,5,8,9,6,4]))
      
  2. Move Zero:

    Using the same two-pointer approach, we have one pointer check if the element is non-zero. If it is, we swap it with a zero element.

     class Solution:
         def moveZeros(self, nums):
             # Your code goes here
                 i=0
                 j=0
                 for i in range(0,len(nums)):
                     if nums[i]!=0 :
                         nums[i],nums[j]=nums[j],nums[i]
                         j=j+1
                 return nums
     s=Solution()
     print(s.moveZeros([0,1,0,0,2,0,3,0,0,0,0,12]))
    

Please check the GitHub link below for all the problems I solved while learning the basics of arrays.

https://github.com/r-rajesh4/DSA-Coding/tree/main/ArrayBasics