Write a program in JAVA to check whether a series belongs to PseudoArithmetic sequence.
The logic is :
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 x 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 x 3 = 42 [ middle element = 7]
schoolstudent:
pseudo arithmetic sequence is a series of integers in an order where the sum of the pairs of elements at equidistance from front and end remain same.
Answers
Answered by
12
import java.util.Scanner;
public class Program
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int []a=new int[n];
System.out.println("enter the sequence values");
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
int sum=a[0]+a[(a.length-1)];
if(n%2==0){
int c=0;
for(int i=0;i<n/2;i++){
int temp_sum=a[i]+a[(a.length-1)-i];
if(temp_sum==sum)
continue;
else{
System.out.println("not a pesudo arithemitic sequence");
c++;
}
break;
}
if(c==0)
System.out.println("sum of pesudo arithmetic number is:" +(sum*(n/2)));
}
else{
int c=0;
for(int i=0;i<n/2;i++){
int temp_sum1;
if(i!=(n/2)+1){
temp_sum1=a[i]+a[(a.length-1)-i];}
else{
a[(a.length-1)-i]=a[i];
temp_sum1=a[i]+a[(a.length-1)-i];
}
if(temp_sum1==sum)
continue;
else{
System.out.println("not a pesudo arithemitic sequence");
c++;
}
break;
}
if(c==0)
System.out.println("sum of pesudo arithmetic number is:" +(sum*((n/2)+1)));
}
}
}
Answered by
0
what is the output of this program
Similar questions