Strivera2z 1
Strivera2z 1
PROBLEM:1
Problem Statement:
Write a Java program to count the number of digits in a given integer.
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
PROBLEM:2
Problem Statement:
Write a Java program to reverse a given integer.
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
PROBLEM:3
Problem Statement:
Write a Java program to check whether a given integer is a palindrome.
Java Code:
java
CopyEdit
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a number: ");
int number = scanner.nextInt();
scanner.close();
Explanation:
PROBLEM:3
Problem Statement:
Write a Java program to find the Greatest Common Divisor (GCD) or Highest Common
Factor (HCF) of two given numbers.
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
PROBLEM : 4
Problem Statement:
Write a Java program to check whether a given integer is an Armstrong number.
An Armstrong number (or Narcissistic number) for an n-digit number is a number where
the sum of its digits raised to the power n is equal to the number itself.
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
PROBLEM: 5
Problem Statement:
Write a Java program to print all divisors of a given integer.
Example:
Input: n = 12
Output: 1 2 3 4 6 12
Input: n = 17
Output: 1 17 (Since 17 is a prime number)
Java Code:
java
CopyEdit
import java.util.Scanner;
System.out.print("Divisors: ");
printDivisors(number);
}
Explanation:
PROBLEM: 6
Problem Statement:
Write a Java program to check whether a given integer is a prime number.
A prime number is a number greater than 1 that has exactly two divisors: 1 and itself.
Example:
Input: n = 7
Output: Prime Number ✅
Input: n = 10
Output: Not a Prime Number ❌
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
PROBLEM: 7
Problem Statement:
Write a Java program to print a message N times using recursion.
Example:
nginx
CopyEdit
Hello
Hello
Hello
Hello
Hello
Java Code:
java
CopyEdit
import java.util.Scanner;
public class RecursionPrinter {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the number of times to print: ");
int n = scanner.nextInt();
scanner.close();
printMessage(n);
}
Explanation:
PROBLEM: 8
Problem Statement:
Write a Java program to print a given name N times using recursion.
Example:
nginx
CopyEdit
Alice
Alice
Alice
Java Code:
java
CopyEdit
import java.util.Scanner;
printName(n, name);
}
Explanation:
PROBLEM: 9
Problem Statement:
Write a Java program to print numbers from 1 to N using recursion.
Example:
Input: N = 5
Output:
CopyEdit
1
2
3
4
5
Java Code:
import java.util.Scanner;
printNumbers(1, n);
}
Explanation:
PROBLEM: 10
Problem Statement:
Write a Java program to print numbers from N to 1 using recursion.
Example:
Input: N = 5
Output:
5
4
3
2
1
Java Code:
java
CopyEdit
import java.util.Scanner;
printNumbers(n);
}
PROBLEM: 11
Problem Statement:
Write a Java program to find the sum of the first N natural numbers using recursion.
Example:
Input: N = 5
Output: 15
(Explanation: 1 + 2 + 3 + 4 + 5 = 15)
Java Code:
import java.util.Scanner;
Explanation:
PROBLEM: 12
Problem Statement:
Write a Java program to find the factorial of N using recursion.
Factorial Formula:
Example:
o Input: N = 5
o Output: 120
(Explanation: 5 × 4 × 3 × 2 × 1 = 120)
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
PROBLEM: 12
Problem Statement:
Write a Java program to reverse an array using recursion.
Example:
Input: [1, 2, 3, 4, 5]
Output: [5, 4, 3, 2, 1]
Java Code:
import java.util.Scanner;
reverseArray(arr, 0, n - 1);
System.out.println("Reversed array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
Explanation:
PROBLEM: 13
Problem Statement:
Write a Java program to check if a given string is a palindrome using recursion.
Example:
Input: "madam"
Output: Yes, it is a palindrome
Input: "hello"
Output: No, it is not a palindrome
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
Problem Statement:
Write a Java program to find the Nth Fibonacci number using recursion.
Fibonacci Sequence:
where
F(0)=0F(0) = 0F(0)=0
F(1)=1F(1) = 1F(1)=1
F(2)=1F(2) = 1F(2)=1
F(3)=2F(3) = 2F(3)=2
F(4)=3F(4) = 3F(4)=3
F(5)=5F(5) = 5F(5)=5
and so on...
Example:
Input: N = 6
Output: 8
(Explanation: 0, 1, 1, 2, 3, 5, 8 → 6th Fibonacci number is 8)
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
1. The program takes an integer N as input.
2. The fibonacci method recursively computes the Nth Fibonacci number:
o Base case: If N == 0, return 0.
o If N == 1, return 1.
o Otherwise, return fibonacci(N-1) + fibonacci(N-2).
3. The recursion continues until reaching the base cases.
4. The final result is printed as the Nth Fibonacci number.
PROBLEM: 15
Problem Statement:
Write a Java program to count the frequency of each element in an array.
Example:
Input: [1, 2, 2, 3, 1, 4, 2, 3, 4, 4]
Output:
bash
CopyEdit
1 → 2 times
2 → 3 times
3 → 2 times
4 → 3 times
Java Code:
java
CopyEdit
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
countFrequencies(arr);
}
Explanation:
PROBLEM:16
Problem Statement:
Write a Java program to count the frequency of each element in an array.
Example:
Input: [1, 2, 2, 3, 1, 4, 2, 3, 4, 4]
Output:
bash
CopyEdit
1 → 2 times
2 → 3 times
3 → 2 times
4 → 3 times
Java Code:
java
CopyEdit
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
countFrequencies(arr);
}
System.out.println("Element Frequencies:");
for (Map.Entry<Integer, Integer> entry : freqMap.entrySet()) {
System.out.println(entry.getKey() + " → " + entry.getValue() +
" times");
}
}
}
Explanation:
PROBLEM:17
Problem Statement:
Write a Java program to find the element with the highest and lowest frequency in an array.
Example:
Input: [1, 2, 2, 3, 1, 4, 2, 3, 4, 4]
Output:
bash
CopyEdit
Highest Frequency Element: 2 (3 times)
Lowest Frequency Element: 1 (2 times)
Java Code:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class FrequencyAnalyzer {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
findHighestLowestFrequency(arr);
}
Explanation:
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
selectionSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
Explanation:
PROBLEM:17
Problem Statement:
Write a Java program to implement Bubble Sort. Bubble Sort repeatedly steps through the
list, compares adjacent elements, and swaps them if they are in the wrong order. The process
is repeated until the array is sorted.
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
bubbleSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
// Flag to check if any swap was made in this iteration
boolean swapped = false;
Explanation:
PROBLEM:17
Problem Statement:
Write a Java program to implement Insertion Sort. Insertion Sort works by picking elements
one by one and placing them in their correct position in the sorted part of the array.
Example:
insertionSort(arr);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
Explanation:
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
mergeSort(arr, 0, n - 1);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i = 0, j = 0, k = left;
while (i < n1 && j < n2) {
if (leftArray[i] <= rightArray[j]) {
arr[k] = leftArray[i];
i++;
} else {
arr[k] = rightArray[j];
j++;
}
k++;
}
Explanation:
PROBLEM:19
Problem Statement:
Write a Java program to implement Recursive Bubble Sort. Instead of using iterative loops,
the sorting is done recursively by repeatedly moving the largest element to the end in each
pass.
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
bubbleSort(arr, n);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
PROBLEM:20
Problem Statement:
Write a Java program to implement Recursive Insertion Sort. Instead of using loops, the
sorting is done recursively by placing each element in its correct position in the sorted part of
the array.
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
insertionSort(arr, n);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
Explanation:
PROBLEM:21
Problem Statement:
Write a Java program to implement Quick Sort using the divide and conquer approach. The
algorithm selects a pivot, partitions the array into elements smaller and greater than the pivot,
and recursively sorts the partitions.
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
public class QuickSort {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the array: ");
int n = scanner.nextInt();
int[] arr = new int[n];
quickSort(arr, 0, n - 1);
System.out.println("Sorted array:");
for (int num : arr) {
System.out.print(num + " ");
}
}
Explanation:
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
return max;
}
}
Explanation:
PROBLEM:23
Problem Statement:
Write a Java program to find the second largest element in a given array without sorting.
Example:
Java Code:
java
CopyEdit
import java.util.Scanner;
return secondLargest;
}
}
Explanation:
PROBLEM:24
Problem Statement:
Example 1:
Input: [1, 2, 3, 4, 5]
Output: Array is sorted
Example 2:
Input: [1, 3, 2, 4, 5]
Output: Array is not sorted
Java Code:
java
CopyEdit
import java.util.Scanner;
if (isSorted(arr, n)) {
System.out.println("Array is sorted");
} else {
System.out.println("Array is not sorted");
}
}
Explanation:
PROBLEM:25
Problem Statement:
Write a Java program to remove duplicates from a sorted array and return the new length
of the array with unique elements.
Example:
Input: [1, 1, 2, 2, 3, 4, 4, 5]
Output: Unique elements: [1, 2, 3, 4, 5]
Java Code:
java
CopyEdit
import java.util.Scanner;
Explanation:
PROBLEM:26
Problem Statement:
Example:
Input: [1, 2, 3, 4, 5]
Output: [2, 3, 4, 5, 1]
Java Code:
java
CopyEdit
import java.util.Scanner;
leftRotateByOne(arr, n);
Explanation:
PROBLEM: 27
Problem Statement:
Example:
Input:
o Array: [1, 2, 3, 4, 5]
o D=2
Output:
o [3, 4, 5, 1, 2]
Java Code:
java
CopyEdit
import java.util.Scanner;
leftRotateByD(arr, n, d);
System.out.print("Array after " + d + " left rotations: ");
for (int num : arr) {
System.out.print(num + " ");
}
}
if (d == 0) {
return; // No need to rotate if D is 0 or a multiple of n
}
Explanation:
1. Input:
The program takes the size of the array N and the array elements.
o
It also takes the number of rotations D from the user.
o
2. Rotation Logic:
o The leftRotateByD method uses the reverse method to efficiently rotate the
array:
First, reverse the portion of the array from 0 to D-1.
Then reverse the portion from D to N-1.
Finally, reverse the entire array to achieve the left rotation by D places.
3. Reverse Method:
o The reverse method swaps elements in the array from the start index to the
end index, effectively reversing the portion of the array.
4. Edge Case Handling:
If D is greater than or equal to N, the program uses d = d % n to ensure the
o
rotations don't exceed the array length.
o If D is 0 or a multiple of N, no rotations are needed.
5. Output:
o The program prints the array after performing the left rotation by D places.
PROBLEM: 28
Problem Statement:
Write a Java program to move all zeros in an array to the end without changing the order of
non-zero elements.
Example:
Input: [0, 1, 2, 0, 3, 0, 4]
Output: [1, 2, 3, 4, 0, 0, 0]
PROBLEM: 29
Problem Statement:
Write a Java program to implement a linear search algorithm to find an element in an array.
Example:
PROBLEM: 30
Problem Statement:
Write a Java program to find the union of two arrays.
Example:
import java.util.HashSet;
Problem Statement:
Write a Java program to find the missing number in a given array of integers from 1 to N.
Example:
Input: [1, 2, 4, 5]
Output: 3
PROBLEM: 32
Problem Statement:
Write a Java program to find the maximum number of consecutive 1's in a binary array.
Example:
Input: [1, 1, 0, 1, 1, 1]
Output: 3
PROBLEM: 33
Problem Statement:
Write a Java program to find the element that appears only once in an array, where all other
elements appear twice.
Example:
Input: [4, 3, 2, 4, 3, 1, 2]
Output: 1
PROBLEM: 34
Problem Statement:
Write a Java program to find the length of the longest subarray with a sum equal to K,
where all elements are positive integers.
Example:
import java.util.HashMap;
PROBLEM: 35
Problem Statement:
Write a Java program to find the length of the longest subarray with sum equal to K,
where the array contains both positive and negative integers.
Example:
import java.util.HashMap;
PROBLEM: 36
Problem Statement:
Write a Java program to find two numbers in an array that add up to a specific target sum.
Example:
import java.util.HashMap;
PROBLEM: 37
Problem Statement:
Write a Java program to sort an array consisting of only 0's, 1's, and 2's.
Example:
Input: [0, 1, 2, 1, 0, 2, 1]
Output: [0, 0, 1, 1, 1, 2, 2]
PROBLEM: 38
Problem Statement:
Write a Java program to find the majority element in an array (element that appears more
than n/2 times).
Example:
Input: [3, 3, 4, 2, 4, 4, 2, 4, 4]
Output: 4
PROBLEM: 39
Problem Statement:
Write a Java program to print the subarray with the maximum sum using Kadane's algorithm.
Example:
PROBLEM: 40
Problem Statement:
Write a Java program to calculate the maximum profit from stock trading (Buy and Sell).
You can only buy and sell once.
Example:
Input: [7, 1, 5, 3, 6, 4]
Output: 5 (Buy at 1 and sell at 6)
PROBLEM: 41
Problem Statement:
Write a Java program to rearrange the array in alternating positive and negative items. The
positive numbers should come first.
Example:
PROBLEM: 42
Problem Statement:
Write a Java program to generate Pascal’s Triangle up to the n-th row.
Example:
Input: n = 5
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
import java.util.*;
PROBLEM: 43
Problem Statement:
Write a Java program to find the majority element in an array (an element that appears more
than n/3 times).
Example:
Input: [3, 3, 4, 2, 4, 4, 2, 4, 4]
Output: 4
import java.util.*;
count1 = count2 = 0;
for (int num : nums) {
if (num == candidate1) count1++;
if (num == candidate2) count2++;
}
if (count1 > nums.length / 3) result.add(candidate1);
if (count2 > nums.length / 3) result.add(candidate2);
return result;
}
}
PROBLEM: 44
Problem Statement:
Write a Java program to find all unique triplets in an array that sum to zero.
Example:
import java.util.*;
Problem Statement:
Write a Java program to find all unique quadruplets in an array that sum to a target.
Example:
import java.util.*;
Problem Statement:
Write a Java program to find the largest subarray with a sum of 0.
Example:
import java.util.*;
PROBLEM: 47
Problem Statement:
Write a Java program to count the number of subarrays that have a given XOR K.
Example:
import java.util.*;
return count;
}
PROBLEM: 48
Problem Statement:
Next Permutation
import java.util.*;
PROBLEM: 49
Problem Statement:
Leaders in an Array
import java.util.*;
Collections.reverse(leaders);
return leaders;
}
}
PROBLEM: 50
Problem Statement:
int longest = 0;
for (int num : nums) {
if (!numSet.contains(num - 1)) {
int currentNum = num;
int currentStreak = 1;
PROBLEM: 51
Problem Statement: S
if (firstRowZero) {
for (int j = 0; j < matrix[0].length; j++) {
matrix[0][j] = 0;
}
}
if (firstColZero) {
for (int i = 0; i < matrix.length; i++) {
matrix[i][0] = 0;
}
}
}
}
PROBLEM: 52
Problem Statement:
PROBLEM: 53
Problem Statement:
PROBLEM: 54
Problem Statement:
return count;
}
}
PROBLEM: 55
Java Code:
import java.util.*;
PROBLEM: 56
Problem Statement: Write a Java program to merge two sorted arrays without using extra
space.
Java Code:
import java.util.*;
int i = m - 1;
int j = 0;
Arrays.sort(arr1);
Arrays.sort(arr2);
}
}
PROBLEM: 57
Problem Statement: Given an array containing n elements where each element is in the
range from 1 to n, find the repeating and missing number in the array.
Java Code:
return result;
}
}
PROBLEM: 58
Problem Statement: Write a Java program to count the number of inversions in an array. An
inversion is a pair of indices (i, j) such that i < j and arr[i] > arr[j].
Java Code:
private static int mergeSort(int[] arr, int[] temp, int left, int right)
{
int invCount = 0;
if (left < right) {
int mid = (left + right) / 2;
invCount += mergeSort(arr, temp, left, mid);
invCount += mergeSort(arr, temp, mid + 1, right);
invCount += merge(arr, temp, left, mid, right);
}
return invCount;
}
private static int merge(int[] arr, int[] temp, int left, int mid, int
right) {
int i = left, j = mid + 1, k = left, invCount = 0;
return invCount;
}
}
PROBLEM: 58
Problem Statement: Given an array of integers, write a Java program to count the number of
reverse pairs. A reverse pair is a pair (i, j) where i < j and arr[i] > 2 * arr[j].
Java Code:
private static int merge(int[] nums, int left, int mid, int right) {
int count = 0;
int j = mid + 1;
return count;
}
}
PROBLEM: 59
Problem Statement: Given an integer array, write a Java program to find the contiguous
subarray within an array which has the largest product.
Java Code:
return result;
}
}
Congratulations on completing the PDF created by SYNTAX ERROR, also known as Abhishek Rathor!
Your dedication and perseverance are truly commendable. For more insights and updates, feel free
to connect on Instagram at SYNTAX ERROR. Keep up the great work!