content stringlengths 219 31.2k | complexity stringclasses 5
values | file_name stringlengths 6 9 | complexity_ranked float64 0.1 0.9 |
|---|---|---|---|
class
SubarraySum
{
/* Returns true if the there is a subarray of arr[] with a sum equal to
'sum' otherwise returns false. Also, prints the result */
int
subArraySum(
int
arr[],
int
n,
int
sum)
{
int
curr_sum, i, j;
// Pick a starting point
for
(i =
0
; i < n; i++)
{
curr_sum = arr[i];
// try ... | n_square | 133.java | 0.9 |
// Java program to find a triplet
class
FindTriplet {
// returns true if there is triplet with sum equal
// to 'sum' present in A[]. Also, prints the triplet
boolean
find3Numbers(
int
A[],
int
arr_size,
int
sum)
{
int
l, r;
/* Sort the elements */
quickSort(A,
0
, arr_size -
1
);
/* Now fix the ... | n_square | 140.java | 0.9 |
// Java program to find triplets in a given
// array whose sum is zero
import
java.util.*;
class
GFG
{
// function to print triplets with 0 sum
static
void
findTriplets(
int
arr[],
int
n)
{
boolean
found =
false
;
for
(
int
i =
0
; i < n -
1
; i++)
{
// Find all pairs with sum equals to
// "... | n_square | 143.java | 0.9 |
// Java program to print a given matrix in spiral form
import
java.io.*;
class
GFG {
// Function print matrix in spiral form
static
void
spiralPrint(
int
m,
int
n,
int
a[][])
{
int
i, k =
0
, l =
0
;
/* k - starting row index
m - ending row index
l - starting column index
n - ending column index... | n_square | 147.java | 0.9 |
// Java implementation to print
// the counter clock wise
// spiral traversal of matrix
import
java.io.*;
class
GFG
{
static
int
R =
4
;
static
int
C =
4
;
// function to print the
// required traversal
static
void
counterClockspiralPrint(
int
m,
int
n,
int
arr[][])
{
int
i, k =
0
, l =
0
... | n_square | 148.java | 0.9 |
// Java prorgam for finding max path in matrix
import
static
java.lang.Math.max;
class
GFG
{
public
static
int
N =
4
, M =
6
;
// Function to calculate max path in matrix
static
int
findMaxPath(
int
mat[][])
{
// To find max val in first row
int
res = -
1
;
for
(
int
i =
0
; i < M; i++)
res = ... | n_square | 149.java | 0.9 |
// Java program to remove duplicates from unsorted
// linked list
class
LinkedList {
static
Node head;
static
class
Node {
int
data;
Node next;
Node(
int
d) {
data = d;
next =
null
;
}
}
/* Function to remove duplicates from an
unsorted linked list */
void
remove_duplicates() {
Node p... | n_square | 162.java | 0.9 |
// Java Code to find the last man Standing
public
class
GFG {
// Node class to store data
static
class
Node
{
public
int
data ;
public
Node next;
public
Node(
int
data )
{
this
.data = data;
}
}
/* Function to find the only person left
after one in every m-th node is killed
in a circle of... | n_square | 171.java | 0.9 |
// A Naive Java program to find
// maximum sum rotation
import
java.util.*;
import
java.io.*;
class
GFG {
// Returns maximum value of i*arr[i]
static
int
maxSum(
int
arr[],
int
n)
{
// Initialize result
int
res = Integer.MIN_VALUE;
// Consider rotation beginning with i
// for all possible values of i.
... | n_square | 18.java | 0.9 |
// Java implementation for brute force method to calculate stock span values
import
java.util.Arrays;
class
GFG {
// method to calculate stock span values
static
void
calculateSpan(
int
price[],
int
n,
int
S[])
{
// Span value of first day is always 1
S[
0
] =
1
;
// Calculate span value of remaini... | n_square | 186.java | 0.9 |
// Java program to sort an
// array using stack
import
java.io.*;
import
java.util.*;
class
GFG
{
// This function return
// the sorted stack
static
Stack<Integer> sortStack(Stack<Integer> input)
{
Stack<Integer> tmpStack =
new
Stack<Integer>();
while
(!input.empty())
{
// pop out the
// fir... | n_square | 199.java | 0.9 |
// Java program to implement sorting a
// queue data structure
import
java.util.LinkedList;
import
java.util.Queue;
class
GFG
{
// Queue elements after sortIndex are
// already sorted. This function returns
// index of minimum element from front to
// sortIndex
public
static
int
minIndex(Queue<Integer> ... | n_square | 226.java | 0.9 |
// Java program for recursive level order traversal in spiral form
/* 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;
// Functi... | n_square | 227.java | 0.9 |
// Java Program to find the maximum for each and every contiguous subarray of size k.
public
class
GFG {
// Method to find the maximum for each and every contiguous subarray of size k.
static
void
printKMax(
int
arr[],
int
n,
int
k)
{
int
j, max;
for
(
int
i =
0
; i <= n - k; i++) {
max = arr[i];
... | n_square | 229.java | 0.9 |
// Java implementation to find the first negative
// integer in every window of size k
import
java.util.*;
class
solution
{
// function to find the first negative
// integer in every window of size k
static
void
printFirstNegativeInteger(
int
arr[],
int
n,
int
k)
{
// flag to check whether window contain... | n_square | 237.java | 0.9 |
// A simple Java program to find max subarray XOR
class
GFG {
static
int
maxSubarrayXOR(
int
arr[],
int
n)
{
int
ans = Integer.MIN_VALUE;
// Initialize result
// Pick starting points of subarrays
for
(
int
i=
0
; i<n; i++)
{
// to store xor of current subarray
int
curr_xor =
0
;
// Pick ending ... | n_square | 249.java | 0.9 |
// Java program to split array and move first
// part to end.
import
java.util.*;
import
java.lang.*;
class
GFG {
public
static
void
splitArr(
int
arr[],
int
n,
int
k)
{
for
(
int
i =
0
; i < k; i++) {
// Rotate array by 1.
int
x = arr[
0
];
for
(
int
j =
0
; j < n -
1
; ++j)
arr[j] = arr[j + ... | n_square | 25.java | 0.9 |
// Java program to CountKSubStr number of substrings
// with exactly distinct characters in a given string
import
java.util.Arrays;
public
class
CountKSubStr
{
// Function to count number of substrings
// with exactly k unique characters
int
countkDist(String str,
int
k)
{
// Initialize result
int
re... | n_square | 251.java | 0.9 |
// Java program to count number of substrings
// with counts of distinct characters as k.
class
GFG
{
static
int
MAX_CHAR =
26
;
// Returns true if all values
// in freq[] are either 0 or k.
static
boolean
check(
int
freq[],
int
k)
{
for
(
int
i =
0
; i < MAX_CHAR; i++)
if
(freq[i] !=
0
&& freq[i] != ... | n_square | 252.java | 0.9 |
// Java program to count number of substrings
// of a string
import
java.io.*;
public
class
GFG {
static
int
countNonEmptySubstr(String str)
{
int
n = str.length();
return
n * (n +
1
) /
2
;
}
// Driver code
public
static
void
main(String args[])
{
String s =
"abcde"
;
System.out.println( ... | n_square | 254.java | 0.9 |
// Java program to count all substrings with same
// first and last characters.
public
class
GFG {
// Returns true if first and last characters
// of s are same.
static
boolean
checkEquality(String s)
{
return
(s.charAt(
0
) == s.charAt(s.length() -
1
));
}
static
int
countSubstringWithEqualEnds(Str... | n_square | 257.java | 0.9 |
// Space efficient Java program to count all
// substrings with same first and last characters.
public
class
GFG {
static
int
countSubstringWithEqualEnds(String s)
{
int
result =
0
;
int
n = s.length();
// Iterating through all substrings in
// way so that we can find first and last
// character eas... | n_square | 258.java | 0.9 |
// Java program to split array and move first
// part to end.
import
java.util.*;
import
java.lang.*;
class
GFG {
// Function to spilt array and
// move first part to end
public
static
void
SplitAndAdd(
int
[] A,
int
length,
int
rotation){
//make a temporary array with double the size
int
[] tmp =
new... | n_square | 26.java | 0.9 |
// Java program to print all words that have
// the same unique character set
import
java.util.ArrayList;
import
java.util.Arrays;
import
java.util.HashMap;
import
java.util.Map.Entry;
public
class
GFG {
static
final
int
MAX_CHAR =
26
;
// Generates a key from given string. The key
// contains all uniqu... | n_square | 264.java | 0.9 |
// A simple C++ program to count number of
//substrings starting and ending with 1
class
CountSubString
{
int
countSubStr(
char
str[],
int
n)
{
int
res =
0
;
// Initialize result
// Pick a starting point
for
(
int
i =
0
; i<n; i++)
{
if
(str[i] ==
'1'
)
{
// Search for all possible ending po... | n_square | 271.java | 0.9 |
// Java implementation to find the character in
// first string that is present at minimum index
// in second string
public
class
GFG
{
// method to find the minimum index character
static
void
printMinIndexChar(String str, String patt)
{
// to store the index of character having
// minimum index
int
... | n_square | 274.java | 0.9 |
// A Simple Java program to find pairs with distance
// equal to English alphabet distance
class
Test {
// Method to count pairs
static
int
countPairs(String str)
{
int
result =
0
;
int
n = str.length();
for
(
int
i =
0
; i < n; i++)
for
(
int
j = i +
1
; j < n; j++)
// Increment count if charac... | n_square | 278.java | 0.9 |
// Java program to count number of times
// S appears as a subsequence in T
import
java.io.*;
class
GFG {
static
int
findSubsequenceCount(String S, String T)
{
int
m = T.length();
int
n = S.length();
// T can't appear as a subsequence in S
if
(m > n)
return
0
;
// mat[i][j] stores the count of
... | n_square | 285.java | 0.9 |
// Java program to find n'th Bell number
import
java.io.*;
class
GFG
{
// Function to find n'th Bell Number
static
int
bellNumber(
int
n)
{
int
[][] bell =
new
int
[n+
1
][n+
1
];
bell[
0
][
0
] =
1
;
for
(
int
i=
1
; i<=n; i++)
{
// Explicitly fill for j = 0
bell[i][
0
] = bell[i-
1
][i-
1
];... | n_square | 288.java | 0.9 |
class
GFG{
// A dynamic programming based function to find nth
// Catalan number
static
int
catalanDP(
int
n) {
// Table to store results of subproblems
int
catalan[] =
new
int
[n +
2
];
// Initialize first two values in table
catalan[
0
] =
1
;
catalan[
1
] =
1
;
// Fill entries in catalan[] us... | n_square | 290.java | 0.9 |
// A Dynamic Programming based solution that uses table C[][] to
// calculate the Binomial Coefficient
class
BinomialCoefficient
{
// Returns value of Binomial Coefficient C(n, k)
static
int
binomialCoeff(
int
n,
int
k)
{
int
C[][] =
new
int
[n+
1
][k+
1
];
int
i, j;
// Calculate value of Binomial C... | n_square | 293.java | 0.9 |
// Java code for Dynamic Programming based
// solution that uses table P[][] to
// calculate the Permutation Coefficient
import
java.io.*;
import
java.math.*;
class
GFG
{
// Returns value of Permutation
// Coefficient P(n, k)
static
int
permutationCoeff(
int
n,
int
k)
{
int
P[][] =
new
int
[n +
2... | n_square | 295.java | 0.9 |
// Java program to solve Gold Mine problem
import
java.util.Arrays;
class
GFG {
static
final
int
MAX =
100
;
// Returns maximum amount of gold that
// can be collected when journey started
// from first column and moves allowed
// are right, right-up and right-down
static
int
getMaxGold(
int
gold[][]... | n_square | 298.java | 0.9 |
// Java program to Rearrange positive
// and negative numbers in a array
import
java.io.*;
class
GFG {
/* Function to print an array */
static
void
printArray(
int
A[],
int
size)
{
for
(
int
i =
0
; i < size; i++)
System.out.print(A[i] +
" "
);
System.out.println();
}
// Merges two subarrays of ... | n_square | 30.java | 0.9 |
// A Dynamic Programming solution for subset
// sum problem
class
GFG {
// Returns true if there is a subset of
// set[] with sun equal to given sum
static
boolean
isSubsetSum(
int
set[],
int
n,
int
sum)
{
// The value of subset[i][j] will be
// true if there is a subset of
// set[0..j-1] with sum ... | n_square | 303.java | 0.9 |
// Java program to check if there is a subset
// with sum divisible by m.
import
java.util.Arrays;
class
GFG {
// Returns true if there is a subset
// of arr[] with sum divisible by m
static
boolean
modularSum(
int
arr[],
int
n,
int
m)
{
if
(n > m)
return
true
;
// This array will keep track of ... | n_square | 304.java | 0.9 |
import
java.util.Arrays;
// Java program to find the largest
// subset which where each pair
// is divisible.
class
GFG {
// function to find the longest Subsequence
static
int
largestSubset(
int
[] a,
int
n)
{
// Sort array in increasing order
Arrays.sort(a);
// dp[i] is going to store size of lar... | n_square | 305.java | 0.9 |
// A Dynamic Programming solution for Rod cutting problem
class
RodCutting
{
/* Returns the best obtainable price for a rod of
length n and price[] as prices of different pieces */
static
int
cutRod(
int
price[],
int
n)
{
int
val[] =
new
int
[n+
1
];
val[
0
] =
0
;
// Build the table val[] in bottom ... | n_square | 307.java | 0.9 |
// A memoization based Java program to
// count even length binary sequences
// such that the sum of first and
// second half bits is same
import
java.io.*;
class
GFG {
// A lookup table to store the results of
// subproblems
static
int
lookup[][] =
new
int
[
1000
][
1000
];
// dif is diference between su... | n_square | 311.java | 0.9 |
// Efficient java program to count total number
// of special sequences of length n where
class
Sequences
{
// DP based function to find the number of special
// sequences
static
int
getTotalNumberOfSequences(
int
m,
int
n)
{
// define T and build in bottom manner to store
// number of special sequence... | n_square | 314.java | 0.9 |
// java program to find maximum
// sum of bi-tonic sub-sequence
import
java.io.*;
class
GFG {
// Function return maximum sum
// of Bi-tonic sub-sequence
static
int
MaxSumBS(
int
arr[],
int
n)
{
int
max_sum = Integer.MIN_VALUE;
// MSIBS[i] ==> Maximum sum Increasing Bi-tonic
// subsequence ending w... | n_square | 317.java | 0.9 |
/* Dynamic Programming Java
implementation of Maximum Sum
Increasing Subsequence (MSIS)
problem */
class
GFG
{
/* maxSumIS() returns the
maximum sum of increasing
subsequence in arr[] of size n */
static
int
maxSumIS(
int
arr[],
int
n)
{
int
i, j, max =
0
;
int
msis[] =
new
int
[n];
/* Initia... | n_square | 318.java | 0.9 |
/* Dynamic programming Java implementation
of maximum product of an increasing
subsequence */
import
java.util.Arrays;
import
java.util.Collections;
class
GFG {
// Returns product of maximum product
// increasing subsequence.
static
int
lis(
int
[] arr,
int
n)
{
int
[] mpis =
new
int
[n];
int
max =... | n_square | 319.java | 0.9 |
// Java program to find the longest subsequence
// such that the difference between adjacent
// elements of the subsequence is one.
import
java.io.*;
class
GFG {
// Function to find the length of longest
// subsequence
static
int
longestSubseqWithDiffOne(
int
arr[],
int
n)
{
// Initialize the dp[] ar... | n_square | 322.java | 0.9 |
// JAVA Code for Maximum length subsequence
// with difference between adjacent elements
// as either 0 or 1
import
java.util.*;
class
GFG {
// function to find maximum length subsequence
// with difference between adjacent elements as
// either 0 or 1
public
static
int
maxLenSub(
int
arr[],
int
n)
{ ... | n_square | 323.java | 0.9 |
// Java program to find maximum sum increasing
// subsequence tiint i-th index and including
// k-th index.
class
GFG {
static
int
pre_compute(
int
a[],
int
n,
int
index,
int
k)
{
int
dp[][] =
new
int
[n][n];
// Initializing the first row of
// the dp[][].
for
(
int
i =
0
; i < n; i++) {
if
(a... | n_square | 324.java | 0.9 |
class
Pair{
int
a;
int
b;
public
Pair(
int
a,
int
b) {
this
.a = a;
this
.b = b;
}
// This function assumes that arr[] is sorted in increasing order
// according the first (or smaller) values in pairs.
static
int
maxChainLength(Pair arr[],
int
n)
{
int
i, j, max =
0
;
int
mcl[] =
new
int
... | n_square | 325.java | 0.9 |
// JAVA Code for Maximum size square
// sub-matrix with all 1s
public
class
GFG
{
// method for Maximum size square sub-matrix with all 1s
static
void
printMaxSubSquare(
int
M[][])
{
int
i,j;
int
R = M.length;
//no of rows in M[][]
int
C = M[
0
].length;
//no of columns in M[][]
int
S[][] =
new
int... | n_square | 328.java | 0.9 |
// Java Code for Maximum weight path ending at
// any element of last row in a matrix
import
java.util.*;
class
GFG {
/* Function which return the maximum weight
path sum */
public
static
int
maxCost(
int
mat[][],
int
N)
{
// create 2D matrix to store the sum of
// the path
int
dp[][]=
new
int
[N][N... | n_square | 332.java | 0.9 |
// Java program to find Maximum path sum
// start any column in row '0' and ends
// up to any column in row 'n-1'
import
java.util.*;
class
GFG {
static
int
N =
4
;
// function find maximum sum path
static
int
MaximumPath(
int
Mat[][])
{
int
result =
0
;
// creat 2D matrix to store the sum
// o... | n_square | 335.java | 0.9 |
/* Java program for Dynamic Programming implementation
of Min Cost Path problem */
import
java.util.*;
class
MinimumCostPath
{
/* A utility function that returns minimum of 3 integers */
private
static
int
min(
int
x,
int
y,
int
z)
{
if
(x < y)
return
(x < z)? x : z;
else
return
(y < z)? y : z;
} ... | n_square | 337.java | 0.9 |
// JAVA Code for Minimum number of jumps to reach end
class
GFG{
private
static
int
minJumps(
int
[] arr,
int
n) {
int
jumps[] =
new
int
[n];
// jumps[n-1] will hold the
// result
int
i, j;
if
(n ==
0
|| arr[
0
] ==
0
)
return
Integer.MAX_VALUE;
// if first element is 0,
// end cannot be reached ... | n_square | 339.java | 0.9 |
// Java program to find minimum removals
// to make max-min <= K
import
java.util.Arrays;
class
GFG
{
static
int
MAX=
100
;
static
int
dp[][]=
new
int
[MAX][MAX];
// function to check all possible combinations
// of removal and return the minimum one
static
int
countRemovals(
int
a[],
int
i,
int
j,
... | n_square | 342.java | 0.9 |
// A Dynamic Programming based Java program to find minimum
// number operations to convert str1 to str2
class
EDIST
{
static
int
min(
int
x,
int
y,
int
z)
{
if
(x <= y && x <= z)
return
x;
if
(y <= x && y <= z)
return
y;
else
return
z;
}
static
int
editDistDP(String str1, String str2,
int
m,
int... | n_square | 344.java | 0.9 |
// Java implementation of finding length of longest
// Common substring using Dynamic Programming
public
class
LongestCommonSubSequence
{
/*
Returns length of longest common substring
of X[0..m-1] and Y[0..n-1]
*/
static
int
LCSubStr(
char
X[],
char
Y[],
int
m,
int
n)
{
// Create a table to store le... | n_square | 346.java | 0.9 |
// Space optimized CPP implementation of
// longest common substring.
import
java.io.*;
import
java.util.*;
public
class
GFG {
// Function to find longest
// common substring.
static
int
LCSubStr(String X, String Y)
{
// Find length of both the strings.
int
m = X.length();
int
n = Y.length();
/... | n_square | 348.java | 0.9 |
// Program to find minimum
// total offerings required
import
java.io.*;
class
GFG
{
// Returns minimum
// offerings required
static
int
offeringNumber(
int
n,
int
templeHeight[])
{
int
sum =
0
;
// Initialize result
// Go through all
// temples one by one
for
(
int
i =
0
; i < n; ++i)
{
//... | n_square | 357.java | 0.9 |
// Java program to print
// equal sum sets of array.
import
java.io.*;
import
java.util.*;
class
GFG
{
// Function to print equal
// sum sets of array.
static
void
printEqualSumSets(
int
[]arr,
int
n)
{
int
i, currSum, sum =
0
;
// Finding sum of array elements
for
(i =
0
; i < arr.length; i++... | n_square | 363.java | 0.9 |
/* Dynamic Programming implementation in Java for longest bitonic
subsequence problem */
import
java.util.*;
import
java.lang.*;
import
java.io.*;
class
LBS
{
/* lbs() returns the length of the Longest Bitonic Subsequence in
arr[] of size n. The function mainly creates two temporary arrays
lis[] and lds[]... | n_square | 366.java | 0.9 |
// A Space optimized Dynamic Programming
// based Java program for LPS problem
class
GFG {
// Returns the length of the longest
// palindromic subsequence in str
static
int
lps(String s)
{
int
n = s.length();
// a[i] is going to store length
// of longest palindromic subsequence
// of substring s[... | n_square | 367.java | 0.9 |
// Java code to Count Palindromic Subsequence
// in a given String
public
class
GFG
{
// Function return the total palindromic
// subsequence
static
int
countPS(String str)
{
int
N = str.length();
// create a 2D array to store the count
// of palindromic subsequence
int
[][] cps =
new
int
[N+
1
][... | n_square | 368.java | 0.9 |
// Java Solution
public
class
LongestPalinSubstring
{
// A utility function to print a substring str[low..high]
static
void
printSubStr(String str,
int
low,
int
high) {
System.out.println(str.substring(low, high +
1
));
}
// This function prints the longest palindrome substring
// of str[].
// It a... | n_square | 369.java | 0.9 |
// Java program to find palindromic substrings of a string
public
class
GFG
{
// Returns total number of palindrome substring of
// length greater then equal to 2
static
int
CountPS(
char
str[],
int
n)
{
// create empty 2-D matrix that counts all palindrome
// substring. dp[i][j] stores counts of palin... | n_square | 370.java | 0.9 |
// Java program to query number of palindromic
// substrings of a string in a range
import
java.io.*;
class
GFG {
// Function to construct the dp array
static
void
constructDp(
int
dp[][], String str)
{
int
l = str.length();
// declare 2D array isPalin, isPalin[i][j] will
// be 1 if str(i..j) is palin... | n_square | 371.java | 0.9 |
// Java program to find sum of maximum
// sum alternating sequence starting with
// first element.
public
class
GFG
{
// Return sum of maximum sum alternating
// sequence starting with arr[0] and is first
// decreasing.
static
int
maxAlternateSum(
int
arr[],
int
n)
{
if
(n ==
1
)
return
arr[
0
]; ... | n_square | 372.java | 0.9 |
// Java program to find longest
// alternating subsequence in an array
import
java.io.*;
class
GFG {
// Function to return longest
// alternating subsequence length
static
int
zzis(
int
arr[],
int
n)
{
/*las[i][0] = Length of the longest
alternating subsequence ending at
index i and last element is
g... | n_square | 373.java | 0.9 |
// Java program to print postorder
// traversal from preorder and
// inorder traversals
import
java.util.Arrays;
class
GFG
{
// A utility function to search x in arr[] of size n
static
int
search(
int
arr[],
int
x,
int
n)
{
for
(
int
i =
0
; i < n; i++)
if
(arr[i] == x)
return
i;
return
-
1
;
}
... | n_square | 375.java | 0.9 |
// Java program to print Postorder traversal from given Inorder
// and Preorder traversals.
public
class
PrintPost {
static
int
preIndex =
0
;
void
printPost(
int
[] in,
int
[] pre,
int
inStrt,
int
inEnd)
{
if
(inStrt > inEnd)
return
;
// Find index of next item in preorder traversal in
// inorde... | n_square | 376.java | 0.9 |
// Java program for recursive level order traversal in spiral form
/* 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;
// Functi... | n_square | 384.java | 0.9 |
// A recursive java program to print reverse level order traversal
// A binary tree node
class
Node
{
int
data;
Node left, right;
Node(
int
item)
{
data = item;
left = right;
}
}
class
BinaryTree
{
Node root;
/* Function to print REVERSE level order traversal a tree*/
void
reverseLevelOrde... | n_square | 387.java | 0.9 |
// Java program to construct a tree using inorder and preorder traversal
/* A binary tree node has data, pointer to left child
and a pointer to right child */
class
Node {
char
data;
Node left, right;
Node(
char
item)
{
data = item;
left = right =
null
;
}
}
class
BinaryTree {
Node root;
sta... | n_square | 396.java | 0.9 |
// Java Program to construct ancestor matrix for a given tree
import
java.util.*;
class
GFG
{
// ancestorMatrix function to populate the matrix of
public
static
void
ancestorMatrix(Node root ,
int
matrix[][],
int
size)
{
// base case:
if
(root==
null
)
return
;
// call recursively for a preorder ... | n_square | 401.java | 0.9 |
// Java program to construct tree from inorder traversal
/* 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;
/* Recursive funct... | n_square | 402.java | 0.9 |
// Java program to construct a tree using inorder
// and postorder traversals
/* 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
data)
{
this
.data = data;
left = right =
null
;
}
}
// Class Index creat... | n_square | 403.java | 0.9 |
// Java program to convert an arbitrary binary tree to a tree that holds
// children sum property
// A binary tree node
class
Node
{
int
data;
Node left, right;
Node(
int
item)
{
data = item;
left = right =
null
;
}
}
class
BinaryTree
{
Node root;
/* This function changes a tree to hold c... | n_square | 407.java | 0.9 |
// 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;
/* A utility functi... | n_square | 416.java | 0.9 |
// Java program to check if there exist an edge whose
// removal creates two trees of same size
class
Node
{
int
key;
Node left, right;
public
Node(
int
key)
{
this
.key = key;
left = right =
null
;
}
}
class
BinaryTree
{
Node root;
// To calculate size of tree with given root
int
count(... | n_square | 421.java | 0.9 |
/* Java program to check if all three given
traversals are of the same tree */
import
java.util.*;
class
GfG {
static
int
preIndex =
0
;
// A Binary Tree Node
static
class
Node
{
int
data;
Node left, right;
}
// Utility function to create a new tree node
static
Node newNode(
int
data)
{
Node temp =... | n_square | 423.java | 0.9 |
// A Java program for Prim's Minimum Spanning Tree (MST) algorithm.
// The program is for adjacency matrix representation of the graph
import
java.util.*;
import
java.lang.*;
import
java.io.*;
class
MST {
// Number of vertices in the graph
private
static
final
int
V =
5
;
// A utility function to find t... | n_square | 465.java | 0.9 |
// A Java program for Dijkstra's single source shortest path algorithm.
// The program is for adjacency matrix representation of the graph
import
java.util.*;
import
java.lang.*;
import
java.io.*;
class
ShortestPath
{
// A utility function to find the vertex with minimum distance value,
// from the set of v... | n_square | 467.java | 0.9 |
// Java program to to maximize array
// sum after k operations.
class
GFG
{
// This function does k operations
// on array in a way that maximize
// the array sum. index --> stores
// the index of current minimum
// element for j'th operation
static
int
maximumSum(
int
arr[],
int
n,
int
k)
{
// M... | n_square | 471.java | 0.9 |
// Java program to find lexicographically minimum
// value after k swaps.
class
GFG {
// Modifies arr[0..n-1] to lexicographically
// smallest with k swaps.
static
void
minimizeWithKSwaps(
int
arr[],
int
n,
int
k)
{
for
(
int
i =
0
; i < n-
1
&& k >
0
; ++i)
{
// Set the position where we want
... | n_square | 488.java | 0.9 |
// Java program to find
// lexicographically
// maximum value after
// k swaps.
import
java.io.*;
class
GFG
{
static
void
SwapInts(
int
array[],
int
position1,
int
position2)
{
// Swaps elements
// in an array.
// Copy the first
// position's element
int
temp = array[position1];
// Assign ... | n_square | 489.java | 0.9 |
// A Java program to implement greedy algorithm for graph coloring
import
java.io.*;
import
java.util.*;
import
java.util.LinkedList;
// This class represents an undirected graph using adjacency list
class
Graph
{
private
int
V;
// No. of vertices
private
LinkedList<Integer> adj[];
//Adjacency List
//C... | n_square | 490.java | 0.9 |
// Java program to fin maximum cash
// flow among a set of persons
class
GFG
{
// Number of persons (or vertices in the graph)
static
final
int
N =
3
;
// A utility function that returns
// index of minimum value in arr[]
static
int
getMin(
int
arr[])
{
int
minInd =
0
;
for
(
int
i =
1
; i < N;... | n_square | 497.java | 0.9 |
import
java.util.*;
import
java.lang.*;
class
Main
{
static
void
minAbsSumPair(
int
arr[],
int
arr_size)
{
int
inv_count =
0
;
int
l, r, min_sum, sum, min_l, min_r;
/* Array should have at least two elements*/
if
(arr_size <
2
)
{
System.out.println(
"Invalid Input"
);
return
;
}
/* Initia... | n_square | 522.java | 0.9 |
// Java implementation of simple
// algorithm to find smaller
// element on left side
import
java.io.*;
class
GFG {
// Prints smaller elements on
// left side of every element
static
void
printPrevSmaller(
int
[]arr,
int
n)
{
// Always print empty or '_'
// for first element
System.out.print(
"_, "
)... | n_square | 550.java | 0.9 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int[] arr = new int [n];
int maxindex=0;
int minindex=0;
int max;
int min;
for(int i=0;i<arr.length;i++) {
arr[i]=sc.ne... | n_square | 556.java | 0.9 |
import java.io.*;
import java.util.*;
public class hi {
public static void main(String[] args) throws IOException{
Reader in=new Reader();
PrintWriter w = new PrintWriter(System.out);
int n=in.nextInt();
int[] arr=in.nextIntArray(n);
int k=Integer.MAX_VALUE;
for (int i = 0; i < n; i++) {
for... | n_square | 560.java | 0.9 |
import java.util.*;
import java.io.*;
public class PartySweet {
static BufferedReader br;
static StringTokenizer tokenizer;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt(), m = nextInt();
int[] b = new int[n];
int[] g = ... | n_square | 565.java | 0.9 |
import java.io.*;
import java.util.*;
import static java.lang.System.*;
public class Main{
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(in));
StringTokenizer st = new StringTokenizer(br.readLine().trim());
int n = Integer.valueOf(s... | n_square | 576.java | 0.9 |
import java.util.Scanner;
public class Codeforces {
public static Scanner input = new Scanner(System.in);
public static void main(String[] args){
int n,k;
n=input.nextInt();
k=input.nextInt();
String s=input.next();
int[] wtArray=new int[n];
for(int i=0;i<s.length... | n_square | 581.java | 0.9 |
//Atcoder
import java.io.*;
import java.util.*;
public class Main {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(b... | n_square | 583.java | 0.9 |
//
// _oo8oo_
// o8888888o
// 88" . "88
// (| -_- |)
// 0\ = /0
// ___/'==='\___
// .' \\| |// ... | n_square | 585.java | 0.9 |
import java.io.*;
import java.util.*;
public class Solution{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
... | n_square | 587.java | 0.9 |
import java.util.Arrays;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Solution ss = new Solution();
ss.test(sc);
}
void test(Scanner sc){
int LEN = sc.nextInt();
int[] a = new int[LEN];
int[] b = new int[LEN];
for (... | n_square | 592.java | 0.9 |
import java.util.*;
import java.lang.*;
import java.io.*;
public class TestClass {
// function for finding size of set
public static int set_size(int[] a, int N){
HashSet <Integer> newset = new HashSet <Integer>();
int i=0;
while(i<N){
newset.add(a[i++]);
}
in... | n_square | 595.java | 0.9 |
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class Cr500 {
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
int n, x, status = -1;
Set<Integer> a = new HashSet<>(), bitA = new HashSet<>();
... | n_square | 596.java | 0.9 |
import java.util.*;
public class B {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int x = scanner.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
... | n_square | 598.java | 0.9 |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int m = in.nextInt();
int[] v = new int[n];
int[] ans = new int[n];
long s = 0;
int t;
for(int i=0; i<n;... | n_square | 608.java | 0.9 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.