Skip to content

Instantly share code, notes, and snippets.

View mounicasrinivas's full-sized avatar

mounicasrinivas

View GitHub Profile
@mounicasrinivas
mounicasrinivas / BestTimeToBuyAndSell.java
Created January 2, 2024 12:13
Best Time To Buy And Sell
public class BestTimeToBuyAndSell {
public int maxProfit(int[] prices) {
int minValue = Integer.MAX_VALUE;
int maxProfit = 0;
for (int i = 0; i < prices.length; i++) {
if (prices[i] < minValue) {
minValue = prices[i];
}
maxProfit = Math.max(maxProfit, prices[i] - minValue);
@mounicasrinivas
mounicasrinivas / NoOfOccurrencesInAnArray.java
Last active December 26, 2023 16:41
No Of Occurrences In An Array
public class NoOfOccuranceInAnArray {
public static void main(String[] args) {
//Given an integer array a and value v,
//find the number of Occurancences of v in a
int [] a = {1,2,2,2,2,3,4,5,5,6,7,7,};
int v = 2;
// Create a HashMap to store element-frequency pairs
@mounicasrinivas
mounicasrinivas / MaximumValueInAnArray.java
Created December 24, 2023 18:28
Maximum Value In An Array
public class MaximumValueInAnArray {
public static void main(String[] args) {
int[] a ={ -1,2,3,-4,5};
int max = Integer.MIN_VALUE;
for(int i = 0; i <a.length; i++){
if (a[i] > max){
max = a[i];
}
}
System.out.println(max);
@mounicasrinivas
mounicasrinivas / SwappingAnArray.java
Created December 24, 2023 17:45
Swapping An Array
public class SwappingAnArray {
public static void main(String[] args) {
int [] a = {1,2,3,4,5};
int n = a.length;
for(int i = 0; i< n/2; i++){
// Swap numbers from ith and (n-1-i)th position;
int temp = a[n-1-i];
a[n-1-i] = a[i];
a[i] = temp;
}
@mounicasrinivas
mounicasrinivas / ReverseAnArray.java
Created December 24, 2023 17:11
Reverse An Array
public class ReverseAnArray {
public static void main(String[] args) {
int [] a = {1,2,3,4,5};
int [] rev = new int [a. length];
for(int i = 0; i< a.length; i++){
rev[i] = a[a.length -1 -i];
}
for(int v: rev){
System.out.println(v + "");
}
@mounicasrinivas
mounicasrinivas / SumOfElements.java
Created December 21, 2023 14:58
Sum Of Elements
public class SumOfElements {
public static void main(String[] args) {
int n[] = {1,2,3,4,5};
int sum = 0;
for(int i = 0; i<n.length;i++){
sum = sum + n[i];
}
System.out.println(sum);
}
}
@mounicasrinivas
mounicasrinivas / IteratingThroughArraysSafely.java
Created December 19, 2023 17:35
Iterating Through Arrays Safely:
for (int i = 0; i < myArray.length; i++) {
// Access elements safely within bounds
System.out.println(myArray[i]);
}
@mounicasrinivas
mounicasrinivas / ArrayIndexOutOfBoundsException.java
Created December 19, 2023 17:31
ArrayIndexOutOfBoundsException
int[] myArray = {10, 20, 30, 40, 50};
// Accessing a valid index
int validElement = myArray[2];
// Attempting to access an invalid index (outside bounds)
// This would throw an ArrayIndexOutOfBoundsException
int invalidElement = myArray[5];
@mounicasrinivas
mounicasrinivas / DeterminingArrayLength.java
Created December 19, 2023 17:24
Determining Array Length
int[] myArray = {10, 20, 30, 40, 50};
int arrayLength = myArray.length;
System.out.println("The length of the array is: " + arrayLength);
@mounicasrinivas
mounicasrinivas / UpdatingArrayElements.java
Created December 19, 2023 17:16
Updating Array Elements:
myArray[2] = 35;
System.out.println("Updated third element: " + myArray[2]);