Skip to content
This repository was archived by the owner on Aug 13, 2024. It is now read-only.

Commit c273572

Browse files
author
Mohamed Sayed-ElMedany
committed
new problems
1 parent ba7226d commit c273572

File tree

5 files changed

+254
-0
lines changed

5 files changed

+254
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package javarevisited.top.fifty.java.programs.from.coding.interviews;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* @author medany
7+
*/
8+
9+
/*
10+
* A number is called an Armstrong number if it is equal to the cube of its each
11+
* digit. for example, 153 is an Armstrong number because 153= 1+ 125+27 which
12+
* is equal to 1^3+5^3+3^3. You need to write a program to check if given number
13+
* is Armstrong number or not.
14+
*/
15+
public class ArmstrongNumber {
16+
17+
public static void main(String[] args) {
18+
Scanner sc = new Scanner(System.in);
19+
int n = sc.nextInt();
20+
sc.close();
21+
22+
System.out.println("Number : " + n + " is " + (isArmStrong(n) ? "armstrong number" : "not a armstrong number"));
23+
24+
}
25+
26+
public static boolean isArmStrong(int number) {
27+
int result = 0;
28+
int orig = number;
29+
while (number != 0) {
30+
int remainder = number % 10;
31+
result = result + remainder * remainder * remainder;
32+
number = number / 10;
33+
}
34+
if (orig == result) {
35+
return true;
36+
}
37+
return false;
38+
}
39+
40+
public static boolean isArmstrong(int input) {
41+
42+
Integer number = 0;
43+
char[] s = ("" + input).toCharArray();
44+
45+
for (char c : s) {
46+
number += Integer.valueOf("" + c) * Integer.valueOf("" + c) * Integer.valueOf("" + c);
47+
if (number.compareTo(input) == 1) {
48+
return false;
49+
}
50+
}
51+
52+
if (number.compareTo(input) == 0) {
53+
return true;
54+
}
55+
return false;
56+
}
57+
58+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package javarevisited.top.fifty.java.programs.from.coding.interviews;
2+
3+
import java.math.BigInteger;
4+
import java.util.Scanner;
5+
6+
/**
7+
* @author medany
8+
*/
9+
10+
/*
11+
* This is one of the simplest programs you can expect on interviews. It is
12+
* generally asked to see if you can code or not. Sometimes interviewer may also
13+
* ask about changing a recursive solution to iterative one or vice-versa.
14+
*/
15+
public class Factorial {
16+
17+
public static void main(String[] args) {
18+
Scanner sc = new Scanner(System.in);
19+
BigInteger i = sc.nextBigInteger();
20+
sc.close();
21+
22+
System.out.println("Factorial of number, " + i + ", is : " + Fact(i));
23+
}
24+
25+
public static BigInteger fact(BigInteger num) {
26+
if (num.compareTo(BigInteger.ZERO) == 0)
27+
return BigInteger.ONE;
28+
BigInteger fact = BigInteger.ONE;
29+
for (int i = num.intValue(); i > 0; i--) {
30+
fact = fact.multiply(BigInteger.valueOf(i));
31+
}
32+
return fact;
33+
}
34+
35+
public static BigInteger Fact(BigInteger num) {
36+
if (num.compareTo(BigInteger.ZERO) == 0)
37+
return BigInteger.ONE;
38+
return num.multiply(Fact(num.subtract(BigInteger.ONE)));
39+
}
40+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package javarevisited.top.fifty.java.programs.from.coding.interviews;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Scanner;
7+
8+
/**
9+
* @author medany
10+
*/
11+
12+
/*
13+
*
14+
*/
15+
public class FindStringRepeatedCharacters {
16+
17+
public static void main(String[] args) {
18+
Scanner in = new Scanner(System.in);
19+
String word = in.nextLine();
20+
in.close();
21+
22+
// Map<String, Integer> result = findDuplicates(word);
23+
// Map<String, Integer> result = findduplicates(word);
24+
Map<String, Integer> result = findduplicaates(word);
25+
for (String key : result.keySet()) {
26+
System.out.println(key + " : " + result.get(key));
27+
}
28+
29+
}
30+
31+
public static Map<String, Integer> findDuplicates(String word) {
32+
33+
char[] keyword = word.toCharArray();
34+
Arrays.sort(keyword);
35+
36+
Map<String, Integer> result = new HashMap<>();
37+
char previous = keyword[0];
38+
39+
for (int i = 1; i < keyword.length; i++) {
40+
char ch = keyword[i];
41+
if (previous == ch) {
42+
if (result.get("" + ch) != null)
43+
result.put("" + ch, result.get("" + ch) + 1);
44+
else
45+
result.put("" + ch, 2);
46+
47+
}
48+
previous = ch;
49+
}
50+
return result;
51+
}
52+
53+
public static Map<String, Integer> findduplicates(String word) {
54+
55+
Map<String, Integer> result = new HashMap<>();
56+
57+
for (int i = 0; i < word.length(); i++) {
58+
boolean found = false;
59+
for (int j = i + 1; j < word.length(); j++) {
60+
if (word.charAt(i) == word.charAt(j)) {
61+
found = true;
62+
break;
63+
}
64+
}
65+
if (found)
66+
if (result.get("" + word.charAt(i)) != null)
67+
result.put("" + word.charAt(i), result.get("" + word.charAt(i)) + 1);
68+
else
69+
result.put("" + word.charAt(i), 2);
70+
71+
}
72+
return result;
73+
}
74+
75+
public static Map<String, Integer> findduplicaates(String word) {
76+
77+
int[] letters = new int[26];
78+
Map<String, Integer> result = new HashMap<>();
79+
for (char f : word.toCharArray()) {
80+
letters[f - 'a']++;
81+
if (letters[f - 'a'] > 1) {
82+
if (result.get("" + f) != null)
83+
result.put("" + f, result.get("" + f) + 1);
84+
else
85+
result.put("" + f, 2);
86+
}
87+
}
88+
89+
return result;
90+
}
91+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package javarevisited.top.fifty.java.programs.from.coding.interviews;
2+
3+
/**
4+
* @author medany
5+
*/
6+
7+
/*
8+
* How to Print a Pattern in Java. ex. Pyramid Pattern
9+
*/
10+
public class PrintingPatterns {
11+
public static void main(String[] args) {
12+
drawPyrmidPattern(5, "* ");
13+
}
14+
15+
public static void drawPyrmidPattern(int n, String block) {
16+
for (int i = 0; i < n; i++) {
17+
for (int j = 0; j <= i; j++) {
18+
System.out.print(block);
19+
}
20+
System.out.println();
21+
}
22+
}
23+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package javarevisited.top.fifty.java.programs.from.coding.interviews;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @author medany
7+
*/
8+
9+
/*
10+
* Write a program to remove duplicates from an array in Java without using the
11+
* Java Collection API. The array can be an array of String, Integer or
12+
* Character, your solution should be independent of the type of array. If you
13+
* want to practice more array based questions then see this list of top 30
14+
* array interview questions from Java interviews.
15+
*/
16+
public class RemoveArrayDuplicates {
17+
18+
public static void main(String[] args) {
19+
Object[] arr = { 'a', 'a', 'd', 'c', 'b', 'c', 'e', 'd' };
20+
21+
System.out.println("Array with doublicates: " + Arrays.toString(arr));
22+
System.out.println("Array without doublicates: " + Arrays.toString(removeDuplicates(arr)));
23+
}
24+
25+
public static Object[] removeDuplicates(Object[] arr) {
26+
27+
Arrays.sort(arr);
28+
29+
Object[] result = new Object[arr.length];
30+
Object previous = arr[0];
31+
result[0] = previous;
32+
33+
for (int i = 1; i < arr.length; i++) {
34+
Object ch = arr[i];
35+
if (previous != ch) {
36+
result[i] = ch;
37+
}
38+
previous = ch;
39+
}
40+
return result;
41+
}
42+
}

0 commit comments

Comments
 (0)