Skip to content
Related Articles

Related Articles

Improve Article

Convert given array to Arithmetic Progression by adding an element

  • Difficulty Level : Easy
  • Last Updated : 20 May, 2021

Given an array arr[], the task is to find an element that can be added to the array in order to convert it to Arithmetic Progression. If it’s impossible to convert the given array into an AP, then print -1.

Examples:  

Input: arr[] = {3, 7} 
Output: 11 
3, 7 and 11 is a finite AP sequence.

Input: a[] = {4, 6, 8, 15} 
Output: -1 

Approach: 



  • Sort the array and start traversing the array element by element and note the difference between the two consecutive elements.
  • If the difference for all the elements is the same then print last element + common difference.
  • If the difference is different for at most one pair (arr[i – 1], arr[i]) and diff = 2 * common difference for all other elements, then print arr[i] – common difference.
  • Else print -1.

Below is the implementation of the above approach: 

C++




// C++ implementation of the approach
 
#include<bits/stdc++.h>
using namespace std;
 
// Function to return the number to be
// added
int getNumToAdd(int arr[], int n)
{
    sort(arr,arr+n);
    int d = arr[1] - arr[0];
    int numToAdd = -1;
    bool numAdded = false;
 
    for (int i = 2; i < n; i++) {
        int diff = arr[i] - arr[i - 1];
 
        // If difference of the current
        // consecutive elements is
        // different from the common
        // difference
        if (diff != d) {
 
            // If number has already been
            // chosen then it's not possible
            // to add another number
            if (numAdded)
                return -1;
 
            // If the current different is
            // twice the common difference
            // then a number can be added midway
            // from current and previous element
            if (diff == 2 * d) {
                numToAdd = arr[i] - d;
 
                // Number has been chosen
                numAdded = true;
            }
 
            // It's not possible to maintain
            // the common difference
            else
                return -1;
        }
    }
 
    // Return last element + common difference
    // if no element is chosen and the array
    // is already in AP
    if (numToAdd == -1)
        return (arr[n - 1] + d);
 
    // Else return the chosen number
    return numToAdd;
}
 
// Driver code
int main()
{
    int arr[] = { 1, 3, 5, 7, 11, 13, 15 };
    int n = sizeof(arr)/sizeof(arr[0]);
    cout << getNumToAdd(arr, n);
}
 
 
// This code is contributed
// by ihritik

Java




// Java implementation of the approach
import java.util.*;
public class GFG {
 
    // Function to return the number to be
    // added
    static int getNumToAdd(int arr[], int n)
    {
        Arrays.sort(arr);
        int d = arr[1] - arr[0];
        int numToAdd = -1;
        boolean numAdded = false;
 
        for (int i = 2; i < n; i++) {
            int diff = arr[i] - arr[i - 1];
 
            // If difference of the current
            // consecutive elements is
            // different from the common
            // difference
            if (diff != d) {
 
                // If number has already been
                // chosen then it's not possible
                // to add another number
                if (numAdded)
                    return -1;
 
                // If the current different is
                // twice the common difference
                // then a number can be added midway
                // from current and previous element
                if (diff == 2 * d) {
                    numToAdd = arr[i] - d;
 
                    // Number has been chosen
                    numAdded = true;
                }
 
                // It's not possible to maintain
                // the common difference
                else
                    return -1;
            }
        }
 
        // Return last element + common difference
        // if no element is chosen and the array
        // is already in AP
        if (numToAdd == -1)
            return (arr[n - 1] + d);
 
        // Else return the chosen number
        return numToAdd;
    }
 
    // Driver code
    public static void main(String[] args)
    {
        int arr[] = { 1, 3, 5, 7, 11, 13, 15 };
        int n = arr.length;
        System.out.println(getNumToAdd(arr, n));
    }
}

Python3




# Python 3 implementation of the approach
 
# Function to return the number
# to be added
def getNumToAdd(arr, n):
    arr.sort(reverse = False)
    d = arr[1] - arr[0]
    numToAdd = -1
    numAdded = False
 
    for i in range(2, n, 1):
        diff = arr[i] - arr[i - 1]
 
        # If difference of the current consecutive
        # elements is different from the common
        # difference
        if (diff != d):
             
            # If number has already been chosen
            # then it's not possible to add
            # another number
            if (numAdded):
                return -1
 
            # If the current different is twice
            # the common difference then a
            # number can be added midway from
            # current and previous element
            if (diff == 2 * d):
                numToAdd = arr[i] - d
 
                # Number has been chosen
                numAdded = True
         
            # It's not possible to maintain
            # the common difference
            else:
                return -1
     
    # Return last element + common difference
    # if no element is chosen and the array
    # is already in AP
    if (numToAdd == -1):
        return (arr[n - 1] + d)
 
    # Else return the chosen number
    return numToAdd
 
