Skip to content

Instantly share code, notes, and snippets.

@alejolp
Created February 3, 2021 17:45
Show Gist options
  • Save alejolp/078ec87739c09fe1d616e2fefcf9d60d to your computer and use it in GitHub Desktop.
Save alejolp/078ec87739c09fe1d616e2fefcf9d60d to your computer and use it in GitHub Desktop.
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;
}
public static boolean isElementPresent(int[] m, int elem) {
for (int i = 0; i < m.length; i++) {
if (m[i] == elem) {
return true;
}
}
return false;
}
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?
}
}
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]);
}
}
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);
}
}
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