Skip to content

Instantly share code, notes, and snippets.

View ravindugm's full-sized avatar
🏡
Working from home

Ravindu Miyuranga ravindugm

🏡
Working from home
View GitHub Profile
@ravindugm
ravindugm / Star.java
Created December 14, 2023 07:19
Print star pattern using Java 8 Streams
// 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)
@ravindugm
ravindugm / ArraySort.java
Created December 13, 2023 04:04
Sort array using Java 8 streams
/* 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);
@ravindugm
ravindugm / ValidateBracket.java
Last active December 5, 2023 12:20
Validate brackets
/*
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
*/
@ravindugm
ravindugm / FindOnes.java
Last active December 13, 2023 09:49
Find ones postion of given decimal number and add count of ones in the start of the return list
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);
}
@ravindugm
ravindugm / FizzBuzz.java
Created November 24, 2023 04:31
FizzBuzz
/*
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);
@ravindugm
ravindugm / PalindromePermutation.java
Created November 24, 2023 01:51
Palindrome permutation
/*
Given a string, determine if a permutation of the string could form a palindrome.
Example 1:
Input: "code"
Output: false
Example 2:
Input: "aab"
@ravindugm
ravindugm / Palindrome.java
Created November 21, 2023 10:49
Valid Palindrome
/*
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"
@ravindugm
ravindugm / RomanToInt.java
Created November 21, 2023 05:11
Given an integer, convert it to a roman numeral
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
@ravindugm
ravindugm / Palindrome.java
Created November 20, 2023 06:44
Check if given string is a palindrome
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 = "";
@ravindugm
ravindugm / Palindrome.java
Created November 19, 2023 20:27
Check if given number is a palindrome
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) {