Manafall Daily Quest

Duplicate Number Finder

Function Name: findDuplicate

Description

Write a function 'findDuplicate' that takes an array of integers. The array contains integers in the range 1 to n (inclusive), with one integer appearing exactly twice. Your task is to find and return the duplicate integer.

Requirements

  • The function 'findDuplicate' should take an array of integers as its only parameter.
  • Do not use sorting methods, as that would change the order of elements and increase time complexity. Aim to solve it in O(n) time and O(1) space complexity.
  • Do not use extra space for another data structure like a set or a hash map.
  • Assume that the input array always has exactly one duplicate.
  • The function should return the duplicate integer value, not its index.

Examples

Given input array: [1, 3, 4, 2, 2]The function should return: 2Given input array: [3, 1, 3, 4, 2]The function should return: 3

Links

https://en.wikipedia.org/wiki/Floyd's_Tortoise_and_Hare

Sort Colors

Fri Nov 15 2024

Given an array consisting of n objects with '0's, '1's, and '2's, sort the array in-place so that objects of the same color are adjacent, with the colors in the order of '0', '1', and '2'.You are not allowed to use the library's sort function and should solve this problem with a linear complexity O(n).The function should modify the array in-place and does not need to return anything.

Prev Quest

Balanced Brackets

Sun Nov 17 2024

Write a function `isBracketBalanced` which checks whether a given string has balanced brackets.The function takes a single string parameter where each character is either a standard opening or closing bracket ('(', ')', '{', '}', '[' or ']').It should return `true` if the brackets in the string are balanced, meaning that each opening bracket has a corresponding closing bracket and the brackets are properly nested.Return `false` if the string is empty, contains any other characters, or the brackets are not balanced.

Next Quest