class GFG { static int operations ( int op [ ] , int n , int k ) { int i , count = 0 ; int nVal = 0 ; int min = Integer . MAX_VALUE ; for ( i = 0 ; i < n ; i ++ ) { nVal += op [ i ] ; min = Math . min ( min , nVal ) ; if ( ( k + nVal ) <= 0 ) return ( i + 1 ) ; } if ( nVal >= 0 ) return - 1 ; int times = ( k - Math . abs ( min ) ) / Math . abs ( nVal ) ; k = ( k - ( times * Math . abs ( nVal ) ) ) ; count = ( times * n ) ; while ( k > 0 ) { for ( i = 0 ; i < n ; i ++ ) { k = k + op [ i ] ; count ++ ; if ( k <= 0 ) break ; } } return count ; } public static void main ( String [ ] args ) { int op [ ] = { - 60 , 65 , - 1 , 14 , - 25 } ; int n = op . length ; int k = 100000 ; System . out . print ( operations ( op , n , k ) ) ; } }

def operations ( op , n , k ) : NEW_LINE INDENT i , count = 0 , 0 NEW_LINE nVal = 0 NEW_LINE minimum = 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT nVal += op [ i ] NEW_LINE minimum = min ( minimum , nVal ) NEW_LINE if ( ( k + nVal ) <= 0 ) : NEW_LINE INDENT return ( i + 1 ) NEW_LINE DEDENT DEDENT if ( nVal >= 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT times = ( k - abs ( minimum ) ) // abs ( nVal ) NEW_LINE k = ( k - ( times * abs ( nVal ) ) ) NEW_LINE count = ( times * n ) NEW_LINE while ( k > 0 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT k = k + op [ i ] NEW_LINE count += 1 NEW_LINE if ( k <= 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT op = [ - 60 , 65 , - 1 , 14 , - 25 ] NEW_LINE n = len ( op ) NEW_LINE k = 100000 NEW_LINE print ( operations ( op , n , k ) ) NEW_LINE


import java . util . * ; class GFG { static int subsequence ( int [ ] S , int [ ] T , int n , int m ) { int [ ] [ ] dp = new int [ n + 1 ] [ m + 1 ] ; int mod = 1000000007 ; for ( int i = 0 ; i <= n ; i ++ ) dp [ i ] [ 0 ] = 1 ; for ( int j = 0 ; j <= m ; j ++ ) dp [ 0 ] [ j ] = 1 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= m ; ++ j ) { dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] - dp [ i - 1 ] [ j - 1 ] ; if ( S [ i - 1 ] == T [ j - 1 ] ) dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] ; dp [ i ] [ j ] += mod ; dp [ i ] [ j ] %= mod ; } } return dp [ n ] [ m ] ; } public static void main ( String [ ] args ) { int S [ ] = { 1 , 1 } ; int n = S . length ; int T [ ] = { 1 , 1 } ; int m = T . length ; System . out . println ( subsequence ( S , T , n , m ) ) ; } }

import numpy as np NEW_LINE mod = int ( 1e9 + 7 ) NEW_LINE def subsequence ( S , T , n , m ) : NEW_LINE INDENT dp = np . zeros ( ( n + 1 , m + 1 ) ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ 0 ] = 1 ; NEW_LINE DEDENT for j in range ( m + 1 ) : NEW_LINE INDENT dp [ 0 ] [ j ] = 1 ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] -   \ NEW_LINE INDENT dp [ i - 1 ] [ j - 1 ] ; NEW_LINE DEDENT if ( S [ i - 1 ] == T [ j - 1 ] ) : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i - 1 ] [ j - 1 ] ; NEW_LINE DEDENT dp [ i ] [ j ] += mod ; NEW_LINE dp [ i ] [ j ] %= mod ; NEW_LINE DEDENT DEDENT return dp [ n ] [ m ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = [ 1 , 1 ] ; NEW_LINE n = len ( S ) ; NEW_LINE T = [ 1 , 1 ] ; NEW_LINE m = len ( T ) ; NEW_LINE print ( subsequence ( S , T , n , m ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int isPowerOfFour ( int n ) { int count = 0 ; int x = n & ( n - 1 ) ; if ( n > 0 && x == 0 ) { while ( n > 1 ) { n >>= 1 ; count += 1 ; } return ( count % 2 == 0 ) ? 1 : 0 ; } return 0 ; } public static void main ( String [ ] args ) { int test_no = 64 ; if ( isPowerOfFour ( test_no ) > 0 ) System . out . println ( test_no + " ▁ is ▁ a ▁ power ▁ of ▁ 4" ) ; else System . out . println ( test_no + " ▁ is ▁ not ▁ a ▁ power ▁ of ▁ 4" ) ; } }

def isPowerOfFour ( n ) : NEW_LINE INDENT count = 0 NEW_LINE if ( n and ( not ( n & ( n - 1 ) ) ) ) : NEW_LINE INDENT while ( n > 1 ) : NEW_LINE INDENT n >>= 1 NEW_LINE count += 1 NEW_LINE DEDENT if ( count % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT test_no = 64 NEW_LINE if ( isPowerOfFour ( 64 ) ) : NEW_LINE INDENT print ( test_no , ' is ▁ a ▁ power ▁ of ▁ 4' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( test_no , ' is ▁ not ▁ a ▁ power ▁ of ▁ 4' ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean checkDistinct ( int x ) { int last = x % 10 ; while ( x > 0 ) { if ( x % 10 != last ) return false ; x = x / 10 ; } return true ; } static int findCount ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( checkDistinct ( i ) ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { int L = 10 , R = 50 ; System . out . println ( findCount ( L , R ) ) ; } }

def checkDistinct ( x ) : NEW_LINE INDENT last = x % 10 NEW_LINE while ( x ) : NEW_LINE INDENT if ( x % 10 != last ) : NEW_LINE INDENT return False NEW_LINE DEDENT x = x // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def findCount ( L , R ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( checkDistinct ( i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT L = 10 NEW_LINE R = 50 NEW_LINE print ( findCount ( L , R ) ) NEW_LINE


class GFG { static void removeDuplicates ( int arr [ ] , int n ) { int st = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 2 && arr [ i ] == arr [ i + 1 ] && arr [ i ] == arr [ i + 2 ] ) continue ; else { arr [ st ] = arr [ i ] ; st ++ ; } } System . out . print ( " { " ) ; for ( int i = 0 ; i < st ; i ++ ) { System . out . print ( arr [ i ] ) ; if ( i != st - 1 ) System . out . print ( " , ▁ " ) ; } System . out . print ( " } " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 } ; int n = arr . length ; removeDuplicates ( arr , n ) ; } }

def removeDuplicates ( arr , n ) : NEW_LINE INDENT st = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i < n - 2 and arr [ i ] == arr [ i + 1 ] and arr [ i ] == arr [ i + 2 ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ st ] = arr [ i ] ; NEW_LINE st += 1 ; NEW_LINE DEDENT DEDENT print ( " { " , end = " " ) NEW_LINE for i in range ( st ) : NEW_LINE INDENT print ( arr [ i ] , end = " " ) ; NEW_LINE if ( i != st - 1 ) : NEW_LINE INDENT print ( " , ▁ " , end = " " ) ; NEW_LINE DEDENT DEDENT print ( " } " , end = " " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 2 , 2 , 2 , 3 , 3 , 3 , 3 , 3 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE removeDuplicates ( arr , n ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int MAX = 1000000 ; static int MOD = 10000007 ; static int [ ] result = new int [ MAX + 1 ] ; static int [ ] fact = new int [ MAX + 1 ] ; static void preCompute ( ) { fact [ 0 ] = 1 ; result [ 0 ] = 1 ; for ( int i = 1 ; i <= MAX ; i ++ ) { fact [ i ] = ( ( fact [ i - 1 ] % MOD ) * i ) % MOD ; result [ i ] = ( ( result [ i - 1 ] % MOD ) * ( fact [ i ] % MOD ) ) % MOD ; } } static void performQueries ( int q [ ] , int n ) { preCompute ( ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . println ( result [ q [ i ] ] ) ; } public static void main ( String [ ] args ) { int q [ ] = { 4 , 5 } ; int n = q . length ; performQueries ( q , n ) ; } }

MAX = 1000000 NEW_LINE MOD = 10 ** 9 + 7 NEW_LINE result = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE fact = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE def preCompute ( ) : NEW_LINE INDENT fact [ 0 ] = 1 NEW_LINE result [ 0 ] = 1 NEW_LINE for i in range ( 1 , MAX + 1 ) : NEW_LINE INDENT fact [ i ] = ( ( fact [ i - 1 ] % MOD ) * i ) % MOD NEW_LINE result [ i ] = ( ( result [ i - 1 ] % MOD ) * ( fact [ i ] % MOD ) ) % MOD NEW_LINE DEDENT DEDENT def performQueries ( q , n ) : NEW_LINE INDENT preCompute ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( result [ q [ i ] ] ) NEW_LINE DEDENT DEDENT q = [ 4 , 5 ] NEW_LINE n = len ( q ) NEW_LINE performQueries ( q , n ) NEW_LINE


import java . io . * ; class GFG { static int findMissingNo ( int [ ] arr , int n ) { int val ; int nextval ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= 0 || arr [ i ] > n ) continue ; val = arr [ i ] ; while ( arr [ val - 1 ] != val ) { nextval = arr [ val - 1 ] ; arr [ val - 1 ] = val ; val = nextval ; if ( val <= 0 || val > n ) break ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != i + 1 ) { return i + 1 ; } } return n + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 } ; int arr_size = arr . length ; int missing = findMissingNo ( arr , arr_size ) ; System . out . println ( " The ▁ smallest ▁ positive " + " ▁ missing ▁ number ▁ is ▁ " + missing ) ; } }

def findMissingNo ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] <= 0 or arr [ i ] > n ) : NEW_LINE INDENT continue NEW_LINE DEDENT val = arr [ i ] NEW_LINE while ( arr [ val - 1 ] != val ) : NEW_LINE INDENT nextval = arr [ val - 1 ] NEW_LINE arr [ val - 1 ] = val NEW_LINE val = nextval NEW_LINE if ( val <= 0 or val > n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != i + 1 ) : NEW_LINE INDENT return i + 1 NEW_LINE DEDENT DEDENT return n + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 7 , 6 , 8 , - 1 , - 10 , 15 ] NEW_LINE arr_size = len ( arr ) NEW_LINE missing = findMissingNo ( arr , arr_size ) NEW_LINE print ( " The ▁ smallest ▁ positive " , " missing ▁ number ▁ is ▁ " , missing ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int numberOfDivisors ( int num ) { int c = 0 ; for ( int i = 1 ; i <= num ; i ++ ) { if ( num % i == 0 ) { c += 1 ; } } return c ; } static int countNumbers ( int n ) { int c = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( numberOfDivisors ( i ) == 9 ) c += 1 ; } return c ; } public static void main ( String [ ] args ) { int n = 1000 ; System . out . print ( countNumbers ( n ) ) ; } }

