import java . io . * ; public class GFG { static void vowelOrConsonant ( char x ) { if ( x == ' a ' || x == ' e ' || x == ' i ' || x == ' o ' || x == ' u ' ) System . out . println ( " Vowel " ) ; else System . out . println ( " Consonant " ) ; } static public void main ( String [ ] args ) { vowelOrConsonant ( ' c ' ) ; vowelOrConsonant ( ' e ' ) ; } }

def vowelOrConsonant ( x ) : NEW_LINE INDENT if ( x == ' a ' or x == ' e ' or x == ' i ' or x == ' o ' or x == ' u ' ) : NEW_LINE INDENT print ( " Vowel " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Consonant " ) NEW_LINE DEDENT DEDENT vowelOrConsonant ( ' c ' ) NEW_LINE vowelOrConsonant ( ' e ' ) NEW_LINE


class GFG { static int extractMaximum ( String str ) { int num = 0 , res = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isDigit ( str . charAt ( i ) ) ) num = num * 10 + ( str . charAt ( i ) - '0' ) ; else { res = Math . max ( res , num ) ; num = 0 ; } } return Math . max ( res , num ) ; } public static void main ( String [ ] args ) { String str = "100klh564abc365bg " ; System . out . println ( extractMaximum ( str ) ) ; } }

def extractMaximum ( ss ) : NEW_LINE INDENT num , res = 0 , 0 NEW_LINE for i in range ( len ( ss ) ) : NEW_LINE INDENT if ss [ i ] >= "0" and ss [ i ] <= "9" : NEW_LINE INDENT num = num * 10 + int ( int ( ss [ i ] ) - 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT res = max ( res , num ) NEW_LINE num = 0 NEW_LINE DEDENT DEDENT return max ( res , num ) NEW_LINE DEDENT ss = "100klh564abc365bg " NEW_LINE print ( extractMaximum ( ss ) ) NEW_LINE


import java . io . * ; class GFG { static int numberOfDays ( int a , int b , int n ) { int Days = b * ( n + a ) / ( a + b ) ; return Days ; } public static void main ( String [ ] args ) { int a = 10 , b = 20 , n = 5 ; System . out . println ( numberOfDays ( a , b , n ) ) ; } }

def numberOfDays ( a , b , n ) : NEW_LINE INDENT Days = b * ( n + a ) // ( a + b ) NEW_LINE return Days NEW_LINE DEDENT a = 10 NEW_LINE b = 20 NEW_LINE n = 5 NEW_LINE print ( numberOfDays ( a , b , n ) ) NEW_LINE


class GFG { static float Cel_To_Fah ( float n ) { return ( ( n * 9.0f / 5.0f ) + 32.0f ) ; } public static void main ( String [ ] args ) { float n = 20.0f ; System . out . println ( Cel_To_Fah ( n ) ) ; } }

def Cel_To_Fah ( n ) : NEW_LINE INDENT return ( n * 1.8 ) + 32 NEW_LINE DEDENT n = 20 NEW_LINE print ( int ( Cel_To_Fah ( n ) ) ) NEW_LINE


class GFG { static void printTwins ( int low , int high ) { boolean prime [ ] = new boolean [ high + 1 ] , twin = false ; for ( int i = 0 ; i < prime . length ; i ++ ) { prime [ i ] = true ; } prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p <= Math . floor ( Math . sqrt ( high ) ) + 1 ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= high ; i += p ) { prime [ i ] = false ; } } } for ( int i = low ; i <= high ; i ++ ) { if ( prime [ i ] && prime [ i + 2 ] ) { int a = i + 2 ; System . out . print ( " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " + i + " , ▁ " + a + " ) " ) ; twin = true ; break ; } } if ( twin == false ) { System . out . println ( " No ▁ such ▁ pair ▁ exists " ) ; } } public static void main ( String [ ] args ) { printTwins ( 10 , 100 ) ; } }

import math NEW_LINE def printTwins ( low , high ) : NEW_LINE INDENT prime = [ True ] * ( high + 1 ) ; NEW_LINE twin = False ; NEW_LINE prime [ 0 ] = prime [ 1 ] = False ; NEW_LINE for p in range ( 2 , int ( math . floor ( math . sqrt ( high ) ) + 2 ) ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * 2 , high + 1 , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( low , high + 1 ) : NEW_LINE INDENT if ( prime [ i ] and prime [ i + 2 ] ) : NEW_LINE INDENT print ( " Smallest ▁ twins ▁ in ▁ given ▁ range : ▁ ( " , i , " , " , ( i + 2 ) , " ) " ) ; NEW_LINE twin = True ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( twin == False ) : NEW_LINE INDENT print ( " No ▁ such ▁ pair ▁ exists " ) ; NEW_LINE DEDENT DEDENT printTwins ( 10 , 100 ) ; NEW_LINE


import java . util . * ; class GFG { static void leftSmaller ( int arr [ ] , int n , int SE [ ] ) { Stack < Integer > S = new Stack < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( ! S . empty ( ) && S . peek ( ) >= arr [ i ] ) { S . pop ( ) ; } if ( ! S . empty ( ) ) { SE [ i ] = S . peek ( ) ; } else { SE [ i ] = 0 ; } S . push ( arr [ i ] ) ; } } static int findMaxDiff ( int arr [ ] , int n ) { int [ ] LS = new int [ n ] ; leftSmaller ( arr , n , LS ) ; int [ ] RRS = new int [ n ] ; reverse ( arr ) ; leftSmaller ( arr , n , RRS ) ; int result = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { result = Math . max ( result , Math . abs ( LS [ i ] - RRS [ n - 1 - i ] ) ) ; } return result ; } static void reverse ( int a [ ] ) { int i , k , n = a . length ; int t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 8 , 7 , 7 , 9 , 3 } ; int n = arr . length ; System . out . println ( " Maximum ▁ diff ▁ : ▁ " + findMaxDiff ( arr , n ) ) ; } }

def leftsmaller ( arr , n , SE ) : NEW_LINE INDENT sta = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( sta != [ ] and sta [ len ( sta ) - 1 ] >= arr [ i ] ) : NEW_LINE INDENT sta . pop ( ) NEW_LINE DEDENT if ( sta != [ ] ) : NEW_LINE INDENT SE [ i ] = sta [ len ( sta ) - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT SE [ i ] = 0 NEW_LINE DEDENT sta . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT def findMaxDiff ( arr , n ) : NEW_LINE INDENT ls = [ 0 ] * n NEW_LINE rs = [ 0 ] * n NEW_LINE leftsmaller ( arr , n , ls ) NEW_LINE leftsmaller ( arr [ : : - 1 ] , n , rs ) NEW_LINE res = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT res = max ( res , abs ( ls [ i ] - rs [ n - 1 - i ] ) ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 8 , 7 , 7 , 9 , 3 ] NEW_LINE print " Maximum ▁ Diff ▁ : " , findMaxDiff ( arr , len ( arr ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static void removeDuplicates ( int [ ] arr ) { LinkedHashSet < Integer > set = new LinkedHashSet < Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) set . add ( arr [ i ] ) ; System . out . print ( set ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 5 , 1 , 7 , 2 , 4 , 2 } ; removeDuplicates ( arr ) ; } }

def removeDulipcates ( arr ) : NEW_LINE INDENT s = dict ( ) NEW_LINE d = [ ] NEW_LINE for i in arr : NEW_LINE INDENT if i not in s . keys ( ) : NEW_LINE INDENT d . append ( i ) NEW_LINE s [ i ] = 1 NEW_LINE DEDENT DEDENT print ( d ) NEW_LINE DEDENT arr = [ 1 , 2 , 5 , 1 , 7 , 2 , 4 , 2 ] NEW_LINE removeDulipcates ( arr ) NEW_LINE


import java . io . * ; class GFG { static int dodecahedral_num ( int n ) { return n * ( 3 * n - 1 ) * ( 3 * n - 2 ) / 2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( n + " the ▁ Dodecahedral " + " ▁ number : " ) ; System . out . println ( dodecahedral_num ( n ) ) ; } }

def dodecahedral_num ( n ) : NEW_LINE INDENT return n * ( 3 * n - 1 ) * ( 3 * n - 2 ) // 2 NEW_LINE DEDENT n = 5 NEW_LINE print ( " % sth ▁ Dodecahedral ▁ number ▁ : " % n , dodecahedral_num ( n ) ) NEW_LINE


import java . util . * ; class GFG { static int Segment ( int x [ ] , int l [ ] , int n ) { if ( n == 1 ) return 1 ; int ans = 2 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( x [ i ] - l [ i ] > x [ i - 1 ] ) ans ++ ; else if ( x [ i ] + l [ i ] < x [ i + 1 ] ) { x [ i ] = x [ i ] + l [ i ] ; ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int x [ ] = { 1 , 3 , 4 , 5 , 8 } , l [ ] = { 10 , 1 , 2 , 2 , 5 } ; int n = x . length ; System . out . println ( Segment ( x , l , n ) ) ; } }

def Segment ( x , l , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT ans = 2 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( x [ i ] - l [ i ] > x [ i - 1 ] ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT elif ( x [ i ] + l [ i ] < x [ i + 1 ] ) : NEW_LINE INDENT x [ i ] = x [ i ] + l [ i ] NEW_LINE ans += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT x = [ 1 , 3 , 4 , 5 , 8 ] NEW_LINE l = [ 10 , 1 , 2 , 2 , 5 ] NEW_LINE n = len ( x ) NEW_LINE print ( Segment ( x , l , n ) ) NEW_LINE


import java . util . * ; class GFG { static int findNthEvenDigitNumber ( int n ) { if ( n == 1 ) { return 0 ; } Vector < Integer > v = new Vector < > ( ) ; n = n - 1 ; while ( n > 0 ) { v . add ( n % 5 ) ; n = n / 5 ; } int result = 0 ; for ( int i = v . size ( ) - 1 ; i >= 0 ; i -- ) { result = result * 10 ; result = result + v . get ( i ) ; } return 2 * result ; } public static void main ( String [ ] args ) { System . out . println ( findNthEvenDigitNumber ( 2 ) ) ; System . out . println ( findNthEvenDigitNumber ( 10 ) ) ; } }

def findNthEvenDigitNumber ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT v = [ ] NEW_LINE n = n - 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT v . append ( n % 5 ) NEW_LINE n = n // 5 NEW_LINE DEDENT result = 0 NEW_LINE for i in range ( len ( v ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT result = result * 10 NEW_LINE result = result + v [ i ] NEW_LINE DEDENT return 2 * result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( findNthEvenDigitNumber ( 2 ) ) NEW_LINE print ( findNthEvenDigitNumber ( 10 ) ) NEW_LINE DEDENT


import java . util . Arrays ; import java . util . Arrays ; class GFG { static int getMin ( int arr [ ] , int n ) { return Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; } static int getMax ( int arr [ ] , int n ) { return Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1234 , 45 , 67 , 1 } ; int n = arr . length ; System . out . println ( " Minimum ▁ element ▁ of ▁ array : ▁ " + getMin ( arr , n ) ) ; System . out . println ( " Maximum ▁ element ▁ of ▁ array : ▁ " + getMax ( arr , n ) ) ; } }

def getMin ( arr , n ) : NEW_LINE INDENT return min ( arr ) NEW_LINE DEDENT def getMax ( arr , n ) : NEW_LINE INDENT return max ( arr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 1234 , 45 , 67 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ element ▁ of ▁ array : ▁ " , getMin ( arr , n ) ) NEW_LINE print ( " Maximum ▁ element ▁ of ▁ array : ▁ " , getMax ( arr , n ) ) NEW_LINE DEDENT


public class GFG { static int countWaysUtil ( int x , int n , int num ) { int val = ( int ) ( x - Math . pow ( num , n ) ) ; if ( val == 0 ) return 1 ; if ( val < 0 ) return 0 ; return countWaysUtil ( val , n , num + 1 ) + countWaysUtil ( x , n , num + 1 ) ; } static int countWays ( int x , int n ) { return countWaysUtil ( x , n , 1 ) ; } public static void main ( String args [ ] ) { int x = 100 , n = 2 ; System . out . println ( countWays ( x , n ) ) ; } }

def countWaysUtil ( x , n , num ) : NEW_LINE INDENT val = ( x - pow ( num , n ) ) NEW_LINE if ( val == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( val < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return countWaysUtil ( val , n , num + 1 ) + \ NEW_LINE INDENT countWaysUtil ( x , n , num + 1 ) NEW_LINE DEDENT DEDENT def countWays ( x , n ) : NEW_LINE INDENT return countWaysUtil ( x , n , 1 ) NEW_LINE DEDENT x = 100 NEW_LINE n = 2 NEW_LINE print ( countWays ( x , n ) ) NEW_LINE


class GFG { public static void main ( String args [ ] ) { int matrix [ ] [ ] = new int [ 5 ] [ 5 ] , row_index , column_index , x = 0 , size = 5 ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { matrix [ row_index ] [ column_index ] = ++ x ; } } System . out . printf ( " The ▁ matrix ▁ is \n " ) ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { System . out . printf ( " % d\t " , matrix [ row_index ] [ column_index ] ) ; } System . out . printf ( " \n " ) ; } System . out . printf ( " \n Elements ▁ above ▁ Main ▁ diagonal ▁ elements ▁ are : \n " ) ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { if ( row_index < column_index ) { System . out . printf ( " % d , ▁ " , matrix [ row_index ] [ column_index ] ) ; } } } } }

if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 for i in range ( 5 ) ] for j in range ( 5 ) ] ; NEW_LINE row_index , column_index , x , size = 0 , 0 , 0 , 5 ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT x += 1 ; NEW_LINE matrix [ row_index ] [ column_index ] = x ; NEW_LINE DEDENT DEDENT print ( " The ▁ matrix ▁ is " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " \t " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT print ( " \n Elements ▁ above ▁ Main ▁ diagonal ▁ elements ▁ are : " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT if ( row_index < column_index ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT


import java . util . * ; import java . lang . * ; import java . util . Arrays ; public class GeeksforGeeks { public static int countDe ( int arr [ ] , int n ) { int v [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) v [ i ] = arr [ i ] ; Arrays . sort ( arr ) ; int count1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != v [ i ] ) count1 ++ ; Collections . reverse ( Arrays . asList ( arr ) ) ; int count2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != v [ i ] ) count2 ++ ; return ( Math . min ( count1 , count2 ) ) ; } public static void main ( String argc [ ] ) { int arr [ ] = { 5 , 9 , 21 , 17 , 13 } ; int n = 5 ; System . out . println ( " Minimum ▁ Dearrangement ▁ = ▁ " + countDe ( arr , n ) ) ; } }

def countDe ( arr , n ) : NEW_LINE INDENT i = 0 NEW_LINE v = arr . copy ( ) NEW_LINE arr . sort ( ) NEW_LINE count1 = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] != v [ i ] ) : NEW_LINE INDENT count1 = count1 + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT arr . sort ( reverse = True ) NEW_LINE count2 = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] != v [ i ] ) : NEW_LINE INDENT count2 = count2 + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return ( min ( count1 , count2 ) ) NEW_LINE DEDENT arr = [ 5 , 9 , 21 , 17 , 13 ] NEW_LINE n = 5 NEW_LINE print ( " Minimum ▁ Dearrangement ▁ = " , countDe ( arr , n ) ) NEW_LINE


