class GFG { static void printArray ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void findArray ( int [ ] q , int n ) { int ans ; int [ ] arr = new int [ n ] ; for ( int k = 0 , j = 0 ; j < n / 4 ; j ++ ) { ans = q [ k ] ^ q [ k + 3 ] ; arr [ k + 1 ] = q [ k + 1 ] ^ ans ; arr [ k + 2 ] = q [ k + 2 ] ^ ans ; arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) ; arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) ; k += 4 ; } printArray ( arr , n ) ; } public static void main ( String args [ ] ) { int [ ] q = { 4 , 1 , 7 , 0 } ; int n = q . length ; findArray ( q , n ) ; } }

def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def findArray ( q , n ) : NEW_LINE INDENT arr = [ None ] * n NEW_LINE k = 0 NEW_LINE for j in range ( int ( n / 4 ) ) : NEW_LINE INDENT ans = q [ k ] ^ q [ k + 3 ] NEW_LINE arr [ k + 1 ] = q [ k + 1 ] ^ ans NEW_LINE arr [ k + 2 ] = q [ k + 2 ] ^ ans NEW_LINE arr [ k ] = q [ k ] ^ ( ( arr [ k + 1 ] ) ^ ( arr [ k + 2 ] ) ) NEW_LINE arr [ k + 3 ] = q [ k + 3 ] ^ ( arr [ k + 1 ] ^ arr [ k + 2 ] ) NEW_LINE k += 4 NEW_LINE DEDENT printArray ( arr , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = [ 4 , 1 , 7 , 0 ] NEW_LINE n = len ( q ) NEW_LINE findArray ( q , n ) NEW_LINE DEDENT


class GFG { static int countWays ( int n , int arr [ ] , int len ) { if ( n == 0 ) return 1 ; int no_ways = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( n - arr [ i ] >= 0 ) { no_ways += countWays ( n - arr [ i ] , arr , len ) ; } } return no_ways ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 3 , 5 } ; int len = arr . length ; ; int n = 5 ; System . out . println ( countWays ( n , arr , len ) ) ; } }

