Describe an algorithm Find-Inversion(A) that takes as input an array A and finds an inversion in O(n) time. code example
Example 1: a recursive function that calculates the greatest common divisor from user's input in java
public class GCDExample {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter first number to find GCD");
int number1 = scanner.nextInt();
System.out.println("Please enter second number to find GCD");
int number2 = scanner.nextInt();
System.out.println("GCD of two numbers " + number1 +" and "
+ number2 +" is :" + findGCD(number1,number2));
}
private static int findGCD(int number1, int number2) {
if(number2 == 0){
return number1;
}
return findGCD(number2, number1%number2);
}
}
Output:
Please enter first number to find GCD
54
Please enter second number to find GCD
24
GCD of two numbers 54 and 24 is :6
Example 2: Write a function called square_odd that has one parameter. Your function must calculate the square of each odd number in a list. Return a Python 3 list containing the squared values Challenge yourself: Solve this problem with a list comprehension!
nums = [square_odds**2 for square_odds in nums if square_odds%2 != 0]
Example 3: create a function that takes in an array of numbers and returns only the number that are even after 1 is added to the value
const evenAfter = (arr) => {
let i = 0
let newArr = []
while (i <= arr.length) {
if ((arr[i] + 1) % 2 === 0){
newArr.push(arr[i])
}
i++
}
return newArr
}
console.log(evenAfter([3,6,7,8,9,11]))