class GFG { static void getXandYintercept ( int P [ ] , int Q [ ] ) { int a = P [ 1 ] - Q [ 1 ] ; int b = P [ 0 ] - Q [ 0 ] ; if ( b == 0 ) { System . out . println ( P [ 0 ] ) ; System . out . println ( " infinity " ) ; return ; } if ( a == 0 ) { System . out . println ( " infinity " ) ; System . out . println ( P [ 1 ] ) ; return ; } double m = a / ( b * 1.0 ) ; int x = P [ 0 ] ; int y = P [ 1 ] ; double c = y - m * x ; y = 0 ; double r = ( y - c ) / ( m * 1.0 ) ; System . out . println ( r ) ; x = 0 ; y = ( int ) ( m * x + c ) ; System . out . print ( c ) ; } public static void main ( String [ ] args ) { int p1 [ ] = { 5 , 2 } ; int p2 [ ] = { 2 , 7 } ; getXandYintercept ( p1 , p2 ) ; } }

def getXandYintercept ( P , Q ) : NEW_LINE INDENT a = P [ 1 ] - Q [ 1 ] NEW_LINE b = P [ 0 ] - Q [ 0 ] NEW_LINE if b == 0 : NEW_LINE INDENT print ( P [ 0 ] ) NEW_LINE print ( " infinity " ) NEW_LINE return NEW_LINE DEDENT if a == 0 : NEW_LINE INDENT print ( " infinity " ) NEW_LINE print ( P [ 1 ] ) NEW_LINE return NEW_LINE DEDENT m = a / b NEW_LINE x = P [ 0 ] NEW_LINE y = P [ 1 ] NEW_LINE c = y - m * x NEW_LINE y = 0 NEW_LINE x = ( y - c ) / m NEW_LINE print ( x ) NEW_LINE x = 0 NEW_LINE y = m * x + c NEW_LINE print ( y ) NEW_LINE DEDENT p1 = [ 5 , 2 ] NEW_LINE p2 = [ 7 , 2 ] NEW_LINE getXandYintercept ( p1 , p2 ) NEW_LINE


import java . util . * ; class GFG { static int MinCostTree ( int arr [ ] , int n ) { int ans = 0 ; Vector < Integer > st = new Vector < Integer > ( ) ; st . add ( Integer . MAX_VALUE ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( st . get ( st . size ( ) - 1 ) <= arr [ i ] ) { int x = st . get ( st . size ( ) - 1 ) ; st . remove ( st . size ( ) - 1 ) ; ans += x * Math . min ( st . get ( st . size ( ) - 1 ) , arr [ i ] ) ; } st . add ( arr [ i ] ) ; } for ( int i = 2 ; i < st . size ( ) ; i ++ ) ans += st . get ( i ) * st . get ( i - 1 ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 3 } ; int n = arr . length ; System . out . print ( MinCostTree ( arr , n ) ) ; } }

