prompt stringlengths 396 31.4k |
|---|
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.io.*;
import java.util.*;
public class Main {
static InputReader in = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
static long oo = 1000000000000L;
static int[][] memo;
public static void main(Str... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.Scanner;
public class NickAndArray {
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int array[]=new int[n];
int max=Integer.MAX_VALUE;
int index=0;
for(int i=0;i<n;i++)
{
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.*;
public class Main {
static int n=5;
static int[] arr=new int[5];
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int arr[]=new int[n];
for (int ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.*;
public class MyClass {
public static void main(String args[]) {
Scanner sc= new Scanner(System.in);
int n=sc.nextInt();
int [] a= new int[n];
int k=0;
int m=0;
for (int i=0;i<n;i++){
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.*;
import java.io.*;
import java.math.*;
public class round569d2c {
public static void main(String args[]) {
FastScanner in = new FastScanner(System.in);
int n = in.nextInt();
int q = in.nextInt();
ArrayDeque<Integer> deq = new Ar... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.io.*;
import java.util.*;
public class Main {
static InputReader in = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
static long oo = 1000000000000L;
public static void main(String[] args) throws IOE... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.*;
public class Kello
{
public static void main(String args[])
{
Scanner sc =new Scanner(System.in);
int n,q,m,max,inp,k,i;
long in_q;
n=sc.nextInt();
q=sc.nextInt();
int a[]=new int[n-1]... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find the Minimum length Unsorted Subarray,
// sorting which makes the complete array sorted
class
Main
{
static
void
printUnsorted(
int
arr[],
int
n)
{
int
s =
0
, e = n-
1
, i, max, min;
// step 1(a) of above algo
for
(s =
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.io.*;
import java.math.BigInteger;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.InputMismatchException;
import java.util.StringTokenizer;
public class C {
static class FastWriter {
private final BufferedWriter b... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.io.*;
import java.util.*;
public class Main
{
class node{
int data;
node next;
public node(int val){
data=val;
next=null;
}
}
class linkedlist{
node start;
node end;
int size;
int turn;
public linkedlist(){
start=nu... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class C {
void solve(){
int n = readInt();
int q = readInt();
int max = 0;
int[] a = new int[n];
D... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|> import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.geom.*;
import java.math.*;
import java.text.*;
import java.math.BigInteger.*;
import java.util.Arrays;
public class CF111111
{
BufferedReader in;
StringTokeni... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.*;
public class Main {
public static void main(String[] args) {
// write your code here
Scanner scan = new Scanner(System.in);
int T = scan.nextInt();
for(; T > 0; T--) {
int n = scan.nextInt();
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.*;
import java.io.*;
import java.math.*;
public class Solution{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|> import java.io.*;
import java.util.*;
import java.lang.*;
import java.awt.*;
import java.awt.geom.*;
import java.math.*;
import java.text.*;
import java.math.BigInteger.*;
import java.util.Arrays;
public class CF111111
{
BufferedReader in;
StringTokeni... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.Scanner;
public class pillar {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[200005];
for (int i=1;i<=n;i++)
a[i]=sc.nextInt();
for (int i=2;i<n;i++)
if (a[i-1... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.util.*;
import java.io.*;
import java.math.*;
public class Solution{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CF1197B {
public static void main(String[] args) {
FastReader input = new FastReader();
int n = inpu... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>import java.io.*;
import java.util.*;
public class codef
{
public static void main(String ar[]) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(br.readLine());
S... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
import
java.io.*;
class
PairSum {
// Fills element in arr[] from its pair sum array pair[].
// n is size of arr[]
static
void
constructArr(
int
arr[],
int
pair[],
int
n)
{
arr[
0
] = (pair[
0
]+pair[
1
]-pair[n-
1
]) /
2
;
for
(
int
i=
1
; i<... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to calculate the
// product of max element of first
// array and min element of second array
import
java.util.*;
import
java.lang.*;
class
GfG
{
// Function to calculate the product
public
static
int
minMaxProduct(
int
arr1[],
int... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java Program to find all the common characters
// in n strings
import
java.util.*;
import
java.lang.*;
class
GFG {
static
int
MAX_CHAR =
26
;
public
static
void
commonCharacters(String str[],
int
n)
{
// primary array for common character... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java implementation to find the uncommon
// characters of the two strings
class
GFG
{
// size of the hash table
static
int
MAX_CHAR =
26
;
// function to find the uncommon
// characters of the two strings
static
void
findAndPrintUncommonChar... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java implementation of alternate vowel and
// consonant string
import
java.util.*;
class
GFG
{
// 'ch' is vowel or not
static
boolean
isVowel(
char
ch)
{
if
(ch ==
'a'
|| ch ==
'e'
|| ch ==
'i'
||
ch ==
'o'
|| ch ==
'u'
)
return
true
;
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A O(n) C++ program to count number of substrings
//starting and ending with 1
class
CountSubString
{
int
countSubStr(
char
str[],
int
n)
{
int
m =
0
;
// Count of 1's in input string
// Traverse input string and count of 1's in it
for
(
in... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to get same frequency character
// string by removal of at most one char
public
class
GFG {
static
final
int
M =
26
;
// Utility method to get index of character ch
// in lower alphabet characters
static
int
getIdx(
char
ch)
{
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java implementation to find the character in
// first string that is present at minimum index
// in second string
import
java.util.HashMap;
public
class
GFG
{
// method to find the minimum index character
static
void
printMinIndexChar(String str,... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to remove duplicates, the order of
// characters is not maintained in this program
public
class
GFG
{
static
final
int
NO_OF_CHARS =
256
;
/* Returns an array of size 256 containg count
of characters in the passed char array */
sta... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java implementation of program to find
// the maximum length that can be removed
import
java.util.ArrayList;
public
class
GFG
{
// User defined class Pair
static
class
Pair{
char
first;
int
second;
Pair(
char
first,
int
second){
this
.firs... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// An otpimized Java program to find pairs with distance
// equal to English alphabet distance
class
Test {
static
final
int
MAX_CHAR =
26
;
// Method to count pairs with distance
// equal to English alphabet distance
static
int
countPairs(String ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Recursive Java Program to reverse an array
import
java.io.*;
class
ReverseArray {
/* Function to reverse arr[] from start to end*/
static
void
rvereseArray(
int
arr[],
int
start,
int
end)
{
int
temp;
if
(start >= end)
return
;
temp = arr[... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to Find longest subsequence where
// every character appears at-least k times
class
GFG {
static
final
int
MAX_CHARS =
26
;
static
void
longestSubseqWithK(String str,
int
k) {
int
n = str.length();
// Count frequencies of all ch... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to count subsequences of the
// form a^i b^j c^k
public
class
No_of_subsequence {
// Returns count of subsequences of the form
// a^i b^j c^k
static
int
countSubsequences(String s)
{
// Initialize counts of different subsequences
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java code to find number of subsequences of
// "ab" in the string S which is repeated K times.
import
java.io.*;
class
GFG {
static
int
countOccurrences(String s,
int
K)
{
int
n = s.length();
int
C =
0
, c1 =
0
, c2 =
0
;
for
(
int
i =
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java Program to find the "GFG" subsequence
// in the given string
public
class
GFG {
static
int
max =
100
;
// Print the count of "GFG" subsequence
// in the string
static
void
countSubsequence(String s,
int
n)
{
int
cntG =
0
, cntF =
0... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to count number of distinct
// subsequences of a given string.
import
java.util.ArrayList;
import
java.util.Arrays;
public
class
Count_Subsequences {
static
final
int
MAX_CHAR =
256
;
// Returns count of distinct sunsequences of str.... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find nth ugly number
import
java.lang.Math;
class
UglyNumber
{
/* Function to get the nth ugly number*/
int
getNthUglyNo(
int
n)
{
int
ugly[] =
new
int
[n];
// To store ugly numbers
int
i2 =
0
, i3 =
0
, i5 =
0
;
int
next... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A O(n) solution that uses
// table fact[] to calculate
// the Permutation Coefficient
import
java .io.*;
public
class
GFG {
// Returns value of Permutation
// Coefficient P(n, k)
static
int
permutationCoeff(
int
n,
int
k)
{
int
[]fact =
n... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program for solution of
// friends pairing problem
import
java.io.*;
class
GFG {
// Returns count of ways n people
// can remain single or paired up.
static
int
countFriendsPairings(
int
n)
{
int
dp[] =
new
int
[n +
1
];
// Filling ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
import
java.util.Arrays;
public
class
Test
{
static
int
arr[] =
new
int
[]{
1
,
2
,
3
,
4
,
5
};
// Method for rotation
static
void
rotate()
{
int
x = arr[arr.length-
1
], i;
for
(i = arr.length-
1
; i >
0
; i--)
arr[i] = arr[i-
1
];
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program for solution of friends
// pairing problem Using Recursion
class
GFG {
static
int
[] dp =
new
int
[
1000
];
// Returns count of ways n people
// can remain single or paired up.
static
int
countFriendsPairings(
int
n)
{
if
(dp[n... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find nth
// element of Newman-Conway Sequence
import
java.io.*;
class
GFG {
// Recursion to find
// n-th element
static
int
sequence(
int
n)
{
if
(n ==
1
|| n ==
2
)
return
1
;
else
return
sequence(sequence(n -
1
))
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// JAVA Code for Newman-Conway Sequence
import
java.util.*;
class
GFG {
// Function to find the n-th element
static
int
sequence(
int
n)
{
// Declare array to store sequence
int
f[] =
new
int
[n +
1
];
f[
0
] =
0
;
f[
1
] =
1
;
f[
2
] =... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A simple Java program to rearrange contents of arr[]
// such that arr[j] becomes j if arr[i] is j
class
RearrangeArray {
// A simple method to rearrange 'arr[0..n-1]' so that 'arr[j]'
// becomes 'i' if 'arr[i]' is 'j'
void
rearrangeNaive(
int
arr[],... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// java program to find the maximum sum
// such that no three are consecutive
import
java.io.*;
class
GFG {
// Returns maximum subsequence sum such that no three
// elements are consecutive
static
int
maxSumWO3Consec(
int
arr[],
int
n)
{
// Sto... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find the maximum
// sum such that no three are
// consecutive using recursion.
import
java.util.Arrays;
class
GFG
{
static
int
arr[] = {
100
,
1000
,
100
,
1000
,
1
};
static
int
sum[] =
new
int
[
10000
];
// Returns maximum ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java implementation to divide N into
// maximum number of segments
// of length a, b and c
import
java.util.*;
class
GFG
{
// function to find the maximum
// number of segments
static
int
maximumSegments(
int
n,
int
a,
int
b,
int
c)
{
/... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A space efficient Java program to rearrange contents of
// arr[] such that arr[j] becomes j if arr[i] is j
class
RearrangeArray {
// A utility function to rearrange elements in the cycle
// starting at arr[i]. This function assumes values in
// arr... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A simple recursive JAVA program to find
// maximum sum by recursively breaking a
// number in 3 parts.
import
java.io.*;
class
GFG {
final
int
MAX =
1000000
;
// Function to find the maximum sum
static
int
breakSum(
int
n)
{
int
dp[] =
ne... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java Code for Maximum sum in a 2 x n grid
// such that no two elements are adjacent
import
java.util.*;
class
GFG {
// Function to find max sum without adjacent
public
static
int
maxSum(
int
grid[][],
int
n)
{
// Sum including maximum element ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java Program to find the length of
// substring with maximum difference of
// zeroes and ones in binary string.
import
java.util.*;
import
java.lang.*;
import
java.io.*;
class
GFG {
// Find the length of substring with maximum
// difference of z... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to write characters in
// minimum time by inserting, removing
// and copying operation
public
class
GFG{
// method returns minimum time to write
// 'N' characters
static
int
minTimeForWritingChars(
int
N,
int
insert,
int
remove,
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to print sum of all substring of
// a number represented as a string
import
java.util.Arrays;
class
GFG{
// Returns sum of all substring of num
public
static
int
sumOfSubstrings(String num)
{
int
n = num.length();
// allocate me... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find the maximum stolen value
import
java.io.*;
class
GFG
{
// Function to calculate the maximum stolen value
static
int
maxLoot(
int
hval[],
int
n)
{
if
(n ==
0
)
return
0
;
if
(n ==
1
)
return
hval[
0
];
if
(n ==
2
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find the maximum stolen value
import
java.io.*;
class
GFG
{
// Function to calculate the maximum stolen value
static
int
maxLoot(
int
hval[],
int
n)
{
if
(n ==
0
)
return
0
;
int
value1 = hval[
0
];
if
(n ==
1
)
retur... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to illustrate
// the number of ways to represent
// N as sum of 1, 3 and 4.
class
GFG {
// Function to count the
// number of ways to represent
// n as sum of 1, 3 and 4
static
int
countWays(
int
n)
{
int
DP[] =
new
int
[n +
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to count ways to build street
// under given constraints
public
class
GFG {
// function to count ways of building
// a street of n rows
static
long
countWays(
int
n) {
long
dp[][] =
new
long
[
2
][n +
1
];
// base case
dp[
0
][
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java implementation to count number
// of ways to tile a floor of size
// n x m using 1 x m tiles
import
java.io.*;
class
GFG {
// function to count the total number of ways
static
int
countWays(
int
n,
int
m)
{
// table to store values
// o... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to count of ways to place 1 x 4 tiles
// on n x 4 grid
import
java.io.*;
class
Grid
{
// Function that count the number of ways to place 1 x 4 tiles
// on n x 4 grid.
static
int
count(
int
n)
{
// Create a table to store results ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program for counting n digit numbers with
// non decreasing digits
import
java.io.*;
class
GFG {
// Function that returns count of non- decreasing numbers
// with n digits
static
int
nonDecNums(
int
n)
{
// a[i][j] = count of all possibl... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find maximum revenue
// by placing billboard on the highway
// with given constarints.
class
GFG
{
static
int
maxRevenue(
int
m,
int
[] x,
int
[] revenue,
int
n,
int
t)
{
// Array to store maximum revenue
// at each miles.... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to rearrange an
// array in minimum maximum form
public
class
Main {
// Prints max at first position, min at second
// position second max at third position, second
// min at fourth position and so on.
public
static
void
rearrange(
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program for calculating LISS
// using dynamic programming
public
class
LisTree
{
/* A binary tree node has data, pointer
to left child and a pointer to right
child */
static
class
node
{
int
data, liss;
node left, right;
public
node... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A dynamic programming based Java program for partition problem
import
java.io.*;
class
Partition {
// Returns true if arr[] can be partitioned in two subsets of
// equal sum, otherwise false
static
boolean
findPartition (
int
arr[],
int
n)
{
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A DP based Java program to find maximum tasks.
class
GFG
{
// Returns the maximum among the 2 numbers
static
int
max(
int
x,
int
y)
{
return
(x > y ? x : y);
}
// Returns maximum amount of task that can be
// done till day n
static
int
maxTask... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to Move All -ve Element At End
// Without changing order Of Array Element
import
java.util.Arrays;
class
GFG {
// Moves all -ve element to end of array in
// same order.
static
void
segregateElements(
int
arr[],
int
n)
{
// Cre... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program for different tree traversals
/* Class containing left and right child of current
node and key value*/
class
Node
{
int
key;
Node left, right;
public
Node(
int
item)
{
key = item;
left = right =
null
;
}
}
class
BinaryTr... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program for finding postorder
// traversal of BST from preorder traversal
import
java.util.* ;
class
Solution
{
static
class
INT
{
int
data;
INT(
int
d)
{
data=d;
}
}
// Function to find postorder traversal from
// preorder traver... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java implementation to replace each node
// in binary tree with the sum of its inorder
// predecessor and successor
import
java.util.*;
class
Solution
{
// node of a binary tree
static
class
Node {
int
data;
Node left, right;
}
//INT class
s... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java code to rearrange an array such
// that even index elements are smaller
// and odd index elements are greater
// than their next.
class
GFG {
static
void
rearrange(
int
arr[],
int
n)
{
int
temp;
for
(
int
i =
0
; i < n -
1
; i++) {
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find inorder successor of a node
class
Solution
{
// A Binary Tree Node
static
class
Node
{
int
data;
Node left, right;
}
// Temporary node for case 2
static
Node temp =
new
Node();
// Utility function to create a new tree n... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program for nth nodes of inorder traversals
import
java.util. *;
class
Solution
{
static
int
count =
0
;
/* A binary tree node has data, pointer to left child
and a pointer to right child */
static
class
Node {
int
data;
Node left;
Node r... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find n-th node of
// Postorder Traversal of Binary Tree
public
class
NthNodePostOrder {
static
int
flag =
0
;
// function to find the N-th node in the postorder
// traversal of a given binary tree
public
static
void
NthPostorder... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java implementation of an O(n) approach of level order
// traversal in spiral form
import
java.util.*;
// A Binary Tree node
class
Node {
int
data;
Node left, right;
public
Node(
int
item)
{
data = item;
left = right =
null
;
}
}
cl... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to do level order
// traversal line by line
import
java.util.LinkedList;
import
java.util.Queue;
public
class
GFG {
static
class
Node {
int
data;
Node left;
Node right;
Node(
int
data) {
this
.data = data;
left =
null
;
r... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A recursive java program to print reverse level order traversal
// using stack and queue
import
java.util.LinkedList;
import
java.util.Queue;
import
java.util.Stack;
/* A binary tree node has data, pointer to left and right children */
class
Node
{ ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program for special level order traversal
import
java.util.LinkedList;
import
java.util.Queue;
/* Class containing left and right child of current
node and key value*/
class
Node
{
int
data;
Node left, right;
public
Node(
int
item)
{
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to update every array element with
// multiplication of previous and next numbers in array
import
java.io.*;
import
java.util.*;
import
java.lang.Math;
class
Multipy
{
static
void
modify(
int
arr[],
int
n)
{
// Nothing to do when ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to reverse alternate levels of perfect binary tree
// A binary tree node
class
Node {
char
data;
Node left, right;
Node(
char
item) {
data = item;
left = right =
null
;
}
}
// class to access index value by reference
class
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to reverse alternate levels of a tree
class
Sol
{
static
class
Node
{
char
key;
Node left, right;
};
static
void
preorder( Node root1, Node root2,
int
lvl)
{
// Base cases
if
(root1 ==
null
|| root2==
null
)
return
;
// ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A java program for iterative postorder traversal using stack
import
java.util.ArrayList;
import
java.util.Stack;
// A binary tree node
class
Node
{
int
data;
Node left, right;
Node(
int
item)
{
data = item;
left = right;
}
}
class
B... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to print boundary traversal of binary tree
/* A binary tree node has data, pointer to left child
and a pointer to right child */
class
Node {
int
data;
Node left, right;
Node(
int
item)
{
data = item;
left = right =
null
;
}... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to create complete Binary Tree from its Linked List
// representation
// importing necessary classes
import
java.util.*;
// A linked list node
class
ListNode
{
int
data;
ListNode next;
ListNode(
int
d)
{
data = d;
next =
nu... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to construct binary tree from
// given array in level order fashion
public
class
Tree {
Node root;
// Tree Node
static
class
Node {
int
data;
Node left, right;
Node(
int
data)
{
this
.data = data;
this
.left =
null
;
th... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find a pair with a given
// sum in a sorted and rotated array
class
PairInSortedRotated
{
// This function returns true if arr[0..n-1]
// has a pair with sum equals to x.
static
boolean
pairInSortedRotated(
int
arr[],
int
n,
int
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to construct a binary tree from preorder traversal
// A Binary Tree node
class
Node
{
int
data;
Node left, right;
Node(
int
item)
{
data = item;
left = right =
null
;
}
}
class
Index
{
int
index =
0
;
}
class
Binar... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to convert BTT to DLL using
// simple inorder traversal
public
class
BinaryTreeToDLL
{
static
class
node
{
int
data;
node left, right;
public
node(
int
data)
{
this
.data = data;
}
}
static
node prev;
// Changes lef... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// A Java program for in-place conversion of Binary Tree to DLL
// A binary tree node has data, left pointers and right pointers
class
Node
{
int
data;
Node left, right;
public
Node(
int
data)
{
this
.data = data;
left = right =
null
;
}
}... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to convert a given Binary Tree to
// Doubly Linked List
/* Structure for tree and Linked List */
class
Node
{
int
data;
Node left, right;
public
Node(
int
data)
{
this
.data = data;
left = right =
null
;
}
}
class
BinaryT... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
/* Java program to convert left-right to
down-right representation of binary tree */
class
GFG
{
// A Binary Tree Node
static
class
node
{
int
key;
node left, right;
node(
int
key)
{
this
.key = key;
this
.left =
null
;
this
.right =
null... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to convert a tree into its sum tree
// A binary tree node
class
Node
{
int
data;
Node left, right;
Node(
int
item)
{
data = item;
left = right =
null
;
}
}
class
BinaryTree
{
Node root;
// Convert a given tree to a ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to store sum of nodes in left subtree in every
// node
class
GfG {
// A tree node
static
class
node
{
int
data;
node left, right;
}
// Function to modify a Binary Tree so that every node
// stores sum of values in its left child i... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to find maximum number to be removed
// to convert a tree into forest containg trees of
// even number of nodes
import
java.util.*;
class
GFG
{
static
int
N =
12
,ans;
static
Vector<Vector<Integer>> tree=
new
Vector<Vector<Integer>>... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to covert a ternary
// expreesion to a tree.
import
java.util.Queue;
import
java.util.LinkedList;
// Class to represent Tree node
class
Node
{
char
data;
Node left, right;
public
Node(
char
item)
{
data = item;
left =
null
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
/* Java program to flip a binary tree */
import
java.util.Queue;
import
java.util.LinkedList;
public
class
FlipTree {
// method to flip the binary tree
public
static
Node flipBinaryTree(Node root)
{
if
(root ==
null
)
return
root;
if
(root.left... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to flip a binary tree
import
java.util.*;
class
GFG
{
// A binary tree node
static
class
Node
{
int
data;
Node left, right;
};
// Utility function to create
// a new Binary Tree Node
static
Node newNode(
int
data)
{
Node tem... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to check children sum property
/* A binary tree node has data, pointer to left child
and a pointer to right child */
class
Node
{
int
data;
Node left, right;
public
Node(
int
d)
{
data = d;
left = right =
null
;
}
}
clas... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to check if Binary tree is sum tree or not
/* A binary tree node has data, left child and right child */
class
Node
{
int
data;
Node left, right, nextRight;
Node(
int
item)
{
data = item;
left = right = nextRight =
null
;
} ... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to check if two binary tree are cousins
class
Node
{
int
data;
Node left, right;
Node(
int
item)
{
data = item;
left = right =
null
;
}
}
class
BinaryTree
{
Node root;
// Recursive function to check if two Nodes are
... |
<|begin_of_text|><|start_header_id|>system<|end_header_id|>
// Java program to check if all leaves are at same level
// A binary tree node
class
Node
{
int
data;
Node left, right;
Node(
int
item)
{
data = item;
left = right =
null
;
}
}
class
Leaf
{
int
leaflevel=
0
;
}
class
BinaryTree
{ ... |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.