import java . util . * ; class GFG { static String partOdd ( String s ) { String st = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( i % 2 != 0 ) st += s . charAt ( i ) ; } return st ; } static String partEven ( String str ) { String s = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( i % 2 == 0 ) s += str . charAt ( i ) ; } return s ; } static boolean canBeMadeEqual ( String s1 , String s2 ) { char [ ] even1 = partEven ( s1 ) . toCharArray ( ) ; char [ ] even2 = partEven ( s2 ) . toCharArray ( ) ; char [ ] odd1 = partOdd ( s1 ) . toCharArray ( ) ; char [ ] odd2 = partOdd ( s2 ) . toCharArray ( ) ; Arrays . sort ( even1 ) ; Arrays . sort ( even2 ) ; Arrays . sort ( odd1 ) ; Arrays . sort ( odd2 ) ; String even_s1 = new String ( even1 ) ; String even_s2 = new String ( even2 ) ; String odd_s1 = new String ( odd1 ) ; String odd_s2 = new String ( odd2 ) ; if ( even_s1 . equals ( even_s2 ) && odd_s1 . equals ( odd_s2 ) ) return true ; return false ; } public static void main ( String [ ] args ) { String s1 = " cdab " ; String s2 = " abcd " ; if ( canBeMadeEqual ( s1 , s2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def partOdd ( s ) : NEW_LINE INDENT odd = [ ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if i % 2 != 0 : NEW_LINE INDENT odd . append ( s [ i ] ) NEW_LINE DEDENT DEDENT return odd NEW_LINE DEDENT def partEven ( s ) : NEW_LINE INDENT even = [ ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if i % 2 == 0 : NEW_LINE INDENT even . append ( s [ i ] ) NEW_LINE DEDENT DEDENT return even NEW_LINE DEDENT def canBeMadeEqual ( s1 , s2 ) : NEW_LINE INDENT even_s1 = partEven ( s1 ) NEW_LINE even_s2 = partEven ( s2 ) NEW_LINE odd_s1 = partOdd ( s1 ) NEW_LINE odd_s2 = partOdd ( s2 ) NEW_LINE even_s1 . sort ( ) NEW_LINE even_s2 . sort ( ) NEW_LINE odd_s1 . sort ( ) NEW_LINE odd_s2 . sort ( ) NEW_LINE if even_s1 == even_s2 and odd_s1 == odd_s2 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT s1 = " cdab " NEW_LINE s2 = " abcd " NEW_LINE if canBeMadeEqual ( s1 , s2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void moveZerosToEnd ( int arr [ ] , int n ) { int count = 0 ; int temp ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] != 0 ) ) { temp = arr [ count ] ; arr [ count ] = arr [ i ] ; arr [ i ] = temp ; count = count + 1 ; } } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 } ; int n = arr . length ; System . out . print ( " Original ▁ array : ▁ " ) ; printArray ( arr , n ) ; moveZerosToEnd ( arr , n ) ; System . out . print ( " \n Modified ▁ array : ▁ " ) ; printArray ( arr , n ) ; } }

def moveZerosToEnd ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] != 0 ) : NEW_LINE INDENT arr [ count ] , arr [ i ] = arr [ i ] , arr [ count ] NEW_LINE count += 1 NEW_LINE DEDENT DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 0 , 1 , 9 , 8 , 4 , 0 , 0 , 2 , 7 , 0 , 6 , 0 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Original ▁ array : " , end = " ▁ " ) NEW_LINE printArray ( arr , n ) NEW_LINE moveZerosToEnd ( arr , n ) NEW_LINE print ( " \n Modified ▁ array : ▁ " , end = " ▁ " ) NEW_LINE printArray ( arr , n ) NEW_LINE


class GFG { static boolean prevPermutation ( char [ ] str ) { int n = str . length - 1 ; int i = n ; while ( i > 0 && str [ i - 1 ] <= str [ i ] ) { i -- ; } if ( i <= 0 ) { return false ; } int j = i - 1 ; while ( j + 1 <= n && str [ j + 1 ] <= str [ i - 1 ] ) { j ++ ; } swap ( str , i - 1 , j ) ; StringBuilder sb = new StringBuilder ( String . valueOf ( str ) ) ; sb . reverse ( ) ; str = sb . toString ( ) . toCharArray ( ) ; return true ; } static String swap ( char [ ] ch , int i , int j ) { char temp = ch [ i ] ; ch [ i ] = ch [ j ] ; ch [ j ] = temp ; return String . valueOf ( ch ) ; } public static void main ( String [ ] args ) { char [ ] str = "4321" . toCharArray ( ) ; if ( prevPermutation ( str ) ) { System . out . println ( " Previous ▁ permutation ▁ is ▁ " + String . valueOf ( str ) ) ; } else { System . out . println ( " Previous ▁ permutation " + " doesn ' t ▁ exist " ) ; } } }

