Skip to content

Commit e0301e0

Browse files
committed
Array Programs
1 parent 7e1ee19 commit e0301e0

20 files changed

+41
-45
lines changed

InterviewPrograms/src/com/java/array/ArrayRotation.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/*
66
* Rotate the Given array d times
7-
*
7+
* -------------------------------
88
* Write java program to rotate (clockwise) the given array
99
* d number of times.
1010
*

InterviewPrograms/src/com/java/array/Average.java

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
package com.java.array;
22

33
/*
4-
* Calculate the Average of Given Array
5-
*
4+
* Calculate the Average of Given Array
5+
* -------------------------------------
6+
*
67
* This is one basic program using array.
78
* Steps
89
* 1. Declare a variable sum with 0

InterviewPrograms/src/com/java/array/FindAllDuplicates.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/*
66
* Find All duplicate elements
7-
*
7+
* ---------------------------
88
* Write java program to find all the duplicates
99
* elements from the given array.
1010
*

InterviewPrograms/src/com/java/array/FindMissingNo.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.java.array;
22
/*
3-
* Find the Missing Number in 1 to N
4-
*
3+
* Find the Missing Number in 1 to N
4+
* ----------------------------------
55
* An array contains 1 to N numbers
66
* but one number in the array is missing
77
* Write the java program to find the missing no
@@ -25,6 +25,7 @@
2525
* missing no = 45 - 37
2626
*
2727
* Note::
28+
* ------
2829
* This approach will work only for array contains
2930
* value from 1 to n (order does not matter)
3031
*/
@@ -40,7 +41,7 @@ public static void main(String[] args) {
4041
arraySum += v;
4142

4243
//array length is 8
43-
//one no is missing, then n should by n+1
44+
//one no is missing, then n should be n+1
4445
// here 8+1
4546
n = n+1;
4647

InterviewPrograms/src/com/java/array/FindMostFrequent.java

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/*
44
* Find the Most Frequent Element in an Array
5+
* -------------------------------------------
56
* Using Brute Force Approach
67
*
78
* For the each element, scan the entire array

InterviewPrograms/src/com/java/array/FindMostFrequentUsingHashtable.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/*
66
* Find the Most Frequent Element in an Array
7-
*
7+
* ------------------------------------------
88
* Using Hash Table
99
*
1010
* We can create a hash table and store elements and

InterviewPrograms/src/com/java/array/FindMostFrequentUsingSorting.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/*
66
* Find the Most Frequent Element in an Array
7-
*
7+
* ------------------------------------------
88
* Using Sorting
99
*
1010
* Lets sort the given array.

InterviewPrograms/src/com/java/array/FindPairForZ.java

+2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
/*
66
* Find the Pair (X,Y) whose sum is Z
7+
* -----------------------------------
78
*
89
* say the given array is {2,45,7,3,5,1,8,9}
910
* and Z = 10
1011
* Find the pairs (x,y) whose sum is 10
1112
* (2,8), (1,9), (3,7) are the pairs
1213
*
1314
* Solution
15+
* ---------
1416
* 1. Sort the given array (ascending order)
1517
* 2. Declare left pointer (l) which points to
1618
* left most element of the array (min of array)

InterviewPrograms/src/com/java/array/FindUniqueElt.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
/*
44
* Find the only one unique Element in the Array
5+
* ---------------------------------------------
56
*
67
* In the given array only one element is
78
* occurred one time, all the other elements are
@@ -16,7 +17,7 @@
1617
* Write the program to calculate 6
1718
*
1819
* Solution
19-
*
20+
* --------
2021
* use ^ (XOR) operator
2122
* apply ^ (XOR) operator to all the values
2223
* in the array.

InterviewPrograms/src/com/java/array/FindingAllSubsets.java

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
package com.java.array;
22

33
/*
4-
* Find All Possible Subsets of Given Array
4+
* Find All Possible Subsets of Given Array
5+
* ----------------------------------------
6+
* say Given array is
7+
* {a, b, c, d}
58
*
6-
* say Given array is
7-
* {a, b, c, d}
8-
*
9-
* Possible subsets are
10-
* {a} {b} {c} {d}
11-
* {a, b} {a, c} {a, d} {b, c} {b, d} {c, d}
12-
* {a, b, c} {a, b, d} {a, c, d} {b, c, d}
13-
* {a, b, c, d}
9+
* Possible subsets are
10+
* {a} {b} {c} {d}
11+
* {a, b} {a, c} {a, d} {b, c} {b, d} {c, d}
12+
* {a, b, c} {a, b, d} {a, c, d} {b, c, d}
13+
* {a, b, c, d}
1414
*
1515
*/
1616
public class FindingAllSubsets {

InterviewPrograms/src/com/java/array/IntersectionOfSortedArrays.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.java.array;
22

33
/*
4-
* Intersection of two Sorted Arrays
4+
* Intersection of two Sorted Arrays
5+
* ----------------------------------
56
*
67
* Assume that both the given arrays are
78
* sorted arrays.
@@ -59,7 +60,6 @@ public static void main(String[] args) {
5960
}
6061
/*
6162
OUTPUT
62-
6363
Given array 1 is :
6464
1 2 3 4 5 6
6565
Given array 2 is :

InterviewPrograms/src/com/java/array/IntersectionOfTwoArrays.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
package com.java.array;
22

33
/*
4-
* Find Intersection of Two Arrays
5-
*
6-
* Write a Java Program to find the intersection of
7-
* Two Arrays
4+
* Find Intersection of Two Arrays
5+
* --------------------------------
86
*
97
* INTERSECTION
108
* Common element in both arrays.

InterviewPrograms/src/com/java/array/MoveAllZerosToEnd.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
* Move All the Zeros to end of the Array
5-
*
5+
* ---------------------------------------
66
* Assume the given array contains some zero
77
* in some index positions.
88
*

InterviewPrograms/src/com/java/array/MoveAllZerosToFront.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
/*
44
* Move All the Zeros to Start of the Array
5-
*
5+
* -----------------------------------------
66
* Assume the given array contains some zero
77
* in some index positions.
88
*

InterviewPrograms/src/com/java/array/ReverseArray.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.java.array;
22

33
/*
4-
* Reverse the Array without using additional Array
4+
* Reverse the Array without using additional Array
5+
* ------------------------------------------------
56
*
67
* given array[]= {1,2,3,4,5,6,7,8,9,10};
78
* reversed array[]= {10,9,8,7,6,5,4,3,2,1};
@@ -22,7 +23,6 @@
2223

2324
public class ReverseArray {
2425
public static void main(String[] args) {
25-
2626
int array[]= {1,2,3,4,5,6,7,8,9,10};
2727
int N = array.length;
2828
int temp;

InterviewPrograms/src/com/java/array/SecondMaximum.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
/*
66
* Find the Second Maximum in the Given Array
7-
*
7+
* ------------------------------------------
88
* array {10,32,34,54,19,29,38,45}
99
* max is 54, second max is 45
1010
*
@@ -27,9 +27,7 @@
2727
* then update only 2nd max value with ith value
2828
*/
2929
public class SecondMaximum {
30-
3130
public static void main(String[] args) {
32-
3331
int array[] = {10,32,34,54,19,29,38,45};
3432

3533
//assume 0th index value is maximum
@@ -67,7 +65,6 @@ public static void main(String[] args) {
6765

6866
/*
6967
OUTPUT
70-
7168
Given array is :
7269
[10, 32, 34, 54, 19, 29, 38, 45]
7370
Max value in the array is : 54

InterviewPrograms/src/com/java/array/SecondMinimum.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
/*
66
* Find the Second Minimum in the Given Array
7+
* -------------------------------------------
78
*
89
* array {10,32,34,54,19,29,38,45}
910
* minimum is 10, second minimum is 19
@@ -28,7 +29,6 @@
2829
*/
2930
public class SecondMinimum {
3031
public static void main(String[] args) {
31-
3232
int array[] = {10,32,34,54,19,29,38,45};
3333

3434
// assume 0th index value is minimum
@@ -65,12 +65,9 @@ public static void main(String[] args) {
6565
}
6666
}
6767
/*
68-
6968
OUTPUT
70-
7169
Given array is :
7270
[10, 32, 34, 54, 19, 29, 38, 45]
7371
Minimum value in the array is : 10
7472
Second minimum value in the array is : 19
75-
7673
*/

InterviewPrograms/src/com/java/array/UnionArrayUsingHashSet.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import java.util.HashSet;
55

66
/*
7-
* Union Of Two Arrays using HashSet
8-
*
7+
* Union Of Two Arrays using HashSet
8+
* ---------------------------------
99
* The Union of Two Arrays Array1, Array2 is the
1010
* new array which contains all the elements which are
1111
* either in array1 or in array2, or in both arrays.
@@ -58,7 +58,6 @@ public static void main(String[] args) {
5858
}
5959
/*
6060
OUTPUT
61-
6261
Given Array 1 is :
6362
17 23 31 14 25 26
6463
Given Array 2 is :

InterviewPrograms/src/com/java/array/UnionOfSortedArrays.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
import java.util.ArrayList;
55

66
/*
7-
* Union Of Two Sorted Arrays
8-
*
7+
* Union Of Two Sorted Arrays
8+
* --------------------------
99
* The Union of Two Arrays Array1, Array2 is the
1010
* new array which contains all the elements which are
1111
* either in array1 or in array2, or in both arrays.

InterviewPrograms/src/com/java/array/UnionOfTwoArrays.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package com.java.array;
22

33
/*
4-
* Union Of Two Arrays
5-
*
4+
* Union Of Two Arrays
5+
* -------------------
66
* The Union of Two Arrays Array1, Array2 is the
77
* new array which contains all the elements which are
88
* either in array1 or in array2, or in both arrays.
@@ -21,7 +21,6 @@
2121
*
2222
* UnionArray = {17, 23, 31, 14, 25, 26, 44, 8};
2323
*
24-
*
2524
*/
2625
public class UnionOfTwoArrays {
2726
public static void main(String[] args) {

0 commit comments

Comments
 (0)