recursion method with 2d array (java) - java

i have to write a method,
The method receives a parameter of two-dimensional array of integers. The method returns the number of the row which has the highest sum of the integers.I'm allowed to use only recursion! no loops allowed!-of course i need to make a private method that will sum a row as a single array and then i have to do another private method that compares the rows, but it doesn't really work since the method i wrote is only for a 1d array, and i need to compare a row from a 2d array..
appreciate all kind of help..
some of my code:
private int rowSum(int[] array, int index) {//the sum of an array(1d array)
if (index == array.length)
return 0;
else
return array[index] + rowSum(array, index + 1);
}
**public int maxRow(int[][] a){------!!!---the problem...
}**

I would try calling a recursive method from main with arguments:
array[][]
current column index
current row index
current row sum
highest row index
highest row sum
I got it working here, so it is definitely possible.
It's about 15 lines of code.
Good luck!

As a starter exercise, try to change the signature of your rowSum function into this:
int rowSum(int[] array, int index, int sumSoFar)
You should be able to write the function such that the recursive part's return expression is just a recursive call:
if (...) // exit case
return sumSoFar;
else
return rowSum(...);
(This is now a tail-recursive implementation.)
Armed with that mindset, think about how you would write:
int indexOfMaxValue(int[] array, int index, int maxValueSoFar, int maxIndexSoFar)
Once you get those, you should be able to combine those two concepts together.

The following code is ready to run and test:
public class RecursionSum {
/* Sum a row until <code>index</code>. */
private static int rowSum(int[] a, int index) {
if (index == 0) { return a[0]; }
// add the current element to the recursive sum
return a[index] + rowSum(a, index - 1);
}
/* Sum a whole row. */
private static int rowSum(int[] a) {
return (a.length == 0) ? 0 : rowSum(a, a.length - 1);
}
/* Find the index of the array having the max sum until <code>index</code>. */
private static int maxRow(int[][] a, int index){
if (index == 0) { return 0; }
// compare the current row's sum with the recursive max row's sum,
// update index when it's a new winner
int maxIndex = maxRow(a, index - 1);
return (rowSum(a[maxIndex]) < rowSum(a[index])) ? index : maxIndex;
}
/* Find the index of the array having the max sum. */
public static int maxRow(int[][] a){
return a.length == 0 ? 0 : maxRow(a, a.length - 1);
}
/* very simple test */
public static void main(String[] args) {
int a1[][] = {};
int a2[][] = {{1, 2, 3, 4}};
int a3[][] = {{ 1, 2, 3, 4}, {8, 90}, {5, 6, 7}, {300, 4, 9}, {4, 6, 12}};
System.out.println(maxRow(a1));
System.out.println(maxRow(a2));
System.out.println(maxRow(a3));
}
}

Here is what I came up with...
public static void main(String[] args)
{
int[][] mat = { { 2, 6, 5, 4 }, { 3, 17, 2, 6 }, { 1, 3, 21, 0 } };
System.out.println(maxRow(mat)); // 1
int[][] mat1 = { { 2, 6, 5, 4 }, { 1, 3, 21, 0 }, { 3, 17, 2, 6 } };
System.out.println(maxRow(mat1)); // 2
}
public static int maxRow(int[][] mat)
{
return maxRow(mat, 0, 0, -1);
}
private static int maxRow(int[][] mat, int row, int maxSumInRow, int maxRowNo)
{
if (row == mat.length)
return maxRowNo;
int sumRow = sumRow(mat, row, 0);
if (sumRow > maxSumInRow)
{
maxSumInRow = sumRow;
maxRowNo = row;
}
return maxRow(mat, row + 1, maxSumInRow, maxRowNo);
}
private static int sumRow(int[][] mat, int row, int col)
{
if (col == mat[row].length)
return 0;
return mat[row][col] + sumRow(mat, row, col + 1);
}

