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

DAY 1

The document outlines three programming problems: searching for an element in an array, finding the maximum height from multiple test cases of mountain heights, and calculating the total cost of groceries based on ingredient quantities and costs. Each problem includes a pseudocode solution, explanations of the approach, and details on time and space complexities. The time complexities range from O(1) to O(N) depending on the problem, while space complexities are generally O(1) or O(T + N).
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)
3 views

DAY 1

The document outlines three programming problems: searching for an element in an array, finding the maximum height from multiple test cases of mountain heights, and calculating the total cost of groceries based on ingredient quantities and costs. Each problem includes a pseudocode solution, explanations of the approach, and details on time and space complexities. The time complexities range from O(1) to O(N) depending on the problem, while space complexities are generally O(1) or O(T + N).
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/ 5

DAY 1

Problem 1: Search an element in an array

Problem Description:

Given an array of N integers and an integer K, determine whether K is present in the array.

Pseudocode:

function searchInArray(arr, K):


for each element in arr:
if element == K:
return "Yes"
return "No"

Explanation:

1. Iterate through each element of the array.


2. Compare the current element with K.
3. If they are equal, return "Yes".
4. If the loop completes without finding K, return "No".

Time Complexity:

- Best Case: O(1) - K is the first element in the array.


- Worst Case: O(N) - K is not in the array, or it's the last element.

Space Complexity:

- Space Complexity: O(1) - Only a few variables are used regardless of the input size.
Problem 2: Find maximum in an Array

Problem Description:

Given a list of N integers representing the heights of mountains, find the height of the tallest
mountain. This has to be done for multiple test cases.

Pseudocode:

function findMaxHeight(testCases):
results = []
for each testCase in testCases:
N = testCase[0]
heights = testCase[1]
max_height = 0
for height in heights:
if height > max_height:
max_height = height
results.append(max_height)
return results

// Read input and process


testCases = readInput()
results = findMaxHeight(testCases)
for result in results:
print(result)

Explanation:

1. Read the Input:


- Read the number of test cases, T .
- For each test case, read N and then read the N heights.

2. Find the Maximum Height:


- Initialize max_height to 0 (since the minimum possible height is 0).
- Iterate through the list of heights.
- For each height, if it is greater than max_height, update max_height.
- Store the result for the current test case.
3. Output the Results:
- After processing all test cases, print the results.

Time Complexity:

- Time Complexity per Test Case: O(N)


- We iterate through the N heights once to find the maximum.
- Total Time Complexity: O(T * N)

Space Complexity:

- Space Complexity per Test Case: O(1)


- We only use a few variables (max_height).
- Total Space Complexity: O(T + N)
- Storing the input and the results requires O(T) for results and O(N) for the heights
array during processing.
Problem 3: Cost of Groceries

Problem Description:

Given two arrays of ingredients and costs, where ingredients[i] is the number of units of
ingredient i needed, and costs[i] is the cost per unit of ingredient i, compute the total cost.

Pseudocode:

function kitchenCost(ingredients, costs):


total_cost = 0
for i from 0 to length(ingredients) - 1:
total_cost = total_cost + (ingredients[i] * costs[i])
return total_cost

Explanation:

1. Initialize total_cost to 0.
2. Traverse both arrays (which have the same length):
- Multiply ingredients[i] by costs[i] to get the cost for that ingredient.
- Add this value to total_cost.
3. Return total_cost.

Time Complexity:

- Time Complexity: O(N) - We traverse the arrays once.

Space Complexity:

- Space Complexity: O(1) - Only a few variables are used regardless of the input size.

You might also like