Sie sind auf Seite 1von 15

# Most Frequent element

Question: Given an array, find the first most repeating element in an array.

## Sample Input 1: Sample Input 2:

5 6
21 31 21 31 31 10 20 30 20 30 20

## Sample Output 1: Sample Output 2:

31 20

Arrays
Most Frequent element
import java.util.*; if (curr_count > max_count)
public class Main {
{ max_count = curr_count;
res = arr[i - 1];
static int mostFrequent(int arr[], int n)
}
{
curr_count = 1;
Arrays.sort(arr); }
int max_count = 1, res = arr[0]; }
int curr_count = 1; if (curr_count > max_count)
for (int i = 1; i < n; i++) {
max_count = curr_count;
{
res = arr[n - 1];
if (arr[i] == arr[i - 1]) }
curr_count++; return res;
else }
{
Arrays
Most Frequent element
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] = new int[n];
for(int i = 0; i<n; i++)
{
arr[i] = sc.nextInt();
}
System.out.println(mostFrequent(arr,n));
}
}

Arrays
Frequency Occurrence
Question: Given an array, the element in the array, and its occurrence, print its index.

## Sample Input 1: Sample Input 2:

6 8
1 2 1 3 2 1 20 21 21 32 24 21 20 21
1 2 21 4

Sample Output 2:
Sample Output 1:
7
2

Arrays
Frequency Occurrence
import java.util.Scanner; for(i = 0; i<n; i++)
public class FrequencyOccurrence {
{ if(s == a[i])
{
public static void main(String[] args) {
count1++;
int a[] = new int[20];
}
int count1=0; if(count == count1)
Scanner sc = new Scanner(System.in); {
int n = sc.nextInt(); break;
for(int i = 0; i<n; i++) }
}
{
System.out.println(i);
a[i] = sc.nextInt();
}
} }
int s = sc.nextInt();
int count = sc.nextInt();
Arrays
Remove Duplicates in array

## Sample Input 1: Sample Input 2:

4 8
1 2 1 3 10 12 10 12 13 14 10 13

Sample Output 2:
Sample Output 1:
10 12 13 14
1 2 3

Arrays
Remove Duplicates in array
import java.util.Scanner;
for(k = j; k < n; k++){
public class DuplicateRemoval{ a[k] = a[k+1];
}
public static void main(String[] args){
n--;
Scanner sc = new Scanner(System.in); }
else{
int n = sc.nextInt();
j++;
int a[] = new int[20]; }
}
int i, j, k;
}
for(i = 0; i < n; i++){ System.out.println();
for(i = 0; i < n; i++){
a[i] = sc.nextInt(); System.out.print(a[i]+" ");
} }
}
for(i = 0; i < n; i++){ }
for(j = i+1; j < n; ){
if(a[j] == a[i]){

Arrays
Equilibrium index in array
Question: Given an array, print the index of an equilibrium element. Equilibrium
index of an array is an index such that the sum of elements at lower indexes is
equal to the sum of elements at higher indexes.

## Sample Input 1: Sample Input 2:

4 8
10 12 10 12 13 14 10 13
1 2 1 3

Sample Output 2:
Sample Output 1: 10 12 13 14
1 2 3

Arrays
Equilibrium index in array
import java.util.Scanner;
class EquilibriumIndex
{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int i,j,flag = 0,count = 0,left_sum,right_sum,c = 0;
int n = sc.nextInt();
int a[] = new int[n];
for(i=0;i<n;i++)
{
a[i] = sc.nextInt();
}

Arrays
Equilibrium index in array
for(i=1;i<n-1;i++){
left_sum=0;right_sum=0;
for(j=i-1;j>=0;j--){
left_sum=left_sum + a[j];
for(j=i+1;j<n;j++)
right_sum = right_sum + a[j];
if(right_sum == left_sum){
System.out.println(i+" ");
c++;
}
}
if(c==0)
}
}
Arrays
Prime Factorisation
Question: Given a number, prime factorise the number and print

## Sample Input 1: Sample Input 2:

45 162

Sample Output 2:
Sample Output 1:
2 3 3 3 3
3 3 5

Number Manipulation
Prime Factorisation
import java.util.*;
public class PrimeFactorise
{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int number = sc.nextInt();
for(int i = 2; i< number; i++) {
while(number%i == 0) {
System.out.println(i+" ");
number = number/i;
}
}
if(number >2) {
System.out.println(number);
}
Number Manipulation
}
Twin Prime numbers
Question: Given a lower range and an upper range, print the twin prime numbers
between the range. Twin prime numbers are at a difference of 2 and being prime
numbers

## Sample Input 1: Sample Input 2:

21 1
35 20

Sample Output 2:
Sample Output 1:
(3,5)(5,7)(11,13)(17,19)
(29,31)

Number Manipulation
Twin Prime numbers
import java.util.*;
class TwinPrime{
boolean isPrime(int n)
{
int count=0;
for(int i=1; i<=n; i++)
{
if(n%i == 0)
count++;
}
if(count == 2)
return true;
else
return false;
}
Number Manipulation
Twin Prime numbers
public static void main(String args[])
{
TwinPrime ob = new TwinPrime();
Scanner sc = new Scanner(System.in);
int p = sc.nextInt();
int q = sc.nextInt();
if(p>q)
System.out.println("Range out of bound");
else
{
for(int i=p; i<=(q-2); i++)
{
if(ob.isPrime(i) == true && ob.isPrime(i+2) == true)
System.out.print(i+","+(i+2));
}
Number Manipulation
}