Code:
public class MainClass {
static int contents[][] = { {1, 2 , 3, 4} , { 4, 5, 8, 7}, { 4, 2, 8, 7}, { 4, 5, 0, 7} };
public static void main(String[] args)
{
System.out.println(getIndexOfRowWithHighestSum(contents, 0, 0));
}
public static int getIndexOfRowWithHighestSum(int[][] twoDAray, int currentIndex,int indexWithHighestSum){
if(currentIndex>=twoDAray.length){
return indexWithHighestSum;
}
int sum1 = getSumOfArray(twoDAray[currentIndex], 0) ;
int sum2 = getSumOfArray(twoDAray[indexWithHighestSum], 0);
indexWithHighestSum = (sum1 > sum2)?currentIndex:indexWithHighestSum;
return getIndexOfRowWithHighestSum(twoDAray, currentIndex+1,indexWithHighestSum);
}
public static int getSumOfArray(int[] array, int currentIndex){
if(currentIndex>=array.length){
return 0;
}
return array[currentIndex]+getSumOfArray(array,currentIndex+1);
}
}

Related

isSorted? Check array if is sorted in ascending or descending order in Java

can someone help me check my codes if are right or help me know if there is any other is way to solve this question I was trying to check if an array is in ascending or descending order then return 1 if not then return 0; at first I created some method for sorting array in increasing order and order and another method for decreasing and then I used those method to compare with the original array if it is sorted. I used the code below:
public class IsSorted {
public static void main(String[] args){
int[] list ={4,3,2,1};
System.out.println(isSorted(list));
}
public static int isSorted(int[] a){
if(a.length==0){
return 1;
}
if(a.length==1){
return 1;
}
int[] holdingArray=new int[a.length];
for (int i =0; i<a.length; i++){
holdingArray[i]=a[i];
}
int[] virtualIncreasedArray= new int[holdingArray.length];
int[] virtualDecreasedArray= new int[holdingArray.length];
sortIncrease(holdingArray);
for(int i=0; i<holdingArray.length;i++){
virtualIncreasedArray[i]=holdingArray[i];
}
sortDecrease(holdingArray);
for(int i=0; i<holdingArray.length;i++){
virtualDecreasedArray[i]=holdingArray[i];
}
//check if array is decreasing
for(int i=0; i<virtualDecreasedArray.length;i++){
if(virtualDecreasedArray[i]!=a[i]&&virtualIncreasedArray[i]!=a[i]){
return 0;
}
}
//check if array is increasing
return 1;
}
static void sortIncrease(int[] a){
for(int unsorted=a.length-1; unsorted>0; unsorted--){
for(int i=0; i<unsorted;i++){
if(a[i]>a[i+1]){
swap(a,i,i+1);
}
}
}
}
static void sortDecrease(int[] a){
for(int unsorted=a.length-1; unsorted>0; unsorted--){
for(int i=0; i<unsorted; i++){
if(a[i]<a[i+1]){
swap(a,i,i+1);
}
}
}
}
static void swap(int[] a, int i, int j){
if(i==j){
return;
}
int temp = a[i];
a[i]=a[j];
a[j]=temp;
}
}
For an accurate verification, the following should be done as there are important side cases to consider.
Checking if any list starts out with some number of equal values.
Determine the starting index where the values first differ.
If all the values are equal, return true immediately.
Note that in the worst case when all the values are equal, the entire array needs to be checked (sans the last value since then it could be either ascending or descending).
int[] sortedAscending = { 1, 1, 3, 4, 7, 10, 11, 15, 15 };
int[] sortedDescending = { 22, 22, 12, 8, 8, 8, 5, 2, 1 };
int[] sortedBoth = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 };
int[] unsorted = { 2, 1, 2, 19, 19, 2, 4 };
System.out.println(isSorted(sortedAscending));
System.out.println(isSorted(sortedDescending));
System.out.println(isSorted(sortedBoth));
System.out.println(isSorted(unsorted));
Prints
true
true
true
false
The check method.
public static boolean isSorted(int[] arr) {
int start = 0;
// if the lists start with equal values, need to
// determine a starting point.
while (arr[start] == arr[start+1]
&& start++ < arr.length - 2);
if (start >= arr.length - 2) {
// all but the last the same value, its sorted
return true;
}
boolean asc = arr[start] < arr[start + 1];
for (int i = start; i < arr.length - 1; i++) {
if (asc) {
//check ascending
if (arr[i] > arr[i + 1]) {
return false;
}
// check descending
} else if (arr[i] < arr[i + 1]) {
return false;
}
}
return true;
}
Since you asked for another way to do this, here is a different approach.
What you could do is:
Determine whether the array is (supposedly) sorted in ascending or descending order based on the first 2 elements (if these exist)
Consider equal values when determining the supposed sorting (thanks for pointing that out #WJS)
Then, check the rest of the array for the correct order, based on what was determined
Updated Example:
public static void main(String[] args) {
int[] sortedAsc = { 1, 2, 3, 4, 5 };
int[] sortedDesc = { 5, 4, 2, 1 };
int[] unsortedArray = { 1, 8, 2, 4 };
int[] allEqual = { 3, 3, 3, 3, 3 };
int[] firstEqual = { 2, 2, 3, 2 };
System.out.println(isSorted(sortedAsc));
System.out.println(isSorted(sortedDesc));
System.out.println(isSorted(unsortedArray));
System.out.println(isSorted(allEqual));
System.out.println(isSorted(firstEqual));
}
public static boolean isSorted(int[] arr) {
boolean isAscending = false;
if (arr.length < 2) { // if the array has less than 2 elements, must be sorted
return true;
}
if (arr[0] < arr[1]) { // do we think this array is sorted ascending?
isAscending = true;
} else {
int index = 0;
while (arr[index] == arr[index + 1] && index++ < arr.length - 2) {
// keep checking for sorting if array consists of equal values
if (index >= arr.length - 2) {
return true; // whole array consists of equal values
}
}
// now that equal values were skipped, check for sorting again
isAscending = arr[index] < arr[index + 1];
}
// check all elements of the array
for (int i = 0; i < arr.length - 1; i++) {
if (isAscending) {
if (arr[i] > arr[i + 1]) {
return false;
}
} else {
if (arr[i] < arr[i + 1]) {
return false;
}
}
}
return true;
}
Output:
true
true
false
true
false
Sidenotes for your code:
Instead of returning an int (0, 1), your isSorted() method should definitely return boolean.
There is no point in the holdingArray.

Binary Search implementation via java

Please see the below java source code for binary search implementation
public class Main {
public static void main(String[] args) {
int[] x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int y = binarySearch(x, 11);
System.out.println(y);
}
public static int binarySearch(int[] arr, int value) {
int searchedIndex = -1;
int first = 0;
int last = arr.length - 1;
int mid;
while (first <= last) {
mid = (first + last) / 2;
if (arr[mid] == value) {
searchedIndex = mid;
break;
} else {
if (value < arr[mid]) {
last = mid - 1;
} else {
first = mid + 1;
}
}
}
return searchedIndex;
}
}
int last = arr.length - 1 is -1 compulsory or not. I feel that code works fine either last = arr.length - 1. If its compulsory please explain why.
Arrays already have a method binarySearch you can just use :
int[] x = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11};
int r = Arrays.binarySearch(x, 11);
The -1 is needed for one very specific case: when you search for a value that is larger than the largest value in the array. For example, calling your function with
int y = binarySearch(x, 50);
will throw an ArrayOutOfBounds exception. That's because mid will eventually be equal to last, and without the -1, last is past the end of the array.

