class GFG { static int countNum ( int N , int arr [ ] ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < N ; j ++ ) { if ( i == j ) continue ; else sum += arr [ j ] ; } if ( sum % arr [ i ] == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 10 , 4 , 6 , 7 } ; int n = arr . length ; System . out . println ( countNum ( n , arr ) ) ; } }

def countNum ( N , arr ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT Sum = 0 NEW_LINE for j in range ( N ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT Sum += arr [ j ] NEW_LINE DEDENT DEDENT if ( Sum % arr [ i ] == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 3 , 10 , 4 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countNum ( n , arr ) ) NEW_LINE


import java . io . * ; class GFG { static int countMaxContiguous ( int arr [ ] , int n ) { int current_max = 0 , max_so_far = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 != 0 ) 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 , 8 , 9 } ; int n = arr . length ; System . out . println ( countMaxContiguous ( arr , n ) ) ; } }

def countMaxContiguous ( arr , n ) : NEW_LINE INDENT current_max = 0 NEW_LINE max_so_far = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 != 0 ) : NEW_LINE INDENT current_max = 0 NEW_LINE DEDENT else : NEW_LINE INDENT 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 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countMaxContiguous ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int minimumflip ( int mat [ ] [ ] , int n ) { int transpose [ ] [ ] = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) transpose [ i ] [ j ] = mat [ j ] [ i ] ; int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) if ( transpose [ i ] [ j ] != mat [ i ] [ j ] ) flip ++ ; return flip / 2 ; } public static void main ( String [ ] args ) { int n = 3 ; int mat [ ] [ ] = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; System . out . println ( minimumflip ( mat , n ) ) ; } }

