import java . util . * ; class Count_Flips { public static int count_flips ( int a [ ] , int n , int k ) { boolean set = false ; int ans = 0 , min_diff = Integer . MAX_VALUE ; for ( int i = 0 ; i < k ; i ++ ) { int curr_index = i , segment = 0 , count_zero = 0 , count_one = 0 ; while ( curr_index < n ) { if ( segment % 2 == 0 ) { if ( a [ curr_index ] == 1 ) count_zero ++ ; else count_one ++ ; } else { if ( a [ curr_index ] == 0 ) count_zero ++ ; else count_one ++ ; } curr_index = curr_index + k ; segment ++ ; } ans += Math . min ( count_one , count_zero ) ; if ( count_one < count_zero ) set = ! set ; min_diff = Math . min ( min_diff , Math . abs ( count_zero - count_one ) ) ; } if ( set ) return ans ; else return ans + min_diff ; } public static void main ( String [ ] args ) { int n = 6 , k = 3 ; int a [ ] = { 0 , 0 , 1 , 1 , 0 , 0 } ; System . out . println ( count_flips ( a , n , k ) ) ; } }

def count_flips ( a , n , k ) : NEW_LINE INDENT min_diff , ans , set = n , 0 , False NEW_LINE for i in range ( k ) : NEW_LINE INDENT curr_index , segment , \ NEW_LINE count_zero , count_one = \ NEW_LINE INDENT i , 0 , 0 , 0 NEW_LINE DEDENT while curr_index < n : NEW_LINE INDENT if segment % 2 == 0 : NEW_LINE INDENT if a [ curr_index ] == 1 : NEW_LINE INDENT count_zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_one += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if a [ curr_index ] == 0 : NEW_LINE INDENT count_zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_one += 1 NEW_LINE DEDENT DEDENT curr_index += k NEW_LINE segment += 1 NEW_LINE DEDENT ans += min ( count_zero , count_one ) NEW_LINE if count_one < count_zero : NEW_LINE INDENT set = not set NEW_LINE DEDENT min_diff = min ( min_diff , \ abs ( count_zero - count_one ) ) NEW_LINE DEDENT if set : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return ans + min_diff NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 6 , 3 NEW_LINE a = [ 0 , 0 , 1 , 1 , 0 , 0 ] NEW_LINE print ( count_flips ( a , n , k ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int findCountOfSolutions ( int n , int p ) { int ans = 0 ; for ( int x = 1 ; x < p ; x ++ ) { if ( ( x * x ) % p == 1 ) { int last = x + p * ( n / p ) ; if ( last > n ) last -= p ; ans += ( ( last - x ) / p + 1 ) ; } } return ans ; } public static void main ( String [ ] args ) { int n = 10 ; int p = 5 ; System . out . println ( findCountOfSolutions ( n , p ) ) ; } }

def findCountOfSolutions ( n , p ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for x in range ( 1 , p ) : NEW_LINE INDENT if ( ( x * x ) % p == 1 ) : NEW_LINE INDENT last = x + p * ( n / p ) ; NEW_LINE if ( last > n ) : NEW_LINE INDENT last -= p ; NEW_LINE DEDENT ans += ( ( last - x ) / p + 1 ) ; NEW_LINE DEDENT DEDENT return int ( ans ) ; NEW_LINE DEDENT n = 10 ; NEW_LINE p = 5 ; NEW_LINE print ( findCountOfSolutions ( n , p ) ) ; NEW_LINE


import java . io . * ; class GFG { static int contiguousPerfectSquare ( int [ ] arr , int n ) { int a ; float b ; int current_length = 0 ; int max_length = 0 ; for ( int i = 0 ; i < n ; i ++ ) { b = ( float ) Math . sqrt ( arr [ i ] ) ; a = ( int ) b ; if ( a == b ) current_length ++ ; else current_length = 0 ; max_length = Math . max ( max_length , current_length ) ; } return max_length ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 75 , 4 , 64 , 121 , 25 } ; int n = arr . length ; System . out . print ( contiguousPerfectSquare ( arr , n ) ) ; } }

from math import sqrt NEW_LINE def contiguousPerfectSquare ( arr , n ) : NEW_LINE INDENT current_length = 0 NEW_LINE max_length = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT b = sqrt ( arr [ i ] ) NEW_LINE a = int ( b ) NEW_LINE if ( a == b ) : NEW_LINE INDENT current_length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT current_length = 0 NEW_LINE DEDENT max_length = max ( max_length , current_length ) NEW_LINE DEDENT return max_length NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 75 , 4 , 64 , 121 , 25 ] NEW_LINE n = len ( arr ) NEW_LINE print ( contiguousPerfectSquare ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GfG { static int longSubarrWthSumDivByK ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int mod_arr [ ] = new int [ n ] ; int max = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { curr_sum += arr [ i ] ; mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k ; } for ( int i = 0 ; i < n ; i ++ ) { if ( mod_arr [ i ] == 0 ) max = i + 1 ; else if ( um . containsKey ( mod_arr [ i ] ) == false ) um . put ( mod_arr [ i ] , i ) ; else if ( max < ( i - um . get ( mod_arr [ i ] ) ) ) max = i - um . get ( mod_arr [ i ] ) ; } return max ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 6 , 1 , 4 , 5 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Length ▁ = ▁ " + longSubarrWthSumDivByK ( arr , n , k ) ) ; } }

def longSubarrWthSumDivByK ( arr , n , k ) : NEW_LINE INDENT um = { i : 0 for i in range ( 8 ) } NEW_LINE mod_arr = [ 0 for i in range ( n ) ] NEW_LINE max = 0 NEW_LINE curr_sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT curr_sum += arr [ i ] NEW_LINE mod_arr [ i ] = ( ( curr_sum % k ) + k ) % k NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( mod_arr [ i ] == 0 ) : NEW_LINE INDENT max = i + 1 NEW_LINE DEDENT elif ( mod_arr [ i ] in um ) : NEW_LINE INDENT um [ mod_arr [ i ] ] = i NEW_LINE DEDENT else : NEW_LINE INDENT if ( max < ( i - um [ mod_arr [ i ] ] ) ) : NEW_LINE INDENT max = i - um [ mod_arr [ i ] ] NEW_LINE DEDENT DEDENT DEDENT return max NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 7 , 6 , 1 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( " Length ▁ = " , longSubarrWthSumDivByK ( arr , n , k ) ) NEW_LINE DEDENT


class GFG { static int countIndices ( int arr [ ] , int n ) { int cnt = 0 ; int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( max < arr [ i ] ) { max = arr [ i ] ; cnt ++ ; } } return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; System . out . println ( countIndices ( arr , n ) ) ; } }

def countIndices ( arr , n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE max = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( max < arr [ i ] ) : NEW_LINE INDENT max = arr [ i ] ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countIndices ( arr , n ) ) ; NEW_LINE DEDENT


class solution { static int countWords ( String str ) { int count = 1 ; for ( int i = 1 ; i < str . length ( ) - 1 ; i ++ ) { if ( str . charAt ( i ) >= 65 && str . charAt ( i ) <= 90 ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String str = " geeksForGeeks " ; System . out . print ( countWords ( str ) ) ; } }

def countWords ( str ) : NEW_LINE INDENT count = 1 NEW_LINE for i in range ( 1 , len ( str ) - 1 ) : NEW_LINE INDENT if ( str [ i ] . isupper ( ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT str = " geeksForGeeks " ; NEW_LINE print ( countWords ( str ) ) NEW_LINE


class GFG { static boolean isPerfect ( int N ) { if ( ( Math . sqrt ( N ) - Math . floor ( Math . sqrt ( N ) ) ) != 0 ) return false ; return true ; } static void getClosestPerfectSquare ( int N ) { if ( isPerfect ( N ) ) { System . out . println ( N + " ▁ " + "0" ) ; return ; } int aboveN = - 1 , belowN = - 1 ; int n1 ; n1 = N + 1 ; while ( true ) { if ( isPerfect ( n1 ) ) { aboveN = n1 ; break ; } else n1 ++ ; } n1 = N - 1 ; while ( true ) { if ( isPerfect ( n1 ) ) { belowN = n1 ; break ; } else n1 -- ; } int diff1 = aboveN - N ; int diff2 = N - belowN ; if ( diff1 > diff2 ) System . out . println ( belowN + " ▁ " + diff2 ) ; else System . out . println ( aboveN + " ▁ " + diff1 ) ; } public static void main ( String args [ ] ) { int N = 1500 ; getClosestPerfectSquare ( N ) ; } }

from math import sqrt , floor NEW_LINE def isPerfect ( N ) : NEW_LINE INDENT if ( sqrt ( N ) - floor ( sqrt ( N ) ) != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def getClosestPerfectSquare ( N ) : NEW_LINE INDENT if ( isPerfect ( N ) ) : NEW_LINE INDENT print ( N , "0" ) NEW_LINE return NEW_LINE DEDENT aboveN = - 1 NEW_LINE belowN = - 1 NEW_LINE n1 = 0 NEW_LINE n1 = N + 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPerfect ( n1 ) ) : NEW_LINE INDENT aboveN = n1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT n1 += 1 NEW_LINE DEDENT DEDENT n1 = N - 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPerfect ( n1 ) ) : NEW_LINE INDENT belowN = n1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT n1 -= 1 NEW_LINE DEDENT DEDENT diff1 = aboveN - N NEW_LINE diff2 = N - belowN NEW_LINE if ( diff1 > diff2 ) : NEW_LINE INDENT print ( belowN , diff2 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( aboveN , diff1 ) NEW_LINE DEDENT DEDENT N = 1500 NEW_LINE getClosestPerfectSquare ( N ) NEW_LINE


import java . io . * ; class GFG { static void newvol ( double x ) { System . out . print ( " percentage ▁ increase ▁ " + " in ▁ the ▁ volume ▁ of ▁ the ▁ cylinder ▁ is ▁ " + x + " % " ) ; } public static void main ( String [ ] args ) { double x = 10 ; newvol ( x ) ; } }

def newvol ( x ) : NEW_LINE INDENT print ( " percentage ▁ increase ▁ in ▁ the ▁ volume ▁ of ▁ the ▁ cylinder ▁ is ▁ " , x , " % " ) NEW_LINE DEDENT x = 10.0 NEW_LINE newvol ( x ) NEW_LINE


class GFG { static int largestNumber ( int n ) { String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( ( ( int ) ( s . charAt ( i ) - '0' ) % 2 & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) return duplicate ; int num = 0 ; for ( int i = 0 ; i < index ; i ++ ) num = num * 10 + ( int ) ( s . charAt ( i ) - '0' ) ; num = num * 10 + ( ( int ) s . charAt ( index ) - ( int ) ( '0' ) - 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) num = num * 10 + 9 ; return num ; } public static void main ( String [ ] args ) { int N = 24578 ; System . out . println ( largestNumber ( N ) ) ; } }

def largestNumber ( n ) : NEW_LINE INDENT s = " " NEW_LINE duplicate = n NEW_LINE while ( n ) : NEW_LINE INDENT s = chr ( n % 10 + 48 ) + s NEW_LINE n //= 10 NEW_LINE DEDENT index = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ( ( ord ( s [ i ] ) - ord ( '0' ) ) % 2 & 1 ) == 0 ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return duplicate NEW_LINE DEDENT num = 0 NEW_LINE for i in range ( index ) : NEW_LINE INDENT num = num * 10 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT num = num * 10 + ( ord ( s [ index ] ) - ord ( '0' ) - 1 ) NEW_LINE for i in range ( index + 1 , len ( s ) ) : NEW_LINE INDENT num = num * 10 + 9 NEW_LINE DEDENT return num NEW_LINE DEDENT N = 24578 NEW_LINE print ( largestNumber ( N ) ) NEW_LINE


import java . io . * ; class GFG { public static void main ( String [ ] args ) { double cm , meter , kilometer ; cm = 1000 ; meter = cm / 100.0 ; kilometer = cm / 100000.0 ; System . out . println ( " Length ▁ in ▁ meter ▁ = ▁ " + meter + " m " ) ; System . out . println ( " Length ▁ in ▁ Kilometer ▁ = ▁ " + kilometer + " km " ) ; } }

cm = 1000 ; NEW_LINE meter = cm / 100.0 ; NEW_LINE kilometer = cm / 100000.0 ; NEW_LINE print ( " Length ▁ in ▁ meter ▁ = ▁ " , meter , " m " ) ; NEW_LINE print ( " Length ▁ in ▁ Kilometer ▁ = ▁ " , kilometer , " km " ) ; NEW_LINE


import java . util . * ; class GFG { static Queue < Integer > reverse ( Queue < Integer > q ) { int s = q . size ( ) ; Queue < Integer > ans = new LinkedList < > ( ) ; for ( int i = 0 ; i < s ; i ++ ) { for ( int j = 0 ; j < q . size ( ) - 1 ; j ++ ) { int x = q . peek ( ) ; q . remove ( ) ; q . add ( x ) ; } ans . add ( q . peek ( ) ) ; q . remove ( ) ; } return ans ; } public static void main ( String [ ] args ) { Queue < Integer > q = new LinkedList < > ( ) ; q . add ( 1 ) ; q . add ( 2 ) ; q . add ( 3 ) ; q . add ( 4 ) ; q . add ( 5 ) ; q = reverse ( q ) ; while ( ! q . isEmpty ( ) ) { System . out . print ( q . peek ( ) + " ▁ " ) ; q . remove ( ) ; } } }

from collections import deque NEW_LINE def reverse ( q ) : NEW_LINE INDENT s = len ( q ) NEW_LINE ans = deque ( ) NEW_LINE for i in range ( s ) : NEW_LINE INDENT for j in range ( s - 1 ) : NEW_LINE INDENT x = q . popleft ( ) NEW_LINE q . appendleft ( x ) NEW_LINE DEDENT ans . appendleft ( q . popleft ( ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT q = deque ( ) NEW_LINE q . append ( 1 ) NEW_LINE q . append ( 2 ) NEW_LINE q . append ( 3 ) NEW_LINE q . append ( 4 ) NEW_LINE q . append ( 5 ) NEW_LINE q = reverse ( q ) NEW_LINE while ( len ( q ) > 0 ) : NEW_LINE INDENT print ( q . popleft ( ) , end = " ▁ " ) NEW_LINE DEDENT


class GFG { static boolean Check_is_possible ( int l , int r , int k ) { int div_count = ( r / k ) - ( l / k ) ; if ( l % k == 0 ) { div_count ++ ; } return ( div_count > 1 ) ; } public static void main ( String [ ] args ) { int l = 30 , r = 70 , k = 10 ; if ( Check_is_possible ( l , r , k ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

def Check_is_possible ( l , r , k ) : NEW_LINE INDENT div_count = ( r // k ) - ( l // k ) NEW_LINE if l % k == 0 : NEW_LINE INDENT div_count += 1 NEW_LINE DEDENT return div_count > 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , r , k = 30 , 70 , 10 NEW_LINE if Check_is_possible ( l , r , k ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class Node { static boolean checkUnimodal ( int arr [ ] , int n ) { int i = 1 ; while ( i < n && arr [ i ] > arr [ i - 1 ] ) ++ i ; while ( i < n && arr [ i ] == arr [ i - 1 ] ) ++ i ; while ( i < n && arr [ i ] < arr [ i - 1 ] ) ++ i ; return ( i == n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 5 , 5 , 4 , 2 } ; int n = arr . length ; if ( checkUnimodal ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def checkUnimodal ( arr , n ) : NEW_LINE INDENT i = 1 NEW_LINE while ( i < n and arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while ( i < n and arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while ( i < n and arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT return ( i == n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 5 , 5 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE if ( checkUnimodal ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static int countSubstrs ( String str , int i , int j , int n ) { if ( n == 1 ) return 1 ; if ( n <= 0 ) return 0 ; int res = countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ; if ( str . charAt ( i ) == str . charAt ( j ) ) res ++ ; return res ; } public static void main ( String [ ] args ) { String str = " abcab " ; int n = str . length ( ) ; System . out . print ( countSubstrs ( str , 0 , n - 1 , n ) ) ; } }

def countSubstrs ( str , i , j , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = ( countSubstrs ( str , i + 1 , j , n - 1 ) + countSubstrs ( str , i , j - 1 , n - 1 ) - countSubstrs ( str , i + 1 , j - 1 , n - 2 ) ) NEW_LINE if ( str [ i ] == str [ j ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT str = " abcab " NEW_LINE n = len ( str ) NEW_LINE print ( countSubstrs ( str , 0 , n - 1 , n ) ) NEW_LINE


import java . lang . Math ; class gfg { static float sph ( float r , float R , float h ) { if ( r < 0 && R < 0 && h < 0 ) return - 1 ; float x = r ; float V = ( float ) ( 4 * 3.14f * Math . pow ( r , 3 ) ) / 3 ; return V ; } public static void main ( String [ ] args ) { float r = 5 , R = 8 , h = 11 ; System . out . println ( sph ( r , R , h ) ) ; } }

import math as mt NEW_LINE def sph ( r , R , h ) : NEW_LINE INDENT if ( r < 0 and R < 0 and h < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = r NEW_LINE V = ( 4 * 3.14 * pow ( r , 3 ) ) / 3 NEW_LINE return V NEW_LINE DEDENT r , R , h = 5 , 8 , 11 NEW_LINE print ( sph ( r , R , h ) ) NEW_LINE


class GFG { static boolean isPowerOfK ( int n , int k ) { boolean oneSeen = false ; while ( n > 0 ) { int digit = n % k ; if ( digit > 1 ) return false ; if ( digit == 1 ) { if ( oneSeen ) return false ; oneSeen = true ; } n /= k ; } return true ; } public static void main ( String [ ] args ) { int n = 64 , k = 4 ; if ( isPowerOfK ( n , k ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def isPowerOfK ( n , k ) : NEW_LINE INDENT oneSeen = False NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % k NEW_LINE if ( digit > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( digit == 1 ) : NEW_LINE INDENT if ( oneSeen ) : NEW_LINE INDENT return False NEW_LINE DEDENT oneSeen = True NEW_LINE DEDENT n //= k NEW_LINE DEDENT return True NEW_LINE DEDENT n = 64 NEW_LINE k = 4 NEW_LINE if ( isPowerOfK ( n , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void findMaxFrequency ( String s ) { int arr [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) arr [ s . charAt ( i ) - ' a ' ] ++ ; HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < 26 ; i ++ ) if ( arr [ i ] != 0 ) { if ( hash . containsKey ( arr [ i ] ) ) { hash . put ( arr [ i ] , hash . get ( arr [ i ] ) + 1 ) ; } else { hash . put ( arr [ i ] , 1 ) ; } } int max_count = 0 , res = - 1 ; for ( Map . Entry < Integer , Integer > i : hash . entrySet ( ) ) { if ( max_count < i . getValue ( ) ) { res = i . getKey ( ) ; max_count = i . getValue ( ) ; } } System . out . println ( " Frequency ▁ " + res + " ▁ is ▁ repeated ▁ " + max_count + " ▁ times " ) ; } public static void main ( String [ ] args ) { String s = " geeksgeeks " ; findMaxFrequency ( s ) ; } }

def findMaxFrequency ( s ) : NEW_LINE INDENT arr = [ 0 ] * 26 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT arr [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT hash = { } NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( arr [ i ] != 0 ) : NEW_LINE INDENT if arr [ i ] not in hash : NEW_LINE INDENT hash [ arr [ i ] ] = 0 NEW_LINE DEDENT hash [ arr [ i ] ] += 1 NEW_LINE DEDENT DEDENT max_count = 0 NEW_LINE res = - 1 NEW_LINE for i in hash : NEW_LINE INDENT if ( max_count < hash [ i ] ) : NEW_LINE INDENT res = i NEW_LINE max_count = hash [ i ] NEW_LINE DEDENT DEDENT print ( " Frequency " , res , " is ▁ repeated " , max_count , " times " ) NEW_LINE DEDENT s = " geeksgeeks " NEW_LINE findMaxFrequency ( s ) NEW_LINE


class GFG { static int maximum ( int x , int y ) { return ( ( x + y + Math . abs ( x - y ) ) / 2 ) ; } static int minimum ( int x , int y ) { return ( ( x + y - Math . abs ( x - y ) ) / 2 ) ; } public static void main ( String [ ] args ) { int x = 99 , y = 18 ; System . out . println ( " Maximum : ▁ " + maximum ( x , y ) ) ; System . out . println ( " Minimum : ▁ " + minimum ( x , y ) ) ; } }

def maximum ( x , y ) : NEW_LINE INDENT return ( ( x + y + abs ( x - y ) ) // 2 ) NEW_LINE DEDENT def minimum ( x , y ) : NEW_LINE INDENT return ( ( x + y - abs ( x - y ) ) // 2 ) NEW_LINE DEDENT x = 99 NEW_LINE y = 18 NEW_LINE print ( " Maximum : " , maximum ( x , y ) ) NEW_LINE print ( " Minimum : " , minimum ( x , y ) ) NEW_LINE


class GFG { static boolean search ( int arr [ ] , int n , int x ) { int front = 0 , back = n - 1 ; while ( front <= back ) { if ( arr [ front ] == x || arr [ back ] == x ) return true ; front ++ ; back -- ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 } ; int x = 130 ; int n = arr . length ; if ( search ( arr , n , x ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def search ( arr , n , x ) : NEW_LINE INDENT front = 0 ; back = n - 1 NEW_LINE while ( front <= back ) : NEW_LINE INDENT if ( arr [ front ] == x or arr [ back ] == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT front += 1 NEW_LINE back -= 1 NEW_LINE DEDENT return False NEW_LINE DEDENT arr = [ 10 , 20 , 80 , 30 , 60 , 50 , 110 , 100 , 130 , 170 ] NEW_LINE x = 130 NEW_LINE n = len ( arr ) NEW_LINE if ( search ( arr , n , x ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static int checkIfStartsWithVowels ( char [ ] str ) { if ( ! ( str [ 0 ] == ' A ' || str [ 0 ] == ' a ' || str [ 0 ] == ' E ' || str [ 0 ] == ' e ' || str [ 0 ] == ' I ' || str [ 0 ] == ' i ' || str [ 0 ] == ' O ' || str [ 0 ] == ' o ' || str [ 0 ] == ' U ' || str [ 0 ] == ' u ' ) ) return 1 ; else return 0 ; } static void check ( String str ) { if ( checkIfStartsWithVowels ( str . toCharArray ( ) ) == 1 ) System . out . print ( " Not ▁ Accepted \n " ) ; else System . out . print ( " Accepted \n " ) ; } public static void main ( String [ ] args ) { String str = " animal " ; check ( str ) ; str = " zebra " ; check ( str ) ; } }

def checkIfStartsWithVowels ( string ) : NEW_LINE INDENT if ( not ( string [ 0 ] == ' A ' or string [ 0 ] == ' a ' or string [ 0 ] == ' E ' or string [ 0 ] == ' e ' or string [ 0 ] == ' I ' or string [ 0 ] == ' i ' or string [ 0 ] == ' O ' or string [ 0 ] == ' o ' or string [ 0 ] == ' U ' or string [ 0 ] == ' u ' ) ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT def check ( string ) : NEW_LINE INDENT if ( checkIfStartsWithVowels ( string ) ) : NEW_LINE INDENT print ( " Not ▁ Accepted " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Accepted " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " animal " ; NEW_LINE check ( string ) ; NEW_LINE string = " zebra " ; NEW_LINE check ( string ) ; NEW_LINE DEDENT


class GFG { static void removeElements ( int [ ] arr , int n ) { int [ ] brr = new int [ n ] ; int l = 1 ; brr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( brr [ l - 1 ] <= arr [ i ] ) { brr [ l ] = arr [ i ] ; l ++ ; } } for ( int i = 0 ; i < l ; i ++ ) System . out . print ( brr [ i ] + " ▁ " ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 10 , 12 , 9 , 10 , 2 , 13 , 14 } ; int n = arr . length ; removeElements ( arr , n ) ; } }

def removeElements ( arr , n ) : NEW_LINE INDENT brr = [ 0 ] * n ; l = 1 ; NEW_LINE brr [ 0 ] = arr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( brr [ l - 1 ] <= arr [ i ] ) : NEW_LINE INDENT brr [ l ] = arr [ i ] ; NEW_LINE l += 1 ; NEW_LINE DEDENT DEDENT for i in range ( l ) : NEW_LINE INDENT print ( brr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 12 , 9 , 10 , 2 , 13 , 14 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE removeElements ( arr , n ) ; NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; public class GFG { private static final int MAX = 0 ; public static void leyland ( int n ) { List < Integer > ans = new ArrayList < Integer > ( ) ; for ( int x = 2 ; x <= n ; x ++ ) { for ( int y = 2 ; y <= x ; y ++ ) { int temp = ( int ) Math . pow ( x , y ) + ( int ) Math . pow ( y , x ) ; ans . add ( temp ) ; } } Collections . sort ( ans ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 6 ; leyland ( n ) ; } }

import math NEW_LINE def leyland ( n ) : NEW_LINE INDENT ans = [ ] NEW_LINE x = 2 NEW_LINE y = 2 NEW_LINE while x <= n : NEW_LINE INDENT y = 2 NEW_LINE while y <= x : NEW_LINE INDENT temp = pow ( x , y ) + pow ( y , x ) NEW_LINE ans . append ( temp ) ; NEW_LINE y = y + 1 NEW_LINE DEDENT x = x + 1 NEW_LINE DEDENT ans . sort ( ) ; NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE i = i + 1 NEW_LINE DEDENT DEDENT n = 6 NEW_LINE leyland ( n ) NEW_LINE


class GFG { static class Node { int data ; Node left ; Node right ; } ; static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; return node ; } static void inorder ( Node root ) { if ( root == null ) return ; inorder ( root . left ) ; System . out . print ( root . data + " ▁ " ) ; inorder ( root . right ) ; } static int isIdentical ( Node root1 , Node root2 ) { if ( root1 == null && root2 == null ) return 1 ; else if ( root1 != null && root2 == null ) return 0 ; else if ( root1 == null && root2 != null ) return 0 ; else { if ( root1 . data == root2 . data && isIdentical ( root1 . left , root2 . left ) == 1 && isIdentical ( root1 . right , root2 . right ) == 1 ) return 1 ; else return 0 ; } } public static void main ( String [ ] args ) { Node root1 = newNode ( 5 ) ; Node root2 = newNode ( 5 ) ; root1 . left = newNode ( 3 ) ; root1 . right = newNode ( 8 ) ; root1 . left . left = newNode ( 2 ) ; root1 . left . right = newNode ( 4 ) ; root2 . left = newNode ( 3 ) ; root2 . right = newNode ( 8 ) ; root2 . left . left = newNode ( 2 ) ; root2 . left . right = newNode ( 4 ) ; if ( isIdentical ( root1 , root2 ) == 1 ) System . out . print ( " Both ▁ BSTs ▁ are ▁ identical " ) ; else System . out . print ( " BSTs ▁ are ▁ not ▁ identical " ) ; } }

class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def inorder ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT inorder ( root . left ) NEW_LINE print ( root . data , end = " ▁ " ) NEW_LINE inorder ( root . right ) NEW_LINE DEDENT def isIdentical ( root1 , root2 ) : NEW_LINE INDENT if ( root1 == None and root2 == None ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( root1 != None and root2 == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( root1 == None and root2 != None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT if ( root1 . data == root2 . data and isIdentical ( root1 . left , root2 . left ) and isIdentical ( root1 . right , root2 . right ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root1 = newNode ( 5 ) NEW_LINE root2 = newNode ( 5 ) NEW_LINE root1 . left = newNode ( 3 ) NEW_LINE root1 . right = newNode ( 8 ) NEW_LINE root1 . left . left = newNode ( 2 ) NEW_LINE root1 . left . right = newNode ( 4 ) NEW_LINE root2 . left = newNode ( 3 ) NEW_LINE root2 . right = newNode ( 8 ) NEW_LINE root2 . left . left = newNode ( 2 ) NEW_LINE root2 . left . right = newNode ( 4 ) NEW_LINE if ( isIdentical ( root1 , root2 ) ) : NEW_LINE INDENT print ( " Both ▁ BSTs ▁ are ▁ identical " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " BSTs ▁ are ▁ not ▁ identical " ) NEW_LINE DEDENT DEDENT


class GFG { static int numberOfPaths ( int m , int n ) { int path = 1 ; for ( int i = n ; i < ( m + n - 1 ) ; i ++ ) { path *= i ; path /= ( i - n + 1 ) ; } return path ; } public static void main ( String [ ] args ) { System . out . println ( numberOfPaths ( 3 , 3 ) ) ; } }

def numberOfPaths ( m , n ) : NEW_LINE INDENT for i in range ( n , ( m + n - 1 ) ) : NEW_LINE INDENT path *= i ; NEW_LINE path //= ( i - n + 1 ) ; NEW_LINE DEDENT return path ; NEW_LINE DEDENT print ( numberOfPaths ( 3 , 3 ) ) ; NEW_LINE


public class GFG { static int countPS ( String str ) { int N = str . length ( ) ; int [ ] [ ] cps = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i < N ; i ++ ) cps [ i ] [ i ] = 1 ; for ( int L = 2 ; L <= N ; L ++ ) { for ( int i = 0 ; i < N ; i ++ ) { int k = L + i - 1 ; if ( k < N ) { if ( str . charAt ( i ) == str . charAt ( k ) ) cps [ i ] [ k ] = cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] + 1 ; else cps [ i ] [ k ] = cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] - cps [ i + 1 ] [ k - 1 ] ; } } } return cps [ 0 ] [ N - 1 ] ; } public static void main ( String args [ ] ) { String str = " abcb " ; System . out . println ( " Total ▁ palindromic ▁ " + " subsequence ▁ are ▁ : ▁ " + countPS ( str ) ) ; } }

def countPS ( str ) : NEW_LINE INDENT N = len ( str ) NEW_LINE cps = [ [ 0 for i in range ( N + 2 ) ] for j in range ( N + 2 ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT cps [ i ] [ i ] = 1 NEW_LINE DEDENT for L in range ( 2 , N + 1 ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT k = L + i - 1 NEW_LINE if ( k < N ) : NEW_LINE INDENT if ( str [ i ] == str [ k ] ) : NEW_LINE INDENT cps [ i ] [ k ] = ( cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] + 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT cps [ i ] [ k ] = ( cps [ i ] [ k - 1 ] + cps [ i + 1 ] [ k ] - cps [ i + 1 ] [ k - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return cps [ 0 ] [ N - 1 ] NEW_LINE DEDENT str = " abcb " NEW_LINE print ( " Total ▁ palindromic ▁ subsequence ▁ are ▁ : ▁ " , countPS ( str ) ) NEW_LINE


import java . util . * ; class GFG { static boolean isSmaller ( String str1 , String str2 ) { int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; if ( n1 < n2 ) return true ; if ( n2 < n1 ) return false ; for ( int i = 0 ; i < n1 ; i ++ ) if ( str1 . charAt ( i ) < str2 . charAt ( i ) ) return true ; else if ( str1 . charAt ( i ) > str2 . charAt ( i ) ) return false ; return false ; } static String findDiff ( String str1 , String str2 ) { if ( isSmaller ( str1 , str2 ) ) { String t = str1 ; str1 = str2 ; str2 = t ; } String str = " " ; int n1 = str1 . length ( ) , n2 = str2 . length ( ) ; str1 = new StringBuilder ( str1 ) . reverse ( ) . toString ( ) ; str2 = new StringBuilder ( str2 ) . reverse ( ) . toString ( ) ; int carry = 0 ; for ( int i = 0 ; i < n2 ; i ++ ) { int sub = ( ( int ) ( str1 . charAt ( i ) - '0' ) - ( int ) ( str2 . charAt ( i ) - '0' ) - carry ) ; if ( sub < 0 ) { sub = sub + 10 ; carry = 1 ; } else carry = 0 ; str += ( char ) ( sub + '0' ) ; } for ( int i = n2 ; i < n1 ; i ++ ) { int sub = ( ( int ) ( str1 . charAt ( i ) - '0' ) - carry ) ; if ( sub < 0 ) { sub = sub + 10 ; carry = 1 ; } else carry = 0 ; str += ( char ) ( sub + '0' ) ; } return new StringBuilder ( str ) . reverse ( ) . toString ( ) ; } public static void main ( String [ ] args ) { String str1 = "978" ; String str2 = "12977" ; System . out . println ( findDiff ( str1 , str2 ) ) ; String s1 = "100" ; String s2 = "1000000" ; System . out . println ( findDiff ( s1 , s2 ) ) ; } }

def isSmaller ( str1 , str2 ) : NEW_LINE INDENT n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE if ( n1 < n2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n2 < n1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( n1 ) : NEW_LINE INDENT if ( str1 [ i ] < str2 [ i ] ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( str1 [ i ] > str2 [ i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def findDiff ( str1 , str2 ) : NEW_LINE INDENT if ( isSmaller ( str1 , str2 ) ) : NEW_LINE INDENT temp = str1 NEW_LINE str1 = str2 NEW_LINE str2 = temp NEW_LINE DEDENT str3 = " " NEW_LINE n1 = len ( str1 ) NEW_LINE n2 = len ( str2 ) NEW_LINE str1 = str1 [ : : - 1 ] NEW_LINE str2 = str2 [ : : - 1 ] NEW_LINE carry = 0 NEW_LINE for i in range ( n2 ) : NEW_LINE INDENT sub = ( ( ord ( str1 [ i ] ) - ord ( '0' ) ) - ( ord ( str2 [ i ] ) - ord ( '0' ) ) - carry ) NEW_LINE if ( sub < 0 ) : NEW_LINE INDENT sub = sub + 10 NEW_LINE carry = 1 NEW_LINE DEDENT else : NEW_LINE INDENT carry = 0 NEW_LINE DEDENT str3 = str3 + str ( sub ) NEW_LINE DEDENT for i in range ( n2 , n1 ) : NEW_LINE INDENT sub = ( ( ord ( str1 [ i ] ) - ord ( '0' ) ) - carry ) NEW_LINE if ( sub < 0 ) : NEW_LINE INDENT sub = sub + 10 NEW_LINE carry = 1 NEW_LINE DEDENT else : NEW_LINE INDENT carry = 0 NEW_LINE DEDENT str3 = str3 + str ( sub ) NEW_LINE DEDENT str3 = str3 [ : : - 1 ] NEW_LINE return str3 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = "978" NEW_LINE str2 = "12977" NEW_LINE print ( findDiff ( str1 , str2 ) ) NEW_LINE s1 = "100" NEW_LINE s2 = "1000000" NEW_LINE print ( findDiff ( s1 , s2 ) ) NEW_LINE DEDENT


class GFG { static int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } static int repeatedNumberSum ( int n , int x ) { int sum = x * digSum ( n ) ; return digSum ( sum ) ; } public static void main ( String [ ] args ) { int n = 24 , x = 3 ; System . out . println ( repeatedNumberSum ( n , x ) ) ; } }

def digSum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( n % 9 == 0 ) and 9 or ( n % 9 ) NEW_LINE DEDENT def repeatedNumberSum ( n , x ) : NEW_LINE INDENT sum = x * digSum ( n ) NEW_LINE return digSum ( sum ) NEW_LINE DEDENT n = 24 ; x = 3 NEW_LINE print ( repeatedNumberSum ( n , x ) ) NEW_LINE


import java . io . * ; class GFG { static int bitCount ( int n ) { int count = 0 ; while ( n > 0 ) { if ( ( n & 1 ) > 0 ) ++ count ; n >>= 1 ; } return count ; } static long countPairsWithKDiff ( int arr [ ] , int n , int k ) { long ans = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { int xoredNum = arr [ i ] ^ arr [ j ] ; if ( k == bitCount ( xoredNum ) ) ++ ans ; } } return ans ; } public static void main ( String [ ] args ) { int k = 2 ; int arr [ ] = { 2 , 4 , 1 , 3 , 1 } ; int n = arr . length ; System . out . println ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ " + countPairsWithKDiff ( arr , n , k ) + " \n " ) ; } }

def bitCount ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT n >>= 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def countPairsWithKDiff ( arr , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , n - 1 , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT xoredNum = arr [ i ] ^ arr [ j ] NEW_LINE if ( k == bitCount ( xoredNum ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT k = 2 NEW_LINE arr = [ 2 , 4 , 1 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Total ▁ pairs ▁ for ▁ k ▁ = " , k , " are " , countPairsWithKDiff ( arr , n , k ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void odd_even ( int arr [ ] , int n ) { Vector < Integer > odd_indexes = new Vector < Integer > ( ) ; Vector < Integer > even_indexes = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) odd_indexes . add ( arr [ i ] ) ; else even_indexes . add ( arr [ i ] ) ; } Collections . sort ( odd_indexes ) ; Collections . sort ( even_indexes ) ; for ( int i = 0 ; i < odd_indexes . size ( ) ; i ++ ) System . out . print ( odd_indexes . get ( i ) + " ▁ " ) ; for ( int i = 0 ; i < even_indexes . size ( ) ; i ++ ) System . out . print ( even_indexes . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 7 , 6 , 8 } ; int n = arr . length ; odd_even ( arr , n ) ; } }

def odd_even ( n ) : NEW_LINE INDENT odd_indexes = [ ] NEW_LINE even_indexes = [ ] NEW_LINE for i in range ( len ( n ) ) : NEW_LINE INDENT if i % 2 == 0 : odd_indexes . append ( n [ i ] ) NEW_LINE else : even_indexes . append ( n [ i ] ) NEW_LINE DEDENT for i in sorted ( odd_indexes ) : print ( i , end = " ▁ " ) NEW_LINE for i in sorted ( even_indexes ) : print ( i , end = " ▁ " ) NEW_LINE DEDENT n = [ 3 , 2 , 7 , 6 , 8 ] NEW_LINE odd_even ( n ) NEW_LINE


import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . left = null ; temp . right = null ; temp . data = val ; return temp ; } static int maxAtLevel ( Node root , int level ) { if ( root == null ) return 0 ; if ( level == 0 ) return root . data ; int x = maxAtLevel ( root . left , level - 1 ) ; int y = maxAtLevel ( root . right , level - 1 ) ; return Math . max ( x , y ) ; } public static void main ( String args [ ] ) { Node root = null ; root = newNode ( 45 ) ; root . left = newNode ( 46 ) ; root . left . left = newNode ( 18 ) ; root . left . left . left = newNode ( 16 ) ; root . left . left . right = newNode ( 23 ) ; root . left . right = newNode ( 17 ) ; root . left . right . left = newNode ( 24 ) ; root . left . right . right = newNode ( 21 ) ; root . right = newNode ( 15 ) ; root . right . left = newNode ( 22 ) ; root . right . left . left = newNode ( 37 ) ; root . right . left . right = newNode ( 41 ) ; root . right . right = newNode ( 19 ) ; root . right . right . left = newNode ( 49 ) ; root . right . right . right = newNode ( 29 ) ; int level = 3 ; System . out . println ( maxAtLevel ( root , level ) ) ; } }

class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def maxAtLevel ( root , level ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( level == 0 ) : NEW_LINE INDENT return root . data NEW_LINE DEDENT x = maxAtLevel ( root . left , level - 1 ) NEW_LINE y = maxAtLevel ( root . right , level - 1 ) NEW_LINE return max ( x , y ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 45 ) NEW_LINE root . left = newNode ( 46 ) NEW_LINE root . left . left = newNode ( 18 ) NEW_LINE root . left . left . left = newNode ( 16 ) NEW_LINE root . left . left . right = newNode ( 23 ) NEW_LINE root . left . right = newNode ( 17 ) NEW_LINE root . left . right . left = newNode ( 24 ) NEW_LINE root . left . right . right = newNode ( 21 ) NEW_LINE root . right = newNode ( 15 ) NEW_LINE root . right . left = newNode ( 22 ) NEW_LINE root . right . left . left = newNode ( 37 ) NEW_LINE root . right . left . right = newNode ( 41 ) NEW_LINE root . right . right = newNode ( 19 ) NEW_LINE root . right . right . left = newNode ( 49 ) NEW_LINE root . right . right . right = newNode ( 29 ) NEW_LINE level = 3 NEW_LINE print ( maxAtLevel ( root , level ) ) NEW_LINE DEDENT


class GFG { static double sumTillX ( double x , double n ) { double odd = Math . ceil ( n / 2.0 ) ; if ( x <= odd ) return x * x ; double even = x - odd ; return ( ( odd * odd ) + ( even * even ) + even ) ; } static double rangeSum ( double N , double L , double R ) { return sumTillX ( R , N ) - sumTillX ( L - 1 , N ) ; } public static void main ( String args [ ] ) { long N = 10 , L = 1 , R = 6 ; int n = 101 ; System . out . println ( ( int ) rangeSum ( N , L , R ) ) ; } }

import math NEW_LINE def sumTillX ( x , n ) : NEW_LINE INDENT odd = math . ceil ( n / 2.0 ) NEW_LINE if ( x <= odd ) : NEW_LINE INDENT return x * x ; NEW_LINE DEDENT even = x - odd ; NEW_LINE return ( ( odd * odd ) + ( even * even ) + even ) ; NEW_LINE DEDENT def rangeSum ( N , L , R ) : NEW_LINE INDENT return ( sumTillX ( R , N ) - sumTillX ( L - 1 , N ) ) ; NEW_LINE DEDENT N = 10 NEW_LINE L = 1 NEW_LINE R = 6 NEW_LINE print ( rangeSum ( N , L , R ) ) NEW_LINE


import java . io . * ; class GFG { public static int fact ( int N ) { int i , product = 1 ; for ( i = 1 ; i <= N ; i ++ ) product = product * i ; return product ; } public static int nthTerm ( int N ) { return ( N * N ) * fact ( N ) ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }

def fact ( N ) : NEW_LINE INDENT product = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT product = product * i NEW_LINE DEDENT return product NEW_LINE DEDENT def nthTerm ( N ) : NEW_LINE INDENT return ( N * N ) * fact ( N ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT


public class GFG { static void isHalfReducible ( int arr [ ] , int n , int m ) { int frequencyHash [ ] = new int [ m + 1 ] ; int i ; for ( i = 0 ; i < frequencyHash . length ; i ++ ) frequencyHash [ i ] = 0 ; for ( i = 0 ; i < n ; i ++ ) { frequencyHash [ arr [ i ] % ( m + 1 ) ] ++ ; } for ( i = 0 ; i <= m ; i ++ ) { if ( frequencyHash [ i ] >= n / 2 ) break ; } if ( i <= m ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 16 , 32 , 3 , 12 } ; int n = arr . length ; int m = 7 ; isHalfReducible ( arr , n , m ) ; } }

def isHalfReducible ( arr , n , m ) : NEW_LINE INDENT frequencyHash = [ 0 ] * ( m + 1 ) ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT frequencyHash [ ( arr [ i ] % ( m + 1 ) ) ] += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT i = 0 ; NEW_LINE while ( i <= m ) : NEW_LINE INDENT if ( frequencyHash [ i ] >= ( n / 2 ) ) : NEW_LINE INDENT break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT if ( i <= m ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT arr = [ 8 , 16 , 32 , 3 , 12 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE m = 7 ; NEW_LINE isHalfReducible ( arr , n , m ) ; NEW_LINE


import java . util . Arrays ; class GFG { static int MAX = 26 ; static int countPairs ( char [ ] str , int k ) { int n = str . length ; int [ ] freq = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ str [ i ] - ' a ' ] ++ ; } int cnt = 0 ; if ( k == 0 ) { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 1 ) { cnt += ( ( freq [ i ] * ( freq [ i ] - 1 ) ) / 2 ) ; } } } else { for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq [ i ] > 0 && i + k < MAX && freq [ i + k ] > 0 ) { cnt += ( freq [ i ] * freq [ i + k ] ) ; } } ; } return cnt ; } public static void main ( String [ ] args ) { String str = " abcdab " ; int k = 0 ; System . out . println ( countPairs ( str . toCharArray ( ) , k ) ) ; } }

MAX = 26 NEW_LINE def countPairs ( string , k ) : NEW_LINE INDENT n = len ( string ) ; NEW_LINE freq = [ 0 ] * MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ ord ( string [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT cnt = 0 ; NEW_LINE if ( k == 0 ) : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq [ i ] > 1 ) : NEW_LINE INDENT cnt += ( ( freq [ i ] * ( freq [ i ] - 1 ) ) // 2 ) ; NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq [ i ] > 0 and i + k < MAX and freq [ i + k ] > 0 ) : NEW_LINE INDENT cnt += ( freq [ i ] * freq [ i + k ] ) ; NEW_LINE DEDENT DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abcdab " ; NEW_LINE k = 0 ; NEW_LINE print ( countPairs ( string , k ) ) ; NEW_LINE DEDENT


import java . util . HashMap ; import java . util . Map ; class GfG { static int count_pairs ( int n , int a [ ] ) { HashMap < Integer , Integer > frequency = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! frequency . containsKey ( a [ i ] ) ) frequency . put ( a [ i ] , 0 ) ; frequency . put ( a [ i ] , frequency . get ( a [ i ] ) + 1 ) ; } int count = 0 ; for ( Map . Entry < Integer , Integer > x : frequency . entrySet ( ) ) { int f = x . getValue ( ) ; count += f * ( f - 1 ) / 2 ; } return ( ( n * ( n - 1 ) ) / 2 ) - count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 2 , 5 , 7 , 2 , 8 } ; int n = arr . length ; System . out . println ( count_pairs ( n , arr ) ) ; } }

def count_pairs ( n , a ) : NEW_LINE INDENT frequency = dict . fromkeys ( a , 0 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT frequency [ a [ i ] ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for f in frequency . values ( ) : NEW_LINE INDENT count += f * ( f - 1 ) // 2 NEW_LINE DEDENT return ( ( n * ( n - 1 ) ) // 2 ) - count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 5 , 2 , 5 , 7 , 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( count_pairs ( n , arr ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int seriesSum ( int n ) { return ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( seriesSum ( n ) ) ; } }

def seriesSum ( n ) : NEW_LINE INDENT return int ( ( n * ( n + 1 ) * ( n + 2 ) ) / 6 ) NEW_LINE DEDENT n = 4 NEW_LINE print ( seriesSum ( n ) ) NEW_LINE


class GFG { static void findMissing ( int a [ ] , int b [ ] , int n , int m ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < m ; j ++ ) if ( a [ i ] == b [ j ] ) break ; if ( j == m ) System . out . print ( a [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 6 , 3 , 4 , 5 } ; int b [ ] = { 2 , 4 , 3 , 1 , 0 } ; int n = a . length ; int m = b . length ; findMissing ( a , b , n , m ) ; } }

def findMissing ( a , b , n , m ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( a [ i ] == b [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == m - 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 6 , 3 , 4 , 5 ] NEW_LINE b = [ 2 , 4 , 3 , 1 , 0 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE findMissing ( a , b , n , m ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int n = 3 ; static int m = 4 ; static int maxMatrixScore ( int A [ ] [ ] , int K ) { HashMap < Integer , Integer > update = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] [ 0 ] == 0 ) { int ans = 0 ; for ( int j = 1 ; j < m ; ++ j ) ans = ( int ) ( ans + A [ i ] [ j ] * Math . pow ( 2 , m - j - 1 ) ) ; update . put ( ans , i ) ; } } for ( Map . Entry < Integer , Integer > it : update . entrySet ( ) ) if ( K > 0 ) { int idx = it . getValue ( ) ; for ( int j = 0 ; j < m ; ++ j ) A [ idx ] [ j ] = ( A [ idx ] [ j ] + 1 ) % 2 ; K -- ; } int ans = 0 ; for ( int j = 0 ; j < m ; ++ j ) { int zero = 0 , one = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] [ j ] == 0 ) zero ++ ; else one ++ ; } if ( K > 0 && zero > one ) { ans += zero * Math . pow ( 2 , m - j - 1 ) ; K -- ; } else ans += one * Math . pow ( 2 , m - j - 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 0 , 0 , 1 , 1 } , { 1 , 0 , 1 , 0 } , { 1 , 1 , 0 , 0 } } ; int K = 2 ; System . out . print ( maxMatrixScore ( A , K ) ) ; } }

n = 3 NEW_LINE m = 4 NEW_LINE def maxMatrixScore ( A , K ) : NEW_LINE INDENT update = { } NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if A [ i ] [ 0 ] == 0 : NEW_LINE INDENT ans = 0 NEW_LINE for j in range ( 1 , m ) : NEW_LINE INDENT ans = ans + A [ i ] [ j ] * 2 ** ( m - j - 1 ) NEW_LINE DEDENT update [ ans ] = i NEW_LINE DEDENT DEDENT for idx in update . values ( ) : NEW_LINE INDENT for j in range ( 0 , m ) : NEW_LINE INDENT A [ idx ] [ j ] = ( A [ idx ] [ j ] + 1 ) % 2 NEW_LINE DEDENT K -= 1 NEW_LINE if K <= 0 : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for j in range ( 0 , m ) : NEW_LINE INDENT zero , one = 0 , 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if A [ i ] [ j ] == 0 : zero += 1 NEW_LINE else : one += 1 NEW_LINE DEDENT if K > 0 and zero > one : NEW_LINE INDENT ans += zero * 2 ** ( m - j - 1 ) NEW_LINE K -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans += one * 2 ** ( m - j - 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ [ 0 , 0 , 1 , 1 ] , [ 1 , 0 , 1 , 0 ] , [ 1 , 1 , 0 , 0 ] ] NEW_LINE K = 2 NEW_LINE print ( maxMatrixScore ( A , K ) ) NEW_LINE DEDENT


class GFG { static int check ( String s , int K ) { for ( int ch = 0 ; ch < 26 ; ch ++ ) { char c = ( char ) ( ' a ' + ch ) ; int last = - 1 ; boolean found = true ; for ( int i = 0 ; i < K ; i ++ ) if ( s . charAt ( i ) == c ) last = i ; if ( last == - 1 ) continue ; for ( int i = K ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) == c ) last = i ; if ( last <= ( i - K ) ) { found = false ; break ; } } if ( found ) return 1 ; } return 0 ; } static int binarySearch ( String s ) { int low = 1 , high = s . length ( ) ; int ans = 0 ; while ( low <= high ) { int mid = ( high + low ) >> 1 ; if ( check ( s , mid ) == 1 ) { ans = mid ; high = mid - 1 ; } else low = mid + 1 ; } return ans ; } public static void main ( String args [ ] ) { String s = " abcde " ; System . out . println ( binarySearch ( s ) ) ; s = " aaaa " ; System . out . println ( binarySearch ( s ) ) ; } }

def check ( s , K ) : NEW_LINE INDENT for ch in range ( 0 , 26 ) : NEW_LINE INDENT c = chr ( 97 + ch ) NEW_LINE last = - 1 NEW_LINE found = True NEW_LINE for i in range ( 0 , K ) : NEW_LINE INDENT if s [ i ] == c : NEW_LINE INDENT last = i NEW_LINE DEDENT DEDENT if last == - 1 : NEW_LINE INDENT continue NEW_LINE DEDENT for i in range ( K , len ( s ) ) : NEW_LINE INDENT if s [ i ] == c : NEW_LINE INDENT last = i NEW_LINE DEDENT if last <= ( i - K ) : NEW_LINE INDENT found = False NEW_LINE break NEW_LINE DEDENT DEDENT if found : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT def binarySearch ( s ) : NEW_LINE INDENT low , high , ans = 1 , len ( s ) , None NEW_LINE while low <= high : NEW_LINE INDENT mid = ( high + low ) >> 1 NEW_LINE if check ( s , mid ) : NEW_LINE INDENT ans , high = mid , mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abcde " NEW_LINE print ( binarySearch ( s ) ) NEW_LINE s = " aaaa " NEW_LINE print ( binarySearch ( s ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean divisibleBy3 ( String number ) { int sumOfDigit = 0 ; for ( int i = 0 ; i < number . length ( ) ; i ++ ) sumOfDigit += number . charAt ( i ) - '0' ; if ( sumOfDigit % 3 == 0 ) return true ; return false ; } static boolean divisibleBy25 ( String number ) { if ( number . length ( ) < 2 ) return false ; int length = number . length ( ) ; int lastTwo = ( number . charAt ( length - 2 ) - '0' ) * 10 + ( number . charAt ( length - 1 ) - '0' ) ; if ( lastTwo % 25 == 0 ) return true ; return false ; } static boolean divisibleBy75 ( String number ) { if ( divisibleBy3 ( number ) && divisibleBy25 ( number ) ) return true ; return false ; } public static void main ( String [ ] args ) { String number = "754586672150" ; boolean divisible = divisibleBy75 ( number ) ; if ( divisible ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def divisibleBy3 ( number ) : NEW_LINE INDENT sumOfDigit = 0 NEW_LINE for i in range ( 0 , len ( number ) , 1 ) : NEW_LINE INDENT sumOfDigit += ord ( number [ i ] ) - ord ( '0' ) NEW_LINE DEDENT if ( sumOfDigit % 3 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def divisibleBy25 ( number ) : NEW_LINE INDENT if ( len ( number ) < 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT length = len ( number ) NEW_LINE lastTwo = ( ( ord ( number [ length - 2 ] ) - ord ( '0' ) ) * 10 + ( ord ( number [ length - 1 ] ) - ord ( '0' ) ) ) NEW_LINE if ( lastTwo % 25 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def divisibleBy75 ( number ) : NEW_LINE INDENT if ( divisibleBy3 ( number ) and divisibleBy25 ( number ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT number = "754586672150" NEW_LINE divisible = divisibleBy75 ( number ) NEW_LINE if ( divisible ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . lang . * ; class GFG { static double PI = 3.14159265 ; public static double area_inscribed ( double a ) { return ( a * a * ( PI / 12 ) ) ; } public static double perm_inscribed ( double a ) { return ( PI * ( a / Math . sqrt ( 3 ) ) ) ; } public static void main ( String [ ] args ) { double a = 6.0 ; System . out . println ( " Area ▁ of ▁ inscribed ▁ circle ▁ is ▁ : " + area_inscribed ( a ) ) ; System . out . println ( " \n Perimeter ▁ of ▁ inscribed ▁ circle ▁ is ▁ : " + perm_inscribed ( a ) ) ; } }

import math NEW_LINE PI = 3.14159265 NEW_LINE def area_inscribed ( a ) : NEW_LINE INDENT return ( a * a * ( PI / 12 ) ) NEW_LINE DEDENT def perm_inscribed ( a ) : NEW_LINE INDENT return ( PI * ( a / math . sqrt ( 3 ) ) ) NEW_LINE DEDENT a = 6.0 NEW_LINE print ( " Area ▁ of ▁ inscribed ▁ circle ▁ is ▁ : % ▁ f " % area_inscribed ( a ) ) NEW_LINE print ( " \n Perimeter ▁ of ▁ inscribed ▁ circle ▁ is ▁ : % ▁ f " % perm_inscribed ( a ) ) NEW_LINE


class GFG { static int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static boolean checkFriendly ( int n , int m ) { int sumFactors_n = sumofFactors ( n ) ; int sumFactors_m = sumofFactors ( m ) ; int gcd_n = gcd ( n , sumFactors_n ) ; int gcd_m = gcd ( m , sumFactors_m ) ; if ( n / gcd_n == m / gcd_m && sumFactors_n / gcd_n == sumFactors_m / gcd_m ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 6 , m = 28 ; if ( checkFriendly ( n , m ) ) System . out . print ( " Yes \n " ) ; else System . out . print ( " No \n " ) ; } }

import math NEW_LINE def sumofFactors ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , int ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT count = 0 ; curr_sum = 1 ; curr_term = 1 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE n = n // i NEW_LINE curr_term *= i NEW_LINE curr_sum += curr_term NEW_LINE DEDENT res *= curr_sum NEW_LINE DEDENT if ( n >= 2 ) : NEW_LINE INDENT res *= ( 1 + n ) NEW_LINE DEDENT return res NEW_LINE DEDENT 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 checkFriendly ( n , m ) : NEW_LINE INDENT sumFactors_n = sumofFactors ( n ) NEW_LINE sumFactors_m = sumofFactors ( m ) NEW_LINE gcd_n = gcd ( n , sumFactors_n ) NEW_LINE gcd_m = gcd ( m , sumFactors_m ) NEW_LINE if ( n // gcd_n == m // gcd_m and sumFactors_n // gcd_n == sumFactors_m // gcd_m ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 6 ; m = 28 NEW_LINE if ( checkFriendly ( n , m ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int last ; static Vector < Integer > findSubset ( Vector < Integer > temp , int k ) { Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int i = last ; i >= 0 ; i -- ) { int cnt = 0 ; for ( Integer it : temp ) { int bit = it & ( 1 << i ) ; if ( bit > 0 ) cnt ++ ; } if ( cnt >= k ) { for ( Integer it : temp ) { int bit = it & ( 1 << i ) ; if ( bit > 0 ) ans . add ( it ) ; } last = i - 1 ; return ans ; } } return ans ; } static int findMaxiumAnd ( int a [ ] , int n , int k ) { last = 31 ; Vector < Integer > temp1 = new Vector < Integer > ( ) ; Vector < Integer > temp2 = new Vector < Integer > ( ) ; ; for ( int i = 0 ; i < n ; i ++ ) { temp2 . add ( a [ i ] ) ; } while ( ( int ) temp2 . size ( ) >= k ) { temp1 = temp2 ; temp2 = findSubset ( temp2 , k ) ; } int ans = temp1 . get ( 0 ) ; for ( int i = 0 ; i < k ; i ++ ) ans = ans & temp1 . get ( i ) ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 255 , 127 , 31 , 5 , 24 , 37 , 15 } ; int n = a . length ; int k = 4 ; System . out . println ( findMaxiumAnd ( a , n , k ) ) ; } }

last = 31 NEW_LINE def findSubset ( temp , k ) : NEW_LINE INDENT global last NEW_LINE ans = [ ] NEW_LINE for i in range ( last , - 1 , - 1 ) : NEW_LINE INDENT cnt = 0 NEW_LINE for it in temp : NEW_LINE INDENT bit = it & ( 1 << i ) NEW_LINE if ( bit > 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT if ( cnt >= k ) : NEW_LINE INDENT for it in temp : NEW_LINE INDENT bit = it & ( 1 << i ) NEW_LINE if ( bit > 0 ) : NEW_LINE INDENT ans . append ( it ) NEW_LINE DEDENT DEDENT last = i - 1 NEW_LINE return ans NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def findMaxiumAnd ( a , n , k ) : NEW_LINE INDENT global last NEW_LINE temp1 , temp2 , = [ ] , [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT temp2 . append ( a [ i ] ) NEW_LINE DEDENT while len ( temp2 ) >= k : NEW_LINE INDENT temp1 = temp2 NEW_LINE temp2 = findSubset ( temp2 , k ) NEW_LINE DEDENT ans = temp1 [ 0 ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT ans = ans & temp1 [ i ] NEW_LINE DEDENT return ans NEW_LINE DEDENT a = [ 255 , 127 , 31 , 5 , 24 , 37 , 15 ] NEW_LINE n = len ( a ) NEW_LINE k = 4 NEW_LINE print ( findMaxiumAnd ( a , n , k ) ) NEW_LINE


import java . util . * ; class GFG { static final int MAX = 32 ; static long [ ] pow2 = new long [ MAX ] ; static boolean [ ] visited = new boolean [ MAX ] ; static Vector < Long > ans = new Vector < > ( ) ; static void power_2 ( ) { long ans = 1 ; for ( int i = 0 ; i < MAX ; i ++ ) { pow2 [ i ] = ans ; ans *= 2 ; } } static int countSetBits ( long x ) { int setBits = 0 ; while ( x != 0 ) { x = x & ( x - 1 ) ; setBits ++ ; } return setBits ; } static void add ( long num ) { int point = 0 ; long value = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( visited [ i ] ) continue ; else { if ( num % 2 == 1 ) { value += ( 1 << i ) ; } num /= 2 ; } } ans . add ( value ) ; } static void solve ( long n , long k ) { ans . add ( k ) ; int countk = countSetBits ( k ) ; if ( pow2 [ countk ] < n ) { System . out . print ( - 1 ) ; return ; } int count = 0 ; for ( long i = 0 ; i < pow2 [ countk ] - 1 ; i ++ ) { add ( i ) ; count ++ ; if ( count == n ) break ; } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { long n = 3 , k = 5 ; power_2 ( ) ; solve ( n , k ) ; } }

MAX = 32 NEW_LINE pow2 = [ 0 for i in range ( MAX ) ] NEW_LINE visited = [ False for i in range ( MAX ) ] NEW_LINE ans = [ ] NEW_LINE def power_2 ( ) : NEW_LINE INDENT an = 1 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT pow2 [ i ] = an NEW_LINE an *= 2 NEW_LINE DEDENT DEDENT def countSetBits ( x ) : NEW_LINE INDENT setBits = 0 NEW_LINE while ( x != 0 ) : NEW_LINE INDENT x = x & ( x - 1 ) NEW_LINE setBits += 1 NEW_LINE DEDENT return setBits NEW_LINE DEDENT def add ( num ) : NEW_LINE INDENT point = 0 NEW_LINE value = 0 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT if ( visited [ i ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT if ( num & 1 ) : NEW_LINE INDENT value += ( 1 << i ) NEW_LINE DEDENT num = num // 2 NEW_LINE DEDENT DEDENT ans . append ( value ) NEW_LINE DEDENT def solve ( n , k ) : NEW_LINE INDENT ans . append ( k ) NEW_LINE countk = countSetBits ( k ) NEW_LINE if ( pow2 [ countk ] < n ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( pow2 [ countk ] - 1 ) : NEW_LINE INDENT add ( i ) NEW_LINE count += 1 NEW_LINE if ( count == n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 3 NEW_LINE k = 5 NEW_LINE power_2 ( ) NEW_LINE solve ( n , k ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int getRemainder ( int num , int divisor ) { return ( num - divisor * ( num / divisor ) ) ; } public static void main ( String [ ] args ) { System . out . println ( getRemainder ( 100 , 7 ) ) ; } }

def getRemainder ( num , divisor ) : NEW_LINE INDENT return ( num - divisor * ( num // divisor ) ) NEW_LINE DEDENT num = 100 NEW_LINE divisor = 7 NEW_LINE print ( getRemainder ( num , divisor ) ) NEW_LINE


class GFG { static void swap ( int a , int b ) { int temp = a ; a = b ; b = temp ; } static int minimumSquare ( int a , int b ) { int result = 0 , rem = 0 ; if ( a < b ) swap ( a , b ) ; while ( b > 0 ) { result += a / b ; rem = a % b ; a = b ; b = rem ; } return result ; } public static void main ( String [ ] args ) { int n = 13 , m = 29 ; System . out . println ( minimumSquare ( n , m ) ) ; } }

def minimumSquare ( a , b ) : NEW_LINE INDENT result = 0 NEW_LINE rem = 0 NEW_LINE if ( a < b ) : NEW_LINE INDENT a , b = b , a NEW_LINE DEDENT while ( b > 0 ) : NEW_LINE INDENT result += int ( a / b ) NEW_LINE rem = int ( a % b ) NEW_LINE a = b NEW_LINE b = rem NEW_LINE DEDENT return result NEW_LINE DEDENT n = 13 NEW_LINE m = 29 NEW_LINE print ( minimumSquare ( n , m ) ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { public static void splitArr ( int arr [ ] , int n , int k ) { for ( int i = 0 ; i < k ; i ++ ) { int x = arr [ 0 ] ; for ( int j = 0 ; j < n - 1 ; ++ j ) arr [ j ] = arr [ j + 1 ] ; arr [ n - 1 ] = x ; } } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 10 , 5 , 6 , 52 , 36 } ; int n = arr . length ; int position = 2 ; splitArr ( arr , 6 , position ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( arr [ i ] + " ▁ " ) ; } }

def splitArr ( arr , n , k ) : NEW_LINE INDENT for i in range ( 0 , k ) : NEW_LINE INDENT x = arr [ 0 ] NEW_LINE for j in range ( 0 , n - 1 ) : NEW_LINE INDENT arr [ j ] = arr [ j + 1 ] NEW_LINE DEDENT arr [ n - 1 ] = x NEW_LINE DEDENT DEDENT arr = [ 12 , 10 , 5 , 6 , 52 , 36 ] NEW_LINE n = len ( arr ) NEW_LINE position = 2 NEW_LINE splitArr ( arr , n , position ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT


import java . util . * ; public class GFG { static void Print3Smallest ( int array [ ] , int n ) { int firstmin = Integer . MAX_VALUE ; int secmin = Integer . MAX_VALUE ; int thirdmin = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] < firstmin ) { thirdmin = secmin ; secmin = firstmin ; firstmin = array [ i ] ; } else if ( array [ i ] < secmin ) { thirdmin = secmin ; secmin = array [ i ] ; } else if ( array [ i ] < thirdmin ) thirdmin = array [ i ] ; } System . out . println ( " First ▁ min ▁ = ▁ " + firstmin ) ; System . out . println ( " Second ▁ min ▁ = ▁ " + secmin ) ; System . out . println ( " Third ▁ min ▁ = ▁ " + thirdmin ) ; } public static void main ( String [ ] args ) { int array [ ] = { 4 , 9 , 1 , 32 , 12 } ; int n = array . length ; Print3Smallest ( array , n ) ; } }

MAX = 100000 NEW_LINE def Print3Smallest ( arr , n ) : NEW_LINE INDENT firstmin = MAX NEW_LINE secmin = MAX NEW_LINE thirdmin = MAX NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] < firstmin : NEW_LINE INDENT thirdmin = secmin NEW_LINE secmin = firstmin NEW_LINE firstmin = arr [ i ] NEW_LINE DEDENT elif arr [ i ] < secmin : NEW_LINE INDENT thirdmin = secmin NEW_LINE secmin = arr [ i ] NEW_LINE DEDENT elif arr [ i ] < thirdmin : NEW_LINE INDENT thirdmin = arr [ i ] NEW_LINE DEDENT DEDENT print ( " First ▁ min ▁ = ▁ " , firstmin ) NEW_LINE print ( " Second ▁ min ▁ = ▁ " , secmin ) NEW_LINE print ( " Third ▁ min ▁ = ▁ " , thirdmin ) NEW_LINE DEDENT arr = [ 4 , 9 , 1 , 32 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE Print3Smallest ( arr , n ) NEW_LINE


class Test { static boolean isDivisible7 ( String num ) { int n = num . length ( ) ; if ( n == 0 && num . charAt ( 0 ) == '0' ) return true ; if ( n % 3 == 1 ) num = "00" + num ; if ( n % 3 == 2 ) num = "0" + num ; n = num . length ( ) ; int gSum = 0 , p = 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { int group = 0 ; group += num . charAt ( i -- ) - '0' ; group += ( num . charAt ( i -- ) - '0' ) * 10 ; group += ( num . charAt ( i ) - '0' ) * 100 ; gSum = gSum + group * p ; p = p * - 1 ; } return ( gSum % 7 == 0 ) ; } public static void main ( String args [ ] ) { String num = "8955795758" ; System . out . println ( isDivisible7 ( num ) ? " Divisible ▁ by ▁ 7" : " Not ▁ Divisible ▁ ▁ by ▁ 7" ) ; } }

def isdivisible7 ( num ) : NEW_LINE INDENT n = len ( num ) NEW_LINE if ( n == 0 and num [ 0 ] == ' \n ' ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 3 == 1 ) : NEW_LINE INDENT num = str ( num ) + "00" NEW_LINE n += 2 NEW_LINE DEDENT elif ( n % 3 == 2 ) : NEW_LINE INDENT num = str ( num ) + "0" NEW_LINE n += 1 NEW_LINE DEDENT GSum = 0 NEW_LINE p = 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT group = 0 NEW_LINE group += ord ( num [ i ] ) - ord ( '0' ) NEW_LINE i -= 1 NEW_LINE group += ( ord ( num [ i ] ) - ord ( '0' ) ) * 10 NEW_LINE i -= 1 NEW_LINE group += ( ord ( num [ i ] ) - ord ( '0' ) ) * 100 NEW_LINE GSum = GSum + group * p NEW_LINE p *= ( - 1 ) NEW_LINE DEDENT return ( GSum % 7 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT num = "8955795758" NEW_LINE if ( isdivisible7 ( num ) ) : NEW_LINE INDENT print ( " Divisible ▁ by ▁ 7" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Divisible ▁ by ▁ 7" ) NEW_LINE DEDENT DEDENT


import java . util . Arrays ; import java . util . Vector ; class Test { static void printEqualModNumbers ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int d = arr [ n - 1 ] - arr [ 0 ] ; if ( d == 0 ) { System . out . println ( " Infinite ▁ solution " ) ; return ; } Vector < Integer > v = new Vector < > ( ) ; for ( int i = 1 ; i * i <= d ; i ++ ) { if ( d % i == 0 ) { v . add ( i ) ; if ( i != d / i ) v . add ( d / i ) ; } } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { int temp = arr [ 0 ] % v . get ( i ) ; int j ; for ( j = 1 ; j < n ; j ++ ) if ( arr [ j ] % v . get ( i ) != temp ) break ; if ( j == n ) System . out . print ( v . get ( i ) + " ▁ " ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 38 , 6 , 34 } ; printEqualModNumbers ( arr , arr . length ) ; } }

def printEqualModNumbers ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE d = arr [ n - 1 ] - arr [ 0 ] ; NEW_LINE // Case when all the array elements are same NEW_LINE if ( d == 0 ) : NEW_LINE INDENT print ( " Infinite ▁ solution " ) NEW_LINE return NEW_LINE DEDENT v = [ ] ; NEW_LINE i = 1 ; NEW_LINE while ( i * i <= d ) : NEW_LINE INDENT if ( d % i == 0 ) : NEW_LINE INDENT v . append ( i ) ; NEW_LINE if ( i != d / i ) : NEW_LINE INDENT v . append ( d / i ) ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT temp = arr [ 0 ] % v [ i ] ; NEW_LINE j = 1 ; NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( arr [ j ] % v [ i ] != temp ) : NEW_LINE INDENT break ; NEW_LINE DEDENT j += 1 ; NEW_LINE DEDENT if ( j == n ) : NEW_LINE INDENT print ( v [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT arr = [ 38 , 6 , 34 ] ; NEW_LINE printEqualModNumbers ( arr , len ( arr ) ) ; NEW_LINE


class GFG { static final int V_SUM_MAX = 1000 ; static final int N_MAX = 100 ; static final int W_MAX = 10000000 ; static int dp [ ] [ ] = new int [ V_SUM_MAX + 1 ] [ N_MAX ] ; static boolean v [ ] [ ] = new boolean [ V_SUM_MAX + 1 ] [ N_MAX ] ; static int solveDp ( int r , int i , int w [ ] , int val [ ] , int n ) { if ( r <= 0 ) return 0 ; if ( i == n ) return W_MAX ; if ( v [ r ] [ i ] ) return dp [ r ] [ i ] ; v [ r ] [ i ] = true ; dp [ r ] [ i ] = Math . min ( solveDp ( r , i + 1 , w , val , n ) , w [ i ] + solveDp ( r - val [ i ] , i + 1 , w , val , n ) ) ; return dp [ r ] [ i ] ; } static int maxWeight ( int w [ ] , int val [ ] , int n , int c ) { for ( int i = V_SUM_MAX ; i >= 0 ; i -- ) { if ( solveDp ( i , 0 , w , val , n ) <= c ) { return i ; } } return 0 ; } public static void main ( String [ ] args ) { int w [ ] = { 3 , 4 , 5 } ; int val [ ] = { 30 , 50 , 60 } ; int n = w . length ; int C = 8 ; System . out . println ( maxWeight ( w , val , n , C ) ) ; } }

V_SUM_MAX = 1000 NEW_LINE N_MAX = 100 NEW_LINE W_MAX = 10000000 NEW_LINE dp = [ [ 0 for i in range ( N_MAX ) ] for i in range ( V_SUM_MAX + 1 ) ] NEW_LINE v = [ [ 0 for i in range ( N_MAX ) ] for i in range ( V_SUM_MAX + 1 ) ] NEW_LINE def solveDp ( r , i , w , val , n ) : NEW_LINE INDENT if ( r <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT return W_MAX NEW_LINE DEDENT if ( v [ r ] [ i ] ) : NEW_LINE INDENT return dp [ r ] [ i ] NEW_LINE DEDENT v [ r ] [ i ] = 1 NEW_LINE dp [ r ] [ i ] = min ( solveDp ( r , i + 1 , w , val , n ) , w [ i ] + solveDp ( r - val [ i ] , i + 1 , w , val , n ) ) NEW_LINE return dp [ r ] [ i ] NEW_LINE DEDENT def maxWeight ( w , val , n , c ) : NEW_LINE INDENT for i in range ( V_SUM_MAX , - 1 , - 1 ) : NEW_LINE INDENT if ( solveDp ( i , 0 , w , val , n ) <= c ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT w = [ 3 , 4 , 5 ] NEW_LINE val = [ 30 , 50 , 60 ] NEW_LINE n = len ( w ) NEW_LINE C = 8 NEW_LINE print ( maxWeight ( w , val , n , C ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean startsWith ( String str , String pre ) { int strLen = str . length ( ) ; int preLen = pre . length ( ) ; int i = 0 , j = 0 ; while ( i < strLen && j < preLen ) { if ( str . charAt ( i ) != pre . charAt ( j ) ) return false ; i ++ ; j ++ ; } return true ; } static boolean endsWith ( String str , String suff ) { int i = str . length ( ) - 1 ; int j = suff . length ( ) - 1 ; while ( i >= 0 && j >= 0 ) { if ( str . charAt ( i ) != suff . charAt ( j ) ) return false ; i -- ; j -- ; } return true ; } static boolean checkString ( String str , String a , String b ) { if ( str . length ( ) != a . length ( ) + b . length ( ) ) return false ; if ( startsWith ( str , a ) ) { if ( endsWith ( str , b ) ) return true ; } if ( startsWith ( str , b ) ) { if ( endsWith ( str , a ) ) return true ; } return false ; } public static void main ( String args [ ] ) { String str = " GeeksforGeeks " ; String a = " Geeksfo " ; String b = " rGeeks " ; if ( checkString ( str , a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def startsWith ( str , pre ) : NEW_LINE INDENT strLen = len ( str ) NEW_LINE preLen = len ( pre ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE while ( i < strLen and j < preLen ) : NEW_LINE INDENT if ( str [ i ] != pre [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def endsWith ( str , suff ) : NEW_LINE INDENT i = len ( str ) - 1 NEW_LINE j = len ( suff ) - 1 NEW_LINE while ( i >= 0 and j >= 0 ) : NEW_LINE INDENT if ( str [ i ] != suff [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT i -= 1 NEW_LINE j -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT def checkString ( str , a , b ) : NEW_LINE INDENT if ( len ( str ) != len ( a ) + len ( b ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( startsWith ( str , a ) ) : NEW_LINE INDENT if ( endsWith ( str , b ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT if ( startsWith ( str , b ) ) : NEW_LINE INDENT if ( endsWith ( str , a ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT str = " GeeksforGeeks " NEW_LINE a = " Geeksfo " NEW_LINE b = " rGeeks " NEW_LINE if ( checkString ( str , a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static int count_numbers ( int k , int n , boolean flag ) { if ( n == 1 ) { if ( flag ) { return ( k - 1 ) ; } else { return 1 ; } } if ( flag ) return ( k - 1 ) * ( count_numbers ( k , n - 1 , false ) + count_numbers ( k , n - 1 , true ) ) ; else return count_numbers ( k , n - 1 , true ) ; } public static void main ( String [ ] args ) { int n = 3 ; int k = 10 ; System . out . println ( count_numbers ( k , n , true ) ) ; } }

def count_numbers ( k , n , flag ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT return ( k - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if ( flag ) : NEW_LINE INDENT return ( k - 1 ) * ( count_numbers ( k , n - 1 , 0 ) + count_numbers ( k , n - 1 , 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return count_numbers ( k , n - 1 , 1 ) NEW_LINE DEDENT DEDENT n = 3 NEW_LINE k = 10 NEW_LINE print ( count_numbers ( k , n , True ) ) NEW_LINE


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

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


import java . util . * ; class GfG { static void pattern ( int n ) { int p , k = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { p = k ; for ( int j = 1 ; j <= i ; j ++ ) { System . out . print ( p ) ; System . out . print ( " ▁ " ) ; p = p - ( n + j - i ) ; } System . out . println ( ) ; k = k + 1 + n - i ; } } public static void main ( String [ ] args ) { int n = 5 ; pattern ( n ) ; } }

def pattern ( n ) : NEW_LINE INDENT k = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT p = k NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( p , end = " ▁ " ) NEW_LINE p = p - ( n + j - i ) NEW_LINE DEDENT print ( " " ) NEW_LINE k = k + 1 + n - i NEW_LINE DEDENT DEDENT n = 5 NEW_LINE pattern ( n ) NEW_LINE


import java . util . Arrays ; class GFG { static int findSubstrings ( String s , int k ) { int ans = 0 ; int cnt [ ] = new int [ 26 ] ; int i , j , n = s . length ( ) ; for ( i = 0 ; i < n ; i ++ ) { Arrays . fill ( cnt , 0 ) ; for ( j = i ; j < n ; j ++ ) { cnt [ s . charAt ( j ) - ' a ' ] ++ ; if ( cnt [ s . charAt ( j ) - ' a ' ] <= k ) { ans ++ ; } else { break ; } } } return ans ; } static public void main ( String [ ] args ) { String S = " aaabb " ; int k = 2 ; System . out . println ( findSubstrings ( S , k ) ) ; } }

def findSubstrings ( s , k ) : NEW_LINE INDENT ans = 0 NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt = [ 0 ] * 26 NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT cnt [ ord ( s [ j ] ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( cnt [ ord ( s [ j ] ) - ord ( ' a ' ) ] <= k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " aaabb " NEW_LINE k = 2 NEW_LINE print ( findSubstrings ( S , k ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static int findRepeatingNumber ( int [ ] arr , int n ) { int sq = ( int ) Math . sqrt ( n ) ; int range = ( n / sq ) + 1 ; int [ ] count = new int [ range ] ; for ( int i = 0 ; i <= n ; i ++ ) { count [ ( arr [ i ] - 1 ) / sq ] ++ ; } int selected_block = range - 1 ; for ( int i = 0 ; i < range - 1 ; i ++ ) { if ( count [ i ] > sq ) { selected_block = i ; break ; } } HashMap < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i <= n ; i ++ ) { if ( ( ( selected_block * sq ) < arr [ i ] ) && ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) { m . put ( arr [ i ] , 1 ) ; if ( m . get ( arr [ i ] ) == 1 ) return arr [ i ] ; } } return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 2 , 3 , 5 , 4 } ; int n = 5 ; System . out . println ( " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in ▁ the ▁ array ▁ is : ▁ " + findRepeatingNumber ( arr , n ) ) ; } }

from math import sqrt NEW_LINE def findRepeatingNumber ( arr , n ) : NEW_LINE INDENT sq = sqrt ( n ) NEW_LINE range__ = int ( ( n / sq ) + 1 ) NEW_LINE count = [ 0 for i in range ( range__ ) ] NEW_LINE for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT count [ int ( ( arr [ i ] - 1 ) / sq ) ] += 1 NEW_LINE DEDENT selected_block = range__ - 1 NEW_LINE for i in range ( 0 , range__ - 1 , 1 ) : NEW_LINE INDENT if ( count [ i ] > sq ) : NEW_LINE INDENT selected_block = i NEW_LINE break NEW_LINE DEDENT DEDENT m = { i : 0 for i in range ( n ) } NEW_LINE for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT if ( ( ( selected_block * sq ) < arr [ i ] ) and ( arr [ i ] <= ( ( selected_block + 1 ) * sq ) ) ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 NEW_LINE if ( m [ arr [ i ] ] > 1 ) : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 3 , 5 , 4 ] NEW_LINE n = 5 NEW_LINE print ( " One ▁ of ▁ the ▁ numbers ▁ repeated ▁ in ▁ the ▁ array ▁ is : " , findRepeatingNumber ( arr , n ) ) NEW_LINE DEDENT


import java . util . Arrays ; class geeksforgeeks { static int minOperations ( int ar [ ] , int k ) { Arrays . sort ( ar ) ; int opsNeeded = 0 ; for ( int i = 0 ; i < k ; i ++ ) { opsNeeded += ar [ k - 1 ] - ar [ i ] ; } int ans = opsNeeded ; for ( int i = k ; i < ar . length ; i ++ ) { opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] ) ; opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] ) ; ans = Math . min ( ans , opsNeeded ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 9 , 100 } ; int n = arr . length ; int k = 3 ; System . out . printf ( " % d " , minOperations ( arr , k ) ) ; } }

def minOperations ( ar , k ) : NEW_LINE INDENT ar = sorted ( ar ) NEW_LINE opsNeeded = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT opsNeeded += ar [ k - 1 ] - ar [ i ] NEW_LINE DEDENT ans = opsNeeded NEW_LINE for i in range ( k , len ( ar ) ) : NEW_LINE INDENT opsNeeded = opsNeeded - ( ar [ i - 1 ] - ar [ i - k ] ) NEW_LINE opsNeeded += ( k - 1 ) * ( ar [ i ] - ar [ i - 1 ] ) NEW_LINE ans = min ( ans , opsNeeded ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 1 , 9 , 100 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( minOperations ( arr , k ) ) NEW_LINE


import java . io . * ; class GFG { static int arrSize = 51 ; static int maxSum = 201 ; static int MAX = 100 ; static int inf = 999999 ; static int dp [ ] [ ] = new int [ arrSize ] [ maxSum ] ; static int visit [ ] [ ] = new int [ arrSize ] [ maxSum ] ; static int RetClose ( int a , int b , int s ) { if ( Math . abs ( a - s ) < Math . abs ( b - s ) ) return a ; else return b ; } static int MinDiff ( int i , int sum , int arr [ ] , int n ) { if ( i == n ) return 0 ; if ( visit [ i ] [ sum + MAX ] > 0 ) return dp [ i ] [ sum + MAX ] ; visit [ i ] [ sum + MAX ] = 1 ; dp [ i ] [ sum + MAX ] = RetClose ( arr [ i ] + MinDiff ( i + 1 , sum + arr [ i ] , arr , n ) , MinDiff ( i + 1 , sum , arr , n ) , - 1 * sum ) ; return dp [ i ] [ sum + MAX ] ; } static void FindClose ( int arr [ ] , int n ) { int ans = inf ; for ( int i = 1 ; i <= n ; i ++ ) ans = RetClose ( arr [ i - 1 ] + MinDiff ( i , arr [ i - 1 ] , arr , n ) , ans , 0 ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 25 , - 9 , - 10 , - 4 , - 7 , - 33 } ; int n = arr . length ; FindClose ( arr , n ) ; } }

import numpy as np NEW_LINE arrSize = 51 NEW_LINE maxSum = 201 NEW_LINE MAX = 100 NEW_LINE inf = 999999 NEW_LINE dp = np . zeros ( ( arrSize , maxSum ) ) ; NEW_LINE visit = np . zeros ( ( arrSize , maxSum ) ) ; NEW_LINE def RetClose ( a , b , s ) : NEW_LINE INDENT if ( abs ( a - s ) < abs ( b - s ) ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT else : NEW_LINE INDENT return b ; NEW_LINE DEDENT DEDENT def MinDiff ( i , sum , arr , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( visit [ i ] [ sum + MAX ] ) : NEW_LINE INDENT return dp [ i ] [ sum + MAX ] ; NEW_LINE DEDENT visit [ i ] [ sum + MAX ] = 1 ; NEW_LINE dp [ i ] [ sum + MAX ] = RetClose ( arr [ i ] + MinDiff ( i + 1 , sum + arr [ i ] , arr , n ) , MinDiff ( i + 1 , sum , arr , n ) , - 1 * sum ) ; NEW_LINE return dp [ i ] [ sum + MAX ] ; NEW_LINE DEDENT def FindClose ( arr , n ) : NEW_LINE INDENT ans = inf ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = RetClose ( arr [ i - 1 ] + MinDiff ( i , arr [ i - 1 ] , arr , n ) , ans , 0 ) ; NEW_LINE DEDENT print ( ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 25 , - 9 , - 10 , - 4 , - 7 , - 33 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE FindClose ( arr , n ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int findSmallest ( int a [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int j ; for ( j = 0 ; j < n ; j ++ ) if ( a [ j ] % a [ i ] >= 1 ) break ; if ( j == n ) return a [ i ] ; } return - 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 25 , 20 , 5 , 10 , 100 } ; int n = a . length ; System . out . println ( findSmallest ( a , n ) ) ; } }

def findSmallest ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( a [ j ] % a [ i ] ) >= 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( j == n - 1 ) : NEW_LINE INDENT return a [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT a = [ 25 , 20 , 5 , 10 , 100 ] NEW_LINE n = len ( a ) NEW_LINE print ( findSmallest ( a , n ) ) NEW_LINE


class GFG { static void findNumbers ( int n ) { int odd = ( int ) Math . pow ( 10 , n ) - 1 ; int even = odd - 1 ; System . out . println ( " Even ▁ = ▁ " + even ) ; System . out . print ( " Odd ▁ = ▁ " + odd ) ; } public static void main ( String args [ ] ) { int n = 4 ; findNumbers ( n ) ; } }

def findNumbers ( n ) : NEW_LINE INDENT odd = pow ( 10 , n ) - 1 NEW_LINE even = odd - 1 NEW_LINE print ( " Even ▁ = ▁ " , even ) NEW_LINE print ( " Odd ▁ = ▁ " , odd ) NEW_LINE DEDENT n = 4 NEW_LINE findNumbers ( n ) NEW_LINE


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

def printNonDivisible ( A , B , n , m ) : NEW_LINE INDENT maxB = 0 NEW_LINE for i in range ( 0 , m , 1 ) : NEW_LINE INDENT if ( B [ i ] > maxB ) : NEW_LINE INDENT maxB = B [ i ] NEW_LINE DEDENT DEDENT mark = [ 0 for i in range ( maxB ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT for x in range ( A [ i ] , maxB , A [ i ] ) : NEW_LINE INDENT mark [ x ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , m - 1 , 1 ) : NEW_LINE INDENT if ( mark [ B [ i ] ] == 0 ) : NEW_LINE INDENT print ( B [ i ] ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 100 , 200 , 400 , 100 ] NEW_LINE n = len ( A ) NEW_LINE B = [ 190 , 200 , 87 , 600 , 800 ] NEW_LINE m = len ( B ) NEW_LINE printNonDivisible ( A , B , n , m ) NEW_LINE DEDENT


class GFG { static int nCr ( int n , int r ) { return fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ; } static int fact ( int n ) { int res = 1 ; for ( int i = 2 ; i <= n ; i ++ ) res = res * i ; return res ; } public static void main ( String [ ] args ) { int n = 5 , r = 3 ; System . out . println ( nCr ( n , r ) ) ; } }

def nCr ( n , r ) : NEW_LINE INDENT return ( fact ( n ) / ( fact ( r ) * fact ( n - r ) ) ) NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT res = res * i NEW_LINE DEDENT return res NEW_LINE DEDENT n = 5 NEW_LINE r = 3 NEW_LINE print ( int ( nCr ( n , r ) ) ) NEW_LINE


import java . util . HashMap ; class GFG { public static boolean multipleOrFactor ( String s1 , String s2 ) { HashMap < Character , Integer > m1 = new HashMap < > ( ) ; HashMap < Character , Integer > m2 = new HashMap < > ( ) ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { if ( m1 . containsKey ( s1 . charAt ( i ) ) ) { int x = m1 . get ( s1 . charAt ( i ) ) ; m1 . put ( s1 . charAt ( i ) , ++ x ) ; } else m1 . put ( s1 . charAt ( i ) , 1 ) ; } for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) { if ( m2 . containsKey ( s2 . charAt ( i ) ) ) { int x = m2 . get ( s2 . charAt ( i ) ) ; m2 . put ( s2 . charAt ( i ) , ++ x ) ; } else m2 . put ( s2 . charAt ( i ) , 1 ) ; } for ( HashMap . Entry < Character , Integer > entry : m1 . entrySet ( ) ) { if ( ! m2 . containsKey ( entry . getKey ( ) ) ) continue ; if ( m2 . get ( entry . getKey ( ) ) != null && ( m2 . get ( entry . getKey ( ) ) % entry . getValue ( ) == 0 || entry . getValue ( ) % m2 . get ( entry . getKey ( ) ) == 0 ) ) continue ; else return false ; } return true ; } public static void main ( String [ ] args ) { String s1 = " geeksforgeeks " , s2 = " geeks " ; if ( multipleOrFactor ( s1 , s2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

from collections import defaultdict NEW_LINE def multipleOrFactor ( s1 , s2 ) : NEW_LINE INDENT m1 = defaultdict ( lambda : 0 ) NEW_LINE m2 = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , len ( s1 ) ) : NEW_LINE INDENT m1 [ s1 [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 0 , len ( s2 ) ) : NEW_LINE INDENT m2 [ s2 [ i ] ] += 1 NEW_LINE DEDENT for it in m1 : NEW_LINE INDENT if it not in m2 : NEW_LINE INDENT continue NEW_LINE DEDENT if ( m2 [ it ] % m1 [ it ] == 0 or m1 [ it ] % m2 [ it ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " geeksforgeeks " NEW_LINE s2 = " geeks " NEW_LINE if multipleOrFactor ( s1 , s2 ) : print ( " YES " ) NEW_LINE else : print ( " NO " ) NEW_LINE DEDENT


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

def max_U_element ( a , N , M ) : NEW_LINE INDENT hsh = dict ( ) NEW_LINE dist_count = 0 NEW_LINE res = 0 NEW_LINE for i in range ( M ) : NEW_LINE INDENT if ( arr [ i ] not in hsh . keys ( ) ) : NEW_LINE INDENT hsh [ a [ i ] ] = 1 NEW_LINE dist_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT hsh [ a [ i ] ] += 1 NEW_LINE DEDENT DEDENT res = dist_count NEW_LINE for i in range ( M , N ) : NEW_LINE INDENT if ( a [ i - M ] in hsh . keys ( ) and hsh [ a [ i - M ] ] == 1 ) : NEW_LINE INDENT del hsh [ a [ i - M ] ] NEW_LINE dist_count -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT hsh [ a [ i - M ] ] -= 1 NEW_LINE DEDENT if ( a [ i ] not in hsh . keys ( ) ) : NEW_LINE INDENT hsh [ a [ i ] ] = 1 NEW_LINE dist_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT hsh [ a [ i ] ] += 1 NEW_LINE DEDENT res = max ( res , dist_count ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 1 , 2 , 1 , 3 , 4 , 2 , 3 ] NEW_LINE M = 4 NEW_LINE N = len ( arr ) NEW_LINE print ( max_U_element ( arr , N , M ) ) NEW_LINE


class GFG { static void knowPair ( int a , int b , int n , int s , int m ) { int i = 0 , rem = 0 ; int count_b = 0 , flag = 0 ; while ( i <= a ) { rem = s - ( n * i ) ; count_b = rem / m ; if ( rem % m == 0 && count_b <= b ) { flag = 1 ; } i ++ ; } if ( flag == 1 ) { System . out . print ( " YES " + " \n " ) ; } else { System . out . print ( " NO " + " \n " ) ; } } public static void main ( String [ ] args ) { int A = 1 ; int B = 2 ; int n = 3 ; int S = 4 ; int m = 2 ; knowPair ( A , B , n , S , m ) ; } }

def knowPair ( a , b , n , s , m ) : NEW_LINE INDENT i = 0 NEW_LINE rem = 0 NEW_LINE count_b = 0 NEW_LINE flag = 0 NEW_LINE while ( i <= a ) : NEW_LINE INDENT rem = s - ( n * i ) NEW_LINE count_b = rem // m NEW_LINE if ( rem % m == 0 and count_b <= b ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 1 NEW_LINE B = 2 NEW_LINE n = 3 NEW_LINE S = 4 NEW_LINE m = 2 NEW_LINE knowPair ( A , B , n , S , m ) NEW_LINE DEDENT


class GFG { public static int gcd ( int a , int b ) { if ( a == 0 ) return b ; else return gcd ( b % a , a ) ; } public static void solve ( int n , int m , int x , int y , int a , int b ) { int k , g ; int x1 , y1 , x2 , y2 ; g = gcd ( a , b ) ; a /= g ; b /= g ; k = Math . min ( n / a , m / b ) ; x1 = x - ( k * a - k * a / 2 ) ; x2 = x + k * a / 2 ; y1 = y - ( k * b - k * b / 2 ) ; y2 = y + k * b / 2 ; if ( x1 < 0 ) { x2 -= x1 ; x1 = 0 ; } if ( x2 > n ) { x1 -= x2 - n ; x2 = n ; } if ( y1 < 0 ) { y2 -= y1 ; y1 = 0 ; } if ( y2 > m ) { y1 -= y2 - m ; y2 = m ; } System . out . println ( x1 + " ▁ " + y1 + " ▁ " + x2 + " ▁ " + y2 ) ; } public static void main ( String args [ ] ) { int n = 70 , m = 10 ; int x = 20 , y = 5 ; int a = 5 , b = 3 ; solve ( n , m , x , y , a , b ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT else : NEW_LINE INDENT return gcd ( b % a , a ) NEW_LINE DEDENT DEDENT def solve ( n , m , x , y , a , b ) : NEW_LINE INDENT g = int ( gcd ( a , b ) ) NEW_LINE a /= g NEW_LINE b /= g NEW_LINE k = int ( min ( n / a , m / b ) ) NEW_LINE x1 = int ( x - ( k * a - k * a / 2 ) ) NEW_LINE x2 = int ( x + k * a / 2 ) NEW_LINE y1 = int ( y - ( k * b - k * b / 2 ) ) NEW_LINE y2 = int ( y + k * b / 2 ) NEW_LINE if ( int ( x1 ) < 0 ) : NEW_LINE INDENT x2 -= x1 NEW_LINE x1 = 0 NEW_LINE DEDENT if ( int ( x2 ) > n ) : NEW_LINE INDENT x1 -= x2 - n NEW_LINE x2 = n NEW_LINE DEDENT if ( int ( y1 ) < 0 ) : NEW_LINE INDENT y2 -= y1 NEW_LINE y1 = 0 NEW_LINE DEDENT if ( int ( y2 ) > m ) : NEW_LINE INDENT y1 -= y2 - m NEW_LINE y2 = m NEW_LINE DEDENT print ( x1 , " ▁ " , y1 , " ▁ " , x2 , " ▁ " , y2 , sep = " " ) NEW_LINE DEDENT n = 70 NEW_LINE m = 10 NEW_LINE x = 20 NEW_LINE y = 5 NEW_LINE a = 5 NEW_LINE b = 3 NEW_LINE solve ( n , m , x , y , a , b ) NEW_LINE


class GFG { static int countStrUtil ( int [ ] [ ] [ ] dp , int n , int bCount , int cCount ) { if ( bCount < 0 || cCount < 0 ) { return 0 ; } if ( n == 0 ) { return 1 ; } if ( bCount == 0 && cCount == 0 ) { return 1 ; } if ( dp [ n ] [ bCount ] [ cCount ] != - 1 ) { return dp [ n ] [ bCount ] [ cCount ] ; } int res = countStrUtil ( dp , n - 1 , bCount , cCount ) ; res += countStrUtil ( dp , n - 1 , bCount - 1 , cCount ) ; res += countStrUtil ( dp , n - 1 , bCount , cCount - 1 ) ; return ( dp [ n ] [ bCount ] [ cCount ] = res ) ; } static int countStr ( int n , int bCount , int cCount ) { int [ ] [ ] [ ] dp = new int [ n + 1 ] [ 2 ] [ 3 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { for ( int k = 0 ; k < 3 ; k ++ ) { dp [ i ] [ j ] [ k ] = - 1 ; } } } return countStrUtil ( dp , n , bCount , cCount ) ; } public static void main ( String [ ] args ) { int n = 3 ; int bCount = 1 , cCount = 2 ; System . out . println ( countStr ( n , bCount , cCount ) ) ; } }

def countStrUtil ( dp , n , bCount = 1 , cCount = 2 ) : NEW_LINE INDENT if ( bCount < 0 or cCount < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( bCount == 0 and cCount == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( dp [ n ] [ bCount ] [ cCount ] != - 1 ) : NEW_LINE INDENT return dp [ n ] [ bCount ] [ cCount ] NEW_LINE DEDENT res = countStrUtil ( dp , n - 1 , bCount , cCount ) NEW_LINE res += countStrUtil ( dp , n - 1 , bCount - 1 , cCount ) NEW_LINE res += countStrUtil ( dp , n - 1 , bCount , cCount - 1 ) NEW_LINE dp [ n ] [ bCount ] [ cCount ] = res NEW_LINE return dp [ n ] [ bCount ] [ cCount ] NEW_LINE DEDENT def countStr ( n ) : NEW_LINE INDENT dp = [ [ [ - 1 for x in range ( n + 2 ) ] for y in range ( 3 ) ] for z in range ( 4 ) ] NEW_LINE return countStrUtil ( dp , n ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( countStr ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void checksum ( int n ) { if ( n % 3 == 0 ) System . out . println ( n / 3 - 1 + " ▁ " + n / 3 + " ▁ " + ( n / 3 + 1 ) ) ; else System . out . println ( " - 1" ) ; } public static void main ( String [ ] args ) { int n = 6 ; checksum ( n ) ; } }

def checksum ( n ) : NEW_LINE INDENT n = int ( n ) NEW_LINE if n % 3 == 0 : NEW_LINE INDENT print ( int ( n / 3 - 1 ) , " ▁ " , int ( n / 3 ) , " ▁ " , int ( n / 3 + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT DEDENT n = 6 NEW_LINE checksum ( n ) NEW_LINE


import java . util . * ; class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void pushBinaryToBorder ( int arr [ ] , int n ) { int count1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] != 1 ) arr [ count1 ++ ] = arr [ i ] ; while ( count1 < n ) arr [ count1 ++ ] = 1 ; int lastNonOne = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( arr [ i ] == 1 ) continue ; if ( lastNonOne == 0 ) { lastNonOne = i ; } if ( arr [ i ] != 0 ) arr [ lastNonOne -- ] = arr [ i ] ; } while ( lastNonOne >= 0 ) arr [ lastNonOne -- ] = 0 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 0 , 0 , 0 , 3 , 6 } ; int n = arr . length ; pushBinaryToBorder ( arr , n ) ; printArr ( arr , n ) ; } }

def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def pushBinaryToBorder ( arr , n ) : NEW_LINE INDENT count1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != 1 ) : NEW_LINE INDENT arr [ count1 ] = arr [ i ] NEW_LINE count1 += 1 NEW_LINE DEDENT DEDENT while ( count1 < n ) : NEW_LINE INDENT arr [ count1 ] = 1 NEW_LINE count1 += 1 NEW_LINE DEDENT lastNonOne = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( not lastNonOne ) : NEW_LINE INDENT lastNonOne = i NEW_LINE DEDENT if ( arr [ i ] != 0 ) : NEW_LINE INDENT arr [ lastNonOne ] = arr [ i ] NEW_LINE lastNonOne -= 1 NEW_LINE DEDENT DEDENT while ( lastNonOne >= 0 ) : NEW_LINE INDENT arr [ lastNonOne ] = 0 NEW_LINE lastNonOne -= 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 0 , 0 , 0 , 3 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE pushBinaryToBorder ( arr , n ) NEW_LINE printArr ( arr , n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void newvol ( double x ) { System . out . print ( " percentage ▁ increase ▁ " + " in ▁ the ▁ volume ▁ of ▁ the ▁ cube ▁ is ▁ " + ( Math . pow ( x , 3 ) / 10000 + 3 * x + ( 3 * Math . pow ( x , 2 ) ) / 100 ) ) ; System . out . print ( " % " ) ; } public static void main ( String [ ] args ) { double x = 10 ; newvol ( x ) ; } }

def newvol ( x ) : NEW_LINE INDENT print ( " percentage ▁ increase " " in ▁ the ▁ volume ▁ of ▁ the ▁ cube ▁ is ▁ " , ( ( x ** ( 3 ) ) / 10000 + 3 * x + ( 3 * ( x ** ( 2 ) ) ) / 100 ) , " % " ) ; NEW_LINE DEDENT x = 10 ; NEW_LINE newvol ( x ) ; NEW_LINE


class GFG { static int GCD ( int a , int b ) { return b == 0 ? a : GCD ( b , a % b ) ; } static boolean isSplittable ( int n , int m ) { int total_sum = ( n * ( n + 1 ) ) / 2 ; int sum_s1 = ( total_sum + m ) / 2 ; int sum_s2 = total_sum - sum_s1 ; if ( total_sum < m ) return false ; if ( sum_s1 + sum_s2 == total_sum && sum_s1 - sum_s2 == m ) return ( GCD ( sum_s1 , sum_s2 ) == 1 ) ; return false ; } public static void main ( String args [ ] ) { int n = 5 , m = 7 ; if ( isSplittable ( n , m ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def __gcd ( a , b ) : NEW_LINE INDENT return a if ( b == 0 ) else __gcd ( b , a % b ) ; NEW_LINE DEDENT def isSplittable ( n , m ) : NEW_LINE INDENT total_sum = ( int ) ( ( n * ( n + 1 ) ) / 2 ) ; NEW_LINE sum_s1 = int ( ( total_sum + m ) / 2 ) ; NEW_LINE sum_s2 = total_sum - sum_s1 ; NEW_LINE if ( total_sum < m ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( sum_s1 + sum_s2 == total_sum and sum_s1 - sum_s2 == m ) : NEW_LINE INDENT return ( __gcd ( sum_s1 , sum_s2 ) == 1 ) ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT n = 5 ; NEW_LINE m = 7 ; NEW_LINE if ( isSplittable ( n , m ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT