Skip to content

Commit 4a013bb

Browse files
Array
Duration 9 day It is taking an extended amount of time because this repository has become a secondary priority. Our primary focus is currently on the JavaScript repository.
1 parent 6fdf8b1 commit 4a013bb

14 files changed

+284
-0
lines changed

Array/Creation.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
3+
public class Creation {
4+
public static void main(String[] args) {
5+
int arrInt[] = new int[2];
6+
String arrString[] = new String[4];
7+
char[] ch = {'a','b','c'};
8+
9+
// Inserting value's
10+
arrInt[0] = 2;
11+
arrInt[1] = 2;
12+
13+
arrString[0] = "4";
14+
arrString[3] = "Four";
15+
arrString[2] = "two";
16+
17+
ch[2] = 'd';
18+
19+
// Output
20+
System.out.println(Arrays.toString(arrString));
21+
System.out.println(Arrays.toString(arrInt));
22+
System.out.println(Arrays.toString(ch));
23+
}
24+
}

Array/binarySearch.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class binarySearch {
2+
public static int binarySearch(int[] arr, int key) {
3+
int beg = 0;
4+
int end = arr.length;
5+
for (int i=0; i<=arr.length; i++) {
6+
int temp = end + beg;
7+
if (arr[temp/2] == key) {
8+
return (temp/2)+1;
9+
} else if (arr[temp/2] > key) {
10+
end = temp/2;
11+
} else if (arr[temp/2] < key) {
12+
beg = temp/2;
13+
}
14+
}
15+
return -1;
16+
}
17+
public static void main(String[] args) {
18+
int[] arr = {2,4,6,8,9};
19+
20+
System.out.print(binarySearch(arr, 4));
21+
}
22+
}

Array/inFunction.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
public class inFunction {
2+
public static void update(int[] arr) {
3+
for (int i=0; i<arr.length; i++) { arr[i] += 1; System.out.println(arr[i]); }
4+
}
5+
public static void main(String[] args) {
6+
int arr[] = {2,5,2,7};
7+
8+
// Call by reference
9+
update(arr);
10+
11+
// Value is updated here for array
12+
}
13+
}

Array/kadaneMaxSubArray.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// 03) Kadane-s algorithm
2+
// Best aproch
3+
// +ve + +ve = +ve Y
4+
// +ve + -ve = +ve Y
5+
// +ve + ---ve = -ve X
6+
// Time Complexity = O(N)
7+
8+
public class kadaneMaxSubArray {
9+
public static void main(String[] args) {
10+
int arr[] = {1, -2, 6, -1, 3};
11+
int currentSum = 0, maxSum = Integer.MIN_VALUE;
12+
13+
for (int i=0; i<arr.length; i++) {
14+
currentSum += arr[i];
15+
if (currentSum < 0) currentSum = 0;
16+
maxSum = Math.max(maxSum, currentSum);
17+
}
18+
System.out.print(maxSum);
19+
}
20+
}

Array/largest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import java.util.Arrays;
2+
3+
public class largest {
4+
public static int largest(int[] arr) {
5+
int largest = Integer.MIN_VALUE;
6+
for (int i=0; i<arr.length; i++) {
7+
if (largest < arr[i]) {
8+
largest = arr[i];
9+
}
10+
}
11+
return largest;
12+
}
13+
14+
public static void main(String[] args) {
15+
int[] arr = {4, 3, 5, -2, -6, -3, -9};
16+
System.out.print(largest(arr)+"\n");
17+
18+
// Using inbuilt methods
19+
System.out.println(Arrays.stream(arr).max());
20+
}
21+
}

Array/linearSearch.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class linearSearch {
2+
public static int linearSearch(int[] arr, int find) {
3+
for (int i=0; i<arr.length; i++) {
4+
if (arr[i] == find) { return i+1; }
5+
}
6+
return -1;
7+
}
8+
9+
public static void main(String[] args) {
10+
int[] arr = {1,2,3,4,5,6,7,8,9,0};
11+
int find = 10;
12+
System.out.print(linearSearch(arr, find));
13+
}
14+
}

Array/maxSubArray.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Max Sub Array
2+
// Applied Brute Force
3+
// TC - O(N^3)
4+
5+
public class maxSubArray {
6+
public static void maxSub(int arr[]) {
7+
int maxSum = 0;
8+
for (int k=0; k<arr.length; k++) {
9+
int temp = arr[k];
10+
for (int i=k; i<arr.length; i++) {
11+
int tem = 0;
12+
for (int j=k; j<=i; j++) {
13+
tem += arr[j];
14+
}
15+
if (tem >= temp) {
16+
temp = tem;
17+
}
18+
}
19+
if (maxSum <= temp) {
20+
maxSum = temp;
21+
}
22+
System.out.println(temp);
23+
}
24+
System.out.println("Max Sum : "+maxSum);
25+
}
26+
27+
public static void main(String[] args) {
28+
int[] arr = {1, -2, 6, -1, 3};
29+
maxSub(arr);
30+
}
31+
}

