Author Topic: arithmetic progression  (Read 824 times)

  • Full Member
  • ***
  • Posts: 114
  • Karma: +6/-3
    • View Profile
arithmetic progression
« on: February 12, 2013, 11:10:40 pm »
Given an array of integers A, give an algorithm to find the longest Arithmetic progression in it, i.e find a sequence i1 < i2 < … < ik, such that
A[i1], A[i2], …, A[ik] forms an arithmetic progression, and k is the largest possible.
The sequence S1, S2, …, Sk is called an arithmetic progression if
Sj+1 – Sj is a constant

Share on Bluesky Share on Facebook


  • Moderator
  • Full Member
  • *****
  • Posts: 126
  • Karma: +1/-0
  • Location: Puttaparthi
    • View Profile
Re: arithmetic progression
« Reply #1 on: February 13, 2013, 12:57:40 pm »
Is the given array a sequence?

Don't think you are. Know you are -Kranthi

  • Full Member
  • ***
  • Posts: 114
  • Karma: +6/-3
    • View Profile
Re: arithmetic progression
« Reply #2 on: February 13, 2013, 01:40:40 pm »
no
just a random array of numbers

  • Jr. Member
  • **
  • Posts: 88
  • Karma: +4/-2
    • View Profile
    • kprocks
Re: arithmetic progression
« Reply #3 on: February 15, 2013, 11:27:36 am »
Pseudo code:
Brute Force method: May be correct may be not

suppose X = x1, x2,....., xn are the numbers


for each element i in X
    for each j= i + 1 in x
    {
        compute | xi - xj | => y
        put y in  set A if y is not present;
     }
  //set A contains all the contant value
 // next compute set B with two loops and count number of pairs have constant values from set A
//find the max from set B and we will know what is the corresponding constant from set A => generate the longest seqence.

@Majeti: I think i made my idea clear....... I have thought of a solution with graph... If u validate this, then I will share the other one which I thought about.

kp.

   
Live life off the edge.......... Just do it...........

  • Full Member
  • ***
  • Posts: 114
  • Karma: +6/-3
    • View Profile
Re: arithmetic progression
« Reply #4 on: February 15, 2013, 01:32:37 pm »
@KP: I feel it is correct - what is its complexity?