Posted on by Kalkicode
Code Hash

Check arithmetic progression in array

Here given code implementation process.

``````/*
Java Program
Check arithmetic progression in array
*/
import java.util.HashSet;
public class Progression
{
//Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print("   " + arr[i]);
}
System.out.print("\n");
}
public void aiProgression(int[] arr, int n)
{
int i = 0;
boolean result = true;
if (n < 2)
{
result = false;
}
else
{
// Use to get unique element
HashSet < Integer > record = new HashSet < Integer > ();
// Use to find first and second smallest element
int first = Integer.MAX_VALUE;
int second = Integer.MAX_VALUE;
// iterate the loop through by array size
// Find two smallest elements
for (i = 0; i < n && result == true; i++)
{
if (record.contains(arr[i]))
{
// When element is repeated
result = false;
}
else if (first > arr[i])
{
if (second > first)
{
second = first;
}
first = arr[i];
}
else if (second > arr[i])
{
second = arr[i];
}
}
if (first == second)
{
result = false;
}
else
{
int diff = second - first;
// Find the differences exist in array or not
for (int value: record)
{
if (value != first && !record.contains(value - diff))
{
result = false;
break;
}
}
}
}
display(arr, n);
if (result == false)
{
System.out.print("  No\n");
}
else
{
System.out.print("  Yes\n");
}
}
public static void main(String[] arg)
{
// Define arrays of integer elements
// first array
int[] arr1 = {
8 , 5 , 11 , 20 , 14 , 17
};
// second array
int[] arr2 = {
6 , 12 , 24 , 0 , 30 , 36 , 42
};
// Get the number of elements
int n = arr1.length;
// Test Cases
n = arr2.length;
}
}``````

Output

``````   8   5   11   20   14   17
Yes
6   12   24   0   30   36   42
No``````
``````// Include header file
#include <iostream>
#include <limits.h>
#include <set>

using namespace std;
/*
C++ Program
Check arithmetic progression in array
*/
class Progression
{
public:
//Function which is display array elements
void display(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << "   " << arr[i];
}
cout << "\n";
}
void aiProgression(int arr[], int n)
{
int i = 0;
bool result = true;
if (n < 2)
{
result = false;
}
else
{
// Use to get unique element
set < int > record ;
// Use to find first and second smallest element
int first = INT_MAX;
int second = INT_MAX;
// iterate the loop through by array size
// Find two smallest elements
for (i = 0; i < n && result == true; i++)
{
if (record.find(arr[i]) != record.end())
{
// When element is repeated
result = false;
}
else if (first > arr[i])
{
if (second > first)
{
second = first;
}
first = arr[i];
}
else if (second > arr[i])
{
second = arr[i];
}
record.insert(arr[i]);
}
if (first == second)
{
result = false;
}
else
{
int diff = second - first;
set <int> :: iterator value;
for (value = record.begin(); value != record.end(); value++)
{
if (*value != first && record.find(*value - diff) == record.end())
{
result = false;
break;
}
}
}
}
this->display(arr, n);
if (result == false)
{
cout << "  No\n";
}
else
{
cout << "  Yes\n";
}
}
};
int main()
{
// Define arrays of integer elements
// first array
int arr1[] = {
8 , 5 , 11 , 20 , 14 , 17
};
// second array
int arr2[] = {
6 , 12 , 24 , 0 , 30 , 36 , 42
};
// Get the number of elements
int n = sizeof(arr1) / sizeof(arr1[0]);
// Test Cases
n = sizeof(arr2) / sizeof(arr2[0]);
return 0;
}``````

Output

