import java . io . * ; class GFG { static int countEvenSum ( int arr [ ] , int n ) { int temp [ ] = { 1 , 0 } ; int result = 0 , sum = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { sum = ( ( sum + arr [ i ] ) % 2 + 2 ) % 2 ; temp [ sum ] ++ ; } result = result + ( temp [ 0 ] * ( temp [ 0 ] - 1 ) / 2 ) ; result = result + ( temp [ 1 ] * ( temp [ 1 ] - 1 ) / 2 ) ; return ( result ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . length ; System . out . println ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " + countEvenSum ( arr , n ) ) ; } }

def countEvenSum ( arr , n ) : NEW_LINE INDENT temp = [ 1 , 0 ] NEW_LINE result = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = ( ( sum + arr [ i ] ) % 2 + 2 ) % 2 NEW_LINE temp [ sum ] += 1 NEW_LINE DEDENT result = result + ( temp [ 0 ] * ( temp [ 0 ] - 1 ) // 2 ) NEW_LINE result = result + ( temp [ 1 ] * ( temp [ 1 ] - 1 ) // 2 ) NEW_LINE return ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 4 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ Number ▁ of ▁ Subarrays ▁ with ▁ even " " ▁ sum ▁ is " , countEvenSum ( arr , n ) ) NEW_LINE DEDENT


class GFG { static String reverseWord ( String str ) { int len = str . length ( ) ; int i = 1 ; int j = str . length ( ) - 2 ; char [ ] strchar = str . toCharArray ( ) ; while ( i < j ) { char temp = strchar [ i ] ; strchar [ i ] = strchar [ j ] ; strchar [ j ] = temp ; i ++ ; j -- ; } str = new String ( strchar ) ; return str ; } static void reverseWords ( String str ) { String [ ] tok = str . split ( " \\ s " ) ; for ( String w : tok ) { System . out . print ( reverseWord ( w ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = " geeks ▁ for ▁ geeks " ; reverseWords ( str ) ; } }

def reverseWord ( Str ) : NEW_LINE INDENT i = 1 NEW_LINE j = len ( Str ) - 2 NEW_LINE while ( i < j ) : NEW_LINE INDENT temp = Str [ i ] NEW_LINE Str [ i ] = Str [ j ] NEW_LINE Str [ j ] = temp NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT return " " . join ( Str ) NEW_LINE DEDENT def reverseWords ( Str ) : NEW_LINE INDENT Str = Str . split ( ) NEW_LINE for i in Str : NEW_LINE INDENT j = [ h for h in i ] NEW_LINE print ( reverseWord ( j ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT Str = " geeks ▁ for ▁ geeks " NEW_LINE reverseWords ( Str ) NEW_LINE


import java . io . * ; class GFG { static void checkEVENodd ( int arr [ ] , int n , int l , int r ) { if ( arr [ r ] == 1 ) System . out . println ( " odd " ) ; else System . out . println ( " even " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 0 , 1 } ; int n = arr . length ; checkEVENodd ( arr , n , 1 , 3 ) ; } }

def checkEVENodd ( arr , n , l , r ) : NEW_LINE INDENT if ( arr [ r ] == 1 ) : NEW_LINE INDENT print ( " odd " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " even " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 1 , 0 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE checkEVENodd ( arr , n , 1 , 3 ) NEW_LINE


class GFG { static int INT_SIZE = 32 ; static int Right_most_setbit ( int num ) { int pos = 1 ; for ( int i = 0 ; i < INT_SIZE ; i ++ ) { if ( ( num & ( 1 << i ) ) == 0 ) pos ++ ; else break ; } return pos ; } static int findValueAtK ( int n , int m , int k ) { int positionOfRightmostSetbit = Right_most_setbit ( k ) ; return ( ( m - 1 ) + positionOfRightmostSetbit ) ; } public static void main ( String [ ] args ) { int k = 100 , n = 9 , m = 74 ; System . out . println ( findValueAtK ( n , m , k ) ) ; } }

import math NEW_LINE def findValueAtK ( n , m , k ) : NEW_LINE INDENT positionOfRightmostSetbit = math . log2 ( k & - k ) + 1 NEW_LINE return ( ( m - 1 ) + positionOfRightmostSetbit ) NEW_LINE DEDENT k = 100 NEW_LINE n = 9 NEW_LINE m = 74 NEW_LINE print ( findValueAtK ( n , m , k ) ) NEW_LINE


class Main { static boolean checkForSorting ( int arr [ ] , int n ) { for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i + 1 ] ) { if ( arr [ i ] - arr [ i + 1 ] == 1 ) { int temp = arr [ i ] ; arr [ i ] = arr [ i + 1 ] ; arr [ i + 1 ] = temp ; } else return false ; } } return true ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 0 , 3 , 2 } ; int n = arr . length ; if ( checkForSorting ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def checkForSorting ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i + 1 ] == 1 ) : NEW_LINE INDENT arr [ i ] , arr [ i + 1 ] = arr [ i + 1 ] , arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 1 , 0 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( checkForSorting ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int slopeOfNum ( String num , int n ) { int slope = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( num . charAt ( i ) > num . charAt ( i - 1 ) && num . charAt ( i ) > num . charAt ( i + 1 ) ) slope ++ ; else if ( num . charAt ( i ) < num . charAt ( i - 1 ) && num . charAt ( i ) < num . charAt ( i + 1 ) ) slope ++ ; } return slope ; } public static void main ( String [ ] args ) { String num = "1213321" ; int n = num . length ( ) ; System . out . println ( " Slope ▁ = ▁ " + slopeOfNum ( num , n ) ) ; } }

def slopeOfNum ( num , n ) : NEW_LINE INDENT slope = 0 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( num [ i ] > num [ i - 1 ] and num [ i ] > num [ i + 1 ] ) : NEW_LINE INDENT slope += 1 NEW_LINE DEDENT elif ( num [ i ] < num [ i - 1 ] and num [ i ] < num [ i + 1 ] ) : NEW_LINE INDENT slope += 1 NEW_LINE DEDENT DEDENT return slope NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT num = "1213321" NEW_LINE n = len ( num ) NEW_LINE print ( " Slpoe ▁ = " , slopeOfNum ( num , n ) ) NEW_LINE DEDENT


class GFG { static boolean isCyclic ( long N ) { long num = N ; int count = 0 ; int digit = ( int ) ( num % 10 ) ; boolean allSame = true ; while ( num > 0 ) { count ++ ; if ( num % 10 != digit ) allSame = false ; num = num / 10 ; } if ( allSame == true ) return false ; if ( count % 2 == 0 ) { long halfPower = ( long ) Math . pow ( 10 , count / 2 ) ; long firstHalf = N % halfPower ; long secondHalf = N / halfPower ; if ( firstHalf == firstHalf && isCyclic ( firstHalf ) ) return false ; } num = N ; while ( true ) { long rem = num % 10 ; long div = num / 10 ; num = ( long ) ( Math . pow ( 10 , count - 1 ) ) * rem + div ; if ( num == N ) break ; if ( num % N != 0 ) return false ; } return true ; } public static void main ( String [ ] args ) { long N = 142857 ; if ( isCyclic ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def isCyclic ( N ) : NEW_LINE INDENT num = N NEW_LINE count = 0 NEW_LINE digit = ( num % 10 ) NEW_LINE allSame = True NEW_LINE while ( num > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE if ( num % 10 != digit ) : NEW_LINE INDENT allSame = False NEW_LINE DEDENT num = num // 10 NEW_LINE DEDENT if ( allSame == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( count % 2 == 0 ) : NEW_LINE INDENT halfPower = pow ( 10 , count // 2 ) NEW_LINE firstHalf = N % halfPower NEW_LINE secondHalf = N / halfPower NEW_LINE if ( firstHalf == firstHalf and isCyclic ( firstHalf ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT num = N NEW_LINE while ( True ) : NEW_LINE INDENT rem = num % 10 NEW_LINE div = num // 10 NEW_LINE num = pow ( 10 , count - 1 ) * rem + div NEW_LINE if ( num == N ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( num % N != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT N = 142857 NEW_LINE if ( isCyclic ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static int countOfLetters ( String str ) { int letter = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) <= ' Z ' ) || ( str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) ) letter ++ ; } return letter ; } static int countOfNumbers ( String str ) { int number = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) >= '0' && str . charAt ( i ) <= '9' ) number ++ ; } return number ; } static void check ( String str ) { if ( countOfLetters ( str ) == countOfNumbers ( str ) ) System . out . print ( " Yes \n " ) ; else System . out . print ( " No \n " ) ; } public static void main ( String [ ] args ) { String str = " GeeKs01324" ; check ( str ) ; } }

def countOfLetters ( string ) : NEW_LINE INDENT letter = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( ( string [ i ] >= ' A ' and string [ i ] <= ' Z ' ) or ( string [ i ] >= ' a ' and string [ i ] <= ' z ' ) ) : NEW_LINE INDENT letter += 1 ; NEW_LINE DEDENT DEDENT return letter ; NEW_LINE DEDENT def countOfNumbers ( string ) : NEW_LINE INDENT number = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT if ( string [ i ] >= '0' and string [ i ] <= '9' ) : NEW_LINE INDENT number += 1 ; NEW_LINE DEDENT DEDENT return number ; NEW_LINE DEDENT def check ( string ) : NEW_LINE INDENT if ( countOfLetters ( string ) == countOfNumbers ( string ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GeeKs01324" ; NEW_LINE check ( string ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int findMaximum ( int l , int p , int q , int r ) { int dp [ ] = new int [ l + 1 ] ; for ( int i = 0 ; i < l + 1 ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int i = 0 ; i <= l ; i ++ ) { if ( dp [ i ] == - 1 ) continue ; if ( i + p <= l ) dp [ i + p ] = Math . max ( dp [ i + p ] , dp [ i ] + 1 ) ; if ( i + q <= l ) dp [ i + q ] = Math . max ( dp [ i + q ] , dp [ i ] + 1 ) ; if ( i + r <= l ) dp [ i + r ] = Math . max ( dp [ i + r ] , dp [ i ] + 1 ) ; } return dp [ l ] ; } public static void main ( String [ ] args ) { int l = 11 , p = 2 , q = 3 , r = 5 ; int ans = findMaximum ( l , p , q , r ) ; System . out . println ( ans ) ; } }

def findMaximum ( l , p , q , r ) : NEW_LINE INDENT dp = [ - 1 ] * ( l + 1 ) NEW_LINE dp [ 0 ] = 0 NEW_LINE for i in range ( l + 1 ) : NEW_LINE INDENT if ( dp [ i ] == - 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( i + p <= l ) : NEW_LINE INDENT dp [ i + p ] = ( max ( dp [ i + p ] , dp [ i ] + 1 ) ) NEW_LINE DEDENT if ( i + q <= l ) : NEW_LINE INDENT dp [ i + q ] = ( max ( dp [ i + q ] , dp [ i ] + 1 ) ) NEW_LINE DEDENT if ( i + r <= l ) : NEW_LINE INDENT dp [ i + r ] = ( max ( dp [ i + r ] , dp [ i ] + 1 ) ) NEW_LINE DEDENT DEDENT return dp [ l ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 11 NEW_LINE p = 2 NEW_LINE q = 3 NEW_LINE r = 5 NEW_LINE ans = findMaximum ( l , p , q , r ) NEW_LINE print ( ans ) NEW_LINE DEDENT


class GFG { static int maxAbsDiff ( int arr [ ] , int n ) { int minEle = arr [ 0 ] ; int maxEle = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { minEle = Math . min ( minEle , arr [ i ] ) ; maxEle = Math . max ( maxEle , arr [ i ] ) ; } return ( maxEle - minEle ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 1 , 5 , 3 } ; int n = arr . length ; System . out . print ( maxAbsDiff ( arr , n ) ) ; } }

def maxAbsDiff ( arr , n ) : NEW_LINE INDENT minEle = arr [ 0 ] NEW_LINE maxEle = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT minEle = min ( minEle , arr [ i ] ) NEW_LINE maxEle = max ( maxEle , arr [ i ] ) NEW_LINE DEDENT return ( maxEle - minEle ) NEW_LINE DEDENT arr = [ 2 , 1 , 5 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxAbsDiff ( arr , n ) ) NEW_LINE


class cfg { static int subarrayCount ( int arr [ ] , int n ) { int result = 0 ; int fast = 0 , slow = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] - arr [ i - 1 ] == 1 ) { fast ++ ; } else { int len = fast - slow + 1 ; result += len * ( len - 1 ) / 2 ; fast = i ; slow = i ; } } if ( fast != slow ) { int len = fast - slow + 1 ; result += len * ( len - 1 ) / 2 ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 5 , 6 , 7 } ; int n = arr . length ; System . out . println ( subarrayCount ( arr , n ) ) ; } }

def subarrayCount ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE fast , slow = 0 , 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] - arr [ i - 1 ] == 1 ) : NEW_LINE INDENT fast += 1 NEW_LINE DEDENT else : NEW_LINE INDENT length = fast - slow + 1 NEW_LINE result += length * ( length - 1 ) // 2 ; NEW_LINE fast = i NEW_LINE slow = i NEW_LINE DEDENT DEDENT if ( fast != slow ) : NEW_LINE INDENT length = fast - slow + 1 NEW_LINE result += length * ( length - 1 ) // 2 ; NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( subarrayCount ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int findSplitPoint ( int arr [ ] , int n ) { int leftSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) leftSum += arr [ i ] ; int rightSum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { rightSum += arr [ i ] ; leftSum -= arr [ i ] ; if ( rightSum == leftSum ) return i ; } return - 1 ; } static void printTwoParts ( int arr [ ] , int n ) { int splitPoint = findSplitPoint ( arr , n ) ; if ( splitPoint == - 1 || splitPoint == n ) { System . out . println ( " Not ▁ Possible " ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) { if ( splitPoint == i ) System . out . println ( ) ; System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 5 } ; int n = arr . length ; printTwoParts ( arr , n ) ; } }

def findSplitPoint ( arr , n ) : NEW_LINE INDENT leftSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT leftSum += arr [ i ] NEW_LINE DEDENT rightSum = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT rightSum += arr [ i ] NEW_LINE leftSum -= arr [ i ] NEW_LINE if ( rightSum == leftSum ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def printTwoParts ( arr , n ) : NEW_LINE INDENT splitPoint = findSplitPoint ( arr , n ) NEW_LINE if ( splitPoint == - 1 or splitPoint == n ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( splitPoint == i ) : NEW_LINE INDENT print ( " " ) NEW_LINE DEDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE printTwoParts ( arr , n ) NEW_LINE


import java . util . * ; class GFG { static void solve ( String s , int k ) { int count = 0 , length = 0 , pos = 0 ; HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( m . containsKey ( s . charAt ( i ) ) ) m . put ( s . charAt ( i ) , m . get ( s . charAt ( i ) ) + 1 ) ; else m . put ( s . charAt ( i ) , 1 ) ; length ++ ; if ( length > k ) { m . put ( s . charAt ( pos ) , m . get ( s . charAt ( pos ) ) - 1 ) ; pos ++ ; length -- ; } if ( length == k && m . get ( s . charAt ( i ) ) == length ) count ++ ; } System . out . println ( count ) ; } public static void main ( String [ ] args ) { String s = " aaaabbbccdddd " ; int k = 4 ; solve ( s , k ) ; } }

def solve ( s , k ) : NEW_LINE INDENT count , length , pos = 0 , 0 , 0 NEW_LINE m = dict . fromkeys ( s , 0 ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE length += 1 NEW_LINE if length > k : NEW_LINE INDENT m [ s [ pos ] ] -= 1 NEW_LINE pos += 1 NEW_LINE length -= 1 NEW_LINE DEDENT if length == k and m [ s [ i ] ] == length : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aaaabbbccdddd " NEW_LINE k = 4 NEW_LINE solve ( s , k ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int findMissingUtil ( int arr [ ] , int low , int high , int diff ) { if ( high <= low ) return Integer . MAX_VALUE ; int mid = low + ( high - low ) / 2 ; if ( arr [ mid + 1 ] - arr [ mid ] != diff ) return ( arr [ mid ] + diff ) ; if ( mid > 0 && arr [ mid ] - arr [ mid - 1 ] != diff ) return ( arr [ mid - 1 ] + diff ) ; if ( arr [ mid ] == arr [ 0 ] + mid * diff ) return findMissingUtil ( arr , mid + 1 , high , diff ) ; return findMissingUtil ( arr , low , mid - 1 , diff ) ; } static int findMissing ( int arr [ ] , int n ) { int diff = ( arr [ n - 1 ] - arr [ 0 ] ) / n ; return findMissingUtil ( arr , 0 , n - 1 , diff ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 8 , 10 , 12 , 14 } ; int n = arr . length ; System . out . println ( " The ▁ missing ▁ element ▁ is ▁ " + findMissing ( arr , n ) ) ; } }

import sys NEW_LINE def findMissingUtil ( arr , low , high , diff ) : NEW_LINE INDENT if ( high <= low ) : NEW_LINE INDENT return sys . maxsize ; NEW_LINE DEDENT mid = int ( low + ( high - low ) / 2 ) ; NEW_LINE if ( arr [ mid + 1 ] - arr [ mid ] != diff ) : NEW_LINE INDENT return ( arr [ mid ] + diff ) ; NEW_LINE DEDENT if ( mid > 0 and arr [ mid ] - arr [ mid - 1 ] != diff ) : NEW_LINE INDENT return ( arr [ mid - 1 ] + diff ) ; NEW_LINE DEDENT if ( arr [ mid ] == arr [ 0 ] + mid * diff ) : NEW_LINE INDENT return findMissingUtil ( arr , mid + 1 , high , diff ) ; NEW_LINE DEDENT return findMissingUtil ( arr , low , mid - 1 , diff ) ; NEW_LINE DEDENT def findMissing ( arr , n ) : NEW_LINE INDENT diff = int ( ( arr [ n - 1 ] - arr [ 0 ] ) / n ) ; NEW_LINE return findMissingUtil ( arr , 0 , n - 1 , diff ) ; NEW_LINE DEDENT arr = [ 2 , 4 , 8 , 10 , 12 , 14 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( " The ▁ missing ▁ element ▁ is " , findMissing ( arr , n ) ) ; NEW_LINE


import java . util . * ; class GFG { static String findSum ( String str1 , String str2 ) { if ( str1 . length ( ) > str2 . length ( ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; int diff = n2 - n1 ; int carry = 0 ; for ( int i = n1 - 1 ; i >= 0 ; i -- ) { int sum = ( ( int ) ( str1 . charAt ( i ) - '0' ) + ( int ) ( str2 . charAt ( i + diff ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } for ( int i = n2 - n1 - 1 ; i >= 0 ; i -- ) { int sum = ( ( int ) ( str2 . charAt ( i ) - '0' ) + carry ) ; str += ( char ) ( sum % 10 + '0' ) ; carry = sum / 10 ; } if ( carry > 0 ) str += ( char ) ( carry + '0' ) ; return new StringBuilder ( str ) . reverse ( ) . toString ( ) ; } public static void main ( String [ ] args ) { String str1 = "12" ; String str2 = "198111" ; System . out . println ( findSum ( str1 , str2 ) ) ; } }

def findSum ( str1 , str2 ) : NEW_LINE INDENT if len ( str1 ) > len ( str2 ) : NEW_LINE INDENT temp = str1 NEW_LINE str1 = str2 NEW_LINE str2 = temp NEW_LINE DEDENT str3 = " " NEW_LINE n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE diff = n2 - n1 NEW_LINE carry = 0 NEW_LINE for i in range ( n1 - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum = ( ( ord ( str1 [ i ] ) - ord ( '0' ) ) + int ( ( ord ( str2 [ i + diff ] ) - ord ( '0' ) ) ) + carry ) NEW_LINE str3 = str3 + str ( sum % 10 ) NEW_LINE carry = sum // 10 NEW_LINE DEDENT for i in range ( n2 - n1 - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum = ( ( ord ( str2 [ i ] ) - ord ( '0' ) ) + carry ) NEW_LINE str3 = str3 + str ( sum % 10 ) NEW_LINE carry = sum // 10 NEW_LINE DEDENT if ( carry ) : NEW_LINE INDENT str3 + str ( carry + '0' ) NEW_LINE DEDENT str3 = str3 [ : : - 1 ] NEW_LINE return str3 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = "12" NEW_LINE str2 = "198111" NEW_LINE print ( findSum ( str1 , str2 ) ) NEW_LINE DEDENT


class GFG { static int MAX = 11 ; static boolean isMultipleof5 ( int n ) { char str [ ] = new char [ MAX ] ; int len = str . length ; if ( str [ len - 1 ] == '5' || str [ len - 1 ] == '0' ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 19 ; if ( isMultipleof5 ( n ) == true ) System . out . println ( n + " ▁ is ▁ multiple ▁ " + " of ▁ 5" ) ; else System . out . println ( n + " ▁ is ▁ not ▁ a ▁ " + " multiple ▁ of ▁ 5" ) ; } }

MAX = 11 ; NEW_LINE def isMultipleof5 ( n ) : NEW_LINE INDENT s = str ( n ) ; NEW_LINE l = len ( s ) ; NEW_LINE if ( s [ l - 1 ] == '5' or s [ l - 1 ] == '0' ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT n = 19 ; NEW_LINE if ( isMultipleof5 ( n ) == True ) : NEW_LINE INDENT print ( n , " is ▁ multiple ▁ of ▁ 5" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( n , " is ▁ not ▁ a ▁ multiple ▁ of ▁ 5" ) ; NEW_LINE DEDENT


class GfG { static void breakLine ( ) { System . out . print ( " \n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \n " ) ; } static int MAX = 10 ; static int arr [ ] = new int [ MAX ] , no ; static void nQueens ( int k , int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( canPlace ( k , i ) ) { arr [ k ] = i ; if ( k == n ) { display ( n ) ; } else { nQueens ( k + 1 , n ) ; } } } } static boolean canPlace ( int k , int i ) { for ( int j = 1 ; j <= k - 1 ; j ++ ) { if ( arr [ j ] == i || ( Math . abs ( arr [ j ] - i ) == Math . abs ( j - k ) ) ) { return false ; } } return true ; } static void display ( int n ) { breakLine ( ) ; System . out . print ( " Arrangement ▁ No . ▁ " + ++ no ) ; breakLine ( ) ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( arr [ i ] != j ) { System . out . print ( " \t _ " ) ; } else { System . out . print ( " \tQ " ) ; } } System . out . println ( " " ) ; } breakLine ( ) ; } public static void main ( String [ ] args ) { int n = 4 ; nQueens ( 1 , n ) ; } }

class GfG : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . MAX = 10 NEW_LINE self . arr = [ 0 ] * self . MAX NEW_LINE self . no = 0 NEW_LINE DEDENT def breakLine ( self ) : NEW_LINE INDENT print ( " \n - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - " ) NEW_LINE DEDENT def canPlace ( self , k , i ) : NEW_LINE INDENT for j in range ( 1 , k ) : NEW_LINE INDENT if ( self . arr [ j ] == i or ( abs ( self . arr [ j ] - i ) == abs ( j - k ) ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def display ( self , n ) : NEW_LINE INDENT self . breakLine ( ) NEW_LINE self . no += 1 NEW_LINE print ( " Arrangement ▁ No . " , self . no , end = " ▁ " ) NEW_LINE self . breakLine ( ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if self . arr [ i ] != j : NEW_LINE INDENT print ( " \t _ " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " \tQ " , end = " ▁ " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT self . breakLine ( ) NEW_LINE DEDENT def nQueens ( self , k , n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if self . canPlace ( k , i ) : NEW_LINE INDENT self . arr [ k ] = i NEW_LINE if k == n : NEW_LINE INDENT self . display ( n ) NEW_LINE DEDENT else : NEW_LINE INDENT self . nQueens ( k + 1 , n ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE obj = GfG ( ) NEW_LINE obj . nQueens ( 1 , n ) NEW_LINE DEDENT


import java . util . Scanner ; public class GFG { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int a , b , i , j , flag ; System . out . printf ( " Enter ▁ lower ▁ bound ▁ of ▁ the ▁ interval : ▁ " ) ; a = sc . nextInt ( ) ; System . out . printf ( " \n Enter ▁ upper ▁ bound ▁ of ▁ the ▁ interval : ▁ " ) ; b = sc . nextInt ( ) ; System . out . printf ( " \n Prime ▁ numbers ▁ between ▁ % d ▁ and ▁ % d ▁ are : ▁ " , a , b ) ; for ( i = a ; i <= b ; i ++ ) { if ( i == 1 || i == 0 ) continue ; flag = 1 ; for ( j = 2 ; j <= i / 2 ; ++ j ) { if ( i % j == 0 ) { flag = 0 ; break ; } } if ( flag == 1 ) System . out . println ( i ) ; } } }

if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b , i , j , flag = 0 , 0 , 0 , 0 , 0 NEW_LINE print ( " Enter ▁ lower ▁ bound ▁ of ▁ the ▁ interval : " , end = " " ) NEW_LINE a = int ( input ( ) ) NEW_LINE print ( a ) NEW_LINE print ( " Enter ▁ upper ▁ bound ▁ of ▁ the ▁ interval : " , end = " " ) NEW_LINE b = int ( input ( ) ) NEW_LINE print ( b ) NEW_LINE print ( " Prime ▁ numbers ▁ between " , a , " and " , b , " are : " , end = " " ) NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT if ( i == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT flag = 1 NEW_LINE for j in range ( 2 , i // 2 + 1 ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT


import java . util . * ; class GFG { static final int N = 100005 ; static void createHash ( HashSet < Integer > hash , int maxElement ) { int prev = 0 , curr = 1 ; hash . add ( prev ) ; hash . add ( curr ) ; while ( curr <= maxElement ) { int temp = curr + prev ; hash . add ( temp ) ; prev = curr ; curr = temp ; } } static int longestFibonacciSubsequence ( int arr [ ] , int n ) { HashSet < Integer > hash = new HashSet < Integer > ( ) ; createHash ( hash , Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ) ; int answer = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hash . contains ( arr [ i ] ) ) { answer ++ ; } } return answer ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 11 , 2 , 9 , 21 } ; int n = arr . length ; System . out . print ( longestFibonacciSubsequence ( arr , n ) + " \n " ) ; } }

N = 100005 NEW_LINE def createHash ( hash , maxElement ) : NEW_LINE INDENT prev = 0 NEW_LINE curr = 1 NEW_LINE hash . add ( prev ) NEW_LINE hash . add ( curr ) NEW_LINE while ( curr <= maxElement ) : NEW_LINE INDENT temp = curr + prev NEW_LINE hash . add ( temp ) NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE DEDENT DEDENT def longestFibonacciSubsequence ( arr , n ) : NEW_LINE INDENT hash = set ( ) NEW_LINE createHash ( hash , max ( arr ) ) NEW_LINE answer = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] in hash ) : NEW_LINE INDENT answer += 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 4 , 11 , 2 , 9 , 21 ] NEW_LINE n = len ( arr ) NEW_LINE print ( longestFibonacciSubsequence ( arr , n ) ) NEW_LINE DEDENT


class GFG { static int countConsecutive ( String s ) { int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) { i ++ ; } else { count ++ ; } } return count ; } public static void main ( String args [ ] ) { String str = "44522255" ; System . out . println ( countConsecutive ( str ) ) ; } }

def countConsecutive ( s ) : NEW_LINE INDENT count = - 1 NEW_LINE for i in range ( len ( s ) - 1 ) : NEW_LINE INDENT if ( i <= len ( s ) ) : NEW_LINE INDENT if ( s [ i ] is s [ i + 1 ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "44522255" NEW_LINE print ( countConsecutive ( str ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void sortArray ( int a [ ] , int n ) { Vector < Integer > ans = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) ans . add ( a [ i ] ) ; } Collections . sort ( ans ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] >= 0 ) { a [ i ] = ans . get ( j ) ; j ++ ; } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 6 , - 3 , 8 , 4 , 1 } ; int n = arr . length ; sortArray ( arr , n ) ; } }

def sortArray ( a , n ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] >= 0 ) : NEW_LINE INDENT ans . append ( a [ i ] ) NEW_LINE DEDENT DEDENT ans = sorted ( ans ) NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] >= 0 ) : NEW_LINE INDENT a [ i ] = ans [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , - 6 , - 3 , 8 , 4 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE sortArray ( arr , n ) NEW_LINE


class GFG { static boolean checkSubarraySum ( int arr [ ] , int n , int k , int sum ) { for ( int i = 0 ; i < n - k + 1 ; i ++ ) { int current_sum = 0 ; for ( int j = 0 ; j < k ; j ++ ) current_sum = current_sum + arr [ i + j ] ; if ( current_sum == sum ) return true ; } return false ; } public static void main ( String args [ ] ) { int arr [ ] = new int [ ] { 1 , 4 , 2 , 10 , 2 , 3 , 1 , 0 , 20 } ; int k = 4 ; int sum = 18 ; int n = arr . length ; if ( checkSubarraySum ( arr , n , k , sum ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def checkSubarraySum ( arr , n , k , sum ) : NEW_LINE INDENT for i in range ( n - k + 1 ) : NEW_LINE INDENT current_sum = 0 ; NEW_LINE for j in range ( k ) : NEW_LINE INDENT current_sum = ( current_sum + arr [ i + j ] ) ; NEW_LINE DEDENT if ( current_sum == sum ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT arr = [ 1 , 4 , 2 , 10 , 2 , 3 , 1 , 0 , 20 ] ; NEW_LINE k = 4 ; NEW_LINE sum = 18 ; NEW_LINE n = len ( arr ) ; NEW_LINE if ( checkSubarraySum ( arr , n , k , sum ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT


class GFG { static boolean isPossible ( int arr [ ] , int n , int dist , int k ) { int req = 0 ; int curr = 0 ; int prev = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( curr != n && arr [ curr ] - arr [ prev ] <= dist ) { curr ++ ; } req ++ ; if ( curr == n ) { break ; } prev = curr - 1 ; } if ( curr != n ) { return false ; } if ( req <= k ) { return true ; } return false ; } static int minDistance ( int arr [ ] , int n , int k ) { int l = 0 ; int h = arr [ n - 1 ] ; int ans = 0 ; while ( l <= h ) { int m = ( l + h ) / 2 ; if ( isPossible ( arr , n , m , k ) ) { ans = m ; h = m - 1 ; } else { l = m + 1 ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 15 , 36 , 43 } ; int n = arr . length ; int k = 2 ; System . out . println ( minDistance ( arr , n , k ) ) ; } }

def isPossible ( arr , n , dist , k ) : NEW_LINE INDENT req = 0 NEW_LINE curr = 0 NEW_LINE prev = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT while ( curr != n and ( arr [ curr ] - arr [ prev ] ) <= dist ) : NEW_LINE INDENT curr = curr + 1 NEW_LINE DEDENT req = req + 1 NEW_LINE if ( curr == n ) : NEW_LINE INDENT break NEW_LINE DEDENT prev = curr - 1 NEW_LINE DEDENT if ( curr != n ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( req <= k ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def minDistance ( arr , n , k ) : NEW_LINE INDENT l = 0 NEW_LINE h = arr [ n - 1 ] NEW_LINE ans = 0 NEW_LINE while ( l <= h ) : NEW_LINE INDENT m = ( l + h ) // 2 ; NEW_LINE if ( isPossible ( arr , n , m , k ) ) : NEW_LINE INDENT ans = m NEW_LINE h = m - 1 NEW_LINE DEDENT else : NEW_LINE INDENT l = m + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 15 , 36 , 43 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( minDistance ( arr , n , k ) ) NEW_LINE


import java . util . * ; class GFG { static final int MAX = 10000 ; static int prefix [ ] = new int [ MAX + 1 ] ; static void buildPrefix ( ) { boolean prime [ ] = new boolean [ MAX + 1 ] ; Arrays . fill ( prime , true ) ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } prefix [ 0 ] = prefix [ 1 ] = 0 ; for ( int p = 2 ; p <= MAX ; p ++ ) { prefix [ p ] = prefix [ p - 1 ] ; if ( prime [ p ] ) prefix [ p ] ++ ; } } static int query ( int L , int R ) { return prefix [ R ] - prefix [ L - 1 ] ; } public static void main ( String [ ] args ) { buildPrefix ( ) ; int L = 5 , R = 10 ; System . out . println ( query ( L , R ) ) ; L = 1 ; R = 10 ; System . out . println ( query ( L , R ) ) ; } }

MAX = 10000 NEW_LINE prefix = [ 0 ] * ( MAX + 1 ) NEW_LINE def buildPrefix ( ) : NEW_LINE INDENT prime = [ 1 ] * ( MAX + 1 ) NEW_LINE p = 2 NEW_LINE while ( p * p <= MAX ) : NEW_LINE INDENT if ( prime [ p ] == 1 ) : NEW_LINE INDENT i = p * 2 NEW_LINE while ( i <= MAX ) : NEW_LINE INDENT prime [ i ] = 0 NEW_LINE i += p NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , MAX + 1 ) : NEW_LINE INDENT prefix [ p ] = prefix [ p - 1 ] NEW_LINE if ( prime [ p ] == 1 ) : NEW_LINE INDENT prefix [ p ] += 1 NEW_LINE DEDENT DEDENT DEDENT def query ( L , R ) : NEW_LINE INDENT return prefix [ R ] - prefix [ L - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT buildPrefix ( ) NEW_LINE L = 5 NEW_LINE R = 10 NEW_LINE print ( query ( L , R ) ) NEW_LINE L = 1 NEW_LINE R = 10 NEW_LINE print ( query ( L , R ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 100000 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; static void sieve ( ) { boolean [ ] isComposite = new boolean [ MAX + 1 ] ; for ( int i = 2 ; i * i <= MAX ; i ++ ) { if ( isComposite [ i ] == false ) for ( int j = 2 ; j * i <= MAX ; j ++ ) isComposite [ i * j ] = true ; } for ( int i = 2 ; i <= MAX ; i ++ ) if ( isComposite [ i ] == false ) primes . add ( i ) ; } static long LCM ( int n ) { long lcm = 1 ; for ( int i = 0 ; i < primes . size ( ) && primes . get ( i ) <= n ; i ++ ) { int pp = primes . get ( i ) ; while ( pp * primes . get ( i ) <= n ) pp = pp * primes . get ( i ) ; lcm *= pp ; lcm %= 1000000007 ; } return lcm ; } public static void main ( String [ ] args ) { sieve ( ) ; int N = 7 ; System . out . println ( LCM ( N ) ) ; } }

MAX = 100000 ; NEW_LINE primes = [ ] ; NEW_LINE def sieve ( ) : NEW_LINE INDENT isComposite = [ False ] * ( MAX + 1 ) ; NEW_LINE i = 2 ; NEW_LINE while ( i * i <= MAX ) : NEW_LINE INDENT if ( isComposite [ i ] == False ) : NEW_LINE INDENT j = 2 ; NEW_LINE while ( j * i <= MAX ) : NEW_LINE INDENT isComposite [ i * j ] = True ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if ( isComposite [ i ] == False ) : NEW_LINE INDENT primes . append ( i ) ; NEW_LINE DEDENT DEDENT DEDENT def LCM ( n ) : NEW_LINE INDENT lcm = 1 ; NEW_LINE i = 0 ; NEW_LINE while ( i < len ( primes ) and primes [ i ] <= n ) : NEW_LINE INDENT pp = primes [ i ] ; NEW_LINE while ( pp * primes [ i ] <= n ) : NEW_LINE INDENT pp = pp * primes [ i ] ; NEW_LINE DEDENT lcm *= pp ; NEW_LINE lcm %= 1000000007 ; NEW_LINE i += 1 ; NEW_LINE DEDENT return lcm ; NEW_LINE DEDENT sieve ( ) ; NEW_LINE N = 7 ; NEW_LINE print ( LCM ( N ) ) ; NEW_LINE


import java . io . * ; class GFG { static void getElements ( int a , int arr [ ] , int n ) { int elements [ ] = new int [ n + 1 ] ; elements [ 0 ] = a ; for ( int i = 0 ; i < n ; i ++ ) { elements [ i + 1 ] = arr [ i ] ^ elements [ i ] ; } for ( int i = 0 ; i < n + 1 ; i ++ ) System . out . print ( elements [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 13 , 2 , 6 , 1 } ; int n = arr . length ; int a = 5 ; getElements ( a , arr , n ) ; } }

def getElements ( a , arr , n ) : NEW_LINE INDENT elements = [ 1 for i in range ( n + 1 ) ] NEW_LINE elements [ 0 ] = a NEW_LINE for i in range ( n ) : NEW_LINE INDENT elements [ i + 1 ] = arr [ i ] ^ elements [ i ] NEW_LINE DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT print ( elements [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 13 , 2 , 6 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE a = 5 NEW_LINE getElements ( a , arr , n ) NEW_LINE


class GFG { static int N = 1000005 ; static void sumOddDigit ( int digitSum [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { digitSum [ i ] = digitSum [ i / 10 ] + ( i & 1 ) * ( i % 10 ) ; } } static void sumFactor ( int digitSum [ ] , int factorDigitSum [ ] ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { factorDigitSum [ j ] += digitSum [ i ] ; } } } static void wrapper ( int q , int n [ ] ) { int digitSum [ ] = new int [ N ] ; int factorDigitSum [ ] = new int [ N ] ; sumOddDigit ( digitSum ) ; sumFactor ( digitSum , factorDigitSum ) ; for ( int i = 0 ; i < q ; i ++ ) System . out . print ( factorDigitSum [ n [ i ] ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int q = 2 ; int n [ ] = new int [ ] { 10 , 36 } ; wrapper ( q , n ) ; } }

N = 100 NEW_LINE digitSum = [ 0 ] * N NEW_LINE factorDigitSum = [ 0 ] * N NEW_LINE def sumOddDigit ( ) : NEW_LINE INDENT global N , digitSum , factorDigitSum NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT digitSum [ i ] = ( digitSum [ int ( i / 10 ) ] + int ( i & 1 ) * ( i % 10 ) ) NEW_LINE DEDENT DEDENT def sumFactor ( ) : NEW_LINE INDENT global N , digitSum , factorDigitSum NEW_LINE j = 0 NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT j = i NEW_LINE while ( j < N ) : NEW_LINE INDENT factorDigitSum [ j ] = ( factorDigitSum [ j ] + digitSum [ i ] ) NEW_LINE j = j + i NEW_LINE DEDENT DEDENT DEDENT def wrapper ( q , n ) : NEW_LINE INDENT global N , digitSum , factorDigitSum NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT digitSum [ i ] = 0 NEW_LINE factorDigitSum [ i ] = 0 NEW_LINE DEDENT sumOddDigit ( ) NEW_LINE sumFactor ( ) NEW_LINE for i in range ( 0 , q ) : NEW_LINE INDENT print ( " { } ▁ " . format ( factorDigitSum [ n [ i ] ] ) , end = " " ) NEW_LINE DEDENT DEDENT q = 2 NEW_LINE n = [ 10 , 36 ] NEW_LINE wrapper ( q , n ) NEW_LINE


import java . lang . * ; class GFG { static double PI = 3.14159265 ; public static double area_leaf ( double a ) { return ( a * a * ( PI / 2 - 1 ) ) ; } public static void main ( String [ ] args ) { double a = 7 ; System . out . println ( area_leaf ( a ) ) ; } }

PI = 3.14159265 NEW_LINE def area_leaf ( a ) : NEW_LINE INDENT return ( a * a * ( PI / 2 - 1 ) ) NEW_LINE DEDENT a = 7 NEW_LINE print ( area_leaf ( a ) ) NEW_LINE


class Arraysum { static void pairwiseSum ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i + 1 < n ; i ++ ) { sum = arr [ i ] + arr [ i + 1 ] ; System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 10 , 15 , 5 , 6 } ; int n = arr . length ; pairwiseSum ( arr , n ) ; } }

def pairwiseSum ( lst , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( len ( lst ) - 1 ) : NEW_LINE INDENT sum = lst [ i ] + lst [ i + 1 ] NEW_LINE print ( sum , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 4 , 10 , 15 , 5 , 6 ] NEW_LINE size = len ( arr ) NEW_LINE pairwiseSum ( arr , size ) NEW_LINE


class GFG { static int N = 10 ; static int findMinimumDeletion ( int l , int r , int dp [ ] [ ] , String s ) { if ( l > r ) { return 0 ; } if ( l == r ) { return 1 ; } if ( dp [ l ] [ r ] != - 1 ) { return dp [ l ] [ r ] ; } int res = 1 + findMinimumDeletion ( l + 1 , r , dp , s ) ; for ( int i = l + 1 ; i <= r ; ++ i ) { if ( s . charAt ( l ) == s . charAt ( i ) ) { res = Math . min ( res , findMinimumDeletion ( l + 1 , i - 1 , dp , s ) + findMinimumDeletion ( i , r , dp , s ) ) ; } } return dp [ l ] [ r ] = res ; } public static void main ( String [ ] args ) { String s = " abcddcba " ; int n = s . length ( ) ; int dp [ ] [ ] = new int [ N ] [ N ] ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . println ( findMinimumDeletion ( 0 , n - 1 , dp , s ) ) ; } }

def findMinimumDeletion ( l , r , dp , s ) : NEW_LINE INDENT if l > r : NEW_LINE INDENT return 0 NEW_LINE DEDENT if l == r : NEW_LINE INDENT return 1 NEW_LINE DEDENT if dp [ l ] [ r ] != - 1 : NEW_LINE INDENT return dp [ l ] [ r ] NEW_LINE DEDENT res = 1 + findMinimumDeletion ( l + 1 , r , dp , s ) NEW_LINE for i in range ( l + 1 , r + 1 ) : NEW_LINE INDENT if s [ l ] == s [ i ] : NEW_LINE INDENT res = min ( res , findMinimumDeletion ( l + 1 , i - 1 , dp , s ) + findMinimumDeletion ( i , r , dp , s ) ) NEW_LINE DEDENT DEDENT dp [ l ] [ r ] = res NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abcddcba " NEW_LINE n = len ( s ) NEW_LINE N = 10 NEW_LINE dp = [ [ - 1 for i in range ( N ) ] for j in range ( N ) ] NEW_LINE print ( findMinimumDeletion ( 0 , n - 1 , dp , s ) ) NEW_LINE DEDENT


class GFG { static void printArray ( int N , int arr [ ] ) { for ( int i = 0 ; i < N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } static void replacedArray ( int N , int arr [ ] ) { int pos_sum , neg_sum , i , j , diff ; pos_sum = 0 ; neg_sum = 0 ; for ( i = N - 1 ; i >= 0 ; i -- ) { diff = Math . abs ( pos_sum ) - Math . abs ( neg_sum ) ; if ( arr [ i ] > 0 ) pos_sum += arr [ i ] ; else neg_sum += arr [ i ] ; arr [ i ] = Math . abs ( diff ) ; } } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 1 , - 1 , 2 , 3 , - 2 } ; replacedArray ( N , arr ) ; printArray ( N , arr ) ; N = 6 ; int arr1 [ ] = { - 3 , - 4 , - 2 , 5 , 1 , - 2 } ; replacedArray ( N , arr1 ) ; printArray ( N , arr1 ) ; } }

def printArray ( N , arr ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def replacedArray ( N , arr ) : NEW_LINE INDENT pos_sum = 0 NEW_LINE neg_sum = 0 NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT diff = abs ( pos_sum ) - abs ( neg_sum ) NEW_LINE if ( arr [ i ] > 0 ) : NEW_LINE INDENT pos_sum = pos_sum + arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT neg_sum = neg_sum + arr [ i ] NEW_LINE DEDENT arr [ i ] = abs ( diff ) NEW_LINE DEDENT DEDENT N = 5 NEW_LINE arr = [ 1 , - 1 , 2 , 3 , - 2 ] NEW_LINE replacedArray ( N , arr ) NEW_LINE printArray ( N , arr ) NEW_LINE N = 6 NEW_LINE arr1 = [ - 3 , - 4 , - 2 , 5 , 1 , - 2 ] NEW_LINE replacedArray ( N , arr1 ) NEW_LINE printArray ( N , arr1 ) NEW_LINE


class GFG { static int pairs ( int n ) { int [ ] [ ] c = new int [ 10 ] [ 10 ] ; int tmp = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i >= tmp * 10 ) tmp *= 10 ; c [ i / tmp ] [ i % 10 ] ++ ; } int ans = 0 ; for ( int i = 1 ; i < 10 ; i ++ ) for ( int j = 1 ; j < 10 ; j ++ ) ans += c [ i ] [ j ] * c [ j ] [ i ] ; return ans ; } public static void main ( String [ ] args ) { int n = 25 ; System . out . print ( pairs ( n ) ) ; } }

def pairs ( n ) : NEW_LINE INDENT c = [ [ 0 for i in range ( 10 ) ] for i in range ( 10 ) ] NEW_LINE tmp = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i >= tmp * 10 ) : NEW_LINE INDENT tmp *= 10 NEW_LINE DEDENT c [ i // tmp ] [ i % 10 ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( 1 , 10 ) : NEW_LINE INDENT for j in range ( 1 , 10 ) : NEW_LINE INDENT ans += c [ i ] [ j ] * c [ j ] [ i ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 25 NEW_LINE print ( pairs ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int totalways ( int [ ] arr , int n ) { HashMap < Integer , Integer > cnt = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( cnt . containsKey ( arr [ i ] ) ) { cnt . put ( arr [ i ] , cnt . get ( arr [ i ] ) + 1 ) ; } else { cnt . put ( arr [ i ] , 1 ) ; } } if ( n % 2 == 1 ) { int start = 0 , endd = n - 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { if ( i == 0 ) { if ( cnt . get ( i ) != 1 ) { return 0 ; } } else { if ( cnt . get ( i ) != 2 ) { return 0 ; } } } int ways = 1 ; start = 2 ; endd = n - 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } else if ( n % 2 == 0 ) { int start = 1 , endd = n - 1 ; for ( int i = 1 ; i <= endd ; i = i + 2 ) { if ( cnt . get ( i ) != 2 ) return 0 ; } int ways = 1 ; for ( int i = start ; i <= endd ; i = i + 2 ) { ways = ways * 2 ; } return ways ; } return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int N = 5 ; int [ ] arr = { 2 , 4 , 4 , 0 , 2 } ; System . out . println ( totalways ( arr , N ) ) ; } }

def totalways ( arr , n ) : NEW_LINE INDENT cnt = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt [ arr [ i ] ] = cnt . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT if ( n % 2 == 1 ) : NEW_LINE INDENT start , endd = 0 , n - 1 NEW_LINE for i in range ( start , endd + 1 , 2 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT if ( cnt [ i ] != 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( cnt [ i ] != 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT ways = 1 NEW_LINE start = 2 NEW_LINE endd = n - 1 NEW_LINE for i in range ( start , endd + 1 , 2 ) : NEW_LINE INDENT ways = ways * 2 NEW_LINE DEDENT return ways NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT start = 1 NEW_LINE endd = n - 1 NEW_LINE for i in range ( 1 , endd + 1 , 2 ) : NEW_LINE INDENT if ( cnt [ i ] != 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT ways = 1 NEW_LINE for i in range ( start , endd + 1 , 2 ) : NEW_LINE INDENT ways = ways * 2 NEW_LINE DEDENT return ways NEW_LINE DEDENT DEDENT N = 5 NEW_LINE arr = [ 2 , 4 , 4 , 0 , 2 ] NEW_LINE print ( totalways ( arr , N ) ) NEW_LINE


import java . io . * ; class GFG { static int longestsubarray ( int arr [ ] , int n , int k ) { int current_count = 0 ; int max_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) current_count ++ ; else current_count = 0 ; max_count = Math . max ( current_count , max_count ) ; } return max_count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 5 , 11 , 32 , 64 , 88 } ; int n = arr . length ; int k = 8 ; System . out . println ( longestsubarray ( arr , n , k ) ) ; } }

def longestsubarray ( arr , n , k ) : NEW_LINE INDENT current_count = 0 NEW_LINE max_count = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT current_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_count = 0 NEW_LINE DEDENT max_count = max ( current_count , max_count ) NEW_LINE DEDENT return max_count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 5 , 11 , 32 , 64 , 88 ] NEW_LINE n = len ( arr ) NEW_LINE k = 8 NEW_LINE print ( longestsubarray ( arr , n , k ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 1000001 ; static int c , n , m , a , b ; static void dfs ( int a , int b , Vector < Integer > v [ ] , int vis [ ] ) { vis [ a ] = 1 ; c ++ ; for ( int i : v [ a ] ) { if ( vis [ i ] == 0 && i != b ) dfs ( i , b , v , vis ) ; } } static void Calculate ( Vector < Integer > v [ ] ) { int [ ] vis = new int [ n + 1 ] ; Arrays . fill ( vis , 0 ) ; c = 0 ; dfs ( a , b , v , vis ) ; int ans1 = n - c - 1 ; Arrays . fill ( vis , 0 ) ; c = 0 ; dfs ( b , a , v , vis ) ; int ans2 = n - c - 1 ; System . out . print ( ans1 * ans2 + " \n " ) ; } public static void main ( String [ ] args ) { n = 7 ; m = 7 ; a = 3 ; b = 5 ; int edges [ ] [ ] = { { 1 , 2 } , { 2 , 3 } , { 3 , 4 } , { 4 , 5 } , { 5 , 6 } , { 6 , 7 } , { 7 , 5 } } ; Vector < Integer > [ ] v = new Vector [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { v [ i ] = new Vector < Integer > ( ) ; } for ( int i = 0 ; i < m ; i ++ ) { v [ edges [ i ] [ 0 ] ] . add ( edges [ i ] [ 1 ] ) ; v [ edges [ i ] [ 1 ] ] . add ( edges [ i ] [ 0 ] ) ; } Calculate ( v ) ; } }

N = 1000001 NEW_LINE c = 0 NEW_LINE n = 0 NEW_LINE m = 0 NEW_LINE a = 0 NEW_LINE b = 0 NEW_LINE def dfs ( a , b , v , vis ) : NEW_LINE INDENT global c NEW_LINE vis [ a ] = 1 NEW_LINE c += 1 NEW_LINE for i in v [ a ] : NEW_LINE INDENT if ( vis [ i ] == 0 and i != b ) : NEW_LINE INDENT dfs ( i , b , v , vis ) NEW_LINE DEDENT DEDENT DEDENT def Calculate ( v ) : NEW_LINE INDENT global c NEW_LINE vis = [ 0 for i in range ( n + 1 ) ] NEW_LINE c = 0 NEW_LINE dfs ( a , b , v , vis ) NEW_LINE ans1 = n - c - 1 NEW_LINE vis = [ 0 for i in range ( len ( vis ) ) ] NEW_LINE c = 0 NEW_LINE dfs ( b , a , v , vis ) NEW_LINE ans2 = n - c - 1 NEW_LINE print ( ans1 * ans2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 NEW_LINE m = 7 NEW_LINE a = 3 NEW_LINE b = 5 NEW_LINE edges = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 4 ] , [ 4 , 5 ] , [ 5 , 6 ] , [ 6 , 7 ] , [ 7 , 5 ] ] NEW_LINE v = [ [ ] for i in range ( n + 1 ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT v [ edges [ i ] [ 0 ] ] . append ( edges [ i ] [ 1 ] ) NEW_LINE v [ edges [ i ] [ 1 ] ] . append ( edges [ i ] [ 0 ] ) NEW_LINE DEDENT Calculate ( v ) NEW_LINE DEDENT


import java . util . * ; class GFG { static long mod = 1000000007 ; static long inv2 = 500000004 ; static long modulo ( String num ) { long res = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) res = ( res * 10 + ( long ) num . charAt ( i ) - '0' ) % mod ; return res ; } static long findSum ( String L , String R ) { long a , b , l , r , ret ; a = modulo ( L ) ; b = modulo ( R ) ; l = ( ( a * ( a - 1 ) ) % mod * inv2 ) % mod ; r = ( ( b * ( b + 1 ) ) % mod * inv2 ) % mod ; ret = ( r % mod - l % mod ) ; if ( ret < 0 ) ret = ret + mod ; else ret = ret % mod ; return ret ; } public static void main ( String [ ] args ) { String L = "88949273204" ; String R = "98429729474298592" ; System . out . println ( findSum ( L , R ) ) ; } }

mod = 1000000007 NEW_LINE inv2 = 500000004 ; NEW_LINE def modulo ( num ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( len ( num ) ) : NEW_LINE INDENT res = ( res * 10 + int ( num [ i ] ) - 0 ) % mod ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def findSum ( L , R ) : NEW_LINE INDENT a = modulo ( L ) ; NEW_LINE b = modulo ( R ) ; NEW_LINE l = ( ( a * ( a - 1 ) ) % mod * inv2 ) % mod ; NEW_LINE r = ( ( b * ( b + 1 ) ) % mod * inv2 ) % mod ; NEW_LINE ret = ( r % mod - l % mod ) ; NEW_LINE if ( ret < 0 ) : NEW_LINE INDENT ret = ret + mod ; NEW_LINE DEDENT else : NEW_LINE INDENT ret = ret % mod ; NEW_LINE DEDENT return ret ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = "88949273204" ; NEW_LINE R = "98429729474298592" ; NEW_LINE print ( findSum ( L , R ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 100001 ; static void find_distinct ( int a [ ] , int n , int q , int queries [ ] ) { int [ ] check = new int [ MAX ] ; int [ ] idx = new int [ MAX ] ; int cnt = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( check [ a [ i ] ] == 0 ) { idx [ i ] = cnt ; check [ a [ i ] ] = 1 ; cnt ++ ; } else { idx [ i ] = cnt - 1 ; } } for ( int i = 0 ; i < q ; i ++ ) { int m = queries [ i ] ; System . out . print ( idx [ m ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 1 , 2 , 3 , 4 , 5 } ; int n = a . length ; int queries [ ] = { 0 , 3 , 5 , 7 } ; int q = queries . length ; find_distinct ( a , n , q , queries ) ; } }

MAX = 100001 ; NEW_LINE def find_distinct ( a , n , q , queries ) : NEW_LINE INDENT check = [ 0 ] * MAX ; NEW_LINE idx = [ 0 ] * MAX ; NEW_LINE cnt = 1 ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( check [ a [ i ] ] == 0 ) : NEW_LINE INDENT idx [ i ] = cnt ; NEW_LINE check [ a [ i ] ] = 1 ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT idx [ i ] = cnt - 1 ; NEW_LINE DEDENT DEDENT for i in range ( 0 , q ) : NEW_LINE INDENT m = queries [ i ] ; NEW_LINE print ( idx [ m ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT a = [ 1 , 2 , 3 , 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( a ) ; NEW_LINE queries = [ 0 , 3 , 5 , 7 ] ; NEW_LINE q = len ( queries ) ; NEW_LINE find_distinct ( a , n , q , queries ) ; NEW_LINE


class GFG { static int count ( int arr [ ] , int low , int high , int x ) { if ( ( low > high ) || ( low == high && arr [ low ] != x ) ) return 0 ; if ( low == high && arr [ low ] == x ) return 1 ; return count ( arr , low , ( low + high ) / 2 , x ) + count ( arr , 1 + ( low + high ) / 2 , high , x ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 30 , 1 , 42 , 5 , 56 , 3 , 56 , 9 } ; int n = arr . length ; int x = 56 ; System . out . print ( count ( arr , 0 , n - 1 , x ) ) ; } }

def count ( arr , low , high , x ) : NEW_LINE INDENT if ( ( low > high ) or ( low == high and arr [ low ] != x ) ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( low == high and arr [ low ] == x ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return count ( arr , low , ( low + high ) // 2 , x ) + \ NEW_LINE count ( arr , 1 + ( low + high ) // 2 , high , x ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 30 , 1 , 42 , 5 , 56 , 3 , 56 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE x = 56 ; NEW_LINE print ( count ( arr , 0 , n - 1 , x ) ) ; NEW_LINE DEDENT


import java . io . * ; import java . math . * ; class GFG { static int reachTarget ( int target ) { target = Math . abs ( target ) ; int sum = 0 , step = 0 ; while ( sum < target || ( sum - target ) % 2 != 0 ) { step ++ ; sum += step ; } return step ; } public static void main ( String args [ ] ) { int target = 5 ; System . out . println ( reachTarget ( target ) ) ; } }

def reachTarget ( target ) : NEW_LINE INDENT target = abs ( target ) NEW_LINE sum = 0 NEW_LINE step = 0 NEW_LINE while ( sum < target or ( sum - target ) % 2 != 0 ) : NEW_LINE INDENT step = step + 1 NEW_LINE sum = sum + step NEW_LINE DEDENT return step NEW_LINE DEDENT target = 5 NEW_LINE print ( reachTarget ( target ) ) NEW_LINE


import java . io . * ; class GFG { static void findTriplets ( int arr [ ] , int n , int sum ) { for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] + arr [ k ] == sum ) { System . out . println ( arr [ i ] + " ▁ " + arr [ j ] + " ▁ " + arr [ k ] ) ; } } } } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , - 1 , 2 , - 3 , 1 } ; int n = arr . length ; findTriplets ( arr , n , - 2 ) ; } }

def findTriplets ( arr , n , sum ) : NEW_LINE INDENT for i in range ( 0 , n - 2 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] + arr [ j ] + arr [ k ] == sum ) : NEW_LINE INDENT print ( arr [ i ] , " ▁ " , arr [ j ] , " ▁ " , arr [ k ] , sep = " " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT arr = [ 0 , - 1 , 2 , - 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE findTriplets ( arr , n , - 2 ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static int findCountUpto ( int d ) { int GP1_Sum = 9 * ( ( int ) ( ( Math . pow ( 10 , d ) ) - 1 ) / 9 ) ; int GP2_Sum = 9 * ( ( int ) ( ( Math . pow ( 9 , d ) ) - 1 ) / 8 ) ; return GP1_Sum - GP2_Sum ; } public static void main ( String args [ ] ) { int d = 1 ; System . out . println ( findCountUpto ( d ) ) ; d = 2 ; System . out . println ( findCountUpto ( d ) ) ; d = 4 ; System . out . println ( findCountUpto ( d ) ) ; } }

import math NEW_LINE def findCount ( d ) : NEW_LINE INDENT return 9 * ( pow ( 10 , d - 1 ) - pow ( 9 , d - 1 ) ) ; NEW_LINE DEDENT def findCountUpto ( d ) : NEW_LINE INDENT totalCount = 0 NEW_LINE for i in range ( 1 , d + 1 ) : NEW_LINE INDENT totalCount = totalCount + findCount ( i ) NEW_LINE DEDENT return totalCount NEW_LINE DEDENT d = 1 NEW_LINE print ( findCountUpto ( d ) ) NEW_LINE d = 2 NEW_LINE print ( findCountUpto ( d ) ) NEW_LINE d = 4 NEW_LINE print ( findCountUpto ( d ) ) NEW_LINE


class GFG { static void substringConversions ( String str , int k , int b ) { for ( int i = 0 ; i + k <= str . length ( ) ; i ++ ) { String sub = str . substring ( i , i + k ) ; int sum = 0 , counter = 0 ; for ( int j = sub . length ( ) - 1 ; j >= 0 ; j -- ) { sum = ( int ) ( sum + ( ( sub . charAt ( j ) - '0' ) * Math . pow ( b , counter ) ) ) ; counter ++ ; } System . out . print ( sum + " ▁ " ) ; } } public static void main ( String [ ] args ) { String str = "12212" ; int b = 3 , k = 3 ; substringConversions ( str , b , k ) ; } }

import math NEW_LINE def substringConversions ( s , k , b ) : NEW_LINE INDENT l = len ( s ) ; NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( ( i + k ) < l + 1 ) : NEW_LINE INDENT sub = s [ i : i + k ] ; NEW_LINE sum , counter = 0 , 0 ; NEW_LINE for i in range ( len ( sub ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum = sum + ( ( ord ( sub [ i ] ) - ord ( '0' ) ) * pow ( b , counter ) ) ; NEW_LINE counter += 1 ; NEW_LINE DEDENT print ( sum , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT s = "12212" ; NEW_LINE b , k = 3 , 3 ; NEW_LINE substringConversions ( s , b , k ) ; NEW_LINE


import java . util . * ; class GFG { static int round ( int n ) { int a = ( n / 10 ) * 10 ; int b = a + 10 ; return ( n - a > b - n ) ? b : a ; } public static void main ( String [ ] args ) { int n = 4722 ; System . out . println ( round ( n ) ) ; } }

def round ( n ) : NEW_LINE INDENT a = ( n // 10 ) * 10 NEW_LINE b = a + 10 NEW_LINE return ( b if n - a > b - n else a ) NEW_LINE DEDENT n = 4722 NEW_LINE print ( round ( n ) ) NEW_LINE


import java . util . Arrays ; import java . util . Random ; public class ReservoirSampling { static void selectKItems ( int stream [ ] , int n , int k ) { int i ; int reservoir [ ] = new int [ k ] ; for ( i = 0 ; i < k ; i ++ ) reservoir [ i ] = stream [ i ] ; Random r = new Random ( ) ; for ( ; i < n ; i ++ ) { int j = r . nextInt ( i + 1 ) ; if ( j < k ) reservoir [ j ] = stream [ i ] ; } System . out . println ( " Following ▁ are ▁ k ▁ randomly ▁ selected ▁ items " ) ; System . out . println ( Arrays . toString ( reservoir ) ) ; } public static void main ( String [ ] args ) { int stream [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 } ; int n = stream . length ; int k = 5 ; selectKItems ( stream , n , k ) ; } }

import random NEW_LINE def printArray ( stream , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( stream [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT def selectKItems ( stream , n , k ) : NEW_LINE INDENT i = 0 ; NEW_LINE reservoir = [ 0 ] * k ; NEW_LINE for i in range ( k ) : NEW_LINE INDENT reservoir [ i ] = stream [ i ] ; NEW_LINE DEDENT while ( i < n ) : NEW_LINE INDENT j = random . randrange ( i + 1 ) ; NEW_LINE if ( j < k ) : NEW_LINE INDENT reservoir [ j ] = stream [ i ] ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT print ( " Following ▁ are ▁ k ▁ randomly ▁ selected ▁ items " ) ; NEW_LINE printArray ( reservoir , k ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT stream = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ] ; NEW_LINE n = len ( stream ) ; NEW_LINE k = 5 ; NEW_LINE selectKItems ( stream , n , k ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int getTotalXorOfSubarrayXors ( int arr [ ] , int N ) { if ( N % 2 == 0 ) return 0 ; int res = 0 ; for ( int i = 0 ; i < N ; i += 2 ) res ^= arr [ i ] ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 2 , 4 , 6 } ; int N = arr . length ; System . out . println ( getTotalXorOfSubarrayXors ( arr , N ) ) ; } }

def getTotalXorOfSubarrayXors ( arr , N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( 0 , N , 2 ) : NEW_LINE INDENT res ^= arr [ i ] NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , 2 , 4 , 6 ] NEW_LINE N = len ( arr ) NEW_LINE print ( getTotalXorOfSubarrayXors ( arr , N ) ) NEW_LINE DEDENT


class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; static Node prev ; static boolean isBSTUtil ( Node root ) { if ( root != null ) { if ( ! isBSTUtil ( root . left ) ) return false ; if ( prev != null && root . data <= prev . data ) return false ; prev = root ; return isBSTUtil ( root . right ) ; } return true ; } static boolean isBST ( Node root ) { return isBSTUtil ( root ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 3 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 5 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 4 ) ; if ( isBST ( root ) ) System . out . print ( " Is ▁ BST " ) ; else System . out . print ( " Not ▁ a ▁ BST " ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isBSTUtil ( root , prev ) : NEW_LINE INDENT if ( root != None ) : NEW_LINE INDENT if ( isBSTUtil ( root . left , prev ) == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( prev != None and root . data <= prev . data ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev = root NEW_LINE return isBSTUtil ( root . right , prev ) NEW_LINE DEDENT return True NEW_LINE DEDENT def isBST ( root ) : NEW_LINE INDENT prev = None NEW_LINE return isBSTUtil ( root , prev ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = Node ( 3 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . right . left = Node ( 1 ) NEW_LINE root . right . right = Node ( 4 ) NEW_LINE if ( isBST ( root ) == None ) : NEW_LINE INDENT print ( " Is ▁ BST " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ BST " ) NEW_LINE DEDENT DEDENT


public class GFG { static int findElement ( int arr [ ] , int size ) { int right_sum = 0 , left_sum = 0 ; for ( int i = 1 ; i < size ; i ++ ) right_sum += arr [ i ] ; for ( int i = 0 , j = 1 ; j < size ; i ++ , j ++ ) { right_sum -= arr [ j ] ; left_sum += arr [ i ] ; if ( left_sum == right_sum ) return arr [ i + 1 ] ; } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 1 , 4 , 5 } ; int size = arr . length ; System . out . println ( findElement ( arr , size ) ) ; } }

def findElement ( arr , size ) : NEW_LINE INDENT right_sum , left_sum = 0 , 0 NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT right_sum += arr [ i ] NEW_LINE DEDENT i , j = 0 , 1 NEW_LINE while j < size : NEW_LINE INDENT right_sum -= arr [ j ] NEW_LINE left_sum += arr [ i ] NEW_LINE if left_sum == right_sum : NEW_LINE INDENT return arr [ i + 1 ] NEW_LINE DEDENT j += 1 NEW_LINE i += 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 1 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findElement ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; public class GFG { static void findPair ( int [ ] arr , int n ) { boolean found = false ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] == arr [ k ] ) { System . out . println ( arr [ i ] + " ▁ " + arr [ j ] ) ; found = true ; } } } } if ( found == false ) System . out . println ( " Not ▁ exist " ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 10 , 4 , 8 , 13 , 5 } ; int n = arr . length ; findPair ( arr , n ) ; } }

def findPair ( arr , n ) : NEW_LINE INDENT found = False NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] + arr [ j ] == arr [ k ] ) : NEW_LINE INDENT print ( arr [ i ] , arr [ j ] ) NEW_LINE found = True NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( found == False ) : NEW_LINE INDENT print ( " Not ▁ exist " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 4 , 8 , 13 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE findPair ( arr , n ) NEW_LINE DEDENT


class GFG { static int maxn = 16 ; static int [ ] precompute ( ) { int dp [ ] = new int [ maxn ] , arr [ ] = { 4 , 6 , 9 } ; for ( int i = 0 ; i < maxn ; i ++ ) dp [ i ] = - 1 ; dp [ 0 ] = 0 ; for ( int i = 1 ; i < maxn ; ++ i ) { for ( int k = 0 ; k < 3 ; k ++ ) { int j = arr [ k ] ; if ( i >= j && dp [ i - j ] != - 1 ) { dp [ i ] = Math . max ( dp [ i ] , dp [ i - j ] + 1 ) ; } } } return dp ; } static int Maximum_Summands ( int [ ] dp , int n ) { if ( n < maxn ) return dp [ n ] ; else { int t = ( n - maxn ) / 4 + 1 ; return t + dp [ n - 4 * t ] ; } } public static void main ( String args [ ] ) { int n = 12 ; int [ ] dp = precompute ( ) ; System . out . println ( Maximum_Summands ( dp , n ) ) ; } }

global maxn NEW_LINE maxn = 16 NEW_LINE def precompute ( ) : NEW_LINE INDENT dp = [ - 1 for i in range ( maxn ) ] NEW_LINE dp [ 0 ] = 0 NEW_LINE v = [ 4 , 6 , 9 ] NEW_LINE for i in range ( 1 , maxn , 1 ) : NEW_LINE INDENT for k in range ( 3 ) : NEW_LINE INDENT j = v [ k ] NEW_LINE if ( i >= j and dp [ i - j ] != - 1 ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i ] , dp [ i - j ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT return dp NEW_LINE DEDENT def Maximum_Summands ( dp , n ) : NEW_LINE INDENT if ( n < maxn ) : NEW_LINE INDENT return dp [ n ] NEW_LINE DEDENT else : NEW_LINE INDENT t = int ( ( n - maxn ) / 4 ) + 1 NEW_LINE return t + dp [ n - 4 * t ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE dp = precompute ( ) NEW_LINE print ( Maximum_Summands ( dp , n ) ) NEW_LINE DEDENT


import java . math . * ; class GFG { static boolean check_char ( String st , char ch ) { int l = st . length ( ) ; for ( int i = 0 ; i < l ; i ++ ) { if ( st . charAt ( i ) == ch ) return true ; } return false ; } static char find_avg ( String st ) { int i , sm = 0 ; int l = st . length ( ) ; char ch ; for ( i = 0 ; i < l ; i ++ ) { ch = st . charAt ( i ) ; sm = sm + ( int ) ( ch ) ; } int avg = ( int ) ( Math . floor ( sm / l ) ) ; return ( ( char ) ( avg ) ) ; } public static void main ( String [ ] args ) { String st = " ag23sdfa " ; char ch = find_avg ( st ) ; System . out . println ( ch ) ; if ( check_char ( st , ch ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

from math import floor NEW_LINE def check_char ( st , ch ) : NEW_LINE INDENT l = len ( st ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT if ( st [ i ] == ch ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def find_avg ( st ) : NEW_LINE INDENT sm = 0 NEW_LINE l = len ( st ) NEW_LINE for i in range ( l ) : NEW_LINE INDENT ch = st [ i ] NEW_LINE sm = sm + ord ( ch ) NEW_LINE DEDENT avg = int ( floor ( sm / l ) ) NEW_LINE return ( chr ( avg ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT st = " ag23sdfa " NEW_LINE ch = find_avg ( st ) NEW_LINE print ( ch ) NEW_LINE if ( check_char ( st , ch ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int sum_even_and_even_index ( int arr [ ] , int n ) { int i = 0 , sum = 0 ; for ( i = 0 ; i < n ; i += 2 ) { if ( arr [ i ] % 2 == 0 ) { sum += arr [ i ] ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 12 , 1 , 18 , 8 } ; int n = arr . length ; System . out . println ( " Sum ▁ of ▁ even ▁ numbers " + " ▁ at ▁ even ▁ indices ▁ is ▁ " + + sum_even_and_even_index ( arr , n ) ) ; } }

def sum_even_and_even_index ( arr , n ) : NEW_LINE INDENT i = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , n , 2 ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT arr = [ 5 , 6 , 12 , 1 , 18 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Sum ▁ of ▁ even ▁ numbers ▁ at ▁ " , " even ▁ indices ▁ is ▁ " , sum_even_and_even_index ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static void pyramid ( int n ) { int i , j , num , gap ; for ( i = n ; i >= 1 ; i -- ) { for ( gap = n - 1 ; gap >= i ; gap -- ) { System . out . print ( " ▁ " ) ; System . out . print ( " ▁ " ) ; } num = ' A ' ; for ( j = 1 ; j <= i ; j ++ ) { System . out . print ( ( char ) num ++ + " ▁ " ) ; } for ( j = i - 1 ; j >= 0 ; j -- ) { System . out . print ( ( char ) -- num + " ▁ " ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 9 ; pyramid ( n ) ; } }

def pyramid ( n ) : NEW_LINE INDENT for i in range ( n , 0 , - 1 ) : NEW_LINE INDENT for gap in range ( n - 1 , i - 1 , - 1 ) : NEW_LINE INDENT print ( " ▁ " , end = ' ' ) NEW_LINE print ( " ▁ " , end = ' ' ) NEW_LINE DEDENT num = ord ( ' A ' ) NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( chr ( num ) , end = ' ▁ ' ) NEW_LINE num += 1 NEW_LINE DEDENT for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT num -= 1 NEW_LINE print ( chr ( num ) , end = ' ▁ ' ) NEW_LINE DEDENT print ( " \n " , end = ' ' ) NEW_LINE DEDENT DEDENT n = 9 NEW_LINE pyramid ( n ) NEW_LINE


class GFG { static int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; static int multiply ( int a [ ] , int n ) { if ( n == 0 ) return ( a [ n ] ) ; else return ( a [ n ] * multiply ( a , n - 1 ) ) ; } public static void main ( String [ ] args ) { System . out . println ( multiply ( arr , arr . length - 1 ) ) ; } }

def multiply ( a , n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return ( a [ n ] ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( a [ n ] * multiply ( a , n - 1 ) ) NEW_LINE DEDENT DEDENT array = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( array ) NEW_LINE print ( multiply ( array , n - 1 ) ) NEW_LINE


class GFG { static int Subtract ( int a , int b ) { int c ; c = a + ( ~ b + 1 ) ; return c ; } public static void main ( String [ ] args ) { int a = 2 , b = 3 ; System . out . println ( Subtract ( a , b ) ) ; a = 9 ; b = 7 ; System . out . println ( Subtract ( a , b ) ) ; } }

def Subtract ( a , b ) : NEW_LINE INDENT c = a + ( ~ b + 1 ) NEW_LINE return c NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 2 , 3 NEW_LINE print ( Subtract ( a , b ) ) NEW_LINE a , b = 9 , 7 NEW_LINE print ( Subtract ( a , b ) ) NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int binary_search ( int arr [ ] , int l , int h , int x ) { while ( l <= h ) { int mid = ( l + h ) / 2 ; if ( arr [ mid ] <= x ) l = mid + 1 ; else h = mid - 1 ; } return h ; } static void countEleLessThanOrEqual ( int arr1 [ ] , int arr2 [ ] , int m , int n ) { Arrays . sort ( arr2 ) ; for ( int i = 0 ; i < m ; i ++ ) { int index = binary_search ( arr2 , 0 , n - 1 , arr1 [ i ] ) ; System . out . print ( ( index + 1 ) + " ▁ " ) ; } } 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 bin_search ( arr , n , x ) : NEW_LINE l = 0 NEW_LINE h = n - 1 NEW_LINE while ( l <= h ) : NEW_LINE INDENT mid = int ( ( l + h ) / 2 ) NEW_LINE if ( arr [ mid ] <= x ) : NEW_LINE l = mid + 1 ; NEW_LINE else : NEW_LINE h = mid - 1 NEW_LINE DEDENT return h NEW_LINE def countElements ( arr1 , arr2 , m , n ) : NEW_LINE arr2 . sort ( ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT index = bin_search ( arr2 , n , arr1 [ i ] ) NEW_LINE print ( index + 1 ) NEW_LINE 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 countElements ( arr1 , arr2 , m , n ) NEW_LINE


import java . util . * ; class GFG { static void findNumbers ( int N ) { int i = 1 ; while ( i <= N ) { System . out . print ( ( i * i * i ) + " ▁ " ) ; i ++ ; } } public static void main ( String [ ] args ) { int N = 4 ; findNumbers ( N ) ; } }

def findNumbers ( N ) : NEW_LINE INDENT i = 1 NEW_LINE while ( i <= N ) : NEW_LINE INDENT print ( ( i * i * i ) , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE findNumbers ( N ) NEW_LINE DEDENT


class GFG { static int seriesSum ( int calculated , int current , int N ) { int i , cur = 1 ; if ( current == N + 1 ) return 0 ; for ( i = calculated ; i < calculated + current ; i ++ ) cur *= i ; return cur + seriesSum ( i , current + 1 , N ) ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( seriesSum ( 1 , 1 , N ) ) ; } }

def seriesSum ( calculated , current , N ) : NEW_LINE INDENT i = calculated ; NEW_LINE cur = 1 ; NEW_LINE if ( current == N + 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT while ( i < calculated + current ) : NEW_LINE INDENT cur *= i ; NEW_LINE i += 1 ; NEW_LINE DEDENT return cur + seriesSum ( i , current + 1 , N ) ; NEW_LINE DEDENT N = 5 ; NEW_LINE print ( seriesSum ( 1 , 1 , N ) ) ; NEW_LINE


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

from math import * NEW_LINE def nthTerm ( n ) : NEW_LINE INDENT return pow ( n , 2 ) + 2 * n + 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT


class GFG { static int oddTriangularNumber ( int N ) { return ( N * ( ( 2 * N ) - 1 ) ) ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( oddTriangularNumber ( N ) ) ; } }

def oddTriangularNumber ( N ) : NEW_LINE INDENT return ( N * ( ( 2 * N ) - 1 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE print ( oddTriangularNumber ( N ) ) NEW_LINE DEDENT


class MaximumDiffrence { int maxDiff ( int arr [ ] , int arr_size ) { int max_diff = arr [ 1 ] - arr [ 0 ] ; int min_element = arr [ 0 ] ; int i ; for ( i = 1 ; i < arr_size ; i ++ ) { if ( arr [ i ] - min_element > max_diff ) max_diff = arr [ i ] - min_element ; if ( arr [ i ] < min_element ) min_element = arr [ i ] ; } return max_diff ; } public static void main ( String [ ] args ) { MaximumDiffrence maxdif = new MaximumDiffrence ( ) ; int arr [ ] = { 1 , 2 , 90 , 10 , 110 } ; int size = arr . length ; System . out . println ( " MaximumDifference ▁ is ▁ " + maxdif . maxDiff ( arr , size ) ) ; } }

def maxDiff ( arr , arr_size ) : NEW_LINE INDENT max_diff = arr [ 1 ] - arr [ 0 ] NEW_LINE min_element = arr [ 0 ] NEW_LINE for i in range ( 1 , arr_size ) : NEW_LINE INDENT if ( arr [ i ] - min_element > max_diff ) : NEW_LINE INDENT max_diff = arr [ i ] - min_element NEW_LINE DEDENT if ( arr [ i ] < min_element ) : NEW_LINE INDENT min_element = arr [ i ] NEW_LINE DEDENT DEDENT return max_diff NEW_LINE DEDENT arr = [ 1 , 2 , 6 , 80 , 100 ] NEW_LINE size = len ( arr ) NEW_LINE print ( " Maximum ▁ difference ▁ is " , maxDiff ( arr , size ) ) NEW_LINE


import java . lang . * ; class GFG { static double PI = 3.14159265 ; public static double length_rope ( double r ) { return ( ( 2 * PI * r ) + 6 * r ) ; } public static void main ( String [ ] args ) { double r = 7 ; System . out . println ( length_rope ( r ) ) ; } }

PI = 3.14159265 NEW_LINE def length_rope ( r ) : NEW_LINE INDENT return ( ( 2 * PI * r ) + 6 * r ) NEW_LINE DEDENT r = 7 NEW_LINE print ( length_rope ( r ) ) NEW_LINE


class GFG { static boolean isMember ( int a , int d , int x ) { if ( d == 0 ) return ( x == a ) ; return ( ( x - a ) % d == 0 && ( x - a ) / d >= 0 ) ; } public static void main ( String args [ ] ) { int a = 1 , x = 7 , d = 3 ; if ( isMember ( a , d , x ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isMember ( a , d , x ) : NEW_LINE INDENT if d == 0 : NEW_LINE INDENT return x == a NEW_LINE DEDENT return ( ( x - a ) % d == 0 & int ( ( x - a ) / d ) >= 0 ) NEW_LINE DEDENT a = 1 NEW_LINE x = 7 NEW_LINE d = 3 NEW_LINE if isMember ( a , d , x ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class GFG { static int sum_of_series ( int n ) { int result = 0 ; if ( n % 2 == 0 ) { result = - ( n * ( n + 1 ) ) / 2 ; } else { result = ( n * ( n + 1 ) ) / 2 ; } return result ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( sum_of_series ( n ) ) ; n = 10 ; System . out . println ( sum_of_series ( n ) ) ; } }

def sum_of_series ( n ) : NEW_LINE INDENT result = 0 NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT result = - ( n * ( n + 1 ) ) // 2 NEW_LINE DEDENT else : NEW_LINE INDENT result = ( n * ( n + 1 ) ) // 2 NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE n = 10 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE DEDENT


import java . lang . * ; class GFG { static void checkSquare ( String s1 , String s2 ) { int c = Integer . valueOf ( s1 + s2 ) ; int d = ( int ) Math . sqrt ( c ) ; if ( d * d == c ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { String s1 = "12" ; String s2 = "1" ; checkSquare ( s1 , s2 ) ; } }

import math NEW_LINE def checkSquare ( s1 , s2 ) : NEW_LINE INDENT c = int ( s1 + s2 ) NEW_LINE d = math . sqrt ( c ) NEW_LINE if ( d * d == c ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = "12" NEW_LINE s2 = "1" NEW_LINE checkSquare ( s1 , s2 ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static long maxPrimeFactors ( long n ) { long maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n >>= 1 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } } if ( n > 2 ) maxPrime = n ; return maxPrime ; } public static void main ( String [ ] args ) { Long n = 15l ; System . out . println ( maxPrimeFactors ( n ) ) ; n = 25698751364526l ; System . out . println ( maxPrimeFactors ( n ) ) ; } }

import math NEW_LINE def maxPrimeFactors ( n ) : NEW_LINE INDENT maxPrime = - 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT maxPrime = 2 NEW_LINE n >>= 1 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT maxPrime = i NEW_LINE n = n / i NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT maxPrime = n NEW_LINE DEDENT return int ( maxPrime ) NEW_LINE DEDENT n = 15 NEW_LINE print ( maxPrimeFactors ( n ) ) NEW_LINE n = 25698751364526 NEW_LINE print ( maxPrimeFactors ( n ) ) NEW_LINE


import java . util . * ; public class SortExample { static void mySort ( Integer [ ] arr ) { int n = arr . length ; Arrays . sort ( arr , 0 , n / 2 ) ; Arrays . sort ( arr , n / 2 , n ) ; int low = n / 2 , high = n - 1 ; while ( low < high ) { Integer temp = arr [ low ] ; arr [ low ] = arr [ high ] ; arr [ high ] = temp ; low ++ ; high -- ; } } public static void main ( String [ ] args ) { Integer [ ] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , 7 } ; mySort ( arr ) ; System . out . printf ( " Modified ▁ arr [ ] ▁ : ▁ % s " , Arrays . toString ( arr ) ) ; } }

def mySort ( arr ) : NEW_LINE INDENT n = len ( arr ) ; NEW_LINE arr1 = arr [ : n // 2 ] NEW_LINE arr2 = arr [ n // 2 : ] NEW_LINE arr1 . sort ( ) NEW_LINE arr2 . sort ( ) NEW_LINE arr = arr1 + arr2 NEW_LINE low = n // 2 ; NEW_LINE high = n - 1 ; NEW_LINE while ( low < high ) : NEW_LINE INDENT temp = arr [ low ] ; NEW_LINE arr [ low ] = arr [ high ] ; NEW_LINE arr [ high ] = temp ; NEW_LINE low += 1 ; NEW_LINE high -= 1 ; NEW_LINE DEDENT return arr ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , 7 ] ; NEW_LINE arr = mySort ( arr ) ; NEW_LINE print ( " Modified ▁ Array ▁ : ▁ " ) NEW_LINE print ( arr ) NEW_LINE DEDENT


class Solution { static int smallestOdd ( int n ) { if ( n == 1 ) return 0 ; return Math . pow ( 10 , n - 1 ) + 1 ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( smallestOdd ( n ) ) ; } }

def smallestOdd ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return pow ( 10 , n - 1 ) + 1 NEW_LINE DEDENT n = 4 NEW_LINE print ( smallestOdd ( n ) ) NEW_LINE


class GFG { static double maxSubArraySum ( double a [ ] , int size ) { double max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static void minPossibleSum ( double a [ ] , int n , double x ) { double mxSum = maxSubArraySum ( a , n ) ; double sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } sum = sum - mxSum + mxSum / x ; System . out . print ( sum + " \n " ) ; } public static void main ( String [ ] args ) { int N = 3 ; double X = 2 ; double A [ ] = { 1 , - 2 , 3 } ; minPossibleSum ( A , N , X ) ; } }

def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = - 10 ** 9 NEW_LINE max_ending_here = 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] NEW_LINE if ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here NEW_LINE DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 NEW_LINE DEDENT DEDENT return max_so_far NEW_LINE DEDENT def minPossibleSum ( a , n , x ) : NEW_LINE INDENT mxSum = maxSubArraySum ( a , n ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT sum = sum - mxSum + mxSum / x NEW_LINE print ( round ( sum , 2 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE X = 2 NEW_LINE A = [ 1 , - 2 , 3 ] NEW_LINE minPossibleSum ( A , N , X ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int MOD = 1000000007 ; static long sum ( long N ) { long val = N * ( N + 1 ) ; val = val % MOD ; return val ; } static int sumX ( int N , int M , int K ) { for ( int i = 0 ; i < M ; i ++ ) { N = ( int ) sum ( K + N ) ; } N = N % MOD ; return N ; } public static void main ( String args [ ] ) { int N = 1 , M = 2 , K = 3 ; System . out . println ( sumX ( N , M , K ) ) ; } }

MOD = 1000000007 NEW_LINE def Sum ( N ) : NEW_LINE INDENT val = N * ( N + 1 ) NEW_LINE val = val % MOD NEW_LINE return val NEW_LINE DEDENT def sumX ( N , M , K ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT N = int ( Sum ( K + N ) ) NEW_LINE DEDENT N = N % MOD NEW_LINE return N NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M , K = 1 , 2 , 3 NEW_LINE print ( sumX ( N , M , K ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static int N = 100005 ; static Vector < Integer > [ ] gr = new Vector [ N ] ; static int [ ] colour = new int [ N ] ; static Vector < pair > edges = new Vector < > ( ) ; static boolean bip ; static void add_edge ( int x , int y ) { gr [ x ] . add ( y ) ; gr [ y ] . add ( x ) ; edges . add ( new pair ( x , y ) ) ; } static void dfs ( int x , int col ) { colour [ x ] = col ; for ( Integer i : gr [ x ] ) { if ( colour [ i ] == - 1 ) dfs ( i , col ^ 1 ) ; else if ( colour [ i ] == col ) bip = false ; } } static void Directed_Graph ( int n , int m ) { for ( int i = 0 ; i < N ; i ++ ) colour [ i ] = - 1 ; bip = true ; dfs ( 1 , 1 ) ; if ( ! bip ) { System . out . print ( - 1 ) ; return ; } for ( int i = 0 ; i < m ; i ++ ) { if ( colour [ edges . get ( i ) . first ] == 0 ) { Collections . swap ( edges , edges . get ( i ) . first , edges . get ( i ) . second ) ; } System . out . println ( edges . get ( i ) . first + " ▁ " + edges . get ( i ) . second ) ; } } public static void main ( String [ ] args ) { int n = 4 , m = 3 ; for ( int i = 0 ; i < N ; i ++ ) gr [ i ] = new Vector < > ( ) ; add_edge ( 1 , 2 ) ; add_edge ( 1 , 3 ) ; add_edge ( 1 , 4 ) ; Directed_Graph ( n , m ) ; } }

N = 100005 NEW_LINE gr = [ [ ] for i in range ( N ) ] NEW_LINE colour = [ - 1 ] * N NEW_LINE edges = [ ] NEW_LINE bip = True NEW_LINE def add_edge ( x , y ) : NEW_LINE INDENT gr [ x ] . append ( y ) NEW_LINE gr [ y ] . append ( x ) NEW_LINE edges . append ( ( x , y ) ) NEW_LINE DEDENT def dfs ( x , col ) : NEW_LINE INDENT colour [ x ] = col NEW_LINE global bip NEW_LINE for i in gr [ x ] : NEW_LINE INDENT if colour [ i ] == - 1 : NEW_LINE INDENT dfs ( i , col ^ 1 ) NEW_LINE DEDENT elif colour [ i ] == col : NEW_LINE INDENT bip = False NEW_LINE DEDENT DEDENT DEDENT def Directed_Graph ( n , m ) : NEW_LINE INDENT dfs ( 1 , 1 ) NEW_LINE if not bip : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , m ) : NEW_LINE INDENT if colour [ edges [ i ] [ 0 ] ] == 0 : NEW_LINE INDENT edges [ i ] [ 0 ] , edges [ i ] [ 1 ] = edges [ i ] [ 1 ] , edges [ i ] [ 0 ] NEW_LINE DEDENT print ( edges [ i ] [ 0 ] , edges [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , m = 4 , 3 NEW_LINE add_edge ( 1 , 2 ) NEW_LINE add_edge ( 1 , 3 ) NEW_LINE add_edge ( 1 , 4 ) NEW_LINE Directed_Graph ( n , m ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void createSorted ( int a [ ] , int n ) { Vector < Integer > b = new Vector < > ( ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( b . isEmpty ( ) ) b . add ( a [ j ] ) ; else { int start = 0 , end = b . size ( ) - 1 ; int pos = 0 ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( b . get ( mid ) == a [ j ] ) { b . add ( ( Math . max ( 0 , mid + 1 ) ) , a [ j ] ) ; break ; } else if ( b . get ( mid ) > a [ j ] ) pos = end = mid - 1 ; else pos = start = mid + 1 ; if ( start > end ) { pos = start ; b . add ( Math . max ( 0 , pos ) , a [ j ] ) ; break ; } } } } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( b . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 2 , 5 , 4 , 9 , 8 } ; int n = a . length ; createSorted ( a , n ) ; } }

def createSorted ( a : list , n : int ) : NEW_LINE INDENT b = [ ] NEW_LINE for j in range ( n ) : NEW_LINE INDENT if len ( b ) == 0 : NEW_LINE INDENT b . append ( a [ j ] ) NEW_LINE DEDENT else : NEW_LINE INDENT start = 0 NEW_LINE end = len ( b ) - 1 NEW_LINE pos = 0 NEW_LINE while start <= end : NEW_LINE INDENT mid = start + ( end - start ) // 2 NEW_LINE if b [ mid ] == a [ j ] : NEW_LINE INDENT b . insert ( max ( 0 , mid + 1 ) , a [ j ] ) NEW_LINE break NEW_LINE DEDENT elif b [ mid ] > a [ j ] : NEW_LINE INDENT pos = end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT pos = start = mid + 1 NEW_LINE DEDENT if start > end : NEW_LINE INDENT pos = start NEW_LINE b . insert ( max ( 0 , pos ) , a [ j ] ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( b [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 5 , 4 , 9 , 8 ] NEW_LINE n = len ( a ) NEW_LINE createSorted ( a , n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int getNumber ( int n , int k ) { int pos ; if ( ( n % 2 ) == 0 ) { pos = n / 2 ; } else { pos = ( n / 2 ) + 1 ; } if ( k <= pos ) { return ( k * 2 - 1 ) ; } else return ( ( k - pos ) * 2 ) ; } public static void main ( String [ ] args ) { int n = 8 , k = 5 ; System . out . println ( getNumber ( n , k ) ) ; } }

def getNumber ( n , k ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT pos = n // 2 ; NEW_LINE DEDENT else : NEW_LINE INDENT pos = ( n // 2 ) + 1 ; NEW_LINE DEDENT if ( k <= pos ) : NEW_LINE INDENT return ( k * 2 - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( ( k - pos ) * 2 ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 ; k = 5 ; NEW_LINE print ( getNumber ( n , k ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static int maxSum ( int a [ ] , int n ) { int S = 0 ; int S1 = maxSubArraySum ( a , n ) ; for ( int i = 0 ; i < n ; i ++ ) S += a [ i ] ; return ( 2 * S1 - S ) ; } public static void main ( String [ ] args ) { int a [ ] = { - 35 , 32 , - 24 , 0 , 27 , - 10 , 0 , - 19 } ; int n = a . length ; System . out . println ( maxSum ( a , n ) ) ; } }

import sys NEW_LINE def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = - ( sys . maxsize - 1 ) NEW_LINE max_ending_here = 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] NEW_LINE if ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here NEW_LINE DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 NEW_LINE DEDENT DEDENT return max_so_far NEW_LINE DEDENT def maxSum ( a , n ) : NEW_LINE INDENT S = 0 ; NEW_LINE S1 = maxSubArraySum ( a , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT S += a [ i ] NEW_LINE DEDENT return ( 2 * S1 - S ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ - 35 , 32 , - 24 , 0 , 27 , - 10 , 0 , - 19 ] NEW_LINE n = len ( a ) NEW_LINE print ( maxSum ( a , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static float findRadiusOfcircumcircle ( float n , float a ) { if ( n < 0 || a < 0 ) return - 1 ; float radius = ( float ) ( a / Math . sqrt ( 2 - ( 2 * Math . cos ( 360 / n ) ) ) ) ; return radius ; } public static void main ( String [ ] args ) { float n = 5 , a = 6 ; System . out . println ( findRadiusOfcircumcircle ( n , a ) ) ; } }

from math import * NEW_LINE def findRadiusOfcircumcircle ( n , a ) : NEW_LINE INDENT if n < 0 or a < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT radius = a / sqrt ( 2 - ( 2 * cos ( 360 / n ) ) ) NEW_LINE return radius NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , a = 5 , 6 NEW_LINE print ( round ( findRadiusOfcircumcircle ( n , a ) , 5 ) ) NEW_LINE DEDENT


class GFG { static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; return __gcd ( b % a , a ) ; } static int LCM ( int den [ ] , int N ) { int ans = den [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) ans = ( ( ( den [ i ] * ans ) ) / ( __gcd ( den [ i ] , ans ) ) ) ; return ans ; } static int HCF ( int num [ ] , int N ) { int ans = num [ 0 ] ; for ( int i = 1 ; i < N ; i ++ ) ans = __gcd ( num [ i ] , ans ) ; return ans ; } static void HCFOfFractions ( int num [ ] , int den [ ] , int N ) { int Numerator = HCF ( num , N ) ; int Denominator = LCM ( den , N ) ; int result = __gcd ( Numerator , Denominator ) ; Numerator = Numerator / result ; Denominator = Denominator / result ; System . out . println ( " HCF ▁ is ▁ = ▁ " + Numerator + " / " + Denominator ) ; } public static void main ( String [ ] args ) { int num [ ] = { 24 , 48 , 72 , 96 } , den [ ] = { 2 , 6 , 8 , 3 } ; int N = num . length ; HCFOfFractions ( num , den , N ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT if ( b == 0 ) : NEW_LINE INDENT return a ; 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 def LCM ( den , N ) : NEW_LINE INDENT ans = den [ 0 ] ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT ans = ( ( ( den [ i ] * ans ) ) / ( gcd ( den [ i ] , ans ) ) ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def HCF ( num , N ) : NEW_LINE INDENT ans = num [ 0 ] ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT ans = gcd ( num [ i ] , ans ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def HCFOfFractions ( num , den , N ) : NEW_LINE INDENT Numerator = HCF ( num , N ) ; NEW_LINE Denominator = LCM ( den , N ) ; NEW_LINE gcd1 = gcd ( Numerator , Denominator ) ; NEW_LINE Numerator = int ( Numerator / gcd1 ) ; NEW_LINE Denominator = int ( Denominator / gcd1 ) ; NEW_LINE print ( " HCF ▁ is ▁ = " , Numerator , " / " , Denominator ) ; NEW_LINE DEDENT num = [ 24 , 48 , 72 , 96 ] ; NEW_LINE den = [ 2 , 6 , 8 , 3 ] ; NEW_LINE N = len ( num ) ; NEW_LINE HCFOfFractions ( num , den , N ) ; NEW_LINE


class GFG { static int CountSubSet ( int arr [ ] , int n , int X ) { int N = ( int ) Math . pow ( 2 , n ) ; int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & ( 1 << j ) ) != 0 ) if ( arr [ j ] == X ) count += 1 ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 6 , 7 } ; int X = 5 ; int n = arr . length ; System . out . print ( CountSubSet ( arr , n , X ) ) ; } }

def CountSubSet ( arr , n , X ) : NEW_LINE INDENT N = 2 ** n ; NEW_LINE count = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( i & ( 1 << j ) ) : NEW_LINE INDENT if ( arr [ j ] == X ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 5 , 6 , 7 ] ; NEW_LINE X = 5 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( CountSubSet ( arr , n , X ) ) ; NEW_LINE DEDENT


class GFG { static void findDivision ( String str , int a , int b ) { int len = str . length ( ) ; int [ ] lr = new int [ len + 1 ] ; lr [ 0 ] = ( ( int ) str . charAt ( 0 ) - ( int ) '0' ) % a ; for ( int i = 1 ; i < len ; i ++ ) lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a + ( ( int ) str . charAt ( i ) - ( int ) '0' ) ) % a ; int [ ] rl = new int [ len + 1 ] ; rl [ len - 1 ] = ( ( int ) str . charAt ( len - 1 ) - ( int ) '0' ) % b ; int power10 = 10 ; for ( int i = len - 2 ; i >= 0 ; i -- ) { rl [ i ] = ( rl [ i + 1 ] + ( ( int ) str . charAt ( i ) - ( int ) '0' ) * power10 ) % b ; power10 = ( power10 * 10 ) % b ; } for ( int i = 0 ; i < len - 1 ; i ++ ) { if ( lr [ i ] != 0 ) continue ; if ( rl [ i + 1 ] == 0 ) { System . out . println ( " YES " ) ; for ( int k = 0 ; k <= i ; k ++ ) System . out . print ( str . charAt ( k ) ) ; System . out . print ( " , ▁ " ) ; for ( int k = i + 1 ; k < len ; k ++ ) System . out . print ( str . charAt ( k ) ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { String str = "123" ; int a = 12 , b = 3 ; findDivision ( str , a , b ) ; } }

def findDivision ( str , a , b ) : NEW_LINE INDENT lenn = len ( str ) NEW_LINE lr = [ 0 ] * ( lenn + 1 ) NEW_LINE lr [ 0 ] = ( int ( str [ 0 ] ) ) % a NEW_LINE for i in range ( 1 , lenn ) : NEW_LINE INDENT lr [ i ] = ( ( lr [ i - 1 ] * 10 ) % a +   \ int ( str [ i ] ) ) % a NEW_LINE DEDENT rl = [ 0 ] * ( lenn + 1 ) NEW_LINE rl [ lenn - 1 ] = int ( str [ lenn - 1 ] ) % b NEW_LINE power10 = 10 NEW_LINE for i in range ( lenn - 2 , - 1 , - 1 ) : NEW_LINE INDENT rl [ i ] = ( rl [ i + 1 ] + int ( str [ i ] ) * power10 ) % b NEW_LINE power10 = ( power10 * 10 ) % b NEW_LINE DEDENT for i in range ( 0 , lenn - 1 ) : NEW_LINE INDENT if ( lr [ i ] != 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( rl [ i + 1 ] == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE for k in range ( 0 , i + 1 ) : NEW_LINE INDENT print ( str [ k ] , end = " " ) NEW_LINE DEDENT print ( " , " , end = " ▁ " ) NEW_LINE for i in range ( i + 1 , lenn ) : NEW_LINE INDENT print ( str [ k ] , end = " " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " NO " ) NEW_LINE DEDENT str = "123" NEW_LINE a , b = 12 , 3 NEW_LINE findDivision ( str , a , b ) NEW_LINE


class GFG { static float y ( float x ) { return 1 / ( 1 + x * x ) ; } static float trapezoidal ( float a , float b , float n ) { float h = ( b - a ) / n ; float s = y ( a ) + y ( b ) ; for ( int i = 1 ; i < n ; i ++ ) s += 2 * y ( a + i * h ) ; return ( h / 2 ) * s ; } public static void main ( String [ ] args ) { float x0 = 0 ; float xn = 1 ; int n = 6 ; System . out . println ( " Value ▁ of ▁ integral ▁ is ▁ " + Math . round ( trapezoidal ( x0 , xn , n ) * 10000.0 ) / 10000.0 ) ; } }

def y ( x ) : NEW_LINE INDENT return ( 1 / ( 1 + x * x ) ) NEW_LINE DEDENT def trapezoidal ( a , b , n ) : NEW_LINE INDENT h = ( b - a ) / n NEW_LINE s = ( y ( a ) + y ( b ) ) NEW_LINE i = 1 NEW_LINE while i < n : NEW_LINE INDENT s += 2 * y ( a + i * h ) NEW_LINE i += 1 NEW_LINE DEDENT return ( ( h / 2 ) * s ) NEW_LINE DEDENT x0 = 0 NEW_LINE xn = 1 NEW_LINE n = 6 NEW_LINE print ( " Value ▁ of ▁ integral ▁ is ▁ " , " % .4f " % trapezoidal ( x0 , xn , n ) ) NEW_LINE


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

def fibonacci ( n ) : NEW_LINE INDENT a = 0 NEW_LINE b = 1 NEW_LINE if n < 0 : NEW_LINE INDENT print ( " Incorrect ▁ input " ) NEW_LINE DEDENT elif n == 0 : NEW_LINE INDENT return a NEW_LINE DEDENT elif n == 1 : NEW_LINE INDENT return b NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE DEDENT return b NEW_LINE DEDENT DEDENT print ( fibonacci ( 9 ) ) NEW_LINE


import java . util . * ; class Gfg { static int countDigits ( int n ) { return ( int ) ( n * Math . log10 ( 2 ) + 1 ) ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( countDigits ( n ) ) ; } }

import math NEW_LINE def countDigits ( n ) : NEW_LINE INDENT return int ( n * math . log10 ( 2 ) + 1 ) ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( countDigits ( n ) ) ; NEW_LINE


import java . io . * ; class GFG { static void alternateFib ( int n ) { if ( n < 0 ) return ; int f1 = 0 ; int f2 = 1 ; System . out . print ( f1 + " ▁ " ) ; for ( int i = 2 ; i <= n ; i ++ ) { int f3 = f2 + f1 ; if ( i % 2 == 0 ) System . out . print ( f3 + " ▁ " ) ; f1 = f2 ; f2 = f3 ; } } public static void main ( String [ ] args ) { int N = 15 ; alternateFib ( N ) ; } }

def alternateFib ( n ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT f1 = 0 ; NEW_LINE f2 = 1 ; NEW_LINE print ( f1 , end = " ▁ " ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT f3 = f2 + f1 ; NEW_LINE if ( i % 2 == 0 ) : NEW_LINE INDENT print ( f3 , end = " ▁ " ) ; NEW_LINE DEDENT f1 = f2 ; NEW_LINE f2 = f3 ; NEW_LINE DEDENT DEDENT N = 15 ; NEW_LINE alternateFib ( N ) ; NEW_LINE


import java . io . * ; class GFG { static int N = 2 ; static int M = 3 ; static boolean check ( int mat [ ] [ ] ) { int xorr = 0 ; for ( int i = 0 ; i < N ; i ++ ) { xorr ^= mat [ i ] [ 0 ] ; } if ( xorr != 0 ) return true ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 1 ; j < M ; j ++ ) { if ( mat [ i ] [ j ] != mat [ i ] [ 0 ] ) return true ; } } return false ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 7 , 7 , 7 } , { 10 , 10 , 7 } } ; if ( check ( mat ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

N = 2 NEW_LINE M = 3 NEW_LINE def check ( mat ) : NEW_LINE INDENT xorr = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT xorr ^= mat [ i ] [ 0 ] NEW_LINE DEDENT if ( xorr != 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( 1 , M ) : NEW_LINE INDENT if ( mat [ i ] [ j ] != mat [ i ] [ 0 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT mat = [ [ 7 , 7 , 7 ] , [ 10 , 10 , 7 ] ] NEW_LINE if ( check ( mat ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT