# Arith Geo Coderbyte Solution

0

Have the function ArithGeo(arr) take the array of numbers stored in arr and return the string "Arithmetic" if the sequence follows an arithmetic pattern or return "Geometric" if it follows a geometric pattern. If the sequence doesn't follow either pattern return -1. An arithmetic sequence is one where the difference between each of the numbers is consistent, whereas, in a geometric sequence, each term after the first is multiplied by some constant or common ratio.

Arithmetic example: [2, 4, 6, 8] and Geometric example: [2, 6, 18, 54].Negative numbers may be entered as parameters, 0 will not be entered, and no array will contain all the same elements

Arith Geo Coderbyte Solution In Java

import java.util.Scanner;

public class ArithGeo {
String ArithGeo(int[] arr) {
if (isArithmetic(arr)) {
return "Arithmetic";
} else if (isGeometric(arr)) {
return "Geometric";
}

return "-1";
}

boolean isArithmetic(int[] arr) {
int diff = arr - arr;
int previousDiff = diff;
for (int i = 2; i < arr.length; i++) {
diff = arr[i] - arr[i - 1];
if (diff != previousDiff) {
return false;
}
previousDiff = diff;
}
return true;
}

boolean isGeometric(int[] arr) {
int diff = arr / arr;
int previousDiff = diff;
for (int i = 2; i < arr.length; i++) {
diff = arr[i] / arr[i - 1];
if (diff != previousDiff) {
return false;
}
previousDiff = diff;
}
return true;
}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
ArithGeo c = new ArithGeo();
System.out.print(c.ArithGeo(new int[] {2, 6, 18, 54}));
}
}

Arith Geo Coderbyte Solution In Python

def ArithGeo(arr):
arith,geo = True, True
for num in range(1, len(arr) - 1):
if arr[num + 1] - arr[num] != arr[num] - arr[num-1]:
arith = False
if arr[num +1] / arr[num] != arr[num] / arr[num-1]:
geo = False
return "Arithmetic" if arith else "Geometric" if geo else -1

Using the JavaScript language, have the function ArithGeo(arr) take the array of numbers stored in arr and return the string "Arithmetic" if the sequence follows an arithmetic pattern or return "Geometric" if it follows a geometric pattern. If the sequence doesn't follow either pattern return -1. An arithmetic sequence is one where the difference between each of the numbers is consistent, where as in a geometric sequence, each term after the first is multiplied by some constant or common ratio. Arithmetic example: [2, 4, 6, 8] and Geometric

example: [2, 6, 18, 54]. Negative numbers may be entered as parameters, 0 will not be entered, and no array will contain all the same elements.

Arith Geo Coderbyte Solution In JavaScript

function ArithGeo(arr) {

var arithFlag = true, geoFlag = true;
var diff = arr - arr;
for (var i = 2; i < arr.length; i++) {
if ((arr[i] - arr[i-1]) !== diff) {
arithFlag = false;
}
}
if (arithFlag) {
return "Arithmetic";
}
else { // check for geometric pattern
diff = arr / arr;
for (var i = 2; i < arr.length; i++) {
if ((arr[i] / arr[i-1]) !== diff) {
geoFlag = false;
}
}
if (geoFlag) {
return "Geometric";
}
else {
return "-1";
}
}
}

Explanation:

To check for arithmetic pattern, start by getting the difference between the first two number. Then loop thru array starting in position 2 and subtract the previous number. If the difference is equal to the initial difference then you have an arithmetic pattern so return arithmetic. Next repeat by getting initial difference by dividing the first and second numbers. Loop through array starting in position 2 and compare the current number divided by previous number. If difference is equal to the initial number then you have a geometric pattern. Else return -1.

Disclaimer: The above Problem is generated by Coderbyte but the Solution is provided by ShouterFolk.