1.

You are given a sequence of N integers, which are called as pseudo arithmetic sequences(sequences that are in arithmetic progression). Sequence of N integers : 2, 5, 6, 8, 9, 12 We observe that 2 + 12 = 5 + 9 = 6 + 8 = 14 The sum of.the above sequence can be calculated as 14 × 3 = 42. For sequence containing an odd number of elements the rule is to double the middle element, for example 2, 5, 7, 9, 12 = 2 + 12 = 5 + 9 = 7 + 7 = 14. 14 × 3 = 42 [middle element = 7]A class Pseudoarithmetic determines whether a given sequence is a pseudoarithmetic sequence. The details of the class are given below: Class name: Pseudoarithmetic Data members/instance variables: n: to store the size of the sequence a[]: integer array to store the sequence of numbers ans, flag: store the status sum: store the sum of the sequence of numbers r: store the sum of the two numbers Member functions: Pseudoarithmetic(): default constructor void accept(int nn): to assign nn to n and to create an integer array. Fill in the elements of the array boolean check(): return true if the sequence is a pseudo arithmetic sequence otherwise returns false Specify the class Pseudoarithmetic, giving the details of the constructor(), void accept(int) and boolean check(). Also, define a main() function to create an object and call the member functions accordingly to enable the task.

Answer»

import java.io. * 

clas Pseudoarithmetic 

Public int n; 

Public int a [ ]; 

Public int ans; 

Public int flag; 

Public int sum;

 Public int r; 

Pseudoarithmetic() 

n = 0; flag = 0; sum = 0; 

void accept(int nn) 

n = nn; 

BufferedReader B = new BufferedReaderf new InputStreamReader (System, in)); 

a [n]; 

(int i = 0; i < n; i++) 

a [i] = Integer.parselnt (B.readLine( )); 

sum = sum + a[i]; 

boolean check( ) 

if(n%2 = = 0) 

{ int i = 0;p = n-1; 

while (i < p) 

{

r = a[i] + a[p] 

if (r == (a [i + 1] + a [p - 1] && (r*3) = = sum) 

flag = 0; 

p = p-1;i = i + 1; 

else 

else if (n%2 ! = 0) 

int i = 0;p = n-1; 

while (i <=p) 

{

r=a[i] + a[p] if (r== (a [i +1] + a[p -1]) && (r*3) = = sum) 

{ flag = 0; 

else 

{

flag= 1; 

p = p-1;i = i+1; 

if(flag = = 0) 

return true; 

else 

return false; 

}



Discussion

No Comment Found

Related InterviewSolutions