def prevPermutation ( str ) : NEW_LINE INDENT n = len ( str ) - 1 NEW_LINE i = n NEW_LINE while ( i > 0 and str [ i - 1 ] <= str [ i ] ) : NEW_LINE INDENT i -= 1 NEW_LINE DEDENT if ( i <= 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT j = i - 1 NEW_LINE while ( j + 1 <= n and str [ j + 1 ] <= str [ i - 1 ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT str = list ( str ) NEW_LINE temp = str [ i - 1 ] NEW_LINE str [ i - 1 ] = str [ j ] NEW_LINE str [ j ] = temp NEW_LINE str = ' ' . join ( str ) NEW_LINE str [ : : - 1 ] NEW_LINE return True , str NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "4321" NEW_LINE b , str = prevPermutation ( str ) NEW_LINE if ( b == True ) : NEW_LINE INDENT print ( " Previous ▁ permutation ▁ is " , str ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Previous ▁ permutation ▁ doesn ' t ▁ exist " ) NEW_LINE DEDENT DEDENT


class GFG { static double longestRodInCuboid ( int length , int breadth , int height ) { double result ; int temp ; temp = length * length + breadth * breadth + height * height ; result = Math . sqrt ( temp ) ; return result ; } public static void main ( String [ ] args ) { int length = 12 , breadth = 9 , height = 8 ; System . out . println ( ( int ) longestRodInCuboid ( length , breadth , height ) ) ; } }

from math import * NEW_LINE def longestRodInCuboid ( length , breadth , height ) : NEW_LINE INDENT temp = length * length + breadth * breadth + height * height NEW_LINE result = sqrt ( temp ) NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT length , breadth , height = 12 , 9 , 8 NEW_LINE print ( longestRodInCuboid ( length , breadth , height ) ) NEW_LINE DEDENT


public class GFG { static void checkSolution ( int a , int b , int c ) { if ( ( ( b * b ) - ( 4 * a * c ) ) > 0 ) System . out . println ( "2 ▁ solutions " ) ; else if ( ( ( b * b ) - ( 4 * a * c ) ) == 0 ) System . out . println ( "1 ▁ solution " ) ; else System . out . println ( " No ▁ solutions " ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 5 , c = 2 ; checkSolution ( a , b , c ) ; } }

def checkSolution ( a , b , c ) : NEW_LINE INDENT if ( ( b * b ) - ( 4 * a * c ) ) > 0 : NEW_LINE INDENT print ( "2 ▁ solutions " ) NEW_LINE DEDENT elif ( ( b * b ) - ( 4 * a * c ) ) == 0 : NEW_LINE INDENT print ( "1 ▁ solution " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ solutions " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , c = 2 , 5 , 2 NEW_LINE checkSolution ( a , b , c ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void checksum ( int n ) { if ( n % 5 == 0 ) System . out . println ( ( n / 5 - 2 ) + " ▁ " + ( n / 5 - 1 ) + " ▁ " + ( n / 5 ) + " ▁ " + ( n / 5 + 1 ) + " ▁ " + ( n / 5 + 2 ) ) ; else System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 15 ; checksum ( n ) ; } }

def checksum ( n ) : NEW_LINE INDENT n = int ( n ) NEW_LINE if n % 5 == 0 : NEW_LINE INDENT print ( int ( n / 5 - 2 ) , " ▁ " , int ( n / 5 - 1 ) , " ▁ " , int ( n / 5 ) , " ▁ " , int ( n / 5 + 1 ) , " ▁ " , int ( n / 5 + 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT n = 15 NEW_LINE checksum ( n ) NEW_LINE


class GFG { static void solve ( String s ) { String z = s ; int l = s . length ( ) ; int i ; String str ; if ( s . indexOf ( ' + ' ) != - 1 ) { i = s . indexOf ( ' + ' ) ; str = s . replace ( ' + ' , ' - ' ) ; } else { i = s . indexOf ( ' - ' ) ; str = s . replace ( ' - ' , ' + ' ) ; } System . out . println ( " Conjugate ▁ of ▁ " + z + " ▁ = ▁ " + str ) ; } public static void main ( String [ ] args ) { String s = "3-4i " ; solve ( s ) ; } }

def solve ( s ) : NEW_LINE INDENT z = s NEW_LINE l = len ( s ) NEW_LINE i = 0 NEW_LINE if ( s . find ( ' + ' ) != - 1 ) : NEW_LINE INDENT i = s . find ( ' + ' ) NEW_LINE s = s . replace ( ' + ' , ' - ' ) NEW_LINE DEDENT else : NEW_LINE INDENT i = s . find ( ' - ' ) NEW_LINE s = s . replace ( ' - ' , ' + ' , 1 ) NEW_LINE DEDENT print ( " Conjugate ▁ of ▁ " , z , " ▁ = ▁ " , s ) NEW_LINE DEDENT s = "3-4i " NEW_LINE solve ( s ) NEW_LINE


class GFG { static int NumUnsignBits = 64 ; static long findNum ( long x ) { for ( int i = 0 ; i < NumUnsignBits - 1 ; i ++ ) { if ( ( ( x >> i ) & 1 ) != ( ( x >> ( i + 1 ) ) & 1 ) ) { x ^= ( 1 << i ) | ( 1 << ( i + 1 ) ) ; return x ; } } return Long . MIN_VALUE ; } public static void main ( String [ ] args ) { int n = 92 ; System . out . println ( findNum ( n ) ) ; } }

NumUnsignBits = 64 ; NEW_LINE def findNum ( x ) : NEW_LINE INDENT for i in range ( NumUnsignBits - 1 ) : NEW_LINE INDENT if ( ( ( x >> i ) & 1 ) != ( ( x >> ( i + 1 ) ) & 1 ) ) : NEW_LINE INDENT x ^= ( 1 << i ) | ( 1 << ( i + 1 ) ) ; NEW_LINE return x ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 92 ; NEW_LINE print ( findNum ( n ) ) ; NEW_LINE DEDENT


import java . * ; class Geeks { public static void main ( String a [ ] ) { int x = 10 ; int y = 5 ; x = x + y ; y = x - y ; x = x - y ; System . out . println ( " After ▁ swaping : " + " ▁ x ▁ = ▁ " + x + " , ▁ y ▁ = ▁ " + y ) ; } }

x = 10 NEW_LINE y = 5 NEW_LINE x = x + y NEW_LINE y = x - y NEW_LINE x = x - y NEW_LINE print ( " After ▁ Swapping : ▁ x ▁ = " , x , " ▁ y ▁ = " , y ) ; NEW_LINE


import java . util . * ; class solution { static int Squares ( int n , int m , int a ) { return ( ( m + a - 1 ) / a ) * ( ( n + a - 1 ) / a ) ; } public static void main ( String arr [ ] ) { int n = 6 , m = 6 , a = 4 ; System . out . println ( Squares ( n , m , a ) ) ; } }

def Squares ( n , m , a ) : NEW_LINE INDENT return ( ( ( m + a - 1 ) // a ) * ( ( n + a - 1 ) // a ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 NEW_LINE m = 6 NEW_LINE a = 4 NEW_LINE print ( Squares ( n , m , a ) ) NEW_LINE DEDENT


class Test { static int gcd ( int a , int b ) { if ( a == 0 || b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return gcd ( a - b , b ) ; return gcd ( a , b - a ) ; } public static void main ( String [ ] args ) { int a = 98 , b = 56 ; System . out . println ( " GCD ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + gcd ( a , b ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 or b == 0 ) : NEW_LINE INDENT False NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return gcd ( a - b , b ) NEW_LINE DEDENT return gcd ( a , b - a ) NEW_LINE DEDENT a = 98 NEW_LINE b = 56 NEW_LINE if ( gcd ( a , b ) ) : NEW_LINE INDENT print ( ' GCD ▁ of ' , a , ' and ' , b , ' is ' , gcd ( a , b ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' not ▁ found ' ) NEW_LINE DEDENT


import java . util . HashMap ; import java . util . HashSet ; class GFG { static int countPiles ( int n , int [ ] a ) { HashMap < Integer , Integer > occ = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) occ . put ( a [ i ] , occ . get ( a [ i ] ) == null ? 1 : occ . get ( a [ i ] ) + 1 ) ; int pile = 0 ; while ( ! occ . isEmpty ( ) ) { pile ++ ; int size = 0 ; HashSet < Integer > toRemove = new HashSet < > ( ) ; for ( HashMap . Entry < Integer , Integer > tm : occ . entrySet ( ) ) { int mx = tm . getKey ( ) ; int ct = tm . getValue ( ) ; int use = Math . min ( ct , mx - size + 1 ) ; occ . put ( mx , occ . get ( mx ) - use ) ; size += use ; if ( occ . get ( mx ) == 0 ) toRemove . add ( mx ) ; } for ( int tm : toRemove ) occ . remove ( tm ) ; } return pile ; } public static void main ( String [ ] args ) { int [ ] a = { 0 , 0 , 1 , 1 , 2 } ; int n = a . length ; System . out . println ( countPiles ( n , a ) ) ; } }

def countPiles ( n , a ) : NEW_LINE INDENT occ = dict ( ) NEW_LINE for i in a : NEW_LINE INDENT if i in occ . keys ( ) : NEW_LINE INDENT occ [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT occ [ i ] = 1 NEW_LINE DEDENT DEDENT pile = 0 NEW_LINE while ( len ( occ ) > 0 ) : NEW_LINE INDENT pile += 1 NEW_LINE size = 0 NEW_LINE toRemove = dict ( ) NEW_LINE for tm in occ : NEW_LINE INDENT mx = tm NEW_LINE ct = occ [ tm ] NEW_LINE use = min ( ct , mx - size + 1 ) NEW_LINE occ [ mx ] -= use NEW_LINE size += use NEW_LINE if ( occ [ mx ] == 0 ) : NEW_LINE INDENT toRemove [ mx ] = 1 NEW_LINE DEDENT DEDENT for tm in toRemove : NEW_LINE INDENT del occ [ tm ] NEW_LINE DEDENT DEDENT return pile NEW_LINE DEDENT a = [ 0 , 0 , 1 , 1 , 2 ] NEW_LINE n = len ( a ) NEW_LINE print ( countPiles ( n , a ) ) NEW_LINE


import java . util . * ; class GfG { static int minCost ( int A [ ] , int n ) { int cost = 0 ; Arrays . sort ( A ) ; int K = A [ n / 2 ] ; for ( int i = 0 ; i < n ; ++ i ) cost += Math . abs ( A [ i ] - K ) ; if ( n % 2 == 0 ) { int tempCost = 0 ; K = A [ ( n / 2 ) - 1 ] ; for ( int i = 0 ; i < n ; ++ i ) tempCost += Math . abs ( A [ i ] - K ) ; cost = Math . min ( cost , tempCost ) ; } return cost ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 6 , 7 , 10 } ; int n = A . length ; System . out . println ( minCost ( A , n ) ) ; } }

def minCost ( A , n ) : NEW_LINE INDENT cost = 0 NEW_LINE A . sort ( ) ; NEW_LINE K = A [ int ( n / 2 ) ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT cost = cost + abs ( A [ i ] - K ) NEW_LINE DEDENT if n % 2 == 0 : NEW_LINE INDENT tempCost = 0 NEW_LINE K = A [ int ( n / 2 ) - 1 ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT tempCost = tempCost + abs ( A [ i ] - K ) NEW_LINE DEDENT cost = min ( cost , tempCost ) NEW_LINE DEDENT return cost NEW_LINE DEDENT A = [ 1 , 6 , 7 , 10 ] NEW_LINE n = len ( A ) NEW_LINE print ( minCost ( A , n ) ) NEW_LINE


class gfg { static boolean isSubArray ( int A [ ] , int B [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( A [ i ] == B [ j ] ) { i ++ ; j ++ ; if ( j == m ) return true ; } else { i = i - j + 1 ; j = 0 ; } } return false ; } public static void main ( String arr [ ] ) { int A [ ] = { 2 , 3 , 0 , 5 , 1 , 1 , 2 } ; int n = A . length ; int B [ ] = { 3 , 0 , 5 , 1 } ; int m = B . length ; if ( isSubArray ( A , B , n , m ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def isSubArray ( A , B , n , m ) : NEW_LINE INDENT i = 0 ; j = 0 ; NEW_LINE while ( i < n and j < m ) : NEW_LINE INDENT if ( A [ i ] == B [ j ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE j += 1 ; NEW_LINE if ( j == m ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT i = i - j + 1 ; NEW_LINE j = 0 ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 2 , 3 , 0 , 5 , 1 , 1 , 2 ] ; NEW_LINE n = len ( A ) ; NEW_LINE B = [ 3 , 0 , 5 , 1 ] ; NEW_LINE m = len ( B ) ; NEW_LINE if ( isSubArray ( A , B , n , m ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT DEDENT


import static java . lang . Math . pow ; class Test { static void nthprimedigitsnumber ( long n ) { long len = 1 ; long prev_count = 0 ; while ( true ) { long curr_count = ( long ) ( prev_count + pow ( 4 , len ) ) ; if ( prev_count < n && curr_count >= n ) break ; len ++ ; prev_count = curr_count ; } for ( int i = 1 ; i <= len ; i ++ ) { for ( long j = 1 ; j <= 4 ; j ++ ) { if ( prev_count + pow ( 4 , len - i ) < n ) prev_count += pow ( 4 , len - i ) ; else { if ( j == 1 ) System . out . print ( "2" ) ; else if ( j == 2 ) System . out . print ( "3" ) ; else if ( j == 3 ) System . out . print ( "5" ) ; else if ( j == 4 ) System . out . print ( "7" ) ; break ; } } } System . out . println ( ) ; } public static void main ( String args [ ] ) { nthprimedigitsnumber ( 10 ) ; nthprimedigitsnumber ( 21 ) ; } }

import math NEW_LINE def nthprimedigitsnumber ( n ) : NEW_LINE INDENT len = 1 ; NEW_LINE prev_count = 0 ; NEW_LINE while ( 1 ) : NEW_LINE INDENT curr_count = ( prev_count + math . pow ( 4 , len ) ) ; NEW_LINE if ( prev_count < n and curr_count >= n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT len += 1 ; NEW_LINE prev_count = curr_count ; NEW_LINE DEDENT for i in range ( 1 , len + 1 ) : NEW_LINE INDENT for j in range ( 1 , 5 ) : NEW_LINE INDENT if ( prev_count + pow ( 4 , len - i ) < n ) : NEW_LINE INDENT prev_count += pow ( 4 , len - i ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( j == 1 ) : NEW_LINE INDENT print ( "2" , end = " " ) ; NEW_LINE DEDENT elif ( j == 2 ) : NEW_LINE INDENT print ( "3" , end = " " ) ; NEW_LINE DEDENT elif ( j == 3 ) : NEW_LINE INDENT print ( "5" , end = " " ) ; NEW_LINE DEDENT elif ( j == 4 ) : NEW_LINE INDENT print ( "7" , end = " " ) ; NEW_LINE DEDENT break ; NEW_LINE DEDENT DEDENT DEDENT print ( ) ; NEW_LINE DEDENT nthprimedigitsnumber ( 10 ) ; NEW_LINE nthprimedigitsnumber ( 21 ) ; NEW_LINE


class GFG { static String even_or_odd ( String N ) { int len = N . length ( ) ; if ( N . charAt ( len - 1 ) == '0' || N . charAt ( len - 1 ) == '2' || N . charAt ( len - 1 ) == '4' || N . charAt ( len - 1 ) == '6' ) return ( " Even " ) ; else return ( " Odd " ) ; } public static void main ( String [ ] args ) { String N = "735" ; System . out . print ( even_or_odd ( N ) ) ; } }

def even_or_odd ( N ) : NEW_LINE INDENT l = len ( N ) ; NEW_LINE if ( N [ l - 1 ] == '0' or N [ l - 1 ] == '2' or N [ l - 1 ] == '4' or N [ l - 1 ] == '6' ) : NEW_LINE INDENT return ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " Odd " ) NEW_LINE DEDENT DEDENT N = "735" NEW_LINE print ( even_or_odd ( N ) ) NEW_LINE


import java . io . * ; class GFG { static int sumOfDigits ( int a ) { int sum = 0 ; while ( a != 0 ) { sum += a % 10 ; a /= 10 ; } return sum ; } static int findMax ( int x ) { int b = 1 , ans = x ; while ( x != 0 ) { int cur = ( x - 1 ) * b + ( b - 1 ) ; if ( sumOfDigits ( cur ) > sumOfDigits ( ans ) || ( sumOfDigits ( cur ) == sumOfDigits ( ans ) && cur > ans ) ) ans = cur ; x /= 10 ; b *= 10 ; } return ans ; } public static void main ( String [ ] args ) { int n = 521 ; System . out . println ( findMax ( n ) ) ; } }

def sumOfDigits ( a ) : NEW_LINE INDENT sm = 0 NEW_LINE while ( a != 0 ) : NEW_LINE INDENT sm = sm + a % 10 NEW_LINE a = a // 10 NEW_LINE DEDENT return sm NEW_LINE DEDENT def findMax ( x ) : NEW_LINE INDENT b = 1 NEW_LINE ans = x NEW_LINE while ( x != 0 ) : NEW_LINE INDENT cur = ( x - 1 ) * b + ( b - 1 ) NEW_LINE if ( sumOfDigits ( cur ) > sumOfDigits ( ans ) or ( sumOfDigits ( cur ) == sumOfDigits ( ans ) and cur > ans ) ) : NEW_LINE INDENT ans = cur NEW_LINE DEDENT x = x // 10 NEW_LINE b = b * 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 521 NEW_LINE print ( findMax ( n ) ) NEW_LINE


import java . util . ArrayList ; import java . util . List ; public class GFG { static List < Integer > factors = new ArrayList < > ( 10 ) ; static void findFactors ( int n ) { for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { if ( ( n / i ) == i ) { factors . add ( factors . size ( ) , i ) ; } else { factors . add ( factors . size ( ) , n / i ) ; factors . add ( factors . size ( ) , i ) ; } } } } static int findProduct ( int n ) { int product = - 1 ; int si = factors . size ( ) ; for ( int i = 0 ; i < si ; i ++ ) { for ( int j = 0 ; j < si ; j ++ ) { for ( int k = 0 ; k < si ; k ++ ) { for ( int l = 0 ; l < si ; l ++ ) { int s = factors . get ( i ) + factors . get ( j ) + factors . get ( k ) + factors . get ( l ) ; if ( s == n ) { int p = factors . get ( i ) * factors . get ( j ) * factors . get ( k ) * factors . get ( l ) ; if ( p > product ) { product = p ; } } } } } } return product ; } public static void main ( String [ ] args ) { int n = 10 ; findFactors ( n ) ; System . out . println ( findProduct ( n ) ) ; } }

from math import sqrt NEW_LINE factors = [ ] NEW_LINE def findFactors ( n ) : NEW_LINE INDENT for i in range ( 1 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( ( n / i ) == i ) : NEW_LINE INDENT factors . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT factors . append ( n / i ) NEW_LINE factors . append ( i ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def findProduct ( n ) : NEW_LINE INDENT product = - 1 NEW_LINE si = len ( factors ) NEW_LINE for i in range ( si ) : NEW_LINE INDENT for j in range ( si ) : NEW_LINE INDENT for k in range ( si ) : NEW_LINE INDENT for l in range ( si ) : NEW_LINE INDENT s = ( factors [ i ] + factors [ j ] + factors [ k ] + factors [ l ] ) NEW_LINE if ( s == n ) : NEW_LINE INDENT p = ( factors [ i ] * factors [ j ] * factors [ k ] * factors [ l ] ) NEW_LINE if ( p > product ) : NEW_LINE INDENT product = p NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT DEDENT return product NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE findFactors ( n ) NEW_LINE print ( int ( findProduct ( n ) ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void checksum ( int n ) { if ( n == 0 ) { System . out . println ( " - 1 ▁ 0 ▁ 1" ) ; return ; } int inc ; if ( n > 0 ) inc = 1 ; else inc = - 1 ; for ( int i = 0 ; i <= n - 2 ; i += inc ) { if ( i + i + 1 + i + 2 == n ) { System . out . println ( i + " ▁ " + ( i + 1 ) + " ▁ " + ( i + 2 ) ) ; return ; } } System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 6 ; checksum ( n ) ; } }

def checksum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT print ( " - 1 ▁ 0 ▁ 1" ) NEW_LINE return 0 NEW_LINE DEDENT inc = 0 NEW_LINE if n > 0 : NEW_LINE INDENT inc = 1 NEW_LINE DEDENT else : NEW_LINE INDENT inc = - 1 NEW_LINE DEDENT for i in range ( 0 , n - 1 , inc ) : NEW_LINE INDENT if i + i + 1 + i + 2 == n : NEW_LINE INDENT print ( i , " ▁ " , i + 1 , " ▁ " , i + 2 ) NEW_LINE return 0 NEW_LINE DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT n = 6 NEW_LINE checksum ( n ) NEW_LINE


import java . util . * ; class GFG { static int findTrailingZeros ( int n ) { if ( ( n & 1 ) == 1 ) return 0 ; else { int ans = 0 ; n /= 2 ; while ( n != 0 ) { ans += n / 5 ; n /= 5 ; } return ans ; } } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( findTrailingZeros ( n ) ) ; } }

def findTrailingZeros ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT ans = 0 NEW_LINE n //= 2 NEW_LINE while ( n ) : NEW_LINE INDENT ans += n // 5 NEW_LINE n //= 5 NEW_LINE DEDENT return ans NEW_LINE DEDENT DEDENT n = 12 NEW_LINE print ( findTrailingZeros ( n ) ) NEW_LINE


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

def pattern ( n ) : NEW_LINE INDENT k = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( n - 1 , k , - 1 ) : NEW_LINE INDENT print ( ' ▁ ' , end = ' ' ) NEW_LINE DEDENT print ( chr ( i + 65 ) , end = ' ' ) NEW_LINE for j in range ( 1 , k * 2 ) : NEW_LINE INDENT print ( ' ▁ ' , end = ' ' ) NEW_LINE DEDENT if i < n - 1 : NEW_LINE INDENT print ( chr ( i + 65 ) , end = ' ' ) NEW_LINE DEDENT print ( ) NEW_LINE k += 1 NEW_LINE DEDENT DEDENT n = 5 NEW_LINE pattern ( n ) NEW_LINE


import java . util . Collections ; import java . util . Arrays ; public class GfG { public static int getMinCost ( Integer arr [ ] , int n ) { int min_ele = Collections . min ( Arrays . asList ( arr ) ) ; return min_ele * ( n - 1 ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 4 , 2 , 5 } ; int n = arr . length ; System . out . println ( getMinCost ( arr , n ) ) ; } }

def getMinCost ( arr , n ) : NEW_LINE INDENT min_ele = min ( arr ) NEW_LINE return min_ele * ( n - 1 ) NEW_LINE DEDENT arr = [ 4 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getMinCost ( arr , n ) ) NEW_LINE


class GFG { static long moduloMultiplication ( long a , long b , long mod ) { long res = 0 ; a %= mod ; while ( b > 0 ) { if ( ( b & 1 ) > 0 ) { res = ( res + a ) % mod ; } a = ( 2 * a ) % mod ; b >>= 1 ; } return res ; } public static void main ( String [ ] args ) { long a = 10123465234878998L ; long b = 65746311545646431L ; long m = 10005412336548794L ; System . out . print ( moduloMultiplication ( a , b , m ) ) ; } }

def moduloMultiplication ( a , b , mod ) : NEW_LINE INDENT res = 0 ; NEW_LINE a = a % mod ; NEW_LINE while ( b ) : NEW_LINE INDENT if ( b & 1 ) : NEW_LINE INDENT res = ( res + a ) % mod ; NEW_LINE DEDENT a = ( 2 * a ) % mod ; NEW_LINE b >>= 1 ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT a = 10123465234878998 ; NEW_LINE b = 65746311545646431 ; NEW_LINE m = 10005412336548794 ; NEW_LINE print ( moduloMultiplication ( a , b , m ) ) ; NEW_LINE


import java . util . * ; class GFG { static Vector < String > splitStrings ( String str , char dl ) { String word = " " ; int num = 0 ; str = str + dl ; int l = str . length ( ) ; Vector < String > substr_list = new Vector < String > ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( str . charAt ( i ) != dl ) { word = word + str . charAt ( i ) ; } else { if ( ( int ) word . length ( ) != 0 ) { substr_list . add ( word ) ; } word = " " ; } } return substr_list ; } public static void main ( String [ ] args ) { String str = " geeks ; for ; geeks " ; char dl = ' ; ' ; Vector < String > res = splitStrings ( str , dl ) ; for ( String x : res ) { System . out . println ( x ) ; } } }

def splitStrings ( st , dl ) : NEW_LINE INDENT word = " " NEW_LINE num = 0 NEW_LINE st += dl NEW_LINE l = len ( st ) NEW_LINE substr_list = [ ] NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( st [ i ] != dl ) : NEW_LINE INDENT word += st [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT if ( len ( word ) != 0 ) : NEW_LINE INDENT substr_list . append ( word ) NEW_LINE DEDENT word = " " NEW_LINE DEDENT DEDENT return substr_list NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeks ; for ; geeks " NEW_LINE dl = ' ; ' NEW_LINE res = splitStrings ( str , dl ) NEW_LINE for x in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ x ] ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int maxcoefficientvalue ( int n ) { int [ ] [ ] C = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= Math . min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } int maxvalue = 0 ; for ( int i = 0 ; i <= n ; i ++ ) maxvalue = Math . max ( maxvalue , C [ n ] [ i ] ) ; return maxvalue ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( maxcoefficientvalue ( n ) ) ; } }

def maxcoefficientvalue ( n ) : NEW_LINE INDENT C = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , n ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) ; NEW_LINE DEDENT DEDENT DEDENT maxvalue = 0 ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT maxvalue = max ( maxvalue , C [ n ] [ i ] ) ; NEW_LINE DEDENT return maxvalue ; NEW_LINE DEDENT n = 4 ; NEW_LINE print ( maxcoefficientvalue ( n ) ) ; NEW_LINE


class GFG { static int dig [ ] = { 1 , 1 , 2 , 6 , 4 , 2 , 2 , 4 , 2 , 8 } ; static int lastNon0Digit ( int n ) { if ( n < 10 ) return dig [ n ] ; if ( ( ( n / 10 ) % 10 ) % 2 == 0 ) return ( 6 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ; else return ( 4 * lastNon0Digit ( n / 5 ) * dig [ n % 10 ] ) % 10 ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . print ( lastNon0Digit ( n ) ) ; } }

dig = [ 1 , 1 , 2 , 6 , 4 , 2 , 2 , 4 , 2 , 8 ] NEW_LINE def lastNon0Digit ( n ) : NEW_LINE INDENT if ( n < 10 ) : NEW_LINE INDENT return dig [ n ] NEW_LINE DEDENT if ( ( ( n // 10 ) % 10 ) % 2 == 0 ) : NEW_LINE INDENT return ( 6 * lastNon0Digit ( n // 5 ) * dig [ n % 10 ] ) % 10 NEW_LINE DEDENT else : NEW_LINE INDENT return ( 4 * lastNon0Digit ( n // 5 ) * dig [ n % 10 ] ) % 10 NEW_LINE DEDENT return 0 NEW_LINE DEDENT n = 14 NEW_LINE print ( lastNon0Digit ( n ) ) NEW_LINE


import java . util . * ; class GFG { static int maxSelectionCount ( int a [ ] , int n ) { int res = 0 ; Arrays . sort ( a ) ; int select = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= select ) { res ++ ; select ++ ; } } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 1 , 3 , 5 , 1 , 4 } ; int N = arr . length ; System . out . println ( maxSelectionCount ( arr , N ) ) ; } }

def maxSelectionCount ( a , n ) : NEW_LINE INDENT res = 0 ; NEW_LINE a . sort ( ) ; NEW_LINE select = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] >= select ) : NEW_LINE INDENT res += 1 ; NEW_LINE select += 1 ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT arr = [ 4 , 2 , 1 , 3 , 5 , 1 , 4 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( maxSelectionCount ( arr , N ) ) ; NEW_LINE


import java . io . * ; class GFG { static int answerQuery ( int a [ ] , int n , int l , int r ) { int count = 0 ; l = l - 1 ; for ( int i = l ; i < r ; i ++ ) { int element = a [ i ] ; int divisors = 0 ; for ( int j = l ; j < r ; j ++ ) { if ( a [ j ] % a [ i ] == 0 ) divisors ++ ; else break ; } if ( divisors == ( r - l ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 5 } ; int n = a . length ; int l = 1 , r = 4 ; System . out . println ( answerQuery ( a , n , l , r ) ) ; l = 2 ; r = 4 ; System . out . println ( answerQuery ( a , n , l , r ) ) ; } }

def answerQuery ( a , n , l , r ) : NEW_LINE INDENT count = 0 NEW_LINE l = l - 1 NEW_LINE for i in range ( l , r , 1 ) : NEW_LINE INDENT element = a [ i ] NEW_LINE divisors = 0 NEW_LINE for j in range ( l , r , 1 ) : NEW_LINE INDENT if ( a [ j ] % a [ i ] == 0 ) : NEW_LINE INDENT divisors += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( divisors == ( r - l ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 2 , 3 , 5 ] NEW_LINE n = len ( a ) NEW_LINE l = 1 NEW_LINE r = 4 NEW_LINE print ( answerQuery ( a , n , l , r ) ) NEW_LINE l = 2 NEW_LINE r = 4 NEW_LINE print ( answerQuery ( a , n , l , r ) ) NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int countEleLessThanOrEqual ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { for ( int i = 0 ; i < m ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) if ( arr2 [ j ] <= arr1 [ i ] ) count ++ ; System . out . print ( count + " ▁ " ) ; } return m ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 , 4 , 7 , 9 } ; int arr2 [ ] = { 0 , 1 , 2 , 1 , 1 , 4 } ; countEleLessThanOrEqual ( arr1 , arr2 , arr1 . length , arr2 . length ) ; } }

def countEleLessThanOrEqual ( arr1 , arr2 , m , n ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr2 [ j ] <= arr1 [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr1 = [ 1 , 2 , 3 , 4 , 7 , 9 ] NEW_LINE arr2 = [ 0 , 1 , 2 , 1 , 1 , 4 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE countEleLessThanOrEqual ( arr1 , arr2 , m , n ) NEW_LINE


import java . util . Arrays ; class Test { static int count ( int x , int Y [ ] , int n , int NoOfY [ ] ) { if ( x == 0 ) return 0 ; if ( x == 1 ) return NoOfY [ 0 ] ; int idx = Arrays . binarySearch ( Y , x ) ; int ans ; if ( idx < 0 ) { idx = Math . abs ( idx + 1 ) ; ans = Y . length - idx ; } else { while ( idx < n && Y [ idx ] == x ) { idx ++ ; } ans = Y . length - idx ; } ans += ( NoOfY [ 0 ] + NoOfY [ 1 ] ) ; if ( x == 2 ) ans -= ( NoOfY [ 3 ] + NoOfY [ 4 ] ) ; if ( x == 3 ) ans += NoOfY [ 2 ] ; return ans ; } static int countPairs ( int X [ ] , int Y [ ] , int m , int n ) { int NoOfY [ ] = new int [ 5 ] ; for ( int i = 0 ; i < n ; i ++ ) if ( Y [ i ] < 5 ) NoOfY [ Y [ i ] ] ++ ; Arrays . sort ( Y ) ; int total_pairs = 0 ; for ( int i = 0 ; i < m ; i ++ ) total_pairs += count ( X [ i ] , Y , n , NoOfY ) ; return total_pairs ; } public static void main ( String args [ ] ) { int X [ ] = { 2 , 1 , 6 } ; int Y [ ] = { 1 , 5 } ; System . out . println ( " Total ▁ pairs ▁ = ▁ " + countPairs ( X , Y , X . length , Y . length ) ) ; } }

import bisect NEW_LINE def count ( x , Y , n , NoOfY ) : NEW_LINE INDENT if x == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if x == 1 : NEW_LINE INDENT return NoOfY [ 0 ] NEW_LINE DEDENT idx = bisect . bisect_right ( Y , x ) NEW_LINE ans = n - idx NEW_LINE ans += NoOfY [ 0 ] + NoOfY [ 1 ] NEW_LINE if x == 2 : NEW_LINE INDENT ans -= NoOfY [ 3 ] + NoOfY [ 4 ] NEW_LINE DEDENT if x == 3 : NEW_LINE INDENT ans += NoOfY [ 2 ] NEW_LINE DEDENT return ans NEW_LINE DEDENT def count_pairs ( X , Y , m , n ) : NEW_LINE INDENT NoOfY = [ 0 ] * 5 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if Y [ i ] < 5 : NEW_LINE INDENT NoOfY [ Y [ i ] ] += 1 NEW_LINE DEDENT DEDENT Y . sort ( ) NEW_LINE total_pairs = 0 NEW_LINE for x in X : NEW_LINE INDENT total_pairs += count ( x , Y , n , NoOfY ) NEW_LINE DEDENT return total_pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = [ 2 , 1 , 6 ] NEW_LINE Y = [ 1 , 5 ] NEW_LINE print ( " Total ▁ pairs ▁ = ▁ " , count_pairs ( X , Y , len ( X ) , len ( Y ) ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int twoEggDrop ( int k ) { return ( int ) Math . ceil ( ( - 1.0 + Math . sqrt ( 1 + 8 * k ) ) / 2.0 ) ; } public static void main ( String [ ] args ) { int k = 100 ; System . out . println ( twoEggDrop ( k ) ) ; } }

import math as mt NEW_LINE def twoEggDrop ( k ) : NEW_LINE INDENT return mt . ceil ( ( - 1.0 + mt . sqrt ( 1 + 8 * k ) ) / 2 ) NEW_LINE DEDENT k = 100 NEW_LINE print ( twoEggDrop ( k ) ) NEW_LINE


import java . io . * ; class GFG { static int getIndex ( int a [ ] , int n ) { if ( n == 1 ) return 0 ; int ptrL = 0 , ptrR = n - 1 , sumL = a [ 0 ] , sumR = a [ n - 1 ] ; while ( ptrR - ptrL > 1 ) { if ( sumL < sumR ) { ptrL ++ ; sumL += a [ ptrL ] ; } else if ( sumL > sumR ) { ptrR -- ; sumR += a [ ptrR ] ; } else { break ; } } return ptrL ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 7 , 9 , 8 , 7 } ; int n = a . length ; System . out . println ( getIndex ( a , n ) ) ; } }

def getIndex ( a , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT ptrL = 0 NEW_LINE ptrR = n - 1 NEW_LINE sumL = a [ 0 ] NEW_LINE sumR = a [ n - 1 ] NEW_LINE while ( ptrR - ptrL > 1 ) : NEW_LINE INDENT if ( sumL < sumR ) : NEW_LINE INDENT ptrL += 1 NEW_LINE sumL += a [ ptrL ] NEW_LINE DEDENT elif ( sumL > sumR ) : NEW_LINE INDENT ptrR -= 1 NEW_LINE sumR += a [ ptrR ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return ptrL NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 7 , 9 , 8 , 7 ] NEW_LINE n = len ( a ) NEW_LINE print ( getIndex ( a , n ) ) NEW_LINE DEDENT


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

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


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