This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Print star pattern using Java 8 Streams | |
| import java.util.stream.Collectors; | |
| import java.util.stream.IntStream; | |
| public class Main { | |
| public static void main(String[] args) { | |
| int row = 5; | |
| IntStream.rangeClosed(1, row) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* 1. Write Java code to sort int array Java 8 streams | |
| 2. Find 3rd, 4th and 5th largest numbers using Java 8 streams in given array */ | |
| import java.util.Arrays; | |
| import java.util.List; | |
| import java.util.stream.Collectors; | |
| public class Main { | |
| public static void main(String[] args) { | |
| List<Integer> integerList = Arrays.asList(2, 4, 5, 19, 18, 40, 13, 6); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Given a string input containing just the characters `(`,`)`,`{`,`}`,`[` and `]`. An input string is valid if, | |
| - Open brackets must be closed by the same type of brackets. | |
| - Open brackets must be closed in the correct order. | |
| - Every close bracket has a corresponding open bracket of same type. | |
| Input s = “()[]{}” | |
| Output = true | |
| Input s = “(]” | |
| Output = false | |
| */ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.ArrayList; | |
| public class Main { | |
| public static void main(String[] args) { | |
| int decimalNum = 161; | |
| ArrayList<Integer> onesPositions = findOnesPositions(decimalNum); | |
| System.out.println("Positions of '1's in the binary representation: " + onesPositions); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| FizzBuzz is a classic programming problem where you print the numbers from 1 to n, but for multiples of 3, print "Fizz" instead of the number, | |
| and for the multiples of 5, print "Buzz". | |
| For numbers that are multiples of both 3 and 5, print "FizzBuzz" | |
| */ | |
| public class Main { | |
| public static void main(String[] args) { | |
| fizzBuzz(15); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| Given a string, determine if a permutation of the string could form a palindrome. | |
| Example 1: | |
| Input: "code" | |
| Output: false | |
| Example 2: | |
| Input: "aab" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers. | |
| Given a string s, return true if it is a palindrome, or false otherwise. | |
| Example 1: | |
| Input: s = "A man, a plan, a canal: Panama" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.HashMap; | |
| import java.util.Map; | |
| public class Main { | |
| public static void main(String[] args) { | |
| // Test cases | |
| System.out.println(romanToInt("III")); // Output: 3 | |
| System.out.println(romanToInt("LVIII")); // Output: 58 | |
| System.out.println(romanToInt("MCMXCIV")); // Output: 1994 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Main { | |
| public static void main(String[] args) { | |
| String input = "RMR"; | |
| System.out.println("Is palindrome " + isPalindrome(input)); | |
| } | |
| public static boolean isPalindrome(String input) { | |
| String reverseStr = ""; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class Main { | |
| public static void main(String[] args) { | |
| int input = -121; | |
| System.out.println("Is palindrome " + isPalindrome(input)); | |
| } | |
| public static boolean isPalindrome(int num) { | |
| if (num < 0) { |