Skip to content

Commit de5e7a5

Browse files
committed
add palindrome and duplicate number problme
1 parent 749d8aa commit de5e7a5

File tree

9 files changed

+235
-0
lines changed

9 files changed

+235
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This problem was asked by **Google**.
2+
3+
- You are given an array of length n + 1 whose elements belong to the set {1, 2, ..., n}.
4+
- By the pigeonhole principle, there must be a duplicate.
5+
- Find it in linear time and space.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This problem was asked by **Palantir**.
2+
3+
Write a program that checks whether an integer is a palindrome.
4+
For example, 121 is a palindrome, as well as 888. 678 is not a palindrome.
5+
Do not convert the integer into a string.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import java.util.Scanner;
2+
public class Approach1 {
3+
public static void main(String [] args){
4+
Scanner sc = new Scanner(System.in);
5+
System.err.println("Enter the size of array :");
6+
int n = sc.nextInt();
7+
int arr[] = new int[n+1];
8+
System.err.println("Enter the values for array");
9+
for (int i =0;i<arr.length ; i++) {
10+
arr[i]= sc.nextInt();
11+
}
12+
13+
System.err.println("Duplicate Value is:"+findDuplicateElement(arr));
14+
}
15+
16+
public static int findDuplicateElement(int [] arr){
17+
int tempArr[] = new int[arr.length-1];
18+
int duplicateValue = 0;
19+
20+
for (int elem:arr){
21+
if (tempArr[elem-1]==0){
22+
tempArr[elem-1]=elem;
23+
}
24+
else{
25+
duplicateValue = elem;
26+
break;
27+
}
28+
}
29+
return duplicateValue;
30+
}
31+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
let arr = [1, 2, 3, 3, 4];
2+
3+
// time and space complexity is O(n)
4+
function findDuplicate(arr) {
5+
let tempArr = Array(arr.length - 1).fill(0);
6+
let duplicateValue = 0;
7+
8+
for (const elem of arr) {
9+
if (tempArr[elem - 1] == 0) tempArr[elem - 1] = elem;
10+
else {
11+
duplicateValue = elem;
12+
break;
13+
}
14+
}
15+
return duplicateValue;
16+
}
17+
18+
console.log(findDuplicate(arr));
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import java.util.Scanner;
2+
3+
public class Approach1{
4+
5+
public static void main(String [] args){
6+
7+
// for taking integer form standard input
8+
Scanner sc = new Scanner(System.in);
9+
System.out.print("Enter the number :");
10+
11+
try {
12+
int num = sc.nextInt();
13+
14+
// Call the palindrome function and print the result
15+
System.out.println(String.format("Is %d a palindrome? %b",num,checkPalindrome(num)));
16+
} catch (NumberFormatException e) {
17+
System.out.println("Please enter a valid number.");
18+
} finally {
19+
// Close the scanner
20+
sc.close();
21+
}
22+
}
23+
24+
public static boolean checkPalindrome(int num) {
25+
// check if negative number
26+
if (num<0){
27+
return false;
28+
}
29+
if (num==0){
30+
return true;
31+
}
32+
33+
int x = 0;
34+
while (num > x)
35+
{
36+
x = x * 10 + num % 10;
37+
num /= 10;
38+
}
39+
return (num == x || (x > num && x / 10 == num));
40+
}
41+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <stdio.h>
2+
3+
int checkPalindrome(int);
4+
5+
int main()
6+
{
7+
int n;
8+
printf("enter the number:");
9+
scanf("%d", &n);
10+
11+
if (checkPalindrome(n))
12+
{
13+
printf("Number is Palindrome\n");
14+
}
15+
else
16+
{
17+
printf("Number is not Palindrome\n");
18+
}
19+
}
20+
21+
int checkPalindrome(int number)
22+
{
23+
int x = 0;
24+
while (number > x)
25+
{
26+
x = x * 10 + number % 10;
27+
number /= 10;
28+
}
29+
return number == x || (x > number && x / 10 == number);
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
bool checkPalindrome(int);
6+
7+
int main()
8+
{
9+
int n;
10+
cout << "Enter the number:";
11+
cin >> n;
12+
13+
if (checkPalindrome(n))
14+
{
15+
cout << "Number is Palindrome" << endl;
16+
}
17+
else
18+
{
19+
cout << "Number is not Palindrome" << endl;
20+
}
21+
}
22+
23+
bool checkPalindrome(int number)
24+
{
25+
int x = 0;
26+
while (number > x)
27+
{
28+
x = x * 10 + number % 10;
29+
number /= 10;
30+
}
31+
return number == x || (x > number && x / 10 == number);
32+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// node.js to run the program
2+
3+
// Prompt the user for input
4+
process.stdout.write("Enter a number: ");
5+
6+
// Handle the input
7+
process.stdin.once("data", (data) => {
8+
// Convert the input to a number
9+
const num = parseInt(data.toString().trim(), 10);
10+
11+
// Check if the input is a valid number
12+
if (isNaN(num)) {
13+
console.log("Please enter a valid number.");
14+
} else {
15+
// Call your palindrome function here
16+
const isPalindrome = checkPalindrome(num);
17+
console.log(`Is ${num} a palindrome? ${isPalindrome}`);
18+
}
19+
20+
// Exit the process
21+
process.exit();
22+
});
23+
24+
// check palindrome number
25+
function checkPalindrome(num) {
26+
// Handle negative numbers and zero separately
27+
if (num < 0) return false;
28+
if (num === 0) return true;
29+
30+
let x = 0;
31+
32+
// reverse number at half of number
33+
while (num > x) {
34+
x = x * 10 + (num % 10);
35+
num = Math.floor(num / 10);
36+
}
37+
38+
// compare first half and second half of number
39+
return num == x || (x > num && x / 10 == num) ? true : false;
40+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# check palindrome
2+
def checkPalindrome(num):
3+
# check number negative
4+
if num<0:
5+
return False
6+
elif num==0:
7+
return True
8+
9+
# reverse the half of number
10+
x = 0
11+
while num>x:
12+
x = x*10 + num%10
13+
num //=10
14+
print(x,num)
15+
16+
# compare first half and reverse number
17+
if x==num or (x > num and x // 10 == num):
18+
return True
19+
else:
20+
return False
21+
22+
# Prompt the user for input
23+
input_str = input('Enter a number: ')
24+
25+
# Convert the input to an integer
26+
try:
27+
num = int(input_str)
28+
except ValueError:
29+
print('Please enter a valid number.')
30+
else:
31+
# Call the palindrome function and print the result
32+
is_palindrome = checkPalindrome(num)
33+
print(f'Is {num} a palindrome? {is_palindrome}')

0 commit comments

Comments
 (0)