Skip to content

Commit

Permalink
code: all string problems included
Browse files Browse the repository at this point in the history
  • Loading branch information
amangit1314 committed Jun 5, 2024
1 parent 3c75905 commit f032874
Show file tree
Hide file tree
Showing 83 changed files with 2,434 additions and 772 deletions.
Binary file added Filter.class
Binary file not shown.
17 changes: 17 additions & 0 deletions Filter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
public class Filter {

public static void filterdArray(int[] arr) {
//! filtering process
for(int i = 0; i < arr.length - 1; i++) {
if(arr[i] % 2 != 0)
System.out.print(arr[i] + " ");
}
}



public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
filterdArray(arr);
}
}
11 changes: 11 additions & 0 deletions Java DSA.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/Java-Programs" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
Binary file added Max.class
Binary file not shown.
16 changes: 16 additions & 0 deletions Max.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
public class Max {
public static void max(int[] arr) {
int max = 0;
for(int i = 0; i < arr.length; i++) {
if(arr[i] > max)
max = arr[i];
}

System.out.println(max);
}

public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
max(arr);
}
}
Binary file added Practice.class
Binary file not shown.
55 changes: 55 additions & 0 deletions Practice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.*;

class Practice {
public static void main(String[] args) {
ArrayList<Integer> arr = new ArrayList<>();

arr.add(4);
arr.add(5);
arr.add(8);
arr.add(9);
arr.add(3);
arr.add(7);

int target = 4;

for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) == target) {
arr.remove(arr.get(i));
}
}

int first = 0;
int end = arr.size() - 1;
int mid = (first + end) / 2;

while (mid < end) {
if (arr.get(first) < arr.get(mid)) {
first++;
}

if (arr.get(first) > arr.get(mid)) {
int temp = arr.get(first);
arr.add(first, arr.get(mid));
arr.add(mid, temp);

first++;
}

if (arr.get(mid) < arr.get(end)) {
end--;
}

if (arr.get(mid) > arr.get(end)) {
int temp = arr.get(mid);
arr.add(mid, arr.get(end));
arr.add(end, temp);

end--;
}
}

System.out.println("List" + arr);
System.out.println("Sorted List" + arr);
}
}
Binary file added Sum_Array.class
Binary file not shown.
14 changes: 14 additions & 0 deletions Sum_Array.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Sum_Array {
public static void sum(int[] array) {
int sum = 0;
for(int i = 0; i < array.length - 1; i++) {
sum += array[i];
}
System.out.println(sum);
}

public static void main(String[] args) {
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
sum(array);
}
}
70 changes: 38 additions & 32 deletions arrays/Array_Problem_10.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,55 @@
import java.io.*;

/*
* Problem :-
* Problem :-
* Minimum no of jumps to reach end of an arrays.array
*/

/*
* Understanding of The Problem: -
*
/*
* Understanding of The Problem: -
*
* Given an arrays.array of integers where each element represents the max number of steps that can be made forward from the element
* Write a function to return the minimum no of jumps to reach the end of the arrays.array(starting from the first element).
* If an element is 0, they cannot move through that element.
*/
@SuppressWarnings("unused")
public class Array_Problem_10 {

static int minJumps(int arr[] , int l , int h) {

//Base case: when source & destination are same
if(h == l)
return 0;

//When nothing is reachable from the given source
if(arr[l] == 0)
return Integer.MAX_VALUE;

/*

// Function to find the minimum number of jumps required to reach the end of the array
public static int findMinimumJumps(int[] jumpDistances, int startingIndex, int endingIndex) {
// Base case: destination reached from the source itself
if (startingIndex == endingIndex) {
return 0;
}

// Base case: no jump possible from the source
if (jumpDistances[startingIndex] == 0) {
return Integer.MAX_VALUE;
}

/*
* Traverse through all the points reachable from arrays.array[1].
* Recursively get the minimum number of jumps needed to reach arrays.array[h] from these reachable points.
*/

int min = Integer.MAX_VALUE;
for(int i = l + 1 ; i <= h
&& i<= l + arr[l] ; i++) {
int jumps = minJumps(arr , i , h);
if(jumps != Integer.MAX_VALUE && jumps + 1 < min)
min = jumps + 1 ;
}
return min;
}

public static void main(String[] args) {
int arr[] = { 1, 3, 6, 3, 2, 3, 6, 8, 9, 5 };
int n = arr.length;
System.out.print("Minimum number of jumps to reach end is " + minJumps(arr , 0 , n -1 ));
}
// Explore all reachable destinations from the current position
int minJumps = Integer.MAX_VALUE;
for (int nextIndex = startingIndex + 1; nextIndex <= endingIndex && nextIndex <= startingIndex + jumpDistances[startingIndex]; nextIndex++) {
int jumpsFromNextIndex = findMinimumJumps(jumpDistances, nextIndex, endingIndex);

// Update minimum jumps if a valid path is found
if (jumpsFromNextIndex != Integer.MAX_VALUE && jumpsFromNextIndex + 1 < minJumps) {
minJumps = jumpsFromNextIndex + 1;
}
}

return minJumps;
}