Find the k smallest integer in an array

Here is my code, it works for finding 1-7 smallest integers, but 8 and 9. It returns null when I find 8 smallest integer in the array. Can anyone help me where is the problem? I am using quicksort here.
Thanks very much!
update: I have figured out the problem, which is the array in the main function. After I change to the following look,
int[] arr = {2, 3, 1, 7, 5, 6, 20, 8, 4, 9};
and
if(front>=end) return input;
It works now!
import java.util.Arrays;
import java.io.*;
class quicksort{
public static void main(String[] args){
int[] arr = new int[9];
arr[0] = 7;
arr[1] = 2;
arr[2] = 4;
arr[3] = 8;
arr[4] = 3;
arr[5] = 5;
arr[6] = 1;
arr[7] = 0;
arr[8] = 10;
System.out.println((Arrays.toString(findKSamllest(arr,8))));
}
public static int partition(int[] input, int front, int end){
int pivot = input[front];
while(front < end){
while(input[front]<pivot)
front++;
while(input[end]>pivot)
end--;
swap(input,front,end);
}
return front;
}
public static void swap(int[] input, int s, int l){
int temp = input[s];
input[s] = input[l];
input[l] = temp;
}
public static int[] findK(int[] input, int front, int end, int k){
if(front>=end) return null;
int pivot = partition(input,front,end);
//System.out.println(pivot);
if(k==pivot){
return Arrays.copyOfRange(input,0,pivot);
}
else {
if(k<pivot)
return findK(input,front,pivot,k);
return findK(input,pivot+1,end,k);
}
}
public static int[] findKSamllest(int[] input, int k){
return findK(input, 0, input.length-1, k);
}
}
Change
if(front >= end) return null;
to
if(front > end) return null;
Stand on the shoulders of giants and use the libraries that are already available:
Arrays.sort(myArray);
int[] returnArray = new int(NUM_ITEMS_TO_RETURN);
for (int i=0; i < NUM_ITEMS_TO_RETURN; i++)
{
returnArray[i] = myArray[i];
}
return returnArray;
Obviously you have to do some error checking, for example that the initial array is larger or equal to the number of items you want returned, but that's trivial.
You could save you a bit time and impress your teacher with the fancy new Java 8 API.
It provides streams and useful functions to solve this in one (long) line or 5, if it should be readable ;-)
final List<Integer> sortedKList = Arrays.asList(7, 2, 4, 8, 3, 5, 1, 0, 10)
.stream()
.sorted()
.limit(7)
.collect(Collectors.toList());
You can then review your result with:
sortedKList.forEach(System.out::println);