def countWays ( n , arr ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT no_ways = 0 NEW_LINE for i in arr : NEW_LINE INDENT if ( n - i >= 0 ) : NEW_LINE INDENT no_ways = no_ways + countWays ( n - i , arr ) NEW_LINE DEDENT DEDENT return no_ways NEW_LINE DEDENT arr = [ 1 , 3 , 5 ] NEW_LINE n = 5 NEW_LINE print ( countWays ( n , arr ) ) NEW_LINE


class GFG { static int minPower ( int n , int x ) { if ( x == 1 ) return n ; int ans = 0 ; while ( n > 0 ) { ans += ( n % x ) ; n /= x ; } return ans ; } public static void main ( String [ ] args ) { int n = 5 , x = 3 ; System . out . println ( minPower ( n , x ) ) ; } }

def minPower ( n , x ) : NEW_LINE INDENT if ( x == 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT ans = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT ans += ( n % x ) NEW_LINE n //= x NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 5 NEW_LINE x = 3 NEW_LINE print ( minPower ( n , x ) ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { public static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } public static void main ( String [ ] args ) { int a = 10 , b = 15 , g ; g = gcd ( a , b ) ; System . out . println ( " GCD ( " + a + " ▁ , ▁ " + b + " ) ▁ = ▁ " + g ) ; a = 35 ; b = 10 ; g = gcd ( a , b ) ; System . out . println ( " GCD ( " + a + " ▁ , ▁ " + b + " ) ▁ = ▁ " + g ) ; a = 31 ; b = 2 ; g = gcd ( a , b ) ; System . out . println ( " GCD ( " + a + " ▁ , ▁ " + b + " ) ▁ = ▁ " + g ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if a == 0 : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT a = 10 NEW_LINE b = 15 NEW_LINE print ( " gcd ( " , a , " , " , b , " ) ▁ = ▁ " , gcd ( a , b ) ) NEW_LINE a = 35 NEW_LINE b = 10 NEW_LINE print ( " gcd ( " , a , " , " , b , " ) ▁ = ▁ " , gcd ( a , b ) ) NEW_LINE a = 31 NEW_LINE b = 2 NEW_LINE print ( " gcd ( " , a , " , " , b , " ) ▁ = ▁ " , gcd ( a , b ) ) NEW_LINE


import java . util . * ; class GFG { static boolean pointsAreOnSameSideOfLine ( int a , int b , int c , int x1 , int y1 , int x2 , int y2 ) { int fx1 ; int fx2 ; fx1 = a * x1 + b * y1 - c ; fx2 = a * x2 + b * y2 - c ; if ( ( fx1 * fx2 ) > 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int a = 1 , b = 1 , c = 1 ; int x1 = 1 , y1 = 1 ; int x2 = 2 , y2 = 1 ; if ( pointsAreOnSameSideOfLine ( a , b , c , x1 , y1 , x2 , y2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def pointsAreOnSameSideOfLine ( a , b , c , x1 , y1 , x2 , y2 ) : NEW_LINE INDENT fx1 = 0 NEW_LINE fx2 = 0 NEW_LINE fx1 = a * x1 + b * y1 - c NEW_LINE fx2 = a * x2 + b * y2 - c NEW_LINE if ( ( fx1 * fx2 ) > 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT a , b , c = 1 , 1 , 1 NEW_LINE x1 , y1 = 1 , 1 NEW_LINE x2 , y2 = 2 , 1 NEW_LINE if ( pointsAreOnSameSideOfLine ( a , b , c , x1 , y1 , x2 , y2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class Node { Node left ; Node right ; int data ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } static void PrintLeafLeftToRight ( Node root ) { Stack < Node > s1 = new Stack < > ( ) ; Stack < Node > s2 = new Stack < > ( ) ; ; s1 . push ( root ) ; while ( ! s1 . empty ( ) ) { Node curr = s1 . peek ( ) ; s1 . pop ( ) ; if ( curr . left != null ) s1 . push ( curr . left ) ; if ( curr . right != null ) s1 . push ( curr . right ) ; else if ( curr . left == null && curr . right == null ) s2 . push ( curr ) ; } while ( ! s2 . empty ( ) ) { System . out . print ( s2 . peek ( ) . data + " ▁ " ) ; s2 . pop ( ) ; } } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 7 ) ; root . left . left . left = newNode ( 10 ) ; root . left . left . right = newNode ( 11 ) ; root . right . right . left = newNode ( 8 ) ; PrintLeafLeftToRight ( root ) ; } }

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 PrintLeafLeftToRight ( root ) : NEW_LINE INDENT s1 = [ ] NEW_LINE s2 = [ ] NEW_LINE s1 . append ( root ) NEW_LINE while len ( s1 ) != 0 : NEW_LINE INDENT curr = s1 . pop ( ) NEW_LINE if curr . left : NEW_LINE INDENT s1 . append ( curr . left ) NEW_LINE DEDENT if curr . right : NEW_LINE INDENT s1 . append ( curr . right ) NEW_LINE DEDENT elif not curr . left and not curr . right : NEW_LINE INDENT s2 . append ( curr ) NEW_LINE DEDENT DEDENT while len ( s2 ) != 0 : NEW_LINE INDENT print ( s2 . pop ( ) . data , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . right . left = Node ( 5 ) NEW_LINE root . right . right = Node ( 7 ) NEW_LINE root . left . left . left = Node ( 10 ) NEW_LINE root . left . left . right = Node ( 11 ) NEW_LINE root . right . right . left = Node ( 8 ) NEW_LINE PrintLeafLeftToRight ( root ) NEW_LINE DEDENT


public class Linked_list_2D_Matrix { static class Node { int data ; Node right ; Node down ; } ; static Node construct ( int arr [ ] [ ] , int i , int j , int m , int n ) { if ( i > n - 1 || j > m - 1 ) return null ; Node temp = new Node ( ) ; temp . data = arr [ i ] [ j ] ; temp . right = construct ( arr , i , j + 1 , m , n ) ; temp . down = construct ( arr , i + 1 , j , m , n ) ; return temp ; } static void display ( Node head ) { Node Rp ; Node Dp = head ; while ( Dp != null ) { Rp = Dp ; while ( Rp != null ) { System . out . print ( Rp . data + " ▁ " ) ; Rp = Rp . right ; } System . out . println ( ) ; Dp = Dp . down ; } } public static void main ( String args [ ] ) { int arr [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int m = 3 , n = 3 ; Node head = construct ( arr , 0 , 0 , m , n ) ; display ( head ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . right = None NEW_LINE self . down = None NEW_LINE DEDENT DEDENT def construct ( arr , i , j , m , n ) : NEW_LINE INDENT if ( i > n - 1 or j > m - 1 ) : NEW_LINE INDENT return None NEW_LINE DEDENT temp = Node ( arr [ i ] [ j ] ) NEW_LINE temp . data = arr [ i ] [ j ] NEW_LINE temp . right = construct ( arr , i , j + 1 , m , n ) NEW_LINE temp . down = construct ( arr , i + 1 , j , m , n ) NEW_LINE return temp NEW_LINE DEDENT def display ( head ) : NEW_LINE INDENT Dp = head NEW_LINE while ( Dp ) : NEW_LINE INDENT Rp = Dp NEW_LINE while ( Rp ) : NEW_LINE INDENT print ( Rp . data , end = " ▁ " ) NEW_LINE Rp = Rp . right NEW_LINE DEDENT print ( ) NEW_LINE Dp = Dp . down NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE m , n = 3 , 3 NEW_LINE head = construct ( arr , 0 , 0 , m , n ) NEW_LINE display ( head ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int unitDigitXRaisedY ( int x , int y ) { int res = 1 ; for ( int i = 0 ; i < y ; i ++ ) res = ( res * x ) % 10 ; return res ; } public static void main ( String args [ ] ) throws IOException { System . out . println ( unitDigitXRaisedY ( 4 , 2 ) ) ; } }

def unitDigitXRaisedY ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( y ) : NEW_LINE INDENT res = ( res * x ) % 10 NEW_LINE DEDENT return res NEW_LINE DEDENT print ( unitDigitXRaisedY ( 4 , 2 ) ) NEW_LINE


import java . util . * ; public class GFG { static long getChicks ( int n ) { int size = Math . max ( n , 7 ) ; long [ ] dp = new long [ size ] ; dp [ 0 ] = 0 ; dp [ 1 ] = 1 ; for ( int i = 2 ; i < 6 ; i ++ ) { dp [ i ] = dp [ i - 1 ] * 3 ; } dp [ 6 ] = 726 ; for ( int i = 8 ; i <= n ; i ++ ) { dp [ i ] = ( dp [ i - 1 ] - ( 2 * dp [ i - 6 ] / 3 ) ) * 3 ; } return dp [ n ] ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( getChicks ( n ) ) ; } }

def getChicks ( n ) : NEW_LINE INDENT size = max ( n , 7 ) ; NEW_LINE dp = [ 0 ] * size ; NEW_LINE dp [ 0 ] = 0 ; NEW_LINE dp [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , 7 ) : NEW_LINE INDENT dp [ i ] = dp [ i - 1 ] * 3 ; NEW_LINE DEDENT dp [ 6 ] = 726 ; NEW_LINE for i in range ( 8 , n + 1 ) : NEW_LINE INDENT dp [ i ] = ( dp [ i - 1 ] - ( 2 * dp [ i - 6 ] // 3 ) ) * 3 ; NEW_LINE DEDENT return dp [ n ] ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( getChicks ( n ) ) ; NEW_LINE


public class GFG { static int findOnBulbs ( int numberOfBulbs ) { int onBulbs = 0 ; int bulb = 1 ; int person = 1 ; for ( bulb = 1 ; bulb <= numberOfBulbs ; bulb ++ ) { int factors = 0 ; for ( person = 1 ; person * person <= numberOfBulbs ; person ++ ) { if ( bulb % person == 0 ) { factors ++ ; if ( bulb / person != person ) { factors ++ ; } } } if ( factors % 2 == 1 ) { System . out . println ( " Light ▁ bulb ▁ " + bulb + " ▁ will ▁ be ▁ on " ) ; onBulbs ++ ; } } return onBulbs ; } public static void main ( String [ ] args ) { int numberOfBulbs = 1000 ; int onBulbs = findOnBulbs ( numberOfBulbs ) ; System . out . println ( " Total ▁ " + onBulbs + " ▁ light ▁ bulbs ▁ will ▁ be ▁ on ▁ in ▁ the ▁ end ▁ out ▁ of ▁ " + numberOfBulbs + " ▁ light ▁ bulbs " ) ; } }

def findOnBulbs ( numberOfBulbs ) : NEW_LINE INDENT onBulbs = 0 NEW_LINE bulb = 1 NEW_LINE person = 1 NEW_LINE for bulb in range ( 1 , numberOfBulbs + 1 ) : NEW_LINE INDENT factors = 0 NEW_LINE for person in range ( 1 , int ( numberOfBulbs ** ( 0.5 ) ) + 1 ) : NEW_LINE INDENT if bulb % person == 0 : NEW_LINE INDENT factors += 1 NEW_LINE if bulb // person != person : NEW_LINE INDENT factors += 1 NEW_LINE DEDENT DEDENT DEDENT if factors % 2 == 1 : NEW_LINE INDENT print ( " Light ▁ bulb " , bulb , " will ▁ be ▁ on " ) NEW_LINE onBulbs += 1 NEW_LINE DEDENT DEDENT return onBulbs NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT numberOfBulbs = 1000 NEW_LINE onBulbs = findOnBulbs ( numberOfBulbs ) NEW_LINE print ( " Total " , onBulbs , " light ▁ bulbs ▁ will " , " be ▁ on ▁ in ▁ the ▁ end ▁ out ▁ of " , numberOfBulbs , " light ▁ bulbs " ) NEW_LINE DEDENT


class GFG { final static int fact [ ] = new int [ 21 ] ; static void preCompute ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < 18 ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) ; } static void nextFactorial ( int N ) { for ( int i = 0 ; i < 21 ; i ++ ) { if ( N < fact [ i ] ) { System . out . println ( fact [ i ] ) ; break ; } } } public static void main ( String [ ] args ) { preCompute ( ) ; int N = 120 ; nextFactorial ( N ) ; } }

fact = [ 0 ] * 21 NEW_LINE def preCompute ( ) : NEW_LINE INDENT fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , 18 ) : NEW_LINE INDENT fact [ i ] = ( fact [ i - 1 ] * i ) NEW_LINE DEDENT DEDENT def nextFactorial ( N ) : NEW_LINE INDENT for i in range ( 21 ) : NEW_LINE INDENT if N < fact [ i ] : NEW_LINE INDENT print ( fact [ i ] ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT preCompute ( ) NEW_LINE N = 120 NEW_LINE nextFactorial ( N ) NEW_LINE


class GFG { static int sticks [ ] = { 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 } ; static int number [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int countSticks ( String str ) { int cnt = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch >= ' A ' && ch <= ' Z ' ) { cnt += sticks [ ch - ' A ' ] ; } else { cnt += number [ ch - '0' ] ; } } return cnt ; } public static void main ( String [ ] args ) { String str = " GEEKSFORGEEKS " ; System . out . println ( countSticks ( str ) ) ; } }

sticks = [ 6 , 7 , 4 , 6 , 5 , 4 , 6 , 5 , 2 , 4 , 4 , 3 , 6 , 6 , 6 , 5 , 7 , 6 , 5 , 3 , 5 , 4 , 6 , 4 , 3 , 4 ] ; NEW_LINE number = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] ; NEW_LINE def countSticks ( string ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT ch = string [ i ] ; NEW_LINE if ( ch >= ' A ' and ch <= ' Z ' ) : NEW_LINE INDENT cnt += sticks [ ord ( ch ) - ord ( ' A ' ) ] ; NEW_LINE DEDENT else : NEW_LINE INDENT cnt += number [ ord ( ch ) - ord ( '0' ) ] ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GEEKSFORGEEKS " ; NEW_LINE print ( countSticks ( string ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static long centeredTridecagonalNum ( long n ) { return ( 13 * n * ( n - 1 ) + 2 ) / 2 ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . println ( centeredTridecagonalNum ( n ) ) ; n = 10 ; System . out . println ( centeredTridecagonalNum ( n ) ) ; } }

def centeredTridecagonalNum ( n ) : NEW_LINE INDENT return ( 13 * n * ( n - 1 ) + 2 ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( centeredTridecagonalNum ( n ) ) NEW_LINE n = 10 NEW_LINE print ( centeredTridecagonalNum ( n ) ) NEW_LINE DEDENT


import java . util . ArrayList ; import java . util . Collections ; import java . util . List ; import java . io . * ; class GFG { static int closetAND ( int arr [ ] , int n , int k ) { int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int X = arr [ i ] ; for ( int j = i ; j < n ; j ++ ) { X &= arr [ j ] ; ans = Math . min ( ans , Math . abs ( k - X ) ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 10 } ; int n = arr . length ; int k = 2 ; System . out . println ( closetAND ( arr , n , k ) ) ; } }

def closetAND ( arr , n , k ) : NEW_LINE INDENT ans = 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT X = arr [ i ] NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT X &= arr [ j ] NEW_LINE ans = min ( ans , abs ( k - X ) ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 4 , 7 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 ; NEW_LINE print ( closetAND ( arr , n , k ) ) NEW_LINE


class GFG { static int summation ( int n ) { int abs_sum = n * ( n + 1 ) / 2 ; int sign = n + 1 % 2 == 0 ? 1 : - 1 ; int result_sum = sign * abs_sum ; return result_sum ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( summation ( N ) ) ; } }

def summation ( n ) : NEW_LINE INDENT abs_sum = n * ( n + 1 ) // 2 ; NEW_LINE sign = 1 if ( ( n + 1 ) % 2 == 0 ) else - 1 ; NEW_LINE result_sum = sign * abs_sum ; NEW_LINE return result_sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2 ; NEW_LINE print ( summation ( N ) ) ; NEW_LINE DEDENT


class GFG { static int longest_substring ( String s , int k ) { int n = s . length ( ) ; if ( k > 1 ) { s += s ; n *= 2 ; } int ans = 0 ; int i = 0 ; while ( i < n ) { int x = 0 ; while ( i < n && s . charAt ( i ) == '0' ) { x ++ ; i ++ ; } ans = Math . max ( ans , x ) ; i ++ ; } if ( k == 1 || ans != n ) return ans ; else return ( ans / 2 ) * k ; } public static void main ( String [ ] args ) { String s = "010001000" ; int k = 4 ; System . out . println ( longest_substring ( s , k ) ) ; } }

def longest_substring ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE if ( k > 1 ) : NEW_LINE INDENT s += s NEW_LINE n *= 2 NEW_LINE DEDENT ans = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT x = 0 NEW_LINE while ( i < n and s [ i ] == '0' ) : NEW_LINE INDENT x , i = x + 1 , i + 1 NEW_LINE DEDENT ans = max ( ans , x ) NEW_LINE i += 1 NEW_LINE DEDENT if ( k == 1 or ans != n ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return ( ans // 2 ) * k NEW_LINE DEDENT DEDENT s = "010001000" NEW_LINE k = 4 NEW_LINE print ( longest_substring ( s , k ) ) NEW_LINE


import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node head ; static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; head = head_ref ; } static int productOfLastN_NodesUtil ( Node head , int n ) { if ( n <= 0 ) return 0 ; Stack < Integer > st = new Stack < Integer > ( ) ; int prod = 1 ; while ( head != null ) { st . push ( head . data ) ; head = head . next ; } while ( n -- > 0 ) { prod *= st . peek ( ) ; st . pop ( ) ; } return prod ; } public static void main ( String [ ] args ) { head = null ; push ( head , 12 ) ; push ( head , 4 ) ; push ( head , 8 ) ; push ( head , 6 ) ; push ( head , 10 ) ; int n = 2 ; System . out . println ( productOfLastN_NodesUtil ( head , n ) ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT head = None NEW_LINE def push ( head_ref , new_data ) : NEW_LINE INDENT global head NEW_LINE new_node = Node ( 0 ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE head = head_ref NEW_LINE DEDENT def productOfLastN_NodesUtil ( head , n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT st = [ ] NEW_LINE prod = 1 NEW_LINE while ( head != None ) : NEW_LINE INDENT st . append ( head . data ) NEW_LINE head = head . next NEW_LINE DEDENT while ( n > 0 ) : NEW_LINE INDENT n = n - 1 NEW_LINE prod *= st [ - 1 ] NEW_LINE st . pop ( ) NEW_LINE DEDENT return prod NEW_LINE DEDENT head = None NEW_LINE push ( head , 12 ) NEW_LINE push ( head , 4 ) NEW_LINE push ( head , 8 ) NEW_LINE push ( head , 6 ) NEW_LINE push ( head , 10 ) NEW_LINE n = 2 NEW_LINE print ( productOfLastN_NodesUtil ( head , n ) ) NEW_LINE


import java . util . * ; class GFG { static int subsetSum ( int arr [ ] , int n , int i , int sum , int count ) { if ( i == n ) { if ( sum == 0 ) { count ++ ; } return count ; } count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) ; count = subsetSum ( arr , n , i + 1 , sum , count ) ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int sum = 10 ; int n = arr . length ; System . out . print ( subsetSum ( arr , n , 0 , sum , 0 ) ) ; } }

def subsetSum ( arr , n , i , sum , count ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT if ( sum == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT count = subsetSum ( arr , n , i + 1 , sum - arr [ i ] , count ) NEW_LINE count = subsetSum ( arr , n , i + 1 , sum , count ) NEW_LINE return count NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE sum = 10 NEW_LINE n = len ( arr ) NEW_LINE print ( subsetSum ( arr , n , 0 , sum , 0 ) ) NEW_LINE


class Test { static int arr [ ] = { 10 , 324 , 45 , 90 , 9808 } ; static int largest ( ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < arr . length ; i ++ ) if ( arr [ i ] > max ) max = arr [ i ] ; return max ; } public static void main ( String [ ] args ) { System . out . println ( " Largest ▁ in ▁ given ▁ array ▁ is ▁ " + largest ( ) ) ; } }

def largest ( arr , n ) : NEW_LINE INDENT return ( max ( arr ) ) NEW_LINE DEDENT arr = [ 10 , 324 , 45 , 90 , 9808 ] NEW_LINE n = len ( arr ) NEW_LINE Ans = largest ( arr , n ) NEW_LINE print ( " Largest ▁ in ▁ given ▁ array ▁ is " , Ans ) NEW_LINE


class GFG { static long factorial ( int n ) { long res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { res *= i ; } return res ; } static void printNComposite ( int n ) { long fact = factorial ( n + 1 ) ; for ( int i = 2 ; i <= n + 1 ; ++ i ) { System . out . print ( fact + i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 4 ; printNComposite ( n ) ; } }

def factorial ( n ) : NEW_LINE INDENT res = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res *= i ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def printNComposite ( n ) : NEW_LINE INDENT fact = factorial ( n + 1 ) ; NEW_LINE for i in range ( 2 , n + 2 ) : NEW_LINE INDENT print ( fact + i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT n = 4 ; NEW_LINE printNComposite ( n ) ; NEW_LINE


class GFG { static boolean isPower ( int n ) { for ( int x = 2 ; x <= Math . sqrt ( n ) ; x ++ ) { int p = x ; while ( p <= n ) { p = p * x ; if ( p == n ) return true ; } } return false ; } public static void main ( String [ ] args ) { for ( int i = 2 ; i < 100 ; i ++ ) if ( isPower ( i ) ) System . out . print ( i + " ▁ " ) ; } }

import math NEW_LINE def isPower ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for x in range ( 2 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT p = x NEW_LINE while ( p <= n ) : NEW_LINE INDENT p = p * x NEW_LINE if ( p == n ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT for i in range ( 2 , 100 ) : NEW_LINE INDENT if ( isPower ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT


import java . awt . Point ; import static java . lang . Math . PI ; import static java . lang . Math . sqrt ; import static java . lang . Math . acos ; class Test { static int lengthSquare ( Point p1 , Point p2 ) { int xDiff = p1 . x - p2 . x ; int yDiff = p1 . y - p2 . y ; return xDiff * xDiff + yDiff * yDiff ; } static void printAngle ( Point A , Point B , Point C ) { int a2 = lengthSquare ( B , C ) ; int b2 = lengthSquare ( A , C ) ; int c2 = lengthSquare ( A , B ) ; float a = ( float ) sqrt ( a2 ) ; float b = ( float ) sqrt ( b2 ) ; float c = ( float ) sqrt ( c2 ) ; float alpha = ( float ) acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; float betta = ( float ) acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; float gamma = ( float ) acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; alpha = ( float ) ( alpha * 180 / PI ) ; betta = ( float ) ( betta * 180 / PI ) ; gamma = ( float ) ( gamma * 180 / PI ) ; System . out . println ( " alpha ▁ : ▁ " + alpha ) ; System . out . println ( " betta ▁ : ▁ " + betta ) ; System . out . println ( " gamma ▁ : ▁ " + gamma ) ; } public static void main ( String [ ] args ) { Point A = new Point ( 0 , 0 ) ; Point B = new Point ( 0 , 1 ) ; Point C = new Point ( 1 , 0 ) ; printAngle ( A , B , C ) ; } }

import math NEW_LINE def lengthSquare ( X , Y ) : NEW_LINE INDENT xDiff = X [ 0 ] - Y [ 0 ] NEW_LINE yDiff = X [ 1 ] - Y [ 1 ] NEW_LINE return xDiff * xDiff + yDiff * yDiff NEW_LINE DEDENT def printAngle ( A , B , C ) : NEW_LINE INDENT a2 = lengthSquare ( B , C ) NEW_LINE b2 = lengthSquare ( A , C ) NEW_LINE c2 = lengthSquare ( A , B ) NEW_LINE a = math . sqrt ( a2 ) ; NEW_LINE b = math . sqrt ( b2 ) ; NEW_LINE c = math . sqrt ( c2 ) ; NEW_LINE alpha = math . acos ( ( b2 + c2 - a2 ) / ( 2 * b * c ) ) ; NEW_LINE betta = math . acos ( ( a2 + c2 - b2 ) / ( 2 * a * c ) ) ; NEW_LINE gamma = math . acos ( ( a2 + b2 - c2 ) / ( 2 * a * b ) ) ; NEW_LINE alpha = alpha * 180 / math . pi ; NEW_LINE betta = betta * 180 / math . pi ; NEW_LINE gamma = gamma * 180 / math . pi ; NEW_LINE print ( " alpha ▁ : ▁ % f " % ( alpha ) ) NEW_LINE print ( " betta ▁ : ▁ % f " % ( betta ) ) NEW_LINE print ( " gamma ▁ : ▁ % f " % ( gamma ) ) NEW_LINE DEDENT A = ( 0 , 0 ) NEW_LINE B = ( 0 , 1 ) NEW_LINE C = ( 1 , 0 ) NEW_LINE printAngle ( A , B , C ) ; NEW_LINE


import java . util . Deque ; import java . util . LinkedList ; public class SlidingWindow { static void printMax ( int arr [ ] , int n , int k ) { Deque < Integer > Qi = new LinkedList < Integer > ( ) ; int i ; for ( i = 0 ; i < k ; ++ i ) { while ( ! Qi . isEmpty ( ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . removeLast ( ) ; Qi . addLast ( i ) ; } for ( ; i < n ; ++ i ) { System . out . print ( arr [ Qi . peek ( ) ] + " ▁ " ) ; while ( ( ! Qi . isEmpty ( ) ) && Qi . peek ( ) <= i - k ) Qi . removeFirst ( ) ; while ( ( ! Qi . isEmpty ( ) ) && arr [ i ] >= arr [ Qi . peekLast ( ) ] ) Qi . removeLast ( ) ; Qi . addLast ( i ) ; } System . out . print ( arr [ Qi . peek ( ) ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 1 , 78 , 90 , 57 , 89 , 56 } ; int k = 3 ; printMax ( arr , arr . length , k ) ; } }

from collections import deque NEW_LINE def printMax ( arr , n , k ) : NEW_LINE INDENT Qi = deque ( ) NEW_LINE for i in range ( k ) : NEW_LINE INDENT while Qi and arr [ i ] >= arr [ Qi [ - 1 ] ] : NEW_LINE INDENT Qi . pop ( ) NEW_LINE DEDENT Qi . append ( i ) ; NEW_LINE DEDENT for i in range ( k , n ) : NEW_LINE INDENT print ( str ( arr [ Qi [ 0 ] ] ) + " ▁ " , end = " " ) NEW_LINE while Qi and Qi [ 0 ] <= i - k : NEW_LINE INDENT Qi . popleft ( ) NEW_LINE DEDENT while Qi and arr [ i ] >= arr [ Qi [ - 1 ] ] : NEW_LINE INDENT Qi . pop ( ) NEW_LINE DEDENT Qi . append ( i ) NEW_LINE DEDENT print ( str ( arr [ Qi [ 0 ] ] ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 1 , 78 , 90 , 57 , 89 , 56 ] NEW_LINE k = 3 NEW_LINE printMax ( arr , len ( arr ) , k ) NEW_LINE DEDENT


class GFG { static int findSmallNum ( int N , int K ) { int rem = N % K ; if ( rem == 0 ) return N ; else return N - rem ; } static int findLargeNum ( int N , int K ) { int rem = ( N + K ) % K ; if ( rem == 0 ) return N ; else return N + K - rem ; } static int sumDivisibles ( int A , int B , int M ) { int first = findSmallNum ( A , M ) ; int last = findLargeNum ( B , M ) ; if ( first < A ) first += M ; if ( last > B ) first -= M ; int n = ( B / M ) - ( A - 1 ) / M ; return n * ( first + last ) / 2 ; } public static void main ( String [ ] args ) { int A = 6 , B = 15 , M = 3 ; System . out . print ( sumDivisibles ( A , B , M ) ) ; } }

def findSmallNum ( N , K ) : NEW_LINE INDENT rem = N % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N - rem NEW_LINE DEDENT DEDENT def findLargeNum ( N , K ) : NEW_LINE INDENT rem = ( N + K ) % K NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT else : NEW_LINE INDENT return N + K - rem NEW_LINE DEDENT DEDENT def sumDivisibles ( A , B , M ) : NEW_LINE INDENT sum = 0 NEW_LINE first = findSmallNum ( A , M ) NEW_LINE last = findLargeNum ( B , M ) NEW_LINE if ( first < A ) : NEW_LINE INDENT first += M NEW_LINE DEDENT if ( last > B ) : NEW_LINE INDENT first -= M NEW_LINE DEDENT n = ( B // M ) - ( A - 1 ) // M NEW_LINE return n * ( first + last ) // 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 6 NEW_LINE B = 15 NEW_LINE M = 3 NEW_LINE print ( sumDivisibles ( A , B , M ) ) NEW_LINE DEDENT


class GfG { static boolean isDefeat ( String s1 , String s2 , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == '1' ) || ( s1 . charAt ( i ) == '1' && s2 . charAt ( i ) == '0' ) ) continue ; else if ( ( s1 . charAt ( i ) == '0' && s2 . charAt ( i ) == ' Z ' ) || ( s1 . charAt ( i ) == ' Z ' && s2 . charAt ( i ) == '0' ) ) continue ; else { return true ; } } return false ; } public static void main ( String [ ] args ) { String s1 = ( "01001101ZZ " ) ; String s2 = ( "10Z1001000" ) ; int n = 10 ; if ( isDefeat ( s1 , s2 , n ) ) System . out . println ( " Defeat " ) ; else System . out . println ( " Victory " ) ; } }

def isDefeat ( s1 , s2 , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( ( s1 [ i ] == '0' and s2 [ i ] == '1' ) or ( s1 [ i ] == '1' and s2 [ i ] == '0' ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT elif ( ( s1 [ i ] == '0' and s2 [ i ] == ' Z ' ) or ( s1 [ i ] == ' Z ' and s2 [ i ] == '0' ) ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT s1 = "01001101ZZ " NEW_LINE s2 = "10Z1001000" NEW_LINE n = 10 NEW_LINE if ( isDefeat ( s1 , s2 , n ) ) : NEW_LINE INDENT print ( " Defeat " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Victory " ) NEW_LINE DEDENT


public class GFG { static final int NO_OF_CHARS = 256 ; static void printDistinct ( String str ) { int [ ] count = new int [ NO_OF_CHARS ] ; int i ; for ( i = 0 ; i < str . length ( ) ; i ++ ) if ( str . charAt ( i ) != ' ▁ ' ) count [ ( int ) str . charAt ( i ) ] ++ ; int n = i ; for ( i = 0 ; i < n ; i ++ ) if ( count [ ( int ) str . charAt ( i ) ] == 1 ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String args [ ] ) { String str = " GeeksforGeeks " ; printDistinct ( str ) ; } }

NO_OF_CHARS = 256 NEW_LINE def printDistinct ( str ) : NEW_LINE INDENT count = [ 0 ] * NO_OF_CHARS NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if ( str [ i ] != ' ▁ ' ) : NEW_LINE INDENT count [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT DEDENT n = i NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( count [ ord ( str [ i ] ) ] == 1 ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " GeeksforGeeks " NEW_LINE printDistinct ( str ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int divisor ( int a ) { int div = 1 , count = 0 ; for ( int i = 2 ; i <= Math . sqrt ( a ) ; i ++ ) { while ( a % i == 0 ) { count ++ ; a = a / i ; } div = div * ( count + 1 ) ; count = 0 ; } if ( a > 1 ) { div = div * ( 2 ) ; } return div ; } static int OddDivCount ( int a , int b ) { int res = 0 ; for ( int i = a ; i <= b ; ++ i ) { int divCount = divisor ( i ) ; if ( divCount % 2 == 1 ) { ++ res ; } } return res ; } public static void main ( String [ ] args ) { int a = 1 , b = 10 ; System . out . println ( OddDivCount ( a , b ) ) ; } }

def divisor ( a ) : NEW_LINE INDENT div = 1 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( 2 , int ( pow ( a , 1 / 2 ) ) + 1 ) : NEW_LINE INDENT while ( a % i == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE a = a / i ; NEW_LINE DEDENT div = div * ( count + 1 ) ; NEW_LINE count = 0 ; NEW_LINE DEDENT if ( a > 1 ) : NEW_LINE INDENT div = div * ( 2 ) ; NEW_LINE DEDENT return div ; NEW_LINE DEDENT def OddDivCount ( a , b ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT divCount = divisor ( i ) ; NEW_LINE if ( divCount % 2 ) : NEW_LINE INDENT res += 1 ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b = 1 , 10 ; NEW_LINE print ( OddDivCount ( a , b ) ) ; NEW_LINE DEDENT


class GFG { static final int BITS = 32 ; static int andSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < BITS ; i ++ ) { int countSetBits = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) countSetBits ++ ; } int subset = ( 1 << countSetBits ) - 1 ; subset = ( subset * ( 1 << i ) ) ; ans += subset ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 } ; int size = 3 ; System . out . println ( andSum ( arr , size ) ) ; } }

BITS = 32 ; NEW_LINE def andSum ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , BITS ) : NEW_LINE INDENT countSetBits = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ j ] & ( 1 << i ) ) : NEW_LINE INDENT countSetBits = ( countSetBits + 1 ) NEW_LINE DEDENT DEDENT subset = ( ( 1 << countSetBits ) - 1 ) NEW_LINE subset = ( subset * ( 1 << i ) ) NEW_LINE ans = ans + subset NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE size = len ( arr ) NEW_LINE print ( andSum ( arr , size ) ) NEW_LINE


import java . io . * ; class GFG { static void centeredHexagonalSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( 3 * i * ( i - 1 ) + 1 + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 10 ; centeredHexagonalSeries ( n ) ; } }

def centeredHexagonalSeries ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( 3 * i * ( i - 1 ) + 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE centeredHexagonalSeries ( n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static String areAllBitsSet ( int n ) { if ( n == 0 ) return " No " ; if ( ( ( n + 1 ) & n ) == 0 ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( areAllBitsSet ( n ) ) ; } }

def areAllBitsSet ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT if ( ( ( n + 1 ) & n ) == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT n = 7 NEW_LINE print ( areAllBitsSet ( n ) ) NEW_LINE


class Solution { static final int max_val = 257 ; static int PrimeCharacters ( String s ) { boolean prime [ ] = new boolean [ max_val + 1 ] ; for ( int i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( prime [ ( int ) ( s . charAt ( i ) ) ] ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String S = " geeksforgeeks " ; System . out . print ( PrimeCharacters ( S ) ) ; } }

from math import sqrt NEW_LINE max_val = 257 NEW_LINE def PrimeCharacters ( s ) : NEW_LINE INDENT prime = [ True ] * ( max_val + 1 ) NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( max_val ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( prime [ ord ( s [ i ] ) ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " geeksforgeeks " ; NEW_LINE print ( PrimeCharacters ( S ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static String tidyNum ( String str1 , int len ) { char [ ] str = str1 . toCharArray ( ) ; for ( int i = len - 2 ; i >= 0 ; i -- ) { if ( str [ i ] > str [ i + 1 ] ) { str [ i ] -- ; for ( int j = i + 1 ; j < len ; j ++ ) str [ j ] = '9' ; } } return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { String str = "11333445538" ; int len = str . length ( ) ; System . out . println ( tidyNum ( str , len ) ) ; } }

def tidyNum ( str , len ) : NEW_LINE INDENT for i in range ( len - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] > str [ i + 1 ] ) : NEW_LINE INDENT str [ i ] -= 1 NEW_LINE for j in range ( i + 1 , len ) : NEW_LINE INDENT str [ j ] = 9 NEW_LINE DEDENT DEDENT DEDENT return str NEW_LINE DEDENT str = [ 1 , 1 , 3 , 3 , 3 , 4 , 4 , 5 , 5 , 3 , 8 ] NEW_LINE len = len ( str ) NEW_LINE num = tidyNum ( str , len ) NEW_LINE for i in range ( 0 , len ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE DEDENT


class GFG { static int find_sub ( String s , int k ) { int len = s . length ( ) ; int lp = 0 , rp = 0 ; int ans = 0 ; int [ ] hash_char = new int [ 26 ] ; for ( ; rp < len ; rp ++ ) { hash_char [ s . charAt ( rp ) - ' a ' ] ++ ; while ( hash_char [ s . charAt ( rp ) - ' a ' ] > k ) { hash_char [ s . charAt ( lp ) - ' a ' ] -- ; lp ++ ; } ans += rp - lp + 1 ; } return ans ; } public static void main ( String [ ] args ) { String S = " aaabb " ; int k = 2 ; System . out . println ( find_sub ( S , k ) ) ; } }

def find_sub ( s , k ) : NEW_LINE INDENT Len = len ( s ) NEW_LINE lp , rp = 0 , 0 NEW_LINE ans = 0 NEW_LINE hash_char = [ 0 for i in range ( 256 ) ] NEW_LINE for rp in range ( Len ) : NEW_LINE INDENT hash_char [ ord ( s [ rp ] ) ] += 1 NEW_LINE while ( hash_char [ ord ( s [ rp ] ) ] > k ) : NEW_LINE INDENT hash_char [ ord ( s [ lp ] ) ] -= 1 NEW_LINE lp += 1 NEW_LINE DEDENT ans += rp - lp + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT s = " aaabb " NEW_LINE k = 2 ; NEW_LINE print ( find_sub ( s , k ) ) NEW_LINE


import java . util . * ; class GFG { static int MAX_CHAR = 256 ; static int countSubStrings ( String s , String anotherStr , int k ) { boolean illegal [ ] = new boolean [ MAX_CHAR ] ; for ( int i = 0 ; i < anotherStr . length ( ) ; i ++ ) { illegal [ anotherStr . charAt ( i ) ] = true ; } HashSet < String > us = new HashSet < String > ( ) ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { String ss = " " ; int count = 0 ; for ( int j = i ; j < s . length ( ) ; ++ j ) { if ( illegal [ s . charAt ( j ) ] ) { ++ count ; } ss = ss + s . charAt ( j ) ; if ( count <= k ) { us . add ( ss ) ; } else { break ; } } } return us . size ( ) ; } public static void main ( String [ ] args ) { String str = " acbacbacaa " ; String anotherStr = " abcdefghijklmnopqrstuvwxyz " ; int k = 2 ; System . out . println ( countSubStrings ( str , anotherStr , k ) ) ; } }

MAX_CHAR = 256 NEW_LINE def countSubStrings ( s , anotherStr , k ) : NEW_LINE INDENT illegal = [ False ] * MAX_CHAR NEW_LINE for i in range ( len ( anotherStr ) ) : NEW_LINE INDENT illegal [ ord ( anotherStr [ i ] ) ] = True NEW_LINE DEDENT us = set ( ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT ss = " " NEW_LINE count = 0 NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT if ( illegal [ ord ( s [ j ] ) ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT ss = ss + s [ j ] NEW_LINE if ( count <= k ) : NEW_LINE INDENT us . add ( ss ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return len ( us ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " acbacbacaa " NEW_LINE anotherStr = " abcdefghijklmnopqrstuvwxyz " NEW_LINE k = 2 NEW_LINE print ( countSubStrings ( string , anotherStr , k ) ) NEW_LINE DEDENT


class GFG { static int Count ( String s , int pos ) { int c = s . charAt ( pos - 1 ) ; int counter = 0 ; for ( int i = 0 ; i < pos - 1 ; i ++ ) { if ( s . charAt ( i ) == c ) counter = counter + 1 ; } return counter ; } public static void main ( String [ ] args ) { String s = " abacsddaa " ; int pos ; int n = s . length ( ) ; int query [ ] = { 9 , 3 , 2 } ; int Q = query . length ; for ( int i = 0 ; i < Q ; i ++ ) { pos = query [ i ] ; System . out . println ( Count ( s , pos ) ) ; } } }

def Count ( s , pos ) : NEW_LINE INDENT c = s [ pos - 1 ] NEW_LINE counter = 0 NEW_LINE for i in range ( pos - 1 ) : NEW_LINE INDENT if s [ i ] == c : NEW_LINE INDENT counter = counter + 1 NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abacsddaa " NEW_LINE n = len ( s ) NEW_LINE query = [ 9 , 3 , 2 ] NEW_LINE Q = len ( query ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT pos = query [ i ] NEW_LINE print ( Count ( s , pos ) ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static void recaman ( int n ) { if ( n <= 0 ) return ; System . out . printf ( " % d , ▁ " , 0 ) ; HashSet < Integer > s = new HashSet < Integer > ( ) ; s . add ( 0 ) ; int prev = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int curr = prev - i ; if ( curr < 0 || s . contains ( curr ) ) curr = prev + i ; s . add ( curr ) ; System . out . printf ( " % d , ▁ " , curr ) ; prev = curr ; } } public static void main ( String [ ] args ) { int n = 17 ; recaman ( n ) ; } }

def recaman ( n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT print ( 0 , " , " , end = ' ' ) NEW_LINE s = set ( [ ] ) NEW_LINE s . add ( 0 ) NEW_LINE prev = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr = prev - i NEW_LINE if ( curr < 0 or curr in s ) : NEW_LINE INDENT curr = prev + i NEW_LINE DEDENT s . add ( curr ) NEW_LINE print ( curr , " , " , end = ' ' ) NEW_LINE prev = curr NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 17 NEW_LINE recaman ( n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int maxProduct ( int a [ ] , int n ) { int [ ] product = new int [ n ] ; int maxA [ ] = new int [ n ] ; int maxProd = 0 ; int maxArr = 0 ; for ( int i = 0 ; i < n ; i ++ ) { product [ i ] = a [ ( i + 1 ) % n ] * a [ ( i + ( n - 1 ) ) % n ] ; if ( maxProd < product [ i ] ) { maxProd = product [ i ] ; } } return maxProd ; } public static void main ( String [ ] args ) { int [ ] a = { 5 , 6 , 4 , 3 , 2 } ; int n = a . length ; System . out . println ( maxProduct ( a , n ) ) ; } }

def maxProduct ( a , n ) : NEW_LINE INDENT product = [ 0 ] * n ; NEW_LINE maxA = [ 0 ] * n ; NEW_LINE maxProd = 0 ; NEW_LINE maxArr = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT product [ i ] = a [ ( i + 1 ) % n ] * a [ ( i + ( n - 1 ) ) % n ] ; NEW_LINE if ( maxProd < product [ i ] ) : NEW_LINE INDENT maxProd = product [ i ] ; NEW_LINE DEDENT DEDENT return maxProd ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 5 , 6 , 4 , 3 , 2 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( maxProduct ( a , n ) ) ; NEW_LINE DEDENT


class GFG { static int maxIncSubarr ( int a [ ] , int n ) { int pre [ ] = new int [ n ] ; int pos [ ] = new int [ n ] ; pre [ 0 ] = 1 ; pos [ n - 1 ] = 1 ; int l = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) pre [ i ] = pre [ i - 1 ] + 1 ; else pre [ i ] = 1 ; } l = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( a [ i ] < a [ i + 1 ] ) pos [ i ] = pos [ i + 1 ] + 1 ; else pos [ i ] = 1 ; } int ans = 0 ; l = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] > a [ i - 1 ] ) l ++ ; else l = 1 ; ans = Math . max ( ans , l ) ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( a [ i - 1 ] < a [ i + 1 ] ) ans = Math . max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 } ; int n = arr . length ; System . out . println ( maxIncSubarr ( arr , n ) ) ; } }

def maxIncSubarr ( a , n ) : NEW_LINE INDENT pre = [ 0 ] * n ; NEW_LINE pos = [ 0 ] * n ; NEW_LINE pre [ 0 ] = 1 ; NEW_LINE pos [ n - 1 ] = 1 ; NEW_LINE l = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] > a [ i - 1 ] ) : NEW_LINE INDENT pre [ i ] = pre [ i - 1 ] + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT pre [ i ] = 1 ; NEW_LINE DEDENT DEDENT l = 1 ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] < a [ i + 1 ] ) : NEW_LINE INDENT pos [ i ] = pos [ i + 1 ] + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT pos [ i ] = 1 ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE l = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] > a [ i - 1 ] ) : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l = 1 ; NEW_LINE DEDENT ans = max ( ans , l ) ; NEW_LINE DEDENT for i in range ( 1 , n - 3 ) : NEW_LINE INDENT if ( a [ i - 1 ] < a [ i + 1 ] ) : NEW_LINE INDENT ans = max ( pre [ i - 1 ] + pos [ i + 1 ] , ans ) ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxIncSubarr ( arr , n ) ) ; NEW_LINE DEDENT


public class NthNodePostOrder { static int flag = 0 ; public static void NthPostordernode ( Node root , int N ) { if ( root == null ) return ; if ( flag <= N ) { NthPostordernode ( root . left , N ) ; NthPostordernode ( root . right , N ) ; flag ++ ; if ( flag == N ) System . out . print ( root . data ) ; } } public static void main ( String args [ ] ) { Node root = new Node ( 25 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . left = new Node ( 18 ) ; root . left . right = new Node ( 22 ) ; root . right . left = new Node ( 24 ) ; root . right . right = new Node ( 32 ) ; int N = 6 ; NthPostordernode ( root , N ) ; } } class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; } } ;

class createNode : 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 flag = [ 0 ] NEW_LINE def NthPostordernode ( root , N ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( flag [ 0 ] <= N [ 0 ] ) : NEW_LINE INDENT NthPostordernode ( root . left , N ) NEW_LINE NthPostordernode ( root . right , N ) NEW_LINE flag [ 0 ] += 1 NEW_LINE if ( flag [ 0 ] == N [ 0 ] ) : NEW_LINE INDENT print ( root . data ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = createNode ( 25 ) NEW_LINE root . left = createNode ( 20 ) NEW_LINE root . right = createNode ( 30 ) NEW_LINE root . left . left = createNode ( 18 ) NEW_LINE root . left . right = createNode ( 22 ) NEW_LINE root . right . left = createNode ( 24 ) NEW_LINE root . right . right = createNode ( 32 ) NEW_LINE N = [ 6 ] NEW_LINE NthPostordernode ( root , N ) NEW_LINE DEDENT


class GFG { static int minRotation ( int input , int unlock_code ) { int rotation = 0 ; int input_digit , code_digit ; while ( input > 0 || unlock_code > 0 ) { input_digit = input % 10 ; code_digit = unlock_code % 10 ; rotation += Math . min ( Math . abs ( input_digit - code_digit ) , 10 - Math . abs ( input_digit - code_digit ) ) ; input /= 10 ; unlock_code /= 10 ; } return rotation ; } public static void main ( String [ ] args ) { int input = 28756 ; int unlock_code = 98234 ; System . out . println ( " Minimum ▁ Rotation ▁ = ▁ " + minRotation ( input , unlock_code ) ) ; } }

def minRotation ( input , unlock_code ) : NEW_LINE INDENT rotation = 0 ; NEW_LINE while ( input > 0 or unlock_code > 0 ) : NEW_LINE INDENT input_digit = input % 10 ; NEW_LINE code_digit = unlock_code % 10 ; NEW_LINE rotation += min ( abs ( input_digit - code_digit ) , 10 - abs ( input_digit - code_digit ) ) ; NEW_LINE input = int ( input / 10 ) ; NEW_LINE unlock_code = int ( unlock_code / 10 ) ; NEW_LINE DEDENT return rotation ; NEW_LINE DEDENT input = 28756 ; NEW_LINE unlock_code = 98234 ; NEW_LINE print ( " Minimum ▁ Rotation ▁ = " , minRotation ( input , unlock_code ) ) ; NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { int sum = 0 ; int i ; int pivot = findPivot ( arr , n ) ; int diff = n - 1 - pivot ; for ( i = 0 ; i < n ; i ++ ) { sum = sum + ( ( i + diff ) % n ) * arr [ i ] ; } return sum ; } static int findPivot ( int arr [ ] , int n ) { int i ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > arr [ ( i + 1 ) % n ] ) return i ; } return 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 3 , 1 , 2 } ; int n = arr . length ; int max = maxSum ( arr , n ) ; System . out . println ( max ) ; } }

def maxSum ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE pivot = findPivot ( arr , n ) NEW_LINE diff = n - 1 - pivot NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( ( i + diff ) % n ) * arr [ i ] ; NEW_LINE DEDENT return sum NEW_LINE DEDENT def findPivot ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > arr [ ( i + 1 ) % n ] ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 3 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE max = maxSum ( arr , n ) NEW_LINE print ( max ) NEW_LINE DEDENT


class GFG { static int countOrderedPairs ( int N ) { int count_pairs = 0 ; for ( int i = 1 ; i <= ( int ) Math . sqrt ( N - 1 ) ; ++ i ) { for ( int j = i ; j * i < N ; ++ j ) ++ count_pairs ; } count_pairs *= 2 ; count_pairs -= ( int ) ( Math . sqrt ( N - 1 ) ) ; return count_pairs ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . println ( countOrderedPairs ( N ) ) ; } }

from math import sqrt NEW_LINE def countOrderedPairs ( N ) : NEW_LINE INDENT count_pairs = 0 NEW_LINE p = int ( sqrt ( N - 1 ) ) + 1 NEW_LINE q = int ( sqrt ( N ) ) + 2 NEW_LINE for i in range ( 1 , p , 1 ) : NEW_LINE INDENT for j in range ( i , q , 1 ) : NEW_LINE INDENT count_pairs += 1 NEW_LINE DEDENT DEDENT count_pairs *= 2 NEW_LINE count_pairs -= int ( sqrt ( N - 1 ) ) NEW_LINE return count_pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE print ( countOrderedPairs ( N ) ) NEW_LINE DEDENT


class GFG { static boolean checkFibinnary ( int n ) { int prev_last = 0 ; while ( n != 0 ) { if ( ( n & 1 ) != 0 && prev_last != 0 ) return false ; prev_last = n & 1 ; n >>= 1 ; } return true ; } public static void main ( String [ ] args ) { int n = 10 ; if ( checkFibinnary ( n ) == true ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def checkFibinnary ( n ) : NEW_LINE INDENT prev_last = 0 NEW_LINE while ( n ) : NEW_LINE INDENT if ( ( n & 1 ) and prev_last ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev_last = n & 1 NEW_LINE n >>= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 10 NEW_LINE if ( checkFibinnary ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX_INDEX = 51 ; static int MAX_SUM = 2505 ; static int [ ] [ ] [ ] dp = new int [ MAX_INDEX ] [ MAX_SUM ] [ MAX_INDEX ] ; static int waysutil ( int index , int sum , int count , Vector < Integer > arr , int K ) { if ( index < 0 ) { return 0 ; } if ( index == 0 ) { if ( count == 0 ) { return 0 ; } int remainder = sum % count ; if ( remainder != 0 ) { return 0 ; } int average = sum / count ; if ( average == K ) { return 1 ; } } if ( dp [ index ] [ sum ] [ count ] != - 1 ) { return dp [ index ] [ sum ] [ count ] ; } int dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; int pick = waysutil ( index - 1 , sum + arr . get ( index ) , count + 1 , arr , K ) ; int total = pick + dontpick ; dp [ index ] [ sum ] [ count ] = total ; return total ; } static int ways ( int N , int K , int [ ] arr ) { Vector < Integer > Arr = new Vector < > ( ) ; Arr . add ( - 1 ) ; for ( int i = 0 ; i < N ; ++ i ) { Arr . add ( arr [ i ] ) ; } for ( int i = 0 ; i < MAX_INDEX ; i ++ ) { for ( int j = 0 ; j < MAX_SUM ; j ++ ) { for ( int l = 0 ; l < MAX_INDEX ; l ++ ) { dp [ i ] [ j ] [ l ] = - 1 ; } } } int answer = waysutil ( N , 0 , 0 , Arr , K ) ; return answer ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 } ; int N = arr . length ; int K = 5 ; System . out . println ( ways ( N , K , arr ) ) ; } }

import numpy as np NEW_LINE MAX_INDEX = 51 NEW_LINE MAX_SUM = 2505 NEW_LINE dp = np . ones ( ( MAX_INDEX , MAX_SUM , MAX_INDEX ) ) * - 1 ; NEW_LINE def waysutil ( index , sum , count , arr , K ) : NEW_LINE INDENT if ( index < 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( index == 0 ) : NEW_LINE INDENT if ( count == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT remainder = sum % count ; NEW_LINE if ( remainder != 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT average = sum // count ; NEW_LINE if ( average == K ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT if ( dp [ index ] [ sum ] [ count ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ sum ] [ count ] ; NEW_LINE DEDENT dontpick = waysutil ( index - 1 , sum , count , arr , K ) ; NEW_LINE pick = waysutil ( index - 1 , sum + arr [ index ] , count + 1 , arr , K ) ; NEW_LINE total = pick + dontpick ; NEW_LINE dp [ index ] [ sum ] [ count ] = total ; NEW_LINE return total ; NEW_LINE DEDENT def ways ( N , K , arr ) : NEW_LINE INDENT Arr = [ ] ; NEW_LINE Arr . append ( - 1 ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT Arr . append ( arr [ i ] ) ; NEW_LINE DEDENT answer = waysutil ( N , 0 , 0 , Arr , K ) ; NEW_LINE return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 6 , 2 , 8 , 7 , 6 , 5 , 9 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE K = 5 ; NEW_LINE print ( ways ( N , K , arr ) ) ; NEW_LINE DEDENT


class GFG { static boolean check ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) { return true ; } } return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 9 , 12 , 13 , 15 } ; int n = arr . length ; if ( check ( arr , n ) ) { System . out . println ( " Odd ▁ Bit - wise ▁ OR " ) ; } else { System . out . println ( " Even ▁ Bit - wise ▁ OR " ) ; } } }

def check ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] & 1 : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 9 , 12 , 13 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE if check ( arr , n ) : NEW_LINE INDENT print ( " Odd ▁ Bit - wise ▁ OR " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Even ▁ Bit - wise ▁ OR " ) NEW_LINE DEDENT DEDENT


class GFG { static final int TEN = 10 ; static void updateFreq ( int n , int [ ] freq ) { while ( n > 0 ) { int digit = n % TEN ; freq [ digit ] ++ ; n /= TEN ; } } static boolean areAnagrams ( int a , int b ) { int [ ] freqA = new int [ TEN ] ; int [ ] freqB = new int [ TEN ] ; updateFreq ( a , freqA ) ; updateFreq ( b , freqB ) ; for ( int i = 0 ; i < TEN ; i ++ ) { if ( freqA [ i ] != freqB [ i ] ) return false ; } return true ; } public static void main ( String [ ] args ) { int a = 204 , b = 240 ; if ( areAnagrams ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

TEN = 10 NEW_LINE def updateFreq ( n , freq ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT digit = n % TEN NEW_LINE freq [ digit ] += 1 NEW_LINE n //= TEN NEW_LINE DEDENT DEDENT def areAnagrams ( a , b ) : NEW_LINE INDENT freqA = [ 0 ] * TEN NEW_LINE freqB = [ 0 ] * TEN NEW_LINE updateFreq ( a , freqA ) NEW_LINE updateFreq ( b , freqB ) NEW_LINE for i in range ( TEN ) : NEW_LINE INDENT if ( freqA [ i ] != freqB [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT a = 240 NEW_LINE b = 204 NEW_LINE if ( areAnagrams ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printArray ( int N , int SUM , int K ) { int minSum = ( N * ( N + 1 ) ) / 2 ; int maxSum = ( N * K ) - ( N * ( N - 1 ) ) / 2 ; if ( minSum > SUM || maxSum < SUM ) { System . out . println ( " Not ▁ Possible " ) ; return ; } int arr [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i ++ ) arr [ i ] = i ; int sum = minSum ; for ( int i = N ; i >= 1 ; i -- ) { int x = sum + ( K - i ) ; if ( x < SUM ) { sum = sum + ( K - i ) ; arr [ i ] = K ; K -- ; } else { arr [ i ] += ( SUM - sum ) ; sum = SUM ; break ; } } for ( int i = 1 ; i <= N ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int N = 3 , SUM = 15 , K = 8 ; printArray ( N , SUM , K ) ; } }

def printArray ( N , SUM , K ) : NEW_LINE INDENT minSum = ( N * ( N + 1 ) ) / 2 NEW_LINE maxSum = ( N * K ) - ( N * ( N - 1 ) ) / 2 NEW_LINE if ( minSum > SUM or maxSum < SUM ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT arr = [ 0 for i in range ( N + 1 ) ] NEW_LINE for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT arr [ i ] = i NEW_LINE DEDENT sum = minSum NEW_LINE i = N NEW_LINE while ( i >= 1 ) : NEW_LINE INDENT x = sum + ( K - i ) NEW_LINE if ( x < SUM ) : NEW_LINE INDENT sum = sum + ( K - i ) NEW_LINE arr [ i ] = K NEW_LINE K -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] += ( SUM - sum ) NEW_LINE sum = SUM NEW_LINE break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT for i in range ( 1 , N + 1 , 1 ) : NEW_LINE INDENT print ( int ( arr [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE SUM = 15 NEW_LINE K = 8 NEW_LINE printArray ( N , SUM , K ) NEW_LINE DEDENT


class Gfg { static int arr [ ] = { 1 , 5 , 6 } ; static int countWays ( int N ) { int count [ ] = new int [ N + 1 ] ; count [ 0 ] = 1 ; for ( int i = 1 ; i <= N ; i ++ ) for ( int j = 0 ; j < arr . length ; j ++ ) if ( i >= arr [ j ] ) count [ i ] += count [ i - arr [ j ] ] ; return count [ N ] ; } public static void main ( String [ ] args ) { int N = 7 ; System . out . println ( " Total ▁ number ▁ of ▁ ways ▁ = ▁ " + countWays ( N ) ) ; } }

def countWays ( arr , m , N ) : NEW_LINE INDENT count = [ 0 for i in range ( N + 1 ) ] NEW_LINE count [ 0 ] = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( i >= arr [ j ] ) : NEW_LINE INDENT count [ i ] += count [ i - arr [ j ] ] NEW_LINE DEDENT DEDENT DEDENT return count [ N ] NEW_LINE DEDENT arr = [ 1 , 5 , 6 ] NEW_LINE m = len ( arr ) NEW_LINE N = 7 NEW_LINE print ( " Total ▁ number ▁ of ▁ ways ▁ = ▁ " , countWays ( arr , m , N ) ) NEW_LINE


import java . util . * ; class GFG { static int check ( int n , List < Integer > marks ) { Integer x = Collections . max ( marks ) ; int bonus = 100 - x ; int c = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( marks . get ( i ) + bonus >= 50 ) c += 1 ; } return c ; } public static void main ( String [ ] args ) { int n = 5 ; List < Integer > marks = Arrays . asList ( 0 , 21 , 83 , 45 , 64 ) ; System . out . println ( check ( n , marks ) ) ; } }

def check ( n , marks ) : NEW_LINE INDENT x = max ( marks ) NEW_LINE bonus = 100 - x NEW_LINE c = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( marks [ i ] + bonus >= 50 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT return c NEW_LINE DEDENT n = 5 NEW_LINE marks = [ 0 , 21 , 83 , 45 , 64 ] NEW_LINE print ( check ( n , marks ) ) NEW_LINE


import java . io . * ; class GFG { static int aliquotSum ( int n ) { int sum = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( n % i == 0 ) sum += i ; return sum ; } public static void main ( String args [ ] ) throws IOException { int n = 12 ; System . out . println ( aliquotSum ( n ) ) ; } }

def aliquotSum ( n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT sm = sm + i NEW_LINE DEDENT DEDENT return sm NEW_LINE DEDENT n = 12 NEW_LINE print ( aliquotSum ( n ) ) NEW_LINE


class GFG { static int [ ] find ( int dividend , int divisor , int start , int end ) { if ( start > end ) return new int [ ] { 0 , dividend } ; int mid = start + ( end - start ) / 2 ; int n = dividend - divisor * mid ; if ( n > divisor ) start = mid + 1 ; else if ( n < 0 ) end = mid - 1 ; else { if ( n == divisor ) { ++ mid ; n = 0 ; } return new int [ ] { mid , n } ; } return find ( dividend , divisor , start , end ) ; } static int [ ] divide ( int dividend , int divisor ) { return find ( dividend , divisor , 1 , dividend ) ; } public static void main ( String [ ] args ) { int dividend = 10 , divisor = 3 ; int [ ] ans = divide ( dividend , divisor ) ; System . out . print ( ans [ 0 ] + " , ▁ " ) ; System . out . print ( ans [ 1 ] + " \n " ) ; } }

def find ( dividend , divisor , start , end ) : NEW_LINE INDENT if ( start > end ) : NEW_LINE INDENT return ( 0 , dividend ) ; NEW_LINE DEDENT mid = start + ( end - start ) // 2 ; NEW_LINE n = dividend - divisor * mid ; NEW_LINE if ( n > divisor ) : NEW_LINE INDENT start = mid + 1 ; NEW_LINE DEDENT elif ( n < 0 ) : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( n == divisor ) : NEW_LINE INDENT mid += 1 ; NEW_LINE n = 0 ; NEW_LINE DEDENT return ( mid , n ) ; NEW_LINE DEDENT return find ( dividend , divisor , start , end ) ; NEW_LINE DEDENT def divide ( dividend , divisor ) : NEW_LINE INDENT return find ( dividend , divisor , 1 , dividend ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT dividend = 10 ; divisor = 3 ; NEW_LINE ans = divide ( dividend , divisor ) ; NEW_LINE print ( ans [ 0 ] , " , ▁ " , ans [ 1 ] ) NEW_LINE DEDENT


import java . util . * ; import java . io . * ; class GFG { static void findElements ( int arr [ ] , int n ) { int first = Integer . MIN_VALUE ; int second = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) second = arr [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] < second ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . length ; findElements ( arr , n ) ; } }

import sys NEW_LINE def findElements ( arr , n ) : NEW_LINE INDENT first = - sys . maxsize NEW_LINE second = - sys . maxsize NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > second ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] < second ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 2 , - 6 , 3 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE findElements ( arr , n ) NEW_LINE


class Funny { static boolean checkFunny ( String word ) { int i = 1 ; int j = word . length ( ) - 2 ; word = word . toLowerCase ( ) ; while ( i <= j ) { if ( ( Math . abs ( word . charAt ( i ) - word . charAt ( i - 1 ) ) ) != Math . abs ( ( word . charAt ( j ) - word . charAt ( j + 1 ) ) ) ) return false ; i ++ ; j -- ; } return true ; } static void printFunnyWords ( String str ) { str = str + " ▁ " ; String word = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char ch = str . charAt ( i ) ; if ( ch != ' ▁ ' ) word = word + ch ; else { if ( Funny . checkFunny ( word ) ) System . out . println ( word ) ; word = " " ; } } } public static void main ( String [ ] args ) { Funny . printFunnyWords ( " Miss ▁ Arora ▁ teaches ▁ us ▁ malayalam ▁ bdwy ▁ " ) ; } }

def checkFunny ( word ) : NEW_LINE INDENT i = 1 NEW_LINE j = len ( word ) - 2 NEW_LINE word = word . lower ( ) NEW_LINE while ( i <= j ) : NEW_LINE INDENT if ( ( abs ( ord ( word [ i ] ) - ord ( word [ i - 1 ] ) ) ) != abs ( ( ord ( word [ j ] ) - ord ( word [ j + 1 ] ) ) ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 1 NEW_LINE j = j - 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def printFunnyWords ( str ) : NEW_LINE INDENT str = str + " ▁ " NEW_LINE word = " " NEW_LINE i = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT ch = str [ i ] NEW_LINE if ( ch != ' ▁ ' ) : NEW_LINE INDENT word = word + ch NEW_LINE DEDENT else : NEW_LINE INDENT if ( checkFunny ( word ) ) : NEW_LINE INDENT print ( word ) NEW_LINE DEDENT word = " " NEW_LINE DEDENT DEDENT DEDENT printFunnyWords ( " Miss ▁ Arora ▁ teaches ▁ us ▁ malayalam ▁ bdwy ▁ " ) NEW_LINE


class GFG { static String find_Square_369 ( String num ) { char a , b , c , d ; if ( num . charAt ( 0 ) == '3' ) { a = '1' ; b = '0' ; c = '8' ; d = '9' ; } else if ( num . charAt ( 0 ) == '6' ) { a = '4' ; b = '3' ; c = '5' ; d = '6' ; } else { a = '9' ; b = '8' ; c = '0' ; d = '1' ; } String result = " " ; int size = num . length ( ) ; for ( int i = 1 ; i < size ; i ++ ) result += a ; result += b ; for ( int i = 1 ; i < size ; i ++ ) result += c ; result += d ; return result ; } public static void main ( String [ ] args ) { String num_3 , num_6 , num_9 ; num_3 = "3333" ; num_6 = "6666" ; num_9 = "9999" ; String result = " " ; result = find_Square_369 ( num_3 ) ; System . out . println ( " Square ▁ of ▁ " + num_3 + " ▁ is ▁ : ▁ " + result ) ; result = find_Square_369 ( num_6 ) ; System . out . println ( " Square ▁ of ▁ " + num_9 + " ▁ is ▁ : ▁ " + result ) ; result = find_Square_369 ( num_9 ) ; System . out . println ( " Square ▁ of ▁ " + num_9 + " ▁ is ▁ : ▁ " + result ) ; } }

def find_Square_369 ( num ) : NEW_LINE INDENT if ( num [ 0 ] == '3' ) : NEW_LINE INDENT a = '1' NEW_LINE b = '0' NEW_LINE c = '8' NEW_LINE d = '9' NEW_LINE DEDENT elif ( num [ 0 ] == '6' ) : NEW_LINE INDENT a = '4' NEW_LINE b = '3' NEW_LINE c = '5' NEW_LINE d = '6' NEW_LINE DEDENT else : NEW_LINE INDENT a = '9' NEW_LINE b = '8' NEW_LINE c = '0' NEW_LINE d = '1' NEW_LINE DEDENT result = " " NEW_LINE size = len ( num ) NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT result += a NEW_LINE DEDENT result += b NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT result += c NEW_LINE DEDENT result += d NEW_LINE return result NEW_LINE DEDENT num_3 = "3333" NEW_LINE num_6 = "6666" NEW_LINE num_9 = "9999" NEW_LINE result = " " NEW_LINE result = find_Square_369 ( num_3 ) NEW_LINE print ( " Square ▁ of ▁ " + num_3 + " ▁ is ▁ : ▁ " + result ) ; NEW_LINE result = find_Square_369 ( num_6 ) NEW_LINE print ( " Square ▁ of ▁ " + num_6 + " ▁ is ▁ : ▁ " + result ) ; NEW_LINE result = find_Square_369 ( num_9 ) NEW_LINE print ( " Square ▁ of ▁ " + num_9 + " ▁ is ▁ : ▁ " + result ) ; NEW_LINE


class GFG { static int longestAlternatingSubarray ( int a [ ] , int n ) { int longest = 1 ; int cnt = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] * a [ i - 1 ] < 0 ) { cnt ++ ; longest = Math . max ( longest , cnt ) ; } else cnt = 1 ; } return longest ; } public static void main ( String [ ] args ) { int a [ ] = { - 5 , - 1 , - 1 , 2 , - 2 , - 3 } ; int n = a . length ; System . out . println ( longestAlternatingSubarray ( a , n ) ) ; } }

def longestAlternatingSubarray ( a , n ) : NEW_LINE INDENT longest = 1 NEW_LINE cnt = 1 NEW_LINE i = 1 NEW_LINE while i < n : NEW_LINE INDENT if ( a [ i ] * a [ i - 1 ] < 0 ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE longest = max ( longest , cnt ) NEW_LINE DEDENT else : NEW_LINE INDENT cnt = 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return longest NEW_LINE DEDENT a = [ - 5 , - 1 , - 1 , 2 , - 2 , - 3 ] NEW_LINE n = len ( a ) NEW_LINE print ( longestAlternatingSubarray ( a , n ) ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class Main { static void converthenumber ( int n ) { String s = Integer . toString ( n ) ; String res = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == '1' || s . charAt ( i ) == '3' || s . charAt ( i ) == '5' || s . charAt ( i ) == '7' || s . charAt ( i ) == '9' ) res += s . charAt ( i ) ; if ( res . length ( ) == 2 ) break ; } if ( res . length ( ) == 2 ) System . out . println ( res ) ; else System . out . println ( - 1 ) ; } public static void main ( String [ ] args ) { int n = 18720 ; converthenumber ( n ) ; } }

def converthenumber ( n ) : NEW_LINE INDENT s = str ( n ) ; NEW_LINE res = " " ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] == '1' or s [ i ] == '3' or s [ i ] == '5' or s [ i ] == '7' or s [ i ] == '9' ) : NEW_LINE INDENT res += s [ i ] ; NEW_LINE DEDENT if ( len ( res ) == 2 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( len ( res ) == 2 ) : NEW_LINE INDENT print ( res ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 18720 ; NEW_LINE converthenumber ( n ) ; NEW_LINE DEDENT


class GFG { static long productAscii ( String str ) { long prod = 1 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { prod *= str . charAt ( i ) ; } return prod ; } public static void main ( String [ ] args ) { String str = " GfG " ; System . out . println ( productAscii ( str ) ) ; } }

def productAscii ( str ) : NEW_LINE INDENT prod = 1 NEW_LINE for i in range ( 0 , len ( str ) ) : NEW_LINE INDENT prod = prod * ord ( str [ i ] ) NEW_LINE DEDENT return prod NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " GfG " NEW_LINE print ( productAscii ( str ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int count_carry ( String a , String b ) { int carry = 0 ; int count = 0 ; int len_a = a . length ( ) , len_b = b . length ( ) ; while ( len_a != 0 || len_b != 0 ) { int x = 0 , y = 0 ; if ( len_a > 0 ) { x = a . charAt ( len_a - 1 ) - '0' ; len_a -- ; } if ( len_b > 0 ) { y = b . charAt ( len_b - 1 ) - '0' ; len_b -- ; } int sum = x + y + carry ; if ( sum >= 10 ) { carry = 1 ; count ++ ; } else carry = 0 ; } return count ; } public static void main ( String [ ] args ) { String a = "9555" , b = "555" ; int count = count_carry ( a , b ) ; if ( count == 0 ) System . out . println ( "0 \n " ) ; else if ( count == 1 ) System . out . println ( "1 \n " ) ; else System . out . println ( count ) ; } }

def count_carry ( a , b ) : NEW_LINE INDENT carry = 0 ; NEW_LINE count = 0 ; NEW_LINE len_a = len ( a ) ; NEW_LINE len_b = len ( b ) ; NEW_LINE while ( len_a != 0 or len_b != 0 ) : NEW_LINE INDENT x = 0 ; NEW_LINE y = 0 ; NEW_LINE if ( len_a > 0 ) : NEW_LINE INDENT x = int ( a [ len_a - 1 ] ) + int ( '0' ) ; NEW_LINE len_a -= 1 ; NEW_LINE DEDENT if ( len_b > 0 ) : NEW_LINE INDENT y = int ( b [ len_b - 1 ] ) + int ( '0' ) ; NEW_LINE len_b -= 1 ; NEW_LINE DEDENT sum = x + y + carry ; NEW_LINE if ( sum >= 10 ) : NEW_LINE INDENT carry = 1 ; NEW_LINE count += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT carry = 0 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT a = "9555" ; NEW_LINE b = "555" ; NEW_LINE count = count_carry ( a , b ) ; NEW_LINE if ( count == 0 ) : NEW_LINE INDENT print ( "0" ) ; NEW_LINE DEDENT elif ( count == 1 ) : NEW_LINE INDENT print ( "1" ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( count ) ; NEW_LINE DEDENT


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

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


class MaximumDiffrence { int maxDiff ( int arr [ ] , int n ) { int diff [ ] = new int [ n - 1 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) diff [ i ] = arr [ i + 1 ] - arr [ i ] ; int max_diff = diff [ 0 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( diff [ i - 1 ] > 0 ) diff [ i ] += diff [ i - 1 ] ; if ( max_diff < diff [ i ] ) max_diff = diff [ i ] ; } return max_diff ; } public static void main ( String [ ] args ) { MaximumDiffrence mxdif = new MaximumDiffrence ( ) ; int arr [ ] = { 80 , 2 , 6 , 3 , 100 } ; int size = arr . length ; System . out . println ( mxdif . maxDiff ( arr , size ) ) ; } }

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


import java . io . * ; import java . lang . * ; public class GFG { static int perfectSquare ( String s ) { int n = s . length ( ) ; int ans = - 1 ; String num = " " ; for ( int i = 1 ; i < ( 1 << n ) ; i ++ ) { String str = " " ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( ( i >> j ) & 1 ) == 1 ) { str += s . charAt ( j ) ; } } if ( str . charAt ( 0 ) != '0' ) { int temp = 0 ; for ( int j = 0 ; j < str . length ( ) ; j ++ ) temp = temp * 10 + ( int ) ( str . charAt ( j ) - '0' ) ; int k = ( int ) Math . sqrt ( temp ) ; if ( k * k == temp ) { if ( ans < ( int ) str . length ( ) ) { ans = ( int ) str . length ( ) ; num = str ; } } } } if ( ans == - 1 ) return ans ; else { System . out . print ( num + " ▁ " ) ; return n - ans ; } } public static void main ( String args [ ] ) { System . out . println ( perfectSquare ( "8314" ) ) ; System . out . println ( perfectSquare ( "753" ) ) ; } }

import math NEW_LINE def perfectSquare ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE ans = - 1 NEW_LINE num = " " NEW_LINE for i in range ( 1 , ( 1 << n ) ) : NEW_LINE INDENT str = " " NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( i >> j ) & 1 ) : NEW_LINE INDENT str = str + s [ j ] NEW_LINE DEDENT DEDENT if ( str [ 0 ] != '0' ) : NEW_LINE INDENT temp = 0 ; NEW_LINE for j in range ( 0 , len ( str ) ) : NEW_LINE INDENT temp = ( temp * 10 + ( ord ( str [ j ] ) - ord ( '0' ) ) ) NEW_LINE DEDENT k = int ( math . sqrt ( temp ) ) NEW_LINE if ( k * k == temp ) : NEW_LINE INDENT if ( ans < len ( str ) ) : NEW_LINE INDENT ans = len ( str ) NEW_LINE num = str NEW_LINE DEDENT DEDENT DEDENT DEDENT if ( ans == - 1 ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT print ( " { } ▁ " . format ( num ) , end = " " ) NEW_LINE return n - ans NEW_LINE DEDENT DEDENT print ( perfectSquare ( "8314" ) ) NEW_LINE print ( perfectSquare ( "753" ) ) ; NEW_LINE


import java . util . * ; class GFG { static int MAX = 1000 ; static int numofArray ( int n , int m ) { int [ ] [ ] dp = new int [ MAX ] [ MAX ] ; Vector < Integer > [ ] di = new Vector [ MAX ] ; Vector < Integer > [ ] mu = new Vector [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = 0 ; j < MAX ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( int i = 0 ; i < MAX ; i ++ ) { di [ i ] = new Vector < > ( ) ; mu [ i ] = new Vector < > ( ) ; } for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 2 * i ; j <= m ; j += i ) { di [ j ] . add ( i ) ; mu [ i ] . add ( j ) ; } di [ i ] . add ( i ) ; } for ( int i = 1 ; i <= m ; i ++ ) dp [ 1 ] [ i ] = 1 ; for ( int i = 2 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { dp [ i ] [ j ] = 0 ; for ( Integer x : di [ j ] ) dp [ i ] [ j ] += dp [ i - 1 ] [ x ] ; for ( Integer x : mu [ j ] ) dp [ i ] [ j ] += dp [ i - 1 ] [ x ] ; } } int ans = 0 ; for ( int i = 1 ; i <= m ; i ++ ) { ans += dp [ n ] [ i ] ; di [ i ] . clear ( ) ; mu [ i ] . clear ( ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 3 , m = 3 ; System . out . println ( numofArray ( n , m ) ) ; } }

MAX = 1000 NEW_LINE def numofArray ( n , m ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] NEW_LINE di = [ [ ] for i in range ( MAX ) ] NEW_LINE mu = [ [ ] for i in range ( MAX ) ] NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT for j in range ( 2 * i , m + 1 , i ) : NEW_LINE INDENT di [ j ] . append ( i ) NEW_LINE mu [ i ] . append ( j ) NEW_LINE DEDENT di [ i ] . append ( i ) NEW_LINE DEDENT for i in range ( 1 , m + 1 ) : NEW_LINE INDENT dp [ 1 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = 0 NEW_LINE for x in di [ j ] : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i - 1 ] [ x ] NEW_LINE DEDENT for x in mu [ j ] : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i - 1 ] [ x ] NEW_LINE DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT ans += dp [ n ] [ i ] NEW_LINE di [ i ] . clear ( ) NEW_LINE mu [ i ] . clear ( ) NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = m = 3 NEW_LINE print ( numofArray ( n , m ) ) NEW_LINE DEDENT


class GFG { static int lis ( int arr [ ] , int n ) { int result = 0 ; int [ ] lis = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) lis [ i ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( arr [ i ] > arr [ j ] && lis [ i ] < lis [ j ] + 1 ) lis [ i ] = lis [ j ] + 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( result < lis [ i ] ) result = lis [ i ] ; return result ; } static int minimumNumberOfDeletions ( int arr [ ] , int n ) { int len = lis ( arr , n ) ; return ( n - len ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 30 , 40 , 2 , 5 , 1 , 7 , 45 , 50 , 8 } ; int n = arr . length ; System . out . println ( " Minimum ▁ number ▁ of " + " ▁ deletions ▁ = ▁ " + minimumNumberOfDeletions ( arr , n ) ) ; } }

def lis ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE lis = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT lis [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] and lis [ i ] < lis [ j ] + 1 ) : NEW_LINE INDENT lis [ i ] = lis [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if ( result < lis [ i ] ) : NEW_LINE INDENT result = lis [ i ] NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT def minimumNumberOfDeletions ( arr , n ) : NEW_LINE INDENT len = lis ( arr , n ) NEW_LINE return ( n - len ) NEW_LINE DEDENT arr = [ 30 , 40 , 2 , 5 , 1 , 7 , 45 , 50 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ number ▁ of ▁ deletions ▁ = ▁ " , minimumNumberOfDeletions ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static void printNonConsecutive ( int n ) { int p = ( 1 << n ) ; for ( int i = 1 ; i < p ; i ++ ) if ( ( i & ( i << 1 ) ) == 0 ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 3 ; printNonConsecutive ( n ) ; } }

def printNonConsecutive ( n ) : NEW_LINE INDENT p = ( 1 << n ) NEW_LINE for i in range ( 1 , p ) : NEW_LINE INDENT if ( ( i & ( i << 1 ) ) == 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 3 NEW_LINE printNonConsecutive ( n ) NEW_LINE


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

def getNthTerm ( N ) : NEW_LINE INDENT return ( 7 * pow ( N , 2 ) - 19 * N + 15 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE print ( getNthTerm ( N ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static final int N = 4 ; static final int M = 4 ; static boolean isPairWithDiff ( int mat [ ] [ ] , int k ) { HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) s . add ( mat [ i ] [ j ] ) ; for ( int i = 0 ; i < N ; i ++ ) for ( int j = 0 ; j < M ; j ++ ) if ( s . contains ( k + mat [ i ] [ j ] ) && k + mat [ i ] [ j ] != ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) return true ; return false ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 5 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 100 } } ; int k = 85 ; System . out . println ( isPairWithDiff ( mat , k ) == true ? " YES " : " NO " ) ; } }

N = 4 NEW_LINE M = 4 NEW_LINE def isPairWithDiff ( mat , k ) : NEW_LINE INDENT s = dict ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT s [ mat [ i ] [ j ] ] = 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if k + mat [ i ] [ j ] in s : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT n , m = 4 , 4 NEW_LINE mat = [ [ 5 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 100 ] ] NEW_LINE k = 85 NEW_LINE if isPairWithDiff ( mat , k ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


public class GFG { static final int MAX = 256 ; static boolean compare ( char arr1 [ ] , char arr2 [ ] ) { for ( int i = 0 ; i < MAX ; i ++ ) if ( arr1 [ i ] != arr2 [ i ] ) return false ; return true ; } static void search ( String pat , String txt ) { int M = pat . length ( ) ; int N = txt . length ( ) ; char [ ] countP = new char [ MAX ] ; char [ ] countTW = new char [ MAX ] ; for ( int i = 0 ; i < M ; i ++ ) { ( countP [ pat . charAt ( i ) ] ) ++ ; ( countTW [ txt . charAt ( i ) ] ) ++ ; } for ( int i = M ; i < N ; i ++ ) { if ( compare ( countP , countTW ) ) System . out . println ( " Found ▁ at ▁ Index ▁ " + ( i - M ) ) ; ( countTW [ txt . charAt ( i ) ] ) ++ ; countTW [ txt . charAt ( i - M ) ] -- ; } if ( compare ( countP , countTW ) ) System . out . println ( " Found ▁ at ▁ Index ▁ " + ( N - M ) ) ; } public static void main ( String args [ ] ) { String txt = " BACDGABCDA " ; String pat = " ABCD " ; search ( pat , txt ) ; } }

MAX = 256 NEW_LINE def compare ( arr1 , arr2 ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT if arr1 [ i ] != arr2 [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def search ( pat , txt ) : NEW_LINE INDENT M = len ( pat ) NEW_LINE N = len ( txt ) NEW_LINE countP = [ 0 ] * MAX NEW_LINE countTW = [ 0 ] * MAX NEW_LINE for i in range ( M ) : NEW_LINE INDENT ( countP [ ord ( pat [ i ] ) ] ) += 1 NEW_LINE ( countTW [ ord ( txt [ i ] ) ] ) += 1 NEW_LINE DEDENT for i in range ( M , N ) : NEW_LINE INDENT if compare ( countP , countTW ) : NEW_LINE INDENT print ( " Found ▁ at ▁ Index " , ( i - M ) ) NEW_LINE DEDENT ( countTW [ ord ( txt [ i ] ) ] ) += 1 NEW_LINE ( countTW [ ord ( txt [ i - M ] ) ] ) -= 1 NEW_LINE DEDENT if compare ( countP , countTW ) : NEW_LINE INDENT print ( " Found ▁ at ▁ Index " , N - M ) NEW_LINE DEDENT DEDENT txt = " BACDGABCDA " NEW_LINE pat = " ABCD " NEW_LINE search ( pat , txt ) NEW_LINE


class GFG { static void countPairs ( int arr [ ] , int n ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 0 ) even ++ ; else odd ++ ; } System . out . println ( ( even ) * ( n - 1 ) ) ; System . out . println ( ( odd ) * ( n - 1 ) ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 5 } ; int n = arr . length ; countPairs ( arr , n ) ; } }

def countPairs ( arr , n ) : NEW_LINE INDENT odd = 0 NEW_LINE even = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 NEW_LINE DEDENT DEDENT odd_pairs = odd * ( n - 1 ) NEW_LINE even_pairs = even * ( n - 1 ) NEW_LINE print ( odd_pairs ) NEW_LINE print ( even_pairs ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE countPairs ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 10000 ; static HashSet < Integer > s = new HashSet < Integer > ( ) ; static void SieveOfEratosthenes ( ) { boolean [ ] prime = new boolean [ MAX ] ; Arrays . fill ( prime , true ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) prime [ i ] = false ; } } int product = 1 ; for ( int p = 2 ; p < MAX ; p ++ ) { if ( prime [ p ] ) { product = product * p ; s . add ( product + 1 ) ; } } } static boolean isEuclid ( int n ) { if ( s . contains ( n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 31 ; if ( isEuclid ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; n = 42 ; if ( isEuclid ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

MAX = 10000 NEW_LINE s = set ( ) NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime = [ True ] * ( MAX ) NEW_LINE prime [ 0 ] , prime [ 1 ] = False , False NEW_LINE for p in range ( 2 , 100 ) : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , MAX , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT product = 1 NEW_LINE for p in range ( 2 , MAX ) : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT product = product * p NEW_LINE s . add ( product + 1 ) NEW_LINE DEDENT DEDENT DEDENT def isEuclid ( n ) : NEW_LINE INDENT if n in s : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE n = 31 NEW_LINE if isEuclid ( n ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT n = 42 NEW_LINE if isEuclid ( n ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int n = 5 ; static int largestSideLen ( int matrix [ ] [ ] ) { int result = 1 ; for ( int i = 0 ; i < ( n / 2 ) ; i ++ ) { int element = matrix [ i ] [ i ] ; int isSquare = 1 ; for ( int j = i ; j < n - i ; j ++ ) { if ( matrix [ i ] [ j ] != element ) isSquare = 0 ; if ( matrix [ n - i - 1 ] [ j ] != element ) isSquare = 0 ; if ( matrix [ j ] [ i ] != element ) isSquare = 0 ; if ( matrix [ j ] [ n - i - 1 ] != element ) isSquare = 0 ; } if ( isSquare > 0 ) return n - ( 2 * i ) ; } return result ; } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 } , { 1 , 2 , 2 , 2 , 1 } , { 1 , 2 , 1 , 2 , 1 } , { 1 , 2 , 2 , 2 , 1 } , { 1 , 1 , 1 , 1 , 1 } } ; System . out . println ( largestSideLen ( matrix ) ) ; } }

n = 5 NEW_LINE def largestSideLen ( matrix ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( int ( n / 2 ) ) : NEW_LINE INDENT element = matrix [ i ] [ i ] NEW_LINE isSquare = 1 NEW_LINE for j in range ( i , n - i ) : NEW_LINE INDENT if ( matrix [ i ] [ j ] != element ) : NEW_LINE INDENT isSquare = 0 NEW_LINE DEDENT if ( matrix [ n - i - 1 ] [ j ] != element ) : NEW_LINE INDENT isSquare = 0 NEW_LINE DEDENT if ( matrix [ j ] [ i ] != element ) : NEW_LINE INDENT isSquare = 0 NEW_LINE DEDENT if ( matrix [ j ] [ n - i - 1 ] != element ) : NEW_LINE INDENT isSquare = 0 NEW_LINE DEDENT DEDENT if ( isSquare ) : NEW_LINE INDENT return n - 2 * i NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 1 , 1 , 1 , 1 , 1 ] , [ 1 , 2 , 2 , 2 , 1 ] , [ 1 , 2 , 1 , 2 , 1 ] , [ 1 , 2 , 2 , 2 , 1 ] , [ 1 , 1 , 1 , 1 , 1 ] ] NEW_LINE print ( largestSideLen ( matrix ) ) NEW_LINE DEDENT


class GFG { static float calcAngle ( int hh , int mm ) { float hour_angle = 0.5f * ( hh * 60 + mm ) ; float minute_angle = 6 * mm ; float angle = Math . abs ( hour_angle - minute_angle ) ; angle = Math . min ( 360 - angle , angle ) ; return angle ; } static void printTime ( float theta ) { for ( int hh = 0 ; hh < 12 ; hh ++ ) { for ( int mm = 0 ; mm < 60 ; mm ++ ) { if ( calcAngle ( hh , mm ) == theta ) { System . out . println ( hh + " : " + mm ) ; return ; } } } System . out . println ( " Input ▁ angle ▁ not ▁ valid . " ) ; return ; } public static void main ( String [ ] args ) { float theta = 90.0f ; printTime ( theta ) ; } }

def calcAngle ( hh , mm ) : NEW_LINE INDENT hour_angle = 0.5 * ( hh * 60 + mm ) NEW_LINE minute_angle = 6 * mm NEW_LINE angle = abs ( hour_angle - minute_angle ) NEW_LINE angle = min ( 360 - angle , angle ) NEW_LINE return angle NEW_LINE DEDENT def printTime ( theta ) : NEW_LINE INDENT for hh in range ( 0 , 12 ) : NEW_LINE INDENT for mm in range ( 0 , 60 ) : NEW_LINE INDENT if ( calcAngle ( hh , mm ) == theta ) : NEW_LINE INDENT print ( hh , " : " , mm , sep = " " ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " Input ▁ angle ▁ not ▁ valid . " ) NEW_LINE return NEW_LINE DEDENT theta = 90.0 NEW_LINE printTime ( theta ) NEW_LINE


import java . util . * ; class GFG { static void findMin ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; Arrays . sort ( arr ) ; int min = arr [ 0 ] ; int max = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int num = arr [ i ] ; int total = num + min ; int j ; for ( j = 2 ; j <= num ; j ++ ) { if ( num % j == 0 ) { int d = j ; int now = ( num / d ) + ( min * d ) ; int reduce = total - now ; if ( reduce > max ) max = reduce ; } } } System . out . println ( sum - max ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; findMin ( arr , n ) ; } }

def findMin ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum = sum + arr [ i ] NEW_LINE DEDENT arr . sort ( ) NEW_LINE min = arr [ 0 ] NEW_LINE max = 0 NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT num = arr [ i ] NEW_LINE total = num + min NEW_LINE for j in range ( 2 , num + 1 ) : NEW_LINE INDENT if ( num % j == 0 ) : NEW_LINE INDENT d = j NEW_LINE now = ( num // d ) + ( min * d ) NEW_LINE reduce = total - now NEW_LINE if ( reduce > max ) : NEW_LINE INDENT max = reduce NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( sum - max ) NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE findMin ( arr , n ) NEW_LINE


import java . util . * ; class GFG { static String findNumber ( int s , int d ) { String num = " " ; int val = s / d ; int rem = s % d ; int i ; for ( i = 1 ; i <= d - rem ; i ++ ) { num = num + String . valueOf ( val ) ; } if ( rem > 0 ) { val ++ ; for ( i = d - rem + 1 ; i <= d ; i ++ ) { num = num + String . valueOf ( val ) ; } } return num ; } public static void main ( String [ ] args ) { int s = 25 , d = 4 ; System . out . print ( findNumber ( s , d ) ) ; } }

def findNumber ( s , d ) : NEW_LINE INDENT num = " " ; NEW_LINE val = s // d ; NEW_LINE rem = s % d ; NEW_LINE for i in range ( 1 , d - rem + 1 ) : NEW_LINE INDENT num = num + str ( val ) ; NEW_LINE DEDENT if ( rem ) : NEW_LINE INDENT val += 1 ; NEW_LINE for i in range ( d - rem + 1 , d + 1 ) : NEW_LINE INDENT num = num + str ( val ) ; NEW_LINE DEDENT DEDENT return num ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = 25 ; d = 4 ; NEW_LINE print ( findNumber ( s , d ) ) ; NEW_LINE DEDENT


import java . util . * ; class Point { int x , y ; Point ( int x , int y ) { this . x = x ; this . y = y ; } } class GFG { public static int orientation ( Point p , Point q , Point r ) { int val = ( q . y - p . y ) * ( r . x - q . x ) - ( q . x - p . x ) * ( r . y - q . y ) ; if ( val == 0 ) return 0 ; return ( val > 0 ) ? 1 : 2 ; } public static void convexHull ( Point points [ ] , int n ) { if ( n < 3 ) return ; Vector < Point > hull = new Vector < Point > ( ) ; int l = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( points [ i ] . x < points [ l ] . x ) l = i ; int p = l , q ; do { hull . add ( points [ p ] ) ; q = ( p + 1 ) % n ; for ( int i = 0 ; i < n ; i ++ ) { if ( orientation ( points [ p ] , points [ i ] , points [ q ] ) == 2 ) q = i ; } p = q ; } while ( p != l ) ; for ( Point temp : hull ) System . out . println ( " ( " + temp . x + " , ▁ " + temp . y + " ) " ) ; } public static void main ( String [ ] args ) { Point points [ ] = new Point [ 7 ] ; points [ 0 ] = new Point ( 0 , 3 ) ; points [ 1 ] = new Point ( 2 , 3 ) ; points [ 2 ] = new Point ( 1 , 1 ) ; points [ 3 ] = new Point ( 2 , 1 ) ; points [ 4 ] = new Point ( 3 , 0 ) ; points [ 5 ] = new Point ( 0 , 0 ) ; points [ 6 ] = new Point ( 3 , 3 ) ; int n = points . length ; convexHull ( points , n ) ; } }

class Point : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT def Left_index ( points ) : NEW_LINE INDENT minn = 0 NEW_LINE for i in range ( 1 , len ( points ) ) : NEW_LINE INDENT if points [ i ] . x < points [ minn ] . x : NEW_LINE INDENT minn = i NEW_LINE DEDENT elif points [ i ] . x == points [ minn ] . x : NEW_LINE INDENT if points [ i ] . y > points [ minn ] . y : NEW_LINE INDENT minn = i NEW_LINE DEDENT DEDENT DEDENT return minn NEW_LINE DEDENT def orientation ( p , q , r ) : NEW_LINE INDENT val = ( q . y - p . y ) * ( r . x - q . x ) -   \ NEW_LINE INDENT ( q . x - p . x ) * ( r . y - q . y ) NEW_LINE DEDENT if val == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif val > 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT def convexHull ( points , n ) : NEW_LINE INDENT if n < 3 : NEW_LINE INDENT return NEW_LINE DEDENT l = Left_index ( points ) NEW_LINE hull = [ ] NEW_LINE p = l NEW_LINE q = 0 NEW_LINE while ( True ) : NEW_LINE INDENT hull . append ( p ) NEW_LINE q = ( p + 1 ) % n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( orientation ( points [ p ] , points [ i ] , points [ q ] ) == 2 ) : NEW_LINE INDENT q = i NEW_LINE DEDENT DEDENT p = q NEW_LINE if ( p == l ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for each in hull : NEW_LINE INDENT print ( points [ each ] . x , points [ each ] . y ) NEW_LINE DEDENT DEDENT points = [ ] NEW_LINE points . append ( Point ( 0 , 3 ) ) NEW_LINE points . append ( Point ( 2 , 2 ) ) NEW_LINE points . append ( Point ( 1 , 1 ) ) NEW_LINE points . append ( Point ( 2 , 1 ) ) NEW_LINE points . append ( Point ( 3 , 0 ) ) NEW_LINE points . append ( Point ( 0 , 0 ) ) NEW_LINE points . append ( Point ( 3 , 3 ) ) NEW_LINE convexHull ( points , len ( points ) ) NEW_LINE


import java . io . * ; class GFG { static int numberOfSolutions ( int n ) { int c = 0 ; while ( n > 0 ) { c += n % 2 ; n /= 2 ; } return ( int ) Math . pow ( 2 , c ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( numberOfSolutions ( n ) ) ; } }

from math import * NEW_LINE def numberOfSolutions ( n ) : NEW_LINE INDENT c = 0 NEW_LINE while ( n ) : NEW_LINE INDENT c += n % 2 NEW_LINE n //= 2 NEW_LINE DEDENT return int ( pow ( 2 , c ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( numberOfSolutions ( n ) ) NEW_LINE DEDENT


class GFG { static int countWays ( int [ ] arr , int n ) { int max_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) max_val = Math . max ( max_val , arr [ i ] ) ; int [ ] freq = new int [ max_val + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ arr [ i ] ] ++ ; int ans = 0 ; ans += freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) / 6 ; for ( int i = 1 ; i <= max_val ; i ++ ) ans += freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) / 2 ; for ( int i = 1 ; 2 * i <= max_val ; i ++ ) ans += freq [ i ] * ( freq [ i ] - 1 ) / 2 * freq [ 2 * i ] ; for ( int i = 1 ; i <= max_val ; i ++ ) { for ( int j = i + 1 ; i + j <= max_val ; j ++ ) ans += freq [ i ] * freq [ j ] * freq [ i + j ] ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( countWays ( arr , n ) ) ; } }

import math as mt NEW_LINE def countWays ( arr , n ) : NEW_LINE INDENT max_val = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_val = max ( max_val , arr [ i ] ) NEW_LINE DEDENT freq = [ 0 for i in range ( max_val + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE ans += ( freq [ 0 ] * ( freq [ 0 ] - 1 ) * ( freq [ 0 ] - 2 ) // 6 ) NEW_LINE for i in range ( 1 , max_val + 1 ) : NEW_LINE INDENT ans += ( freq [ 0 ] * freq [ i ] * ( freq [ i ] - 1 ) // 2 ) NEW_LINE DEDENT for i in range ( 1 , ( max_val + 1 ) // 2 ) : NEW_LINE INDENT ans += ( freq [ i ] * ( freq [ i ] - 1 ) // 2 * freq [ 2 * i ] ) NEW_LINE DEDENT for i in range ( 1 , max_val + 1 ) : NEW_LINE INDENT for j in range ( i + 1 , max_val - i + 1 ) : NEW_LINE INDENT ans += freq [ i ] * freq [ j ] * freq [ i + j ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countWays ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int maxPrimeSubarray ( int arr [ ] , int n ) { int max_ele = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; boolean prime [ ] = new boolean [ max_ele + 1 ] ; Arrays . fill ( prime , true ) ; SieveOfEratosthenes ( prime , max_ele ) ; int current_max = 0 , max_so_far = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] == false ) current_max = 0 ; else { current_max ++ ; max_so_far = Math . max ( current_max , max_so_far ) ; } } return max_so_far ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 2 , 4 , 3 , 29 , 11 , 7 , 8 , 9 } ; int n = arr . length ; System . out . print ( maxPrimeSubarray ( arr , n ) ) ; } }

def SieveOfEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , p_size + 1 ) : NEW_LINE INDENT if ( p * p > p_size ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] ) : NEW_LINE INDENT i = p * 2 NEW_LINE while ( i <= p_size ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE i = i + p NEW_LINE DEDENT DEDENT DEDENT DEDENT def maxPrimeSubarray ( arr , n ) : NEW_LINE INDENT max_ele = max ( arr ) NEW_LINE prime = [ True ] * ( max_ele + 1 ) NEW_LINE SieveOfEratosthenes ( prime , max_ele ) NEW_LINE current_max = 0 NEW_LINE max_so_far = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] == False ) : NEW_LINE INDENT current_max = 0 NEW_LINE DEDENT else : NEW_LINE INDENT current_max = current_max + 1 NEW_LINE max_so_far = max ( current_max , max_so_far ) NEW_LINE DEDENT DEDENT return max_so_far NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 , 2 , 4 , 3 , 29 , 11 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxPrimeSubarray ( arr , n ) ) NEW_LINE DEDENT