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. Algorithms
  4. Answer for this algorithm

Answer for this algorithm

Scheduled Pinned Locked Moved Algorithms
comalgorithmsdata-structurestoolsquestion
8 Posts 4 Posters 5 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.
  • M Offline
    M Offline
    MoustafaS
    wrote on last edited by
    #1

    Imagine you have a array of integers, and you want to get any two numbers whose sum is 51, what you'll do? Using both O(n^2) and O(n).:^)


    About : Islam
    About : Me

    C L C 3 Replies Last reply
    0
    • M MoustafaS

      Imagine you have a array of integers, and you want to get any two numbers whose sum is 51, what you'll do? Using both O(n^2) and O(n).:^)


      About : Islam
      About : Me

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      I do brutal

      for (i=0 ; i < iMax-1; i++)
      {
      int temp = a[i];
      if (temp > 51) continue;

      for ( j=i+1; j < iMax; j++)
      {
      if (temp + a[j] == 51)
      {
      // do whatever you need...
      }
      }
      }

      :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

      M 1 Reply Last reply
      0
      • C CPallini

        I do brutal

        for (i=0 ; i < iMax-1; i++)
        {
        int temp = a[i];
        if (temp > 51) continue;

        for ( j=i+1; j < iMax; j++)
        {
        if (temp + a[j] == 51)
        {
        // do whatever you need...
        }
        }
        }

        :)

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.

        M Offline
        M Offline
        MoustafaS
        wrote on last edited by
        #3

        Good, but this is the O(n^2), do you know the n's ?


        About : Islam
        About : Me

        1 Reply Last reply
        0
        • M MoustafaS

          Imagine you have a array of integers, and you want to get any two numbers whose sum is 51, what you'll do? Using both O(n^2) and O(n).:^)


          About : Islam
          About : Me

          L Offline
          L Offline
          Leslie Sanford
          wrote on last edited by
          #4

          LongHC wrote:

          Imagine you have a array of integers, and you want to get any two numbers whose sum is 51, what you'll do?

          First, determine how to break the number down into two numbers whose sum is the original number. Instead of 51, let's pick a smaller number, say 11. Here are the numbers whose sum is 11: 11 + 0 = 11 10 + 1 = 11 9 + 2 = 11 8 + 3 = 11 7 + 4 = 11 6 + 5 = 11 To find these numbers, we can start with two values: the original number and zero, call these x and y. Run a loop in which 1 is subtracted from x and 1 is added to y at each iteration. Continue the loop until x is less than y:

          int x = 11;
          int y = 0;

          while(x > y)
          {
          x--;
          y++;
          }

          Now, we need to search for these values in our array. It would be helpful to have our array sorted first. Then at each iteration through the loop, we perform a binary search for x an y and store the indexes to the numbers in a list, if they are in the array. We'll assume that the binary search returns -1 if the number isn't in the array:

          Sort(array);

          List list;
          int x = 11;
          int y = 0;

          while(x > y)
          {
          int index = BinarySearch(array, x);

          if(index >= 0)
          {
              list.Add(index);
          }
          
          index = BinarySearch(array, y);
          
          if(index >= 0)
          {
              list.Add(index);
          }
          
          x--;
          y++;   
          

          }

          Note: this algorithm is off the top of my head. There may be better ways of doing this. [EDIT] My algorithm doesn't take into account possible duplicates in the array. :doh: [/EDIT] -- modified at 10:54 Friday 13th April, 2007

          1 Reply Last reply
          0
          • M MoustafaS

            Imagine you have a array of integers, and you want to get any two numbers whose sum is 51, what you'll do? Using both O(n^2) and O(n).:^)


            About : Islam
            About : Me

            C Offline
            C Offline
            cp9876
            wrote on last edited by
            #5

            If the integers are positive, then I can think of an O(n) solution. As this sounds like homework I will just give a hint - try using an array of 51 integers. If the integers are unbounded then I can't see an O(n) solution.


            Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

            M 1 Reply Last reply
            0
            • C cp9876

              If the integers are positive, then I can think of an O(n) solution. As this sounds like homework I will just give a hint - try using an array of 51 integers. If the integers are unbounded then I can't see an O(n) solution.


              Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

              M Offline
              M Offline
              MoustafaS
              wrote on last edited by
              #6

              cp9876 wrote:

              If the integers are positive, then I can think of an O(n) solution.

              They are +ve numbers.

              cp9876 wrote:

              As this sounds like homework

              In fact this is not, I just came by this problem while reading and I tried so much in it and didn't get to any thing.


              About : Islam
              About : Me

              C 1 Reply Last reply
              0
              • M MoustafaS

                cp9876 wrote:

                If the integers are positive, then I can think of an O(n) solution.

                They are +ve numbers.

                cp9876 wrote:

                As this sounds like homework

                In fact this is not, I just came by this problem while reading and I tried so much in it and didn't get to any thing.


                About : Islam
                About : Me

                C Offline
                C Offline
                cp9876
                wrote on last edited by
                #7

                In that case, start with an array of 51 numbers c[i] initialised to -1.

                for (int i = 0 ; i < 51 ; i++)
                {
                if (a[i] < 51)
                {
                int n = 51 - a[i];
                if (c[n] >= 0)
                {
                // found one, at a[i], a[c[n]]
                }
                else
                {
                c[n] = i;
                }
                }
                }

                Basically, for each element, store its location where its complement can find it


                Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                M 1 Reply Last reply
                0
                • C cp9876

                  In that case, start with an array of 51 numbers c[i] initialised to -1.

                  for (int i = 0 ; i < 51 ; i++)
                  {
                  if (a[i] < 51)
                  {
                  int n = 51 - a[i];
                  if (c[n] >= 0)
                  {
                  // found one, at a[i], a[c[n]]
                  }
                  else
                  {
                  c[n] = i;
                  }
                  }
                  }

                  Basically, for each element, store its location where its complement can find it


                  Peter "Until the invention of the computer, the machine gun was the device that enabled humans to make the most mistakes in the smallest amount of time."

                  M Offline
                  M Offline
                  MoustafaS
                  wrote on last edited by
                  #8

                  Thanks for your solution and for Leslie's one.:-O


                  About : Islam
                  About : Me

                  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