Skip to content

Commit 4e69287

Browse files
committed
#Modification 16
1 parent c83e9d3 commit 4e69287

132 files changed

Lines changed: 5043 additions & 1 deletion

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
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+
}
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+
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+
}
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+
}
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+
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package arrays;
2+
3+
/*
4+
* find all pairs on integer array whose sum is equal to given number
5+
* OR
6+
* Equal_Sum_Pairs_of_Array
7+
*/
8+
9+
public class Array_Problem_18 {
10+
11+
//function to find & print the sum & elements
12+
static void pairs_value(int iA[], int iN) {
13+
System.out.println("Pairs of elements & their sum : ");
14+
15+
//loop to iterate & find the pair of elements whose sum is equal
16+
for(int i = 0 ; i < iA.length ; i++) {
17+
for(int j = i + 1 ; j < iA.length ; j++)
18+
//check if the sum of pair is equal, if equal then print the pair of elements & their sum
19+
if(iA[i]+iA[j] == iN) System.out.println(iA[i] + " + " + iA[j] + " = " + iN);
20+
}
21+
}
22+
23+
//Driver Method
24+
public static void main(String[] args) {
25+
pairs_value(new int[] {2,7,4,-5,11,5,20}, 15);
26+
pairs_value(new int[] {14,-15,9,16,25,45,12,8},30);
27+
}
28+
29+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package arrays;
2+
3+
//Java program to find the maximum & minimum element in given or array as user input
4+
public class Array_Problem_2 {
5+
/* Class Pair is used to return two values from getMinMax() */
6+
static class Pair {
7+
8+
int min;
9+
int max;
10+
}
11+
12+
static Pair getMinMax(int arr[], int n) {
13+
Pair minmax = new Pair();
14+
int i;
15+
16+
/*If there is only one element then return it as min and max both*/
17+
if (n == 1) {
18+
minmax.max = arr[0];
19+
minmax.min = arr[0];
20+
return minmax;
21+
}
22+
23+
/* If there are more than one elements, then initialize min
24+
and max*/
25+
if (arr[0] > arr[1]) {
26+
minmax.max = arr[0];
27+
minmax.min = arr[1];
28+
} else {
29+
minmax.max = arr[1];
30+
minmax.min = arr[0];
31+
}
32+
33+
for (i = 2; i < n; i++) {
34+
if (arr[i] > minmax.max) {
35+
minmax.max = arr[i];
36+
} else if (arr[i] < minmax.min) {
37+
minmax.min = arr[i];
38+
}
39+
}
40+
41+
return minmax;
42+
}
43+
44+
/* Driver program to test above function */
45+
public static void main(String args[]) {
46+
int arr[] = {1000, 11, 445, 1, 330, 3000};
47+
int arr_size = 6;
48+
Pair minmax = getMinMax(arr, arr_size);
49+
System.out.printf("\nMinimum element is %d", minmax.min);
50+
System.out.printf("\nMaximum element is %d", minmax.max);
51+
52+
}
53+
54+
}
55+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package arrays;
2+
3+
/*
4+
* Java program for maximum product sub-array problem
5+
*/
6+
7+
public class Array_Problem_23 {
8+
9+
static void maxSubArraySum(int a[], int size)
10+
{
11+
int max_so_far = Integer.MIN_VALUE,
12+
max_ending_here = 0,start = 0,
13+
end = 0, s = 0;
14+
15+
for (int i = 0; i < size; i++)
16+
{
17+
max_ending_here += a[i];
18+
19+
if (max_so_far < max_ending_here)
20+
{
21+
max_so_far = max_ending_here;
22+
start = s;
23+
end = i;
24+
}
25+
26+
if (max_ending_here < 0)
27+
{
28+
max_ending_here = 0;
29+
s = i + 1;
30+
}
31+
}
32+
System.out.println("Maximum contiguous sum is " + max_so_far);
33+
System.out.println("Starting index " + start);
34+
System.out.println("Ending index " + end);
35+
}
36+
37+
// Driver code
38+
public static void main(String[] args)
39+
{
40+
int a[] = { -2, -3, 4, -1, -2, 1, 5, -3 };
41+
int n = a.length;
42+
maxSubArraySum(a, n);
43+
}
44+
}
45+

0 commit comments

Comments
 (0)