``````   8   5   11   20   14   17
Yes
6   12   24   0   30   36   42
No``````
``````// Include namespace system
using System;
using System.Collections.Generic;
/*
C# Program
Check arithmetic progression in array
*/
public class Progression
{
//Function which is display array elements
public void display(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write("   " + arr[i]);
}
Console.Write("\n");
}
public void aiProgression(int[] arr, int n)
{
int i = 0;
Boolean result = true;
if (n < 2)
{
result = false;
}
else
{
// Use to get unique element
HashSet < int > record = new HashSet < int > ();
// Use to find first and second smallest element
int first  = int.MaxValue;
int second = int.MaxValue;
// iterate the loop through by array size
// Find two smallest elements
for (i = 0; i < n && result == true; i++)
{
if (record.Contains(arr[i]))
{
// When element is repeated
result = false;
}
else if (first > arr[i])
{
if (second > first)
{
second = first;
}
first = arr[i];
}
else if (second > arr[i])
{
second = arr[i];
}
}
if (first == second)
{
result = false;

}
else
{
int diff = second - first;
foreach(int value in record)
{
if (value != first && !record.Contains(value - diff))
{
result = false;

break;
}
}
}
}
display(arr, n);
if (result == false)
{
Console.Write("  No\n");
}
else
{
Console.Write("  Yes\n");
}
}
public static void Main(String[] arg)
{
// Define arrays of integer elements
// first array
int[] arr1 = {
8 , 5 , 11 , 20 , 14 , 17
};
// second array
int[] arr2 = {
6 , 12 , 24 , 0 , 30 , 36 , 42
};
// Get the number of elements
int n = arr1.Length;
// Test Cases
n = arr2.Length;
}
}``````

Output

``````   8   5   11   20   14   17
Yes
6   12   24   0   30   36   42
No``````
``````<?php
/*
Php Program
Check arithmetic progression in array
*/
class Progression
{
//Function which is display array elements
public	function display( & \$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo "   ". \$arr[\$i];
}
echo "\n";
}
public	function aiProgression( & \$arr, \$n)
{
\$i = 0;
\$result = true;
if (\$n < 2)
{
\$result = false;
}
else
{
// Use to get unique element
\$record = array();
// Use to find first and second smallest element
\$first = PHP_INT_MAX;
\$second = PHP_INT_MAX;
// iterate the loop through by array size
// Find two smallest elements
for (\$i = 0; \$i < \$n && \$result == true; \$i++)
{
if (in_array(\$arr[\$i], \$record, TRUE))
{
// When element is repeated
\$result = false;
}
else if (\$first > \$arr[\$i])
{
if (\$second > \$first)
{
\$second = \$first;
}
\$first = \$arr[\$i];
}
else if (\$second > \$arr[\$i])
{
\$second = \$arr[\$i];
}
\$record[] = \$arr[\$i];
}
if (\$first == \$second)
{
\$result = false;
}
else
{
\$diff = \$second - \$first;
foreach(\$record as \$value)
{
if (\$value != \$first && !in_array(\$value - \$diff, \$record, TRUE))
{
\$result = false;
break;
}
}
}
}
\$this->display(\$arr, \$n);
if (\$result == false)
{
echo "  No\n";
}
else
{
echo "  Yes\n";
}
}
}

function main()
{
// Define arrays of integer elements
// first array
\$arr1 = array(8, 5, 11, 20, 14, 17);
// second array
\$arr2 = array(6, 12, 24, 0, 30, 36, 42);
// Get the number of elements
\$n = count(\$arr1);
\$n = count(\$arr2);
}
main();``````

Output

``````   8   5   11   20   14   17
Yes
6   12   24   0   30   36   42
No``````
``````/*
Node Js Program
Check arithmetic progression in array
*/
class Progression
{
//Function which is display array elements
display(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write("   " + arr[i]);
}
process.stdout.write("\n");
}
aiProgression(arr, n)
{
var i = 0;
var result = true;
if (n < 2)
{
result = false;
}
else
{
// Use to get unique element

var record = new Set();
// Use to find first and second smallest element
var first  = Number.MAX_VALUE
var second = Number.MAX_VALUE;
// iterate the loop through by array size
// Find two smallest elements
for (i = 0; i < n && result == true; i++)
{
if (record.has(arr[i]))
{
// When element is repeated
result = false;
}
else if (first > arr[i])
{
if (second > first)
{
second = first;
}
first = arr[i];
}
else if (second > arr[i])
{
second = arr[i];
}
}
if (first == second)
{
result = false;
}
else
{
var diff = second - first;
for (let value of record)
{
if (value != first && !record.has(value - diff))
{
result = false;
break;
}
}
}
}
this.display(arr, n);
if (result == false)
{
process.stdout.write("  No\n");
}
else
{
process.stdout.write("  Yes\n");
}
}
}

