0% found this document useful (0 votes)
4 views13 pages

Array 01

The document discusses three array problems: insertion of elements in an array, finding the maximum number of consecutive ones in a binary array, and calculating the maximum sum of a subarray of size K. It provides problem statements, example inputs and outputs, and complexity analyses for each problem. The time and space complexities for the solutions are also detailed, emphasizing efficient approaches for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views13 pages

Array 01

The document discusses three array problems: insertion of elements in an array, finding the maximum number of consecutive ones in a binary array, and calculating the maximum sum of a subarray of size K. It provides problem statements, example inputs and outputs, and complexity analyses for each problem. The time and space complexities for the solutions are also detailed, emphasizing efficient approaches for each problem.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

ARRAY PROBLEMS

12 February 2024 20:41

i. COMPLEXITY ANALYSIS IN
INSERTION OF ELEMENT IN ARRAY

ii. MAX CONSECUTIVE ONE'S

iii. MAX SUM SUB-ARRAY OF SIZE K

CREATED BY :- SHIVAM BAREKAR


TIME COMPLEXITY – INSERTING AN ELEMENT IN
ARRAY
12 February 2024 18:41

Problem Statement : Given an array of N integers, write a program to add an array


element at the beginning, end, and at a specific position.

Example :

Input: N = 5, array[] = {1,2,3,4,5}, insertbeginning(6), insertending(7), insertatpos(8,4)

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 :

The approach is very simple for all three insertions.

• 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 :

Before inserting the value at beginning:


10 9 14 8 20 48 16 9
After inserting the value at beginning:
40 10 9 14 8 20 48 16 9

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 :

Before inserting the value at beginning:


10 9 14 8 20 48 16 9
After inserting the value at beginning:
10 9 14 8 20 48 16 9 40

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)

• Insertion at specific position

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 :

Before inserting the value at beginning:


10 9 14 8 20 48 16 9
After inserting the value at beginning:
10 9 14 8 40 20 48 16 9

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 :

Input: nums = [1,1,0,1,1,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 :

Input: nums = [1,0,1,1,0,1]

Output: 2

Constraints :

1 <= nums.length <= 105


nums[i] is either 0 or 1.

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 :

The maximum consecutive 1’s are 3.

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

Problem Statement : Given an array of integers Arr of size N and a number


K. Return the maximum sum of a subarray of size K.

NOTE*: A subarray is a contiguous part of any given array.

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.

• Expected Time Complexity: O(N).


• Expected Auxiliary Space: O(1).

Constraints :

1 <= N <= 105


1 <= Arri <= 105
1 <= K <= N
Naive Solution :

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).

Below is the implementation of the above idea.

Output :

24

Complexity Analysis :

Time Complexity: O(n2)


Auxiliary Space: O(1)

Optimized Solution (Space Optimization) :

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.

Below is the implementation of the above idea.


Output :

24

Complexity Analysis :

Time Complexity: O(n)


Auxiliary Space: O(1)

You might also like