Manafall Daily Quest

Subarray with Given Sum

Function Name: findSubarrayWithSum

Description

Write a function that finds a contiguous subarray within a one-dimensional array of positive integers which adds up to a given sum.

The function should return the indices of the first and last elements in the subarray that adds up to the specified sum, or indicate that no such subarray exists.

The array and sum will be passed as parameters to the function.

If there are multiple such subarrays, the function should return the indices of the subarray with the smallest starting index.

Requirements

  • The function must accept two arguments: an array of positive integers and the target sum.
  • The function should return a tuple or array with two elements representing the starting and ending indices of the subarray.
  • Return a special value (like `null`, `None`, `-1`, etc. depending on the language) if no subarray is found.
  • The array will not be empty and will contain only positive integers.
  • The target sum will be a positive integer.
  • Optimize the function for time complexity.

Examples

If the function is called with `([1, 4, 20, 3, 10, 5], 33)`, it should return `[2, 4]` because the subarray `[20, 3, 10]` adds up to 33.Calling the function with `([1, 2, 3, 7, 5], 12)` should return `[3, 4]` as the subarray `[7, 5]` sums to 12.If the function is passed `([1, 2, 3, 4, 5], 9)` it should return `[2, 4]`, because the subarray `[3, 4, 5]` adds up to 9.If no subarray matches, for example if the function is called with `([1, 2, 3, 4], 10)` it should return `null`.

Links

https://en.wikipedia.org/wiki/Subarrayhttps://www.geeksforgeeks.org/find-subarray-with-given-sum/

LinkedList Merge Sort

Sat Sep 14 2024

In this challenge, you are tasked with implementing a function that performs a merge sort on a singly linked list. The function should be able to sort the elements of the linked list in ascending order, considering that the nodes hold integer values.The merge sort algorithm for a linked list is somewhat similar to merge sort for arrays, but rather than using array indices, you will need to manipulate node references and pointers. Your implementation should be efficient and should not just extract values into an array, sort, and recreate the list.

Prev Quest

Prime Factorization

Mon Sep 16 2024

In this challenge, you are to write a function named calculatePrimeFactors that takes an integer greater than 1 as a parameter and returns a list of its prime factors.Prime factors are the prime numbers that multiply together to equal the original number.The function should return the prime factors in ascending order.

Next Quest