function main()
{
// Define arrays of integer elements
// first array
var arr1 = [8, 5, 11, 20, 14, 17];
// second array
var arr2 = [6, 12, 24, 0, 30, 36, 42];
// Get the number of elements
var n = arr1.length;
// Test Cases
n = arr2.length;
}
main();``````

Output

``````   8   5   11   20   14   17
Yes
6   12   24   0   30   36   42
No``````
``````import sys
#    Python 3 Program
#    Check arithmetic progression in array

class Progression :
# Function which is display list elements
def display(self, arr, n) :
i = 0
while (i < n) :
print("   ", arr[i], end = "")
i += 1

print(end = "\n")

def aiProgression(self, arr, n) :
i = 0
result = True
if (n < 2) :
result = False
else :
#  Use to get unique element
record = set()
#  Use to find first and second smallest element
first  = sys.maxsize
second = sys.maxsize
#  iterate the loop through by list size
#  Find two smallest elements
while (i < n and result == True) :
if (arr[i] in record) :
#  When element is repeated
result = False

elif(first > arr[i]) :
if (second > first) :
second = first

first = arr[i]

elif(second > arr[i]) :
second = arr[i]

i += 1

if (first == second) :
result = False
else :
diff = second - first
for value in record :
if (value != first and not value - diff in record) :
result = False
break

self.display(arr, n)
if (result == False) :
print("  No")
else :
print("  Yes")

def main() :
#  Define lists of integer elements
#  first list
arr1 = [8, 5, 11, 20, 14, 17]
#  second list
arr2 = [6, 12, 24, 0, 30, 36, 42]
#  Get the number of elements
n = len(arr1)
#  Test Cases
n = len(arr2)

if __name__ == "__main__": main()``````

Output

``````    8    5    11    20    14    17
Yes
6    12    24    0    30    36    42
No``````
``````require 'set'

#    Ruby Program
#    Check arithmetic progression in array

class Progression
# Function which is display array elements
def display(arr, n)
i = 0
while (i < n)
print("   ", arr[i])
i += 1
end

print("\n")
end

def aiProgression(arr, n)
i = 0
result = true
if (n < 2)
result = false
else
#  Use to get unique element
record = Set[]
#  Use to find first and second smallest element
first = (2 ** (0. size * 8 - 2))
second = (2 ** (0. size * 8 - 2))
#  iterate the loop through by array size
#  Find two smallest elements
while (i < n && result == true)
if (record.include?(arr[i]))
#  When element is repeated
result = false
elsif(first > arr[i])
if (second > first)
second = first
end

first = arr[i]
elsif(second > arr[i])
second = arr[i]
end

i += 1
end

if (first == second)
result = false
else
diff = second - first
for value in record do
if (value != first && !record.include?(value - diff))
result = false
break
end

end

end

end

self.display(arr, n)
if (result == false)
print("  No\n")
else
print("  Yes\n")
end

end

end

def main()
#  Define arrays of integer elements
#  first array
arr1 = [8, 5, 11, 20, 14, 17]
#  second array
arr2 = [6, 12, 24, 0, 30, 36, 42]
#  Get the number of elements
n = arr1.length
#  Test Cases
n = arr2.length
end

main()``````

Output

``````   8   5   11   20   14   17
Yes
6   12   24   0   30   36   42
No
``````
``````import scala.collection.mutable._;
import scala.util.control.Breaks._;
/*
Scala Program
Check arithmetic progression in array
*/
class Progression
{
//Function which is display array elements
def display(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print("   " + arr(i));
i += 1;
}
print("\n");
}
def aiProgression(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
var result: Boolean = true;
if (n < 2)
{
result = false;
}
else
{
// Use to get unique element
var record: Set[Int] = Set();
// Use to find first and second smallest element
var first: Int = Integer.MAX_VALUE;
var second: Int = Integer.MAX_VALUE;
// iterate the loop through by array size
// Find two smallest elements
while (i < n && result == true)
{
if (record.contains(arr(i)))
{
// When element is repeated
result = false;
}
else if (first > arr(i))
{
if (second > first)
{
second = first;
}
first = arr(i);
}
else if (second > arr(i))
{
second = arr(i);
}
i += 1;
}
if (first == second)
{
result = false;
}
else
{
var diff: Int = second - first;
breakable
{
for (value <- record)
{
if (value != first && !record.contains(value - diff))
{
result = false;
break;
}
}
}
}
}
this.display(arr, n);
if (result == false)
{
print("  No\n");
}
else
{
print("  Yes\n");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Progression = new Progression();
// Define arrays of integer elements
// first array
var arr1: Array[Int] = Array(8, 5, 11, 20, 14, 17);
// second array
var arr2: Array[Int] = Array(6, 12, 24, 0, 30, 36, 42);
// Get the number of elements
var n: Int = arr1.length;
// Test Cases
n = arr2.length;
}
}``````

