Array 01
Array 01
i. COMPLEXITY ANALYSIS IN
INSERTION OF ELEMENT IN ARRAY
Example :
Output : 6,1,2,8,3,4,5,7
Explanation : 6 is added at the beginning and 7 is added at the end and 8 is added at position 4
Approach :
• Insertion at beginning
For inserting the element at the beginning we should first shift all elements of the array to left by
index and then insert an element at the 0th position.
Output :
Complexity Analysis :
Time Complexity: θ(n), since n iterations are required to shift the array element to right by 1 posit
Space Complexity: O(1).
• Insertion at Ending
For adding the elements at the end, just add the element at the nth index.
Output :
Complexity Analysis :
Time Complexity: θ(n), since n iterations are required to shiftθ(1) since we need to directly add an
element at the end of the array
Space Complexity: O(1)
For adding the element at a specific position, just shift array elements to right by one position, and
after that add an element at the desired position.
Output :
Complexity Analysis :
Time Complexity: O(n) since we need to shift the elements to right according to the position.
Space Complexity: O(1)
MAX CONSECUTIVE ONES - LEETCODE
12 February 2024 19:15
Problem Statement : Given a binary array nums, return the maximum number
of consecutive 1's in the array.
Link : https://leetcode.com/problems/max-consecutive-ones/description/
Example 1 :
Output: 3
Explanation: The first two digits or the last three digits are consecutive 1s. The maximum
number of consecutive 1s is 3.
Example 2 :
Output: 2
Constraints :
Approach :
We maintain a variable count that keeps a track of the number of consecutive 1’s while
traversing the array. The other variable max_count maintains the maximum number of 1’s,
in other words, it maintains the answer.
We start traversing from the beginning of the array. Since we can encounter either a 1 or
0 there can be two situations:-
1. If the value at the current index is equal to 1 we increase the value of count by one.
After updating the count variable if it becomes more than the max_count update
the max_count.
2. If the value at the current index is equal to zero we make the variable count as 0
since there are no more consecutive ones.
See the illustration below for a better understanding
Output :
Complexity Analysis :
Time Complexity: O(N) since the solution involves only a single pass.
Space Complexity: O(1) because no extra space is used.
MAX SUM SUB-ARRAY OF SIZE K - GFG
12 February 2024 19:30
Link : https://www.geeksforgeeks.org/problems/max-sum-subarray-of-size-k5313/1
Example 1 :
Input:
N = 4, K = 2
Arr = [100, 200, 300, 400]
Output:
700
Explanation:
Arr3 + Arr4 =700,
which is maximum.
Example 2 :
Input:
N = 4, K = 4
Arr = [100, 200, 300, 400]
Output:
1000
Explanation :
Arr1 + Arr2 + Arr3 + Arr4 =1000,
which is maximum.
Your Task :
You don't need to read input or print anything. Your task is to complete the function
maximumSumSubarray() which takes the integer K, vector Arr with size N, containing the
elements of the array and returns the maximum sum of a subarray of size K.
Constraints :
A Simple Solution is to generate all subarrays of size k, compute their sums and finally
return the maximum of all sums. The time complexity of this solution is O(n*k).
Output :
24
Complexity Analysis :
An Efficient Solution is based on the fact that sum of a subarray (or window) of size k can
be obtained in O(1) time using the sum of the previous subarray (or window) of size k.
Except for the first subarray of size k, for other subarrays, we compute the sum by
removing the first element of the last window and adding the last element of the current
window.
24
Complexity Analysis :