class GFG { static boolean isEven ( int n ) { return ( ( n & 1 ) != 1 ) ; } public static void main ( String [ ] args ) { int n = 101 ; System . out . print ( isEven ( n ) == true ? " Even " : " Odd " ) ; } }

def isEven ( n ) : NEW_LINE INDENT return ( not ( n & 1 ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 101 ; NEW_LINE if isEven ( n ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) ; NEW_LINE DEDENT DEDENT


class GFG { static void NumberOfShortestPaths ( int n , int m ) { int [ ] [ ] a = new int [ n ] [ m ] ; for ( int i = n - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < m ; j ++ ) { if ( j == 0 || i == n - 1 ) a [ i ] [ j ] = 1 ; else a [ i ] [ j ] = a [ i ] [ j - 1 ] + a [ i + 1 ] [ j ] ; } } for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { System . out . print ( a [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 5 , m = 2 ; NumberOfShortestPaths ( n , m ) ; } }

def NumberOfShortestPaths ( n , m ) : NEW_LINE INDENT a = [ [ 0 for i in range ( m ) ] for j in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT a [ i ] [ j ] = 0 NEW_LINE DEDENT DEDENT i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( j == 0 or i == n - 1 ) : NEW_LINE INDENT a [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT a [ i ] [ j ] = a [ i ] [ j - 1 ] +   \ NEW_LINE INDENT a [ i + 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT i -= 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT print ( a [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE m = 2 NEW_LINE NumberOfShortestPaths ( n , m ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int findLen ( int [ ] arr , int n , int m ) { Vector < Integer > filter = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & m ) == m ) filter . add ( arr [ i ] ) ; if ( filter . size ( ) == 0 ) return 0 ; int c_and = filter . get ( 0 ) ; for ( int i = 1 ; i < filter . size ( ) ; i ++ ) c_and &= filter . get ( i ) ; if ( c_and == m ) return filter . size ( ) ; return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 3 , 3 , 1 , 3 } ; int n = arr . length ; int m = 3 ; System . out . println ( findLen ( arr , n , m ) ) ; } }

def findLen ( arr , n , m ) : NEW_LINE INDENT filter = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr [ i ] & m ) == m ) : NEW_LINE INDENT filter . append ( arr [ i ] ) ; NEW_LINE DEDENT DEDENT if ( len ( filter ) == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT c_and = filter [ 0 ] ; NEW_LINE for i in range ( 1 , len ( filter ) ) : NEW_LINE INDENT c_and &= filter [ i ] ; NEW_LINE DEDENT if ( c_and == m ) : NEW_LINE INDENT return len ( filter ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 3 , 3 , 1 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE m = 3 ; NEW_LINE print ( findLen ( arr , n , m ) ) ; NEW_LINE DEDENT


class GFG { static int xorPairSum ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum ^ ar [ i ] ; return 2 * sum ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( xorPairSum ( arr , n ) ) ; } }

def xor_pair_sum ( ar , n ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total = total ^ ar [ i ] NEW_LINE DEDENT return 2 * total NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT data = [ 1 , 2 , 3 ] NEW_LINE print ( xor_pair_sum ( data , len ( data ) ) ) NEW_LINE DEDENT


class Test { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } public static void main ( String [ ] args ) { int a = 15 , b = 20 ; System . out . println ( " LCM ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + lcm ( a , b ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if a == 0 : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def lcm ( a , b ) : NEW_LINE INDENT return ( a * b ) / gcd ( a , b ) NEW_LINE DEDENT a = 15 NEW_LINE b = 20 NEW_LINE print ( ' LCM ▁ of ' , a , ' and ' , b , ' is ' , lcm ( a , b ) ) NEW_LINE


import java . util . * ; class solution { static int nthTerm ( int n ) { return 2 * ( int ) Math . pow ( n , 2 ) - n - 1 ; } public static void main ( String arr [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }

def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 2 ) - n - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT


class GFG { static void printArray ( int arr [ ] , int len ) { int i ; for ( i = 0 ; i < len ; i ++ ) { System . out . printf ( " % d ▁ " , arr [ i ] ) ; } } public static void main ( String [ ] args ) { int original_arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int len = original_arr . length ; int copied_arr [ ] = new int [ len ] , i , j ; for ( i = 0 ; i < len ; i ++ ) { copied_arr [ i ] = original_arr [ len - i - 1 ] ; } System . out . printf ( " \n Original ▁ array : ▁ " ) ; printArray ( original_arr , len ) ; System . out . printf ( " \n Resultant ▁ array : ▁ " ) ; printArray ( copied_arr , len ) ; } }

import math as mt NEW_LINE def printArray ( arr , Len ) : NEW_LINE INDENT for i in range ( Len ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT original_arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE Len = len ( original_arr ) NEW_LINE copied_arr = [ 0 for i in range ( Len ) ] NEW_LINE for i in range ( Len ) : NEW_LINE INDENT copied_arr [ i ] = original_arr [ Len - i - 1 ] NEW_LINE DEDENT print ( " Original ▁ array : ▁ " , end = " " ) NEW_LINE printArray ( original_arr , Len ) NEW_LINE print ( " \n Resultant ▁ array : ▁ " , end = " " ) NEW_LINE printArray ( copied_arr , Len ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static long fourthPowerSum ( int n ) { return ( ( 6 * n * n * n * n * n ) + ( 15 * n * n * n * n ) + ( 10 * n * n * n ) - n ) / 30 ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( fourthPowerSum ( n ) ) ; } }

import math NEW_LINE def fourthPowerSum ( n ) : NEW_LINE INDENT return ( ( 6 * n * n * n * n * n ) + ( 15 * n * n * n * n ) + ( 10 * n * n * n ) - n ) / 30 NEW_LINE DEDENT n = 6 NEW_LINE print ( fourthPowerSum ( n ) ) NEW_LINE


class GFG { static int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } public static void main ( String [ ] args ) { int num = 4562 ; System . out . println ( " Reverse ▁ of ▁ no . ▁ is ▁ " + reversDigits ( num ) ) ; } }

n = 4562 ; NEW_LINE rev = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT a = n % 10 NEW_LINE rev = rev * 10 + a NEW_LINE n = n // 10 NEW_LINE DEDENT print ( rev ) NEW_LINE


import java . io . * ; class GFG { static float altitude ( float a , float b ) { return ( float ) ( Math . sqrt ( Math . pow ( a , 2 ) - ( Math . pow ( b , 2 ) / 4 ) ) ) ; } static float area ( float b , float h ) { return ( 1 * b * h ) / 2 ; } public static void main ( String [ ] args ) { float a = 2 , b = 3 ; float h = altitude ( a , b ) ; System . out . print ( " Altitude = ▁ " + h + " , ▁ " ) ; System . out . print ( " Area = ▁ " + area ( b , h ) ) ; } }

import math NEW_LINE def altitude ( a , b ) : NEW_LINE INDENT return math . sqrt ( pow ( a , 2 ) - ( pow ( b , 2 ) / 4 ) ) NEW_LINE DEDENT def area ( b , h ) : NEW_LINE INDENT return ( 1 * b * h ) / 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2 NEW_LINE b = 3 NEW_LINE h = altitude ( a , b ) NEW_LINE print ( " Altitude ▁ = ▁ " + str ( round ( h , 3 ) ) , end = " , ▁ " ) NEW_LINE print ( " Area ▁ = ▁ " + str ( round ( area ( b , h ) , 3 ) ) ) NEW_LINE DEDENT


class GFG { static int MAX = 100000 ; static int sequence [ ] = new int [ MAX + 1 ] ; static void vanEckSequence ( ) { for ( int i = 0 ; i < MAX ; i ++ ) { sequence [ i ] = 0 ; } for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( sequence [ j ] == sequence [ i ] ) { sequence [ i + 1 ] = i - j ; break ; } } } } static int getCount ( int n ) { int nthTerm = sequence [ n - 1 ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( sequence [ i ] == nthTerm ) count ++ ; } return count ; } public static void main ( String [ ] args ) { vanEckSequence ( ) ; int n = 5 ; System . out . println ( getCount ( n ) ) ; n = 11 ; System . out . println ( getCount ( n ) ) ; } }

MAX = 10000 NEW_LINE sequence = [ 0 ] * ( MAX + 1 ) ; NEW_LINE def vanEckSequence ( ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( sequence [ j ] == sequence [ i ] ) : NEW_LINE INDENT sequence [ i + 1 ] = i - j ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def getCount ( n ) : NEW_LINE INDENT nthTerm = sequence [ n - 1 ] ; NEW_LINE count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( sequence [ i ] == nthTerm ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT vanEckSequence ( ) ; NEW_LINE n = 5 ; NEW_LINE print ( getCount ( n ) ) ; NEW_LINE n = 11 ; NEW_LINE print ( getCount ( n ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int factorial ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } static int calculateSeries ( int n ) { return 2 + ( n * n + n - 2 ) * factorial ( n + 1 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( calculateSeries ( n ) ) ; } }

import math NEW_LINE def factorial ( n ) : NEW_LINE INDENT res = 1 NEW_LINE i = 2 NEW_LINE for i in ( n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT def calculateSeries ( n ) : NEW_LINE INDENT return ( 2 + ( n * n + n - 2 ) * math . factorial ( n + 1 ) ) NEW_LINE DEDENT n = 3 NEW_LINE print ( calculateSeries ( n ) ) NEW_LINE


import java . util . Arrays ; public class GFG { static int findSum ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int sum = arr [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] != arr [ i + 1 ] ) { sum = sum + arr [ i + 1 ] ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 } ; int n = arr . length ; System . out . println ( findSum ( arr , n ) ) ; } }

def findSum ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE sum = arr [ 0 ] NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] ) : NEW_LINE INDENT sum = sum + arr [ i + 1 ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 1 , 1 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSum ( arr , n ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


public class GFG { static int CountPS ( char str [ ] , int n ) { int dp [ ] [ ] = new int [ n ] [ n ] ; boolean P [ ] [ ] = new boolean [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) P [ i ] [ i ] = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ i + 1 ] ) { P [ i ] [ i + 1 ] = true ; dp [ i ] [ i + 1 ] = 1 ; } } for ( int gap = 2 ; gap < n ; gap ++ ) { for ( int i = 0 ; i < n - gap ; i ++ ) { int j = gap + i ; if ( str [ i ] == str [ j ] && P [ i + 1 ] [ j - 1 ] ) P [ i ] [ j ] = true ; if ( P [ i ] [ j ] == true ) dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] + 1 - dp [ i + 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j - 1 ] ; } } return dp [ 0 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { String str = " abaab " ; System . out . println ( CountPS ( str . toCharArray ( ) , str . length ( ) ) ) ; } }

def CountPS ( str , n ) : NEW_LINE INDENT dp = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] NEW_LINE P = [ [ False for x in range ( n ) ] for y in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT P [ i ] [ i ] = True NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT if ( str [ i ] == str [ i + 1 ] ) : NEW_LINE INDENT P [ i ] [ i + 1 ] = True NEW_LINE dp [ i ] [ i + 1 ] = 1 NEW_LINE DEDENT DEDENT for gap in range ( 2 , n ) : NEW_LINE INDENT for i in range ( n - gap ) : NEW_LINE INDENT j = gap + i ; NEW_LINE if ( str [ i ] == str [ j ] and P [ i + 1 ] [ j - 1 ] ) : NEW_LINE INDENT P [ i ] [ j ] = True NEW_LINE DEDENT if ( P [ i ] [ j ] == True ) : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] + 1 - dp [ i + 1 ] [ j - 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i + 1 ] [ j ] - dp [ i + 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ 0 ] [ n - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " abaab " NEW_LINE n = len ( str ) NEW_LINE print ( CountPS ( str , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printNumber ( int holes ) { if ( holes == 0 ) System . out . print ( "1" ) ; else if ( holes == 1 ) System . out . print ( "0" ) ; else { int rem = 0 , quo = 0 ; rem = holes % 2 ; quo = holes / 2 ; if ( rem == 1 ) System . out . print ( "4" ) ; for ( int i = 0 ; i < quo ; i ++ ) System . out . print ( "8" ) ; } } public static void main ( String [ ] args ) { int holes = 3 ; printNumber ( holes ) ; } }

def printNumber ( holes ) : NEW_LINE INDENT if ( holes == 0 ) : NEW_LINE INDENT print ( "1" ) NEW_LINE DEDENT elif ( holes == 1 ) : NEW_LINE INDENT print ( "0" , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT rem = 0 NEW_LINE quo = 0 NEW_LINE rem = holes % 2 NEW_LINE quo = holes // 2 NEW_LINE if ( rem == 1 ) : NEW_LINE INDENT print ( "4" , end = " " ) NEW_LINE DEDENT for i in range ( quo ) : NEW_LINE INDENT print ( "8" , end = " " ) NEW_LINE DEDENT DEDENT DEDENT holes = 3 NEW_LINE printNumber ( holes ) NEW_LINE


import java . util . * ; public class GfG { public static int findNumbers ( int N ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; while ( N > 0 ) { int n = N , m = 0 , p = 1 ; while ( n > 0 ) { if ( n % 10 != 0 ) m += p ; n /= 10 ; p *= 10 ; } v . add ( m ) ; N -= m ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( " ▁ " + v . get ( i ) ) ; return 0 ; } public static void main ( String [ ] args ) { int N = 31 ; findNumbers ( N ) ; } }

def findNumbers ( N ) : NEW_LINE INDENT v = [ ] ; NEW_LINE while ( N ) : NEW_LINE INDENT n , m , p = N , 0 , 1 NEW_LINE while ( n ) : NEW_LINE INDENT if ( n % 10 ) : NEW_LINE INDENT m += p NEW_LINE DEDENT n //= 10 NEW_LINE p *= 10 NEW_LINE DEDENT v . append ( m ) ; NEW_LINE N -= m NEW_LINE DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT print ( v [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 31 NEW_LINE findNumbers ( N ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int minimumSubarrays ( int ar [ ] , int n ) { Vector se = new Vector ( ) ; int cnt = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( se . contains ( ar [ i ] ) == false ) { se . add ( ar [ i ] ) ; } else { cnt ++ ; se . clear ( ) ; se . add ( ar [ i ] ) ; } } return cnt ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 1 , 3 , 4 , 2 , 4 , 4 , 4 } ; int n = ar . length ; System . out . println ( minimumSubarrays ( ar , n ) ) ; } }

def minimumSubarrays ( ar , n ) : NEW_LINE INDENT se = [ ] NEW_LINE cnt = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if se . count ( ar [ i ] ) == 0 : NEW_LINE INDENT se . append ( ar [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE se . clear ( ) NEW_LINE se . append ( ar [ i ] ) NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT ar = [ 1 , 2 , 1 , 3 , 4 , 2 , 4 , 4 , 4 ] NEW_LINE n = len ( ar ) NEW_LINE print ( minimumSubarrays ( ar , n ) ) NEW_LINE


class GFG { static String strBitwiseOR ( String [ ] arr , int n ) { String res = " " ; int max_size = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { max_size = Math . max ( max_size , ( int ) arr [ i ] . length ( ) ) ; arr [ i ] = reverse ( arr [ i ] ) ; } for ( int i = 0 ; i < n ; i ++ ) { String s = " " ; for ( int j = 0 ; j < max_size - arr [ i ] . length ( ) ; j ++ ) s += '0' ; arr [ i ] = arr [ i ] + s ; } for ( int i = 0 ; i < max_size ; i ++ ) { int curr_bit = 0 ; for ( int j = 0 ; j < n ; j ++ ) curr_bit = curr_bit | ( arr [ j ] . charAt ( i ) - '0' ) ; res += ( char ) ( curr_bit + '0' ) ; } res = reverse ( res ) ; return res ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { String arr [ ] = { "10" , "11" , "1000001" } ; int n = arr . length ; System . out . println ( strBitwiseOR ( arr , n ) ) ; } }

def strBitwiseOR ( arr , n ) : NEW_LINE INDENT res = " " NEW_LINE max_size = - ( 2 ** 32 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_size = max ( max_size , len ( arr [ i ] ) ) NEW_LINE arr [ i ] = arr [ i ] [ : : - 1 ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT s = " " NEW_LINE for j in range ( max_size - len ( arr [ i ] ) ) : NEW_LINE INDENT s += '0' NEW_LINE DEDENT arr [ i ] = arr [ i ] + s NEW_LINE DEDENT for i in range ( max_size ) : NEW_LINE INDENT curr_bit = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT curr_bit = curr_bit | ord ( arr [ j ] [ i ] ) NEW_LINE DEDENT res += chr ( curr_bit ) NEW_LINE DEDENT res = res [ : : - 1 ] NEW_LINE return res NEW_LINE DEDENT arr = [ "10" , "11" , "1000001" ] NEW_LINE n = len ( arr ) NEW_LINE print ( strBitwiseOR ( arr , n ) ) NEW_LINE


class GFG { static int min_elimination ( int n , int arr [ ] ) { int countOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] % 2 == 1 ) countOdd ++ ; return Math . min ( countOdd , n - countOdd ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 7 , 9 } ; int n = arr . length ; System . out . println ( min_elimination ( n , arr ) ) ; } }

def min_elimination ( n , arr ) : NEW_LINE INDENT countOdd = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 ) : NEW_LINE INDENT countOdd += 1 NEW_LINE DEDENT DEDENT return min ( countOdd , n - countOdd ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( min_elimination ( n , arr ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printNumber ( int n ) { int count [ ] = new int [ 10 ] ; while ( n > 0 ) { count [ n % 10 ] ++ ; n /= 10 ; } for ( int i = 1 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < count [ i ] * i ; j ++ ) System . out . print ( i ) ; } } public static void main ( String [ ] args ) { int n = 3225 ; printNumber ( n ) ; } }

def printNumber ( n ) : NEW_LINE INDENT count = [ 0 ] * 10 NEW_LINE while ( n ) : NEW_LINE INDENT count [ n % 10 ] += 1 NEW_LINE n //= 10 NEW_LINE DEDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT for j in range ( count [ i ] * i ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3225 NEW_LINE printNumber ( n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void interLeaveQueue ( Queue < Integer > q ) { if ( q . size ( ) % 2 != 0 ) System . out . println ( " Input ▁ even ▁ number ▁ of ▁ integers . " ) ; Stack < Integer > s = new Stack < > ( ) ; int halfSize = q . size ( ) / 2 ; for ( int i = 0 ; i < halfSize ; i ++ ) { s . push ( q . peek ( ) ) ; q . poll ( ) ; } while ( ! s . empty ( ) ) { q . add ( s . peek ( ) ) ; s . pop ( ) ; } for ( int i = 0 ; i < halfSize ; i ++ ) { q . add ( q . peek ( ) ) ; q . poll ( ) ; } for ( int i = 0 ; i < halfSize ; i ++ ) { s . push ( q . peek ( ) ) ; q . poll ( ) ; } while ( ! s . empty ( ) ) { q . add ( s . peek ( ) ) ; s . pop ( ) ; q . add ( q . peek ( ) ) ; q . poll ( ) ; } } public static void main ( String [ ] args ) { Queue < Integer > q = new java . util . LinkedList < > ( ) ; q . add ( 11 ) ; q . add ( 12 ) ; q . add ( 13 ) ; q . add ( 14 ) ; q . add ( 15 ) ; q . add ( 16 ) ; q . add ( 17 ) ; q . add ( 18 ) ; q . add ( 19 ) ; q . add ( 20 ) ; interLeaveQueue ( q ) ; int length = q . size ( ) ; for ( int i = 0 ; i < length ; i ++ ) { System . out . print ( q . peek ( ) + " ▁ " ) ; q . poll ( ) ; } } }

from queue import Queue NEW_LINE def interLeaveQueue ( q ) : NEW_LINE INDENT if ( q . qsize ( ) % 2 != 0 ) : NEW_LINE INDENT print ( " Input ▁ even ▁ number ▁ of ▁ integers . " ) NEW_LINE DEDENT s = [ ] NEW_LINE halfSize = int ( q . qsize ( ) / 2 ) NEW_LINE for i in range ( halfSize ) : NEW_LINE INDENT s . append ( q . queue [ 0 ] ) NEW_LINE q . get ( ) NEW_LINE DEDENT while len ( s ) != 0 : NEW_LINE INDENT q . put ( s [ - 1 ] ) NEW_LINE s . pop ( ) NEW_LINE DEDENT for i in range ( halfSize ) : NEW_LINE INDENT q . put ( q . queue [ 0 ] ) NEW_LINE q . get ( ) NEW_LINE DEDENT for i in range ( halfSize ) : NEW_LINE INDENT s . append ( q . queue [ 0 ] ) NEW_LINE q . get ( ) NEW_LINE DEDENT while len ( s ) != 0 : NEW_LINE INDENT q . put ( s [ - 1 ] ) NEW_LINE s . pop ( ) NEW_LINE q . put ( q . queue [ 0 ] ) NEW_LINE q . get ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = Queue ( ) NEW_LINE q . put ( 11 ) NEW_LINE q . put ( 12 ) NEW_LINE q . put ( 13 ) NEW_LINE q . put ( 14 ) NEW_LINE q . put ( 15 ) NEW_LINE q . put ( 16 ) NEW_LINE q . put ( 17 ) NEW_LINE q . put ( 18 ) NEW_LINE q . put ( 19 ) NEW_LINE q . put ( 20 ) NEW_LINE interLeaveQueue ( q ) NEW_LINE length = q . qsize ( ) NEW_LINE for i in range ( length ) : NEW_LINE INDENT print ( q . queue [ 0 ] , end = " ▁ " ) NEW_LINE q . get ( ) NEW_LINE DEDENT DEDENT


class GFG { static int count ( int n ) { return ( int ) ( - 1 + Math . sqrt ( 1 + 8 * n ) ) / 2 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( count ( n ) ) ; } }

from math import sqrt NEW_LINE def count ( n ) : NEW_LINE INDENT return ( - 1 + sqrt ( 1 + 8 * n ) ) // 2 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; NEW_LINE print ( count ( n ) ) ; NEW_LINE DEDENT


class GFG { static int rev_num = 0 ; static int base_pos = 1 ; static int reversDigits ( int num ) { if ( num > 0 ) { reversDigits ( num / 10 ) ; rev_num += ( num % 10 ) * base_pos ; base_pos *= 10 ; } return rev_num ; } public static void main ( String [ ] args ) { int num = 4562 ; System . out . println ( reversDigits ( num ) ) ; } }

rev_num = 0 NEW_LINE base_pos = 1 NEW_LINE def reversDigits ( num ) : NEW_LINE INDENT global rev_num NEW_LINE global base_pos NEW_LINE if ( num > 0 ) : NEW_LINE INDENT reversDigits ( ( int ) ( num / 10 ) ) NEW_LINE rev_num += ( num % 10 ) * base_pos NEW_LINE base_pos *= 10 NEW_LINE DEDENT return rev_num NEW_LINE DEDENT num = 4562 NEW_LINE print ( " Reverse ▁ of ▁ no . ▁ is ▁ " , reversDigits ( num ) ) NEW_LINE


class GFG { static boolean areChractersUnique ( String str ) { int checker = 0 ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { int val = ( str . charAt ( i ) - ' a ' ) ; if ( ( checker & ( 1 << val ) ) > 0 ) return false ; checker |= ( 1 << val ) ; } return true ; } public static void main ( String [ ] args ) { String s = " aaabbccdaa " ; if ( areChractersUnique ( s ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def areCharactersUnique ( s ) : NEW_LINE INDENT checker = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) - ord ( ' a ' ) NEW_LINE if ( checker & ( 1 << val ) ) > 0 : NEW_LINE INDENT return False NEW_LINE DEDENT checker |= ( 1 << val ) NEW_LINE DEDENT return True NEW_LINE DEDENT s = " aaabbccdaa " NEW_LINE if areCharactersUnique ( s ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GfG { static int countSteps ( int x , int y ) { if ( x < y ) { return x + y + 2 * ( ( y - x ) / 2 ) ; } else { return x + y + 2 * ( ( ( x - y ) + 1 ) / 2 ) ; } } public static void main ( String [ ] args ) { int x = 4 , y = 3 ; System . out . println ( countSteps ( x , y ) ) ; } }

def countSteps ( x , y ) : NEW_LINE INDENT if x < y : NEW_LINE INDENT return x + y + 2 * ( ( y - x ) // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT return x + y + 2 * ( ( ( x - y ) + 1 ) // 2 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y = 4 , 3 NEW_LINE print ( countSteps ( x , y ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static float trianglearea ( float l , float b ) { if ( l < 0 || b < 0 ) return - 1 ; float area = ( l * b ) / 2 ; return area ; } public static void main ( String args [ ] ) { float l = 5 , b = 4 ; System . out . println ( trianglearea ( l , b ) ) ; } }

def trianglearea ( l , b ) : NEW_LINE INDENT if ( l < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT area = ( l * b ) / 2 NEW_LINE return area NEW_LINE DEDENT l = 5 NEW_LINE b = 4 NEW_LINE print ( trianglearea ( l , b ) ) NEW_LINE


class GFG { static final int MAX = 100 ; static int maxRowDiff ( int mat [ ] [ ] , int m , int n ) { int rowSum [ ] = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) sum += mat [ i ] [ j ] ; rowSum [ i ] = sum ; } int max_diff = rowSum [ 1 ] - rowSum [ 0 ] ; int min_element = rowSum [ 0 ] ; for ( int i = 1 ; i < m ; i ++ ) { if ( rowSum [ i ] - min_element > max_diff ) max_diff = rowSum [ i ] - min_element ; if ( rowSum [ i ] < min_element ) min_element = rowSum [ i ] ; } return max_diff ; } public static void main ( String [ ] args ) { int m = 5 , n = 4 ; int mat [ ] [ ] = { { - 1 , 2 , 3 , 4 } , { 5 , 3 , - 2 , 1 } , { 6 , 7 , 2 , - 3 } , { 2 , 9 , 1 , 4 } , { 2 , 1 , - 2 , 0 } } ; System . out . print ( maxRowDiff ( mat , m , n ) ) ; } }

def maxRowDiff ( mat , m , n ) : NEW_LINE INDENT rowSum = [ 0 ] * m NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT sum += mat [ i ] [ j ] NEW_LINE DEDENT rowSum [ i ] = sum NEW_LINE DEDENT max_diff = rowSum [ 1 ] - rowSum [ 0 ] NEW_LINE min_element = rowSum [ 0 ] NEW_LINE for i in range ( 1 , m ) : NEW_LINE INDENT if ( rowSum [ i ] - min_element > max_diff ) : NEW_LINE INDENT max_diff = rowSum [ i ] - min_element NEW_LINE DEDENT if ( rowSum [ i ] < min_element ) : NEW_LINE INDENT min_element = rowSum [ i ] NEW_LINE DEDENT DEDENT return max_diff NEW_LINE DEDENT m = 5 NEW_LINE n = 4 NEW_LINE mat = [ [ - 1 , 2 , 3 , 4 ] , [ 5 , 3 , - 2 , 1 ] , [ 6 , 7 , 2 , - 3 ] , [ 2 , 9 , 1 , 4 ] , [ 2 , 1 , - 2 , 0 ] ] NEW_LINE print ( maxRowDiff ( mat , m , n ) ) NEW_LINE


class GFG { static int dayofweek ( int d , int m , int y ) { int t [ ] = { 0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 } ; y -= ( m < 3 ) ? 1 : 0 ; return ( y + y / 4 - y / 100 + y / 400 + t [ m - 1 ] + d ) % 7 ; } public static void main ( String arg [ ] ) { int day = dayofweek ( 30 , 8 , 2010 ) ; System . out . print ( day ) ; } }

def dayofweek ( d , m , y ) : NEW_LINE INDENT t = [ 0 , 3 , 2 , 5 , 0 , 3 , 5 , 1 , 4 , 6 , 2 , 4 ] NEW_LINE y -= m < 3 NEW_LINE return ( ( y + int ( y / 4 ) - int ( y / 100 ) + int ( y / 400 ) + t [ m - 1 ] + d ) % 7 ) NEW_LINE DEDENT day = dayofweek ( 30 , 8 , 2010 ) NEW_LINE print ( day ) NEW_LINE


import java . lang . * ; import java . util . * ; class GFG { public int countPaths ( int m , int n ) { if ( m == 1 || n == 1 ) return 1 ; return countPaths ( m - 1 , n ) + countPaths ( m , n - 1 ) ; } public static void main ( String args [ ] ) { GFG g = new GFG ( ) ; int n = 5 , m = 5 ; System . out . println ( g . countPaths ( n , m ) ) ; } }

def countPaths ( m , n ) : NEW_LINE INDENT if m == 1 or n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return ( countPaths ( m - 1 , n ) + countPaths ( m , n - 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE m = 5 NEW_LINE print ( countPaths ( n , m ) ) NEW_LINE DEDENT


class GFG { static long countWays ( int n , int k ) { long total = k ; int mod = 1000000007 ; int same = 0 , diff = k ; for ( int i = 2 ; i <= n ; i ++ ) { same = diff ; diff = ( int ) total * ( k - 1 ) ; diff = diff % mod ; total = ( same + diff ) % mod ; } return total ; } public static void main ( String [ ] args ) { int n = 3 , k = 2 ; System . out . println ( countWays ( n , k ) ) ; } }

def countWays ( n , k ) : NEW_LINE INDENT total = k NEW_LINE mod = 1000000007 NEW_LINE same , diff = 0 , k NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT same = diff NEW_LINE diff = total * ( k - 1 ) NEW_LINE diff = diff % mod NEW_LINE total = ( same + diff ) % mod NEW_LINE DEDENT return total NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 3 , 2 NEW_LINE print ( countWays ( n , k ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class divisible { public static void check ( int n , int x , int y ) { long fact = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { fact *= i ; } long divisor = ( long ) Math . pow ( x , y ) ; if ( fact % divisor == 0 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String args [ ] ) { int n = 10 ; int x = 2 ; int y = 8 ; check ( n , x , y ) ; } }

def check ( n , x , y ) : NEW_LINE INDENT fact = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fact *= i ; NEW_LINE DEDENT divisor = x ** y ; NEW_LINE if ( fact % divisor == 0 ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 ; NEW_LINE x = 2 ; NEW_LINE y = 8 ; NEW_LINE check ( n , x , y ) ; NEW_LINE DEDENT


class GFG { static boolean isInside ( int circle_x , int circle_y , int rad , int x , int y ) { if ( ( x - circle_x ) * ( x - circle_x ) + ( y - circle_y ) * ( y - circle_y ) <= rad * rad ) return true ; else return false ; } public static void main ( String arg [ ] ) { int x = 1 , y = 1 ; int circle_x = 0 , circle_y = 1 , rad = 2 ; if ( isInside ( circle_x , circle_y , rad , x , y ) ) System . out . print ( " Inside " ) ; else System . out . print ( " Outside " ) ; } }

def isInside ( circle_x , circle_y , rad , x , y ) : NEW_LINE INDENT if ( ( x - circle_x ) * ( x - circle_x ) + ( y - circle_y ) * ( y - circle_y ) <= rad * rad ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT x = 1 ; NEW_LINE y = 1 ; NEW_LINE circle_x = 0 ; NEW_LINE circle_y = 1 ; NEW_LINE rad = 2 ; NEW_LINE if ( isInside ( circle_x , circle_y , rad , x , y ) ) : NEW_LINE INDENT print ( " Inside " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Outside " ) ; NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static void bitonicGenerator ( int arr [ ] , int n ) { int i = 1 ; int j = n - 1 ; if ( j % 2 != 0 ) j -- ; while ( i < j ) { arr = swap ( arr , i , j ) ; i += 2 ; j -= 2 ; } Arrays . sort ( arr , 0 , ( n + 1 ) / 2 ) ; Arrays . sort ( arr , ( n + 1 ) / 2 , n ) ; int low = ( n + 1 ) / 2 , high = n - 1 ; while ( low < high ) { Integer temp = arr [ low ] ; arr [ low ] = arr [ high ] ; arr [ high ] = temp ; low ++ ; high -- ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int n = arr . length ; bitonicGenerator ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }

def bitonicGenerator ( arr , n ) : NEW_LINE INDENT i = 1 NEW_LINE j = n - 1 NEW_LINE if ( j % 2 != 0 ) : NEW_LINE INDENT j = j - 1 NEW_LINE DEDENT while ( i < j ) : NEW_LINE INDENT arr [ j ] , arr [ i ] = arr [ i ] , arr [ j ] NEW_LINE i = i + 2 NEW_LINE j = j - 2 NEW_LINE DEDENT arr_f = [ ] NEW_LINE arr_s = [ ] NEW_LINE for i in range ( int ( ( n + 1 ) / 2 ) ) : NEW_LINE INDENT arr_f . append ( arr [ i ] ) NEW_LINE DEDENT i = int ( ( n + 1 ) / 2 ) NEW_LINE while ( i < n ) : NEW_LINE INDENT arr_s . append ( arr [ i ] ) NEW_LINE i = i + 1 NEW_LINE DEDENT arr_f . sort ( ) NEW_LINE arr_s . sort ( reverse = True ) NEW_LINE for i in arr_s : NEW_LINE INDENT arr_f . append ( i ) NEW_LINE DEDENT return arr_f NEW_LINE DEDENT arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE arr = bitonicGenerator ( arr , n ) NEW_LINE print ( arr ) NEW_LINE


import java . lang . Math ; class GFG { private static final int MAX = 2147483647 ; static int [ ] [ ] one = new int [ 100001 ] [ 32 ] ; static void make_prefix ( int A [ ] , int n ) { for ( int j = 0 ; j < 32 ; j ++ ) one [ 0 ] [ j ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int a = A [ i - 1 ] ; for ( int j = 0 ; j < 32 ; j ++ ) { int x = ( int ) Math . pow ( 2 , j ) ; if ( ( a & x ) != 0 ) one [ i ] [ j ] = 1 + one [ i - 1 ] [ j ] ; else one [ i ] [ j ] = one [ i - 1 ] [ j ] ; } } } static int Solve ( int L , int R ) { int l = L , r = R ; int tot_bits = r - l + 1 ; int X = MAX ; for ( int i = 0 ; i < 31 ; i ++ ) { int x = one [ r ] [ i ] - one [ l - 1 ] [ i ] ; if ( x >= tot_bits - x ) { int ith_bit = ( int ) Math . pow ( 2 , i ) ; X = X ^ ith_bit ; } } return X ; } public static void main ( String [ ] args ) { int n = 5 , q = 3 ; int A [ ] = { 210 , 11 , 48 , 22 , 133 } ; int L [ ] = { 1 , 4 , 2 } , R [ ] = { 3 , 14 , 4 } ; make_prefix ( A , n ) ; for ( int j = 0 ; j < q ; j ++ ) System . out . println ( Solve ( L [ j ] , R [ j ] ) ) ; } }

import math NEW_LINE one = [ [ 0 for x in range ( 32 ) ] for y in range ( 100001 ) ] NEW_LINE MAX = 2147483647 NEW_LINE def make_prefix ( A , n ) : NEW_LINE INDENT global one , MAX NEW_LINE for j in range ( 0 , 32 ) : NEW_LINE INDENT one [ 0 ] [ j ] = 0 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT a = A [ i - 1 ] NEW_LINE for j in range ( 0 , 32 ) : NEW_LINE INDENT x = int ( math . pow ( 2 , j ) ) NEW_LINE if ( a & x ) : NEW_LINE INDENT one [ i ] [ j ] = 1 + one [ i - 1 ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT one [ i ] [ j ] = one [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT DEDENT def Solve ( L , R ) : NEW_LINE INDENT global one , MAX NEW_LINE l = L NEW_LINE r = R NEW_LINE tot_bits = r - l + 1 NEW_LINE X = MAX NEW_LINE for i in range ( 0 , 31 ) : NEW_LINE INDENT x = one [ r ] [ i ] - one [ l - 1 ] [ i ] NEW_LINE if ( x >= ( tot_bits - x ) ) : NEW_LINE INDENT ith_bit = pow ( 2 , i ) NEW_LINE X = X ^ ith_bit NEW_LINE DEDENT DEDENT return X NEW_LINE DEDENT n = 5 NEW_LINE q = 3 NEW_LINE A = [ 210 , 11 , 48 , 22 , 133 ] NEW_LINE L = [ 1 , 4 , 2 ] NEW_LINE R = [ 3 , 14 , 4 ] NEW_LINE make_prefix ( A , n ) NEW_LINE for j in range ( 0 , q ) : NEW_LINE INDENT print ( Solve ( L [ j ] , R [ j ] ) , end = " \n " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int findTransitionPoint ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == 1 ) return i ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 1 , 1 } ; int n = arr . length ; int point = findTransitionPoint ( arr , n ) ; if ( point >= 0 ) System . out . print ( " Transition ▁ point ▁ is ▁ " + point ) ; else System . out . print ( " There ▁ is ▁ no ▁ transition ▁ point " ) ; } }

def findTransitionPoint ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 0 , 0 , 0 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE point = findTransitionPoint ( arr , n ) NEW_LINE if point >= 0 : NEW_LINE INDENT print ( " Transition ▁ point ▁ is " , point ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " There ▁ is ▁ no ▁ transition ▁ point " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static long maxProd ( int arr [ ] , int n , int k ) { long product = 1 ; HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . containsKey ( arr [ i ] ) == false ) { product = product * arr [ i ] ; s . put ( arr [ i ] , 1 ) ; } else s . put ( arr [ i ] , s . get ( arr [ i ] ) + 1 ) ; } for ( int j = n - 1 ; j >= 0 && k > 0 ; j -- ) { if ( ( k > ( s . get ( arr [ j ] ) - 1 ) ) && ( ( s . get ( arr [ j ] ) - 1 ) > 0 ) ) { product *= Math . pow ( arr [ j ] , s . get ( arr [ j ] ) - 1 ) ; k = k - s . get ( arr [ j ] ) + 1 ; s . put ( arr [ j ] , 0 ) ; } if ( k <= ( s . get ( arr [ j ] ) - 1 ) && ( ( s . get ( arr [ j ] ) - 1 ) > 0 ) ) { product *= Math . pow ( arr [ j ] , k ) ; break ; } } return product ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 7 , 8 , 2 , 5 , 6 , 8 } ; int n = arr . length ; int k = 2 ; System . out . println ( maxProd ( arr , n , k ) ) ; } }

def maxProd ( arr , n , k ) : NEW_LINE INDENT product = 1 ; NEW_LINE s = dict . fromkeys ( arr , 0 ) ; NEW_LINE arr . sort ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ arr [ i ] ] == 0 ) : NEW_LINE INDENT product = product * arr [ i ] ; NEW_LINE DEDENT s [ arr [ i ] ] = s [ arr [ i ] ] + 1 ; NEW_LINE DEDENT j = n - 1 ; NEW_LINE while ( j >= 0 and k > 0 ) : NEW_LINE INDENT if ( ( k > ( s [ arr [ j ] ] - 1 ) ) and ( ( s [ arr [ j ] ] - 1 ) > 0 ) ) : NEW_LINE INDENT product *= pow ( arr [ j ] , s [ arr [ j ] ] - 1 ) ; NEW_LINE k = k - s [ arr [ j ] ] + 1 ; NEW_LINE s [ arr [ j ] ] = 0 ; NEW_LINE DEDENT if ( k <= ( s [ arr [ j ] ] - 1 ) and ( ( s [ arr [ j ] ] - 1 ) > 0 ) ) : NEW_LINE INDENT product *= pow ( arr [ j ] , k ) ; NEW_LINE break ; NEW_LINE DEDENT j -= 1 NEW_LINE DEDENT return product ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 7 , 8 , 2 , 5 , 6 , 8 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( maxProd ( arr , n , k ) ) ; NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static boolean isSubSeqDivisible ( String str ) { int n = str . length ( ) ; int dp [ ] [ ] = new int [ n + 1 ] [ 10 ] ; int arr [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) arr [ i ] = ( int ) ( str . charAt ( i - 1 ) - '0' ) ; for ( int i = 1 ; i <= n ; i ++ ) { dp [ i ] [ arr [ i ] % 8 ] = 1 ; for ( int j = 0 ; j < 8 ; j ++ ) { if ( dp [ i - 1 ] [ j ] > dp [ i ] [ ( j * 10 + arr [ i ] ) % 8 ] ) dp [ i ] [ ( j * 10 + arr [ i ] ) % 8 ] = dp [ i - 1 ] [ j ] ; if ( dp [ i - 1 ] [ j ] > dp [ i ] [ j ] ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] ; } } for ( int i = 1 ; i <= n ; i ++ ) { if ( dp [ i ] [ 0 ] == 1 ) return true ; } return false ; } public static void main ( String args [ ] ) { String str = "3144" ; if ( isSubSeqDivisible ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isSubSeqDivisible ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE dp = [ [ 0 for i in range ( 10 ) ] for i in range ( n + 1 ) ] NEW_LINE arr = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT arr [ i ] = int ( str [ i - 1 ] ) ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT dp [ i ] [ arr [ i ] % 8 ] = 1 ; NEW_LINE for j in range ( 8 ) : NEW_LINE INDENT if ( dp [ i - 1 ] [ j ] > dp [ i ] [ ( j * 10 + arr [ i ] ) % 8 ] ) : NEW_LINE INDENT dp [ i ] [ ( j * 10 + arr [ i ] ) % 8 ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT if ( dp [ i - 1 ] [ j ] > dp [ i ] [ j ] ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( dp [ i ] [ 0 ] == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT str = "3144" NEW_LINE if ( isSubSeqDivisible ( str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static boolean ispalindrome ( String s ) { int l = s . length ( ) ; for ( int i = 0 , j = l - 1 ; i <= j ; i ++ , j -- ) { if ( s . charAt ( i ) != s . charAt ( j ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String s = " BABABAA " ; int cnt = 0 ; int flag = 0 ; while ( s . length ( ) > 0 ) { if ( ispalindrome ( s ) ) { flag = 1 ; break ; } else { cnt ++ ; s = s . substring ( 0 , s . length ( ) - 1 ) ; } } if ( flag == 1 ) { System . out . println ( cnt ) ; } } }

def ispalindrome ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE i = 0 NEW_LINE j = l - 1 NEW_LINE while i <= j : NEW_LINE INDENT if ( s [ i ] != s [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " BABABAA " NEW_LINE cnt = 0 NEW_LINE flag = 0 NEW_LINE while ( len ( s ) > 0 ) : NEW_LINE INDENT if ( ispalindrome ( s ) ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE s = s [ : - 1 ] NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT print ( cnt ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static boolean isPossible ( int n , int k , int arr [ ] ) { int sum = arr [ 0 ] ; int maxVal = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { sum += arr [ i ] ; maxVal = Math . max ( maxVal , arr [ i ] ) ; } if ( ( float ) maxVal > ( float ) ( sum + k ) / n ) return false ; return true ; } public static void main ( String [ ] args ) { int k = 8 ; int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; if ( isPossible ( n , k , arr ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isPossible ( n , k , arr ) : NEW_LINE INDENT sum = arr [ 0 ] NEW_LINE maxVal = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE maxVal = max ( maxVal , arr [ i ] ) NEW_LINE DEDENT if ( int ( maxVal ) > int ( ( sum + k ) / n ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 8 NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isPossible ( n , k , arr ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static int claculate sum ( int n ) { int sum = ( 1 << ( n + 1 ) ) ; return sum - 1 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( " Sum ▁ of ▁ the ▁ series ▁ of ▁ power ▁ 2 ▁ is ▁ : ▁ " + calculateSum ( n ) ) ; } } ;

def calculateSum ( n ) : NEW_LINE INDENT sum = ( 1 << ( n + 1 ) ) NEW_LINE return sum - 1 NEW_LINE DEDENT n = 10 NEW_LINE print ( " Sum ▁ of ▁ series ▁ " , calculateSum ( n ) ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static int sameSetBits ( int arr [ ] , int n ) { Vector < Integer > v = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int count = Integer . bitCount ( arr [ i ] ) ; v . add ( count ) ; } int current_count = 1 , max_count = 1 ; for ( int i = 1 ; i < v . size ( ) - 1 ; i ++ ) { if ( v . get ( i + 1 ) == v . get ( i ) ) current_count ++ ; else current_count = 1 ; max_count = Math . max ( max_count , current_count ) ; } return max_count ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 75 , 14 , 7 , 13 , 11 } ; int n = arr . length ; System . out . println ( sameSetBits ( arr , n ) ) ; } }

def sameSetBits ( arr , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT v . append ( bin ( arr [ i ] ) . count ( '1' ) ) NEW_LINE DEDENT current_count = 1 NEW_LINE max_count = 1 NEW_LINE for i in range ( 1 , len ( v ) - 1 , 1 ) : NEW_LINE INDENT if ( v [ i + 1 ] == v [ i ] ) : NEW_LINE INDENT current_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_count = 1 NEW_LINE DEDENT max_count = max ( max_count , current_count ) NEW_LINE DEDENT return max_count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 75 , 14 , 7 , 13 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sameSetBits ( arr , n ) ) NEW_LINE DEDENT


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

def countStr ( n , bCount , cCount ) : NEW_LINE INDENT if ( bCount < 0 or cCount < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( bCount == 0 and cCount == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT res = countStr ( n - 1 , bCount , cCount ) NEW_LINE res += countStr ( n - 1 , bCount - 1 , cCount ) NEW_LINE res += countStr ( n - 1 , bCount , cCount - 1 ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( countStr ( n , 1 , 2 ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int count_element ( int N , int K , int [ ] arr ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; ++ i ) if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } int answer = 0 ; for ( Map . Entry < Integer , Integer > i : mp . entrySet ( ) ) { if ( mp . containsKey ( i . getKey ( ) + K ) ) answer += i . getValue ( ) ; } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . length ; int K = 2 ; System . out . print ( count_element ( N , K , arr ) ) ; } }

def count_element ( N , K , arr ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT mp [ arr [ i ] ] = mp . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT answer = 0 NEW_LINE for i in mp : NEW_LINE INDENT if i + K in mp : NEW_LINE INDENT answer += mp [ i ] NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE K = 2 NEW_LINE print ( count_element ( N , K , arr ) ) NEW_LINE DEDENT


import java . io . * ; class Main { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( String str , int base ) { int len = str . length ( ) ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str . charAt ( i ) ) >= base ) { System . out . println ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str . charAt ( i ) ) * power ; power = power * base ; } return num ; } public static boolean isEven ( String num , int N ) { int deci = toDeci ( num , N ) ; return ( deci % 2 == 0 ) ; } public static void main ( String [ ] args ) { String num = "11A " ; int N = 16 ; if ( isEven ( num , N ) ) { System . out . println ( " Even " ) ; } else { System . out . println ( " Odd " ) ; } } }

def val ( c ) : NEW_LINE INDENT if ( ord ( c ) >= ord ( '0' ) and ord ( c ) <= ord ( '9' ) ) : NEW_LINE INDENT return ord ( c ) - ord ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT return ord ( c ) - ord ( ' A ' ) + 10 NEW_LINE DEDENT DEDENT def toDeci ( str , base ) : NEW_LINE INDENT Len = len ( str ) NEW_LINE power = 1 NEW_LINE num = 0 NEW_LINE for i in range ( Len - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( val ( str [ i ] ) >= base ) : NEW_LINE INDENT print ( " Invalid ▁ Number " ) NEW_LINE return - 1 NEW_LINE DEDENT num += val ( str [ i ] ) * power NEW_LINE power = power * base NEW_LINE DEDENT return num NEW_LINE DEDENT def isEven ( num , N ) : NEW_LINE INDENT deci = toDeci ( num , N ) NEW_LINE return ( deci % 2 == 0 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = "11A " NEW_LINE N = 16 NEW_LINE if ( isEven ( num , N ) ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class Solution { static class INT { int data ; INT ( int d ) { data = d ; } } static void findPostOrderUtil ( int pre [ ] , int n , int minval , int maxval , INT preIndex ) { if ( preIndex . data == n ) return ; if ( pre [ preIndex . data ] < minval || pre [ preIndex . data ] > maxval ) { return ; } int val = pre [ preIndex . data ] ; preIndex . data ++ ; findPostOrderUtil ( pre , n , minval , val , preIndex ) ; findPostOrderUtil ( pre , n , val , maxval , preIndex ) ; System . out . print ( val + " ▁ " ) ; } static void findPostOrder ( int pre [ ] , int n ) { INT preIndex = new INT ( 0 ) ; findPostOrderUtil ( pre , n , Integer . MIN_VALUE , Integer . MAX_VALUE , preIndex ) ; } public static void main ( String args [ ] ) { int pre [ ] = { 40 , 30 , 35 , 80 , 100 } ; int n = pre . length ; findPostOrder ( pre , n ) ; } }

INT_MIN = - 2 ** 31 NEW_LINE INT_MAX = 2 ** 31 NEW_LINE def findPostOrderUtil ( pre , n , minval , maxval , preIndex ) : NEW_LINE INDENT if ( preIndex [ 0 ] == n ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( pre [ preIndex [ 0 ] ] < minval or pre [ preIndex [ 0 ] ] > maxval ) : NEW_LINE INDENT return NEW_LINE DEDENT val = pre [ preIndex [ 0 ] ] NEW_LINE preIndex [ 0 ] += 1 NEW_LINE findPostOrderUtil ( pre , n , minval , val , preIndex ) NEW_LINE findPostOrderUtil ( pre , n , val , maxval , preIndex ) NEW_LINE print ( val , end = " ▁ " ) NEW_LINE DEDENT def findPostOrder ( pre , n ) : NEW_LINE INDENT preIndex = [ 0 ] NEW_LINE findPostOrderUtil ( pre , n , INT_MIN , INT_MAX , preIndex ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT pre = [ 40 , 30 , 35 , 80 , 100 ] NEW_LINE n = len ( pre ) NEW_LINE findPostOrder ( pre , n ) NEW_LINE DEDENT


public class GFG { static void printKMax ( int arr [ ] , int n , int k ) { int j , max ; for ( int i = 0 ; i <= n - k ; i ++ ) { max = arr [ i ] ; for ( j = 1 ; j < k ; j ++ ) { if ( arr [ i + j ] > max ) max = arr [ i + j ] ; } System . out . print ( max + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int k = 3 ; printKMax ( arr , arr . length , k ) ; } }

def printMax ( arr , n , k ) : NEW_LINE INDENT max = 0 NEW_LINE for i in range ( n - k + 1 ) : NEW_LINE INDENT max = arr [ i ] NEW_LINE for j in range ( 1 , k ) : NEW_LINE INDENT if arr [ i + j ] > max : NEW_LINE INDENT max = arr [ i + j ] NEW_LINE DEDENT DEDENT print ( str ( max ) + " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE printMax ( arr , n , k ) NEW_LINE DEDENT


class GFG { static int maxOperations ( String str ) { int i , g , gk , gks ; i = g = gk = gks = 0 ; for ( i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == ' g ' ) { g ++ ; } else if ( str . charAt ( i ) == ' k ' ) { if ( g > 0 ) { g -- ; gk ++ ; } } else if ( str . charAt ( i ) == ' s ' ) { if ( gk > 0 ) { gk -- ; gks ++ ; } } } return gks ; } public static void main ( String args [ ] ) { String a = " ggkssk " ; System . out . print ( maxOperations ( a ) ) ; } }

def maxOperations ( str ) : NEW_LINE INDENT i , g , gk , gks = 0 , 0 , 0 , 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] == ' g ' ) : NEW_LINE INDENT g += 1 NEW_LINE DEDENT elif ( str [ i ] == ' k ' ) : NEW_LINE INDENT if ( g > 0 ) : NEW_LINE INDENT g -= 1 NEW_LINE gk += 1 NEW_LINE DEDENT DEDENT elif ( str [ i ] == ' s ' ) : NEW_LINE INDENT if ( gk > 0 ) : NEW_LINE INDENT gk -= 1 NEW_LINE gks += 1 NEW_LINE DEDENT DEDENT DEDENT return gks NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = " ggkssk " NEW_LINE print ( maxOperations ( a ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static final int N = 100005 ; static Set < Integer > AMICABLE ( ) { int sum [ ] = new int [ N ] ; for ( int i = 0 ; i < N ; i ++ ) sum [ i ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) { sum [ i ] ++ ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % j == 0 ) { sum [ i ] += j ; if ( i / j != j ) sum [ i ] += i / j ; } } } Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 2 ; i < N ; i ++ ) { if ( i != sum [ i ] && sum [ i ] < N && i == sum [ sum [ i ] ] ) { s . add ( i ) ; s . add ( sum [ i ] ) ; } } return s ; } static int SumOfAmicable ( int n ) { int sum = 0 ; Set < Integer > s = AMICABLE ( ) ; for ( Integer x : s ) { if ( x <= n ) sum += x ; } return sum ; } public static void main ( String args [ ] ) { int n = 284 ; System . out . println ( SumOfAmicable ( n ) ) ; } }

import math as mt NEW_LINE N = 100005 NEW_LINE def AMICABLE ( ) : NEW_LINE INDENT Sum = [ 0 for i in range ( N ) ] NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT Sum [ i ] += 1 NEW_LINE for j in range ( 2 , mt . ceil ( mt . sqrt ( i ) ) ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT Sum [ i ] += j NEW_LINE if ( i // j != j ) : NEW_LINE INDENT Sum [ i ] += i // j NEW_LINE DEDENT DEDENT DEDENT DEDENT s = set ( ) NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( i != Sum [ i ] and Sum [ i ] < N and i == Sum [ Sum [ i ] ] and i not in s and Sum [ i ] not in s ) : NEW_LINE INDENT s . add ( i ) NEW_LINE s . add ( Sum [ i ] ) NEW_LINE DEDENT DEDENT return s NEW_LINE DEDENT def SumOfAmicable ( n ) : NEW_LINE INDENT Sum = 0 NEW_LINE s = AMICABLE ( ) NEW_LINE s = sorted ( s ) NEW_LINE for i in s : NEW_LINE INDENT if ( i <= n ) : NEW_LINE INDENT Sum += i NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return Sum NEW_LINE DEDENT n = 284 NEW_LINE print ( SumOfAmicable ( n ) ) NEW_LINE


import java . util . ArrayList ; class GFG { static ArrayList values_of_r_and_a ( double m , double n , double mth , double nth ) { if ( m < n ) { double t = m ; n = m ; m = t ; t = mth ; mth = nth ; nth = t ; } double r = Math . pow ( mth / nth , 1.0 / ( m - n ) ) ; double a = mth / Math . pow ( r , ( m - 1 ) ) ; ArrayList arr = new ArrayList ( ) ; arr . add ( a ) ; arr . add ( r ) ; return arr ; } static double FindSum ( double m , double n , double mth , double nth , double p ) { ArrayList ar = values_of_r_and_a ( m , n , mth , nth ) ; double a = ( double ) ar . get ( 0 ) ; double r = ( double ) ar . get ( 1 ) ; double pth = a * Math . pow ( r , ( p - 1.0 ) ) ; return pth ; } public static void main ( String [ ] args ) { double m = 10 ; double n = 5 ; double p = 15 ; double mth = 2560 ; double nth = 80 ; System . out . println ( ( int ) FindSum ( m , n , mth , nth , p ) ) ; } }

def values_of_r_and_a ( m , n , mth , nth ) : NEW_LINE INDENT a , r = 0.0 , 0.0 NEW_LINE if ( m < n ) : NEW_LINE INDENT m , n = n , m NEW_LINE mth , nth = mth , nth NEW_LINE DEDENT r = pow ( mth // nth , 1.0 / ( m - n ) ) NEW_LINE a = mth // pow ( r , ( m - 1 ) ) NEW_LINE return a , r NEW_LINE DEDENT def FindSum ( m , n , mth , nth , p ) : NEW_LINE INDENT a , r = values_of_r_and_a ( m , n , mth , nth ) NEW_LINE pth = a * pow ( r , ( p - 1.0 ) ) NEW_LINE return pth NEW_LINE DEDENT m , n , p = 10 , 5 , 15 NEW_LINE mth , nth = 2560.0 , 80.0 NEW_LINE print ( FindSum ( m , n , mth , nth , p ) ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GfG { public static int counLastDigitK ( int low , int high , int k ) { int count = 0 ; for ( int i = low ; i <= high ; i ++ ) if ( i % 10 == k ) count ++ ; return count ; } public static void main ( String args [ ] ) { int low = 3 , high = 35 , k = 3 ; System . out . println ( counLastDigitK ( low , high , k ) ) ; } }

def counLastDigitK ( low , high , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( low , high + 1 ) : NEW_LINE INDENT if ( i % 10 == k ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT low = 3 NEW_LINE high = 35 NEW_LINE k = 3 NEW_LINE print ( counLastDigitK ( low , high , k ) ) NEW_LINE


import java . io . * ; class GFG { static void modInverse ( int a , int m ) { int g = gcd ( a , m ) ; if ( g != 1 ) System . out . println ( " Inverse ▁ doesn ' t ▁ exist " ) ; else { System . out . println ( " Modular ▁ multiplicative ▁ inverse ▁ is ▁ " + power ( a , m - 2 , m ) ) ; } } static int power ( int x , int y , int m ) { if ( y == 0 ) return 1 ; int p = power ( x , y / 2 , m ) % m ; p = ( p * p ) % m ; if ( y % 2 == 0 ) return p ; else return ( x * p ) % m ; } static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } public static void main ( String args [ ] ) { int a = 3 , m = 11 ; modInverse ( a , m ) ; } }

def modInverse ( a , m ) : NEW_LINE INDENT g = gcd ( a , m ) NEW_LINE if ( g != 1 ) : NEW_LINE INDENT print ( " Inverse ▁ doesn ' t ▁ exist " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Modular ▁ multiplicative ▁ inverse ▁ is ▁ " , power ( a , m - 2 , m ) ) NEW_LINE DEDENT DEDENT def power ( x , y , m ) : NEW_LINE INDENT if ( y == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT p = power ( x , y // 2 , m ) % m NEW_LINE p = ( p * p ) % m NEW_LINE if ( y % 2 == 0 ) : NEW_LINE INDENT return p NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( x * p ) % m ) NEW_LINE DEDENT DEDENT def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT a = 3 ; m = 11 NEW_LINE modInverse ( a , m ) NEW_LINE


class GFG { static boolean isVowel ( char x ) { x = Character . toLowerCase ( x ) ; return ( x == ' a ' || x == ' e ' || x == ' i ' || x == ' o ' || x == ' u ' ) ; } static String longestVowelSubsequence ( String str ) { String answer = " " ; int n = str . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isVowel ( str . charAt ( i ) ) ) { answer += str . charAt ( i ) ; } } return answer ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . print ( longestVowelSubsequence ( str ) + " \n " ) ; } }

def isVowel ( x ) : NEW_LINE INDENT return ( x == ' a ' or x == ' e ' or x == ' i ' or x == ' o ' or x == ' u ' ) NEW_LINE DEDENT def longestVowelSubsequence ( str ) : NEW_LINE INDENT answer = " " NEW_LINE n = len ( str ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isVowel ( str [ i ] ) ) : NEW_LINE INDENT answer += str [ i ] NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE print ( longestVowelSubsequence ( str ) ) NEW_LINE


import java . io . * ; class GFG { static int R = 4 ; static int C = 4 ; static int countPaths ( int maze [ ] [ ] ) { if ( maze [ 0 ] [ 0 ] == - 1 ) return 0 ; for ( int i = 0 ; i < R ; i ++ ) { if ( maze [ i ] [ 0 ] == 0 ) maze [ i ] [ 0 ] = 1 ; else break ; } for ( int i = 1 ; i < C ; i ++ ) { if ( maze [ 0 ] [ i ] == 0 ) maze [ 0 ] [ i ] = 1 ; else break ; } for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 1 ; j < C ; j ++ ) { if ( maze [ i ] [ j ] == - 1 ) continue ; if ( maze [ i - 1 ] [ j ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) ; if ( maze [ i ] [ j - 1 ] > 0 ) maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) ; } } return ( maze [ R - 1 ] [ C - 1 ] > 0 ) ? maze [ R - 1 ] [ C - 1 ] : 0 ; } public static void main ( String [ ] args ) { int maze [ ] [ ] = { { 0 , 0 , 0 , 0 } , { 0 , - 1 , 0 , 0 } , { - 1 , 0 , 0 , 0 } , { 0 , 0 , 0 , 0 } } ; System . out . println ( countPaths ( maze ) ) ; } }

R = 4 NEW_LINE C = 4 NEW_LINE def countPaths ( maze ) : NEW_LINE INDENT if ( maze [ 0 ] [ 0 ] == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( R ) : NEW_LINE INDENT if ( maze [ i ] [ 0 ] == 0 ) : NEW_LINE INDENT maze [ i ] [ 0 ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( 1 , C , 1 ) : NEW_LINE INDENT if ( maze [ 0 ] [ i ] == 0 ) : NEW_LINE INDENT maze [ 0 ] [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( 1 , R , 1 ) : NEW_LINE INDENT for j in range ( 1 , C , 1 ) : NEW_LINE INDENT if ( maze [ i ] [ j ] == - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( maze [ i - 1 ] [ j ] > 0 ) : NEW_LINE INDENT maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i - 1 ] [ j ] ) NEW_LINE DEDENT if ( maze [ i ] [ j - 1 ] > 0 ) : NEW_LINE INDENT maze [ i ] [ j ] = ( maze [ i ] [ j ] + maze [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT if ( maze [ R - 1 ] [ C - 1 ] > 0 ) : NEW_LINE INDENT return maze [ R - 1 ] [ C - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT maze = [ [ 0 , 0 , 0 , 0 ] , [ 0 , - 1 , 0 , 0 ] , [ - 1 , 0 , 0 , 0 ] , [ 0 , 0 , 0 , 0 ] ] NEW_LINE print ( countPaths ( maze ) ) NEW_LINE DEDENT


class MajorityElement { void printMajority ( int a [ ] , int size ) { int cand = findCandidate ( a , size ) ; if ( isMajority ( a , size , cand ) ) System . out . println ( " ▁ " + cand + " ▁ " ) ; else System . out . println ( " No ▁ Majority ▁ Element " ) ; } int findCandidate ( int a [ ] , int size ) { int maj_index = 0 , count = 1 ; int i ; for ( i = 1 ; i < size ; i ++ ) { if ( a [ maj_index ] == a [ i ] ) count ++ ; else count -- ; if ( count == 0 ) { maj_index = i ; count = 1 ; } } return a [ maj_index ] ; } boolean isMajority ( int a [ ] , int size , int cand ) { int i , count = 0 ; for ( i = 0 ; i < size ; i ++ ) { if ( a [ i ] == cand ) count ++ ; } if ( count > size / 2 ) return true ; else return false ; } public static void main ( String [ ] args ) { MajorityElement majorelement = new MajorityElement ( ) ; int a [ ] = new int [ ] { 1 , 3 , 3 , 1 , 2 } ; int size = a . length ; majorelement . printMajority ( a , size ) ; } }

def findCandidate ( A ) : NEW_LINE INDENT maj_index = 0 NEW_LINE count = 1 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if A [ maj_index ] == A [ i ] : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT if count == 0 : NEW_LINE INDENT maj_index = i NEW_LINE count = 1 NEW_LINE DEDENT DEDENT return A [ maj_index ] NEW_LINE DEDENT def isMajority ( A , cand ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if A [ i ] == cand : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if count > len ( A ) / 2 : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printMajority ( A ) : NEW_LINE INDENT cand = findCandidate ( A ) NEW_LINE if isMajority ( A , cand ) == True : NEW_LINE INDENT print ( cand ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Majority ▁ Element " ) NEW_LINE DEDENT DEDENT A = [ 1 , 3 , 3 , 1 , 2 ] NEW_LINE printMajority ( A ) NEW_LINE


import java . util . * ; class solution { static int calculateSum ( int n ) { return n * ( int ) ( Math . pow ( n , 2 ) + 3 * n + 5 ) / 3 ; } public static void main ( String arr [ ] ) { int n = 3 ; System . out . println ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }

from math import pow NEW_LINE def calculateSum ( n ) : NEW_LINE INDENT return n * ( pow ( n , 2 ) + 3 * n + 5 ) / 3 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( " Sum ▁ = " , int ( calculateSum ( n ) ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printTrib ( int n ) { if ( n < 1 ) return ; int first = 0 , second = 0 ; int third = 1 ; System . out . print ( first + " ▁ " ) ; if ( n > 1 ) System . out . print ( second + " ▁ " ) ; if ( n > 2 ) System . out . print ( second + " ▁ " ) ; for ( int i = 3 ; i < n ; i ++ ) { int curr = first + second + third ; first = second ; second = third ; third = curr ; System . out . print ( curr + " ▁ " ) ; } } public static void main ( String args [ ] ) { int n = 10 ; printTrib ( n ) ; } }

def printTrib ( n ) : NEW_LINE INDENT if ( n < 1 ) : NEW_LINE INDENT return NEW_LINE DEDENT first = 0 NEW_LINE second = 0 NEW_LINE third = 1 NEW_LINE print ( first , " ▁ " , end = " " ) NEW_LINE if ( n > 1 ) : NEW_LINE INDENT print ( second , " ▁ " , end = " " ) NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT print ( second , " ▁ " , end = " " ) NEW_LINE DEDENT for i in range ( 3 , n ) : NEW_LINE INDENT curr = first + second + third NEW_LINE first = second NEW_LINE second = third NEW_LINE third = curr NEW_LINE print ( curr , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE printTrib ( n ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class KPaths { static final int V = 4 ; int countwalks ( int graph [ ] [ ] , int u , int v , int k ) { if ( k == 0 && u == v ) return 1 ; if ( k == 1 && graph [ u ] [ v ] == 1 ) return 1 ; if ( k <= 0 ) return 0 ; int count = 0 ; for ( int i = 0 ; i < V ; i ++ ) if ( graph [ u ] [ i ] == 1 ) count += countwalks ( graph , i , v , k - 1 ) ; return count ; } public static void main ( String [ ] args ) throws java . lang . Exception { int graph [ ] [ ] = new int [ ] [ ] { { 0 , 1 , 1 , 1 } , { 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 1 } , { 0 , 0 , 0 , 0 } } ; int u = 0 , v = 3 , k = 2 ; KPaths p = new KPaths ( ) ; System . out . println ( p . countwalks ( graph , u , v , k ) ) ; } }

V = 4 NEW_LINE def countwalks ( graph , u , v , k ) : NEW_LINE INDENT if ( k == 0 and u == v ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( k == 1 and graph [ u ] [ v ] ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( k <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 0 , V ) : NEW_LINE INDENT if ( graph [ u ] [ i ] == 1 ) : NEW_LINE INDENT count += countwalks ( graph , i , v , k - 1 ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT graph = [ [ 0 , 1 , 1 , 1 , ] , [ 0 , 0 , 0 , 1 , ] , [ 0 , 0 , 0 , 1 , ] , [ 0 , 0 , 0 , 0 ] ] NEW_LINE u = 0 ; v = 3 ; k = 2 NEW_LINE print ( countwalks ( graph , u , v , k ) ) NEW_LINE


import java . util . * ; class GFG { static void maxProduct ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . println ( " No ▁ pairs ▁ exists " ) ; return ; } if ( n == 2 ) { System . out . println ( arr [ 0 ] + " ▁ " + arr [ 1 ] ) ; return ; } int posa = Integer . MIN_VALUE , posb = Integer . MIN_VALUE ; int nega = Integer . MIN_VALUE , negb = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > posa ) { posb = posa ; posa = arr [ i ] ; } else if ( arr [ i ] > posb ) posb = arr [ i ] ; if ( arr [ i ] < 0 && Math . abs ( arr [ i ] ) > Math . abs ( nega ) ) { negb = nega ; nega = arr [ i ] ; } else if ( arr [ i ] < 0 && Math . abs ( arr [ i ] ) > Math . abs ( negb ) ) negb = arr [ i ] ; } if ( nega * negb > posa * posb ) System . out . println ( " Max ▁ product ▁ pair ▁ is ▁ { " + nega + " , ▁ " + negb + " } " ) ; else System . out . println ( " Max ▁ product ▁ pair ▁ is ▁ { " + posa + " , ▁ " + posb + " } " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 3 , 6 , 7 , 0 } ; int n = arr . length ; maxProduct ( arr , n ) ; } }

def maxProduct ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " No ▁ pairs ▁ exists " ) NEW_LINE return NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT print ( arr [ 0 ] , " ▁ " , arr [ 1 ] ) NEW_LINE return NEW_LINE DEDENT posa = 0 NEW_LINE posb = 0 NEW_LINE nega = 0 NEW_LINE negb = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > posa ) : NEW_LINE INDENT posb = posa NEW_LINE posa = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > posb ) : NEW_LINE INDENT posb = arr [ i ] NEW_LINE DEDENT if ( arr [ i ] < 0 and abs ( arr [ i ] ) > abs ( nega ) ) : NEW_LINE INDENT negb = nega NEW_LINE nega = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < 0 and abs ( arr [ i ] ) > abs ( negb ) ) : NEW_LINE INDENT negb = arr [ i ] NEW_LINE DEDENT DEDENT if ( nega * negb > posa * posb ) : NEW_LINE INDENT print ( " Max ▁ product ▁ pair ▁ is ▁ { " , nega , " , ▁ " , negb , " } " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Max ▁ product ▁ pair ▁ is ▁ { " , posa , " , ▁ " , posb , " } " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 4 , 3 , 6 , 7 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE maxProduct ( arr , n ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float ellipse ( float l , float b ) { if ( l < 0 || b < 0 ) return - 1 ; float x = ( float ) ( 3.14 * l * b ) / 4 ; return x ; } public static void main ( String args [ ] ) { float a = 5 , b = 3 ; System . out . println ( ellipse ( a , b ) ) ; } }

def ellipse ( l , b ) : NEW_LINE INDENT if l < 0 or b < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = ( 3.14 * l * b ) / 4 NEW_LINE return x NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , b = 5 , 3 NEW_LINE print ( ellipse ( l , b ) ) NEW_LINE DEDENT


class GFG { static int getHCF ( int x , int y ) { int minimum = Math . min ( x , y ) ; if ( x % minimum == 0 && y % minimum == 0 ) return minimum ; for ( int i = minimum / 2 ; i >= 2 ; i -- ) { if ( x % i == 0 && y % i == 0 ) return i ; } return 1 ; } public static void main ( String [ ] args ) { int x = 16 , y = 32 ; System . out . println ( getHCF ( x , y ) ) ; } }

def getHCF ( x , y ) : NEW_LINE INDENT minimum = min ( x , y ) NEW_LINE if ( x % minimum == 0 and y % minimum == 0 ) : NEW_LINE INDENT return minimum NEW_LINE DEDENT for i in range ( minimum // 2 , 1 , - 1 ) : NEW_LINE INDENT if ( x % i == 0 and y % i == 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT x , y = 16 , 32 NEW_LINE print ( getHCF ( x , y ) ) NEW_LINE


import java . io . * ; class GFG { static int RODS = 3 ; static int N = 3 ; static int [ ] [ ] [ ] dp = new int [ N + 1 ] [ RODS + 1 ] [ RODS + 1 ] ; static void initialize ( ) { for ( int i = 0 ; i <= N ; i += 1 ) { for ( int j = 1 ; j <= RODS ; j ++ ) { for ( int k = 1 ; k <= RODS ; k += 1 ) { dp [ i ] [ j ] [ k ] = Integer . MAX_VALUE ; } } } } static int mincost ( int idx , int src , int dest , int costs [ ] [ ] ) { if ( idx > N ) return 0 ; if ( dp [ idx ] [ src ] [ dest ] != Integer . MAX_VALUE ) return dp [ idx ] [ src ] [ dest ] ; int rem = 6 - ( src + dest ) ; int ans = Integer . MAX_VALUE ; int case1 = costs [ src - 1 ] [ dest - 1 ] + mincost ( idx + 1 , src , rem , costs ) + mincost ( idx + 1 , rem , dest , costs ) ; int case2 = costs [ src - 1 ] [ rem - 1 ] + mincost ( idx + 1 , src , dest , costs ) + mincost ( idx + 1 , dest , src , costs ) + costs [ rem - 1 ] [ dest - 1 ] + mincost ( idx + 1 , src , dest , costs ) ; ans = Math . min ( case1 , case2 ) ; dp [ idx ] [ src ] [ dest ] = ans ; return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] costs = { { 0 , 1 , 2 } , { 2 , 0 , 1 } , { 3 , 2 , 0 } } ; initialize ( ) ; System . out . print ( mincost ( 1 , 1 , 3 , costs ) ) ; } }

import numpy as np NEW_LINE import sys NEW_LINE RODS = 3 NEW_LINE N = 3 NEW_LINE dp = np . zeros ( ( N + 1 , RODS + 1 , RODS + 1 ) ) ; NEW_LINE def initialize ( ) : NEW_LINE INDENT for i in range ( N + 1 ) : NEW_LINE INDENT for j in range ( 1 , RODS + 1 ) : NEW_LINE INDENT for k in range ( 1 , RODS + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] = sys . maxsize ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def mincost ( idx , src , dest , costs ) : NEW_LINE INDENT if ( idx > N ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ idx ] [ src ] [ dest ] != sys . maxsize ) : NEW_LINE INDENT return dp [ idx ] [ src ] [ dest ] ; NEW_LINE DEDENT rem = 6 - ( src + dest ) ; NEW_LINE ans = sys . maxsize ; NEW_LINE case1 = costs [ src - 1 ] [ dest - 1 ] + mincost ( idx + 1 , src , rem , costs ) + mincost ( idx + 1 , rem , dest , costs ) ; NEW_LINE case2 = ( costs [ src - 1 ] [ rem - 1 ] + mincost ( idx + 1 , src , dest , costs ) + mincost ( idx + 1 , dest , src , costs ) + costs [ rem - 1 ] [ dest - 1 ] + mincost ( idx + 1 , src , dest , costs ) ) ; NEW_LINE ans = min ( case1 , case2 ) ; NEW_LINE dp [ idx ] [ src ] [ dest ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT costs = [ [ 0 , 1 , 2 ] , [ 2 , 0 , 1 ] , [ 3 , 2 , 0 ] ] ; NEW_LINE initialize ( ) ; NEW_LINE print ( mincost ( 1 , 1 , 3 , costs ) ) ; NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class CountTriangles { static int findNumberOfTriangles ( int arr [ ] ) { int n = arr . length ; Arrays . sort ( arr ) ; int count = 0 ; for ( int i = 0 ; i < n - 2 ; ++ i ) { int k = i + 2 ; for ( int j = i + 1 ; j < n ; ++ j ) { while ( k < n && arr [ i ] + arr [ j ] > arr [ k ] ) ++ k ; if ( k > j ) count += k - j - 1 ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 21 , 22 , 100 , 101 , 200 , 300 } ; System . out . println ( " Total ▁ number ▁ of ▁ triangles ▁ is ▁ " + findNumberOfTriangles ( arr ) ) ; } }

def findnumberofTriangles ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE arr . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT k = i + 2 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT while ( k < n and arr [ i ] + arr [ j ] > arr [ k ] ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT if ( k > j ) : NEW_LINE INDENT count += k - j - 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 10 , 21 , 22 , 100 , 101 , 200 , 300 ] NEW_LINE print " Number ▁ of ▁ Triangles : " , findnumberofTriangles ( arr ) NEW_LINE


class GFG { static String addZeros ( String str , int n ) { for ( int i = 0 ; i < n ; i ++ ) { str = "0" + str ; } return str ; } static String getXOR ( String a , String b ) { int aLen = a . length ( ) ; int bLen = b . length ( ) ; if ( aLen > bLen ) { a = addZeros ( b , aLen - bLen ) ; } else if ( bLen > aLen ) { a = addZeros ( a , bLen - aLen ) ; } int len = Math . max ( aLen , bLen ) ; String res = " " ; for ( int i = 0 ; i < len ; i ++ ) { if ( a . charAt ( i ) == b . charAt ( i ) ) res += "0" ; else res += "1" ; } return res ; } public static void main ( String [ ] args ) { String a = "11001" , b = "111111" ; System . out . println ( getXOR ( a , b ) ) ; } }

def addZeros ( strr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT strr = "0" + strr NEW_LINE DEDENT return strr NEW_LINE DEDENT def getXOR ( a , b ) : NEW_LINE INDENT aLen = len ( a ) NEW_LINE bLen = len ( b ) NEW_LINE if ( aLen > bLen ) : NEW_LINE INDENT b = addZeros ( b , aLen - bLen ) NEW_LINE DEDENT elif ( bLen > aLen ) : NEW_LINE INDENT a = addZeros ( a , bLen - aLen ) NEW_LINE DEDENT lenn = max ( aLen , bLen ) ; NEW_LINE res = " " NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( a [ i ] == b [ i ] ) : NEW_LINE INDENT res += "0" NEW_LINE DEDENT else : NEW_LINE INDENT res += "1" NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT a = "11001" NEW_LINE b = "111111" NEW_LINE print ( getXOR ( a , b ) ) NEW_LINE


class GFG { public static long SubArraySum ( int arr [ ] , int n ) { long result = 0 , temp = 0 ; for ( int i = 0 ; i < n ; i ++ ) { temp = 0 ; for ( int j = i ; j < n ; j ++ ) { temp += arr [ j ] ; result += temp ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( " Sum ▁ of ▁ SubArray ▁ : ▁ " + SubArraySum ( arr , n ) ) ; } }

def SubArraySum ( arr , n ) : NEW_LINE INDENT temp , result = 0 , 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT temp = 0 ; NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT temp += arr [ j ] NEW_LINE result += temp NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Sum ▁ of ▁ SubArray ▁ : " , SubArraySum ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static int countSubarray ( int arr [ ] , int n , int k ) { int start = 0 , end = 0 ; int count = 0 , sum = arr [ 0 ] ; while ( start < n && end < n ) { if ( sum < k ) { end ++ ; if ( end >= start ) count += end - start ; if ( end < n ) sum += arr [ end ] ; } else { sum -= arr [ start ] ; start ++ ; } } return count ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 11 , 2 , 3 , 15 } ; int k = 10 ; int size = array . length ; int count = countSubarray ( array , size , k ) ; System . out . println ( count ) ; } }

def countSubarrays ( arr , n , k ) : NEW_LINE INDENT start = 0 NEW_LINE end = 0 NEW_LINE count = 0 NEW_LINE sum = arr [ 0 ] NEW_LINE while ( start < n and end < n ) : NEW_LINE INDENT if ( sum < k ) : NEW_LINE INDENT end += 1 NEW_LINE if ( end >= start ) : NEW_LINE INDENT count += end - start NEW_LINE DEDENT if ( end < n ) : NEW_LINE INDENT sum += arr [ end ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT sum -= arr [ start ] NEW_LINE start += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT array = [ 1 , 11 , 2 , 3 , 15 ] NEW_LINE k = 10 NEW_LINE size = len ( array ) NEW_LINE print ( countSubarrays ( array , size , k ) ) NEW_LINE DEDENT


import java . io . * ; class ring { public static boolean checkcircle ( int r , int R , int r1 , int x1 , int y1 ) { int dis = ( int ) Math . sqrt ( x1 * x1 + y1 * y1 ) ; return ( dis - r1 >= R && dis + r1 <= r ) ; } public static void main ( String args [ ] ) { int r = 8 , R = 4 , r1 = 2 , x1 = 6 , y1 = 0 ; if ( checkcircle ( r , R , r1 , x1 , y1 ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }

import math NEW_LINE def checkcircle ( r , R , r1 , x1 , y1 ) : NEW_LINE INDENT dis = int ( math . sqrt ( x1 * x1 + y1 * y1 ) ) NEW_LINE return ( dis - r1 >= R and dis + r1 <= r ) NEW_LINE DEDENT r = 8 ; R = 4 ; r1 = 2 ; x1 = 6 ; y1 = 0 NEW_LINE if ( checkcircle ( r , R , r1 , x1 , y1 ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int solve ( int n , int m , int obstacles , double range [ ] ) { double val = Math . min ( n , m ) ; Arrays . sort ( range ) ; int c = 1 ; for ( int i = obstacles - 1 ; i >= 0 ; i -- ) { range [ i ] = 2 * range [ i ] ; val -= range [ i ] ; if ( val <= 0 ) { return c ; } else { c ++ ; } } if ( val > 0 ) { return - 1 ; } return 0 ; } public static void main ( String [ ] args ) { int n = 4 , m = 5 , obstacles = 3 ; double range [ ] = { 1.0 , 1.25 , 1.15 } ; System . out . print ( solve ( n , m , obstacles , range ) + " \n " ) ; } }

def solve ( n , m , obstacles , rangee ) : NEW_LINE INDENT val = min ( n , m ) NEW_LINE rangee = sorted ( rangee ) NEW_LINE c = 1 NEW_LINE for i in range ( obstacles - 1 , - 1 , - 1 ) : NEW_LINE INDENT rangee [ i ] = 2 * rangee [ i ] NEW_LINE val -= rangee [ i ] NEW_LINE if ( val <= 0 ) : NEW_LINE INDENT return c NEW_LINE DEDENT else : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT if ( val > 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT n = 4 NEW_LINE m = 5 NEW_LINE obstacles = 3 NEW_LINE rangee = [ 1.0 , 1.25 , 1.15 ] NEW_LINE print ( solve ( n , m , obstacles , rangee ) ) NEW_LINE


import java . io . * ; class GFG { static int maxSubArraySumRepeated ( int a [ ] , int n , int k ) { int max_so_far = 0 ; int INT_MIN , max_ending_here = 0 ; for ( int i = 0 ; i < n * k ; i ++ ) { max_ending_here = max_ending_here + a [ i % n ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 20 , - 30 , - 1 } ; int n = a . length ; int k = 3 ; System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + maxSubArraySumRepeated ( a , n , k ) ) ; } }

def maxSubArraySumRepeated ( a , n , k ) : NEW_LINE INDENT max_so_far = - 2147483648 NEW_LINE max_ending_here = 0 NEW_LINE for i in range ( n * k ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i % n ] NEW_LINE if ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here NEW_LINE DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 NEW_LINE DEDENT DEDENT return max_so_far NEW_LINE DEDENT a = [ 10 , 20 , - 30 , - 1 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE print ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " , maxSubArraySumRepeated ( a , n , k ) ) NEW_LINE


class GFG { static void binaryRightAngleTriangle ( int n ) { int row , col ; for ( row = 0 ; row < n ; row ++ ) { for ( col = 0 ; col <= row ; col ++ ) { if ( ( ( row + col ) % 2 ) == 0 ) System . out . print ( "0" ) ; else System . out . print ( "1" ) ; System . out . print ( " \t " ) ; } System . out . print ( " \n " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; binaryRightAngleTriangle ( n ) ; } }

def binaryRightAngleTriangle ( n ) : NEW_LINE INDENT for row in range ( 0 , n ) : NEW_LINE INDENT for col in range ( 0 , row + 1 ) : NEW_LINE INDENT if ( ( ( row + col ) % 2 ) == 0 ) : NEW_LINE INDENT print ( "0" , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( "1" , end = " " ) NEW_LINE DEDENT print ( " \t " , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT n = 4 NEW_LINE binaryRightAngleTriangle ( n ) NEW_LINE


class GFG { static final int INT_SIZE = 32 ; static int getSingle ( int arr [ ] , int n ) { int result = 0 ; int x , sum ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { sum = 0 ; x = ( 1 << i ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & x ) == 0 ) sum ++ ; } if ( ( sum % 3 ) == 0 ) result |= x ; } return result ; } public static void main ( String args [ ] ) { int arr [ ] = { 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 } ; int n = arr . length ; System . out . println ( " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " + getSingle ( arr , n ) ) ; } }

INT_SIZE = 32 NEW_LINE def getSingle ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 0 , INT_SIZE ) : NEW_LINE INDENT sm = 0 NEW_LINE x = ( 1 << i ) NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ j ] & x ) : NEW_LINE INDENT sm = sm + 1 NEW_LINE DEDENT DEDENT if ( sm % 3 ) : NEW_LINE INDENT result = result | x NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 12 , 1 , 12 , 3 , 12 , 1 , 1 , 2 , 3 , 2 , 2 , 3 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " , getSingle ( arr , n ) ) NEW_LINE


def hut_pattern ( n ) : NEW_LINE INDENT if n % 2 == 0 : NEW_LINE INDENT n = n + 1 NEW_LINE DEDENT for i in range ( 0 , n - n // 3 + 1 , 1 ) : NEW_LINE INDENT for j in range ( 0 , n , 1 ) : NEW_LINE INDENT t = 2 * n / 5 NEW_LINE if t % 2 != 0 : NEW_LINE INDENT t = t - 1 NEW_LINE DEDENT if ( ( i == n / 5 ) or ( i == n - n / 3 ) or ( j == n - 1 and i >= n / 5 ) or ( j >= n / 5 and j < n - n / 5 and i == 0 ) or ( j == 0 and i >= n / 5 ) or ( j == t and i > n / 5 ) or ( i <= n / 5 and ( i + j == n / 5 or j - i == n / 5 ) ) or ( j - i == n - n / 5 ) ) : NEW_LINE INDENT print ( " * " , end = " ▁ " ) NEW_LINE DEDENT elif ( ( i == n // 5 + n // 7 ) and ( j >= n // 7 and j <= t - n // 7 ) ) : NEW_LINE INDENT print ( " _ " , end = " ▁ " ) NEW_LINE DEDENT elif ( ( i >= n // 5 + n // 7 ) and ( j == n // 7 or j == t - n // 7 ) ) : NEW_LINE INDENT print ( " | " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ " , end = " ▁ " ) NEW_LINE DEDENT DEDENT print ( " \n " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 15 NEW_LINE hut_pattern ( n ) NEW_LINE DEDENT


import java . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int largestGCD1Subset ( int A [ ] , int n ) { int currentGCD = A [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { currentGCD = gcd ( currentGCD , A [ i ] ) ; if ( currentGCD == 1 ) return n ; } return 0 ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 18 , 6 , 3 } ; int n = A . length ; System . out . println ( largestGCD1Subset ( A , n ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def largestGCD1Subset ( A , n ) : NEW_LINE INDENT currentGCD = A [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT currentGCD = gcd ( currentGCD , A [ i ] ) NEW_LINE if ( currentGCD == 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT A = [ 2 , 18 , 6 , 3 ] NEW_LINE n = len ( A ) NEW_LINE print ( largestGCD1Subset ( A , n ) ) NEW_LINE


class GFG { static boolean isPalindrome ( String str , int l , int r ) { while ( l < r ) { if ( str . charAt ( l ) != str . charAt ( r ) ) return false ; l ++ ; r -- ; } return true ; } static int CyclicShifts ( String str ) { int n = str . length ( ) ; int left = 0 ; int right = n - 1 ; str = str + str ; int cnt = 0 ; while ( right < 2 * n - 1 ) { if ( isPalindrome ( str , left , right ) ) break ; cnt ++ ; left ++ ; right ++ ; } return cnt ; } public static void main ( String [ ] args ) { String str = " bccbbaab " ; System . out . println ( CyclicShifts ( str ) ) ; } }

def isPalindrome ( str , l , r ) : NEW_LINE INDENT while ( l < r ) : NEW_LINE INDENT if ( str [ l ] != str [ r ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def CyclicShifts ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE left = 0 NEW_LINE right = n - 1 NEW_LINE str = str + str NEW_LINE cnt = 0 NEW_LINE while ( right < 2 * n - 1 ) : NEW_LINE INDENT if ( isPalindrome ( str , left , right ) ) : NEW_LINE INDENT break NEW_LINE DEDENT cnt += 1 NEW_LINE left += 1 NEW_LINE right += 1 NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " bccbbaab " ; NEW_LINE print ( CyclicShifts ( str ) ) NEW_LINE DEDENT


import java . lang . * ; import java . util . * ; class GFG { static int findNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } public static void main ( String args [ ] ) { int N = 45 , K = 6 ; System . out . print ( " Largest ▁ number ▁ smaller ▁ " + " than ▁ or ▁ equal ▁ to ▁ " + N + " \n that ▁ is ▁ divisible ▁ by ▁ " + K + " ▁ is ▁ " + findNum ( N , K ) ) ; } }

def findNum ( N , K ) : NEW_LINE INDENT rem = N % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N - rem NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 45 NEW_LINE K = 6 NEW_LINE print ( " Largest ▁ number ▁ smaller ▁ than ▁ or ▁ equal ▁ to " + str ( N ) + " that ▁ is ▁ divisible ▁ by " + str ( K ) + " is " , findNum ( N , K ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int fib ( int n ) { if ( n <= 1 ) return n ; return fib ( n - 1 ) + fib ( n - 2 ) ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( fib ( n ) ) ; } }

def fib ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT return fib ( n - 1 ) + fib ( n - 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE print ( fib ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void findSubSeq ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count [ ] = new int [ n ] ; int prev [ ] = new int [ n ] ; int i , j ; for ( i = 0 ; i < n ; i ++ ) count [ i ] = 1 ; for ( j = 0 ; j < n ; j ++ ) prev [ j ] = - 1 ; int max = 0 ; int maxprev = - 1 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] % arr [ j ] == 0 && count [ j ] + 1 > count [ i ] ) { count [ i ] = count [ j ] + 1 ; prev [ i ] = j ; } } if ( max < count [ i ] ) { max = count [ i ] ; maxprev = i ; } } i = maxprev ; while ( i >= 0 ) { if ( arr [ i ] != - 1 ) System . out . print ( arr [ i ] + " ▁ " ) ; i = prev [ i ] ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; findSubSeq ( arr , n ) ; } }

def findSubSeq ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE count = [ 1 ] * n ; NEW_LINE prev = [ - 1 ] * n ; NEW_LINE max = 0 ; NEW_LINE maxprev = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] % arr [ j ] == 0 and count [ j ] + 1 > count [ i ] ) : NEW_LINE INDENT count [ i ] = count [ j ] + 1 ; NEW_LINE prev [ i ] = j ; NEW_LINE DEDENT DEDENT if ( max < count [ i ] ) : NEW_LINE INDENT max = count [ i ] ; NEW_LINE maxprev = i ; NEW_LINE DEDENT DEDENT i = maxprev ; NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( arr [ i ] != - 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT i = prev [ i ] ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findSubSeq ( arr , n ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void print_asterisk ( int asterisk ) { if ( asterisk == 0 ) return ; System . out . print ( " * ▁ " ) ; print_asterisk ( asterisk - 1 ) ; } static void print_space ( int space ) { if ( space == 0 ) return ; System . out . print ( " ▁ " ) ; System . out . print ( " ▁ " ) ; print_space ( space - 1 ) ; } static void pattern_upper ( int n , int num ) { if ( n == 0 ) return ; print_asterisk ( n ) ; print_space ( 2 * ( num - n ) + 1 ) ; print_asterisk ( n ) ; System . out . print ( " \n " ) ; pattern_upper ( n - 1 , num ) ; } static void pattern_lower ( int n , int num ) { if ( n == 0 ) return ; print_asterisk ( num - n + 1 ) ; print_space ( 2 * n - 1 ) ; print_asterisk ( num - n + 1 ) ; System . out . print ( " \n " ) ; pattern_lower ( n - 1 , num ) ; } static void pattern ( int n , int num ) { pattern_upper ( n , num ) ; pattern_lower ( n - 1 , num ) ; } public static void main ( String [ ] args ) { int n = 5 ; pattern ( n , n ) ; } }

def print_asterisk ( asterisk ) : NEW_LINE INDENT if ( asterisk == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT print ( " * ▁ " , end = " " ) ; NEW_LINE print_asterisk ( asterisk - 1 ) ; NEW_LINE DEDENT def print_space ( space ) : NEW_LINE INDENT if ( space == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT print ( " ▁ " , end = " " ) ; NEW_LINE print ( " ▁ " , end = " " ) ; NEW_LINE print_space ( space - 1 ) ; NEW_LINE DEDENT def pattern_upper ( n , num ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT print_asterisk ( n ) ; NEW_LINE print_space ( 2 * ( num - n ) + 1 ) ; NEW_LINE print_asterisk ( n ) ; NEW_LINE print ( ) ; NEW_LINE pattern_upper ( n - 1 , num ) ; NEW_LINE DEDENT def pattern_lower ( n , num ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT print_asterisk ( num - n + 1 ) ; NEW_LINE print_space ( 2 * n - 1 ) ; NEW_LINE print_asterisk ( num - n + 1 ) ; NEW_LINE print ( ) ; NEW_LINE pattern_lower ( n - 1 , num ) ; NEW_LINE DEDENT def pattern ( n , num ) : NEW_LINE INDENT pattern_upper ( n , num ) ; NEW_LINE pattern_lower ( n - 1 , num ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE pattern ( n , n ) ; NEW_LINE DEDENT


class geeks { public static int firstSetBit ( int n ) { int x = n & ( n - 1 ) ; return ( n ^ x ) ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( firstSetBit ( n ) ) ; } }

def firstSetBit ( n ) : NEW_LINE INDENT x = n & ( n - 1 ) NEW_LINE return ( n ^ x ) NEW_LINE DEDENT n = 12 NEW_LINE print ( firstSetBit ( n ) ) NEW_LINE


import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int sumcommDiv ( int a , int b ) { int n = gcd ( a , b ) ; int sum = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) sum += i ; else sum += ( n / i ) + i ; } } return sum ; } public static void main ( String [ ] args ) { int a = 10 , b = 15 ; System . out . println ( " Sum ▁ = ▁ " + sumcommDiv ( a , b ) ) ; } }

from math import gcd , sqrt NEW_LINE def sumcommDiv ( a , b ) : NEW_LINE INDENT n = gcd ( a , b ) NEW_LINE sum = 0 NEW_LINE N = int ( sqrt ( n ) ) + 1 NEW_LINE for i in range ( 1 , N , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n / i == i ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT else : NEW_LINE INDENT sum += ( n / i ) + i NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE b = 15 NEW_LINE print ( " Sum ▁ = " , int ( sumcommDiv ( a , b ) ) ) NEW_LINE DEDENT


class cfg { static float squareSide ( float a ) { if ( a < 0 ) return - 1 ; float x = ( 0.423f * a ) ; return x ; } public static void main ( String [ ] args ) { float a = 8 ; System . out . println ( squareSide ( a ) ) ; } }

def squareSide ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = 0.423 * a NEW_LINE return x NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 8 NEW_LINE print ( squareSide ( a ) ) NEW_LINE DEDENT


class GFG { static int MAX = 100 ; static int check ( int i , int sum , int n , int k , int a [ ] , int dp [ ] [ ] ) { if ( sum <= 0 ) { return 0 ; } if ( i >= n ) { if ( sum == k ) { return 1 ; } return 0 ; } if ( dp [ i ] [ sum ] != - 1 ) { return dp [ i ] [ sum ] ; } dp [ i ] [ sum ] = check ( i + 1 , sum - 2 * a [ i ] , n , k , a , dp ) | check ( i + 1 , sum , n , k , a , dp ) ; dp [ i ] [ sum ] = check ( i + 1 , sum - ( i + 1 ) , n , k , a , dp ) | dp [ i ] [ sum ] ; dp [ i ] [ sum ] = check ( i + 1 , sum + i + 1 , n , k , a , dp ) | dp [ i ] [ sum ] ; return dp [ i ] [ sum ] ; } static int wrapper ( int n , int k , int a [ ] ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } int [ ] [ ] dp = new int [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } return check ( 0 , sum , n , k , a , dp ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 } ; int n = 4 , k = 5 ; if ( wrapper ( n , k , a ) == 1 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }

MAX = 100 NEW_LINE def check ( i , add , n , k , a , dp ) : NEW_LINE INDENT if add <= 0 : NEW_LINE INDENT return False NEW_LINE DEDENT if i >= n : NEW_LINE INDENT if add == k : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if dp [ i ] [ add ] != - 1 : NEW_LINE INDENT return dp [ i ] [ add ] NEW_LINE DEDENT dp [ i ] [ add ] = ( check ( i + 1 , add - 2 * a [ i ] , n , k , a , dp ) or check ( i + 1 , add , n , k , a , dp ) ) NEW_LINE dp [ i ] [ add ] = ( check ( i + 1 , add - ( i + 1 ) , n , k , a , dp ) or dp [ i ] [ add ] ) NEW_LINE dp [ i ] [ add ] = ( check ( i + 1 , add + i + 1 , n , k , a , dp ) or dp [ i ] [ add ] ) NEW_LINE return dp [ i ] [ add ] NEW_LINE DEDENT def wrapper ( n , k , a ) : NEW_LINE INDENT add = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT add += a [ i ] NEW_LINE DEDENT dp = [ - 1 ] * MAX NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT dp [ i ] = [ - 1 ] * MAX NEW_LINE DEDENT return check ( 0 , add , n , k , a , dp ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 , 4 ] NEW_LINE n = 4 NEW_LINE k = 5 NEW_LINE print ( " Yes " ) if wrapper ( n , k , a ) else print ( " No " ) NEW_LINE DEDENT


class GFG { static int AVLnodes ( int height ) { if ( height == 0 ) return 1 ; else if ( height == 1 ) return 2 ; return ( 1 + AVLnodes ( height - 1 ) + AVLnodes ( height - 2 ) ) ; } public static void main ( String args [ ] ) { int H = 3 ; System . out . println ( AVLnodes ( H ) ) ; } }

def AVLnodes ( height ) : NEW_LINE INDENT if ( height == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( height == 1 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT return ( 1 + AVLnodes ( height - 1 ) + AVLnodes ( height - 2 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT H = 3 NEW_LINE print ( AVLnodes ( H ) ) NEW_LINE DEDENT


import java . util . * ; class Fibonacci { public static int min ( int x , int y ) { return ( x <= y ) ? x : y ; } public static int fibMonaccianSearch ( int arr [ ] , int x , int n ) { int fibMMm2 = 0 ; int fibMMm1 = 1 ; int fibM = fibMMm2 + fibMMm1 ; while ( fibM < n ) { fibMMm2 = fibMMm1 ; fibMMm1 = fibM ; fibM = fibMMm2 + fibMMm1 ; } int offset = - 1 ; while ( fibM > 1 ) { int i = min ( offset + fibMMm2 , n - 1 ) ; if ( arr [ i ] < x ) { fibM = fibMMm1 ; fibMMm1 = fibMMm2 ; fibMMm2 = fibM - fibMMm1 ; offset = i ; } else if ( arr [ i ] > x ) { fibM = fibMMm2 ; fibMMm1 = fibMMm1 - fibMMm2 ; fibMMm2 = fibM - fibMMm1 ; } else return i ; } if ( fibMMm1 == 1 && arr [ offset + 1 ] == x ) return offset + 1 ; return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 22 , 35 , 40 , 45 , 50 , 80 , 82 , 85 , 90 , 100 } ; int n = 11 ; int x = 85 ; System . out . print ( " Found ▁ at ▁ index : ▁ " + fibMonaccianSearch ( arr , x , n ) ) ; } }

from bisect import bisect_left NEW_LINE def fibMonaccianSearch ( arr , x , n ) : NEW_LINE INDENT fibMMm2 = 0 NEW_LINE fibMMm1 = 1 NEW_LINE fibM = fibMMm2 + fibMMm1 NEW_LINE while ( fibM < n ) : NEW_LINE INDENT fibMMm2 = fibMMm1 NEW_LINE fibMMm1 = fibM NEW_LINE fibM = fibMMm2 + fibMMm1 NEW_LINE DEDENT offset = - 1 ; NEW_LINE while ( fibM > 1 ) : NEW_LINE INDENT i = min ( offset + fibMMm2 , n - 1 ) NEW_LINE if ( arr [ i ] < x ) : NEW_LINE INDENT fibM = fibMMm1 NEW_LINE fibMMm1 = fibMMm2 NEW_LINE fibMMm2 = fibM - fibMMm1 NEW_LINE offset = i NEW_LINE DEDENT elif ( arr [ i ] > x ) : NEW_LINE INDENT fibM = fibMMm2 NEW_LINE fibMMm1 = fibMMm1 - fibMMm2 NEW_LINE fibMMm2 = fibM - fibMMm1 NEW_LINE DEDENT else : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT if ( fibMMm1 and arr [ offset + 1 ] == x ) : NEW_LINE INDENT return offset + 1 ; NEW_LINE DEDENT return - 1 NEW_LINE DEDENT arr = [ 10 , 22 , 35 , 40 , 45 , 50 , 80 , 82 , 85 , 90 , 100 ] NEW_LINE n = len ( arr ) NEW_LINE x = 85 NEW_LINE print ( " Found ▁ at ▁ index : " , fibMonaccianSearch ( arr , x , n ) ) NEW_LINE


import java . io . * ; class GFG { static boolean checkForElement ( int array [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += array [ i ] ; if ( sum % 2 > 0 ) return false ; sum /= 2 ; int start = 0 ; int end = n - 1 ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( array [ mid ] == sum ) return true ; else if ( array [ mid ] > sum ) end = mid - 1 ; else start = mid + 1 ; } return false ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 2 , 3 } ; int n = array . length ; if ( checkForElement ( array , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def checkForElement ( array , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += array [ i ] NEW_LINE DEDENT if ( sum % 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT sum //= 2 NEW_LINE start = 0 NEW_LINE end = n - 1 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = start + ( end - start ) // 2 NEW_LINE if ( array [ mid ] == sum ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( array [ mid ] > sum ) : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT array = [ 1 , 2 , 3 ] NEW_LINE n = len ( array ) NEW_LINE if ( checkForElement ( array , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT