0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Codewars Find the missing term in an Arithmetic Progression

Posted at

import java.util.Arrays;
public class Solution 
{
	public static int findMissing(int[] numbers)
	{
    
    Arrays.sort(numbers);
    int []temp = new int[numbers.length];
    for(int i = 1 ; i < numbers.length ; i++){
      
      
      temp[i - 1] = numbers[i] - numbers[i - 1];      
    }
    
    int min = Integer.MAX_VALUE;
    for(int t : temp){
      if(t == 0){
        continue;
      }
      
     min= Math.min(min, Math.abs(t));
    }
    
    if(min == Integer.MAX_VALUE){
      return numbers[0];
    }

    
   for(int i = 1 ; i < numbers.length ; i++){
    if (min != Math.abs(numbers[i] - numbers[i-1])){
          System.out.println( Math.abs(numbers[i]));
      return numbers[i] - min;
    } 
   }
    
    return 0;
	}
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?