How to find out the odd integers in an array using a recursive method?

I am trying to write a method that finds how many odd numbers are between first position and last position. The method accepts an array, and then two ints for the low and high position. This method needs to be made recursively. Here is what I have so far. Here is the method call and the int array. I'm getting an output of 1 but the answer should be 2.
int array [] = {5, 2, 5, 6, 3, 1};
int n = countOddsInRange(array, 1, 4)
public static int countOddsInRange(int [] a, int first, int last)
{
int count = 0;
if(first <= last)
{
countOddsInRange(a, a[first + 1], a[last]);
if(a[first] % 2 == 0)
{
count++;
}
}
return count;
}
You have some bugs in your code :
You are counting even numbers, not odd. Change your condition to if(a[first] % 2 != 0)
The recursive call should get indices of the array, not the values in those locations.
You should add the result of the recursive call to the total : count+=countOddsInRange(a, first + 1, last)
To summarize :
public static int countOddsInRange(int [] a, int first, int last)
{
int count = 0;
if(first <= last)
{
count+=countOddsInRange(a, first + 1, last);
if(a[first] % 2 != 0)
{
count++;
}
}
return count;
}
Your function should looks like :
public static int countOddNumber(int [] a, int first, int last){
return countOddNumber(a, first, last, 0);
}
public static int countOddNumber(int [] a, int first, int last, int count){
//in recursive function start with termination test.
if (first == last){
return count + isOdd(a[first]);
}
else{
return countOddNumber(a, first+1, last, count) + isOdd(a[first]);
}
}
public static int isOdd(int number){
if(number%2 == 0){return 1;}
else{return 0}
}
You should also add a test to check if first is lesser than last to avoid infinite loop ;)
PS : filter element whose mod(element,2) = 0 and then get the size of the collection. That method use functional style too, and use new Java8 feature. And is probably much more faster.
public class CountOddsInRange {
public static void main(String[] args) {
int array[] = {5, 2, 5, 6, 3, 1};
int n = countOddsInRange(array, 0, array.length - 1);
System.out.println("No of odds is " + n);
}
public static int countOddsInRange(int [] a, int first, int last)
{
int count = 0;
if(first <= last)
{
count+=countOddsInRange(a, first + 1, last);
if(a[first] % 2 != 0)
{
count++;
}
}
return count;
}
}
public class CountOddsInRange {
public static void main(String[] args) {
int array[] = {5, 2, 5, 6, 3, 1};
int n = countOddsInRange(array, 0, array.length - 1, 0);
System.out.println("No of odds is " + n);
}
public static int countOddsInRange(int[] a, int first, int last, int count) {
if (first <= last) {
if (a[first] % 2 != 0) {
count++;
}
return countOddsInRange(a, first + 1, last, count);
}
return count;
}
}

