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 / LongestPrefix.java
Created February 3, 2024 18:06
Find the longest common prefix string amongst an array of strings
/*Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
Example 1:
Input: strs = ["flower","flow","flight"]
Output: "fl"
Example 2:
Input: strs = ["dog","racecar","car"]
Output: ""
@ravindugm
ravindugm / AverageCalculator.java
Created February 3, 2024 14:23
Calculate the average of student marks
/*Imagine you are a teacher. At the end of the school year, you need to calculate the average marks of your students.
The marks of the students are passed as an array of strings.
Each student's details are provided in a string in the following format:
‹student><mark 1><mark 2><mark N>
Return a string in the following format (without white-spaces): ‹student A>-<average mark>;<student B>-‹average mark>;
Take the following into account:
• Any kind of separator can be used in the string.
• The mark is an integer from 1-10. If the mark is out of that range, discard it,
and don't use it in the average calculation.
Don't forget to think through any corner cases.
@ravindugm
ravindugm / Numbers.java
Created February 1, 2024 06:30
Number of '1' bits
/*Write a function that takes the binary representation of
an unsigned integer and returns the number of '1' bits it has (also known as the Hamming weight).
Note:
Note that in some languages, such as Java, there is no unsigned integer type. In this case, the input will be given as a signed integer type.
It should not affect your implementation, as the integer's internal binary representation is the same, whether it is signed or unsigned.
In Java, the compiler represents the signed integers using 2's complement notation. Therefore, in Example 3, the input represents the signed integer. -3.
Example 1:
Input: n = 00000000000000000000000000001011
@ravindugm
ravindugm / RelativeSortArray.java
Created February 1, 2024 05:38
Relative sort array
/*Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2.
Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.
Example 1:
Input: arr1 = [2,3,1,3,2,4,6,7,9,2,19], arr2 = [2,1,4,3,9,6]
Output: [2,2,2,1,4,3,3,9,6,7,19]
Example 2:
Input: arr1 = [28,6,22,8,44,17], arr2 = [22,28,8,6]
Output: [22,28,8,6,17,44]*/
@ravindugm
ravindugm / MatchingCharacter.java
Created December 22, 2023 11:24
Java code for return count of matching charaters of two strings
import java.util.*;
public class Main {
public static void main(String[] args) {
String input1 = "aaaaaaaaaabbbbbbccccccdddddeeeee";
String input2 = "aaaaaaaaaaaaaaaabbbbbbbbbbb";
int count = countMatchingCharacter(input1, input2);
System.out.println(count);
}
@ravindugm
ravindugm / Prime.java
Created December 20, 2023 07:39
Find prime numbers in between 0 to 10
public class Main {
public static void main(String[] args) {
int num = 10;
for (int i = 0; i < num; i++) {
if (isPrime(i)) {
System.out.println(i);
}
}
@ravindugm
ravindugm / Fibonacci.java
Created December 20, 2023 02:54
Fibonacci series using Java recursion
public class Main {
public static void main(String[] args) {
int fibonacciLength = 15;
for (int i = 0; i < fibonacciLength; i++) {
System.out.print(findFibonacci(i) + " ");
}
}
public static int findFibonacci(int input) {
@ravindugm
ravindugm / PlusOne.java
Created December 16, 2023 19:15
Find plus one of given array of integers
/*You are given a large integer represented as an integer array digits, where each digits[i] is the ith digit of the integer.
The digits are ordered from most significant to least significant in left-to-right order.
The large integer does not contain any leading 0's.
Increment the large integer by one and return the resulting array of digits.
Example 1:
Input: digits = [1,2,3]
Output: [1,2,4]
Explanation: The array represents the integer 123.
@ravindugm
ravindugm / FindCount.java
Created December 16, 2023 17:45
Find character count of the given string
import java.util.HashMap;
import java.util.Map;
public class Main {
public static void main(String[] args) {
String input = "abcdabc"; // Output = a2 b2 c2 d1
Map<Character, Integer> count = findCount(input);
System.out.println(count);
}
@ravindugm
ravindugm / Strems.java
Created December 15, 2023 08:00
Streams in Java
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
List<Integer> integerList = Arrays.asList(1, 3, 90, 99, 50, 65, 30, 84);