def numberOfDivisors ( num ) : NEW_LINE INDENT c = 0 NEW_LINE for i in range ( 1 , num + 1 ) : NEW_LINE INDENT if ( num % i == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT def countNumbers ( n ) : NEW_LINE INDENT c = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( numberOfDivisors ( i ) == 9 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1000 NEW_LINE print ( countNumbers ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 10000 ; static int MAX_ELEMENT = 50 ; static int [ ] [ ] [ ] dp = new int [ MAX_ELEMENT ] [ MAX_ELEMENT ] [ MAX ] ; static int Max_Xor ( int arr [ ] , int i , int j , int mask , int n ) { if ( i >= n ) { if ( j == 0 ) return mask ; else return 0 ; } if ( dp [ i ] [ j ] [ mask ] != - 1 ) return dp [ i ] [ j ] [ mask ] ; int ans = 0 ; if ( j > 0 ) ans = Max_Xor ( arr , i + 1 , j - 1 , mask ^ arr [ i ] , n ) ; ans = Math . max ( ans , Max_Xor ( arr , i + 1 , j , mask , n ) ) ; return dp [ i ] [ j ] [ mask ] = ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 } ; int n = arr . length ; int k = 3 ; for ( int i = 0 ; i < MAX_ELEMENT ; i ++ ) { for ( int j = 0 ; j < MAX_ELEMENT ; j ++ ) { for ( int l = 0 ; l < MAX ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; } } System . out . println ( Max_Xor ( arr , 0 , k , 0 , n ) ) ; } }

MAX = 10000 NEW_LINE MAX_ELEMENT = 50 NEW_LINE dp = [ [ [ - 1 for i in range ( MAX ) ] for j in range ( MAX_ELEMENT ) ] for k in range ( MAX_ELEMENT ) ] NEW_LINE def Max_Xor ( arr , i , j , mask , n ) : NEW_LINE INDENT if ( i >= n ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT return mask NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( dp [ i ] [ j ] [ mask ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] [ mask ] NEW_LINE DEDENT ans = 0 NEW_LINE if ( j > 0 ) : NEW_LINE INDENT ans = Max_Xor ( arr , i + 1 , j - 1 , mask ^ arr [ i ] , n ) NEW_LINE DEDENT ans = max ( ans , Max_Xor ( arr , i + 1 , j , mask , n ) ) NEW_LINE dp [ i ] [ j ] [ mask ] = ans NEW_LINE return ans NEW_LINE DEDENT arr = [ 2 , 5 , 4 , 1 , 3 , 7 , 6 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( Max_Xor ( arr , 0 , k , 0 , n ) ) NEW_LINE


import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { public static int countOddSquares ( int n , int m ) { return ( int ) Math . pow ( ( double ) m , 0.5 ) - ( int ) Math . pow ( ( double ) n - 1 , 0.5 ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 100 ; System . out . print ( " Count ▁ is ▁ " + countOddSquares ( n , m ) ) ; } }

def countOddSquares ( n , m ) : NEW_LINE INDENT return int ( m ** 0.5 ) - int ( ( n - 1 ) ** 0.5 ) NEW_LINE DEDENT n = 5 NEW_LINE m = 100 NEW_LINE print ( " Count ▁ is " , countOddSquares ( n , m ) ) NEW_LINE


import java . io . * ; class Binary { static int maxOnesIndex ( int arr [ ] , int n ) { int max_count = 0 ; int max_index = 0 ; int prev_zero = - 1 ; int prev_prev_zero = - 1 ; for ( int curr = 0 ; curr < n ; ++ curr ) { if ( arr [ curr ] == 0 ) { if ( curr - prev_prev_zero > max_count ) { max_count = curr - prev_prev_zero ; max_index = prev_zero ; } prev_prev_zero = prev_zero ; prev_zero = curr ; } } if ( n - prev_prev_zero > max_count ) max_index = prev_zero ; return max_index ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Index ▁ of ▁ 0 ▁ to ▁ be ▁ replaced ▁ is ▁ " + maxOnesIndex ( arr , n ) ) ; } }

def maxOnesIndex ( arr , n ) : NEW_LINE INDENT max_count = 0 NEW_LINE max_index = 0 NEW_LINE prev_zero = - 1 NEW_LINE prev_prev_zero = - 1 NEW_LINE for curr in range ( n ) : NEW_LINE INDENT if ( arr [ curr ] == 0 ) : NEW_LINE INDENT if ( curr - prev_prev_zero > max_count ) : NEW_LINE INDENT max_count = curr - prev_prev_zero NEW_LINE max_index = prev_zero NEW_LINE DEDENT prev_prev_zero = prev_zero NEW_LINE prev_zero = curr NEW_LINE DEDENT DEDENT if ( n - prev_prev_zero > max_count ) : NEW_LINE INDENT max_index = prev_zero NEW_LINE DEDENT return max_index NEW_LINE DEDENT arr = [ 1 , 1 , 0 , 0 , 1 , 0 , 1 , 1 , 1 , 0 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Index ▁ of ▁ 0 ▁ to ▁ be ▁ replaced ▁ is ▁ " , maxOnesIndex ( arr , n ) ) NEW_LINE


import java . io . * ; import java . lang . * ; class GFG { public static double answer ( double X , double K ) { double i = 10 ; double MIN = Math . pow ( i , K - 1 ) ; if ( MIN % X == 0 ) return ( MIN ) ; else return ( ( MIN + X ) - ( ( MIN + X ) % X ) ) ; } public static void main ( String [ ] args ) { double X = 83 ; double K = 5 ; System . out . println ( ( int ) answer ( X , K ) ) ; } }

def answer ( X , K ) : NEW_LINE INDENT MIN = pow ( 10 , K - 1 ) NEW_LINE if ( MIN % X == 0 ) : NEW_LINE INDENT return ( MIN ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( MIN + X ) - ( ( MIN + X ) % X ) ) NEW_LINE DEDENT DEDENT X = 83 ; NEW_LINE K = 5 ; NEW_LINE print ( answer ( X , K ) ) ; NEW_LINE


class GFG { static double extrapolate ( double [ ] [ ] d , double x ) { double y = d [ 0 ] [ 1 ] + ( x - d [ 0 ] [ 0 ] ) / ( d [ 1 ] [ 0 ] - d [ 0 ] [ 0 ] ) * ( d [ 1 ] [ 1 ] - d [ 0 ] [ 1 ] ) ; return y ; } public static void main ( String [ ] args ) { double [ ] [ ] d = { { 1.2 , 2.7 } , { 1.4 , 3.1 } } ; double x = 2.1 ; System . out . println ( " Value ▁ of ▁ y ▁ at ▁ x ▁ = ▁ 2.1 ▁ : ▁ " + extrapolate ( d , x ) ) ; } }

def extrapolate ( d , x ) : NEW_LINE INDENT y = ( d [ 0 ] [ 1 ] + ( x - d [ 0 ] [ 0 ] ) / ( d [ 1 ] [ 0 ] - d [ 0 ] [ 0 ] ) * ( d [ 1 ] [ 1 ] - d [ 0 ] [ 1 ] ) ) ; NEW_LINE return y ; NEW_LINE DEDENT d = [ [ 1.2 , 2.7 ] , [ 1.4 , 3.1 ] ] ; NEW_LINE x = 2.1 ; NEW_LINE print ( " Value ▁ of ▁ y ▁ at ▁ x ▁ = ▁ 2.1 ▁ : " , extrapolate ( d , x ) ) ; NEW_LINE


class GFG { static void ifPossible ( int X , int Y ) { if ( X > Y ) swap ( X , Y ) ; if ( ( X + Y ) % 5 == 0 && 3 * X >= 2 * Y ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } public static void main ( String [ ] args ) { int X = 33 , Y = 27 ; ifPossible ( X , Y ) ; } }

def ifPossible ( X , Y ) : NEW_LINE INDENT if ( X > Y ) : NEW_LINE INDENT X , Y = Y , X NEW_LINE DEDENT if ( ( X + Y ) % 5 == 0 and 3 * X >= 2 * Y ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT X = 33 NEW_LINE Y = 27 NEW_LINE ifPossible ( X , Y ) NEW_LINE


class GFG { static int MAX = 1000 ; static char [ ] replaceSpaces ( char [ ] str ) { int space_count = 0 , i = 0 ; for ( i = 0 ; i < str . length ; i ++ ) if ( str [ i ] == ' ▁ ' ) space_count ++ ; while ( str [ i - 1 ] == ' ▁ ' ) { space_count -- ; i -- ; } int new_length = i + space_count * 2 ; if ( new_length > MAX ) return str ; int index = new_length - 1 ; char [ ] new_str = str ; str = new char [ new_length ] ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( new_str [ j ] == ' ▁ ' ) { str [ index ] = '0' ; str [ index - 1 ] = '2' ; str [ index - 2 ] = ' % ' ; index = index - 3 ; } else { str [ index ] = new_str [ j ] ; index -- ; } } return str ; } public static void main ( String [ ] args ) { char [ ] str = " Mr ▁ John ▁ Smith ▁ " . toCharArray ( ) ; str = replaceSpaces ( str ) ; for ( int i = 0 ; i < str . length ; i ++ ) System . out . print ( str [ i ] ) ; } }

MAX = 1000 ; NEW_LINE def replaceSpaces ( string ) : NEW_LINE INDENT string = string . strip ( ) NEW_LINE i = len ( string ) NEW_LINE space_count = string . count ( ' ▁ ' ) NEW_LINE new_length = i + space_count * 2 NEW_LINE if new_length > MAX : NEW_LINE INDENT return - 1 NEW_LINE DEDENT index = new_length - 1 NEW_LINE string = list ( string ) NEW_LINE for f in range ( i - 2 , new_length - 2 ) : NEW_LINE INDENT string . append ( '0' ) NEW_LINE DEDENT for j in range ( i - 1 , 0 , - 1 ) : NEW_LINE INDENT if string [ j ] == ' ▁ ' : NEW_LINE INDENT string [ index ] = '0' NEW_LINE string [ index - 1 ] = '2' NEW_LINE string [ index - 2 ] = ' % ' NEW_LINE index = index - 3 NEW_LINE DEDENT else : NEW_LINE INDENT string [ index ] = string [ j ] NEW_LINE index -= 1 NEW_LINE DEDENT DEDENT return ' ' . join ( string ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " Mr ▁ John ▁ Smith ▁ " NEW_LINE s = replaceSpaces ( s ) NEW_LINE print ( s ) NEW_LINE DEDENT


import java . util . Arrays ; public class Test { static int arr [ ] = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; static void rotate ( ) { int x = arr [ arr . length - 1 ] , i ; for ( i = arr . length - 1 ; i > 0 ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ 0 ] = x ; } public static void main ( String [ ] args ) { System . out . println ( " Given ▁ Array ▁ is " ) ; System . out . println ( Arrays . toString ( arr ) ) ; rotate ( ) ; System . out . println ( " Rotated ▁ Array ▁ is " ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }

def rotate ( arr , n ) : NEW_LINE INDENT x = arr [ n - 1 ] NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] ; NEW_LINE DEDENT arr [ 0 ] = x ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ array ▁ is " ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT rotate ( arr , n ) NEW_LINE print ( " \n Rotated ▁ array ▁ is " ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT


class CountSubString { int countSubStr ( char str [ ] , int n ) { int m = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( str [ i ] == '1' ) m ++ ; } return m * ( m - 1 ) / 2 ; } public static void main ( String [ ] args ) { CountSubString count = new CountSubString ( ) ; String string = "00100101" ; char str [ ] = string . toCharArray ( ) ; int n = str . length ; System . out . println ( count . countSubStr ( str , n ) ) ; } }

def countSubStr ( st , n ) : NEW_LINE INDENT m = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( st [ i ] == '1' ) : NEW_LINE INDENT m = m + 1 NEW_LINE DEDENT DEDENT return m * ( m - 1 ) // 2 NEW_LINE DEDENT st = "00100101" ; NEW_LINE list ( st ) NEW_LINE n = len ( st ) NEW_LINE print ( countSubStr ( st , n ) , end = " " ) NEW_LINE


import java . util . * ; class GFG { static int countStaircases ( int N ) { int [ ] [ ] memo = new int [ N + 5 ] [ N + 5 ] ; for ( int i = 0 ; i <= N ; i ++ ) { for ( int j = 0 ; j <= N ; j ++ ) { memo [ i ] [ j ] = 0 ; } } memo [ 3 ] [ 2 ] = memo [ 4 ] [ 2 ] = 1 ; for ( int i = 5 ; i <= N ; i ++ ) { for ( int j = 2 ; j <= i ; j ++ ) { if ( j == 2 ) { memo [ i ] [ j ] = memo [ i - j ] [ j ] + 1 ; } else { memo [ i ] [ j ] = memo [ i - j ] [ j ] + memo [ i - j ] [ j - 1 ] ; } } } int answer = 0 ; for ( int i = 1 ; i <= N ; i ++ ) answer = answer + memo [ N ] [ i ] ; return answer ; } public static void main ( String [ ] args ) { int N = 7 ; System . out . println ( countStaircases ( N ) ) ; } }

def countStaircases ( N ) : NEW_LINE INDENT memo = [ [ 0 for x in range ( N + 5 ) ] for y in range ( N + 5 ) ] NEW_LINE for i in range ( N + 1 ) : NEW_LINE INDENT for j in range ( N + 1 ) : NEW_LINE INDENT memo [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT memo [ 3 ] [ 2 ] = memo [ 4 ] [ 2 ] = 1 NEW_LINE for i in range ( 5 , N + 1 ) : NEW_LINE INDENT for j in range ( 2 , i + 1 ) : NEW_LINE INDENT if ( j == 2 ) : NEW_LINE INDENT memo [ i ] [ j ] = memo [ i - j ] [ j ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT memo [ i ] [ j ] = ( memo [ i - j ] [ j ] + memo [ i - j ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT answer = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT answer = answer + memo [ N ] [ i ] NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 NEW_LINE print ( countStaircases ( N ) ) NEW_LINE DEDENT


class GFG { static long dp [ ] [ ] [ ] = new long [ 18 ] [ 2 ] [ 162 ] ; static long solve ( int i , boolean tight , int sum_so_far , int Sum , String number , int len ) { if ( i == len ) { if ( sum_so_far == Sum ) return 1 ; else return 0 ; } long ans = dp [ i ] [ 1 ] [ sum_so_far ] ; if ( ans != - 1 ) { return ans ; } ans = 0 ; boolean ntight ; int nsum_so_far ; for ( char currdigit = '0' ; currdigit <= '9' ; currdigit ++ ) { if ( ! tight && currdigit > number . charAt ( i ) ) { break ; } ntight = tight || currdigit < number . charAt ( i ) ; nsum_so_far = sum_so_far + ( currdigit - '0' ) ; ans += solve ( i + 1 , ntight , nsum_so_far , Sum , number , len ) ; } return ans ; } public static void main ( String [ ] args ) { long count = 0 ; int sum = 4 ; String number = "100" ; for ( int i = 0 ; i < 18 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { for ( int k = 0 ; k < 162 ; k ++ ) dp [ i ] [ j ] [ k ] = - 1 ; } } System . out . println ( solve ( 0 , false , 0 , sum , number , number . length ( ) ) ) ; } }

def solve ( i , tight , sum_so_far , Sum , number , length ) : NEW_LINE INDENT if i == length : NEW_LINE INDENT if sum_so_far == Sum : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT ans = dp [ i ] [ tight ] [ sum_so_far ] NEW_LINE if ans != - 1 : NEW_LINE INDENT return ans NEW_LINE DEDENT ans = 0 NEW_LINE for currdigit in range ( 0 , 10 ) : NEW_LINE INDENT currdigitstr = str ( currdigit ) NEW_LINE if not tight and currdigitstr > number [ i ] : NEW_LINE INDENT break NEW_LINE DEDENT ntight = tight or currdigitstr < number [ i ] NEW_LINE nsum_so_far = sum_so_far + currdigit NEW_LINE ans += solve ( i + 1 , ntight , nsum_so_far , Sum , number , length ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT count , Sum = 0 , 4 NEW_LINE number = "100" NEW_LINE dp = [ [ [ - 1 for i in range ( 162 ) ] for j in range ( 2 ) ] for k in range ( 18 ) ] NEW_LINE print ( solve ( 0 , 0 , 0 , Sum , number , len ( number ) ) ) NEW_LINE DEDENT


static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = 0 , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_ending_here < 0 ) max_ending_here = 0 ; else if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; } return max_so_far ; }

def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = 0 NEW_LINE max_ending_here = 0 NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] NEW_LINE if max_ending_here < 0 : NEW_LINE INDENT max_ending_here = 0 NEW_LINE DEDENT elif ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here NEW_LINE DEDENT DEDENT return max_so_far NEW_LINE DEDENT


class GFG { static int countSC ( int N ) { int res = ( int ) Math . sqrt ( N ) + ( int ) Math . cbrt ( N ) - ( int ) ( Math . sqrt ( Math . cbrt ( N ) ) ) ; return res ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . println ( " Number ▁ of ▁ squares ▁ " + " and ▁ cubes ▁ is ▁ " + countSC ( N ) ) ; } }

import math NEW_LINE def countSC ( N ) : NEW_LINE INDENT res = ( int ( math . sqrt ( N ) ) + int ( N ** ( 1 / 3 ) ) - int ( math . sqrt ( N ** ( 1 / 3 ) ) ) ) NEW_LINE return res NEW_LINE DEDENT N = 20 NEW_LINE print ( " Number ▁ of ▁ squares ▁ and ▁ cubes ▁ is " , countSC ( N ) ) NEW_LINE


class GFG { static void SieveOfSundaram ( boolean marked [ ] , int nNew ) { for ( int i = 1 ; i <= nNew ; i ++ ) for ( int j = i ; ( i + j + 2 * i * j ) <= nNew ; j ++ ) marked [ i + j + 2 * i * j ] = true ; } static int countPrimePairs ( int n ) { int nNew = ( n - 2 ) / 2 ; boolean marked [ ] = new boolean [ nNew + 1 ] ; SieveOfSundaram ( marked , nNew ) ; int count = 0 , prime_num ; for ( int i = 1 ; i <= nNew ; i ++ ) { if ( marked [ i ] == false ) { prime_num = 2 * i + 1 ; count = count + ( prime_num / 2 ) ; } } return count ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( " Number ▁ of ▁ prime ▁ pairs : ▁ " + countPrimePairs ( n ) ) ; } }

def SieveOfSundaram ( marked , nNew ) : NEW_LINE INDENT for i in range ( 1 , nNew + 1 ) : NEW_LINE INDENT for j in range ( i , nNew ) : NEW_LINE INDENT if i + j + 2 * i * j > nNew : NEW_LINE INDENT break NEW_LINE DEDENT marked [ i + j + 2 * i * j ] = True NEW_LINE DEDENT DEDENT DEDENT def countPrimePairs ( n ) : NEW_LINE INDENT nNew = ( n - 2 ) // 2 NEW_LINE marked = [ False for i in range ( nNew + 1 ) ] NEW_LINE SieveOfSundaram ( marked , nNew ) NEW_LINE count , prime_num = 0 , 0 NEW_LINE for i in range ( 1 , nNew + 1 ) : NEW_LINE INDENT if ( marked [ i ] == False ) : NEW_LINE INDENT prime_num = 2 * i + 1 NEW_LINE count = count + ( prime_num // 2 ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 12 NEW_LINE print ( " Number ▁ of ▁ prime ▁ pairs : ▁ " , countPrimePairs ( n ) ) NEW_LINE


class GFG { static void segregate0and1 ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 ) count ++ ; } for ( int i = 0 ; i < count ; i ++ ) arr [ i ] = 0 ; for ( int i = count ; i < n ; i ++ ) arr [ i ] = 1 ; } static void print ( int arr [ ] , int n ) { System . out . print ( " Array ▁ after ▁ segregation ▁ is ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 0 , 1 , 0 , 1 , 1 , 1 } ; int n = arr . length ; segregate0and1 ( arr , n ) ; print ( arr , n ) ; } }

def segregate0and1 ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , count ) : NEW_LINE INDENT arr [ i ] = 0 NEW_LINE DEDENT for i in range ( count , n ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT DEDENT def print_arr ( arr , n ) : NEW_LINE INDENT print ( " Array ▁ after ▁ segregation ▁ is ▁ " , end = " " ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 0 , 1 , 0 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE segregate0and1 ( arr , n ) NEW_LINE print_arr ( arr , n ) NEW_LINE


import java . io . * ; class GFG { static int BitCount [ ] = new int [ 10000 ] ; static void fillSetBitsMatrix ( int arr [ ] , int n ) { for ( int i = 0 ; i < 32 ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { long temp = arr [ j ] >> i ; if ( temp % 2 != 0 ) BitCount [ j ] += 1 ; } } for ( int i = 1 ; i < n ; i ++ ) BitCount [ i ] += BitCount [ i - 1 ] ; } static void Query ( int Q [ ] [ ] , int q ) { for ( int i = 0 ; i < q ; i ++ ) System . out . println ( ( BitCount [ Q [ i ] [ 1 ] ] - BitCount [ Q [ i ] [ 0 ] - 1 ] ) ) ; } public static void main ( String [ ] args ) { int Arr [ ] = { 1 , 5 , 6 , 10 , 9 , 4 , 67 } ; int n = Arr . length ; fillSetBitsMatrix ( Arr , n ) ; int q = 2 ; int Q [ ] [ ] = { { 1 , 5 } , { 2 , 6 } } ; Query ( Q , q ) ; } }

BitCount = [ 0 ] * 10000 NEW_LINE def fillSetBitsmatrix ( arr : list , n : int ) : NEW_LINE INDENT global BitCount NEW_LINE for i in range ( 32 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT temp = arr [ j ] >> i NEW_LINE if temp % 2 != 0 : NEW_LINE INDENT BitCount [ j ] += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT BitCount [ i ] += BitCount [ i - 1 ] NEW_LINE DEDENT DEDENT def Query ( Q : list , q : int ) : NEW_LINE INDENT for i in range ( q ) : NEW_LINE INDENT print ( BitCount [ Q [ i ] [ 1 ] ] - BitCount [ Q [ i ] [ 0 ] - 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Arr = [ 1 , 5 , 6 , 10 , 9 , 4 , 67 ] NEW_LINE n = len ( Arr ) NEW_LINE fillSetBitsmatrix ( Arr , n ) NEW_LINE q = 2 NEW_LINE Q = [ ( 1 , 5 ) , ( 2 , 6 ) ] NEW_LINE Query ( Q , q ) NEW_LINE DEDENT


import java . util . * ; public class Seasons { public static void findSeason ( int M ) { switch ( M ) { case 12 : case 1 : case 2 : System . out . println ( " WINTER " ) ; break ; case 3 : case 4 : case 5 : System . out . println ( " SPRING " ) ; break ; case 6 : case 7 : case 8 : System . out . println ( " SUMMER " ) ; break ; case 9 : case 10 : case 11 : System . out . println ( " AUTUMN " ) ; break ; default : System . out . println ( " Invalid ▁ Month ▁ number " ) ; break ; } } public static void main ( String abc [ ] ) { int M = 5 ; System . out . println ( " For ▁ Month ▁ number : ▁ " + M ) ; findSeason ( M ) ; M = 10 ; System . out . println ( " For ▁ Month ▁ number : ▁ " + M ) ; findSeason ( M ) ; } }

def findseason ( M ) : NEW_LINE INDENT list1 = [ [ 12 , 1 , 2 ] , [ 3 , 4 , 5 ] , [ 6 , 7 , 8 ] , [ 9 , 10 , 11 ] ] NEW_LINE if M in list1 [ 0 ] : NEW_LINE INDENT print ( " WINTER " ) NEW_LINE DEDENT elif M in list1 [ 1 ] : NEW_LINE INDENT print ( " SPRING " ) NEW_LINE DEDENT elif M in list1 [ 2 ] : NEW_LINE INDENT print ( " SUMMER " ) NEW_LINE DEDENT elif M in list1 [ 3 ] : NEW_LINE INDENT print ( " AUTUMN " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid ▁ Month ▁ Number " ) NEW_LINE DEDENT DEDENT M = 5 NEW_LINE print ( " For ▁ Month ▁ number : " , M ) ; NEW_LINE findseason ( M ) NEW_LINE M = 10 NEW_LINE print ( " For ▁ Month ▁ number : " , M ) ; NEW_LINE findseason ( M ) NEW_LINE


import java . util . * ; public class GFG { static class Graph { int V ; LinkedList < Integer > [ ] adj ; Graph ( int V ) { this . V = V ; adj = new LinkedList [ V ] ; for ( int i = 0 ; i < adj . length ; i ++ ) adj [ i ] = new LinkedList < Integer > ( ) ; } void addEdge ( int v , int w ) { adj [ v ] . add ( w ) ; } void DFSUtil ( int s , Vector < Boolean > visited ) { Stack < Integer > stack = new Stack < > ( ) ; stack . push ( s ) ; while ( stack . empty ( ) == false ) { s = stack . peek ( ) ; stack . pop ( ) ; if ( visited . get ( s ) == false ) { System . out . print ( s + " ▁ " ) ; visited . set ( s , true ) ; } Iterator < Integer > itr = adj [ s ] . iterator ( ) ; while ( itr . hasNext ( ) ) { int v = itr . next ( ) ; if ( ! visited . get ( v ) ) stack . push ( v ) ; } } } void DFS ( ) { Vector < Boolean > visited = new Vector < Boolean > ( V ) ; for ( int i = 0 ; i < V ; i ++ ) visited . add ( false ) ; for ( int i = 0 ; i < V ; i ++ ) if ( ! visited . get ( i ) ) DFSUtil ( i , visited ) ; } } public static void main ( String [ ] args ) { Graph g = new Graph ( 5 ) ; g . addEdge ( 1 , 0 ) ; g . addEdge ( 2 , 1 ) ; g . addEdge ( 3 , 4 ) ; g . addEdge ( 4 , 0 ) ; System . out . println ( " Following ▁ is ▁ Depth ▁ First ▁ Traversal " ) ; g . DFS ( ) ; } }

class Graph : NEW_LINE INDENT def __init__ ( self , V ) : NEW_LINE INDENT self . V = V NEW_LINE self . adj = [ [ ] for i in range ( V ) ] NEW_LINE DEDENT def addEdge ( self , v , w ) : NEW_LINE INDENT self . adj [ v ] . append ( w ) NEW_LINE DEDENT def DFSUtil ( self , s , visited ) : NEW_LINE INDENT stack = [ ] NEW_LINE stack . append ( s ) NEW_LINE while ( len ( stack ) != 0 ) : NEW_LINE INDENT s = stack . pop ( ) NEW_LINE if ( not visited [ s ] ) : NEW_LINE INDENT print ( s , end = " ▁ " ) NEW_LINE visited [ s ] = True NEW_LINE DEDENT i = 0 NEW_LINE while i < len ( self . adj [ s ] ) : NEW_LINE INDENT if ( not visited [ self . adj [ s ] [ i ] ] ) : NEW_LINE INDENT stack . append ( self . adj [ s ] [ i ] ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT DEDENT def DFS ( self ) : NEW_LINE INDENT visited = [ False ] * self . V NEW_LINE for i in range ( self . V ) : NEW_LINE INDENT if ( not visited [ i ] ) : NEW_LINE INDENT self . DFSUtil ( i , visited ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT g = Graph ( 5 ) NEW_LINE g . addEdge ( 1 , 0 ) NEW_LINE g . addEdge ( 2 , 1 ) NEW_LINE g . addEdge ( 3 , 4 ) NEW_LINE g . addEdge ( 4 , 0 ) NEW_LINE print ( " Following ▁ is ▁ Depth ▁ First ▁ Traversal " ) NEW_LINE g . DFS ( ) NEW_LINE DEDENT


class sol { static class Node { int data ; Node next ; } ; static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . data = key ; temp . next = null ; return temp ; } static Node rearrangeEvenOdd ( Node head ) { if ( head == null ) return null ; Node odd = head ; Node even = head . next ; Node evenFirst = even ; while ( true ) { if ( odd == null || even == null || ( even . next ) == null ) { break ; } odd . next = even . next ; odd = even . next ; if ( odd . next == null ) { even . next = null ; break ; } even . next = odd . next ; even = odd . next ; } Node current = evenFirst ; Node prev = null ; Node front = null ; while ( current != null ) { front = current . next ; current . next = prev ; prev = current ; current = front ; } evenFirst = prev ; odd . next = evenFirst ; return head ; } static void printlist ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ - > ▁ " ) ; node = node . next ; } System . out . println ( " null " ) ; } public static void main ( String args [ ] ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; head . next . next . next . next = newNode ( 5 ) ; head . next . next . next . next . next = newNode ( 6 ) ; head = rearrangeEvenOdd ( head ) ; printlist ( head ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def newNode ( key ) : NEW_LINE INDENT temp = Node ( key ) NEW_LINE temp . data = key NEW_LINE temp . next = None NEW_LINE return temp NEW_LINE DEDENT def rearrangeEvenOdd ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT odd = head NEW_LINE even = head . next NEW_LINE evenFirst = even NEW_LINE while True : NEW_LINE INDENT if ( odd == None or even == None or ( even . next ) == None ) : NEW_LINE INDENT break NEW_LINE DEDENT odd . next = even . next NEW_LINE odd = even . next NEW_LINE if ( odd . next == None ) : NEW_LINE INDENT even . next = None NEW_LINE break NEW_LINE DEDENT even . next = odd . next NEW_LINE even = odd . next NEW_LINE DEDENT current = evenFirst NEW_LINE prev = None NEW_LINE front = None NEW_LINE while ( current != None ) : NEW_LINE INDENT front = current . next NEW_LINE current . next = prev NEW_LINE prev = current NEW_LINE current = front NEW_LINE DEDENT evenFirst = prev NEW_LINE odd . next = evenFirst NEW_LINE return head NEW_LINE DEDENT def printlist ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , end = " - > " ) NEW_LINE node = node . next NEW_LINE DEDENT print ( " NULL " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = newNode ( 1 ) NEW_LINE head . next = newNode ( 2 ) NEW_LINE head . next . next = newNode ( 3 ) NEW_LINE head . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next = newNode ( 5 ) NEW_LINE head . next . next . next . next . next = newNode ( 6 ) NEW_LINE head = rearrangeEvenOdd ( head ) NEW_LINE printlist ( head ) NEW_LINE DEDENT


class GFG { static void moveSpaceInFront ( char str [ ] ) { int i = str . length - 1 ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) str [ i -- ] = str [ j ] ; while ( i >= 0 ) str [ i -- ] = ' ▁ ' ; } public static void main ( String [ ] args ) { char str [ ] = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " . toCharArray ( ) ; moveSpaceInFront ( str ) ; System . out . println ( String . valueOf ( str ) ) ; } }

def moveSpaceInFront ( s ) : NEW_LINE INDENT i = len ( s ) - 1 ; NEW_LINE for j in range ( i , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ j ] != ' ▁ ' ) : NEW_LINE INDENT s = s [ : i ] + s [ j ] + s [ i + 1 : ] NEW_LINE i -= 1 ; NEW_LINE DEDENT DEDENT while ( i >= 0 ) : NEW_LINE INDENT s = s [ : i ] + ' ▁ ' + s [ i + 1 : ] NEW_LINE i -= 1 NEW_LINE DEDENT return s ; NEW_LINE DEDENT s = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " ; NEW_LINE s = moveSpaceInFront ( s ) ; NEW_LINE print ( s ) ; NEW_LINE


class GFG { static int MAX = 1000 ; static int f [ ] = new int [ MAX ] ; static int fib ( int n ) { if ( n == 0 ) { return 0 ; } if ( n == 1 || n == 2 ) { return ( f [ n ] = 1 ) ; } if ( f [ n ] == 1 ) { return f [ n ] ; } int k = ( n % 2 == 1 ) ? ( n + 1 ) / 2 : n / 2 ; f [ n ] = ( n % 2 == 1 ) ? ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) : ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } static int calculateEvenSum ( int n ) { return ( fib ( 2 * n + 1 ) - 1 ) ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + calculateEvenSum ( n ) ) ; } }

MAX = 1000 ; NEW_LINE f = [ 0 ] * MAX ; NEW_LINE def fib ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n == 1 or n == 2 ) : NEW_LINE INDENT f [ n ] = 1 ; NEW_LINE return f [ n ] ; NEW_LINE DEDENT if ( f [ n ] ) : NEW_LINE INDENT return f [ n ] ; NEW_LINE DEDENT k = ( n + 1 ) // 2 if ( n % 2 == 1 ) else n // 2 ; NEW_LINE f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) )   \ NEW_LINE if ( n % 2 == 1 ) else ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; NEW_LINE return f [ n ] ; NEW_LINE DEDENT def calculateEvenSum ( n ) : NEW_LINE INDENT return ( fib ( 2 * n + 1 ) - 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 ; NEW_LINE print ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto " , n , " terms : " , calculateEvenSum ( n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = node . right = null ; return ( node ) ; } static boolean CheckChildrenSum ( Node root ) { Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left != null && temp . right != null ) { if ( temp . data != temp . left . data + temp . right . data ) return false ; q . add ( temp . left ) ; q . add ( temp . right ) ; } else if ( temp . left == null && temp . right != null ) { if ( temp . data != temp . right . data ) return false ; q . add ( temp . right ) ; } else if ( temp . right == null && temp . left != null ) { if ( temp . data != temp . left . data ) return false ; q . add ( temp . left ) ; } } return true ; } public static void main ( String args [ ] ) { Node root = newNode ( 10 ) ; root . left = newNode ( 8 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 2 ) ; if ( CheckChildrenSum ( root ) ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def CheckChildrenSum ( root ) : NEW_LINE INDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while len ( q ) != 0 : NEW_LINE INDENT temp = q . pop ( ) NEW_LINE if temp . left and temp . right : NEW_LINE INDENT if ( temp . data != temp . left . data + temp . right . data ) : NEW_LINE INDENT return False NEW_LINE DEDENT q . append ( temp . left ) NEW_LINE q . append ( temp . right ) NEW_LINE DEDENT elif not temp . left and temp . right : NEW_LINE INDENT if temp . data != temp . right . data : NEW_LINE INDENT return False NEW_LINE DEDENT q . append ( temp . right ) NEW_LINE DEDENT elif not temp . right and temp . left : NEW_LINE INDENT if temp . data != temp . left . data : NEW_LINE INDENT return False NEW_LINE DEDENT q . append ( temp . left ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 10 ) NEW_LINE root . left = Node ( 8 ) NEW_LINE root . right = Node ( 2 ) NEW_LINE root . left . left = Node ( 3 ) NEW_LINE root . left . right = Node ( 5 ) NEW_LINE root . right . right = Node ( 2 ) NEW_LINE if CheckChildrenSum ( root ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static boolean sameJavaharAdj ( char str [ ] ) { int n = str . length , i ; TreeSet < Character > st = new TreeSet < > ( ) ; st . add ( str [ 0 ] ) ; for ( i = 1 ; i < n ; i ++ ) { if ( str [ i ] == str [ i - 1 ] ) { continue ; } if ( st . contains ( str [ i ] ) & ( str [ i ] != st . last ( ) ) ) { return false ; } st . add ( str [ i ] ) ; } return true ; } static int minSwaps ( char str [ ] , int l , int r , int cnt , int minm ) { if ( l == r ) { if ( sameJavaharAdj ( str ) ) { return cnt ; } else { return Integer . MAX_VALUE ; } } for ( int i = l + 1 ; i <= r ; i ++ ) { swap ( str , i , l ) ; cnt ++ ; int x = minSwaps ( str , l + 1 , r , cnt , minm ) ; swap ( str , i , l ) ; cnt -- ; int y = minSwaps ( str , l + 1 , r , cnt , minm ) ; minm = Math . min ( minm , Math . min ( x , y ) ) ; } return minm ; } static void swap ( char [ ] arr , int i , int j ) { char temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } public static void main ( String [ ] args ) { String str = " abbaacb " ; int n = str . length ( ) , cnt = 0 , minm = Integer . MAX_VALUE ; System . out . print ( minSwaps ( str . toCharArray ( ) , 0 , n - 1 , cnt , minm ) ) ; ; } }

from sys import maxsize NEW_LINE def sameCharAdj ( string ) : NEW_LINE INDENT n = len ( string ) NEW_LINE st = set ( ) NEW_LINE st . add ( string [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if string [ i ] == string [ i - 1 ] : NEW_LINE INDENT continue NEW_LINE DEDENT if string [ i ] in st : NEW_LINE INDENT return False NEW_LINE DEDENT st . add ( string [ i ] ) NEW_LINE DEDENT return True NEW_LINE DEDENT def minSwaps ( string , l , r , cnt , minm ) : NEW_LINE INDENT if l == r : NEW_LINE INDENT if sameCharAdj ( string ) : NEW_LINE INDENT return cnt NEW_LINE DEDENT else : NEW_LINE INDENT return maxsize NEW_LINE DEDENT DEDENT for i in range ( l + 1 , r + 1 , 1 ) : NEW_LINE INDENT string [ i ] , string [ l ] = string [ l ] , string [ i ] NEW_LINE cnt += 1 NEW_LINE x = minSwaps ( string , l + 1 , r , cnt , minm ) NEW_LINE string [ i ] , string [ l ] = string [ l ] , string [ i ] NEW_LINE cnt -= 1 NEW_LINE y = minSwaps ( string , l + 1 , r , cnt , minm ) NEW_LINE minm = min ( minm , min ( x , y ) ) NEW_LINE DEDENT return minm NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abbaacb " NEW_LINE string = list ( string ) NEW_LINE n = len ( string ) NEW_LINE cnt = 0 NEW_LINE minm = maxsize NEW_LINE print ( minSwaps ( string , 0 , n - 1 , cnt , minm ) ) NEW_LINE DEDENT


import java . util . HashMap ; class GFG { public static int maxLengthSub ( int [ ] arr ) { int max_val = 0 ; int start = 0 ; HashMap < Integer , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { int temp = 0 ; if ( map . containsKey ( arr [ i ] - 1 ) ) { temp = map . get ( arr [ i ] - 1 ) ; } if ( map . containsKey ( arr [ i ] ) ) { temp = Math . max ( temp , map . get ( arr [ i ] ) ) ; } if ( map . containsKey ( arr [ i ] + 1 ) ) { temp = Math . max ( temp , map . get ( arr [ i ] + 1 ) ) ; } temp ++ ; if ( temp > max_val ) { max_val = temp ; } map . put ( arr [ i ] , temp ) ; } return max_val ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 } ; System . out . println ( maxLengthSub ( arr ) ) ; } }

from collections import defaultdict NEW_LINE def maxLenSub ( arr , n ) : NEW_LINE INDENT um = defaultdict ( lambda : 0 ) NEW_LINE maxLen = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT length = 0 NEW_LINE if ( arr [ i ] - 1 ) in um and length < um [ arr [ i ] - 1 ] : NEW_LINE INDENT length = um [ arr [ i ] - 1 ] NEW_LINE DEDENT if arr [ i ] in um and length < um [ arr [ i ] ] : NEW_LINE INDENT length = um [ arr [ i ] ] NEW_LINE DEDENT if ( arr [ i ] + 1 ) in um and length < um [ arr [ i ] + 1 ] : NEW_LINE INDENT length = um [ arr [ i ] + 1 ] NEW_LINE DEDENT um [ arr [ i ] ] = length + 1 NEW_LINE if maxLen < um [ arr [ i ] ] : NEW_LINE INDENT maxLen = um [ arr [ i ] ] NEW_LINE DEDENT DEDENT return maxLen NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 5 , 6 , 3 , 7 , 6 , 5 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ length ▁ subsequence ▁ = " , maxLenSub ( arr , n ) ) NEW_LINE DEDENT


import java . util . Arrays ; public class GFG { static void shuffleArray ( int a [ ] , int n ) { for ( int i = 0 , q = 1 , k = n ; i < n ; i ++ , k ++ , q ++ ) for ( int j = k ; j > i + q ; j -- ) { int temp = a [ j - 1 ] ; a [ j - 1 ] = a [ j ] ; a [ j ] = temp ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 } ; shuffleArray ( a , a . length / 2 ) ; System . out . println ( Arrays . toString ( a ) ) ; } }

def shuffleArray ( a , n ) : NEW_LINE INDENT i , q , k = 0 , 1 , n NEW_LINE while ( i < n ) : NEW_LINE INDENT j = k NEW_LINE while ( j > i + q ) : NEW_LINE INDENT a [ j - 1 ] , a [ j ] = a [ j ] , a [ j - 1 ] NEW_LINE j -= 1 NEW_LINE DEDENT i += 1 NEW_LINE k += 1 NEW_LINE q += 1 NEW_LINE DEDENT DEDENT a = [ 1 , 3 , 5 , 7 , 2 , 4 , 6 , 8 ] NEW_LINE n = len ( a ) NEW_LINE shuffleArray ( a , int ( n / 2 ) ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printSubArray ( int arr [ ] , int start , int end ) { System . out . print ( " [ ▁ " ) ; for ( int i = start ; i <= end ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . print ( " ] ▁ " ) ; } static boolean divideArray ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int sum_so_far = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( 2 * sum_so_far + arr [ i ] == sum ) { System . out . print ( " The ▁ array ▁ can ▁ be ▁ divided ▁ into ▁ " + " two ▁ subarrays ▁ with ▁ equal ▁ sum \n The " + " ▁ two ▁ subarrays ▁ are ▁ - ▁ " ) ; printSubArray ( arr , 0 , i - 1 ) ; printSubArray ( arr , i + 1 , n - 1 ) ; return true ; } sum_so_far += arr [ i ] ; } System . out . println ( " The ▁ array ▁ cannot ▁ be ▁ divided ▁ into ▁ two ▁ " + " subarrays ▁ with ▁ equal ▁ sum " ) ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 2 , 3 , 2 , 1 } ; int n = arr . length ; divideArray ( arr , n ) ; } }

def printSubArray ( arr , start , end ) : NEW_LINE INDENT print ( " [ ▁ " , end = " " ) NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " ] " , end = " " ) NEW_LINE DEDENT def divideArray ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT sum_so_far = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if 2 * sum_so_far + arr [ i ] == sum : NEW_LINE INDENT print ( " The ▁ array ▁ can ▁ be ▁ divided ▁ into " , " two ▁ subarrays ▁ with ▁ equal ▁ sum " ) NEW_LINE print ( " two ▁ subarrays ▁ are ▁ - " , end = " " ) NEW_LINE printSubArray ( arr , 0 , i - 1 ) NEW_LINE printSubArray ( arr , i + 1 , n - 1 ) NEW_LINE return True NEW_LINE DEDENT sum_so_far += arr [ i ] NEW_LINE DEDENT print ( " The ▁ array ▁ cannot ▁ be ▁ divided ▁ into " " two ▁ subarrays ▁ with ▁ equal ▁ sum " , end = " " ) NEW_LINE return False NEW_LINE DEDENT arr = [ 6 , 2 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE divideArray ( arr , n ) NEW_LINE


class GFG { static int ways ( String s , int n ) { int dp [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) dp [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { String temp = " " ; for ( int j = i ; j < n ; j ++ ) { temp += s . charAt ( j ) ; String test = " " ; for ( int k = i - 1 ; k >= 0 ; k -- ) { test += s . charAt ( k ) ; if ( test . compareTo ( temp ) < 0 ) { dp [ i ] [ j ] += dp [ k ] [ i - 1 ] ; } } } } int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans += dp [ i ] [ n - 1 ] ; } return ans ; } public static void main ( String [ ] args ) { String s = " aabc " ; int n = s . length ( ) ; System . out . println ( ways ( s , n ) ) ; } }

def ways ( s , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT temp = " " NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT temp += s [ j ] NEW_LINE test = " " NEW_LINE for k in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT test += s [ k ] NEW_LINE if ( test < temp ) : NEW_LINE INDENT dp [ i ] [ j ] += dp [ k ] [ i - 1 ] NEW_LINE DEDENT DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += dp [ i ] [ n - 1 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT s = " aabc " NEW_LINE n = len ( s ) NEW_LINE print ( ways ( s , n ) ) NEW_LINE


import java . util . * ; class GFG { static void minOperation ( String S , int N , int K ) { if ( N % K != 0 ) { System . out . println ( " Not ▁ Possible " ) ; } else { int [ ] count = new int [ 26 ] ; for ( int i = 0 ; i < N ; i ++ ) { count [ ( S . charAt ( i ) - 97 ) ] ++ ; } int E = N / K ; Vector < Integer > greaterE = new Vector < > ( ) ; Vector < Integer > lessE = new Vector < > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( count [ i ] < E ) lessE . add ( E - count [ i ] ) ; else greaterE . add ( count [ i ] - E ) ; } Collections . sort ( greaterE ) ; Collections . sort ( lessE ) ; int mi = Integer . MAX_VALUE ; for ( int i = 0 ; i <= K ; i ++ ) { int set1 = i ; int set2 = K - i ; if ( greaterE . size ( ) >= set1 && lessE . size ( ) >= set2 ) { int step1 = 0 ; int step2 = 0 ; for ( int j = 0 ; j < set1 ; j ++ ) step1 += greaterE . get ( j ) ; for ( int j = 0 ; j < set2 ; j ++ ) step2 += lessE . get ( j ) ; mi = Math . min ( mi , Math . max ( step1 , step2 ) ) ; } } System . out . println ( mi ) ; } } public static void main ( String [ ] args ) { String S = " accb " ; int N = S . length ( ) ; int K = 2 ; minOperation ( S , N , K ) ; } }

def minOperation ( S , N , K ) : NEW_LINE INDENT if N % K : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT count = [ 0 ] * 26 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT count [ ord ( S [ i ] ) - 97 ] += 1 NEW_LINE DEDENT E = N // K NEW_LINE greaterE = [ ] NEW_LINE lessE = [ ] NEW_LINE for i in range ( 0 , 26 ) : NEW_LINE INDENT if count [ i ] < E : NEW_LINE INDENT lessE . append ( E - count [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT greaterE . append ( count [ i ] - E ) NEW_LINE DEDENT DEDENT greaterE . sort ( ) NEW_LINE lessE . sort ( ) NEW_LINE mi = float ( ' inf ' ) NEW_LINE for i in range ( 0 , K + 1 ) : NEW_LINE INDENT set1 , set2 = i , K - i NEW_LINE if ( len ( greaterE ) >= set1 and len ( lessE ) >= set2 ) : NEW_LINE INDENT step1 , step2 = 0 , 0 NEW_LINE for j in range ( 0 , set1 ) : NEW_LINE INDENT step1 += greaterE [ j ] NEW_LINE DEDENT for j in range ( 0 , set2 ) : NEW_LINE INDENT step2 += lessE [ j ] NEW_LINE DEDENT mi = min ( mi , max ( step1 , step2 ) ) NEW_LINE DEDENT DEDENT print ( mi ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " accb " NEW_LINE N = len ( S ) NEW_LINE K = 2 NEW_LINE minOperation ( S , N , K ) NEW_LINE DEDENT


import java . util . Stack ; class GFG { static int countNumber ( int n ) { int result = 0 ; for ( int i = 1 ; i <= 9 ; i ++ ) { Stack < Integer > s = new Stack < > ( ) ; if ( i <= n ) { s . push ( i ) ; result ++ ; } while ( ! s . empty ( ) ) { int tp = s . peek ( ) ; s . pop ( ) ; for ( int j = tp % 10 ; j <= 9 ; j ++ ) { int x = tp * 10 + j ; if ( x <= n ) { s . push ( x ) ; result ++ ; } } } } return result ; } public static void main ( String [ ] args ) { int n = 15 ; System . out . println ( countNumber ( n ) ) ; } }

def countNumber ( n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT s = [ ] NEW_LINE if ( i <= n ) : NEW_LINE INDENT s . append ( i ) NEW_LINE result += 1 NEW_LINE DEDENT while len ( s ) != 0 : NEW_LINE INDENT tp = s [ - 1 ] NEW_LINE s . pop ( ) NEW_LINE for j in range ( tp % 10 , 10 ) : NEW_LINE INDENT x = tp * 10 + j NEW_LINE if ( x <= n ) : NEW_LINE INDENT s . append ( x ) NEW_LINE result += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 NEW_LINE print ( countNumber ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int countWays ( int n , int arr [ ] , int k ) { if ( k <= 0 || k >= n ) return 0 ; Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( arr [ i ] ) ; if ( s . size ( ) <= k ) return 0 ; return s . size ( ) - k ; } public static void main ( String args [ ] ) { int arr [ ] = { 100 , 200 , 400 , 50 } ; int k = 3 ; int n = arr . length ; System . out . println ( countWays ( n , arr , k ) ) ; } }

def countWays ( n , arr , k ) : NEW_LINE INDENT if ( k <= 0 or k >= n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT s = set ( ) NEW_LINE for element in arr : NEW_LINE INDENT s . add ( element ) NEW_LINE DEDENT if ( len ( s ) <= k ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT return len ( s ) - k ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 100 , 200 , 400 , 50 ] NEW_LINE k = 3 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countWays ( n , arr , k ) ) NEW_LINE DEDENT


class Main { static int binarySearch ( int arr [ ] , int low , int high , int key ) { if ( high < low ) return - 1 ; int mid = ( low + high ) / 2 ; if ( key == arr [ mid ] ) return mid ; if ( key > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , key ) ; return binarySearch ( arr , low , ( mid - 1 ) , key ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 7 , 8 , 9 , 10 } ; int n , key ; n = arr . length ; key = 10 ; System . out . println ( " Index : ▁ " + binarySearch ( arr , 0 , n , key ) ) ; } }

def binarySearch ( arr , low , high , key ) : NEW_LINE INDENT if ( high < low ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mid = ( low + high ) / 2 NEW_LINE if ( key == arr [ int ( mid ) ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( key > arr [ int ( mid ) ] ) : NEW_LINE INDENT return binarySearch ( arr , ( mid + 1 ) , high , key ) NEW_LINE DEDENT return ( binarySearch ( arr , low , ( mid - 1 ) , key ) ) NEW_LINE DEDENT arr = [ 5 , 6 , 7 , 8 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE key = 10 NEW_LINE print ( " Index : " , int ( binarySearch ( arr , 0 , n , key ) ) ) NEW_LINE


import java . util . Arrays ; class GFG { public static int partition ( int [ ] arr , int low , int high ) { int pivot = arr [ high ] , pivotloc = low ; for ( int i = low ; i <= high ; i ++ ) { if ( arr [ i ] < pivot ) { int temp = arr [ i ] ; arr [ i ] = arr [ pivotloc ] ; arr [ pivotloc ] = temp ; pivotloc ++ ; } } int temp = arr [ high ] ; arr [ high ] = arr [ pivotloc ] ; arr [ pivotloc ] = temp ; return pivotloc ; } public static int kthSmallest ( int [ ] arr , int low , int high , int k ) { int partition = partition ( arr , low , high ) ; if ( partition == k ) return arr [ partition ] ; else if ( partition < k ) return kthSmallest ( arr , partition + 1 , high , k ) ; else return kthSmallest ( arr , low , partition - 1 , k ) ; } public static void main ( String [ ] args ) { int [ ] array = new int [ ] { 10 , 4 , 5 , 8 , 6 , 11 , 26 } ; int [ ] arraycopy = new int [ ] { 10 , 4 , 5 , 8 , 6 , 11 , 26 } ; int kPosition = 3 ; int length = array . length ; if ( kPosition > length ) { System . out . println ( " Index ▁ out ▁ of ▁ bound " ) ; } else { System . out . println ( " K - th ▁ smallest ▁ element ▁ in ▁ array ▁ : ▁ " + kthSmallest ( arraycopy , 0 , length - 1 , kPosition - 1 ) ) ; } } }

def partition ( arr , l , r ) : NEW_LINE INDENT x = arr [ r ] NEW_LINE i = l NEW_LINE for j in range ( l , r ) : NEW_LINE INDENT if arr [ j ] <= x : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT DEDENT arr [ i ] , arr [ r ] = arr [ r ] , arr [ i ] NEW_LINE return i NEW_LINE DEDENT def kthSmallest ( arr , l , r , k ) : NEW_LINE INDENT if ( k > 0 and k <= r - l + 1 ) : NEW_LINE INDENT index = partition ( arr , l , r ) NEW_LINE if ( index - l == k - 1 ) : NEW_LINE INDENT return arr [ index ] NEW_LINE DEDENT if ( index - l > k - 1 ) : NEW_LINE INDENT return kthSmallest ( arr , l , index - 1 , k ) NEW_LINE DEDENT return kthSmallest ( arr , index + 1 , r , k - index + l - 1 ) NEW_LINE DEDENT return INT_MAX NEW_LINE DEDENT arr = [ 10 , 4 , 5 , 8 , 6 , 11 , 26 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( " K - th ▁ smallest ▁ element ▁ is ▁ " , end = " " ) NEW_LINE print ( kthSmallest ( arr , 0 , n - 1 , k ) ) NEW_LINE


class Main { static int getMissingNo ( int a [ ] , int n ) { int x1 = a [ 0 ] ; int x2 = 1 ; for ( int i = 1 ; i < n ; i ++ ) x1 = x1 ^ a [ i ] ; for ( int i = 2 ; i <= n + 1 ; i ++ ) x2 = x2 ^ i ; return ( x1 ^ x2 ) ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 4 , 5 , 6 } ; int miss = getMissingNo ( a , 5 ) ; System . out . println ( miss ) ; } }

def getMissingNo ( a , n ) : NEW_LINE INDENT x1 = a [ 0 ] NEW_LINE x2 = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT x1 = x1 ^ a [ i ] NEW_LINE DEDENT for i in range ( 2 , n + 2 ) : NEW_LINE INDENT x2 = x2 ^ i NEW_LINE DEDENT return x1 ^ x2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 2 , 4 , 5 , 6 ] NEW_LINE n = len ( a ) NEW_LINE miss = getMissingNo ( a , n ) NEW_LINE print ( miss ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int mod = 1000000000 ; static int ways ( int i , int arr [ ] , int n ) { if ( i == n - 1 ) return 1 ; int sum = 0 ; for ( int j = 1 ; j + i < n && j <= arr [ i ] ; j ++ ) { sum += ( ways ( i + j , arr , n ) ) % mod ; sum %= mod ; } return sum % mod ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 1 , 4 , 3 } ; int n = arr . length ; System . out . println ( ways ( 0 , arr , n ) ) ; } }

mod = 1e9 + 7 ; NEW_LINE def ways ( i , arr , n ) : NEW_LINE INDENT if ( i == n - 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT sum = 0 ; NEW_LINE for j in range ( 1 , arr [ i ] + 1 ) : NEW_LINE INDENT if ( i + j < n ) : NEW_LINE INDENT sum += ( ways ( i + j , arr , n ) ) % mod ; NEW_LINE sum %= mod ; NEW_LINE DEDENT DEDENT return int ( sum % mod ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 3 , 1 , 4 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( ways ( 0 , arr , n ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int Centered_Trigunal_num ( int n ) { return ( 3 * n * n + 3 * n + 2 ) / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( Centered_Trigunal_num ( n ) ) ; n = 12 ; System . out . println ( Centered_Trigunal_num ( n ) ) ; } }

def Centered_Trigunal_num ( n ) : NEW_LINE INDENT return ( 3 * n * n + 3 * n + 2 ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( Centered_Trigunal_num ( n ) ) NEW_LINE n = 12 NEW_LINE print ( Centered_Trigunal_num ( n ) ) NEW_LINE DEDENT


class Main { public static int nth_term ( int n ) { int a = 1 , b = 1 , c = 1 ; for ( int i = 0 ; i < n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } public static void main ( String [ ] args ) { int n = 10 ; int c = nth_term ( n ) ; System . out . println ( c ) ; } }

def nth_term ( n ) : NEW_LINE INDENT a = 1 NEW_LINE b = 1 NEW_LINE c = 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE DEDENT return c NEW_LINE DEDENT n = 10 NEW_LINE c = nth_term ( n ) NEW_LINE print ( c ) NEW_LINE


class GFG { static void findFactors ( int a , int b ) { int c ; c = a + b - 1 ; System . out . print ( c ) ; } public static void main ( String [ ] args ) { int a , b ; a = 13 ; b = 36 ; findFactors ( a , b ) ; } }

def findFactors ( a , b ) : NEW_LINE INDENT c = a + b - 1 NEW_LINE print ( c ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 13 NEW_LINE b = 36 NEW_LINE findFactors ( a , b ) NEW_LINE DEDENT


class GFG { static boolean isVowel ( char c ) { return ( c == ' a ' || c == ' e ' || c == ' i ' || c == ' o ' || c == ' u ' ) ; } static String encryptString ( char [ ] s , int n , int k ) { int [ ] cv = new int [ n ] ; int [ ] cc = new int [ n ] ; if ( isVowel ( s [ 0 ] ) ) cv [ 0 ] = 1 ; else cc [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { cv [ i ] = cv [ i - 1 ] + ( isVowel ( s [ i ] ) == true ? 1 : 0 ) ; cc [ i ] = cc [ i - 1 ] + ( isVowel ( s [ i ] ) == true ? 0 : 1 ) ; } String ans = " " ; int prod = 0 ; prod = cc [ k - 1 ] * cv [ k - 1 ] ; ans += String . valueOf ( prod ) ; for ( int i = k ; i < s . length ; i ++ ) { prod = ( cc [ i ] - cc [ i - k ] ) * ( cv [ i ] - cv [ i - k ] ) ; ans += String . valueOf ( prod ) ; } return ans ; } public static void main ( String [ ] args ) { String s = " hello " ; int n = s . length ( ) ; int k = 2 ; System . out . print ( encryptString ( s . toCharArray ( ) , n , k ) + " \n " ) ; } }

def isVowel ( c ) : NEW_LINE INDENT return ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) NEW_LINE DEDENT def encryptString ( s , n , k ) : NEW_LINE INDENT cv = [ 0 for i in range ( n ) ] NEW_LINE cc = [ 0 for i in range ( n ) ] NEW_LINE if ( isVowel ( s [ 0 ] ) ) : NEW_LINE INDENT cv [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT cc [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT cv [ i ] = cv [ i - 1 ] + isVowel ( s [ i ] ) NEW_LINE cc [ i ] = cc [ i - 1 ] + ( isVowel ( s [ i ] ) == False ) NEW_LINE DEDENT ans = " " NEW_LINE prod = 0 NEW_LINE prod = cc [ k - 1 ] * cv [ k - 1 ] NEW_LINE ans += str ( prod ) NEW_LINE for i in range ( k , len ( s ) ) : NEW_LINE INDENT prod = ( ( cc [ i ] - cc [ i - k ] ) * ( cv [ i ] - cv [ i - k ] ) ) NEW_LINE ans += str ( prod ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " hello " NEW_LINE n = len ( s ) NEW_LINE k = 2 NEW_LINE print ( encryptString ( s , n , k ) ) NEW_LINE DEDENT


import java . util . * ; class solution { static void CountPair ( int L , int R ) { int x = ( R - L + 1 ) ; System . out . println ( x / 2 + " \n " ) ; } public static void main ( String args [ ] ) { int L , R ; L = 1 ; R = 8 ; CountPair ( L , R ) ; } }

def CountPair ( L , R ) : NEW_LINE INDENT x = ( R - L + 1 ) NEW_LINE print ( x // 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L , R = 1 , 8 NEW_LINE CountPair ( L , R ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean isPossibleToZero ( int a [ ] , int n ) { int num = 0 ; for ( int i = 0 ; i < n ; i ++ ) num = num * 10 + a [ i ] ; return ( num % 11 == 0 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 0 } ; int n = arr . length ; if ( isPossibleToZero ( arr , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def isPossibleToZero ( a , n ) : NEW_LINE INDENT num = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT num = num * 10 + a [ i ] ; NEW_LINE DEDENT return ( num % 11 == 0 ) ; NEW_LINE DEDENT arr = [ 0 , 1 , 1 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE if ( isPossibleToZero ( arr , n ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT


class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int maxProfit ( int n , int a , int b , int x , int y ) { int res = x * ( n / a ) ; res += y * ( n / b ) ; res -= Math . min ( x , y ) * ( n / ( ( a * b ) / __gcd ( a , b ) ) ) ; return res ; } public static void main ( String [ ] args ) { int n = 6 , a = 6 , b = 2 , x = 8 , y = 2 ; System . out . println ( maxProfit ( n , a , b , x , y ) ) ; } }

from math import gcd NEW_LINE def maxProfit ( n , a , b , x , y ) : NEW_LINE INDENT res = x * ( n // a ) ; NEW_LINE res += y * ( n // b ) ; NEW_LINE res -= min ( x , y ) * ( n // ( ( a * b ) // gcd ( a , b ) ) ) ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 ; a = 6 ; b = 2 ; x = 8 ; y = 2 ; NEW_LINE print ( maxProfit ( n , a , b , x , y ) ) ; NEW_LINE DEDENT


class GFG { static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } public static void main ( String [ ] args ) { double x = 2500 ; if ( isPerfectSquare ( x ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

import math NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = math . sqrt ( x ) NEW_LINE return ( ( sr - math . floor ( sr ) ) == 0 ) NEW_LINE DEDENT x = 2500 NEW_LINE if ( isPerfectSquare ( x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static boolean isSubsetSum ( int set [ ] , int n , int sum ) { if ( sum == 0 ) return true ; if ( n == 0 && sum != 0 ) return false ; if ( set [ n - 1 ] > sum ) return isSubsetSum ( set , n - 1 , sum ) ; return isSubsetSum ( set , n - 1 , sum ) || isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) ; } public static void main ( String args [ ] ) { int set [ ] = { 3 , 34 , 4 , 12 , 5 , 2 } ; int sum = 9 ; int n = set . length ; if ( isSubsetSum ( set , n , sum ) == true ) System . out . println ( " Found ▁ a ▁ subset " + " ▁ with ▁ given ▁ sum " ) ; else System . out . println ( " No ▁ subset ▁ with " + " ▁ given ▁ sum " ) ; } }

def isSubsetSum ( set , n , sum ) : NEW_LINE INDENT if ( sum == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n == 0 and sum != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( set [ n - 1 ] > sum ) : NEW_LINE INDENT return isSubsetSum ( set , n - 1 , sum ) ; NEW_LINE DEDENT return isSubsetSum ( set , n - 1 , sum ) or isSubsetSum ( set , n - 1 , sum - set [ n - 1 ] ) NEW_LINE DEDENT set = [ 3 , 34 , 4 , 12 , 5 , 2 ] NEW_LINE sum = 9 NEW_LINE n = len ( set ) NEW_LINE if ( isSubsetSum ( set , n , sum ) == True ) : NEW_LINE INDENT print ( " Found ▁ a ▁ subset ▁ with ▁ given ▁ sum " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ subset ▁ with ▁ given ▁ sum " ) NEW_LINE DEDENT


class Main { public static void main ( String [ ] args ) { int N = 43 ; int ans = findRemainder ( N ) ; System . out . println ( ans ) ; } public static int findRemainder ( int n ) { int x = n & 3 ; return x ; } }

def findRemainder ( n ) : NEW_LINE INDENT x = n & 3 NEW_LINE return x NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 43 NEW_LINE ans = findRemainder ( N ) NEW_LINE print ( ans ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class GFG { public static void main ( String args [ ] ) { float x1 = 2 , x2 = 1 , x3 = 3 ; float y1 = 2 , y2 = 1 , y3 = 1 ; float a = 2 , b = 1 , c = 1 ; float x = ( a * x1 + b * x2 + c * x3 ) / ( a + b + c ) ; float y = ( a * y1 + b * y2 + c * y3 ) / ( a + b + c ) ; System . out . println ( " Incenter = ▁ " + " ( " + x + " , ▁ " + y + " ) " ) ; } }


class GFG { static int properDivisorSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { if ( i / j == j ) sum += j ; else sum += j + i / j ; } } sum = sum - i ; } return sum ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( properDivisorSum ( n ) ) ; n = 5 ; System . out . println ( properDivisorSum ( n ) ) ; } }

def properDivisorSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if j * j > i : NEW_LINE INDENT break NEW_LINE DEDENT if ( i % j == 0 ) : NEW_LINE INDENT if ( i // j == j ) : NEW_LINE INDENT sum += j NEW_LINE DEDENT else : NEW_LINE INDENT sum += j + i // j NEW_LINE DEDENT DEDENT DEDENT sum = sum - i NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE n = 5 NEW_LINE print ( properDivisorSum ( n ) ) NEW_LINE DEDENT


class GFG { static int countFactors ( int n , int p ) { int pwr = 0 ; while ( n > 0 && n % p == 0 ) { n /= p ; pwr ++ ; } return pwr ; } static int getCount ( int l , int r , int p ) { int cnt = 0 ; for ( int i = l ; i <= r ; i ++ ) { cnt += countFactors ( i , p ) ; } return cnt ; } public static void main ( String [ ] args ) { int l = 2 , r = 8 , p = 2 ; System . out . println ( getCount ( l , r , p ) ) ; } }

def countFactors ( n , p ) : NEW_LINE INDENT pwr = 0 ; NEW_LINE while ( n > 0 and n % p == 0 ) : NEW_LINE INDENT n //= p ; NEW_LINE pwr += 1 ; NEW_LINE DEDENT return pwr ; NEW_LINE DEDENT def getCount ( l , r , p ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT cnt += countFactors ( i , p ) ; NEW_LINE DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 2 ; r = 8 ; p = 2 ; NEW_LINE print ( getCount ( l , r , p ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int countPairs ( int a [ ] , int b [ ] , int n , int m ) { int cnt = 0 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { int sum = a [ i ] + b [ j ] ; if ( s . contains ( sum ) == false ) { cnt ++ ; s . add ( sum ) ; } } } return cnt ; } static public void main ( String args [ ] ) { int a [ ] = { 12 , 2 , 7 } ; int n = a . length ; int b [ ] = { 4 , 3 , 8 } ; int m = b . length ; System . out . println ( countPairs ( a , b , n , m ) ) ; } }

def countPairs ( a , b , n , m ) : NEW_LINE INDENT cnt = 0 NEW_LINE s = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT sum = a [ i ] + b [ j ] NEW_LINE if ( sum not in s . keys ( ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE s [ sum ] = 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT a = [ 12 , 2 , 7 ] NEW_LINE n = len ( a ) NEW_LINE b = [ 4 , 3 , 8 ] NEW_LINE m = len ( b ) NEW_LINE print ( countPairs ( a , b , n , m ) ) NEW_LINE


import java . io . * ; class GFG { static void printDuplicates ( int arr [ ] , int n ) { int i ; int fl = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ arr [ i ] % n ] >= n ) { if ( arr [ arr [ i ] % n ] < 2 * n ) { System . out . print ( arr [ i ] % n + " ▁ " ) ; fl = 1 ; } } arr [ arr [ i ] % n ] += n ; } if ( ! ( fl > 0 ) ) System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 3 , 1 , 3 , 6 , 6 } ; int arr_size = arr . length ; printDuplicates ( arr , arr_size ) ; } }

def printDuplicates ( arr , n ) : NEW_LINE INDENT fl = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ arr [ i ] % n ] >= n ) : NEW_LINE INDENT if ( arr [ arr [ i ] % n ] < 2 * n ) : NEW_LINE INDENT print ( arr [ i ] % n , end = " ▁ " ) NEW_LINE fl = 1 ; NEW_LINE DEDENT DEDENT arr [ arr [ i ] % n ] += n ; NEW_LINE DEDENT if ( fl == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT arr = [ 1 , 6 , 3 , 1 , 3 , 6 , 6 ] ; NEW_LINE arr_size = len ( arr ) ; NEW_LINE printDuplicates ( arr , arr_size ) ; NEW_LINE


import java . util . * ; class GFG { static int givesum ( int A [ ] , int n ) { int max = Arrays . stream ( A ) . max ( ) . getAsInt ( ) ; int maxBit = ( int ) Math . ceil ( Math . log ( max ) + 1 ) ; int totalSubarrays = n * ( n + 1 ) / 2 ; int s = 0 ; for ( int i = 0 ; i < maxBit ; i ++ ) { int c1 = 0 ; Vector < Integer > vec = new Vector < > ( ) ; int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int a = A [ j ] >> i ; if ( ! ( a % 2 == 1 ) ) { vec . add ( j ) ; } } int cntSubarrNotSet = 0 ; int cnt = 1 ; for ( int j = 1 ; j < vec . size ( ) ; j ++ ) { if ( vec . get ( j ) - vec . get ( j - 1 ) == 1 ) { cnt ++ ; } else { cntSubarrNotSet += cnt * ( cnt + 1 ) / 2 ; cnt = 1 ; } } cntSubarrNotSet += cnt * ( cnt + 1 ) / 2 ; if ( vec . size ( ) == 0 ) cntSubarrNotSet = 0 ; int cntSubarrIthSet = totalSubarrays - cntSubarrNotSet ; s += cntSubarrIthSet * Math . pow ( 2 , i ) ; } return s ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = A . length ; System . out . println ( givesum ( A , n ) ) ; } }

from math import log2 NEW_LINE def givesum ( A , n ) : NEW_LINE INDENT max_element = max ( A ) NEW_LINE maxBit = int ( log2 ( max_element ) ) + 1 NEW_LINE totalSubarrays = n * ( n + 1 ) // 2 NEW_LINE s = 0 NEW_LINE for i in range ( maxBit ) : NEW_LINE INDENT c1 = 0 NEW_LINE vec = [ ] NEW_LINE sum = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT a = A [ j ] >> i NEW_LINE if ( not ( a & 1 ) ) : NEW_LINE INDENT vec . append ( j ) NEW_LINE DEDENT DEDENT cntSubarrNotSet = 0 NEW_LINE cnt = 1 NEW_LINE for j in range ( 1 , len ( vec ) ) : NEW_LINE INDENT if ( vec [ j ] - vec [ j - 1 ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cntSubarrNotSet += cnt * ( cnt + 1 ) // 2 NEW_LINE cnt = 1 NEW_LINE DEDENT DEDENT cntSubarrNotSet += cnt * ( cnt + 1 ) // 2 NEW_LINE if len ( vec ) == 0 : NEW_LINE INDENT cntSubarrNotSet = 0 NEW_LINE DEDENT cntSubarrIthSet = totalSubarrays - cntSubarrNotSet NEW_LINE s += cntSubarrIthSet * pow ( 2 , i ) NEW_LINE DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( A ) NEW_LINE print ( givesum ( A , n ) ) NEW_LINE DEDENT


import java . util . ArrayList ; import java . util . List ; public class AllPalindromeSubstringsPossible { public static List < String > allPalindromeSubstring ( String s ) { List < String > list = new ArrayList < String > ( ) ; for ( float pivot = 0 ; pivot < s . length ( ) ; pivot += .5 ) { float palindromeRadius = pivot - ( int ) pivot ; while ( ( pivot + palindromeRadius ) < s . length ( ) && ( pivot - palindromeRadius ) >= 0 && s . charAt ( ( int ) ( pivot - palindromeRadius ) ) == s . charAt ( ( int ) ( pivot + palindromeRadius ) ) ) { list . add ( s . substring ( ( int ) ( pivot - palindromeRadius ) , ( int ) ( pivot + palindromeRadius + 1 ) ) ) ; palindromeRadius ++ ; } } return list ; } public static void main ( String [ ] args ) { List < String > list = allPalindromeSubstring ( " hellolle " ) ; System . out . println ( list . size ( ) ) ; System . out . println ( list ) ; list = allPalindromeSubstring ( " geeksforgeeks " ) ; System . out . println ( list . size ( ) ) ; System . out . println ( list ) ; } }

def substring ( s , a , b ) : NEW_LINE INDENT s1 = " " NEW_LINE for i in range ( a , b , 1 ) : NEW_LINE INDENT s1 += s [ i ] NEW_LINE DEDENT return s1 NEW_LINE DEDENT def allPalindromeSubstring ( s ) : NEW_LINE INDENT v = [ ] NEW_LINE pivot = 0.0 NEW_LINE while pivot < len ( s ) : NEW_LINE INDENT palindromeRadius = pivot - int ( pivot ) NEW_LINE while ( ( pivot + palindromeRadius ) < len ( s ) and ( pivot - palindromeRadius ) >= 0 and ( s [ int ( pivot - palindromeRadius ) ] == s [ int ( pivot + palindromeRadius ) ] ) ) : NEW_LINE INDENT v . append ( s [ int ( pivot - palindromeRadius ) : int ( pivot + palindromeRadius + 1 ) ] ) NEW_LINE palindromeRadius += 1 NEW_LINE DEDENT pivot += 0.5 NEW_LINE DEDENT return v NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT v = allPalindromeSubstring ( " hellolle " ) NEW_LINE print ( len ( v ) ) NEW_LINE print ( v ) NEW_LINE v = allPalindromeSubstring ( " geeksforgeeks " ) NEW_LINE print ( len ( v ) ) NEW_LINE print ( v ) NEW_LINE DEDENT


class GfG { static long ll ; static boolean is_power_of_two ( long n ) { return false ; } static int countSubArrays ( int arr [ ] , int n ) { int cnt = 0 ; for ( int j = 0 ; j < n ; j ++ ) { long xorval = 0 ; for ( int k = j ; k < n ; k ++ ) { long num = 1 << arr [ k ] ; xorval ^= num ; if ( xorval == 0 || is_power_of_two ( xorval ) ) cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( countSubArrays ( arr , n ) + "1" ) ; } }

def is_power_of_two ( n ) : NEW_LINE INDENT return 0 if ( n & ( n - 1 ) ) else 1 ; NEW_LINE DEDENT def countSubArrays ( arr , n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT xorval = 0 ; NEW_LINE for k in range ( j , n ) : NEW_LINE INDENT num = 1 << arr [ k ] ; NEW_LINE xorval ^= num ; NEW_LINE if ( xorval == 0 or is_power_of_two ( xorval ) ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT DEDENT return cnt ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countSubArrays ( arr , n ) ) ; NEW_LINE


public class GFG { static final int MAX = 256 ; static int freq [ ] [ ] ; static void preCalculate ( String str , int n ) { freq [ ( int ) str . charAt ( 0 ) ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { char ch = str . charAt ( i ) ; for ( int j = 0 ; j < MAX ; j ++ ) { char charToUpdate = ( char ) j ; if ( charToUpdate == ch ) freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 ; else freq [ j ] [ i ] = freq [ j ] [ i - 1 ] ; } } } static int getFrequency ( char ch , int l , int r ) { if ( l == 0 ) return freq [ ( int ) ch ] [ r ] ; else return ( freq [ ( int ) ch ] [ r ] - freq [ ( int ) ch ] [ l - 1 ] ) ; } static String firstNonRepeating ( String str , int n , int l , int r ) { for ( int i = l ; i < r ; i ++ ) { char ch = str . charAt ( i ) ; if ( getFrequency ( ch , l , r ) == 1 ) return ( " " + ch ) ; } return " - 1" ; } public static void main ( String [ ] args ) { String str = " GeeksForGeeks " ; int n = str . length ( ) ; int queries [ ] [ ] = { { 0 , 3 } , { 2 , 3 } , { 5 , 12 } } ; int q = queries . length ; freq = new int [ MAX ] [ n ] ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( firstNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } } }

MAX = 256 NEW_LINE freq = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] NEW_LINE def preCalculate ( string , n ) : NEW_LINE INDENT freq [ ord ( string [ 0 ] ) ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ch = string [ i ] NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT charToUpdate = chr ( j ) NEW_LINE if charToUpdate == ch : NEW_LINE INDENT freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ j ] [ i ] = freq [ j ] [ i - 1 ] NEW_LINE DEDENT DEDENT DEDENT DEDENT def getFrequency ( ch , l , r ) : NEW_LINE INDENT if l == 0 : NEW_LINE INDENT return freq [ ord ( ch ) ] [ r ] NEW_LINE DEDENT else : NEW_LINE INDENT return ( freq [ ord ( ch ) ] [ r ] - freq [ ord ( ch ) ] [ l - 1 ] ) NEW_LINE DEDENT DEDENT def firstNonRepeating ( string , n , l , r ) : NEW_LINE INDENT t = [ " " ] * 2 NEW_LINE for i in range ( l , r ) : NEW_LINE INDENT ch = string [ i ] NEW_LINE if getFrequency ( ch , l , r ) == 1 : NEW_LINE INDENT t [ 0 ] = ch NEW_LINE return t [ 0 ] NEW_LINE DEDENT DEDENT return " - 1" NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GeeksForGeeks " NEW_LINE n = len ( string ) NEW_LINE queries = [ ( 0 , 3 ) , ( 2 , 3 ) , ( 5 , 12 ) ] NEW_LINE q = len ( queries ) NEW_LINE preCalculate ( string , n ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( firstNonRepeating ( string , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) NEW_LINE DEDENT DEDENT


class GFG { static String s ; static void reverseStr ( int l , int h ) { int n = h - l ; for ( int i = 0 ; i < n / 2 ; i ++ ) { s = swap ( i + l , n - i - 1 + l ) ; } } static void reverseString ( int A [ ] , int n ) { reverseStr ( 0 , A [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) reverseStr ( A [ i - 1 ] , A [ i ] ) ; reverseStr ( A [ n - 1 ] , s . length ( ) ) ; } static String swap ( int i , int j ) { char ch [ ] = s . toCharArray ( ) ; char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . valueOf ( ch ) ; } public static void main ( String [ ] args ) { s = " abcdefgh " ; int A [ ] = { 2 , 4 , 6 } ; int n = A . length ; reverseString ( A , n ) ; System . out . print ( s ) ; } }

def reverseStr ( str , l , h ) : NEW_LINE INDENT n = h - l NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT str [ i + l ] , str [ n - i - 1 + l ] = str [ n - i - 1 + l ] , str [ i + l ] NEW_LINE DEDENT DEDENT def reverseString ( s , A , n ) : NEW_LINE INDENT reverseStr ( s , 0 , A [ 0 ] ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT reverseStr ( s , A [ i - 1 ] , A [ i ] ) NEW_LINE DEDENT reverseStr ( s , A [ n - 1 ] , len ( s ) ) NEW_LINE DEDENT s = " abcdefgh " NEW_LINE s = [ i for i in s ] NEW_LINE A = [ 2 , 4 , 6 ] NEW_LINE n = len ( A ) NEW_LINE reverseString ( s , A , n ) NEW_LINE print ( " " . join ( s ) ) NEW_LINE


class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int countUnsetBitsInGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( r - l + 1 ) - countSetBits ( n & num ) ; } public static void main ( String [ ] args ) { int n = 80 ; int l = 1 , r = 4 ; System . out . print ( countUnsetBitsInGivenRange ( n , l , r ) ) ; } }

def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while n : NEW_LINE INDENT n &= ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def countUnsetBitsInGivenRange ( n , l , r ) : NEW_LINE INDENT num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) NEW_LINE return ( r - l + 1 ) - countSetBits ( n & num ) NEW_LINE DEDENT n = 80 NEW_LINE l = 1 NEW_LINE r = 4 NEW_LINE print ( countUnsetBitsInGivenRange ( n , l , r ) ) NEW_LINE


import java . io . * ; class GFG { static long factorial ( int n ) { if ( n == 0 ) return 1 ; if ( n < 0 ) return - 1 ; long res = 1 ; for ( int i = 2 ; i < n + 1 ; ++ i ) res *= i ; return res ; } static int nCr ( int n , int r ) { if ( r > n ) return - 1 ; if ( n == r ) return 1 ; if ( r == 0 ) return 1 ; return nCr ( n - 1 , r - 1 ) + nCr ( n - 1 , r ) ; } static long stirlingNumber ( int r , int n ) { if ( n > r ) return - 1 ; if ( n == 0 ) return 0 ; if ( r == n ) return 1 ; if ( n == 1 ) return factorial ( r - 1 ) ; if ( r - n == 1 ) return nCr ( r , 2 ) ; else return stirlingNumber ( r - 1 , n - 1 ) + ( r - 1 ) * stirlingNumber ( r - 1 , n ) ; } public static void main ( String [ ] args ) { int r = 9 , n = 2 ; long val = stirlingNumber ( r , n ) ; if ( val == - 1 ) System . out . println ( " ▁ No ▁ stirling ▁ number " ) ; else System . out . println ( " The ▁ Stirling ▁ Number ▁ s ( " + r + " , ▁ " + n + " ) ▁ is ▁ : ▁ " + val ) ; } }

def nCr ( n , r ) : NEW_LINE INDENT if ( n == r ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( r == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return nCr ( n - 1 , r - 1 ) + nCr ( n - 1 , r ) NEW_LINE DEDENT def factorial ( n ) : NEW_LINE INDENT res = 1 NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return res NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT res *= i NEW_LINE DEDENT return res NEW_LINE DEDENT def stirlingNumber ( r , n ) : NEW_LINE INDENT if ( r == n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == r - 1 ) : NEW_LINE INDENT return nCr ( r , 2 ) NEW_LINE DEDENT if ( r - n == 1 ) : NEW_LINE INDENT return factorial ( r - 1 ) NEW_LINE DEDENT return ( stirlingNumber ( r - 1 , n - 1 ) + ( r - 1 ) * stirlingNumber ( r - 1 , n ) ) NEW_LINE DEDENT r , n = 9 , 2 NEW_LINE print ( stirlingNumber ( r , n ) ) NEW_LINE


class FindSmallestInteger { int findSmallest ( int arr [ ] , int n ) { int res = 1 ; for ( int i = 0 ; i < n && arr [ i ] <= res ; i ++ ) res = res + arr [ i ] ; return res ; } public static void main ( String [ ] args ) { FindSmallestInteger small = new FindSmallestInteger ( ) ; int arr1 [ ] = { 1 , 3 , 4 , 5 } ; int n1 = arr1 . length ; System . out . println ( small . findSmallest ( arr1 , n1 ) ) ; int arr2 [ ] = { 1 , 2 , 6 , 10 , 11 , 15 } ; int n2 = arr2 . length ; System . out . println ( small . findSmallest ( arr2 , n2 ) ) ; int arr3 [ ] = { 1 , 1 , 1 , 1 } ; int n3 = arr3 . length ; System . out . println ( small . findSmallest ( arr3 , n3 ) ) ; int arr4 [ ] = { 1 , 1 , 3 , 4 } ; int n4 = arr4 . length ; System . out . println ( small . findSmallest ( arr4 , n4 ) ) ; } }

def findSmallest ( arr , n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] <= res : NEW_LINE INDENT res = res + arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr1 = [ 1 , 3 , 4 , 5 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( findSmallest ( arr1 , n1 ) ) NEW_LINE arr2 = [ 1 , 2 , 6 , 10 , 11 , 15 ] NEW_LINE n2 = len ( arr2 ) NEW_LINE print ( findSmallest ( arr2 , n2 ) ) NEW_LINE arr3 = [ 1 , 1 , 1 , 1 ] NEW_LINE n3 = len ( arr3 ) NEW_LINE print ( findSmallest ( arr3 , n3 ) ) NEW_LINE arr4 = [ 1 , 1 , 3 , 4 ] NEW_LINE n4 = len ( arr4 ) NEW_LINE print ( findSmallest ( arr4 , n4 ) ) NEW_LINE


import java . util . * ; class GFG { static void findCombinationsUtil ( int arr [ ] , int index , int n , int red_num ) { HashSet < Integer > s = new HashSet < > ( ) ; int sum = 0 ; if ( red_num < 0 ) { return ; } if ( red_num == 0 ) { for ( int i = 0 ; i < index ; i ++ ) { s . add ( arr [ i ] ) ; } for ( Integer itr : s ) { sum = sum + itr ; } if ( sum == n ) { for ( Integer i : s ) { System . out . print ( i + " ▁ " ) ; } System . out . println ( ) ; return ; } } int prev = ( index == 0 ) ? 1 : arr [ index - 1 ] ; for ( int k = prev ; k <= n ; k ++ ) { if ( index < n ) { arr [ index ] = k ; findCombinationsUtil ( arr , index + 1 , n , red_num - k ) ; } } } static void findCombinations ( int n ) { int [ ] a = new int [ n ] ; findCombinationsUtil ( a , 0 , n , n ) ; } public static void main ( String arr [ ] ) { int n = 7 ; findCombinations ( n ) ; } }

def findCombinationsUtil ( arr , index , n , red_num ) : NEW_LINE INDENT s = set ( ) NEW_LINE sum = 0 NEW_LINE if ( red_num < 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( red_num == 0 ) : NEW_LINE INDENT for i in range ( index ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT for itr in s : NEW_LINE INDENT sum = sum + ( itr ) NEW_LINE DEDENT if ( sum == n ) : NEW_LINE INDENT for i in s : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE return NEW_LINE DEDENT DEDENT if ( index == 0 ) : NEW_LINE INDENT prev = 1 NEW_LINE DEDENT else : NEW_LINE INDENT prev = arr [ index - 1 ] NEW_LINE DEDENT for k in range ( prev , n + 1 , 1 ) : NEW_LINE INDENT arr [ index ] = k NEW_LINE findCombinationsUtil ( arr , index + 1 , n , red_num - k ) NEW_LINE DEDENT DEDENT def findCombinations ( n ) : NEW_LINE INDENT a = [ 0 for i in range ( n + 1 ) ] NEW_LINE findCombinationsUtil ( a , 0 , n , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 NEW_LINE findCombinations ( n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean validate ( long n ) { int count [ ] = new int [ 10 ] ; while ( n > 0 ) { int r = ( int ) n % 10 ; if ( count [ r ] == r ) return false ; count [ r ] ++ ; n /= 10 ; } return true ; } public static void main ( String [ ] args ) { long n = 1552793 ; if ( validate ( n ) ) System . out . println ( " True " ) ; else System . out . println ( " False " ) ; } }

import math as mt NEW_LINE def validate ( n ) : NEW_LINE INDENT count = [ 0 for i in range ( 10 ) ] NEW_LINE while ( n > 0 ) : NEW_LINE INDENT r = n % 10 NEW_LINE if ( count [ r ] == r ) : NEW_LINE INDENT return False NEW_LINE DEDENT count [ r ] += 1 NEW_LINE n = n // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 1552793 NEW_LINE if ( validate ( n ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT


class Test { static int factorial ( int n ) { int i = n , fact = 1 ; while ( n / i != n ) { fact = fact * i ; i -- ; } return fact ; } public static void main ( String [ ] args ) { int num = 5 ; System . out . println ( " Factorial ▁ of ▁ " + num + " ▁ is ▁ " + factorial ( 5 ) ) ; } }

def factorial ( n ) : NEW_LINE INDENT i = n NEW_LINE fact = 1 NEW_LINE while ( n / i != n ) : NEW_LINE INDENT fact = fact * i NEW_LINE i -= 1 NEW_LINE DEDENT return fact NEW_LINE DEDENT num = 5 ; NEW_LINE print ( " Factorial ▁ of " , num , " is " , factorial ( num ) ) NEW_LINE


import java . util . * ; class Solution { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static void findTwoNumbers ( int sum , int gcd ) { if ( __gcd ( gcd , sum - gcd ) == gcd && sum != gcd ) System . out . println ( " a ▁ = ▁ " + Math . min ( gcd , sum - gcd ) + " , ▁ b ▁ = ▁ " + ( int ) ( sum - Math . min ( gcd , sum - gcd ) ) ) ; else System . out . println ( - 1 ) ; } public static void main ( String args [ ] ) { int sum = 8 ; int gcd = 2 ; findTwoNumbers ( sum , gcd ) ; } }

from math import gcd as __gcd NEW_LINE def findTwoNumbers ( sum , gcd ) : NEW_LINE INDENT if ( __gcd ( gcd , sum - gcd ) == gcd and sum != gcd ) : NEW_LINE INDENT print ( " a ▁ = " , min ( gcd , sum - gcd ) , " , ▁ b ▁ = " , sum - min ( gcd , sum - gcd ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sum = 8 NEW_LINE gcd = 2 NEW_LINE findTwoNumbers ( sum , gcd ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int solve ( int M , int N , int s ) { if ( N % s == 0 ) { N = N / s ; } else { N = ( N / s ) + 1 ; } if ( M % s == 0 ) { M = M / s ; } else { M = ( M / s ) + 1 ; } return M * N ; } public static void main ( String args [ ] ) { int N = 12 , M = 13 , s = 4 ; System . out . println ( solve ( M , N , s ) ) ; } }

def solve ( M , N , s ) : NEW_LINE INDENT if ( N % s == 0 ) : NEW_LINE INDENT N = N // s NEW_LINE DEDENT else : NEW_LINE INDENT N = ( N // s ) + 1 NEW_LINE DEDENT if ( M % s == 0 ) : NEW_LINE INDENT M = M // s NEW_LINE DEDENT else : NEW_LINE INDENT M = ( M // s ) + 1 NEW_LINE DEDENT return M * N NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , s = 12 , 13 , 4 NEW_LINE print ( solve ( M , N , s ) ) NEW_LINE DEDENT


public class GFG { static int countWords ( String str , int len ) { int count = 1 ; if ( len == 1 ) return count ; if ( str . charAt ( 0 ) == str . charAt ( 1 ) ) count *= 1 ; else count *= 2 ; for ( int j = 1 ; j < len - 1 ; j ++ ) { if ( str . charAt ( j ) == str . charAt ( j - 1 ) && str . charAt ( j ) == str . charAt ( j + 1 ) ) count *= 1 ; else if ( str . charAt ( j ) == str . charAt ( j - 1 ) || str . charAt ( j ) == str . charAt ( j + 1 ) || str . charAt ( j - 1 ) == str . charAt ( j + 1 ) ) count *= 2 ; else count *= 3 ; } if ( str . charAt ( len - 1 ) == str . charAt ( len - 2 ) ) count *= 1 ; else count *= 2 ; return count ; } public static void main ( String args [ ] ) { String str = " abc " ; int len = str . length ( ) ; System . out . println ( countWords ( str , len ) ) ; } }

def countWords ( str , l ) : NEW_LINE INDENT count = 1 ; NEW_LINE if ( l == 1 ) : NEW_LINE INDENT return count NEW_LINE DEDENT if ( str [ 0 ] == str [ 1 ] ) : NEW_LINE INDENT count *= 1 NEW_LINE DEDENT else : NEW_LINE INDENT count *= 2 NEW_LINE DEDENT for j in range ( 1 , l - 1 ) : NEW_LINE INDENT if ( str [ j ] == str [ j - 1 ] and str [ j ] == str [ j + 1 ] ) : NEW_LINE INDENT count *= 1 NEW_LINE DEDENT elif ( str [ j ] == str [ j - 1 ] or str [ j ] == str [ j + 1 ] or str [ j - 1 ] == str [ j + 1 ] ) : NEW_LINE INDENT count *= 2 NEW_LINE DEDENT else : NEW_LINE INDENT count *= 3 NEW_LINE DEDENT DEDENT if ( str [ l - 1 ] == str [ l - 2 ] ) : NEW_LINE INDENT count *= 1 NEW_LINE DEDENT else : NEW_LINE INDENT count *= 2 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " abc " NEW_LINE l = len ( str ) NEW_LINE print ( countWords ( str , l ) ) NEW_LINE DEDENT


import java . util . * ; import java . io . * ; class GFG { public static void sortSquares ( int arr [ ] ) { int n = arr . length ; int k ; for ( k = 0 ; k < n ; k ++ ) { if ( arr [ k ] >= 0 ) break ; } int i = k - 1 ; int j = k ; int ind = 0 ; int [ ] temp = new int [ n ] ; while ( i >= 0 && j < n ) { if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) { temp [ ind ] = arr [ i ] * arr [ i ] ; i -- ; } else { temp [ ind ] = arr [ j ] * arr [ j ] ; j ++ ; } ind ++ ; } while ( i >= 0 ) { temp [ ind ++ ] = arr [ i ] * arr [ i ] ; i -- ; } while ( j < n ) { temp [ ind ++ ] = arr [ j ] * arr [ j ] ; j ++ ; } for ( int x = 0 ; x < n ; x ++ ) arr [ x ] = temp [ x ] ; } public static void main ( String [ ] args ) { int arr [ ] = { - 6 , - 3 , - 1 , 2 , 4 , 5 } ; int n = arr . length ; System . out . println ( " Before ▁ sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; sortSquares ( arr ) ; System . out . println ( " " ) ; System . out . println ( " After ▁ Sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }

def sortSquares ( arr , n ) : NEW_LINE INDENT K = 0 NEW_LINE for K in range ( n ) : NEW_LINE INDENT if ( arr [ K ] >= 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i = K - 1 NEW_LINE j = K NEW_LINE ind = 0 NEW_LINE temp = [ 0 ] * n NEW_LINE while ( i >= 0 and j < n ) : NEW_LINE INDENT if ( arr [ i ] * arr [ i ] < arr [ j ] * arr [ j ] ) : NEW_LINE INDENT temp [ ind ] = arr [ i ] * arr [ i ] NEW_LINE i -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp [ ind ] = arr [ j ] * arr [ j ] NEW_LINE j += 1 NEW_LINE DEDENT ind += 1 NEW_LINE DEDENT while ( i >= 0 ) : NEW_LINE INDENT temp [ ind ] = arr [ i ] * arr [ i ] NEW_LINE i -= 1 NEW_LINE ind += 1 NEW_LINE DEDENT while ( j < n ) : NEW_LINE INDENT temp [ ind ] = arr [ j ] * arr [ j ] NEW_LINE j += 1 NEW_LINE ind += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = temp [ i ] NEW_LINE DEDENT DEDENT arr = [ - 6 , - 3 , - 1 , 2 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Before ▁ sort ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT sortSquares ( arr , n ) NEW_LINE print ( " \n After ▁ Sort ▁ " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT


class AlternateSetBits { static boolean allBitsAreSet ( int n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } static boolean bitsAreInAltOrder ( int n ) { int num = n ^ ( n >>> 1 ) ; return allBitsAreSet ( num ) ; } public static void main ( String args [ ] ) { int n = 10 ; if ( bitsAreInAltOrder ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def allBitsAreSet ( n ) : NEW_LINE INDENT if ( ( ( n + 1 ) & n ) == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def bitsAreInAltOrder ( n ) : NEW_LINE INDENT num = n ^ ( n >> 1 ) ; NEW_LINE return allBitsAreSet ( num ) ; NEW_LINE DEDENT n = 10 ; NEW_LINE if ( bitsAreInAltOrder ( n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT


class GFG { public static void main ( String [ ] args ) { int [ ] [ ] matrix = new int [ 5 ] [ 5 ] ; int 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 Corner ▁ Elements ▁ are : \n " ) ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { if ( ( row_index == 0 || row_index == size - 1 ) && ( column_index == 0 || column_index == size - 1 ) ) 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 Corner ▁ 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 == 0 or row_index == size - 1 ) and ( column_index == 0 or column_index == size - 1 ) ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " \t " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT