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 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); |
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 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 |
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 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); |
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 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; | |
} |
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 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 + ""); | |
} |
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 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); | |
} | |
} |
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
for (int i = 0; i < myArray.length; i++) { | |
// Access elements safely within bounds | |
System.out.println(myArray[i]); | |
} |
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
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]; |
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
int[] myArray = {10, 20, 30, 40, 50}; | |
int arrayLength = myArray.length; | |
System.out.println("The length of the array is: " + arrayLength); |
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
myArray[2] = 35; | |
System.out.println("Updated third element: " + myArray[2]); |
NewerOlder