You are on page 1of 6

Sum triangle from array

// Java program to create Special triangle.


import java.util.*;

public class ConstructTriangle


{
// Function to generate Special Triangle.
public static void printTriangle(int[] A)
{
// Base case
if (A.length < 1)
return;

// Creating new array which contains the


// Sum of consecutive elements in
// the array passes as parameter.
int[] temp = new int[A.length - 1];
for (int i = 0; i < A.length - 1; i++)
{
int x = A[i] + A[i + 1];
temp[i] = x;
}

// Make a recursive call and pass


// the newly created array
printTriangle(temp);

// Print current array in the end so


// that smaller arrays are printed first
System.out.println(Arrays.toString(A));
}

// Driver function
public static void main(String[] args)
{
int[] A = { 1, 2, 3, 4, 5 };
printTriangle(A);
}
}

OUTPUT

[48]
[20, 28]
[8, 12, 16]
[3, 5, 7, 9]
[1, 2, 3, 4, 5]
----------------------------------------------------

REVERSE STRING USING RECURSION

public class Reverse


{
private static void swap(char[] c, int i, int j) {
char temp = c[i];
c[i] = c[j];
c[j] = temp;
}

// Recursive function to reverse a given String


public static void reverse(char[] c, int l, int h)
{
if (l < h)
{
swap(c, l, h);
reverse(c, l + 1, h - 1);
}
}

// main function
public static void main(String[] args)
{
String str = "ejie lontoco";

char[] c = str.toCharArray();
reverse(c, 0, c.length - 1);
str = new String(c);

System.out.print("Reverse of the given String is : " + str);


}
}

OUTPUT
ocotnol eije

---------------------------------------------------------------

FIND ALL N-DIGIT NUMBERS HAVING MORE 1'S THAN O'S FOR ANY PREFIX

class Util
{
// Function to find all N-digit binary numbers having
// more 1's than 0's at any position
public static void find(String str, int n, int zeros, int ones)
{

if (ones < zeros) {


return;
}
if (n == 0)
{
System.out.println(str);
return;
}

find(str + "1", n - 1, zeros, ones + 1);


find(str + "0", n - 1, zeros + 1, ones);
}

public static void main(String[] args)


{
// given number of digits
int n = 4;
String str = "";
find(str, n, 0, 0);
}
}

OUTPUT

1111
1110
1101
1100
1011
1010

---------------------------

FIND THE PATH BETWEEN GIVEN VERTICES IN A DIRECT GRAPT

import java.util.*;

public class Edge


{
public final int source, dest;

private Edge(int source, int dest) {


this.source = source;
this.dest = dest;
}
public static Edge of(int a, int b) {
return new Edge(a, b);
}
};

class Graph
{
List<List<Integer>> adjList = null;
Graph(List<Edge> edges, int N) {
adjList = new ArrayList<>(N);

for (int i = 0; i < N; i++) {


adjList.add(i, new ArrayList<>());
}

for (int i = 0; i < edges.size(); i++)


{
int src = edges.get(i).source;
int dest = edges.get(i).dest;

adjList.get(src).add(dest);
adjList.get(dest).add(src);
}
}
}
class Main {

public static boolean isConnected(Graph graph, int src, int dest,


boolean[] discovered, Queue<Integer> path)
{
discovered[src] = true;
path.add(src);
if (src == dest) {
return true;
}
for (int i: graph.adjList.get(src))
{

if (!discovered[i])
{
if (isConnected(graph, i, dest, discovered, path)) {
return true;
}
}
}
path.poll();
return false;
}

public static void main(String[] args)


{
// List of graph edges as per above diagram
List<Edge> edges = Arrays.asList(Edge.of(0, 3), Edge.of(1, 0),
Edge.of(1, 2),
Edge.of(1, 4),
Edge.of(2, 7),
Edge.of(3, 4),
Edge.of(3, 5),
Edge.of(4, 3),
Edge.of(4, 6),
Edge.of(5, 6),
Edge.of(6, 7)
);

int N = 8;
Graph graph = new Graph(edges, N);
boolean[] discovered = new boolean[N];
int src = 0, dest = 7;
Queue<Integer> path = new ArrayDeque<>();
if (isConnected(graph, src, dest, discovered, path))
{
System.out.println("Path exists from vertex " + src +
" to vertex " +
dest);
System.out.println("The complete path is: " + path);
}
else {
System.out.println("No path exists between vertices " + src +
" and
" + dest);
}
}
}
OUPUT
Path exists from vertex 0 to vertex 7
The complete path is: [0, 3, 4, 1, 2, 7]

----------------------------------------------------------

FIND ALL N-DIGITS NUMBERS WITH EQUAL OF DIGITS AT EVEN ODD INDEX

class Util
{
// Function to find all N-digit numbers with equal sum of digits at even
// and odd index in Bottom-up manner
public static void findNdigitNums(String res, int n, int diff)
{
// if number is less than N-digit
if (n > 0)
{
char ch = '0';

if (res.equals("")) {
ch = '1';
}
while (ch <= '9')
{
int absdiff;

if ((n & 1) != 0) {
// add value to diff if odd digit
absdiff = diff + (ch - '0');
}
else {
// subtract value from diff if even
absdiff = diff - (ch - '0');
}

findNdigitNums(res + ch, n - 1, absdiff);


ch++;
}
}
else if (n == 0 && Math.abs(diff) == 0) {
System.out.println(res);
}
}

public static void main(String[] args)


{
int n = 3; // N-digit
String res = "";

findNdigitNums(res, n, 0);
}
}

OUTPUT

110121 132 143 154 165 176 187 198 220 231
242 253 264 275 286 297 330 341 352 363 374
385 396 440 451 462 473 484 495 550 561 572
583 594 660 671 682 693 770 781 792 880 891 990

-----------------------------------------------------------

You might also like