import java . util . * ; class GFG { static int maxCount ( int A [ ] , int N , int K ) { int maximum = 0 ; int i = 0 , j = 0 ; int start = 0 ; int end = 0 ; Arrays . sort ( A ) ; for ( i = 0 ; i < N ; i ++ ) { while ( j < N && A [ j ] <= A [ i ] + K ) j ++ ; if ( maximum < ( j - i ) ) { maximum = ( j - i ) ; start = i ; end = j ; } } return maximum ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 26 , 17 , 12 , 15 , 2 } ; int N = A . length ; int K = 5 ; System . out . println ( maxCount ( A , N , K ) ) ; } }

def maxCount ( A , N , K ) : NEW_LINE INDENT maximum = 0 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE j = 0 NEW_LINE A . sort ( ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT while ( j < N and A [ j ] <= A [ i ] + K ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if maximum < ( j - i ) : NEW_LINE INDENT maximum = ( j - i ) NEW_LINE start = i ; NEW_LINE end = j ; NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT A = [ 1 , 26 , 17 , 12 , 15 , 2 ] NEW_LINE N = len ( A ) NEW_LINE K = 5 NEW_LINE print ( maxCount ( A , N , K ) ) NEW_LINE


class Test { static boolean isDivisible999 ( String num ) { int n = num . length ( ) ; if ( n == 0 && num . charAt ( 0 ) == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; int gSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int group = 0 ; group += ( num . charAt ( i ++ ) - '0' ) * 100 ; group += ( num . charAt ( i ++ ) - '0' ) * 10 ; group += num . charAt ( i ) - '0' ; gSum += group ; } if ( gSum > 1000 ) { num = Integer . toString ( gSum ) ; n = num . length ( ) ; gSum = isDivisible999 ( num ) ? 1 : 0 ; } return ( gSum == 999 ) ; } public static void main ( String args [ ] ) { String num = "1998" ; System . out . println ( isDivisible999 ( num ) ? " Divisible " : " Not ▁ divisible " ) ; } }

def isDivisible999 ( num ) : NEW_LINE INDENT n = len ( num ) ; NEW_LINE if ( n == 0 or num [ 0 ] == '0' ) : NEW_LINE INDENT return true NEW_LINE DEDENT if ( ( n % 3 ) == 1 ) : NEW_LINE INDENT num = "00" + num NEW_LINE DEDENT if ( ( n % 3 ) == 2 ) : NEW_LINE INDENT num = "0" + num NEW_LINE DEDENT gSum = 0 NEW_LINE for i in range ( 0 , n , 3 ) : NEW_LINE INDENT group = 0 NEW_LINE group += ( ord ( num [ i ] ) - 48 ) * 100 NEW_LINE group += ( ord ( num [ i + 1 ] ) - 48 ) * 10 NEW_LINE group += ( ord ( num [ i + 2 ] ) - 48 ) NEW_LINE gSum += group NEW_LINE DEDENT if ( gSum > 1000 ) : NEW_LINE INDENT num = str ( gSum ) NEW_LINE n = len ( num ) NEW_LINE gSum = isDivisible999 ( num ) NEW_LINE DEDENT return ( gSum == 999 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT num = "1998" NEW_LINE n = len ( num ) NEW_LINE if ( isDivisible999 ( num ) ) : NEW_LINE INDENT print ( " Divisible " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ divisible " ) NEW_LINE DEDENT DEDENT


import java . util . Arrays ; class GFG { static void sortString ( String str ) { char [ ] arr = str . toCharArray ( ) ; Arrays . sort ( arr ) ; System . out . print ( String . valueOf ( arr ) ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; sortString ( s ) ; } }

def sortString ( str ) : NEW_LINE INDENT str = ' ' . join ( sorted ( str ) ) NEW_LINE print ( str ) NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE sortString ( s ) NEW_LINE


class GFG { static boolean isTriangular ( int num ) { if ( num < 0 ) return false ; int sum = 0 ; for ( int n = 1 ; sum <= num ; n ++ ) { sum = sum + n ; if ( sum == num ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 55 ; if ( isTriangular ( n ) ) System . out . print ( " The ▁ number ▁ " + " is ▁ a ▁ triangular ▁ number " ) ; else System . out . print ( " The ▁ number " + " ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ) ; } }

def isTriangular ( num ) : NEW_LINE INDENT if ( num < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT sum , n = 0 , 1 NEW_LINE while ( sum <= num ) : NEW_LINE INDENT sum = sum + n NEW_LINE if ( sum == num ) : NEW_LINE INDENT return True NEW_LINE DEDENT n += 1 NEW_LINE DEDENT return False NEW_LINE DEDENT n = 55 NEW_LINE if ( isTriangular ( n ) ) : NEW_LINE INDENT print ( " The ▁ number ▁ is ▁ a ▁ triangular ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ number ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void convert_To_Len_th_base ( int n , int arr [ ] , int len , int L ) { for ( int i = 0 ; i < L ; i ++ ) { System . out . print ( arr [ n % len ] ) ; n /= len ; } System . out . println ( ) ; } static void print ( int arr [ ] , int len , int L ) { for ( int i = 0 ; i < ( int ) Math . pow ( len , L ) ; i ++ ) { convert_To_Len_th_base ( i , arr , len , L ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int len = arr . length ; int L = 2 ; print ( arr , len , L ) ; } }

def convert_To_Len_th_base ( n , arr , Len , L ) : NEW_LINE INDENT for i in range ( L ) : NEW_LINE INDENT print ( arr [ n % Len ] , end = " " ) NEW_LINE n //= Len NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def printf ( arr , Len , L ) : NEW_LINE INDENT for i in range ( pow ( Len , L ) ) : NEW_LINE INDENT convert_To_Len_th_base ( i , arr , Len , L ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE Len = len ( arr ) NEW_LINE L = 2 NEW_LINE printf ( arr , Len , L ) NEW_LINE


class GFG { static int minReplacement ( String s , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( i % 2 == 0 && s . charAt ( i ) == '1' ) ans ++ ; if ( i % 2 == 1 && s . charAt ( i ) == '0' ) ans ++ ; } return Math . min ( ans , len - ans ) ; } public static void main ( String args [ ] ) { String s = "1100" ; int len = s . length ( ) ; System . out . print ( minReplacement ( s , len ) ) ; } }

def minReplacement ( s , length ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , length ) : NEW_LINE INDENT if i % 2 == 0 and s [ i ] == '1' : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT if i % 2 == 1 and s [ i ] == '0' : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT return min ( ans , length - ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "1100" NEW_LINE length = len ( s ) NEW_LINE print ( minReplacement ( s , length ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void solve ( int A , int B ) { int count = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { if ( ( ( A >> i ) & 1 ) != ( ( B >> i ) & 1 ) ) { count ++ ; } } System . out . println ( " Number ▁ of ▁ different ▁ bits ▁ : ▁ " + count ) ; } public static void main ( String [ ] args ) { int A = 12 , B = 15 ; solve ( A , B ) ; } }

def solve ( A , B ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT if ( ( ( A >> i ) & 1 ) != ( ( B >> i ) & 1 ) ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT print ( " Number ▁ of ▁ different ▁ bits ▁ : " , count ) NEW_LINE DEDENT A = 12 NEW_LINE B = 15 NEW_LINE solve ( A , B ) NEW_LINE


import java . util . * ; class GFG { static boolean isFrequencyEqual ( int arr [ ] , int len ) { if ( len % 2 == 1 ) { System . out . print ( " No ▁ Such ▁ Element " ) ; return false ; } HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < len ; i ++ ) if ( freq . containsKey ( arr [ i ] ) ) { freq . put ( arr [ i ] , freq . get ( arr [ i ] ) + 1 ) ; } else { freq . put ( arr [ i ] , 1 ) ; } for ( int i = 0 ; i < len ; i ++ ) { if ( freq . containsKey ( arr [ i ] ) && freq . get ( arr [ i ] ) == len / 2 ) { System . out . print ( arr [ i ] + " \n " ) ; return true ; } } System . out . print ( " No ▁ such ▁ element " ) ; return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 3 , 3 } ; int n = 6 ; isFrequencyEqual ( arr , n ) ; } }

def isFrequencyEqual ( arr , length ) : NEW_LINE INDENT if ( length % 2 == 1 ) : NEW_LINE INDENT print ( " No ▁ Such ▁ Element " ) ; NEW_LINE return False ; NEW_LINE DEDENT freq = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( length ) : NEW_LINE INDENT if ( freq [ arr [ i ] ] == length / 2 ) : NEW_LINE INDENT print ( arr [ i ] ) ; NEW_LINE return True ; NEW_LINE DEDENT DEDENT print ( " No ▁ such ▁ element " , end = " " ) ; NEW_LINE return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 3 , 3 ] ; NEW_LINE n = 6 ; NEW_LINE isFrequencyEqual ( arr , n ) ; NEW_LINE DEDENT


class GFG { static int MAX_CHAR = 26 ; static void findAndPrintUncommonChars ( String str1 , String str2 ) { int present [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { present [ i ] = 0 ; } int l1 = str1 . length ( ) ; int l2 = str2 . length ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) { present [ str1 . charAt ( i ) - ' a ' ] = 1 ; } for ( int i = 0 ; i < l2 ; i ++ ) { if ( present [ str2 . charAt ( i ) - ' a ' ] == 1 || present [ str2 . charAt ( i ) - ' a ' ] == - 1 ) { present [ str2 . charAt ( i ) - ' a ' ] = - 1 ; } else { present [ str2 . charAt ( i ) - ' a ' ] = 2 ; } } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( present [ i ] == 1 || present [ i ] == 2 ) { System . out . print ( ( char ) ( i + ' a ' ) + " ▁ " ) ; } } } public static void main ( String [ ] args ) { String str1 = " characters " ; String str2 = " alphabets " ; findAndPrintUncommonChars ( str1 , str2 ) ; } }

MAX_CHAR = 26 NEW_LINE def findAndPrintUncommonChars ( str1 , str2 ) : NEW_LINE INDENT present = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT present [ i ] = 0 NEW_LINE DEDENT l1 = len ( str1 ) NEW_LINE l2 = len ( str2 ) NEW_LINE for i in range ( 0 , l1 ) : NEW_LINE INDENT present [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] = 1 NEW_LINE DEDENT for i in range ( 0 , l2 ) : NEW_LINE INDENT if ( present [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] == 1 or present [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] == - 1 ) : NEW_LINE INDENT present [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] = - 1 NEW_LINE DEDENT else : NEW_LINE INDENT present [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] = 2 NEW_LINE DEDENT DEDENT for i in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT if ( present [ i ] == 1 or present [ i ] == 2 ) : NEW_LINE INDENT print ( chr ( i + ord ( ' a ' ) ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " characters " NEW_LINE str2 = " alphabets " NEW_LINE findAndPrintUncommonChars ( str1 , str2 ) NEW_LINE DEDENT


class GFG { static int countDivisibles ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] % arr [ j ] == 0 || arr [ j ] % arr [ i ] == 0 ) res ++ ; return res ; } public static void main ( String [ ] args ) { int a [ ] = new int [ ] { 1 , 2 , 3 , 9 } ; int n = a . length ; System . out . print ( countDivisibles ( a , n ) ) ; } }

def countDivisibles ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] % arr [ j ] == 0 or arr [ j ] % arr [ i ] == 0 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 2 , 3 , 9 ] NEW_LINE n = len ( a ) NEW_LINE print ( countDivisibles ( a , n ) ) NEW_LINE DEDENT


class GFG { static String nextGreater ( String num ) { int l = num . length ( ) ; int i ; for ( i = l - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '0' ) { num = num . substring ( 0 , i ) + '1' + num . substring ( i + 1 ) ; break ; } else { num = num . substring ( 0 , i ) + '0' + num . substring ( i + 1 ) ; } } if ( i < 0 ) { num = "1" + num ; } return num ; } public static void main ( String [ ] args ) { String num = "10011" ; System . out . println ( " Binary ▁ representation ▁ of ▁ next ▁ number ▁ = ▁ " + nextGreater ( num ) ) ; } }

def nextGreater ( num1 ) : NEW_LINE INDENT l = len ( num1 ) ; NEW_LINE num = list ( num1 ) ; NEW_LINE i = l - 1 ; NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( num [ i ] == '0' ) : NEW_LINE INDENT num [ i ] = '1' ; NEW_LINE break ; NEW_LINE DEDENT else : NEW_LINE INDENT num [ i ] = '0' ; NEW_LINE DEDENT i -= 1 ; NEW_LINE DEDENT num1 = ' ' . join ( num ) ; NEW_LINE if ( i < 0 ) : NEW_LINE INDENT num1 = '1' + num1 ; NEW_LINE DEDENT return num1 ; NEW_LINE DEDENT num = "10011" ; NEW_LINE print ( " Binary ▁ representation ▁ of ▁ next ▁ number ▁ = ▁ " , nextGreater ( num ) ) ; NEW_LINE


import java . util . * ; class GFG { static boolean isPrime ( int n ) { int i = 2 ; while ( i * i <= n ) { if ( n % i == 0 ) return false ; i ++ ; } return true ; } static int minimumSum ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; if ( isPrime ( n - 2 ) ) return 2 ; return 3 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( minimumSum ( n ) ) ; } }

def isPrime ( n ) : NEW_LINE INDENT i = 2 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def minimumSum ( n ) : NEW_LINE INDENT if ( isPrime ( n ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( isPrime ( n - 2 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT return 3 NEW_LINE DEDENT n = 27 NEW_LINE print ( minimumSum ( n ) ) NEW_LINE


public class GFG { static int countOddPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( ( A [ i ] % 2 != 1 ) ) { count ++ ; } } int evenPairCount = count * ( count - 1 ) / 2 ; int totPairs = N * ( N - 1 ) / 2 ; return totPairs - evenPairCount ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = A . length ; System . out . println ( countOddPair ( A , N ) ) ; } }

def countOddPair ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT if ( A [ i ] % 2 != 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT evenPairCount = count * ( count - 1 ) / 2 NEW_LINE totPairs = N * ( N - 1 ) / 2 NEW_LINE return ( int ) ( totPairs - evenPairCount ) NEW_LINE DEDENT A = [ 5 , 6 , 2 , 8 ] NEW_LINE N = len ( A ) NEW_LINE print ( countOddPair ( A , N ) ) NEW_LINE


class GFG { static int getMax ( int [ ] Arr ) { int max = Arr [ 0 ] ; for ( int i = 1 ; i < Arr . length ; i ++ ) if ( Arr [ i ] > max ) max = Arr [ i ] ; return max ; } static int maxPrimefactorNum ( int N ) { int [ ] arr = new int [ N + 1 ] ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( arr [ i ] == 0 ) for ( int j = 2 * i ; j <= N ; j += i ) arr [ j ] ++ ; arr [ i ] = 1 ; } return getMax ( arr ) ; } public static void main ( String [ ] args ) { int N = 40 ; System . out . println ( maxPrimefactorNum ( N ) ) ; } }

def maxPrimefactorNum ( N ) : NEW_LINE INDENT arr = [ 0 ] * ( N + 1 ) ; NEW_LINE i = 2 ; NEW_LINE while ( i * i <= N ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT for j in range ( 2 * i , N + 1 , i ) : NEW_LINE INDENT arr [ j ] += 1 ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE arr [ i ] = 1 ; NEW_LINE DEDENT return max ( arr ) ; NEW_LINE DEDENT N = 40 ; NEW_LINE print ( maxPrimefactorNum ( N ) ) ; NEW_LINE


import java . io . * ; class GFG { static int countDigits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n = n / 10 ; } return count ; } static int checkPrime ( int n ) { if ( n <= 1 ) return - 1 ; if ( n <= 3 ) return 0 ; if ( n % 2 == 0 || n % 3 == 0 ) return - 1 ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return - 1 ; return 0 ; } static void printPrimePoints ( int n ) { int count = countDigits ( n ) ; if ( count == 1 || count == 2 ) { System . out . print ( " - 1" ) ; return ; } boolean found = false ; for ( int i = 1 ; i < ( count - 1 ) ; i ++ ) { int left = n / ( ( int ) Math . pow ( 10 , count - i ) ) ; int right = n % ( ( int ) Math . pow ( 10 , count - i - 1 ) ) ; if ( checkPrime ( left ) == 0 && checkPrime ( right ) == 0 ) { System . out . print ( i + " ▁ " ) ; found = true ; } } if ( found == false ) System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 2317 ; printPrimePoints ( n ) ; } }

def countDigits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT count += 1 NEW_LINE n = n // 10 NEW_LINE DEDENT return count NEW_LINE DEDENT def checkPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT i = 5 NEW_LINE while i * i <= n : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def printPrimePoints ( n ) : NEW_LINE INDENT count = countDigits ( n ) NEW_LINE if ( count == 1 or count == 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT found = False NEW_LINE for i in range ( 1 , ( count - 1 ) ) : NEW_LINE INDENT left = n // ( pow ( 10 , count - i ) ) NEW_LINE right = n % ( pow ( 10 , count - i - 1 ) ) NEW_LINE if ( checkPrime ( left ) == 0 and checkPrime ( right ) == 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE found = True NEW_LINE DEDENT DEDENT if ( found == False ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2317 NEW_LINE printPrimePoints ( n ) NEW_LINE DEDENT


class GfG { static int returnWinner ( String s , int l ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; } int cnt = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] % 2 != 0 ) cnt ++ ; } if ( ( cnt == 0 ) || ( cnt & 1 ) == 1 ) return 1 ; else return 2 ; } public static void main ( String [ ] args ) { String s = " abaaab " ; int l = s . length ( ) ; int winner = returnWinner ( s , l ) ; System . out . println ( " Player - " + winner ) ; } }

def returnWinner ( s , l ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( 0 , l , 1 ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] % 2 != 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT if ( cnt == 0 or cnt & 1 == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " abaaab " NEW_LINE l = len ( s ) NEW_LINE winner = returnWinner ( s , l ) NEW_LINE print ( " Player - " , winner ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void solve ( int n ) { int n_2 = n / 2 ; String den ; den = "1" ; while ( n_2 -- > 0 ) den += '0' ; System . out . println ( 1 + " / " + den ) ; } public static void main ( String [ ] args ) { int N = 5 ; solve ( N ) ; } }

def solve ( n ) : NEW_LINE INDENT n_2 = n // 2 ; NEW_LINE den = "1" ; NEW_LINE while ( n_2 ) : NEW_LINE INDENT den += '0' ; NEW_LINE n_2 -= 1 NEW_LINE DEDENT print ( str ( 1 ) + " / " + str ( den ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE solve ( N ) ; NEW_LINE DEDENT


class FindDuplicate { void printRepeating ( int arr [ ] , int size ) { int i ; System . out . println ( " The ▁ repeating ▁ elements ▁ are ▁ : ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( arr [ Math . abs ( arr [ i ] ) ] >= 0 ) arr [ Math . abs ( arr [ i ] ) ] = - arr [ Math . abs ( arr [ i ] ) ] ; else System . out . print ( Math . abs ( arr [ i ] ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { FindDuplicate duplicate = new FindDuplicate ( ) ; int arr [ ] = { 1 , 2 , 3 , 1 , 3 , 6 , 6 } ; int arr_size = arr . length ; duplicate . printRepeating ( arr , arr_size ) ; } }

def printRepeating ( arr , size ) : NEW_LINE INDENT print ( " The ▁ repeating ▁ elements ▁ are : ▁ " ) NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT if arr [ abs ( arr [ i ] ) ] >= 0 : NEW_LINE INDENT arr [ abs ( arr [ i ] ) ] = - arr [ abs ( arr [ i ] ) ] NEW_LINE DEDENT else : NEW_LINE INDENT print ( abs ( arr [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 2 , 3 , 1 , 3 , 6 , 6 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printRepeating ( arr , arr_size ) NEW_LINE


import java . util . Collections ; import java . util . Vector ; class GFG { static int MinRemove ( int [ ] a , int n , int k ) { int [ ] cnt = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) cnt [ 0 ] ++ ; else if ( cnt [ a [ i ] - 1 ] > 0 ) { cnt [ a [ i ] - 1 ] -- ; cnt [ a [ i ] ] ++ ; } } return n - ( k * cnt [ k - 1 ] ) ; } public static void main ( String [ ] args ) { int a [ ] = { 0 , 1 , 2 , 3 , 4 , 0 , 1 , 0 , 1 , 2 , 3 , 4 } ; int k = 5 ; int n = a . length ; System . out . println ( MinRemove ( a , n , k ) ) ; } }

def MinRemove ( a , n , k ) : NEW_LINE INDENT cnt = [ 0 ] * k NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT cnt [ 0 ] += 1 ; NEW_LINE DEDENT elif ( cnt [ a [ i ] - 1 ] > 0 ) : NEW_LINE INDENT cnt [ a [ i ] - 1 ] -= 1 ; NEW_LINE cnt [ a [ i ] ] += 1 ; NEW_LINE DEDENT DEDENT return n - ( k * cnt [ k - 1 ] ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 0 , 1 , 2 , 3 , 4 , 0 , 1 , 0 , 1 , 2 , 3 , 4 ] NEW_LINE k = 5 ; NEW_LINE n = len ( a ) ; NEW_LINE print ( MinRemove ( a , n , k ) ) ; NEW_LINE DEDENT


class GFG { static int floorSqrt ( int x ) { if ( x == 0 || x == 1 ) return x ; int i = 1 , result = 1 ; while ( result <= x ) { i ++ ; result = i * i ; } return i - 1 ; } public static void main ( String [ ] args ) { int x = 11 ; System . out . print ( floorSqrt ( x ) ) ; } }

def floorSqrt ( x ) : NEW_LINE INDENT if ( x == 0 or x == 1 ) : NEW_LINE INDENT return x NEW_LINE DEDENT i = 1 ; result = 1 NEW_LINE while ( result <= x ) : NEW_LINE INDENT i += 1 NEW_LINE result = i * i NEW_LINE DEDENT return i - 1 NEW_LINE DEDENT x = 11 NEW_LINE print ( floorSqrt ( x ) ) NEW_LINE


import java . util . ArrayList ; class GFG { public static int nobleInteger ( int arr [ ] ) { int size = arr . length ; for ( int i = 0 ; i < size ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < size ; j ++ ) if ( arr [ i ] < arr [ j ] ) count ++ ; if ( count == arr [ i ] ) return arr [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 10 , 3 , 20 , 40 , 2 } ; int res = nobleInteger ( arr ) ; if ( res != - 1 ) System . out . println ( " The ▁ noble ▁ " + " integer ▁ is ▁ " + res ) ; else System . out . println ( " No ▁ Noble ▁ " + " Integer ▁ Found " ) ; } }

def nobleInteger ( arr , size ) : NEW_LINE INDENT for i in range ( 0 , size ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( 0 , size ) : NEW_LINE INDENT if ( arr [ i ] < arr [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count == arr [ i ] ) : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 10 , 3 , 20 , 40 , 2 ] NEW_LINE size = len ( arr ) NEW_LINE res = nobleInteger ( arr , size ) NEW_LINE if ( res != - 1 ) : NEW_LINE INDENT print ( " The ▁ noble ▁ integer ▁ is ▁ " , res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ Noble ▁ Integer ▁ Found " ) NEW_LINE DEDENT


class GFG { static int findPivot ( int arr [ ] , int low , int high ) { if ( high < low ) return - 1 ; if ( high == low ) return low ; int mid = ( low + high ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) { return mid ; } if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) { return mid - 1 ; } if ( arr [ low ] > arr [ mid ] ) { return findPivot ( arr , low , mid - 1 ) ; } else { return findPivot ( arr , mid + 1 , high ) ; } } public static boolean isRotated ( int arr [ ] , int n ) { int l = 0 ; int r = n - 1 ; int pivot = - 1 ; if ( arr [ l ] > arr [ r ] ) { pivot = findPivot ( arr , l , r ) ; if ( l < pivot ) { while ( pivot > l ) { if ( arr [ pivot ] < arr [ pivot - 1 ] ) { return false ; } pivot -- ; } } else { pivot ++ ; while ( pivot < r ) { if ( arr [ pivot ] > arr [ pivot + 1 ] ) { return false ; } pivot ++ ; } } return true ; } else { return false ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 5 , 1 , 2 } ; System . out . println ( isRotated ( arr , 5 ) ) ; } }

def findPivot ( arr , low , high ) : NEW_LINE INDENT if ( high < low ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( high == low ) : NEW_LINE INDENT return low ; NEW_LINE DEDENT mid = ( low + high ) // 2 ; NEW_LINE if ( mid < high and arr [ mid + 1 ] < arr [ mid ] ) : NEW_LINE INDENT return mid ; NEW_LINE DEDENT if ( mid > low and arr [ mid ] < arr [ mid - 1 ] ) : NEW_LINE INDENT return mid - 1 ; NEW_LINE DEDENT if ( arr [ low ] > arr [ mid ] ) : NEW_LINE INDENT return findPivot ( arr , low , mid - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return findPivot ( arr , mid + 1 , high ) ; NEW_LINE DEDENT DEDENT def isRotated ( arr , n ) : NEW_LINE INDENT l = 0 ; NEW_LINE r = n - 1 ; NEW_LINE pivot = - 1 ; NEW_LINE if ( arr [ l ] > arr [ r ] ) : NEW_LINE INDENT pivot = findPivot ( arr , l , r ) ; NEW_LINE if ( l < pivot ) : NEW_LINE INDENT while ( pivot > l ) : NEW_LINE INDENT if ( arr [ pivot ] < arr [ pivot - 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT pivot -= 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT pivot += 1 ; NEW_LINE while ( pivot < r ) : NEW_LINE INDENT if ( arr [ pivot ] > arr [ pivot + 1 ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT pivot + + 1 ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 4 , 5 , 1 , 2 ] ; NEW_LINE if ( isRotated ( arr , 5 ) ) : NEW_LINE INDENT print ( " True " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) ; NEW_LINE DEDENT DEDENT


public class GFG { static final int MAX_CHAR = 26 ; static boolean checkCorrectOrNot ( String s ) { int [ ] count = new int [ MAX_CHAR ] ; int n = s . length ( ) ; if ( n == 1 ) return true ; for ( int i = 0 , j = n - 1 ; i < j ; i ++ , j -- ) { count [ s . charAt ( i ) - ' a ' ] ++ ; count [ s . charAt ( j ) - ' a ' ] -- ; } for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count [ i ] != 0 ) return false ; return true ; } public static void main ( String args [ ] ) { String s = " abab " ; if ( checkCorrectOrNot ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

MAX_CHAR = 26 NEW_LINE def checkCorrectOrNot ( s ) : NEW_LINE INDENT global MAX_CHAR NEW_LINE count = [ 0 ] * MAX_CHAR NEW_LINE n = len ( s ) NEW_LINE if n == 1 : NEW_LINE INDENT return true NEW_LINE DEDENT i = 0 ; j = n - 1 NEW_LINE while i < j : NEW_LINE INDENT count [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE count [ ord ( s [ j ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE i += 1 ; j -= 1 NEW_LINE DEDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT if count [ i ] != 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT s = " abab " NEW_LINE print ( " Yes " if checkCorrectOrNot ( s ) else " No " ) NEW_LINE


class GFG { static int flipSign ( int a ) { int neg = 0 ; int tmp = a < 0 ? 1 : - 1 ; while ( a != 0 ) { neg += tmp ; a += tmp ; } return neg ; } static boolean areDifferentSign ( int a , int b ) { return ( ( a < 0 && b > 0 ) || ( a > 0 && b < 0 ) ) ; } static int sub ( int a , int b ) { return a + flipSign ( b ) ; } static int mul ( int a , int b ) { if ( a < b ) return mul ( b , a ) ; int sum = 0 ; for ( int i = Math . abs ( b ) ; i > 0 ; i -- ) sum += a ; if ( b < 0 ) sum = flipSign ( sum ) ; return sum ; } static int division ( int a , int b ) { if ( b == 0 ) throw new ArithmeticException ( ) ; int quotient = 0 , dividend ; int divisor = flipSign ( Math . abs ( b ) ) ; for ( dividend = Math . abs ( a ) ; dividend >= Math . abs ( divisor ) ; dividend += divisor ) quotient ++ ; if ( areDifferentSign ( a , b ) ) quotient = flipSign ( quotient ) ; return quotient ; } public static void main ( String [ ] args ) { System . out . println ( " Subtraction ▁ is ▁ " + sub ( 4 , - 2 ) ) ; System . out . println ( " Product ▁ is ▁ " + mul ( - 9 , 6 ) ) ; try { System . out . println ( " Division ▁ is ▁ " + division ( 8 , 2 ) ) ; } catch ( ArithmeticException e ) { System . out . println ( " Exception ▁ : - ▁ Divide ▁ by ▁ 0" ) ; } } }

def flipSign ( a ) : NEW_LINE INDENT neg = 0 ; NEW_LINE tmp = 1 if a < 0 else - 1 ; NEW_LINE while ( a != 0 ) : NEW_LINE INDENT neg += tmp ; NEW_LINE a += tmp ; NEW_LINE DEDENT return neg ; NEW_LINE DEDENT def areDifferentSign ( a , b ) : NEW_LINE INDENT return ( ( a < 0 and b > 0 ) or ( a > 0 and b < 0 ) ) ; NEW_LINE DEDENT def sub ( a , b ) : NEW_LINE INDENT return a + flipSign ( b ) ; NEW_LINE DEDENT def mul ( a , b ) : NEW_LINE INDENT if ( a < b ) : NEW_LINE INDENT return mul ( b , a ) ; NEW_LINE DEDENT sum = 0 ; NEW_LINE for i in range ( abs ( b ) , 0 , - 1 ) : NEW_LINE INDENT sum += a ; NEW_LINE DEDENT if ( b < 0 ) : NEW_LINE INDENT sum = flipSign ( sum ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def division ( a , b ) : NEW_LINE INDENT quotient = 0 ; NEW_LINE divisor = flipSign ( abs ( b ) ) ; NEW_LINE for dividend in range ( abs ( a ) , abs ( divisor ) + divisor , divisor ) : NEW_LINE INDENT quotient += 1 ; NEW_LINE DEDENT if ( areDifferentSign ( a , b ) ) : NEW_LINE INDENT quotient = flipSign ( quotient ) ; NEW_LINE DEDENT return quotient ; NEW_LINE DEDENT print ( " Subtraction ▁ is " , sub ( 4 , - 2 ) ) ; NEW_LINE print ( " Product ▁ is " , mul ( - 9 , 6 ) ) ; NEW_LINE a , b = 8 , 2 ; NEW_LINE if ( b ) : NEW_LINE INDENT print ( " Division ▁ is " , division ( a , b ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Exception ▁ : - ▁ Divide ▁ by ▁ 0" ) ; NEW_LINE DEDENT


class GFG { static void printPattern ( int n ) { int var1 , var = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) { var = var + i - 1 ; for ( int j = 1 ; j <= i ; j ++ ) { if ( j == 1 ) { System . out . print ( var ) ; } else System . out . print ( " * " + var ) ; var ++ ; } } else { var1 = var + i - 1 ; for ( int j = 1 ; j <= i ; j ++ ) { if ( j == 1 ) { System . out . print ( var1 ) ; } else { System . out . print ( " * " + var1 ) ; } var1 -- ; } } System . out . print ( " \n " ) ; } } public static void main ( String [ ] arg ) { int n = 5 ; printPattern ( n ) ; } }

def printPattern ( n ) : NEW_LINE INDENT var = 0 NEW_LINE var = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT var = var + i - 1 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if ( j == 1 ) : NEW_LINE INDENT print ( var , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE print ( var , end = " " ) NEW_LINE DEDENT var += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT var1 = var + i - 1 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if ( j == 1 ) : NEW_LINE INDENT print ( var1 , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE print ( var1 , end = " " ) NEW_LINE DEDENT var1 -= 1 NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE printPattern ( n ) NEW_LINE


import java . io . * ; public class GFG { static void printNonDivisible ( int [ ] A , int [ ] B , int n , int m ) { for ( int i = 0 ; i < m ; i ++ ) { int j = 0 ; for ( j = 0 ; j < n ; j ++ ) if ( B [ i ] % A [ j ] == 0 ) break ; if ( j == n ) System . out . println ( B [ i ] ) ; } } static public void main ( String [ ] args ) { int [ ] A = { 100 , 200 , 400 , 100 } ; int n = A . length ; int [ ] B = { 190 , 200 , 87 , 600 , 800 } ; int m = B . length ; printNonDivisible ( A , B , n , m ) ; } }

import math as mt NEW_LINE def printNonDivisible ( A , B , n , m ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT j = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( B [ i ] % A [ j ] == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == n - 1 ) : NEW_LINE INDENT print ( B [ i ] ) NEW_LINE DEDENT DEDENT DEDENT A = [ 100 , 200 , 400 , 100 ] NEW_LINE n = len ( A ) NEW_LINE B = [ 190 , 200 , 87 , 600 , 800 ] NEW_LINE m = len ( B ) NEW_LINE printNonDivisible ( A , B , n , m ) NEW_LINE


import java . io . * ; class GFG { static int digitProduct ( int n ) { int prod = 1 ; while ( n > 0 ) { prod = prod * ( n % 10 ) ; n = n / 10 ; } return prod ; } static void printMultiplicativePrimes ( int n ) { boolean prime [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= n ; i += p ) prime [ i ] = false ; } } for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] && prime [ digitProduct ( i ) ] ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; printMultiplicativePrimes ( n ) ; } }

from math import sqrt NEW_LINE def digitProduct ( n ) : NEW_LINE INDENT prod = 1 NEW_LINE while ( n ) : NEW_LINE INDENT prod = prod * ( n % 10 ) NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return prod NEW_LINE DEDENT def printMultiplicativePrimes ( n ) : NEW_LINE INDENT prime = [ True for i in range ( n + 1 ) ] NEW_LINE prime [ 0 ] = prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT if ( prime [ i ] and prime [ digitProduct ( i ) ] ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE printMultiplicativePrimes ( n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void prefixSum ( int [ ] p_arr , int [ ] arr , int n ) { p_arr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ] ; } static void qPrefixSum ( int [ ] p_arr , int n , HashSet < Integer > q ) { for ( int i = 0 ; i < n ; i ++ ) q . add ( p_arr [ i ] ) ; } static void canDivide ( int [ ] p_arr , int n , HashSet < Integer > q , int l , int r ) { int sum ; if ( l == 0 ) sum = p_arr [ r ] ; else sum = p_arr [ r ] - p_arr [ l - 1 ] ; if ( sum % 2 == 1 ) { System . out . println ( " No " ) ; return ; } int beg = 0 ; if ( l != 0 ) beg = p_arr [ l - 1 ] ; if ( q . contains ( beg + sum / 2 ) && ( beg + sum / 2 ) != ( int ) q . toArray ( ) [ q . size ( ) - 1 ] ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 2 , 3 } ; int n = arr . length ; int p_arr [ ] = new int [ n ] ; prefixSum ( p_arr , arr , n ) ; HashSet < Integer > q = new HashSet < > ( ) ; qPrefixSum ( p_arr , n , q ) ; canDivide ( p_arr , n , q , 0 , 1 ) ; canDivide ( p_arr , n , q , 1 , 3 ) ; canDivide ( p_arr , n , q , 1 , 2 ) ; } }

def prefixSum ( p_arr , arr , n ) : NEW_LINE INDENT p_arr [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT p_arr [ i ] = arr [ i ] + p_arr [ i - 1 ] NEW_LINE DEDENT DEDENT def hashPrefixSum ( p_arr , n , q ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT q [ p_arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT def canDivide ( p_arr , n , q , l , r ) : NEW_LINE INDENT Sum = 0 NEW_LINE if ( l == 0 ) : NEW_LINE INDENT Sum = p_arr [ r ] NEW_LINE DEDENT else : NEW_LINE INDENT Sum = p_arr [ r ] - p_arr [ l - 1 ] NEW_LINE DEDENT if ( Sum % 2 == 1 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT beg = 0 NEW_LINE if ( l != 0 ) : NEW_LINE INDENT beg = p_arr [ l - 1 ] NEW_LINE DEDENT if ( beg + Sum // 2 in q . keys ( ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE p_arr = [ 0 for i in range ( n ) ] NEW_LINE prefixSum ( p_arr , arr , n ) NEW_LINE q = dict ( ) NEW_LINE hashPrefixSum ( p_arr , n , q ) NEW_LINE canDivide ( p_arr , n , q , 0 , 1 ) NEW_LINE canDivide ( p_arr , n , q , 1 , 3 ) NEW_LINE canDivide ( p_arr , n , q , 1 , 2 ) NEW_LINE


class GFG { static void checkArray ( int a [ ] , int b [ ] , int n ) { int operations = 0 ; int i = 0 ; while ( i < n ) { if ( a [ i ] - b [ i ] == 0 ) { i ++ ; continue ; } int diff = a [ i ] - b [ i ] ; i ++ ; while ( i < n && a [ i ] - b [ i ] == diff ) { i ++ ; } operations ++ ; } System . out . println ( operations ) ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 7 , 1 , 4 , 1 , 2 } ; int b [ ] = { 3 , 7 , 3 , 6 , 3 , 2 } ; int size = a . length ; checkArray ( a , b , size ) ; } }

def checkArray ( a , b , n ) : NEW_LINE INDENT operations = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( a [ i ] - b [ i ] == 0 ) : NEW_LINE INDENT i += 1 ; NEW_LINE continue ; NEW_LINE DEDENT diff = a [ i ] - b [ i ] ; NEW_LINE i += 1 ; NEW_LINE while ( i < n and a [ i ] - b [ i ] == diff ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT operations += 1 ; NEW_LINE DEDENT print ( operations ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 3 , 7 , 1 , 4 , 1 , 2 ] ; NEW_LINE b = [ 3 , 7 , 3 , 6 , 3 , 2 ] ; NEW_LINE size = len ( a ) ; NEW_LINE checkArray ( a , b , size ) ; NEW_LINE DEDENT


class GFG { static int countdigits ( int N ) { int count = 0 ; while ( N > 0 ) { count ++ ; N = N / 10 ; } return count ; } static void cyclic ( int N ) { int num = N ; int n = countdigits ( N ) ; while ( true ) { System . out . println ( num ) ; int rem = num % 10 ; int dev = num / 10 ; num = ( int ) ( ( Math . pow ( 10 , n - 1 ) ) * rem + dev ) ; if ( num == N ) break ; } } public static void main ( String [ ] args ) { int N = 5674 ; cyclic ( N ) ; } }

import math NEW_LINE def countdigits ( N ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( N ) : NEW_LINE INDENT count = count + 1 ; NEW_LINE N = int ( math . floor ( N / 10 ) ) ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT def cyclic ( N ) : NEW_LINE INDENT num = N ; NEW_LINE n = countdigits ( N ) ; NEW_LINE while ( 1 ) : NEW_LINE INDENT print ( int ( num ) ) ; NEW_LINE rem = num % 10 ; NEW_LINE div = math . floor ( num / 10 ) ; NEW_LINE num = ( ( math . pow ( 10 , n - 1 ) ) * rem + div ) ; NEW_LINE if ( num == N ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT N = 5674 ; NEW_LINE cyclic ( N ) ; NEW_LINE


class GFG { static int findSum ( int [ ] arr , int n , int left , int right ) { int k = right - left ; int d = arr [ 1 ] - arr [ 0 ] ; int ans = arr [ left - 1 ] * ( k + 1 ) ; ans = ans + ( d * ( k * ( k + 1 ) ) ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 } ; int queries = 3 ; int q [ ] [ ] = { { 2 , 4 } , { 2 , 6 } , { 5 , 6 } } ; int n = arr . length ; for ( int i = 0 ; i < queries ; i ++ ) System . out . print ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) + " \n " ) ; } }

def findSum ( arr , n , left , right ) : NEW_LINE INDENT k = right - left ; NEW_LINE d = arr [ 1 ] - arr [ 0 ] ; NEW_LINE ans = arr [ left - 1 ] * ( k + 1 ) ; NEW_LINE ans = ans + ( d * ( k * ( k + 1 ) ) ) // 2 ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ] ; NEW_LINE queries = 3 ; NEW_LINE q = [ [ 2 , 4 ] , [ 2 , 6 ] , [ 5 , 6 ] ] ; NEW_LINE n = len ( arr ) ; NEW_LINE for i in range ( queries ) : NEW_LINE INDENT print ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) ; NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static void Find_Sequence ( int [ ] arr , int n ) { Vector < Integer > inc_arr = new Vector < > ( ) , dec_arr = new Vector < > ( ) ; int flag = 0 ; long inc = - 1 , dec = ( long ) 1e7 ; for ( int i = 0 ; i < n ; i ++ ) { if ( inc < arr [ i ] && arr [ i ] < dec ) { if ( arr [ i ] < arr [ i + 1 ] ) { inc = arr [ i ] ; inc_arr . add ( arr [ i ] ) ; } else { dec = arr [ i ] ; dec_arr . add ( arr [ i ] ) ; } } else if ( inc < arr [ i ] ) { inc = arr [ i ] ; inc_arr . add ( arr [ i ] ) ; } else if ( dec > arr [ i ] ) { dec = arr [ i ] ; dec_arr . add ( arr [ i ] ) ; } else { System . out . println ( - 1 ) ; flag = 1 ; break ; } } if ( flag == 0 ) { for ( int i : inc_arr ) System . out . print ( i + " ▁ " ) ; System . out . println ( ) ; for ( int i : dec_arr ) System . out . print ( i + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 1 , 3 , 6 , 8 , 2 , 9 , 0 , 10 } ; int n = arr . length ; Find_Sequence ( arr , n ) ; } }

def Find_Sequence ( array , n ) : NEW_LINE INDENT inc_arr , dec_arr = [ ] , [ ] NEW_LINE inc , dec = - 1 , 1e7 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if inc < array [ i ] < dec : NEW_LINE INDENT if array [ i ] < array [ i + 1 ] : NEW_LINE INDENT inc = array [ i ] NEW_LINE inc_arr . append ( array [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dec = array [ i ] NEW_LINE dec_arr . append ( array [ i ] ) NEW_LINE DEDENT DEDENT elif inc < array [ i ] : NEW_LINE INDENT inc = array [ i ] NEW_LINE inc_arr . append ( array [ i ] ) NEW_LINE DEDENT elif dec > array [ i ] : NEW_LINE INDENT dec = array [ i ] NEW_LINE dec_arr . append ( array [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' - 1' ) NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( inc_arr , dec_arr ) NEW_LINE DEDENT DEDENT arr = [ 5 , 1 , 3 , 6 , 8 , 2 , 9 , 0 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE Find_Sequence ( arr , n ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class Gfg { static void Digits ( int n ) { int largest = 0 ; int smallest = 9 ; while ( n != 0 ) { int r = n % 10 ; largest = Math . max ( r , largest ) ; smallest = Math . min ( r , smallest ) ; n = n / 10 ; } System . out . println ( largest + " ▁ " + smallest ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 2346 ; Digits ( n ) ; } }

def Digits ( n ) : NEW_LINE INDENT largest = 0 NEW_LINE smallest = 9 NEW_LINE while ( n ) : NEW_LINE INDENT r = n % 10 NEW_LINE largest = max ( r , largest ) NEW_LINE smallest = min ( r , smallest ) NEW_LINE n = n // 10 NEW_LINE DEDENT print ( largest , smallest ) NEW_LINE DEDENT n = 2346 NEW_LINE Digits ( n ) NEW_LINE


import java . io . * ; class Series { static int sumOfSeries ( int n ) { return n * ( n + 1 ) * ( 6 * n * n * n + 9 * n * n + n - 1 ) / 30 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( sumOfSeries ( n ) ) ; } }

def sumOfSeries ( n ) : NEW_LINE INDENT return n * ( n + 1 ) * ( 6 * n * n * n + 9 * n * n + n - 1 ) / 30 NEW_LINE DEDENT n = 4 NEW_LINE print sumOfSeries ( n ) NEW_LINE


class GFG { static int p = 1000000007 ; static long power ( long x , long y ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res % p ; } public static void main ( String [ ] args ) { long n = 3 ; long x = ( power ( 3 , n ) % p + 1 ) % p ; x = ( x - power ( 2 , n + 1 ) + p ) % p ; x = ( x * power ( 2 , p - 2 ) ) % p ; System . out . println ( x ) ; } }

p = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res % p NEW_LINE DEDENT n = 3 NEW_LINE x = ( power ( 3 , n ) % p + 1 ) % p NEW_LINE x = ( x - power ( 2 , n + 1 ) + p ) % p NEW_LINE x = ( x * power ( 2 , p - 2 ) ) % p NEW_LINE print ( x ) NEW_LINE


import java . util . Arrays ; class GFG { static void printKMissing ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < n && arr [ i ] <= 0 ) i ++ ; int count = 0 , curr = 1 ; while ( count < k && i < n ) { if ( arr [ i ] != curr ) { System . out . print ( curr + " ▁ " ) ; count ++ ; } else i ++ ; curr ++ ; } while ( count < k ) { System . out . print ( curr + " ▁ " ) ; curr ++ ; count ++ ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 } ; int n = arr . length ; int k = 3 ; printKMissing ( arr , n , k ) ; } }

def printKMissing ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE i = 0 NEW_LINE while ( i < n and arr [ i ] <= 0 ) : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT count = 0 NEW_LINE curr = 1 NEW_LINE while ( count < k and i < n ) : NEW_LINE INDENT if ( arr [ i ] != curr ) : NEW_LINE INDENT print ( str ( curr ) + " ▁ " , end = ' ' ) NEW_LINE count = count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT curr = curr + 1 NEW_LINE DEDENT while ( count < k ) : NEW_LINE INDENT print ( str ( curr ) + " ▁ " , end = ' ' ) NEW_LINE curr = curr + 1 NEW_LINE count = count + 1 NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE printKMissing ( arr , n , k ) ; NEW_LINE


import java . util . * ; class GFG { public static int countPairs ( int arr [ ] , int n ) { HashMap < Integer , Integer > hm = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hm . containsKey ( arr [ i ] ) ) hm . put ( arr [ i ] , hm . get ( arr [ i ] ) + 1 ) ; else hm . put ( arr [ i ] , 1 ) ; } int ans = 0 ; for ( Map . Entry < Integer , Integer > it : hm . entrySet ( ) ) { int count = it . getValue ( ) ; ans += ( count * ( count - 1 ) ) / 2 ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 1 , 2 , 3 , 1 } ; System . out . println ( countPairs ( arr , arr . length ) ) ; } }

import math as mt NEW_LINE def countPairs ( arr , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for it in mp : NEW_LINE INDENT count = mp [ it ] NEW_LINE ans += ( count * ( count - 1 ) ) // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static int countTrees ( int n ) { int BT [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) BT [ i ] = 0 ; BT [ 0 ] = BT [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; ++ i ) for ( int j = 0 ; j < i ; j ++ ) BT [ i ] += BT [ j ] * BT [ i - j - 1 ] ; return BT [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " Total ▁ Possible ▁ " + " Binary ▁ Trees ▁ are ▁ : ▁ " + countTrees ( n ) ) ; } }

def countTrees ( n ) : NEW_LINE INDENT BT = [ 0 ] * ( n + 1 ) NEW_LINE BT [ 0 ] = BT [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT BT [ i ] += BT [ j ] * BT [ i - j - 1 ] NEW_LINE DEDENT DEDENT return BT [ n ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( " Total ▁ Possible ▁ Binary ▁ Trees ▁ are ▁ : ▁ " , countTrees ( n ) ) NEW_LINE DEDENT


import java . util . Vector ; public class GFG { static int max_val ( int [ ] arr ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < arr . length ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; } } return max ; } static void compositeSumProduct ( int arr [ ] , int n , int k ) { int max_val = max_val ( arr ) ; Vector < Boolean > prime = new Vector < > ( ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) { prime . add ( i , Boolean . TRUE ) ; } for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime . get ( p ) == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) { prime . add ( i , Boolean . FALSE ) ; } } } int sum = 0 , product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! prime . get ( arr [ i ] ) && arr [ i ] % k == 0 ) { sum += arr [ i ] ; product *= arr [ i ] ; } } System . out . println ( " Sum ▁ of ▁ composite ▁ numbers ▁ " + " divisible ▁ by ▁ k ▁ is ▁ " + sum ) ; System . out . println ( " \n Product ▁ of ▁ composite ▁ numbers " + " ▁ divisible ▁ by ▁ k ▁ is ▁ " + product ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; int k = 2 ; compositeSumProduct ( arr , n , k ) ; } }

from math import sqrt , ceil NEW_LINE def compositeSumProduct ( arr , n , k ) : NEW_LINE INDENT max_val = arr [ 0 ] ; NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( max_val < arr [ i ] ) : NEW_LINE INDENT max_val = arr [ i ] NEW_LINE DEDENT DEDENT prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE k = int ( sqrt ( max_val ) ) NEW_LINE for p in range ( 2 , k + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , max_val , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE product = 1 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] == False and arr [ i ] % k == 0 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE product *= arr [ i ] NEW_LINE DEDENT DEDENT print ( " Sum ▁ of ▁ composite ▁ numbers " , " divisible ▁ by ▁ k ▁ is " , sum ) NEW_LINE print ( " Product ▁ of ▁ composite ▁ numbers " , " divisible ▁ by ▁ k ▁ is " , product ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE compositeSumProduct ( arr , n , k ) NEW_LINE DEDENT


class GFG { static final int MAX = 32768 ; static int [ ] arr = new int [ 2 * MAX ] ; static void gouldSequence ( ) { arr [ 0 ] = 1 ; int i = 1 ; int p = 1 ; while ( i <= MAX ) { int j = 0 ; while ( j < i ) { arr [ i + j ] = 2 * arr [ j ] ; j ++ ; } i = ( 1 << p ) ; p ++ ; } } static void printSequence ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { gouldSequence ( ) ; int n = 16 ; printSequence ( n ) ; } }

MAX = 32768 NEW_LINE arr = [ None ] * ( 2 * MAX ) NEW_LINE def gouldSequence ( ) : NEW_LINE INDENT arr [ 0 ] = 1 NEW_LINE i = 1 NEW_LINE p = 1 NEW_LINE while i <= MAX : NEW_LINE INDENT j = 0 NEW_LINE while j < i : NEW_LINE INDENT arr [ i + j ] = 2 * arr [ j ] NEW_LINE j += 1 NEW_LINE DEDENT i = ( 1 << p ) NEW_LINE p += 1 NEW_LINE DEDENT DEDENT def printSequence ( n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT gouldSequence ( ) NEW_LINE n = 16 NEW_LINE printSequence ( n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int [ ] Findpermutation ( int n ) { int [ ] a = new int [ n + 1 ] ; a [ 1 ] = n ; for ( int i = 2 ; i <= n ; i ++ ) a [ i ] = i - 1 ; return a ; } public static void main ( String [ ] args ) { int n = 8 ; int [ ] v = Findpermutation ( n ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( v [ i ] + " ▁ " ) ; } }

def Findpermutation ( n ) : NEW_LINE INDENT a = [ 0 ] * ( n + 1 ) ; NEW_LINE a [ 1 ] = n ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT a [ i ] = i - 1 ; NEW_LINE DEDENT return a ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 ; NEW_LINE v = Findpermutation ( n ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( v [ i ] , end = ' ▁ ' ) ; NEW_LINE DEDENT DEDENT


class GFG { static class Node { int data ; Node next , prev ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; return newNode ; } static Node insertEnd ( Node head , Node new_node ) { if ( head == null ) { new_node . next = new_node . prev = new_node ; head = new_node ; return head ; } Node last = ( head ) . prev ; new_node . next = head ; ( head ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; return head ; } static Node reverse ( Node head ) { if ( head == null ) return null ; Node new_head = null ; Node last = head . prev ; Node curr = last , prev ; while ( curr . prev != last ) { prev = curr . prev ; new_head = insertEnd ( new_head , curr ) ; curr = prev ; } new_head = insertEnd ( new_head , curr ) ; return new_head ; } static void display ( Node head ) { if ( head == null ) return ; Node temp = head ; System . out . print ( " Forward ▁ direction : ▁ " ) ; while ( temp . next != head ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . print ( temp . data + " ▁ " ) ; Node last = head . prev ; temp = last ; System . out . print ( " \n Backward ▁ direction : ▁ " ) ; while ( temp . prev != last ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . prev ; } System . out . print ( temp . data + " ▁ " ) ; } public static void main ( String args [ ] ) { Node head = null ; head = insertEnd ( head , getNode ( 1 ) ) ; head = insertEnd ( head , getNode ( 2 ) ) ; head = insertEnd ( head , getNode ( 3 ) ) ; head = insertEnd ( head , getNode ( 4 ) ) ; head = insertEnd ( head , getNode ( 5 ) ) ; System . out . print ( " Current ▁ list : \n " ) ; display ( head ) ; head = reverse ( head ) ; System . out . print ( " \n \n Reversed ▁ list : \n " ) ; display ( head ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getNode ( data ) : NEW_LINE INDENT newNode = Node ( data ) NEW_LINE newNode . data = data NEW_LINE return newNode NEW_LINE DEDENT def insertEnd ( head , new_node ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT new_node . next = new_node NEW_LINE new_node . prev = new_node NEW_LINE head = new_node NEW_LINE return head NEW_LINE DEDENT last = head . prev NEW_LINE new_node . next = head NEW_LINE head . prev = new_node NEW_LINE new_node . prev = last NEW_LINE last . next = new_node NEW_LINE return head NEW_LINE DEDENT def reverse ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT new_head = None NEW_LINE last = head . prev NEW_LINE curr = last NEW_LINE while ( curr . prev != last ) : NEW_LINE INDENT prev = curr . prev NEW_LINE new_head = insertEnd ( new_head , curr ) NEW_LINE curr = prev NEW_LINE DEDENT new_head = insertEnd ( new_head , curr ) NEW_LINE return new_head NEW_LINE DEDENT def display ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT temp = head NEW_LINE print ( " Forward ▁ direction : ▁ " , end = " " ) NEW_LINE while ( temp . next != head ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( temp . data ) NEW_LINE last = head . prev NEW_LINE temp = last NEW_LINE print ( " Backward ▁ direction : ▁ " , end = " " ) NEW_LINE while ( temp . prev != last ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . prev NEW_LINE DEDENT print ( temp . data ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = insertEnd ( head , getNode ( 1 ) ) NEW_LINE head = insertEnd ( head , getNode ( 2 ) ) NEW_LINE head = insertEnd ( head , getNode ( 3 ) ) NEW_LINE head = insertEnd ( head , getNode ( 4 ) ) NEW_LINE head = insertEnd ( head , getNode ( 5 ) ) NEW_LINE print ( " Current ▁ list : " ) NEW_LINE display ( head ) NEW_LINE head = reverse ( head ) NEW_LINE print ( " \n Reversed ▁ list : " ) NEW_LINE display ( head ) NEW_LINE DEDENT


class GFG { static int maxSize = 50 ; static double dp [ ] = new double [ maxSize ] ; static int v [ ] = new int [ maxSize ] ; static double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 5 ) return 6 ; if ( v [ x ] == 1 ) return dp [ x ] ; v [ x ] = 1 ; dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 ; return dp [ x ] ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( expectedSteps ( n - 1 ) ) ; } }

maxSize = 50 NEW_LINE dp = [ 0 ] * maxSize NEW_LINE v = [ 0 ] * maxSize NEW_LINE def expectedSteps ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( x <= 5 ) : NEW_LINE INDENT return 6 NEW_LINE DEDENT if ( v [ x ] ) : NEW_LINE INDENT return dp [ x ] NEW_LINE DEDENT v [ x ] = 1 NEW_LINE dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 NEW_LINE return dp [ x ] NEW_LINE DEDENT n = 10 NEW_LINE print ( round ( expectedSteps ( n - 1 ) , 5 ) ) NEW_LINE


import java . util . * ; class GFG { static int Hosoya ( int n , int m ) { if ( ( n == 0 && m == 0 ) || ( n == 1 && m == 0 ) || ( n == 1 && m == 1 ) || ( n == 2 && m == 1 ) ) return 1 ; if ( n > m ) return Hosoya ( n - 1 , m ) + Hosoya ( n - 2 , m ) ; else if ( m == n ) return Hosoya ( n - 1 , m - 1 ) + Hosoya ( n - 2 , m - 2 ) ; else return 0 ; } static void printHosoya ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( Hosoya ( i , j ) + " ▁ " ) ; System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printHosoya ( n ) ; } }

def Hosoya ( n , m ) : NEW_LINE INDENT if ( ( n == 0 and m == 0 ) or ( n == 1 and m == 0 ) or ( n == 1 and m == 1 ) or ( n == 2 and m == 1 ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n > m : NEW_LINE INDENT return Hosoya ( n - 1 , m ) NEW_LINE INDENT + Hosoya ( n - 2 , m ) NEW_LINE DEDENT DEDENT elif m == n : NEW_LINE INDENT return Hosoya ( n - 1 , m - 1 ) NEW_LINE INDENT + Hosoya ( n - 2 , m - 2 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def printHosoya ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT print ( Hosoya ( i , j ) , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE printHosoya ( n ) NEW_LINE


public class AQES { static int LowerInsertionPoint ( int arr [ ] , int n , int X ) { if ( X < arr [ 0 ] ) return 0 ; else if ( X > arr [ n - 1 ] ) return n ; int lowerPnt = 0 ; int i = 1 ; while ( i < n && arr [ i ] < X ) { lowerPnt = i ; i = i * 2 ; } while ( lowerPnt < n && arr [ lowerPnt ] < X ) lowerPnt ++ ; return lowerPnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 4 , 5 , 6 , 7 , 9 } ; int n = arr . length ; int X = 4 ; System . out . println ( LowerInsertionPoint ( arr , n , X ) ) ; } }

def LowerInsertionPoint ( arr , n , X ) : NEW_LINE INDENT if ( X < arr [ 0 ] ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT elif ( X > arr [ n - 1 ] ) : NEW_LINE INDENT return n NEW_LINE DEDENT lowerPnt = 0 NEW_LINE i = 1 NEW_LINE while ( i < n and arr [ i ] < X ) : NEW_LINE INDENT lowerPnt = i NEW_LINE i = i * 2 NEW_LINE DEDENT while ( lowerPnt < n and arr [ lowerPnt ] < X ) : NEW_LINE INDENT lowerPnt += 1 NEW_LINE DEDENT return lowerPnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 4 , 5 , 6 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE X = 4 NEW_LINE print ( LowerInsertionPoint ( arr , n , X ) ) NEW_LINE DEDENT


class GFG { static void maximumSuffix ( String s , int n , int k ) { int i = s . length ( ) - 1 ; int arr [ ] = new int [ 26 ] ; String suffix = " " ; while ( i > - 1 ) { int index = s . charAt ( i ) - ' a ' ; if ( arr [ index ] < n ) { arr [ index ] ++ ; suffix += s . charAt ( i ) ; i -- ; continue ; } if ( k == 0 ) break ; k -- ; i -- ; } suffix = reverse ( suffix ) ; System . out . print ( suffix ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String str = " iahagafedcba " ; int n = 1 , k = 2 ; maximumSuffix ( str , n , k ) ; } }

def maximumSuffix ( s , n , k ) : NEW_LINE INDENT i = len ( s ) - 1 NEW_LINE arr = [ 0 for i in range ( 26 ) ] NEW_LINE suffix = " " NEW_LINE while ( i > - 1 ) : NEW_LINE INDENT index = ord ( s [ i ] ) - ord ( ' a ' ) ; NEW_LINE if ( arr [ index ] < n ) : NEW_LINE INDENT arr [ index ] += 1 NEW_LINE suffix += s [ i ] NEW_LINE i -= 1 NEW_LINE continue NEW_LINE DEDENT if ( k == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT k -= 1 NEW_LINE i -= 1 NEW_LINE DEDENT suffix = suffix [ : : - 1 ] NEW_LINE print ( suffix ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " iahagafedcba " NEW_LINE n = 1 NEW_LINE k = 2 NEW_LINE maximumSuffix ( str , n , k ) NEW_LINE DEDENT


class GFG { static int countHexadecimal ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( i >= 10 && i <= 15 ) count ++ ; else if ( i > 15 ) { int k = i ; while ( k != 0 ) { if ( k % 16 >= 10 ) count ++ ; k = k / 16 ; } } } return count ; } public static void main ( String args [ ] ) { int L = 5 , R = 100 ; System . out . print ( countHexadecimal ( L , R ) ) ; } }

def countHexadecimal ( L , R ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( i >= 10 and i <= 15 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT elif ( i > 15 ) : NEW_LINE INDENT k = i ; NEW_LINE while ( k != 0 ) : NEW_LINE INDENT if ( k % 16 >= 10 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT k = k // 16 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 5 ; R = 100 ; NEW_LINE print ( countHexadecimal ( L , R ) ) ; NEW_LINE DEDENT


public class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static void updateParent ( int parent [ ] , Node node ) { if ( node == null ) return ; if ( node . left != null ) parent [ node . left . data ] = node . data ; if ( node . right != null ) parent [ node . right . data ] = node . data ; updateParent ( parent , node . left ) ; updateParent ( parent , node . right ) ; } static int getMaxXor ( Node root , int n , int val , int x ) { int parent [ ] = new int [ n + 1 ] ; updateParent ( parent , root ) ; int max = x ^ val ; val = parent [ val ] ; while ( val != 0 ) { max = Math . max ( max , x ^ val ) ; val = parent [ val ] ; } return max ; } public static void main ( String [ ] args ) { int n = 6 ; Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; root . right . right . left = new Node ( 6 ) ; int val = 6 , x = 4 ; System . out . println ( getMaxXor ( root , n , val , x ) ) ; } }

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 updateParent ( parent , node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return parent NEW_LINE DEDENT if ( node . left != None ) : NEW_LINE INDENT parent [ node . left . data ] = node . data NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT parent [ node . right . data ] = node . data NEW_LINE DEDENT parent = updateParent ( parent , node . left ) NEW_LINE parent = updateParent ( parent , node . right ) NEW_LINE return parent NEW_LINE DEDENT def getMaxXor ( root , n , val , x ) : NEW_LINE INDENT parent = [ 0 ] * ( n + 1 ) NEW_LINE parent = updateParent ( parent , root ) NEW_LINE maximum = x ^ val NEW_LINE val = parent [ val ] NEW_LINE while ( val != 0 ) : NEW_LINE INDENT maximum = max ( maximum , x ^ val ) NEW_LINE val = parent [ val ] NEW_LINE DEDENT return maximum NEW_LINE DEDENT n = 6 NEW_LINE root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 5 ) NEW_LINE root . right . right . left = Node ( 6 ) NEW_LINE val = 6 NEW_LINE x = 4 NEW_LINE print ( getMaxXor ( root , n , val , x ) ) NEW_LINE


class GFG { static int number_of_ways ( int n ) { int [ ] includes_3 = new int [ n + 1 ] ; int [ ] not_includes_3 = new int [ n + 1 ] ; includes_3 [ 3 ] = 1 ; not_includes_3 [ 1 ] = 1 ; not_includes_3 [ 2 ] = 2 ; not_includes_3 [ 3 ] = 3 ; for ( int i = 4 ; i <= n ; i ++ ) { includes_3 [ i ] = includes_3 [ i - 1 ] + includes_3 [ i - 2 ] + not_includes_3 [ i - 3 ] ; not_includes_3 [ i ] = not_includes_3 [ i - 1 ] + not_includes_3 [ i - 2 ] ; } return includes_3 [ n ] ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( number_of_ways ( n ) ) ; } }

def number_of_ways ( n ) : NEW_LINE INDENT includes_3 = [ 0 ] * ( n + 1 ) NEW_LINE not_includes_3 = [ 0 ] * ( n + 1 ) NEW_LINE includes_3 [ 3 ] = 1 NEW_LINE not_includes_3 [ 1 ] = 1 NEW_LINE not_includes_3 [ 2 ] = 2 NEW_LINE not_includes_3 [ 3 ] = 3 NEW_LINE for i in range ( 4 , n + 1 ) : NEW_LINE INDENT includes_3 [ i ] = includes_3 [ i - 1 ] +   \ NEW_LINE INDENT includes_3 [ i - 2 ] +   \ NEW_LINE not_includes_3 [ i - 3 ] NEW_LINE DEDENT not_includes_3 [ i ] = not_includes_3 [ i - 1 ] +   \ NEW_LINE INDENT not_includes_3 [ i - 2 ] NEW_LINE DEDENT DEDENT return includes_3 [ n ] NEW_LINE DEDENT n = 7 NEW_LINE print ( number_of_ways ( n ) ) NEW_LINE


class GFG { static int equivalentBase4 ( String bin ) { if ( bin . compareTo ( "00" ) == 0 ) return 0 ; if ( bin . compareTo ( "01" ) == 0 ) return 1 ; if ( bin . compareTo ( "10" ) == 0 ) return 2 ; return 3 ; } static String isDivisibleBy5 ( String bin ) { int l = bin . length ( ) ; if ( l % 2 != 0 ) bin = '0' + bin ; int odd_sum = 0 , even_sum = 0 ; int isOddDigit = 1 ; for ( int i = 0 ; i < bin . length ( ) ; i += 2 ) { if ( isOddDigit != 0 ) odd_sum += equivalentBase4 ( bin . substring ( i , i + 2 ) ) ; else even_sum += equivalentBase4 ( bin . substring ( i , i + 2 ) ) ; isOddDigit ^= 1 ; } if ( Math . abs ( odd_sum - even_sum ) % 5 == 0 ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { String bin = "10000101001" ; System . out . println ( isDivisibleBy5 ( bin ) ) ; } }

def equivalentBase4 ( bin ) : NEW_LINE INDENT if ( bin == "00" ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( bin == "01" ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( bin == "10" ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( bin == "11" ) : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT def isDivisibleBy5 ( bin ) : NEW_LINE INDENT l = len ( bin ) NEW_LINE if ( ( l % 2 ) == 1 ) : NEW_LINE INDENT bin = '0' + bin NEW_LINE DEDENT odd_sum = 0 NEW_LINE even_sum = 0 NEW_LINE isOddDigit = 1 NEW_LINE for i in range ( 0 , len ( bin ) , 2 ) : NEW_LINE INDENT if ( isOddDigit ) : NEW_LINE INDENT odd_sum += equivalentBase4 ( bin [ i : i + 2 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT even_sum += equivalentBase4 ( bin [ i : i + 2 ] ) NEW_LINE DEDENT isOddDigit = isOddDigit ^ 1 NEW_LINE DEDENT if ( abs ( odd_sum - even_sum ) % 5 == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT bin = "10000101001" NEW_LINE print ( isDivisibleBy5 ( bin ) ) NEW_LINE DEDENT


class GFG { static final int DP_s = 9 ; static int getNumMonotone ( int len ) { int [ ] [ ] DP = new int [ len ] [ DP_s ] ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 ] [ i ] = i + 1 ; for ( int i = 0 ; i < len ; ++ i ) DP [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ] ; return DP [ len - 1 ] [ DP_s - 1 ] ; } public static void main ( String [ ] args ) { System . out . println ( getNumMonotone ( 10 ) ) ; } }

DP_s = 9 NEW_LINE def getNumMonotone ( ln ) : NEW_LINE INDENT DP = [ [ 0 ] * DP_s for i in range ( ln ) ] NEW_LINE for i in range ( DP_s ) : NEW_LINE INDENT DP [ 0 ] [ i ] = i + 1 NEW_LINE DEDENT for i in range ( ln ) : NEW_LINE INDENT DP [ i ] [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 1 , ln ) : NEW_LINE INDENT for j in range ( 1 , DP_s ) : NEW_LINE INDENT DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT return DP [ ln - 1 ] [ DP_s - 1 ] NEW_LINE DEDENT print ( getNumMonotone ( 10 ) ) NEW_LINE


import java . util . * ; class GFG { static int lis ( int [ ] arr , int n ) { int max = 0 ; int [ ] lst = new int [ n ] ; Arrays . fill ( lst , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && lst [ i ] < lst [ j ] + 1 ) lst [ i ] = lst [ j ] + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) if ( max < lst [ i ] ) max = lst [ i ] ; return max ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ lis ▁ is ▁ " + lis ( arr , n ) ) ; } }

def lis ( arr , n ) : NEW_LINE INDENT i , j , maxm = 0 , 0 , 0 NEW_LINE lst = [ 1 for s in range ( n ) ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 0 , i ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] and lst [ i ] < lst [ j ] + 1 ) : NEW_LINE INDENT lst [ i ] = lst [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if maxm < lst [ i ] : NEW_LINE INDENT maxm = lst [ i ] NEW_LINE DEDENT DEDENT return maxm NEW_LINE DEDENT arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ of ▁ lst ▁ is " , lis ( arr , n ) ) NEW_LINE


class GFG { static int findSubsequence ( int arr [ ] , int n ) { int len = 1 ; int tmp ; int i , j , d ; int [ ] [ ] dp = new int [ n ] [ 10 ] ; int [ ] cnt = new int [ 10 ] ; int locMax ; tmp = arr [ 0 ] ; while ( tmp > 0 ) { dp [ 0 ] [ tmp % 10 ] = 1 ; tmp /= 10 ; } for ( i = 1 ; i < n ; i ++ ) { tmp = arr [ i ] ; locMax = 1 ; for ( int x = 0 ; x < 10 ; x ++ ) cnt [ x ] = 0 ; while ( tmp > 0 ) { cnt [ tmp % 10 ] = 1 ; tmp /= 10 ; } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i ] [ d ] = 1 ; for ( j = 0 ; j < i ; j ++ ) { dp [ i ] [ d ] = Math . max ( dp [ i ] [ d ] , dp [ j ] [ d ] + 1 ) ; locMax = Math . max ( dp [ i ] [ d ] , locMax ) ; } } } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i ] [ d ] = locMax ; } } len = Math . max ( len , locMax ) ; } return len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 12 , 44 , 29 , 33 , 96 , 89 } ; int n = arr . length ; System . out . println ( findSubsequence ( arr , n ) ) ; } }

def findSubsequence ( arr , n ) : NEW_LINE INDENT Len = 1 NEW_LINE tmp = 0 NEW_LINE i , j , d = 0 , 0 , 0 NEW_LINE dp = [ [ 0 for i in range ( 10 ) ] for i in range ( n ) ] NEW_LINE cnt = [ 0 for i in range ( 10 ) ] NEW_LINE locMax = 0 NEW_LINE tmp = arr [ 0 ] NEW_LINE while ( tmp > 0 ) : NEW_LINE INDENT dp [ 0 ] [ tmp % 10 ] = 1 NEW_LINE tmp //= 10 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT tmp = arr [ i ] NEW_LINE locMax = 1 NEW_LINE cnt = [ 0 for i in range ( 10 ) ] NEW_LINE while ( tmp > 0 ) : NEW_LINE INDENT cnt [ tmp % 10 ] = 1 NEW_LINE tmp //= 10 NEW_LINE DEDENT for d in range ( 10 ) : NEW_LINE INDENT if ( cnt [ d ] ) : NEW_LINE INDENT dp [ i ] [ d ] = 1 NEW_LINE for j in range ( i ) : NEW_LINE INDENT dp [ i ] [ d ] = max ( dp [ i ] [ d ] , dp [ j ] [ d ] + 1 ) NEW_LINE locMax = max ( dp [ i ] [ d ] , locMax ) NEW_LINE DEDENT DEDENT DEDENT for d in range ( 10 ) : NEW_LINE INDENT if ( cnt [ d ] ) : NEW_LINE INDENT dp [ i ] [ d ] = locMax NEW_LINE DEDENT DEDENT Len = max ( Len , locMax ) NEW_LINE DEDENT return Len NEW_LINE DEDENT arr = [ 1 , 12 , 44 , 29 , 33 , 96 , 89 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSubsequence ( arr , n ) ) NEW_LINE


public class GFG { static int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static void icanobifNumbers ( int N ) { int first = 0 , second = 1 ; if ( N == 1 ) System . out . print ( first ) ; else if ( N == 2 ) System . out . print ( first + " ▁ " + second ) ; else { System . out . print ( first + " ▁ " + second + " ▁ " ) ; for ( int i = 3 ; i <= N ; i ++ ) { int x = reversDigits ( first ) ; int y = reversDigits ( second ) ; System . out . print ( x + y + " ▁ " ) ; int temp = second ; second = x + y ; first = temp ; } } } public static void main ( String [ ] args ) { int N = 12 ; icanobifNumbers ( N ) ; } }

def reversedigit ( num ) : NEW_LINE INDENT rev_num = 0 NEW_LINE while num > 0 : NEW_LINE INDENT rev_num = rev_num * 10 + num % 10 NEW_LINE num = num // 10 NEW_LINE DEDENT return rev_num NEW_LINE DEDENT def icanobifNumbers ( N ) : NEW_LINE INDENT first = 0 NEW_LINE second = 1 NEW_LINE if N == 1 : NEW_LINE INDENT print ( first ) NEW_LINE DEDENT elif N == 2 : NEW_LINE INDENT print ( first , second ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( first , second , end = " ▁ " ) NEW_LINE for i in range ( 3 , N + 1 ) : NEW_LINE INDENT x = reversedigit ( first ) NEW_LINE y = reversedigit ( second ) NEW_LINE print ( x + y , end = " ▁ " ) NEW_LINE temp = second NEW_LINE second = x + y NEW_LINE first = temp NEW_LINE DEDENT DEDENT DEDENT N = 12 NEW_LINE icanobifNumbers ( N ) NEW_LINE


class GFG { static final int MAX = 1000 ; static int max_prime [ ] = new int [ MAX ] ; static int min_prime [ ] = new int [ MAX ] ; static void sieve ( int n ) { for ( int i = 2 ; i < n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; int j = i + i ; while ( j < n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } static int findCost ( int A , int B , int C , int X ) { sieve ( MAX ) ; int N , M ; if ( X == 1 ) { return - 1 ; } int min_cost = C + X ; int cost_for_prime = A ; N = max_prime [ X ] ; if ( N != X ) { M = X / N ; cost_for_prime += M ; min_cost = Math . min ( min_cost , cost_for_prime ) ; } M = min_prime [ X ] ; N = X / M ; if ( N != min_prime [ N ] ) { int cost_for_comp = B + M ; min_cost = Math . min ( min_cost , cost_for_comp ) ; } return min_cost ; } public static void main ( String [ ] args ) { int A = 7 , B = 11 , C = 2 , X = 20 ; System . out . println ( findCost ( A , B , C , X ) ) ; } }

MAX = 10000 ; NEW_LINE max_prime = [ 0 ] * MAX ; NEW_LINE min_prime = [ 0 ] * MAX ; NEW_LINE def sieve ( n ) : NEW_LINE INDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( min_prime [ i ] > 0 ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT min_prime [ i ] = i ; NEW_LINE max_prime [ i ] = i ; NEW_LINE j = i + i ; NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( min_prime [ j ] == 0 ) : NEW_LINE INDENT min_prime [ j ] = i ; NEW_LINE DEDENT max_prime [ j ] = i ; NEW_LINE j += i ; NEW_LINE DEDENT DEDENT DEDENT def findCost ( A , B , C , X ) : NEW_LINE INDENT sieve ( MAX ) ; NEW_LINE if ( X == 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT min_cost = C + X ; NEW_LINE cost_for_prime = A ; NEW_LINE N = max_prime [ X ] ; NEW_LINE if ( N != X ) : NEW_LINE INDENT M = X // N ; NEW_LINE cost_for_prime += M ; NEW_LINE min_cost = min ( min_cost , cost_for_prime ) ; NEW_LINE DEDENT M = min_prime [ X ] ; NEW_LINE N = X // M ; NEW_LINE if ( N != min_prime [ N ] ) : NEW_LINE INDENT cost_for_comp = B + M ; NEW_LINE min_cost = min ( min_cost , cost_for_comp ) ; NEW_LINE DEDENT return min_cost ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 7 ; B = 11 ; C = 2 ; X = 20 ; NEW_LINE print ( findCost ( A , B , C , X ) ) ; NEW_LINE DEDENT


import java . util . * ; import java . io . * ; class GFG { static int binarySearch ( int arr [ ] , int low , int high , int x ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( x == arr [ mid ] ) return mid ; if ( x > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return - 1 ; } static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 , i ; Arrays . sort ( arr ) ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != - 1 ) count ++ ; return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }

def binarySearch ( arr , low , high , x ) : NEW_LINE INDENT if ( high >= low ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if x == arr [ mid ] : NEW_LINE INDENT return ( mid ) NEW_LINE DEDENT elif ( x > arr [ mid ] ) : NEW_LINE INDENT return binarySearch ( arr , ( mid + 1 ) , high , x ) NEW_LINE DEDENT else : NEW_LINE INDENT return binarySearch ( arr , low , ( mid - 1 ) , x ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def countPairsWithDiffK ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != - 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( arr , n , k ) ) NEW_LINE


import java . io . * ; public class gfg { public int digitsum ( int n ) { int r = n % 9 ; if ( r == 0 ) return 9 ; else return r ; } public int find ( int n , int d ) { int minimum = 10 ; for ( int i = 0 ; i < 9 ; i ++ ) { int current = ( n + i * d ) ; minimum = Math . min ( minimum , digitsum ( current ) ) ; } return minimum ; } } class geek { public static void main ( String [ ] args ) { gfg g = new gfg ( ) ; int n = 2546 , d = 124 ; System . out . println ( " Minimum ▁ possible ▁ digitsum ▁ is ▁ : ▁ " + ( g . find ( n , d ) ) ) ; } }

def digitsum ( n ) : NEW_LINE INDENT r = n % 9 ; NEW_LINE if ( r == 0 ) : NEW_LINE INDENT return 9 ; NEW_LINE DEDENT else : NEW_LINE INDENT return r ; NEW_LINE DEDENT DEDENT def find ( n , d ) : NEW_LINE INDENT minimum = 10 ; NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT current = ( n + i * d ) ; NEW_LINE minimum = min ( minimum , digitsum ( current ) ) ; NEW_LINE DEDENT return minimum ; NEW_LINE DEDENT n = 2546 ; NEW_LINE d = 124 ; NEW_LINE print ( " Minimum ▁ possible ▁ digitsum ▁ is ▁ : " , find ( n , d ) ) ; NEW_LINE


class GFG { static int countSubSeq ( int i , int sum , int cnt , int a [ ] , int n ) { if ( i == n ) { if ( sum == 0 && cnt > 0 ) { return 1 ; } else { return 0 ; } } int ans = 0 ; ans += countSubSeq ( i + 1 , sum , cnt , a , n ) ; ans += countSubSeq ( i + 1 , sum + a [ i ] , cnt + 1 , a , n ) ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { - 1 , 2 , - 2 , 1 } ; int n = a . length ; System . out . println ( countSubSeq ( 0 , 0 , 0 , a , n ) ) ; } }

def countSubSeq ( i , Sum , cnt , a , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT if ( Sum == 0 and cnt > 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE ans += countSubSeq ( i + 1 , Sum , cnt , a , n ) NEW_LINE ans += countSubSeq ( i + 1 , Sum + a [ i ] , cnt + 1 , a , n ) NEW_LINE return ans NEW_LINE DEDENT a = [ - 1 , 2 , - 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( countSubSeq ( 0 , 0 , 0 , a , n ) ) NEW_LINE


class GFG { static int countNumbers ( int x , int n ) { if ( x > n ) return 0 ; return 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ; } public static void main ( String [ ] args ) { int n = 120 ; System . out . println ( countNumbers ( 1 , n ) ) ; } }

def countNumbers ( x , n ) : NEW_LINE INDENT if x > n : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 120 ; NEW_LINE print ( countNumbers ( 1 , n ) ) ; NEW_LINE DEDENT


import java . util . Arrays ; public class GFG { static int solution ( int [ ] A ) { int n = A . length ; boolean [ ] present = new boolean [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] > 0 && A [ i ] <= n ) present [ A [ i ] ] = true ; } for ( int i = 1 ; i <= n ; i ++ ) if ( ! present [ i ] ) return i ; return n + 1 ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , 10 , 2 , - 10 , - 20 } ; System . out . println ( solution ( A ) ) ; } }

def solution ( A ) : NEW_LINE INDENT m = max ( A ) NEW_LINE if m < 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if len ( A ) == 1 : NEW_LINE INDENT return 2 if A [ 0 ] == 1 else 1 NEW_LINE DEDENT l = [ 0 ] * m NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if A [ i ] > 0 : NEW_LINE INDENT if l [ A [ i ] - 1 ] != 1 : NEW_LINE INDENT l [ A [ i ] - 1 ] = 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( len ( l ) ) : NEW_LINE INDENT if l [ i ] == 0 : NEW_LINE INDENT return i + 1 NEW_LINE DEDENT DEDENT return i + 2 NEW_LINE DEDENT A = [ 0 , 10 , 2 , - 10 , - 20 ] NEW_LINE print ( solution ( A ) ) NEW_LINE


import java . io . * ; class GFG { static void printUpperHalf ( String str ) { char first , second ; int pos = 0 ; for ( int i = 1 ; i <= 4 ; i ++ ) { first = str . charAt ( pos ) ; second = str . charAt ( pos + 1 ) ; pos += 2 ; for ( int j = 4 - i ; j >= 1 ; j -- ) System . out . print ( " ▁ " ) ; System . out . print ( first ) ; for ( int j = 1 ; j < i ; j ++ ) System . out . print ( " - - " ) ; System . out . println ( second ) ; } } static void printLowerHalf ( String str ) { char first , second ; int pos = 0 ; for ( int i = 1 ; i <= 4 ; i ++ ) { first = str . charAt ( pos ) ; second = str . charAt ( pos + 1 ) ; pos += 2 ; for ( int j = 1 ; j < i ; j ++ ) System . out . print ( " ▁ " ) ; System . out . print ( first ) ; for ( int j = 4 - i ; j >= 1 ; j -- ) System . out . print ( " - - " ) ; System . out . println ( second ) ; } } static void printDNA ( String str [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int x = i % 6 ; if ( x % 2 == 0 ) printUpperHalf ( str [ x ] ) ; else printLowerHalf ( str [ x ] ) ; } } public static void main ( String [ ] args ) { int n = 8 ; String DNA [ ] = { " ATTAATTA " , " TAGCTAGC " , " CGCGATAT " , " TAATATGC " , " ATCGTACG " , " CGTAGCAT " } ; printDNA ( DNA , n ) ; } }

import math NEW_LINE def printUpperHalf ( str ) : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE pos = 0 NEW_LINE for i in range ( 1 , 5 ) : NEW_LINE INDENT first = str [ pos ] NEW_LINE second = str [ pos + 1 ] NEW_LINE pos += 2 NEW_LINE for j in range ( 4 - i , 0 , - 1 ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT print ( first , end = " " ) NEW_LINE for j in range ( 1 , i ) : NEW_LINE INDENT print ( " - - " , end = " " ) NEW_LINE DEDENT print ( second ) NEW_LINE DEDENT DEDENT def printLowerHalf ( str ) : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE pos = 0 NEW_LINE for i in range ( 1 , 5 ) : NEW_LINE INDENT first = str [ pos ] NEW_LINE second = str [ pos + 1 ] NEW_LINE pos += 2 NEW_LINE for j in range ( 1 , i ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT print ( first , end = " " ) NEW_LINE for j in range ( 4 - i , 0 , - 1 ) : NEW_LINE INDENT print ( " - - " , end = " " ) NEW_LINE DEDENT print ( second ) NEW_LINE DEDENT DEDENT def printDNA ( str , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT x = i % 6 NEW_LINE if ( x % 2 == 0 ) : NEW_LINE INDENT printUpperHalf ( str [ x ] ) NEW_LINE DEDENT else : NEW_LINE INDENT printLowerHalf ( str [ x ] ) NEW_LINE DEDENT DEDENT DEDENT n = 8 NEW_LINE DNA = [ " ATTAATTA " , " TAGCTAGC " , " CGCGATAT " , " TAATATGC " , " ATCGTACG " , " CGTAGCAT " ] NEW_LINE printDNA ( DNA , n ) NEW_LINE


import java . util . Arrays ; import java . util . Collections ; public class GFG { static void twoWaySort ( Integer arr [ ] , int n ) { int l = 0 , r = n - 1 ; int k = 0 ; while ( l < r ) { while ( arr [ l ] % 2 != 0 ) { l ++ ; k ++ ; } while ( arr [ r ] % 2 == 0 && l < r ) r -- ; if ( l < r ) { int temp = arr [ l ] ; arr [ l ] = arr [ r ] ; arr [ r ] = temp ; } } Arrays . sort ( arr , 0 , k , Collections . reverseOrder ( ) ) ; Arrays . sort ( arr , k , n ) ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 1 , 3 , 2 , 7 , 5 , 4 } ; twoWaySort ( arr , arr . length ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }

def two_way_sort ( arr , arr_len ) : NEW_LINE INDENT l , r = 0 , arr_len - 1 NEW_LINE k = 0 NEW_LINE while ( l < r ) : NEW_LINE INDENT while ( arr [ l ] % 2 != 0 ) : NEW_LINE INDENT l += 1 NEW_LINE k += 1 NEW_LINE DEDENT while ( arr [ r ] % 2 == 0 and l < r ) : NEW_LINE INDENT r -= 1 NEW_LINE DEDENT if ( l < r ) : NEW_LINE INDENT arr [ l ] , arr [ r ] = arr [ r ] , arr [ l ] NEW_LINE DEDENT DEDENT odd = arr [ : k ] NEW_LINE even = arr [ k : ] NEW_LINE odd . sort ( reverse = True ) NEW_LINE even . sort ( ) NEW_LINE odd . extend ( even ) NEW_LINE return odd NEW_LINE DEDENT arr_len = 6 NEW_LINE arr = [ 1 , 3 , 2 , 7 , 5 , 4 ] NEW_LINE result = two_way_sort ( arr , arr_len ) NEW_LINE for i in result : NEW_LINE INDENT print ( str ( i ) + " ▁ " ) , NEW_LINE DEDENT


class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; static boolean isBSTUtil ( Node root , int prev ) { if ( root != null ) { if ( ! isBSTUtil ( root . left , prev ) ) return false ; if ( root . data <= prev ) return false ; prev = root . data ; return isBSTUtil ( root . right , prev ) ; } return true ; } static boolean isBST ( Node root ) { int prev = Integer . MIN_VALUE ; return isBSTUtil ( root , prev ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 5 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 15 ) ; 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 " ) ; } }

class newNode : 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 ) : NEW_LINE INDENT if ( not isBSTUtil ( root . left , prev ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( root . data <= prev ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev = root . data NEW_LINE return isBSTUtil ( root . right , prev ) NEW_LINE DEDENT return True NEW_LINE DEDENT def isBST ( root ) : NEW_LINE INDENT prev = - ( 2 ** 32 ) NEW_LINE return isBSTUtil ( root , prev ) NEW_LINE DEDENT root = newNode ( 5 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 15 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 4 ) NEW_LINE if ( isBST ( root ) ) : NEW_LINE INDENT print ( " Is ▁ BST " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ BST " ) NEW_LINE DEDENT


class GFG { static int percent ( int a , int b ) { float result = 0 ; result = ( ( b - a ) * 100 ) / a ; return ( int ) result ; } public static void main ( String [ ] args ) { int a = 20 , b = 25 ; System . out . println ( percent ( a , b ) + " % " ) ; } }

def percent ( a , b ) : NEW_LINE INDENT result = int ( ( ( b - a ) * 100 ) / a ) NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 20 , 25 NEW_LINE print ( percent ( a , b ) , " % " ) NEW_LINE DEDENT


class GFG { public static void main ( String args [ ] ) { int n = 5 ; int a [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 5 , 6 , 7 , 8 , 6 } , { 9 , 5 , 6 , 8 , 7 } , { 2 , 3 , 5 , 6 , 8 } , { 1 , 2 , 3 , 4 , 5 } } ; int convergingele , mid ; int i , j ; if ( n % 2 == 0 ) { System . out . printf ( " NULL \n " ) ; } else { mid = n / 2 ; convergingele = a [ mid ] [ mid ] ; System . out . printf ( " % d \n " , convergingele ) ; } } }

n = 5 NEW_LINE a = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 5 , 6 , 7 , 8 , 6 ] , [ 9 , 5 , 6 , 8 , 7 ] , [ 2 , 3 , 5 , 6 , 8 ] , [ 1 , 2 , 3 , 4 , 5 ] ] NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT print ( " NULL " ) NEW_LINE DEDENT else : NEW_LINE INDENT mid = n // 2 NEW_LINE convergingele = a [ mid ] [ mid ] NEW_LINE print ( convergingele ) NEW_LINE DEDENT


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

def moveSpaceInFront ( s ) : NEW_LINE INDENT i = len ( s ) - 1 ; NEW_LINE for j in range ( i , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ j ] != ' ▁ ' ) : NEW_LINE INDENT s = swap ( s , i , j ) ; NEW_LINE i -= 1 ; NEW_LINE DEDENT DEDENT return s ; NEW_LINE DEDENT def swap ( c , i , j ) : NEW_LINE INDENT c = list ( c ) NEW_LINE c [ i ] , c [ j ] = c [ j ] , c [ i ] NEW_LINE return ' ' . join ( c ) NEW_LINE DEDENT s = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " ; NEW_LINE s = moveSpaceInFront ( s ) ; NEW_LINE print ( s ) ; NEW_LINE


import java . lang . Math ; public class GfG { public static boolean is_possible ( String s ) { int l = s . length ( ) ; int one = 0 , zero = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( s . charAt ( i ) == '0' ) zero ++ ; else one ++ ; } if ( l % 2 == 0 ) return ( one == zero ) ; else return ( Math . abs ( one - zero ) == 1 ) ; } public static void main ( String [ ] args ) { String s = "100110" ; if ( is_possible ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def is_possible ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE one = 0 NEW_LINE zero = 0 NEW_LINE for i in range ( 0 , l ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT one += 1 NEW_LINE DEDENT DEDENT if ( l % 2 == 0 ) : NEW_LINE INDENT return ( one == zero ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( abs ( one - zero ) == 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "100110" NEW_LINE if ( is_possible ( s ) ) : 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 phi ( int n ) { float result = n ; for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) n /= p ; result *= ( 1.0 - ( 1.0 / ( float ) p ) ) ; } } if ( n > 1 ) result *= ( 1.0 - ( 1.0 / ( float ) n ) ) ; return ( int ) result ; } public static void main ( String args [ ] ) { int n ; for ( n = 1 ; n <= 10 ; n ++ ) System . out . println ( " phi ( " + n + " ) ▁ = ▁ " + phi ( n ) ) ; } }

def phi ( n ) : NEW_LINE INDENT result = n NEW_LINE p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( n % p == 0 ) : NEW_LINE INDENT while ( n % p == 0 ) : NEW_LINE INDENT n = n // p NEW_LINE DEDENT result = result * ( 1.0 - ( 1.0 / ( float ) ( p ) ) ) NEW_LINE DEDENT p = p + 1 NEW_LINE DEDENT if ( n > 1 ) : NEW_LINE INDENT result = result * ( 1.0 - ( 1.0 / ( float ) ( n ) ) ) NEW_LINE DEDENT return ( int ) ( result ) NEW_LINE DEDENT for n in range ( 1 , 11 ) : NEW_LINE INDENT print ( " phi ( " , n , " ) ▁ = ▁ " , phi ( n ) ) NEW_LINE DEDENT


import java . util . * ; public class GFG { static int maxLength ( int arr [ ] , int N ) { int product = 1 , len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } if ( product >= 0 ) { return N ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { len = Math . max ( len , Math . max ( N - i - 1 , i ) ) ; } } return len ; } public static void main ( String args [ ] ) { int arr [ ] = { - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 } ; int N = arr . length ; System . out . println ( maxLength ( arr , N ) ) ; } }

def maxLength ( arr , N ) : NEW_LINE INDENT product = 1 NEW_LINE Len = 0 NEW_LINE for i in arr : NEW_LINE INDENT product *= i NEW_LINE DEDENT if ( product >= 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT Len = max ( Len , max ( N - i - 1 , i ) ) NEW_LINE DEDENT DEDENT return Len NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxLength ( arr , N ) ) NEW_LINE DEDENT


class BinarySearch { int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 1 , x ) ; return binarySearch ( arr , mid + 1 , r , x ) ; } return - 1 ; } public static void main ( String args [ ] ) { BinarySearch ob = new BinarySearch ( ) ; int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int n = arr . length ; int x = 10 ; int result = ob . binarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ not ▁ present " ) ; else System . out . println ( " Element ▁ found ▁ at ▁ index ▁ " + result ) ; } }

def binarySearch ( arr , l , r , x ) : NEW_LINE INDENT if r >= l : NEW_LINE INDENT mid = l + ( r - l ) // 2 NEW_LINE if arr [ mid ] == x : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ mid ] > x : NEW_LINE INDENT return binarySearch ( arr , l , mid - 1 , x ) NEW_LINE DEDENT else : NEW_LINE INDENT return binarySearch ( arr , mid + 1 , r , x ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 4 , 10 , 40 ] NEW_LINE x = 10 NEW_LINE result = binarySearch ( arr , 0 , len ( arr ) - 1 , x ) NEW_LINE if result != - 1 : NEW_LINE INDENT print ( " Element ▁ is ▁ present ▁ at ▁ index ▁ % ▁ d " % result ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void minSet ( ArrayList < Integer > nums ) { ArrayList < Integer > A = new ArrayList < Integer > ( ) ; Collections . sort ( nums ) ; int sum = 0 ; for ( int i = 0 ; i < nums . size ( ) ; i ++ ) sum += nums . get ( i ) ; int temp = 0 ; for ( int i = nums . size ( ) - 1 ; i >= 0 && temp <= sum / 2 ; i -- ) { A . add ( nums . get ( i ) ) ; temp += nums . get ( i ) ; } for ( int i = 0 ; i < A . size ( ) ; i ++ ) System . out . print ( A . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { ArrayList < Integer > gfg = new ArrayList < Integer > ( ) ; gfg . add ( 7 ) ; gfg . add ( 6 ) ; gfg . add ( 13 ) ; gfg . add ( 13 ) ; gfg . add ( 12 ) ; gfg . add ( 11 ) ; minSet ( gfg ) ; } }

def minSet ( nums ) : NEW_LINE INDENT A = [ ] NEW_LINE nums . sort ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , len ( nums ) ) : NEW_LINE INDENT sum += nums [ i ] NEW_LINE DEDENT temp = 0 NEW_LINE for i in range ( len ( nums ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( temp > sum / 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT A . append ( nums [ i ] ) NEW_LINE temp += nums [ i ] NEW_LINE DEDENT for i in range ( 0 , len ( A ) ) : NEW_LINE INDENT print ( A [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT vec = [ 7 , 6 , 13 , 13 , 12 , 11 ] NEW_LINE minSet ( vec ) ; NEW_LINE


import java . util . * ; class GFG { static boolean checkPair ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % 2 != 0 ) { return false ; } sum = sum / 2 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int val = sum - arr [ i ] ; if ( s . contains ( val ) && val == ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) { System . out . printf ( " Pair ▁ elements ▁ are ▁ % d ▁ and ▁ % d \n " , arr [ i ] , val ) ; return true ; } s . add ( arr [ i ] ) ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 11 , 5 , 1 , 4 , 7 } ; int n = arr . length ; if ( checkPair ( arr , n ) == false ) { System . out . printf ( " No ▁ pair ▁ found " ) ; } } }

def checkPair ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if sum % 2 != 0 : NEW_LINE INDENT return False NEW_LINE DEDENT sum = sum / 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT val = sum - arr [ i ] NEW_LINE if arr [ i ] not in s : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT if val in s : NEW_LINE INDENT print ( " Pair ▁ elements ▁ are " , arr [ i ] , " and " , int ( val ) ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 2 , 11 , 5 , 1 , 4 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE if checkPair ( arr , n ) == False : NEW_LINE INDENT print ( " No ▁ pair ▁ found " ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; public class GFG { static int missingK ( int [ ] a , int k , int n ) { int difference = 0 , ans = 0 , count = k ; boolean flag = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { difference = 0 ; if ( ( a [ i ] + 1 ) != a [ i + 1 ] ) { difference += ( a [ i + 1 ] - a [ i ] ) - 1 ; if ( difference >= count ) { ans = a [ i ] + count ; flag = true ; break ; } else count -= difference ; } } if ( flag ) return ans ; else return - 1 ; } public static void main ( String args [ ] ) { int [ ] a = { 1 , 5 , 11 , 19 } ; int k = 11 ; int n = a . length ; int missing = missingK ( a , k , n ) ; System . out . print ( missing ) ; } }

def missingK ( a , k , n ) : NEW_LINE INDENT difference = 0 NEW_LINE ans = 0 NEW_LINE count = k NEW_LINE flag = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT difference = 0 NEW_LINE if ( ( a [ i ] + 1 ) != a [ i + 1 ] ) : NEW_LINE INDENT difference += ( a [ i + 1 ] - a [ i ] ) - 1 NEW_LINE if ( difference >= count ) : NEW_LINE INDENT ans = a [ i ] + count NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT count -= difference NEW_LINE DEDENT DEDENT DEDENT if ( flag ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT a = [ 1 , 5 , 11 , 19 ] NEW_LINE k = 11 NEW_LINE n = len ( a ) NEW_LINE missing = missingK ( a , k , n ) NEW_LINE print ( missing ) NEW_LINE