content stringlengths 219 31.2k | complexity stringclasses 5
values | file_name stringlengths 6 9 | complexity_ranked float64 0.1 0.9 |
|---|---|---|---|
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(String[] args) throws IOException {
int n = in.nextInt();
... | n | 1003.java | 0.5 |
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++)
{
int k=sc.nextInt();
array[i]=k;
if(array[i]>=0)
... | n | 1004.java | 0.5 |
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 i=0;i<n;i++)
{
arr[i]=sc.nextInt();
... | n | 1005.java | 0.5 |
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++){
a[i]=sc.nextInt();
if (a[i]>=0){
... | n | 1006.java | 0.5 |
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 ArrayDeque<>();
for (int i = 0; i < n; i++) {
deq.addLas... | n | 1007.java | 0.5 |
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 IOException {
int n = in.nextInt();
int q = in.nextInt()... | n | 1008.java | 0.5 |
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];
int b[]=new int[n-1];
int c[]=new int[n-1... | n | 1009.java | 0.5 |
// 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 =
0
; s < n-
1
; s++)
{
if
(arr[s] > arr[s+
1
])
break
... | n | 101.java | 0.5 |
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 bw;
public FastWriter() {
this.bw = new... | n | 1010.java | 0.5 |
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=null;
end=null;
size=0;
}
void add(int val){
if(... | n | 1011.java | 0.5 |
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];
Deque<Integer> deque = new ArrayDeque<>();
for(int i ... | n | 1012.java | 0.5 |
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;
StringTokenizer as;
int nums[],nums2[];
int[] nums1[];
boolean co... | n | 1021.java | 0.5 |
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();
int[] arr = new int[n];
for(int i = 0; ... | n | 1022.java | 0.5 |
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.readLine());
StringTokenizer st;
for(int z=... | n | 1023.java | 0.5 |
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;
StringTokenizer as;
int nums[],nums2[];
int[] nums1[];
boolean co... | n | 1025.java | 0.5 |
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]>a[i]&&a[i]<a[i+1]) {
System.out.println("NO");
retu... | n | 1026.java | 0.5 |
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.readLine());
StringTokenizer st = new StringTokeniz... | n | 1027.java | 0.5 |
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 = input.nextInt();
int[] arr = new int[n];
int ma... | n | 1028.java | 0.5 |
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());
String st[]=br.readLine().split(" ");
int a[]=new in... | n | 1029.java | 0.5 |
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<n; i++)
arr[i] = pair[i-
1
]-arr[
0
];
}
// Driver p... | n | 106.java | 0.5 |
// 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
arr2[],
int
n1,
int
n2)
{
// Initialize max of
... | n | 109.java | 0.5 |
// 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 characters
// we assume all characters are seen before.
Boolean[... | n | 268.java | 0.5 |
// 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
findAndPrintUncommonChars(String str1,
String str2)
{
// mark presence of eac... | n | 269.java | 0.5 |
// 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
;
return
false
;
}
// create alternate vowel and consonant... | n | 270.java | 0.5 |
// 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
(
int
i =
0
; i < n; i++)
{
if
(str[i] ==
'1'
)
m++;
... | n | 272.java | 0.5 |
// 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)
{
return
(ch -
'a'
);
}
// Returns true if all non-zero... | n | 273.java | 0.5 |
// 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, String patt)
{
// map to store the first index of each... | n | 275.java | 0.5 |
// 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 */
static
int
[] getCharCountArray(String str)
{
int
count[] ... | n | 276.java | 0.5 |
// 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
.first = first;
this
.second = second;
}
}
/* Function ... | n | 277.java | 0.5 |
// 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 str)
{
int
result =
0
;
int
n = str.length();
for... | n | 279.java | 0.5 |
// 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[start];
arr[start] = arr[end];
arr[end] = temp;
rvere... | n | 28.java | 0.5 |
// 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 characters
int
freq[] =
new
int
[MAX_CHARS];
for
(
int
i... | n | 280.java | 0.5 |
// 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
// caused by different combination of 'a'
int
aCount =
... | n | 281.java | 0.5 |
// 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 =
0
; i < n; i++) {
if
(s.charAt(i) ==
'a'
)
c1++;
// C... | n | 282.java | 0.5 |
// 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
, result =
0
, C=
0
;
// Traversing the given string
... | n | 283.java | 0.5 |
// 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.
static
int
countSub(String str)
{
// Create an array... | n | 284.java | 0.5 |
// 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_multiple_of_2 =
2
;
int
next_multiple_of_3 =
3
;
int... | n | 287.java | 0.5 |
// 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 =
new
int
[n+
1
];
// base case
fact[
0
] =
1
;
// Ca... | n | 296.java | 0.5 |
// 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 dp[] in bottom-up manner using
// recursive formula expla... | n | 299.java | 0.5 |
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
];
arr[
0
] = x;
}
/* Driver program */
public
static
vo... | n | 3.java | 0.5 |
// 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] != -
1
)
return
dp[n];
if
(n >
2
)
return
dp[n] =... | n | 300.java | 0.5 |
// 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
))
+ sequence(n - sequence(n -
1
));
}
// Driver Program... | n | 308.java | 0.5 |
// 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
] =
1
;
int
i;
for
(i =
3
; i <= n; i++)
f[i] = f[f[... | n | 309.java | 0.5 |
// 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[],
int
n)
{
// Create an auxiliary array of same size
... | n | 32.java | 0.5 |
// 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)
{
// Stores result for subarray arr[0..i], i.e.,
// maximum possi... | n | 320.java | 0.5 |
// 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 subsequence
// sum such that no three
// elements are con... | n | 321.java | 0.5 |
// 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)
{
// stores the maximum number of
// segments each index can... | n | 329.java | 0.5 |
// 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[] be from 1 to n. It changes arr[j-1] to i+1
// if arr[i... | n | 33.java | 0.5 |
// 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[] =
new
int
[n+
1
];
// base conditions
dp[
0
] =
0
; dp[
1... | n | 331.java | 0.5 |
// 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 of first
// column
int
incl = Math.max(grid[
0
][
0
], ... | n | 333.java | 0.5 |
// 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 zeros and ones in binary
// string
public
static
int
fin... | n | 334.java | 0.5 |
// 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,
int
copy)
{
if
(N ==
0
)
return
0
;
if
(N ==
1
) ... | n | 345.java | 0.5 |
// 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 memory equal to length of string
int
sumofdigit[] =
new
in... | n | 349.java | 0.5 |
// 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
)
return
Math.max(hval[
0
], hval[
1
]);
// dp[i] repr... | n | 350.java | 0.5 |
// 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
)
return
value1;
int
value2 = Math.max(hval[
0
], hval[
1
]);
... | n | 351.java | 0.5 |
// 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 +
1
];
// base cases
DP[
0
] = DP[
1
] = DP[
2
] =
1
; ... | n | 352.java | 0.5 |
// 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
][
1
] =
1
;
dp[
1
][
1
] =
2
;
for
(
int
i =
2
; i <=... | n | 353.java | 0.5 |
// 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
// of subproblems
int
count[] =
new
int
[n +
1
];
count[
... | n | 354.java | 0.5 |
// 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 of sub-problems
// dp[i] stores count of ways for i x 4 g... | n | 355.java | 0.5 |
// 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 possible number
// with i digits having leading digit as j
int... | n | 356.java | 0.5 |
// 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.
int
[] maxRev =
new
int
[m +
1
];
for
(
int
i =
0
;... | n | 358.java | 0.5 |
// 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(
int
arr[],
int
n)
{
// initialize index of first minim... | n | 36.java | 0.5 |
// 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(
int
data)
{
this
.data = data;
this
.liss =
0
;
... | n | 360.java | 0.5 |
// 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)
{
int
sum =
0
;
int
i, j;
// Caculcate sun of all eleme... | n | 362.java | 0.5 |
// 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
maxTasks(
int
[]high,
int
[]low,
int
n)
{
// An array task_dp... | n | 365.java | 0.5 |
// 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)
{
// Create an empty array to store result
int
temp[] =
new
int
... | n | 37.java | 0.5 |
// 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
BinaryTree
{
// Root of Binary Tree
Node root;
BinaryTree()... | n | 374.java | 0.5 |
// 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 traversal.
static
void
findPostOrderUtil(
int
pre[],
int
n,
in... | n | 377.java | 0.5 |
// 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
static
class
INT
{
int
data;
}
// function to get a ne... | n | 378.java | 0.5 |
// 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++) {
if
(i %
2
==
0
&& arr[i] > arr[i +
1
]) {
temp = arr[i... | n | 38.java | 0.5 |
// 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 node
static
Node newNode(
int
data)
{
Node temp =
new
... | n | 381.java | 0.5 |
// 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 right;
}
/* Helper function that allocates a new node wit... | n | 382.java | 0.5 |
// 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
NthPostordernode(Node root,
int
N)
{
if
(root ==
null
)
return... | n | 383.java | 0.5 |
// 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
;
}
}
class
BinaryTree {
static
Node root;
void
printSpiral(N... | n | 385.java | 0.5 |
// 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
;
right =
null
;
}
}
// Prints level order traversal l... | n | 386.java | 0.5 |
// 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
{
int
data;
Node left, right;
Node(
int
item)
{
da... | n | 388.java | 0.5 |
// 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)
{
data = item;
left = right =
null
;
}
}
class
Binar... | n | 389.java | 0.5 |
// 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 array size is 1
if
(n <=
1
)
return
;
// store curr... | n | 39.java | 0.5 |
// 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
Index {
int
index;
}
class
BinaryTree {
Node root;... | n | 390.java | 0.5 |
// 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
;
// Swap subtrees if level is even
if
(lvl %
2
==
0
)
{
... | n | 391.java | 0.5 |
// 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
BinaryTree
{
Node root;
ArrayList<Integer> list =
new
... | n | 393.java | 0.5 |
// 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
;
}
}
class
BinaryTree {
Node root;
// A simple functi... | n | 394.java | 0.5 |
// 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 =
null
;
}
}
// A binary tree node
class
BinaryTreeNode
... | n | 398.java | 0.5 |
// 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
;
this
.right =
null
;
}
}
// Function to insert nodes ... | n | 399.java | 0.5 |
// 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
x)
{
// Find the pivot element
int
i;
for
(i =
0
;... | n | 4.java | 0.5 |
// 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
BinaryTree
{
Node root;
Index myindex =
new
Index();
/*... | n | 400.java | 0.5 |
// 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 left pointers to work as previous
// pointers in converted D... | n | 404.java | 0.5 |
// 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
;
}
}
class
BinaryTree
{
Node root;
// head --> Pointer ... | n | 405.java | 0.5 |
// 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
BinaryTree
{
// 'root' - root of binary tree
Node root;
//... | n | 406.java | 0.5 |
/* 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
;
}
}
// An Iterative level order traversal
// based... | n | 408.java | 0.5 |
// 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 tree where every node contains sum of
// values of nodes ... | n | 409.java | 0.5 |
// 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 including its
// own value
static
int
updatetree(node root... | n | 410.java | 0.5 |
// 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>>();
// Return the number of nodes of subtree having
//... | n | 411.java | 0.5 |
// 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
;
right =
null
;
}
}
// Class to covert a ternary e... | n | 412.java | 0.5 |
/* 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 ==
null
&& root.right ==
null
)
return
root;
// recu... | n | 413.java | 0.5 |
// 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 temp =
new
Node();
temp.data = data;
temp.left = temp.rig... | n | 414.java | 0.5 |
// 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
;
}
}
class
BinaryTree
{
Node root;
/* returns 1 if children su... | n | 415.java | 0.5 |
// 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
;
}
}
class
BinaryTree
{
Node root;
/* Utility functio... | n | 417.java | 0.5 |
// 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
// siblings
boolean
isSibling(Node node, Node a, Node b)... | n | 418.java | 0.5 |
// 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
{
Node root;
Leaf mylevel =
new
Leaf();
/* Recursive ... | n | 419.java | 0.5 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.