import java . util . * ; class GFG { static void quadrant ( String s ) { int l = s . length ( ) ; int i ; if ( s . contains ( " + " ) ) { i = s . indexOf ( ' + ' ) ; } else { i = s . indexOf ( ' - ' ) ; } String real = s . substring ( 0 , i ) ; String imaginary = s . substring ( i + 1 , l - 1 ) ; int x = Integer . valueOf ( real ) ; int y = Integer . valueOf ( imaginary ) ; if ( x > 0 && y > 0 ) System . out . print ( " Quadrant ▁ 1" ) ; else if ( x < 0 && y > 0 ) System . out . print ( " Quadrant ▁ 2" ) ; else if ( x < 0 && y < 0 ) System . out . print ( " Quadrant ▁ 3" ) ; else if ( x > 0 && y < 0 ) System . out . print ( " Quadrant ▁ 4" ) ; else if ( x == 0 && y > 0 ) System . out . print ( " Lies ▁ on ▁ positive " + " ▁ Imaginary ▁ axis " ) ; else if ( x == 0 && y < 0 ) System . out . print ( " Lies ▁ on ▁ negative " + " ▁ Imaginary ▁ axis " ) ; else if ( y == 0 && x < 0 ) System . out . print ( " Lies ▁ on ▁ negative " + " ▁ X - axis " ) ; else if ( y == 0 && x > 0 ) System . out . print ( " Lies ▁ on ▁ positive " + " ▁ X - axis " ) ; else System . out . print ( " Lies ▁ on ▁ the ▁ Origin " ) ; } public static void main ( String [ ] args ) { String s = "5 + 3i " ; quadrant ( s ) ; } }