def MinCostTree ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE st = [ 2 ** 32 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( st [ - 1 ] <= arr [ i ] ) : NEW_LINE INDENT x = st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE ans += x * min ( st [ - 1 ] , arr [ i ] ) NEW_LINE DEDENT st . append ( arr [ i ] ) NEW_LINE DEDENT for i in range ( 2 , len ( st ) ) : NEW_LINE INDENT ans += st [ i ] * st [ i - 1 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( MinCostTree ( arr , n ) ) NEW_LINE


class GFG { static int MAXN = 30 ; static int countMaxLength ( int N ) { int res ; int pre [ ] = new int [ MAXN ] ; int p = 1 ; pre [ 0 ] = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { p *= 2 ; pre [ i ] = pre [ i - 1 ] + i * p ; } int ind = 0 ; for ( int i = 1 ; i < MAXN ; i ++ ) { if ( pre [ i ] >= N ) { ind = i ; break ; } } int x = N - pre [ ind - 1 ] ; int y = 2 * ind - 1 ; if ( x >= y ) res = Math . min ( x , y ) ; else res = Math . max ( x , 2 * ( ind - 2 ) + 1 ) ; return res ; } public static void main ( String [ ] args ) { int N = 25 ; System . out . println ( countMaxLength ( N ) ) ; } }

MAXN = 30 NEW_LINE def countMaxLength ( N ) : NEW_LINE INDENT pre = [ 0 for i in range ( MAXN ) ] NEW_LINE p = 1 NEW_LINE pre [ 0 ] = 0 NEW_LINE for i in range ( 1 , MAXN , 1 ) : NEW_LINE INDENT p *= 2 NEW_LINE pre [ i ] = pre [ i - 1 ] + i * p NEW_LINE DEDENT for i in range ( 1 , MAXN , 1 ) : NEW_LINE INDENT if ( pre [ i ] >= N ) : NEW_LINE INDENT ind = i NEW_LINE break NEW_LINE DEDENT DEDENT x = N - pre [ ind - 1 ] NEW_LINE y = 2 * ind - 1 NEW_LINE if ( x >= y ) : NEW_LINE INDENT res = min ( x , y ) NEW_LINE DEDENT else : NEW_LINE INDENT res = max ( x , 2 * ( ind - 2 ) + 1 ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 25 NEW_LINE print ( countMaxLength ( N ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { public static void main ( String [ ] args ) { double n = 12 ; System . out . println ( Math . sqrt ( n ) ) ; } }

import math NEW_LINE if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE print ( math . sqrt ( n ) ) NEW_LINE DEDENT


class GFG { static int binarySearch ( int arr [ ] , int low , int high ) { if ( high < low ) return - 1 ; int mid = ( low + high ) / 2 ; int midValue = arr [ mid ] ; if ( mid == arr [ mid ] ) return mid ; int leftindex = Math . min ( mid - 1 , midValue ) ; int left = binarySearch ( arr , low , leftindex ) ; if ( left >= 0 ) return left ; int rightindex = Math . max ( mid + 1 , midValue ) ; int right = binarySearch ( arr , rightindex , high ) ; return right ; } public static void main ( String [ ] args ) { int arr [ ] = { - 10 , - 5 , 2 , 2 , 2 , 3 , 4 , 7 , 9 , 12 , 13 } ; System . out . println ( " Fixed ▁ Point ▁ is ▁ " + binarySearch ( arr , 0 , arr . length - 1 ) ) ; int arr1 [ ] = { - 10 , - 1 , 3 , 3 , 10 , 30 , 30 , 50 , 100 } ; System . out . println ( " Fixed ▁ Point ▁ is ▁ " + binarySearch ( arr1 , 0 , arr1 . length - 1 ) ) ; } }

def binarySearch ( arr , low , high ) : NEW_LINE INDENT if ( high < low ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mid = int ( ( low + high ) / 2 ) NEW_LINE midValue = arr [ mid ] NEW_LINE if ( mid == arr [ mid ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT leftindex = min ( mid - 1 , midValue ) NEW_LINE left = binarySearch ( arr , low , leftindex ) NEW_LINE if ( left >= 0 ) : NEW_LINE INDENT return left NEW_LINE DEDENT rightindex = max ( mid + 1 , midValue ) NEW_LINE right = binarySearch ( arr , rightindex , high ) NEW_LINE return right NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 10 , - 5 , 2 , 2 , 2 , 3 , 4 , 7 , 9 , 12 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Fixed ▁ Point ▁ is " , binarySearch ( arr , 0 , n - 1 ) ) NEW_LINE arr1 = [ - 10 , - 1 , 3 , 3 , 10 , 30 , 30 , 50 , 100 ] NEW_LINE n1 = len ( arr ) NEW_LINE print ( " Fixed ▁ Point ▁ is " , binarySearch ( arr1 , 0 , n1 - 1 ) ) NEW_LINE DEDENT


class GFG { static int countOcc ( String s ) { int cnt = 0 ; for ( int i = 0 ; i < s . length ( ) - 3 ; i ++ ) { int c = 0 , l = 0 , a = 0 , p = 0 ; for ( int j = i ; j < i + 4 ; j ++ ) { switch ( s . charAt ( j ) ) { case ' c ' : c ++ ; break ; case ' l ' : l ++ ; break ; case ' a ' : a ++ ; break ; case ' p ' : p ++ ; break ; } } if ( c == 1 && l == 1 && a == 1 && p == 1 ) cnt ++ ; } return cnt ; } public static void main ( String args [ ] ) { String s = " clapc " ; System . out . print ( countOcc ( s . toLowerCase ( ) ) ) ; } }

def countOcc ( s ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 0 , len ( s ) - 3 ) : NEW_LINE INDENT c , l , a , p = 0 , 0 , 0 , 0 NEW_LINE for j in range ( i , i + 4 ) : NEW_LINE INDENT if s [ j ] == ' c ' : NEW_LINE INDENT c += 1 NEW_LINE DEDENT elif s [ j ] == ' l ' : NEW_LINE INDENT l += 1 NEW_LINE DEDENT elif s [ j ] == ' a ' : NEW_LINE INDENT a += 1 NEW_LINE DEDENT elif s [ j ] == ' p ' : NEW_LINE INDENT p += 1 NEW_LINE DEDENT DEDENT if c == 1 and l == 1 and a == 1 and p == 1 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " clapc " NEW_LINE print ( countOcc ( s . lower ( ) ) ) NEW_LINE DEDENT


class GFG { static long oddNumSum ( int n ) { return ( n * ( 2 * n + 1 ) * ( 24 * n * n * n - 12 * n * n - 14 * n + 7 ) ) / 15 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( oddNumSum ( n ) ) ; } }

def oddNumSum ( n ) : NEW_LINE INDENT return ( n * ( 2 * n + 1 ) * ( 24 * n * n * n - 12 * n * n - 14 * n + 7 ) ) / 15 NEW_LINE DEDENT n = 4 NEW_LINE print ( int ( oddNumSum ( n ) ) ) NEW_LINE


import java . io . * ; class GFG { static int N = 2 ; static int M = 2 ; static void Matrix ( char a [ ] [ ] ) { char ch = '1' ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( a [ i ] [ j ] == ' . ' ) a [ i ] [ j ] = ch ; ch = ( ch == '1' ) ? '0' : '1' ; System . out . print ( a [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; if ( i % 2 == 0 ) ch = '0' ; else ch = '1' ; } } public static void main ( String [ ] args ) { char a [ ] [ ] = { { ' . ' , ' _ ' } , { ' _ ' , ' . ' } } ; Matrix ( a ) ; } }

N = 2 NEW_LINE M = 2 NEW_LINE def Matrix ( a ) : NEW_LINE INDENT ch = '1' ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( a [ i ] [ j ] == ' . ' ) : NEW_LINE INDENT a [ i ] [ j ] = ch ; NEW_LINE DEDENT if ( ch == '1' ) : NEW_LINE INDENT ch == '0' NEW_LINE DEDENT else : NEW_LINE INDENT ch = '1' NEW_LINE DEDENT print ( a [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) NEW_LINE if ( i % 2 == 0 ) : NEW_LINE INDENT ch = '0' ; NEW_LINE DEDENT else : NEW_LINE INDENT ch = '1' ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ [ ' . ' , ' _ ' ] , [ ' _ ' , ' . ' ] , ] NEW_LINE Matrix ( a ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int MAX = 1000000 ; static int [ ] prime = new int [ MAX + 1 ] ; static void updatePrimes ( ) { for ( int i = 2 ; i <= MAX ; i ++ ) { prime [ i ] = 1 ; } prime [ 0 ] = prime [ 1 ] = 0 ; for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( prime [ i ] == 1 ) { for ( int j = i * i ; j <= MAX ; j += i ) { prime [ j ] = 0 ; } } } for ( int i = 1 ; i <= MAX ; i ++ ) { prime [ i ] += prime [ i - 1 ] ; } } static int getDifference ( int l , int r ) { int total = r - l + 1 ; int primes = prime [ r ] - prime [ l - 1 ] ; int composites = total - primes ; return ( Math . abs ( primes - composites ) ) ; } public static void main ( String [ ] args ) { int queries [ ] [ ] = { { 1 , 10 } , { 5 , 30 } } ; int q = queries . length ; updatePrimes ( ) ; for ( int i = 0 ; i < q ; i ++ ) System . out . println ( getDifference ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } }

from math import sqrt NEW_LINE MAX = 1000000 NEW_LINE prime = [ 0 ] * ( MAX + 1 ) ; NEW_LINE def updatePrimes ( ) : NEW_LINE INDENT for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT prime [ i ] = 1 ; NEW_LINE DEDENT prime [ 0 ] = prime [ 1 ] = 0 ; NEW_LINE for i in range ( 2 , int ( sqrt ( MAX ) + 1 ) ) : NEW_LINE INDENT if ( prime [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i * i , MAX , i ) : NEW_LINE INDENT prime [ j ] = 0 ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT prime [ i ] += prime [ i - 1 ] ; NEW_LINE DEDENT DEDENT def getDifference ( l , r ) : NEW_LINE INDENT total = r - l + 1 ; NEW_LINE primes = prime [ r ] - prime [ l - 1 ] ; NEW_LINE composites = total - primes ; NEW_LINE return ( abs ( primes - composites ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT queries = [ [ 1 , 10 ] , [ 5 , 30 ] ] ; NEW_LINE q = len ( queries ) ; NEW_LINE updatePrimes ( ) ; NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( getDifference ( queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static int cntSubsets ( int [ ] arr , int n ) { int max = ( int ) Math . pow ( 2 , n ) ; int result = 0 ; for ( int i = 0 ; i < max ; i ++ ) { int counter = i ; if ( ( counter & ( counter >> 1 ) ) > 0 ) continue ; result ++ ; } return result ; } static public void main ( String [ ] arg ) { int arr [ ] = { 3 , 5 , 7 } ; int n = arr . length ; System . out . println ( cntSubsets ( arr , n ) ) ; } }

def cntSubsets ( arr , n ) : NEW_LINE INDENT max = pow ( 2 , n ) NEW_LINE result = 0 NEW_LINE for i in range ( max ) : NEW_LINE INDENT counter = i NEW_LINE if ( counter & ( counter >> 1 ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT result += 1 NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( cntSubsets ( arr , n ) ) NEW_LINE


class GFG { static int minFlipsSub ( String mat [ ] , int i , int j ) { int cnt0 = 0 , cnt1 = 0 ; if ( mat [ i ] . charAt ( j ) == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i ] . charAt ( j + 1 ) == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] . charAt ( j ) == '1' ) cnt1 ++ ; else cnt0 ++ ; if ( mat [ i + 1 ] . charAt ( j + 1 ) == '1' ) cnt1 ++ ; else cnt0 ++ ; return Math . min ( cnt0 , cnt1 ) ; } static int minFlips ( String mat [ ] , int r , int c ) { int res = Integer . MAX_VALUE ; for ( int i = 0 ; i < r - 1 ; i ++ ) { for ( int j = 0 ; j < c - 1 ; j ++ ) { res = Math . min ( res , minFlipsSub ( mat , i , j ) ) ; } } return res ; } public static void main ( String [ ] args ) { String mat [ ] = { "0101" , "0101" , "0101" } ; int r = mat . length ; int c = mat [ 0 ] . length ( ) ; System . out . print ( minFlips ( mat , r , c ) ) ; } }

import sys NEW_LINE def minFlipsSub ( mat , i , j ) : NEW_LINE INDENT cnt0 = 0 NEW_LINE cnt1 = 0 NEW_LINE if ( mat [ i ] [ j ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i ] [ j + 1 ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i + 1 ] [ j ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT if ( mat [ i + 1 ] [ j + 1 ] == '1' ) : NEW_LINE INDENT cnt1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt0 += 1 NEW_LINE DEDENT return min ( cnt0 , cnt1 ) NEW_LINE DEDENT def minFlips ( mat , r , c ) : NEW_LINE INDENT res = sys . maxsize NEW_LINE for i in range ( r - 1 ) : NEW_LINE INDENT for j in range ( c - 1 ) : NEW_LINE INDENT res = min ( res , minFlipsSub ( mat , i , j ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ "0101" , "0101" , "0101" ] NEW_LINE r = len ( mat ) NEW_LINE c = len ( mat [ 0 ] ) NEW_LINE print ( minFlips ( mat , r , c ) ) NEW_LINE DEDENT


class GFG { static final int n = 5 ; static int FindMaxProduct ( int arr [ ] [ ] , int n ) { int max = 0 , result ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( j - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i ] [ j - 1 ] * arr [ i ] [ j - 2 ] * arr [ i ] [ j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j ] * arr [ i - 2 ] [ j ] * arr [ i - 3 ] [ j ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 3 ) >= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j - 1 ] * arr [ i - 2 ] [ j - 2 ] * arr [ i - 3 ] [ j - 3 ] ; if ( max < result ) max = result ; } if ( ( i - 3 ) >= 0 && ( j - 1 ) <= 0 ) { result = arr [ i ] [ j ] * arr [ i - 1 ] [ j + 1 ] * arr [ i - 2 ] [ j + 2 ] * arr [ i - 3 ] [ j + 3 ] ; if ( max < result ) max = result ; } } } return max ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 6 , 7 , 8 , 9 , 1 } , { 2 , 3 , 4 , 5 , 6 } , { 7 , 8 , 9 , 1 , 0 } , { 9 , 6 , 4 , 2 , 3 } } ; System . out . print ( FindMaxProduct ( arr , n ) ) ; } }

n = 5 NEW_LINE def FindMaxProduct ( arr , n ) : NEW_LINE INDENT max = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( ( j - 3 ) >= 0 ) : NEW_LINE INDENT result = ( arr [ i ] [ j ] * arr [ i ] [ j - 1 ] * arr [ i ] [ j - 2 ] * arr [ i ] [ j - 3 ] ) NEW_LINE if ( max < result ) : NEW_LINE INDENT max = result NEW_LINE DEDENT DEDENT if ( ( i - 3 ) >= 0 ) : NEW_LINE INDENT result = ( arr [ i ] [ j ] * arr [ i - 1 ] [ j ] * arr [ i - 2 ] [ j ] * arr [ i - 3 ] [ j ] ) NEW_LINE if ( max < result ) : NEW_LINE INDENT max = result NEW_LINE DEDENT DEDENT if ( ( i - 3 ) >= 0 and ( j - 3 ) >= 0 ) : NEW_LINE INDENT result = ( arr [ i ] [ j ] * arr [ i - 1 ] [ j - 1 ] * arr [ i - 2 ] [ j - 2 ] * arr [ i - 3 ] [ j - 3 ] ) NEW_LINE if ( max < result ) : NEW_LINE INDENT max = result NEW_LINE DEDENT DEDENT if ( ( i - 3 ) >= 0 and ( j - 1 ) <= 0 ) : NEW_LINE INDENT result = ( arr [ i ] [ j ] * arr [ i - 1 ] [ j + 1 ] * arr [ i - 2 ] [ j + 2 ] * arr [ i - 3 ] [ j + 3 ] ) NEW_LINE if ( max < result ) : NEW_LINE INDENT max = result NEW_LINE DEDENT DEDENT DEDENT DEDENT return max NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 6 , 7 , 8 , 9 , 1 ] , [ 2 , 3 , 4 , 5 , 6 ] , [ 7 , 8 , 9 , 1 , 0 ] , [ 9 , 6 , 4 , 2 , 3 ] ] NEW_LINE print ( FindMaxProduct ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int prevPowerof2 ( int n ) { int p = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; return ( int ) Math . pow ( 2 , p ) ; } static int nextPowerOf2 ( int n ) { int p = 1 ; if ( ( n == 0 ) && ! ( ( n & ( n - 1 ) ) == 0 ) ) return n ; while ( p < n ) p <<= 1 ; return p ; } static int minDiff ( int n ) { int low = prevPowerof2 ( n ) ; int high = nextPowerOf2 ( n ) ; return Math . min ( n - low , high - n ) ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( minDiff ( n ) ) ; } }

from math import log NEW_LINE def prevPowerof2 ( n ) : NEW_LINE INDENT p = int ( log ( n ) ) NEW_LINE return pow ( 2 , p ) NEW_LINE DEDENT def nextPowerOf2 ( n ) : NEW_LINE INDENT p = 1 NEW_LINE if ( n and ( n & ( n - 1 ) ) == 0 ) : NEW_LINE INDENT return n NEW_LINE DEDENT while ( p < n ) : NEW_LINE INDENT p <<= 1 NEW_LINE DEDENT return p NEW_LINE DEDENT def minDiff ( n ) : NEW_LINE INDENT low = prevPowerof2 ( n ) NEW_LINE high = nextPowerOf2 ( n ) NEW_LINE return min ( n - low , high - n ) NEW_LINE DEDENT n = 6 NEW_LINE print ( minDiff ( n ) ) NEW_LINE


class GFG { static int swapBits ( int x , int p1 , int p2 , int n ) { int set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) ; int set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) ; int xor = ( set1 ^ set2 ) ; xor = ( xor << p1 ) | ( xor << p2 ) ; int result = x ^ xor ; return result ; } public static void main ( String [ ] args ) { int res = swapBits ( 28 , 0 , 3 , 2 ) ; System . out . println ( " Result ▁ = ▁ " + res ) ; } }

def swapBits ( x , p1 , p2 , n ) : NEW_LINE INDENT set1 = ( x >> p1 ) & ( ( 1 << n ) - 1 ) NEW_LINE set2 = ( x >> p2 ) & ( ( 1 << n ) - 1 ) NEW_LINE xor = ( set1 ^ set2 ) NEW_LINE xor = ( xor << p1 ) | ( xor << p2 ) NEW_LINE result = x ^ xor NEW_LINE return result NEW_LINE DEDENT res = swapBits ( 28 , 0 , 3 , 2 ) NEW_LINE print ( " Result ▁ = " , res ) NEW_LINE


import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static final int MAX_VAL = 200000 ; static int countIntervals ( int [ ] [ ] arr , int V , int N ) { int min = Integer . MAX_VALUE , max = Integer . MIN_VALUE ; int li , ri ; int [ ] freq = new int [ MAX_VAL ] ; for ( int i = 0 ; i < N ; i ++ ) { li = arr [ i ] [ 0 ] ; freq [ li ] = freq [ li ] + 1 ; ri = arr [ i ] [ 1 ] ; freq [ ri + 1 ] = freq [ ri + 1 ] - 1 ; if ( li < min ) min = li ; if ( ri > max ) max = ri ; } for ( int i = min ; i <= max ; i ++ ) freq [ i ] = freq [ i ] + freq [ i - 1 ] ; return freq [ V ] ; } public static void main ( String args [ ] ) { int [ ] [ ] arr = { { 1 , 10 } , { 5 , 10 } , { 15 , 25 } , { 7 , 12 } , { 20 , 25 } } ; int V = 7 ; int N = arr . length ; System . out . println ( countIntervals ( arr , V , N ) ) ; } }

MAX_VAL = 200000 NEW_LINE def countIntervals ( arr , V , N ) : NEW_LINE INDENT minimum = float ( " inf " ) NEW_LINE maximum = 0 NEW_LINE freq = [ 0 ] * ( MAX_VAL ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT li = arr [ i ] [ 0 ] NEW_LINE freq [ li ] = freq [ li ] + 1 NEW_LINE ri = arr [ i ] [ 1 ] NEW_LINE freq [ ri + 1 ] = freq [ ri + 1 ] - 1 NEW_LINE if li < minimum : NEW_LINE INDENT minimum = li NEW_LINE DEDENT if ri > maximum : NEW_LINE INDENT maximum = ri NEW_LINE DEDENT DEDENT for i in range ( minimum , maximum + 1 ) : NEW_LINE INDENT freq [ i ] = freq [ i ] + freq [ i - 1 ] NEW_LINE DEDENT return freq [ V ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 10 ] , [ 5 , 10 ] , [ 15 , 25 ] , [ 7 , 12 ] , [ 20 , 25 ] ] NEW_LINE V = 7 NEW_LINE N = len ( arr ) NEW_LINE print ( countIntervals ( arr , V , N ) ) NEW_LINE DEDENT


import java . util . Arrays ; import java . util . Comparator ; public class FractionalKnapSack { public static void main ( String [ ] args ) { int [ ] wt = { 10 , 40 , 20 , 30 } ; int [ ] val = { 60 , 40 , 100 , 120 } ; int capacity = 50 ; double maxValue = getMaxValue ( wt , val , capacity ) ; System . out . println ( " Maximum ▁ value ▁ we ▁ can ▁ obtain ▁ = ▁ " + maxValue ) ; } private static double getMaxValue ( int [ ] wt , int [ ] val , int capacity ) { ItemValue [ ] iVal = new ItemValue [ wt . length ] ; for ( int i = 0 ; i < wt . length ; i ++ ) { iVal [ i ] = new ItemValue ( wt [ i ] , val [ i ] , i ) ; } Arrays . sort ( iVal , new Comparator < ItemValue > ( ) { @ Override public int compare ( ItemValue o1 , ItemValue o2 ) { return o2 . cost . compareTo ( o1 . cost ) ; } } ) ; double totalValue = 0d ; for ( ItemValue i : iVal ) { int curWt = ( int ) i . wt ; int curVal = ( int ) i . val ; if ( capacity - curWt >= 0 ) { capacity = capacity - curWt ; totalValue += curVal ; } else { double fraction = ( ( double ) capacity / ( double ) curWt ) ; totalValue += ( curVal * fraction ) ; capacity = ( int ) ( capacity - ( curWt * fraction ) ) ; break ; } } return totalValue ; } static class ItemValue { Double cost ; double wt , val , ind ; public ItemValue ( int wt , int val , int ind ) { this . wt = wt ; this . val = val ; this . ind = ind ; cost = new Double ( val / wt ) ; } } }

class ItemValue : NEW_LINE INDENT def __init__ ( self , wt , val , ind ) : NEW_LINE INDENT self . wt = wt NEW_LINE self . val = val NEW_LINE self . ind = ind NEW_LINE self . cost = val // wt NEW_LINE DEDENT def __lt__ ( self , other ) : NEW_LINE INDENT return self . cost < other . cost NEW_LINE DEDENT DEDENT class FractionalKnapSack : NEW_LINE INDENT @ staticmethod NEW_LINE def getMaxValue ( wt , val , capacity ) : NEW_LINE INDENT iVal = [ ] NEW_LINE for i in range ( len ( wt ) ) : NEW_LINE INDENT iVal . append ( ItemValue ( wt [ i ] , val [ i ] , i ) ) NEW_LINE DEDENT iVal . sort ( reverse = True ) NEW_LINE totalValue = 0 NEW_LINE for i in iVal : NEW_LINE INDENT curWt = int ( i . wt ) NEW_LINE curVal = int ( i . val ) NEW_LINE if capacity - curWt >= 0 : NEW_LINE INDENT capacity -= curWt NEW_LINE totalValue += curVal NEW_LINE DEDENT else : NEW_LINE INDENT fraction = capacity / curWt NEW_LINE totalValue += curVal * fraction NEW_LINE capacity = int ( capacity - ( curWt * fraction ) ) NEW_LINE break NEW_LINE DEDENT DEDENT return totalValue NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT wt = [ 10 , 40 , 20 , 30 ] NEW_LINE val = [ 60 , 40 , 100 , 120 ] NEW_LINE capacity = 50 NEW_LINE maxValue = FractionalKnapSack . getMaxValue ( wt , val , capacity ) NEW_LINE print ( " Maximum ▁ value ▁ in ▁ Knapsack ▁ = " , maxValue ) NEW_LINE DEDENT


class GFG { static int MAX = 100000 ; static boolean [ ] prime = new boolean [ 100002 ] ; static void SieveOfEratosthenes ( ) { for ( int p = 2 ; p * p <= MAX ; p ++ ) if ( prime [ p ] == false ) for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = true ; } static int superpower ( int n ) { SieveOfEratosthenes ( ) ; int superPower = 0 , factor = 0 ; int i = 2 ; while ( n > 1 && i <= MAX ) { if ( ! prime [ i ] ) { factor = 0 ; while ( n % i == 0 && n > 1 ) { factor ++ ; n = n / i ; } if ( superPower < factor ) superPower = factor ; } i ++ ; } return superPower ; } public static void main ( String [ ] args ) { int n = 256 ; System . out . println ( superpower ( n ) ) ; } }

MAX = 100000 ; NEW_LINE prime = [ True ] * 100002 ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT p = 2 ; NEW_LINE while ( p * p <= MAX ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT i = p * 2 ; NEW_LINE while ( i <= MAX ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE i += p ; NEW_LINE DEDENT DEDENT p += 1 ; NEW_LINE DEDENT DEDENT def superpower ( n ) : NEW_LINE INDENT SieveOfEratosthenes ( ) ; NEW_LINE superPower = 0 ; NEW_LINE factor = 0 ; NEW_LINE i = 2 ; NEW_LINE while ( n > 1 and i <= MAX ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT factor = 0 ; NEW_LINE while ( n % i == 0 and n > 1 ) : NEW_LINE INDENT factor += 1 ; NEW_LINE n = int ( n / i ) ; NEW_LINE DEDENT if ( superPower < factor ) : NEW_LINE INDENT superPower = factor ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT return superPower ; NEW_LINE DEDENT n = 256 ; NEW_LINE print ( superpower ( n ) ) ; NEW_LINE


import java . util . * ; class GFG { static final int N = 100000 ; static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static int longestFibSubarray ( int arr [ ] , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , max_val ) ; int [ ] left = new int [ n ] ; int [ ] right = new int [ n ] ; int fibcount = 0 , res = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = fibcount ; if ( hash . contains ( arr [ i ] ) ) { fibcount ++ ; } else fibcount = 0 ; } fibcount = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { right [ i ] = fibcount ; if ( hash . contains ( arr [ i ] ) ) { fibcount ++ ; } else fibcount = 0 ; } for ( int i = 0 ; i < n ; i ++ ) res = Math . max ( res , left [ i ] + right [ i ] ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 8 , 5 , 7 , 3 , 5 , 7 } ; int n = arr . length ; System . out . print ( longestFibSubarray ( arr , n ) + " \n " ) ; } }

N = 100000 NEW_LINE def createHash ( hash , maxElement ) : NEW_LINE INDENT prev = 0 NEW_LINE curr = 1 NEW_LINE hash . add ( prev ) NEW_LINE hash . add ( curr ) NEW_LINE while ( curr <= maxElement ) : NEW_LINE INDENT temp = curr + prev NEW_LINE hash . add ( temp ) NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE DEDENT DEDENT def longestFibSubarray ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE hash = { int } NEW_LINE createHash ( hash , max_val ) NEW_LINE left = [ 0 for i in range ( n ) ] NEW_LINE right = [ 0 for i in range ( n ) ] NEW_LINE fibcount = 0 NEW_LINE res = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT left [ i ] = fibcount NEW_LINE if ( arr [ i ] in hash ) : NEW_LINE INDENT fibcount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT fibcount = 0 NEW_LINE DEDENT DEDENT fibcount = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT right [ i ] = fibcount NEW_LINE if ( arr [ i ] in hash ) : NEW_LINE INDENT fibcount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT fibcount = 0 NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT res = max ( res , left [ i ] + right [ i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 2 , 8 , 5 , 7 , 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestFibSubarray ( arr , n ) ) NEW_LINE


class GFG { static int cntSubArr ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int curr_gcd = 0 ; for ( int j = i ; j < n ; j ++ ) { curr_gcd = __gcd ( curr_gcd , arr [ j ] ) ; ans += ( curr_gcd == 1 ) ? 1 : 0 ; } } return ans ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( cntSubArr ( arr , n ) ) ; } }

from math import gcd NEW_LINE def cntSubArr ( arr , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr_gcd = 0 ; NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT curr_gcd = gcd ( curr_gcd , arr [ j ] ) ; NEW_LINE ans += ( curr_gcd == 1 ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntSubArr ( arr , n ) ) ; NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class GFG { static int N = 3 ; static double calcDeterminant ( int arr [ ] ) { double determinant = 0 ; for ( int i = 0 ; i < N ; i ++ ) { determinant += Math . pow ( arr [ i ] , 3 ) ; } determinant -= 3 * arr [ 0 ] * arr [ 1 ] * arr [ 2 ] ; return determinant ; } static public void main ( String args [ ] ) { int [ ] arr = { 4 , 5 , 3 } ; System . out . println ( calcDeterminant ( arr ) ) ; } }

def calcDeterminant ( arr , n ) : NEW_LINE INDENT determinant = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT determinant += pow ( arr [ i ] , 3 ) NEW_LINE DEDENT determinant -= 3 * arr [ 0 ] * arr [ 1 ] * arr [ 2 ] NEW_LINE return determinant NEW_LINE DEDENT arr = [ 4 , 5 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( calcDeterminant ( arr , n ) ) NEW_LINE


import java . util . * ; import java . text . * ; class solution { static void foot ( float a , float b , float c , float d , float x1 , float y1 , float z1 ) { float k = ( - a * x1 - b * y1 - c * z1 - d ) / ( float ) ( a * a + b * b + c * c ) ; float x2 = a * k + x1 ; float y2 = b * k + y1 ; float z2 = c * k + z1 ; DecimalFormat form = new DecimalFormat ( "0.0" ) ; System . out . print ( " ▁ x2 ▁ = ▁ " + form . format ( x2 ) ) ; System . out . print ( " ▁ y2 ▁ = ▁ " + form . format ( y2 ) ) ; System . out . print ( " ▁ z2 ▁ = ▁ " + form . format ( z2 ) ) ; } public static void main ( String arr [ ] ) { float a = 1 ; float b = - 2 ; float c = 0 ; float d = 0 ; float x1 = - 1 ; float y1 = 3 ; float z1 = 4 ; foot ( a , b , c , d , x1 , y1 , z1 ) ; } }

def foot ( a , b , c , d , x1 , y1 , z1 ) : NEW_LINE INDENT k = ( - a * x1 - b * y1 - c * z1 - d ) / ( a * a + b * b + c * c ) ; NEW_LINE x2 = a * k + x1 ; NEW_LINE y2 = b * k + y1 ; NEW_LINE z2 = c * k + z1 ; NEW_LINE print ( " x2 ▁ = " , round ( x2 , 1 ) ) NEW_LINE print ( " y2 ▁ = " , round ( y2 , 1 ) ) NEW_LINE print ( " z2 ▁ = " , round ( z2 , 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 1 NEW_LINE b = - 2 NEW_LINE c = 0 NEW_LINE d = 0 NEW_LINE x1 = - 1 NEW_LINE y1 = 3 NEW_LINE z1 = 4 NEW_LINE foot ( a , b , c , d , x1 , y1 , z1 ) NEW_LINE DEDENT


class GFG { static float area ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 ) { return ( float ) Math . abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) ; } static boolean check ( int x1 , int y1 , int x2 , int y2 , int x3 , int y3 , int x4 , int y4 , int x , int y ) { float A = area ( x1 , y1 , x2 , y2 , x3 , y3 ) + area ( x1 , y1 , x4 , y4 , x3 , y3 ) ; float A1 = area ( x , y , x1 , y1 , x2 , y2 ) ; float A2 = area ( x , y , x2 , y2 , x3 , y3 ) ; float A3 = area ( x , y , x3 , y3 , x4 , y4 ) ; float A4 = area ( x , y , x1 , y1 , x4 , y4 ) ; return ( A == A1 + A2 + A3 + A4 ) ; } public static void main ( String [ ] args ) { if ( check ( 0 , 10 , 10 , 0 , 0 , - 10 , - 10 , 0 , 10 , 15 ) ) System . out . print ( " yes " ) ; else System . out . print ( " no " ) ; } }

def area ( x1 , y1 , x2 , y2 , x3 , y3 ) : NEW_LINE INDENT return abs ( ( x1 * ( y2 - y3 ) + x2 * ( y3 - y1 ) + x3 * ( y1 - y2 ) ) / 2.0 ) NEW_LINE DEDENT def check ( x1 , y1 , x2 , y2 , x3 , y3 , x4 , y4 , x , y ) : NEW_LINE INDENT A = ( area ( x1 , y1 , x2 , y2 , x3 , y3 ) + area ( x1 , y1 , x4 , y4 , x3 , y3 ) ) NEW_LINE A1 = area ( x , y , x1 , y1 , x2 , y2 ) NEW_LINE A2 = area ( x , y , x2 , y2 , x3 , y3 ) NEW_LINE A3 = area ( x , y , x3 , y3 , x4 , y4 ) NEW_LINE A4 = area ( x , y , x1 , y1 , x4 , y4 ) ; NEW_LINE return ( A == A1 + A2 + A3 + A4 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT if ( check ( 0 , 10 , 10 , 0 , 0 , - 10 , - 10 , 0 , 10 , 15 ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT DEDENT


def sum ( N ) : NEW_LINE INDENT S1 = ( ( N // 2 ) ) * ( 2 * 2 + ( N // 2 - 1 ) * 2 ) // 2 NEW_LINE S2 = ( ( N // 7 ) ) * ( 2 * 7 + ( N // 7 - 1 ) * 7 ) // 2 NEW_LINE S3 = ( ( N // 14 ) ) * ( 2 * 14 + ( N // 14 - 1 ) * 14 ) // 2 NEW_LINE return S1 + S2 - S3 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 20 NEW_LINE print ( sum ( N ) ) NEW_LINE DEDENT


import java . io . * ; class GeeksforGeeks { static double compute ( int a , int b ) { double AM , GM , HM ; AM = ( a + b ) / 2 ; GM = Math . sqrt ( a * b ) ; HM = ( GM * GM ) / AM ; return HM ; } public static void main ( String args [ ] ) { int a = 5 , b = 15 ; double HM = compute ( a , b ) ; String str = " " ; str = str + HM ; System . out . print ( " Harmonic ▁ Mean ▁ between ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + str . substring ( 0 , 5 ) ) ; } }

import math NEW_LINE def compute ( a , b ) : NEW_LINE INDENT AM = ( a + b ) / 2 NEW_LINE GM = math . sqrt ( a * b ) NEW_LINE HM = ( GM * GM ) / AM NEW_LINE return HM NEW_LINE DEDENT a = 5 NEW_LINE b = 15 NEW_LINE HM = compute ( a , b ) NEW_LINE print ( " Harmonic ▁ Mean ▁ between ▁ " , a , " ▁ and ▁ " , b , " ▁ is ▁ " , HM ) NEW_LINE


import java . util . * ; class GFG { static int [ ] status = new int [ 100000 ] ; static int countSteps ( int [ ] val , int n ) { int sol = 0 ; Vector < Integer > vec = new Vector < > ( 1 ) ; vec . add ( 0 ) ; status [ 0 ] = - 1 ; for ( int i = 1 ; i < n ; ++ i ) { status [ i ] = 0 ; while ( vec . size ( ) > 0 && val [ vec . lastElement ( ) ] >= val [ i ] ) { status [ i ] = Math . max ( status [ i ] , status [ vec . lastElement ( ) ] + 1 ) ; vec . remove ( vec . lastElement ( ) ) ; } if ( vec . isEmpty ( ) ) { status [ i ] = - 1 ; } vec . add ( i ) ; sol = Math . max ( sol , status [ i ] + 1 ) ; } return sol ; } public static void main ( String [ ] args ) { int val [ ] = { 6 , 5 , 8 , 4 , 7 , 10 , 9 } ; int n = val . length ; System . out . println ( countSteps ( val , n ) ) ; } }

status = [ 0 ] * 100000 ; NEW_LINE def countSteps ( val , n ) : NEW_LINE INDENT sol = 0 ; NEW_LINE vec = [ 1 , 0 ] ; NEW_LINE status [ 0 ] = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT status [ i ] = 0 ; NEW_LINE while ( len ( vec ) > 0 and val [ vec [ len ( vec ) - 1 ] ] >= val [ i ] ) : NEW_LINE INDENT status [ i ] = max ( status [ i ] , status [ len ( vec ) - 1 ] + 1 ) ; NEW_LINE vec . pop ( ) ; NEW_LINE DEDENT if ( len ( vec ) == 0 ) : NEW_LINE INDENT status [ i ] = - 1 ; NEW_LINE DEDENT vec . append ( i ) ; NEW_LINE sol = max ( sol , status [ i ] + 1 ) ; NEW_LINE DEDENT return sol ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT val = [ 6 , 5 , 8 , 4 , 7 , 10 , 9 ] ; NEW_LINE n = len ( val ) ; NEW_LINE print ( countSteps ( val , n ) ) ; NEW_LINE DEDENT


import java . util . Arrays ; public class GFG { static int printSmallest ( int a [ ] ) { int sum , sum1 ; Arrays . sort ( a ) ; int i , j , k , num ; for ( i = 0 ; i < 3 ; i ++ ) { if ( a [ i ] % 3 == 0 ) { return a [ i ] ; } } for ( i = 0 ; i < 3 ; i ++ ) { for ( j = 0 ; j < 3 ; j ++ ) { num = ( a [ i ] * 10 ) + a [ j ] ; if ( num % 3 == 0 ) { return num ; } } } return a [ 0 ] * 100 + a [ 0 ] * 10 + a [ 0 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 7 , 1 } ; System . out . println ( printSmallest ( arr ) ) ; } }

def printSmallest ( a , n ) : NEW_LINE INDENT sum0 , sum1 = 0 , 0 NEW_LINE a = sorted ( a ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 3 == 0 ) : NEW_LINE INDENT return a [ i ] NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT num = ( a [ i ] * 10 ) + a [ j ] NEW_LINE if ( num % 3 == 0 ) : NEW_LINE INDENT return num NEW_LINE DEDENT DEDENT DEDENT return a [ 0 ] * 100 + a [ 0 ] * 10 + a [ 0 ] NEW_LINE DEDENT arr = [ 7 , 7 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( printSmallest ( arr , n ) ) NEW_LINE


public class GFG { static void printExpansion ( String str ) { for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { String subStr = str . substring ( i ) ; System . out . print ( subStr ) ; } } public static void main ( String args [ ] ) { String str = " geeks " ; printExpansion ( str ) ; } }

def printExpansion ( str ) : NEW_LINE INDENT for i in range ( len ( str ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( i , len ( str ) ) : NEW_LINE INDENT print ( str [ j ] , end = " " ) NEW_LINE DEDENT DEDENT DEDENT str = " geeks " NEW_LINE printExpansion ( str ) NEW_LINE


class GFG { static void printFactorialNums ( int n ) { int fact = 1 ; int x = 2 ; while ( fact <= n ) { System . out . print ( fact + " ▁ " ) ; fact = fact * x ; x ++ ; } } public static void main ( String [ ] args ) { int n = 100 ; printFactorialNums ( n ) ; } }

def printFactorialNums ( n ) : NEW_LINE INDENT fact = 1 NEW_LINE x = 2 NEW_LINE while fact <= n : NEW_LINE INDENT print ( fact , end = " ▁ " ) NEW_LINE fact = fact * x NEW_LINE x += 1 NEW_LINE DEDENT DEDENT n = 100 NEW_LINE printFactorialNums ( n ) NEW_LINE


import java . io . * ; class GFG { static void dupLastIndex ( int arr [ ] , int n ) { if ( arr == null || n <= 0 ) return ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( arr [ i ] == arr [ i - 1 ] ) { System . out . println ( " Last ▁ index : " + i ) ; System . out . println ( " Last ▁ duplicate ▁ item : ▁ " + arr [ i ] ) ; return ; } } System . out . print ( " no ▁ duplicate ▁ found " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 5 , 6 , 6 , 7 , 9 } ; int n = arr . length ; dupLastIndex ( arr , n ) ; } }

def dupLastIndex ( arr , n ) : NEW_LINE INDENT if ( arr == None or n <= 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT print ( " Last ▁ index : " , i , " \n Last " , " duplicate ▁ item : " , arr [ i ] ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " no ▁ duplicate ▁ found " ) NEW_LINE DEDENT arr = [ 1 , 5 , 5 , 6 , 6 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE dupLastIndex ( arr , n ) NEW_LINE


import java . util . * ; class GFG { static int MinimizeleftOverSum ( int a [ ] , int n ) { Vector < Integer > v1 = new Vector < Integer > ( ) , v2 = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) v1 . add ( a [ i ] ) ; else v2 . add ( a [ i ] ) ; } if ( v1 . size ( ) > v2 . size ( ) ) { Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; int x = v1 . size ( ) - v2 . size ( ) - 1 ; int sum = 0 ; int i = 0 ; while ( i < x ) { sum += v1 . get ( i ++ ) ; } return sum ; } else if ( v2 . size ( ) > v1 . size ( ) ) { Collections . sort ( v1 ) ; Collections . sort ( v2 ) ; int x = v2 . size ( ) - v1 . size ( ) - 1 ; int sum = 0 ; int i = 0 ; while ( i < x ) { sum += v2 . get ( i ++ ) ; } return sum ; } else return 0 ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 2 , 2 , 2 } ; int n = a . length ; System . out . println ( MinimizeleftOverSum ( a , n ) ) ; } }

def MinimizeleftOverSum ( a , n ) : NEW_LINE INDENT v1 , v2 = [ ] , [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 ) : NEW_LINE INDENT v1 . append ( a [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT v2 . append ( a [ i ] ) ; NEW_LINE DEDENT DEDENT if ( len ( v1 ) > len ( v2 ) ) : NEW_LINE INDENT v1 . sort ( ) ; NEW_LINE v2 . sort ( ) ; NEW_LINE x = len ( v1 ) - len ( v2 ) - 1 ; NEW_LINE sum = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < x ) : NEW_LINE INDENT sum += v1 [ i ] ; NEW_LINE i += 1 NEW_LINE DEDENT return sum ; NEW_LINE DEDENT elif ( len ( v2 ) > len ( v1 ) ) : NEW_LINE INDENT v1 . sort ( ) ; NEW_LINE v2 . sort ( ) ; NEW_LINE x = len ( v2 ) - len ( v1 ) - 1 ; NEW_LINE sum = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < x ) : NEW_LINE INDENT sum += v2 [ i ] ; NEW_LINE i += 1 NEW_LINE DEDENT return sum ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 2 , 2 , 2 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( MinimizeleftOverSum ( a , n ) ) ; NEW_LINE DEDENT


import java . util . * ; class Eulerian { public static void precisionCompute ( int x , int y , int n ) { if ( y == 0 ) { System . out . print ( " Infinite " ) ; return ; } if ( x == 0 ) { System . out . print ( "0" ) ; return ; } if ( n <= 0 ) { System . out . print ( x / y ) ; return ; } if ( ( ( x > 0 ) && ( y < 0 ) ) || ( ( x < 0 ) && ( y > 0 ) ) ) { System . out . print ( " - " ) ; x = x > 0 ? x : - x ; y = y > 0 ? y : - y ; } int d = x / y ; for ( int i = 0 ; i <= n ; i ++ ) { System . out . print ( d ) ; x = x - ( y * d ) ; if ( x == 0 ) break ; x = x * 10 ; d = x / y ; if ( i == 0 ) System . out . print ( " . " ) ; } } public static void main ( String [ ] args ) { int x = 22 , y = 7 , n = 15 ; precisionCompute ( x , y , n ) ; } }

def precisionCompute ( x , y , n ) : NEW_LINE INDENT if y == 0 : NEW_LINE INDENT print ( " Infinite " ) ; NEW_LINE return ; NEW_LINE DEDENT if x == 0 : NEW_LINE INDENT print ( 0 ) ; NEW_LINE return ; NEW_LINE DEDENT if n <= 0 : NEW_LINE INDENT print ( x / y ) ; NEW_LINE return ; NEW_LINE DEDENT if ( ( ( x > 0 ) and ( y < 0 ) ) or ( ( x < 0 ) and ( y > 0 ) ) ) : NEW_LINE INDENT print ( " - " , end = " " ) ; NEW_LINE if x < 0 : NEW_LINE INDENT x = - x ; NEW_LINE DEDENT if y < 0 : NEW_LINE INDENT y = - y ; NEW_LINE DEDENT DEDENT d = x / y ; NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT print ( d ) ; NEW_LINE x = x - ( y * d ) ; NEW_LINE if x == 0 : NEW_LINE INDENT break ; NEW_LINE DEDENT x = x * 10 ; NEW_LINE d = x / y ; NEW_LINE if ( i == 0 ) : NEW_LINE INDENT print ( " . " , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT x = 22 ; NEW_LINE y = 7 ; NEW_LINE n = 15 ; NEW_LINE precisionCompute ( x , y , n ) ; NEW_LINE


import java . util . * ; class GFG { public static int MinimumCost ( int cost [ ] , int n , int W ) { Vector < Integer > val = new Vector < Integer > ( ) ; Vector < Integer > wt = new Vector < Integer > ( ) ; int size = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( cost [ i ] != - 1 ) { val . add ( cost [ i ] ) ; wt . add ( i + 1 ) ; size ++ ; } } n = size ; int min_cost [ ] [ ] = new int [ n + 1 ] [ W + 1 ] ; for ( int i = 0 ; i <= W ; i ++ ) min_cost [ 0 ] [ i ] = Integer . MAX_VALUE ; for ( int i = 1 ; i <= n ; i ++ ) min_cost [ i ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= W ; j ++ ) { if ( wt . get ( i - 1 ) > j ) min_cost [ i ] [ j ] = min_cost [ i - 1 ] [ j ] ; else min_cost [ i ] [ j ] = Math . min ( min_cost [ i - 1 ] [ j ] , min_cost [ i ] [ j - wt . get ( i - 1 ) ] + val . get ( i - 1 ) ) ; } } return ( min_cost [ n ] [ W ] == Integer . MAX_VALUE ) ? - 1 : min_cost [ n ] [ W ] ; } public static void main ( String [ ] args ) { int cost [ ] = { 1 , 2 , 3 , 4 , 5 } , W = 5 ; int n = cost . length ; System . out . println ( MinimumCost ( cost , n , W ) ) ; } }

INF = 1000000 NEW_LINE def MinimumCost ( cost , n , W ) : NEW_LINE INDENT val = list ( ) NEW_LINE wt = list ( ) NEW_LINE size = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( cost [ i ] != - 1 ) : NEW_LINE INDENT val . append ( cost [ i ] ) NEW_LINE wt . append ( i + 1 ) NEW_LINE size += 1 NEW_LINE DEDENT DEDENT n = size NEW_LINE min_cost = [ [ 0 for i in range ( W + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( W + 1 ) : NEW_LINE INDENT min_cost [ 0 ] [ i ] = INF NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT min_cost [ i ] [ 0 ] = 0 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , W + 1 ) : NEW_LINE INDENT if ( wt [ i - 1 ] > j ) : NEW_LINE INDENT min_cost [ i ] [ j ] = min_cost [ i - 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT min_cost [ i ] [ j ] = min ( min_cost [ i - 1 ] [ j ] , min_cost [ i ] [ j - wt [ i - 1 ] ] + val [ i - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT if ( min_cost [ n ] [ W ] == INF ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return min_cost [ n ] [ W ] NEW_LINE DEDENT DEDENT cost = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE W = 5 NEW_LINE n = len ( cost ) NEW_LINE print ( MinimumCost ( cost , n , W ) ) NEW_LINE


import java . util . * ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int lcm ( int x , int y ) { int temp = ( x * y ) / __gcd ( x , y ) ; return temp ; } static void findNumbers ( int a [ ] , int n , int b [ ] , int m ) { int lcmA = 1 , gcdB = 0 ; for ( int i = 0 ; i < n ; i ++ ) lcmA = lcm ( lcmA , a [ i ] ) ; for ( int i = 0 ; i < m ; i ++ ) gcdB = __gcd ( gcdB , b [ i ] ) ; if ( gcdB % lcmA != 0 ) { System . out . print ( " - 1" ) ; return ; } int num = lcmA ; while ( num <= gcdB ) { if ( gcdB % num == 0 ) System . out . print ( num + " ▁ " ) ; num += lcmA ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 4 } ; int b [ ] = { 16 , 32 , 64 } ; int n = a . length ; int m = b . length ; findNumbers ( a , n , b , m ) ; } }

from math import gcd NEW_LINE def lcm ( x , y ) : NEW_LINE INDENT temp = ( x * y ) // gcd ( x , y ) ; NEW_LINE return temp ; NEW_LINE DEDENT def findNumbers ( a , n , b , m ) : NEW_LINE INDENT lcmA = 1 ; __gcdB = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT lcmA = lcm ( lcmA , a [ i ] ) ; NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT __gcdB = gcd ( __gcdB , b [ i ] ) ; NEW_LINE DEDENT if ( __gcdB % lcmA != 0 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT num = lcmA ; NEW_LINE while ( num <= __gcdB ) : NEW_LINE INDENT if ( __gcdB % num == 0 ) : NEW_LINE INDENT print ( num , end = " ▁ " ) ; NEW_LINE DEDENT num += lcmA ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 2 , 4 ] ; NEW_LINE b = [ 16 , 32 , 64 ] ; NEW_LINE n = len ( a ) ; NEW_LINE m = len ( b ) ; NEW_LINE findNumbers ( a , n , b , m ) ; NEW_LINE DEDENT


class GFG { static int maxSubStr ( String str , int n ) { int count0 = 0 , count1 = 0 ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == '0' ) { count0 ++ ; } else { count1 ++ ; } if ( count0 == count1 ) { cnt ++ ; } } if ( count0 != count1 ) { return - 1 ; } return cnt ; } public static void main ( String [ ] args ) { String str = "0100110101" ; int n = str . length ( ) ; System . out . println ( maxSubStr ( str , n ) ) ; } }

def maxSubStr ( str , n ) : NEW_LINE INDENT count0 = 0 NEW_LINE count1 = 0 NEW_LINE cnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if str [ i ] == '0' : NEW_LINE INDENT count0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count1 += 1 NEW_LINE DEDENT if count0 == count1 : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT if count0 != count1 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT str = "0100110101" NEW_LINE n = len ( str ) NEW_LINE print ( maxSubStr ( str , n ) ) NEW_LINE


import java . util . Arrays ; public class GFG { static void solve ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; if ( k == 1 && arr [ n - 2 ] != arr [ n - 1 ] ) { System . out . println ( arr [ n - 1 ] ) ; return ; } int count = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == arr [ i + 1 ] ) count ++ ; else count = 1 ; if ( count == k && ( i == 0 || ( arr [ i - 1 ] != arr [ i ] ) ) ) { System . out . println ( arr [ i ] ) ; return ; } } System . out . println ( " No ▁ such ▁ element " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 } ; int k = 2 ; int n = arr . length ; solve ( arr , n , k ) ; } }

def solve ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE if ( k == 1 and arr [ n - 2 ] != arr [ n - 1 ] ) : NEW_LINE INDENT print ( arr [ n - 1 ] ) NEW_LINE return NEW_LINE DEDENT count = 1 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = 1 NEW_LINE DEDENT if ( count == k and ( i == 0 or ( arr [ i - 1 ] != arr [ i ] ) ) ) : NEW_LINE INDENT print ( arr [ i ] ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " No ▁ such ▁ element " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE solve ( arr , n , k ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; public class GfG { public final static int SIZE = 26 ; public static int longSubstring ( String str1 , int k ) { int [ ] freq = new int [ SIZE ] ; char [ ] str = str1 . toCharArray ( ) ; int start = 0 ; int maxLen = 0 ; char ch ; int n = str1 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ch = str [ i ] ; freq [ ch - ' a ' ] ++ ; if ( freq [ ch - ' a ' ] > k ) { if ( maxLen < ( i - start ) ) maxLen = i - start ; while ( freq [ ch - ' a ' ] > k ) { freq [ str [ start ] - ' a ' ] -- ; start ++ ; } } } if ( maxLen < ( n - start ) ) maxLen = n - start ; return maxLen ; } public static void main ( String argc [ ] ) { String str = " babcaag " ; int k = 1 ; System . out . println ( " Length ▁ = ▁ " + longSubstring ( str , k ) ) ; } }

import numpy as np NEW_LINE SIZE = 26 NEW_LINE def longSub ( str , k ) : NEW_LINE INDENT freq = np . zeros ( 26 , dtype = np . int ) NEW_LINE start = 0 NEW_LINE maxLen = 0 NEW_LINE n = len ( str ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT ch = str [ i ] NEW_LINE freq [ ord ( ch ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( freq [ ord ( ch ) - ord ( ' a ' ) ] > k ) : NEW_LINE INDENT if ( maxLen < ( i - start ) ) : NEW_LINE INDENT maxLen = i - start NEW_LINE DEDENT while ( freq [ ord ( ch ) - ord ( ' a ' ) ] > k ) : NEW_LINE INDENT freq [ ord ( str [ start ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE start = start + 1 NEW_LINE DEDENT DEDENT DEDENT if ( maxLen < ( n - start ) ) : NEW_LINE INDENT maxLen = n - start NEW_LINE DEDENT return maxLen ; NEW_LINE DEDENT str = " babcaag " NEW_LINE k = 1 NEW_LINE print ( " Length ▁ = " , longSub ( str , k ) ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { public static void psuedoBinary ( int n ) { while ( n != 0 ) { int temp = n , m = 0 , p = 1 ; while ( temp != 0 ) { int rem = temp % 10 ; temp = temp / 10 ; if ( rem != 0 ) m += p ; p *= 10 ; } System . out . print ( m + " ▁ " ) ; n = n - m ; } System . out . println ( " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 31 ; psuedoBinary ( n ) ; } }

def psuedoBinary ( n ) : NEW_LINE INDENT while ( n > 0 ) : NEW_LINE INDENT temp = n ; NEW_LINE m = 0 ; NEW_LINE p = 1 ; NEW_LINE while ( temp ) : NEW_LINE INDENT rem = temp % 10 ; NEW_LINE temp = int ( temp / 10 ) ; NEW_LINE if ( rem != 0 ) : NEW_LINE INDENT m += p ; NEW_LINE DEDENT p *= 10 ; NEW_LINE DEDENT print ( m , end = " ▁ " ) ; NEW_LINE n = n - m ; NEW_LINE DEDENT DEDENT n = 31 ; NEW_LINE psuedoBinary ( n ) ; NEW_LINE


class GFG { static void findEncryptedArray ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( sum - arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 3 , 2 , 4 } ; int N = arr . length ; findEncryptedArray ( arr , N ) ; } }

def findEncryptedArray ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( sum - arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 1 , 3 , 2 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE findEncryptedArray ( arr , N ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean prime [ ] = new boolean [ 100005 ] ; static void SieveOfEratosthenes ( int n ) { Arrays . fill ( prime , true ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < n ; i += p ) { prime [ i ] = false ; } } } } static void sortPrimes ( int arr [ ] , int n ) { SieveOfEratosthenes ( 100005 ) ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { v . add ( arr [ i ] ) ; } } Comparator comparator = Collections . reverseOrder ( ) ; Collections . sort ( v , comparator ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { arr [ i ] = v . get ( j ++ ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 2 , 6 , 100 , 17 } ; int n = arr . length ; sortPrimes ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }

def SieveOfEratosthenes ( n ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= n : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def sortPrimes ( arr , n ) : NEW_LINE INDENT SieveOfEratosthenes ( 100005 ) NEW_LINE v = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v . sort ( reverse = True ) NEW_LINE j = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT arr [ i ] = v [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return arr NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 2 , 6 , 100 , 17 ] NEW_LINE n = len ( arr ) NEW_LINE prime = [ True ] * 100006 NEW_LINE arr = sortPrimes ( arr , n ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int nondecdigits ( int n ) { int x = 0 ; for ( x = n ; x >= 1 ; x -- ) { int no = x ; int prev_dig = 11 ; boolean flag = true ; while ( no != 0 ) { if ( prev_dig < no % 10 ) { flag = false ; break ; } prev_dig = no % 10 ; no /= 10 ; } if ( flag == true ) break ; } return x ; } public static void main ( String [ ] args ) { int n = 200 ; System . out . println ( nondecdigits ( n ) ) ; } }

def nondecdigits ( n ) : NEW_LINE INDENT x = 0 NEW_LINE for x in range ( n , 0 , - 1 ) : NEW_LINE INDENT no = x NEW_LINE prev_dig = 11 NEW_LINE flag = True NEW_LINE while ( no != 0 ) : NEW_LINE INDENT if ( prev_dig < no % 10 ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT prev_dig = no % 10 NEW_LINE no //= 10 NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 200 NEW_LINE print ( nondecdigits ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean checkPrime ( String number ) { if ( number . length ( ) == 0 ) return true ; int num = Integer . parseInt ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) if ( ( num % i ) == 0 ) return false ; return true ; } static int splitIntoPrimes ( String number ) { int numLen = number . length ( ) ; int [ ] splitDP = new int [ numLen + 1 ] ; Arrays . fill ( splitDP , - 1 ) ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 6 && checkPrime ( number . substring ( 0 , i ) ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 6 && i + j <= numLen ; j ++ ) { if ( checkPrime ( number . substring ( i , i + j ) ) ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void main ( String [ ] args ) { System . out . print ( splitIntoPrimes ( "13499315" ) + " \n " ) ; System . out . print ( splitIntoPrimes ( "43" ) + " \n " ) ; } }

from math import sqrt NEW_LINE def checkPrime ( number ) : NEW_LINE INDENT if ( len ( number ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT num = int ( number ) NEW_LINE for i in range ( 2 , int ( sqrt ( num ) ) + 1 , 1 ) : NEW_LINE INDENT if ( ( num % i ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def splitIntoPrimes ( number ) : NEW_LINE INDENT numLen = len ( number ) NEW_LINE splitDP = [ - 1 for i in range ( numLen + 1 ) ] NEW_LINE for i in range ( 1 , numLen + 1 , 1 ) : NEW_LINE INDENT if ( i <= 6 and checkPrime ( number [ 0 : i ] ) ) : NEW_LINE INDENT splitDP [ i ] = 1 NEW_LINE DEDENT if ( splitDP [ i ] != - 1 ) : NEW_LINE INDENT j = 1 NEW_LINE while ( j <= 6 and i + j <= numLen ) : NEW_LINE INDENT if ( checkPrime ( number [ i : i + j ] ) ) : NEW_LINE INDENT if ( splitDP [ i + j ] == - 1 ) : NEW_LINE INDENT splitDP [ i + j ] = 1 + splitDP [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT splitDP [ i + j ] = min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT return splitDP [ numLen ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( splitIntoPrimes ( "13499315" ) ) NEW_LINE print ( splitIntoPrimes ( "43" ) ) NEW_LINE DEDENT


class GFG { static int countUnsetBits ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = i ; while ( temp > 0 ) { if ( temp % 2 == 0 ) { cnt = cnt + 1 ; } temp = temp / 2 ; } } return cnt ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( countUnsetBits ( n ) ) ; } }

def countUnsetBits ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT temp = i ; NEW_LINE while ( temp ) : NEW_LINE INDENT if ( temp % 2 == 0 ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT temp = temp // 2 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE print ( countUnsetBits ( n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int n = 3 ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPrefixCount ( int p_arr [ ] [ ] , boolean arr [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( arr [ i ] [ j ] == false ? 1 : 0 ) ; } } static int matrixOrValueOne ( boolean arr [ ] [ ] ) { int [ ] [ ] p_arr = new int [ n ] [ n ] ; findPrefixCount ( p_arr , arr ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; Stack < pair > q = new Stack < pair > ( ) ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . size ( ) != 0 && q . peek ( ) . first > p_arr [ i ] [ j ] ) { to_sum -= ( q . peek ( ) . second + 1 ) * ( q . peek ( ) . first - p_arr [ i ] [ j ] ) ; c += q . peek ( ) . second + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; count_zero_submatrices += to_sum ; q . add ( new pair ( p_arr [ i ] [ j ] , c ) ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } public static void main ( String [ ] args ) { boolean arr [ ] [ ] = { { false , false , false } , { false , true , false } , { false , false , false } } ; System . out . println ( matrixOrValueOne ( arr ) ) ; } }

def findPrefixCount ( p_arr , arr ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if arr [ i ] [ j ] : NEW_LINE INDENT continue NEW_LINE DEDENT if j != n - 1 : NEW_LINE INDENT p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] NEW_LINE DEDENT p_arr [ i ] [ j ] += int ( not arr [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT def matrixOrValueOne ( arr ) : NEW_LINE INDENT p_arr = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE findPrefixCount ( p_arr , arr ) NEW_LINE count_zero_submatrices = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT i = n - 1 NEW_LINE q = [ ] NEW_LINE to_sum = 0 NEW_LINE while i >= 0 : NEW_LINE INDENT c = 0 NEW_LINE while ( len ( q ) != 0 and q [ - 1 ] [ 0 ] > p_arr [ i ] [ j ] ) : NEW_LINE INDENT to_sum -= ( ( q [ - 1 ] [ 1 ] + 1 ) * ( q [ - 1 ] [ 0 ] - p_arr [ i ] [ j ] ) ) NEW_LINE c += q . pop ( ) [ 1 ] + 1 NEW_LINE DEDENT to_sum += p_arr [ i ] [ j ] NEW_LINE count_zero_submatrices += to_sum NEW_LINE q . append ( ( p_arr [ i ] [ j ] , c ) ) NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT return ( ( n * ( n + 1 ) * n * ( n + 1 ) ) // 4 - count_zero_submatrices ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE arr = [ [ 0 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 0 ] ] NEW_LINE print ( matrixOrValueOne ( arr ) ) NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int [ ] dp = new int [ W + 1 ] ; Arrays . fill ( dp , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = W ; j >= wt [ i ] ; j -- ) dp [ j ] = Math . max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; return dp [ W ] ; } public static void main ( String [ ] args ) { int val [ ] = { 7 , 8 , 4 } , wt [ ] = { 3 , 8 , 6 } , W = 10 , n = 3 ; System . out . println ( KnapSack ( val , wt , n , W ) ) ; } }

def KnapSack ( val , wt , n , W ) : NEW_LINE INDENT dp = [ 0 ] * ( W + 1 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( W , wt [ i ] , - 1 ) : NEW_LINE INDENT dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; NEW_LINE DEDENT DEDENT return dp [ W ] ; NEW_LINE DEDENT val = [ 7 , 8 , 4 ] ; NEW_LINE wt = [ 3 , 8 , 6 ] ; NEW_LINE W = 10 ; n = 3 ; NEW_LINE print ( KnapSack ( val , wt , n , W ) ) ; NEW_LINE


import java . * ; public class GFG { static int getOccurence ( int n , int d ) { int result = 0 ; int itr = d ; while ( itr <= n ) { if ( itr % 10 == d ) result ++ ; if ( itr != 0 && itr / 10 == d ) { result ++ ; itr ++ ; } else if ( itr / 10 == d - 1 ) itr = itr + ( 10 - d ) ; else itr = itr + 10 ; } return result ; } public static void main ( String [ ] args ) { int n = 11 , d = 1 ; System . out . println ( getOccurence ( n , d ) ) ; } }

import math ; NEW_LINE def getOccurence ( n , d ) : NEW_LINE INDENT result = 0 ; NEW_LINE itr = d ; NEW_LINE while ( itr <= n ) : NEW_LINE INDENT if ( itr % 10 == d ) : NEW_LINE INDENT result += 1 ; NEW_LINE DEDENT if ( itr != 0 and math . floor ( itr / 10 ) == d ) : NEW_LINE INDENT result += 1 ; NEW_LINE itr += 1 ; NEW_LINE DEDENT elif ( math . floor ( itr / 10 ) == d - 1 ) : NEW_LINE INDENT itr = itr + ( 10 - d ) ; NEW_LINE DEDENT else : NEW_LINE INDENT itr = itr + 10 ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT n = 11 ; NEW_LINE d = 1 ; NEW_LINE print ( getOccurence ( n , d ) ) ; NEW_LINE


import java . util . * ; class GFG { static int min_val = Integer . MAX_VALUE ; static int min_steps = 0 ; static int sumOfDigits ( int n ) { String s = String . valueOf ( n ) ; int sum = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum += ( s . charAt ( i ) - '0' ) ; } return sum ; } static void Transform ( int n , int d , int steps ) { if ( n < min_val ) { min_val = n ; min_steps = steps ; } else if ( n == min_val ) { min_steps = Math . min ( min_steps , steps ) ; } if ( steps < 15 ) { Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; Transform ( n + d , d , steps + 1 ) ; } } public static void main ( String [ ] args ) { int N = 9 , D = 3 ; Transform ( N , D , 0 ) ; System . out . print ( min_val + " ▁ " + min_steps ) ; } }

import sys ; NEW_LINE min_val = sys . maxsize ; NEW_LINE min_steps = 0 ; NEW_LINE def sumOfDigits ( n ) : NEW_LINE INDENT s = str ( n ) ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT sum += ( ord ( s [ i ] ) - ord ( '0' ) ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def Transform ( n , d , steps ) : NEW_LINE INDENT global min_val ; global min_steps ; NEW_LINE if ( n < min_val ) : NEW_LINE INDENT min_val = n ; NEW_LINE min_steps = steps ; NEW_LINE DEDENT elif ( n == min_val ) : NEW_LINE INDENT min_steps = min ( min_steps , steps ) ; NEW_LINE DEDENT if ( steps < 15 ) : NEW_LINE INDENT Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; NEW_LINE Transform ( n + d , d , steps + 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 ; D = 3 ; NEW_LINE Transform ( N , D , 0 ) ; NEW_LINE print ( min_val , min_steps ) ; NEW_LINE DEDENT


class GFG { static boolean isPalindrome ( int num ) { int reverse_num = 0 , remainder , temp ; temp = num ; while ( temp != 0 ) { remainder = temp % 10 ; reverse_num = reverse_num * 10 + remainder ; temp /= 10 ; } if ( reverse_num == num ) { return true ; } return false ; } static boolean isOddLength ( int num ) { int count = 0 ; while ( num > 0 ) { num /= 10 ; count ++ ; } if ( count % 2 != 0 ) { return true ; } return false ; } static long sumOfAllPalindrome ( int L , int R ) { long sum = 0 ; if ( L <= R ) for ( int i = L ; i <= R ; i ++ ) { if ( isPalindrome ( i ) && isOddLength ( i ) ) { sum += i ; } } return sum ; } public static void main ( String [ ] args ) { int L = 110 , R = 1130 ; System . out . println ( sumOfAllPalindrome ( L , R ) ) ; } }

def isPalindrome ( num ) : NEW_LINE INDENT reverse_num = 0 NEW_LINE temp = num NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT remainder = temp % 10 NEW_LINE reverse_num = reverse_num * 10 + remainder NEW_LINE temp = int ( temp / 10 ) NEW_LINE DEDENT if ( reverse_num == num ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isOddLength ( num ) : NEW_LINE INDENT count = 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT num = int ( num / 10 ) NEW_LINE count += 1 NEW_LINE DEDENT if ( count % 2 != 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def sumOfAllPalindrome ( L , R ) : NEW_LINE INDENT sum = 0 NEW_LINE if ( L <= R ) : NEW_LINE INDENT for i in range ( L , R + 1 , 1 ) : NEW_LINE INDENT if ( isPalindrome ( i ) and isOddLength ( i ) ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 110 NEW_LINE R = 1130 NEW_LINE print ( sumOfAllPalindrome ( L , R ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { Set < Integer > s = new HashSet < Integer > ( ) ; int j ; for ( j = i ; j < n ; j ++ ) { s . add ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }

def minRange ( arr , n , k ) : NEW_LINE INDENT l = 0 NEW_LINE r = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = [ ] NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT s . append ( arr [ j ] ) NEW_LINE if ( len ( s ) == k ) : NEW_LINE INDENT if ( ( j - i ) < ( r - l ) ) : NEW_LINE INDENT r = j NEW_LINE l = i NEW_LINE DEDENT break NEW_LINE DEDENT DEDENT if ( j == n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( l == 0 and r == n ) : NEW_LINE INDENT print ( " Invalid ▁ k " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( l , r ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE minRange ( arr , n , k ) NEW_LINE DEDENT


import java . io . * ; class Increasing { void findStrictlyIncreasingNum ( int start , String out , int n ) { if ( n == 0 ) { System . out . print ( out + " ▁ " ) ; return ; } for ( int i = start ; i <= 9 ; i ++ ) { String str = out + Integer . toString ( i ) ; findStrictlyIncreasingNum ( i + 1 , str , n - 1 ) ; } } public static void main ( String args [ ] ) throws IOException { Increasing obj = new Increasing ( ) ; int n = 3 ; obj . findStrictlyIncreasingNum ( 0 , " ▁ " , n ) ; } }

def findStrictlyIncreasingNum ( start , out , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT print ( out , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT for i in range ( start , 10 ) : NEW_LINE INDENT str1 = out + str ( i ) NEW_LINE findStrictlyIncreasingNum ( i + 1 , str1 , n - 1 ) NEW_LINE DEDENT DEDENT n = 3 NEW_LINE findStrictlyIncreasingNum ( 0 , " " , n ) NEW_LINE


import java . lang . Math ; import java . util . * ; class GFG { public static void findX ( int [ ] a , int n ) { Collections . sort ( Arrays . asList ( a ) , null ) ; int itr = a [ n - 1 ] ; int p = ( int ) ( Math . log ( itr ) / Math . log ( 2 ) ) + 1 ; int x = 0 ; for ( int i = 0 ; i < p ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count ++ ; } if ( count > ( n / 2 ) ) { x += 1 << i ; } } long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( x ^ a [ i ] ) ; System . out . println ( " X ▁ = ▁ " + x + " , ▁ Sum ▁ = ▁ " + sum ) ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; findX ( a , n ) ; } }

from math import log2 NEW_LINE def findX ( arr , n ) : NEW_LINE INDENT itr = arr [ 0 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] > itr ) : NEW_LINE INDENT itr = arr [ i ] NEW_LINE DEDENT DEDENT p = int ( log2 ( itr ) ) + 1 NEW_LINE X = 0 NEW_LINE for i in range ( p ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr [ j ] & ( 1 << i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count > int ( n / 2 ) ) : NEW_LINE INDENT X += 1 << i NEW_LINE DEDENT DEDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ( X ^ arr [ i ] ) NEW_LINE DEDENT print ( " X ▁ = " , X , " , ▁ Sum ▁ = " , sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE findX ( arr , n ) NEW_LINE DEDENT


class GFG { static int k_sum ( int a [ ] , int n , int k ) { int r = 0 , sum = 0 ; int ans = 0 ; for ( int l = 0 ; l < n ; l ++ ) { while ( sum < k ) { if ( r == n ) break ; else { sum += a [ r ] ; r ++ ; } } if ( sum < k ) break ; ans += n - r + 1 ; sum -= a [ l ] ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 6 , 1 , 2 , 7 } , k = 10 ; int n = a . length ; System . out . println ( k_sum ( a , n , k ) ) ; } }

def k_sum ( a , n , k ) : NEW_LINE INDENT r , sum = 0 , 0 ; NEW_LINE ans = 0 ; NEW_LINE for l in range ( n ) : NEW_LINE INDENT while ( sum < k ) : NEW_LINE INDENT if ( r == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT else : NEW_LINE INDENT sum += a [ r ] ; NEW_LINE r += 1 ; NEW_LINE DEDENT DEDENT if ( sum < k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT ans += n - r + 1 ; NEW_LINE sum -= a [ l ] ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT a = [ 6 , 1 , 2 , 7 ] ; k = 10 ; NEW_LINE n = len ( a ) ; NEW_LINE print ( k_sum ( a , n , k ) ) ; NEW_LINE


import java . lang . * ; class GFG { public static double per ( double a , double b ) { return ( a + b ) ; } public static double area ( double s ) { return ( s / 2 ) ; } public static void main ( String [ ] args ) { double a = 7 , b = 8 , s = 10 ; System . out . println ( per ( a , b ) ) ; System . out . println ( area ( s ) ) ; } }

def per ( a , b ) : NEW_LINE INDENT return ( a + b ) NEW_LINE DEDENT def area ( s ) : NEW_LINE INDENT return ( s / 2 ) NEW_LINE DEDENT a = 7 NEW_LINE b = 8 NEW_LINE s = 10 NEW_LINE print ( per ( a , b ) ) NEW_LINE print ( area ( s ) ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static boolean isRevDiffDivisible ( int x , int k ) { int n = x ; int m = 0 ; int flag ; while ( x > 0 ) { m = m * 10 + x % 10 ; x /= 10 ; } return ( Math . abs ( n - m ) % k == 0 ) ; } static int countNumbers ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) if ( isRevDiffDivisible ( i , k ) ) ++ count ; return count ; } public static void main ( String args [ ] ) { int l = 35 , r = 45 , k = 5 ; System . out . println ( countNumbers ( l , r , k ) ) ; } }

def isRevDiffDivisible ( x , k ) : NEW_LINE INDENT n = x ; m = 0 NEW_LINE while ( x > 0 ) : NEW_LINE INDENT m = m * 10 + x % 10 NEW_LINE x = x // 10 NEW_LINE DEDENT return ( abs ( n - m ) % k == 0 ) NEW_LINE DEDENT def countNumbers ( l , r , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( isRevDiffDivisible ( i , k ) ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT l = 20 ; r = 23 ; k = 6 NEW_LINE print ( countNumbers ( l , r , k ) ) NEW_LINE


class GenerateArrays { void generateUtil ( int A [ ] , int B [ ] , int C [ ] , int i , int j , int m , int n , int len , boolean flag ) { if ( flag ) { if ( len != 0 ) printArr ( C , len + 1 ) ; for ( int k = i ; k < m ; k ++ ) { if ( len == 0 ) { C [ len ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len , ! flag ) ; } else if ( A [ k ] > C [ len ] ) { C [ len + 1 ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len + 1 , ! flag ) ; } } } else { for ( int l = j ; l < n ; l ++ ) { if ( B [ l ] > C [ len ] ) { C [ len + 1 ] = B [ l ] ; generateUtil ( A , B , C , i , l + 1 , m , n , len + 1 , ! flag ) ; } } } } void generate ( int A [ ] , int B [ ] , int m , int n ) { int C [ ] = new int [ m + n ] ; generateUtil ( A , B , C , 0 , 0 , m , n , 0 , true ) ; } void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { GenerateArrays generate = new GenerateArrays ( ) ; int A [ ] = { 10 , 15 , 25 } ; int B [ ] = { 5 , 20 , 30 } ; int n = A . length ; int m = B . length ; generate . generate ( A , B , n , m ) ; } }

def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def generateUtil ( A , B , C , i , j , m , n , len , flag ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT if ( len ) : NEW_LINE INDENT printArr ( C , len + 1 ) NEW_LINE DEDENT for k in range ( i , m ) : NEW_LINE INDENT if ( not len ) : NEW_LINE INDENT C [ len ] = A [ k ] NEW_LINE generateUtil ( A , B , C , k + 1 , j , m , n , len , not flag ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( A [ k ] > C [ len ] ) : NEW_LINE INDENT C [ len + 1 ] = A [ k ] NEW_LINE generateUtil ( A , B , C , k + 1 , j , m , n , len + 1 , not flag ) NEW_LINE DEDENT DEDENT DEDENT DEDENT else : NEW_LINE INDENT for l in range ( j , n ) : NEW_LINE INDENT if ( B [ l ] > C [ len ] ) : NEW_LINE INDENT C [ len + 1 ] = B [ l ] NEW_LINE generateUtil ( A , B , C , i , l + 1 , m , n , len + 1 , not flag ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def generate ( A , B , m , n ) : NEW_LINE INDENT C = [ ] NEW_LINE for i in range ( m + n + 1 ) : NEW_LINE INDENT C . append ( 0 ) NEW_LINE DEDENT generateUtil ( A , B , C , 0 , 0 , m , n , 0 , True ) NEW_LINE DEDENT A = [ 10 , 15 , 25 ] NEW_LINE B = [ 5 , 20 , 30 ] NEW_LINE n = len ( A ) NEW_LINE m = len ( B ) NEW_LINE generate ( A , B , n , m ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GFG { public static int findNth ( int n ) { int count = 0 ; for ( int curr = 1 ; ; curr ++ ) { int sum = 0 ; for ( int x = curr ; x > 0 ; x = x / 10 ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } } public static void main ( String [ ] args ) { System . out . print ( findNth ( 5 ) ) ; } }

import itertools NEW_LINE def findNth ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for curr in itertools . count ( ) : NEW_LINE INDENT sum = 0 NEW_LINE x = curr NEW_LINE while ( x ) : NEW_LINE INDENT sum = sum + x % 10 NEW_LINE x = x // 10 NEW_LINE DEDENT if ( sum == 10 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if ( count == n ) : NEW_LINE INDENT return curr NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( findNth ( 5 ) ) NEW_LINE DEDENT


class GFG { static boolean check ( int mid , int array [ ] , int n , int K ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] > mid ) return false ; sum += array [ i ] ; if ( sum > mid ) { count ++ ; sum = array [ i ] ; } } count ++ ; if ( count <= K ) return true ; return false ; } static int solve ( int array [ ] , int n , int K ) { int start = 1 ; int end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { end += array [ i ] ; } int answer = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( check ( mid , array , n , K ) ) { answer = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } return answer ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 2 , 3 , 4 } ; int n = array . length ; int K = 3 ; System . out . println ( solve ( array , n , K ) ) ; } }

def check ( mid , array , n , K ) : NEW_LINE INDENT count = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( array [ i ] > mid ) : NEW_LINE INDENT return False NEW_LINE DEDENT sum += array [ i ] NEW_LINE if ( sum > mid ) : NEW_LINE INDENT count += 1 NEW_LINE sum = array [ i ] NEW_LINE DEDENT DEDENT count += 1 NEW_LINE if ( count <= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def solve ( array , n , K ) : NEW_LINE INDENT start = 1 NEW_LINE end = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT end += array [ i ] NEW_LINE DEDENT answer = 0 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if ( check ( mid , array , n , K ) ) : NEW_LINE INDENT answer = mid NEW_LINE end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT array = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( array ) NEW_LINE K = 3 NEW_LINE print ( solve ( array , n , K ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float rectanglearea ( float a , float b ) { if ( a < 0 || b < 0 ) return - 1 ; return 2 * a * b ; } public static void main ( String args [ ] ) { float a = 10 , b = 8 ; System . out . println ( rectanglearea ( a , b ) ) ; } }

def rectanglearea ( a , b ) : NEW_LINE INDENT if a < 0 or b < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return 2 * a * b NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 10 , 8 NEW_LINE print ( rectanglearea ( a , b ) ) NEW_LINE DEDENT


class GFG { static long mod_power ( long a , long b , long mod ) { long result = 1l ; while ( b > 0 ) { if ( ( b & 1 ) == 0 ) { result = a * a ; a %= mod ; b /= 2 ; } else { result = result * a ; result %= mod ; } } return result ; } static long nCr_RangeSum ( long n , long r1 , long r2 , long p ) { long num = 1 , den = 1 ; long sum = 0l ; if ( r1 == 0 ) sum += 1l ; for ( int i = 0 ; i < r2 ; i ++ ) { num = ( num * ( n - i ) ) % p ; den = ( den * ( i + 1 ) ) % p ; if ( i - r1 >= - 1 && ( i - r1 + 1 ) % 2 == 0 ) { sum += ( num * mod_power ( den , p - 2 , p ) ) % p ; sum %= p ; } } return sum ; } static long compute ( String A , String B , int N ) { int c0A = 0 , c1A = 0 , c0B = 0 , c1B = 0 ; for ( char c : A . toCharArray ( ) ) { if ( c == '1' ) c1A += 1 ; else if ( c == '0' ) c0A += 1 ; } for ( char c : B . toCharArray ( ) ) { if ( c == '1' ) c1B += 1 ; else if ( c == '0' ) c0B += 1 ; } int max1xor = Math . min ( c0A , c1B ) + Math . min ( c1A , c0B ) ; int min1xor = N - Math . min ( c0B , c0A ) - Math . min ( c1A , c1B ) ; long ans = nCr_RangeSum ( N , min1xor , max1xor , 1000000000 + 7 ) ; return ans ; } public static void main ( String [ ] args ) { int N = 3 ; String A = "010" ; String B = "100" ; System . out . print ( compute ( A , B , N ) ) ; } }

def nCrRangeSum ( n , r1 , r2 , p ) : NEW_LINE INDENT num = den = 1 NEW_LINE sum = 0 NEW_LINE if r1 == 0 : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT for i in range ( r2 ) : NEW_LINE INDENT num = ( num * ( n - i ) ) % p NEW_LINE den = ( den * ( i + 1 ) ) % p NEW_LINE if ( i - r1 >= - 1 and ( i - r1 + 1 ) % 2 == 0 ) : NEW_LINE INDENT sum += ( num * pow ( den , p - 2 , p ) ) % p NEW_LINE sum %= p NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def compute ( A , B ) : NEW_LINE INDENT c0A = c1A = c0B = c1B = 0 NEW_LINE for c in A : NEW_LINE INDENT if c == '1' : NEW_LINE INDENT c1A += 1 NEW_LINE DEDENT elif c == '0' : NEW_LINE INDENT c0A += 1 NEW_LINE DEDENT DEDENT for c in B : NEW_LINE INDENT if c == '1' : NEW_LINE INDENT c1B += 1 NEW_LINE DEDENT elif c == '0' : NEW_LINE INDENT c0B += 1 NEW_LINE DEDENT DEDENT max1xor = min ( c0A , c1B ) + min ( c1A , c0B ) NEW_LINE min1xor = N - min ( c0B , c0A ) - min ( c1A , c1B ) NEW_LINE ans = nCrRangeSum ( N , min1xor , max1xor , 10 ** 9 + 7 ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE A = "010" NEW_LINE B = "100" NEW_LINE print ( compute ( A , B ) ) NEW_LINE DEDENT


import java . util . * ; class solution { static class TNode { int data ; TNode left ; TNode right ; } static TNode sortedArrayToBST ( int arr [ ] , int start , int end ) { if ( start > end ) return null ; int mid = ( start + end ) / 2 ; TNode root = newNode ( arr [ mid ] ) ; root . left = sortedArrayToBST ( arr , start , mid - 1 ) ; root . right = sortedArrayToBST ( arr , mid + 1 , end ) ; return root ; } static TNode newNode ( int data ) { TNode node = new TNode ( ) ; node . data = data ; node . left = null ; node . right = null ; return node ; } static void printLevelOrder ( TNode root ) { if ( root == null ) return ; Queue < TNode > q = new LinkedList < TNode > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { TNode node = q . element ( ) ; System . out . print ( node . data + " ▁ " ) ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; TNode root = sortedArrayToBST ( arr , 0 , n - 1 ) ; printLevelOrder ( root ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def sortedArrayToBST ( arr ) : NEW_LINE INDENT if not arr : NEW_LINE INDENT return None NEW_LINE DEDENT mid = int ( ( len ( arr ) ) / 2 ) NEW_LINE root = Node ( arr [ mid ] ) NEW_LINE root . left = sortedArrayToBST ( arr [ : mid ] ) NEW_LINE root . right = sortedArrayToBST ( arr [ mid + 1 : ] ) NEW_LINE return root NEW_LINE DEDENT def printLevelOrder ( root ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while q != [ ] : NEW_LINE INDENT node = q . pop ( 0 ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE if node . left : NEW_LINE INDENT q . append ( node . left ) NEW_LINE DEDENT if node . right : NEW_LINE INDENT q . append ( node . right ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE root = sortedArrayToBST ( arr ) NEW_LINE printLevelOrder ( root ) NEW_LINE


import java . util . * ; class GFG { static String constructString ( int n ) { String s = " " ; for ( int i = 0 ; i < n ; i ++ ) { s += "10" ; } return s ; } static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } static int findNumber ( int n ) { String s = constructString ( n - 1 ) ; int num = binaryToDecimal ( s ) ; return num ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( findNumber ( n ) ) ; } }

def constructString ( n ) : NEW_LINE INDENT s = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT s += "10" NEW_LINE DEDENT return s NEW_LINE DEDENT def binaryToDecimal ( n ) : NEW_LINE INDENT num = n NEW_LINE dec_value = 0 NEW_LINE base = 1 NEW_LINE Len = len ( num ) NEW_LINE for i in range ( Len - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( num [ i ] == '1' ) : NEW_LINE INDENT dec_value += base NEW_LINE DEDENT base = base * 2 NEW_LINE DEDENT return dec_value NEW_LINE DEDENT def findNumber ( n ) : NEW_LINE INDENT s = constructString ( n - 1 ) NEW_LINE num = binaryToDecimal ( s ) NEW_LINE return num NEW_LINE DEDENT n = 4 NEW_LINE print ( findNumber ( n ) ) NEW_LINE


class GFG { static int digitSum ( int n ) { int ans = 0 ; while ( n != 0 ) { ans += n % 10 ; n /= 10 ; } return ans ; } static int findInt ( int n , int m ) { int minDigit = ( int ) Math . floor ( ( double ) ( m / 9 ) ) ; int start = ( int ) Math . pow ( 10 , minDigit ) - ( int ) Math . pow ( 10 , minDigit ) % n ; while ( start < Integer . MAX_VALUE ) { if ( digitSum ( start ) == m ) return start ; else start += n ; } return - 1 ; } static public void main ( String args [ ] ) { int n = 13 , m = 32 ; System . out . print ( findInt ( n , m ) ) ; } }

from math import floor , pow NEW_LINE import sys NEW_LINE def digitSum ( n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT ans += n % 10 ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return ans NEW_LINE DEDENT def findInt ( n , m ) : NEW_LINE INDENT minDigit = floor ( m / 9 ) NEW_LINE start = ( int ( pow ( 10 , minDigit ) ) - int ( pow ( 10 , minDigit ) ) % n ) NEW_LINE while ( start < sys . maxsize ) : NEW_LINE INDENT if ( digitSum ( start ) == m ) : NEW_LINE INDENT return start NEW_LINE DEDENT else : NEW_LINE INDENT start += n NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 13 NEW_LINE m = 32 NEW_LINE print ( findInt ( n , m ) ) NEW_LINE DEDENT