Git Product home page Git Product logo

dsa-data_structures_arrays's Introduction

Data Structures: Arrays

Here are two problems that involve the use of arrays. For each problem two different algorithms are suggested. Implement the two algorithms and consider the running time behavior of each.

Tasks

Find the missing number

Given an array of length n-1 containing all the integers from 1 to n with one number missing, find the missing number. The array is not necessarily sorted.

For example, given the array: [2, 5, 1, 4] you would say that the number 3 was missing.

Implement two different algorithms for solving this problem.

  1. Brute force Use a loop to consider every possible integer from 1 to n and check if that integer exists in the array or not.

  2. sum and subtract Find the expected sum of all integers from 1 to n. Then find the actual sum of the numbers in the array. The difference is the missing number.

Find maximum and minimum

Given an unsorted array of numbers, find the maximum and minimum numbers in the array. For example, given the array [8, 3, 7, 9, 4, 1, 2, 5, 6] your function should return an output array with two values like this: [1, 9], where the first element is the minimum and the second element is the maximum. If the given array is empty return the empty array [].

Keep in mind that numbers in the array may also be negative.

Implement two different algorithms to solve this problem.

1. Using sort

Start by sorting the array using the built-in array sort() method then simply return the first and last elements of the array.

Note that sort() compares the string values of array elements, so the results will not be in numerical order. E.g. [5,0,-3,510000,6].sort() outputs [ -3, 0, 5, 510000, 6 ]. The sort() method will work to pass the test cases, since the numbers in the tests range within -9 and 9.

2. Using iteration

Iterate over the entire array and track the smallest (minimum) and largest (maximum) numbers found so far.

dsa-data_structures_arrays's People

Contributors

brianhook1183 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.