import java . util . * ; class Solution { static class Node { int data ; Node left , right ; } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = newNode . right = null ; return newNode ; } static void storePath ( Node root , Map < Integer , Integer > mp , int XOR ) { if ( root == null ) return ; mp . put ( root . data , XOR ^ root . data ) ; XOR ^= root . data ; if ( root . left != null ) storePath ( root . left , mp , XOR ) ; if ( root . right != null ) storePath ( root . right , mp , XOR ) ; } static int findXORPath ( Map < Integer , Integer > mp , int node1 , int node2 ) { return mp . get ( node1 ) ^ mp . get ( node2 ) ; } public static void main ( String args [ ] ) { Node root = getNode ( 0 ) ; root . left = getNode ( 1 ) ; root . left . left = getNode ( 3 ) ; root . left . left . left = getNode ( 7 ) ; root . left . right = getNode ( 4 ) ; root . left . right . left = getNode ( 8 ) ; root . left . right . right = getNode ( 9 ) ; root . right = getNode ( 2 ) ; root . right . left = getNode ( 5 ) ; root . right . right = getNode ( 6 ) ; int XOR = 0 ; Map < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int node1 = 3 ; int node2 = 5 ; storePath ( root , mp , XOR ) ; System . out . println ( findXORPath ( mp , node1 , node2 ) ) ; } }

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 getNode ( data ) : NEW_LINE INDENT newNode = Node ( 0 ) NEW_LINE newNode . data = data NEW_LINE newNode . left = newNode . right = None NEW_LINE return newNode NEW_LINE DEDENT mp = dict ( ) NEW_LINE def storePath ( root , XOR ) : NEW_LINE INDENT global mp NEW_LINE if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT mp [ root . data ] = XOR ^ root . data ; NEW_LINE XOR = XOR ^ root . data NEW_LINE if ( root . left != None ) : NEW_LINE INDENT storePath ( root . left , XOR ) NEW_LINE DEDENT if ( root . right != None ) : NEW_LINE INDENT storePath ( root . right , XOR ) NEW_LINE DEDENT DEDENT def findXORPath ( node1 , node2 ) : NEW_LINE INDENT global mp NEW_LINE return mp . get ( node1 , 0 ) ^ mp . get ( node2 , 0 ) NEW_LINE DEDENT root = getNode ( 0 ) NEW_LINE root . left = getNode ( 1 ) NEW_LINE root . left . left = getNode ( 3 ) NEW_LINE root . left . left . left = getNode ( 7 ) NEW_LINE root . left . right = getNode ( 4 ) NEW_LINE root . left . right . left = getNode ( 8 ) NEW_LINE root . left . right . right = getNode ( 9 ) NEW_LINE root . right = getNode ( 2 ) NEW_LINE root . right . left = getNode ( 5 ) NEW_LINE root . right . right = getNode ( 6 ) NEW_LINE XOR = 0 NEW_LINE node1 = 3 NEW_LINE node2 = 5 NEW_LINE storePath ( root , XOR ) NEW_LINE print ( findXORPath ( node1 , node2 ) ) NEW_LINE


