Skip to content

Commit c57c742

Browse files
committed
2 parents 3c2e9c2 + fc390b5 commit c57c742

210 files changed

Lines changed: 9413 additions & 0 deletions

File tree

Some content is hidden

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

README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,16 @@
1+
<<<<<<< HEAD
12
# Java-Programs
23
This is a repository where, I will upload and keep up to date my Java, DataStructure and algorithms code and question regarding to them,which i practiced on eclipse & IntelliJ with help of Books and programming websites. The books I prefer and use for learning java and algorithms are:
34

45
1. Java for Dummies.
56
2. Java The Complete Reference by Herberth Schildt.
67
3. Algorithms by Sedwig.
78
4. Algorithms by Cormen.
9+
=======
10+
# Java-Programs <a href="https://www.java.com" target="_blank"> <img src="https://devicons.github.io/devicon/devicon.git/icons/java/java-original-wordmark.svg" alt="java" width="40" height="40"/> </a>
11+
This is a repository where, I will upload and keep up to date my Java, DataStructure and algorithms code and question regarding to them,which i practiced on eclipse & IntelliJ with help of Books and programming websites. The books I prefer and use for learning java and algorithms are:
12+
13+
1. Java for Dummies.
14+
2. Algorithms by Sedwig.
15+
3. Algorithms by Cormen.
16+
>>>>>>> fc390b5cc5867b507eabc367ba4f0b0ac46bf243
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package array;
2+
import java.util.*;
3+
4+
//Language: Java
5+
//Time Complexity: O(n) 3 Linear traversals.
6+
//Space Complexity: O(n) Array of candies.
7+
8+
public class Candy_Distribution_Problem{
9+
10+
public int candy(int[] ratings) {
11+
if (ratings.length < 2)
12+
return ratings.length;
13+
14+
15+
int[] candies = new int[ratings.length];
16+
Arrays.fill(candies, 1);
17+
18+
// ** Step 1: Forward **
19+
for (int i=0; i<ratings.length-1; i++) {
20+
if (ratings[i] >= ratings[i+1]) {
21+
continue;
22+
}
23+
candies[i+1] = candies[i] + 1;
24+
}
25+
26+
// ** Step 2: Backward **
27+
for (int i=ratings.length-1; i>0; i--) {
28+
if (ratings[i] >= ratings[i-1]) {
29+
continue;
30+
}
31+
candies[i-1] = Math.max(candies[i] + 1, candies[i-1]);
32+
}
33+
34+
// ** Step 3: Count Candies **
35+
int count = 0;
36+
for (int i=0; i<candies.length; i++) {
37+
count += candies[i];
38+
}
39+
40+
return count;
41+
}
42+
43+
public static void main(String[] args) {
44+
@SuppressWarnings("unused")
45+
int[] ratings = {12 , 34 , 45 , 67 , 43 , 75};
46+
}
47+
48+
}
49+

array/KadanesAlgorithm.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package array;
2+
import java.util.*;
3+
4+
/*
5+
* Kadane's Algorithm to find the maxSubarray Sum in O(n)[Linear Time Complexity]
6+
*/
7+
public class KadanesAlgorithm {
8+
9+
public static void main(String[] args) {
10+
@SuppressWarnings("resource")
11+
Scanner sc = new Scanner(System.in);
12+
int n = sc.nextInt();
13+
int a[]=new int[n];
14+
15+
for(int i=0;i<n;i++)
16+
a[i] = sc.nextInt();
17+
18+
int maxSum = 0;
19+
int currentSum = 0;
20+
21+
for(int i=0 ;i<n ;i++){
22+
currentSum += a[i];
23+
if(currentSum<0)
24+
currentSum=0;
25+
26+
maxSum = Math.max(maxSum, currentSum);
27+
}
28+
29+
System.out.println(maxSum);
30+
}
31+
}

arrays/ArrayLevel1.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package arrays;
2+
3+
public class ArrayLevel1 {
4+
5+
public static void main(String[] args) {
6+
int[] arr;
7+
arr = new int[5];
8+
9+
arr[0] = 10;
10+
arr[1] = 20;
11+
arr[2] = 30;
12+
arr[3] = 40;
13+
arr[4] = 50;
14+
15+
for(int i = 0 ; i <arr.length ; i++) System.out.println("Element at index " + i + " : " + arr[i]);
16+
}
17+
}

arrays/ArrayLevel2.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package arrays;
2+
3+
import java.io.*;
4+
5+
//Array Implementation using try-catch & Buffers
6+
public class ArrayLevel2 {
7+
8+
public static void main(String[] args) {
9+
10+
int a[];
11+
a = new int[5];
12+
13+
try {
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
for(int i=0;i<5;i++) {
16+
int j=i+1;
17+
System.out.println(" Enter element no." +j);
18+
String s = br.readLine();
19+
a[i] = Integer.parseInt(s);
20+
}
21+
for(int i=0;i<5;i++) {
22+
System.out.println("arr["+i+"]="+a[i]);
23+
}
24+
}
25+
26+
catch(Exception e) {
27+
System.out.println(""+e);
28+
}
29+
}
30+
}
31+
32+
33+
34+

