Skip to content

Commit f032874

Browse files
committed
code: all string problems included
1 parent 3c75905 commit f032874

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

83 files changed

+2434
-772
lines changed

Diff for: Filter.class

1.02 KB
Binary file not shown.

Diff for: Filter.java

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
public class Filter {
2+
3+
public static void filterdArray(int[] arr) {
4+
//! filtering process
5+
for(int i = 0; i < arr.length - 1; i++) {
6+
if(arr[i] % 2 != 0)
7+
System.out.print(arr[i] + " ");
8+
}
9+
}
10+
11+
12+
13+
public static void main(String[] args) {
14+
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
15+
filterdArray(arr);
16+
}
17+
}

Diff for: Java DSA.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/Java-Programs" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

Diff for: Max.class

585 Bytes
Binary file not shown.

Diff for: Max.java

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
public class Max {
2+
public static void max(int[] arr) {
3+
int max = 0;
4+
for(int i = 0; i < arr.length; i++) {
5+
if(arr[i] > max)
6+
max = arr[i];
7+
}
8+
9+
System.out.println(max);
10+
}
11+
12+
public static void main(String[] args) {
13+
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
14+
max(arr);
15+
}
16+
}

Diff for: Practice.class

1.72 KB
Binary file not shown.

Diff for: Practice.java

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import java.util.*;
2+
3+
class Practice {
4+
public static void main(String[] args) {
5+
ArrayList<Integer> arr = new ArrayList<>();
6+
7+
arr.add(4);
8+
arr.add(5);
9+
arr.add(8);
10+
arr.add(9);
11+
arr.add(3);
12+
arr.add(7);
13+
14+
int target = 4;
15+
16+
for (int i = 0; i < arr.size(); i++) {
17+
if (arr.get(i) == target) {
18+
arr.remove(arr.get(i));
19+
}
20+
}
21+
22+
int first = 0;
23+
int end = arr.size() - 1;
24+
int mid = (first + end) / 2;
25+
26+
while (mid < end) {
27+
if (arr.get(first) < arr.get(mid)) {
28+
first++;
29+
}
30+
31+
if (arr.get(first) > arr.get(mid)) {
32+
int temp = arr.get(first);
33+
arr.add(first, arr.get(mid));
34+
arr.add(mid, temp);
35+
36+
first++;
37+
}
38+
39+
if (arr.get(mid) < arr.get(end)) {
40+
end--;
41+
}
42+
43+
if (arr.get(mid) > arr.get(end)) {
44+
int temp = arr.get(mid);
45+
arr.add(mid, arr.get(end));
46+
arr.add(end, temp);
47+
48+
end--;
49+
}
50+
}
51+
52+
System.out.println("List" + arr);
53+
System.out.println("Sorted List" + arr);
54+
}
55+
}

Diff for: Sum_Array.class

589 Bytes
Binary file not shown.

Diff for: Sum_Array.java

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class Sum_Array {
2+
public static void sum(int[] array) {
3+
int sum = 0;
4+
for(int i = 0; i < array.length - 1; i++) {
5+
sum += array[i];
6+
}
7+
System.out.println(sum);
8+
}
9+
10+
public static void main(String[] args) {
11+
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
12+
sum(array);
13+
}
14+
}

Diff for: arrays/Array_Problem_10.java

+38-32
Original file line numberDiff line numberDiff line change
@@ -3,49 +3,55 @@
33
import java.io.*;
44

55
/*
6-
* Problem :-
6+
* Problem :-
77
* Minimum no of jumps to reach end of an arrays.array
88
*/
99

10-
/*
11-
* Understanding of The Problem: -
12-
*
10+
/*
11+
* Understanding of The Problem: -
12+
*
1313
* Given an arrays.array of integers where each element represents the max number of steps that can be made forward from the element
1414
* Write a function to return the minimum no of jumps to reach the end of the arrays.array(starting from the first element).
1515
* If an element is 0, they cannot move through that element.
1616
*/
1717
@SuppressWarnings("unused")
1818
public class Array_Problem_10 {
19-
20-
static int minJumps(int arr[] , int l , int h) {
21-
22-
//Base case: when source & destination are same
23-
if(h == l)
24-
return 0;
25-
26-
//When nothing is reachable from the given source
27-
if(arr[l] == 0)
28-
return Integer.MAX_VALUE;
29-
30-
/*
19+
20+
// Function to find the minimum number of jumps required to reach the end of the array
21+
public static int findMinimumJumps(int[] jumpDistances, int startingIndex, int endingIndex) {
22+
// Base case: destination reached from the source itself
23+
if (startingIndex == endingIndex) {
24+
return 0;
25+
}
26+
27+
// Base case: no jump possible from the source
28+
if (jumpDistances[startingIndex] == 0) {
29+
return Integer.MAX_VALUE;
30+
}
31+
32+
/*
3133
* Traverse through all the points reachable from arrays.array[1].
3234
* Recursively get the minimum number of jumps needed to reach arrays.array[h] from these reachable points.
3335
*/
34-
35-
int min = Integer.MAX_VALUE;
36-
for(int i = l + 1 ; i <= h
37-
&& i<= l + arr[l] ; i++) {
38-
int jumps = minJumps(arr , i , h);
39-
if(jumps != Integer.MAX_VALUE && jumps + 1 < min)
40-
min = jumps + 1 ;
41-
}
42-
return min;
43-
}
44-
45-
public static void main(String[] args) {
46-
int arr[] = { 1, 3, 6, 3, 2, 3, 6, 8, 9, 5 };
47-
int n = arr.length;
48-
System.out.print("Minimum number of jumps to reach end is " + minJumps(arr , 0 , n -1 ));
49-
}
36+
// Explore all reachable destinations from the current position
37+
int minJumps = Integer.MAX_VALUE;
38+
for (int nextIndex = startingIndex + 1; nextIndex <= endingIndex && nextIndex <= startingIndex + jumpDistances[startingIndex]; nextIndex++) {
39+
int jumpsFromNextIndex = findMinimumJumps(jumpDistances, nextIndex, endingIndex);
40+
41+
// Update minimum jumps if a valid path is found
42+
if (jumpsFromNextIndex != Integer.MAX_VALUE && jumpsFromNextIndex + 1 < minJumps) {
43+
minJumps = jumpsFromNextIndex + 1;
44+
}
45+
}
46+
47+
return minJumps;
48+
}
49+
50+
public static void main(String[] args) {
51+
int[] jumpDistances = {1, 3, 6, 3, 2, 3, 6, 8, 9, 5};
52+
int numberOfElements = jumpDistances.length;
53+
54+
System.out.println("Minimum number of jumps to reach the end: " + findMinimumJumps(jumpDistances, 0, numberOfElements - 1));
55+
}
5056