N = 3 NEW_LINE def minimumflip ( mat , n ) : NEW_LINE INDENT transpose = [ [ 0 ] * n ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT transpose [ i ] [ j ] = mat [ j ] [ i ] NEW_LINE DEDENT DEDENT flip = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if transpose [ i ] [ j ] != mat [ i ] [ j ] : NEW_LINE INDENT flip += 1 NEW_LINE DEDENT DEDENT DEDENT return int ( flip / 2 ) NEW_LINE DEDENT n = 3 NEW_LINE mat = [ [ 0 , 0 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] ] NEW_LINE print ( minimumflip ( mat , n ) ) NEW_LINE


import java . util . * ; class GFG { static int R = 4 , C = 4 , t = 0 ; static void reverseColumns ( int arr [ ] [ ] ) { for ( int i = 0 ; i < C ; i ++ ) { for ( int j = 0 , k = C - 1 ; j < k ; j ++ , k -- ) { t = arr [ j ] [ i ] ; arr [ j ] [ i ] = arr [ k ] [ i ] ; arr [ k ] [ i ] = t ; } } } static void transpose ( int arr [ ] [ ] ) { for ( int i = 0 ; i < R ; i ++ ) { for ( int j = i ; j < C ; j ++ ) { t = arr [ i ] [ j ] ; arr [ i ] [ j ] = arr [ j ] [ i ] ; arr [ j ] [ i ] = t ; } } } static void printMatrix ( int arr [ ] [ ] ) { for ( int i = 0 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static void rotate180 ( int arr [ ] [ ] ) { transpose ( arr ) ; reverseColumns ( arr ) ; transpose ( arr ) ; reverseColumns ( arr ) ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; rotate180 ( arr ) ; printMatrix ( arr ) ; } }

R = 4 NEW_LINE C = 4 NEW_LINE def reverseColumns ( arr ) : NEW_LINE INDENT for i in range ( C ) : NEW_LINE INDENT j = 0 NEW_LINE k = C - 1 NEW_LINE while j < k : NEW_LINE INDENT t = arr [ j ] [ i ] NEW_LINE arr [ j ] [ i ] = arr [ k ] [ i ] NEW_LINE arr [ k ] [ i ] = t NEW_LINE j += 1 NEW_LINE k -= 1 NEW_LINE DEDENT DEDENT DEDENT def transpose ( arr ) : NEW_LINE INDENT for i in range ( R ) : NEW_LINE INDENT for j in range ( i , C ) : NEW_LINE INDENT t = arr [ i ] [ j ] NEW_LINE arr [ i ] [ j ] = arr [ j ] [ i ] NEW_LINE arr [ j ] [ i ] = t NEW_LINE DEDENT DEDENT DEDENT def printMatrix ( arr ) : NEW_LINE INDENT for i in range ( R ) : NEW_LINE INDENT for j in range ( C ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT DEDENT def rotate180 ( arr ) : NEW_LINE INDENT transpose ( arr ) ; NEW_LINE reverseColumns ( arr ) ; NEW_LINE transpose ( arr ) ; NEW_LINE reverseColumns ( arr ) ; NEW_LINE DEDENT arr = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] ; NEW_LINE rotate180 ( arr ) ; NEW_LINE printMatrix ( arr ) ; NEW_LINE


class GFG { static boolean isPowerOfTwo ( int n ) { return n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static int findPosition ( int n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 ; int count = 0 ; while ( n > 0 ) { n = n >> 1 ; ++ count ; } return count ; } public static void main ( String [ ] args ) { int n = 0 ; int pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; } }

def isPowerOfTwo ( n ) : NEW_LINE INDENT return ( n and ( not ( n & ( n - 1 ) ) ) ) NEW_LINE DEDENT def findPosition ( n ) : NEW_LINE INDENT if not isPowerOfTwo ( n ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT n = n >> 1 NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 0 NEW_LINE pos = findPosition ( n ) NEW_LINE if pos == - 1 : NEW_LINE INDENT print ( " n ▁ = " , n , " Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = " , n , " Position " , pos ) NEW_LINE DEDENT n = 12 NEW_LINE pos = findPosition ( n ) NEW_LINE if pos == - 1 : NEW_LINE INDENT print ( " n ▁ = " , n , " Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = " , n , " Position " , pos ) NEW_LINE DEDENT n = 128 NEW_LINE pos = findPosition ( n ) NEW_LINE if pos == - 1 : NEW_LINE INDENT print ( " n ▁ = " , n , " Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = " , n , " Position " , pos ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i += 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } static boolean twinPrime ( int n1 , int n2 ) { return ( isPrime ( n1 ) && isPrime ( n2 ) && Math . abs ( n1 - n2 ) == 2 ) ; } static int countTwinPairs ( int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( twinPrime ( arr [ i ] , arr [ j ] ) ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 11 } ; int n = arr . length ; System . out . print ( countTwinPairs ( arr , n ) ) ; } }

from math import sqrt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 5 , int ( sqrt ( n ) ) + 1 , 6 ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def twinPrime ( n1 , n2 ) : NEW_LINE INDENT return ( isPrime ( n1 ) and isPrime ( n2 ) and abs ( n1 - n2 ) == 2 ) NEW_LINE DEDENT def countTwinPairs ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( twinPrime ( arr [ i ] , arr [ j ] ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countTwinPairs ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void checkAllOnes ( int arr [ ] , int n , int k ) { int brr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { int h = k + 1 ; int j = i ; while ( ( j >= 0 ) && ( h -- != 0 ) ) { brr [ j ] = 1 ; j -- ; } h = k + 1 ; j = i ; while ( ( j < n ) && ( h -- != 0 ) ) { brr [ j ] = 1 ; j ++ ; } } } int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( brr [ i ] == 0 ) { flag = 1 ; break ; } } if ( flag == 1 ) System . out . println ( "0" ) ; else System . out . println ( "1" ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 0 } ; int k = 2 ; int n = arr . length ; checkAllOnes ( arr , n , k ) ; } }

def checkAllOnes ( arr , n , k ) : NEW_LINE INDENT brr = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT h = k + 1 NEW_LINE j = i NEW_LINE while ( j >= 0 and ( h ) ) : NEW_LINE INDENT brr [ j ] = 1 NEW_LINE h -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT h = k + 1 NEW_LINE j = i NEW_LINE while ( j < n and ( h ) ) : NEW_LINE INDENT brr [ j ] = 1 NEW_LINE j += 1 NEW_LINE h -= 1 NEW_LINE DEDENT DEDENT DEDENT flag = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( brr [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( "1" ) NEW_LINE DEDENT DEDENT arr = [ 1 , 0 , 1 , 0 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE checkAllOnes ( arr , n , k ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int solve ( int N , int M , int cp [ ] , int sp [ ] ) { Integer [ ] profit = new Integer [ N ] ; for ( int i = 0 ; i < N ; i ++ ) profit [ i ] = sp [ i ] - cp [ i ] ; Arrays . sort ( profit , Collections . reverseOrder ( ) ) ; int sum = 0 ; for ( int i = 0 ; i < M ; i ++ ) { if ( profit [ i ] > 0 ) sum += profit [ i ] ; else break ; } return sum ; } public static void main ( String args [ ] ) { int N = 5 , M = 3 ; int CP [ ] = { 5 , 10 , 35 , 7 , 23 } ; int SP [ ] = { 11 , 10 , 0 , 9 , 19 } ; System . out . println ( solve ( N , M , CP , SP ) ) ; } }

def solve ( N , M , cp , sp ) : NEW_LINE INDENT profit = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT profit . append ( sp [ i ] - cp [ i ] ) NEW_LINE DEDENT profit . sort ( reverse = True ) NEW_LINE sum = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT if profit [ i ] > 0 : NEW_LINE INDENT sum += profit [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N , M = 5 , 3 NEW_LINE CP = [ 5 , 10 , 35 , 7 , 23 ] NEW_LINE SP = [ 11 , 10 , 0 , 9 , 19 ] NEW_LINE print ( solve ( N , M , CP , SP ) ) NEW_LINE DEDENT


class GFG { static int maxProfit ( int profitA [ ] , int profitB [ ] , int n ) { int preSum [ ] = new int [ n ] ; preSum [ 0 ] = profitA [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { preSum [ i ] = preSum [ i - 1 ] + profitA [ i ] ; } int suffSum [ ] = new int [ n ] ; suffSum [ n - 1 ] = profitB [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffSum [ i ] = suffSum [ i + 1 ] + profitB [ i ] ; } int res = preSum [ n - 1 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { res = Math . max ( res , preSum [ i ] + suffSum [ i + 1 ] ) ; } res = Math . max ( res , suffSum [ 0 ] ) ; return res ; } public static void main ( String [ ] args ) { int profitA [ ] = { 2 , 3 , 2 } ; int profitB [ ] = { 10 , 30 , 40 } ; int n = profitA . length ; System . out . println ( maxProfit ( profitA , profitB , n ) ) ; } }

def maxProfit ( profitA , profitB , n ) : NEW_LINE INDENT preSum = [ 0 ] * n ; NEW_LINE preSum [ 0 ] = profitA [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT preSum [ i ] = preSum [ i - 1 ] + profitA [ i ] ; NEW_LINE DEDENT suffSum = [ 0 ] * n ; NEW_LINE suffSum [ n - 1 ] = profitB [ n - 1 ] ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffSum [ i ] = suffSum [ i + 1 ] + profitB [ i ] ; NEW_LINE DEDENT res = preSum [ n - 1 ] ; NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT res = max ( res , preSum [ i ] + suffSum [ i + 1 ] ) ; NEW_LINE DEDENT res = max ( res , suffSum [ 0 ] ) ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT profitA = [ 2 , 3 , 2 ] ; NEW_LINE profitB = [ 10 , 30 , 40 ] ; NEW_LINE n = len ( profitA ) ; NEW_LINE print ( maxProfit ( profitA , profitB , n ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static void find ( int n ) { String [ ] arr = new String [ n + 1 ] ; arr [ 0 ] = " " ; int size = 1 , m = 1 ; while ( size <= n ) { for ( int i = 0 ; i < m && ( size + i ) <= n ; i ++ ) arr [ size + i ] = "3" + arr [ size - m + i ] ; for ( int i = 0 ; i < m && ( size + m + i ) <= n ; i ++ ) arr [ size + m + i ] = "4" + arr [ size - m + i ] ; m = m << 1 ; size = size + m ; } System . out . println ( arr [ n ] ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < 16 ; i ++ ) find ( i ) ; } }

def find ( n ) : NEW_LINE INDENT arr = [ ' ' ] * ( n + 1 ) ; NEW_LINE size = 1 ; NEW_LINE m = 1 ; NEW_LINE while ( size <= n ) : NEW_LINE INDENT i = 0 ; NEW_LINE while ( i < m and ( size + i ) <= n ) : NEW_LINE INDENT arr [ size + i ] = "3" + arr [ size - m + i ] ; NEW_LINE i += 1 ; NEW_LINE DEDENT i = 0 ; NEW_LINE while ( i < m and ( size + m + i ) <= n ) : NEW_LINE INDENT arr [ size + m + i ] = "4" + arr [ size - m + i ] ; NEW_LINE i += 1 ; NEW_LINE DEDENT m = m << 1 ; NEW_LINE size = size + m ; NEW_LINE DEDENT print ( arr [ n ] ) ; NEW_LINE DEDENT for i in range ( 1 , 16 ) : NEW_LINE INDENT find ( i ) ; NEW_LINE DEDENT


class GFG { static int sum = 0 ; static class Node { int data ; Node next ; } static Node 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 ; return head_ref ; } static void sumOfNodes ( Node head ) { if ( head == null ) return ; sumOfNodes ( head . next ) ; sum = sum + head . data ; } static int sumOfNodesUtil ( Node head ) { sum = 0 ; sumOfNodes ( head ) ; return sum ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 7 ) ; head = push ( head , 6 ) ; head = push ( head , 8 ) ; head = push ( head , 4 ) ; head = push ( head , 1 ) ; System . out . println ( " Sum ▁ of ▁ nodes ▁ = ▁ " + sumOfNodesUtil ( head ) ) ; } }

import math NEW_LINE class Sum : NEW_LINE INDENT tsum = None NEW_LINE DEDENT class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head , data ) : NEW_LINE INDENT if not head : NEW_LINE INDENT return Node ( data ) NEW_LINE DEDENT new_node = Node ( data ) NEW_LINE new_node . next = head NEW_LINE head = new_node NEW_LINE return head NEW_LINE DEDENT def sumOfNode ( head , S ) : NEW_LINE INDENT if not head : NEW_LINE INDENT return NEW_LINE DEDENT sumOfNode ( head . next , S ) NEW_LINE S . tsum += head . data NEW_LINE DEDENT def sumOfNodesUtil ( head ) : NEW_LINE INDENT S = Sum ( ) NEW_LINE S . tsum = 0 NEW_LINE sumOfNode ( head , S ) NEW_LINE return S . tsum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 7 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 1 ) NEW_LINE print ( " Sum ▁ of ▁ Nodes ▁ = ▁ { } " . format ( sumOfNodesUtil ( head ) ) ) NEW_LINE DEDENT


class GFG { static int MAX = 26 ; static boolean canBeMadeEqual ( String str1 , String str2 ) { int len1 = str1 . length ( ) ; int len2 = str2 . length ( ) ; if ( len1 == len2 ) { int freq [ ] = new int [ MAX ] ; for ( int i = 0 ; i < len1 ; i ++ ) { freq [ str1 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < len2 ; i ++ ) { if ( freq [ str2 . charAt ( i ) - ' a ' ] > 0 ) return true ; } } return false ; } public static void main ( String [ ] args ) { String str1 = " abc " , str2 = " defa " ; if ( canBeMadeEqual ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

MAX = 26 NEW_LINE def canBeMadeEqual ( str1 , str2 ) : NEW_LINE INDENT len1 = len ( str1 ) NEW_LINE len2 = len ( str2 ) NEW_LINE if ( len1 == len2 ) : NEW_LINE INDENT freq = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT freq [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( len2 ) : NEW_LINE INDENT if ( freq [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] > 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT str1 = " abc " NEW_LINE str2 = " defa " NEW_LINE if ( canBeMadeEqual ( str1 , str2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; class solution { static long minimumMoves ( int a [ ] , int n ) { long operations = 0 ; Arrays . sort ( a ) ; for ( int i = 0 ; i < n ; i ++ ) operations += ( long ) Math . abs ( a [ i ] - ( i + 1 ) ) ; return operations ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 3 , 2 } ; int n = arr . length ; System . out . print ( minimumMoves ( arr , n ) ) ; } }

def minimumMoves ( a , n ) : NEW_LINE INDENT operations = 0 NEW_LINE a . sort ( reverse = False ) NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT operations = operations + abs ( a [ i ] - ( i + 1 ) ) NEW_LINE DEDENT return operations NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minimumMoves ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static Vector < Integer > c = new Vector < > ( ) ; static int ans = 0 ; static void subsetSum ( int i , int curr ) { if ( i == c . size ( ) ) { ans += curr ; return ; } subsetSum ( i + 1 , curr + c . elementAt ( i ) ) ; subsetSum ( i + 1 , curr ) ; } static void subsetGen ( int [ ] arr , int i , int n ) { if ( i == n ) { subsetSum ( 0 , 0 ) ; return ; } subsetGen ( arr , i + 1 , n ) ; c . add ( arr [ i ] ) ; subsetGen ( arr , i + 1 , n ) ; c . remove ( c . size ( ) - 1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 1 } ; int n = arr . length ; subsetGen ( arr , 0 , n ) ; System . out . println ( ans ) ; } }

c = [ ] NEW_LINE ans = 0 NEW_LINE def subsetSum ( i , curr ) : NEW_LINE INDENT global ans , c NEW_LINE if ( i == len ( c ) ) : NEW_LINE INDENT ans += curr NEW_LINE return NEW_LINE DEDENT subsetSum ( i + 1 , curr + c [ i ] ) NEW_LINE subsetSum ( i + 1 , curr ) NEW_LINE DEDENT def subsetGen ( arr , i , n ) : NEW_LINE INDENT global ans , c NEW_LINE if ( i == n ) : NEW_LINE INDENT subsetSum ( 0 , 0 ) NEW_LINE return NEW_LINE DEDENT subsetGen ( arr , i + 1 , n ) NEW_LINE c . append ( arr [ i ] ) NEW_LINE subsetGen ( arr , i + 1 , n ) NEW_LINE del c [ - 1 ] NEW_LINE DEDENT arr = [ 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE subsetGen ( arr , 0 , n ) NEW_LINE print ( ans ) NEW_LINE


class GFG { public static int getFirstSetBitPos ( int n ) { return ( int ) ( ( Math . log10 ( n & - n ) ) / Math . log10 ( 2 ) ) + 1 ; } public static void main ( String [ ] args ) { int n = 12 ; System . out . println ( getFirstSetBitPos ( n ) ) ; } }

import math NEW_LINE def getFirstSetBitPos ( n ) : NEW_LINE INDENT return math . log2 ( n & - n ) + 1 NEW_LINE DEDENT n = 12 NEW_LINE print ( int ( getFirstSetBitPos ( n ) ) ) NEW_LINE


class GFG { static int AVLtree ( int H , int a , int b ) { if ( H == 0 ) return 1 ; if ( H == 1 ) return b ; return AVLtree ( H - 1 , b , a + b + 1 ) ; } public static void main ( String [ ] args ) { int H = 5 ; int answer = AVLtree ( H , 1 , 2 ) ; System . out . println ( " n ( " + H + " ) ▁ = ▁ " + answer ) ; } }

def AVLtree ( H , a , b ) : NEW_LINE INDENT if ( H == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( H == 1 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return AVLtree ( H - 1 , b , a + b + 1 ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT H = 5 ; NEW_LINE answer = AVLtree ( H , 1 , 2 ) ; NEW_LINE print ( " n ( " , H , " ) ▁ = ▁ " \ , answer ) ; NEW_LINE DEDENT


class GFG { static void findLargest ( int m , int s ) { if ( s == 0 ) { System . out . print ( m == 1 ? " Largest ▁ number ▁ is ▁ 0" : " Not ▁ possible " ) ; return ; } if ( s > 9 * m ) { System . out . println ( " Not ▁ possible " ) ; return ; } int [ ] res = new int [ m ] ; for ( int i = 0 ; i < m ; i ++ ) { if ( s >= 9 ) { res [ i ] = 9 ; s -= 9 ; } else { res [ i ] = s ; s = 0 ; } } System . out . print ( " Largest ▁ number ▁ is ▁ " ) ; for ( int i = 0 ; i < m ; i ++ ) System . out . print ( res [ i ] ) ; } public static void main ( String [ ] args ) { int s = 9 , m = 2 ; findLargest ( m , s ) ; } }

def findLargest ( m , s ) : NEW_LINE INDENT if ( s == 0 ) : NEW_LINE INDENT if ( m == 1 ) : NEW_LINE INDENT print ( " Largest ▁ number ▁ is ▁ " , "0" , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " , end = " " ) NEW_LINE DEDENT return NEW_LINE DEDENT if ( s > 9 * m ) : NEW_LINE INDENT print ( " Not ▁ possible " , end = " " ) NEW_LINE return NEW_LINE DEDENT res = [ 0 ] * m NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT if ( s >= 9 ) : NEW_LINE INDENT res [ i ] = 9 NEW_LINE s = s - 9 NEW_LINE DEDENT else : NEW_LINE INDENT res [ i ] = s NEW_LINE s = 0 NEW_LINE DEDENT DEDENT print ( " Largest ▁ number ▁ is ▁ " , end = " " ) NEW_LINE for i in range ( 0 , m ) : NEW_LINE INDENT print ( res [ i ] , end = " " ) NEW_LINE DEDENT DEDENT s = 9 NEW_LINE m = 2 NEW_LINE findLargest ( m , s ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { public static double distance ( double lat1 , double lat2 , double lon1 , double lon2 ) { lon1 = Math . toRadians ( lon1 ) ; lon2 = Math . toRadians ( lon2 ) ; lat1 = Math . toRadians ( lat1 ) ; lat2 = Math . toRadians ( lat2 ) ; double dlon = lon2 - lon1 ; double dlat = lat2 - lat1 ; double a = Math . pow ( Math . sin ( dlat / 2 ) , 2 ) + Math . cos ( lat1 ) * Math . cos ( lat2 ) * Math . pow ( Math . sin ( dlon / 2 ) , 2 ) ; double c = 2 * Math . asin ( Math . sqrt ( a ) ) ; double r = 6371 ; return ( c * r ) ; } public static void main ( String [ ] args ) { double lat1 = 53.32055555555556 ; double lat2 = 53.31861111111111 ; double lon1 = - 1.7297222222222221 ; double lon2 = - 1.6997222222222223 ; System . out . println ( distance ( lat1 , lat2 , lon1 , lon2 ) + " ▁ K . M " ) ; } }

from math import radians , cos , sin , asin , sqrt NEW_LINE def distance ( lat1 , lat2 , lon1 , lon2 ) : NEW_LINE INDENT lon1 = radians ( lon1 ) NEW_LINE lon2 = radians ( lon2 ) NEW_LINE lat1 = radians ( lat1 ) NEW_LINE lat2 = radians ( lat2 ) NEW_LINE dlon = lon2 - lon1 NEW_LINE dlat = lat2 - lat1 NEW_LINE a = sin ( dlat / 2 ) ** 2 + cos ( lat1 ) * cos ( lat2 ) * sin ( dlon / 2 ) ** 2 NEW_LINE c = 2 * asin ( sqrt ( a ) ) NEW_LINE r = 6371 NEW_LINE return ( c * r ) NEW_LINE DEDENT lat1 = 53.32055555555556 NEW_LINE lat2 = 53.31861111111111 NEW_LINE lon1 = - 1.7297222222222221 NEW_LINE lon2 = - 1.6997222222222223 NEW_LINE print ( distance ( lat1 , lat2 , lon1 , lon2 ) , " K . M " ) NEW_LINE


import java . io . * ; class GFG { static int findDelta ( int a , int b , int c , int d ) { return ( b * c - a * d ) / ( d - c ) ; } public static void main ( String args [ ] ) { int a = 3 , b = 9 , c = 3 , d = 5 ; System . out . print ( " \u0394X ▁ = ▁ " + findDelta ( a , b , c , d ) ) ; } }

def findDelta ( a , b , c , d ) : NEW_LINE INDENT return int ( ( b * c - a * d ) / ( d - c ) ) ; NEW_LINE DEDENT a = 3 ; b = 9 ; NEW_LINE c = 3 ; d = 5 ; NEW_LINE print ( " X ▁ = ▁ { } " . format ( findDelta ( a , b , c , d ) ) ) ; NEW_LINE


import java . util . * ; class solution { static long getSum ( int n , int d ) { long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 10 == d ) sum += i ; } return sum ; } public static void main ( String args [ ] ) { int n = 30 , d = 3 ; System . out . println ( getSum ( n , d ) ) ; } }

def getSum ( n , d ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT if ( i % 10 == d ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , d = 30 , 3 NEW_LINE print ( getSum ( n , d ) ) NEW_LINE DEDENT


class GFG { public static int logicOfSequence ( int N ) { if ( N % 2 == 0 ) N = N * N ; else N = N * N * N ; return N ; } public static void main ( String args [ ] ) { int N = 6 ; System . out . println ( logicOfSequence ( N ) ) ; } }

def logicOfSequence ( N ) : NEW_LINE INDENT if ( N % 2 == 0 ) : NEW_LINE INDENT N = N * N NEW_LINE DEDENT else : NEW_LINE INDENT N = N * N * N NEW_LINE DEDENT return N NEW_LINE DEDENT N = 6 NEW_LINE print ( logicOfSequence ( N ) ) NEW_LINE


import java . io . * ; class GFG { static long sumXOR ( int arr [ ] , int n ) { long sum = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int zc = 0 , oc = 0 ; long idsum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] % 2 == 0 ) zc ++ ; else oc ++ ; arr [ j ] /= 2 ; } idsum = oc * zc * ( 1 << i ) ; sum += idsum ; } return sum ; } public static void main ( String args [ ] ) { long sum = 0 ; int arr [ ] = { 5 , 9 , 7 , 6 } ; int n = arr . length ; sum = sumXOR ( arr , n ) ; System . out . println ( sum ) ; } }

def sumXOR ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT zc = 0 NEW_LINE oc = 0 NEW_LINE idsum = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ j ] % 2 == 0 ) : NEW_LINE INDENT zc = zc + 1 NEW_LINE DEDENT else : NEW_LINE INDENT oc = oc + 1 NEW_LINE DEDENT arr [ j ] = int ( arr [ j ] / 2 ) NEW_LINE DEDENT idsum = oc * zc * ( 1 << i ) NEW_LINE sum = sum + idsum ; NEW_LINE DEDENT return sum NEW_LINE DEDENT sum = 0 NEW_LINE arr = [ 5 , 9 , 7 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE sum = sumXOR ( arr , n ) ; NEW_LINE print ( sum ) NEW_LINE


import java . io . * ; class GFG { static long count_of_ways ( long n ) { long count = 0 ; for ( int i = 0 ; i <= n ; i ++ ) for ( int j = 0 ; j <= n ; j ++ ) for ( int k = 0 ; k <= n ; k ++ ) if ( i + j + k == n ) count ++ ; return count ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . println ( count_of_ways ( n ) ) ; } }

def count_of_ways ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT for j in range ( 0 , n + 1 ) : NEW_LINE INDENT for k in range ( 0 , n + 1 ) : NEW_LINE INDENT if ( i + j + k == n ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE print ( count_of_ways ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static boolean coPrime ( int n1 , int n2 ) { if ( gcd ( n1 , n2 ) == 1 ) return true ; else return false ; } static int largestCoprime ( int N ) { int half = ( int ) ( N / 2 ) ; while ( coPrime ( N , half ) == false ) half -- ; return half ; } public static void main ( String args [ ] ) { int n = 50 ; System . out . println ( largestCoprime ( n ) ) ; } }

import math as mt NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( b , a % b ) NEW_LINE DEDENT DEDENT def coPrime ( n1 , n2 ) : NEW_LINE INDENT if ( gcd ( n1 , n2 ) == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def largestCoprime ( N ) : NEW_LINE INDENT half = mt . floor ( N / 2 ) NEW_LINE while ( coPrime ( N , half ) == False ) : NEW_LINE INDENT half -= 1 NEW_LINE DEDENT return half NEW_LINE DEDENT n = 50 NEW_LINE print ( largestCoprime ( n ) ) NEW_LINE


class GFG { static void checkEqualMatrix ( int arr [ ] [ ] , int n , int m , int k ) { int c = 0 , cnt1 = 0 , cnt2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( c % 2 == 0 ) { if ( arr [ i ] [ j ] == k ) { cnt1 ++ ; } } else { if ( arr [ i ] [ j ] == k ) { cnt2 ++ ; } } c = c + 1 ; } } if ( cnt1 >= 1 && cnt2 >= 1 ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 1 , 8 , 3 } , { 1 , 2 , 2 } , { 4 , 1 , 9 } } ; int k = 2 ; checkEqualMatrix ( arr , 3 , 3 , k ) ; } }

def checkEqualMatrix ( arr , n , m , k ) : NEW_LINE INDENT c = 0 ; cnt1 = 0 ; cnt2 = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( c % 2 == 0 ) : NEW_LINE INDENT if ( arr [ i ] [ j ] == k ) : NEW_LINE INDENT cnt1 += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( arr [ i ] [ j ] == k ) : NEW_LINE INDENT cnt2 += 1 ; NEW_LINE DEDENT DEDENT c = c + 1 ; NEW_LINE DEDENT DEDENT if ( cnt1 >= 1 and cnt2 >= 1 ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ [ 1 , 8 , 3 ] , [ 1 , 2 , 2 ] , [ 4 , 1 , 9 ] ] ; NEW_LINE k = 2 ; NEW_LINE checkEqualMatrix ( arr , 3 , 3 , k ) ; NEW_LINE DEDENT


class GFG { static long max_gcd ( long n , long p ) { int count = 0 ; long gcd = 1 ; while ( p % 2 == 0 ) { p >>= 1 ; count ++ ; } if ( count > 0 ) gcd *= ( long ) Math . pow ( 2 , count / n ) ; for ( long i = 3 ; i <= Math . sqrt ( p ) ; i += 2 ) { count = 0 ; while ( p % i == 0 ) { count ++ ; p = p / i ; } if ( count > 0 ) { gcd *= ( long ) Math . pow ( i , count / n ) ; } } if ( p > 2 ) gcd *= ( long ) Math . pow ( p , 1 / n ) ; return gcd ; } public static void main ( String [ ] args ) { long n = 3 ; long p = 80 ; System . out . println ( max_gcd ( n , p ) ) ; } }

import math NEW_LINE def max_gcd ( n , p ) : NEW_LINE INDENT count = 0 ; NEW_LINE gcd = 1 ; NEW_LINE while ( p % 2 == 0 ) : NEW_LINE INDENT p >>= 1 ; NEW_LINE count = count + 1 ; NEW_LINE DEDENT if ( count > 0 ) : NEW_LINE INDENT gcd = gcd * pow ( 2 , count // n ) ; NEW_LINE DEDENT for i in range ( 3 , ( int ) ( math . sqrt ( p ) ) , 2 ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( p % i == 0 ) : NEW_LINE INDENT count = count + 1 ; NEW_LINE p = p // i ; NEW_LINE DEDENT if ( count > 0 ) : NEW_LINE INDENT gcd = gcd * pow ( i , count // n ) ; NEW_LINE DEDENT DEDENT if ( p > 2 ) : NEW_LINE INDENT gcd = gcd * pow ( p , 1 // n ) ; NEW_LINE DEDENT return gcd ; NEW_LINE DEDENT n = 3 ; NEW_LINE p = 80 ; NEW_LINE print ( max_gcd ( n , p ) ) ; NEW_LINE


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

def countSetBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT n &= ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT i = 9 NEW_LINE print ( countSetBits ( i ) ) NEW_LINE


import java . util . * ; class GFG { static int findX ( int a [ ] , int n ) { Arrays . sort ( a ) ; int x = a [ 0 ] * a [ n - 1 ] ; Vector < Integer > vec = new Vector < Integer > ( ) ; for ( int i = 2 ; i * i <= x ; i ++ ) { if ( n % i == 0 ) { vec . add ( i ) ; if ( ( n / i ) != i ) vec . add ( n / i ) ; } } if ( vec . size ( ) == n ) { int i = 0 ; for ( int it : vec ) { if ( a [ i ++ ] != it ) return - 1 ; } } return x ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 5 , 4 , 10 } ; int n = a . length ; System . out . print ( findX ( a , n ) ) ; } }

import math NEW_LINE def findX ( list , int ) : NEW_LINE INDENT list . sort ( ) NEW_LINE x = list [ 0 ] * list [ int - 1 ] ; NEW_LINE vec = [ ] NEW_LINE i = 2 NEW_LINE while ( i * i <= x ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT vec . append ( i ) NEW_LINE if ( ( int // i ) != i ) : NEW_LINE INDENT vec . append ( int // i ) NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT if ( len ( vec ) == int ) : NEW_LINE INDENT j = 0 NEW_LINE for it in range ( int ) : NEW_LINE INDENT if ( a [ j ] != it ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT return x NEW_LINE DEDENT a = [ 2 , 5 , 4 , 10 ] NEW_LINE n = len ( a ) NEW_LINE print ( findX ( a , n ) ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int lds ( Integer arr [ ] , int n ) { Integer lds [ ] = new Integer [ n ] ; lds [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { lds [ i ] = 1 ; for ( int j = 0 ; j < i ; j ++ ) if ( lds [ j ] != 0 && arr [ i ] % arr [ j ] == 0 ) lds [ i ] = Math . max ( lds [ i ] , lds [ j ] + 1 ) ; } int max = ( int ) Collections . max ( Arrays . asList ( lds ) ) ; return max ; } public static void main ( String args [ ] ) { Integer arr [ ] = { 2 , 11 , 16 , 12 , 36 , 60 , 71 , 17 , 29 , 144 , 288 , 129 , 432 , 993 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ lds ▁ is ▁ " + lds ( arr , n ) ) ; } }

def lds ( arr , n ) : NEW_LINE INDENT lds = [ 0 for i in range ( n ) ] NEW_LINE lds [ 0 ] = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT lds [ i ] = 1 NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( lds [ j ] != 0 and arr [ i ] % arr [ j ] == 0 ) : NEW_LINE INDENT lds [ i ] = max ( lds [ i ] , lds [ j ] + 1 ) NEW_LINE DEDENT DEDENT DEDENT return max ( lds ) NEW_LINE DEDENT arr = [ 2 , 11 , 16 , 12 , 36 , 60 , 71 , 17 , 29 , 144 , 288 , 129 , 432 , 993 ] NEW_LINE print ( " Length ▁ of ▁ lds ▁ is " , lds ( arr , len ( arr ) ) ) NEW_LINE


public class GFG { static void perfectCube ( int N ) { int cube_root ; cube_root = ( int ) Math . round ( Math . cbrt ( N ) ) ; if ( cube_root * cube_root * cube_root == N ) { System . out . println ( " Yes " ) ; return ; } else { System . out . println ( " NO " ) ; return ; } } public static void main ( String [ ] args ) { int N = 125 ; perfectCube ( N ) ; } }

def perfectCube ( N ) : NEW_LINE INDENT cube_root = round ( N ** ( 1 / 3 ) ) ; NEW_LINE if cube_root * cube_root * cube_root == N : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE return ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 125 ; NEW_LINE perfectCube ( N ) ; NEW_LINE DEDENT


class GFG { static boolean pointIsOnLine ( int m , int c , int x , int y ) { if ( y == ( ( m * x ) + c ) ) return true ; return false ; } public static void main ( String [ ] args ) { int m = 3 , c = 2 ; int x = 1 , y = 5 ; if ( pointIsOnLine ( m , c , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def pointIsOnLine ( m , c , x , y ) : NEW_LINE INDENT if ( y == ( ( m * x ) + c ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT m = 3 ; c = 2 ; NEW_LINE x = 1 ; y = 5 ; NEW_LINE if ( pointIsOnLine ( m , c , x , y ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT


class GFG { static final int MAX = 10000 ; static boolean prime [ ] = new boolean [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { int i ; for ( i = 0 ; i < MAX + 1 ; i ++ ) { prime [ i ] = true ; } prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static int xorFirstNPrime ( int n ) { int count = 0 , num = 1 ; int xorVal = 0 ; while ( count < n ) { if ( prime [ num ] ) { xorVal ^= num ; count ++ ; } num ++ ; } return xorVal ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 4 ; System . out . println ( xorFirstNPrime ( n ) ) ; } }

MAX = 10000 NEW_LINE prime = [ True for i in range ( MAX + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE for p in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( 2 * p , MAX + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def xorFirstNPrime ( n ) : NEW_LINE INDENT count = 0 NEW_LINE num = 1 NEW_LINE xorVal = 0 NEW_LINE while ( count < n ) : NEW_LINE INDENT if ( prime [ num ] ) : NEW_LINE INDENT xorVal ^= num NEW_LINE count += 1 NEW_LINE DEDENT num += 1 NEW_LINE DEDENT return xorVal NEW_LINE DEDENT SieveOfEratosthenes ( ) NEW_LINE n = 4 NEW_LINE print ( xorFirstNPrime ( n ) ) NEW_LINE


class GFG { static int extraElement ( int A [ ] , int B [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans ^= A [ i ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) ans ^= B [ i ] ; return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 10 , 15 , 5 } ; int B [ ] = { 10 , 100 , 15 , 5 } ; int n = A . length ; System . out . println ( extraElement ( A , B , n ) ) ; } }

def extraElement ( A , B , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans ^= A [ i ] ; NEW_LINE DEDENT for i in range ( n + 1 ) : NEW_LINE INDENT ans ^= B [ i ] ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT A = [ 10 , 15 , 5 ] ; NEW_LINE B = [ 10 , 100 , 15 , 5 ] ; NEW_LINE n = len ( A ) ; NEW_LINE print ( extraElement ( A , B , n ) ) ; NEW_LINE


import java . io . * ; class GFG { static void survival ( int S , int N , int M ) { if ( ( ( N * 6 ) < ( M * 7 ) && S > 6 ) || M > N ) System . out . println ( " No " ) ; else { int days = ( M * S ) / N ; if ( ( ( M * S ) % N ) != 0 ) days ++ ; System . out . println ( " Yes ▁ " + days ) ; } } public static void main ( String [ ] args ) { int S = 10 , N = 16 , M = 2 ; survival ( S , N , M ) ; } }

def survival ( S , N , M ) : NEW_LINE INDENT if ( ( ( N * 6 ) < ( M * 7 ) and S > 6 ) or M > N ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT days = ( M * S ) / N NEW_LINE if ( ( ( M * S ) % N ) != 0 ) : NEW_LINE INDENT days += 1 NEW_LINE DEDENT print ( " Yes ▁ " ) , NEW_LINE print ( days ) NEW_LINE DEDENT DEDENT S = 10 ; N = 16 ; M = 2 NEW_LINE survival ( S , N , M ) NEW_LINE


class GFG { static final int MAXN = 1000005 ; static int even [ ] = new int [ MAXN ] ; static int odd [ ] = new int [ MAXN ] ; static void precompute ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % 2 == 1 ) odd [ i ] = 1 ; if ( arr [ i ] % 2 == 0 ) even [ i ] = 1 ; } for ( int i = 1 ; i < n ; i ++ ) { even [ i ] = even [ i ] + even [ i - 1 ] ; odd [ i ] = odd [ i ] + odd [ i - 1 ] ; } } static boolean isOdd ( int L , int R ) { int cnt = odd [ R ] ; if ( L > 0 ) cnt -= odd [ L - 1 ] ; if ( cnt == R - L + 1 ) return true ; return false ; } static void performQueries ( int a [ ] , int n , int q [ ] [ ] , int m ) { precompute ( a , n ) ; for ( int i = 0 ; i < m ; i ++ ) { int L = q [ i ] [ 0 ] , R = q [ i ] [ 1 ] ; if ( isOdd ( L , R ) ) System . out . println ( " Odd " ) ; else System . out . println ( " Even " ) ; } } public static void main ( String args [ ] ) { int [ ] a = { 2 , 1 , 5 , 7 , 6 , 8 , 9 } ; int n = a . length ; int q [ ] [ ] = { { 0 , 2 } , { 1 , 2 } , { 2 , 3 } , { 3 , 6 } } ; int m = q . length ; performQueries ( a , n , q , m ) ; } }

MAXN = 1000005 ; NEW_LINE even = [ 0 ] * MAXN ; NEW_LINE odd = [ 0 ] * MAXN ; NEW_LINE def precompute ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 == 1 ) : NEW_LINE INDENT odd [ i ] = 1 ; NEW_LINE DEDENT if ( arr [ i ] % 2 == 0 ) : NEW_LINE INDENT even [ i ] = 1 ; NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT even [ i ] = even [ i ] + even [ i - 1 ] ; NEW_LINE odd [ i ] = odd [ i ] + odd [ i - 1 ] ; NEW_LINE DEDENT DEDENT def isOdd ( L , R ) : NEW_LINE INDENT cnt = odd [ R ] ; NEW_LINE if ( L > 0 ) : NEW_LINE INDENT cnt -= odd [ L - 1 ] ; NEW_LINE DEDENT if ( cnt == R - L + 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def performQueries ( a , n , q , m ) : NEW_LINE INDENT precompute ( a , n ) ; NEW_LINE for i in range ( m ) : NEW_LINE INDENT L = q [ i ] [ 0 ] ; NEW_LINE R = q [ i ] [ 1 ] ; NEW_LINE if ( isOdd ( L , R ) ) : NEW_LINE INDENT print ( " Odd " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Even " ) ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 2 , 1 , 5 , 7 , 6 , 8 , 9 ] ; NEW_LINE n = len ( a ) ; NEW_LINE q = [ [ 0 , 2 ] , [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 6 ] ] ; NEW_LINE m = len ( q ) ; NEW_LINE performQueries ( a , n , q , m ) ; NEW_LINE DEDENT


class GFG { static boolean checkpossible ( int mask , int arr [ ] , int prefix [ ] , int n , int k ) { int i , j ; boolean dp [ ] [ ] = new boolean [ n + 1 ] [ k + 1 ] ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < k + 1 ; j ++ ) { dp [ i ] [ j ] = false ; } } dp [ 0 ] [ 0 ] = true ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 ; j <= k ; j ++ ) { for ( int l = i - 1 ; l >= 0 ; -- l ) { if ( dp [ l ] [ j - 1 ] && ( ( ( prefix [ i ] - prefix [ l ] ) & mask ) == mask ) ) { dp [ i ] [ j ] = true ; break ; } } } } return dp [ n ] [ k ] ; } static int Partition ( int arr [ ] , int n , int k ) { int prefix [ ] = new int [ n + 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { prefix [ i ] = prefix [ i - 1 ] + arr [ i ] ; } int LOGS = 20 ; int ans = 0 ; for ( int i = LOGS ; i >= 0 ; -- i ) { if ( checkpossible ( ans | ( 1 << i ) , arr , prefix , n , k ) ) { ans = ans | ( 1 << i ) ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 2 , 7 , 10 , 23 , 21 , 6 , 8 , 7 , 3 } , k = 2 ; int n = arr . length - 1 ; System . out . println ( Partition ( arr , n , k ) ) ; } }

def checkpossible ( mask , arr , prefix , n , k ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( k + 1 ) ] for i in range ( n + 1 ) ] NEW_LINE dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , k + 1 ) : NEW_LINE INDENT for l in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( dp [ l ] [ j - 1 ] and ( ( ( prefix [ i ] - prefix [ l ] ) & mask ) == mask ) ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT return dp [ n ] [ k ] NEW_LINE DEDENT def Partition ( arr , n , k ) : NEW_LINE INDENT prefix = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] + arr [ i ] NEW_LINE DEDENT LOGS = 20 NEW_LINE ans = 0 NEW_LINE for i in range ( LOGS , - 1 , - 1 ) : NEW_LINE INDENT if ( checkpossible ( ans | ( 1 << i ) , arr , prefix , n , k ) ) : NEW_LINE INDENT ans = ans | ( 1 << i ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 0 , 1 , 2 , 7 , 10 , 23 , 21 , 6 , 8 , 7 , 3 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) - 1 NEW_LINE print ( Partition ( arr , n , k ) ) NEW_LINE


import java . io . * ; class GFG { static int findGreatest ( int [ ] arr , int n ) { int result = - 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n - 1 ; j ++ ) for ( int k = j + 1 ; k < n ; k ++ ) if ( arr [ j ] * arr [ k ] == arr [ i ] ) result = Math . max ( result , arr [ i ] ) ; return result ; } static public void main ( String [ ] args ) { int [ ] arr = { 30 , 10 , 9 , 3 , 35 } ; int n = arr . length ; System . out . println ( findGreatest ( arr , n ) ) ; } }

def findGreatest ( arr , n ) : NEW_LINE INDENT result = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] * arr [ k ] == arr [ i ] ) : NEW_LINE INDENT result = max ( result , arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 30 , 10 , 9 , 3 , 35 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findGreatest ( arr , n ) ) NEW_LINE DEDENT


class GFG { static int MOD = 1000000007 ; static int totalSubSets ( int n , int l , int r ) { int zero = ( int ) Math . floor ( ( double ) r / 3 ) - ( int ) Math . ceil ( ( double ) l / 3 ) + 1 ; int one = ( int ) Math . floor ( ( double ) ( r - 1 ) / 3 ) - ( int ) Math . ceil ( ( double ) ( l - 1 ) / 3 ) + 1 ; int two = ( int ) Math . floor ( ( double ) ( r - 2 ) / 3 ) - ( int ) Math . ceil ( ( double ) ( l - 2 ) / 3 ) + 1 ; int [ ] [ ] dp = new int [ n ] [ 3 ] ; dp [ 0 ] [ 0 ] = zero ; dp [ 0 ] [ 1 ] = one ; dp [ 0 ] [ 2 ] = two ; for ( int i = 1 ; i < n ; ++ i ) { dp [ i ] [ 0 ] = ( ( dp [ i - 1 ] [ 0 ] * zero ) + ( dp [ i - 1 ] [ 1 ] * two ) + ( dp [ i - 1 ] [ 2 ] * one ) ) % MOD ; dp [ i ] [ 1 ] = ( ( dp [ i - 1 ] [ 0 ] * one ) + ( dp [ i - 1 ] [ 1 ] * zero ) + ( dp [ i - 1 ] [ 2 ] * two ) ) % MOD ; dp [ i ] [ 2 ] = ( ( dp [ i - 1 ] [ 0 ] * two ) + ( dp [ i - 1 ] [ 1 ] * one ) + ( dp [ i - 1 ] [ 2 ] * zero ) ) % MOD ; } return dp [ n - 1 ] [ 0 ] ; } public static void main ( String [ ] args ) { int n = 5 ; int l = 10 ; int r = 100 ; System . out . println ( totalSubSets ( n , l , r ) ) ; } }

import math NEW_LINE def totalSubSets ( n , l , r ) : NEW_LINE INDENT MOD = 1000000007 ; NEW_LINE zero = ( math . floor ( r / 3 ) - math . ceil ( l / 3 ) + 1 ) ; NEW_LINE one = ( math . floor ( ( r - 1 ) / 3 ) - math . ceil ( ( l - 1 ) / 3 ) + 1 ) ; NEW_LINE two = ( math . floor ( ( r - 2 ) / 3 ) - math . ceil ( ( l - 2 ) / 3 ) + 1 ) ; NEW_LINE dp = [ [ 0 for x in range ( 3 ) ] for y in range ( n ) ] NEW_LINE dp [ 0 ] [ 0 ] = zero ; NEW_LINE dp [ 0 ] [ 1 ] = one ; NEW_LINE dp [ 0 ] [ 2 ] = two ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = ( ( dp [ i - 1 ] [ 0 ] * zero ) + ( dp [ i - 1 ] [ 1 ] * two ) + ( dp [ i - 1 ] [ 2 ] * one ) ) % MOD ; NEW_LINE dp [ i ] [ 1 ] = ( ( dp [ i - 1 ] [ 0 ] * one ) + ( dp [ i - 1 ] [ 1 ] * zero ) + ( dp [ i - 1 ] [ 2 ] * two ) ) % MOD ; NEW_LINE dp [ i ] [ 2 ] = ( ( dp [ i - 1 ] [ 0 ] * two ) + ( dp [ i - 1 ] [ 1 ] * one ) + ( dp [ i - 1 ] [ 2 ] * zero ) ) % MOD ; NEW_LINE DEDENT return dp [ n - 1 ] [ 0 ] ; NEW_LINE DEDENT n = 5 ; NEW_LINE l = 10 ; NEW_LINE r = 100 ; NEW_LINE print ( totalSubSets ( n , l , r ) ) ; NEW_LINE


class CalcAvg { static double avgRec ( int a [ ] , int i , int n ) { if ( i == n - 1 ) return a [ i ] ; if ( i == 0 ) return ( ( a [ i ] + avgRec ( a , i + 1 , n ) ) / n ) ; return ( a [ i ] + avgRec ( a , i + 1 , n ) ) ; } static double average ( int a [ ] , int n ) { return avgRec ( a , 0 , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; System . out . println ( average ( arr , n ) ) ; } }

def avgRec ( a , i , n ) : NEW_LINE INDENT if i == n - 1 : NEW_LINE INDENT return a [ i ] NEW_LINE DEDENT if i == 0 : NEW_LINE INDENT return ( ( a [ i ] + avgRec ( a , i + 1 , n ) ) / n ) NEW_LINE DEDENT return ( a [ i ] + avgRec ( a , i + 1 , n ) ) NEW_LINE DEDENT def average ( a , n ) : NEW_LINE INDENT return avgRec ( a , 0 , n ) NEW_LINE DEDENT arr = [ 10 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( average ( arr , n ) ) NEW_LINE


class GFG { static int changeToOnes ( String str ) { int i , l , ctr = 0 ; l = str . length ( ) ; for ( i = l - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '1' ) ctr ++ ; else break ; } return l - ctr ; } static String removeZeroesFromFront ( String str ) { String s ; int i = 0 ; while ( i < str . length ( ) && str . charAt ( i ) == '0' ) i ++ ; if ( i == str . length ( ) ) s = "0" ; else s = str . substring ( i , str . length ( ) - i ) ; return s ; } public static void main ( String [ ] args ) { String str = "10010111" ; str = removeZeroesFromFront ( str ) ; System . out . println ( changeToOnes ( str ) ) ; } }

def changeToOnes ( string ) : NEW_LINE INDENT ctr = 0 ; NEW_LINE l = len ( string ) ; NEW_LINE for i in range ( l - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( string [ i ] == '1' ) : NEW_LINE INDENT ctr += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return l - ctr ; NEW_LINE DEDENT def removeZeroesFromFront ( string ) : NEW_LINE INDENT s = " " ; NEW_LINE i = 0 ; NEW_LINE while ( i < len ( string ) and string [ i ] == '0' ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT if ( i == len ( string ) ) : NEW_LINE INDENT s = "0" ; NEW_LINE DEDENT else : NEW_LINE INDENT s = string [ i : len ( string ) - i ] ; NEW_LINE DEDENT return s ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "10010111" ; NEW_LINE string = removeZeroesFromFront ( string ) ; NEW_LINE print ( changeToOnes ( string ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static void indexedSequentialSearch ( int arr [ ] , int n , int k ) { int elements [ ] = new int [ 20 ] ; int indices [ ] = new int [ 20 ] ; int temp , i ; int j = 0 , ind = 0 , start = 0 , end = 0 ; for ( i = 0 ; i < n ; i += 3 ) { elements [ ind ] = arr [ i ] ; indices [ ind ] = i ; ind ++ ; } if ( k < elements [ 0 ] ) { System . out . println ( " Not ▁ found " ) ; return ; } else { for ( i = 1 ; i <= ind ; i ++ ) if ( k < elements [ i ] ) { start = indices [ i - 1 ] ; end = indices [ i ] ; break ; } } for ( i = start ; i <= end ; i ++ ) { if ( k == arr [ i ] ) { j = 1 ; break ; } } if ( j == 1 ) System . out . println ( " Found ▁ at ▁ index ▁ " + i ) ; else System . out . println ( " Not ▁ found " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 8 , 9 , 10 } ; int n = arr . length ; int k = 8 ; indexedSequentialSearch ( arr , n , k ) ; } }

def indexedSequentialSearch ( arr , n , k ) : NEW_LINE INDENT elements = [ 0 ] * 20 NEW_LINE indices = [ 0 ] * 20 NEW_LINE j , ind = 0 , 0 NEW_LINE for i in range ( 0 , n , 3 ) : NEW_LINE INDENT elements [ ind ] = arr [ i ] NEW_LINE indices [ ind ] = i NEW_LINE ind += 1 NEW_LINE DEDENT if k < elements [ 0 ] : NEW_LINE INDENT print ( " Not ▁ found " ) NEW_LINE exit ( 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 1 , ind + 1 ) : NEW_LINE INDENT if k < elements [ i ] : NEW_LINE INDENT start = indices [ i - 1 ] NEW_LINE end = indices [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT for i in range ( start , end + 1 ) : NEW_LINE INDENT if k == arr [ i ] : NEW_LINE INDENT j = 1 NEW_LINE break NEW_LINE DEDENT DEDENT if j == 1 : NEW_LINE INDENT print ( " Found ▁ at ▁ index " , i ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ found " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 7 , 8 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE k = 8 NEW_LINE indexedSequentialSearch ( arr , n , k ) NEW_LINE DEDENT


class GFG { static byte sizeofInt = 8 ; static int countZeros ( int x ) { int total_bits = sizeofInt * 8 ; int res = 0 ; while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) { x = ( x << 1 ) ; res ++ ; } return res ; } public static void main ( String [ ] args ) { int x = 101 ; System . out . println ( countZeros ( x ) ) ; } }

def countZeros ( x ) : NEW_LINE INDENT total_bits = 32 NEW_LINE res = 0 NEW_LINE while ( ( x & ( 1 << ( total_bits - 1 ) ) ) == 0 ) : NEW_LINE INDENT x = ( x << 1 ) NEW_LINE res += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT x = 101 NEW_LINE print ( countZeros ( x ) ) NEW_LINE


class GFG { static int cntPairs ( int a [ ] , int n ) { int odd = 0 , even = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd ++ ; else if ( ( a [ i ] / 2 ) % 2 == 1 ) even ++ ; } int ans = odd * even + ( odd * ( odd - 1 ) ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 2 , 7 , 11 , 14 , 15 , 18 } ; int n = a . length ; System . out . println ( cntPairs ( a , n ) ) ; } }

def cntPairs ( a , n ) : NEW_LINE INDENT odd = 0 ; even = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 1 ) : NEW_LINE INDENT odd += 1 ; NEW_LINE DEDENT elif ( ( a [ i ] / 2 ) % 2 == 1 ) : NEW_LINE INDENT even += 1 ; NEW_LINE DEDENT DEDENT ans = odd * even + ( odd * ( odd - 1 ) ) // 2 ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 2 , 7 , 11 , 14 , 15 , 18 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( cntPairs ( a , n ) ) ; NEW_LINE DEDENT


import java . util . * ; class solution { static int sameOccurrence ( int arr [ ] , int n , int x , int y ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int ctX = 0 , ctY = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { if ( arr [ j ] == x ) ctX += 1 ; else if ( arr [ j ] == y ) ctY += 1 ; if ( ctX == ctY ) result += 1 ; } } return ( result ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . length ; int x = 2 , y = 3 ; System . out . println ( sameOccurrence ( arr , n , x , y ) ) ; } }

def sameOccurrence ( arr , n , x , y ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ctX = 0 NEW_LINE ctY = 0 NEW_LINE for j in range ( i , n , 1 ) : NEW_LINE INDENT if ( arr [ j ] == x ) : NEW_LINE INDENT ctX += 1 ; NEW_LINE DEDENT elif ( arr [ j ] == y ) : NEW_LINE INDENT ctY += 1 NEW_LINE DEDENT if ( ctX == ctY ) : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT DEDENT return ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 4 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE x = 2 NEW_LINE y = 3 NEW_LINE print ( sameOccurrence ( arr , n , x , y ) ) NEW_LINE DEDENT


class GFG { static int maxZeros ( int n ) { if ( n == 0 || ( n & ( n - 1 ) ) == 0 ) { return - 1 ; } byte b = 4 ; int setBit = 1 , prev = 0 , i ; for ( i = 1 ; i <= b * 8 ; i ++ ) { prev ++ ; if ( ( n & setBit ) == setBit ) { setBit = setBit << 1 ; break ; } setBit = setBit << 1 ; } int max0 = Integer . MIN_VALUE , cur = prev ; for ( int j = i + 1 ; j <= b * 8 ; j ++ ) { cur ++ ; if ( ( n & setBit ) == setBit ) { if ( max0 < ( cur - prev - 1 ) ) { max0 = cur - prev - 1 ; } prev = cur ; } setBit = setBit << 1 ; } return max0 ; } static public void main ( String [ ] args ) { int n = 549 ; System . out . println ( maxZeros ( n ) ) ; } }

def maxZeros ( n ) : NEW_LINE INDENT if ( n == 0 or ( n & ( n - 1 ) ) == 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT setBit = 1 NEW_LINE prev = 0 NEW_LINE i = 1 NEW_LINE while ( i < 33 ) : NEW_LINE INDENT prev += 1 NEW_LINE if ( ( n & setBit ) == setBit ) : NEW_LINE INDENT setBit = setBit << 1 NEW_LINE break NEW_LINE DEDENT setBit = setBit << 1 NEW_LINE DEDENT max0 = - 10 ** 9 NEW_LINE cur = prev NEW_LINE for j in range ( i + 1 , 33 ) : NEW_LINE INDENT cur += 1 NEW_LINE if ( ( n & setBit ) == setBit ) : NEW_LINE INDENT if ( max0 < ( cur - prev - 1 ) ) : NEW_LINE INDENT max0 = cur - prev - 1 NEW_LINE DEDENT prev = cur NEW_LINE DEDENT setBit = setBit << 1 NEW_LINE DEDENT return max0 NEW_LINE DEDENT n = 549 NEW_LINE print ( maxZeros ( n ) ) NEW_LINE


public class GFG { static final int M = 26 ; static int getIdx ( char ch ) { return ( ch - ' a ' ) ; } static boolean allSame ( int freq [ ] , int N ) { int same = 0 ; int i ; for ( i = 0 ; i < N ; i ++ ) { if ( freq [ i ] > 0 ) { same = freq [ i ] ; break ; } } for ( int j = i + 1 ; j < N ; j ++ ) if ( freq [ j ] > 0 && freq [ j ] != same ) return false ; return true ; } static boolean possibleSameCharFreqByOneRemoval ( String str ) { int l = str . length ( ) ; int [ ] freq = new int [ M ] ; for ( int i = 0 ; i < l ; i ++ ) freq [ getIdx ( str . charAt ( i ) ) ] ++ ; if ( allSame ( freq , M ) ) return true ; for ( char c = ' a ' ; c <= ' z ' ; c ++ ) { int i = getIdx ( c ) ; if ( freq [ i ] > 0 ) { freq [ i ] -- ; if ( allSame ( freq , M ) ) return true ; freq [ i ] ++ ; } } return false ; } public static void main ( String args [ ] ) { String str = " xyyzz " ; if ( possibleSameCharFreqByOneRemoval ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

M = 26 NEW_LINE def getIdx ( ch ) : NEW_LINE INDENT return ( ord ( ch ) - ord ( ' a ' ) ) NEW_LINE DEDENT def allSame ( freq , N ) : NEW_LINE INDENT for i in range ( 0 , N ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT same = freq [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( freq [ j ] > 0 and freq [ j ] != same ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def possibleSameCharFreqByOneRemoval ( str1 ) : NEW_LINE INDENT l = len ( str1 ) NEW_LINE freq = [ 0 ] * M NEW_LINE for i in range ( 0 , l ) : NEW_LINE INDENT freq [ getIdx ( str1 [ i ] ) ] += 1 NEW_LINE DEDENT if ( allSame ( freq , M ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 0 , 26 ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT freq [ i ] -= 1 NEW_LINE if ( allSame ( freq , M ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT freq [ i ] += 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " xyyzz " NEW_LINE if ( possibleSameCharFreqByOneRemoval ( str1 ) ) : 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 power ( int x , int y , int p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) == 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static boolean squareRootExists ( int n , int p ) { if ( power ( n , ( p - 1 ) / 2 , p ) == 1 ) return true ; return false ; } public static void main ( String [ ] args ) { int p = 7 ; int n = 2 ; if ( squareRootExists ( n , p ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def squareRootExists ( n , p ) : NEW_LINE INDENT if ( power ( n , ( int ) ( ( p - 1 ) / 2 ) , p ) == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT p = 7 NEW_LINE n = 2 NEW_LINE if ( squareRootExists ( n , p ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


public class GFG { public static int checkValidity ( int a , int b , int c ) { if ( a + b <= c || a + c <= b || b + c <= a ) return 0 ; else return 1 ; } public static void main ( String args [ ] ) { int a = 7 , b = 10 , c = 5 ; if ( ( checkValidity ( a , b , c ) ) == 1 ) System . out . print ( " Valid " ) ; else System . out . print ( " Invalid " ) ; } }

def checkValidity ( a , b , c ) : NEW_LINE INDENT if ( a + b <= c ) or ( a + c <= b ) or ( b + c <= a ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT a = 7 NEW_LINE b = 10 NEW_LINE c = 5 NEW_LINE if checkValidity ( a , b , c ) : NEW_LINE INDENT print ( " Valid " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE DEDENT


class Rearrange { void rearrange ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] += ( arr [ arr [ i ] ] % n ) * n ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] /= n ; } void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { Rearrange rearrange = new Rearrange ( ) ; int arr [ ] = { 3 , 2 , 0 , 1 } ; int n = arr . length ; System . out . println ( " Given ▁ Array ▁ is ▁ : " ) ; rearrange . printArr ( arr , n ) ; rearrange . rearrange ( arr , n ) ; System . out . println ( " Modified ▁ Array ▁ is ▁ : " ) ; rearrange . printArr ( arr , n ) ; } }

def rearrange ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT arr [ i ] += ( arr [ arr [ i ] ] % n ) * n NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT arr [ i ] = int ( arr [ i ] / n ) NEW_LINE DEDENT DEDENT def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT arr = [ 3 , 2 , 0 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ array ▁ is " ) NEW_LINE printArr ( arr , n ) NEW_LINE rearrange ( arr , n ) ; NEW_LINE print ( " Modified ▁ array ▁ is " ) NEW_LINE printArr ( arr , n ) NEW_LINE


import java . util . * ; class GFG { static void PrintBothArrays ( int a [ ] , int n ) { Vector < Integer > v1 = new Vector < Integer > ( ) , v2 = new Vector < Integer > ( ) ; HashMap < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { mpp . put ( a [ i ] , ( mpp . get ( a [ i ] ) == null ? 0 : mpp . get ( a [ i ] ) ) + 1 ) ; if ( mpp . get ( a [ i ] ) == 1 ) v1 . add ( a [ i ] ) ; else if ( mpp . get ( a [ i ] ) == 2 ) v2 . add ( a [ i ] ) ; else { System . out . println ( " Not ▁ possible " ) ; return ; } } Collections . sort ( v1 ) ; System . out . println ( " Strictly ▁ increasing ▁ array ▁ is : " ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) System . out . print ( v1 . get ( i ) + " ▁ " ) ; Collections . sort ( v2 ) ; Collections . reverse ( v2 ) ; System . out . println ( " \n Strictly ▁ decreasing ▁ array ▁ is : " ) ; for ( int i = 0 ; i < v2 . size ( ) ; i ++ ) System . out . print ( v2 . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 7 , 2 , 7 , 3 , 3 , 1 , 4 } ; int n = a . length ; PrintBothArrays ( a , n ) ; } }

def PrintBothArrays ( a , n ) : NEW_LINE INDENT v1 , v2 = [ ] , [ ] ; NEW_LINE mpp = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT mpp [ a [ i ] ] += 1 ; NEW_LINE if ( mpp [ a [ i ] ] == 1 ) : NEW_LINE INDENT v1 . append ( a [ i ] ) ; NEW_LINE DEDENT elif ( mpp [ a [ i ] ] == 2 ) : NEW_LINE INDENT v2 . append ( a [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) ; NEW_LINE return ; NEW_LINE DEDENT DEDENT v1 . sort ( ) ; NEW_LINE print ( " Strictly ▁ increasing ▁ array ▁ is : " ) ; NEW_LINE for it in v1 : NEW_LINE INDENT print ( it , end = " ▁ " ) ; NEW_LINE DEDENT v2 . sort ( reverse = True ) ; NEW_LINE print ( " \n Strictly ▁ decreasing ▁ array ▁ is : " ) ; NEW_LINE for it in v2 : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 7 , 2 , 7 , 3 , 3 , 1 , 4 ] ; NEW_LINE n = len ( a ) ; NEW_LINE PrintBothArrays ( a , n ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void maxCircularSum ( int [ ] arr , int n , int k ) { if ( n < k ) { System . out . println ( " Invalid " ) ; return ; } int sum = 0 , start = 0 , end = k - 1 ; for ( int i = 0 ; i < k ; i ++ ) sum += arr [ i ] ; int ans = sum ; for ( int i = k ; i < n + k ; i ++ ) { sum += arr [ i % n ] - arr [ ( i - k ) % n ] ; if ( sum > ans ) { ans = sum ; start = ( i - k + 1 ) % n ; end = i % n ; } } System . out . println ( " max ▁ circular ▁ sum ▁ = ▁ " + ans ) ; System . out . println ( " start ▁ index ▁ = ▁ " + start + " \n end ▁ index ▁ = ▁ " + end ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 18 , 4 , 3 , 4 , 5 , 6 , 7 , 8 , 2 , 10 } ; int n = arr . length ; int k = 3 ; maxCircularSum ( arr , n , k ) ; } }

def maxCircularSum ( arr , n , k ) : NEW_LINE INDENT if ( n < k ) : NEW_LINE INDENT print ( " Invalid " ) ; NEW_LINE return ; NEW_LINE DEDENT sum = 0 ; start = 0 ; end = k - 1 ; NEW_LINE for i in range ( k ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT ans = sum ; NEW_LINE for i in range ( k , n + k ) : NEW_LINE INDENT sum += arr [ i % n ] - arr [ ( i - k ) % n ] ; NEW_LINE if ( sum > ans ) : NEW_LINE INDENT ans = sum ; NEW_LINE start = ( i - k + 1 ) % n ; NEW_LINE end = i % n ; NEW_LINE DEDENT DEDENT print ( " max ▁ circular ▁ sum ▁ = ▁ " , ans ) ; NEW_LINE print ( " start ▁ index ▁ = ▁ " , start , " \n end ▁ index ▁ = ▁ " , end ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 18 , 4 , 3 , 4 , 5 , 6 , 7 , 8 , 2 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 3 ; NEW_LINE maxCircularSum ( arr , n , k ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int [ ] seg = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; static int computeSegment ( int x ) { if ( x == 0 ) return seg [ 0 ] ; int count = 0 ; while ( x > 0 ) { count += seg [ x % 10 ] ; x /= 10 ; } return count ; } static int elementMinSegment ( int [ ] arr , int n ) { int minseg = computeSegment ( arr [ 0 ] ) ; int minindex = 0 ; for ( int i = 1 ; i < n ; i ++ ) { int temp = computeSegment ( arr [ i ] ) ; if ( temp < minseg ) { minseg = temp ; minindex = i ; } } return arr [ minindex ] ; } static public void main ( String [ ] args ) { int [ ] arr = { 489 , 206 , 745 , 123 , 756 } ; int n = arr . length ; System . out . println ( elementMinSegment ( arr , n ) ) ; } }

seg = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE def computeSegment ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return seg [ 0 ] NEW_LINE DEDENT count = 0 NEW_LINE while ( x ) : NEW_LINE INDENT count += seg [ x % 10 ] NEW_LINE x = x // 10 NEW_LINE DEDENT return count NEW_LINE DEDENT def elementMinSegment ( arr , n ) : NEW_LINE INDENT minseg = computeSegment ( arr [ 0 ] ) NEW_LINE minindex = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT temp = computeSegment ( arr [ i ] ) NEW_LINE if ( temp < minseg ) : NEW_LINE INDENT minseg = temp NEW_LINE minindex = i NEW_LINE DEDENT DEDENT return arr [ minindex ] NEW_LINE DEDENT arr = [ 489 , 206 , 745 , 123 , 756 ] NEW_LINE n = len ( arr ) NEW_LINE print ( elementMinSegment ( arr , n ) ) NEW_LINE


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 ) ) ; if ( X <= k ) break ; } } 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 ) ) ; } }

import sys NEW_LINE def closetAND ( arr , n , k ) : NEW_LINE INDENT ans = sys . maxsize ; 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 if ( X <= k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT 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


public class GFG { static int CommomSubsequencesCount ( String s , String t ) { int n1 = s . length ( ) ; int n2 = t . length ( ) ; int dp [ ] [ ] = new int [ n1 + 1 ] [ n2 + 1 ] ; char ch1 , ch2 ; for ( int i = 0 ; i <= n1 ; i ++ ) { for ( int j = 0 ; j <= n2 ; j ++ ) { dp [ i ] [ j ] = 0 ; } } for ( int i = 1 ; i <= n1 ; i ++ ) { for ( int j = 1 ; j <= n2 ; j ++ ) { ch1 = s . charAt ( i - 1 ) ; ch2 = t . charAt ( j - 1 ) ; if ( ch1 == ch2 ) dp [ i ] [ j ] = 1 + dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] ; else dp [ i ] [ j ] = dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] - dp [ i - 1 ] [ j - 1 ] ; } } return dp [ n1 ] [ n2 ] ; } public static void main ( String args [ ] ) { String s = " ajblqcpdz " ; String t = " aefcnbtdi " ; System . out . println ( CommomSubsequencesCount ( s , t ) ) ; } }

def CommomSubsequencesCount ( s , t ) : NEW_LINE INDENT n1 = len ( s ) NEW_LINE n2 = len ( t ) NEW_LINE dp = [ [ 0 for i in range ( n2 + 1 ) ] for i in range ( n1 + 1 ) ] NEW_LINE for i in range ( 1 , n1 + 1 ) : NEW_LINE INDENT for j in range ( 1 , n2 + 1 ) : NEW_LINE INDENT if ( s [ i - 1 ] == t [ j - 1 ] ) : NEW_LINE INDENT dp [ i ] [ j ] = ( 1 + dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i ] [ j - 1 ] + dp [ i - 1 ] [ j ] - dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ n1 ] [ n2 ] NEW_LINE DEDENT s = " ajblqcpdz " NEW_LINE t = " aefcnbtdi " NEW_LINE print ( CommomSubsequencesCount ( s , t ) ) NEW_LINE


import java . io . * ; import java . util . * ; public class GFG { static int digitProduct ( int [ ] digits , int start , int end ) { int pro = 1 ; for ( int i = start ; i <= end ; i ++ ) { pro *= digits [ i ] ; } return pro ; } static boolean isDistinct ( int N ) { String s = " " + N ; int len = s . length ( ) ; int [ ] digits = new int [ len ] ; ArrayList < Integer > products = new ArrayList < > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { digits [ i ] = Integer . parseInt ( " " + s . charAt ( i ) ) ; } for ( int i = 0 ; i < len ; i ++ ) { for ( int j = i ; j < len ; j ++ ) { int val = digitProduct ( digits , i , j ) ; if ( products . contains ( val ) ) return false ; else products . add ( val ) ; } } return true ; } public static void main ( String args [ ] ) { int N = 324 ; if ( isDistinct ( N ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def digitProduct ( digits , start , end ) : NEW_LINE INDENT pro = 1 NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT pro *= digits [ i ] NEW_LINE DEDENT return pro NEW_LINE DEDENT def isDistinct ( N ) : NEW_LINE INDENT s = str ( N ) NEW_LINE length = len ( s ) NEW_LINE digits = [ None ] * length NEW_LINE products = set ( ) NEW_LINE for i in range ( 0 , length ) : NEW_LINE INDENT digits [ i ] = int ( s [ i ] ) NEW_LINE DEDENT for i in range ( 0 , length ) : NEW_LINE INDENT for j in range ( i , length ) : NEW_LINE INDENT val = digitProduct ( digits , i , j ) NEW_LINE if val in products : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT products . add ( val ) NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 324 NEW_LINE if isDistinct ( N ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


class GFG { static void primeFactors ( int n , int freq [ ] ) { int cnt = 0 ; while ( n % 2 == 0 ) { cnt ++ ; n = n / 2 ; } freq [ 2 ] = cnt ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { cnt = 0 ; while ( n % i == 0 ) { cnt ++ ; n = n / i ; } freq [ i ] = cnt ; } if ( n > 2 ) freq [ n ] = 1 ; } static int getMaximumPower ( int n , int m ) { int freq1 [ ] = new int [ n + 1 ] , freq2 [ ] = new int [ m + 1 ] ; primeFactors ( n , freq1 ) ; primeFactors ( m , freq2 ) ; int maxi = 0 ; for ( int i = 2 ; i <= m ; i ++ ) { if ( freq1 [ i ] == 0 && freq2 [ i ] == 0 ) continue ; if ( freq2 [ i ] > freq1 [ i ] ) return 0 ; if ( freq2 [ i ] != 0 ) { maxi = Math . max ( maxi , freq1 [ i ] / freq2 [ i ] ) ; } } return maxi ; } public static void main ( String [ ] args ) { int n = 48 , m = 4 ; System . out . println ( getMaximumPower ( n , m ) ) ; } }

import math NEW_LINE def primeFactors ( n , freq ) : NEW_LINE INDENT cnt = 0 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE n = int ( n // 2 ) NEW_LINE DEDENT freq [ 2 ] = cnt NEW_LINE i = 3 NEW_LINE while i <= math . sqrt ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT cnt = cnt + 1 NEW_LINE n = int ( n // i ) NEW_LINE DEDENT freq [ int ( i ) ] = cnt NEW_LINE i = i + 2 NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT freq [ int ( n ) ] = 1 NEW_LINE DEDENT DEDENT def getMaximumPower ( n , m ) : NEW_LINE INDENT freq1 = [ 0 ] * ( n + 1 ) NEW_LINE freq2 = [ 0 ] * ( m + 1 ) NEW_LINE primeFactors ( n , freq1 ) NEW_LINE primeFactors ( m , freq2 ) NEW_LINE maxi = 0 NEW_LINE i = 2 NEW_LINE while i <= m : NEW_LINE INDENT if ( freq1 [ i ] == 0 and freq2 [ i ] == 0 ) : NEW_LINE INDENT i = i + 1 NEW_LINE continue NEW_LINE DEDENT if ( freq2 [ i ] > freq1 [ i ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( freq2 [ i ] ) : NEW_LINE INDENT maxi = max ( maxi , int ( freq1 [ i ] // freq2 [ i ] ) ) NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return maxi NEW_LINE DEDENT n = 48 NEW_LINE m = 4 NEW_LINE print ( getMaximumPower ( n , m ) ) NEW_LINE


class GFG { static int findMinSwaps ( String s , int k ) { int ans = 0 ; int c_one = 0 , c_zero = 0 ; for ( int i = s . length ( ) - 1 ; i >= 0 ; i -- ) { if ( s . charAt ( i ) == '1' ) c_one ++ ; if ( s . charAt ( i ) == '0' ) { c_zero ++ ; ans += c_one ; } if ( c_zero == k ) break ; } if ( c_zero < k ) return - 1 ; return ans ; } public static void main ( String [ ] args ) { String s = "100111" ; int k = 2 ; System . out . println ( findMinSwaps ( s , k ) ) ; } }

def findMinSwaps ( s , k ) : NEW_LINE INDENT ans = 0 ; NEW_LINE c_one = 0 ; c_zero = 0 ; NEW_LINE for i in range ( len ( s ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT c_one += 1 ; NEW_LINE DEDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT c_zero += 1 ; NEW_LINE ans += c_one ; NEW_LINE DEDENT if ( c_zero == k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( c_zero < k ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "100111" ; NEW_LINE k = 2 ; NEW_LINE print ( findMinSwaps ( s , k ) ) ; NEW_LINE DEDENT


class GFG { static final int MAX = 1000000 ; static boolean [ ] prime = new boolean [ MAX + 1 ] ; static int [ ] sum = new int [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) prime [ i ] = true ; for ( int i = 0 ; i <= MAX ; i ++ ) sum [ i ] = 0 ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } for ( int i = 1 ; i <= MAX ; i ++ ) { if ( prime [ i ] == true ) sum [ i ] = 1 ; sum [ i ] += sum [ i - 1 ] ; } } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int l = 3 , r = 9 ; int c = ( sum [ r ] - sum [ l - 1 ] ) ; System . out . println ( " Count : ▁ " + c ) ; } }

MAX = 1000000 NEW_LINE prime = [ True ] * ( MAX + 1 ) NEW_LINE sum = [ 0 ] * ( MAX + 1 ) NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= MAX : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT i = p * 2 NEW_LINE while i <= MAX : NEW_LINE INDENT prime [ i ] = False NEW_LINE i += p NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for i in range ( 1 , MAX + 1 ) : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT sum [ i ] = 1 NEW_LINE DEDENT sum [ i ] += sum [ i - 1 ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT SieveOfEratosthenes ( ) NEW_LINE l = 3 NEW_LINE r = 9 NEW_LINE c = ( sum [ r ] - sum [ l - 1 ] ) NEW_LINE print ( " Count : " , c ) NEW_LINE DEDENT


class GFG { static void noOfMoves ( int arr [ ] , int n ) { int cnt = 0 ; int maxCnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) { cnt ++ ; } else { if ( cnt != 0 ) { maxCnt = Math . max ( maxCnt , cnt ) ; cnt = 0 ; } } } System . out . print ( maxCnt + " \n " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 } ; int N = arr . length ; noOfMoves ( arr , N ) ; int arr1 [ ] = { 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 } ; N = arr1 . length ; noOfMoves ( arr1 , N ) ; } }

def noOfMoves ( arr , n ) : NEW_LINE INDENT cnt = 0 NEW_LINE maxCnt = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( cnt != 0 ) : NEW_LINE INDENT maxCnt = max ( maxCnt , cnt ) NEW_LINE cnt = 0 NEW_LINE DEDENT DEDENT DEDENT print ( maxCnt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 1 , 1 , 1 , 1 , 0 , 0 , 1 , 1 , 0 , 0 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE noOfMoves ( arr , N ) NEW_LINE arr1 = [ 1 , 0 , 1 , 0 , 1 , 0 , 1 , 0 ] NEW_LINE N = len ( arr1 ) NEW_LINE noOfMoves ( arr1 , N ) NEW_LINE DEDENT


import java . util . * ; import java . util . Arrays ; import java . util . Collections ; class GFG { public static int calculateDifference ( int [ ] arr , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; boolean [ ] prime = new boolean [ max_val + 1 ] ; Arrays . fill ( prime , 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 P1 = 1 , P2 = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { P1 *= arr [ i ] ; } else if ( arr [ i ] != 1 ) { P2 *= arr [ i ] ; } } return Math . abs ( P2 - P1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 3 , 5 , 10 , 15 , 7 } ; int n = arr . length ; System . out . println ( calculateDifference ( arr , n ) ) ; System . exit ( 0 ) ; } }

def calculateDifference ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = ( max_val + 1 ) * [ True ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= max_val : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT P1 = 1 ; P2 = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT P1 *= arr [ i ] NEW_LINE DEDENT elif arr [ i ] != 1 : NEW_LINE INDENT P2 *= arr [ i ] NEW_LINE DEDENT DEDENT return abs ( P2 - P1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 3 , 5 , 10 , 15 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( calculateDifference ( arr , n ) ) NEW_LINE DEDENT


class GFG { static double p = ( ( 1 + Math . sqrt ( 5 ) ) / 2 ) ; static double q = ( ( 1 - Math . sqrt ( 5 ) ) / 2 ) ; static int fib ( int n ) { int i = n - 1 ; int x = ( int ) ( ( Math . pow ( p , i ) - Math . pow ( q , i ) ) / ( p - q ) ) ; return x ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( fib ( n ) ) ; } }

import math NEW_LINE p = ( 1 + math . sqrt ( 5 ) ) / 2 NEW_LINE q = ( 1 - math . sqrt ( 5 ) ) / 2 NEW_LINE def fib ( n ) : NEW_LINE INDENT i = n - 1 NEW_LINE x = ( p ** i - q ** i ) / ( p - q ) NEW_LINE return int ( x ) NEW_LINE DEDENT n = 5 NEW_LINE print ( fib ( n ) ) NEW_LINE


class GFG { static final int R = 3 ; static final int C = 3 ; static void swap ( int mat [ ] [ ] , int row1 , int row2 , int col ) { for ( int i = 0 ; i < col ; i ++ ) { int temp = mat [ row1 ] [ i ] ; mat [ row1 ] [ i ] = mat [ row2 ] [ i ] ; mat [ row2 ] [ i ] = temp ; } } static void display ( int mat [ ] [ ] , int row , int col ) { for ( int i = 0 ; i < row ; i ++ ) { for ( int j = 0 ; j < col ; j ++ ) System . out . print ( " ▁ " + mat [ i ] [ j ] ) ; System . out . print ( " \n " ) ; } } static int rankOfMatrix ( int mat [ ] [ ] ) { int rank = C ; for ( int row = 0 ; row < rank ; row ++ ) { if ( mat [ row ] [ row ] != 0 ) { for ( int col = 0 ; col < R ; col ++ ) { if ( col != row ) { double mult = ( double ) mat [ col ] [ row ] / mat [ row ] [ row ] ; for ( int i = 0 ; i < rank ; i ++ ) mat [ col ] [ i ] -= mult * mat [ row ] [ i ] ; } } } else { boolean reduce = true ; for ( int i = row + 1 ; i < R ; i ++ ) { if ( mat [ i ] [ row ] != 0 ) { swap ( mat , row , i , rank ) ; reduce = false ; break ; } } if ( reduce ) { rank -- ; for ( int i = 0 ; i < R ; i ++ ) mat [ i ] [ row ] = mat [ i ] [ rank ] ; } row -- ; } } return rank ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 10 , 20 , 10 } , { - 20 , - 30 , 10 } , { 30 , 50 , 0 } } ; System . out . print ( " Rank ▁ of ▁ the ▁ matrix ▁ is ▁ : ▁ " + rankOfMatrix ( mat ) ) ; } }

class rankMatrix ( object ) : NEW_LINE INDENT def __init__ ( self , Matrix ) : NEW_LINE INDENT self . R = len ( Matrix ) NEW_LINE self . C = len ( Matrix [ 0 ] ) NEW_LINE DEDENT def swap ( self , Matrix , row1 , row2 , col ) : NEW_LINE INDENT for i in range ( col ) : NEW_LINE INDENT temp = Matrix [ row1 ] [ i ] NEW_LINE Matrix [ row1 ] [ i ] = Matrix [ row2 ] [ i ] NEW_LINE Matrix [ row2 ] [ i ] = temp NEW_LINE DEDENT DEDENT def Display ( self , Matrix , row , col ) : NEW_LINE INDENT for i in range ( row ) : NEW_LINE INDENT for j in range ( col ) : NEW_LINE INDENT print ( " ▁ " + str ( Matrix [ i ] [ j ] ) ) NEW_LINE DEDENT print ( ' \n ' ) NEW_LINE DEDENT DEDENT def rankOfMatrix ( self , Matrix ) : NEW_LINE INDENT rank = self . C NEW_LINE for row in range ( 0 , rank , 1 ) : NEW_LINE INDENT if Matrix [ row ] [ row ] != 0 : NEW_LINE INDENT for col in range ( 0 , self . R , 1 ) : NEW_LINE INDENT if col != row : NEW_LINE INDENT multiplier = ( Matrix [ col ] [ row ] / Matrix [ row ] [ row ] ) NEW_LINE for i in range ( rank ) : NEW_LINE INDENT Matrix [ col ] [ i ] -= ( multiplier * Matrix [ row ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT else : NEW_LINE INDENT reduce = True NEW_LINE for i in range ( row + 1 , self . R , 1 ) : NEW_LINE INDENT if Matrix [ i ] [ row ] != 0 : NEW_LINE INDENT self . swap ( Matrix , row , i , rank ) NEW_LINE reduce = False NEW_LINE break NEW_LINE DEDENT DEDENT if reduce : NEW_LINE INDENT rank -= 1 NEW_LINE for i in range ( 0 , self . R , 1 ) : NEW_LINE INDENT Matrix [ i ] [ row ] = Matrix [ i ] [ rank ] NEW_LINE DEDENT DEDENT row -= 1 NEW_LINE DEDENT DEDENT return ( rank ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Matrix = [ [ 10 , 20 , 10 ] , [ - 20 , - 30 , 10 ] , [ 30 , 50 , 0 ] ] NEW_LINE RankMatrix = rankMatrix ( Matrix ) NEW_LINE print ( " Rank ▁ of ▁ the ▁ Matrix ▁ is : " , ( RankMatrix . rankOfMatrix ( Matrix ) ) ) NEW_LINE DEDENT


class GFG { static void arrange ( int N ) { if ( N == 1 ) { System . out . println ( "1" ) ; return ; } if ( N == 2 || N == 3 ) { System . out . println ( " - 1" ) ; return ; } int even = - 1 , odd = - 1 ; if ( N % 2 == 0 ) { even = N ; odd = N - 1 ; } else { odd = N ; even = N - 1 ; } while ( odd >= 1 ) { System . out . print ( odd ) ; System . out . print ( " ▁ " ) ; odd = odd - 2 ; } while ( even >= 2 ) { System . out . print ( even ) ; System . out . print ( " ▁ " ) ; even = even - 2 ; } } public static void main ( String [ ] args ) { int N = 5 ; arrange ( N ) ; } }

def arrange ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( "1" ) NEW_LINE return NEW_LINE DEDENT if ( N == 2 or N == 3 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT even = - 1 NEW_LINE odd = - 1 NEW_LINE if ( N % 2 == 0 ) : NEW_LINE INDENT even = N NEW_LINE odd = N - 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd = N NEW_LINE even = N - 1 NEW_LINE DEDENT while ( odd >= 1 ) : NEW_LINE INDENT print ( odd , end = " ▁ " ) NEW_LINE odd = odd - 2 NEW_LINE DEDENT while ( even >= 2 ) : NEW_LINE INDENT print ( even , end = " ▁ " ) NEW_LINE even = even - 2 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE arrange ( N ) NEW_LINE DEDENT


class GFG { static int countFact ( int low , int high ) { int fact = 1 , x = 1 ; while ( fact < low ) { fact = fact * x ; x ++ ; } int res = 0 ; while ( fact <= high ) { res ++ ; fact = fact * x ; x ++ ; } return res ; } public static void main ( String [ ] args ) { System . out . print ( " Count ▁ is ▁ " + countFact ( 2 , 720 ) ) ; } }

def countFact ( low , high ) : NEW_LINE INDENT fact = 1 NEW_LINE x = 1 NEW_LINE while ( fact < low ) : NEW_LINE INDENT fact = fact * x NEW_LINE x += 1 NEW_LINE DEDENT res = 0 NEW_LINE while ( fact <= high ) : NEW_LINE INDENT res += 1 NEW_LINE fact = fact * x NEW_LINE x += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT print ( " Count ▁ is ▁ " , countFact ( 2 , 720 ) ) NEW_LINE


class GFG { static int maxHandshake ( int n ) { return ( n * ( n - 1 ) ) / 2 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( maxHandshake ( n ) ) ; } }

def maxHandshake ( n ) : NEW_LINE INDENT return int ( ( n * ( n - 1 ) ) / 2 ) NEW_LINE DEDENT n = 10 NEW_LINE print ( maxHandshake ( n ) ) NEW_LINE


import java . util . * ; public class GFG { static void findWeights ( int X ) { int sum = 0 ; int power = 0 ; int number = 3 ; while ( sum < X ) { sum = number - 1 ; sum /= 2 ; power ++ ; number *= 3 ; } int ans = 1 ; for ( int i = 1 ; i <= power ; i ++ ) { System . out . print ( ans + " ▁ " ) ; ans = ans * 3 ; } } public static void main ( String [ ] args ) { int X = 2 ; findWeights ( X ) ; } }

def findWeights ( X ) : NEW_LINE INDENT sum = 0 NEW_LINE power = 0 NEW_LINE while ( sum < X ) : NEW_LINE INDENT sum = pow ( 3 , power + 1 ) - 1 NEW_LINE sum //= 2 NEW_LINE power += 1 NEW_LINE DEDENT ans = 1 NEW_LINE for i in range ( 1 , power + 1 ) : NEW_LINE INDENT print ( ans , end = " ▁ " ) NEW_LINE ans = ans * 3 NEW_LINE DEDENT DEDENT X = 2 NEW_LINE findWeights ( X ) NEW_LINE


class GFG { static int MAX_CHAR = 26 ; static boolean check ( int freq [ ] , int k ) { for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( freq [ i ] != 0 && freq [ i ] != k ) return false ; return true ; } static int substrings ( String s , int k ) { int res = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int freq [ ] = new int [ MAX_CHAR ] ; for ( int j = i ; j < s . length ( ) ; j ++ ) { int index = s . charAt ( j ) - ' a ' ; freq [ index ] ++ ; if ( freq [ index ] > k ) break ; else if ( freq [ index ] == k && check ( freq , k ) == true ) res ++ ; } } return res ; } public static void main ( String [ ] args ) { String s = " aabbcc " ; int k = 2 ; System . out . println ( substrings ( s , k ) ) ; s = " aabbc " ; k = 2 ; System . out . println ( substrings ( s , k ) ) ; } }

MAX_CHAR = 26 NEW_LINE def check ( freq , k ) : NEW_LINE INDENT for i in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT if ( freq [ i ] and freq [ i ] != k ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def substrings ( s , k ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT freq = [ 0 ] * MAX_CHAR NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT index = ord ( s [ j ] ) - ord ( ' a ' ) NEW_LINE freq [ index ] += 1 NEW_LINE if ( freq [ index ] > k ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( freq [ index ] == k and check ( freq , k ) == True ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aabbcc " NEW_LINE k = 2 NEW_LINE print ( substrings ( s , k ) ) NEW_LINE s = " aabbc " ; NEW_LINE k = 2 ; NEW_LINE print ( substrings ( s , k ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int SIZE = 4 ; static boolean productOfSingelDgt ( int n ) { if ( n >= 0 && n <= 9 ) return true ; int [ ] prime = { 2 , 3 , 5 , 7 } ; for ( int i = 0 ; i < SIZE && n > 1 ; i ++ ) while ( n % prime [ i ] == 0 ) n = n / prime [ i ] ; return ( n == 1 ) ; } public static void main ( String [ ] args ) { int n = 24 ; if ( productOfSingelDgt ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

SIZE = 4 NEW_LINE def productOfSingelDgt ( n ) : NEW_LINE INDENT if n >= 0 and n <= 9 : NEW_LINE INDENT return True NEW_LINE DEDENT prime = [ 2 , 3 , 5 , 7 ] NEW_LINE i = 0 NEW_LINE while i < SIZE and n > 1 : NEW_LINE INDENT while n % prime [ i ] == 0 : NEW_LINE INDENT n = n / prime [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return n == 1 NEW_LINE DEDENT n = 24 NEW_LINE if productOfSingelDgt ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static boolean isPrefix ( String str , int len , int i , int k ) { if ( i + k > len ) return false ; for ( int j = 0 ; j < k ; j ++ ) { if ( str . charAt ( i ) != str . charAt ( j ) ) return false ; i ++ ; } return true ; } static boolean isKPeriodic ( String str , int len , int k ) { for ( int i = k ; i < len ; i += k ) if ( ! isPrefix ( str , len , i , k ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " geeksgeeks " ; int len = str . length ( ) ; int k = 5 ; if ( isKPeriodic ( str , len , k ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def isPrefix ( string , length , i , k ) : NEW_LINE INDENT if i + k > length : NEW_LINE INDENT return False NEW_LINE DEDENT for j in range ( 0 , k ) : NEW_LINE INDENT if string [ i ] != string [ j ] : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def isKPeriodic ( string , length , k ) : NEW_LINE INDENT for i in range ( k , length , k ) : NEW_LINE INDENT if isPrefix ( string , length , i , k ) == False : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksgeeks " NEW_LINE length = len ( string ) NEW_LINE k = 5 NEW_LINE if isKPeriodic ( string , length , k ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static boolean isOsiris ( int n ) { int a = n % 10 ; int b = ( n / 10 ) % 10 ; int c = n / 100 ; int digit_sum = a + b + c ; if ( n == ( 2 * ( digit_sum ) * 11 ) ) { return true ; } return false ; } public static void main ( String args [ ] ) { int n = 132 ; if ( isOsiris ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isOsiris ( n ) : NEW_LINE INDENT a = n % 10 NEW_LINE b = ( n // 10 ) % 10 NEW_LINE c = n // 100 NEW_LINE digit_sum = a + b + c NEW_LINE if ( n == ( 2 * ( digit_sum ) * 11 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 132 NEW_LINE if isOsiris ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static double getMaxMedian ( int [ ] arr , int n , int k ) { int size = n + k ; Arrays . sort ( arr ) ; if ( size % 2 == 0 ) { double median = ( double ) ( arr [ ( size / 2 ) - 1 ] + arr [ size / 2 ] ) / 2 ; return median ; } double median1 = arr [ size / 2 ] ; return median1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 2 , 3 , 4 , 2 } ; int n = arr . length ; int k = 2 ; System . out . print ( ( int ) getMaxMedian ( arr , n , k ) ) ; } }

def getMaxMedian ( arr , n , k ) : NEW_LINE INDENT size = n + k NEW_LINE arr . sort ( reverse = False ) NEW_LINE if ( size % 2 == 0 ) : NEW_LINE INDENT median = ( arr [ int ( size / 2 ) - 1 ] + arr [ int ( size / 2 ) ] ) / 2 NEW_LINE return median NEW_LINE DEDENT median = arr [ int ( size / 2 ) ] NEW_LINE return median NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( getMaxMedian ( arr , n , k ) ) NEW_LINE DEDENT


class GFG { static int calgcd ( int a , int b ) { if ( b == 0 ) return a ; return calgcd ( b , a % b ) ; } static int getCount ( int [ ] a , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = calgcd ( gcd , a [ i ] ) ; int cnt = 0 ; for ( int i = 1 ; i * i <= gcd ; i ++ ) { if ( gcd % i == 0 ) { if ( i * i == gcd ) cnt ++ ; else cnt += 2 ; } } return cnt ; } public static void main ( String [ ] args ) { int [ ] a = { 4 , 16 , 1024 , 48 } ; int n = a . length ; System . out . println ( getCount ( a , n ) ) ; } }

from math import gcd as __gcd NEW_LINE def getCount ( a , n ) : NEW_LINE INDENT gcd = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT gcd = __gcd ( gcd , a [ i ] ) NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( 1 , gcd + 1 ) : NEW_LINE INDENT if i * i > gcd : NEW_LINE INDENT break NEW_LINE DEDENT if ( gcd % i == 0 ) : NEW_LINE INDENT if ( i * i == gcd ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 2 NEW_LINE DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT a = [ 4 , 16 , 1024 , 48 ] NEW_LINE n = len ( a ) NEW_LINE print ( getCount ( a , n ) ) NEW_LINE


class GFG { static int countDigit ( long n ) { int count = 0 ; while ( n != 0 ) { n = n / 10 ; ++ count ; } return count ; } public static void main ( String [ ] args ) { long n = 345289467 ; System . out . print ( " Number ▁ of ▁ digits ▁ : ▁ " + countDigit ( n ) ) ; } }

def countDigit ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while n != 0 : NEW_LINE INDENT n //= 10 NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT n = 345289467 NEW_LINE print ( " Number ▁ of ▁ digits ▁ : ▁ % ▁ d " % ( countDigit ( n ) ) ) NEW_LINE


class GFG { static int counDivisors ( int X ) { int count = 0 ; for ( int i = 1 ; i <= X ; ++ i ) { if ( X % i == 0 ) { count ++ ; } } return count ; } static int countDivisorsMult ( int arr [ ] , int n ) { int mul = 1 ; for ( int i = 0 ; i < n ; ++ i ) mul *= arr [ i ] ; return counDivisors ( mul ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 } ; int n = arr . length ; System . out . println ( countDivisorsMult ( arr , n ) ) ; } }

def counDivisors ( X ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , X + 1 ) : NEW_LINE INDENT if ( X % i == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def countDivisorsMult ( arr , n ) : NEW_LINE INDENT mul = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mul *= arr [ i ] NEW_LINE DEDENT return counDivisors ( mul ) NEW_LINE DEDENT arr = [ 2 , 4 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countDivisorsMult ( arr , n ) ) NEW_LINE


import java . util . Arrays ; class GFG { static boolean isPalindrome ( String s ) { for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) != s . charAt ( s . length ( ) - i - 1 ) ) { return false ; } } return true ; } static boolean ans ( String s ) { String s2 = s ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { s2 = s2 . charAt ( s2 . length ( ) - 1 ) + s2 ; s2 = s2 . substring ( 0 , s2 . length ( ) - 1 ) ; if ( ( s == null ? s2 != null : ! s . equals ( s2 ) ) && isPalindrome ( s2 ) ) { return true ; } } return false ; } static int solve ( String s ) { if ( s . length ( ) <= 3 ) { return - 1 ; } int cnt [ ] = new int [ 25 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { cnt [ s . charAt ( i ) - ' a ' ] ++ ; } if ( Arrays . stream ( cnt ) . max ( ) . getAsInt ( ) >= ( s . length ( ) - 1 ) ) { return - 1 ; } else { return ( ans ( s ) ? 1 : 2 ) ; } } public static void main ( String [ ] args ) { String s = " nolon " ; System . out . println ( solve ( s ) ) ; } }

def isPalindrome ( s ) : NEW_LINE INDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] != s [ len ( s ) - i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return true NEW_LINE DEDENT def ans ( s ) : NEW_LINE INDENT s2 = s NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT s2 = s2 [ len ( s2 ) - 1 ] + s2 NEW_LINE s2 = s2 [ 0 : len ( s2 ) - 1 ] NEW_LINE if ( s != s2 and isPalindrome ( s2 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def solve ( s ) : NEW_LINE INDENT if ( len ( s ) <= 3 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT cnt = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT cnt [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT max = cnt [ 0 ] NEW_LINE for i in range ( len ( cnt ) ) : NEW_LINE INDENT if cnt [ i ] > max : NEW_LINE INDENT max = cnt [ i ] NEW_LINE DEDENT DEDENT if ( max >= len ( s ) - 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ans ( s ) == True : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " nolon " NEW_LINE print ( solve ( s ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int XorSum ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int k = 0 ; for ( int j = 1 ; j <= arr [ i ] ; j ++ ) k ^= arr [ i ] ; result += k ; } return result ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 6 , 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( XorSum ( arr , n ) ) ; } }

def XorSum ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT k = 0 NEW_LINE for j in range ( 1 , arr [ i ] + 1 ) : NEW_LINE INDENT k = k ^ arr [ i ] NEW_LINE DEDENT result = result + k NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 1 , 2 , 6 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( XorSum ( arr , n ) ) NEW_LINE


import java . util . Stack ; class Test { static int arr [ ] = { 10 , 20 , 30 , 50 , 10 , 70 , 30 } ; static void printMaxOfMin ( int n ) { Stack < Integer > s = new Stack < > ( ) ; int left [ ] = new int [ n + 1 ] ; int right [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { left [ i ] = - 1 ; right [ i ] = n ; } for ( int i = 0 ; i < n ; i ++ ) { while ( ! s . empty ( ) && arr [ s . peek ( ) ] >= arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) left [ i ] = s . peek ( ) ; s . push ( i ) ; } while ( ! s . empty ( ) ) s . pop ( ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { while ( ! s . empty ( ) && arr [ s . peek ( ) ] >= arr [ i ] ) s . pop ( ) ; if ( ! s . empty ( ) ) right [ i ] = s . peek ( ) ; s . push ( i ) ; } int ans [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) ans [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int len = right [ i ] - left [ i ] - 1 ; ans [ len ] = Math . max ( ans [ len ] , arr [ i ] ) ; } for ( int i = n - 1 ; i >= 1 ; i -- ) ans [ i ] = Math . max ( ans [ i ] , ans [ i + 1 ] ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( ans [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { printMaxOfMin ( arr . length ) ; } }

def printMaxOfMin ( arr , n ) : NEW_LINE INDENT s = [ ] NEW_LINE left = [ - 1 ] * ( n + 1 ) NEW_LINE right = [ n ] * ( n + 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( len ( s ) != 0 and arr [ s [ - 1 ] ] >= arr [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if ( len ( s ) != 0 ) : NEW_LINE INDENT left [ i ] = s [ - 1 ] NEW_LINE DEDENT s . append ( i ) NEW_LINE DEDENT while ( len ( s ) != 0 ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( len ( s ) != 0 and arr [ s [ - 1 ] ] >= arr [ i ] ) : NEW_LINE INDENT s . pop ( ) NEW_LINE DEDENT if ( len ( s ) != 0 ) : NEW_LINE INDENT right [ i ] = s [ - 1 ] NEW_LINE DEDENT s . append ( i ) NEW_LINE DEDENT ans = [ 0 ] * ( n + 1 ) NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT ans [ i ] = 0 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT Len = right [ i ] - left [ i ] - 1 NEW_LINE ans [ Len ] = max ( ans [ Len ] , arr [ i ] ) NEW_LINE DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT ans [ i ] = max ( ans [ i ] , ans [ i + 1 ] ) NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 20 , 30 , 50 , 10 , 70 , 30 ] NEW_LINE n = len ( arr ) NEW_LINE printMaxOfMin ( arr , n ) NEW_LINE DEDENT