public static void main(String[] args) {
int[] jumpDistances = {1, 3, 6, 3, 2, 3, 6, 8, 9, 5};
int numberOfElements = jumpDistances.length;

System.out.println("Minimum number of jumps to reach the end: " + findMinimumJumps(jumpDistances, 0, numberOfElements - 1));
}

}
12 changes: 6 additions & 6 deletions arrays/Array_Problem_17.java
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
package arrays;

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

// * You are allowed to buy and sell only once
// ! You are not allowed to sell first and then buy,
// ! You are not allowed to sell first and then buy,
// ! (you must buy first and then sell)

public class Array_Problem_17 {

public int maxProfit(int[] prices) {
public static int maxProfit(int[] prices) {
int mini = prices[0];
int maxProfit = 0;
int n = prices.size();

int n = prices.length;

for(int i = 0; i < n; i++) {
int cost = prices[i] - mini;
maxProfit = Math.max(maxProfit, cost);
mini = Math.min(mini, prices[i]);
}

return maxProfit;
}

Expand Down
55 changes: 28 additions & 27 deletions arrays/Array_Problem_19.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
package arrays;

/* Problem Title :-> find common elements in 3 sorted arrays */
/* Problem Title: Find common elements in 3 sorted arrays */
public class Array_Problem_19 {
// This function prints common elements in a1
void findCommon(int[] a1, int[] a2, int[] a3) {
// Initialize starting indexes for a1[], a2[] and a3[]
int i = 0, j = 0, k = 0;
// Iterate through three arrays while all arrays have elements
while (i < a1.length && j < a2.length && k < a3.length) {
// if x = y and y = z, print any of them and move ahead in all arrays
if (a1[i] >= a2[j] && a2[j] == a3[k]) {
System.out.print(a1[i] + " ");
i++;
k++;

// This function prints common elements in firstArray
void findCommon(int[] firstArray, int[] secondArray, int[] thirdArray) {
// Initialize starting indices for arrays
int firstIndex = 0, secondIndex = 0, thirdIndex = 0;

// Iterate through arrays while all have elements
while (firstIndex < firstArray.length && secondIndex < secondArray.length && thirdIndex < thirdArray.length) {
// If elements are equal, print any and move forward in all arrays
if (firstArray[firstIndex] == secondArray[secondIndex] && secondArray[secondIndex] == thirdArray[thirdIndex]) {
System.out.print(firstArray[firstIndex] + " ");
firstIndex++;
thirdIndex++;
} else if (firstArray[firstIndex] < secondArray[secondIndex]) {
// First element is smaller, so move its index
firstIndex++;
} else if (secondArray[secondIndex] < thirdArray[thirdIndex]) {
// Second element is smaller, so move its index
secondIndex++;
} else {
// Third element is smaller, so move its index
thirdIndex++;
}
// x < y
else if (a1[j] < a2[j])
i++;
// y < z
else if (a2[j] < a3[k])
j++;
// we reach here whem x > y and z < y, i.e., z is smallest
else
k++;
}
}

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

Array_Problem_19 ob = new Array_Problem_19();
int a1[] = { 1, 5, 10, 20, 40, 80 };
int a2[] = { 6, 7, 20, 80, 100 };
int a3[] = { 3, 4, 15, 20, 30, 70, 80, 120 };

Array_Problem_19 object = new Array_Problem_19();
int[] firstArray = {1, 5, 10, 20, 40, 80};
int[] secondArray = {6, 7, 20, 80, 100};
int[] thirdArray = {3, 4, 15, 20, 30, 70, 80, 120};

System.out.print("Common elements are ");
ob.findCommon(a1, a2, a3);
object.findCommon(firstArray, secondArray, thirdArray);
}
}
Loading

0 comments on commit f032874

Please sign in to comment.