Professional Documents
Culture Documents
Codes
Codes
// 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]
----------------------------------------------------
// 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);
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)
{
OUTPUT
1111
1110
1101
1100
1011
1010
---------------------------
import java.util.*;
class Graph
{
List<List<Integer>> adjList = null;
Graph(List<Edge> edges, int N) {
adjList = new ArrayList<>(N);
adjList.get(src).add(dest);
adjList.get(dest).add(src);
}
}
}
class Main {
if (!discovered[i])
{
if (isConnected(graph, i, dest, discovered, path)) {
return true;
}
}
}
path.poll();
return false;
}
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, 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
-----------------------------------------------------------