0% found this document useful (0 votes)
2 views9 pages

Java

The document provides a series of coding examples in Java, demonstrating various algorithms such as reversing a string, checking for palindromes, and finding the second largest number. Each example includes a brief explanation, the corresponding code, and the time complexity of the algorithm. The document serves as a practical guide for common programming tasks and their implementations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views9 pages

Java

The document provides a series of coding examples in Java, demonstrating various algorithms such as reversing a string, checking for palindromes, and finding the second largest number. Each example includes a brief explanation, the corresponding code, and the time complexity of the algorithm. The document serves as a practical guide for common programming tasks and their implementations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

🔹 1️⃣ Reverse String

️ What You Say:

"I will iterate the string from last to first and build a new reversed string."

💻 Code:
public class ReverseString {
public static void main(String[] args) {

String str = "hello";


String reversed = "";

for(int i = [Link]() - 1; i >= 0; i--) {


reversed = reversed + [Link](i);
}

[Link]("Reversed String: " + reversed);


}
}

⏱ Time Complexity: O(n)

🔹 2️⃣ Palindrome Check


️ What You Say:

"I will reverse the string and compare it with original."

💻 Code:
public class PalindromeCheck {
public static void main(String[] args) {

String str = "madam";


String reversed = "";

for(int i = [Link]() - 1; i >= 0; i--) {


reversed = reversed + [Link](i);
}
if([Link](reversed)) {
[Link]("Palindrome");
} else {
[Link]("Not Palindrome");
}
}
}

⏱ O(n)

🔹 3️⃣ Anagram Check


️ What You Say:

"I will convert both strings to char array, sort them, then compare."

💻 Code:
import [Link];

public class AnagramCheck {


public static void main(String[] args) {

String s1 = "listen";
String s2 = "silent";

char[] a1 = [Link]();
char[] a2 = [Link]();

[Link](a1);
[Link](a2);

if([Link](a1, a2)) {
[Link]("Anagram");
} else {
[Link]("Not Anagram");
}
}
}

⏱ O(n log n)

🔹 4️⃣ Count Characters


️ What You Say:

"I will use HashMap to count frequency."

import [Link];

public class CountCharacters {


public static void main(String[] args) {

String str = "hello";


HashMap<Character, Integer> map = new HashMap<>();

for(char c : [Link]()) {
if([Link](c)) {
[Link](c, [Link](c) + 1);
} else {
[Link](c, 1);
}
}

[Link](map);
}
}

⏱ O(n)

🔹 5️⃣ Remove Duplicates (String)


public class RemoveDuplicates {
public static void main(String[] args) {

String str = "programming";


String result = "";

for(int i = 0; i < [Link](); i++) {


if([Link]([Link](i)) == -1) {
result = result + [Link](i);
}
}

[Link](result);
}
}

⏱ O(n²)
🔹 6️⃣ Second Largest Number
public class SecondLargest {
public static void main(String[] args) {

int arr[] = {10, 20, 4, 45, 99};


int first = Integer.MIN_VALUE;
int second = Integer.MIN_VALUE;

for(int i = 0; i < [Link]; i++) {


if(arr[i] > first) {
second = first;
first = arr[i];
} else if(arr[i] > second && arr[i] != first) {
second = arr[i];
}
}

[Link]("Second Largest: " + second);


}
}

⏱ O(n)

🔹 7️⃣ Two Sum (Brute Force)


️ What You Say:

"I will use nested loops which gives O(n²).


If optimized, I can use HashMap which gives O(n)."

public class TwoSum {


public static void main(String[] args) {

int arr[] = {2, 7, 11, 15};


int target = 9;

for(int i = 0; i < [Link]; i++) {


for(int j = i + 1; j < [Link]; j++) {
if(arr[i] + arr[j] == target) {
[Link]("Index: " + i + " " + j);
}
}
}
}
}

⏱ O(n²)
🔹 8️⃣ Prime Number
public class PrimeCheck {
public static void main(String[] args) {

int num = 29;


boolean isPrime = true;

for(int i = 2; i <= num/2; i++) {


if(num % i == 0) {
isPrime = false;
break;
}
}

if(isPrime && num > 1)


[Link]("Prime");
else
[Link]("Not Prime");
}
}

⏱ O(n)

🔹 9️⃣ Fibonacci Series


public class Fibonacci {
public static void main(String[] args) {

int n = 10;
int a = 0, b = 1;

[Link](a + " " + b + " ");

for(int i = 2; i < n; i++) {


int c = a + b;
[Link](c + " ");
a = b;
b = c;
}
}
}

⏱ O(n)
🔹 🔟 Factorial
public class Factorial {
public static void main(String[] args) {

int num = 5;
int fact = 1;

for(int i = 1; i <= num; i++) {


fact = fact * i;
}

[Link]("Factorial: " + fact);


}
}

⏱ O(n)

Find Missing Number (Array 1 to N)


️ What You Say:

"I will calculate expected sum using formula n(n+1)/2 and subtract actual array sum."

💻 Code:
public class MissingNumber {
public static void main(String[] args) {

int arr[] = {1, 2, 3, 5}; // Missing 4


int n = 5; // Total numbers should be 1 to 5

int expectedSum = n * (n + 1) / 2;
int actualSum = 0;

for(int i = 0; i < [Link]; i++) {


actualSum = actualSum + arr[i];
}

int missing = expectedSum - actualSum;

[Link]("Missing Number: " + missing);


}
}

⏱ Time Complexity: O(n)


✔ Best and optimized approach
🔹 2️⃣ Count Frequency of Elements
(HashMap Based – VERY COMMON)
️ What You Say:

"I will use HashMap to store element as key and frequency as value."

💻 Code:
import [Link];

public class CountFrequency {


public static void main(String[] args) {

int arr[] = {1, 2, 2, 3, 3, 3};


HashMap<Integer, Integer> map = new HashMap<>();

for(int i = 0; i < [Link]; i++) {


if([Link](arr[i])) {
[Link](arr[i], [Link](arr[i]) + 1);
} else {
[Link](arr[i], 1);
}
}

[Link](map);
}
}

⏱ Time Complexity: O(n)

🔹 3️⃣ First Non-Repeating Character


️ What You Say:

"I will first store frequency using HashMap, then iterate again to find first character with
frequency 1."

💻 Code:
import [Link];

public class FirstNonRepeating {


public static void main(String[] args) {

String str = "aabbcde";


HashMap<Character, Integer> map = new HashMap<>();

for(int i = 0; i < [Link](); i++) {


char ch = [Link](i);
if([Link](ch)) {
[Link](ch, [Link](ch) + 1);
} else {
[Link](ch, 1);
}
}

for(int i = 0; i < [Link](); i++) {


if([Link]([Link](i)) == 1) {
[Link]("First Non-Repeating: " + [Link](i));
break;
}
}
}
}

⏱ Time Complexity: O(n)

🔹 4️⃣ Duplicate Detection (Array)


️ What You Say:

"I will use HashSet to check if element already exists."

💻 Code:
import [Link];

public class DuplicateDetection {


public static void main(String[] args) {

int arr[] = {1, 2, 3, 4, 2, 5};


HashSet<Integer> set = new HashSet<>();

for(int i = 0; i < [Link]; i++) {


if([Link](arr[i])) {
[Link]("Duplicate Found: " + arr[i]);
} else {
[Link](arr[i]);
}
}
}
}

⏱ Time Complexity: O(n)

You might also like