5157
}

Diff for: arrays/Array_Problem_17.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
11
package arrays;
22

3-
//? Problem Title :-> Best Time to buy and sell stocks [1]
3+
//? Problem Title :-> Best Time to buy and sell stocks [1]
44

55
// * You are allowed to buy and sell only once
6-
// ! You are not allowed to sell first and then buy,
6+
// ! You are not allowed to sell first and then buy,
77
// ! (you must buy first and then sell)
88

99
public class Array_Problem_17 {
1010

11-
public int maxProfit(int[] prices) {
11+
public static int maxProfit(int[] prices) {
1212
int mini = prices[0];
1313
int maxProfit = 0;
14-
15-
int n = prices.size();
14+
15+
int n = prices.length;
1616

1717
for(int i = 0; i < n; i++) {
1818
int cost = prices[i] - mini;
1919
maxProfit = Math.max(maxProfit, cost);
2020
mini = Math.min(mini, prices[i]);
2121
}
22-
22+
2323
return maxProfit;
2424
}
2525

Diff for: arrays/Array_Problem_19.java

+28-27
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,42 @@
11
package arrays;
22

3-
/* Problem Title :-> find common elements in 3 sorted arrays */
3+
/* Problem Title: Find common elements in 3 sorted arrays */
44
public class Array_Problem_19 {
5-
// This function prints common elements in a1
6-
void findCommon(int[] a1, int[] a2, int[] a3) {
7-
// Initialize starting indexes for a1[], a2[] and a3[]
8-
int i = 0, j = 0, k = 0;
9-
// Iterate through three arrays while all arrays have elements
10-
while (i < a1.length && j < a2.length && k < a3.length) {
11-
// if x = y and y = z, print any of them and move ahead in all arrays
12-
if (a1[i] >= a2[j] && a2[j] == a3[k]) {
13-
System.out.print(a1[i] + " ");
14-
i++;
15-
k++;
5+
6+
// This function prints common elements in firstArray
7+
void findCommon(int[] firstArray, int[] secondArray, int[] thirdArray) {
8+
// Initialize starting indices for arrays
9+
int firstIndex = 0, secondIndex = 0, thirdIndex = 0;
10+
11+
// Iterate through arrays while all have elements
12+
while (firstIndex < firstArray.length && secondIndex < secondArray.length && thirdIndex < thirdArray.length) {
13+
// If elements are equal, print any and move forward in all arrays
14+
if (firstArray[firstIndex] == secondArray[secondIndex] && secondArray[secondIndex] == thirdArray[thirdIndex]) {
15+
System.out.print(firstArray[firstIndex] + " ");
16+
firstIndex++;
17+
thirdIndex++;
18+
} else if (firstArray[firstIndex] < secondArray[secondIndex]) {
19+
// First element is smaller, so move its index
20+
firstIndex++;
21+
} else if (secondArray[secondIndex] < thirdArray[thirdIndex]) {
22+
// Second element is smaller, so move its index
23+
secondIndex++;
24+
} else {
25+
// Third element is smaller, so move its index
26+
thirdIndex++;
1627
}
17-
// x < y
18-
else if (a1[j] < a2[j])
19-
i++;
20-
// y < z
21-
else if (a2[j] < a3[k])
22-
j++;
23-
// we reach here whem x > y and z < y, i.e., z is smallest
24-
else
25-
k++;
2628
}
2729
}
2830

2931
/* Driver Code */
3032
public static void main(String[] args) {
3133

32-
Array_Problem_19 ob = new Array_Problem_19();
33-
int a1[] = { 1, 5, 10, 20, 40, 80 };
34-
int a2[] = { 6, 7, 20, 80, 100 };
35-
int a3[] = { 3, 4, 15, 20, 30, 70, 80, 120 };
36-
34+
Array_Problem_19 object = new Array_Problem_19();
35+
int[] firstArray = {1, 5, 10, 20, 40, 80};
36+
int[] secondArray = {6, 7, 20, 80, 100};
37+
int[] thirdArray = {3, 4, 15, 20, 30, 70, 80, 120};
3738

3839
System.out.print("Common elements are ");
39-
ob.findCommon(a1, a2, a3);
40+
object.findCommon(firstArray, secondArray, thirdArray);
4041
}
4142
}

0 commit comments

Comments
 (0)