Skip to main content

Posts

Showing posts with the label array

Full Stack Developer Without Degree: How to Become One

If you're interested in becoming a Full Stack Developer without a degree, we've put together some tips and resources to help you get started. Learn the Fundamentals The first step to becoming a Full Stack Developer is to learn the fundamentals of coding. You can start with HTML, CSS, and JavaScript, which are the building blocks of web development. There are plenty of free resources available online, such as Codecademy and FreeCodeCamp, which offer interactive courses that teach you the basics of web development. Once you have a solid understanding of the basics, you can move on to more advanced topics such as back-end development, databases, and frameworks. You can learn these topics through online courses or by working on personal projects. Build a Portfolio One of the most important things you can do as a Full Stack Developer is to build a portfolio. Your portfolio should showcase your skills and experience and demonstrate your ability to build real-world applications. You c...

Stock buy and sell (DSA)

  Problem Statement :(👈click here for GFG) The cost of stock on each day is given in an array  A [] of size  N . Find all the segments of days on which you buy and sell the stock so that in between those days  for which profit can be generated . Note:   Since there can be multiple solutions ,  the driver code will print 1 if your answer is correct, otherwise, it will return 0. In case there's no profit the driver code will print the string " No Profit " for a correct solution. Example 1: Input: N = 7 A[] = {100,180,260,310,40,535,695} Output: 1 Explanation: One possible solution is (0 3) (4 6) We can buy stock on day 0, and sell it on 3rd day, which will give us maximum profit. Now, we buy stock on day 4 and sell it on day 6. Example 2: Input: N = 5 A[] = {4,2,2,2,4} Output: 1 Explanation: There are multiple possible solutions. one of them is (3 4) We can buy stock on day 3, and sell it on 4th day, which will give us maximum profit. Your Task...

Maximum of all subarrays of size k

  Problem Statement :(👈click here for GFG) Given an array arr[] of size N and an integer K. Find the maximum for each and every contiguous subarray of size K. Example 1: Input: N = 9, K = 3 arr[] = 1 2 3 1 4 5 2 3 6 Output: 3 3 4 5 5 5 6 Explanation: 1st contiguous subarray = {1 2 3} Max = 3 2nd contiguous subarray = {2 3 1} Max = 3 3rd contiguous subarray = {3 1 4} Max = 4 4th contiguous subarray = {1 4 5} Max = 5 5th contiguous subarray = {4 5 2} Max = 5 6th contiguous subarray = {5 2 3} Max = 5 7th contiguous subarray = {2 3 6} Max = 6 Example 2: Input: N = 10, K = 4 arr[] = 8 5 10 7 9 4 15 12 90 13 Output: 10 10 10 15 15 90 90 Explanation: 1st contiguous subarray = {8 5 10 7}, Max = 10 2nd contiguous subarray = {5 10 7 9}, Max = 10 3rd contiguous subarray = {10 7 9 4}, Max = 10 4th contiguous subarray = {7 9 4 15}, Max = 15 5th contiguous subarray = {9 4 15 12}, Max = 15 6th contiguous subarray = {4 15 12 90}, Max = 90 7th contiguous subarray = {15 12 90 13}, Max = ...

Count pairs with given sum (DSA)

  Problem Statement: (👈click here for GFG) Given an array of  N  integers, and an integer  K , find the number of pairs of elements in the array whose sum is equal to  K . Example 1: Input: N = 4, K = 6 arr[] = {1, 5, 7, 1} Output: 2 Explanation: arr[0] + arr[1] = 1 + 5 = 6 and arr[1] + arr[3] = 5 + 1 = 6. Example 2: Input: N = 4, K = 2 arr[] = {1, 1, 1, 1} Output: 6 Explanation:   Each 1 will produce sum 2 with any 1. Your Task: You don't need to read input or print anything. Your task is to complete the function getPairsCount() which takes arr[], n and k as input parameters and returns the number of pairs that have sum K. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 1 <= N <= 105 1 <= K <= 108 1 <= Arr[i] <= 106 Solution: class Solution { getPairsCount(arr,n,k){ //code here let count = 0; let hash = {}; for(let i = 0; i < n; i++){ ...

Trapping Rain Water (DSA)

  Problem Statement: (👈click here for GFG) Given an array  arr[]  of  N  non-negative integers representing the height of blocks. If width of each block is 1, compute how much water can be trapped between the blocks during the rainy season.    Example 1: Input: N = 6 arr[] = {3,0,0,2,0,4} Output: 10 Explanation: Example 2: Input: N = 4 arr[] = {7,4,0,9} Output: 10 Explanation: Water trapped by above block of height 4 is 3 units and above block of height 0 is 7 units. So, the total unit of water trapped is 10 units. Example 3: Input: N = 3 arr[] = {6,9,9} Output: 0 Explanation: No water will be trapped. Your Task: You don't need to read input or print anything. The task is to complete the function trappingWater() which takes arr[] and N as input parameters and returns the total amount of water that can be trapped. Expected Time Complexity: O(N) Expected Auxiliary Space: O(N) Constraints: 3 < N < 106 0 < Ai < 108 Solut...

