Here is my array code:
int h = 0;
int i = 1;
while (h < a.length - 1){
if (a[h] < a[i]){
h++;
i++;
}
if (a[h] > a[i]){
a[i] = a[h];
a[h] = a[i + 1];
}
}
if (h == a.length - 1){
System.out.println(a[i]);
}
I don't know what I'm doing wrong. I need the answer for a school assignment. I'm in my school's computer science class.
check this out, i'm not sure what language are you using, but i would choose to use for loop instead of while
int h = 0;
int i = 0;
int v = null;
while (h < a.length - 1){
if(v == null || v < a[i])
{
v = a[i];
h = i;
}
i++;
}
if (v != null){
System.out.println(v); //for value
System.out.println(h); //highest index
System.out.println(a[h]); //for value from array a
}
int max = a[0];
int index = 0;
for (int i = 1; i < a.length; i++) {
if (a[i] > max) {
max = a[i];
index = i;
}
}
Try to create this method
public int max(int[] a) {
int temp = a[0];
for (int i = 1; i < a.length; i++) {
if (a[i] > temp) {
temp = a[i];
}
}
return temp;
}
and in your main method
int[] a = new int[3];
a[0]=2;
a[1]=3;
a[2]=54;
System.out.println("Highest value is "+ max(a));
Arrays.sort(a);
System.out.println(a[a.length - 1]);
Related
You have been given a random integer array/list(ARR) and a number X. Find and return the number of distinct triplet(s) in the array/list which sum to X.
I have written this code:
public class Solution {
public static void merge(int arr[], int lb, int mid, int ub) {
int n1 = mid - lb + 1;
int n2 = ub - mid;
int arr1[] = new int[n1];
int arr2[] = new int[n2];
for (int i = 0; i < n1; i++)
arr1[i] = arr[lb + i];
for (int j = 0; j < n2; j++)
arr2[j] = arr[mid + 1 + j];
int i = 0, j = 0;
int k = lb;
while (i < n1 && j < n2) {
if (arr1[i] <= arr2[j])
arr[k] = arr1[i++];
else
arr[k] = arr2[j++];
k++;
}
while (i < n1)
arr[k++] = arr1[i++];
while (j < n2)
arr[k++] = arr2[j++];
}
public static void mergeSort(int arr[], int lb, int ub) {
if (lb < ub) {
int mid = lb + (ub - lb) / 2;
mergeSort(arr, lb, mid);
mergeSort(arr, mid + 1, ub);
merge(arr, lb, mid, ub);
}
}
public static int tripletSum(int[] arr, int num) {
mergeSort(arr, 0, arr.length - 1);
int n = arr.length;
int count = 0;
for (int i = 0; i < n - 2; i++) {
int sum = num - arr[i];
int j = i + 1;
int k = n - 1;
while (j < k) {
if (arr[j] + arr[k] == sum) {
count++;
k--;
} else if (arr[j] + arr[k] > sum) {
k--;
} else
j++;
}
}
return count;
}
}
Input array was - 1 3 3 3 3 3 3
Input num = 9
Output Generated - 10
Expected output - 20
Help me out with this I'm trying to find the solution for many hours. Also, the time complexity of the program should not exceed O(n²).
The given code works fine for all test cases.
import java.util.Arrays;
public class Solution {
public static int tripletSum(int[] arr, int num) {
Arrays.sort(arr);
int n = arr.length;
int Numtripletsum = 0;
for(int i=0;i<n;i++)
{
int pairSumFor = num - arr[i];
int numPairs = pairSum(arr, (i+1), (n-1), pairSumFor);
Numtripletsum+=numPairs;
}
return Numtripletsum;
}
private static int pairSum(int[] arr, int startIndex, int endIndex, int num ){
int numPair = 0;
while(startIndex < endIndex){
if(arr[startIndex] + arr[endIndex] < num){
startIndex++;
}
else if(arr[startIndex] + arr[endIndex] > num){
endIndex--;
}
else
{
int elementAtStart = arr[startIndex];
int elementAtEnd = arr[endIndex];
if(elementAtStart == elementAtEnd){
int totalElementsFromStartToEnd = (endIndex - startIndex) + 1;
numPair += (totalElementsFromStartToEnd * (totalElementsFromStartToEnd -1) /2);
return numPair;
}
int tempStartIndex = startIndex + 1;
int tempEndIndex = endIndex - 1;
while(tempStartIndex <= tempEndIndex && arr[tempStartIndex] == elementAtStart){
tempStartIndex+=1;
}
while(tempEndIndex >= tempStartIndex && arr[tempEndIndex] == elementAtEnd){
tempEndIndex-=1;
}
int totalElementsFromStart = (tempStartIndex - startIndex);
int totalElementsFromEnd = (endIndex - tempEndIndex);
numPair += (totalElementsFromStart * totalElementsFromEnd);
startIndex = tempStartIndex;
endIndex = tempEndIndex;
}
}
return numPair;
}
}
I did it in this way, hope I understood it well.
public static int tripletSum(int[] arr, int num) {
int loops = 0; // just to check the quality of the code
int counter = 0;
for (int i1 = 0; i1 < arr.length - 2; i1++) {
for (int i2 = i1 + 1; i2 < arr.length - 1; i2++) {
for (int i3 = i2 + 1; i3 < arr.length; i3++) {
loops++;
if (arr[i1] + arr[i2] + arr[i3] == num) {
counter++;
}
}
}
}
// Check for not exceeding O(n^2)
if (loops <= arr.length * arr.length) {
System.io.println("Well done");
} else {
System.io.println("Too many cycles");
}
return counter;
}
I iterate from "left" to "right" over the array and walking more and more to the "right".
1st, 2nd, 3rd
1st, 2nd, 4th
...
1st, 2nd, nth
2nd, 3rd, 4th
2nd, 3rd, 5th
...
2nd, 3rd, nth
.
.
.
nth - 2, nth - 1, nth
I iterated 35 times, but could iterate 7^2 = 49 times --> "Well done"
You missed one loop. The fixed code:
public static int tripletSum(int[] arr, int num) {
//Your code goes here
mergeSort(arr, 0, arr.length - 1);
int n = arr.length;
int count = 0;
for (int i = 0; i < n - 2; i++) {
int sum = num - arr[i];
for (int j = i+1; j < n-1; j++) {
int k = n-1;
while (j < k) {
if (arr[j] + arr[k] == sum) {
count++;
k--;
} else if (arr[j] + arr[k] > sum) {
k--;
} else {
j++;
}
}
}
}
return count;
}
OK, then optimized version:
public static int tripletSum(int[] arr, int num) {
//Your code goes here
mergeSort(arr, 0, arr.length - 1);
int n = arr.length;
int count = 0;
for (int i = 0; i < n - 2; i++) {
int sum = num - arr[i];
int maxK = n - 1;
for (int j = i + 1; j < n - 1; j++) {
int k = maxK;
while (j < k) {
if (arr[j] + arr[k] == sum) {
count++;
k--;
} else if (arr[j] + arr[k] > sum) {
k--;
maxK--;
} else {
j++;
}
}
}
}
return count;
}
If the array to be sorted is 10 elements and above, I get the NegativeArraySizeException. It says it's on this line of code "int R[] = new int[n2];" , how can I fix this?
If you were wondering why my merge sort doesn't have any other methods, my professor specifically asked for a merge sort within just the main method. This merge sort is the iterative version because I couldn't compress the recursive version to main method only.
public static void main(String[] args)
{
int arr[] = {100,99,98,97,96,95,94,93,92,93};
int n = arr.length;
int curr_size;
int left_start;
for (curr_size = 1; curr_size <= n-1;
curr_size = 2*curr_size)
{
for (left_start = 0; left_start < n-1;
left_start += 2*curr_size)
{
int mid = left_start + curr_size - 1;
int right_end = Math.min(left_start
+ 2*curr_size - 1, n-1);
int i, j, k;
int n1 = mid - left_start + 1;
int n2 = right_end - mid;
/* create temp arrays */
int L[] = new int[n1];
int R[] = new int[n2];
/* Copy data to temp arrays L[]
and R[] */
for (i = 0; i < n1; i++){
L[i] = arr[left_start + i];
}
for (j = 0; j < n2; j++){
R[j] = arr[mid + 1+ j];
}
i = 0;
j = 0;
k = left_start;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1){
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
}
System.out.printf("\nSorted array is \n");
int i= 0;
for (i=0; i < n; i++){
System.out.print(arr[i]+ " ");
}
}
}
I think I've fixed it. Certainly works for given array.
Try
int mid = Math.min(left_start + curr_size - 1, n-1);
instead of
int mid = left_start + curr_size - 1;
When my sort is run on this data {7,8,4,2,3,9,5,8,4,1} only the first element is not put in its correct place. How can I fix this? Thanks for the help.
public void segmentedInsertionSort(int[] array, int size, int h)
{
int temp;
for(int i = h + 1 ;i < size;i++)
{
int j = i - h;
while(j > 0)
{
if(array[j+h] < array[j])
{
temp = array[j];
array[j] = array[j+h];
array[j+h] = temp;
j = j - h;
}
else
{
j = 0;
}
}
}
}
public void shellSort(int[] array, int size)
{
int h = size/2;
while(h > 0)
{
segmentedInsertionSort(array,size,h);
h = h/2;
}
}
for(int i = h + 1 ;i < size;i++)
{
int j = i - h;
while(j > 0)
{
if(array[j+h] < array[j])
{
temp = array[j];
array[j] = array[j+h];
array[j+h] = temp;
j = j - h;
}
In this part, you define i = h + 1 and then increase i value. So, so the j value is never less than 1 when the sort runs. Therefore, it never processes the first element of array. You need to fix this part.
I think you have misvalued some variables.Your variable j never reaches index 0 so as to compare 7 with any other value in this Comparison Sort.
Change:
for(int i = h +1 ;i < size;i++) to for(int i = h ;i < size;i++)
And
while(j > 0) to while(j >= 0)
else
{
j = 0;
}
to
else{
j = -1;
}
Final Code looks like:
` void segmentedInsertionSort(int arr[], int size, int h)
{
int temp;
for(int i = h ;i < size;i++)
{
int j = i-h ;
while(j >= 0)
{
if(arr[j+h] < arr[j])
{
temp = arr[j];
arr[j] = arr[j+h];
arr[j+h] = temp;
j = j - h;
}
else
{
j = -1;
}
}
}
}
void shellSort(int arr[], int size)
{
int h = size/2;
while(h > 0)
{
print(arr);
segmentedInsertionSort(arr,size,h);
h = h/2;
}
}
Using selection sort to sort an array. I think my logic is right but there's this stupid error.
public static void arraySort(int[] a) {
//for loop to go through array
for(int i = 0; i < a.length; i++) {
int temp = a[i]; //set a temp value for first value
for (int x = i + 1; x < a.length; x++) {
if (a[x] < temp) {
a[i] = a[x];
temp = a[x];
}
}
}
}
At the end it keeps printing only one group of values repeatedly.
You should track the minimum index in the outer loop and swap two elements after each inner loop completion, e.g.:
for (i = 0; i < size - 1; i++) {
minIndex = i;
for (j = i; j < size; j++) {
if (array[j] < array[minIndex]) {
minIndex = j;
}
}
swap(array, i, minIndex);
}
Your fixed code:
public static void arraySort(int[] a) {
//for loop to go through array
for(int i = 0; i < a.length; i++) {
int temp = a[i]; //set a temp value for first value
int minIndex = i;
for (int x = i + 1; x < a.length; x++) {
if (a[x] < temp) {
a[i] = a[x];
temp = a[x];
minIndex = x;
}
}
//swap
a[minIndex] = a[i];
a[i] = temp;
}
}
Recently, I took Linkedin placement test in which there was a question in which output for 4 test cases were wrong for me. I could not figure out what was my mistake becasue inputs/outputs were hidden.
Anyways here was the question:
Find the maximum element from an array where product of any other two elements would be equal to that number and return that number .If no, such element is there then return -1.
Here was my solution:
static int maxElement(int[] arr) {
Arrays.sort(arr);
int max = arr[arr.length-1];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
return -1;
}
I guess you need to find the possible maximum number in the array and the product of two elements in the array.
If I assume this, your code fails for this test case:
int[] arr = {2,4,5,3,7,6}; , where the answer should be 6
Check this below code it will work for above test-case.
Just add one more reverse for loop to check the possible value and product.
static int maxElement(int[] arr) {
Arrays.sort(arr);
for (int k = arr.length-1; k >= 0; k--) {
int max = arr[k];
int result = 0;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i + 1; j < arr.length; j++) {
result = arr[i] * arr[j];
if (result == max) {
return max;
}
}
}
}
return -1;
}
List<Integer> list = new ArrayList<Integer>();
for (int i = 0; i < a.length; i++) {
list.add(a[i]);
}
int maxSum = 0;
for (int i = 0; i < a.length; i++) {
for (int j = i + 1; j < a.length; j++) {
if ((a[i] * a[j]) > maxSum) {
if(list.contains(a[i] * a[j]))
maxSum = a[i] * a[j];
}
}
}
if (maxSum != 0)
return maxSum;
return -1;