arrays/Array_Problem_1.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package arrays;
2+
3+
//Java program to reverse the given array
4+
public class Array_Problem_1 {
5+
6+
static void rvereseArray(int arr[],int start, int end)
7+
{
8+
int temp;
9+
10+
while (start < end)
11+
{
12+
temp = arr[start];
13+
arr[start] = arr[end];
14+
arr[end] = temp;
15+
start++;
16+
end--;
17+
}
18+
}
19+
20+
/* Utility that prints out an
21+
array on a line */
22+
static void printArray(int arr[], int size)
23+
{
24+
for (int i = 0; i < size; i++)
25+
System.out.print(arr[i] + " ");
26+
27+
System.out.println();
28+
}
29+
30+
// Driver code
31+
public static void main(String args[]) {
32+
33+
int arr[] = {1, 2, 3, 4, 5, 6};
34+
printArray(arr, 6);
35+
rvereseArray(arr, 0, 5);
36+
System.out.print("Reversed array is \n");
37+
printArray(arr, 6);
38+
39+
}
40+
41+
}

arrays/Array_Problem_10.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package arrays;
2+
import java.util.*;
3+
import java.io.*;
4+
5+
/*
6+
* Problem :-
7+
* Minimum no of jumps to reach end of an array
8+
*/
9+
10+
/*
11+
* Understanding of The Problem: -
12+
*
13+
* Given an array of integers where each element represents the max number of steps that can be made forward from the element
14+
* Write a function to return the minimum no of jumps to reach the end of the array(starting from the first element).
15+
* If an element is 0, they cannot move through that element.
16+
*/
17+
@SuppressWarnings("unused")
18+
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+
/*
31+
* Traverse through all the points reachable from array[1].
32+
* Recursively get the minimum number of jumps needed to reach array[h] from these reachable points.
33+
*/
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+
}
50+
51+
}

arrays/Array_Problem_11.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package arrays;
2+
3+
/*
4+
* find duplicate in an array of N+1 Integers
5+
*/
6+
public class Array_Problem_11 {
7+
8+
// Implementing an array as a HashMap for finding duplicate elements
9+
void printRepeating(int m[],int size) {
10+
int i;
11+
System.out.print("The repeating elements are: ");
12+
13+
for(i = 0 ; i < size ; i++) {
14+
// using Math.abs() function to find the duplicate element in HashMap.
15+
if(m[Math.abs(m[i])]>=0) m[Math.abs(m[i])] = -m[Math.abs(m[i])];
16+
else System.out.print(Math.abs(m[i]) + " ");
17+
}
18+
}
19+
20+
//Driver Method
21+
public static void main(String[] args) {
22+
23+
Array_Problem_11 dublicate= new Array_Problem_11();
24+
25+
int m[] = {1,2,3,1,3,6,6};
26+
int m_size = m.length;
27+
28+
dublicate.printRepeating(m,m_size);
29+
30+
}
31+
32+
}
33+

arrays/Array_Problem_12.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package arrays;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
* Problem :-
7+
* Merge two sorted array without using extra space
8+
*/
9+
10+
/*
11+
* Understanding of the Problem :-
12+
*
13+
* We are given two sorted array.
14+
* We need to merge these two arrays,
15+
* such that the initial numbers(after complete sorting) are in the first array ,
16+
* and the remaining numbers are in the second array.
17+
* Extra space allowed in O(1).
18+
*/
19+
20+
/*
21+
* Simple Discussion :
22+
* This task is simple and O(m+n) if we are allowed to use extra space.
23+
* But it becomes really complicated when extra space is not allowed,
24+
* and doesn't look possible in less than O(m*n) worst case time.
25+
*/
26+
27+
/*
28+
* Idea or Approach of Solution :-
29+
* The idea is to begin from last element of ar2[] and search it in ar1[].
30+
* If there is a greater element in ae1[], then we moe lastt element of ar2[] at correct place in ar1[].
31+
*
32+
* We can use INSERTION Sort type of insertion for this.
33+
*/
34+
35+
public class Array_Problem_12 {
36+
37+
static int[] arr1 = new int[] {1, 5 , 9, 10, 15, 20};
38+
static int[] arr2 = new int[] {2, 3, 8, 13};
39+
40+
static void merge(int m , int n) {
41+
42+
//Iterate through all elements of the last element
43+
for(int i = n-1; i >= 0; i--) {
44+
45+
/*
46+
* FInd the smallest element greater than ar2[i].
47+
* Move all elements one position ahead till the smallest greater element is not found.
48+
*/
49+
int j , last = arr1[m -1];
50+
for( j = m-2 ; j>= 0 && arr1[j] > arr2[i] ; j--)
51+
arr1[j+1] = arr1[j];
52+
53+
//if there was a greater element
54+
if(j != m-2 || last > arr2[i]) {
55+
arr1[j+1] = arr2[i];
56+
arr2[i] = last;
57+
}
58+
}
59+
}
60+
61+
public static void main(String[] args) {
62+
merge(arr1.length, arr2.length);
63+
System.out.println("After Merging nFirst Array: ");
64+
System.out.println(Arrays.toString(arr1));
65+
System.out.println("Second Array: ");
66+
System.out.println(Arrays.toString(arr2));
67+
}
68+
69+
}

arrays/Array_Problem_13.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package arrays;
2+
import java.util.*;
3+
4+
/* Problem Title :-> Kadane's Algo [V.V.V.V.V IMP] */
5+
public class Array_Problem_13 {
6+
public static void main(String[] args) {
7+
@SuppressWarnings("resource")
8+
Scanner sc = new Scanner(System.in);
9+
int n = sc.nextInt();
10+
int[] a=new int[n];
11+
12+
for(int i=0;i<n;i++)
13+
a[i] = sc.nextInt();
14+
15+
int maxSum = 0;
16+
int currentSum = 0;
17+
18+
for(int i=0 ;i<n ;i++){
19+
currentSum += a[i];
20+
if(currentSum<0)
21+
currentSum=0;
22+
23+
maxSum = Math.max(maxSum, currentSum);
24+
}
25+
26+
System.out.println(maxSum);
27+
}
28+
}

0 commit comments

Comments
 (0)