
Find HCF (Highest common factor) of n numbers given in an integer array. Fill in the function HCF() and return the HCF.
Input Specification:
input1: the size array
input2: an integer array
Output specification:
Return the HCF of given numbers.
Example 1
Input 1:
3
Input 2:
2 4 8
Output:
2
Explanation:
The common factor for 2, 4, and 8 are 1 and 2. Hence the highest common factor or HCF is 2.
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
static int getGCD(int arr[], int n) {
int result = arr[0];
for (int element: arr){
result = gcd(result, element);
if(result == 1) return 1;
}
return result;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int arr[] = new int[n];
String[] str = br.readLine().split(" ");
for(int i=0; i<n; i++)
arr[i] = Integer.parseInt(str[i]);
System.out.println(getGCD(arr, n));
}
}
Annual Day
On the eve of the annual day, a competition is held among N students in which each student gains some points based on their performance. All students were asked to stand in a queue in an increasing order based on the points they scored. The clever students stood in a random order so that the students with lower points are not noticed.
You will be given a points array where points[i] tells the teacher the number of points that the ith student earned. Your task is to help the teacher to find all pairs such that for all (0<=i<j<N), the points scored by the ith student is greater than that of the jth student, i.e. points[i]>points[j] where i<j and return the total count of such pairs.
Input Specification:
input1: An integer value N(1<=N<=1000) denoting the number of students in the class.
input2: An integer array of length N representing the points scored where input2[i] = points scored by ith student and input2[i]<=1000(0<=i<N)
Output Specification:
Return the total number of pairs that satisfy the constraints given in the problem statement

Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int arr[] = new int[n];
String[] str = br.readLine().split(" ");
for(int i=0; i<n; i++)
arr[i] = Integer.parseInt(str[i]);
int total = 0;
for(int i=0; i<n-1; i++)
for(int j=i+1; j<n; j++)
if(arr[i] > arr[j])
total++;
System.out.println(total);
}
}
Fibonacci
The Fibonacci series is a series in which each number is the sum of the preceding two numbers. For example: 0, 1, 1, 2, 3, 5, 8, 13, 21, etc.
The first two numbers of the series are 0 and 1.
Write a code to return nth Fibonacci number.
The sequence Fn of Fibonacci numbers is defined by the following recurrence relation Fn = F(n-1) + F(n-2) with initial values F0 = 0 and F1 = 1
Input Specification:
input1: A number n
Output Specification:
Return the nth Fibonacci number
Solution: In Java
import java.util.*;
import java.lang.*;
import java.io.*;
class CodeWindow
{
static int fibo(int n)
{
if (n <= 1)
return n;
return fibo(n - 1) + fibo(n - 2);
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
System.out.println(fibo(n));
}
}
Also Checkout