-
Notifications
You must be signed in to change notification settings - Fork 2
/
Missing_in_AP.java
37 lines (34 loc) · 1.02 KB
/
Missing_in_AP.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package com.company;
import java.io.IOException;
import java.util.Scanner;
public class Missing_in_AP {
public static void main(String[] args) throws IOException {
//write your code here
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t!=0){
int n = s.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
int low=0,high=n-1;
int mid=low+(high-low)/2;
int cd=0;
if((arr[mid]-arr[mid-1])==(arr[mid+1]-arr[mid]))
cd=arr[mid]-arr[mid-1];
else
cd=Math.abs((arr[mid]-arr[mid-1])-(arr[mid+1]-arr[mid]));
for (int i= 0; i < n-1 ; i++){
if(arr[i]+cd==arr[i+1]) {
continue;
}
else{
System.out.println(arr[i]+cd);
break;
}
}
t--;
}
}
}