Skip to content

Commit ed70e67

Browse files
Add Day 13 medium level array Programs
1 parent 1dc5ebe commit ed70e67

10 files changed

+555
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package array_Programming.medium.day_13;
2+
import java.util.Scanner;
3+
//3. Write a Java program to check if an array is a palindrome.
4+
public class CheckPalindromicArray
5+
{
6+
public static boolean isPalindrome(int[] arr)
7+
{
8+
for (int i = 0; i < arr.length / 2; i++)
9+
{
10+
if (arr[i] != arr[arr.length - 1 - i])
11+
{
12+
return false;
13+
}
14+
}
15+
return true;
16+
}
17+
18+
public static void checkPalindromicArray(int[] arr)
19+
{
20+
if (isPalindrome(arr))
21+
{
22+
System.out.println("The array is a palindrome.");
23+
}
24+
else
25+
{
26+
System.out.println("The array is not a palindrome.");
27+
}
28+
}
29+
30+
public static void main(String[] args)
31+
{
32+
Scanner sc = new Scanner(System.in);
33+
34+
System.out.println("Enter the size of the array:");
35+
int size = sc.nextInt();
36+
37+
int[] arr = new int[size];
38+
39+
System.out.println("Enter " + size + " elements:");
40+
for (int i = 0; i < arr.length; i++)
41+
{
42+
arr[i] = sc.nextInt();
43+
}
44+
45+
checkPalindromicArray(arr);
46+
47+
sc.close();
48+
}
49+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package array_Programming.medium.day_13;
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
//5. Write a Java program to check if two arrays are equal or not.
5+
public class CheckTwoArraysEqaul
6+
{
7+
public static boolean areArraysEqual(int[] arr1, int[] arr2)
8+
{
9+
if (arr1.length != arr2.length)
10+
{
11+
return false;
12+
}
13+
14+
for (int i = 0; i < arr1.length; i++)
15+
{
16+
if (arr1[i] != arr2[i])
17+
{
18+
return false;
19+
}
20+
}
21+
22+
return true;
23+
}
24+
public static void checkEquality(int[] arr1, int[] arr2)
25+
{
26+
if (areArraysEqual(arr1, arr2))
27+
{
28+
System.out.println("Both arrays are equal.");
29+
}
30+
else
31+
{
32+
System.out.println("Both arrays are not equal.");
33+
}
34+
}
35+
36+
public static void main(String[] args)
37+
{
38+
Scanner sc = new Scanner(System.in);
39+
System.out.println("Enter the size of the first array :");
40+
int n1 = sc.nextInt();
41+
int [] arr1 = new int[n1];
42+
System.out.println("Enter" + n1 + " elements");
43+
for(int i = 0;i<=arr1.length-1;i++)
44+
{
45+
arr1[i] =sc.nextInt();
46+
}
47+
System.out.println(Arrays.toString(arr1));
48+
49+
System.out.println("Enter the size of the second array :");
50+
int n2 = sc.nextInt();
51+
int [] arr2 = new int[n2];
52+
System.out.println("Enter" + n2 + " elements");
53+
54+
for(int j = 0;j<=arr2.length-1;j++)
55+
{
56+
arr2[j] = sc.nextInt();
57+
}
58+
System.out.println(Arrays.toString(arr2));
59+
checkEquality(arr1, arr2);
60+
sc.close();
61+
62+
63+
}
64+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package array_Programming.medium.day_13;
2+
//6. Count the number of elements in an array without using the .length property.
3+
import java.util.Scanner;
4+
public class CountElementsWithoutLength
5+
{
6+
public static int countElements(int[] arr)
7+
{
8+
int count = 0;
9+
10+
try
11+
{
12+
while (true)
13+
{
14+
int temp = arr[count];
15+
count++;
16+
}
17+
}
18+
catch (Exception e)
19+
{
20+
}
21+
22+
return count;
23+
}
24+
25+
public static void printCount(int[] arr)
26+
{
27+
int total = countElements(arr);
28+
System.out.println("Number of elements in the array: " + total);
29+
}
30+
31+
public static void main(String[] args)
32+
{
33+
Scanner sc = new Scanner(System.in);
34+
35+
System.out.println("Enter the size of the array:");
36+
int size = sc.nextInt();
37+
38+
int[] arr = new int[size];
39+
40+
System.out.println("Enter " + size + " elements:");
41+
for (int i = 0; i < size; i++)
42+
{
43+
arr[i] = sc.nextInt();
44+
}
45+
46+
printCount(arr);
47+
48+
sc.close();
49+
}
50+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package array_Programming.medium.day_13;
2+
//2. Write a Java program to find the first non-repeating element in an array.
3+
import java.util.Scanner;
4+
5+
public class FirstNonReapeatingElement
6+
{
7+
public static void printFirstNonRepeating(int[] arr)
8+
{
9+
boolean found = false;
10+
11+
for (int i = 0; i < arr.length; i++)
12+
{
13+
int count = 0;
14+
15+
for (int j = 0; j < arr.length; j++)
16+
{
17+
if (arr[i] == arr[j])
18+
{
19+
count++;
20+
}
21+
}
22+
23+
if (count == 1)
24+
{
25+
System.out.println("First non-repeating element: " + arr[i]);
26+
found = true;
27+
break;
28+
}
29+
}
30+
31+
if (!found)
32+
{
33+
System.out.println("No non-repeating element found.");
34+
}
35+
}
36+
public static void main(String[] args)
37+
{
38+
Scanner sc = new Scanner(System.in);
39+
40+
System.out.println("Enter the size of the array:");
41+
int size = sc.nextInt();
42+
43+
int[] arr = new int[size];
44+
45+
System.out.println("Enter " + size + " elements:");
46+
for (int i = 0; i < arr.length; i++)
47+
{
48+
arr[i] = sc.nextInt();
49+
}
50+
51+
printFirstNonRepeating(arr);
52+
53+
sc.close();
54+
}
55+
}
56+
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package array_Programming.medium.day_13;
2+
//7. Write a Java program to find the maximum product of two integers in an array.
3+
import java.util.Scanner;
4+
public class MaximumProductOfTwo
5+
{
6+
public static int findMaxProduct(int[] arr)
7+
{
8+
int max1 = Integer.MIN_VALUE;
9+
int max2 = Integer.MIN_VALUE;
10+
11+
int min1 = Integer.MAX_VALUE;
12+
int min2 = Integer.MAX_VALUE;
13+
14+
for (int i = 0; i < arr.length; i++)
15+
{
16+
int val = arr[i];
17+
if (val > max1)
18+
{
19+
max2 = max1;
20+
max1 = val;
21+
}
22+
else if (val > max2)
23+
{
24+
max2 = val;
25+
}
26+
if (val < min1)
27+
{
28+
min2 = min1;
29+
min1 = val;
30+
}
31+
else if (val < min2)
32+
{
33+
min2 = val;
34+
}
35+
}
36+
37+
int product1 = max1 * max2;
38+
int product2 = min1 * min2;
39+
40+
return Math.max(product1, product2);
41+
}
42+
43+
public static void printMaxProduct(int[] arr)
44+
{
45+
if (arr.length < 2)
46+
{
47+
System.out.println("Array must contain at least two elements.");
48+
return;
49+
}
50+
51+
int result = findMaxProduct(arr);
52+
System.out.println("Maximum product of two elements: " + result);
53+
}
54+
55+
public static void main(String[] args)
56+
{
57+
Scanner sc = new Scanner(System.in);
58+
59+
System.out.println("Enter the size of the array:");
60+
int size = sc.nextInt();
61+
62+
int[] arr = new int[size];
63+
64+
System.out.println("Enter " + size + " elements:");
65+
for (int i = 0; i < arr.length; i++)
66+
{
67+
arr[i] = sc.nextInt();
68+
}
69+
70+
printMaxProduct(arr);
71+
72+
sc.close();
73+
}
74+
}
75+
76+
77+
78+
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package array_Programming.medium.day_13;
2+
import java.util.Arrays;
3+
import java.util.Scanner;
4+
//10. write a java program to move all the zeros to the end of the array.
5+
public class MoveZerosToTheEnd
6+
{
7+
public static int[] moveZeros(int[] arr)
8+
{
9+
int[] result = new int[arr.length];
10+
int index = 0;
11+
for (int i = 0; i < arr.length; i++)
12+
{
13+
if (arr[i] != 0)
14+
{
15+
result[index++] = arr[i];
16+
}
17+
}
18+
return result;
19+
}
20+
public static void printArray(int[] arr)
21+
{
22+
System.out.println("Array after moving zeros to end:");
23+
System.out.println(Arrays.toString(arr));
24+
}
25+
26+
public static void main(String[] args)
27+
{
28+
Scanner sc = new Scanner(System.in);
29+
30+
System.out.println("Enter the size of the array:");
31+
int size = sc.nextInt();
32+
33+
int[] arr = new int[size];
34+
35+
System.out.println("Enter " + size + " elements:");
36+
for (int i = 0; i < arr.length; i++)
37+
{
38+
arr[i] = sc.nextInt();
39+
}
40+
41+
int[] result = moveZeros(arr);
42+
printArray(result);
43+
44+
sc.close();
45+
}
46+
}
47+
48+
49+

0 commit comments

Comments
 (0)