Skip to content

Commit 428b69d

Browse files
authored
Merge pull request #1065 from TANISHA3665/main
Create Palindrome_Number_Java.java
2 parents 3fc1c89 + 7c08b75 commit 428b69d

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
3+
Problem Statement :
4+
5+
Write a function that takes in an array of at least three integers and, without sorting the input array, returns a sorted array of the three largest integers in the input array.
6+
7+
Explanation:
8+
9+
Here's how the function works:
10+
11+
1. We create a new array result with three elements to store the three largest integers.
12+
2. We loop through the input array, and for each integer, we compare it to the largest integer in result.
13+
3. If the integer is larger than the largest integer in result, we shift the elements in result down one position and add the integer to the end.
14+
4. If the integer is larger than the second largest integer in result, we shift the elements in result down one position starting from the second position and add the integer to the second position.
15+
5. If the integer is larger than the third largest integer in result, we add the integer to the third position.
16+
6. At the end of the loop, result will contain the three largest integers in the input array, sorted in descending order.
17+
18+
*/
19+
20+
public static int[] findThreeLargest(int[] array) {
21+
int[] result = new int[3];
22+
for (int num : array) {
23+
if (num > result[2]) {
24+
result[0] = result[1];
25+
result[1] = result[2];
26+
result[2] = num;
27+
} else if (num > result[1]) {
28+
result[0] = result[1];
29+
result[1] = num;
30+
} else if (num > result[0]) {
31+
result[0] = num;
32+
}
33+
}
34+
return result;
35+
}
36+

Math/Palindrome_Number_Java.java

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* Question : Given an integer x, return true if x is a palindrome, and false otherwise.
2+
3+
Example 1:
4+
5+
Input: x = 121
6+
Output: true
7+
Explanation: 121 reads as 121 from left to right and from right to left.
8+
Example 2:
9+
10+
Input: x = -121
11+
Output: false
12+
Explanation: From left to right, it reads -121. From right to left, it becomes 121-. Therefore it is not a palindrome.
13+
Example 3:
14+
15+
Input: x = 10
16+
Output: false
17+
Explanation: Reads 01 from right to left. Therefore it is not a palindrome.
18+
19+
20+
Constraints:
21+
22+
-231 <= x <= 231 - 1 */
23+
24+
class Solution {
25+
public boolean isPalindrome(int x) {
26+
if(x < 0){
27+
return false;
28+
}
29+
30+
//Checking if the number is equal to its reverse, if true, it is a palindrome
31+
return x == rev(x);
32+
}
33+
34+
35+
// Function to find reverse of a number
36+
37+
public int reverse(int n){
38+
int reverse = 0;
39+
while(n != 0){
40+
int digit = n % 10;
41+
rev = digit + reverse * 10;
42+
n /= 10;
43+
}
44+
45+
return reverse;
46+
}
47+
}
48+

0 commit comments

Comments
 (0)