Created
February 3, 2021 17:45
-
-
Save alejolp/078ec87739c09fe1d616e2fefcf9d60d to your computer and use it in GitHub Desktop.
This file contains 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 static boolean isElementPresentSorted(int[] m, int elem) { | |
// BINARY SEARCH | |
int first = 0, last = m.length - 1; | |
while (first <= last) { | |
int middle = first + (last - first) / 2; | |
if (m[middle] == elem) { | |
return true; | |
} else if (m[middle] < elem) { | |
first = middle + 1; | |
} else { | |
last = middle - 1; | |
} | |
} | |
return false; | |
} |
This file contains 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 static boolean isElementPresent(int[] m, int elem) { | |
for (int i = 0; i < m.length; i++) { | |
if (m[i] == elem) { | |
return true; | |
} | |
} | |
return false; | |
} |
This file contains 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 MyClass1 { | |
public static void main(String args[]) { | |
int[] m = new int[100]; | |
System.out.println("The length of m is: " + m.length); // What's the output? | |
} | |
} |
This file contains 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 MyClass2 { | |
public static void main(String args[]) { | |
int[] m = new int[100]; | |
m[30] = 60; | |
m[60] = 40; | |
System.out.println("The index 30 has a value of: " + m[30]); | |
} | |
} |
This file contains 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.Arrays; | |
public class MyClass3 { | |
public static void main(String args[]) { | |
int[] m = new int[100]; | |
m = Arrays.copyOf(m, 200); | |
System.out.println("The new size of m is: " + m.length); | |
} | |
} |
This file contains 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.Scanner; | |
public class MyClass { | |
public static void main(String args[]) { | |
int[] values = new int[100]; | |
int numOfValues = 0; | |
Scanner s = new Scanner(System.in); | |
while (s.hasNext()) { | |
int v = s.nextInt(); | |
if (v == -1) break; | |
values[numOfValues] = v; | |
numOfValues += 1; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment