Posts

Showing posts from February, 2024

Linux Basic Commands

  ls Lists a directory’s content pwd Shows the current working directory’s path cd Changes the working directory mkdir Creates a new directory rm Deletes a file cp Copies files and directories, including their content mv Moves or renames files and directories touch Creates a new empty file file Checks a file’s type zip and unzip Creates and extracts a ZIP archive tar Archives files without compression in a TAR format nano, vi, and jed Edits a file with a text editor cat Lists, combines, and writes a file’s content as a standard output grep Searches a string within a file sed Finds, replaces, or deletes patterns in a file head Displays a file’s first ten lines tail Prints a file’s last ten lines awk Finds and manipulates patterns in a file sort Reorders a file’s content cut Sections and prints lines from a file diff Compares two files’ content and their differences tee Prints command outputs in Terminal and a file locate Finds files in a system’s database find Outputs a file or fold...

Array

  A  subarray  of an  -element array is an array composed from a contiguous block of the original array's elements. For example, if  , then the subarrays are  ,  ,  ,  ,  , and  . Something like   would  not  be a subarray as it's not a contiguous subsection of the original array. The  sum  of an array is the total sum of its elements. An array's sum is  negative  if the total sum of its elements is negative. An array's sum is  positive  if the total sum of its elements is positive. Given an array of   integers, find and print its number of  negative subarrays  on a new line. Input Format The first line contains a single integer,  , denoting the length of array  . The second line contains   space-separated integers describing each respective element,  , in array  . Constraints Output Format Print the number of subarrays of  ...

Three Sum Problem

 Given an array and a target. You need to find the triplet such that a[i] + a[j] + a[k] = target and  a[i] != a[j] != a[k]. Note: Solution must contain unique triplets. eg: Number of element in array is 7 array = [7, -6, 3, 8, -1, 8, -11] target = 0 Solution: [-11, 3, 8] [-6, -1, 7]  Time complexity of brut force solution is O(n^3).  Optimisation 1: Using Hashmap We can have some optimisation with hashmap. such that a[k] = target - (a[i] + a[j]) we need to find all the pairs of a[i] and a[j] and time complexity is O(n^2) and space complexity is O(m) + O(n) Optimisation: Two Pointer Approach a = [-1, -1, 1, 2, 2, 2, 2, 3, 3, 4, 4, 6] and target = 6.  Here array is sorted. a[i] = -1 a[j] = -1 a[k] = 6 when j is less than k and calculate new target  (tempTarget)  int tempTarget = target - a[i] ; target - a[i] = a[j] + a[k] case 1: 6 - (-1)  = -1 + 6               7 not equal 5 case 2: a[j] = 1 6 - (-1)  = ...