# Driver code
if __name__ == '__main__':
    arr = [1, 3, 5, 7, 11, 13, 15]
    n = len(arr)
    print(getNumToAdd(arr, n))
 
# This code is contributed
# mohit kumar 29

C#




// C# implementation of the approach
 
using System;
public class GFG {
 
    // Function to return the number to be
    // added
    static int getNumToAdd(int []arr, int n)
    {
        Array.Sort(arr);
        int d = arr[1] - arr[0];
        int numToAdd = -1;
        bool numAdded = false;
 
        for (int i = 2; i < n; i++) {
            int diff = arr[i] - arr[i - 1];
 
            // If difference of the current
            // consecutive elements is
            // different from the common
            // difference
            if (diff != d) {
 
                // If number has already been
                // chosen then it's not possible
                // to add another number
                if (numAdded)
                    return -1;
 
                // If the current different is
                // twice the common difference
                // then a number can be added midway
                // from current and previous element
                if (diff == 2 * d) {
                    numToAdd = arr[i] - d;
 
                    // Number has been chosen
                    numAdded = true;
                }
 
                // It's not possible to maintain
                // the common difference
                else
                    return -1;
            }
        }
 
        // Return last element + common difference
        // if no element is chosen and the array
        // is already in AP
        if (numToAdd == -1)
            return (arr[n - 1] + d);
 
        // Else return the chosen number
        return numToAdd;
    }
 
    // Driver code
    public static void Main()
    {
        int []arr = { 1, 3, 5, 7, 11, 13, 15 };
        int n = arr.Length;
        Console.WriteLine(getNumToAdd(arr, n));
    }
}
 
// This code is contributed
// by ihritik

PHP




<?php
// PHP implementation of the approach
 
// Function to return the number
// to be added
function getNumToAdd($arr, $n)
{
    sort($arr);
    $d = $arr[1] - $arr[0];
    $numToAdd = -1;
    $numAdded = false;
 
    for ($i = 2; $i < $n; $i++)
    {
        $diff = $arr[$i] - $arr[$i - 1];
 
        // If difference of the current
        // consecutive elements is
        // different from the common
        // difference
        if ($diff != $d)
        {
 
            // If number has already been
            // chosen then it's not possible
            // to add another number
            if ($numAdded)
                return -1;
 
            // If the current different is
            // twice the common difference
            // then a number can be added midway
            // from current and previous element
            if ($diff == 2 * $d)
            {
                $numToAdd = $arr[$i] - $d;
 
                // Number has been chosen
                $numAdded = true;
            }
 
            // It's not possible to maintain
            // the common difference
            else
                return -1;
        }
    }
 
    // Return last element + common difference
    // if no element is chosen and the array
    // is already in AP
    if ($numToAdd == -1)
        return ($arr[$n - 1] + $d);
 
    // Else return the chosen number
    return $numToAdd;
}
 
// Driver code
$arr = array( 1, 3, 5, 7, 11, 13, 15 );
$n = sizeof($arr);
echo getNumToAdd($arr, $n);
 
// This code is contributed by Sachin..
?>

Javascript




<script>
 
// Javascript implementation of the approach
 
// Function to return the number to be
// added
function getNumToAdd(arr, n)
{
    arr.sort(function(a, b){return a - b});
    var d = arr[1] - arr[0];
    var numToAdd = -1;
    var numAdded = false;
 
    for(var i = 2; i < n; i++)
    {
        var diff = arr[i] - arr[i - 1];
 
        // If difference of the current
        // consecutive elements is
        // different from the common
        // difference
        if (diff != d)
        {
             
            // If number has already been
            // chosen then it's not possible
            // to add another number
            if (numAdded)
                return -1;
 
            // If the current different is
            // twice the common difference
            // then a number can be added midway
            // from current and previous element
            if (diff == 2 * d)
            {
                numToAdd = arr[i] - d;
 
                // Number has been chosen
                numAdded = true;
            }
 
            // It's not possible to maintain
            // the common difference
            else
                return -1;
        }
    }
 
    // Return last element + common difference
    // if no element is chosen and the array
    // is already in AP
    if (numToAdd == -1)
        return (arr[n - 1] + d);
 
    // Else return the chosen number
    return numToAdd;
}
 
// Driver code
var arr = [ 1, 3, 5, 7, 11, 13, 15 ];
var n = arr.length;
document.write(getNumToAdd(arr, n));
 
// This code is contributed by Ankita saini
 
</script>
Output: 
9

 

Time Complexity : O(n Log n)
 

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with experts, please refer DSA Live Classes for Working Professionals and Competitive Programming Live for Students.




My Personal Notes arrow_drop_up
Recommended Articles
Page :