0% found this document useful (0 votes)
17 views

Array Problem - 04

The document discusses two array problems - range sum query and rotate array. It provides the problem statements, examples, constraints and approaches to solve each problem including using prefix sums and reversal algorithms.

Uploaded by

instavinsta007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Array Problem - 04

The document discusses two array problems - range sum query and rotate array. It provides the problem statements, examples, constraints and approaches to solve each problem including using prefix sums and reversal algorithms.

Uploaded by

instavinsta007
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

ARRAY PROBLEM - 04

16 February 2024 17:21

1- RANGE SUM QUERY ( LEETCODE-303 )

2- ROTATE ARRAY ( LEETCODE-189 )

CREATED BY - SHIVAM BAREKAR


RANGE SUM QUERY ( LEETCODE-303 )
16 February 2024 13:43

Link : https://leetcode.com/problems/range-sum-query-immutable/description/

Problem Statement : Given an integer array nums, handle multiple queries of the
following types :

1. Update the value of an element in nums.


2. Calculate the sum of the elements of nums between indices left and right
inclusive where left <= right.

• Implement the NumArray class:


• NumArray(int[] nums) Initializes the object with the integer array nums.
• void update(int index, int val) Updates the value of nums[index] to be val.
• int sumRange(int left, int right) Returns the sum of the elements of nums
between indices left and right inclusive (i.e. nums[left] + nums[left + 1] + ... +
nums[right]).

Example 1 :

Input :
["NumArray", "sumRange", "update", "sumRange"]
[[[1, 3, 5]], [0, 2], [1, 2], [0, 2]]
Output :
[null, 9, null, 8]
Explanation :
NumArray numArray = new NumArray([1, 3, 5]);
numArray.sumRange(0, 2); // return 1 + 3 + 5 = 9
numArray.update(1, 2); // nums = [1, 2, 5]
numArray.sumRange(0, 2); // return 1 + 2 + 5 = 8

Constraints :

1 <= nums.length <= 3 * 104


-100 <= nums[i] <= 100
0 <= index < nums.length
-100 <= val <= 100
0 <= left <= right < nums.length
At most 3 * 104 calls will be made to update and sumRange.

Approach :

• We can use a prefix sum to store the total sum up until each index. We only need
O(N) TIme to actually create our prefixSum array and we can return the actual
sumRange in O(1) time.
• If you look at the array and prefixSum array you'll notice that if you try to subtract
the prefix sum range using i and j that you will end up with the wrong value every
time. If you subtract j - (i-1) then you will get the right value !
import java.util.*;

class Main {

public static void main(String[] args) {


// Note: Don't change class name
Scanner sc = new Scanner(System.in);

// Input for N and Q


int N = sc.nextInt();
int Q = sc.nextInt();
// Input for the array
int arr[] = new int[N];
for(int i = 0; i < arr.length; ++i)
arr[i] = sc.nextInt();
NumArray sol = new NumArray(arr);
// Input the queries
for(int i = 0; i < Q; ++i) {
int l = sc.nextInt();
int r = sc.nextInt();
System.out.println(sol.sumRange(l, r));
}
}
}

// Precomputation: O(N)
// Time per query: O(1)
// Overall time: O(N + Q)
// Overall Space: O(N)

class NumArray {
int preSum[];

// Make the prefix sum array in the constructor


public NumArray(int[] nums) {
preSum = new int[nums.length];
for(int i = 0; i < preSum.length; ++i)
preSum[i] = i == 0? nums[0] : preSum[i - 1]+nums[i];
}

// Now, sum[l....r] is simply equal to preSum[r] - preSum[l-1]


public int sumRange(int left, int right) {
if(left == 0)
return preSum[right];
return preSum[right] - preSum[left - 1];
}
}
Solution For Leetcode :

Submission On Leetcode :

Note : Further when you are comfortable with the topics like Segment Tree, then you can
solve this question with optimal approach using Segment Trees concept.
ROTATE ARRAY ( LEETCODE - 189 )
16 February 2024 16:41

Link : https://leetcode.com/problems/rotate-array/description/

Problem Statement : Given an integer array nums, rotate the array to the right by k
steps, where k is non-negative.

Example 1 :

Input: nums = [1,2,3,4,5,6,7], k = 3


Output: [5,6,7,1,2,3,4]
Explanation:
rotate 1 steps to the right: [7,1,2,3,4,5,6]
rotate 2 steps to the right: [6,7,1,2,3,4,5]
rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

Input: nums = [-1,-100,3,99], k = 2


Output: [3,99,-1,-100]
Explanation:
rotate 1 steps to the right: [99,-1,-100,3]
rotate 2 steps to the right: [3,99,-1,-100]

Constraints :

1 <= nums.length <= 105


-231 <= nums[i] <= 231 - 1
0 <= k <= 105

Solution-01 : Brute-Force Approach :

Approach 1 : Using a temp array :

• For Rotating the Elements to right

Step 1: Copy the last k elements into the temp array.

Step 2: Shift n-k elements from the beginning by k position to the right

Step 3: Copy the elements into the main array from the temp array.
Output :

After Rotating the elements to right 6 7 1 2 3 4 5

Complexity Analysis :

Time Complexity: O(n)

Space Complexity: O(k) since k array element needs to be stored in temp array

• For Rotating the Elements to left :

Step 1: Copy the first k elements into the temp array.

Step 2: Shift n-k elements from last by k position to the left

Step 3: Copy the elements into the main array from the temp array.
Output :

After Rotating the elements to left 3 4 5 6 7 1 2

Complexity Analysis :

Time Complexity: O(n)

Space Complexity: O(k) since k array element needs to be stored in temp array

Solution-02 : Optimal Approach :


Approach 2: Using ” Reversal Algorithm“ :

• For Rotating Elements to right :

Step 1: Reverse the last k elements of the array

Step 2: Reverse the first n-k elements of the array.

Step 3: Reverse the whole array.

For Eg , arr[]={1,2,3,4,5,6,7} , k=2

Output :

After Rotating the k elements to right 6 7 1 2 3 4 5


Complexity Analysis :

Time Complexity – O(N) where N is the number of elements in an array

Space Complexity – O(1) since no extra space is required

• For Rotating Elements to left :

Step 1: Reverse the first k elements of the array

Step 2: Reverse the last n-k elements of the array.

Step 3: Reverse the whole array.

For Eg , arr[]={1,2,3,4,5,6,7} , k=2


Output :

After Rotating the k elements to left 3 4 5 6 7 1 2

Complexity Analysis :

Time Complexity – O(N) where N is the number of elements in an array

Space Complexity – O(1) since no extra space is required

Submission On Leetcode :

You might also like