Count Inversions (DSA)

  Problem Statement:(👈click here for GFG) Given an array of integers. Find the Inversion Count in the array.  Inversion Count: For an array, inversion count indicates how far (or close) the array is from being sorted. If array is already sorted then the inversion count is 0. If an array is sorted in the reverse order then the inversion count is the maximum.  Formally, two elements a[i] and a[j] form an inversion if a[i] > a[j] and i < j. Example 1: Input : N = 5, arr[] = {2, 4, 1, 3, 5} Output : 3 Explanation : The sequence 2, 4, 1, 3, 5 has three inversions (2, 1), (4, 1), (4, 3). Example 2: Input : N = 5 arr[] = {2, 3, 4, 5, 6} Output : 0 Explanation : As the sequence is already sorted so there is no inversion count. Example 3: Input : N = 3, arr[] = {10, 10, 10} Output : 0 Explanation : As all the elements of array are same, so there is no inversion count. Your Task: You don't need to read input or print anything. Your task is to complete the function inversi...

Find duplicates in an array (DSA)

  Problem statement :(👈click here for GFG) Given an array a[] of size N which contains elements from 0 to N-1, you need to find all the elements occurring more than once in the given array. Note: The extra space is only for the array to be returned. Try and perform all operations within the provided array.  Example 1: Input: N = 4 a[] = {0,3,1,2} Output: -1 Explanation: N=4 and all elements from 0 to (N-1 = 3) are present in the given array. Therefore output is -1. Example 2: Input: N = 5 a[] = {2,3,1,2,3} Output: 2 3  Explanation: 2 and 3 occur more than once in the given array. Your Task: Complete the function duplicates() which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in a sorted manner. If no such element is found, return list containing [-1]. Expected Time Complexity: O(n). Expected Auxiliary Space: O(n). Constraints: 1 <= N <= 105 0 <= A[i] <= N-1, for each valid i Sol...

Kth smallest element (DSA)

Problem Statement :(👈click here for GFG) Given an array arr[] and an integer K where K is smaller than size of array, the task is to find the Kth smallest element in the given array. It is given that all array elements are distinct. Note :-  l and r denotes the starting and ending index of the array. Example 1: Input: N = 6 arr[] = 7 10 4 3 20 15 K = 3 Output : 7 Explanation : 3rd smallest element in the given array is 7. Example 2: Input: N = 5 arr[] = 7 10 4 20 15 K = 4 Output : 15 Explanation : 4th smallest element in the given array is 15. Your Task: You don't have to read input or print anything. Your task is to complete the function kthSmallest() which takes the array arr[], integers l and r denoting the starting and ending index of the array and an integer K as input and returns the Kth smallest element. Expected Time Complexity: O(n) Expected Auxiliary Space: O(log(n)) Constraints: 1 <= N <= 105 1 <= arr[i] <= 105 1 <= K <= N Solution:1 cl...

Leaders in an array (DSA)

Problem Statement :(👈click here for GFG) Given an array A of positive integers. Your task is to find the leaders in the array. An element of array is leader if it is greater than or equal to all the elements to its right side. The rightmost element is always a leader.  Example 1: Input: n = 6 A[] = {16,17,4,3,5,2} Output: 17 5 2 Explanation: The first leader is 17 as it is greater than all the elements to its right.  Similarly, the next leader is 5. The right most element is always a leader so it is also included.  Example 2: Input: n = 5 A[] = {1,2,3,4,0} Output: 4 0 Your Task: You don't need to read input or print anything. The task is to complete the function leader() which takes array A and n as input parameters and returns an array of leaders in order of their appearance. Expected Time Complexity: O(n) Expected Auxiliary Space: O(n) Constraints: 1 <= n <= 107 0 <= Ai <= 107 Solution: The approach of this code is to find the leaders in the given arr...

Majority Element (DSA)

Problem statement :(👈click here for GFG) Given an array   A   of   N   elements. Find the majority element in the array. A majority element in an array A of size N is an   element that appears more than N/2 times in the array .   Example 1: Input: N = 3 A[] = {1,2,3} Output: -1 Explanation: Since, each element in {1,2,3} appears only once so there is no majority element. Example 2: Input: N = 5 A[] = {3,1,3,3,2} Output: 3 Explanation: Since, 3 is present more than N/2 times, so it is the majority element. Your Task: The task is to complete the function  majorityElement () which returns the majority element in the array. If no majority exists, return -1.   Expected Time Complexity:  O(N). Expected Auxiliary Space:  O(1).   Constraints: 1 ≤ N ≤ 10 7 0 ≤ A i  ≤ 10 6 Solution: Approach: One way to solve this problem is by using the Boyer-Moore majority vote algorithm, which works as follows: Initialize a varia...

Sort an array of 0s, 1s and 2s (DSA)

  Problem statement :(👈click here for GFG) Given an array of size  N  containing only 0s, 1s, and 2s; sort the array in ascending order. Example 1: Input: N = 5 arr[]= {0 2 1 2 0} Output: 0 0 1 2 2 Explanation: 0s 1s and 2s are segregated into ascending order. Example 2: Input: N = 3 arr[] = {0 1 0} Output: 0 0 1 Explanation: 0s 1s and 2s are segregated into ascending order. Your Task: You don't need to read input or print anything. Your task is to complete the function  sort012()  that takes an array  arr  and  N  as input parameters and sorts the array in-place. Expected Time Complexity:  O(N) Expected Auxiliary Space:  O(1) Constraints: 1 <= N <= 10^6 0 <= A[i] <= 2 Solution: Approach:  Initialize three variables, count_0, count_1, and count_2 to 0. These variables will be used to count the number of occurrences of 0s, 1s, and 2s in the given array. Traverse the array from left to right and for each elem...