codeparrot/xlcost-text-to-code huggingface dataset:
Count pairs from a given range having even sum | Java program for the above approach ; Function to find the count of ordered pairs having even sum ; Stores count of even numbers in the range [ L , R ] ; If L is even ; Update count_even ; Update count_odd ; Stores count of odd numbers in the range [ L , R ] ; Update count_odd ; Update count_odd ; Stores count of pairs whose sum is even and both elements of the pairs are also even ; Stores count of pairs whose sum is even and both elements of the pairs are odd ; Print total ordered pairs whose sum is even ; Driver Code ; Given L & R ; Function Call
class GFG { static void countPairs ( int L , int R ) { int count_even ; if ( L % 2 == 0 ) { count_even = ( R / 2 ) - ( L / 2 ) + 1 ; } else { count_even = ( R / 2 ) - ( L / 2 ) ; } int count_odd ; if ( L % 2 == 0 ) { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) ; } else { count_odd = ( ( R + 1 ) / 2 ) - ( ( L + 1 ) / 2 ) + 1 ; } count_even *= count_even ; count_odd *= count_odd ; System . out . println ( count_even + count_odd ) ; } public static void main ( String [ ] args ) { int L = 1 , R = 3 ; countPairs ( L , R ) ; } }
Count of array elements that can be found using Randomized Binary Search on every array element | Java program for the above approach ; Function to find minimum count of array elements found by repeatedly applying Randomized Binary Search ; Stores count of array elements ; smallestRight [ i ] : Stores the smallest array element on the right side of i ; Update smallestRight [ 0 ] ; Traverse the array from right to left ; Update smallestRight [ i ] ; Stores the largest element upto i - th index ; Stores the minimum count of elements found by repeatedly applying Randomized Binary Search ; If largest element on left side is less than smallest element on right side ; Update ans ; Update mn ; Driver Code ; Given array ; Function Call
import java . io . * ; import java . util . * ; class GFG { static int getDefiniteFinds ( int [ ] arr ) { int n = arr . length ; int [ ] smallestRight = new int [ n + 1 ] ; smallestRight [ n ] = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { smallestRight [ i ] = Math . min ( smallestRight [ i + 1 ] , arr [ i ] ) ; } int mn = Integer . MIN_VALUE ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mn < arr [ i ] && arr [ i ] < smallestRight [ i + 1 ] ) { ans ++ ; } mn = Math . max ( arr [ i ] , mn ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 5 , 4 , 9 } ; System . out . println ( getDefiniteFinds ( arr ) ) ; } }
Count numbers from a given range having same first and last digits in their binary representation | Java program to implement the above approach ; Function to count numbers in range [ L , R ] having same first and last digit in the binary representation ; Driver code ; Given range [ L , R ] ; Function Call
import java . util . * ; class GFG { static void Count_numbers ( int L , int R ) { int count = ( R - L ) / 2 ; if ( R % 2 != 0 L % 2 != 0 ) count += 1 ; System . out . print ( count ) ; } public static void main ( String [ ] args ) { int L = 6 , R = 30 ; Count_numbers ( L , R ) ; } }
Find the index having sum of elements on its left equal to reverse of the sum of elements on its right | Java program for the above approach ; Function to check if a number is equal to the reverse of digits of other number ; Stores reverse of digits of rightSum ; Stores rightSum ; Calculate reverse of digits of temp ; Update rev ; Update temp ; If reverse of digits of rightSum equal to leftSum ; Function to find the index that satisfies the condition ; Stores sum of array elements on right side of each index ; Stores sum of array elements on left side of each index ; Traverse the array ; Update rightSum ; Traverse the array ; Update rightSum ; If leftSum equal to reverse of digits of rightSum ; Update leftSum ; Driver code
import java . util . * ; class GFG { static boolean checkReverse ( int leftSum , int rightSum ) { int rev = 0 ; int temp = rightSum ; while ( temp != 0 ) { rev = ( rev * 10 ) + ( temp % 10 ) ; temp /= 10 ; } if ( rev == leftSum ) { return true ; } return false ; } static int findIndex ( int [ ] arr , int N ) { int rightSum = 0 ; int leftSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { rightSum += arr [ i ] ; } for ( int i = 0 ; i < N ; i ++ ) { rightSum -= arr [ i ] ; if ( checkReverse ( leftSum , rightSum ) ) { return i ; } leftSum += arr [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 7 , 3 , 6 , 4 , 9 , 2 } ; int N = arr . length ; System . out . print ( findIndex ( arr , N ) ) ; } }
Longest Common Subsequence of two arrays out of which one array consists of distinct elements only | Java program to to implement the above approach ; Function to find the Longest Common Subsequence between the two arrays ; Maps elements of firstArr [ ] to their respective indices ; Traverse the array firstArr [ ] ; Stores the indices of common elements between firstArr [ ] and secondArr [ ] ; Traverse the array secondArr [ ] ; If current element exists in the Map ; Stores lIS from tempArr [ ] ; Driver Code
import java . util . * ; class GFG { static int LCS ( int [ ] firstArr , int [ ] secondArr ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < firstArr . length ; i ++ ) { mp . put ( firstArr [ i ] , i + 1 ) ; } Vector < Integer > tempArr = new Vector < > ( ) ; for ( int i = 0 ; i < secondArr . length ; i ++ ) { if ( mp . containsKey ( secondArr [ i ] ) ) { tempArr . add ( mp . get ( secondArr [ i ] ) ) ; } } Vector < Integer > tail = new Vector < > ( ) ; tail . add ( tempArr . get ( 0 ) ) ; for ( int i = 1 ; i < tempArr . size ( ) ; i ++ ) { if ( tempArr . get ( i ) > tail . lastElement ( ) ) tail . add ( tempArr . get ( i ) ) ; else if ( tempArr . get ( i ) < tail . get ( 0 ) ) tail . add ( 0 , tempArr . get ( i ) ) ; } return ( int ) tail . size ( ) ; } public static void main ( String [ ] args ) { int [ ] firstArr = { 3 , 5 , 1 , 8 } ; int [ ] secondArr = { 3 , 3 , 5 , 3 , 8 } ; System . out . print ( LCS ( firstArr , secondArr ) ) ; } }
Count ways to partition a Binary String such that each substring contains exactly two 0 s | Java program for the above approach ; Function to find count of ways to partition the string such that each partition contains exactly two 0 s . ; Stores indices of 0 s in the given string . ; Store the count of ways to partition the string such that each partition contains exactly two 0 s . ; Iterate over each characters of the given string ; If current character is '0' ; Insert index ; Stores total count of 0 s in str ; Traverse the array , IdxOf0s [ ] ; Update cntWays ; Driver code
import java . util . * ; class GFG { static int totalWays ( int n , String str ) { ArrayList < Integer > IdxOf0s = new ArrayList < Integer > ( ) ; int cntWays = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) { IdxOf0s . add ( i ) ; } } int M = IdxOf0s . size ( ) ; if ( ( M == 0 ) || ( ( M % 2 ) != 0 ) ) { return 0 ; } for ( int i = 2 ; i < M ; i += 2 ) { cntWays = cntWays * ( IdxOf0s . get ( i ) - IdxOf0s . get ( i - 1 ) ) ; } return cntWays ; } public static void main ( String [ ] args ) { String str = "00100" ; int n = str . length ( ) ; System . out . print ( totalWays ( n , str ) ) ; } }
Count non | Java program to implement the above approach ; Function to find the total count of triplets ( i , j , k ) such that i < j < k and ( j - i ) != ( k - j ) ; Stores indices of 0 s ; Stores indices of 1 s ; Stores indices of 2 s ; Traverse the array ; If current array element is 0 ; If current array element is 1 ; If current array element is 2 ; Total count of triplets ; Traverse the array zero_i [ ] ; Traverse the array one_i [ ] ; Stores index of 0 s ; Stores index of 1 s ; Stores third element of triplets that does not satisfy the condition ; If r present in the map ; Update r ; If r present in the map ; Update r ; If r present in the map and equidistant ; Print the obtained count ; Driver Code
import java . util . * ; class GFG { static void countTriplets ( int [ ] arr , int N ) { Vector < Integer > zero_i = new Vector < Integer > ( ) ; Vector < Integer > one_i = new Vector < Integer > ( ) ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] == 0 ) zero_i . add ( i + 1 ) ; else if ( arr [ i ] == 1 ) one_i . add ( i + 1 ) ; else mp . put ( i + 1 , 1 ) ; } int total = zero_i . size ( ) * one_i . size ( ) * mp . size ( ) ; for ( int i = 0 ; i < zero_i . size ( ) ; i ++ ) { for ( int j = 0 ; j < one_i . size ( ) ; j ++ ) { int p = zero_i . get ( i ) ; int q = one_i . get ( j ) ; int r = 2 * p - q ; if ( mp . containsKey ( r ) && mp . get ( r ) > 0 ) total -- ; r = 2 * q - p ; if ( mp . containsKey ( r ) && mp . get ( r ) > 0 ) total -- ; r = ( p + q ) / 2 ; if ( mp . containsKey ( r ) && mp . get ( r ) > 0 && Math . abs ( r - p ) == Math . abs ( r - q ) ) total -- ; } } System . out . print ( total ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 2 , 1 } ; int N = arr . length ; countTriplets ( arr , N ) ; } }
Print prime factors of a given integer in decreasing order using Stack | Java program for the above approach ; Function to print prime factors of N in decreasing order ; Stores prime factors of N in decreasing order ; Insert i into stack ; Update N ; Update i ; Print value of stack st ; Driver Code ; Function Call
import java . util . * ; class GFG { static void PrimeFactors ( int N ) { Stack < Integer > st = new Stack < > ( ) ; int i = 2 ; while ( N != 1 ) { if ( N % i == 0 ) { st . push ( i ) ; while ( N % i == 0 ) { N = N / i ; } } i ++ ; } while ( ! st . isEmpty ( ) ) { System . out . println ( st . peek ( ) ) ; st . pop ( ) ; } } public static void main ( String [ ] args ) { int N = 8 ; PrimeFactors ( N ) ; ; } }
Count subarrays consisting of first K natural numbers in descending order | Java program for the above approach ; Function to count subarray having the decreasing sequence K to 1 ; Traverse the array ; Check if required sequence is present or not ; Reset temp to k ; Return the count ; Driver code ; Function Call
import java . util . * ; class GFG { static int CountSubarray ( int arr [ ] , int n , int k ) { int temp = k , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == temp ) { if ( temp == 1 ) { count ++ ; temp = k ; } else temp -- ; } else { temp = k ; if ( arr [ i ] == k ) i -- ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 7 , 9 , 3 , 2 , 1 , 8 , 3 , 2 , 1 } ; int N = arr . length ; int K = 3 ; System . out . println ( CountSubarray ( arr , N , K ) ) ; } }
Smallest number not less than N which is divisible by all digits of N | Java program for the above approach ; Function to calculate the LCM ; Function to find the smallest number satisfying given constraints ; LCM value is 1 initially ; Finding the LCM of all non zero digits ; Update the value lcm ; Stores ceil value ; Print the answer ; Driver Code ; Function Call
class GFG { static int LCM ( int A , int B ) { return ( A * B / __gcd ( A , B ) ) ; } static void findSmallestNumber ( int X ) { int lcm = 1 ; int temp = X ; while ( temp > 0 ) { int last = temp % 10 ; temp /= 10 ; if ( last == 0 ) continue ; lcm = LCM ( lcm , last ) ; } int answer = ( ( X + lcm - 1 ) / lcm ) * lcm ; System . out . print ( answer ) ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int X = 280 ; findSmallestNumber ( X ) ; } }
Minimum sum of two integers whose product is strictly greater than N | Java program for the above approach ; Function to find the minimum sum of two integers such that their product is strictly greater than N ; Initialise low as 0 and high as 1e9 ; Iterate to find the first number ; Find the middle value ; If mid ^ 2 is greater than equal to A , then update high to mid ; Otherwise update low ; Store the first number ; Again , set low as 0 and high as 1e9 ; Iterate to find the second number ; Find the middle value ; If first number * mid is greater than N then update high to mid ; Else , update low to mid ; Store the second number ; Print the result ; Driver Code ; Function Call
import java . io . * ; class GFG { static void minSum ( int N ) { long low = 0 , high = 1000000000 ; while ( low + 1 < high ) { long mid = low + ( high - low ) / 2 ; if ( mid * mid >= N ) { high = mid ; } else { low = mid ; } } long first = high ; low = 0 ; high = 1000000000 ; while ( low + 1 < high ) { long mid = low + ( high - low ) / 2 ; if ( first * mid > N ) { high = mid ; } else { low = mid ; } } long second = high ; System . out . println ( first + second ) ; } public static void main ( String [ ] args ) { int N = 10 ; minSum ( N ) ; } }
Minimum sum of two integers whose product is strictly greater than N | Java program for the above approach ; Function to find the minimum sum of two integers such that their product is strictly greater than N ; Store the answer using the AP - GP inequality ; Print the answer ; Driver Code ; Function Call
import java . lang . * ; class GFG { static void minSum ( int N ) { int ans = ( int ) Math . ceil ( 2 * Math . sqrt ( N + 1 ) ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int N = 10 ; minSum ( N ) ; } }
Query to find length of the longest subarray consisting only of 1 s | Java Program for the above approach ; Arrays to store prefix sums , suffix and MAX 's node respectively ; Function to conSegment Tree ; MAX array for node MAX ; Array for prefix sum node ; Array for suffix sum node ; Calculate MAX node ; Function to update Segment Tree ; Update at position ; Update sums from bottom to the top of Segment Tree ; Update MAX tree ; Update pref tree ; Update suf tree ; Function to perform given queries ; Stores count of queries ; Traverse each query ; Print longest length of subarray in [ 1 , N ] ; Flip the character ; Driver Code ; Size of array ; Given array ; Given queries ; Number of queries
import java . util . * ; class GFG { static final int INF = 1000000 ; static int [ ] pref = new int [ 500005 ] ; static int [ ] suf = new int [ 500005 ] ; static int [ ] MAX = new int [ 500005 ] ; static void build ( int a [ ] , int tl , int tr , int v ) { if ( tl == tr ) { MAX [ v ] = a [ tl ] ; pref [ v ] = a [ tl ] ; suf [ v ] = a [ tl ] ; } else { int tm = ( tl + tr ) / 2 ; build ( a , tl , tm , v * 2 ) ; build ( a , tm + 1 , tr , v * 2 + 1 ) ; MAX [ v ] = Math . max ( MAX [ v * 2 ] , Math . max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = Math . max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == ( tm - tl + 1 ) ? 1 : 0 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = Math . max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + suf [ v * 2 ] * ( suf [ 2 * v + 1 ] == ( tr - tm ) ? 1 : 0 ) ) ; } } static void update ( int a [ ] , int pos , int tl , int tr , int v ) { if ( tl > pos tr < pos ) { return ; } if ( tl == tr && tl == pos ) { MAX [ v ] = a [ pos ] ; pref [ v ] = a [ pos ] ; suf [ v ] = a [ pos ] ; } else { int tm = ( tl + tr ) / 2 ; update ( a , pos , tl , tm , v * 2 ) ; update ( a , pos , tm + 1 , tr , v * 2 + 1 ) ; MAX [ v ] = Math . max ( MAX [ v * 2 ] , Math . max ( MAX [ v * 2 + 1 ] , suf [ v * 2 ] + pref [ v * 2 + 1 ] ) ) ; pref [ v ] = Math . max ( pref [ v * 2 ] , pref [ 2 * v ] + ( pref [ 2 * v ] == ( tm - tl + 1 ) ? 1 : 0 ) * pref [ v * 2 + 1 ] ) ; suf [ v ] = Math . max ( suf [ v * 2 + 1 ] , suf [ 2 * v + 1 ] + ( suf [ 2 * v + 1 ] == ( tr - tm ) ? 1 : 0 ) * suf [ v * 2 ] ) ; } } static void solveQueries ( int arr [ ] , int n , int [ ] [ ] Q , int k ) { int cntQuery = Q . length ; build ( arr , 0 , n - 1 , 1 ) ; for ( int i = 0 ; i < cntQuery ; i ++ ) { if ( Q [ i ] [ 0 ] == 1 ) { System . out . print ( MAX [ 1 ] + " ▁ " ) ; } else { arr [ Q [ i ] [ 1 ] - 1 ] ^= 1 ; update ( arr , Q [ i ] [ 1 ] - 1 , 0 , n - 1 , 1 ) ; } } } public static void main ( String [ ] args ) { int N = 10 ; int arr [ ] = { 1 , 1 , 0 , 1 , 1 , 1 , 0 , 0 , 1 , 1 } ; int [ ] [ ] Q = { { 1 } , { 2 , 3 } , { 1 } } ; int K = 3 ; solveQueries ( arr , N , Q , K ) ; } }
Find the array element having maximum frequency of the digit K | Java program for the above approach ; Function to find the count of digits , k in the given number n ; Stores count of occurrence of digit K in N ; Iterate over digits of N ; If current digit is k ; Update count ; Update N ; Utility function to find an array element having maximum frequency of digit k ; Stores frequency of digit K in arr [ i ] ; Stores maximum frequency of digit K in the array ; Stores an array element having maximum frequency of digit k ; Initialize max ; Traverse the array ; Count the frequency of digit k in arr [ i ] ; Update max with maximum frequency found so far ; Update ele ; If there is no array element having digit k in it ; Function to find an array element having maximum frequency of digit k ; Stores an array element having maximum frequency of digit k ; If there is no element found having digit k in it ; Print the element having max frequency of digit k ; Driver Code ; The digit whose max occurrence has to be found ; Given array ; Size of array ; Function Call
class GFG { static int countFreq ( int N , int K ) { int count = 0 ; while ( N > 0 ) { if ( N % 10 == K ) { count ++ ; } N = N / 10 ; } return count ; } static int findElementUtil ( int arr [ ] , int N , int K ) { int c ; int max ; int ele = 0 ; max = 0 ; for ( int i = 0 ; i < N ; i ++ ) { c = countFreq ( arr [ i ] , K ) ; if ( c > max ) { max = c ; ele = arr [ i ] ; } } if ( max == 0 ) return - 1 ; else return ele ; } static void findElement ( int arr [ ] , int N , int K ) { int ele = findElementUtil ( arr , N , K ) ; if ( ele == - 1 ) System . out . print ( " - 1" ) ; else System . out . print ( ele ) ; } public static void main ( String [ ] args ) { int K = 3 ; int arr [ ] = { 3 , 77 , 343 , 456 } ; int N = arr . length ; findElement ( arr , K , N ) ; } }
Minimum removals required to make any interval equal to the union of the given Set | Java implementation of the above approach ; Function to count minimum number of removals required to make an interval equal to the union of the given Set ; Stores the minimum number of removals ; Traverse the Set ; Left Boundary ; Right Boundary ; Stores count of intervals lying within current interval ; Traverse over all remaining intervals ; Check if interval lies within the current interval ; Increase count ; Update minimum removals required ; Driver Code ; Returns the minimum removals
import java . util . * ; class GFG { static int findMinDeletions ( int [ ] [ ] v , int n ) { int minDel = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int L = v [ i ] [ 0 ] ; int R = v [ i ] [ 1 ] ; int Count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( v [ j ] [ 0 ] >= L && v [ j ] [ 1 ] <= R ) { Count += 1 ; } } minDel = Math . min ( minDel , n - Count ) ; } return minDel ; } public static void main ( String [ ] args ) { int [ ] [ ] v = { { 1 , 3 } , { 4 , 12 } , { 5 , 8 } , { 13 , 20 } } ; int N = v . length ; System . out . print ( findMinDeletions ( v , N ) ) ; } }
Maximum even numbers present in any subarray of size K | Java program to implement the above approach ; Function to find the maximum count of even numbers from all the subarrays of size K ; Stores the count of even numbers in a subarray of size K ; Calculate the count of even numbers in the current subarray ; If current element is an even number ; Stores the maximum count of even numbers from all the subarrays of size K ; Traverse remaining subarrays of size K using sliding window technique ; If the first element of the subarray is even ; Update curr ; If i - th element is even increment the count ; Update the answer ; Return answer ; Driver Code ; Size of the input array ; Function call
import java . util . * ; class GFG { static int maxEvenIntegers ( int arr [ ] , int N , int M ) { int curr = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( arr [ i ] % 2 == 0 ) curr ++ ; } int ans = curr ; for ( int i = M ; i < N ; i ++ ) { if ( arr [ i - M ] % 2 == 0 ) { curr -- ; } if ( arr [ i ] % 2 == 0 ) curr ++ ; ans = Math . max ( ans , curr ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 , 6 } ; int M = 3 ; int N = arr . length ; System . out . print ( maxEvenIntegers ( arr , N , M ) + "NEW_LINE"); } }
Count numbers up to N having digit D in its octal representation | Java program to implement the above approach ; Function to count the numbers in given range whose octal representation contains atleast digit , d ; Store count of numbers up to n whose octal representation contains digit d ; Iterate over the range [ 1 , n ] ; Calculate digit of i in octal representation ; Check if octal representation of x contains digit d ; Update total ; Update x ; Print the answer ; Driver code ; Given N and D ; Counts and prints numbers up to N having D as a digit in its octal representation
import java . io . * ; import java . util . * ; class GFG { static void countNumbers ( int n , int d ) { int total = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int x = i ; while ( x > 0 ) { if ( x % 8 == d ) { total ++ ; break ; } x = x / 8 ; } } System . out . println ( total ) ; } public static void main ( String [ ] args ) { int n = 20 , d = 7 ; countNumbers ( n , d ) ; } }
Count number of triplets ( a , b , c ) from first N natural numbers such that a * b + c = N | Java program to implement the above approach ; Function to find the count of triplets ( a , b , c ) with a * b + c = N ; Stores count of triplets of 1 st N natural numbers which are of the form a * b + c = N ; Iterate over the range [ 1 , N ] ; If N is divisible by i ; Update cntTriplet ; Update cntTriplet ; Driver code
import java . util . * ; class GFG { static int findCntTriplet ( int N ) { int cntTriplet = 0 ; for ( int i = 1 ; i < N ; i ++ ) { if ( N % i != 0 ) { cntTriplet += N / i ; } else { cntTriplet += ( N / i ) - 1 ; } } return cntTriplet ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( findCntTriplet ( N ) ) ; } }
Count divisors which generates same Quotient and Remainder | Java program of the above approach ; Function to calculate the count of numbers such that it gives same quotient and remainder ; Stores divisor of number N . ; Iterate through numbers from 2 to sqrt ( N ) and store divisors of N ; As N is also divisor of itself ; Iterate through divisors ; Checking whether x satisfies the required condition ; Print the count ; Driver Code ; Given N ; Function Call
class GFG { static void countDivisors ( int n ) { int count = 0 ; int j = 0 ; int divisor [ ] = new int [ n ] ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { divisor [ j ] = i ; j += 1 ; } else { divisor [ j ] = i ; divisor [ j + 1 ] = n / i ; j += 2 ; } } } divisor [ j ] = n ; for ( int i = 0 ; i <= j ; i ++ ) { int x = divisor [ i ] ; x -= 1 ; if ( ( n / x ) == ( n % x ) ) count ++ ; } System . out . print ( count ) ; } public static void main ( String [ ] args ) { int N = 10000000 ; countDivisors ( N ) ; } }
Smallest indexed array element required to be flipped to make sum of array equal to 0 | Java program to implement the above approach ; Function to find the smallest indexed array element required to be flipped to make sum of the given array equal to 0 ; Stores the required index ; Traverse the given array ; Flip the sign of current element ; Stores the sum of array elements ; Find the sum of the array ; Update sum ; If sum is equal to 0 ; Update pos ; Reset the current element to its original value ; Reset the value of arr [ i ] ; Driver Code
import java . io . * ; class GFG { static int smallestIndexArrayElementsFlip ( int arr [ ] , int N ) { int pos = - 1 ; for ( int i = 0 ; i < N ; i ++ ) { arr [ i ] *= - 1 ; int sum = 0 ; for ( int j = 0 ; j < N ; j ++ ) { sum += arr [ j ] ; } if ( sum == 0 ) { pos = i ; break ; } else { arr [ i ] *= - 1 ; } } return pos ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , - 5 , 3 , 4 } ; int N = arr . length ; System . out . println ( smallestIndexArrayElementsFlip ( arr , N ) ) ; } }