Array/pairArray.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
public class pairArray {
2+
public static void main(String[] args) {
3+
int[] arr = {1,2,3,4,5,6,7,8,9};
4+
5+
for (int i=0; i<arr.length; i++) {
6+
for (int j=i+1; j<arr.length; j++) {
7+
System.out.print("("+arr[i]+","+arr[j]+") ");
8+
}
9+
System.out.println();
10+
}
11+
}
12+
}

Array/prefixMaxSubArray.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// 02) Find the Max Sub Array
2+
// Time complexity = O(N^2)
3+
4+
public class prefixMaxSubArray {
5+
public static void main(String[] args) {
6+
int arr[] = {1, -2, 6, -1, 3};
7+
int maxValue = Integer.MIN_VALUE;
8+
9+
// Calculation within array
10+
for (int j=0; j<arr.length; j++) {
11+
int prefix[] = new int[arr.length];
12+
13+
for (int i=j; i<arr.length; i++) {
14+
if (i != 0) {
15+
prefix[i] = arr[i] + prefix[i - 1];
16+
if (maxValue < prefix[i]) maxValue = prefix[i];
17+
} else {
18+
maxValue = arr[i];
19+
prefix[i] = arr[i];
20+
}
21+
}
22+
}
23+
System.out.println("Max : "+maxValue);
24+
}
25+
}

Array/reverseArray.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
public class reverseArray {
2+
public static void main(String[] args) {
3+
int[] arr = {1,2,3,4,5,6,7,8,9};
4+
5+
for (int i=0; i<arr.length/2; i++) {
6+
int end = arr.length-i-1;
7+
8+
// Swap
9+
int temp = arr[i];
10+
arr[i] = arr[end];
11+
arr[end] = temp;
12+
}
13+
for (int i=0; i<arr.length; i++) System.out.print(arr[i]+" ");
14+
}
15+
}

Array/singleSubArray.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
public class singleSubArray {
2+
public static void main(String[] args) {
3+
int arr[] = {1, -2, 6, -1, 3};
4+
int prefix[] = new int[arr.length];
5+
6+
// Calculation within array
7+
for (int i=0; i<arr.length; i++)
8+
prefix[i] = (i != 0) ? (arr[i] + prefix[i - 1]) : arr[i];
9+
10+
// Print the calculated value/prefix array
11+
for (int i=0; i<prefix.length; i++)
12+
System.out.println(prefix[i]);
13+
}
14+
}

Array/stockBuyAndSell.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Approch : When stock price goes low buy and sell after buying with the profit otherwise return 0
2+
3+
public class stockBuyAndSell {
4+
public static void main(String[] args) {
5+
int[] stockPrice = {4,1,3,6,5};
6+
7+
int buyStock = Integer.MAX_VALUE;
8+
int maxProfit = 0;
9+
10+
for (int i=0; i<stockPrice.length; i++) {
11+
if (buyStock > stockPrice[i]) {
12+
buyStock = stockPrice[i];
13+
} else {
14+
int profit = stockPrice[i] - buyStock;
15+
maxProfit = Math.max(profit, maxProfit);
16+
}
17+
}
18+
System.out.print(maxProfit);
19+
}
20+
}

Array/subArray.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Print Sub-Array
2+
public class subArray {
3+
public static void main(String[] args) {
4+
int[] arr = {2,4,6,8,10};
5+
int count = 0;
6+
7+
for (int i=0; i<arr.length; i++) {
8+
int start = i;
9+
10+
for (int j=i; j<arr.length; j++) {
11+
int end = j;
12+
int temp = 0;
13+
14+
for (int k=start; k<=end; k++) {
15+
System.out.print(arr[k]+" ");
16+
temp += arr[k];
17+
}
18+
System.out.println("\nSum : "+temp);
19+
System.out.println();
20+
21+
count++;
22+
}
23+
System.out.println();
24+
}
25+
System.out.print("\n"+count);
26+
}
27+
}

Array/trappingWater.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
public class trappingWater {
2+
public static void main(String[] args) {
3+
int[] arr = {0,1,0,2,1,0,1,3,2,1,2,1};
4+
5+
int currentLeft = 0, currentRight = 0, totalTrap = 0;
6+
7+
int[] temp = new int[arr.length];
8+
for (int j=0; j<arr.length; j++) {
9+
int maxRight = Math.max(currentRight, arr[arr.length-j-1]);
10+
currentRight = maxRight;
11+
temp[arr.length-j-1] = maxRight;
12+
}
13+
14+
for (int i=0; i<arr.length; i++) {
15+
int maxLeft = Math.max(currentLeft,arr[i]);
16+
currentLeft = maxLeft;
17+
18+
int maxRight = temp[i];
19+
20+
int minOf2Method = Math.min(maxRight, maxLeft);
21+
int currentTrap = minOf2Method - arr[i];
22+
totalTrap += currentTrap;
23+
}
24+
System.out.print(totalTrap);
25+
}
26+
}

0 commit comments

Comments
 (0)