A recursive algorithm to find every possible sum given an integer array?

So given an array for example [3, 5, 7, 2] I want to use recursion to give me all the possible combinations of sums, for example: 3, 5, 7, 2, 8(3+5),10(3+7),5(3+5)... 15(3+5+7) etc. I'm not exactly sure how to go about this using java.
You have two choice with each number in the array.
Use the number
Don't use the number
void foo(int[] array, int start, int sum) {
if(array.length == start) return;
int val = sum + array[start];
//print val;
foo(array, start + 1, val); //use the number
foo(array, start + 1, sum); //don't use the number
}
the initial call is foo(a, 0, 0)
An recursive algorithm for this could work as follows:
All the sums for a list equals the union of:
The first number plus the sums of the sublist without the first number
The sums of the sublist without the first number
Eventually your recursive call will hit the stopping condition of an empty list, which has only one sum(zero)
Here's one way of doing it in pseudo code:
getAllPossibleSums(list)
if(list.length == 1)
return list[0];
otherSums = getAllPossibleSums(list[1:end])
return union(
otherSums, list[0] + otherSums);
public static void main(String[] args) {
findAllSums(new int[] {3, 5, 7, 2}, 0, 0);
}
static void findAllSums(int[] arrayOfNumbers, int index, int sum) {
if (index == arrayOfNumbers.length) {
System.out.println(sum);
return;
}
findAllSums(arrayOfNumbers, index + 1, sum + arrayOfNumbers[index]);
findAllSums(arrayOfNumbers, index + 1, sum);
}
You have two branches, one in which you add the current number and another in which you don't.
public static void main(String[] args) {
int [] A = {3, 5, 7, 2};
int summation = recursiveSum(A, 0, A.length-1);
System.out.println(summation);
}
static int recursiveSum(int[] Array, int p, int q) {
int mid = (p+q)/2; //Base case
if (p>q) return 0; //Base case
else if (p==q) return Array[p];
**else
return recursiveSum(Array, p, mid) + recursiveSum(Array, mid + 1, q);**
}
Simplest way ever:
private int noOfSet;
Add below method:
private void checkSum() {
List<Integer> input = new ArrayList<>();
input.add(9);
input.add(8);
input.add(10);
input.add(4);
input.add(5);
input.add(7);
input.add(3);
int targetSum = 15;
checkSumRecursive(input, targetSum, new ArrayList<Integer>());
}
private void checkSumRecursive(List<Integer> remaining, int targetSum, List<Integer> listToSum) {
// Sum up partial
int sum = 0;
for (int x : listToSum) {
sum += x;
}
//Check if sum matched with potential
if (sum == targetSum) {
noOfSet++;
Log.i("Set Count", noOfSet + "");
for (int value : listToSum) {
Log.i("Value", value + "");
}
}
//Check sum passed
if (sum >= targetSum)
return;
//Iterate each input character
for (int i = 0; i < remaining.size(); i++) {
// Build list of remaining items to iterate
List<Integer> newRemaining = new ArrayList<>();
for (int j = i + 1; j < remaining.size(); j++)
newRemaining.add(remaining.get(j));
// Update partial list
List<Integer> newListToSum = new ArrayList<>(listToSum);
int currentItem = remaining.get(i);
newListToSum.add(currentItem);
checkSumRecursive(newRemaining, targetSum, newListToSum);
}
}
Hope this will help you.

Categories

Resources