Computer Science, asked by svaqqqw, 9 months ago

Write a python/Java/C++ Program for
Please Do not spam answers.

Rahul loves to solve puzzles based on integers. He recently tried to solve a puzzle but was unable to solve it. Given an array A of length L, find the lenght of largest sequence of number with the difference N between them. for eg: if A = [1, 3, 5, 7 ,8, 9, 7, 11, 13] and N = 2, then the answer would be 4, as (1, 3, 5, 7) is the largest sequence of numbers having common difference 2 . Note that (11, 13) are also sequence of number having common difference 2 but not the largest possible sequence.(The user has to get the lenght, common difference and array as an input)

Input Format

The first line contains two positive integer L , N .The second line contains L spaced integers.

Constraints

0≤ L ≤10¹⁰

1≤ N ≤ 100

Output Format

One line containg an integer indicating the lenght of the longest sequence of consecutive integers havinf difference N.

Sample Input 0

9 2
1 3 5 7 8 9 7 11 13
Sample Output 0

4

Answers

Answered by hoangmustang
0

Answer:

HELLO, my solution is written in JAVA. HOPE THIS WILL HELP YOU!

package newPack;

import java.util.*;

public class longestTerm {

public static void main(String[] args) {

Scanner input = new Scanner(System.in);

int L = input.nextInt();//read the length of the array

int N = input.nextInt();//read the different between two numbers

int t = 1;//declare the time

int[]a=new int[L];//declare the array with length L

 

for(int i = 0; i < L; i++) {

 a[i] = input.nextInt();//read input from the user

}

for(int e = 0; e < a.length; e++) {

int w = 0;//declare a time to know how long the sequence

int r = a[e];//declare the variable r equal to a[e]

for(int h = e; h < a.length; h++) {

 if(a[h]==r) {

  w++;

  r+=N;

 }

 else

  break;

}

if(w>t)

 t=w;

}

System.out.println(t);

}

}

Explanation:

Similar questions