Code Monkey home page Code Monkey logo

maximum-increasing-subarray's Introduction

Maximum Increasing Subarray

Author: Jack Robbins

Problem Description

Given an unordered array of k integers, find the largest subarray a such that a is an increasing subarray. That is, that every element in a is greater than or equal to the preceeding element.

Note

There are two approaches to this problem: iterative and recursive.

Iterative Solution

The iterative solution to this problem makes use of 3 for loops, of the traditional variety, to generate subarrays in an iterative manner. The first for loop is used to vary the "start" position, while the middle for loop is used to vary the "end" position. The final for loop simply populates a vector with all of the array elements between the "start" and "end" positions. Both the "start" and "end" values are ranged from 0 to the length of the array, allowed for every possible subarray to be generated. Following this, the generated subarrays are checked to see if they are increasing, and if they are, are added to a set of all subarrays. A set is used to guarantee uniqueness of elements and improve efficiency.

Recursive Solution

The recursive solution to this problem works by having "start" and "end" paramaters passed as function arguments. There are 3 conditions that are checked by the recursive function upon every call. First, if the "end" value is equal to the length of the array, our base case is considered to be reached and we exit the function. Second, the function will check if "start" is greater than "end", and if this condition is satisified, will recursively call itself with an "end" parameter incremented by 1. Finally, if both of those conditions fail, we have the conditions to generate a subarray. Much in the same mannger as the iterative solution, the subarray will be generated with a for loop that grabs array elements from "start" to "end", places the into a vector, and adds that vector to a set if it satisfies the condition of being an increasing array. After generating the subarray, the function will make a recursive call to itself, incrementing the "start" variable by 1. With this approach, every possible subarray is generated.

Final Thoughts

Both solutions then make use of some simple logic to iterate over the set and find the largest subarray. Timing is also used in this project, to determine the runtimes of each solution. It is observed that, as expected, the recursive solution is usually slower than the iterative one. I would encourage anyone who is interested to download, look at, and run this code for themselves. The project is very simple, only having one .cpp file in it.

maximum-increasing-subarray's People

Contributors

jackr276 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.