Output

``````   8   5   11   20   14   17
Yes
6   12   24   0   30   36   42
No``````
``````import Foundation
/*
Swift 4 Program
Check arithmetic progression in array
*/
class Progression
{
//Function which is display array elements
func display(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print("   ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func aiProgression(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
var result: Bool = true;
if (n < 2)
{
result = false;
}
else
{
// Use to get unique element

var record = Set<Int>();
// Use to find first and second smallest element
var first: Int = Int.max;
var second: Int = Int.max;
// iterate the loop through by array size
// Find two smallest elements
while (i < n && result == true)
{
if (record.contains(arr[i]))
{
// When element is repeated
result = false;
}
else if (first > arr[i])
{
if (second > first)
{
second = first;
}
first = arr[i];
}
else if (second > arr[i])
{
second = arr[i];
}
record.insert(arr[i]);
i += 1;
}
if (first == second)
{
result = false;
}
else
{
let diff: Int = second - first;
for value in record
{
if (value  != first && !record.contains(value - diff))
{
result = false;
break;
}
}
}
}
self.display(arr, n);
if (result == false)
{
print("  No");
}
else
{
print("  Yes");
}
}
}
func main()
{
// Define arrays of integer elements
// first array
let arr1: [Int] = [8, 5, 11, 20, 14, 17];
// second array
let arr2: [Int] = [6, 12, 24, 0, 30, 36, 42];
// Get the number of elements
var n: Int = arr1.count;
// Test Cases
n = arr2.count;
}
main();``````

Output

``````    8    5    11    20    14    17
Yes
6    12    24    0    30    36    42
No``````
``````/*
Kotlin Program
Check arithmetic progression in array
*/
class Progression
{
//Function which is display array elements
fun display(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print("   " + arr[i]);
i += 1;
}
print("\n");
}
fun aiProgression(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
var result: Boolean = true;
if (n < 2)
{
result = false;
}
else
{
// Use to get unique element

var record: MutableSet <Int> = mutableSetOf <Int> ();
// Use to find first and second smallest element
var first: Int = Integer.MAX_VALUE;
var second: Int = Integer.MAX_VALUE;
// iterate the loop through by array size
// Find two smallest elements
while (i < n && result == true)
{
if (record.contains(arr[i]))
{
// When element is repeated
result = false;
}
else if (first > arr[i])
{
if (second > first)
{
second = first;
}
first = arr[i];
}
else if (second > arr[i])
{
second = arr[i];
}
i += 1;
}
if (first == second)
{
result = false;
}
else
{
var diff: Int = second - first;
// Find the differences exist in array or not
for (value in record)
{
if (value != first && !record.contains(value - diff))
{
result = false;
break;
}
}
}
}
this.display(arr, n);
if (result == false)
{
print("  No\n");
}
else
{
print("  Yes\n");
}
}
}
fun main(args: Array < String > ): Unit
{
// Define arrays of integer elements
// first array
var arr1: Array < Int > = arrayOf(8, 5, 11, 20, 14, 17);
// second array
var arr2: Array < Int > = arrayOf(6, 12, 24, 0, 30, 36, 42);
// Get the number of elements
var n: Int = arr1.count();
// Test Cases
n = arr2.count();
}``````

Output

``````   8   5   11   20   14   17
Yes
6   12   24   0   30   36   42
No``````

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post