class Test { static int FirstDigit ( int arr [ ] , int n ) { double S = 0 ; for ( int i = 0 ; i < n ; i ++ ) S = S + Math . log10 ( arr [ i ] * 1.0 ) ; double fract_S = S - Math . floor ( S ) ; int ans = ( int ) Math . pow ( 10 , fract_S ) ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 8 , 3 , 7 } ; System . out . println ( FirstDigit ( arr , arr . length ) ) ; } }

import math NEW_LINE def FirstDigit ( arr , n ) : NEW_LINE INDENT S = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT S = S + math . log10 ( arr [ i ] * 1.0 ) NEW_LINE DEDENT fract_S = S - math . floor ( S ) NEW_LINE ans = math . pow ( 10 , fract_S ) NEW_LINE return ans NEW_LINE DEDENT arr = [ 5 , 8 , 3 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( ( int ) ( FirstDigit ( arr , n ) ) ) NEW_LINE


import java . io . * ; public class GFG { static int countNonEmptySubstr ( String str ) { int n = str . length ( ) ; return n * ( n + 1 ) / 2 ; } public static void main ( String args [ ] ) { String s = " abcde " ; System . out . println ( countNonEmptySubstr ( s ) ) ; } }

def countNonEmptySubstr ( str ) : NEW_LINE INDENT n = len ( str ) ; NEW_LINE return int ( n * ( n + 1 ) / 2 ) ; NEW_LINE DEDENT s = " abcde " ; NEW_LINE print ( countNonEmptySubstr ( s ) ) ; NEW_LINE


import java . util . * ; class GFG { static int MAX = 100000 ; static void print ( int g1 [ ] , int a , int g2 [ ] , int b ) { for ( int i = 0 ; i < a ; i ++ ) { System . out . print ( g1 [ i ] + " ▁ " ) ; } System . out . print ( " and ▁ " ) ; for ( int i = 0 ; i < b ; i ++ ) { System . out . print ( g2 [ i ] + " ▁ " ) ; } System . out . println ( ) ; } static boolean checksum ( int g1 [ ] , int a , int g2 [ ] , int b ) { int i , x ; for ( i = 0 , x = 0 ; i < a ; i ++ ) { x += g1 [ i ] ; } for ( i = 0 ; i < b ; i ++ ) { x -= g2 [ i ] ; } return ( x == 0 ) ; } static void formgroups ( int arr [ ] , int x , int g1 [ ] , int a , int g2 [ ] , int b , int n ) { if ( x == n ) { if ( checksum ( g1 , a , g2 , b ) ) { print ( g1 , a , g2 , b ) ; } return ; } g1 [ a ] = arr [ x ] ; formgroups ( arr , x + 1 , g1 , a + 1 , g2 , b , n ) ; g2 [ b ] = arr [ x ] ; formgroups ( arr , x + 1 , g1 , a , g2 , b + 1 , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 9 , 4 , 5 } ; int n = arr . length ; int [ ] g1 = new int [ MAX ] ; int [ ] g2 = new int [ MAX ] ; formgroups ( arr , 0 , g1 , 0 , g2 , 0 , n ) ; } }

MAX = 100000 NEW_LINE def prints ( g1 , a , g2 , b ) : NEW_LINE INDENT for i in range ( a ) : NEW_LINE INDENT print ( g1 [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " and ▁ " , end = " " ) NEW_LINE for i in range ( b ) : NEW_LINE INDENT print ( g2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE DEDENT def checksum ( g1 , a , g2 , b ) : NEW_LINE INDENT x = 0 NEW_LINE for i in range ( 0 , a , 1 ) : NEW_LINE INDENT x += g1 [ i ] NEW_LINE DEDENT for i in range ( b ) : NEW_LINE INDENT x -= g2 [ i ] NEW_LINE DEDENT return ( x == 0 ) NEW_LINE DEDENT def formgroups ( arr , x , g1 , a , g2 , b , n ) : NEW_LINE INDENT if ( x == n ) : NEW_LINE INDENT if ( checksum ( g1 , a , g2 , b ) ) : NEW_LINE INDENT prints ( g1 , a , g2 , b ) NEW_LINE DEDENT return NEW_LINE DEDENT g1 [ a ] = arr [ x ] NEW_LINE formgroups ( arr , x + 1 , g1 , a + 1 , g2 , b , n ) NEW_LINE g2 [ b ] = arr [ x ] NEW_LINE formgroups ( arr , x + 1 , g1 , a , g2 , b + 1 , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 9 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE g1 = [ 0 for i in range ( MAX ) ] NEW_LINE g2 = [ 0 for i in range ( MAX ) ] NEW_LINE formgroups ( arr , 0 , g1 , 0 , g2 , 0 , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static long gcd ( long a , long b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static long divTermCount ( long a , long b , long c , long num ) { return ( ( num / a ) + ( num / b ) + ( num / c ) - ( num / ( ( a * b ) / gcd ( a , b ) ) ) - ( num / ( ( c * b ) / gcd ( c , b ) ) ) - ( num / ( ( a * c ) / gcd ( a , c ) ) ) + ( num / ( ( a * b * c ) / gcd ( gcd ( a , b ) , c ) ) ) ) ; } static public void main ( String [ ] arr ) { long a = 7 , b = 3 , c = 5 , n = 100 ; System . out . println ( divTermCount ( a , b , c , n ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def divTermCount ( a , b , c , num ) : NEW_LINE INDENT return ( ( num // a ) + ( num // b ) + ( num // c ) - ( num // ( ( a * b ) // gcd ( a , b ) ) ) - ( num // ( ( c * b ) // gcd ( c , b ) ) ) - ( num // ( ( a * c ) // gcd ( a , c ) ) ) + ( num // ( ( a * b * c ) // gcd ( gcd ( a , b ) , c ) ) ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 7 ; b = 3 ; c = 5 ; n = 100 ; NEW_LINE print ( divTermCount ( a , b , c , n ) ) ; NEW_LINE DEDENT


import java . io . * ; import java . util . * ; public class GFG { static int findSplit ( int [ ] arr , int n ) { int i ; int preSum = 0 ; int ind1 = - 1 , ind2 = - 1 ; int S ; S = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) S += arr [ i ] ; if ( S % 3 != 0 ) return 0 ; int S1 = S / 3 ; int S2 = 2 * S1 ; for ( i = 0 ; i < n ; i ++ ) { preSum += arr [ i ] ; if ( preSum % S1 == 0 && ind1 == - 1 ) ind1 = i ; else if ( preSum % S2 == 0 ) { ind2 = i ; break ; } } if ( ind1 != - 1 && ind2 != - 1 ) { System . out . print ( " ( " + ind1 + " , ▁ " + ind2 + " ) " ) ; return 1 ; } return 0 ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 3 , 4 , 0 , 4 } ; int n = arr . length ; if ( findSplit ( arr , n ) == 0 ) System . out . print ( " - 1" ) ; } }

def findSplit ( arr , n ) : NEW_LINE INDENT preSum = 0 NEW_LINE ind1 = - 1 NEW_LINE ind2 = - 1 NEW_LINE S = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT S += arr [ i ] NEW_LINE DEDENT if ( S % 3 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT S1 = S / 3 NEW_LINE S2 = 2 * S1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT preSum += arr [ i ] NEW_LINE if ( preSum % S1 == 0 and ind1 == - 1 ) : NEW_LINE INDENT ind1 = i NEW_LINE DEDENT elif ( preSum % S2 == 0 ) : NEW_LINE INDENT ind2 = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( ind1 != - 1 and ind2 != - 1 ) : NEW_LINE INDENT print ( " ( { } , ▁ { } ) " . format ( ind1 , ind2 ) ) NEW_LINE return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT arr = [ 1 , 3 , 4 , 0 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE if ( findSplit ( arr , n ) == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int Circumference ( int l , int w ) { return ( 2 * ( l + w ) ) ; } static public void main ( String [ ] args ) { int l = 8 , w = 4 ; int c = Circumference ( l , w ) ; System . out . println ( " Circumference ▁ of ▁ " + " a ▁ rectangle ▁ is ▁ " + c ) ; } }

def Circumference ( l , w ) : NEW_LINE INDENT return ( 2 * ( l + w ) ) NEW_LINE DEDENT l = 8 NEW_LINE w = 4 NEW_LINE c = Circumference ( l , w ) NEW_LINE print ( " Circumference ▁ of ▁ a " + " ▁ rectangle ▁ is ▁ % ▁ d " % ( c ) ) NEW_LINE


class GFG { static int solve ( int freq [ ] ) { int n = 0 ; int max_freq = 0 ; int first = 0 ; int flag = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { n += freq [ i ] ; if ( freq [ i ] != 0 && flag == 0 ) { first = freq [ i ] ; flag = 1 ; } if ( max_freq < freq [ i ] ) { max_freq = freq [ i ] ; } } if ( n == 0 ) { return 0 ; } if ( first != max_freq ) { flag = 1 ; } else { flag = 0 ; } return Math . min ( ( n - 1 ) / 2 , n - max_freq - flag ) ; } static void solve ( String s ) { int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; } System . out . println ( solve ( freq ) ) ; } public static void main ( String [ ] args ) { String s = " geeks " ; solve ( s ) ; } }

def Solve_1 ( freq ) : NEW_LINE INDENT n = 0 NEW_LINE max_freq = 0 NEW_LINE flag = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT n += freq [ i ] NEW_LINE if ( freq [ i ] != 0 and flag == 0 ) : NEW_LINE INDENT first = freq [ i ] NEW_LINE flag = 1 NEW_LINE DEDENT if ( max_freq < freq [ i ] ) : NEW_LINE INDENT max_freq = freq [ i ] NEW_LINE DEDENT DEDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( first != max_freq ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT else : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT return min ( ( n - 1 ) // 2 , n - max_freq - flag ) NEW_LINE DEDENT def solve ( s ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT print ( Solve_1 ( freq ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeks " NEW_LINE solve ( s ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printPattern ( int n , int m , boolean flag ) { System . out . print ( m + " ▁ " ) ; if ( flag == false && n == m ) return ; if ( flag ) { if ( m - 5 > 0 ) printPattern ( n , m - 5 , true ) ; else printPattern ( n , m - 5 , false ) ; } else printPattern ( n , m + 5 , false ) ; } public static void main ( String [ ] args ) { int n = 16 ; printPattern ( n , n , true ) ; } }

def printPattern ( n , m , flag ) : NEW_LINE INDENT print ( m ) NEW_LINE if flag == False and n == m : NEW_LINE INDENT return NEW_LINE DEDENT if flag : NEW_LINE INDENT if m - 5 > 0 : NEW_LINE INDENT printPattern ( n , m - 5 , True ) NEW_LINE DEDENT else : NEW_LINE INDENT printPattern ( n , m - 5 , False ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT printPattern ( n , m + 5 , False ) NEW_LINE DEDENT DEDENT n = 16 NEW_LINE printPattern ( n , n , True ) NEW_LINE


import java . util . Arrays ; class GFG { static void possibleOrNot ( long a1 , long a2 , long b1 , long b2 , long c1 , long c2 ) { long dis1 = ( long ) Math . pow ( b1 - a1 , 2 ) + ( long ) Math . pow ( b2 - a2 , 2 ) ; long dis2 = ( long ) Math . pow ( c1 - b1 , 2 ) + ( long ) Math . pow ( c2 - b2 , 2 ) ; if ( dis1 != dis2 ) System . out . print ( " No " ) ; else if ( b1 == ( ( a1 + c1 ) / 2.0 ) && b2 == ( ( a2 + c2 ) / 2.0 ) ) System . out . print ( " No " ) ; else System . out . print ( " Yes " ) ; } public static void main ( String [ ] args ) { long a1 = 1 , a2 = 0 , b1 = 2 , b2 = 0 , c1 = 3 , c2 = 0 ; possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) ; } }

def possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) : NEW_LINE INDENT dis1 = ( pow ( b1 - a1 , 2 ) + pow ( b2 - a2 , 2 ) ) NEW_LINE dis2 = ( pow ( c1 - b1 , 2 ) + pow ( c2 - b2 , 2 ) ) NEW_LINE if ( dis1 != dis2 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT elif ( b1 == ( ( a1 + c1 ) // 2.0 ) and b2 == ( ( a2 + c2 ) // 2.0 ) ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT DEDENT a1 , b1 , c1 = 1 , 2 , 3 NEW_LINE a2 = b2 = c2 = 0 NEW_LINE possibleOrNot ( a1 , a2 , b1 , b2 , c1 , c2 ) NEW_LINE


class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void constructArr ( int pair [ ] , int n ) { int size = ( 1 + ( int ) Math . sqrt ( 1 + 8 * n ) ) / 2 ; int [ ] arr = new int [ size ] ; arr [ 0 ] = ( int ) Math . sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ; for ( int i = 1 ; i < size ; i ++ ) arr [ i ] = pair [ i - 1 ] / arr [ 0 ] ; printArr ( arr , size ) ; } public static void main ( String [ ] args ) { int pair [ ] = { 48 , 18 , 24 , 24 , 32 , 12 } ; int n = pair . length ; constructArr ( pair , n ) ; } }

from math import sqrt NEW_LINE def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT def constructArr ( pair , n ) : NEW_LINE INDENT size = int ( ( 1 + sqrt ( 1 + 8 * n ) ) // 2 ) ; NEW_LINE arr = [ 0 ] * ( size ) ; NEW_LINE arr [ 0 ] = int ( sqrt ( ( pair [ 0 ] * pair [ 1 ] ) / pair [ size - 1 ] ) ) ; NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT arr [ i ] = pair [ i - 1 ] // arr [ 0 ] ; NEW_LINE DEDENT printArr ( arr , size ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT pair = [ 48 , 18 , 24 , 24 , 32 , 12 ] ; NEW_LINE n = len ( pair ) ; NEW_LINE constructArr ( pair , n ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int findMaxOddSubarraySum ( int arr [ ] , int n ) { int min_odd = Integer . MAX_VALUE ; boolean isOdd = false ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) sum = sum + arr [ i ] ; if ( arr [ i ] % 2 != 0 ) { isOdd = true ; if ( min_odd > Math . abs ( arr [ i ] ) ) min_odd = Math . abs ( arr [ i ] ) ; } } if ( isOdd == false ) return - 1 ; if ( sum % 2 == 0 ) sum = sum - min_odd ; return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 3 , 5 , - 1 , 4 } ; int n = arr . length ; System . out . println ( findMaxOddSubarraySum ( arr , n ) ) ; } }

def findMaxOddSubarraySum ( arr , n ) : NEW_LINE INDENT min_odd = + 2147483647 NEW_LINE isOdd = False NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT sum = sum + arr [ i ] NEW_LINE DEDENT if ( arr [ i ] % 2 != 0 ) : NEW_LINE INDENT isOdd = True NEW_LINE if ( min_odd > abs ( arr [ i ] ) ) : NEW_LINE INDENT min_odd = abs ( arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT if ( isOdd == False ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( sum % 2 == 0 ) : NEW_LINE INDENT sum = sum - m NEW_LINE DEDENT return sum NEW_LINE DEDENT arr = [ 2 , - 3 , 5 , - 1 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxOddSubarraySum ( arr , n ) ) NEW_LINE


class Main { static int ceilSearch ( int arr [ ] , int low , int high , int x ) { int i ; if ( x <= arr [ low ] ) return low ; for ( i = low ; i < high ; i ++ ) { if ( arr [ i ] == x ) return i ; if ( arr [ i ] < x && arr [ i + 1 ] >= x ) return i + 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 8 , 10 , 10 , 12 , 19 } ; int n = arr . length ; int x = 3 ; int index = ceilSearch ( arr , 0 , n - 1 , x ) ; if ( index == - 1 ) System . out . println ( " Ceiling ▁ of ▁ " + x + " ▁ doesn ' t ▁ exist ▁ in ▁ array " ) ; else System . out . println ( " ceiling ▁ of ▁ " + x + " ▁ is ▁ " + arr [ index ] ) ; } }

def ceilSearch ( arr , low , high , x ) : NEW_LINE INDENT if x <= arr [ low ] : NEW_LINE INDENT return low NEW_LINE DEDENT i = low NEW_LINE for i in range ( high ) : NEW_LINE INDENT if arr [ i ] == x : NEW_LINE INDENT return i NEW_LINE DEDENT if arr [ i ] < x and arr [ i + 1 ] >= x : NEW_LINE INDENT return i + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 1 , 2 , 8 , 10 , 10 , 12 , 19 ] NEW_LINE n = len ( arr ) NEW_LINE x = 3 NEW_LINE index = ceilSearch ( arr , 0 , n - 1 , x ) ; NEW_LINE if index == - 1 : NEW_LINE INDENT print ( " Ceiling ▁ of ▁ % d ▁ doesn ' t ▁ exist ▁ in ▁ array ▁ " % x ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ceiling ▁ of ▁ % d ▁ is ▁ % d " % ( x , arr [ index ] ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int minOperations ( int n , int m , int k , int matrix [ ] [ ] ) { int [ ] arr = new int [ n * m ] ; int mod = matrix [ 0 ] [ 0 ] % k ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < m ; ++ j ) { arr [ i * m + j ] = matrix [ i ] [ j ] ; if ( matrix [ i ] [ j ] % k != mod ) { return - 1 ; } } } Arrays . sort ( arr ) ; int median = arr [ ( n * m ) / 2 ] ; int minOperations = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations += Math . abs ( arr [ i ] - median ) / k ; if ( ( n * m ) % 2 == 0 ) { int median2 = arr [ ( n * m ) / 2 ] ; int minOperations2 = 0 ; for ( int i = 0 ; i < n * m ; ++ i ) minOperations2 += Math . abs ( arr [ i ] - median2 ) / k ; minOperations = Math . min ( minOperations , minOperations2 ) ; } return minOperations ; } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 2 , 4 , 6 } , { 8 , 10 , 12 } , { 14 , 16 , 18 } , { 20 , 22 , 24 } } ; int n = matrix . length ; int m = matrix [ 0 ] . length ; int k = 2 ; System . out . println ( minOperations ( n , m , k , matrix ) ) ; } }

def minOperations ( n , m , k , matrix ) : NEW_LINE INDENT arr = [ 0 ] * ( n * m ) NEW_LINE mod = matrix [ 0 ] [ 0 ] % k NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , m ) : NEW_LINE INDENT arr [ i * m + j ] = matrix [ i ] [ j ] NEW_LINE if matrix [ i ] [ j ] % k != mod : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT DEDENT arr . sort ( ) NEW_LINE median = arr [ ( n * m ) // 2 ] NEW_LINE minOperations = 0 NEW_LINE for i in range ( 0 , n * m ) : NEW_LINE INDENT minOperations += abs ( arr [ i ] - median ) // k NEW_LINE DEDENT if ( n * m ) % 2 == 0 : NEW_LINE INDENT median2 = arr [ ( n * m ) // 2 ] NEW_LINE minOperations2 = 0 NEW_LINE for i in range ( 0 , n * m ) : NEW_LINE INDENT minOperations2 += abs ( arr [ i ] - median2 ) // k NEW_LINE DEDENT minOperations = min ( minOperations , minOperations2 ) NEW_LINE DEDENT return minOperations NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT matrix = [ [ 2 , 4 , 6 ] , [ 8 , 10 , 12 ] , [ 14 , 16 , 18 ] , [ 20 , 22 , 24 ] ] NEW_LINE n = len ( matrix ) NEW_LINE m = len ( matrix [ 0 ] ) NEW_LINE k = 2 NEW_LINE print ( minOperations ( n , m , k , matrix ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int nonDecNums ( int n ) { int [ ] [ ] a = new int [ n + 1 ] [ 10 ] ; for ( int i = 0 ; i <= 9 ; i ++ ) a [ 0 ] [ i ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) a [ i ] [ 9 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) for ( int j = 8 ; j >= 0 ; j -- ) a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] ; return a [ n ] [ 0 ] ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( " Non - decreasing ▁ digits ▁ = ▁ " + nonDecNums ( n ) ) ; } }

import numpy as np NEW_LINE def nonDecNums ( n ) : NEW_LINE INDENT a = np . zeros ( ( n + 1 , 10 ) ) NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT a [ 0 ] [ i ] = 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT a [ i ] [ 9 ] = 1 NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 8 , - 1 , - 1 ) : NEW_LINE INDENT a [ i ] [ j ] = a [ i - 1 ] [ j ] + a [ i ] [ j + 1 ] NEW_LINE DEDENT DEDENT return int ( a [ n ] [ 0 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 NEW_LINE print ( " Non - decreasing ▁ digits ▁ = ▁ " , nonDecNums ( n ) ) NEW_LINE DEDENT


class GFG { static void encode ( String s , int k ) { String newS = " " ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { int val = s . charAt ( i ) ; int dup = k ; if ( val + k > 122 ) { k -= ( 122 - val ) ; k = k % 26 ; newS += ( char ) ( 96 + k ) ; } else { newS += ( char ) ( val + k ) ; } k = dup ; } System . out . println ( newS ) ; } public static void main ( String [ ] args ) { String str = " abc " ; int k = 28 ; encode ( str , k ) ; } }

def encode ( s , k ) : NEW_LINE INDENT newS = " " NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT val = ord ( s [ i ] ) NEW_LINE dup = k NEW_LINE if val + k > 122 : NEW_LINE INDENT k -= ( 122 - val ) NEW_LINE k = k % 26 NEW_LINE newS += chr ( 96 + k ) NEW_LINE DEDENT else : NEW_LINE INDENT newS += chr ( val + k ) NEW_LINE DEDENT k = dup NEW_LINE DEDENT print ( newS ) NEW_LINE DEDENT str = " abc " NEW_LINE k = 28 NEW_LINE encode ( str , k ) NEW_LINE


class CountSmaller { void constructLowerArray ( int arr [ ] , int countSmaller [ ] , int n ) { int i , j ; for ( i = 0 ; i < n ; i ++ ) countSmaller [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < arr [ i ] ) countSmaller [ i ] ++ ; } } } void printArray ( int arr [ ] , int size ) { int i ; for ( i = 0 ; i < size ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { CountSmaller small = new CountSmaller ( ) ; int arr [ ] = { 12 , 10 , 5 , 4 , 2 , 20 , 6 , 1 , 0 , 2 } ; int n = arr . length ; int low [ ] = new int [ n ] ; small . constructLowerArray ( arr , low , n ) ; small . printArray ( low , n ) ; } }

def constructLowerArray ( arr , countSmaller , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT countSmaller [ i ] = 0 ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] < arr [ i ] ) : NEW_LINE INDENT countSmaller [ i ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def printArray ( arr , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT arr = [ 12 , 10 , 5 , 4 , 2 , 20 , 6 , 1 , 0 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE low = [ 0 ] * n NEW_LINE constructLowerArray ( arr , low , n ) NEW_LINE printArray ( low , n ) NEW_LINE


import java . util . * ; class GFG { static int compareStrings ( String str1 , String str2 ) { int i = 0 ; while ( i < str1 . length ( ) - 1 && str1 . charAt ( i ) == str2 . charAt ( i ) ) i ++ ; if ( str1 . charAt ( i ) > str2 . charAt ( i ) ) return - 1 ; if ( str1 . charAt ( i ) < str2 . charAt ( i ) ) return 1 ; else return 0 ; } static int searchStr ( String [ ] arr , String str , int first , int last ) { if ( first > last ) return - 1 ; int mid = ( last + first ) / 2 ; if ( arr [ mid ] . isEmpty ( ) ) { int left = mid - 1 ; int right = mid + 1 ; while ( true ) { if ( left < right && right > last ) return - 1 ; if ( right <= last && ! arr [ right ] . isEmpty ( ) ) { mid = right ; break ; } if ( left >= right && ! arr [ left ] . isEmpty ( ) ) { mid = left ; break ; } right ++ ; left -- ; } } if ( compareStrings ( str , arr [ mid ] ) == 0 ) return mid ; if ( compareStrings ( str , arr [ mid ] ) < 0 ) return searchStr ( arr , str , mid + 1 , last ) ; return searchStr ( arr , str , first , mid - 1 ) ; } public static void main ( String [ ] args ) { String [ ] arr = { " for " , " " , " " , " " , " geeks " , " ide " , " " , " practice " , " " , " " , " quiz " , " " , " " } ; String str = " quiz " ; int n = arr . length ; System . out . println ( searchStr ( arr , str , 0 , n - 1 ) ) ; } }

def compareStrings ( str1 , str2 ) : NEW_LINE INDENT i = 0 NEW_LINE while i < len ( str1 ) - 1 and str1 [ i ] == str2 [ i ] : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if str1 [ i ] > str2 [ i ] : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return str1 [ i ] < str2 [ i ] NEW_LINE DEDENT def searchStr ( arr , string , first , last ) : NEW_LINE INDENT if first > last : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mid = ( last + first ) // 2 NEW_LINE if len ( arr [ mid ] ) == 0 : NEW_LINE INDENT left , right = mid - 1 , mid + 1 NEW_LINE while True : NEW_LINE INDENT if left < first and right > last : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if right <= last and len ( arr [ right ] ) != 0 : NEW_LINE INDENT mid = right NEW_LINE break NEW_LINE DEDENT if left >= first and len ( arr [ left ] ) != 0 : NEW_LINE INDENT mid = left NEW_LINE break NEW_LINE DEDENT right += 1 NEW_LINE left -= 1 NEW_LINE DEDENT DEDENT if compareStrings ( string , arr [ mid ] ) == 0 : NEW_LINE INDENT return mid NEW_LINE DEDENT if compareStrings ( string , arr [ mid ] ) < 0 : NEW_LINE INDENT return searchStr ( arr , string , mid + 1 , last ) NEW_LINE DEDENT return searchStr ( arr , string , first , mid - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ " for " , " " , " " , " " , " geeks " , " ide " , " " , " practice " , " " , " " , " quiz " , " " , " " ] NEW_LINE string = " quiz " NEW_LINE n = len ( arr ) NEW_LINE print ( searchStr ( arr , string , 0 , n - 1 ) ) NEW_LINE DEDENT


import java . util . * ; class Geeks { static int distribution ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] > arr [ i - 1 ] ) count ++ ; return Math . min ( count , n / 2 ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 1 , 3 , 4 } ; int n = arr . length ; System . out . println ( distribution ( arr , n ) ) ; } }

def distribution ( arr , n ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE count = 1 NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return min ( count , n / 2 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 1 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( int ( distribution ( arr , n ) ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int findNth ( int N ) { int b = 14 ; int i ; for ( i = 2 ; i <= N ; i ++ ) { if ( i % 2 == 0 ) b = b * 2 ; else b = b - 8 ; } return b ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . print ( findNth ( N ) ) ; } }

def findNth ( N ) : NEW_LINE INDENT b = 14 NEW_LINE for i in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT b = b * 2 NEW_LINE DEDENT else : NEW_LINE INDENT b = b - 8 NEW_LINE DEDENT DEDENT return b NEW_LINE DEDENT N = 6 NEW_LINE print ( findNth ( N ) ) NEW_LINE


import java . util . * ; class GFG { static int SIZE = 26 ; static void printChar ( String str , int n ) { int [ ] freq = new int [ SIZE ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ str . charAt ( i ) - ' a ' ] % 2 == 0 ) { System . out . print ( str . charAt ( i ) ) ; } } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int n = str . length ( ) ; printChar ( str , n ) ; } }

SIZE = 26 NEW_LINE def printChar ( string , n ) : NEW_LINE INDENT freq = [ 0 ] * SIZE NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] % 2 == 0 ) : NEW_LINE INDENT print ( string [ i ] , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT string = " geeksforgeeks " NEW_LINE n = len ( string ) NEW_LINE printChar ( string , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 100000 ; static int [ ] divisors = new int [ MAX ] ; static void generateDivisors ( int n ) { for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { divisors [ i ] ++ ; } else { divisors [ i ] ++ ; divisors [ n / i ] ++ ; } } } } static int findMaxMultiples ( int [ ] arr , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ans = Math . max ( divisors [ arr [ i ] ] , ans ) ; generateDivisors ( arr [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; System . out . print ( findMaxMultiples ( arr , n ) ) ; } }

from math import ceil , sqrt NEW_LINE MAX = 100000 NEW_LINE divisors = [ 0 ] * MAX NEW_LINE def generateDivisors ( n ) : NEW_LINE INDENT for i in range ( 1 , ceil ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n // i == i ) : NEW_LINE INDENT divisors [ i ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT divisors [ i ] += 1 NEW_LINE divisors [ n // i ] += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT def findMaxMultiples ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = max ( divisors [ arr [ i ] ] , ans ) NEW_LINE generateDivisors ( arr [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxMultiples ( arr , n ) ) NEW_LINE


class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; if ( ( str . charAt ( n - 1 ) - '0' ) % 2 != 0 ) return false ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - '0' ) ; return ( digitSum % 3 == 0 ) ; } public static void main ( String [ ] args ) { String str = "1332" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def check ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE if ( ( ( int ) ( st [ n - 1 ] ) % 2 ) != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT digitSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT digitSum = digitSum + ( int ) ( st [ i ] ) NEW_LINE DEDENT return ( digitSum % 3 == 0 ) NEW_LINE DEDENT st = "1332" NEW_LINE if ( check ( st ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ " ) NEW_LINE DEDENT


import java . awt . Point ; import java . util . * ; class GFG { static int countUnique ( int arr [ ] , int n ) { Set < Point > s = new HashSet < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) s . add ( new Point ( arr [ i ] , arr [ j ] ) ) ; return s . size ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 } ; int n = arr . length ; System . out . print ( countUnique ( arr , n ) ) ; } }

def countUnique ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT s . add ( ( arr [ i ] , arr [ j ] ) ) NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countUnique ( arr , n ) ) NEW_LINE


class GFG { static int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int sumModArr ( int arr [ ] , int n ) { int subSum = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { int curr = arr [ i ] ; arr [ i ] -= subSum ; subSum += curr ; } return sumArr ( arr , n ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 40 , 25 , 12 , 10 } ; int n = arr . length ; System . out . println ( sumModArr ( arr , n ) ) ; } }

def sumArr ( arr , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def sumModArr ( arr , n ) : NEW_LINE INDENT subSum = arr [ n - 1 ] ; NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT curr = arr [ i ] ; NEW_LINE arr [ i ] -= subSum ; NEW_LINE subSum += curr ; NEW_LINE DEDENT return sumArr ( arr , n ) ; NEW_LINE DEDENT arr = [ 40 , 25 , 12 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( sumModArr ( arr , n ) ) ; NEW_LINE


import java . io . * ; public class GFG { static int __gcd ( int a , int b ) { if ( a == 0 || b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static void solveQuery ( int [ ] arr , int n , int Q , int [ ] [ ] query ) { int [ ] even = new int [ n + 1 ] ; int [ ] odd = new int [ n + 1 ] ; even [ 0 ] = odd [ 0 ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) > 0 ) { odd [ i + 1 ] = odd [ i ] + 1 ; even [ i + 1 ] = even [ i ] ; } else { even [ i + 1 ] = even [ i ] + 1 ; odd [ i + 1 ] = odd [ i ] ; } } for ( int i = 0 ; i < Q ; i ++ ) { int r = query [ i ] [ 2 ] ; int l = query [ i ] [ 1 ] ; int k = query [ i ] [ 0 ] ; int q = r - l + 1 ; int p ; if ( k > 0 ) p = odd [ r ] - odd [ l - 1 ] ; else p = even [ r ] - even [ l - 1 ] ; if ( p <= 0 ) System . out . println ( "0" ) ; else if ( p == q ) System . out . println ( "1" ) ; else { int g = __gcd ( p , q ) ; System . out . println ( p / g + " ▁ " + q / g ) ; } } } static public void main ( String [ ] args ) { int [ ] arr = { 6 , 5 , 2 , 1 , 7 } ; int n = arr . length ; int Q = 2 ; int [ ] [ ] query = { { 0 , 2 , 2 } , { 1 , 2 , 5 } } ; solveQuery ( arr , n , Q , query ) ; } }

import math NEW_LINE C = 3 NEW_LINE def solveQuery ( arr , n , Q , query ) : NEW_LINE INDENT even = [ 0 ] * ( n + 1 ) NEW_LINE odd = [ 0 ] * ( n + 1 ) NEW_LINE even [ 0 ] = 0 NEW_LINE odd [ 0 ] = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT odd [ i + 1 ] = odd [ i ] + 1 NEW_LINE even [ i + 1 ] = even [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT even [ i + 1 ] = even [ i ] + 1 NEW_LINE odd [ i + 1 ] = odd [ i ] NEW_LINE DEDENT DEDENT for i in range ( Q ) : NEW_LINE INDENT r = query [ i ] [ 2 ] NEW_LINE l = query [ i ] [ 1 ] NEW_LINE k = query [ i ] [ 0 ] NEW_LINE q = r - l + 1 NEW_LINE if ( k ) : NEW_LINE INDENT p = odd [ r ] - odd [ l - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT p = even [ r ] - even [ l - 1 ] NEW_LINE DEDENT if ( not p ) : NEW_LINE INDENT print ( "0" ) NEW_LINE DEDENT elif ( p == q ) : NEW_LINE INDENT print ( "1" ) NEW_LINE DEDENT else : NEW_LINE INDENT g = math . gcd ( p , q ) NEW_LINE print ( ( p // g ) , ( q // g ) ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 6 , 5 , 2 , 1 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE Q = 2 NEW_LINE query = [ [ 0 , 2 , 2 ] , [ 1 , 2 , 5 ] ] NEW_LINE solveQuery ( arr , n , Q , query ) NEW_LINE DEDENT


class GFG { static int oddDays ( int N ) { int hund1 = N / 100 ; int hund4 = N / 400 ; int leap = N >> 2 ; int ord = N - leap ; if ( hund1 > 0 ) { ord += hund1 ; leap -= hund1 ; } if ( hund4 > 0 ) { ord -= hund4 ; leap += hund4 ; } int days = ord + leap * 2 ; int odd = days % 7 ; return odd ; } public static void main ( String args [ ] ) { int N = 100 ; System . out . print ( oddDays ( N ) ) ; } }

def oddDays ( N ) : NEW_LINE INDENT hund1 = N // 100 NEW_LINE hund4 = N // 400 NEW_LINE leap = N >> 2 NEW_LINE ordd = N - leap NEW_LINE if ( hund1 ) : NEW_LINE INDENT ordd += hund1 NEW_LINE leap -= hund1 NEW_LINE DEDENT if ( hund4 ) : NEW_LINE INDENT ordd -= hund4 NEW_LINE leap += hund4 NEW_LINE DEDENT days = ordd + leap * 2 NEW_LINE odd = days % 7 NEW_LINE return odd NEW_LINE DEDENT N = 100 NEW_LINE print ( oddDays ( N ) ) NEW_LINE


class GFG { static String to_upper ( char [ ] in ) { for ( int i = 0 ; i < in . length ; i ++ ) { if ( ' a ' <= in [ i ] & in [ i ] <= ' z ' ) { in [ i ] = ( char ) ( in [ i ] - ' a ' + ' A ' ) ; } } return String . valueOf ( in ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; System . out . println ( to_upper ( str . toCharArray ( ) ) ) ; } }

def to_upper ( string ) : NEW_LINE INDENT for i in range ( len ( string ) ) : NEW_LINE INDENT if ( ' a ' <= string [ i ] <= ' z ' ) : NEW_LINE INDENT string = ( string [ 0 : i ] + chr ( ord ( string [ i ] ) - ord ( ' a ' ) + ord ( ' A ' ) ) + string [ i + 1 : ] ) NEW_LINE DEDENT DEDENT return string ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " geeksforgeeks " ; NEW_LINE print ( to_upper ( str ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int countDivisors ( int n , int k ) { int count = 0 , i ; for ( i = 1 ; i < Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( i % k == 0 ) { count ++ ; } if ( ( n / i ) % k == 0 ) { count ++ ; } } } if ( ( i * i == n ) && ( i % k == 0 ) ) { count -- ; } return count ; } public static void main ( String [ ] args ) { int n = 12 , k = 3 ; System . out . println ( countDivisors ( n , k ) ) ; } }

import math NEW_LINE def countDivisors ( n , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( i % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( ( n // i ) % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT if ( ( i * i == n ) and ( i % k == 0 ) ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE k = 3 NEW_LINE print ( countDivisors ( n , k ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 3 ; static int M = 3 ; static int sumOddOccurring ( int arr [ ] [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mp . containsKey ( arr [ i ] [ j ] ) ) { mp . put ( arr [ i ] [ j ] , mp . get ( arr [ i ] [ j ] ) + 1 ) ; } else { mp . put ( arr [ i ] [ j ] , 1 ) ; } } } int sum = 0 ; for ( Map . Entry < Integer , Integer > itr : mp . entrySet ( ) ) { if ( itr . getValue ( ) % 2 != 0 ) { sum += ( itr . getKey ( ) ) * ( itr . getValue ( ) ) ; } } return sum ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 1 , 3 , 2 } , { 1 , 5 , 6 } } ; System . out . println ( sumOddOccurring ( mat ) ) ; } }

def sumOddOccurring ( mat ) : NEW_LINE INDENT mp = { } NEW_LINE n , m = len ( mat ) , len ( mat [ 0 ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if mat [ i ] [ j ] in mp : NEW_LINE INDENT mp [ mat [ i ] [ j ] ] = mp . get ( mat [ i ] [ j ] ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ mat [ i ] [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT _sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if mp . get ( mat [ i ] [ j ] ) % 2 == 1 : NEW_LINE INDENT _sum += mat [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return _sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 1 , 2 , 3 ] , [ 1 , 3 , 2 ] , [ 1 , 5 , 6 ] ] NEW_LINE print ( sumOddOccurring ( mat ) ) NEW_LINE DEDENT


import java . util . * ; class geeks { static int N = 100005 ; static int [ ] lazy = new int [ 4 * N ] ; static Set < Integer > se = new HashSet < Integer > ( ) ; public static void update ( int x , int y , int value , int id , int l , int r ) { if ( x >= r || l >= y ) return ; if ( x <= l && r <= y ) { lazy [ id ] = value ; return ; } int mid = ( l + r ) / 2 ; if ( lazy [ id ] != 0 ) lazy [ 2 * id ] = lazy [ 2 * id + 1 ] = lazy [ id ] ; lazy [ id ] = 0 ; update ( x , y , value , 2 * id , l , mid ) ; update ( x , y , value , 2 * id + 1 , mid , r ) ; } public static void query ( int id , int l , int r ) { if ( lazy [ id ] != 0 ) { se . add ( lazy [ id ] ) ; return ; } if ( r - l < 2 ) return ; int mid = ( l + r ) / 2 ; query ( 2 * id , l , mid ) ; query ( 2 * id + 1 , mid , r ) ; } public static void main ( String [ ] args ) { int n = 5 , q = 3 ; update ( 1 , 4 , 1 , 1 , 0 , n ) ; update ( 0 , 2 , 2 , 1 , 0 , n ) ; update ( 3 , 4 , 3 , 1 , 0 , n ) ; query ( 1 , 0 , n ) ; System . out . println ( se . size ( ) ) ; } }

N = 100005 NEW_LINE lazy = [ 0 ] * ( 4 * N ) ; NEW_LINE se = set ( ) NEW_LINE def update ( x , y , value , id , l , r ) : NEW_LINE INDENT if ( x >= r or l >= y ) : NEW_LINE INDENT return ; NEW_LINE DEDENT if ( x <= l and r <= y ) : NEW_LINE INDENT lazy [ id ] = value ; NEW_LINE return ; NEW_LINE DEDENT mid = ( l + r ) // 2 ; NEW_LINE if ( lazy [ id ] ) : NEW_LINE INDENT lazy [ 2 * id ] = lazy [ 2 * id + 1 ] = lazy [ id ] ; NEW_LINE DEDENT lazy [ id ] = 0 ; NEW_LINE update ( x , y , value , 2 * id , l , mid ) ; NEW_LINE update ( x , y , value , 2 * id + 1 , mid , r ) ; NEW_LINE DEDENT def query ( id , l , r ) : NEW_LINE INDENT if ( lazy [ id ] ) : NEW_LINE INDENT se . add ( lazy [ id ] ) ; NEW_LINE return ; NEW_LINE DEDENT if ( r - l < 2 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT mid = ( l + r ) // 2 ; NEW_LINE query ( 2 * id , l , mid ) ; NEW_LINE query ( 2 * id + 1 , mid , r ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; q = 3 ; NEW_LINE update ( 1 , 4 , 1 , 1 , 0 , n ) ; NEW_LINE update ( 0 , 2 , 2 , 1 , 0 , n ) ; NEW_LINE update ( 3 , 4 , 3 , 1 , 0 , n ) ; NEW_LINE query ( 1 , 0 , n ) ; NEW_LINE print ( len ( se ) ) ; NEW_LINE DEDENT


class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static boolean coprime ( int a , int b ) { return ( __gcd ( a , b ) == 1 ) ; } static void pairSum ( int n ) { int mid = n / 2 ; for ( int i = mid ; i >= 1 ; i -- ) { if ( coprime ( i , n - i ) == true ) { System . out . print ( i + " ▁ " + ( n - i ) ) ; break ; } } } public static void main ( String args [ ] ) { int n = 11 ; pairSum ( n ) ; } }

import math NEW_LINE def coprime ( a , b ) : NEW_LINE INDENT return 1 if ( math . gcd ( a , b ) == 1 ) else 0 ; NEW_LINE DEDENT def pairSum ( n ) : NEW_LINE INDENT mid = int ( n / 2 ) ; NEW_LINE i = mid ; NEW_LINE while ( i >= 1 ) : NEW_LINE INDENT if ( coprime ( i , n - i ) == 1 ) : NEW_LINE INDENT print ( i , n - i ) ; NEW_LINE break ; NEW_LINE DEDENT i = i - 1 ; NEW_LINE DEDENT DEDENT n = 11 ; NEW_LINE pairSum ( n ) ; NEW_LINE


import java . util . * ; class GFG { static float func ( float x ) { return ( 1 / ( 1 + x * x ) ) ; } static float calculate ( float lower_limit , float upper_limit , int interval_limit ) { float value ; float interval_size = ( upper_limit - lower_limit ) / interval_limit ; float sum = func ( lower_limit ) + func ( upper_limit ) ; for ( int i = 1 ; i < interval_limit ; i ++ ) { if ( i % 3 == 0 ) sum = sum + 2 * func ( lower_limit + i * interval_size ) ; else sum = sum + 3 * func ( lower_limit + i * interval_size ) ; } return ( 3 * interval_size / 8 ) * sum ; } public static void main ( String [ ] args ) { int interval_limit = 10 ; float lower_limit = 1 ; float upper_limit = 10 ; float integral_res = calculate ( lower_limit , upper_limit , interval_limit ) ; System . out . println ( integral_res ) ; } }

def func ( x ) : NEW_LINE INDENT return ( float ( 1 ) / ( 1 + x * x ) ) NEW_LINE DEDENT def calculate ( lower_limit , upper_limit , interval_limit ) : NEW_LINE INDENT interval_size = ( float ( upper_limit - lower_limit ) / interval_limit ) NEW_LINE sum = func ( lower_limit ) + func ( upper_limit ) ; NEW_LINE for i in range ( 1 , interval_limit ) : NEW_LINE INDENT if ( i % 3 == 0 ) : NEW_LINE INDENT sum = sum + 2 * func ( lower_limit + i * interval_size ) NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + 3 * func ( lower_limit + i * interval_size ) NEW_LINE DEDENT DEDENT return ( ( float ( 3 * interval_size ) / 8 ) * sum ) NEW_LINE DEDENT interval_limit = 10 NEW_LINE lower_limit = 1 NEW_LINE upper_limit = 10 NEW_LINE integral_res = calculate ( lower_limit , upper_limit , interval_limit ) NEW_LINE print ( round ( integral_res , 6 ) ) NEW_LINE


import java . util . * ; class GFG { static boolean sumSquare ( int n ) { HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i * i <= n ; ++ i ) { s . put ( i * i , 1 ) ; if ( s . containsKey ( n - i * i ) ) { System . out . println ( ( int ) Math . sqrt ( n - i * i ) + " ^ 2 ▁ + ▁ " + i + " ^ 2" ) ; return true ; } } return false ; } public static void main ( String [ ] args ) { int n = 169 ; System . out . print ( sumSquare ( n ) ? " YES \n " : " NO \n " ) ; } }

def sumSquare ( n ) : NEW_LINE INDENT s = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if i * i > n : NEW_LINE INDENT break NEW_LINE DEDENT s [ i * i ] = 1 NEW_LINE if ( n - i * i ) in s . keys ( ) : NEW_LINE INDENT print ( ( n - i * i ) ** ( 1 / 2 ) , " ^ 2 ▁ + " , i , " ^ 2" ) NEW_LINE return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT n = 169 NEW_LINE if ( sumSquare ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int sz = ( int ) 1e5 ; static boolean [ ] isPrime = new boolean [ sz + 1 ] ; static void sieve ( ) { Arrays . fill ( isPrime , true ) ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static int minDifference ( int L , int R ) { int fst = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( isPrime [ i ] ) { fst = i ; break ; } } int snd = 0 ; for ( int i = fst + 1 ; i <= R ; i ++ ) { if ( isPrime [ i ] ) { snd = i ; break ; } } if ( snd == 0 ) return - 1 ; int diff = snd - fst ; int left = snd + 1 ; int right = R ; for ( int i = left ; i <= right ; i ++ ) { if ( isPrime [ i ] ) { if ( i - snd <= diff ) { fst = snd ; snd = i ; diff = snd - fst ; } } } return diff ; } public static void main ( String [ ] args ) { sieve ( ) ; int L = 21 , R = 50 ; System . out . println ( minDifference ( L , R ) ) ; } }

from math import sqrt NEW_LINE sz = int ( 1e5 ) ; NEW_LINE isPrime = [ True ] * ( sz + 1 ) ; NEW_LINE def sieve ( ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False ; NEW_LINE for i in range ( 2 , int ( sqrt ( sz ) ) + 1 ) : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , sz , i ) : NEW_LINE INDENT isPrime [ j ] = False ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def minDifference ( L , R ) : NEW_LINE INDENT fst = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT fst = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT snd = 0 ; NEW_LINE for i in range ( fst + 1 , R + 1 ) : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT snd = i ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( snd == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT diff = snd - fst ; NEW_LINE left = snd + 1 ; NEW_LINE right = R ; NEW_LINE for i in range ( left , right + 1 ) : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT if ( i - snd <= diff ) : NEW_LINE INDENT fst = snd ; NEW_LINE snd = i ; NEW_LINE diff = snd - fst ; NEW_LINE DEDENT DEDENT DEDENT return diff ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sieve ( ) ; NEW_LINE L = 21 ; R = 50 ; NEW_LINE print ( minDifference ( L , R ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static float area ( float a ) { if ( a < 0 ) return - 1 ; float area = ( float ) Math . sqrt ( a ) / 6 ; return area ; } public static void main ( String [ ] args ) { float a = 10 ; System . out . println ( area ( a ) ) ; } }

from math import * NEW_LINE def area ( a ) : NEW_LINE INDENT if a < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT area = sqrt ( a ) / 6 NEW_LINE return area NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 10 NEW_LINE print ( round ( area ( a ) , 6 ) ) NEW_LINE DEDENT


class CFG { static int Minsteps ( int n , int m ) { int ans = 0 ; while ( m > n ) { if ( m % 2 != 0 ) { m ++ ; ans ++ ; } m /= 2 ; ans ++ ; } return ans + n - m ; } public static void main ( String [ ] args ) { int n = 4 , m = 6 ; System . out . println ( Minsteps ( n , m ) ) ; } }

def Minsteps ( n , m ) : NEW_LINE INDENT ans = 0 NEW_LINE while ( m > n ) : NEW_LINE INDENT if ( m & 1 ) : NEW_LINE INDENT m += 1 NEW_LINE ans += 1 NEW_LINE DEDENT m //= 2 NEW_LINE ans += 1 NEW_LINE DEDENT return ans + n - m NEW_LINE DEDENT n = 4 NEW_LINE m = 6 NEW_LINE print ( Minsteps ( n , m ) ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static int getLevenstein ( StringBuilder input ) { StringBuilder revInput = new StringBuilder ( input ) ; revInput = revInput . reverse ( ) ; int n = input . length ( ) ; int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= n ; ++ i ) { dp [ 0 ] [ i ] = i ; dp [ i ] [ 0 ] = i ; } for ( int i = 1 ; i <= n ; ++ i ) { for ( int j = 1 ; j <= n ; ++ j ) { if ( input . charAt ( i - 1 ) == revInput . charAt ( j - 1 ) ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + Math . min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) ; } } int res = Integer . MAX_VALUE ; for ( int i = n , j = 0 ; i >= 0 ; i -- , j ++ ) { res = Math . min ( res , dp [ i ] [ j ] ) ; if ( i < n ) res = Math . min ( res , dp [ i + 1 ] [ j ] ) ; if ( i > 0 ) res = Math . min ( res , dp [ i - 1 ] [ j ] ) ; } return res ; } public static void main ( String [ ] args ) { StringBuilder input = new StringBuilder ( " myfirstgeekarticle " ) ; System . out . println ( getLevenstein ( input ) ) ; } }

INT_MAX = 99999999999 NEW_LINE def getLevenstein ( inpt ) : NEW_LINE INDENT revInput = inpt [ : : - 1 ] NEW_LINE n = len ( inpt ) NEW_LINE dp = [ [ - 1 for _ in range ( n + 1 ) ] for __ in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = i NEW_LINE dp [ i ] [ 0 ] = i NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if inpt [ i - 1 ] == revInput [ j - 1 ] : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = 1 + min ( dp [ i - 1 ] [ j ] , dp [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT res = INT_MAX NEW_LINE i , j = n , 0 NEW_LINE while i >= 0 : NEW_LINE INDENT res = min ( res , dp [ i ] [ j ] ) NEW_LINE if i < n : NEW_LINE INDENT res = min ( res , dp [ i + 1 ] [ j ] ) NEW_LINE DEDENT if i > 0 : NEW_LINE INDENT res = min ( res , dp [ i - 1 ] [ j ] ) NEW_LINE DEDENT i -= 1 NEW_LINE j += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT inpt = " myfirstgeekarticle " NEW_LINE print ( getLevenstein ( inpt ) ) NEW_LINE DEDENT


class GFG { static String convertToString ( int sum ) { String str = " " ; while ( sum > 0 ) { str = str + ( char ) ( ( sum % 10 ) + '0' ) ; sum = sum / 10 ; } return str ; } static String GetIndividulaDigitSum ( String str , int len ) { int sum = 0 ; for ( int i = 0 ; i < len ; i ++ ) { sum = sum + str . charAt ( i ) - '0' ; } return convertToString ( sum ) ; } static int GetDigitalRoot ( String str ) { if ( str . length ( ) == 1 ) { return str . charAt ( 0 ) - '0' ; } str = GetIndividulaDigitSum ( str , str . length ( ) ) ; return GetDigitalRoot ( str ) ; } public static void main ( String [ ] args ) { String str = "675987890789756545689070986776987" ; System . out . print ( GetDigitalRoot ( str ) ) ; } }

def convertToString ( sum ) : NEW_LINE INDENT str1 = " " NEW_LINE while ( sum ) : NEW_LINE INDENT str1 = str1 + chr ( ( sum % 10 ) + ord ( '0' ) ) NEW_LINE sum = sum // 10 NEW_LINE DEDENT return str1 NEW_LINE DEDENT def GetIndividulaDigitSum ( str1 , len1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT sum = sum + ord ( str1 [ i ] ) - ord ( '0' ) NEW_LINE DEDENT return convertToString ( sum ) NEW_LINE DEDENT def GetDigitalRoot ( str1 ) : NEW_LINE INDENT if ( len ( str1 ) == 1 ) : NEW_LINE INDENT return ord ( str1 [ 0 ] ) - ord ( '0' ) NEW_LINE DEDENT str1 = GetIndividulaDigitSum ( str1 , len ( str1 ) ) NEW_LINE return GetDigitalRoot ( str1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = "675987890789756545689070986776987" NEW_LINE print ( GetDigitalRoot ( str1 ) ) NEW_LINE DEDENT


public class GFG { static int costToPanagram ( String str , int cost [ ] ) { int i , n = str . length ( ) ; int occurrences [ ] = new int [ 26 ] ; for ( i = 0 ; i < n ; i ++ ) occurrences [ str . charAt ( i ) - ' a ' ] ++ ; int gain = 0 ; for ( i = 0 ; i < 26 ; i ++ ) { if ( occurrences [ i ] == 0 ) gain -= ( 2 * cost [ i ] ) ; else if ( occurrences [ i ] > 1 ) gain += ( cost [ i ] * ( occurrences [ i ] - 1 ) ) ; } if ( gain >= 0 ) return 0 ; return ( gain * - 1 ) ; } public static void main ( String [ ] args ) { int cost [ ] = { 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 } ; String str = " geeksforgeeks " ; System . out . println ( costToPanagram ( str , cost ) ) ; } }

def costToPanagram ( string , cost ) : NEW_LINE INDENT n = len ( string ) NEW_LINE occurrences = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT occurrences [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT gain = 0 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if occurrences [ i ] == 0 : NEW_LINE INDENT gain -= 2 * cost [ i ] NEW_LINE DEDENT elif occurrences [ i ] > 1 : NEW_LINE INDENT gain += cost [ i ] * ( occurrences [ i ] - 1 ) NEW_LINE DEDENT DEDENT if gain >= 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return gain * - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT cost = [ 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 ] NEW_LINE string = " geeksforgeeks " NEW_LINE print ( costToPanagram ( string , cost ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int maxSum1 ( int arr [ ] , int n ) { int dp [ ] = new int [ n ] ; int maxi = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { dp [ i ] = arr [ i ] ; if ( maxi < arr [ i ] ) maxi = arr [ i ] ; } for ( int i = 2 ; i < n - 1 ; i ++ ) { for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( dp [ i ] < dp [ j ] + arr [ i ] ) { dp [ i ] = dp [ j ] + arr [ i ] ; if ( maxi < dp [ i ] ) maxi = dp [ i ] ; } } } return maxi ; } static int maxSum2 ( int arr [ ] , int n ) { int dp [ ] = new int [ n ] ; int maxi = 0 ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] = arr [ i ] ; if ( maxi < arr [ i ] ) maxi = arr [ i ] ; } for ( int i = 3 ; i < n ; i ++ ) { for ( int j = 1 ; j < i - 1 ; j ++ ) { if ( dp [ i ] < arr [ i ] + dp [ j ] ) { dp [ i ] = arr [ i ] + dp [ j ] ; if ( maxi < dp [ i ] ) maxi = dp [ i ] ; } } } return maxi ; } static int findMaxSum ( int arr [ ] , int n ) { int t = Math . max ( maxSum1 ( arr , n ) , maxSum2 ( arr , n ) ) ; return t ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 1 } ; int n = arr . length ; System . out . println ( findMaxSum ( arr , n ) ) ; } }

def maxSum1 ( arr , n ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE maxi = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT dp [ i ] = arr [ i ] NEW_LINE if ( maxi < arr [ i ] ) : NEW_LINE INDENT maxi = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( 2 , n - 1 ) : NEW_LINE INDENT for j in range ( i - 1 ) : NEW_LINE INDENT if ( dp [ i ] < dp [ j ] + arr [ i ] ) : NEW_LINE INDENT dp [ i ] = dp [ j ] + arr [ i ] NEW_LINE if ( maxi < dp [ i ] ) : NEW_LINE INDENT maxi = dp [ i ] NEW_LINE DEDENT DEDENT DEDENT DEDENT return maxi NEW_LINE DEDENT def maxSum2 ( arr , n ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE maxi = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] = arr [ i ] NEW_LINE if ( maxi < arr [ i ] ) : NEW_LINE INDENT maxi = arr [ i ] NEW_LINE DEDENT DEDENT for i in range ( 3 , n ) : NEW_LINE INDENT for j in range ( 1 , i - 1 ) : NEW_LINE INDENT if ( dp [ i ] < arr [ i ] + dp [ j ] ) : NEW_LINE INDENT dp [ i ] = arr [ i ] + dp [ j ] NEW_LINE if ( maxi < dp [ i ] ) : NEW_LINE INDENT maxi = dp [ i ] NEW_LINE DEDENT DEDENT DEDENT DEDENT return maxi NEW_LINE DEDENT def findMaxSum ( arr , n ) : NEW_LINE INDENT return max ( maxSum1 ( arr , n ) , maxSum2 ( arr , n ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxSum ( arr , n ) ) NEW_LINE DEDENT


class GFG { static float angleOncirCumference ( float z ) { return ( z / 2 ) ; } public static void main ( String [ ] args ) { float angle = 65 ; float z = angleOncirCumference ( angle ) ; System . out . println ( " The ▁ angle ▁ is ▁ " + z + " ▁ degrees " ) ; } }

def angleOncirCumference ( z ) : NEW_LINE INDENT return ( z / 2 ) ; NEW_LINE DEDENT angle = 65 ; NEW_LINE z = angleOncirCumference ( angle ) ; NEW_LINE print ( " The ▁ angle ▁ is " , ( z ) , " degrees " ) ; NEW_LINE


import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int maxLengthSubArray ( int arr [ ] , int n ) { int maxLen = - 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int lcm = 1 * arr [ i ] ; int product = 1 * arr [ i ] ; for ( int k = i + 1 ; k <= j ; k ++ ) { lcm = ( ( ( arr [ k ] * lcm ) ) / ( gcd ( arr [ k ] , lcm ) ) ) ; product = product * arr [ k ] ; } if ( lcm == product ) { maxLen = Math . max ( maxLen , j - i + 1 ) ; } } } return maxLen ; } public static void main ( String args [ ] ) { int arr [ ] = { 6 , 10 , 21 } ; int n = arr . length ; System . out . println ( maxLengthSubArray ( arr , n ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def maxLengthSubArray ( arr , n ) : NEW_LINE INDENT maxLen = - 1 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT lcm = arr [ i ] NEW_LINE product = arr [ i ] NEW_LINE for k in range ( i + 1 , j + 1 ) : NEW_LINE INDENT lcm = ( ( ( arr [ k ] * lcm ) ) // ( gcd ( arr [ k ] , lcm ) ) ) NEW_LINE product = product * arr [ k ] NEW_LINE DEDENT if ( lcm == product ) : NEW_LINE INDENT maxLen = max ( maxLen , j - i + 1 ) NEW_LINE DEDENT DEDENT DEDENT return maxLen NEW_LINE DEDENT arr = [ 6 , 10 , 21 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxLengthSubArray ( arr , n ) ) NEW_LINE


import java . io . * ; import java . util . * ; public class GFG { static int powerNumbers ( int n ) { HashSet < Integer > v = new HashSet < Integer > ( ) ; v . add ( 1 ) ; for ( int i = 2 ; i * i <= n ; i ++ ) { int j = i * i ; v . add ( j ) ; while ( j * i <= n ) { v . add ( j * i ) ; j = j * i ; } } return v . size ( ) ; } public static void main ( String args [ ] ) { System . out . print ( powerNumbers ( 50 ) ) ; } }

def powerNumbers ( n ) : NEW_LINE INDENT v = set ( ) ; NEW_LINE v . add ( 1 ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( i * i <= n ) : NEW_LINE INDENT j = i * i ; NEW_LINE v . add ( j ) ; NEW_LINE while ( j * i <= n ) : NEW_LINE INDENT v . add ( j * i ) ; NEW_LINE j = j * i ; NEW_LINE DEDENT DEDENT DEDENT return len ( v ) ; NEW_LINE DEDENT print ( powerNumbers ( 50 ) ) ; NEW_LINE


class GFG { static double angle_triangle ( int x1 , int x2 , int x3 , int y1 , int y2 , int y3 , int z1 , int z2 , int z3 ) { int num = ( x2 - x1 ) * ( x3 - x1 ) + ( y2 - y1 ) * ( y3 - y1 ) + ( z2 - z1 ) * ( z3 - z1 ) ; double den = Math . sqrt ( Math . pow ( ( x2 - x1 ) , 2 ) + Math . pow ( ( y2 - y1 ) , 2 ) + Math . pow ( ( z2 - z1 ) , 2 ) ) * Math . sqrt ( Math . pow ( ( x3 - x1 ) , 2 ) + Math . pow ( ( y3 - y1 ) , 2 ) + Math . pow ( ( z3 - z1 ) , 2 ) ) ; double angle = Math . acos ( num / den ) * ( 180.0 / 3.141592653589793238463 ) ; return angle ; } public static void main ( String [ ] args ) { int x1 = - 1 ; int y1 = 3 ; int z1 = 2 ; int x2 = 2 ; int y2 = 3 ; int z2 = 5 ; int x3 = 3 ; int y3 = 5 ; int z3 = - 2 ; double angle_A = angle_triangle ( x1 , x2 , x3 , y1 , y2 , y3 , z1 , z2 , z3 ) ; double angle_B = angle_triangle ( x2 , x3 , x1 , y2 , y3 , y1 , z2 , z3 , z1 ) ; double angle_C = angle_triangle ( x3 , x2 , x1 , y3 , y2 , y1 , z3 , z2 , z1 ) ; System . out . println ( " Angles ▁ are ▁ : " ) ; System . out . println ( " angle ▁ A ▁ = ▁ " + angle_A + " ▁ degree " ) ; System . out . println ( " angle ▁ B ▁ = ▁ " + angle_B + " ▁ degree " ) ; System . out . println ( " angle ▁ C ▁ = ▁ " + angle_C + " ▁ degree " ) ; } }

import math NEW_LINE def angle_triangle ( x1 , x2 , x3 , y1 , y2 , y3 , z1 , z2 , z3 ) : NEW_LINE INDENT num = ( x2 - x1 ) * ( x3 - x1 ) + ( y2 - y1 ) * ( y3 - y1 ) + ( z2 - z1 ) * ( z3 - z1 ) NEW_LINE den = math . sqrt ( ( x2 - x1 ) ** 2 + ( y2 - y1 ) ** 2 + ( z2 - z1 ) ** 2 ) * \ NEW_LINE INDENT math . sqrt ( ( x3 - x1 ) ** 2 + ( y3 - y1 ) ** 2 + ( z3 - z1 ) ** 2 ) NEW_LINE DEDENT angle = math . degrees ( math . acos ( num / den ) ) NEW_LINE return round ( angle , 3 ) NEW_LINE DEDENT x1 = - 1 NEW_LINE y1 = 3 NEW_LINE z1 = 2 NEW_LINE x2 = 2 NEW_LINE y2 = 3 NEW_LINE z2 = 5 NEW_LINE x3 = 3 NEW_LINE y3 = 5 NEW_LINE z3 = - 2 NEW_LINE angle_A = angle_triangle ( x1 , x2 , x3 , y1 , y2 , y3 , z1 , z2 , z3 ) NEW_LINE angle_B = angle_triangle ( x2 , x3 , x1 , y2 , y3 , y1 , z2 , z3 , z1 ) NEW_LINE angle_C = angle_triangle ( x3 , x2 , x1 , y3 , y2 , y1 , z3 , z2 , z1 ) NEW_LINE print ( " Angles ▁ are ▁ : " ) NEW_LINE print ( " angle ▁ A ▁ = ▁ " , angle_A , " degree " ) NEW_LINE print ( " angle ▁ B ▁ = ▁ " , angle_B , " degree " ) NEW_LINE print ( " angle ▁ C ▁ = ▁ " , angle_C , " degree " ) NEW_LINE


public class close_to_n_divisible_m { static int closestNumber ( int n , int m ) { int q = n / m ; int n1 = m * q ; int n2 = ( n * m ) > 0 ? ( m * ( q + 1 ) ) : ( m * ( q - 1 ) ) ; if ( Math . abs ( n - n1 ) < Math . abs ( n - n2 ) ) return n1 ; return n2 ; } public static void main ( String args [ ] ) { int n = 13 , m = 4 ; System . out . println ( closestNumber ( n , m ) ) ; n = - 15 ; m = 6 ; System . out . println ( closestNumber ( n , m ) ) ; n = 0 ; m = 8 ; System . out . println ( closestNumber ( n , m ) ) ; n = 18 ; m = - 7 ; System . out . println ( closestNumber ( n , m ) ) ; } }

def closestNumber ( n , m ) : NEW_LINE INDENT q = int ( n / m ) NEW_LINE n1 = m * q NEW_LINE if ( ( n * m ) > 0 ) : NEW_LINE INDENT n2 = ( m * ( q + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT n2 = ( m * ( q - 1 ) ) NEW_LINE DEDENT if ( abs ( n - n1 ) < abs ( n - n2 ) ) : NEW_LINE INDENT return n1 NEW_LINE DEDENT return n2 NEW_LINE DEDENT n = 13 ; m = 4 NEW_LINE print ( closestNumber ( n , m ) ) NEW_LINE n = - 15 ; m = 6 NEW_LINE print ( closestNumber ( n , m ) ) NEW_LINE n = 0 ; m = 8 NEW_LINE print ( closestNumber ( n , m ) ) NEW_LINE n = 18 ; m = - 7 NEW_LINE print ( closestNumber ( n , m ) ) NEW_LINE


import java . io . * ; class GFG { static int findSum ( int n ) { int ans = 0 , temp = 0 , num ; for ( int i = 1 ; i <= n && temp < n ; i ++ ) { temp = i - 1 ; num = 1 ; while ( temp < n ) { if ( temp + i <= n ) ans += ( i * num ) ; else ans += ( ( n - temp ) * num ) ; temp += i ; num ++ ; } } return ans ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( findSum ( N ) ) ; } }

def findSum ( n ) : NEW_LINE INDENT ans = 0 ; temp = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if temp < n : NEW_LINE INDENT temp = i - 1 NEW_LINE num = 1 NEW_LINE while temp < n : NEW_LINE INDENT if temp + i <= n : NEW_LINE INDENT ans += i * num NEW_LINE DEDENT else : NEW_LINE INDENT ans += ( n - temp ) * num NEW_LINE DEDENT temp += i NEW_LINE num += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT N = 2 NEW_LINE print ( findSum ( N ) ) NEW_LINE


import java . util . * ; class GFG { static int maxSubArraySum ( int arr [ ] , int size ) { int max_so_far = arr [ 0 ] ; int curr_max = arr [ 0 ] ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = Math . max ( arr [ i ] , curr_max + arr [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } static int lenOfLongSubarrWithGivenSum ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == k ) maxLen = i + 1 ; if ( um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - k ) ) { if ( maxLen < ( i - um . get ( sum - k ) ) ) maxLen = i - um . get ( sum - k ) ; } } return maxLen ; } static int lenLongSubarrWithMaxSum ( int arr [ ] , int n ) { int maxSum = maxSubArraySum ( arr , n ) ; return lenOfLongSubarrWithGivenSum ( arr , n , maxSum ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , - 2 , - 1 , 3 , - 4 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ longest ▁ subarray ▁ " + " having ▁ maximum ▁ sum ▁ = ▁ " + lenLongSubarrWithMaxSum ( arr , n ) ) ; } }

def maxSubArraySum ( arr , size ) : NEW_LINE INDENT max_so_far = arr [ 0 ] NEW_LINE curr_max = arr [ 0 ] NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT curr_max = max ( arr [ i ] , curr_max + arr [ i ] ) NEW_LINE max_so_far = max ( max_so_far , curr_max ) NEW_LINE DEDENT return max_so_far NEW_LINE DEDENT def lenOfLongSubarrWithGivenSum ( arr , n , k ) : NEW_LINE INDENT um = dict ( ) NEW_LINE Sum , maxLen = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE if ( Sum == k ) : NEW_LINE INDENT maxLen = i + 1 NEW_LINE DEDENT if ( Sum not in um . keys ( ) ) : NEW_LINE INDENT um [ Sum ] = i NEW_LINE DEDENT if ( Sum in um . keys ( ) ) : NEW_LINE INDENT if ( ( Sum - k ) in um . keys ( ) and maxLen < ( i - um [ Sum - k ] ) ) : NEW_LINE INDENT maxLen = i - um [ Sum - k ] NEW_LINE DEDENT DEDENT DEDENT return maxLen NEW_LINE DEDENT def lenLongSubarrWithMaxSum ( arr , n ) : NEW_LINE INDENT maxSum = maxSubArraySum ( arr , n ) NEW_LINE return lenOfLongSubarrWithGivenSum ( arr , n , maxSum ) NEW_LINE DEDENT arr = [ 5 , - 2 , - 1 , 3 , - 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ of ▁ longest ▁ subarray ▁ having ▁ maximum ▁ sum ▁ = ▁ " , lenLongSubarrWithMaxSum ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { public static int squareFree ( int n ) { int cnt = 0 ; for ( int i = 1 ; ; i ++ ) { boolean isSqFree = true ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % ( j * j ) == 0 ) { isSqFree = false ; break ; } } if ( isSqFree == true ) { cnt ++ ; if ( cnt == n ) return i ; } } } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( " " + squareFree ( n ) ) ; } }

def squareFree ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE i = 1 ; NEW_LINE while ( True ) : NEW_LINE INDENT isSqFree = True ; NEW_LINE j = 2 ; NEW_LINE while ( j * j <= i ) : NEW_LINE INDENT if ( i % ( j * j ) == 0 ) : NEW_LINE INDENT isSqFree = False ; NEW_LINE break ; NEW_LINE DEDENT j += 1 ; NEW_LINE DEDENT if ( isSqFree == True ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE if ( cnt == n ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT n = 10 ; NEW_LINE print ( squareFree ( n ) ) ; NEW_LINE


class GFG { static int MAX_CHAR = 256 ; static int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } static void populateAndIncreaseCount ( int [ ] count , char [ ] str ) { int i ; for ( i = 0 ; i < str . length ; ++ i ) ++ count [ str [ i ] ] ; for ( i = 1 ; i < MAX_CHAR ; ++ i ) count [ i ] += count [ i - 1 ] ; } static void updatecount ( int [ ] count , char ch ) { int i ; for ( i = ch ; i < MAX_CHAR ; ++ i ) -- count [ i ] ; } static int findRank ( char [ ] str ) { int len = str . length ; int mul = fact ( len ) ; int rank = 1 , i ; int count [ ] = new int [ MAX_CHAR ] ; populateAndIncreaseCount ( count , str ) ; for ( i = 0 ; i < len ; ++ i ) { mul /= len - i ; rank += count [ str [ i ] - 1 ] * mul ; updatecount ( count , str [ i ] ) ; } return rank ; } public static void main ( String args [ ] ) { char str [ ] = " string " . toCharArray ( ) ; System . out . println ( findRank ( str ) ) ; } }

MAX_CHAR = 256 ; NEW_LINE count = [ 0 ] * ( MAX_CHAR + 1 ) ; NEW_LINE def fact ( n ) : NEW_LINE INDENT return 1 if ( n <= 1 ) else ( n * fact ( n - 1 ) ) ; NEW_LINE DEDENT def populateAndIncreaseCount ( str ) : NEW_LINE INDENT for i in range ( len ( str ) ) : NEW_LINE INDENT count [ ord ( str [ i ] ) ] += 1 ; NEW_LINE DEDENT for i in range ( 1 , MAX_CHAR ) : NEW_LINE INDENT count [ i ] += count [ i - 1 ] ; NEW_LINE DEDENT DEDENT def updatecount ( ch ) : NEW_LINE INDENT for i in range ( ord ( ch ) , MAX_CHAR ) : NEW_LINE INDENT count [ i ] -= 1 ; NEW_LINE DEDENT DEDENT def findRank ( str ) : NEW_LINE INDENT len1 = len ( str ) ; NEW_LINE mul = fact ( len1 ) ; NEW_LINE rank = 1 ; NEW_LINE populateAndIncreaseCount ( str ) ; NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT mul = mul // ( len1 - i ) ; NEW_LINE rank += count [ ord ( str [ i ] ) - 1 ] * mul ; NEW_LINE updatecount ( str [ i ] ) ; NEW_LINE DEDENT return rank ; NEW_LINE DEDENT str = " string " ; NEW_LINE print ( findRank ( str ) ) ; NEW_LINE


class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - '0' ) ; return ( digitSum % 9 == 0 ) ; } public static void main ( String [ ] args ) { String str = "99333" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def check ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE digitSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT digitSum = digitSum + ( int ) ( st [ i ] ) NEW_LINE DEDENT return ( digitSum % 9 == 0 ) NEW_LINE DEDENT st = "99333" NEW_LINE if ( check ( st ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; public class GeeksforGeeks { public static void StarPattern ( int height ) { for ( int i = 0 ; i < height ; i ++ ) { for ( int j = height - 1 ; j > i ; j -- ) { System . out . print ( " * " ) ; } boolean printChar = false ; for ( int j = 0 ; j < ( ( i * 2 ) + 1 ) ; j ++ ) { if ( printChar ) { System . out . print ( " * " ) ; } else { System . out . print ( i + 1 ) ; } printChar = ! printChar ; } for ( int j = height - 1 ; j > i ; j -- ) { System . out . print ( " * " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int height = 7 ; StarPattern ( height ) ; } }

def StarPattern ( height ) : NEW_LINE INDENT for i in range ( height ) : NEW_LINE INDENT for j in range ( height - 1 , i , - 1 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT printChar = False NEW_LINE for j in range ( ( ( i * 2 ) + 1 ) ) : NEW_LINE INDENT if ( printChar ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( i + 1 , end = " " ) NEW_LINE DEDENT printChar = not printChar NEW_LINE DEDENT for j in range ( height - 1 , i , - 1 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT height = 7 NEW_LINE StarPattern ( height ) NEW_LINE


import java . io . * ; class GFG { static void findValues ( int a , int b ) { if ( ( a - b ) % 2 == 1 ) { System . out . println ( " - 1" ) ; return ; } System . out . println ( ( ( a - b ) / 2 ) + " ▁ " + ( ( a + b ) / 2 ) ) ; } public static void main ( String [ ] args ) { int a = 12 , b = 8 ; findValues ( a , b ) ; } }

def findValues ( a , b ) : NEW_LINE INDENT if ( ( a - b ) % 2 == 1 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT print ( ( a - b ) // 2 , ( a + b ) // 2 ) ; NEW_LINE DEDENT a = 12 ; b = 8 ; NEW_LINE findValues ( a , b ) ; NEW_LINE


import java . io . * ; class GFG { static boolean isDvisibleBy12 ( String num ) { if ( num . length ( ) >= 3 ) { int d1 = ( int ) num . charAt ( num . length ( ) - 1 ) ; if ( d1 % 2 != 0 ) return false ; int d2 = ( int ) num . charAt ( num . length ( ) - 2 ) ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) sum += num . charAt ( i ) ; return ( sum % 3 == 0 && ( d2 * 10 + d1 ) % 4 == 0 ) ; } else { int number = Integer . parseInt ( num ) ; return ( number % 12 == 0 ) ; } } public static void main ( String [ ] args ) { String num = "12244824607284961224" ; if ( isDvisibleBy12 ( num ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

import math NEW_LINE def isDvisibleBy12 ( num ) : NEW_LINE INDENT if ( len ( num ) >= 3 ) : NEW_LINE INDENT d1 = int ( num [ len ( num ) - 1 ] ) ; NEW_LINE if ( d1 % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT d2 = int ( num [ len ( num ) - 2 ] ) NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , len ( num ) ) : NEW_LINE INDENT sum += int ( num [ i ] ) NEW_LINE DEDENT return ( sum % 3 == 0 and ( d2 * 10 + d1 ) % 4 == 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT number = int ( num ) NEW_LINE return ( number % 12 == 0 ) NEW_LINE DEDENT DEDENT num = "12244824607284961224" NEW_LINE if ( isDvisibleBy12 ( num ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void count_ ( int count [ ] , int e ) { boolean hash [ ] = new boolean [ 10 ] ; while ( e > 0 ) { if ( hash [ e % 10 ] == false ) count [ e % 10 ] ++ ; hash [ e % 10 ] = true ; e /= 10 ; } } static void find_subsequence ( int arr [ ] , int n ) { int count [ ] = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { count_ ( count , arr [ i ] ) ; } int longest = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) longest = Math . max ( count [ i ] , longest ) ; System . out . print ( longest ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 12 , 23 , 74 , 13 } ; int n = arr . length ; find_subsequence ( arr , n ) ; } }

def count_ ( count , e ) : NEW_LINE INDENT hash = [ False ] * 10 NEW_LINE while ( e > 0 ) : NEW_LINE INDENT if ( hash [ e % 10 ] == False ) : NEW_LINE INDENT count [ e % 10 ] += 1 NEW_LINE DEDENT hash [ e % 10 ] = True NEW_LINE e //= 10 NEW_LINE DEDENT DEDENT def find_subsequence ( arr , n ) : NEW_LINE INDENT count = [ 0 ] * 10 NEW_LINE for i in range ( n ) : NEW_LINE INDENT count_ ( count , arr [ i ] ) NEW_LINE DEDENT longest = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT longest = max ( count [ i ] , longest ) NEW_LINE DEDENT print ( longest ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 11 , 12 , 23 , 74 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE find_subsequence ( arr , n ) NEW_LINE DEDENT


class GFG { static String findLongestPalindrome ( String str ) { int count [ ] = new int [ 256 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { count [ str . charAt ( i ) ] ++ ; } String beg = " " , mid = " " , end = " " ; for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { if ( count [ ch ] % 2 == 1 ) { mid = String . valueOf ( ch ) ; count [ ch -- ] -- ; } else { for ( int i = 0 ; i < count [ ch ] / 2 ; i ++ ) { beg += ch ; } } } end = beg ; end = reverse ( end ) ; return beg + mid + end ; } static String reverse ( String str ) { String ans = " " ; char [ ] try1 = str . toCharArray ( ) ; for ( int i = try1 . length - 1 ; i >= 0 ; i -- ) { ans += try1 [ i ] ; } return ans ; } public static void main ( String [ ] args ) { String str = " abbaccd " ; System . out . println ( findLongestPalindrome ( str ) ) ; } }

def findLongestPalindrome ( strr ) : NEW_LINE INDENT count = [ 0 ] * 256 NEW_LINE for i in range ( len ( strr ) ) : NEW_LINE INDENT count [ ord ( strr [ i ] ) ] += 1 NEW_LINE DEDENT beg = " " NEW_LINE mid = " " NEW_LINE end = " " NEW_LINE ch = ord ( ' a ' ) NEW_LINE while ch <= ord ( ' z ' ) : NEW_LINE INDENT if ( count [ ch ] & 1 ) : NEW_LINE INDENT mid = ch NEW_LINE count [ ch ] -= 1 NEW_LINE ch -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( count [ ch ] // 2 ) : NEW_LINE INDENT beg += chr ( ch ) NEW_LINE DEDENT DEDENT ch += 1 NEW_LINE DEDENT end = beg NEW_LINE end = end [ : : - 1 ] NEW_LINE return beg + chr ( mid ) + end NEW_LINE DEDENT strr = " abbaccd " NEW_LINE print ( findLongestPalindrome ( strr ) ) NEW_LINE


import java . io . * ; class GFG { static int totalSumDivisibleByNum ( int n , int number ) { int firstnum = ( int ) Math . pow ( 10 , n - 1 ) ; int lastnum = ( int ) Math . pow ( 10 , n ) ; int sum = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int n = 3 , num = 7 ; System . out . println ( totalSumDivisibleByNum ( n , num ) ) ; } }

def totalSumDivisibleByNum ( n , number ) : NEW_LINE INDENT firstnum = pow ( 10 , n - 1 ) NEW_LINE lastnum = pow ( 10 , n ) NEW_LINE sum = 0 NEW_LINE for i in range ( firstnum , lastnum ) : NEW_LINE INDENT if ( i % number == 0 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 3 ; num = 7 NEW_LINE print ( totalSumDivisibleByNum ( n , num ) ) NEW_LINE


class GFG { static int N = 3 ; static int maxPathSum ( int tri [ ] [ ] ) { int ans = 0 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < N - i ; j ++ ) { if ( j - 1 >= 0 ) tri [ i ] [ j ] += Math . max ( tri [ i + 1 ] [ j ] , tri [ i + 1 ] [ j - 1 ] ) ; else tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; ans = Math . max ( ans , tri [ i ] [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int tri [ ] [ ] = { { 1 , 5 , 3 } , { 4 , 8 , 0 } , { 1 , 0 , 0 } } ; System . out . println ( maxPathSum ( tri ) ) ; } }

N = 3 NEW_LINE def maxPathSum ( tri ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( 0 , N - i ) : NEW_LINE INDENT if ( j - 1 >= 0 ) : NEW_LINE INDENT tri [ i ] [ j ] += max ( tri [ i + 1 ] [ j ] , tri [ i + 1 ] [ j - 1 ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; NEW_LINE DEDENT ans = max ( ans , tri [ i ] [ j ] ) ; NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT tri = [ [ 1 , 5 , 3 ] , [ 4 , 8 , 0 ] , [ 1 , 0 , 0 ] ] NEW_LINE print ( maxPathSum ( tri ) ) NEW_LINE


class GFG { static long squareDigitSum ( String s ) { long lengthN = s . length ( ) ; long result = ( lengthN / 9 ) * 81 + ( long ) Math . pow ( ( lengthN % 9 ) , 2 ) ; return result ; } public static void main ( String [ ] args ) { String s = "1111" ; System . out . println ( squareDigitSum ( s ) ) ; } }

def squareDigitSum ( num ) : NEW_LINE INDENT lengthN = len ( num ) NEW_LINE result = ( lengthN // 9 ) * 81 + ( lengthN % 9 ) ** 2 NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = "1111" NEW_LINE print ( squareDigitSum ( N ) ) NEW_LINE DEDENT


class GFG { static String even_or_odd ( String N ) { int len = N . length ( ) ; if ( N . charAt ( len - 1 ) == '0' || N . charAt ( len - 1 ) == '2' || N . charAt ( len - 1 ) == '4' || N . charAt ( len - 1 ) == '6' || N . charAt ( len - 1 ) == '8' || N . charAt ( len - 1 ) == ' A ' || N . charAt ( len - 1 ) == ' C ' || N . charAt ( len - 1 ) == ' E ' ) return ( " Even " ) ; else return ( " Odd " ) ; } public static void main ( String [ ] args ) { String N = " AB3454D " ; System . out . print ( even_or_odd ( N ) ) ; } }

def even_or_odd ( N ) : NEW_LINE INDENT l = len ( N ) NEW_LINE if ( N [ l - 1 ] == '0' or N [ l - 1 ] == '2' or N [ l - 1 ] == '4' or N [ l - 1 ] == '6' or N [ l - 1 ] == '8' or N [ l - 1 ] == ' A ' or N [ l - 1 ] == ' C ' or N [ l - 1 ] == ' E ' ) : NEW_LINE INDENT return ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " Odd " ) NEW_LINE DEDENT DEDENT N = " AB3454D " NEW_LINE print ( even_or_odd ( N ) ) NEW_LINE


import java . util . Arrays ; import java . util . Collections ; class GFG { static int MAX_SIZE = 10 ; static void sortByRow ( Integer mat [ ] [ ] , int n , boolean ascending ) { for ( int i = 0 ; i < n ; i ++ ) { if ( ascending ) Arrays . sort ( mat [ i ] ) ; else Arrays . sort ( mat [ i ] , Collections . reverseOrder ( ) ) ; } } static void transpose ( Integer mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ j ] [ i ] ; mat [ j ] [ i ] = temp ; } } static void sortMatRowAndColWise ( Integer mat [ ] [ ] , int n ) { sortByRow ( mat , n , true ) ; transpose ( mat , n ) ; sortByRow ( mat , n , false ) ; transpose ( mat , n ) ; } static void printMat ( Integer mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 3 ; Integer mat [ ] [ ] = { { 3 , 2 , 1 } , { 9 , 8 , 7 } , { 6 , 5 , 4 } } ; System . out . print ( " Original ▁ Matrix : \n " ) ; printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; System . out . print ( " \n Matrix ▁ After ▁ Sorting : \n " ) ; printMat ( mat , n ) ; } }

MAX_SIZE = 10 NEW_LINE def sortByRow ( mat , n , ascending ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( ascending ) : NEW_LINE INDENT mat [ i ] . sort ( ) NEW_LINE DEDENT else : NEW_LINE INDENT mat [ i ] . sort ( reverse = True ) NEW_LINE DEDENT DEDENT DEDENT def transpose ( mat , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT temp = mat [ i ] [ j ] NEW_LINE mat [ i ] [ j ] = mat [ j ] [ i ] NEW_LINE mat [ j ] [ i ] = temp NEW_LINE DEDENT DEDENT DEDENT def sortMatRowAndColWise ( mat , n ) : NEW_LINE INDENT sortByRow ( mat , n , True ) NEW_LINE transpose ( mat , n ) NEW_LINE sortByRow ( mat , n , False ) NEW_LINE transpose ( mat , n ) NEW_LINE DEDENT def printMat ( mat , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , " ▁ " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 3 NEW_LINE mat = [ [ 3 , 2 , 1 ] , [ 9 , 8 , 7 ] , [ 6 , 5 , 4 ] ] NEW_LINE print ( " Original ▁ Matrix : " ) NEW_LINE printMat ( mat , n ) NEW_LINE sortMatRowAndColWise ( mat , n ) NEW_LINE print ( " Matrix ▁ After ▁ Sorting : " ) NEW_LINE printMat ( mat , n ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { public static int minNumber ( int a [ ] , int n , int x ) { int l = 0 , h = 0 , e = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == x ) e ++ ; else if ( a [ i ] > x ) h ++ ; else if ( a [ i ] < x ) l ++ ; } int ans = 0 ; if ( l > h ) ans = l - h ; else if ( l < h ) ans = h - l - 1 ; return ans + 1 - e ; } public static void main ( String [ ] args ) { int x = 10 ; int a [ ] = { 10 , 20 , 30 } ; int n = a . length ; System . out . println ( minNumber ( a , n , x ) ) ; } }

def minNumber ( a , n , x ) : NEW_LINE INDENT l = 0 NEW_LINE h = 0 NEW_LINE e = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] == x : NEW_LINE INDENT e += 1 NEW_LINE DEDENT elif a [ i ] > x : NEW_LINE INDENT h += 1 NEW_LINE DEDENT elif a [ i ] < x : NEW_LINE INDENT l += 1 NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE if l > h : NEW_LINE INDENT ans = l - h NEW_LINE DEDENT elif l < h : NEW_LINE INDENT ans = h - l - 1 ; NEW_LINE DEDENT return ans + 1 - e NEW_LINE DEDENT x = 10 NEW_LINE a = [ 10 , 20 , 30 ] NEW_LINE n = len ( a ) NEW_LINE print ( minNumber ( a , n , x ) ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GfG { public static int findremainder ( int arr [ ] , int len , int n ) { int mul = 1 ; for ( int i = 0 ; i < len ; i ++ ) mul = ( mul * ( arr [ i ] % n ) ) % n ; return mul % n ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 100 , 10 , 5 , 25 , 35 , 14 } ; int len = 6 ; int n = 11 ; System . out . println ( findremainder ( arr , len , n ) ) ; } }

def findremainder ( arr , lens , n ) : NEW_LINE INDENT mul = 1 NEW_LINE for i in range ( lens ) : NEW_LINE INDENT mul = ( mul * ( arr [ i ] % n ) ) % n NEW_LINE DEDENT return mul % n NEW_LINE DEDENT arr = [ 100 , 10 , 5 , 25 , 35 , 14 ] NEW_LINE lens = len ( arr ) NEW_LINE n = 11 NEW_LINE print ( findremainder ( arr , lens , n ) ) NEW_LINE


public class GFG { static double hexDiagonal ( float a ) { if ( a < 0 ) return - 1 ; double d = ( double ) 1.73 * a ; return d ; } public static void main ( String [ ] args ) { float a = 9 ; System . out . println ( hexDiagonal ( a ) ) ; } }

def hexDiagonal ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT d = 1.73 * a ; NEW_LINE return d ; NEW_LINE DEDENT a = 9 ; NEW_LINE print ( hexDiagonal ( a ) ) ; NEW_LINE


import java . io . * ; class GFG { static int MAX = 100 ; static void middleProduct ( int mat [ ] [ ] , int n ) { int row_prod = 1 , col_prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { row_prod *= mat [ n / 2 ] [ i ] ; col_prod *= mat [ i ] [ n / 2 ] ; } System . out . print ( " Product ▁ of ▁ middle ▁ row ▁ = ▁ " + row_prod ) ; System . out . print ( " Product ▁ of ▁ middle ▁ column ▁ = ▁ " + col_prod ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 2 , 1 , 7 } , { 3 , 7 , 2 } , { 5 , 4 , 9 } } ; middleProduct ( mat , 3 ) ; } }

MAX = 100 NEW_LINE def middleProduct ( mat , n ) : NEW_LINE INDENT row_prod = 1 NEW_LINE col_prod = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT row_prod *= mat [ n // 2 ] [ i ] NEW_LINE col_prod *= mat [ i ] [ n // 2 ] NEW_LINE DEDENT print ( " Product ▁ of ▁ middle ▁ row ▁ = ▁ " , row_prod ) NEW_LINE print ( " Product ▁ of ▁ middle ▁ column ▁ = ▁ " , col_prod ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 2 , 1 , 7 ] , [ 3 , 7 , 2 ] , [ 5 , 4 , 9 ] ] NEW_LINE middleProduct ( mat , 3 ) NEW_LINE DEDENT


class GFG { static int oddFib ( int n ) { n = ( 3 * n + 1 ) / 2 ; int a = - 1 , b = 1 , c = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( oddFib ( n ) ) ; } }

def oddFib ( n ) : NEW_LINE INDENT n = ( 3 * n + 1 ) // 2 NEW_LINE a = - 1 NEW_LINE b = 1 NEW_LINE c = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE DEDENT return c NEW_LINE DEDENT n = 4 NEW_LINE print ( oddFib ( n ) ) NEW_LINE


import java . io . * ; class GFG { static int m = 6 , n = 4 ; static int linearCheck ( int ar [ ] [ ] , int arr [ ] ) { for ( int i = 0 ; i < m ; i ++ ) { boolean matched = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ar [ i ] [ j ] != arr [ j ] ) { matched = false ; break ; } } if ( matched ) return i + 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int row [ ] = { 10 , 9 , 22 , 23 } ; System . out . println ( linearCheck ( mat , row ) ) ; } }

m , n = 6 , 4 ; NEW_LINE def linearCheck ( ar , arr ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT matched = True ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( ar [ i ] [ j ] != arr [ j ] ) : NEW_LINE INDENT matched = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( matched ) : NEW_LINE INDENT return i + 1 ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 0 , 0 , 1 , 0 ] , [ 10 , 9 , 22 , 23 ] , [ 40 , 40 , 40 , 40 ] , [ 43 , 44 , 55 , 68 ] , [ 81 , 73 , 100 , 132 ] , [ 100 , 75 , 125 , 133 ] ] ; NEW_LINE row = [ 10 , 9 , 22 , 23 ] ; NEW_LINE print ( linearCheck ( mat , row ) ) ; NEW_LINE DEDENT


class GfG { static int largestCoprime ( int N ) { if ( N == 6 ) return 1 ; else if ( N % 4 == 0 ) return ( N / 2 ) - 1 ; else if ( N % 2 == 0 ) return ( N / 2 ) - 2 ; else return ( ( N - 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( largestCoprime ( n ) ) ; } }

def largestCoprime ( N ) : NEW_LINE INDENT if N == 6 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif N % 4 == 0 : NEW_LINE INDENT return N // 2 - 1 NEW_LINE DEDENT elif N % 2 == 0 : NEW_LINE INDENT return N // 2 - 2 NEW_LINE DEDENT else : NEW_LINE INDENT return ( N - 1 ) // 2 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 50 NEW_LINE print ( largestCoprime ( n ) ) NEW_LINE DEDENT


class GFG { static int fastmod ( int base , int exp , int mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { int ans = fastmod ( base , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int findPowerSum ( int n , int ar [ ] ) { final int mod = ( int ) 1e9 + 7 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int base = countSetBits ( ar [ i ] ) ; int exp = ar [ i ] ; sum += fastmod ( base , exp , mod ) ; sum %= mod ; } return sum ; } public static void main ( String [ ] args ) { int n = 3 ; int ar [ ] = { 1 , 2 , 3 } ; System . out . println ( findPowerSum ( n , ar ) ) ; } }

def fastmod ( base , exp , mod ) : NEW_LINE INDENT if ( exp == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( exp % 2 == 0 ) : NEW_LINE INDENT ans = fastmod ( base , exp / 2 , mod ) ; NEW_LINE return ( ans % mod * ans % mod ) % mod ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; NEW_LINE DEDENT DEDENT def findPowerSum ( n , ar ) : NEW_LINE INDENT mod = int ( 1e9 ) + 7 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT base = bin ( ar [ i ] ) . count ( '1' ) ; NEW_LINE exp = ar [ i ] ; NEW_LINE sum += fastmod ( base , exp , mod ) ; NEW_LINE sum %= mod ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE ar = [ 1 , 2 , 3 ] ; NEW_LINE print ( findPowerSum ( n , ar ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean isPresent ( int arr [ ] , int low , int high , int value ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == value ) return true ; else if ( arr [ mid ] > value ) high = mid - 1 ; else low = mid + 1 ; } return false ; } static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int value = x - arr1 [ i ] ; if ( isPresent ( arr2 , 0 , n - 1 , value ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }

def isPresent ( arr , low , high , value ) : NEW_LINE INDENT while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( arr [ mid ] == value ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( arr [ mid ] > value ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def countPairs ( arr1 , arr2 , m , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT value = x - arr1 [ i ] NEW_LINE if ( isPresent ( arr2 , 0 , n - 1 , value ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , 3 , 5 , 7 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 8 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE x = 10 NEW_LINE print ( " Count ▁ = ▁ " , countPairs ( arr1 , arr2 , m , n , x ) ) NEW_LINE DEDENT


import java . util . * ; class Main { static void printDistinct ( int arr [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ! set . contains ( arr [ i ] ) ) { set . add ( arr [ i ] ) ; System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; printDistinct ( arr ) ; } }

def printDistinct ( arr , n ) : NEW_LINE INDENT s = dict ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] not in s . keys ( ) ) : NEW_LINE INDENT s [ arr [ i ] ] = arr [ i ] ; NEW_LINE print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ] ; NEW_LINE n = 7 ; NEW_LINE printDistinct ( arr , n ) ; NEW_LINE


import java . io . * ; class GFG { static float Mean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } static float meanAbsDevtion ( float arr [ ] , int n ) { float absSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) absSum = absSum + Math . abs ( arr [ i ] - Mean ( arr , n ) ) ; return absSum / n ; } public static void main ( String [ ] args ) { float arr [ ] = { 10 , 15 , 15 , 17 , 18 , 21 } ; int n = arr . length ; System . out . println ( meanAbsDevtion ( arr , n ) ) ; } }

def Mean ( arr , n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sm = sm + arr [ i ] NEW_LINE DEDENT return sm // n NEW_LINE DEDENT def meanAbsoluteDeviation ( arr , n ) : NEW_LINE INDENT absSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT absSum = absSum + abs ( arr [ i ] - Mean ( arr , n ) ) NEW_LINE DEDENT return absSum / n NEW_LINE DEDENT arr = [ 10 , 15 , 15 , 17 , 18 , 21 ] NEW_LINE n = len ( arr ) NEW_LINE print ( meanAbsoluteDeviation ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static int cntSubsets ( int [ ] arr , int n ) { int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } int result = a [ n - 1 ] + b [ n - 1 ] ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 } ; int n = arr . length ; System . out . println ( cntSubsets ( arr , n ) ) ; } }

def cntSubsets ( arr , n ) : NEW_LINE INDENT a = [ 0 ] * n NEW_LINE b = [ 0 ] * n ; NEW_LINE a [ 0 ] = b [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; NEW_LINE b [ i ] = a [ i - 1 ] ; NEW_LINE DEDENT result = a [ n - 1 ] + b [ n - 1 ] ; NEW_LINE return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntSubsets ( arr , n ) ) ; NEW_LINE DEDENT


class Segregate { void segregate0and1 ( int arr [ ] , int size ) { int left = 0 , right = size - 1 ; while ( left < right ) { while ( arr [ left ] == 0 && left < right ) left ++ ; while ( arr [ right ] == 1 && left < right ) right -- ; if ( left < right ) { arr [ left ] = 0 ; arr [ right ] = 1 ; left ++ ; right -- ; } } } public static void main ( String [ ] args ) { Segregate seg = new Segregate ( ) ; int arr [ ] = new int [ ] { 0 , 1 , 0 , 1 , 1 , 1 } ; int i , arr_size = arr . length ; seg . segregate0and1 ( arr , arr_size ) ; System . out . print ( " Array ▁ after ▁ segregation ▁ is ▁ " ) ; for ( i = 0 ; i < 6 ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }

def segregate0and1 ( arr , size ) : NEW_LINE INDENT left , right = 0 , size - 1 NEW_LINE while left < right : NEW_LINE INDENT while arr [ left ] == 0 and left < right : NEW_LINE INDENT left += 1 NEW_LINE DEDENT while arr [ right ] == 1 and left < right : NEW_LINE INDENT right -= 1 NEW_LINE DEDENT if left < right : NEW_LINE INDENT arr [ left ] = 0 NEW_LINE arr [ right ] = 1 NEW_LINE left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT DEDENT return arr NEW_LINE DEDENT arr = [ 0 , 1 , 0 , 1 , 1 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE print ( " Array ▁ after ▁ segregation " ) NEW_LINE print ( segregate0and1 ( arr , arr_size ) ) NEW_LINE


import java . util . ArrayList ; class Interval { int buy , sell ; } class StockBuySell { void stockBuySell ( int price [ ] , int n ) { if ( n == 1 ) return ; int count = 0 ; ArrayList < Interval > sol = new ArrayList < Interval > ( ) ; int i = 0 ; while ( i < n - 1 ) { while ( ( i < n - 1 ) && ( price [ i + 1 ] <= price [ i ] ) ) i ++ ; if ( i == n - 1 ) break ; Interval e = new Interval ( ) ; e . buy = i ++ ; while ( ( i < n ) && ( price [ i ] >= price [ i - 1 ] ) ) i ++ ; e . sell = i - 1 ; sol . add ( e ) ; count ++ ; } if ( count == 0 ) System . out . println ( " There ▁ is ▁ no ▁ day ▁ when ▁ buying ▁ the ▁ stock ▁ " + " will ▁ make ▁ profit " ) ; else for ( int j = 0 ; j < count ; j ++ ) System . out . println ( " Buy ▁ on ▁ day : ▁ " + sol . get ( j ) . buy + " ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ " + " Sell ▁ on ▁ day ▁ : ▁ " + sol . get ( j ) . sell ) ; return ; } public static void main ( String args [ ] ) { StockBuySell stock = new StockBuySell ( ) ; int price [ ] = { 100 , 180 , 260 , 310 , 40 , 535 , 695 } ; int n = price . length ; stock . stockBuySell ( price , n ) ; } }

def stockBuySell ( price , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return NEW_LINE DEDENT i = 0 NEW_LINE while ( i < ( n - 1 ) ) : NEW_LINE INDENT while ( ( i < ( n - 1 ) ) and ( price [ i + 1 ] <= price [ i ] ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i == n - 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT buy = i NEW_LINE i += 1 NEW_LINE while ( ( i < n ) and ( price [ i ] >= price [ i - 1 ] ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT sell = i - 1 NEW_LINE print ( " Buy ▁ on ▁ day : ▁ " , buy , " \t " , " Sell ▁ on ▁ day : ▁ " , sell ) NEW_LINE DEDENT DEDENT price = [ 100 , 180 , 260 , 310 , 40 , 535 , 695 ] NEW_LINE n = len ( price ) NEW_LINE stockBuySell ( price , n ) NEW_LINE


import java . io . * ; class GFG { static int digitSum ( long n ) { int digSum = 0 ; while ( n > 0 ) { digSum += n % 10 ; n /= 10 ; } return digSum ; } public static long countInteger ( long n , long s ) { if ( n < s ) return 0 ; for ( long i = s ; i <= Math . min ( n , s + 163 ) ; i ++ ) if ( ( i - digitSum ( i ) ) > s ) return ( n - i + 1 ) ; return 0 ; } public static void main ( String args [ ] ) { long n = 1000 , s = 100 ; System . out . println ( countInteger ( n , s ) ) ; } }

def digitSum ( n ) : NEW_LINE INDENT digSum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digSum += n % 10 NEW_LINE n //= 10 NEW_LINE DEDENT return digSum NEW_LINE DEDENT def countInteger ( n , s ) : NEW_LINE INDENT if ( n < s ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( s , min ( n , s + 163 ) + 1 ) : NEW_LINE INDENT if ( ( i - digitSum ( i ) ) > s ) : NEW_LINE INDENT return ( n - i + 1 ) NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT n = 1000 NEW_LINE s = 100 NEW_LINE print ( countInteger ( n , s ) ) NEW_LINE