import java . util . * ; class GFG { static boolean checkPermutation ( int [ ] arr , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; int [ ] prefix = new int [ n + 1 ] ; Arrays . fill ( prefix , 0 ) ; prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int lsum = prefix [ i ] ; int rsum = sum - prefix [ i ] ; int l_len = i + 1 , r_len = n - i - 1 ; if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) && ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) return true ; } return false ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 2 , 5 , 3 , 4 , 1 , 2 } ; int n = arr . length ; if ( checkPermutation ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def checkPermutation ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT prefix = [ 0 ] * ( n + 1 ) ; NEW_LINE prefix [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT lsum = prefix [ i ] ; NEW_LINE rsum = sum - prefix [ i ] ; NEW_LINE l_len = i + 1 NEW_LINE r_len = n - i - 1 ; NEW_LINE if ( ( ( 2 * lsum ) == ( l_len * ( l_len + 1 ) ) ) and ( ( 2 * rsum ) == ( r_len * ( r_len + 1 ) ) ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 3 , 4 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( checkPermutation ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


class GFG { static boolean isOperand ( char c ) { return ( c >= '0' && c <= '9' ) ; } static int value ( char c ) { return ( int ) ( c - '0' ) ; } static int evaluate ( String exp ) { if ( exp . length ( ) == 0 ) return - 1 ; int res = value ( exp . charAt ( 0 ) ) ; for ( int i = 1 ; i < exp . length ( ) ; i += 2 ) { char opr = exp . charAt ( i ) , opd = exp . charAt ( i + 1 ) ; if ( isOperand ( opd ) == false ) return - 1 ; if ( opr == ' + ' ) res += value ( opd ) ; else if ( opr == ' - ' ) res -= value ( opd ) ; else if ( opr == ' * ' ) res *= value ( opd ) ; else if ( opr == ' / ' ) res /= value ( opd ) ; else return - 1 ; } return res ; } public static void main ( String [ ] args ) { String expr1 = "1 + 2*5 + 3" ; int res = evaluate ( expr1 ) ; if ( res == - 1 ) System . out . println ( expr1 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr1 + " ▁ is ▁ " + res ) ; String expr2 = "1 + 2*3" ; res = evaluate ( expr2 ) ; if ( res == - 1 ) System . out . println ( expr2 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr2 + " ▁ is ▁ " + res ) ; String expr3 = "4-2 + 6*3" ; res = evaluate ( expr3 ) ; if ( res == - 1 ) System . out . println ( expr3 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr3 + " ▁ is ▁ " + res ) ; String expr4 = "1 + + 2" ; res = evaluate ( expr4 ) ; if ( res == - 1 ) System . out . println ( expr4 + " ▁ is ▁ Invalid " ) ; else System . out . println ( " Value ▁ of ▁ " + expr4 + " ▁ is ▁ " + res ) ; } }

def isOperand ( c ) : NEW_LINE INDENT return ( c >= '0' and c <= '9' ) ; NEW_LINE DEDENT def value ( c ) : NEW_LINE INDENT return ord ( c ) - ord ( '0' ) ; NEW_LINE DEDENT def evaluate ( exp ) : NEW_LINE INDENT len1 = len ( exp ) ; NEW_LINE if ( len1 == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT res = value ( exp [ 0 ] ) ; NEW_LINE for i in range ( 1 , len1 , 2 ) : NEW_LINE INDENT opr = exp [ i ] ; NEW_LINE opd = exp [ i + 1 ] ; NEW_LINE if ( isOperand ( opd ) == False ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT if ( opr == ' + ' ) : NEW_LINE INDENT res += value ( opd ) ; NEW_LINE DEDENT elif ( opr == ' - ' ) : NEW_LINE INDENT res -= int ( value ( opd ) ) ; NEW_LINE DEDENT elif ( opr == ' * ' ) : NEW_LINE INDENT res *= int ( value ( opd ) ) ; NEW_LINE DEDENT elif ( opr == ' / ' ) : NEW_LINE INDENT res /= int ( value ( opd ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT expr1 = "1 + 2*5 + 3" ; NEW_LINE res = evaluate ( expr1 ) ; NEW_LINE print ( expr1 , " is ▁ Invalid " ) if ( res == - 1 ) else print ( " Value ▁ of " , expr1 , " is " , res ) ; NEW_LINE expr2 = "1 + 2*3" ; NEW_LINE res = evaluate ( expr2 ) ; NEW_LINE print ( expr2 , " is ▁ Invalid " ) if ( res == - 1 ) else print ( " Value ▁ of " , expr2 , " is " , res ) ; NEW_LINE expr3 = "4-2 + 6*3" ; NEW_LINE res = evaluate ( expr3 ) ; NEW_LINE print ( expr3 , " is ▁ Invalid " ) if ( res == - 1 ) else print ( " Value ▁ of " , expr3 , " is " , res ) ; NEW_LINE expr4 = "1 + + 2" ; NEW_LINE res = evaluate ( expr4 ) ; NEW_LINE print ( expr4 , " is ▁ Invalid " ) if ( res == - 1 ) else print ( " Value ▁ of " , expr4 , " is " , res ) ; NEW_LINE


import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static class INT { int a ; } static Node newNode ( int num ) { Node temp = new Node ( ) ; temp . data = num ; temp . left = temp . right = null ; return temp ; } static Node insert ( Node root , int key ) { if ( root == null ) return newNode ( key ) ; if ( root . data > key ) root . left = insert ( root . left , key ) ; else root . right = insert ( root . right , key ) ; return root ; } static void seqExistUtil ( Node ptr , int seq [ ] , INT index ) { if ( ptr == null ) return ; seqExistUtil ( ptr . left , seq , index ) ; if ( ptr . data == seq [ index . a ] ) index . a ++ ; seqExistUtil ( ptr . right , seq , index ) ; } static boolean seqExist ( Node root , int seq [ ] , int n ) { INT index = new INT ( ) ; index . a = 0 ; seqExistUtil ( root , seq , index ) ; return ( index . a == n ) ; } public static void main ( String args [ ] ) { Node root = null ; root = insert ( root , 8 ) ; root = insert ( root , 10 ) ; root = insert ( root , 3 ) ; root = insert ( root , 6 ) ; root = insert ( root , 1 ) ; root = insert ( root , 4 ) ; root = insert ( root , 7 ) ; root = insert ( root , 14 ) ; root = insert ( root , 13 ) ; int seq [ ] = { 4 , 6 , 8 , 14 } ; int n = seq . length ; if ( seqExist ( root , seq , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def insert ( root , key ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return Node ( key ) NEW_LINE DEDENT if root . data > key : NEW_LINE INDENT root . left = insert ( root . left , key ) NEW_LINE DEDENT else : NEW_LINE INDENT root . right = insert ( root . right , key ) NEW_LINE DEDENT return root NEW_LINE DEDENT def seqExistUtil ( ptr , seq , index ) : NEW_LINE INDENT if ptr == None : NEW_LINE INDENT return NEW_LINE DEDENT seqExistUtil ( ptr . left , seq , index ) NEW_LINE if ptr . data == seq [ index [ 0 ] ] : NEW_LINE INDENT index [ 0 ] += 1 NEW_LINE DEDENT seqExistUtil ( ptr . right , seq , index ) NEW_LINE DEDENT def seqExist ( root , seq , n ) : NEW_LINE INDENT index = [ 0 ] NEW_LINE seqExistUtil ( root , seq , index ) NEW_LINE if index [ 0 ] == n : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = None NEW_LINE root = insert ( root , 8 ) NEW_LINE root = insert ( root , 10 ) NEW_LINE root = insert ( root , 3 ) NEW_LINE root = insert ( root , 6 ) NEW_LINE root = insert ( root , 1 ) NEW_LINE root = insert ( root , 4 ) NEW_LINE root = insert ( root , 7 ) NEW_LINE root = insert ( root , 14 ) NEW_LINE root = insert ( root , 13 ) NEW_LINE seq = [ 4 , 6 , 8 , 14 ] NEW_LINE n = len ( seq ) NEW_LINE if seqExist ( root , seq , n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


public class Main { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { n &= ( n - 1 ) ; count ++ ; } return count ; } static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) if ( countSetBits ( i ) == 2 ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findSum ( n ) ) ; } }

def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT n = n & ( n - 1 ) NEW_LINE count = count + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def findSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( countSetBits ( i ) == 2 ) : NEW_LINE INDENT sum = sum + i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 10 NEW_LINE print ( findSum ( n ) ) NEW_LINE


class LeftRotate { static void preprocess ( int arr [ ] , int n , int temp [ ] ) { for ( int i = 0 ; i < n ; i ++ ) temp [ i ] = temp [ i + n ] = arr [ i ] ; } static void leftRotate ( int arr [ ] , int n , int k , int temp [ ] ) { int start = k % n ; for ( int i = start ; i < start + n ; i ++ ) System . out . print ( temp [ i ] + " ▁ " ) ; System . out . print ( " \n " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 , 9 } ; int n = arr . length ; int temp [ ] = new int [ 2 * n ] ; preprocess ( arr , n , temp ) ; int k = 2 ; leftRotate ( arr , n , k , temp ) ; k = 3 ; leftRotate ( arr , n , k , temp ) ; k = 4 ; leftRotate ( arr , n , k , temp ) ; } }

def preprocess ( arr , n ) : NEW_LINE INDENT temp = [ None ] * ( 2 * n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp [ i ] = temp [ i + n ] = arr [ i ] NEW_LINE DEDENT return temp NEW_LINE DEDENT def leftRotate ( arr , n , k , temp ) : NEW_LINE INDENT start = k % n NEW_LINE for i in range ( start , start + n ) : NEW_LINE INDENT print ( temp [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE temp = preprocess ( arr , n ) NEW_LINE k = 2 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE k = 3 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE k = 4 NEW_LINE leftRotate ( arr , n , k , temp ) NEW_LINE


import java . io . * ; class GFG { static int getRemainder ( int num , int divisor ) { if ( divisor == 0 ) { System . out . println ( " Error : ▁ divisor ▁ " + " can ' t ▁ be ▁ zero ▁ \n " ) ; return - 1 ; } if ( divisor < 0 ) divisor = - divisor ; if ( num < 0 ) num = - num ; int i = 1 ; int product = 0 ; while ( product <= num ) { product = divisor * i ; i ++ ; } return num - ( product - divisor ) ; } public static void main ( String [ ] args ) { System . out . println ( getRemainder ( 100 , 7 ) ) ; } }

def getRemainder ( num , divisor ) : NEW_LINE INDENT if ( divisor == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( divisor < 0 ) : NEW_LINE INDENT divisor = - divisor NEW_LINE DEDENT if ( num < 0 ) : NEW_LINE INDENT num = - num NEW_LINE DEDENT i = 1 NEW_LINE product = 0 NEW_LINE while ( product <= num ) : NEW_LINE INDENT product = divisor * i NEW_LINE i += 1 NEW_LINE DEDENT return num - ( product - divisor ) NEW_LINE DEDENT num = 100 NEW_LINE divisor = 7 NEW_LINE print ( getRemainder ( num , divisor ) ) NEW_LINE


import java . util . Arrays ; class GFG1 { static long findMinValue ( long arr [ ] , int n ) { Arrays . sort ( arr ) ; double val = 0 ; for ( int i = 0 ; i < n ; i ++ ) { val += ( double ) ( Math . log ( ( double ) ( arr [ i ] ) ) ) ; } long left = arr [ 0 ] , right = arr [ n - 1 ] ; long ans = 0 ; while ( left <= right ) { long mid = ( left + right ) / 2 ; double temp = ( double ) n * ( double ) ( Math . log ( ( double ) ( mid ) ) ) ; if ( val < temp ) { ans = mid ; right = mid - 1 ; } else { left = mid + 1 ; } } return ans ; } public static void main ( String [ ] args ) { long arr [ ] = { 4 , 2 , 1 , 10 , 6 } ; int n = arr . length ; System . out . println ( findMinValue ( arr , n ) ) ; } }

import math NEW_LINE def findMinValue ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE val = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT val += ( math . log ( arr [ i ] ) ) NEW_LINE DEDENT left = arr [ 0 ] NEW_LINE right = arr [ n - 1 ] + 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE temp = n * ( math . log ( mid ) ) NEW_LINE if ( val < temp ) : NEW_LINE INDENT ans = mid NEW_LINE right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 1 , 10 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinValue ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void asciiToSentence ( String str , int len ) { int num = 0 ; for ( int i = 0 ; i < len ; i ++ ) { num = num * 10 + ( str . charAt ( i ) - '0' ) ; if ( num >= 32 && num <= 122 ) { char ch = ( char ) num ; System . out . print ( ch ) ; num = 0 ; } } } public static void main ( String args [ ] ) { String str = "7110110110711510211111471101101107115" ; int len = str . length ( ) ; asciiToSentence ( str , len ) ; } }

def asciiToSentence ( string , length ) : NEW_LINE INDENT num = 0 ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT num = num * 10 + ( ord ( string [ i ] ) - ord ( '0' ) ) ; NEW_LINE if ( num >= 32 and num <= 122 ) : NEW_LINE INDENT ch = chr ( num ) ; NEW_LINE print ( ch , end = " " ) ; NEW_LINE num = 0 ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "7110110110711510211111471101101107115" ; NEW_LINE length = len ( string ) ; NEW_LINE asciiToSentence ( string , length ) ; NEW_LINE DEDENT


class GFG { static boolean isValidISBN ( String isbn ) { int n = isbn . length ( ) ; if ( n != 10 ) return false ; int sum = 0 ; for ( int i = 0 ; i < 9 ; i ++ ) { int digit = isbn . charAt ( i ) - '0' ; if ( 0 > digit || 9 < digit ) return false ; sum += ( digit * ( 10 - i ) ) ; } char last = isbn . charAt ( 9 ) ; if ( last != ' X ' && ( last < '0' || last > '9' ) ) return false ; sum += ( ( last == ' X ' ) ? 10 : ( last - '0' ) ) ; return ( sum % 11 == 0 ) ; } public static void main ( String [ ] args ) { String isbn = "007462542X " ; if ( isValidISBN ( isbn ) ) System . out . print ( " Valid " ) ; else System . out . print ( " Invalid " ) ; } }

def isValidISBN ( isbn ) : NEW_LINE INDENT if len ( isbn ) != 10 : NEW_LINE INDENT return False NEW_LINE DEDENT _sum = 0 NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT if 0 <= int ( isbn [ i ] ) <= 9 : NEW_LINE INDENT _sum += int ( isbn [ i ] ) * ( 10 - i ) NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( isbn [ 9 ] != ' X ' and 0 <= int ( isbn [ 9 ] ) <= 9 ) : NEW_LINE INDENT return False NEW_LINE DEDENT _sum += 10 if isbn [ 9 ] == ' X ' else int ( isbn [ 9 ] ) NEW_LINE return ( _sum % 11 == 0 ) NEW_LINE DEDENT isbn = "007462542X " NEW_LINE if isValidISBN ( isbn ) : NEW_LINE INDENT print ( ' Valid ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE DEDENT


class GFG { static int maxProdSum ( int arr [ ] , int n ) { int [ ] prefixArraySum = new int [ n ] ; int maxProduct = 0 ; prefixArraySum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefixArraySum [ i ] = prefixArraySum [ i - 1 ] + arr [ i ] ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { int leftArraySum = prefixArraySum [ i ] ; int rightArraySum = prefixArraySum [ n - 1 ] - prefixArraySum [ i ] ; int k = leftArraySum * rightArraySum ; if ( k > maxProduct ) { maxProduct = k ; } } return maxProduct ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 10 , 1 , 7 , 2 , 9 } ; int n = arr . length ; System . out . print ( maxProdSum ( arr , n ) ) ; } }

def maxProdSum ( arr , n ) : NEW_LINE INDENT prefixArraySum = [ 0 ] * n NEW_LINE maxProduct = 0 NEW_LINE prefixArraySum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixArraySum [ i ] = prefixArraySum [ i - 1 ] + arr [ i ] NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT leftArraySum = prefixArraySum [ i ] NEW_LINE rightArraySum = prefixArraySum [ n - 1 ] -   \ NEW_LINE INDENT prefixArraySum [ i ] NEW_LINE DEDENT k = leftArraySum * rightArraySum NEW_LINE if ( k > maxProduct ) : NEW_LINE INDENT maxProduct = k NEW_LINE DEDENT DEDENT return maxProduct NEW_LINE DEDENT arr = [ 4 , 10 , 1 , 7 , 2 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxProdSum ( arr , n ) ) NEW_LINE


public class GFG { public int lengthOfLastWord ( final String a ) { int len = 0 ; String x = a . trim ( ) ; for ( int i = 0 ; i < x . length ( ) ; i ++ ) { if ( x . charAt ( i ) == ' ▁ ' ) len = 0 ; else len ++ ; } return len ; } public static void main ( String [ ] args ) { String input = " Geeks ▁ For ▁ Geeks ▁ ▁ " ; GFG gfg = new GFG ( ) ; System . out . println ( " The ▁ length ▁ of ▁ last ▁ word ▁ is ▁ " + gfg . lengthOfLastWord ( input ) ) ; } }

def lengthOfLastWord ( a ) : NEW_LINE INDENT l = 0 NEW_LINE x = a . strip ( ) NEW_LINE for i in range ( len ( x ) ) : NEW_LINE INDENT if x [ i ] == " ▁ " : NEW_LINE INDENT l = 0 NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 NEW_LINE DEDENT DEDENT return l NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT inp = " Geeks ▁ For ▁ Geeks ▁ " NEW_LINE print ( " The ▁ length ▁ of ▁ last ▁ word ▁ is " , lengthOfLastWord ( inp ) ) NEW_LINE DEDENT


import java . io . * ; public class GFG { static int rangesum ( int n , int l , int r ) { int [ ] arr = new int [ n ] ; int c = 1 , i = 0 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } c = 2 ; while ( c <= n ) { arr [ i ++ ] = c ; c += 2 ; } int sum = 0 ; for ( i = l - 1 ; i < r ; i ++ ) { sum += arr [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int n = 12 ; int l = 1 , r = 11 ; System . out . println ( rangesum ( n , l , r ) ) ; } }

def rangesum ( n , l , r ) : NEW_LINE INDENT arr = [ 0 ] * n ; NEW_LINE c = 1 ; i = 0 ; NEW_LINE while ( c <= n ) : NEW_LINE INDENT arr [ i ] = c ; NEW_LINE i += 1 ; NEW_LINE c += 2 ; NEW_LINE DEDENT c = 2 ; NEW_LINE while ( c <= n ) : NEW_LINE INDENT arr [ i ] = c ; NEW_LINE i += 1 ; NEW_LINE c += 2 ; NEW_LINE DEDENT sum = 0 ; NEW_LINE for i in range ( l - 1 , r , 1 ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 ; NEW_LINE l , r = 1 , 11 ; NEW_LINE print ( rangesum ( n , l , r ) ) ; NEW_LINE DEDENT


class GFG { public static boolean check ( int [ ] arr , int n ) { int modify = 0 ; if ( arr [ n - 1 ] >= arr [ n - 2 ] ) { arr [ n - 1 ] = arr [ n - 2 ] - 1 ; modify ++ ; } if ( arr [ 0 ] <= arr [ 1 ] ) { arr [ 0 ] = arr [ 1 ] + 1 ; modify ++ ; } for ( int i = n - 2 ; i > 0 ; i -- ) { if ( ( arr [ i - 1 ] <= arr [ i ] && arr [ i + 1 ] <= arr [ i ] ) || ( arr [ i - 1 ] >= arr [ i ] && arr [ i + 1 ] >= arr [ i ] ) ) { arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) / 2 ; modify ++ ; if ( arr [ i ] == arr [ i - 1 ] || arr [ i ] == arr [ i + 1 ] ) return false ; } } if ( modify > 1 ) return false ; return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 5 , 11 , 3 } ; int n = arr . length ; if ( check ( arr , n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def check ( arr , n ) : NEW_LINE INDENT modify = 0 NEW_LINE if ( arr [ n - 1 ] >= arr [ n - 2 ] ) : NEW_LINE INDENT arr [ n - 1 ] = arr [ n - 2 ] - 1 NEW_LINE modify += 1 NEW_LINE DEDENT if ( arr [ 0 ] <= arr [ 1 ] ) : NEW_LINE INDENT arr [ 0 ] = arr [ 0 ] + 1 NEW_LINE modify += 1 NEW_LINE DEDENT for i in range ( n - 2 , 0 , - 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] <= arr [ i ] and arr [ i + 1 ] <= arr [ i ] ) or   \ NEW_LINE ( arr [ i - 1 ] >= arr [ i ] and arr [ i + 1 ] >= arr [ i ] ) : NEW_LINE INDENT arr [ i ] = ( arr [ i - 1 ] + arr [ i + 1 ] ) // 2 NEW_LINE modify += 1 NEW_LINE if ( arr [ i ] == arr [ i - 1 ] or arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT if ( modify > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 5 , 11 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE if ( check ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static int CntcontSubs ( int a [ ] , int n ) { int c = 0 , d = 0 , i , sum = 1 , j ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 != 0 || a [ i ] % 4 == 0 ) d ++ ; sum = a [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { sum = sum * a [ j ] ; if ( sum % 2 != 0 || sum % 4 == 0 ) c ++ ; } sum = 1 ; } return c + d ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 4 , 2 , 9 , 8 } ; int n = arr . length ; System . out . print ( CntcontSubs ( arr , n ) ) ; } }

def CntcontSubs ( a , n ) : NEW_LINE INDENT c = 0 NEW_LINE d = 0 NEW_LINE sum = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 != 0 or a [ i ] % 4 == 0 ) : NEW_LINE INDENT d += 1 NEW_LINE DEDENT sum = a [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT sum = sum * a [ j ] NEW_LINE if ( sum % 2 != 0 or sum % 4 == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT sum = 1 NEW_LINE DEDENT return c + d NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 2 , 9 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( CntcontSubs ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int equalize ( int arr [ ] , int n , int k ) { int min_ele = Arrays . stream ( arr ) . min ( ) . getAsInt ( ) ; boolean flag = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! ( ( arr [ i ] + k ) >= min_ele + k && min_ele + k >= ( arr [ i ] - k ) ) ) { flag = false ; break ; } } if ( flag ) { return min_ele + k ; } else return - 1 ; } public static void main ( String [ ] args ) { int K = 2 ; int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int N = arr . length ; System . out . print ( equalize ( arr , N , K ) ) ; } }

def equalize ( arr , n , k ) : NEW_LINE INDENT min_ele = min ( arr ) ; NEW_LINE flag = True ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( not ( ( arr [ i ] + k ) >= ( min_ele + k ) and ( min_ele + k ) >= ( arr [ i ] - k ) ) ) : NEW_LINE INDENT flag = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT return min_ele + k ; NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 2 ; NEW_LINE arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE N = len ( arr ) NEW_LINE print ( equalize ( arr , N , K ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int divisible ( String num ) { int n = num . length ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( int ) ( num . charAt ( i ) ) ; if ( sum % 3 == 0 ) return 0 ; if ( n == 1 ) return - 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( sum % 3 == ( num . charAt ( i ) - '0' ) % 3 ) return 1 ; if ( n == 2 ) return - 1 ; return 2 ; } public static void main ( String [ ] args ) { String num = "1234" ; System . out . println ( divisible ( num ) ) ; } }

def divisible ( num ) : NEW_LINE INDENT n = len ( num ) ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += int ( num [ i ] ) ; NEW_LINE DEDENT if ( sum % 3 == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( sum % 3 == int ( num [ i ] ) % 3 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT if ( n == 2 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return 2 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = "1234" ; NEW_LINE print ( divisible ( num ) ) ; NEW_LINE DEDENT


class GFG { static int get ( int x , int y , int z ) { if ( x > z ) return - 1 ; int val = z - x ; int div = ( z - x ) / y ; int ans = div * y + x ; return ans ; } public static void main ( String [ ] args ) { int x = 1 , y = 5 , z = 8 ; System . out . print ( get ( x , y , z ) + " \n " ) ; } }

def get ( x , y , z ) : NEW_LINE INDENT if ( x > z ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT val = z - x NEW_LINE div = ( z - x ) // y NEW_LINE ans = div * y + x NEW_LINE return ans NEW_LINE DEDENT x = 1 NEW_LINE y = 5 NEW_LINE z = 8 NEW_LINE print ( get ( x , y , z ) ) NEW_LINE


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

def findSum ( str1 , str2 ) : NEW_LINE INDENT if ( len ( str1 ) > len ( str2 ) ) : NEW_LINE INDENT t = str1 ; NEW_LINE str1 = str2 ; NEW_LINE str2 = t ; NEW_LINE DEDENT str = " " ; NEW_LINE n1 = len ( str1 ) ; NEW_LINE n2 = len ( str2 ) ; NEW_LINE str1 = str1 [ : : - 1 ] ; NEW_LINE str2 = str2 [ : : - 1 ] ; NEW_LINE carry = 0 ; NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT sum = ( ( ord ( str1 [ i ] ) - 48 ) + ( ( ord ( str2 [ i ] ) - 48 ) + carry ) ) ; NEW_LINE str += chr ( sum % 10 + 48 ) ; NEW_LINE carry = int ( sum / 10 ) ; NEW_LINE DEDENT for i in range ( n1 , n2 ) : NEW_LINE INDENT sum = ( ( ord ( str2 [ i ] ) - 48 ) + carry ) ; NEW_LINE str += chr ( sum % 10 + 48 ) ; NEW_LINE carry = ( int ) ( sum / 10 ) ; NEW_LINE DEDENT if ( carry ) : NEW_LINE INDENT str += chr ( carry + 48 ) ; NEW_LINE DEDENT str = str [ : : - 1 ] ; NEW_LINE return str ; NEW_LINE DEDENT str1 = "12" ; NEW_LINE str2 = "198111" ; NEW_LINE print ( findSum ( str1 , str2 ) ) ; NEW_LINE


import java . io . * ; class GFG { static int FindElement ( int [ ] A , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int flag = 0 ; for ( int j = 0 ; j < i ; j ++ ) if ( A [ j ] >= A [ i ] ) { flag = 1 ; break ; } for ( int j = i + 1 ; j < n ; j ++ ) if ( A [ j ] <= A [ i ] ) { flag = 1 ; break ; } if ( flag == 0 ) return A [ i ] ; } return - 1 ; } static public void main ( String [ ] args ) { int [ ] A = { 4 , 3 , 2 , 5 , 8 , 6 , 7 } ; int n = A . length ; System . out . println ( FindElement ( A , n ) ) ; } }

def FindElement ( A , n ) : NEW_LINE INDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE for j in range ( 0 , i , 1 ) : NEW_LINE INDENT if ( A [ j ] >= A [ i ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT if ( A [ j ] <= A [ i ] ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT return A [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 3 , 2 , 5 , 8 , 6 , 7 ] NEW_LINE n = len ( A ) NEW_LINE print ( FindElement ( A , n ) ) NEW_LINE DEDENT


import java . util . Scanner ; class Node { int data ; public Node left ; public Node right ; Node ( int data ) { this . data = data ; left = right = null ; } } class GFG { public static Node insert ( Node root , int data ) { if ( root == null ) return new Node ( data ) ; if ( data < root . data ) root . left = insert ( root . left , data ) ; if ( data > root . data ) root . right = insert ( root . right , data ) ; return root ; } public static void inOrder ( Node root ) { if ( root == null ) return ; inOrder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inOrder ( root . right ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 2 , 5 , 4 , 4 } ; int n = arr . length ; Node root = null ; for ( int i = 0 ; i < n ; i ++ ) { root = insert ( root , arr [ i ] ) ; } inOrder ( root ) ; } }

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 insert ( root , data ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return newNode ( data ) ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( data < root . data ) : NEW_LINE INDENT root . left = insert ( root . left , data ) ; NEW_LINE DEDENT if ( data > root . data ) : NEW_LINE INDENT root . right = insert ( root . right , data ) ; NEW_LINE DEDENT return root ; NEW_LINE DEDENT DEDENT def inOrder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return ; NEW_LINE DEDENT else : NEW_LINE INDENT inOrder ( root . left ) ; NEW_LINE print ( root . data , end = " ▁ " ) ; NEW_LINE inOrder ( root . right ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 2 , 5 , 4 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE root = None ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT root = insert ( root , arr [ i ] ) ; NEW_LINE DEDENT inOrder ( root ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean makeAndCheckString ( Vector < String > words , String str ) { int n = words . size ( ) ; boolean first = false , second = false ; for ( int i = 0 ; i < n ; i ++ ) { if ( words . get ( i ) == str ) return true ; if ( str . charAt ( 0 ) == words . get ( i ) . charAt ( 1 ) ) first = true ; if ( str . charAt ( 1 ) == words . get ( i ) . charAt ( 0 ) ) second = true ; if ( first && second ) return true ; } return false ; } public static void main ( String [ ] args ) { String str = " ya " ; String [ ] array = { " ah " , " oy " , " to " , " ha " } ; Vector < String > words = new Vector < String > ( Arrays . asList ( array ) ) ; if ( makeAndCheckString ( words , str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def makeAndCheckString ( words , str ) : NEW_LINE INDENT n = len ( words ) NEW_LINE first = second = False NEW_LINE for i in range ( n ) : NEW_LINE INDENT if words [ i ] == str : NEW_LINE INDENT return True NEW_LINE DEDENT if str [ 0 ] == words [ i ] [ 1 ] : NEW_LINE INDENT first = True NEW_LINE DEDENT if str [ 1 ] == words [ i ] [ 0 ] : NEW_LINE INDENT second = True NEW_LINE DEDENT if first and second : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT str = ' ya ' NEW_LINE words = [ ' ah ' , ' oy ' , ' to ' , ' ha ' ] NEW_LINE if makeAndCheckString ( words , str ) : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int maximumOccurrence ( String s ) { int n = s . length ( ) ; HashMap < String , Integer > freq = new HashMap < String , Integer > ( ) ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { String temp = " " ; temp += s . charAt ( i ) ; if ( freq . containsKey ( temp ) ) { freq . put ( temp , freq . get ( temp ) + 1 ) ; } else { freq . put ( temp , 1 ) ; } } for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { String temp = " " ; temp += s . charAt ( i ) ; temp += s . charAt ( j ) ; if ( freq . containsKey ( temp ) ) freq . put ( temp , freq . get ( temp ) + 1 ) ; else freq . put ( temp , 1 ) ; } } int answer = Integer . MIN_VALUE ; for ( int it : freq . values ( ) ) answer = Math . max ( answer , it ) ; return answer ; } public static void main ( String [ ] args ) { String s = " xxxyy " ; System . out . print ( maximumOccurrence ( s ) ) ; } }

def maximumOccurrence ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE freq = { } NEW_LINE for i in s : NEW_LINE INDENT temp = " " NEW_LINE temp += i NEW_LINE freq [ temp ] = freq . get ( temp , 0 ) + 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT temp = " " NEW_LINE temp += s [ i ] NEW_LINE temp += s [ j ] NEW_LINE freq [ temp ] = freq . get ( temp , 0 ) + 1 NEW_LINE DEDENT DEDENT answer = - 10 ** 9 NEW_LINE for it in freq : NEW_LINE INDENT answer = max ( answer , freq [ it ] ) NEW_LINE DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " xxxyy " NEW_LINE print ( maximumOccurrence ( s ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int factors ( int n ) { HashMap < Integer , Integer > prime = new HashMap < Integer , Integer > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; ++ i ) { while ( n % i == 0 ) { if ( prime . containsKey ( i ) ) { prime . put ( i , prime . get ( i ) + 1 ) ; } else { prime . put ( i , 1 ) ; } n = n / i ; } } if ( n > 2 ) { if ( prime . containsKey ( n ) ) { prime . put ( n , prime . get ( n ) + 1 ) ; } else { prime . put ( n , 1 ) ; } } int ans1 = 1 , ans2 = 1 ; for ( Map . Entry < Integer , Integer > it : prime . entrySet ( ) ) { ans1 *= 2 * it . getValue ( ) + 1 ; ans2 *= it . getValue ( ) + 1 ; } return ans1 - ans2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( factors ( n ) ) ; n = 8 ; System . out . println ( factors ( n ) ) ; } }

import math as mt NEW_LINE def factors ( n ) : NEW_LINE INDENT prime = dict ( ) NEW_LINE for i in range ( 2 , mt . ceil ( mt . sqrt ( n + 1 ) ) ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT if i in prime . keys ( ) : NEW_LINE INDENT prime [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prime [ i ] = 1 NEW_LINE DEDENT n = n // i NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT if n in prime . keys ( ) : NEW_LINE INDENT prime [ n ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT prime [ n ] = 1 NEW_LINE DEDENT DEDENT ans1 = 1 NEW_LINE ans2 = 1 NEW_LINE for it in prime : NEW_LINE INDENT ans1 *= 2 * prime [ it ] + 1 NEW_LINE ans2 *= prime [ it ] + 1 NEW_LINE DEDENT return ans1 - ans2 NEW_LINE DEDENT n = 5 NEW_LINE print ( factors ( n ) ) NEW_LINE n = 8 NEW_LINE print ( factors ( n ) ) NEW_LINE


class GFG { static int countPairs ( int A , int B ) { int cnt = 0 ; for ( int i = 0 ; i <= A ; ++ i ) { for ( int j = i ; j <= A ; ++ j ) { int AND = i & j ; int OR = i | j ; if ( OR == A && AND == B ) { cnt ++ ; } } } return cnt ; } public static void main ( String [ ] args ) { int A = 3 , B = 0 ; System . out . println ( countPairs ( A , B ) ) ; } }

def countPairs ( A , B ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 0 , A + 1 ) : NEW_LINE INDENT for j in range ( i , A + 1 ) : NEW_LINE INDENT AND = i & j NEW_LINE OR = i | j NEW_LINE if ( OR == A and AND == B ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 3 NEW_LINE B = 0 NEW_LINE print ( countPairs ( A , B ) ) NEW_LINE DEDENT


class Test { static final double PI = Math . PI ; static double findArea ( int r ) { return PI * Math . pow ( r , 2 ) ; } public static void main ( String [ ] args ) { System . out . println ( " Area ▁ is ▁ " + findArea ( 5 ) ) ; } }

def findArea ( r ) : NEW_LINE INDENT PI = 3.142 NEW_LINE return PI * ( r * r ) ; NEW_LINE DEDENT print ( " Area ▁ is ▁ % .6f " % findArea ( 5 ) ) ; NEW_LINE


public class GFG { static boolean isProductEven ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( ( arr [ i ] & 1 ) == 0 ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 4 , 3 , 5 } ; int n = arr . length ; if ( isProductEven ( arr , n ) ) System . out . println ( " Even " ) ; else System . out . println ( " Odd " ) ; } }

def isProductEven ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( arr [ i ] & 1 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT arr = [ 2 , 4 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE if ( isProductEven ( arr , n ) ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT


class GFG { static int cntWays ( int arr [ ] , int n ) { if ( n == 1 ) { return 0 ; } int tot_sum = 0 , sum = 0 , ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { tot_sum += arr [ i ] ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { sum += arr [ i ] ; if ( sum == tot_sum / 2 ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 1 , 1 , - 1 , 1 , - 1 } ; int n = arr . length ; System . out . println ( cntWays ( arr , n ) ) ; } }

def cntWays ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT tot_sum = 0 ; sum = 0 ; ans = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT tot_sum += arr [ i ] ; NEW_LINE DEDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE if ( sum == tot_sum / 2 ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT arr = [ 1 , - 1 , 1 , - 1 , 1 , - 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntWays ( arr , n ) ) ; NEW_LINE


import java . util . * ; class GFG { static void print ( int n , int k ) { System . out . print ( k + " ▁ " ) ; for ( int i = 2 ; i <= n ; i ++ ) { int x = ( int ) ( Math . pow ( k , i ) - Math . pow ( k , i - 1 ) ) ; System . out . print ( x + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 3 , K = 4 ; print ( N , K ) ; } }

from math import pow NEW_LINE def printf ( n , k ) : NEW_LINE INDENT print ( int ( k ) , end = " ▁ " ) NEW_LINE for i in range ( 2 , n + 1 , 1 ) : NEW_LINE INDENT x = pow ( k , i ) - pow ( k , i - 1 ) NEW_LINE print ( int ( x ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE K = 4 NEW_LINE printf ( N , K ) NEW_LINE DEDENT


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

def countWays ( n , k ) : NEW_LINE INDENT dp = [ 0 ] * ( n + 1 ) NEW_LINE total = k NEW_LINE mod = 1000000007 NEW_LINE dp [ 1 ] = k NEW_LINE dp [ 2 ] = k * k NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT dp [ i ] = ( ( k - 1 ) * ( dp [ i - 1 ] + dp [ i - 2 ] ) ) % mod NEW_LINE DEDENT return dp [ n ] NEW_LINE DEDENT n = 3 NEW_LINE k = 2 NEW_LINE print ( countWays ( n , k ) ) NEW_LINE


public class GFG { final static int MAX = 100 ; static void smallestInRow ( int mat [ ] [ ] , int n , int m ) { System . out . print ( " ▁ { ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { int minm = mat [ i ] [ 0 ] ; for ( int j = 1 ; j < m ; j ++ ) { if ( mat [ i ] [ j ] < minm ) { minm = mat [ i ] [ j ] ; } } System . out . print ( minm + " , ▁ " ) ; } System . out . println ( " } " ) ; } static void smallestInCol ( int mat [ ] [ ] , int n , int m ) { System . out . print ( " ▁ { ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) { int minm = mat [ 0 ] [ i ] ; for ( int j = 1 ; j < n ; j ++ ) { if ( mat [ j ] [ i ] < minm ) { minm = mat [ j ] [ i ] ; } } System . out . print ( minm + " , ▁ " ) ; } System . out . print ( " } " ) ; } public static void main ( String args [ ] ) { int n = 3 , m = 3 ; int mat [ ] [ ] = { { 2 , 1 , 7 } , { 3 , 7 , 2 } , { 5 , 4 , 9 } } ; System . out . print ( " Minimum ▁ element ▁ of ▁ each ▁ row ▁ is ▁ " ) ; smallestInRow ( mat , n , m ) ; System . out . print ( " \n Minimum ▁ element ▁ of ▁ each ▁ column ▁ is ▁ " ) ; smallestInCol ( mat , n , m ) ; } }

MAX = 100 NEW_LINE def smallestInRow ( mat , n , m ) : NEW_LINE INDENT print ( " { " , end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT minm = mat [ i ] [ 0 ] NEW_LINE for j in range ( 1 , m , 1 ) : NEW_LINE INDENT if ( mat [ i ] [ j ] < minm ) : NEW_LINE INDENT minm = mat [ i ] [ j ] NEW_LINE DEDENT DEDENT print ( minm , end = " , " ) NEW_LINE DEDENT print ( " } " ) NEW_LINE DEDENT def smallestInCol ( mat , n , m ) : NEW_LINE INDENT print ( " { " , end = " " ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT minm = mat [ 0 ] [ i ] NEW_LINE for j in range ( 1 , n , 1 ) : NEW_LINE INDENT if ( mat [ j ] [ i ] < minm ) : NEW_LINE INDENT minm = mat [ j ] [ i ] NEW_LINE DEDENT DEDENT print ( minm , end = " , " ) NEW_LINE DEDENT print ( " } " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE m = 3 NEW_LINE mat = [ [ 2 , 1 , 7 ] , [ 3 , 7 , 2 ] , [ 5 , 4 , 9 ] ] ; NEW_LINE print ( " Minimum ▁ element ▁ of ▁ each ▁ row ▁ is " , end = " ▁ " ) NEW_LINE smallestInRow ( mat , n , m ) NEW_LINE print ( " Minimum ▁ element ▁ of ▁ each ▁ column ▁ is " , end = " ▁ " ) NEW_LINE smallestInCol ( mat , n , m ) NEW_LINE DEDENT


class GFG { static int MinOfCubed ( int k ) { if ( k < 8 ) return k ; int res = k ; for ( int i = 1 ; i <= k ; i ++ ) { if ( ( i * i * i ) > k ) return res ; res = Math . min ( res , MinOfCubed ( k - ( i * i * i ) ) + 1 ) ; } return res ; } public static void main ( String [ ] args ) { int num = 15 ; System . out . println ( MinOfCubed ( num ) ) ; } }

def MinOfCubed ( k ) : NEW_LINE INDENT if ( k < 8 ) : NEW_LINE INDENT return k ; NEW_LINE DEDENT res = k ; NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT if ( ( i * i * i ) > k ) : NEW_LINE INDENT return res ; NEW_LINE DEDENT res = min ( res , MinOfCubed ( k - ( i * i * i ) ) + 1 ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT num = 15 ; NEW_LINE print ( MinOfCubed ( num ) ) ; NEW_LINE


import java . io . * ; class GFG { public static int countSetBits ( int n ) { if ( n == 0 ) return 0 ; else return ( n & 1 ) + countSetBits ( n >> 1 ) ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( countSetBits ( n ) ) ; } }

def countSetBits ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return ( n & 1 ) + countSetBits ( n >> 1 ) NEW_LINE DEDENT DEDENT n = 9 NEW_LINE print ( countSetBits ( n ) ) NEW_LINE


import java . io . * ; class GFG { static float findArea ( float a ) { float area ; area = ( float ) ( Math . sqrt ( 5 * ( 5 + 2 * ( Math . sqrt ( 5 ) ) ) ) * a * a ) / 4 ; return area ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( " Area ▁ of ▁ Pentagon : ▁ " + findArea ( a ) ) ; } }

from math import sqrt NEW_LINE def findArea ( a ) : NEW_LINE INDENT area = ( sqrt ( 5 * ( 5 + 2 * ( sqrt ( 5 ) ) ) ) * a * a ) / 4 NEW_LINE return area NEW_LINE DEDENT a = 5 NEW_LINE print ( " Area ▁ of ▁ Pentagon : ▁ " , findArea ( a ) ) NEW_LINE


import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static int findSmallest ( int N ) { if ( N == 0 ) return 2 ; if ( N == 1 ) return 1 ; int prime = N , counter = 0 ; boolean found = false ; while ( ! found ) { if ( isPrime ( prime ) ) found = true ; else { prime ++ ; counter ++ ; } } return counter ; } public static void main ( String [ ] args ) { int N = 10 ; System . out . print ( findSmallest ( N ) ) ; } }

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False 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 False NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def findSmallest ( N ) : NEW_LINE INDENT if ( N == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( N == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT prime , counter = N , 0 NEW_LINE found = False NEW_LINE while ( not found ) : NEW_LINE INDENT if ( isPrime ( prime ) ) : NEW_LINE INDENT found = True NEW_LINE DEDENT else : NEW_LINE INDENT prime += 1 NEW_LINE counter += 1 NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10 NEW_LINE print ( findSmallest ( N ) ) NEW_LINE DEDENT


class GFG { static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum += ( ( i * ( i + 1 ) * ( 2 * i + 1 ) ) / 6 ) ; return sum ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( findSum ( n ) ) ; } }

def findSum ( n ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT summ = ( summ + ( ( i * ( i + 1 ) * ( 2 * i + 1 ) ) / 6 ) ) NEW_LINE DEDENT return summ NEW_LINE DEDENT n = 3 NEW_LINE print ( int ( findSum ( n ) ) ) NEW_LINE


import java . io . * ; class GFG { static void findAllTriplets ( int arr [ ] , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { for ( int j = i - 1 , k = i + 1 ; j >= 0 && k < n ; ) { if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) { System . out . println ( arr [ j ] + " ▁ " + arr [ i ] + " ▁ " + arr [ k ] ) ; k ++ ; j -- ; } else if ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) k ++ ; else j -- ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 } ; int n = arr . length ; findAllTriplets ( arr , n ) ; } }

def printAllAPTriplets ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n - 1 ) : NEW_LINE INDENT j = i - 1 NEW_LINE k = i + 1 NEW_LINE while ( j >= 0 and k < n ) : NEW_LINE INDENT if ( arr [ j ] + arr [ k ] == 2 * arr [ i ] ) : NEW_LINE INDENT print ( arr [ j ] , " " , arr [ i ] , " " , arr [ k ] ) NEW_LINE k += 1 NEW_LINE j -= 1 NEW_LINE DEDENT elif ( arr [ j ] + arr [ k ] < 2 * arr [ i ] ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT else : NEW_LINE INDENT j -= 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 2 , 6 , 9 , 12 , 17 , 22 , 31 , 32 , 35 , 42 ] NEW_LINE n = len ( arr ) NEW_LINE printAllAPTriplets ( arr , n ) NEW_LINE


class GFG { static int findDigitsInFactorial ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double x = ( ( n * Math . log10 ( n / Math . E ) + Math . log10 ( 2 * Math . PI * n ) / 2.0 ) ) ; return ( int ) ( Math . floor ( x ) + 1 ) ; } static int findNum ( int n ) { int low = 0 , hi = 2 * n ; if ( n <= 0 ) return - 1 ; if ( findDigitsInFactorial ( low ) == n ) return low ; while ( low <= hi ) { int mid = ( low + hi ) / 2 ; if ( findDigitsInFactorial ( mid ) >= n && findDigitsInFactorial ( mid - 1 ) < n ) return mid ; else if ( findDigitsInFactorial ( mid ) < n ) low = mid + 1 ; else hi = mid - 1 ; } return low ; } public static void main ( String [ ] args ) { System . out . println ( findNum ( 1 ) ) ; System . out . println ( findNum ( 2 ) ) ; System . out . println ( findNum ( 5 ) ) ; System . out . println ( findNum ( 24 ) ) ; System . out . println ( findNum ( 100 ) ) ; System . out . println ( findNum ( 1221 ) ) ; } }

import math NEW_LINE def findDigitsInFactorial ( n ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n <= 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT M_E = 2.7182818284590452354 NEW_LINE M_PI = 3.14159265358979323846 NEW_LINE x = ( ( n * math . log10 ( n / M_E ) + math . log10 ( 2 * M_PI * n ) / 2.0 ) ) NEW_LINE return int ( math . floor ( x ) + 1 ) NEW_LINE DEDENT def findNum ( n ) : NEW_LINE INDENT low = 0 NEW_LINE hi = 2 * n NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( findDigitsInFactorial ( low ) == n ) : NEW_LINE INDENT return int ( round ( low ) ) NEW_LINE DEDENT while ( low <= hi ) : NEW_LINE INDENT mid = int ( ( low + hi ) / 2 ) NEW_LINE if ( ( findDigitsInFactorial ( mid ) >= n and findDigitsInFactorial ( mid - 1 ) < n ) ) : NEW_LINE INDENT return int ( round ( mid ) ) NEW_LINE DEDENT elif ( findDigitsInFactorial ( mid ) < n ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hi = mid - 1 NEW_LINE DEDENT DEDENT return int ( round ( low ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( findNum ( 1 ) ) ; NEW_LINE print ( findNum ( 2 ) ) ; NEW_LINE print ( findNum ( 5 ) ) ; NEW_LINE print ( findNum ( 24 ) ) ; NEW_LINE print ( findNum ( 100 ) ) ; NEW_LINE print ( findNum ( 1221 ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean Prime ( int n ) { for ( int j = 2 ; j <= Math . sqrt ( n ) ; j ++ ) if ( n % j == 0 ) return false ; return true ; } static int find_prime ( int x , int xsqmodp , int y , int ysqmodp ) { int n = x * x - xsqmodp ; int n1 = y * y - ysqmodp ; for ( int j = 2 ; j <= Math . max ( Math . sqrt ( n ) , Math . sqrt ( n1 ) ) ; j ++ ) { if ( n % j == 0 && ( x * x ) % j == xsqmodp && n1 % j == 0 && ( y * y ) % j == ysqmodp ) if ( Prime ( j ) ) return j ; int j1 = n / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; j1 = n1 / j ; if ( n % j1 == 0 && ( x * x ) % j1 == xsqmodp && n1 % j1 == 0 && ( y * y ) % j1 == ysqmodp ) if ( Prime ( j1 ) ) return j1 ; } if ( n == n1 ) return n ; return Integer . MIN_VALUE ; } public static void main ( String [ ] args ) { int x = 3 , xsqmodp = 0 , y = 5 , ysqmodp = 1 ; System . out . println ( find_prime ( x , xsqmodp , y , ysqmodp ) ) ; } }

from math import sqrt NEW_LINE def Prime ( n ) : NEW_LINE INDENT for j in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % j == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def find_prime ( x , xsqmodp , y , ysqmodp ) : NEW_LINE INDENT n = x * x - xsqmodp NEW_LINE n1 = y * y - ysqmodp NEW_LINE for j in range ( 2 , max ( int ( sqrt ( n ) ) , int ( sqrt ( n1 ) ) ) , 1 ) : NEW_LINE INDENT if ( n % j == 0 and ( x * x ) % j == xsqmodp and n1 % j == 0 and ( y * y ) % j == ysqmodp ) : NEW_LINE INDENT if ( Prime ( j ) ) : NEW_LINE INDENT return j NEW_LINE DEDENT DEDENT j1 = n // j NEW_LINE if ( n % j1 == 0 and ( x * x ) % j1 == xsqmodp and n1 % j1 == 0 and ( y * y ) % j1 == ysqmodp ) : NEW_LINE INDENT if ( Prime ( j1 ) ) : NEW_LINE INDENT return j1 NEW_LINE DEDENT DEDENT j1 = n1 // j NEW_LINE if ( n % j1 == 0 and ( x * x ) % j1 == xsqmodp and n1 % j1 == 0 and ( y * y ) % j1 == ysqmodp ) : NEW_LINE INDENT if ( Prime ( j1 ) ) : NEW_LINE INDENT return j1 NEW_LINE DEDENT DEDENT DEDENT if ( n == n1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 3 NEW_LINE xsqmodp = 0 NEW_LINE y = 5 NEW_LINE ysqmodp = 1 NEW_LINE print ( find_prime ( x , xsqmodp , y , ysqmodp ) ) NEW_LINE DEDENT


import java . util . HashMap ; class CountDistinctWindow { static void countDistinct ( int arr [ ] , int k ) { HashMap < Integer , Integer > hM = new HashMap < Integer , Integer > ( ) ; int dist_count = 0 ; for ( int i = 0 ; i < k ; i ++ ) { if ( hM . get ( arr [ i ] ) == null ) { hM . put ( arr [ i ] , 1 ) ; dist_count ++ ; } else { int count = hM . get ( arr [ i ] ) ; hM . put ( arr [ i ] , count + 1 ) ; } } System . out . println ( dist_count ) ; for ( int i = k ; i < arr . length ; i ++ ) { if ( hM . get ( arr [ i - k ] ) == 1 ) { hM . remove ( arr [ i - k ] ) ; dist_count -- ; } else { int count = hM . get ( arr [ i - k ] ) ; hM . put ( arr [ i - k ] , count - 1 ) ; } if ( hM . get ( arr [ i ] ) == null ) { hM . put ( arr [ i ] , 1 ) ; dist_count ++ ; } else { int count = hM . get ( arr [ i ] ) ; hM . put ( arr [ i ] , count + 1 ) ; } System . out . println ( dist_count ) ; } } public static void main ( String arg [ ] ) { int arr [ ] = { 1 , 2 , 1 , 3 , 4 , 2 , 3 } ; int k = 4 ; countDistinct ( arr , k ) ; } }

from collections import defaultdict NEW_LINE def countDistinct ( arr , k , n ) : NEW_LINE INDENT mp = defaultdict ( lambda : 0 ) NEW_LINE dist_count = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT if mp [ arr [ i ] ] == 0 : NEW_LINE INDENT dist_count += 1 NEW_LINE DEDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT print ( dist_count ) NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT if mp [ arr [ i - k ] ] == 1 : NEW_LINE INDENT dist_count -= 1 NEW_LINE DEDENT mp [ arr [ i - k ] ] -= 1 NEW_LINE if mp [ arr [ i ] ] == 0 : NEW_LINE INDENT dist_count += 1 NEW_LINE DEDENT mp [ arr [ i ] ] += 1 NEW_LINE print ( dist_count ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 1 , 3 , 4 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE countDistinct ( arr , k , n ) NEW_LINE


import java . util . Arrays ; class GfG { static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void printMaxMean ( int arr [ ] , int n ) { int newArr [ ] = new int [ n ] ; Arrays . sort ( arr , 0 , 2 * n ) ; for ( int i = 0 ; i < n ; i ++ ) newArr [ i ] = arr [ i + n ] ; printArray ( newArr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 } ; int n = arr . length ; printMaxMean ( arr , n / 2 ) ; } }

def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def printMaxMean ( arr , n ) : NEW_LINE INDENT newArr = [ 0 ] * n NEW_LINE arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT newArr [ i ] = arr [ i + n ] NEW_LINE DEDENT printArray ( newArr , n ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 8 , 3 , 1 , 3 , 7 , 0 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE printMaxMean ( arr , n // 2 ) NEW_LINE DEDENT


import java . util . * ; import java . lang . Math ; class GfG { static void powerfulIntegers ( int x , int y , int bound ) { Set < Integer > s = new HashSet < > ( ) ; ArrayList < Integer > powersOfY = new ArrayList < > ( ) ; int i ; powersOfY . add ( 1 ) ; for ( i = y ; i < bound ; i = i * y ) powersOfY . add ( i ) ; i = 0 ; while ( true ) { int xPowI = ( int ) Math . pow ( ( double ) x , ( double ) i ) ; if ( xPowI >= bound ) break ; for ( int j = 0 ; j < powersOfY . size ( ) ; ++ j ) { int num = xPowI + powersOfY . get ( j ) ; if ( num <= bound ) s . add ( num ) ; else break ; } i ++ ; } Iterator itr = s . iterator ( ) ; while ( itr . hasNext ( ) ) { System . out . print ( itr . next ( ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int x = 2 , y = 3 , bound = 10 ; powerfulIntegers ( x , y , bound ) ; } }

def powerfulIntegers ( x , y , bound ) : NEW_LINE INDENT s = set ( ) NEW_LINE powersOfY = [ ] NEW_LINE powersOfY . append ( 1 ) NEW_LINE i = y NEW_LINE while i < bound : NEW_LINE INDENT powersOfY . append ( i ) NEW_LINE i *= y NEW_LINE DEDENT i = 0 NEW_LINE while ( True ) : NEW_LINE INDENT xPowI = pow ( x , i ) NEW_LINE if ( xPowI >= bound ) : NEW_LINE INDENT break NEW_LINE DEDENT for j in powersOfY : NEW_LINE INDENT num = xPowI + j NEW_LINE if ( num <= bound ) : NEW_LINE INDENT s . add ( num ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT for itr in s : NEW_LINE INDENT print ( itr , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 2 NEW_LINE y = 3 NEW_LINE bound = 10 NEW_LINE powerfulIntegers ( x , y , bound ) NEW_LINE DEDENT


import java . util . HashMap ; class GFG { static void removeElements ( int arr [ ] , int n , int l , int r ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { int val = 0 ; if ( mp . get ( arr [ i ] ) == null ) { val = 1 ; } else { val = mp . get ( arr [ i ] ) + 1 ; } mp . put ( arr [ i ] , val ) ; } for ( int i = 0 ; i < n ; ++ i ) { if ( mp . get ( arr [ i ] ) < l || mp . get ( arr [ i ] ) > r ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 3 , 2 , 2 , 5 } ; int n = arr . length ; int l = 2 , r = 3 ; removeElements ( arr , n , l , r ) ; } }

def removeElements ( arr , n , l , r ) : NEW_LINE INDENT mp = { i : 0 for i in range ( len ( arr ) ) } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( mp [ arr [ i ] ] < l or mp [ arr [ i ] > r ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 3 , 2 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE l = 2 NEW_LINE r = 3 NEW_LINE removeElements ( arr , n , l , r ) ; NEW_LINE DEDENT


public class RatMaze { static int N ; void printSolution ( int sol [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( " ▁ " + sol [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } boolean isSafe ( int maze [ ] [ ] , int x , int y ) { return ( x >= 0 && x < N && y >= 0 && y < N && maze [ x ] [ y ] == 1 ) ; } boolean solveMaze ( int maze [ ] [ ] ) { int sol [ ] [ ] = new int [ N ] [ N ] ; if ( solveMazeUtil ( maze , 0 , 0 , sol ) == false ) { System . out . print ( " Solution ▁ doesn ' t ▁ exist " ) ; return false ; } printSolution ( sol ) ; return true ; } boolean solveMazeUtil ( int maze [ ] [ ] , int x , int y , int sol [ ] [ ] ) { if ( x == N - 1 && y == N - 1 && maze [ x ] [ y ] == 1 ) { sol [ x ] [ y ] = 1 ; return true ; } if ( isSafe ( maze , x , y ) == true ) { sol [ x ] [ y ] = 1 ; if ( solveMazeUtil ( maze , x + 1 , y , sol ) ) return true ; if ( solveMazeUtil ( maze , x , y + 1 , sol ) ) return true ; sol [ x ] [ y ] = 0 ; return false ; } return false ; } public static void main ( String args [ ] ) { RatMaze rat = new RatMaze ( ) ; int maze [ ] [ ] = { { 1 , 0 , 0 , 0 } , { 1 , 1 , 0 , 1 } , { 0 , 1 , 0 , 0 } , { 1 , 1 , 1 , 1 } } ; N = maze . length ; rat . solveMaze ( maze ) ; } }

N = 4 NEW_LINE def printSolution ( sol ) : NEW_LINE INDENT for i in sol : NEW_LINE INDENT for j in i : NEW_LINE INDENT print ( str ( j ) + " ▁ " , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT def isSafe ( maze , x , y ) : NEW_LINE INDENT if x >= 0 and x < N and y >= 0 and y < N and maze [ x ] [ y ] == 1 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def solveMaze ( maze ) : NEW_LINE INDENT sol = [ [ 0 for j in range ( 4 ) ] for i in range ( 4 ) ] NEW_LINE if solveMazeUtil ( maze , 0 , 0 , sol ) == False : NEW_LINE INDENT print ( " Solution ▁ doesn ' t ▁ exist " ) ; NEW_LINE return False NEW_LINE DEDENT printSolution ( sol ) NEW_LINE return True NEW_LINE DEDENT def solveMazeUtil ( maze , x , y , sol ) : NEW_LINE INDENT if x == N - 1 and y == N - 1 and maze [ x ] [ y ] == 1 : NEW_LINE INDENT sol [ x ] [ y ] = 1 NEW_LINE return True NEW_LINE DEDENT if isSafe ( maze , x , y ) == True : NEW_LINE INDENT sol [ x ] [ y ] = 1 NEW_LINE if solveMazeUtil ( maze , x + 1 , y , sol ) == True : NEW_LINE INDENT return True NEW_LINE DEDENT if solveMazeUtil ( maze , x , y + 1 , sol ) == True : NEW_LINE INDENT return True NEW_LINE DEDENT sol [ x ] [ y ] = 0 NEW_LINE return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT maze = [ [ 1 , 0 , 0 , 0 ] , [ 1 , 1 , 0 , 1 ] , [ 0 , 1 , 0 , 0 ] , [ 1 , 1 , 1 , 1 ] ] NEW_LINE solveMaze ( maze ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void printLines ( int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . println ( k * ( 6 * i + 1 ) + " ▁ " + k * ( 6 * i + 2 ) + " ▁ " + k * ( 6 * i + 3 ) + " ▁ " + k * ( 6 * i + 5 ) ) ; } } public static void main ( String args [ ] ) { int n = 2 , k = 2 ; printLines ( n , k ) ; } }

def printLines ( n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( k * ( 6 * i + 1 ) , k * ( 6 * i + 2 ) , k * ( 6 * i + 3 ) , k * ( 6 * i + 5 ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 2 , 2 NEW_LINE printLines ( n , k ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int findpos ( String n ) { int k = 0 , pos = 0 , i = 0 ; while ( k != n . length ( ) ) { switch ( n . charAt ( i ) ) { case '4' : pos = pos * 2 + 1 ; break ; case '7' : pos = pos * 2 + 2 ; break ; } i ++ ; k ++ ; } return pos ; } public static void main ( String [ ] args ) { String n = "774" ; System . out . println ( findpos ( n ) ) ; } }

def findpos ( n ) : NEW_LINE INDENT i = 0 NEW_LINE j = len ( n ) NEW_LINE pos = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( n [ i ] == '4' ) : NEW_LINE INDENT pos = pos * 2 + 1 NEW_LINE DEDENT if ( n [ i ] == '7' ) : NEW_LINE INDENT pos = pos * 2 + 2 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return pos NEW_LINE DEDENT n = "774" NEW_LINE print ( findpos ( n ) ) NEW_LINE


import java . util . * ; class GFG { static int NotParallel ( int p [ ] [ ] , int n ) { HashMap < Integer , Integer > x_axis = new HashMap < Integer , Integer > ( ) ; HashMap < Integer , Integer > y_axis = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( x_axis . containsKey ( p [ i ] [ 0 ] ) ) x_axis . put ( p [ i ] [ 0 ] , x_axis . get ( p [ i ] [ 0 ] ) + 1 ) ; else x_axis . put ( p [ i ] [ 0 ] , 1 ) ; if ( y_axis . containsKey ( p [ i ] [ 1 ] ) ) y_axis . put ( p [ i ] [ 1 ] , y_axis . get ( p [ i ] [ 1 ] ) + 1 ) ; else y_axis . put ( p [ i ] [ 1 ] , 1 ) ; } int total = ( n * ( n - 1 ) ) / 2 ; for ( Map . Entry < Integer , Integer > i : x_axis . entrySet ( ) ) { int c = i . getValue ( ) ; total -= ( c * ( c - 1 ) ) / 2 ; } for ( Map . Entry < Integer , Integer > i : y_axis . entrySet ( ) ) { int c = i . getValue ( ) ; total -= ( c * ( c - 1 ) ) / 2 ; } return total ; } public static void main ( String [ ] args ) { int p [ ] [ ] = { { 1 , 2 } , { 1 , 5 } , { 1 , 15 } , { 2 , 10 } } ; int n = p . length ; System . out . print ( NotParallel ( p , n ) ) ; } }

def NotParallel ( p , n ) : NEW_LINE INDENT x_axis = { } ; y_axis = { } ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if p [ i ] [ 0 ] not in x_axis : NEW_LINE INDENT x_axis [ p [ i ] [ 0 ] ] = 0 ; NEW_LINE DEDENT x_axis [ p [ i ] [ 0 ] ] += 1 ; NEW_LINE if p [ i ] [ 1 ] not in y_axis : NEW_LINE INDENT y_axis [ p [ i ] [ 1 ] ] = 0 ; NEW_LINE DEDENT y_axis [ p [ i ] [ 1 ] ] += 1 ; NEW_LINE DEDENT total = ( n * ( n - 1 ) ) // 2 ; NEW_LINE for i in x_axis : NEW_LINE INDENT c = x_axis [ i ] ; NEW_LINE total -= ( c * ( c - 1 ) ) // 2 ; NEW_LINE DEDENT for i in y_axis : NEW_LINE INDENT c = y_axis [ i ] ; NEW_LINE total -= ( c * ( c - 1 ) ) // 2 ; NEW_LINE DEDENT return total ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = [ [ 1 , 2 ] , [ 1 , 5 ] , [ 1 , 15 ] , [ 2 , 10 ] ] ; NEW_LINE n = len ( p ) ; NEW_LINE print ( NotParallel ( p , n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int sz = 100000 ; static boolean isPrime [ ] = new boolean [ sz + 1 ] ; static void sieve ( ) { for ( int i = 0 ; i <= sz ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static void findPrimesD ( int d ) { int left = ( int ) Math . pow ( 10 , d - 1 ) ; int right = ( int ) Math . pow ( 10 , d ) - 1 ; for ( int i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { System . out . print ( i + " ▁ " ) ; } } } public static void main ( String args [ ] ) { sieve ( ) ; int d = 1 ; findPrimesD ( d ) ; } }

from math import sqrt , pow NEW_LINE sz = 100005 NEW_LINE isPrime = [ True for i in range ( sz + 1 ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE for i in range ( 2 , int ( sqrt ( sz ) ) + 1 , 1 ) : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , sz , i ) : NEW_LINE INDENT isPrime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def findPrimesD ( d ) : NEW_LINE INDENT left = int ( pow ( 10 , d - 1 ) ) NEW_LINE right = int ( pow ( 10 , d ) - 1 ) NEW_LINE for i in range ( left , right + 1 , 1 ) : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sieve ( ) NEW_LINE d = 1 NEW_LINE findPrimesD ( d ) NEW_LINE DEDENT


class GFG { static int multiplyWith4 ( int n ) { return ( n << 2 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . print ( multiplyWith4 ( n ) ) ; } }

def multiplyWith4 ( n ) : NEW_LINE INDENT return ( n << 2 ) NEW_LINE DEDENT n = 4 NEW_LINE print ( multiplyWith4 ( n ) ) NEW_LINE


import java . util . * ; class GFG { static String nthprimedigitsnumber ( int number ) { int rem ; String num = " " ; while ( number > 0 ) { rem = number % 4 ; switch ( rem ) { case 1 : num += '2' ; break ; case 2 : num += '3' ; break ; case 3 : num += '5' ; break ; case 0 : num += '7' ; break ; } if ( number % 4 == 0 ) number -- ; number = number / 4 ; } return new StringBuilder ( num ) . reverse ( ) . toString ( ) ; } public static void main ( String [ ] args ) { int number = 21 ; System . out . println ( nthprimedigitsnumber ( 10 ) ) ; System . out . println ( nthprimedigitsnumber ( 21 ) ) ; } }

def nthprimedigitsnumber ( number ) : NEW_LINE INDENT num = " " ; NEW_LINE while ( number > 0 ) : NEW_LINE INDENT rem = number % 4 ; NEW_LINE if ( rem == 1 ) : NEW_LINE INDENT num += '2' ; NEW_LINE DEDENT if ( rem == 2 ) : NEW_LINE INDENT num += '3' ; NEW_LINE DEDENT if ( rem == 3 ) : NEW_LINE INDENT num += '5' ; NEW_LINE DEDENT if ( rem == 0 ) : NEW_LINE INDENT num += '7' ; NEW_LINE DEDENT if ( number % 4 == 0 ) : NEW_LINE INDENT number = number - 1 NEW_LINE DEDENT number = number // 4 ; NEW_LINE DEDENT return num [ : : - 1 ] ; NEW_LINE DEDENT number = 21 ; NEW_LINE print ( nthprimedigitsnumber ( 10 ) ) ; NEW_LINE print ( nthprimedigitsnumber ( number ) ) ; NEW_LINE


import java . io . * ; class GFG { static int ncr ( int n , int r ) { int ans = 1 ; for ( int i = 1 ; i <= r ; i += 1 ) { ans *= ( n - r + i ) ; ans /= i ; } return ans ; } static int totalWays ( int X , int Y , int M , int W ) { return ( ncr ( M , X ) * ncr ( W , Y ) ) ; } public static void main ( String [ ] args ) { int X = 4 , Y = 3 , M = 6 , W = 5 ; System . out . println ( totalWays ( X , Y , M , W ) ) ; } }

def ncr ( n , r ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE INDENT ans *= ( n - r + i ) NEW_LINE ans //= i NEW_LINE DEDENT return ans NEW_LINE DEDENT def totalWays ( X , Y , M , W ) : NEW_LINE INDENT return ( ncr ( M , X ) * ncr ( W , Y ) ) NEW_LINE DEDENT X = 4 NEW_LINE Y = 3 NEW_LINE M = 6 NEW_LINE W = 5 NEW_LINE print ( totalWays ( X , Y , M , W ) ) NEW_LINE


import java . util . * ; class GFG { static String first_subString ( String s ) { int n = s . length ( ) , c = 0 ; String s1 , s2 ; HashMap < String , Integer > mpp = new HashMap < String , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ▁ ' || s . charAt ( i ) == ' # ' ) { s1 = s . substring ( c , i ) ; mpp . put ( s1 , 1 ) ; c = i + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ▁ ' ) { continue ; } for ( int j = 0 ; j < n ; j ++ ) { if ( s . charAt ( i ) == ' ▁ ' ) { break ; } s1 = s . substring ( i , j - i + 1 ) ; s2 = s1 ; s1 = reverse ( s1 ) ; if ( mpp . containsKey ( s1 ) ) { return s2 ; } } } return " - 1" ; } 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 s , s1 ; s = " mango ▁ is ▁ sweet ▁ when ▁ nam ▁ en ▁ tastes ▁ it # " ; s1 = first_subString ( s ) ; System . out . print ( s1 + " \n " ) ; } }

def first_substring ( s ) : NEW_LINE INDENT n = len ( s ) ; c = 0 ; NEW_LINE mpp = { } ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' ▁ ' or s [ i ] == ' # ' ) : NEW_LINE INDENT s1 = s [ c : i ] ; NEW_LINE mpp [ s1 ] = 1 ; NEW_LINE c = i + 1 ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' ▁ ' ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT if ( s [ j ] == ' ▁ ' ) : NEW_LINE INDENT break ; NEW_LINE DEDENT s1 = s [ i : j + 1 ] ; NEW_LINE s2 = s1 ; NEW_LINE s1 = s1 [ : : - 1 ] ; NEW_LINE if s1 in mpp : NEW_LINE INDENT if mpp [ s1 ] : NEW_LINE INDENT return s2 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return " - 1" ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " mango ▁ is ▁ sweet ▁ when ▁ nam ▁ en ▁ tastes ▁ it # " ; NEW_LINE s1 = first_substring ( s ) ; NEW_LINE print ( s1 ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int numberofterm ( int digit , int number ) { int firstnum = ( int ) Math . pow ( 10 , digit - 1 ) ; int lastnum = ( int ) Math . pow ( 10 , digit ) ; firstnum = ( firstnum - firstnum % number ) + number ; lastnum = ( lastnum - lastnum % number ) ; return ( ( lastnum - firstnum ) / number + 1 ) ; } public static void main ( String [ ] args ) { int n = 3 ; int number = 7 ; System . out . println ( numberofterm ( n , number ) ) ; } }

def numberofterm ( digit , number ) : NEW_LINE INDENT firstnum = pow ( 10 , digit - 1 ) NEW_LINE lastnum = pow ( 10 , digit ) NEW_LINE firstnum = ( firstnum - firstnum % number ) + number NEW_LINE lastnum = ( lastnum - lastnum % number ) NEW_LINE return ( ( lastnum - firstnum ) // number + 1 ) ; NEW_LINE DEDENT n = 3 ; number = 7 NEW_LINE print ( numberofterm ( n , number ) ) NEW_LINE


import java . io . * ; class GFG { static int pairsInSortedRotated ( int arr [ ] , int n , int x ) { int i ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( arr [ i ] > arr [ i + 1 ] ) break ; int l = ( i + 1 ) % n ; int r = i ; int cnt = 0 ; while ( l != r ) { if ( arr [ l ] + arr [ r ] == x ) { cnt ++ ; if ( l == ( r - 1 + n ) % n ) { return cnt ; } l = ( l + 1 ) % n ; r = ( r - 1 + n ) % n ; } else if ( arr [ l ] + arr [ r ] < x ) l = ( l + 1 ) % n ; else r = ( n + r - 1 ) % n ; } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 15 , 6 , 7 , 9 , 10 } ; int sum = 16 ; int n = arr . length ; System . out . println ( pairsInSortedRotated ( arr , n , sum ) ) ; } }

def pairsInSortedRotated ( arr , n , x ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] > arr [ i + 1 ] : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT l = ( i + 1 ) % n NEW_LINE r = i NEW_LINE cnt = 0 NEW_LINE while ( l != r ) : NEW_LINE INDENT if arr [ l ] + arr [ r ] == x : NEW_LINE INDENT cnt += 1 NEW_LINE if l == ( r - 1 + n ) % n : NEW_LINE INDENT return cnt NEW_LINE DEDENT l = ( l + 1 ) % n NEW_LINE r = ( r - 1 + n ) % n NEW_LINE DEDENT elif arr [ l ] + arr [ r ] < x : NEW_LINE INDENT l = ( l + 1 ) % n NEW_LINE DEDENT else : NEW_LINE INDENT r = ( n + r - 1 ) % n NEW_LINE DEDENT DEDENT return cnt NEW_LINE DEDENT arr = [ 11 , 15 , 6 , 7 , 9 , 10 ] NEW_LINE s = 16 NEW_LINE print ( pairsInSortedRotated ( arr , 6 , s ) ) NEW_LINE


import java . io . * ; class GFG { static long getSum ( long n , int d ) { if ( n < d ) return 0 ; while ( n % 10 != d ) n -- ; long k = n / 10 ; return ( k + 1 ) * d + ( k * 10 + 10 * k * k ) / 2 ; } public static void main ( String [ ] args ) { long n = 30 ; int d = 3 ; System . out . println ( getSum ( n , d ) ) ; } }

def getSum ( n , d ) : NEW_LINE INDENT if ( n < d ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT while ( n % 10 != d ) : NEW_LINE INDENT n -= 1 NEW_LINE DEDENT k = n // 10 NEW_LINE return ( ( k + 1 ) * d + ( k * 10 + 10 * k * k ) // 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 30 NEW_LINE d = 3 NEW_LINE print ( getSum ( n , d ) ) NEW_LINE DEDENT


class GFG { static float rmsValue ( int arr [ ] , int n ) { int square = 0 ; float mean = 0 ; float root = 0 ; for ( int i = 0 ; i < n ; i ++ ) { square += Math . pow ( arr [ i ] , 2 ) ; } mean = ( square / ( float ) ( n ) ) ; root = ( float ) Math . sqrt ( mean ) ; return root ; } public static void main ( String args [ ] ) { int arr [ ] = { 10 , 4 , 6 , 8 } ; int n = arr . length ; System . out . println ( rmsValue ( arr , n ) ) ; } }

import math NEW_LINE def rmsValue ( arr , n ) : NEW_LINE INDENT square = 0 NEW_LINE mean = 0.0 NEW_LINE root = 0.0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT square += ( arr [ i ] ** 2 ) NEW_LINE DEDENT mean = ( square / ( float ) ( n ) ) NEW_LINE root = math . sqrt ( mean ) NEW_LINE return root NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 4 , 6 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( rmsValue ( arr , n ) ) NEW_LINE DEDENT


class GFG { static final long MAX = 1000000000 ; static void findTriangle ( long S ) { long X1 = 0 , Y1 = 0 ; long X2 = MAX , Y2 = 1 ; long X3 = ( MAX - S % MAX ) % MAX ; long Y3 = ( S + X3 ) / MAX ; System . out . println ( " ( " + X1 + " , ▁ " + Y1 + " ) " ) ; System . out . println ( " ( " + X2 + " , ▁ " + Y2 + " ) " ) ; System . out . println ( " ( " + X3 + " , ▁ " + Y3 + " ) " ) ; } public static void main ( String [ ] args ) { long S = 4 ; findTriangle ( S ) ; } }

MAX = 1000000000 ; NEW_LINE def findTriangle ( S ) : NEW_LINE INDENT X1 = 0 ; Y1 = 0 ; NEW_LINE X2 = MAX ; Y2 = 1 ; NEW_LINE X3 = ( MAX - S % MAX ) % MAX ; NEW_LINE Y3 = ( S + X3 ) / MAX ; NEW_LINE print ( " ( " , X1 , " , " , Y1 , " ) " ) ; NEW_LINE print ( " ( " , X2 , " , " , Y2 , " ) " ) ; NEW_LINE print ( " ( " , X3 , " , " , Y3 , " ) " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = 4 ; NEW_LINE findTriangle ( S ) ; NEW_LINE DEDENT


class GFG { static int xorOfArray ( int arr [ ] , int n ) { int xor_arr = 0 ; for ( int i = 0 ; i < n ; i ++ ) { xor_arr = xor_arr ^ arr [ i ] ; } return xor_arr ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . length ; System . out . println ( xorOfArray ( arr , n ) ) ; } }

def xorOfArray ( arr , n ) : NEW_LINE INDENT xor_arr = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT xor_arr = xor_arr ^ arr [ i ] NEW_LINE DEDENT return xor_arr NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 9 , 12 , 13 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( xorOfArray ( arr , n ) ) NEW_LINE DEDENT


class GFG { static boolean check ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( s . charAt ( i ) > s . charAt ( i + 1 ) ) return true ; } return false ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; if ( check ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def check ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( s [ i ] > s [ i + 1 ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE if ( check ( 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 minJumps ( int a , int b , int d ) { int temp = a ; a = Math . min ( a , b ) ; b = Math . max ( temp , b ) ; if ( d >= b ) return ( d + b - 1 ) / b ; if ( d == 0 ) return 0 ; if ( d == a ) return 1 ; return 2 ; } public static void main ( String [ ] args ) { int a = 3 , b = 4 , d = 11 ; System . out . println ( minJumps ( a , b , d ) ) ; } }

def minJumps ( a , b , d ) : NEW_LINE INDENT temp = a NEW_LINE a = min ( a , b ) NEW_LINE b = max ( temp , b ) NEW_LINE if ( d >= b ) : NEW_LINE INDENT return ( d + b - 1 ) / b NEW_LINE DEDENT if ( d == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( d == a ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 2 NEW_LINE DEDENT a , b , d = 3 , 4 , 11 NEW_LINE print ( int ( minJumps ( a , b , d ) ) ) NEW_LINE


class GFG { static String maxInt ( char str [ ] ) { for ( int i = 0 ; i < str . length ; i ++ ) { if ( str [ i ] < '5' ) { str [ i ] = ( char ) ( ( '9' - str [ i ] ) + '0' ) ; } } if ( str [ 0 ] == '0' ) str [ 0 ] = '9' ; String str2 = new String ( str ) ; return str2 ; } public static void main ( String [ ] args ) { String str = "42" ; System . out . println ( maxInt ( str . toCharArray ( ) ) ) ; } }

def maxInt ( string ) : NEW_LINE INDENT string2 = " " NEW_LINE for i in range ( 0 , len ( string ) ) : NEW_LINE INDENT if ( string [ i ] < '5' ) : NEW_LINE INDENT string2 += str ( ( ord ( '9' ) - ord ( string [ i ] ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT string2 += str ( string [ i ] ) NEW_LINE DEDENT DEDENT if ( string2 [ 0 ] == '0' ) : NEW_LINE INDENT string2 [ 0 ] = '9' NEW_LINE DEDENT return string2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT string = "42" NEW_LINE print ( maxInt ( string ) ) NEW_LINE DEDENT


import java . util . * ; class gfg { static int sub_arrays ( int [ ] a , int n , int k ) { int sum [ ] = new int [ n + 2 ] ; for ( int i = 0 ; i < n + 2 ; i ++ ) { sum [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { a [ i ] -- ; a [ i ] %= k ; sum [ i + 1 ] += sum [ i ] + a [ i ] ; sum [ i + 1 ] %= k ; } int ans = 0 , l = 0 , r = 0 ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n + 1 ; i ++ ) { mp . put ( sum [ i ] , 0 ) ; } int temp ; for ( int i = 0 ; i < n + 1 ; i ++ ) { ans += ( int ) mp . get ( sum [ i ] ) ; temp = ( int ) mp . get ( sum [ i ] ) + 1 ; mp . put ( sum [ i ] , temp ) ; r ++ ; if ( r - l >= k ) { temp = ( int ) mp . get ( sum [ l ] ) - 1 ; mp . put ( sum [ l ] , temp ) ; l ++ ; } } return ans ; } public static void main ( String args [ ] ) { int [ ] a = { 1 , 4 , 2 , 3 , 5 } ; int n = a . length ; int k = 4 ; System . out . print ( sub_arrays ( a , n , k ) ) ; } }

def sub_arrays ( a , n , k ) : NEW_LINE INDENT sum = [ 0 for i in range ( n + 2 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] -= 1 NEW_LINE a [ i ] %= k NEW_LINE sum [ i + 1 ] += sum [ i ] + a [ i ] NEW_LINE sum [ i + 1 ] %= k NEW_LINE DEDENT ans = 0 NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE mp = dict ( ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if sum [ i ] in mp : NEW_LINE INDENT ans += mp [ sum [ i ] ] NEW_LINE DEDENT mp [ sum [ i ] ] = mp . get ( sum [ i ] , 0 ) + 1 NEW_LINE r += 1 NEW_LINE if ( r - l >= k ) : NEW_LINE INDENT mp [ sum [ l ] ] -= 1 NEW_LINE l += 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 1 , 4 , 2 , 3 , 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 4 NEW_LINE print ( sub_arrays ( a , n , k ) ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static int lastKDigits ( int a [ ] , int n , int k ) { int num = ( int ) ( Math . pow ( 10 , k ) ) ; int mul = a [ 0 ] % num ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i ] % num ; mul = ( a [ i ] * mul ) % num ; } return mul ; } public static void main ( String args [ ] ) { int a [ ] = { 22 , 31 , 44 , 27 , 37 , 43 } ; int k = 2 ; int n = a . length ; System . out . println ( lastKDigits ( a , n , k ) ) ; } }

import math NEW_LINE def lastKDigits ( a , n , k ) : NEW_LINE INDENT num = ( int ) ( math . pow ( 10 , k ) ) NEW_LINE mul = a [ 0 ] % num NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = a [ i ] % num NEW_LINE mul = ( a [ i ] * mul ) % num NEW_LINE DEDENT return mul NEW_LINE DEDENT a = [ 22 , 31 , 44 , 27 , 37 , 43 ] NEW_LINE k = 2 NEW_LINE n = len ( a ) NEW_LINE print ( lastKDigits ( a , n , k ) ) NEW_LINE


import java . io . * ; class GFG { static int getMinVal ( int p , int q ) { if ( q % p == 0 ) return p ; return - 1 ; } public static void main ( String [ ] args ) { int p = 24 , q = 48 ; System . out . println ( getMinVal ( p , q ) ) ; } }

def getMinVal ( p , q ) : NEW_LINE INDENT if q % p == 0 : NEW_LINE INDENT return p NEW_LINE DEDENT return - 1 NEW_LINE DEDENT p = 24 ; q = 48 NEW_LINE print ( getMinVal ( p , q ) ) NEW_LINE


class GFG { static double increaseInVol ( double l , double b , double h ) { double percentInc = ( 1 + ( l / 100 ) ) * ( 1 + ( b / 100 ) ) * ( 1 + ( h / 100 ) ) ; percentInc -= 1 ; percentInc *= 100 ; return percentInc ; } public static void main ( String [ ] args ) { double l = 50 , b = 20 , h = 10 ; System . out . println ( increaseInVol ( l , b , h ) + " % " ) ; } }

def increaseInVol ( l , b , h ) : NEW_LINE INDENT percentInc = ( ( 1 + ( l / 100 ) ) * ( 1 + ( b / 100 ) ) * ( 1 + ( h / 100 ) ) ) NEW_LINE percentInc -= 1 NEW_LINE percentInc *= 100 NEW_LINE return percentInc NEW_LINE DEDENT l = 50 NEW_LINE b = 20 NEW_LINE h = 10 NEW_LINE print ( increaseInVol ( l , b , h ) , " % " ) NEW_LINE


import java . util . * ; class GFG { static boolean isPrime ( int n ) { int i ; if ( n == 1 ) return false ; for ( i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static int countDistinct ( String s ) { Set < Character > m = new HashSet < Character > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { m . add ( s . charAt ( i ) ) ; } return m . size ( ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; if ( isPrime ( countDistinct ( str ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def countDistinct ( s ) : NEW_LINE INDENT m = { } NEW_LINE m = m . fromkeys ( s , 0 ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE DEDENT return len ( m . keys ( ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " NEW_LINE if isPrime ( countDistinct ( str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static boolean isSemiPrime ( int num ) { int cnt = 0 ; for ( int i = 2 ; cnt < 2 && i * i <= num ; ++ i ) while ( num % i == 0 ) { num /= i ; ++ cnt ; } if ( num > 1 ) ++ cnt ; return cnt == 2 ? true : false ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static boolean isChenPrime ( int n ) { if ( isPrime ( n ) && ( isSemiPrime ( n + 2 ) || isPrime ( n + 2 ) ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 7 ; if ( isChenPrime ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

import math NEW_LINE def isSemiPrime ( num ) : NEW_LINE INDENT cnt = 0 NEW_LINE for i in range ( 2 , int ( math . sqrt ( num ) ) + 1 ) : NEW_LINE INDENT while num % i == 0 : NEW_LINE INDENT num /= i NEW_LINE cnt += 1 NEW_LINE DEDENT if cnt >= 2 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( num > 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT return cnt == 2 NEW_LINE DEDENT def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False 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 False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def isChenPrime ( n ) : NEW_LINE INDENT if ( isPrime ( n ) and ( isSemiPrime ( n + 2 ) or isPrime ( n + 2 ) ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 7 NEW_LINE if ( isChenPrime ( n ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT


class GFG { static int ___gcd ( int a , int b ) { if ( a == 0 || b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return ___gcd ( a - b , b ) ; return ___gcd ( a , b - a ) ; } static int findLargest ( int arr [ ] , int n ) { int gcd = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) gcd = ___gcd ( gcd , arr [ i ] ) ; return ( gcd == 1 ) ? n : - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 7 } ; int n = arr . length ; System . out . print ( " Length ▁ of ▁ the ▁ " + " largest ▁ subarray ▁ = ▁ " + findLargest ( arr , n ) ) ; } }

def ___gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 or b == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return ___gcd ( a - b , b ) NEW_LINE DEDENT return ___gcd ( a , b - a ) NEW_LINE DEDENT def findLargest ( arr , n ) : NEW_LINE INDENT gcd = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT gcd = ___gcd ( gcd , arr [ i ] ) NEW_LINE DEDENT return n if ( gcd == 1 ) else - 1 NEW_LINE DEDENT arr = [ 1 , 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ of ▁ the ▁ largest ▁ subarray ▁ = ▁ " , findLargest ( arr , n ) ) NEW_LINE


class GFG { static final int MAXN = 100001 ; static boolean prime [ ] = new boolean [ MAXN ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < prime . length ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAXN ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * p ; i < MAXN ; i += p ) prime [ i ] = false ; } } } static void common_prime ( int a , int b ) { int gcd = ( int ) __gcd ( a , b ) ; for ( int i = 2 ; i <= ( gcd ) ; i ++ ) { if ( prime [ i ] && gcd % i == 0 ) { System . out . print ( i + " ▁ " ) ; } } } static long __gcd ( long a , long b ) { if ( a == 0 ) return b ; return __gcd ( b % a , a ) ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int a = 6 , b = 12 ; common_prime ( a , b ) ; } }

from math import gcd , sqrt NEW_LINE prime = [ True ] * 100001 NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( 100001 ) ) + 1 ) : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p ** 2 , 100001 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def common_prime ( a , b ) : NEW_LINE INDENT __gcd = gcd ( a , b ) NEW_LINE for i in range ( 2 , __gcd + 1 ) : NEW_LINE INDENT if prime [ i ] and __gcd % i == 0 : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE a , b = 6 , 12 NEW_LINE common_prime ( a , b ) NEW_LINE DEDENT


class GFG { static String smallestPoss ( String s , int n ) { String ans = " " ; int arr [ ] = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ s . charAt ( i ) - 48 ] ++ ; } for ( int i = 0 ; i < 10 ; i ++ ) { for ( int j = 0 ; j < arr [ i ] ; j ++ ) ans = ans + String . valueOf ( i ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 15 ; String K = "325343273113434" ; System . out . print ( smallestPoss ( K , N ) ) ; } }

def smallestPoss ( s , n ) : NEW_LINE INDENT ans = " " ; NEW_LINE arr = [ 0 ] * 10 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ ord ( s [ i ] ) - 48 ] += 1 ; NEW_LINE DEDENT for i in range ( 10 ) : NEW_LINE INDENT for j in range ( arr [ i ] ) : NEW_LINE INDENT ans = ans + str ( i ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 15 ; NEW_LINE K = "325343273113434" ; NEW_LINE print ( smallestPoss ( K , N ) ) ; NEW_LINE DEDENT


class GFG { static String maximumNum ( String curr_num , String res ) { int len1 = curr_num . length ( ) ; int len2 = res . length ( ) ; if ( len1 == len2 ) { int i = 0 ; while ( curr_num . charAt ( i ) == res . charAt ( i ) ) i ++ ; if ( curr_num . charAt ( i ) < res . charAt ( i ) ) return res ; else return curr_num ; } return len1 < len2 ? res : curr_num ; } static String extractMaximum ( String str ) { int n = str . length ( ) ; String curr_num = " " ; String res = " " ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n && str . charAt ( i ) == '0' ) i ++ ; while ( i < n && Character . isDigit ( str . charAt ( i ) ) ) { curr_num = curr_num + str . charAt ( i ) ; i ++ ; } if ( i == n ) break ; if ( curr_num . length ( ) > 0 ) i -- ; res = maximumNum ( curr_num , res ) ; curr_num = " " ; } if ( curr_num . length ( ) == 0 && res . length ( ) == 0 ) res = res + '0' ; return maximumNum ( curr_num , res ) ; } public static void main ( String [ ] args ) { String str = "100klh564abc365bg " ; System . out . println ( extractMaximum ( str ) ) ; } }

def maximumNum ( curr_num , res ) : NEW_LINE INDENT len1 = len ( curr_num ) ; NEW_LINE len2 = len ( res ) ; NEW_LINE if ( len1 == len2 ) : NEW_LINE INDENT i = 0 ; NEW_LINE while ( curr_num [ i ] == res [ i ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( curr_num [ i ] < res [ i ] ) : NEW_LINE INDENT return res ; NEW_LINE DEDENT else : NEW_LINE INDENT return curr_num ; NEW_LINE DEDENT DEDENT return res if ( len1 < len2 ) else curr_num ; NEW_LINE DEDENT def extractMaximum ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE curr_num = " " ; NEW_LINE res = " " ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( i < n and str [ i ] == '0' ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT while ( i < n and str [ i ] >= '0' and str [ i ] <= '9' ) : NEW_LINE INDENT curr_num += str [ i ] ; NEW_LINE i += 1 ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT if ( len ( curr_num ) > 0 ) : NEW_LINE INDENT i -= 1 ; NEW_LINE DEDENT res = maximumNum ( curr_num , res ) ; NEW_LINE curr_num = " " ; NEW_LINE DEDENT if ( len ( curr_num ) == 0 and len ( res ) == 0 ) : NEW_LINE INDENT res += '0' ; NEW_LINE DEDENT return maximumNum ( curr_num , res ) ; NEW_LINE DEDENT str = "100klh564abc365bg " ; NEW_LINE print ( extractMaximum ( str ) ) ; NEW_LINE


public class GFG { static int add ( int x , int y ) { while ( y > 0 && y != 0 ) { x ++ ; y -- ; } while ( y < 0 && y != 0 ) { x -- ; y ++ ; } return x ; } public static void main ( String args [ ] ) { System . out . println ( add ( 43 , 23 ) ) ; System . out . println ( add ( 43 , - 23 ) ) ; } }

INDENT while ( y > 0 and y ) : NEW_LINE INDENT x = x + 1 NEW_LINE y = y - 1 NEW_LINE DEDENT while ( y < 0 and y ) : NEW_LINE INDENT x = x - 1 NEW_LINE y = y + 1 NEW_LINE DEDENT return x NEW_LINE DEDENT print ( add ( 43 , 23 ) ) NEW_LINE print ( add ( 43 , - 23 ) ) NEW_LINE


class GFG { static int CHAR_BIT = 8 ; static int min ( int x , int y ) { return y + ( ( x - y ) & ( ( x - y ) >> ( ( Integer . SIZE / 8 ) * CHAR_BIT - 1 ) ) ) ; } static int smallest ( int x , int y , int z ) { return Math . min ( x , Math . min ( y , z ) ) ; } public static void main ( String [ ] args ) { int x = 12 , y = 15 , z = 5 ; System . out . println ( " Minimum ▁ of ▁ 3 ▁ numbers ▁ is ▁ " + smallest ( x , y , z ) ) ; } }

CHAR_BIT = 8 NEW_LINE def min ( x , y ) : NEW_LINE INDENT return y + ( ( x - y ) &   \ ( ( x - y ) >> ( 32 * CHAR_BIT - 1 ) ) ) NEW_LINE DEDENT def smallest ( x , y , z ) : NEW_LINE INDENT return min ( x , min ( y , z ) ) NEW_LINE DEDENT x = 12 NEW_LINE y = 15 NEW_LINE z = 5 NEW_LINE print ( " Minimum ▁ of ▁ 3 ▁ numbers ▁ is ▁ " , smallest ( x , y , z ) ) NEW_LINE


import java . util . Vector ; class Test { static final int MAX_SIZE = 1000001 ; static Vector < Boolean > isprime = new Vector < > ( MAX_SIZE ) ; static Vector < Integer > prime = new Vector < > ( ) ; static Vector < Integer > SPF = new Vector < > ( MAX_SIZE ) ; static void manipulated_seive ( int N ) { isprime . set ( 0 , false ) ; isprime . set ( 1 , false ) ; for ( int i = 2 ; i < N ; i ++ ) { if ( isprime . get ( i ) ) { prime . add ( i ) ; SPF . set ( i , i ) ; } for ( int j = 0 ; j < prime . size ( ) && i * prime . get ( j ) < N && prime . get ( j ) <= SPF . get ( i ) ; j ++ ) { isprime . set ( i * prime . get ( j ) , false ) ; SPF . set ( i * prime . get ( j ) , prime . get ( j ) ) ; } } } public static void main ( String args [ ] ) { int N = 13 ; for ( int i = 0 ; i < MAX_SIZE ; i ++ ) { isprime . add ( true ) ; SPF . add ( 2 ) ; } manipulated_seive ( N ) ; for ( int i = 0 ; i < prime . size ( ) && prime . get ( i ) <= N ; i ++ ) System . out . print ( prime . get ( i ) + " ▁ " ) ; } }

MAX_SIZE = 1000001 NEW_LINE isprime = [ True ] * MAX_SIZE NEW_LINE prime = [ ] NEW_LINE SPF = [ None ] * ( MAX_SIZE ) NEW_LINE def manipulated_seive ( N ) : NEW_LINE INDENT isprime [ 0 ] = isprime [ 1 ] = False NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if isprime [ i ] == True : NEW_LINE INDENT prime . append ( i ) NEW_LINE SPF [ i ] = i NEW_LINE DEDENT j = 0 NEW_LINE while ( j < len ( prime ) and i * prime [ j ] < N and prime [ j ] <= SPF [ i ] ) : NEW_LINE INDENT isprime [ i * prime [ j ] ] = False NEW_LINE SPF [ i * prime [ j ] ] = prime [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 13 NEW_LINE manipulated_seive ( N ) NEW_LINE i = 0 NEW_LINE while i < len ( prime ) and prime [ i ] <= N : NEW_LINE INDENT print ( prime [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static boolean checkPerfectcube ( int n ) { int d = ( int ) Math . cbrt ( n ) ; if ( d * d * d == n ) return true ; return false ; } static int largestNonPerfectcubeNumber ( int [ ] a , int n ) { int maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! checkPerfectcube ( a [ i ] ) ) maxi = Math . max ( a [ i ] , maxi ) ; } return maxi ; } public static void main ( String [ ] args ) { int a [ ] = { 16 , 64 , 25 , 2 , 3 , 10 } ; int n = a . length ; System . out . print ( largestNonPerfectcubeNumber ( a , n ) ) ; } }

import math NEW_LINE def checkPerfectcube ( n ) : NEW_LINE INDENT cube_root = n ** ( 1. / 3. ) NEW_LINE if round ( cube_root ) ** 3 == n : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def largestNonPerfectcubeNumber ( a , n ) : NEW_LINE INDENT maxi = - 1 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( checkPerfectcube ( a [ i ] ) == False ) : NEW_LINE INDENT maxi = max ( a [ i ] , maxi ) NEW_LINE DEDENT DEDENT return maxi NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 16 , 64 , 25 , 2 , 3 , 10 ] NEW_LINE n = len ( a ) NEW_LINE print ( largestNonPerfectcubeNumber ( a , n ) ) NEW_LINE DEDENT


class GFG { static void findPair ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . print ( " - 1" ) ; return ; } int maximum = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) maximum = Math . max ( maximum , arr [ i ] ) ; int temp = 10000001 , r = 0 , middle = maximum / 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . abs ( middle - arr [ i ] ) < temp && n % 2 == 0 ) { temp = Math . abs ( middle - arr [ i ] ) ; r = arr [ i ] ; } else if ( Math . min ( Math . abs ( middle - arr [ i ] ) , Math . abs ( middle + 1 - arr [ i ] ) ) < temp && n % 2 == 1 ) { temp = Math . min ( Math . abs ( middle - arr [ i ] ) , Math . abs ( middle + 1 - arr [ i ] ) ) ; r = arr [ i ] ; } } System . out . print ( " n ▁ = ▁ " + maximum + " ▁ and ▁ r ▁ = ▁ " + r ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . length ; findPair ( arr , n ) ; } }

def findPair ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " - 1" , end = " ▁ " ) ; NEW_LINE return ; NEW_LINE DEDENT maximum = max ( arr ) ; NEW_LINE temp = 10000001 ; r = 0 ; middle = maximum // 2 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( abs ( middle - arr [ i ] ) < temp and n % 2 == 0 ) : NEW_LINE INDENT temp = abs ( middle - arr [ i ] ) ; NEW_LINE r = arr [ i ] ; NEW_LINE DEDENT elif ( min ( abs ( middle - arr [ i ] ) , abs ( middle + 1 - arr [ i ] ) ) < temp and n % 2 == 1 ) : NEW_LINE INDENT temp = min ( abs ( middle - arr [ i ] ) , abs ( middle + 1 - arr [ i ] ) ) ; NEW_LINE r = arr [ i ] ; NEW_LINE DEDENT DEDENT print ( " n ▁ = " , maximum , " and ▁ r ▁ = " , r ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findPair ( arr , n ) ; NEW_LINE DEDENT