Skip to content

Instantly share code, notes, and snippets.

@Blezzoh
Last active August 13, 2021 18:03
Show Gist options
  • Save Blezzoh/43abdec6f4ea780f4bde12c0ada55114 to your computer and use it in GitHub Desktop.
Save Blezzoh/43abdec6f4ea780f4bde12c0ada55114 to your computer and use it in GitHub Desktop.
public class MyClass {
/*
Consider an array A with n of positive integers.
An integer idx is called a POE (point of equilibrium) of A,
if A[0] + A[1] + … + A[idx – 1] is equal to A[idx + 1] + A[idx + 2] + … + A[n – 1].
Write a function to return POE of an array, if it exists and -1 otherwise.
The signature of the function is:
int f(int[] a)
*/
public static int arraySum(int [] arr, int start, int end){
int sum = 0;
if(arr == null || arr.length == 0) return sum;
for(int i =start; i<= end; i++){
sum += arr[i];
}
return sum;
}
public static int pointOfEquilibrium(int [] arr){
if(arr == null || arr.length == 0) return -1;
for(int i = 1; i<arr.length; i++){
int left = arraySum(arr, 0, i);
int right = arraySum(arr, i+2, arr.length -1);
if(left == right){
return i+1;
}
}
return -1;
}
/*
Write a function to reverse an integer using numeric operators and without using any arrays or other data structures.
The signature of the function is:
int f(int n)
*/
public static int power(int num){
int tens = 10;
while(true){
if(num / tens >= 10){
tens *=10;
}
else
return tens;
}
}
public static int reverseWithoutDataStructure(int number){
int sign = number < 0? -1 : 1;
int num = number * sign;
if(num<10) return num;
int tenPowerRange = power(num);
int reverse = 0, start = 1, n = num;
for(int i = tenPowerRange; i >0; i = i/10){
int reminder = n / i;
reverse += reminder * start;
start *=10;
n = n - (reminder * i);
}
return reverse * sign;
}
// book
static int a4(int n)
{
int sign = 1;
if (n == 0) return 0;
if (n < 0)
{
sign = -1;
n = -n;
}
int reverse = 0;
while (n != 0)
{
reverse = (reverse * 10) + (n % 10);
n /= 10;
}
return sign * reverse;
}
/*
Write a function that takes an array of integers as an argument and returns a value based on the sums of the even
and odd numbers in the array. Let X = the sum of the odd numbers in the array and let Y = the sum of the even numbers.
The function should return X – Y
*/
public static int oddMinusEven(int [] arr){
int odds= 0, evens = 0;
for(int i =0; i < arr.length; i++){
if(arr[i]%2 > 0){
odds += arr[i];
}
else{
evens += arr[i];
}
}
return odds - evens;
}
/*
An array with an odd number of elements is said to be centered if all elements (except the middle one)
are strictly greater than the value of the middle element. Note that only arrays with an odd number of elements have
a middle element.
Write a function that accepts an integer array and returns 1 if it is a centered array, otherwise it returns 0.
*/
public static int hasMiddle(int [] arr){
int rem = arr.length %2;
if(rem >0){
int middle = arr.length/2;
int el = arr[middle];
for(int i = 0; i <arr.length; i++){
if(i != middle){
if(arr[i] <= el){
return 0;
}
}
}
return 1;
}
return 0;
}
public static void main(String args[]) {
int [] arr = {1, 2, 3, 4, 5};
int [] arr2 = {3, 2, 1, 4, 5};//1
int [] arr3 = {3, 2, 1, 4, 1};
int [] arr4 = {1, 2, 3, 4};
int [] arr5 = {};
int [] arr6 = {10};//1
System.out.println(hasMiddle(arr));
System.out.println(hasMiddle(arr2));
System.out.println(hasMiddle(arr3));
System.out.println(hasMiddle(arr4));
System.out.println(hasMiddle(arr5));
System.out.println(hasMiddle(arr6));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment