codeparrot/xlcost-text-to-code huggingface dataset:
Check if a Binary Tree contains node values in strictly increasing and decreasing order at even and odd levels | Java Program for the above approach ; Structure of Tree node ; Function to create new Tree node ; Function to check if given binary tree satisfies the required conditions ; Queue to store nodes of each level ; Stores the current level of the binary tree ; Traverse until the queue is empty ; Stores the number of nodes present in the current level ; Insert left and right child of node into the queue ; If the level is even ; If the nodes in this level are in strictly increasing order or not ; If the level is odd ; If the nodes in this level are in strictly decreasing order or not ; Increment the level count ; Driver Code ; Construct a Binary Tree ; Function Call
import java . util . * ; class GFG { static class Node { int val ; Node left , right ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . val = data ; temp . left = null ; temp . right = null ; return temp ; } public static boolean checkEvenOddLevel ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int level = 0 ; while ( ! q . isEmpty ( ) ) { ArrayList < Integer > list = new ArrayList < > ( ) ; int size = q . size ( ) ; for ( int i = 0 ; i < size ; i ++ ) { Node node = q . poll ( ) ; list . add ( node . val ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } if ( level % 2 == 0 ) { for ( int i = 0 ; i < list . size ( ) - 1 ; i ++ ) { if ( list . get ( i + 1 ) > list . get ( i ) ) continue ; return false ; } } else if ( level % 2 == 1 ) { for ( int i = 0 ; i < list . size ( ) - 1 ; i ++ ) { if ( list . get ( i + 1 ) < list . get ( i ) ) continue ; return false ; } } level ++ ; } return true ; } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 2 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 7 ) ; root . right . right = newNode ( 11 ) ; root . left . left . left = newNode ( 10 ) ; root . left . left . right = newNode ( 5 ) ; root . left . right . right = newNode ( 1 ) ; if ( checkEvenOddLevel ( root ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }
Longest Span with same Sum in two Binary arrays | Java program to find largest subarray with equal number of 0 ' s ▁ and ▁ 1' s . ; Returns largest common subarray with equal number of 0 s and 1 s ; Find difference between the two ; Creates an empty hashMap hM ; Initialize sum of elements ; Initialize result ; Traverse through the given array ; Add current element to sum ; To handle sum = 0 at last index ; If this sum is seen before , then update max_len if required ; Else put this sum in hash table ; Driver code
import java . io . * ; import java . util . * ; class GFG { static int longestCommonSum ( int [ ] arr1 , int [ ] arr2 , int n ) { int [ ] arr = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr1 [ i ] - arr2 [ i ] ; HashMap < Integer , Integer > hM = new HashMap < > ( ) ; int sum = 0 ; int max_len = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) max_len = i + 1 ; if ( hM . containsKey ( sum ) ) max_len = Math . max ( max_len , i - hM . get ( sum ) ) ; else hM . put ( sum , i ) ; } return max_len ; } public static void main ( String args [ ] ) { int [ ] arr1 = { 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; int [ ] arr2 = { 1 , 1 , 1 , 1 , 1 , 0 , 1 } ; int n = arr1 . length ; System . out . println ( longestCommonSum ( arr1 , arr2 , n ) ) ; } }
Minimum length of Run Length Encoding possible by removing at most K characters from a given string | Java program to implement the above approach ; Function which solves the desired problem ; Base Case ; If the entire string has been traversed ; If precomputed subproblem occurred ; Minimum run length encoding by removing the current character ; Minimum run length encoding by retaining the current character ; If the current and the previous characters match ; Otherwise ; Function to return minimum run - length encoding for string s by removing atmost k characters ; Driver Code
import java . util . * ; class GFG { static int maxN = 20 ; static int dp [ ] [ ] [ ] [ ] = new int [ maxN ] [ maxN ] [ 27 ] [ maxN ] ; public static int solve ( String s , int n , int idx , int k , char last , int count ) { if ( k < 0 ) return n + 1 ; if ( idx == n ) return 0 ; int ans = dp [ idx ] [ k ] [ last - ' a ' ] [ count ] ; if ( ans != - 1 ) return ans ; ans = n + 1 ; ans = Math . min ( ans , solve ( s , n , idx + 1 , k - 1 , last , count ) ) ; int inc = 0 ; if ( count == 1 count == 9 count == 99 ) inc = 1 ; if ( last == s . charAt ( idx ) ) { ans = Math . min ( ans , inc + solve ( s , n , idx + 1 , k , s . charAt ( idx ) , count + 1 ) ) ; } else { ans = Math . min ( ans , 1 + solve ( s , n , idx + 1 , k , s . charAt ( idx ) , 1 ) ) ; } return dp [ idx ] [ k ] [ last - ' a ' ] [ count ] = ans ; } public static int MinRunLengthEncoding ( String s , int n , int k ) { for ( int i [ ] [ ] [ ] : dp ) for ( int j [ ] [ ] : i ) for ( int p [ ] : j ) Arrays . fill ( p , - 1 ) ; return solve ( s , n , 0 , k , ( char ) 123 , 0 ) ; } public static void main ( String args [ ] ) { String S = " abbbcdcdd " ; int N = 9 , K = 2 ; System . out . println ( MinRunLengthEncoding ( S , N , K ) ) ; } }
Count unique paths is a matrix whose product of elements contains odd number of divisors | Java program for the above approach ; Function to calculate and store all the paths product in vector ; Base Case ; If reaches the bottom right corner and product is a perfect square ; Find square root ; If square root is an integer ; Move towards down in the matrix ; Move towards right in the matrix ; Driver Code ; Given matrix mat [ ] [ ] ; Function call
import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int countPaths = 0 ; static void CountUniquePaths ( int [ ] [ ] a , int i , int j , int m , int n , int ans ) { if ( i >= m j >= n ) return ; if ( i == m - 1 && j == n - 1 ) { double sr = Math . sqrt ( ans * a [ i ] [ j ] ) ; if ( ( sr - Math . floor ( sr ) ) == 0 ) countPaths ++ ; } CountUniquePaths ( a , i + 1 , j , m , n , ans * a [ i ] [ j ] ) ; CountUniquePaths ( a , i , j + 1 , m , n , ans * a [ i ] [ j ] ) ; } public static void main ( String [ ] args ) { int M = 3 , N = 2 ; int [ ] [ ] mat = { { 1 , 1 } , { 3 , 1 } , { 3 , 1 } } ; CountUniquePaths ( mat , 0 , 0 , M , N , 1 ) ; System . out . println ( countPaths ) ; } }
Maximize sum by selecting M elements from the start or end of rows of a Matrix | Java program for the above approach ; Function to select m elements having maximum sum ; Base case ; If precomputed subproblem occurred ; Either skip the current row ; Iterate through all the possible segments of current row ; Check if it is possible to select elements from i to j ; Compuete the sum of i to j as calculated ; Store the computed answer and return ; Function to precompute the prefix sum for every row of the matrix ; Preprocessing to calculate sum from i to j ; Utility function to select m elements having maximum sum ; Preprocessing step ; Initialize dp array with - 1 ; Stores maximum sum of M elements ; Driver Code ; Given N ; Given M ; Given matrix ; Function Call
import java . util . * ; public class GFG { public static long mElementsWithMaxSum ( long [ ] [ ] matrix , int M , int block , long [ ] [ ] dp ) { if ( block == matrix . length ) return 0 ; if ( dp [ block ] [ M ] != - 1 ) return dp [ block ] [ M ] ; long ans = mElementsWithMaxSum ( matrix , M , block + 1 , dp ) ; for ( int i = 0 ; i < matrix [ block ] . length ; i ++ ) { for ( int j = i ; j < matrix [ block ] . length ; j ++ ) { if ( j - i + 1 <= M ) { ans = Math . max ( ans , matrix [ block ] [ j ] - ( ( i - 1 ) >= 0 ? matrix [ block ] [ i - 1 ] : 0 ) + mElementsWithMaxSum ( matrix , M - j + i - 1 , block + 1 , dp ) ) ; } } } return dp [ block ] [ M ] = ans ; } public static void preComputing ( long [ ] [ ] matrix , int N ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < matrix [ i ] . length ; j ++ ) { matrix [ i ] [ j ] = ( j > 0 ? matrix [ i ] [ j - 1 ] : 0 ) + matrix [ i ] [ j ] ; } } } public static void mElementsWithMaxSumUtil ( long [ ] [ ] matrix , int M , int N ) { preComputing ( matrix , N ) ; long dp [ ] [ ] = new long [ N + 5 ] [ M + 5 ] ; for ( long i [ ] : dp ) Arrays . fill ( i , - 1 ) ; long sum = mElementsWithMaxSum ( matrix , M , 0 , dp ) ; System . out . print ( sum ) ; } public static void main ( String args [ ] ) { int N = 3 ; int M = 4 ; long [ ] [ ] matrix = { { 2 , 3 , 5 } , { - 1 , 7 } , { 8 , 10 } } ; mElementsWithMaxSumUtil ( matrix , M , N ) ; } }
Find the last remaining element after repeated removal of odd and even indexed elements alternately | Java program for the above approach ; Function to calculate the last remaining element from the sequence ; If dp [ n ] is already calculated ; Base Case : ; Recursive call ; Return the value of dp [ n ] ; Driver Code ; Given N ; Stores the ; Function call
import java . util . * ; class GFG { static int lastRemaining ( int n , HashMap < Integer , Integer > dp ) { if ( dp . containsKey ( n ) ) return dp . get ( n ) ; if ( n == 1 ) return 1 ; else dp . put ( n , 2 * ( 1 + n / 2 - lastRemaining ( n / 2 , dp ) ) ) ; return dp . get ( n ) ; } public static void main ( String [ ] args ) { int N = 5 ; HashMap < Integer , Integer > dp = new HashMap < Integer , Integer > ( ) ; System . out . print ( lastRemaining ( N , dp ) ) ; } }
Maximum subsequence sum possible by multiplying each element by its index | Java program for the above approach ; Initialize dp array ; Function to find the maximum sum of the subsequence formed ; Base Case ; If already calculated state occurs ; Include the current element ; Exclude the current element ; Update the maximum ans ; Function to calculate maximum sum of the subsequence obtained ; Initialise the dp array with - 1 ; Print the maximum sum possible ; Driver Code ; Given array ; Size of the array ; Function Call
import java . util . * ; class GFG { static int [ ] [ ] dp = new int [ 1005 ] [ 1005 ] ; static int maximumSumUtil ( int a [ ] , int index , int count , int n ) { if ( index > n count > n + 1 ) { return 0 ; } if ( dp [ index ] [ count ] != - 1 ) return dp [ index ] [ count ] ; int ans1 = maximumSumUtil ( a , index + 1 , count + 1 , n ) + a [ index ] * count ; int ans2 = maximumSumUtil ( a , index + 1 , count , n ) ; return ( dp [ index ] [ count ] = Math . max ( ans1 , ans2 ) ) ; } static void maximumSum ( int arr [ ] , int N ) { for ( int i = 0 ; i < 1005 ; i ++ ) { for ( int j = 0 ; j < 1005 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . print ( maximumSumUtil ( arr , 0 , 1 , N - 1 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , 2 , - 10 , 4 , - 20 } ; int N = arr . length ; maximumSum ( arr , N ) ; } }
Sort an array according to absolute difference with given value | Java program to sort an array according absolute difference with x . ; Function to sort an array according absolute difference with x . ; Store values in a map with the difference with X as key ; Update the values of array ; Function to print the array ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void rearrange ( int [ ] arr , int n , int x ) { TreeMap < Integer , ArrayList < Integer > > m = new TreeMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int diff = Math . abs ( x - arr [ i ] ) ; if ( m . containsKey ( diff ) ) { ArrayList < Integer > al = m . get ( diff ) ; al . add ( arr [ i ] ) ; m . put ( diff , al ) ; } else { ArrayList < Integer > al = new ArrayList < > ( ) ; al . add ( arr [ i ] ) ; m . put ( diff , al ) ; } } int index = 0 ; for ( Map . Entry entry : m . entrySet ( ) ) { ArrayList < Integer > al = m . get ( entry . getKey ( ) ) ; for ( int i = 0 ; i < al . size ( ) ; i ++ ) arr [ index ++ ] = al . get ( i ) ; } } static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 5 , 3 , 9 , 2 } ; int n = arr . length ; int x = 7 ; rearrange ( arr , n , x ) ; printArray ( arr , n ) ; } }
Minimum Steps to obtain N from 1 by the given operations | Java program to implement the above approach ; Base Case ; Recursive Call for n - 1 ; Check if n is divisible by 2 ; Check if n is divisible by 3 ; Returns a tuple ( a , b ) , where a : Minimum steps to obtain x from 1 b : Previous number ; Function that find the optimal solution ; Print the length ; Exit condition for loop = - 1 when n has reached 1 ; Return the sequence in reverse order ; Driver Code ; Given N ; Function call
import java . util . * ; import java . util . Collections ; import java . util . Vector ; Vector < Integer > v = new Vector < Integer > ( n ) ; class GFG { static Vector < Integer > find_sequence ( int n ) { Vector < Integer > temp = new Vector < Integer > ( ) ; temp . add ( 1 ) ; temp . add ( - 1 ) ; if ( n == 1 ) return temp ; Vector < Integer > arr = find_sequence ( n - 1 ) ; Vector < Integer > ans = new Vector < Integer > ( n ) ; ans . add ( arr . get ( 0 ) + 1 ) ; ans . add ( n - 1 ) ; if ( n % 2 == 0 ) { Vector < Integer > div_by_2 = find_sequence ( n / 2 ) ; if ( div_by_2 . get ( 0 ) < ans . get ( 0 ) ) { ans . clear ( ) ; ans . add ( div_by_2 . get ( 0 ) + 1 ) ; ans . add ( n / 2 ) ; } } if ( n % 3 == 0 ) { Vector < Integer > div_by_3 = find_sequence ( n / 3 ) ; if ( div_by_3 . get ( 0 ) < ans . get ( 0 ) ) { ans . clear ( ) ; ans . add ( div_by_3 . get ( 0 ) + 1 ) ; ans . add ( n / 3 ) ; } } return ans ; } static Vector < Integer > find_solution ( int n ) { Vector < Integer > a = find_sequence ( n ) ; System . out . println ( a . get ( 0 ) ) ; Vector < Integer > sequence = new Vector < Integer > ( ) ; sequence . add ( n ) ; while ( a . get ( 1 ) != - 1 ) { sequence . add ( a . get ( 1 ) ) ; Vector < Integer > arr = find_sequence ( a . get ( 1 ) ) ; a . set ( 1 , arr . get ( 1 ) ) ; } Collections . reverse ( sequence ) ; return sequence ; } public static void main ( String args [ ] ) { int n = 5 ; Vector < Integer > res = find_solution ( n ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { System . out . print ( res . get ( i ) + " ▁ " ) ; } } }
Count of vessels completely filled after a given time | Java Program to implement the above approach ; Function to find the number of completely filled vessels ; Store the vessels ; Assuming all water is present in the vessel at the first level ; Store the number of vessel that are completely full ; Traverse all the levels ; Number of vessel at each level is j ; Calculate the exceeded amount of water ; If current vessel has less than 1 unit of water then continue ; One more vessel is full ; If left bottom vessel present ; If right bottom vessel present ; Driver Code ; Number of levels ; Number of seconds ; Function call
import java . io . * ; import java . util . * ; class GFG { static int n , t ; public static int FindNoOfFullVessels ( int n , int t ) { double Matrix [ ] [ ] = new double [ n ] [ n ] ; Matrix [ 0 ] [ 0 ] = t * 1.0 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { double exceededwater = Matrix [ i ] [ j ] - 1.0 ; if ( exceededwater < 0 ) continue ; ans ++ ; if ( i + 1 < n ) Matrix [ i + 1 ] [ j ] += exceededwater / 2 ; if ( i + 1 < n && j + 1 < n ) Matrix [ i + 1 ] [ j + 1 ] += exceededwater / 2 ; } } return ans ; } public static void main ( String [ ] args ) { int N = 3 ; int T = 4 ; System . out . println ( FindNoOfFullVessels ( N , T ) ) ; } }
Sort an array in wave form | Java implementation of naive method for sorting an array in wave form . ; A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] . . ; Sort the input array ; Swap adjacent elements ; Driver method
import java . util . * ; class SortWave { void swap ( int arr [ ] , int a , int b ) { int temp = arr [ a ] ; arr [ a ] = arr [ b ] ; arr [ b ] = temp ; } void sortInWave ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i += 2 ) swap ( arr , i , i + 1 ) ; } public static void main ( String args [ ] ) { SortWave ob = new SortWave ( ) ; int arr [ ] = { 10 , 90 , 49 , 2 , 1 , 5 , 23 } ; int n = arr . length ; ob . sortInWave ( arr , n ) ; for ( int i : arr ) System . out . print ( i + " ▁ " ) ; } }
Count of maximum occurring subsequence using only those characters whose indices are in GP | Java program for the above approach ; Function to count maximum occurring subsequence using only those characters whose indexes are in GP ; Iterate till the length of the given String ; Update ans for 1 - length subsequence ; Update ans for 2 - length subsequence ; Return the answer ; Driver Code ; Given String s ; Function call
import java . util . * ; class GFG { static int findMaxTimes ( String S ) { int [ ] arr = new int [ 26 ] ; int [ ] [ ] dp = new int [ 26 ] [ 26 ] ; for ( int i = 0 ; i < S . length ( ) ; i ++ ) { int now = S . charAt ( i ) - ' a ' ; for ( int j = 0 ; j < 26 ; j ++ ) { dp [ j ] [ now ] += arr [ j ] ; } arr [ now ] ++ ; } int ans = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) ans = Math . max ( ans , arr [ i ] ) ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { ans = Math . max ( ans , dp [ i ] [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { String S = " ddee " ; System . out . print ( findMaxTimes ( S ) ) ; } }
Sort an array in wave form | A O ( n ) Java program to sort an input array in wave form ; A utility method to swap two numbers . ; This function sorts arr [ 0. . n - 1 ] in wave form , i . e . , arr [ 0 ] >= arr [ 1 ] <= arr [ 2 ] >= arr [ 3 ] <= arr [ 4 ] ... . ; Traverse all even elements ; If current even element is smaller than previous ; If current even element is smaller than next ; Driver program to test above function
class SortWave { void swap ( int arr [ ] , int a , int b ) { int temp = arr [ a ] ; arr [ a ] = arr [ b ] ; arr [ b ] = temp ; } void sortInWave ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i += 2 ) { if ( i > 0 && arr [ i - 1 ] > arr [ i ] ) swap ( arr , i - 1 , i ) ; if ( i < n - 1 && arr [ i ] < arr [ i + 1 ] ) swap ( arr , i , i + 1 ) ; } } public static void main ( String args [ ] ) { SortWave ob = new SortWave ( ) ; int arr [ ] = { 10 , 90 , 49 , 2 , 1 , 5 , 23 } ; int n = arr . length ; ob . sortInWave ( arr , n ) ; for ( int i : arr ) System . out . print ( i + " ▁ " ) ; } }
Remove all nodes which don 't lie in any path with sum>= k | Class representing binary tree node ; for print traversal ; class to truncate binary tree recursive method to truncate binary tree ; base case ; recur for left and right subtree ; if node is a leaf node whose data is smaller than the sum we delete the leaf . An important thing to note is a non - leaf node can become leaf when its children are deleted . ; utility method to check if node is leaf ; Driver class to test above function
class Node { int data ; Node left ; Node right ; public Node ( int data ) { this . data = data ; left = null ; right = null ; } } public void print ( Node root ) { if ( root == null ) return ; print ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; print ( root . right ) ; } } class BinaryTree { Node root ; public Node prune ( Node root , int sum ) { if ( root == null ) return null ; root . left = prune ( root . left , sum - root . data ) ; root . right = prune ( root . right , sum - root . data ) ; if ( isLeaf ( root ) ) { if ( sum > root . data ) root = null ; } return root ; } public boolean isLeaf ( Node root ) { if ( root == null ) return false ; if ( root . left == null && root . right == null ) return true ; return false ; } public class GFG { public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . root . right . left = new Node ( 6 ) ; tree . root . right . right = new Node ( 7 ) ; tree . root . left . left . left = new Node ( 8 ) ; tree . root . left . left . right = new Node ( 9 ) ; tree . root . left . right . left = new Node ( 12 ) ; tree . root . right . right . left = new Node ( 10 ) ; tree . root . right . right . left . right = new Node ( 11 ) ; tree . root . left . left . right . left = new Node ( 13 ) ; tree . root . left . left . right . right = new Node ( 14 ) ; tree . root . left . left . right . right . left = new Node ( 15 ) ; System . out . println ( " Tree ▁ before ▁ truncation " ) ; tree . print ( tree . root ) ; tree . prune ( tree . root , 45 ) ; System . out . println ( " Tree after truncation "); tree . print ( tree . root ) ; } }
Count all possible unique sum of series K , K + 1 , K + 2 , K + 3 , K + 4 , ... , K + N | Java program for the above approach ; Function to count the unique sum ; Initialize array fsum [ ] with 0 ; Initialize array rsum [ ] with 0 ; Set fsum [ 0 ] as ar [ 0 ] ; Set rsum [ 0 ] as ar [ n ] ; For each i update fsum [ i ] with ar [ i ] + fsum [ i - 1 ] ; For each i from n - 1 , update rsum [ i ] with ar [ i ] + fsum [ i + 1 ] ; K represent size of subset as explained above ; Using above relation ; Return the result ; Driver Code ; Given a number N ; Function Call
import java . util . * ; class GFG { static int count_unique_sum ( int n ) { int i ; int ar [ ] = new int [ n + 1 ] ; int fsum [ ] = new int [ n + 1 ] ; int rsum [ ] = new int [ n + 1 ] ; int ans = 1 ; Arrays . fill ( fsum , 0 ) ; Arrays . fill ( rsum , 0 ) ; for ( i = 0 ; i <= n ; i ++ ) { ar [ i ] = i ; } fsum [ 0 ] = ar [ 0 ] ; rsum [ n ] = ar [ n ] ; for ( i = 1 ; i <= n ; i ++ ) { fsum [ i ] = ar [ i ] + fsum [ i - 1 ] ; } for ( i = n - 1 ; i >= 0 ; i -- ) { rsum [ i ] = ar [ i ] + rsum [ i + 1 ] ; } for ( int k = 2 ; k <= n ; k ++ ) { ans += 1 + rsum [ n + 1 - k ] - fsum [ k - 1 ] ; } return ans ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . print ( count_unique_sum ( N ) ) ; } }
Merge an array of size n into another array of size m + n | Java program to Merge an array of size n into another array of size m + n ; Function to move m elements at the end of array * mPlusN [ ] ; Merges array N [ ] of size n into array mPlusN [ ] of size m + n ; Current index of i / p part of mPlusN [ ] ; Current index of N [ ] ; Current index of output mPlusN [ ] ; Take an element from mPlusN [ ] if a ) value of the picked element is smaller and we have not reached end of it b ) We have reached end of N [ ] ; Otherwise take element from N [ ] ; Utility that prints out an array on a line ; Driver Code ; Initialize arrays ; Move the m elements at the end of mPlusN ; Merge N [ ] into mPlusN [ ] ; Print the resultant mPlusN
class MergeArrays { void moveToEnd ( int mPlusN [ ] , int size ) { int i , j = size - 1 ; for ( i = size - 1 ; i >= 0 ; i -- ) { if ( mPlusN [ i ] != - 1 ) { mPlusN [ j ] = mPlusN [ i ] ; j -- ; } } } void merge ( int mPlusN [ ] , int N [ ] , int m , int n ) { int i = n ; int j = 0 ; int k = 0 ; while ( k < ( m + n ) ) { if ( ( i < ( m + n ) && mPlusN [ i ] <= N [ j ] ) || ( j == n ) ) { mPlusN [ k ] = mPlusN [ i ] ; k ++ ; i ++ ; } else { mPlusN [ k ] = N [ j ] ; k ++ ; j ++ ; } } } void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { MergeArrays mergearray = new MergeArrays ( ) ; int mPlusN [ ] = { 2 , 8 , - 1 , - 1 , - 1 , 13 , - 1 , 15 , 20 } ; int N [ ] = { 5 , 7 , 9 , 25 } ; int n = N . length ; int m = mPlusN . length - n ; mergearray . moveToEnd ( mPlusN , m + n ) ; mergearray . merge ( mPlusN , N , m , n ) ; mergearray . printArray ( mPlusN , m + n ) ; } }