def quadrant ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE if ( ' + ' in s ) : NEW_LINE INDENT i = s . index ( ' + ' ) NEW_LINE DEDENT else : NEW_LINE INDENT i = s . index ( ' - ' ) NEW_LINE DEDENT real = s [ 0 : i ] NEW_LINE imaginary = s [ i + 1 : l - 1 ] NEW_LINE x = int ( real ) NEW_LINE y = int ( imaginary ) NEW_LINE if ( x > 0 and y > 0 ) : NEW_LINE INDENT print ( " Quadrant ▁ 1" ) NEW_LINE DEDENT elif ( x < 0 and y > 0 ) : NEW_LINE INDENT print ( " Quadrant ▁ 2" ) NEW_LINE DEDENT elif ( x < 0 and y < 0 ) : NEW_LINE INDENT print ( " Quadrant ▁ 3" ) NEW_LINE DEDENT elif ( x > 0 and y < 0 ) : NEW_LINE INDENT print ( " Quadrant ▁ 4" ) NEW_LINE DEDENT elif ( x == 0 and y > 0 ) : NEW_LINE INDENT print ( " Lies ▁ on ▁ positive " , " Imaginary ▁ axis " ) NEW_LINE DEDENT elif ( x == 0 and y < 0 ) : NEW_LINE INDENT print ( " Lies ▁ on ▁ negative " , " Imaginary ▁ axis " ) NEW_LINE DEDENT elif ( y == 0 and x < 0 ) : NEW_LINE INDENT print ( " Lies ▁ on ▁ negative " , " X - axis " ) NEW_LINE DEDENT elif ( y == 0 and x > 0 ) : NEW_LINE INDENT print ( " Lies ▁ on ▁ positive " , " X - axis " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Lies ▁ on ▁ the ▁ Origin " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "5 + 3i " NEW_LINE quadrant ( s ) NEW_LINE DEDENT


class GFG { public static void printSeries ( int n ) { int [ ] nmtr = new int [ ] { 1 , 1 , 1 , 3 } ; int [ ] dntr = new int [ ] { 0 , 4 , 2 , 4 } ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 4 == 0 ) System . out . print ( nmtr [ i % 4 ] + ( i / 4 ) - 1 + " ▁ " ) ; else { System . out . print ( nmtr [ i % 4 ] + ( ( i / 4 ) * dntr [ i % 4 ] ) + " / " + dntr [ i % 4 ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int n = 9 ; printSeries ( n ) ; } }

def printSeries ( n ) : NEW_LINE INDENT nmtr = [ 1 , 1 , 1 , 3 ] NEW_LINE dntr = [ 0 , 4 , 2 , 4 ] NEW_LINE for i in range ( 1 , n + 1 , 1 ) : NEW_LINE INDENT if ( i % 4 == 0 ) : NEW_LINE INDENT print ( nmtr [ i % 4 ] + int ( i / 4 ) - 1 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( nmtr [ i % 4 ] + ( int ( i / 4 ) * dntr [ i % 4 ] ) , end = " " ) NEW_LINE print ( " / " , end = " " ) NEW_LINE print ( dntr [ i % 4 ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 9 NEW_LINE printSeries ( n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int countPairs ( int arr [ ] , int n ) { int forward [ ] = new int [ n ] ; Stack < Integer > sForward = new Stack < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! sForward . empty ( ) && arr [ i ] > arr [ ( Integer ) sForward . peek ( ) ] ) { forward [ ( Integer ) sForward . peek ( ) ] = 1 ; sForward . pop ( ) ; } sForward . push ( i ) ; } int backward [ ] = new int [ n ] ; Stack < Integer > sBackward = new Stack < Integer > ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ! sBackward . empty ( ) && arr [ i ] > arr [ ( Integer ) sBackward . peek ( ) ] ) { backward [ ( Integer ) sBackward . peek ( ) ] = 1 ; sBackward . pop ( ) ; } sBackward . push ( i ) ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { res += forward [ i ] + backward [ i ] ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }

def countPairs ( arr , n ) : NEW_LINE INDENT forward = [ 0 ] * n ; NEW_LINE sForward = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( len ( sForward ) != 0 and arr [ i ] > arr [ sForward [ - 1 ] ] ) : NEW_LINE INDENT forward [ sForward [ - 1 ] ] = 1 ; NEW_LINE sForward . pop ( ) ; NEW_LINE DEDENT sForward . append ( i ) ; NEW_LINE DEDENT backward = [ 0 ] * n ; NEW_LINE sBackward = [ ] ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( len ( sBackward ) != 0 and arr [ i ] > arr [ sBackward [ - 1 ] ] ) : NEW_LINE INDENT backward [ sBackward [ - 1 ] ] = 1 ; NEW_LINE sBackward . pop ( ) ; NEW_LINE DEDENT sBackward . append ( i ) ; NEW_LINE DEDENT res = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT res += forward [ i ] + backward [ i ] ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countPairs ( arr , n ) ) ; NEW_LINE DEDENT


class Test { static int findTransitionPoint ( int arr [ ] , int n ) { int lb = 0 , ub = n - 1 ; while ( lb <= ub ) { int mid = ( lb + ub ) / 2 ; if ( arr [ mid ] == 0 ) lb = mid + 1 ; else if ( arr [ mid ] == 1 ) { if ( arr [ mid - 1 ] == 0 ) return mid ; ub = mid - 1 ; } } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 0 , 0 , 0 , 1 , 1 } ; int point = findTransitionPoint ( arr , arr . length ) ; System . out . println ( point >= 0 ? " Transition ▁ point ▁ is ▁ " + point : " There ▁ is ▁ no ▁ transition ▁ point " ) ; } }

def findTransitionPoint ( arr , n ) : NEW_LINE INDENT lb = 0 NEW_LINE ub = n - 1 NEW_LINE while ( lb <= ub ) : NEW_LINE INDENT mid = ( int ) ( ( lb + ub ) / 2 ) NEW_LINE if ( arr [ mid ] == 0 ) : NEW_LINE INDENT lb = mid + 1 NEW_LINE DEDENT elif ( arr [ mid ] == 1 ) : NEW_LINE INDENT if ( arr [ mid - 1 ] == 0 ) : NEW_LINE INDENT return mid NEW_LINE DEDENT ub = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 0 , 0 , 0 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE point = findTransitionPoint ( arr , n ) ; NEW_LINE if ( point >= 0 ) : NEW_LINE INDENT print ( " Transition ▁ point ▁ is ▁ " , point ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ is ▁ no ▁ transition ▁ point " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int sumNatural ( int n ) { int sum = ( n * ( n + 1 ) ) ; return sum ; } static int sumEven ( int l , int r ) { return sumNatural ( r / 2 ) - sumNatural ( ( l - 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int l = 2 , r = 5 ; System . out . println ( " Sum ▁ of ▁ Natural ▁ numbers ▁ from ▁ L ▁ to ▁ R ▁ is ▁ " + sumEven ( l , r ) ) ; } }

def sumNatural ( n ) : NEW_LINE INDENT sum = ( n * ( n + 1 ) ) NEW_LINE return int ( sum ) NEW_LINE DEDENT def sumEven ( l , r ) : NEW_LINE INDENT return ( sumNatural ( int ( r / 2 ) ) - sumNatural ( int ( ( l - 1 ) / 2 ) ) ) NEW_LINE DEDENT l , r = 2 , 5 NEW_LINE print ( " Sum ▁ of ▁ Natural ▁ numbers " , " from ▁ L ▁ to ▁ R ▁ is " , sumEven ( l , r ) ) NEW_LINE


import java . util . LinkedList ; import java . util . Queue ; import java . util . Arrays ; public class GFG { static int minimumBox ( int [ ] arr , int n ) { Queue < Integer > q = new LinkedList < > ( ) ; Arrays . sort ( arr ) ; q . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { int now = q . element ( ) ; if ( arr [ i ] >= 2 * now ) q . remove ( ) ; q . add ( arr [ i ] ) ; } return q . size ( ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 4 , 1 , 2 , 8 } ; int n = arr . length ; System . out . println ( minimumBox ( arr , n ) ) ; } }

import collections NEW_LINE def minimumBox ( arr , n ) : NEW_LINE INDENT q = collections . deque ( [ ] ) NEW_LINE arr . sort ( ) NEW_LINE q . append ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT now = q [ 0 ] NEW_LINE if ( arr [ i ] >= 2 * now ) : NEW_LINE INDENT q . popleft ( ) NEW_LINE DEDENT q . append ( arr [ i ] ) NEW_LINE DEDENT return len ( q ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 1 , 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimumBox ( arr , n ) ) NEW_LINE DEDENT


class GFG { static int findLength ( String str , int n ) { int sum [ ] = new int [ n + 1 ] ; sum [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum [ i ] = ( sum [ i - 1 ] + str . charAt ( i - 1 ) - '0' ) ; int ans = 0 ; for ( int len = 2 ; len <= n ; len += 2 ) { for ( int i = 0 ; i <= n - len ; i ++ ) { int j = i + len - 1 ; if ( sum [ i + len / 2 ] - sum [ i ] == sum [ i + len ] - sum [ i + len / 2 ] ) ans = Math . max ( ans , len ) ; } } return ans ; } public static void main ( String [ ] args ) { String str = "123123" ; System . out . println ( " Length ▁ of ▁ the ▁ substring ▁ is ▁ " + findLength ( str , str . length ( ) ) ) ; } }

def findLength ( string , n ) : NEW_LINE INDENT Sum = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT Sum [ i ] = ( Sum [ i - 1 ] + int ( string [ i - 1 ] ) ) NEW_LINE DEDENT ans = 0 NEW_LINE for length in range ( 2 , n + 1 , 2 ) : NEW_LINE INDENT for i in range ( 0 , n - length + 1 ) : NEW_LINE INDENT j = i + length - 1 NEW_LINE if ( Sum [ i + length // 2 ] - Sum [ i ] == Sum [ i + length ] - Sum [ i + length // 2 ] ) : NEW_LINE INDENT ans = max ( ans , length ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "123123" NEW_LINE print ( " Length ▁ of ▁ the ▁ substring ▁ is " , findLength ( string , len ( string ) ) ) NEW_LINE DEDENT


class GFG { static void kmax ( int arr [ ] , int k , int n ) { for ( int c = 0 ; c < k ; c ++ ) { int max_so_far = Integer . MIN_VALUE ; int max_here = 0 ; int start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_here += arr [ i ] ; if ( max_so_far < max_here ) { max_so_far = max_here ; start = s ; end = i ; } if ( max_here < 0 ) { max_here = 0 ; s = i + 1 ; } } System . out . println ( " Maximum ▁ non - overlapping ▁ sub - arraysum " + ( c + 1 ) + " : ▁ " + max_so_far + " , ▁ starting ▁ index : ▁ " + start + " , ▁ ending ▁ index : ▁ " + end + " . " ) ; for ( int l = start ; l <= end ; l ++ ) arr [ l ] = Integer . MIN_VALUE ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 4 , 1 , 1 , - 1 , - 3 , - 5 , 6 , 2 , - 6 , - 2 } ; int k1 = 3 ; int n1 = arr1 . length ; kmax ( arr1 , k1 , n1 ) ; int arr2 [ ] = { 5 , 1 , 2 , - 6 , 2 , - 1 , 3 , 1 } ; int k2 = 2 ; int n2 = arr2 . length ; kmax ( arr2 , k2 , n2 ) ; } }

def kmax ( arr , k , n ) : NEW_LINE INDENT for c in range ( k ) : NEW_LINE INDENT max_so_far = - float ( " inf " ) NEW_LINE max_here = 0 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE s = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_here += arr [ i ] NEW_LINE if ( max_so_far < max_here ) : NEW_LINE INDENT max_so_far = max_here NEW_LINE start = s NEW_LINE end = i NEW_LINE DEDENT if ( max_here < 0 ) : NEW_LINE INDENT max_here = 0 NEW_LINE s = i + 1 NEW_LINE DEDENT DEDENT print ( " Maximum ▁ non - overlapping ▁ sub - array ▁ sum " , c + 1 , " : ▁ " , max_so_far , " , ▁ starting ▁ index : ▁ " , start , " , ▁ ending ▁ index : ▁ " , end , " . " , sep = " " ) NEW_LINE for l in range ( start , end + 1 ) : NEW_LINE INDENT arr [ l ] = - float ( " inf " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT arr1 = [ 4 , 1 , 1 , - 1 , - 3 , - 5 , 6 , 2 , - 6 , - 2 ] NEW_LINE k1 = 3 NEW_LINE n1 = len ( arr1 ) NEW_LINE kmax ( arr1 , k1 , n1 ) NEW_LINE arr2 = [ 5 , 1 , 2 , - 6 , 2 , - 1 , 3 , 1 ] NEW_LINE k2 = 2 NEW_LINE n2 = len ( arr2 ) NEW_LINE kmax ( arr2 , k2 , n2 ) NEW_LINE


class GFG { static boolean isAMultipleOf4 ( int n ) { if ( ( n & 3 ) == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 16 ; System . out . println ( isAMultipleOf4 ( n ) ? " Yes " : " No " ) ; } }

def isAMultipleOf4 ( n ) : NEW_LINE INDENT if ( ( n & 3 ) == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 16 NEW_LINE print ( isAMultipleOf4 ( n ) ) NEW_LINE DEDENT


import java . io . * ; import java . math . * ; import java . text . * ; import java . util . * ; import java . util . regex . * ; class GFG { static int summingSeries ( long n ) { return ( int ) Math . pow ( n , 2 ) ; } public static void main ( String [ ] args ) { int n = 100 ; System . out . println ( " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " + summingSeries ( n ) ) ; } }

import math NEW_LINE def summingSeries ( n ) : NEW_LINE INDENT return math . pow ( n , 2 ) NEW_LINE DEDENT n = 100 NEW_LINE print ( " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " , summingSeries ( n ) ) NEW_LINE


public class GFG { public static int cubesum ( int n ) { return 2 * n * n * ( n + 1 ) * ( n + 1 ) ; } public static void main ( String args [ ] ) { int a = 8 ; System . out . println ( cubesum ( a ) ) ; } }

def cubeSum ( n ) : NEW_LINE INDENT return 2 * n * n * ( n + 1 ) * ( n + 1 ) NEW_LINE DEDENT print ( cubeSum ( 8 ) ) NEW_LINE


import java . io . * ; class GFG { static void printArray ( int [ ] A , int n , int K ) { int minEle = Integer . MAX_VALUE , maxEle = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { minEle = Math . min ( minEle , A [ i ] ) ; maxEle = Math . max ( maxEle , A [ i ] ) ; } if ( K != 0 ) { if ( K % 2 == 1 ) { for ( int i = 0 ; i < n ; i ++ ) A [ i ] = maxEle - A [ i ] ; } else { for ( int i = 0 ; i < n ; i ++ ) A [ i ] = A [ i ] - minEle ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( A [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 8 , 12 , 16 } ; int K = 4 ; int N = arr . length ; printArray ( arr , N , K ) ; } }

def printArray ( A , n , K ) : NEW_LINE INDENT minEle = 10 ** 9 NEW_LINE maxEle = - 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT minEle = min ( minEle , A [ i ] ) NEW_LINE maxEle = max ( maxEle , A [ i ] ) NEW_LINE DEDENT if ( K != 0 ) : NEW_LINE INDENT if ( K % 2 == 1 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT A [ i ] = maxEle - A [ i ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT A [ i ] = A [ i ] - minEle NEW_LINE DEDENT DEDENT DEDENT for i in A : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 8 , 12 , 16 ] NEW_LINE K = 4 NEW_LINE N = len ( arr ) NEW_LINE printArray ( arr , N , K ) NEW_LINE DEDENT


class GFG { static int max_size = 20 ; static int max_k = 20 ; static int [ ] [ ] dp = new int [ max_size ] [ max_k ] ; static boolean [ ] [ ] v = new boolean [ max_size ] [ max_k ] ; static int sum = 0 ; static void findSum ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; } static int cntWays ( int arr [ ] , int i , int ck , int k , int n , int curr_sum ) { if ( sum % k != 0 ) return 0 ; if ( i != n && ck == k + 1 ) return 0 ; if ( i == n ) { if ( ck == k + 1 ) return 1 ; else return 0 ; } if ( v [ i ] [ ck ] ) return dp [ i ] [ ck ] ; curr_sum += arr [ i ] ; v [ i ] [ ck ] = true ; dp [ i ] [ ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; if ( curr_sum == ( sum / k ) * ck ) dp [ i ] [ ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; return dp [ i ] [ ck ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , 1 , - 1 , 1 , - 1 } ; int n = arr . length ; int k = 2 ; findSum ( arr , n ) ; System . out . println ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ; } }

import numpy as np NEW_LINE max_size = 20 NEW_LINE max_k = 20 NEW_LINE dp = np . zeros ( ( max_size , max_k ) ) ; NEW_LINE v = np . zeros ( ( max_size , max_k ) ) ; NEW_LINE sum = 0 ; NEW_LINE def findSum ( arr , n ) : NEW_LINE INDENT global sum NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT DEDENT def cntWays ( arr , i , ck , k , n , curr_sum ) : NEW_LINE INDENT if ( sum % k != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i != n and ck == k + 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT if ( ck == k + 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if ( v [ i ] [ ck ] ) : NEW_LINE INDENT return dp [ i ] [ ck ] ; NEW_LINE DEDENT curr_sum += arr [ i ] ; NEW_LINE v [ i ] [ ck ] = 1 ; NEW_LINE dp [ i ] [ ck ] = cntWays ( arr , i + 1 , ck , k , n , curr_sum ) ; NEW_LINE if ( curr_sum == ( sum / k ) * ck ) : NEW_LINE INDENT dp [ i ] [ ck ] += cntWays ( arr , i + 1 , ck + 1 , k , n , curr_sum ) ; NEW_LINE DEDENT return dp [ i ] [ ck ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE findSum ( arr , n ) ; NEW_LINE print ( cntWays ( arr , 0 , 1 , k , n , 0 ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { public static int countP ( int n , int k ) { if ( n == 0 || k == 0 || k > n ) return 0 ; if ( k == 1 || k == n ) return 1 ; return ( k * countP ( n - 1 , k ) + countP ( n - 1 , k - 1 ) ) ; } public static void main ( String args [ ] ) { System . out . println ( countP ( 3 , 2 ) ) ; } }

def countP ( n , k ) : NEW_LINE INDENT if ( n == 0 or k == 0 or k > n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( k == 1 or k == n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return ( k * countP ( n - 1 , k ) + countP ( n - 1 , k - 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( countP ( 3 , 2 ) ) NEW_LINE DEDENT


class GFG { static boolean isPrime ( long n ) { if ( n < 2 ) return false ; for ( long i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static String reverse ( String s ) { String s1 = " " ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) s1 += s . charAt ( i ) ; return s1 ; } static long nextPrimePalindrome ( long N ) { for ( long k = 1 ; k < 1000000l ; k ++ ) { String s = " " + k ; String z ; z = reverse ( s ) ; long x = Long . parseLong ( s + z . substring ( 1 , z . length ( ) ) ) ; if ( x >= N && isPrime ( x ) ) return x ; s = " " + ( k ) ; z = s ; z = reverse ( z ) ; x = Long . parseLong ( s + z ) ; if ( x >= N && isPrime ( x ) ) return x ; } return - 1 ; } public static void main ( String args [ ] ) { long N = 7000000000l ; System . out . println ( nextPrimePalindrome ( N ) ) ; } }

import math NEW_LINE def is_prime ( n ) : NEW_LINE INDENT return n > 1 and all ( n % d for d in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) ) NEW_LINE DEDENT def NextprimePalindrome ( N ) : NEW_LINE INDENT for k in range ( 1 , 10 ** 6 ) : NEW_LINE INDENT s = str ( k ) NEW_LINE x = int ( s + s [ - 2 : : - 1 ] ) NEW_LINE if x >= N and is_prime ( x ) : NEW_LINE INDENT return x NEW_LINE DEDENT s = str ( k ) NEW_LINE x = int ( s + s [ - 1 : : - 1 ] ) NEW_LINE if x >= N and is_prime ( x ) : NEW_LINE INDENT return x NEW_LINE DEDENT DEDENT DEDENT N = 7000000000 NEW_LINE print ( NextprimePalindrome ( N ) ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static boolean isPerfectSquare ( int n ) { if ( n < 0 ) return false ; int root = Math . round ( ( int ) ( Math . sqrt ( n ) ) ) ; return n == root * root ; } static void printnonsquare ( int n ) { int count = 0 ; for ( int i = 1 ; count < n ; ++ i ) { if ( ! isPerfectSquare ( i ) ) { System . out . print ( i + " ▁ " ) ; count ++ ; } } } public static void main ( String args [ ] ) { int n = 10 ; printnonsquare ( n ) ; } }

import math NEW_LINE def isPerfectSquare ( n ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT root = round ( math . sqrt ( n ) ) NEW_LINE return ( n == root * root ) NEW_LINE DEDENT def printnonsquare ( n ) : NEW_LINE INDENT count = 0 NEW_LINE i = 1 NEW_LINE while ( count < n ) : NEW_LINE INDENT if ( isPerfectSquare ( i ) == False ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE count = count + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT DEDENT n = 10 NEW_LINE printnonsquare ( n ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static int findRepeatFirstN2 ( String s ) { int p = - 1 , i , j ; for ( i = 0 ; i < s . length ( ) ; i ++ ) { for ( j = i + 1 ; j < s . length ( ) ; j ++ ) { if ( s . charAt ( i ) == s . charAt ( j ) ) { p = i ; break ; } } if ( p != - 1 ) break ; } return p ; } static public void main ( String [ ] args ) { String str = " geeksforgeeks " ; int pos = findRepeatFirstN2 ( str ) ; if ( pos == - 1 ) System . out . println ( " Not ▁ found " ) ; else System . out . println ( str . charAt ( pos ) ) ; } }

def findRepeatFirstN2 ( s ) : NEW_LINE INDENT p = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == s [ j ] ) : NEW_LINE INDENT p = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( p != - 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return p NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE pos = findRepeatFirstN2 ( str ) NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT print ( " Not ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( str [ pos ] ) NEW_LINE DEDENT DEDENT


class GFG { static long countStrs ( int n ) { long [ ] [ ] dp = new long [ n + 1 ] [ 27 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < 27 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i <= 25 ; i ++ ) { dp [ 1 ] [ i ] = 1 ; } for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= 25 ; j ++ ) { if ( j == 0 ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; } else { dp [ i ] [ j ] = ( dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ) ; } } } long sum = 0 ; for ( int i = 0 ; i <= 25 ; i ++ ) { sum = ( sum + dp [ n ] [ i ] ) ; } return sum ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( " Total ▁ strings ▁ are ▁ : ▁ " + countStrs ( n ) ) ; } }

def countStrs ( n ) : NEW_LINE INDENT dp = [ [ 0 for j in range ( 27 ) ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( 0 , 26 ) : NEW_LINE INDENT dp [ 1 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 0 , 26 ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j + 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i - 1 ] [ j - 1 ] + dp [ i - 1 ] [ j + 1 ] ) NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE for i in range ( 0 , 26 ) : NEW_LINE INDENT sum = sum + dp [ n ] [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( " Total ▁ strings ▁ are ▁ : ▁ " , countStrs ( n ) ) NEW_LINE DEDENT


class GFG { static int maximumPoints ( int arr [ ] , int n , int k , int i ) { int start ; if ( k > i ) start = 0 ; else start = i - k ; int res = 0 ; for ( int j = start ; j <= start + k && j < n ; j ++ ) res += arr [ j ] ; int curr_sum = res ; for ( int j = start + k + 1 ; j < n && j <= i + k ; j ++ ) { curr_sum += arr [ j ] - arr [ j - k - 1 ] ; res = Math . max ( res , curr_sum ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 4 , 2 , 8 , 3 , 1 } ; int k = 3 , i = 3 ; int n = arr . length ; System . out . print ( " Maximum ▁ points ▁ : ▁ " + maximumPoints ( arr , n , k - 1 , i ) ) ; } }

def maximumPoints ( arr , n , k , i ) : NEW_LINE INDENT start = 0 NEW_LINE if ( k > i ) : NEW_LINE INDENT start = 0 NEW_LINE DEDENT else : NEW_LINE INDENT start = i - k NEW_LINE DEDENT res = 0 NEW_LINE j = start NEW_LINE while ( j <= start + k and j < n ) : NEW_LINE INDENT res += arr [ j ] NEW_LINE j += 1 NEW_LINE DEDENT curr_sum = res NEW_LINE j = start + k + 1 NEW_LINE while ( j < n and j <= i + k ) : NEW_LINE INDENT curr_sum += arr [ j ] - arr [ j - k - 1 ] NEW_LINE res = max ( res , curr_sum ) NEW_LINE j += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 5 , 6 , 4 , 2 , 8 , 3 , 1 ] NEW_LINE k , i = 3 , 3 NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ points ▁ : " , maximumPoints ( arr , n , k - 1 , i ) ) NEW_LINE


public class GFG { static int stringReduction ( String str ) { int n = str . length ( ) ; int count [ ] = new int [ 3 ] ; for ( int i = 0 ; i < n ; ++ i ) { count [ str . charAt ( i ) - ' a ' ] ++ ; } if ( count [ 0 ] == n || count [ 1 ] == n || count [ 2 ] == n ) { return n ; } if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) && ( count [ 1 ] % 2 ) == ( count [ 2 ] % 2 ) ) { return 2 ; } return 1 ; } public static void main ( String [ ] args ) { String str = " abcbbaacb " ; System . out . println ( stringReduction ( str ) ) ; } }

def stringReduction ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE count = [ 0 ] * 3 NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT if ( count [ 0 ] == n or count [ 1 ] == n or count [ 2 ] == n ) : NEW_LINE INDENT return n NEW_LINE DEDENT if ( ( count [ 0 ] % 2 ) == ( count [ 1 ] % 2 ) and ( count [ 1 ] % 2 ) == ( count [ 2 ] % 2 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT return 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " abcbbaacb " NEW_LINE print ( stringReduction ( str ) ) NEW_LINE DEDENT


class GFG { static int divisibleby37 ( String n1 ) { int l = n1 . length ( ) ; if ( n1 == "0" ) return 0 ; if ( l % 3 == 1 ) { n1 = "00" + n1 ; l += 2 ; } else if ( l % 3 == 2 ) { n1 = "0" + n1 ; l += 1 ; } char [ ] n = n1 . toCharArray ( ) ; int gSum = 0 ; while ( l != 0 ) { int gvalue ; if ( l == 2 ) gvalue = ( ( int ) n [ ( l - 2 ) ] - 48 ) * 100 + ( ( int ) n [ ( l - 1 ) ] - 48 ) * 10 ; else if ( l == 1 ) gvalue = ( ( int ) n [ ( l - 1 ) ] - 48 ) * 100 ; else gvalue = ( ( int ) n [ ( l - 3 ) ] - 48 ) * 100 + ( ( int ) n [ ( l - 2 ) ] - 48 ) * 10 + ( ( int ) n [ ( l - 1 ) ] - 48 ) * 1 ; l = l - 3 ; gSum = gSum + gvalue ; } if ( gSum >= 1000 ) return ( divisibleby37 ( String . valueOf ( gSum ) ) ) ; else return ( gSum % 37 == 0 ) ? 1 : 0 ; } public static void main ( String [ ] args ) { String s = "8955795758" ; if ( divisibleby37 ( s ) == 1 ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }

def divisibleby37 ( n ) : NEW_LINE INDENT l = len ( n ) NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( l % 3 == 1 ) : NEW_LINE INDENT n = "00" + n NEW_LINE l += 2 NEW_LINE DEDENT elif ( l % 3 == 2 ) : NEW_LINE INDENT n = "0" + n NEW_LINE l += 1 NEW_LINE DEDENT gSum = 0 NEW_LINE while ( l != 0 ) : NEW_LINE INDENT group = int ( n [ l - 3 : l ] ) NEW_LINE l = l - 3 NEW_LINE gSum = gSum + group NEW_LINE DEDENT if ( gSum >= 1000 ) : NEW_LINE INDENT return ( divisibleby37 ( str ( gSum ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( gSum % 37 == 0 ) NEW_LINE DEDENT DEDENT print ( divisibleby37 ( "8955795758" ) ) NEW_LINE


class GFG { static void candies ( int n , int k ) { int count = 0 ; int ind = 1 ; int [ ] arr = new int [ k ] ; for ( int i = 0 ; i < k ; i ++ ) arr [ i ] = 0 ; int low = 0 , high = n ; while ( low <= high ) { int mid = ( low + high ) >> 1 ; int sum = ( mid * ( mid + 1 ) ) >> 1 ; if ( sum <= n ) { count = mid / k ; low = mid + 1 ; } else { high = mid - 1 ; } } int last = ( count * k ) ; n -= ( last * ( last + 1 ) ) / 2 ; int j = 0 ; int term = ( count * k ) + 1 ; while ( n > 0 ) { if ( term <= n ) { arr [ j ++ ] = term ; n -= term ; term ++ ; } else { arr [ j ] += n ; n = 0 ; } } for ( int i = 0 ; i < k ; i ++ ) arr [ i ] += ( count * ( i + 1 ) ) + ( k * ( count * ( count - 1 ) ) / 2 ) ; for ( int i = 0 ; i < k ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 7 , k = 4 ; candies ( n , k ) ; } }

def candies ( n , k ) : NEW_LINE INDENT count = 0 ; NEW_LINE ind = 1 ; NEW_LINE arr = [ 0 ] * k ; NEW_LINE low = 0 ; NEW_LINE high = n ; NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) >> 1 ; NEW_LINE sum = ( mid * ( mid + 1 ) ) >> 1 ; NEW_LINE if ( sum <= n ) : NEW_LINE INDENT count = int ( mid / k ) ; NEW_LINE low = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 ; NEW_LINE DEDENT DEDENT last = ( count * k ) ; NEW_LINE n -= int ( ( last * ( last + 1 ) ) / 2 ) ; NEW_LINE i = 0 ; NEW_LINE term = ( count * k ) + 1 ; NEW_LINE while ( n ) : NEW_LINE INDENT if ( term <= n ) : NEW_LINE INDENT arr [ i ] = term ; NEW_LINE i += 1 ; NEW_LINE n -= term ; NEW_LINE term += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] += n ; NEW_LINE n = 0 ; NEW_LINE DEDENT DEDENT for i in range ( k ) : NEW_LINE INDENT arr [ i ] += ( ( count * ( i + 1 ) ) + int ( k * ( count * ( count - 1 ) ) / 2 ) ) ; NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT n = 7 ; NEW_LINE k = 4 ; NEW_LINE candies ( n , k ) ; NEW_LINE


import java . util . * ; class GFG { static int countSubsets ( int arr [ ] , int n ) { HashSet < Integer > us = new HashSet < > ( ) ; int even_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 0 ) us . add ( arr [ i ] ) ; even_count = us . size ( ) ; return ( int ) ( Math . pow ( 2 , even_count ) - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 } ; int n = arr . length ; System . out . println ( " Number ▁ of ▁ subsets ▁ = ▁ " + countSubsets ( arr , n ) ) ; } }

def countSubSets ( arr , n ) : NEW_LINE INDENT us = set ( ) NEW_LINE even_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] % 2 == 0 : NEW_LINE INDENT us . add ( arr [ i ] ) NEW_LINE DEDENT DEDENT even_count = len ( us ) NEW_LINE return pow ( 2 , even_count ) - 1 NEW_LINE DEDENT arr = [ 4 , 2 , 1 , 9 , 2 , 6 , 5 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Numbers ▁ of ▁ subset = " , countSubSets ( arr , n ) ) NEW_LINE


class GFG { static void printWords ( int h , int m ) { String nums [ ] = { " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " , " twenty " , " twenty ▁ one " , " twenty ▁ two " , " twenty ▁ three " , " twenty ▁ four " , " twenty ▁ five " , " twenty ▁ six " , " twenty ▁ seven " , " twenty ▁ eight " , " twenty ▁ nine " , } ; if ( m == 0 ) System . out . println ( nums [ h ] + " ▁ o ' ▁ clock ▁ " ) ; else if ( m == 1 ) System . out . println ( " one ▁ minute ▁ past ▁ " + nums [ h ] ) ; else if ( m == 59 ) System . out . println ( " one ▁ minute ▁ to ▁ " + nums [ ( h % 12 ) + 1 ] ) ; else if ( m == 15 ) System . out . println ( " quarter ▁ past ▁ " + nums [ h ] ) ; else if ( m == 30 ) System . out . println ( " half ▁ past ▁ " + nums [ h ] ) ; else if ( m == 45 ) System . out . println ( " quarter ▁ to ▁ " + nums [ ( h % 12 ) + 1 ] ) ; else if ( m <= 30 ) System . out . println ( nums [ m ] + " ▁ minutes ▁ past ▁ " + nums [ h ] ) ; else if ( m > 30 ) System . out . println ( nums [ 60 - m ] + " ▁ minutes ▁ to ▁ " + nums [ ( h % 12 ) + 1 ] ) ; } public static void main ( String [ ] args ) { int h = 6 ; int m = 24 ; printWords ( h , m ) ; } }

def printWords ( h , m ) : NEW_LINE INDENT nums = [ " zero " , " one " , " two " , " three " , " four " , " five " , " six " , " seven " , " eight " , " nine " , " ten " , " eleven " , " twelve " , " thirteen " , " fourteen " , " fifteen " , " sixteen " , " seventeen " , " eighteen " , " nineteen " , " twenty " , " twenty ▁ one " , " twenty ▁ two " , " twenty ▁ three " , " twenty ▁ four " , " twenty ▁ five " , " twenty ▁ six " , " twenty ▁ seven " , " twenty ▁ eight " , " twenty ▁ nine " ] ; NEW_LINE if ( m == 0 ) : NEW_LINE INDENT print ( nums [ h ] , " o ' ▁ clock " ) ; NEW_LINE DEDENT elif ( m == 1 ) : NEW_LINE INDENT print ( " one ▁ minute ▁ past " , nums [ h ] ) ; NEW_LINE DEDENT elif ( m == 59 ) : NEW_LINE INDENT print ( " one ▁ minute ▁ to " , nums [ ( h % 12 ) + 1 ] ) ; NEW_LINE DEDENT elif ( m == 15 ) : NEW_LINE INDENT print ( " quarter ▁ past " , nums [ h ] ) ; NEW_LINE DEDENT elif ( m == 30 ) : NEW_LINE INDENT print ( " half ▁ past " , nums [ h ] ) ; NEW_LINE DEDENT elif ( m == 45 ) : NEW_LINE INDENT print ( " quarter ▁ to " , ( nums [ ( h % 12 ) + 1 ] ) ) ; NEW_LINE DEDENT elif ( m <= 30 ) : NEW_LINE INDENT print ( nums [ m ] , " minutes ▁ past " , nums [ h ] ) ; NEW_LINE DEDENT elif ( m > 30 ) : NEW_LINE INDENT print ( nums [ 60 - m ] , " minutes ▁ to " , nums [ ( h % 12 ) + 1 ] ) ; NEW_LINE DEDENT DEDENT h = 6 ; NEW_LINE m = 24 ; NEW_LINE printWords ( h , m ) ; NEW_LINE


import java . util . * ; class GFG { public static int dp [ ] [ ] ; public static void computeDpArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 1 ; j <= 15000 ; ++ j ) { if ( i == 0 && j == arr [ i ] ) dp [ i ] [ j ] = 1 ; else if ( i == 0 ) dp [ i ] [ j ] = 0 ; else if ( arr [ i ] == j ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + 1 ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } } public static int countTripletSum ( int arr [ ] , int n ) { computeDpArray ( arr , n ) ; int ans = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { for ( int j = i + 1 ; j < n - 1 ; ++ j ) { for ( int k = 1 ; k <= 24 ; ++ k ) { int cube = k * k * k ; int rem = cube - ( arr [ i ] + arr [ j ] ) ; if ( rem > 0 ) ans += dp [ n - 1 ] [ rem ] - dp [ j ] [ rem ] ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 1 , 20 , 6 } ; int n = arr . length ; dp = new int [ 1001 ] [ 15001 ] ; System . out . println ( countTripletSum ( arr , n ) ) ; } }

dp = [ [ 0 for i in range ( 15001 ) ] for j in range ( 1001 ) ] NEW_LINE def computeDpArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( 1 , 15001 , 1 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = ( j == arr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + ( arr [ i ] == j ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def countTripletSum ( arr , n ) : NEW_LINE INDENT computeDpArray ( arr , n ) NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , n - 2 , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 , 1 ) : NEW_LINE INDENT for k in range ( 1 , 25 , 1 ) : NEW_LINE INDENT cube = k * k * k NEW_LINE rem = cube - ( arr [ i ] + arr [ j ] ) NEW_LINE if ( rem > 0 ) : NEW_LINE INDENT ans += dp [ n - 1 ] [ rem ] - dp [ j ] [ rem ] NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 1 , 20 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countTripletSum ( arr , n ) ) NEW_LINE DEDENT


public void InsertAfter ( Node prev_Node , int new_data ) { if ( prev_Node == null ) { System . out . println ( " The ▁ given ▁ previous ▁ node ▁ cannot ▁ be ▁ NULL ▁ " ) ; return ; } Node new_node = new Node ( new_data ) ; new_node . next = prev_Node . next ; prev_Node . next = new_node ; new_node . prev = prev_Node ; if ( new_node . next != null ) new_node . next . prev = new_node ; }

def insertAfter ( self , prev_node , new_data ) : NEW_LINE INDENT if prev_node is None : NEW_LINE INDENT print ( " This ▁ node ▁ doesn ' t ▁ exist ▁ in ▁ DLL " ) NEW_LINE return NEW_LINE DEDENT new_node = Node ( data = new_data ) NEW_LINE new_node . next = prev_node . next NEW_LINE prev_node . next = new_node NEW_LINE new_node . prev = prev_node NEW_LINE if new_node . next is not None : NEW_LINE INDENT new_node . next . prev = new_node NEW_LINE DEDENT DEDENT


class GFG { static void drawPattern ( int N ) { int n = N ; int row = 1 ; int nst = 1 ; int nsp1 = n - 1 ; int nsp2 = - 1 ; int val1 = row ; int val2 = 1 ; while ( row <= n ) { int csp1 = 1 ; while ( csp1 <= nsp1 ) { System . out . print ( " ▁ ▁ " ) ; csp1 = csp1 + 1 ; } int cst1 = 1 ; while ( cst1 <= nst ) { System . out . print ( val1 + " ▁ " ) ; val1 = val1 - 1 ; cst1 = cst1 + 1 ; } int csp2 = 1 ; while ( csp2 <= nsp2 ) { System . out . print ( " ▁ ▁ " ) ; csp2 = csp2 + 1 ; } if ( row != 1 && row != n ) { int cst2 = 1 ; while ( cst2 <= nst ) { System . out . print ( val2 + " ▁ " ) ; val2 = val2 + 1 ; cst2 = cst2 + 1 ; } } System . out . println ( ) ; if ( row <= n / 2 ) { nst = nst + 1 ; nsp1 = nsp1 - 2 ; nsp2 = nsp2 + 2 ; val1 = row + 1 ; val2 = 1 ; } else { nst = nst - 1 ; nsp1 = nsp1 + 2 ; nsp2 = nsp2 - 2 ; val1 = n - row ; val2 = 1 ; } row = row + 1 ; } } public static void main ( String args [ ] ) { int N = 7 ; drawPattern ( N ) ; } }

def drawPattern ( N ) : NEW_LINE INDENT n = N ; NEW_LINE row = 1 ; NEW_LINE nst = 1 ; NEW_LINE nsp1 = n - 1 ; NEW_LINE nsp2 = - 1 ; NEW_LINE val1 = row ; NEW_LINE val2 = 1 ; NEW_LINE while ( row <= n ) : NEW_LINE INDENT csp1 = 1 ; NEW_LINE while ( csp1 <= nsp1 ) : NEW_LINE INDENT print ( " ▁ " , end = " ▁ " ) ; NEW_LINE csp1 = csp1 + 1 ; NEW_LINE DEDENT cst1 = 1 ; NEW_LINE while ( cst1 <= nst ) : NEW_LINE INDENT print ( val1 , end = " ▁ " ) ; NEW_LINE val1 = val1 - 1 ; NEW_LINE cst1 = cst1 + 1 ; NEW_LINE DEDENT csp2 = 1 ; NEW_LINE while ( csp2 <= nsp2 ) : NEW_LINE INDENT print ( " ▁ " , end = " ▁ " ) ; NEW_LINE csp2 = csp2 + 1 ; NEW_LINE DEDENT if ( row != 1 and row != n ) : NEW_LINE INDENT cst2 = 1 ; NEW_LINE while ( cst2 <= nst ) : NEW_LINE INDENT print ( val2 , end = " ▁ " ) ; NEW_LINE val2 = val2 + 1 ; NEW_LINE cst2 = cst2 + 1 ; NEW_LINE DEDENT DEDENT print ( ) NEW_LINE if ( row <= n // 2 ) : NEW_LINE INDENT nst = nst + 1 ; NEW_LINE nsp1 = nsp1 - 2 ; NEW_LINE nsp2 = nsp2 + 2 ; NEW_LINE val1 = row + 1 ; NEW_LINE val2 = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT nst = nst - 1 ; NEW_LINE nsp1 = nsp1 + 2 ; NEW_LINE nsp2 = nsp2 - 2 ; NEW_LINE val1 = n - row ; NEW_LINE val2 = 1 ; NEW_LINE DEDENT row = row + 1 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 ; NEW_LINE drawPattern ( N ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { public static int atMostSum ( int arr [ ] , int n , int k ) { int sum = 0 ; int cnt = 0 , maxcnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( sum + arr [ i ] ) <= k ) { sum += arr [ i ] ; cnt ++ ; } else if ( sum != 0 ) { sum = sum - arr [ i - cnt ] + arr [ i ] ; } maxcnt = Math . max ( cnt , maxcnt ) ; } return maxcnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 0 , 1 , 1 , 0 } ; int n = arr . length ; int k = 4 ; System . out . print ( atMostSum ( arr , n , k ) ) ; } }

def atMostSum ( arr , n , k ) : NEW_LINE INDENT _sum = 0 NEW_LINE cnt = 0 NEW_LINE maxcnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( _sum + arr [ i ] ) <= k ) : NEW_LINE INDENT _sum += arr [ i ] NEW_LINE cnt += 1 NEW_LINE DEDENT elif ( sum != 0 ) : NEW_LINE INDENT _sum = _sum - arr [ i - cnt ] + arr [ i ] NEW_LINE DEDENT maxcnt = max ( cnt , maxcnt ) NEW_LINE DEDENT return maxcnt NEW_LINE DEDENT arr = [ 1 , 2 , 1 , 0 , 1 , 1 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE print ( atMostSum ( arr , n , k ) ) NEW_LINE


import java . io . * ; class FibZero { static int findMinZero ( int p ) { int first = 1 , second = 1 , number = 2 , next = 1 ; while ( next > 0 ) { next = ( first + second ) % p ; first = second ; second = next ; number ++ ; } return number ; } public static void main ( String [ ] args ) { int p = 7 ; System . out . println ( " Minimal ▁ zero ▁ is ▁ " + findMinZero ( p ) ) ; } }

def findMinZero ( p ) : NEW_LINE INDENT first = 1 NEW_LINE second = 1 NEW_LINE number = 2 NEW_LINE next = 1 NEW_LINE while ( next ) : NEW_LINE INDENT next = ( first + second ) % p NEW_LINE first = second NEW_LINE second = next NEW_LINE number = number + 1 NEW_LINE DEDENT return number NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = 7 NEW_LINE print ( " Minimal ▁ zero ▁ is : " , findMinZero ( p ) ) NEW_LINE DEDENT


import java . util . Arrays ; public class Main { public static char [ ] lexo_small ( int n , int k ) { char arr [ ] = new char [ n ] ; Arrays . fill ( arr , ' a ' ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { k -= i ; if ( k >= 0 ) { if ( k >= 26 ) { arr [ i ] = ' z ' ; k -= 26 ; } else { arr [ i ] = ( char ) ( k + 97 - 1 ) ; k -= arr [ i ] - ' a ' + 1 ; } } else break ; k += i ; } return arr ; } public static void main ( String [ ] args ) { int n = 5 , k = 42 ; char arr [ ] = lexo_small ( n , k ) ; System . out . print ( new String ( arr ) ) ; } }

def lexo_small ( n , k ) : NEW_LINE INDENT arr = " " ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr += ' a ' ; NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT k -= i ; NEW_LINE if ( k >= 0 ) : NEW_LINE INDENT if ( k >= 26 ) : NEW_LINE INDENT arr = arr [ : i ] + ' z ' + arr [ i + 1 : ] ; NEW_LINE k -= 26 ; NEW_LINE DEDENT else : NEW_LINE INDENT c = ( k + 97 - 1 ) ; NEW_LINE arr = arr [ : i ] + chr ( c ) + arr [ i + 1 : ] ; NEW_LINE k -= ord ( arr [ i ] ) - ord ( ' a ' ) + 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT k += i ; NEW_LINE DEDENT return arr ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; k = 42 ; NEW_LINE arr = lexo_small ( n , k ) ; NEW_LINE print ( arr ) ; NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int solveEven ( String s ) { if ( s . length ( ) % 2 == 1 ) { return 2 ; } String ls = s . substring ( 0 , s . length ( ) / 2 ) ; String rs = s . substring ( s . length ( ) / 2 , s . length ( ) ) ; if ( ls != rs ) { return 1 ; } return solveEven ( ls ) ; } static int solveOdd ( String s ) { return 2 ; } static int solve ( String s ) { if ( s . length ( ) <= 3 ) { return - 1 ; } int cnt [ ] = new int [ 25 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] ++ ; } if ( Arrays . stream ( cnt ) . max ( ) . getAsInt ( ) >= s . length ( ) - 1 ) { return - 1 ; } if ( s . length ( ) % 2 == 0 ) { return solveEven ( s ) ; } if ( s . length ( ) % 2 == 1 ) { return solveOdd ( s ) ; } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { String s = " nolon " ; System . out . println ( solve ( s ) ) ; } }

def solveEven ( s ) : NEW_LINE INDENT if len ( s ) % 2 == 1 : NEW_LINE INDENT return 2 NEW_LINE DEDENT ls = s [ 0 : len ( s ) // 2 ] NEW_LINE rs = s [ len ( s ) // 2 : len ( s ) ] NEW_LINE if ls != rs : NEW_LINE INDENT return 1 NEW_LINE DEDENT return solveEven ( ls ) NEW_LINE DEDENT def solveOdd ( s ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT def solve ( s ) : NEW_LINE INDENT if len ( s ) <= 3 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT cnt = [ 0 ] * 25 NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT cnt [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT if max ( cnt ) >= len ( s ) - 1 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if len ( s ) % 2 == 0 : NEW_LINE INDENT return solveEven ( s ) NEW_LINE DEDENT if len ( s ) % 2 == 1 : NEW_LINE INDENT return solveOdd ( s ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " nolon " NEW_LINE print ( solve ( s ) ) NEW_LINE DEDENT


class GFG { static boolean isHeap ( int arr [ ] , int n ) { for ( int i = 0 ; i <= ( n - 2 ) / 2 ; i ++ ) { if ( arr [ 2 * i + 1 ] > arr [ i ] ) { return false ; } if ( 2 * i + 2 < n && arr [ 2 * i + 2 ] > arr [ i ] ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 } ; int n = arr . length ; if ( isHeap ( arr , n ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }

def isHeap ( arr , n ) : NEW_LINE INDENT for i in range ( int ( ( n - 2 ) / 2 ) + 1 ) : NEW_LINE INDENT if arr [ 2 * i + 1 ] > arr [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT if ( 2 * i + 2 < n and arr [ 2 * i + 2 ] > arr [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 90 , 15 , 10 , 7 , 12 , 2 , 7 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE if isHeap ( arr , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . HashMap ; import java . util . Vector ; class GFG { static String isFibonacci ( String s ) { HashMap < Character , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) m . put ( s . charAt ( i ) , m . get ( s . charAt ( i ) ) == null ? 1 : m . get ( s . charAt ( i ) ) + 1 ) ; Vector < Integer > v = new Vector < > ( ) ; int n = m . size ( ) ; int a = 1 , b = 1 ; int c ; v . add ( a ) ; v . add ( b ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { v . add ( a + b ) ; c = a + b ; a = b ; b = c ; } int flag = 1 ; int i = 0 ; for ( HashMap . Entry < Character , Integer > entry : m . entrySet ( ) ) { if ( entry . getValue ( ) != v . elementAt ( i ) ) { flag = 1 ; break ; } i ++ ; } if ( flag == 1 ) return " YES " ; else return " NO " ; } public static void main ( String [ ] args ) { String s = " abeeedd " ; System . out . println ( isFibonacci ( s ) ) ; } }

from collections import defaultdict NEW_LINE def isFibonacci ( s ) : NEW_LINE INDENT m = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE DEDENT v = [ ] NEW_LINE n = len ( m ) NEW_LINE a = b = 1 NEW_LINE v . append ( a ) NEW_LINE v . append ( b ) NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT v . append ( a + b ) NEW_LINE c = a + b NEW_LINE a , b = b , c NEW_LINE DEDENT flag , i = 1 , 0 NEW_LINE for itr in sorted ( m ) : NEW_LINE INDENT if m [ itr ] != v [ i ] : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if flag == 1 : NEW_LINE INDENT return " YES " NEW_LINE DEDENT else : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abeeedd " NEW_LINE print ( isFibonacci ( s ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int recamanGenerator ( int arr [ ] , int n ) { arr [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == temp ) || temp < 0 ) { temp = arr [ i - 1 ] + i ; break ; } } arr [ i ] = temp ; } return 0 ; } static int recamanArray ( int arr [ ] , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; int recaman [ ] = new int [ n + 1 ] ; recamanGenerator ( recaman , n ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( recaman [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) s . remove ( arr [ i ] ) ; } return s . size ( ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 } ; int n = arr . length ; System . out . print ( recamanArray ( arr , n ) ) ; } }

def recamanGenerator ( arr , n ) : NEW_LINE INDENT arr [ 0 ] = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT temp = arr [ i - 1 ] - i NEW_LINE j = 0 NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( ( arr [ j ] == temp ) or temp < 0 ) : NEW_LINE INDENT temp = arr [ i - 1 ] + i NEW_LINE break NEW_LINE DEDENT DEDENT arr [ i ] = temp NEW_LINE DEDENT DEDENT def recamanArray ( arr , n ) : NEW_LINE INDENT s = dict ( ) NEW_LINE recaman = [ 0 for i in range ( n ) ] NEW_LINE recamanGenerator ( recaman , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s [ recaman [ i ] ] = s . get ( recaman [ i ] , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in s . keys ( ) : NEW_LINE INDENT del s [ arr [ i ] ] NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT arr = [ 7 , 11 , 20 , 4 , 2 , 1 , 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( recamanArray ( arr , n ) ) NEW_LINE


class MaxSum { static int maxSumLCM ( int n ) { int max_sum = 0 ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { max_sum += i ; if ( n / i != i ) max_sum += ( n / i ) ; } } return max_sum ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( maxSumLCM ( n ) ) ; } }

def maxSumLCM ( n ) : NEW_LINE INDENT max_sum = 0 NEW_LINE i = 1 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT max_sum = max_sum + i NEW_LINE if ( n // i != i ) : NEW_LINE INDENT max_sum = max_sum + ( n // i ) NEW_LINE DEDENT DEDENT i = i + 1 NEW_LINE DEDENT return max_sum NEW_LINE DEDENT n = 2 NEW_LINE print ( maxSumLCM ( n ) ) NEW_LINE


import java . io . * ; import java . util . * ; public class GFG { static boolean isPalindrome ( String s ) { String s1 = " " ; int N = s . length ( ) ; for ( int i = N - 1 ; i >= 0 ; i -- ) s1 += s . charAt ( i ) ; if ( s . equals ( s1 ) ) return true ; return false ; } static boolean createString ( int N ) { String str = " " ; String s = " " + N ; String letters = " abcdefghij " ; int sum = 0 ; String substr = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s . charAt ( i ) - '0' ; substr += letters . charAt ( digit ) ; sum += digit ; } while ( str . length ( ) <= sum ) { str += substr ; } str = str . substring ( 0 , sum ) ; return isPalindrome ( str ) ; } public static void main ( String args [ ] ) { int N = 61 ; boolean flag = createString ( N ) ; if ( flag ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def isPalindrome ( s ) : NEW_LINE INDENT s1 = " " NEW_LINE N = len ( s ) NEW_LINE i = ( N - 1 ) NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT s1 += s [ i ] NEW_LINE i = i - 1 NEW_LINE DEDENT if ( s == s1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def createString ( N ) : NEW_LINE INDENT s2 = " " NEW_LINE s = str ( N ) NEW_LINE letters = " abcdefghij " NEW_LINE sum = 0 NEW_LINE substr = " " NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT digit = int ( s [ i ] ) NEW_LINE substr += letters [ digit ] NEW_LINE sum += digit NEW_LINE DEDENT while ( len ( s2 ) <= sum ) : NEW_LINE INDENT s2 += substr NEW_LINE DEDENT s2 = s2 [ : sum ] NEW_LINE return isPalindrome ( s2 ) NEW_LINE DEDENT N = 61 ; NEW_LINE flag = createString ( N ) NEW_LINE if ( flag ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void deleteElements ( int arr [ ] , int n , int k ) { Stack < Integer > s = new Stack < > ( ) ; s . push ( arr [ 0 ] ) ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) { while ( ! s . empty ( ) && s . peek ( ) < arr [ i ] && count < k ) { s . pop ( ) ; count ++ ; } s . push ( arr [ i ] ) ; } int m = s . size ( ) ; Integer [ ] v = new Integer [ m ] ; while ( ! s . empty ( ) ) { v [ -- m ] = s . peek ( ) ; s . pop ( ) ; } for ( Integer x : v ) { System . out . print ( x + " ▁ " ) ; } ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { int n = 5 , k = 2 ; int arr [ ] = { 20 , 10 , 25 , 30 , 40 } ; deleteElements ( arr , n , k ) ; } }

def deleteElements ( arr , n , k ) : NEW_LINE INDENT st = [ ] NEW_LINE st . append ( arr [ 0 ] ) NEW_LINE top = 0 NEW_LINE count = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT while ( len ( st ) != 0 and count < k and st [ top ] < arr [ i ] ) : NEW_LINE INDENT st . pop ( ) NEW_LINE count += 1 NEW_LINE top -= 1 NEW_LINE DEDENT st . append ( arr [ i ] ) NEW_LINE top += 1 NEW_LINE DEDENT for i in range ( 0 , len ( st ) ) : NEW_LINE INDENT print ( st [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT k = 2 NEW_LINE arr = [ 20 , 10 , 25 , 30 , 40 ] NEW_LINE deleteElements ( arr , len ( arr ) , k ) NEW_LINE


class GFG { public static int numLen ( int K ) { if ( K % 2 == 0 || K % 5 == 0 ) return - 1 ; int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = ( number * 10 + 1 ) % K ; if ( number == 0 ) return len ; } return - 1 ; } public static void main ( String [ ] args ) { int K = 7 ; System . out . print ( numLen ( K ) ) ; } }

def numLen ( K ) : NEW_LINE INDENT if ( K % 2 == 0 or K % 5 == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT number = 0 NEW_LINE len = 1 NEW_LINE for len in range ( 1 , K + 1 ) : NEW_LINE INDENT number = ( number * 10 + 1 ) % K NEW_LINE if number == 0 : NEW_LINE INDENT return len NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT K = 7 NEW_LINE print ( numLen ( K ) ) NEW_LINE


class GFG { static int C_MAX = 30 ; static int max_arr_len = 10 ; static int dp [ ] [ ] = new int [ max_arr_len ] [ C_MAX + 1 ] ; static boolean v [ ] [ ] = new boolean [ max_arr_len ] [ C_MAX + 1 ] ; static int findMax ( int i , int r , int w [ ] , int n ) { if ( r < 0 ) return Integer . MIN_VALUE ; if ( i == n ) return 0 ; if ( v [ i ] [ r ] ) return dp [ i ] [ r ] ; v [ i ] [ r ] = true ; dp [ i ] [ r ] = Math . max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; return dp [ i ] [ r ] ; } static void preCompute ( int w [ ] , int n ) { for ( int i = C_MAX ; i >= 0 ; i -- ) findMax ( 0 , i , w , n ) ; } static int ansQuery ( int w ) { return dp [ 0 ] [ w ] ; } public static void main ( String [ ] args ) { int w [ ] = new int [ ] { 3 , 8 , 9 } ; int n = w . length ; preCompute ( w , n ) ; int queries [ ] = new int [ ] { 11 , 10 , 4 } ; int q = queries . length ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( ansQuery ( queries [ i ] ) ) ; } }

import numpy as np NEW_LINE import sys NEW_LINE C_MAX = 30 NEW_LINE max_arr_len = 10 NEW_LINE dp = np . zeros ( ( max_arr_len , C_MAX + 1 ) ) ; NEW_LINE v = np . zeros ( ( max_arr_len , C_MAX + 1 ) ) ; NEW_LINE INT_MIN = - ( sys . maxsize ) + 1 NEW_LINE def findMax ( i , r , w , n ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return INT_MIN ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( v [ i ] [ r ] ) : NEW_LINE INDENT return dp [ i ] [ r ] ; NEW_LINE DEDENT v [ i ] [ r ] = 1 ; NEW_LINE dp [ i ] [ r ] = max ( w [ i ] + findMax ( i + 1 , r - w [ i ] , w , n ) , findMax ( i + 1 , r , w , n ) ) ; NEW_LINE return dp [ i ] [ r ] ; NEW_LINE DEDENT def preCompute ( w , n ) : NEW_LINE INDENT for i in range ( C_MAX , - 1 , - 1 ) : NEW_LINE INDENT findMax ( 0 , i , w , n ) ; NEW_LINE DEDENT DEDENT def ansQuery ( w ) : NEW_LINE INDENT return dp [ 0 ] [ w ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT w = [ 3 , 8 , 9 ] ; NEW_LINE n = len ( w ) NEW_LINE preCompute ( w , n ) ; NEW_LINE queries = [ 11 , 10 , 4 ] ; NEW_LINE q = len ( queries ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( ansQuery ( queries [ i ] ) ) ; NEW_LINE DEDENT DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static long findWays ( int m , int n , int x ) { long [ ] [ ] table = new long [ n + 1 ] [ x + 1 ] ; for ( int j = 1 ; j <= m && j <= x ; j ++ ) table [ 1 ] [ j ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= x ; j ++ ) { for ( int k = 1 ; k < j && k <= m ; k ++ ) table [ i ] [ j ] += table [ i - 1 ] [ j - k ] ; } } return table [ n ] [ x ] ; } public static void main ( String [ ] args ) { System . out . println ( findWays ( 4 , 2 , 1 ) ) ; System . out . println ( findWays ( 2 , 2 , 3 ) ) ; System . out . println ( findWays ( 6 , 3 , 8 ) ) ; System . out . println ( findWays ( 4 , 2 , 5 ) ) ; System . out . println ( findWays ( 4 , 3 , 5 ) ) ; } }

def findWays ( m , n , x ) : NEW_LINE INDENT table = [ [ 0 ] * ( x + 1 ) for i in range ( n + 1 ) ] NEW_LINE for j in range ( 1 , min ( m + 1 , x + 1 ) ) : NEW_LINE INDENT table [ 1 ] [ j ] = 1 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , x + 1 ) : NEW_LINE INDENT for k in range ( 1 , min ( m + 1 , j ) ) : NEW_LINE INDENT table [ i ] [ j ] += table [ i - 1 ] [ j - k ] NEW_LINE DEDENT DEDENT DEDENT return table [ - 1 ] [ - 1 ] NEW_LINE DEDENT print ( findWays ( 4 , 2 , 1 ) ) NEW_LINE print ( findWays ( 2 , 2 , 3 ) ) NEW_LINE print ( findWays ( 6 , 3 , 8 ) ) NEW_LINE print ( findWays ( 4 , 2 , 5 ) ) NEW_LINE print ( findWays ( 4 , 3 , 5 ) ) NEW_LINE


class GFG { static int toggle ( int n ) { int temp = 1 ; while ( temp <= n ) { n = n ^ temp ; temp = temp << 1 ; } return n ; } public static void main ( String arg [ ] ) { int n = 10 ; n = toggle ( n ) ; System . out . print ( n ) ; } }

def toggle ( n ) : NEW_LINE INDENT temp = 1 NEW_LINE while ( temp <= n ) : NEW_LINE INDENT n = n ^ temp NEW_LINE temp = temp << 1 NEW_LINE DEDENT return n NEW_LINE DEDENT n = 10 NEW_LINE n = toggle ( n ) NEW_LINE print ( n ) NEW_LINE


class GFG { static void sieve ( int maxm , int prime [ ] ) { prime [ 0 ] = prime [ 1 ] = 1 ; for ( int i = 2 ; i * i <= maxm ; i ++ ) if ( prime [ i ] == 0 ) for ( int j = 2 * i ; j <= maxm ; j += i ) prime [ j ] = 1 ; } static int countPair ( int a [ ] , int n ) { int maxm = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) if ( a [ i ] > maxm ) maxm = a [ i ] ; int [ ] prime = new int [ maxm + 1 ] ; for ( int i = 0 ; i < maxm + 1 ; i ++ ) prime [ i ] = 0 ; sieve ( maxm , prime ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( prime [ a [ i ] ] == 0 || prime [ a [ j ] ] == 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 4 , 7 } ; int n = arr . length ; System . out . println ( countPair ( arr , n ) ) ; } }

from math import sqrt NEW_LINE def countPair ( a , n ) : NEW_LINE INDENT maxm = a [ 0 ] NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT if ( a [ i ] > maxm ) : NEW_LINE INDENT maxm = a [ i ] NEW_LINE DEDENT DEDENT prime = [ 0 for i in range ( maxm + 1 ) ] NEW_LINE prime [ 0 ] = prime [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , int ( sqrt ( maxm ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ i ] == 0 ) : NEW_LINE INDENT for j in range ( 2 * i , maxm + 1 , i ) : NEW_LINE INDENT prime [ j ] = 1 NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT if ( prime [ a [ i ] ] == 0 or prime [ a [ j ] ] == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 4 , 7 ] NEW_LINE n = 5 NEW_LINE print ( countPair ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; public class GFG { static int MAX_CHAR = 256 ; static int maximumChars ( String str ) { int n = str . length ( ) ; int res = - 1 ; int [ ] firstInd = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) firstInd [ i ] = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int first_ind = firstInd [ str . charAt ( i ) ] ; if ( first_ind == - 1 ) firstInd [ str . charAt ( i ) ] = i ; else res = Math . max ( res , Math . abs ( i - first_ind - 1 ) ) ; } return res ; } static public void main ( String [ ] args ) { String str = " abba " ; System . out . println ( maximumChars ( str ) ) ; } }

MAX_CHAR = 256 NEW_LINE def maximumChars ( str1 ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE res = - 1 NEW_LINE firstInd = [ - 1 for i in range ( MAX_CHAR ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT first_ind = firstInd [ ord ( str1 [ i ] ) ] NEW_LINE if ( first_ind == - 1 ) : NEW_LINE INDENT firstInd [ ord ( str1 [ i ] ) ] = i NEW_LINE DEDENT else : NEW_LINE INDENT res = max ( res , abs ( i - first_ind - 1 ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT str1 = " abba " NEW_LINE print ( maximumChars ( str1 ) ) NEW_LINE


class GFG { final int MAX_CHAR = 26 ; static void frequency ( int fre [ ] [ ] , String s [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { String str = s [ i ] ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) fre [ i ] [ str . charAt ( j ) - ' a ' ] ++ ; } } static void LongestSequence ( int fre [ ] [ ] , int n ) { for ( int i = 24 ; i >= 0 ; i -- ) { int mi = fre [ 0 ] [ i ] ; for ( int j = 1 ; j < n ; j ++ ) mi = Math . min ( fre [ j ] [ i ] , mi ) ; while ( mi -- != 0 ) System . out . print ( ( char ) ( ' a ' + i ) ) ; } } public static void main ( String args [ ] ) { String s [ ] = { " loo " , " lol " , " olive " } ; int n = s . length ; int fre [ ] [ ] = new int [ n ] [ 26 ] ; frequency ( fre , s , n ) ; LongestSequence ( fre , n ) ; } }

def frequency ( fre , s , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT string = s [ i ] NEW_LINE for j in range ( 0 , len ( string ) ) : NEW_LINE INDENT fre [ i ] [ ord ( string [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT DEDENT def LongestSequence ( fre , n ) : NEW_LINE INDENT for i in range ( MAX_CHAR - 1 , - 1 , - 1 ) : NEW_LINE INDENT mi = fre [ 0 ] [ i ] NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT mi = min ( fre [ j ] [ i ] , mi ) NEW_LINE DEDENT while mi : NEW_LINE INDENT print ( chr ( ord ( ' a ' ) + i ) , end = " " ) NEW_LINE mi -= 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = [ " loo " , " lol " , " olive " ] NEW_LINE n = len ( s ) NEW_LINE MAX_CHAR = 26 NEW_LINE fre = [ [ 0 for i in range ( 26 ) ] for j in range ( n ) ] NEW_LINE frequency ( fre , s , n ) NEW_LINE LongestSequence ( fre , n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int NumberOfSolutions ( int x , int y , int z , int n ) { int ans = 0 ; for ( int i = 0 ; i <= x ; i ++ ) { for ( int j = 0 ; j <= y ; j ++ ) { int temp = n - i - j ; if ( temp >= 0 ) { temp = Math . min ( temp , z ) ; ans += temp + 1 ; } } } return ans ; } public static void main ( String [ ] args ) { int x = 1 , y = 2 , z = 3 , n = 4 ; System . out . println ( NumberOfSolutions ( x , y , z , n ) ) ; } }

def NumberOfSolutions ( x , y , z , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( x + 1 ) : NEW_LINE INDENT for j in range ( y + 1 ) : NEW_LINE INDENT temp = n - i - j NEW_LINE if temp >= 0 : NEW_LINE INDENT temp = min ( temp , z ) NEW_LINE ans += temp + 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y , z , n = 1 , 2 , 3 , 4 NEW_LINE print ( NumberOfSolutions ( x , y , z , n ) ) NEW_LINE DEDENT


import java . util . * ; class solution { boolean compare ( int a , int b ) { return a > b ; } static int findMaxOR ( int [ ] arr , int n ) { int temp ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } int maxOR = arr [ 0 ] ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( ( maxOR | arr [ i ] ) > maxOR ) { maxOR = maxOR | arr [ i ] ; count ++ ; } } return count ; } public static void main ( String arrgs [ ] ) { int [ ] arr = { 5 , 1 , 3 , 4 , 2 } ; int n = arr . length ; System . out . println ( findMaxOR ( arr , n ) ) ; } }

def findMaxOR ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = True ) NEW_LINE maxOR = arr [ 0 ] NEW_LINE count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( ( maxOR | arr [ i ] ) > maxOR ) : NEW_LINE INDENT maxOR = maxOR | arr [ i ] ; NEW_LINE count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 1 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxOR ( arr , n ) ) NEW_LINE DEDENT


class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static int maximizeSum ( int a [ ] , int n ) { int cnt = 0 ; int mini = Integer . MAX_VALUE ; int minSubarray = Integer . MAX_VALUE ; int sum = maxSubArraySum ( a , n ) ; int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; cnt ++ ; minSubarray = Math . min ( a [ i ] , minSubarray ) ; if ( sum == max_ending_here ) { if ( cnt == 1 ) mini = Math . min ( mini , 0 ) ; else mini = Math . min ( mini , minSubarray ) ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; cnt = 0 ; minSubarray = Integer . MAX_VALUE ; } } return sum - mini ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , - 2 , 3 } ; int n = a . length ; System . out . println ( maximizeSum ( a , n ) ) ; } }

import sys ; NEW_LINE def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = - ( sys . maxsize - 1 ) ; NEW_LINE max_ending_here = 0 ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] ; NEW_LINE if ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here ; NEW_LINE DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 ; NEW_LINE DEDENT DEDENT return max_so_far ; NEW_LINE DEDENT def maximizeSum ( a , n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE mini = sys . maxsize ; NEW_LINE minSubarray = sys . maxsize ; NEW_LINE sum = maxSubArraySum ( a , n ) ; NEW_LINE max_so_far = - ( sys . maxsize - 1 ) ; NEW_LINE max_ending_here = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] ; NEW_LINE cnt += 1 ; NEW_LINE minSubarray = min ( a [ i ] , minSubarray ) ; NEW_LINE if ( sum == max_ending_here ) : NEW_LINE INDENT if ( cnt == 1 ) : NEW_LINE INDENT mini = min ( mini , 0 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT mini = min ( mini , minSubarray ) ; NEW_LINE DEDENT DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 ; NEW_LINE cnt = 0 ; NEW_LINE minSubarray = sys . maxsize ; NEW_LINE DEDENT DEDENT return sum - mini ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 , - 2 , 3 ] ; NEW_LINE n = len ( a ) NEW_LINE print ( maximizeSum ( a , n ) ) ; NEW_LINE DEDENT


class GFG { static int findSum ( int [ ] arr , int n , int k ) { int ans = arr [ n - k - 1 ] - arr [ 0 ] ; for ( int i = 1 ; i <= k ; i ++ ) { ans = Math . min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . length ; System . out . println ( findSum ( arr , n , k ) ) ; } }

def findSum ( arr , n , k ) : NEW_LINE INDENT ans = arr [ n - k - 1 ] - arr [ 0 ] ; NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT ans = min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 100 , 120 , 140 ] ; NEW_LINE k = 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findSum ( arr , n , k ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void longest ( int a [ ] , int n , int k ) { int [ ] freq = new int [ 7 ] ; int start = 0 , end = 0 , now = 0 , l = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; if ( freq [ a [ i ] ] == 1 ) now ++ ; while ( now > k ) { freq [ a [ l ] ] -- ; if ( freq [ a [ l ] ] == 0 ) now -- ; l ++ ; } if ( i - l + 1 >= end - start + 1 ) { end = i ; start = l ; } } for ( int i = start ; i <= end ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 6 , 5 , 1 , 2 , 3 , 2 , 1 , 4 , 5 } ; int n = a . length ; int k = 3 ; longest ( a , n , k ) ; } }

def longest ( a , n , k ) : NEW_LINE INDENT freq = [ 0 ] * n NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE now = 0 NEW_LINE l = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE if ( freq [ a [ i ] ] == 1 ) : NEW_LINE INDENT now += 1 NEW_LINE DEDENT while ( now > k ) : NEW_LINE INDENT freq [ a [ l ] ] -= 1 NEW_LINE if ( freq [ a [ l ] ] == 0 ) : NEW_LINE INDENT now -= 1 NEW_LINE DEDENT l += 1 NEW_LINE DEDENT if ( i - l + 1 >= end - start + 1 ) : NEW_LINE INDENT end = i NEW_LINE start = l NEW_LINE DEDENT DEDENT for i in range ( start , end + 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 6 , 5 , 1 , 2 , 3 , 2 , 1 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE longest ( a , n , k ) NEW_LINE DEDENT


class GFG { public static void printMultiples ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { System . out . print ( ( i * j ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int N = 5 ; printMultiples ( N ) ; } }

def prMultiples ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( ( i * j ) , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE prMultiples ( N ) NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int maxEvenLenSum ( int arr [ ] , int n ) { if ( n < 2 ) return 0 ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = arr [ n - 2 ] + arr [ n - 1 ] ; for ( int i = n - 3 ; i >= 0 ; i -- ) { dp [ i ] = arr [ i ] + arr [ i + 1 ] ; if ( dp [ i + 2 ] > 0 ) dp [ i ] += dp [ i + 2 ] ; } int maxSum = Arrays . stream ( dp ) . max ( ) . getAsInt ( ) ; return maxSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 9 , - 8 , 9 , 10 } ; int n = arr . length ; System . out . println ( maxEvenLenSum ( arr , n ) ) ; } }

def maxEvenLenSum ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT dp = [ 0 for i in range ( n ) ] NEW_LINE dp [ n - 1 ] = 0 NEW_LINE dp [ n - 2 ] = arr [ n - 2 ] + arr [ n - 1 ] NEW_LINE for i in range ( n - 3 , - 1 , - 1 ) : NEW_LINE INDENT dp [ i ] = arr [ i ] + arr [ i + 1 ] NEW_LINE if ( dp [ i + 2 ] > 0 ) : NEW_LINE INDENT dp [ i ] += dp [ i + 2 ] NEW_LINE DEDENT DEDENT maxSum = max ( dp ) NEW_LINE return maxSum NEW_LINE DEDENT arr = [ 8 , 9 , - 8 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxEvenLenSum ( arr , n ) ) NEW_LINE


import java . util . HashMap ; class Test { static int countDistictSubarray ( int arr [ ] , int n ) { HashMap < Integer , Integer > vis = new HashMap < Integer , Integer > ( ) { @ Override public Integer get ( Object key ) { if ( ! containsKey ( key ) ) return 0 ; return super . get ( key ) ; } } ; for ( int i = 0 ; i < n ; ++ i ) vis . put ( arr [ i ] , 1 ) ; int k = vis . size ( ) ; vis . clear ( ) ; int ans = 0 , right = 0 , window = 0 ; for ( int left = 0 ; left < n ; ++ left ) { while ( right < n && window < k ) { vis . put ( arr [ right ] , vis . get ( arr [ right ] ) + 1 ) ; if ( vis . get ( arr [ right ] ) == 1 ) ++ window ; ++ right ; } if ( window == k ) ans += ( n - right + 1 ) ; vis . put ( arr [ left ] , vis . get ( arr [ left ] ) - 1 ) ; if ( vis . get ( arr [ left ] ) == 0 ) -- window ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 3 , 2 , 3 } ; System . out . println ( countDistictSubarray ( arr , arr . length ) ) ; } }

def countDistictSubarray ( arr , n ) : NEW_LINE INDENT vis = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT vis [ arr [ i ] ] = 1 NEW_LINE DEDENT k = len ( vis ) NEW_LINE vid = dict ( ) NEW_LINE ans = 0 NEW_LINE right = 0 NEW_LINE window = 0 NEW_LINE for left in range ( n ) : NEW_LINE INDENT while ( right < n and window < k ) : NEW_LINE INDENT if arr [ right ] in vid . keys ( ) : NEW_LINE INDENT vid [ arr [ right ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT vid [ arr [ right ] ] = 1 NEW_LINE DEDENT if ( vid [ arr [ right ] ] == 1 ) : NEW_LINE INDENT window += 1 NEW_LINE DEDENT right += 1 NEW_LINE DEDENT if ( window == k ) : NEW_LINE INDENT ans += ( n - right + 1 ) NEW_LINE DEDENT vid [ arr [ left ] ] -= 1 NEW_LINE if ( vid [ arr [ left ] ] == 0 ) : NEW_LINE INDENT window -= 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 1 , 3 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countDistictSubarray ( arr , n ) ) NEW_LINE


class GFG { static int nth_term ( int a , int b , int n ) { int z = 0 ; if ( n % 6 == 1 ) z = a ; else if ( n % 6 == 2 ) z = b ; else if ( n % 6 == 3 ) z = b - a ; else if ( n % 6 == 4 ) z = - a ; else if ( n % 6 == 5 ) z = - b ; if ( n % 6 == 0 ) z = - ( b - a ) ; return z ; } public static void main ( String [ ] args ) { int a = 10 , b = 17 , n = 3 ; System . out . println ( nth_term ( a , b , n ) ) ; } }

def nth_term ( a , b , n ) : NEW_LINE INDENT z = 0 NEW_LINE if ( n % 6 == 1 ) : NEW_LINE INDENT z = a NEW_LINE DEDENT elif ( n % 6 == 2 ) : NEW_LINE INDENT z = b NEW_LINE DEDENT elif ( n % 6 == 3 ) : NEW_LINE INDENT z = b - a NEW_LINE DEDENT elif ( n % 6 == 4 ) : NEW_LINE INDENT z = - a NEW_LINE DEDENT elif ( n % 6 == 5 ) : NEW_LINE INDENT z = - b NEW_LINE DEDENT if ( n % 6 == 0 ) : NEW_LINE INDENT z = - ( b - a ) NEW_LINE DEDENT return z NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE b = 17 NEW_LINE n = 3 NEW_LINE print ( nth_term ( a , b , n ) ) NEW_LINE DEDENT


public class GFG { static int totalDigits ( int n ) { int number_of_digits = 0 ; for ( int i = 1 ; i <= n ; i *= 10 ) number_of_digits += ( n - i + 1 ) ; return number_of_digits ; } public static void main ( String [ ] args ) { int n = 13 ; System . out . println ( totalDigits ( n ) ) ; } }

def totalDigits ( n ) : NEW_LINE INDENT number_of_digits = 0 ; NEW_LINE for i in range ( 1 , n , 10 ) : NEW_LINE INDENT number_of_digits = ( number_of_digits + ( n - i + 1 ) ) ; NEW_LINE DEDENT return number_of_digits ; NEW_LINE DEDENT n = 13 ; NEW_LINE s = totalDigits ( n ) + 1 ; NEW_LINE print ( s ) ; NEW_LINE


import java . io . * ; public class GFG { static void isSubsetAndZero ( int array [ ] , int length , int N ) { int arrAnd = array [ 0 ] ; for ( int i = 1 ; i < length ; i ++ ) { arrAnd = arrAnd & array [ i ] ; } if ( ( arrAnd & N ) == 0 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 2 , 4 } ; int length = array . length ; int N = 3 ; isSubsetAndZero ( array , length , N ) ; } }

def isSubsetAndZero ( array , length , N ) : NEW_LINE INDENT arrAnd = array [ 0 ] NEW_LINE for i in range ( 1 , length ) : NEW_LINE INDENT arrAnd = arrAnd & array [ i ] NEW_LINE DEDENT if ( ( arrAnd & N ) == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT array = [ 1 , 2 , 4 ] NEW_LINE length = len ( array ) NEW_LINE N = 3 NEW_LINE isSubsetAndZero ( array , length , N ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int maximumAbsolute ( int arr [ ] , int n ) { int mn = Integer . MAX_VALUE ; int mx = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 && arr [ i ] == - 1 && arr [ i - 1 ] != - 1 ) { mn = Math . min ( mn , arr [ i - 1 ] ) ; mx = Math . max ( mx , arr [ i - 1 ] ) ; } if ( i < n - 1 && arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { mn = Math . min ( mn , arr [ i + 1 ] ) ; mx = Math . max ( mx , arr [ i + 1 ] ) ; } } int common_integer = ( mn + mx ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == - 1 ) arr [ i ] = common_integer ; } int max_diff = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int diff = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; if ( diff > max_diff ) max_diff = diff ; } return max_diff ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 1 , 11 , - 1 , 3 , - 1 } ; int n = arr . length ; System . out . print ( maximumAbsolute ( arr , n ) ) ; } }

def maximumAbsolute ( arr , n ) : NEW_LINE INDENT mn = 10 ** 9 NEW_LINE mx = - 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i > 0 and arr [ i ] == - 1 and arr [ i - 1 ] != - 1 ) : NEW_LINE INDENT mn = min ( mn , arr [ i - 1 ] ) NEW_LINE mx = max ( mx , arr [ i - 1 ] ) NEW_LINE DEDENT if ( i < n - 1 and arr [ i ] == - 1 and arr [ i + 1 ] != - 1 ) : NEW_LINE INDENT mn = min ( mn , arr [ i + 1 ] ) NEW_LINE mx = max ( mx , arr [ i + 1 ] ) NEW_LINE DEDENT DEDENT common_integer = ( mn + mx ) // 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == - 1 ) : NEW_LINE INDENT arr [ i ] = common_integer NEW_LINE DEDENT DEDENT max_diff = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT diff = abs ( arr [ i ] - arr [ i + 1 ] ) NEW_LINE if ( diff > max_diff ) : NEW_LINE INDENT max_diff = diff NEW_LINE DEDENT DEDENT return max_diff NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 1 , - 1 , 11 , - 1 , 3 , - 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maximumAbsolute ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void pattern ( int n ) { int i , j ; for ( i = n - 1 ; i >= 0 ; i -- ) { for ( j = n - 1 ; j > i ; j -- ) { System . out . print ( " ▁ " ) ; } System . out . print ( ( char ) ( i + 65 ) ) ; for ( j = 1 ; j < ( i * 2 ) ; j ++ ) System . out . print ( " ▁ " ) ; if ( i >= 1 ) System . out . print ( ( char ) ( i + 65 ) ) ; System . out . print ( " \n " ) ; } } public static void main ( String args [ ] ) { int n = 5 ; pattern ( n ) ; } }

import math as mt NEW_LINE def pattern ( n ) : NEW_LINE INDENT i = n - 1 NEW_LINE j = 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( n - 1 , i , - 1 ) : NEW_LINE INDENT print ( ' ▁ ' , end = ' ' ) NEW_LINE DEDENT print ( chr ( i + 65 ) , end = ' ' ) NEW_LINE for j in range ( 1 , i * 2 ) : NEW_LINE INDENT print ( ' ▁ ' , end = ' ' ) NEW_LINE DEDENT if ( i >= 1 ) : NEW_LINE INDENT print ( chr ( i + 65 ) , end = ' ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE pattern ( n ) NEW_LINE


import java . io . * ; class GFG { static int octalToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base = 1 ; int temp = num ; while ( temp > 0 ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base ; base = base * 8 ; } return dec_value ; } public static void main ( String [ ] args ) { int num = 67 ; System . out . println ( octalToDecimal ( num ) ) ; } }

def octalToDecimal ( n ) : NEW_LINE INDENT num = n ; NEW_LINE dec_value = 0 ; NEW_LINE base = 1 ; NEW_LINE temp = num ; NEW_LINE while ( temp ) : NEW_LINE INDENT last_digit = temp % 10 ; NEW_LINE temp = int ( temp / 10 ) ; NEW_LINE dec_value += last_digit * base ; NEW_LINE base = base * 8 ; NEW_LINE DEDENT return dec_value ; NEW_LINE DEDENT num = 67 ; NEW_LINE print ( octalToDecimal ( num ) ) ; NEW_LINE


class OddOccurance { int getOddOccurrence ( int ar [ ] , int ar_size ) { int i ; int res = 0 ; for ( i = 0 ; i < ar_size ; i ++ ) { res = res ^ ar [ i ] ; } return res ; } public static void main ( String [ ] args ) { OddOccurance occur = new OddOccurance ( ) ; int ar [ ] = new int [ ] { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = ar . length ; System . out . println ( occur . getOddOccurrence ( ar , n ) ) ; } }

def getOddOccurrence ( arr ) : NEW_LINE INDENT res = 0 NEW_LINE for element in arr : NEW_LINE INDENT res = res ^ element NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ] NEW_LINE print ( " % d " % getOddOccurrence ( arr ) ) NEW_LINE


public class GFG { public static int find_value ( int a [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return sum % k ; } public static void main ( String [ ] args ) { int n = 5 , k = 3 ; int a [ ] = { 12 , 4 , 13 , 0 , 5 } ; System . out . println ( find_value ( a , n , k ) ) ; } }

def find_value ( a , n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT return sum % k NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 5 , 3 ; NEW_LINE a = [ 12 , 4 , 13 , 0 , 5 ] ; NEW_LINE print ( find_value ( a , n , k ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static Vector < Integer > Diffs ( int [ ] a , int k ) { Vector < Integer > out = new Vector < Integer > ( ) ; int [ ] inc , dec ; inc = new int [ a . length ] ; dec = new int [ a . length ] ; int inc_sum = 0 ; int dec_sum = 0 ; for ( int i = 0 ; i < a . length ; ++ i ) { for ( int j = i - 1 ; j >= 0 && j > i - k && a [ j + 1 ] > a [ j ] ; -- j ) { ++ inc [ j ] ; ++ inc_sum ; } for ( int j = i - 1 ; j >= 0 && j > i - k && a [ j + 1 ] < a [ j ] ; -- j ) { ++ dec [ j ] ; ++ dec_sum ; } if ( i >= k - 1 ) { if ( i >= k ) { inc_sum -= inc [ i - k ] ; dec_sum -= dec [ i - k ] ; } out . add ( inc_sum - dec_sum ) ; } } return out ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 30 , 15 , 15 } ; Vector < Integer > out = Diffs ( arr , 3 ) ; for ( int n : out ) System . out . print ( n + " , ▁ " ) ; } }

def Diffs ( a , k ) : NEW_LINE INDENT out , inc , dec = [ ] , [ 0 ] * len ( a ) , [ 0 ] * len ( a ) NEW_LINE inc_sum , dec_sum = 0 , 0 NEW_LINE for i in range ( 0 , len ( a ) ) : NEW_LINE INDENT j = i - 1 NEW_LINE while ( j >= 0 and j > i - k and a [ j + 1 ] > a [ j ] ) : NEW_LINE INDENT inc [ j ] += 1 NEW_LINE inc_sum += 1 NEW_LINE j -= 1 NEW_LINE DEDENT j = i - 1 NEW_LINE while ( j >= 0 and j > i - k and a [ j + 1 ] < a [ j ] ) : NEW_LINE INDENT dec [ j ] += 1 NEW_LINE dec_sum += 1 NEW_LINE j -= 1 NEW_LINE DEDENT if i >= k - 1 : NEW_LINE INDENT if i >= k : NEW_LINE INDENT inc_sum -= inc [ i - k ] NEW_LINE dec_sum -= dec [ i - k ] NEW_LINE DEDENT out . append ( inc_sum - dec_sum ) NEW_LINE DEDENT DEDENT return out NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT out = Diffs ( [ 10 , 20 , 30 , 15 , 15 ] , 3 ) NEW_LINE for n in out : NEW_LINE INDENT print ( n , end = " , ▁ " ) NEW_LINE DEDENT DEDENT


public class Evaluate_BoolExp { static int evaluateBoolExpr ( StringBuffer s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i += 2 ) { if ( i + 1 < n && i + 2 < n ) { if ( s . charAt ( i + 1 ) == ' A ' ) { if ( s . charAt ( i + 2 ) == '0' || s . charAt ( i ) == 0 ) s . setCharAt ( i + 2 , '0' ) ; else s . setCharAt ( i + 2 , '1' ) ; } else if ( ( i + 1 ) < n && s . charAt ( i + 1 ) == ' B ' ) { if ( s . charAt ( i + 2 ) == '1' || s . charAt ( i ) == '1' ) s . setCharAt ( i + 2 , '1' ) ; else s . setCharAt ( i + 2 , '0' ) ; } else { if ( s . charAt ( i + 2 ) == s . charAt ( i ) ) s . setCharAt ( i + 2 , '0' ) ; else s . setCharAt ( i + 2 , '1' ) ; } } } return s . charAt ( n - 1 ) - '0' ; } public static void main ( String [ ] args ) { String s = "1C1B1B0A0" ; StringBuffer sb = new StringBuffer ( s ) ; System . out . println ( evaluateBoolExpr ( sb ) ) ; } }

import math as mt NEW_LINE def evaluateBoolExpr ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( 0 , n - 2 , 2 ) : NEW_LINE INDENT if ( s [ i + 1 ] == " A " ) : NEW_LINE INDENT if ( s [ i + 2 ] == "0" or s [ i ] == "0" ) : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT DEDENT elif ( s [ i + 1 ] == " B " ) : NEW_LINE INDENT if ( s [ i + 2 ] == "1" or s [ i ] == "1" ) : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( s [ i + 2 ] == s [ i ] ) : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT DEDENT DEDENT return ord ( s [ n - 1 ] ) - ord ( "0" ) NEW_LINE DEDENT s = "1C1B1B0A0" NEW_LINE string = [ s [ i ] for i in range ( len ( s ) ) ] NEW_LINE print ( evaluateBoolExpr ( string ) ) NEW_LINE


import java . io . * ; class GFG { static void printPair ( int g , int l ) { System . out . print ( g + " ▁ " + l ) ; } public static void main ( String [ ] args ) { int g = 3 , l = 12 ; printPair ( g , l ) ; } }

def printPair ( g , l ) : NEW_LINE INDENT print ( g , l ) NEW_LINE DEDENT g = 3 ; l = 12 ; NEW_LINE printPair ( g , l ) ; NEW_LINE


class GFG { static int no_of_ways ( int a [ ] , int n ) { int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; } if ( count_1 % 2 == 0 ) return count_0 ; else return count_1 ; } public static void main ( String [ ] args ) { int n = 4 ; int a1 [ ] = { 1 , 1 , 0 , 0 } ; System . out . println ( no_of_ways ( a1 , n ) ) ; n = 5 ; int a2 [ ] = { 1 , 1 , 1 , 0 , 0 } ; System . out . println ( no_of_ways ( a2 , n ) ) ; n = 5 ; int a3 [ ] = { 1 , 1 , 0 , 0 , 0 } ; System . out . println ( no_of_ways ( a3 , n ) ) ; n = 6 ; int a4 [ ] = { 1 , 1 , 1 , 0 , 0 , 0 } ; System . out . println ( no_of_ways ( a4 , n ) ) ; } }

def no_of_ways ( a , n ) : NEW_LINE INDENT count_0 = 0 NEW_LINE count_1 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT DEDENT if ( count_1 % 2 == 0 ) : NEW_LINE INDENT return count_0 NEW_LINE DEDENT else : NEW_LINE INDENT return count_1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE a1 = [ 1 , 1 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a1 , n ) ) NEW_LINE n = 5 NEW_LINE a2 = [ 1 , 1 , 1 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a2 , n ) ) NEW_LINE n = 5 NEW_LINE a3 = [ 1 , 1 , 0 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a3 , n ) ) NEW_LINE n = 6 NEW_LINE a4 = [ 1 , 1 , 1 , 0 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a4 , n ) ) NEW_LINE DEDENT


class FindNth { static int printNthElement ( int n ) { int arr [ ] = new int [ n + 1 ] ; arr [ 1 ] = 1 ; arr [ 2 ] = 2 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 1 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 2 ; } return arr [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( printNthElement ( n ) ) ; } }

def printNthElement ( n ) : NEW_LINE INDENT arr = [ 0 ] * ( n + 1 ) ; NEW_LINE arr [ 1 ] = 1 NEW_LINE arr [ 2 ] = 2 NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i // 2 ] * 10 + 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = arr [ ( i // 2 ) - 1 ] * 10 + 2 NEW_LINE DEDENT DEDENT return arr [ n ] NEW_LINE DEDENT n = 5 NEW_LINE print ( printNthElement ( n ) ) NEW_LINE


import java . io . * ; class SmallPrime { static boolean isPrime ( int n ) { int i , c = 0 ; for ( i = 1 ; i < n / 2 ; i ++ ) { if ( n % i == 0 ) c ++ ; } if ( c == 1 ) { return true ; } else { return false ; } } static void findMinNum ( int arr [ ] , int n ) { int first = 0 , last = 0 , num , rev , i ; int hash [ ] = new int [ 10 ] ; for ( i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } System . out . print ( " Minimum ▁ number : ▁ " ) ; for ( i = 0 ; i <= 9 ; i ++ ) { for ( int j = 0 ; j < hash [ i ] ; j ++ ) System . out . print ( i ) ; } System . out . println ( ) ; System . out . println ( ) ; for ( i = 0 ; i <= 9 ; i ++ ) { if ( hash [ i ] != 0 ) { first = i ; break ; } } for ( i = 9 ; i >= 0 ; i -- ) { if ( hash [ i ] != 0 ) { last = i ; break ; } } num = first * 10 + last ; rev = last * 10 + first ; System . out . print ( " Prime ▁ combinations : ▁ " ) ; if ( isPrime ( num ) && isPrime ( rev ) ) { System . out . println ( num + " ▁ " + rev ) ; } else if ( isPrime ( num ) ) { System . out . println ( num ) ; } else if ( isPrime ( rev ) ) { System . out . println ( rev ) ; } else { System . out . println ( " No ▁ combinations ▁ exist " ) ; } } public static void main ( String [ ] args ) { SmallPrime smallprime = new SmallPrime ( ) ; int arr [ ] = { 1 , 2 , 4 , 7 , 8 } ; smallprime . findMinNum ( arr , 5 ) ; } }

import math as mt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT i , c = 0 , 0 NEW_LINE for i in range ( 1 , n // 2 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT if ( c == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findMinNum ( arr , n ) : NEW_LINE INDENT first , last = 0 , 0 NEW_LINE Hash = [ 0 for i in range ( 10 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT Hash [ arr [ i ] ] += 1 NEW_LINE DEDENT print ( " Minimum ▁ number : ▁ " , end = " " ) NEW_LINE for i in range ( 0 , 10 ) : NEW_LINE INDENT for j in range ( Hash [ i ] ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( Hash [ i ] != 0 ) : NEW_LINE INDENT first = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( 9 , - 1 , - 1 ) : NEW_LINE INDENT if ( Hash [ i ] != 0 ) : NEW_LINE INDENT last = i NEW_LINE break NEW_LINE DEDENT DEDENT num = first * 10 + last NEW_LINE rev = last * 10 + first NEW_LINE print ( " Prime ▁ combinations : ▁ " , end = " " ) NEW_LINE if ( isPrime ( num ) and isPrime ( rev ) ) : NEW_LINE INDENT print ( num , " ▁ " , rev ) NEW_LINE DEDENT elif ( isPrime ( num ) ) : NEW_LINE INDENT print ( num ) NEW_LINE DEDENT elif ( isPrime ( rev ) ) : NEW_LINE INDENT print ( rev ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ combinations ▁ exist " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 4 , 7 , 8 ] NEW_LINE findMinNum ( arr , 5 ) NEW_LINE


import java . util . * ; class GFG { static String check ( int [ ] Arr , int n , int M , int K ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Arr [ i ] == 0 ) flag = 1 ; } if ( K % 2 != 0 && flag == 0 ) return " No " ; else return " Yes " ; } public static void main ( String args [ ] ) { int [ ] Arr = { 1 , 1 , 2 , 4 , 7 , 8 } ; int M = 5 ; int K = 6 ; int n = Arr . length ; System . out . println ( check ( Arr , n , M , K ) ) ; } }

def check ( Arr , n , M , K ) : NEW_LINE INDENT flag = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( Arr [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT if ( K % 2 != 0 and flag == 0 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT else : NEW_LINE INDENT return " Yes " ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Arr = [ 1 , 1 , 2 , 4 , 7 , 8 ] NEW_LINE M = 5 ; NEW_LINE K = 6 ; NEW_LINE n = len ( Arr ) ; NEW_LINE print ( check ( Arr , n , M , K ) ) NEW_LINE DEDENT


import java . util . Vector ; class Test { static final int MAXN = 100001 ; static int spf [ ] = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static Vector < Integer > getFactorization ( int x ) { Vector < Integer > ret = new Vector < > ( ) ; while ( x != 1 ) { ret . add ( spf [ x ] ) ; x = x / spf [ x ] ; } return ret ; } public static void main ( String args [ ] ) { sieve ( ) ; int x = 12246 ; System . out . print ( " prime ▁ factorization ▁ for ▁ " + x + " ▁ : ▁ " ) ; Vector < Integer > p = getFactorization ( x ) ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) System . out . print ( p . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } }

import math as mt NEW_LINE MAXN = 100001 NEW_LINE spf = [ 0 for i in range ( MAXN ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT spf [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAXN ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAXN , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT for i in range ( 3 , mt . ceil ( mt . sqrt ( MAXN ) ) ) : NEW_LINE INDENT if ( spf [ i ] == i ) : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE INDENT if ( spf [ j ] == j ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def getFactorization ( x ) : NEW_LINE INDENT ret = list ( ) NEW_LINE while ( x != 1 ) : NEW_LINE INDENT ret . append ( spf [ x ] ) NEW_LINE x = x // spf [ x ] NEW_LINE DEDENT return ret NEW_LINE DEDENT sieve ( ) NEW_LINE x = 12246 NEW_LINE print ( " prime ▁ factorization ▁ for " , x , " : ▁ " , end = " " ) NEW_LINE p = getFactorization ( x ) NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT print ( p [ i ] , end = " ▁ " ) NEW_LINE DEDENT


class GFG { static class Node { int data ; Node next ; } ; static int getIntesectionNode ( Node head1 , Node head2 ) { Node current1 = head1 ; Node current2 = head2 ; if ( current1 == null || current2 == null ) return - 1 ; while ( current1 != null && current2 != null && current1 != current2 ) { current1 = current1 . next ; current2 = current2 . next ; if ( current1 == current2 ) return current1 . data ; if ( current1 == null ) current1 = head2 ; if ( current2 == null ) current2 = head1 ; } return current1 . data ; } public static void main ( String [ ] args ) { Node newNode ; Node head1 = new Node ( ) ; head1 . data = 10 ; Node head2 = new Node ( ) ; head2 . data = 3 ; newNode = new Node ( ) ; newNode . data = 6 ; head2 . next = newNode ; newNode = new Node ( ) ; newNode . data = 9 ; head2 . next . next = newNode ; newNode = new Node ( ) ; newNode . data = 15 ; head1 . next = newNode ; head2 . next . next . next = newNode ; newNode = new Node ( ) ; newNode . data = 30 ; head1 . next . next = newNode ; head1 . next . next . next = null ; System . out . print ( getIntesectionNode ( head1 , head2 ) ) ; } }

class new_Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getIntesectionNode ( head1 , head2 ) : NEW_LINE INDENT current1 = head1 NEW_LINE current2 = head2 NEW_LINE if ( not current1 or not current2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while ( current1 and current2 and current1 != current2 ) : NEW_LINE INDENT current1 = current1 . next NEW_LINE current2 = current2 . next NEW_LINE if ( current1 == current2 ) : NEW_LINE INDENT return current1 . data NEW_LINE DEDENT if ( not current1 ) : NEW_LINE INDENT current1 = head2 NEW_LINE DEDENT if ( not current2 ) : NEW_LINE INDENT current2 = head1 NEW_LINE DEDENT DEDENT return current1 . data NEW_LINE DEDENT head1 = new_Node ( 10 ) NEW_LINE head2 = new_Node ( 3 ) NEW_LINE newNode = new_Node ( 6 ) NEW_LINE head2 . next = newNode NEW_LINE newNode = new_Node ( 9 ) NEW_LINE head2 . next . next = newNode NEW_LINE newNode = new_Node ( 15 ) NEW_LINE head1 . next = newNode NEW_LINE head2 . next . next . next = newNode NEW_LINE newNode = new_Node ( 30 ) NEW_LINE head1 . next . next = newNode NEW_LINE head1 . next . next . next = None NEW_LINE print ( getIntesectionNode ( head1 , head2 ) ) NEW_LINE


class GfG { static void reverse ( String str , int x ) { int n = ( str . length ( ) - x ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( str . charAt ( i ) ) ; for ( int i = n + x - 1 ; i >= n ; i -- ) System . out . print ( str . charAt ( i ) ) ; for ( int i = n + x ; i < str . length ( ) ; i ++ ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int x = 3 ; reverse ( str , x ) ; } }

def reverse ( str1 , x ) : NEW_LINE INDENT n = ( len ( str1 ) - x ) // 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT for i in range ( n + x - 1 , n - 1 , - 1 ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT for i in range ( n + x , len ( str1 ) ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT DEDENT str1 = " geeksforgeeks " NEW_LINE x = 3 NEW_LINE reverse ( str1 , x ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static int countDigits ( int a , int b ) { int count = 0 ; int p = Math . abs ( a * b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } public static void main ( String args [ ] ) { int a = 33 ; int b = - 24 ; System . out . println ( " Number ▁ of ▁ digits ▁ = ▁ " + countDigits ( a , b ) ) ; } }

def countDigits ( a , b ) : NEW_LINE INDENT count = 0 NEW_LINE p = abs ( a * b ) NEW_LINE if ( p == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT while ( p > 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE p = p // 10 NEW_LINE DEDENT return count NEW_LINE DEDENT a = 33 NEW_LINE b = - 24 NEW_LINE print ( " Number ▁ of ▁ digits ▁ = ▁ " , countDigits ( a , b ) ) NEW_LINE


class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int n = 17 ; if ( isPrime ( n ) && ( n % 16 == 1 ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 17 NEW_LINE if ( isPrime ( n ) and ( n % 16 == 1 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT


class GFG { static int minOperations ( int x , int y , int p , int q ) { if ( y % x != 0 ) return - 1 ; int d = y / x ; int a = 0 ; while ( d % p == 0 ) { d /= p ; a ++ ; } int b = 0 ; while ( d % q == 0 ) { d /= q ; b ++ ; } if ( d != 1 ) return - 1 ; return ( a + b ) ; } public static void main ( String [ ] args ) { int x = 12 , y = 2592 , p = 2 , q = 3 ; System . out . println ( minOperations ( x , y , p , q ) ) ; } }

def minOperations ( x , y , p , q ) : NEW_LINE INDENT if ( y % x != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT d = y // x NEW_LINE a = 0 NEW_LINE while ( d % p == 0 ) : NEW_LINE INDENT d //= p NEW_LINE a += 1 NEW_LINE DEDENT b = 0 NEW_LINE while ( d % q == 0 ) : NEW_LINE INDENT d //= q NEW_LINE b += 1 NEW_LINE DEDENT if ( d != 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( a + b ) NEW_LINE DEDENT x = 12 NEW_LINE y = 2592 NEW_LINE p = 2 NEW_LINE q = 3 NEW_LINE print ( minOperations ( x , y , p , q ) ) NEW_LINE


class Leet442 { public static void main ( String args [ ] ) { int numRay [ ] = { 0 , 4 , 3 , 2 , 7 , 8 , 2 , 3 , 1 } ; for ( int i = 0 ; i < numRay . length ; i ++ ) { numRay [ numRay [ i ] % numRay . length ] = numRay [ numRay [ i ] % numRay . length ] + numRay . length ; } System . out . println ( " The ▁ repeating ▁ elements ▁ are ▁ : ▁ " ) ; for ( int i = 0 ; i < numRay . length ; i ++ ) { if ( numRay [ i ] >= numRay . length * 2 ) { System . out . println ( i + " ▁ " ) ; } } } }

numRay = [ 0 , 4 , 3 , 2 , 7 , 8 , 2 , 3 , 1 ] ; NEW_LINE arr_size = len ( numRay ) ; NEW_LINE for i in range ( arr_size ) : NEW_LINE INDENT numRay [ numRay [ i ] % arr_size ] = numRay [ numRay [ i ] % arr_size ] + arr_size ; NEW_LINE DEDENT print ( " The ▁ repeating ▁ elements ▁ are ▁ : ▁ " ) ; NEW_LINE for i in range ( arr_size ) : NEW_LINE INDENT if ( numRay [ i ] >= arr_size * 2 ) : NEW_LINE INDENT print ( i , " ▁ " ) ; NEW_LINE DEDENT DEDENT