You are on page 1of 4

OTHER JAVA PROBLEMS:

read the two array from the user and generate a third array containing all the
common elements from the first two.
in java
import java.util.*;

public class CommonElements {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int size1 = scanner.nextInt();

int[] array1 = new int[size1];


for (int i = 0; i < size1; i++) {
array1[i] = scanner.nextInt();
}
int size2 = scanner.nextInt();

int[] array2 = new int[size2];


for (int i = 0; i < size2; i++) {
array2[i] = scanner.nextInt();
}
List<Integer> commonElements = new ArrayList<>();
for (int num1 : array1) {
for (int num2 : array2) {
if (num1 == num2) {
commonElements.add(num1);
break;
}
}
}

int[] commonArray = new int[commonElements.size()];


for (int i = 0; i < commonElements.size(); i++) {
commonArray[i] = commonElements.get(i);
}

System.out.println("Common elements in the two arrays:");


System.out.println(Arrays.toString(commonArray));

scanner.close();
}
}

------

import java.util.*;

public class MinMax {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int size = scanner.nextInt();


int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = scanner.nextInt();
}
int min = array[0];
int max = array[0];
for (int i = 1; i < size; i++) {
if (array[i] < min) {
min = array[i];
}
if (array[i] > max) {
max = array[i];
}
}

int[] result = {min, max};

for (int i = 0; i < result.length; i++) {


if (i > 0) {
System.out.print(", ");
}
System.out.print(result[i]);
}
System.out.println("]");

scanner.close();
}
}

-----
reverse integer:

class Solution {
public int reverse(int x) {
int revN = 0;
while (x != 0) {
int digit = x % 10;
x /= 10;
if (revN > Integer.MAX_VALUE / 10 || (revN == Integer.MAX_VALUE / 10 &&
digit > 7)) return 0;
if (revN < Integer.MIN_VALUE / 10 || (revN == Integer.MIN_VALUE / 10 &&
digit < -8)) return 0;
revN = revN * 10 + digit;
}
return revN;
}
}

-----

Armstrong number:

import java.util.*;
public class Main{
static boolean ArmstrongNumber(int n)
{
int originalno = n;
int count = 0;
int temp = n;
while (temp != 0)
{
count++;
temp = temp / 10;
}
int sumofpower = 0;
while (n != 0)
{
int digit = n % 10;
sumofpower += Math.pow(digit,count);
n /= 10;
}
return (sumofpower == originalno);
}
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
int n1 = sc.nextInt();
if (ArmstrongNumber(n1))
{
System.out.println("true");
}
else
{
System.out.println("false");
}

}
}

--
print all divisors or sum of all divisors:

Problem statement
You are given an integer ‘n’.

Function ‘sumOfDivisors(n)’ is defined as the sum of all divisors of ‘n’.

Find the sum of ‘sumOfDivisors(i)’ for all ‘i’ from 1 to ‘n’.

Example:
Input: ‘n’ = 5

Output: 21

Explanation:
We need to find the sum of ‘sumOfDivisors(i)’ for all ‘i’ from 1 to 5.
‘sumOfDivisors(1)’ = 1
‘sumOfDivisors(2)’ = 2 + 1 = 3
‘sumOfDivisors(3)’ = 3 + 1 = 4
‘sumOfDivisors(4)’ = 4 + 2 +1 = 7
‘sumOfDivisors(5)’ = 5 + 1 = 6
Therefore our answer is sumOfDivisors(1) + sumOfDivisors(2) + sumOfDivisors(3) +
sumOfDivisors(4) + sumOfDivisors(5) = 1 + 3 + 4 + 7 + 6 = 21.

public class Solution {


public static int sumOfAllDivisors(int n){
int totalSum = 0;
for (int i = 1; i <= n; i++) {
totalSum += sumOfDivisors(i);
}
return totalSum;
}

private static int sumOfDivisors(int num) {


int sum = 0;
for (int i = 1; i <= num; i++) {
if (num % i == 0) {
sum += i;
}
}
return sum;
}
}

OR

----

Hashing:

import java.util.*;

class tUf {

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);

int n;
n = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}

//precompute:
int[] hash = new int[13];
for (int i = 0; i < n; i++) {
hash[arr[i]] += 1;
}

int q;
q = sc.nextInt();
while (q-- != 0) {
int number;
number = sc.nextInt();
// fetching:
System.out.println(hash[number]);
}
}
}

You might also like