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

Problems On Arrays

About arrays

Uploaded by

logavignesh25
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)
11 views

Problems On Arrays

About arrays

Uploaded by

logavignesh25
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/ 4

Conveyor Belt System

Scenario: Imagine you are managing a conveyor belt system in a factory. The conveyor belt
is represented as an array of items, and you need to rotate the conveyor belt by a specified
number of steps to the right. This helps in ensuring that the items on the belt are evenly
distributed for further processing.

Task: Write a function to rotate the array of items to the right by K steps, where K is non-
negative.

Sample Test Cases with Detailed Output Explanations

Test Case 1:

Input: items = [1, 2, 3, 4, 5, 6, 7], K = 3

Output: [5, 6, 7, 1, 2, 3, 4]

Explanation:

• Rotate 1 step 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]

Test Case 2:

Input: items = [-1, -100, 3, 99], K = 2

Output: [3, 99, -1, -100]

Explanation:

• Rotate 1 step to the right: [99, -1, -100, 3]


• Rotate 2 steps to the right: [3, 99, -1, -100]

Test Case 3:

Input: items = [10, 20, 30, 40, 50], K = 1

Output: [50, 10, 20, 30, 40]

Explanation:

• Rotate 1 step to the right: [50, 10, 20, 30, 40]

Test Case 4:

Input: items = [5, 6, 7], K = 0


Output: [5, 6, 7]

Explanation:

• No rotation is needed as K is 0.

Test Case 5:

Input: items = [15, 25, 35, 45], k = 6

Output: [35, 45, 15, 25]

Explanation:

Rotate 2 steps to the right (since K % itemsSize =6 % 4=2):

o Rotate 1 step to the right: [45, 15, 25, 35]


o Rotate 2 steps to the right: [35, 45, 15, 25]

Constraints:
• 1 <= items.length <= 10^5
• -2^{31} <= items[i] <= 2^{31} - 1
• 0 <= k <= 10^5

Expected Time Complexity: O(n)

Expected Space Complexity: O(1)


Expense Tracker
Scenario: Imagine you are developing an expense-tracking application. Users
can input their daily expenses, and the app stores these expenses in an array. A
user wants to find two specific expenses that add up to a given target amount
(e.g., two expenses that together make up a specific budget or a known
transaction total). Your task is to write a function that helps users find the
indices of these two expenses.

Task: Write a function to find the indices of the two numbers in the array that
add up to the given target. Each input will have exactly one solution, and you
may not use the same element twice. The order of the indices returned does not
matter.

Sample Test Cases with Detailed Output Explanations

Test Case 1:

Input: expenses = [2, 7, 11, 15], target = 9

Output: [0, 1]

Explanation:

• The first expense is 2 at index 0.


• The second expense is 7 at index 1.
• Their sum is 9, which matches the target.
• Thus, the indices [0, 1] are returned.

Test Case 2:

Input: expenses = [3, 2, 4], target = 6

Output: [1, 2]

Explanation:

• The first expense is 2 at index 1.


• The second expense is 4 at index 2.
• Their sum is 6, which matches the target.
• Thus, the indices [1, 2] are returned.

Test Case 3:

Input: expenses = [3, 3], target = 6

Output: [0, 1]
Explanation:

• Both expenses are 3 at indices 0 and 1.


• Their sum is 6, which matches the target.
• Thus, the indices [0, 1] are returned.

Test Case 4:

Input: expenses = [1, 5, 7, 2, 8], target = 9

Output: [1, 4]

Explanation:

• The first expense is 5 at index 1.


• The second expense is 8 at index 4.
• Their sum is 9, which matches the target.
• Thus, the indices [1, 4] are returned.

Test Case 5:

Input: expenses = [0, 4, 3, 0], target = 0

Output: [0, 3]

Explanation:

• Both expenses are 0 at indices 0 and 3.


• Their sum is 0, which matches the target.
• Thus, the indices [0, 3] are returned.

Constraints:
• 2 <= expenses.length <= 10^4
• -10^9 <= expenses[i] <= 10^9
• -10^9 <= target <= 10^9
• Only one valid answer exists.

Expected Time Complexity: O(n)

Expected Space Complexity: O(n)

You might also like