0% found this document useful (0 votes)
41 views3 pages

A Definite Guide For Java Learners

The document describes a function that takes an integer array as input and returns 1 if the array is a "centered array", or 0 if it is not. An array is centered if the middle element is strictly less than all other elements, except in the case of an array with only one element, which is always considered centered. It provides examples of arrays that would return 1 or 0.

Uploaded by

Shafiqul Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views3 pages

A Definite Guide For Java Learners

The document describes a function that takes an integer array as input and returns 1 if the array is a "centered array", or 0 if it is not. An array is centered if the middle element is strictly less than all other elements, except in the case of an array with only one element, which is always considered centered. It provides examples of arrays that would return 1 or 0.

Uploaded by

Shafiqul Islam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

1.

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.

Examples:

if the input array is return


{1, 2, 3, 4, 5} 0 (the middle element 3 is not strictly less than all other elements)
{3, 2, 1, 4, 5} 1 (the middle element 1 is strictly less than all other elements)
{3, 2, 1, 4, 1} 0 (the middle element 1 is not strictly less than all other elements)
{1, 2, 3, 4} 0 (no middle element)
{} 0 (no middle element)
{10} 1 (the middle element 10 is strictly less than all other elements)

Answers
First answer
static int a1(int[] a) { if (a == null || a.length % 2 == 0)
return 0; int midIndex = a.length / 2 ; int middleItem =
a[midIndex]; for (int i=0; i<a.length; i++) { if (i !=
midIndex && middleItem >= a[i]) return 0; } return 1; }
Second answer
static int a2(int[] a)
{
int sumEven = 0;
int sumOdd = 0;

for (int i=0; i<a.length; i++)


{
if (a[i]%2 == 0)
sumEven += a[i];
else
sumOdd += a[i];
}

return sumOdd - sumEven;


}
Third answer
static char[] a3(char[] a, int start, int length)
{
if (length < 0 || start < 0 || start+length-1>=a.length)
{
return null;
}

char[] sub = new char[length];


for (int i=start, j=0; j<length; i++, j++)
{
sub[j] = a[i];
}

return sub;
}

Fourth answer
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; }

Fifth answer
static int[] a5(int[] first, int[] second) { if (first == null
|| second == null) return null; if (first.length == 0 ||
second.length == 0) return new int[0]; int min = (first.length
< second.length) ? first.length : second.length; int[] a, b;
if (min == first.length) { a = first; b = second; } else { a =
second; b = first; } int[] c = new int[min]; int k = 0; for
(int i = 0; i < a.length; i++) for (int j = 0; j < b.length;
j++) if (a[i] == b[j]) { c[k] = a[i]; k++; } int[] retArray =
new int[k]; for (int t = 0; t < retArray.length; t++)
retArray[t] = c[t]; return retArray; }
Sixth answer static int a6(int[] a) { if (a.length < 3)
return -1; int i = 0; int j = a.length - 1; int idx = 1; int
leftSum = a[i]; int rightSum = a[j]; for (int k = 1; k <
a.length - 2; k++) { if (leftSum < rightSum) { i++; leftSum +=
a[i]; idx = i + 1; } else { j--; rightSum += a[j]; idx = j -
1; } } if (leftSum == rightSum) return idx; else return -1; }

You might also like