forked from dharmanshu1921/Website-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjava07.java
40 lines (40 loc) · 1.13 KB
/
java07.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;
import java.util.*;
class RECURSION {
public static void main(String[] args)
{
//input Array
int[] x = { 1, 2, 3, 4, 5 };
//Recursive function that will print answer
printTriangle(x);
System.out.println(Arrays.toString(x));
}
static void printTriangle(int[] x)
{
//base condition
if (x.length == 1) {
return;
}
/*temprory Array of input array in print(int[] arr) function
for example arr = {1,2,3,4,5}
then temp[] = {3,5,7,9} of size(arr.length-1)=4*/
int[] temp = new int[x.length - 1];
//Recursive function to fill elements in temp Array according to Que.
helper(temp, x, 0);
//Recursive call for print(int[] arr) function
printTriangle(temp);
//prints String format of temp Array//
System.out.println(Arrays.toString(temp));
}
//Recursive function to fill elements in temp Array//
static int[] helper(int[] temp, int[] x, int index)
{
//base condition//
if (index == x.length - 1) {
return temp;
}
temp[index] = x[index] + x[index + 1];
//Recursive Call for helper() function//
return helper(temp, x, index + 1);
}
}