Manafall Daily Quest

Magic Index Finder

Function Name: findMagicIndex

Description

In a given sorted array of distinct integers, write a function named 'findMagicIndex' that finds and returns a 'magic index' if one exists. A 'magic index' in an array A is defined as an index i such that A[i] equals i. If no such index exists, the function should return -1. Assume the array is sorted in ascending order and there are no duplicate elements.

Requirements

  • The function should take a single parameter which is a sorted array of distinct integers.
  • It should return the magic index (an integer) if one exists, or -1 if no magic index exists.
  • The function must run with a worst-case time complexity better than O(n).
  • You are not allowed to use built-in array search functions or methods specific to any standard library.
  • Write the function in a way that can handle arrays with negative integers as well.

Examples

Example 1: findMagicIndex([-20, -10, 0, 1, 4, 10]) should return 4 because A[4] = 4.Example 2: findMagicIndex([0, 2, 5, 8, 17]) should return 0 because A[0] = 0.Example 3: findMagicIndex([1, 2, 3, 4, 5]) should return -1 because there is no i such that A[i] = i.

Matrix Diagonal Sum

Wed Aug 21 2024

This challenge requires the developer to implement a function that calculates the sum of the elements on the primary diagonal of a square matrix (2D array).A square matrix is a matrix with the same number of rows and columns.The primary diagonal is the diagonal that extends from the top left corner to the bottom right corner of the square matrix.

Prev Quest

Prime Factorization

Fri Aug 23 2024

Write a function that computes the prime factors of a given number, n.A prime factor is a factor that is a prime number, and prime factorization is the determination of the set of prime numbers which multiply together to give the original integer.The function should return an array of integers that represents the prime factors of the given number in ascending order.

Next Quest