Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Find maximun range of an array

Find maximun range of an array

Scheduled Pinned Locked Moved C / C++ / MFC
helpgraphicsalgorithmsdata-structurestutorial
4 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13469062
    wrote on last edited by
    #1

    Greetings, i have a problem finding a solution for this algorithm: i have to find the maximun range in an unordered array (i cant order the array). An example will be: +--------------------------+ | 100 | -10 | -10 | -10 | 100 | --> this sould return the range 0-4 +--------------------------+ +-----------------+ | -7 | 5 | -1 | 9 | -6 | --> this sould return the range 1-3 +-----------------+ +-----------------+ | -7 | 5 | -6 | 9 | -6 | --> this sould return the range 3-3 +-----------------+ I dont have problems doing the algorithm in O(n^2) but i need to do this algoithm in O(n), can anyone help me? Cuadratic algorithm:

    void maximunSegment(vector &V, int &start, int &end){
    int i = 0, j = 0, r = 0, aux = 0;
    start = 0; end = 0;

    while(i < V.size()){
    	j = i;
    	while(j < V.size() - 1){
    		aux += V\[j\];
    		if(aux >= r){
    			r = aux;
    			start = i + 1; end = j + 1;
    		}
    		++j;
    	}
    	aux = 0;
    	++i;
    }
    

    }

    IMPORTANTE NOTE: the positions of the array are important, i mean that the content of V[i] is linked to that i (if v[3] = 89, that 89 always have to refered to v[3] even if you change the array you need to remember that this 89 was on position 3), so if you reorder the array you need to keep that reference. Thank you so much.

    D S 2 Replies Last reply
    0
    • U User 13469062

      Greetings, i have a problem finding a solution for this algorithm: i have to find the maximun range in an unordered array (i cant order the array). An example will be: +--------------------------+ | 100 | -10 | -10 | -10 | 100 | --> this sould return the range 0-4 +--------------------------+ +-----------------+ | -7 | 5 | -1 | 9 | -6 | --> this sould return the range 1-3 +-----------------+ +-----------------+ | -7 | 5 | -6 | 9 | -6 | --> this sould return the range 3-3 +-----------------+ I dont have problems doing the algorithm in O(n^2) but i need to do this algoithm in O(n), can anyone help me? Cuadratic algorithm:

      void maximunSegment(vector &V, int &start, int &end){
      int i = 0, j = 0, r = 0, aux = 0;
      start = 0; end = 0;

      while(i < V.size()){
      	j = i;
      	while(j < V.size() - 1){
      		aux += V\[j\];
      		if(aux >= r){
      			r = aux;
      			start = i + 1; end = j + 1;
      		}
      		++j;
      	}
      	aux = 0;
      	++i;
      }
      

      }

      IMPORTANTE NOTE: the positions of the array are important, i mean that the content of V[i] is linked to that i (if v[3] = 89, that 89 always have to refered to v[3] even if you change the array you need to remember that this 89 was on position 3), so if you reorder the array you need to keep that reference. Thank you so much.

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      Member 13501021 wrote:

      ...i have to find the maximun range

      When I hear the phrase "maximum range" I think of things like: 1) what's the farthest that ball can be hit, 2) what's the longest distance that bullet will travel in 50% humidity, 3) what's the farthest that car can go on a full tank. What is your definition of maximum range?

      Member 13501021 wrote:

      +--------------------------+ | 100 | -10 | -10 | -10 | 100 | --> this sould return the range 0-4 +--------------------------+ +-----------------+ | -7 | 5 | -1 | 9 | -6 | --> this sould return the range 1-3 +-----------------+ +-----------------+ | -7 | 5 | -6 | 9 | -6 | --> this sould return the range 3-3

      What's the pattern?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      S 1 Reply Last reply
      0
      • D David Crow

        Member 13501021 wrote:

        ...i have to find the maximun range

        When I hear the phrase "maximum range" I think of things like: 1) what's the farthest that ball can be hit, 2) what's the longest distance that bullet will travel in 50% humidity, 3) what's the farthest that car can go on a full tank. What is your definition of maximum range?

        Member 13501021 wrote:

        +--------------------------+ | 100 | -10 | -10 | -10 | 100 | --> this sould return the range 0-4 +--------------------------+ +-----------------+ | -7 | 5 | -1 | 9 | -6 | --> this sould return the range 1-3 +-----------------+ +-----------------+ | -7 | 5 | -6 | 9 | -6 | --> this sould return the range 3-3

        What's the pattern?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        S Offline
        S Offline
        Sascha Lefevre
        wrote on last edited by
        #3

        Pretty sure that the "maximum range" should be the segment of the array where the sum of its values is the highest of all possible segments.

        If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

        1 Reply Last reply
        0
        • U User 13469062

          Greetings, i have a problem finding a solution for this algorithm: i have to find the maximun range in an unordered array (i cant order the array). An example will be: +--------------------------+ | 100 | -10 | -10 | -10 | 100 | --> this sould return the range 0-4 +--------------------------+ +-----------------+ | -7 | 5 | -1 | 9 | -6 | --> this sould return the range 1-3 +-----------------+ +-----------------+ | -7 | 5 | -6 | 9 | -6 | --> this sould return the range 3-3 +-----------------+ I dont have problems doing the algorithm in O(n^2) but i need to do this algoithm in O(n), can anyone help me? Cuadratic algorithm:

          void maximunSegment(vector &V, int &start, int &end){
          int i = 0, j = 0, r = 0, aux = 0;
          start = 0; end = 0;

          while(i < V.size()){
          	j = i;
          	while(j < V.size() - 1){
          		aux += V\[j\];
          		if(aux >= r){
          			r = aux;
          			start = i + 1; end = j + 1;
          		}
          		++j;
          	}
          	aux = 0;
          	++i;
          }
          

          }

          IMPORTANTE NOTE: the positions of the array are important, i mean that the content of V[i] is linked to that i (if v[3] = 89, that 89 always have to refered to v[3] even if you change the array you need to remember that this 89 was on position 3), so if you reorder the array you need to keep that reference. Thank you so much.

          S Offline
          S Offline
          Sascha Lefevre
          wrote on last edited by
          #4

          This surely is your homework, so I won't rob you of the opportunity to find the solution yourself. But I'll try to nudge you onto the right track: - Take a pen and paper, write down some random value sequences as input arrays and solve them manually. Try to do this in an algorithmic way. Use some longer sequences than you showed here. - You can't have nested loops over n for O(n), obviously. So you will have to make do with one pass over the array. But you're free to use more or other variables. - The solution is surprisingly easy and succinct - don't think too complicated.

          If the brain were so simple we could understand it, we would be so simple we couldn't. — Lyall Watson

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups