import java . util . Arrays ; public class GFG { static void solve ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; if ( k == 1 && arr [ n - 2 ] != arr [ n - 1 ] ) { System . out . println ( arr [ n - 1 ] ) ; return ; } int count = 1 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == arr [ i + 1 ] ) count ++ ; else count = 1 ; if ( count == k && ( i == 0 || ( arr [ i - 1 ] != arr [ i ] ) ) ) { System . out . println ( arr [ i ] ) ; return ; } } System . out . println ( " No ▁ such ▁ element " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 } ; int k = 2 ; int n = arr . length ; solve ( arr , n , k ) ; } }

def solve ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE if ( k == 1 and arr [ n - 2 ] != arr [ n - 1 ] ) : NEW_LINE INDENT print ( arr [ n - 1 ] ) NEW_LINE return NEW_LINE DEDENT count = 1 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count = 1 NEW_LINE DEDENT if ( count == k and ( i == 0 or ( arr [ i - 1 ] != arr [ i ] ) ) ) : NEW_LINE INDENT print ( arr [ i ] ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " No ▁ such ▁ element " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 , 6 , 6 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE solve ( arr , n , k ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; public class GfG { public final static int SIZE = 26 ; public static int longSubstring ( String str1 , int k ) { int [ ] freq = new int [ SIZE ] ; char [ ] str = str1 . toCharArray ( ) ; int start = 0 ; int maxLen = 0 ; char ch ; int n = str1 . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { ch = str [ i ] ; freq [ ch - ' a ' ] ++ ; if ( freq [ ch - ' a ' ] > k ) { if ( maxLen < ( i - start ) ) maxLen = i - start ; while ( freq [ ch - ' a ' ] > k ) { freq [ str [ start ] - ' a ' ] -- ; start ++ ; } } } if ( maxLen < ( n - start ) ) maxLen = n - start ; return maxLen ; } public static void main ( String argc [ ] ) { String str = " babcaag " ; int k = 1 ; System . out . println ( " Length ▁ = ▁ " + longSubstring ( str , k ) ) ; } }

import numpy as np NEW_LINE SIZE = 26 NEW_LINE def longSub ( str , k ) : NEW_LINE INDENT freq = np . zeros ( 26 , dtype = np . int ) NEW_LINE start = 0 NEW_LINE maxLen = 0 NEW_LINE n = len ( str ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT ch = str [ i ] NEW_LINE freq [ ord ( ch ) - ord ( ' a ' ) ] += 1 NEW_LINE if ( freq [ ord ( ch ) - ord ( ' a ' ) ] > k ) : NEW_LINE INDENT if ( maxLen < ( i - start ) ) : NEW_LINE INDENT maxLen = i - start NEW_LINE DEDENT while ( freq [ ord ( ch ) - ord ( ' a ' ) ] > k ) : NEW_LINE INDENT freq [ ord ( str [ start ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE start = start + 1 NEW_LINE DEDENT DEDENT DEDENT if ( maxLen < ( n - start ) ) : NEW_LINE INDENT maxLen = n - start NEW_LINE DEDENT return maxLen ; NEW_LINE DEDENT str = " babcaag " NEW_LINE k = 1 NEW_LINE print ( " Length ▁ = " , longSub ( str , k ) ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { public static void psuedoBinary ( int n ) { while ( n != 0 ) { int temp = n , m = 0 , p = 1 ; while ( temp != 0 ) { int rem = temp % 10 ; temp = temp / 10 ; if ( rem != 0 ) m += p ; p *= 10 ; } System . out . print ( m + " ▁ " ) ; n = n - m ; } System . out . println ( " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 31 ; psuedoBinary ( n ) ; } }

def psuedoBinary ( n ) : NEW_LINE INDENT while ( n > 0 ) : NEW_LINE INDENT temp = n ; NEW_LINE m = 0 ; NEW_LINE p = 1 ; NEW_LINE while ( temp ) : NEW_LINE INDENT rem = temp % 10 ; NEW_LINE temp = int ( temp / 10 ) ; NEW_LINE if ( rem != 0 ) : NEW_LINE INDENT m += p ; NEW_LINE DEDENT p *= 10 ; NEW_LINE DEDENT print ( m , end = " ▁ " ) ; NEW_LINE n = n - m ; NEW_LINE DEDENT DEDENT n = 31 ; NEW_LINE psuedoBinary ( n ) ; NEW_LINE


class GFG { static void findEncryptedArray ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( sum - arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 1 , 3 , 2 , 4 } ; int N = arr . length ; findEncryptedArray ( arr , N ) ; } }

def findEncryptedArray ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( sum - arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 1 , 3 , 2 , 4 ] NEW_LINE N = len ( arr ) NEW_LINE findEncryptedArray ( arr , N ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean prime [ ] = new boolean [ 100005 ] ; static void SieveOfEratosthenes ( int n ) { Arrays . fill ( prime , true ) ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i < n ; i += p ) { prime [ i ] = false ; } } } } static void sortPrimes ( int arr [ ] , int n ) { SieveOfEratosthenes ( 100005 ) ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { v . add ( arr [ i ] ) ; } } Comparator comparator = Collections . reverseOrder ( ) ; Collections . sort ( v , comparator ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { arr [ i ] = v . get ( j ++ ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 2 , 6 , 100 , 17 } ; int n = arr . length ; sortPrimes ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } }

def SieveOfEratosthenes ( n ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= n : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT for i in range ( p * 2 , n + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def sortPrimes ( arr , n ) : NEW_LINE INDENT SieveOfEratosthenes ( 100005 ) NEW_LINE v = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v . sort ( reverse = True ) NEW_LINE j = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT arr [ i ] = v [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return arr NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 3 , 2 , 6 , 100 , 17 ] NEW_LINE n = len ( arr ) NEW_LINE prime = [ True ] * 100006 NEW_LINE arr = sortPrimes ( arr , n ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int nondecdigits ( int n ) { int x = 0 ; for ( x = n ; x >= 1 ; x -- ) { int no = x ; int prev_dig = 11 ; boolean flag = true ; while ( no != 0 ) { if ( prev_dig < no % 10 ) { flag = false ; break ; } prev_dig = no % 10 ; no /= 10 ; } if ( flag == true ) break ; } return x ; } public static void main ( String [ ] args ) { int n = 200 ; System . out . println ( nondecdigits ( n ) ) ; } }

def nondecdigits ( n ) : NEW_LINE INDENT x = 0 NEW_LINE for x in range ( n , 0 , - 1 ) : NEW_LINE INDENT no = x NEW_LINE prev_dig = 11 NEW_LINE flag = True NEW_LINE while ( no != 0 ) : NEW_LINE INDENT if ( prev_dig < no % 10 ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT prev_dig = no % 10 NEW_LINE no //= 10 NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 200 NEW_LINE print ( nondecdigits ( n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean checkPrime ( String number ) { if ( number . length ( ) == 0 ) return true ; int num = Integer . parseInt ( number ) ; for ( int i = 2 ; i * i <= num ; i ++ ) if ( ( num % i ) == 0 ) return false ; return true ; } static int splitIntoPrimes ( String number ) { int numLen = number . length ( ) ; int [ ] splitDP = new int [ numLen + 1 ] ; Arrays . fill ( splitDP , - 1 ) ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 6 && checkPrime ( number . substring ( 0 , i ) ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 6 && i + j <= numLen ; j ++ ) { if ( checkPrime ( number . substring ( i , i + j ) ) ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void main ( String [ ] args ) { System . out . print ( splitIntoPrimes ( "13499315" ) + " \n " ) ; System . out . print ( splitIntoPrimes ( "43" ) + " \n " ) ; } }

from math import sqrt NEW_LINE def checkPrime ( number ) : NEW_LINE INDENT if ( len ( number ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT num = int ( number ) NEW_LINE for i in range ( 2 , int ( sqrt ( num ) ) + 1 , 1 ) : NEW_LINE INDENT if ( ( num % i ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def splitIntoPrimes ( number ) : NEW_LINE INDENT numLen = len ( number ) NEW_LINE splitDP = [ - 1 for i in range ( numLen + 1 ) ] NEW_LINE for i in range ( 1 , numLen + 1 , 1 ) : NEW_LINE INDENT if ( i <= 6 and checkPrime ( number [ 0 : i ] ) ) : NEW_LINE INDENT splitDP [ i ] = 1 NEW_LINE DEDENT if ( splitDP [ i ] != - 1 ) : NEW_LINE INDENT j = 1 NEW_LINE while ( j <= 6 and i + j <= numLen ) : NEW_LINE INDENT if ( checkPrime ( number [ i : i + j ] ) ) : NEW_LINE INDENT if ( splitDP [ i + j ] == - 1 ) : NEW_LINE INDENT splitDP [ i + j ] = 1 + splitDP [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT splitDP [ i + j ] = min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT return splitDP [ numLen ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( splitIntoPrimes ( "13499315" ) ) NEW_LINE print ( splitIntoPrimes ( "43" ) ) NEW_LINE DEDENT


class GFG { static int countUnsetBits ( int n ) { int cnt = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int temp = i ; while ( temp > 0 ) { if ( temp % 2 == 0 ) { cnt = cnt + 1 ; } temp = temp / 2 ; } } return cnt ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( countUnsetBits ( n ) ) ; } }

def countUnsetBits ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT temp = i ; NEW_LINE while ( temp ) : NEW_LINE INDENT if ( temp % 2 == 0 ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT temp = temp // 2 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE print ( countUnsetBits ( n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int n = 3 ; static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static void findPrefixCount ( int p_arr [ ] [ ] , boolean arr [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = n - 1 ; j >= 0 ; j -- ) { if ( arr [ i ] [ j ] ) continue ; if ( j != n - 1 ) p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] ; p_arr [ i ] [ j ] += ( arr [ i ] [ j ] == false ? 1 : 0 ) ; } } static int matrixOrValueOne ( boolean arr [ ] [ ] ) { int [ ] [ ] p_arr = new int [ n ] [ n ] ; findPrefixCount ( p_arr , arr ) ; int count_zero_submatrices = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int i = n - 1 ; Stack < pair > q = new Stack < pair > ( ) ; int to_sum = 0 ; while ( i >= 0 ) { int c = 0 ; while ( q . size ( ) != 0 && q . peek ( ) . first > p_arr [ i ] [ j ] ) { to_sum -= ( q . peek ( ) . second + 1 ) * ( q . peek ( ) . first - p_arr [ i ] [ j ] ) ; c += q . peek ( ) . second + 1 ; q . pop ( ) ; } to_sum += p_arr [ i ] [ j ] ; count_zero_submatrices += to_sum ; q . add ( new pair ( p_arr [ i ] [ j ] , c ) ) ; i -- ; } } return ( n * ( n + 1 ) * n * ( n + 1 ) ) / 4 - count_zero_submatrices ; } public static void main ( String [ ] args ) { boolean arr [ ] [ ] = { { false , false , false } , { false , true , false } , { false , false , false } } ; System . out . println ( matrixOrValueOne ( arr ) ) ; } }

def findPrefixCount ( p_arr , arr ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if arr [ i ] [ j ] : NEW_LINE INDENT continue NEW_LINE DEDENT if j != n - 1 : NEW_LINE INDENT p_arr [ i ] [ j ] += p_arr [ i ] [ j + 1 ] NEW_LINE DEDENT p_arr [ i ] [ j ] += int ( not arr [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT def matrixOrValueOne ( arr ) : NEW_LINE INDENT p_arr = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE findPrefixCount ( p_arr , arr ) NEW_LINE count_zero_submatrices = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT i = n - 1 NEW_LINE q = [ ] NEW_LINE to_sum = 0 NEW_LINE while i >= 0 : NEW_LINE INDENT c = 0 NEW_LINE while ( len ( q ) != 0 and q [ - 1 ] [ 0 ] > p_arr [ i ] [ j ] ) : NEW_LINE INDENT to_sum -= ( ( q [ - 1 ] [ 1 ] + 1 ) * ( q [ - 1 ] [ 0 ] - p_arr [ i ] [ j ] ) ) NEW_LINE c += q . pop ( ) [ 1 ] + 1 NEW_LINE DEDENT to_sum += p_arr [ i ] [ j ] NEW_LINE count_zero_submatrices += to_sum NEW_LINE q . append ( ( p_arr [ i ] [ j ] , c ) ) NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT return ( ( n * ( n + 1 ) * n * ( n + 1 ) ) // 4 - count_zero_submatrices ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE arr = [ [ 0 , 0 , 0 ] , [ 0 , 1 , 0 ] , [ 0 , 0 , 0 ] ] NEW_LINE print ( matrixOrValueOne ( arr ) ) NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int KnapSack ( int val [ ] , int wt [ ] , int n , int W ) { int [ ] dp = new int [ W + 1 ] ; Arrays . fill ( dp , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = W ; j >= wt [ i ] ; j -- ) dp [ j ] = Math . max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; return dp [ W ] ; } public static void main ( String [ ] args ) { int val [ ] = { 7 , 8 , 4 } , wt [ ] = { 3 , 8 , 6 } , W = 10 , n = 3 ; System . out . println ( KnapSack ( val , wt , n , W ) ) ; } }

def KnapSack ( val , wt , n , W ) : NEW_LINE INDENT dp = [ 0 ] * ( W + 1 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( W , wt [ i ] , - 1 ) : NEW_LINE INDENT dp [ j ] = max ( dp [ j ] , val [ i ] + dp [ j - wt [ i ] ] ) ; NEW_LINE DEDENT DEDENT return dp [ W ] ; NEW_LINE DEDENT val = [ 7 , 8 , 4 ] ; NEW_LINE wt = [ 3 , 8 , 6 ] ; NEW_LINE W = 10 ; n = 3 ; NEW_LINE print ( KnapSack ( val , wt , n , W ) ) ; NEW_LINE


import java . * ; public class GFG { static int getOccurence ( int n , int d ) { int result = 0 ; int itr = d ; while ( itr <= n ) { if ( itr % 10 == d ) result ++ ; if ( itr != 0 && itr / 10 == d ) { result ++ ; itr ++ ; } else if ( itr / 10 == d - 1 ) itr = itr + ( 10 - d ) ; else itr = itr + 10 ; } return result ; } public static void main ( String [ ] args ) { int n = 11 , d = 1 ; System . out . println ( getOccurence ( n , d ) ) ; } }

import math ; NEW_LINE def getOccurence ( n , d ) : NEW_LINE INDENT result = 0 ; NEW_LINE itr = d ; NEW_LINE while ( itr <= n ) : NEW_LINE INDENT if ( itr % 10 == d ) : NEW_LINE INDENT result += 1 ; NEW_LINE DEDENT if ( itr != 0 and math . floor ( itr / 10 ) == d ) : NEW_LINE INDENT result += 1 ; NEW_LINE itr += 1 ; NEW_LINE DEDENT elif ( math . floor ( itr / 10 ) == d - 1 ) : NEW_LINE INDENT itr = itr + ( 10 - d ) ; NEW_LINE DEDENT else : NEW_LINE INDENT itr = itr + 10 ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT n = 11 ; NEW_LINE d = 1 ; NEW_LINE print ( getOccurence ( n , d ) ) ; NEW_LINE


import java . util . * ; class GFG { static int min_val = Integer . MAX_VALUE ; static int min_steps = 0 ; static int sumOfDigits ( int n ) { String s = String . valueOf ( n ) ; int sum = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum += ( s . charAt ( i ) - '0' ) ; } return sum ; } static void Transform ( int n , int d , int steps ) { if ( n < min_val ) { min_val = n ; min_steps = steps ; } else if ( n == min_val ) { min_steps = Math . min ( min_steps , steps ) ; } if ( steps < 15 ) { Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; Transform ( n + d , d , steps + 1 ) ; } } public static void main ( String [ ] args ) { int N = 9 , D = 3 ; Transform ( N , D , 0 ) ; System . out . print ( min_val + " ▁ " + min_steps ) ; } }

import sys ; NEW_LINE min_val = sys . maxsize ; NEW_LINE min_steps = 0 ; NEW_LINE def sumOfDigits ( n ) : NEW_LINE INDENT s = str ( n ) ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT sum += ( ord ( s [ i ] ) - ord ( '0' ) ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT def Transform ( n , d , steps ) : NEW_LINE INDENT global min_val ; global min_steps ; NEW_LINE if ( n < min_val ) : NEW_LINE INDENT min_val = n ; NEW_LINE min_steps = steps ; NEW_LINE DEDENT elif ( n == min_val ) : NEW_LINE INDENT min_steps = min ( min_steps , steps ) ; NEW_LINE DEDENT if ( steps < 15 ) : NEW_LINE INDENT Transform ( sumOfDigits ( n ) , d , steps + 1 ) ; NEW_LINE Transform ( n + d , d , steps + 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 9 ; D = 3 ; NEW_LINE Transform ( N , D , 0 ) ; NEW_LINE print ( min_val , min_steps ) ; NEW_LINE DEDENT


class GFG { static boolean isPalindrome ( int num ) { int reverse_num = 0 , remainder , temp ; temp = num ; while ( temp != 0 ) { remainder = temp % 10 ; reverse_num = reverse_num * 10 + remainder ; temp /= 10 ; } if ( reverse_num == num ) { return true ; } return false ; } static boolean isOddLength ( int num ) { int count = 0 ; while ( num > 0 ) { num /= 10 ; count ++ ; } if ( count % 2 != 0 ) { return true ; } return false ; } static long sumOfAllPalindrome ( int L , int R ) { long sum = 0 ; if ( L <= R ) for ( int i = L ; i <= R ; i ++ ) { if ( isPalindrome ( i ) && isOddLength ( i ) ) { sum += i ; } } return sum ; } public static void main ( String [ ] args ) { int L = 110 , R = 1130 ; System . out . println ( sumOfAllPalindrome ( L , R ) ) ; } }

def isPalindrome ( num ) : NEW_LINE INDENT reverse_num = 0 NEW_LINE temp = num NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT remainder = temp % 10 NEW_LINE reverse_num = reverse_num * 10 + remainder NEW_LINE temp = int ( temp / 10 ) NEW_LINE DEDENT if ( reverse_num == num ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isOddLength ( num ) : NEW_LINE INDENT count = 0 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT num = int ( num / 10 ) NEW_LINE count += 1 NEW_LINE DEDENT if ( count % 2 != 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def sumOfAllPalindrome ( L , R ) : NEW_LINE INDENT sum = 0 NEW_LINE if ( L <= R ) : NEW_LINE INDENT for i in range ( L , R + 1 , 1 ) : NEW_LINE INDENT if ( isPalindrome ( i ) and isOddLength ( i ) ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 110 NEW_LINE R = 1130 NEW_LINE print ( sumOfAllPalindrome ( L , R ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void minRange ( int arr [ ] , int n , int k ) { int l = 0 , r = n ; for ( int i = 0 ; i < n ; i ++ ) { Set < Integer > s = new HashSet < Integer > ( ) ; int j ; for ( j = i ; j < n ; j ++ ) { s . add ( arr [ j ] ) ; if ( s . size ( ) == k ) { if ( ( j - i ) < ( r - l ) ) { r = j ; l = i ; } break ; } } if ( j == n ) break ; } if ( l == 0 && r == n ) System . out . println ( " Invalid ▁ k " ) ; else System . out . println ( l + " ▁ " + r ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 3 ; minRange ( arr , n , k ) ; } }

def minRange ( arr , n , k ) : NEW_LINE INDENT l = 0 NEW_LINE r = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT s = [ ] NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT s . append ( arr [ j ] ) NEW_LINE if ( len ( s ) == k ) : NEW_LINE INDENT if ( ( j - i ) < ( r - l ) ) : NEW_LINE INDENT r = j NEW_LINE l = i NEW_LINE DEDENT break NEW_LINE DEDENT DEDENT if ( j == n ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( l == 0 and r == n ) : NEW_LINE INDENT print ( " Invalid ▁ k " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( l , r ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE minRange ( arr , n , k ) NEW_LINE DEDENT


import java . io . * ; class Increasing { void findStrictlyIncreasingNum ( int start , String out , int n ) { if ( n == 0 ) { System . out . print ( out + " ▁ " ) ; return ; } for ( int i = start ; i <= 9 ; i ++ ) { String str = out + Integer . toString ( i ) ; findStrictlyIncreasingNum ( i + 1 , str , n - 1 ) ; } } public static void main ( String args [ ] ) throws IOException { Increasing obj = new Increasing ( ) ; int n = 3 ; obj . findStrictlyIncreasingNum ( 0 , " ▁ " , n ) ; } }

def findStrictlyIncreasingNum ( start , out , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT print ( out , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT for i in range ( start , 10 ) : NEW_LINE INDENT str1 = out + str ( i ) NEW_LINE findStrictlyIncreasingNum ( i + 1 , str1 , n - 1 ) NEW_LINE DEDENT DEDENT n = 3 NEW_LINE findStrictlyIncreasingNum ( 0 , " " , n ) NEW_LINE


import java . lang . Math ; import java . util . * ; class GFG { public static void findX ( int [ ] a , int n ) { Collections . sort ( Arrays . asList ( a ) , null ) ; int itr = a [ n - 1 ] ; int p = ( int ) ( Math . log ( itr ) / Math . log ( 2 ) ) + 1 ; int x = 0 ; for ( int i = 0 ; i < p ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( a [ j ] & ( 1 << i ) ) != 0 ) count ++ ; } if ( count > ( n / 2 ) ) { x += 1 << i ; } } long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += ( x ^ a [ i ] ) ; System . out . println ( " X ▁ = ▁ " + x + " , ▁ Sum ▁ = ▁ " + sum ) ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; findX ( a , n ) ; } }

from math import log2 NEW_LINE def findX ( arr , n ) : NEW_LINE INDENT itr = arr [ 0 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] > itr ) : NEW_LINE INDENT itr = arr [ i ] NEW_LINE DEDENT DEDENT p = int ( log2 ( itr ) ) + 1 NEW_LINE X = 0 NEW_LINE for i in range ( p ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr [ j ] & ( 1 << i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count > int ( n / 2 ) ) : NEW_LINE INDENT X += 1 << i NEW_LINE DEDENT DEDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += ( X ^ arr [ i ] ) NEW_LINE DEDENT print ( " X ▁ = " , X , " , ▁ Sum ▁ = " , sum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE findX ( arr , n ) NEW_LINE DEDENT


class GFG { static int k_sum ( int a [ ] , int n , int k ) { int r = 0 , sum = 0 ; int ans = 0 ; for ( int l = 0 ; l < n ; l ++ ) { while ( sum < k ) { if ( r == n ) break ; else { sum += a [ r ] ; r ++ ; } } if ( sum < k ) break ; ans += n - r + 1 ; sum -= a [ l ] ; } return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 6 , 1 , 2 , 7 } , k = 10 ; int n = a . length ; System . out . println ( k_sum ( a , n , k ) ) ; } }

def k_sum ( a , n , k ) : NEW_LINE INDENT r , sum = 0 , 0 ; NEW_LINE ans = 0 ; NEW_LINE for l in range ( n ) : NEW_LINE INDENT while ( sum < k ) : NEW_LINE INDENT if ( r == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT else : NEW_LINE INDENT sum += a [ r ] ; NEW_LINE r += 1 ; NEW_LINE DEDENT DEDENT if ( sum < k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT ans += n - r + 1 ; NEW_LINE sum -= a [ l ] ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT a = [ 6 , 1 , 2 , 7 ] ; k = 10 ; NEW_LINE n = len ( a ) ; NEW_LINE print ( k_sum ( a , n , k ) ) ; NEW_LINE


import java . lang . * ; class GFG { public static double per ( double a , double b ) { return ( a + b ) ; } public static double area ( double s ) { return ( s / 2 ) ; } public static void main ( String [ ] args ) { double a = 7 , b = 8 , s = 10 ; System . out . println ( per ( a , b ) ) ; System . out . println ( area ( s ) ) ; } }

def per ( a , b ) : NEW_LINE INDENT return ( a + b ) NEW_LINE DEDENT def area ( s ) : NEW_LINE INDENT return ( s / 2 ) NEW_LINE DEDENT a = 7 NEW_LINE b = 8 NEW_LINE s = 10 NEW_LINE print ( per ( a , b ) ) NEW_LINE print ( area ( s ) ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static boolean isRevDiffDivisible ( int x , int k ) { int n = x ; int m = 0 ; int flag ; while ( x > 0 ) { m = m * 10 + x % 10 ; x /= 10 ; } return ( Math . abs ( n - m ) % k == 0 ) ; } static int countNumbers ( int l , int r , int k ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) if ( isRevDiffDivisible ( i , k ) ) ++ count ; return count ; } public static void main ( String args [ ] ) { int l = 35 , r = 45 , k = 5 ; System . out . println ( countNumbers ( l , r , k ) ) ; } }

def isRevDiffDivisible ( x , k ) : NEW_LINE INDENT n = x ; m = 0 NEW_LINE while ( x > 0 ) : NEW_LINE INDENT m = m * 10 + x % 10 NEW_LINE x = x // 10 NEW_LINE DEDENT return ( abs ( n - m ) % k == 0 ) NEW_LINE DEDENT def countNumbers ( l , r , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( isRevDiffDivisible ( i , k ) ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT l = 20 ; r = 23 ; k = 6 NEW_LINE print ( countNumbers ( l , r , k ) ) NEW_LINE


class GenerateArrays { void generateUtil ( int A [ ] , int B [ ] , int C [ ] , int i , int j , int m , int n , int len , boolean flag ) { if ( flag ) { if ( len != 0 ) printArr ( C , len + 1 ) ; for ( int k = i ; k < m ; k ++ ) { if ( len == 0 ) { C [ len ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len , ! flag ) ; } else if ( A [ k ] > C [ len ] ) { C [ len + 1 ] = A [ k ] ; generateUtil ( A , B , C , k + 1 , j , m , n , len + 1 , ! flag ) ; } } } else { for ( int l = j ; l < n ; l ++ ) { if ( B [ l ] > C [ len ] ) { C [ len + 1 ] = B [ l ] ; generateUtil ( A , B , C , i , l + 1 , m , n , len + 1 , ! flag ) ; } } } } void generate ( int A [ ] , int B [ ] , int m , int n ) { int C [ ] = new int [ m + n ] ; generateUtil ( A , B , C , 0 , 0 , m , n , 0 , true ) ; } void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { GenerateArrays generate = new GenerateArrays ( ) ; int A [ ] = { 10 , 15 , 25 } ; int B [ ] = { 5 , 20 , 30 } ; int n = A . length ; int m = B . length ; generate . generate ( A , B , n , m ) ; } }

def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , " ▁ " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def generateUtil ( A , B , C , i , j , m , n , len , flag ) : NEW_LINE INDENT if ( flag ) : NEW_LINE INDENT if ( len ) : NEW_LINE INDENT printArr ( C , len + 1 ) NEW_LINE DEDENT for k in range ( i , m ) : NEW_LINE INDENT if ( not len ) : NEW_LINE INDENT C [ len ] = A [ k ] NEW_LINE generateUtil ( A , B , C , k + 1 , j , m , n , len , not flag ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( A [ k ] > C [ len ] ) : NEW_LINE INDENT C [ len + 1 ] = A [ k ] NEW_LINE generateUtil ( A , B , C , k + 1 , j , m , n , len + 1 , not flag ) NEW_LINE DEDENT DEDENT DEDENT DEDENT else : NEW_LINE INDENT for l in range ( j , n ) : NEW_LINE INDENT if ( B [ l ] > C [ len ] ) : NEW_LINE INDENT C [ len + 1 ] = B [ l ] NEW_LINE generateUtil ( A , B , C , i , l + 1 , m , n , len + 1 , not flag ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def generate ( A , B , m , n ) : NEW_LINE INDENT C = [ ] NEW_LINE for i in range ( m + n + 1 ) : NEW_LINE INDENT C . append ( 0 ) NEW_LINE DEDENT generateUtil ( A , B , C , 0 , 0 , m , n , 0 , True ) NEW_LINE DEDENT A = [ 10 , 15 , 25 ] NEW_LINE B = [ 5 , 20 , 30 ] NEW_LINE n = len ( A ) NEW_LINE m = len ( B ) NEW_LINE generate ( A , B , n , m ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GFG { public static int findNth ( int n ) { int count = 0 ; for ( int curr = 1 ; ; curr ++ ) { int sum = 0 ; for ( int x = curr ; x > 0 ; x = x / 10 ) sum = sum + x % 10 ; if ( sum == 10 ) count ++ ; if ( count == n ) return curr ; } } public static void main ( String [ ] args ) { System . out . print ( findNth ( 5 ) ) ; } }

import itertools NEW_LINE def findNth ( n ) : NEW_LINE INDENT count = 0 NEW_LINE for curr in itertools . count ( ) : NEW_LINE INDENT sum = 0 NEW_LINE x = curr NEW_LINE while ( x ) : NEW_LINE INDENT sum = sum + x % 10 NEW_LINE x = x // 10 NEW_LINE DEDENT if ( sum == 10 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT if ( count == n ) : NEW_LINE INDENT return curr NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( findNth ( 5 ) ) NEW_LINE DEDENT


class GFG { static boolean check ( int mid , int array [ ] , int n , int K ) { int count = 0 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( array [ i ] > mid ) return false ; sum += array [ i ] ; if ( sum > mid ) { count ++ ; sum = array [ i ] ; } } count ++ ; if ( count <= K ) return true ; return false ; } static int solve ( int array [ ] , int n , int K ) { int start = 1 ; int end = 0 ; for ( int i = 0 ; i < n ; i ++ ) { end += array [ i ] ; } int answer = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; if ( check ( mid , array , n , K ) ) { answer = mid ; end = mid - 1 ; } else { start = mid + 1 ; } } return answer ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 2 , 3 , 4 } ; int n = array . length ; int K = 3 ; System . out . println ( solve ( array , n , K ) ) ; } }

def check ( mid , array , n , K ) : NEW_LINE INDENT count = 0 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( array [ i ] > mid ) : NEW_LINE INDENT return False NEW_LINE DEDENT sum += array [ i ] NEW_LINE if ( sum > mid ) : NEW_LINE INDENT count += 1 NEW_LINE sum = array [ i ] NEW_LINE DEDENT DEDENT count += 1 NEW_LINE if ( count <= K ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def solve ( array , n , K ) : NEW_LINE INDENT start = 1 NEW_LINE end = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT end += array [ i ] NEW_LINE DEDENT answer = 0 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE if ( check ( mid , array , n , K ) ) : NEW_LINE INDENT answer = mid NEW_LINE end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT array = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( array ) NEW_LINE K = 3 NEW_LINE print ( solve ( array , n , K ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static float rectanglearea ( float a , float b ) { if ( a < 0 || b < 0 ) return - 1 ; return 2 * a * b ; } public static void main ( String args [ ] ) { float a = 10 , b = 8 ; System . out . println ( rectanglearea ( a , b ) ) ; } }

def rectanglearea ( a , b ) : NEW_LINE INDENT if a < 0 or b < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return 2 * a * b NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 10 , 8 NEW_LINE print ( rectanglearea ( a , b ) ) NEW_LINE DEDENT


class GFG { static long mod_power ( long a , long b , long mod ) { long result = 1l ; while ( b > 0 ) { if ( ( b & 1 ) == 0 ) { result = a * a ; a %= mod ; b /= 2 ; } else { result = result * a ; result %= mod ; } } return result ; } static long nCr_RangeSum ( long n , long r1 , long r2 , long p ) { long num = 1 , den = 1 ; long sum = 0l ; if ( r1 == 0 ) sum += 1l ; for ( int i = 0 ; i < r2 ; i ++ ) { num = ( num * ( n - i ) ) % p ; den = ( den * ( i + 1 ) ) % p ; if ( i - r1 >= - 1 && ( i - r1 + 1 ) % 2 == 0 ) { sum += ( num * mod_power ( den , p - 2 , p ) ) % p ; sum %= p ; } } return sum ; } static long compute ( String A , String B , int N ) { int c0A = 0 , c1A = 0 , c0B = 0 , c1B = 0 ; for ( char c : A . toCharArray ( ) ) { if ( c == '1' ) c1A += 1 ; else if ( c == '0' ) c0A += 1 ; } for ( char c : B . toCharArray ( ) ) { if ( c == '1' ) c1B += 1 ; else if ( c == '0' ) c0B += 1 ; } int max1xor = Math . min ( c0A , c1B ) + Math . min ( c1A , c0B ) ; int min1xor = N - Math . min ( c0B , c0A ) - Math . min ( c1A , c1B ) ; long ans = nCr_RangeSum ( N , min1xor , max1xor , 1000000000 + 7 ) ; return ans ; } public static void main ( String [ ] args ) { int N = 3 ; String A = "010" ; String B = "100" ; System . out . print ( compute ( A , B , N ) ) ; } }

def nCrRangeSum ( n , r1 , r2 , p ) : NEW_LINE INDENT num = den = 1 NEW_LINE sum = 0 NEW_LINE if r1 == 0 : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT for i in range ( r2 ) : NEW_LINE INDENT num = ( num * ( n - i ) ) % p NEW_LINE den = ( den * ( i + 1 ) ) % p NEW_LINE if ( i - r1 >= - 1 and ( i - r1 + 1 ) % 2 == 0 ) : NEW_LINE INDENT sum += ( num * pow ( den , p - 2 , p ) ) % p NEW_LINE sum %= p NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT def compute ( A , B ) : NEW_LINE INDENT c0A = c1A = c0B = c1B = 0 NEW_LINE for c in A : NEW_LINE INDENT if c == '1' : NEW_LINE INDENT c1A += 1 NEW_LINE DEDENT elif c == '0' : NEW_LINE INDENT c0A += 1 NEW_LINE DEDENT DEDENT for c in B : NEW_LINE INDENT if c == '1' : NEW_LINE INDENT c1B += 1 NEW_LINE DEDENT elif c == '0' : NEW_LINE INDENT c0B += 1 NEW_LINE DEDENT DEDENT max1xor = min ( c0A , c1B ) + min ( c1A , c0B ) NEW_LINE min1xor = N - min ( c0B , c0A ) - min ( c1A , c1B ) NEW_LINE ans = nCrRangeSum ( N , min1xor , max1xor , 10 ** 9 + 7 ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE A = "010" NEW_LINE B = "100" NEW_LINE print ( compute ( A , B ) ) NEW_LINE DEDENT


import java . util . * ; class solution { static class TNode { int data ; TNode left ; TNode right ; } static TNode sortedArrayToBST ( int arr [ ] , int start , int end ) { if ( start > end ) return null ; int mid = ( start + end ) / 2 ; TNode root = newNode ( arr [ mid ] ) ; root . left = sortedArrayToBST ( arr , start , mid - 1 ) ; root . right = sortedArrayToBST ( arr , mid + 1 , end ) ; return root ; } static TNode newNode ( int data ) { TNode node = new TNode ( ) ; node . data = data ; node . left = null ; node . right = null ; return node ; } static void printLevelOrder ( TNode root ) { if ( root == null ) return ; Queue < TNode > q = new LinkedList < TNode > ( ) ; q . add ( root ) ; while ( q . size ( ) > 0 ) { TNode node = q . element ( ) ; System . out . print ( node . data + " ▁ " ) ; q . remove ( ) ; if ( node . left != null ) q . add ( node . left ) ; if ( node . right != null ) q . add ( node . right ) ; } } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; TNode root = sortedArrayToBST ( arr , 0 , n - 1 ) ; printLevelOrder ( root ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , d ) : NEW_LINE INDENT self . data = d NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def sortedArrayToBST ( arr ) : NEW_LINE INDENT if not arr : NEW_LINE INDENT return None NEW_LINE DEDENT mid = int ( ( len ( arr ) ) / 2 ) NEW_LINE root = Node ( arr [ mid ] ) NEW_LINE root . left = sortedArrayToBST ( arr [ : mid ] ) NEW_LINE root . right = sortedArrayToBST ( arr [ mid + 1 : ] ) NEW_LINE return root NEW_LINE DEDENT def printLevelOrder ( root ) : NEW_LINE INDENT if not root : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while q != [ ] : NEW_LINE INDENT node = q . pop ( 0 ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE if node . left : NEW_LINE INDENT q . append ( node . left ) NEW_LINE DEDENT if node . right : NEW_LINE INDENT q . append ( node . right ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE root = sortedArrayToBST ( arr ) NEW_LINE printLevelOrder ( root ) NEW_LINE


import java . util . * ; class GFG { static String constructString ( int n ) { String s = " " ; for ( int i = 0 ; i < n ; i ++ ) { s += "10" ; } return s ; } static int binaryToDecimal ( String n ) { String num = n ; int dec_value = 0 ; int base = 1 ; int len = num . length ( ) ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '1' ) dec_value += base ; base = base * 2 ; } return dec_value ; } static int findNumber ( int n ) { String s = constructString ( n - 1 ) ; int num = binaryToDecimal ( s ) ; return num ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( findNumber ( n ) ) ; } }

def constructString ( n ) : NEW_LINE INDENT s = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT s += "10" NEW_LINE DEDENT return s NEW_LINE DEDENT def binaryToDecimal ( n ) : NEW_LINE INDENT num = n NEW_LINE dec_value = 0 NEW_LINE base = 1 NEW_LINE Len = len ( num ) NEW_LINE for i in range ( Len - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( num [ i ] == '1' ) : NEW_LINE INDENT dec_value += base NEW_LINE DEDENT base = base * 2 NEW_LINE DEDENT return dec_value NEW_LINE DEDENT def findNumber ( n ) : NEW_LINE INDENT s = constructString ( n - 1 ) NEW_LINE num = binaryToDecimal ( s ) NEW_LINE return num NEW_LINE DEDENT n = 4 NEW_LINE print ( findNumber ( n ) ) NEW_LINE


class GFG { static int digitSum ( int n ) { int ans = 0 ; while ( n != 0 ) { ans += n % 10 ; n /= 10 ; } return ans ; } static int findInt ( int n , int m ) { int minDigit = ( int ) Math . floor ( ( double ) ( m / 9 ) ) ; int start = ( int ) Math . pow ( 10 , minDigit ) - ( int ) Math . pow ( 10 , minDigit ) % n ; while ( start < Integer . MAX_VALUE ) { if ( digitSum ( start ) == m ) return start ; else start += n ; } return - 1 ; } static public void main ( String args [ ] ) { int n = 13 , m = 32 ; System . out . print ( findInt ( n , m ) ) ; } }

from math import floor , pow NEW_LINE import sys NEW_LINE def digitSum ( n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT ans += n % 10 ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return ans NEW_LINE DEDENT def findInt ( n , m ) : NEW_LINE INDENT minDigit = floor ( m / 9 ) NEW_LINE start = ( int ( pow ( 10 , minDigit ) ) - int ( pow ( 10 , minDigit ) ) % n ) NEW_LINE while ( start < sys . maxsize ) : NEW_LINE INDENT if ( digitSum ( start ) == m ) : NEW_LINE INDENT return start NEW_LINE DEDENT else : NEW_LINE INDENT start += n NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 13 NEW_LINE m = 32 NEW_LINE print ( findInt ( n , m ) ) NEW_LINE DEDENT


class GFG { static String findLargest ( int N ) { String largest = " " ; for ( int i = 0 ; i < N ; i ++ ) largest += ' F ' ; return largest ; } static String findSmallest ( int N ) { String smallest = "1" ; for ( int i = 0 ; i < N - 1 ; i ++ ) smallest += '0' ; return smallest ; } static void print ( int largest ) { System . out . println ( " Largest : ▁ " + findLargest ( largest ) ) ; System . out . println ( " Smallest : ▁ " + findSmallest ( largest ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; print ( N ) ; } }

def findLargest ( N ) : NEW_LINE INDENT largest = ' F ' * N NEW_LINE return largest ; NEW_LINE DEDENT def findSmallest ( N ) : NEW_LINE INDENT smallest = '1' + '0' * ( N - 1 ) NEW_LINE return smallest ; NEW_LINE DEDENT def printAns ( largest ) : NEW_LINE INDENT print ( " Largest : ▁ " , findLargest ( largest ) ) ; NEW_LINE print ( " Smallest : ▁ " , findSmallest ( largest ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; NEW_LINE printAns ( N ) ; NEW_LINE DEDENT


class GFG { static int TotalWays ( int n , int s , int k ) { int [ ] dp = new int [ n ] ; dp [ s - 1 ] = 1 ; for ( int i = s ; i < n ; i ++ ) { int idx = Math . max ( s - 1 , i - k ) ; for ( int j = idx ; j < i ; j ++ ) { dp [ i ] += dp [ j ] ; } } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 2 ; int s = 2 ; System . out . print ( " Total ▁ Ways ▁ = ▁ " + TotalWays ( n , s , k ) ) ; } }

def TotalWays ( n , s , k ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE dp [ s - 1 ] = 1 NEW_LINE for i in range ( s , n ) : NEW_LINE INDENT idx = max ( s - 1 , i - k ) NEW_LINE for j in range ( idx , i ) : NEW_LINE INDENT dp [ i ] += dp [ j ] NEW_LINE DEDENT DEDENT return dp [ n - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE k = 2 NEW_LINE s = 2 NEW_LINE print ( " Total ▁ Ways ▁ = ▁ " , TotalWays ( n , s , k ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static int PointInKSquares ( int n , int a [ ] , int k ) { Arrays . sort ( a ) ; return a [ n - k ] ; } public static void main ( String [ ] args ) { int k = 2 ; int [ ] a = { 1 , 2 , 3 , 4 } ; int n = a . length ; int x = PointInKSquares ( n , a , k ) ; System . out . println ( " ( " + x + " , ▁ " + x + " ) " ) ; } }

def PointInKSquares ( n , a , k ) : NEW_LINE INDENT a . sort ( ) NEW_LINE return a [ n - k ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 2 NEW_LINE a = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( a ) NEW_LINE x = PointInKSquares ( n , a , k ) NEW_LINE print ( " ( " , x , " , " , x , " ) " ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static String printVertically ( String s ) { String result = " " ; HashMap < Integer , String > map = new HashMap < > ( ) ; Scanner sc = new Scanner ( s ) ; int max = Integer . MIN_VALUE ; while ( sc . hasNext ( ) ) { String st = sc . next ( ) ; max = Math . max ( max , st . length ( ) ) ; } sc = new Scanner ( s ) ; while ( sc . hasNext ( ) ) { String st = sc . next ( ) ; for ( int i = 0 ; i < st . length ( ) ; i ++ ) { char ch = st . charAt ( i ) ; map . put ( i , map . getOrDefault ( i , " " ) + ch ) ; } for ( int i = st . length ( ) ; i < max ; i ++ ) { map . put ( i , map . getOrDefault ( i , " " ) + " $ " ) ; } } for ( int i = 0 ; i < max ; i ++ ) { String a = map . get ( i ) ; result += a + " ▁ " ; } return result ; } public static void main ( String [ ] args ) { String st = " ge ▁ ek ▁ sfor ▁ ge ▁ eks " ; System . out . println ( printVertically ( st ) ) ; } }

def printVertically ( s ) : NEW_LINE INDENT s = s . split ( " ▁ " ) NEW_LINE result = " " NEW_LINE map = dict ( ) NEW_LINE mx = 0 NEW_LINE for i in s : NEW_LINE INDENT mx = max ( mx , len ( i ) ) NEW_LINE DEDENT n = mx NEW_LINE i = 0 NEW_LINE while ( i < mx ) : NEW_LINE INDENT for j in s : NEW_LINE INDENT if i >= len ( j ) : NEW_LINE INDENT map [ i ] = map . get ( i , " " ) + " $ " NEW_LINE DEDENT else : NEW_LINE INDENT map [ i ] = map . get ( i , " " ) + j [ i ] NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT for j in range ( mx ) : NEW_LINE INDENT result += map [ j ] + " ▁ " NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT st = " ge ▁ ek ▁ sfor ▁ ge ▁ eks " NEW_LINE print ( printVertically ( st ) ) NEW_LINE DEDENT


class GFG { static int getMaximumSumWithLCMN ( int N ) { int sum = 0 ; int LIM = ( int ) Math . sqrt ( N ) ; for ( int i = 1 ; i <= LIM ; i ++ ) { if ( N % i == 0 ) { if ( i == ( N / i ) ) sum += i ; else sum += ( i + N / i ) ; } } return sum ; } public static void main ( String [ ] args ) { int N = 12 ; System . out . println ( getMaximumSumWithLCMN ( N ) ) ; } }

import math NEW_LINE def getMaximumSumWithLCMN ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE LIM = int ( math . sqrt ( N ) ) NEW_LINE for i in range ( 1 , LIM + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( i == ( N // i ) ) : NEW_LINE INDENT sum = sum + i NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + ( i + N // i ) NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT N = 12 NEW_LINE print ( getMaximumSumWithLCMN ( N ) ) NEW_LINE


public class GFG { static final int MAX_CHAR = 26 ; static boolean arekAnagrams ( String str1 , String str2 , int k ) { int n = str1 . length ( ) ; if ( str2 . length ( ) != n ) return false ; int [ ] count1 = new int [ MAX_CHAR ] ; int [ ] count2 = new int [ MAX_CHAR ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) count1 [ str1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) count2 [ str2 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count1 [ i ] > count2 [ i ] ) count = count + Math . abs ( count1 [ i ] - count2 [ i ] ) ; return ( count <= k ) ; } public static void main ( String args [ ] ) { String str1 = " anagram " ; String str2 = " grammar " ; int k = 2 ; if ( arekAnagrams ( str1 , str2 , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

MAX_CHAR = 26 NEW_LINE def arekAnagrams ( str1 , str2 , k ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE if ( len ( str2 ) != n ) : NEW_LINE INDENT return False NEW_LINE DEDENT count1 = [ 0 ] * MAX_CHAR NEW_LINE count2 = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT count1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT count2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( MAX_CHAR ) : NEW_LINE INDENT if ( count1 [ i ] > count2 [ i ] ) : NEW_LINE INDENT count = count + abs ( count1 [ i ] - count2 [ i ] ) NEW_LINE DEDENT DEDENT return ( count <= k ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " anagram " NEW_LINE str2 = " grammar " NEW_LINE k = 2 NEW_LINE if ( arekAnagrams ( str1 , str2 , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static void findNumbers ( int n , int b ) { double even = 0 , odd = 0 ; if ( b % 2 == 0 ) { even = Math . pow ( b , n ) - 2 ; odd = Math . pow ( b , n ) - 1 ; } else { even = Math . pow ( b , n ) - 1 ; odd = Math . pow ( b , n ) - 2 ; } System . out . println ( " Even ▁ Number ▁ = ▁ " + ( int ) even ) ; System . out . print ( " Odd ▁ Number ▁ = ▁ " + ( int ) odd ) ; } public static void main ( String [ ] args ) { int N = 2 , B = 5 ; findNumbers ( N , B ) ; } }

def findNumbers ( n , b ) : NEW_LINE INDENT even = 0 ; NEW_LINE odd = 0 ; NEW_LINE if ( b % 2 == 0 ) : NEW_LINE INDENT even = pow ( b , n ) - 2 ; NEW_LINE odd = pow ( b , n ) - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even = pow ( b , n ) - 1 ; NEW_LINE odd = pow ( b , n ) - 2 ; NEW_LINE DEDENT print ( " Even ▁ Number ▁ = ▁ " , int ( even ) ) ; NEW_LINE print ( " Odd ▁ Number ▁ = ▁ " , int ( odd ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 ; NEW_LINE B = 5 ; NEW_LINE findNumbers ( N , B ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void count_unique ( HashSet < Integer > s , int n ) { if ( s . contains ( n ) ) return ; s . add ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = n / 10 ; } count_unique ( s , n ) ; } public static void main ( String [ ] args ) { int n = 10 ; HashSet < Integer > s = new HashSet < > ( ) ; count_unique ( s , n ) ; System . out . println ( s . size ( ) ) ; } }

def count_unique ( s , n ) : NEW_LINE INDENT if ( s . count ( n ) ) : NEW_LINE INDENT return ; NEW_LINE DEDENT s . append ( n ) ; NEW_LINE n += 1 ; NEW_LINE while ( n % 10 == 0 ) : NEW_LINE INDENT n = n // 10 ; NEW_LINE DEDENT count_unique ( s , n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE s = [ ] NEW_LINE count_unique ( s , n ) NEW_LINE print ( len ( s ) ) NEW_LINE DEDENT


class Main { static int findMaximum ( int arr [ ] , int low , int high ) { int max = arr [ low ] ; int i ; for ( i = low ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 30 , 40 , 50 , 60 , 70 , 23 , 20 } ; int n = arr . length ; System . out . println ( " The ▁ maximum ▁ element ▁ is ▁ " + findMaximum ( arr , 0 , n - 1 ) ) ; } }

def findMaximum ( arr , low , high ) : NEW_LINE INDENT max = arr [ low ] NEW_LINE i = low NEW_LINE for i in range ( high + 1 ) : NEW_LINE INDENT if arr [ i ] > max : NEW_LINE INDENT max = arr [ i ] NEW_LINE DEDENT DEDENT return max NEW_LINE DEDENT arr = [ 1 , 30 , 40 , 50 , 60 , 70 , 23 , 20 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ maximum ▁ element ▁ is ▁ % d " % findMaximum ( arr , 0 , n - 1 ) ) NEW_LINE


import java . io . * ; class GFG { static int countOddSquares ( int n , int m ) { return ( int ) Math . pow ( m , 0.5 ) - ( int ) Math . pow ( n - 1 , 0.5 ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 100 ; System . out . println ( " Count ▁ is ▁ " + ( ( m - n + 1 ) - countOddSquares ( n , m ) ) ) ; } }

def countOddSquares ( n , m ) : NEW_LINE INDENT return ( int ( pow ( m , 0.5 ) ) - int ( pow ( n - 1 , 0.5 ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; m = 100 ; NEW_LINE print ( " Count ▁ is " , ( m - n + 1 ) - countOddSquares ( n , m ) ) NEW_LINE DEDENT


public class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void findRatio ( int m , int n ) { int Am = m - 5 , An = n - 5 ; int numerator = Am / GCD ( Am , An ) ; int denominator = An / GCD ( Am , An ) ; System . out . println ( numerator + " / " + denominator ) ; } public static void main ( String args [ ] ) { int m = 10 , n = 20 ; findRatio ( m , n ) ; } }

from fractions import gcd NEW_LINE def findRatio ( m , n ) : NEW_LINE INDENT Am = m - 5 NEW_LINE An = n - 5 NEW_LINE numerator = Am // ( gcd ( Am , An ) ) NEW_LINE denominator = An // ( gcd ( Am , An ) ) NEW_LINE print ( numerator , ' / ' , denominator ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 10 NEW_LINE n = 20 NEW_LINE findRatio ( m , n ) NEW_LINE DEDENT


class GFG { static void nDigitPerfectSquares ( int n ) { int smallest = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n - 1 ) ) ) , 2 ) ; System . out . print ( smallest + " ▁ " ) ; int largest = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n ) ) ) - 1 , 2 ) ; System . out . print ( largest ) ; } public static void main ( String args [ ] ) { int n = 4 ; nDigitPerfectSquares ( n ) ; } }

import math NEW_LINE def nDigitPerfectSquares ( n ) : NEW_LINE INDENT print ( pow ( math . ceil ( math . sqrt ( pow ( 10 , n - 1 ) ) ) , 2 ) , end = " ▁ " ) ; NEW_LINE print ( pow ( math . ceil ( math . sqrt ( pow ( 10 , n ) ) ) - 1 , 2 ) ) ; NEW_LINE DEDENT n = 4 ; NEW_LINE nDigitPerfectSquares ( n ) ; NEW_LINE


class GFG { public static boolean isMagic ( int n ) { int sum = 0 ; while ( n > 0 || sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum == 1 ) ; } public static void main ( String args [ ] ) { int n = 1234 ; if ( isMagic ( n ) ) System . out . println ( " Magic ▁ Number " ) ; else System . out . println ( " Not ▁ a ▁ magic ▁ Number " ) ; } }

def isMagic ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n > 0 or sum > 9 ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT n = sum ; NEW_LINE sum = 0 ; NEW_LINE DEDENT sum = sum + n % 10 ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return True if ( sum == 1 ) else False ; NEW_LINE DEDENT n = 1234 ; NEW_LINE if ( isMagic ( n ) ) : NEW_LINE INDENT print ( " Magic ▁ Number " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ magic ▁ Number " ) ; NEW_LINE DEDENT


class GFG { static long countPairs ( int arr [ ] , int n ) { long count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] * arr [ j ] == arr [ i ] + arr [ j ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 3 , 2 , 0 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }

def countPairs ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] * arr [ j ] == arr [ i ] + arr [ j ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 0 , 3 , 2 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countPairs ( arr , n ) ) ; NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int nextPerfectSquare ( int N ) { int nextN = ( int ) Math . floor ( Math . sqrt ( N ) ) + 1 ; return nextN * nextN ; } public static void main ( String args [ ] ) { int n = 35 ; System . out . println ( nextPerfectSquare ( n ) ) ; } }

import math NEW_LINE def nextPerfectSquare ( N ) : NEW_LINE INDENT nextN = math . floor ( math . sqrt ( N ) ) + 1 NEW_LINE return nextN * nextN NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 35 NEW_LINE print ( nextPerfectSquare ( N ) ) NEW_LINE DEDENT


class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; boolean isBST ( ) { return isBSTUtil ( root , Integer . MIN_VALUE , Integer . MAX_VALUE ) ; } boolean isBSTUtil ( Node node , int min , int max ) { if ( node == null ) return true ; if ( node . data < min || node . data > max ) return false ; return ( isBSTUtil ( node . left , min , node . data - 1 ) && isBSTUtil ( node . right , node . data + 1 , max ) ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 4 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 3 ) ; if ( tree . isBST ( ) ) System . out . println ( " IS ▁ BST " ) ; else System . out . println ( " Not ▁ a ▁ BST " ) ; } }

INT_MAX = 4294967296 NEW_LINE INT_MIN = - 4294967296 NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isBST ( node ) : NEW_LINE INDENT return ( isBSTUtil ( node , INT_MIN , INT_MAX ) ) NEW_LINE DEDENT def isBSTUtil ( node , mini , maxi ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return True NEW_LINE DEDENT if node . data < mini or node . data > maxi : NEW_LINE INDENT return False NEW_LINE DEDENT return ( isBSTUtil ( node . left , mini , node . data - 1 ) and isBSTUtil ( node . right , node . data + 1 , maxi ) ) NEW_LINE DEDENT root = Node ( 4 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . left = Node ( 1 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE if ( isBST ( root ) ) : NEW_LINE INDENT print " Is ▁ BST " NEW_LINE DEDENT else : NEW_LINE INDENT print " Not ▁ a ▁ BST " NEW_LINE DEDENT


class GFG { static void printValueOfPi ( int N ) { double pi = 2 * Math . acos ( 0.0 ) ; System . out . println ( pi ) ; } public static void main ( String [ ] args ) { int N = 4 ; printValueOfPi ( N ) ; } }

from math import acos NEW_LINE def printValueOfPi ( N ) : NEW_LINE INDENT pi = round ( 2 * acos ( 0.0 ) , N ) ; NEW_LINE print ( pi ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; NEW_LINE printValueOfPi ( N ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int __gcd ( int a , int b ) { if ( a == 0 || b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static int find_Gcd ( int crr [ ] , int n ) { int i ; int result = crr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) result = __gcd ( crr [ i ] , result ) ; return result ; } static void profitRatio ( int amountArr [ ] , int timeTrr [ ] , int n ) { int i ; int crr [ ] = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) crr [ i ] = amountArr [ i ] * timeTrr [ i ] ; int Share = find_Gcd ( crr , n ) ; for ( i = 0 ; i < n - 1 ; i ++ ) System . out . print ( crr [ i ] / Share + " ▁ : ▁ " ) ; System . out . print ( crr [ i ] / Share ) ; } public static void main ( String [ ] args ) { int amountArr [ ] = { 5000 , 6000 , 1000 } ; int timeTrr [ ] = { 6 , 6 , 12 } ; int n = amountArr . length ; profitRatio ( amountArr , timeTrr , n ) ; } }

def __gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 or b == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return __gcd ( a - b , b ) ; NEW_LINE DEDENT return __gcd ( a , b - a ) ; NEW_LINE DEDENT def find_Gcd ( crr , n ) : NEW_LINE INDENT result = crr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT result = __gcd ( crr [ i ] , result ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def profitRatio ( amountArr , timeTrr , n ) : NEW_LINE INDENT i = 0 ; NEW_LINE crr = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT crr [ i ] = amountArr [ i ] * timeTrr [ i ] ; NEW_LINE DEDENT Share = find_Gcd ( crr , n ) ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT print ( int ( crr [ i ] / Share ) , end = " ▁ : ▁ " ) ; NEW_LINE DEDENT print ( int ( crr [ i + 1 ] / Share ) ) ; NEW_LINE DEDENT amountArr = [ 5000 , 6000 , 1000 ] ; NEW_LINE timeTrr = [ 6 , 6 , 12 ] ; NEW_LINE n = len ( amountArr ) ; NEW_LINE profitRatio ( amountArr , timeTrr , n ) ; NEW_LINE


import java . util . Arrays ; import java . util . Collections ; class GFG { public static int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- > 0 ) { sum += cur ; cur += 2 ; } return sum ; } public static void main ( String [ ] args ) { int k = 3 ; System . out . print ( kthgroupsum ( k ) ) ; } }

def kthgroupsum ( k ) : NEW_LINE INDENT cur = int ( ( k * ( k - 1 ) ) + 1 ) NEW_LINE sum = 0 NEW_LINE while k : NEW_LINE INDENT sum += cur NEW_LINE cur += 2 NEW_LINE k = k - 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT k = 3 NEW_LINE print ( kthgroupsum ( k ) ) NEW_LINE


import java . io . * ; class GFG { static void pattern ( ) { int i , j , k = 0 , spaces = 1 , n = 7 ; for ( i = n ; i >= 1 ; i -- ) { for ( j = 1 ; j <= i ; j ++ ) { System . out . print ( i ) ; } if ( i != n ) { for ( k = 1 ; k <= spaces ; k ++ ) { System . out . print ( " ▁ " ) ; } spaces = spaces + 2 ; } for ( j = i ; j >= 1 ; j -- ) { if ( j != n ) System . out . print ( i ) ; } System . out . println ( ) ; } spaces = spaces - 4 ; for ( i = 2 ; i <= n ; i ++ ) { for ( j = 1 ; j <= i ; j ++ ) { System . out . print ( i ) ; } if ( i != n ) { for ( k = 1 ; k <= spaces ; k ++ ) { System . out . printf ( " ▁ " ) ; } spaces = spaces - 2 ; } for ( j = i ; j >= 1 ; j -- ) { if ( j != n ) System . out . print ( i ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { pattern ( ) ; } }

def pattern ( ) : NEW_LINE INDENT k = 0 NEW_LINE spaces = 1 NEW_LINE n = 7 NEW_LINE for i in range ( n , 0 , - 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT if ( i != n ) : NEW_LINE INDENT for k in range ( 1 , spaces + 1 ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT spaces = spaces + 2 NEW_LINE DEDENT for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT if ( j != n ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT spaces = spaces - 4 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT if ( i != n ) : NEW_LINE INDENT for k in range ( 1 , spaces + 1 ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT spaces = spaces - 2 NEW_LINE DEDENT for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT if ( j != n ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT pattern ( ) NEW_LINE


class GFG { static void printPermutation ( int n , int k ) { if ( k >= n || ( n % 2 == 0 && k == 0 ) ) { System . out . print ( - 1 ) ; return ; } int per [ ] = new int [ n + 1 ] , i ; for ( i = 1 ; i <= n ; i ++ ) { per [ i ] = i ; } int cnt = n - 1 ; i = 2 ; while ( i < n ) { if ( cnt - 1 > k ) { swap ( per , i , i + 1 ) ; cnt -= 2 ; } else if ( cnt - 1 == k ) { swap ( per , 1 , i ) ; cnt -- ; } else { break ; } i += 2 ; } for ( i = 1 ; i <= n ; i ++ ) { System . out . print ( per [ i ] + " ▁ " ) ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int n = 4 , k = 3 ; printPermutation ( n , k ) ; } }

def printPermutation ( n , k ) : NEW_LINE INDENT if ( k >= n or ( n % 2 == 0 and k == 0 ) ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE return ; NEW_LINE DEDENT per = [ 0 ] * ( n + 1 ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT per [ i ] = i ; NEW_LINE DEDENT cnt = n - 1 ; NEW_LINE i = 2 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( cnt - 1 > k ) : NEW_LINE INDENT t = per [ i ] ; NEW_LINE per [ i ] = per [ i + 1 ] ; NEW_LINE per [ i + 1 ] = t ; NEW_LINE cnt -= 2 ; NEW_LINE DEDENT elif ( cnt - 1 == k ) : NEW_LINE INDENT t = per [ 1 ] ; NEW_LINE per [ 1 ] = per [ i ] ; NEW_LINE per [ i ] = t ; NEW_LINE cnt -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT i += 2 ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( per [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT n = 4 ; NEW_LINE k = 3 ; NEW_LINE printPermutation ( n , k ) ; NEW_LINE


class GFG { static int MAX = 100001 ; static int [ ] prime = new int [ MAX + 1 ] ; static int [ ] countdiv = new int [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 2 ; i * i <= MAX ; ++ i ) { if ( prime [ i ] == 0 ) for ( int j = i * 2 ; j <= MAX ; j += i ) prime [ j ] = i ; } for ( int i = 1 ; i < MAX ; ++ i ) if ( prime [ i ] == 0 ) prime [ i ] = i ; } static int largestGCDSubsequence ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int element = arr [ i ] ; while ( element > 1 ) { int div = prime [ element ] ; ++ countdiv [ div ] ; ans = Math . max ( ans , countdiv [ div ] ) ; while ( element % div == 0 ) element /= div ; } } return ans ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int arr [ ] = { 10 , 15 , 7 , 25 , 9 , 35 } ; int size = arr . length ; System . out . println ( largestGCDSubsequence ( arr , size ) ) ; } }

import math as mt NEW_LINE MAX = 100001 NEW_LINE prime = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE countdiv = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for i in range ( 2 , mt . ceil ( mt . sqrt ( MAX + 1 ) ) ) : NEW_LINE INDENT if ( prime [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i * 2 , MAX + 1 , i ) : NEW_LINE INDENT prime [ j ] = i NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT if ( prime [ i ] == 0 ) : NEW_LINE INDENT prime [ i ] = i NEW_LINE DEDENT DEDENT DEDENT def largestGCDSubsequence ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT element = arr [ i ] NEW_LINE while ( element > 1 ) : NEW_LINE INDENT div = prime [ element ] NEW_LINE countdiv [ div ] += 1 NEW_LINE ans = max ( ans , countdiv [ div ] ) NEW_LINE while ( element % div == 0 ) : NEW_LINE INDENT element = element // div NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT SieveOfEratosthenes ( ) NEW_LINE arr = [ 10 , 15 , 7 , 25 , 9 , 35 ] NEW_LINE size = len ( arr ) NEW_LINE print ( largestGCDSubsequence ( arr , size ) ) NEW_LINE


import java . util . HashSet ; import java . util . Set ; public class GFG { static int distinct_year ( String str ) { String str2 = " " ; Set < String > uniqueDates = new HashSet < > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isDigit ( str . charAt ( i ) ) ) { str2 += ( str . charAt ( i ) ) ; } if ( str . charAt ( i ) == ' - ' ) { str2 = " " ; } if ( str2 . length ( ) == 4 ) { uniqueDates . add ( str2 ) ; str2 = " " ; } } return uniqueDates . size ( ) ; } static public void main ( String [ ] args ) { String str = " UN ▁ was ▁ established ▁ on ▁ 24-10-1945 . " + " India ▁ got ▁ freedom ▁ on ▁ 15-08-1947 . " ; System . out . println ( distinct_year ( str ) ) ; } }

def distinct_years ( str ) : NEW_LINE INDENT str2 = " " NEW_LINE uniqueDates = set ( ) NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if str [ i ] . isdigit ( ) : NEW_LINE INDENT str2 += str [ i ] NEW_LINE DEDENT if str [ i ] == ' - ' : NEW_LINE INDENT str2 = " " NEW_LINE DEDENT if len ( str2 ) == 4 : NEW_LINE INDENT uniqueDates . add ( str2 ) NEW_LINE str2 = " " NEW_LINE DEDENT DEDENT return len ( uniqueDates ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str =   " UN was established on 24 - 10 - 1945. \ NEW_LINE INDENT India got freedom on 15 - 0 8 - 1947. " NEW_LINE DEDENT print ( distinct_years ( str ) ) NEW_LINE DEDENT


class GFG { static int distinct ( int [ ] arr , int n ) { int count = 0 ; if ( n == 1 ) return 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] != arr [ i + 1 ] ) count += 1 ; } else { if ( arr [ i ] != arr [ i + 1 ] || arr [ i ] != arr [ i - 1 ] ) count += 1 ; } } if ( arr [ n - 1 ] != arr [ n - 2 ] ) count += 1 ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 , 1 , 0 } ; int n = arr . length ; System . out . println ( distinct ( arr , n ) ) ; } }

def distinct ( arr ) : NEW_LINE INDENT count = 0 NEW_LINE if len ( arr ) == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT for i in range ( 0 , len ( arr ) - 1 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT elif ( i > 0 & i < len ( arr ) - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] or arr [ i ] != arr [ i - 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT if ( arr [ len ( arr ) - 1 ] != arr [ len ( arr ) - 2 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] NEW_LINE print ( distinct ( arr ) ) NEW_LINE


class Test { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a = 98 , b = 56 ; System . out . println ( " GCD ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + gcd ( a , b ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT a = 98 NEW_LINE b = 56 NEW_LINE if ( gcd ( a , b ) ) : NEW_LINE INDENT print ( ' GCD ▁ of ' , a , ' and ' , b , ' is ' , gcd ( a , b ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' not ▁ found ' ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void printPrime ( int n ) { for ( int i = 2 ; i <= n ; i ++ ) { if ( isPrime ( i ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 7 ; printPrime ( n ) ; } }

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def printPrime ( n ) : NEW_LINE INDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if isPrime ( i ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 7 NEW_LINE printPrime ( n ) NEW_LINE


import java . util . * ; class GFG { static boolean isAnagram ( String s1 , String s2 ) { s1 = sortString ( s1 ) ; s2 = sortString ( s2 ) ; return ( s1 . equals ( s2 ) ) ; } public static String sortString ( String inputString ) { char tempArray [ ] = inputString . toCharArray ( ) ; Arrays . sort ( tempArray ) ; return new String ( tempArray ) ; } static int CountSteps ( char [ ] s1 , char [ ] s2 , int size ) { int i = 0 , j = 0 ; int result = 0 ; while ( i < size ) { j = i ; while ( s1 [ j ] != s2 [ i ] ) { j += 1 ; } while ( i < j ) { char temp = s1 [ j ] ; s1 [ j ] = s1 [ j - 1 ] ; s1 [ j - 1 ] = temp ; j -= 1 ; result += 1 ; } i += 1 ; } return result ; } public static void main ( String [ ] args ) { String s1 = " abcd " ; String s2 = " cdab " ; int size = s2 . length ( ) ; if ( isAnagram ( s1 , s2 ) ) System . out . println ( CountSteps ( s1 . toCharArray ( ) , s2 . toCharArray ( ) , size ) ) ; else System . out . println ( - 1 ) ; } }

def isAnagram ( s1 , s2 ) : NEW_LINE INDENT s1 = list ( s1 ) ; NEW_LINE s2 = list ( s2 ) ; NEW_LINE s1 = s1 . sort ( ) ; NEW_LINE s2 = s2 . sort ( ) ; NEW_LINE if ( s1 == s2 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT def CountSteps ( s1 , s2 , size ) : NEW_LINE INDENT s1 = list ( s1 ) ; NEW_LINE s2 = list ( s2 ) ; NEW_LINE i = 0 ; NEW_LINE j = 0 ; NEW_LINE result = 0 ; NEW_LINE while ( i < size ) : NEW_LINE INDENT j = i ; NEW_LINE while ( s1 [ j ] != s2 [ i ] ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT while ( i < j ) : NEW_LINE INDENT temp = s1 [ j ] ; NEW_LINE s1 [ j ] = s1 [ j - 1 ] ; NEW_LINE s1 [ j - 1 ] = temp ; NEW_LINE j -= 1 ; NEW_LINE result += 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " abcd " ; NEW_LINE s2 = " cdab " ; NEW_LINE size = len ( s2 ) ; NEW_LINE if ( isAnagram ( s1 , s2 ) ) : NEW_LINE INDENT print ( CountSteps ( s1 , s2 , size ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT DEDENT


public class GFG { static int CHARS = 26 ; static boolean isValidString ( String str ) { int freq [ ] = new int [ CHARS ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } int i , freq1 = 0 , count_freq1 = 0 ; for ( i = 0 ; i < CHARS ; i ++ ) { if ( freq [ i ] != 0 ) { freq1 = freq [ i ] ; count_freq1 = 1 ; break ; } } int j , freq2 = 0 , count_freq2 = 0 ; for ( j = i + 1 ; j < CHARS ; j ++ ) { if ( freq [ j ] != 0 ) { if ( freq [ j ] == freq1 ) { count_freq1 ++ ; } else { count_freq2 = 1 ; freq2 = freq [ j ] ; break ; } } } for ( int k = j + 1 ; k < CHARS ; k ++ ) { if ( freq [ k ] != 0 ) { if ( freq [ k ] == freq1 ) { count_freq1 ++ ; } if ( freq [ k ] == freq2 ) { count_freq2 ++ ; } else { return false ; } } if ( count_freq1 > 1 && count_freq2 > 1 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = " abcbc " ; if ( isValidString ( str ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

CHARS = 26 NEW_LINE def isValidString ( str ) : NEW_LINE INDENT freq = [ 0 ] * CHARS NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT freq1 = 0 NEW_LINE count_freq1 = 0 NEW_LINE for i in range ( CHARS ) : NEW_LINE INDENT if ( freq [ i ] != 0 ) : NEW_LINE INDENT freq1 = freq [ i ] NEW_LINE count_freq1 = 1 NEW_LINE break NEW_LINE DEDENT DEDENT freq2 = 0 NEW_LINE count_freq2 = 0 NEW_LINE for j in range ( i + 1 , CHARS ) : NEW_LINE INDENT if ( freq [ j ] != 0 ) : NEW_LINE INDENT if ( freq [ j ] == freq1 ) : NEW_LINE INDENT count_freq1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_freq2 = 1 NEW_LINE freq2 = freq [ j ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT for k in range ( j + 1 , CHARS ) : NEW_LINE INDENT if ( freq [ k ] != 0 ) : NEW_LINE INDENT if ( freq [ k ] == freq1 ) : NEW_LINE INDENT count_freq1 += 1 NEW_LINE DEDENT if ( freq [ k ] == freq2 ) : NEW_LINE INDENT count_freq2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( count_freq1 > 1 and count_freq2 > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " abcbc " NEW_LINE if ( isValidString ( str ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int n = 4 ; static int count ( int arr [ ] [ ] ) { int diag1 = 0 , diag2 = 0 ; int row = 0 , col = 0 , count = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { diag1 += arr [ i ] [ i ] ; diag2 += arr [ i ] [ j ] ; } for ( int i = 0 ; i < n ; i ++ ) { row = 0 ; col = 0 ; for ( int j = 0 ; j < n ; j ++ ) { row = row + arr [ i ] [ j ] ; } for ( int j = 0 ; j < n ; j ++ ) { col = col + arr [ j ] [ i ] ; } if ( ( row == diag1 ) || ( row == diag2 ) ) count ++ ; if ( ( col == diag1 ) || ( col == diag2 ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 7 , 2 , 3 , 5 } , { 4 , 5 , 6 , 3 } , { 7 , 9 , 10 , 12 } , { 1 , 5 , 4 , 3 } } ; System . out . println ( count ( arr ) ) ; } }

n = 4 NEW_LINE def count ( arr ) : NEW_LINE INDENT diag1 = 0 ; diag2 = 0 ; row = 0 NEW_LINE col = 0 ; count = 0 ; j = n - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT diag1 += arr [ i ] [ i ] NEW_LINE diag2 += arr [ i ] [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT row = 0 ; col = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT row += arr [ i ] [ j ] NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT col += arr [ j ] [ i ] NEW_LINE DEDENT if ( ( row == diag1 ) or ( row == diag2 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( ( col == diag1 ) or ( col == diag2 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ [ 7 , 2 , 3 , 5 ] , [ 4 , 5 , 6 , 3 ] , [ 7 , 9 , 10 , 12 ] , [ 1 , 5 , 4 , 3 ] ] NEW_LINE print ( count ( arr ) ) NEW_LINE


class GFG { static final int MAX = 1000000 ; static boolean [ ] prime = new boolean [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static void solve ( int [ ] arr , int n , int k ) { int c = 0 ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { sum += arr [ i ] ; c = 0 ; } } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 5 , k = 2 ; int [ ] arr = { 2 , 3 , 5 , 7 , 11 } ; solve ( arr , n , k ) ; } }

def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE prime [ 0 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= MAX : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , MAX + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def solve ( arr , n , k ) : NEW_LINE INDENT c = 0 NEW_LINE Sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT c += 1 NEW_LINE if c % k == 0 : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE c = 0 NEW_LINE DEDENT DEDENT DEDENT print ( Sum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT MAX = 1000000 NEW_LINE prime = [ True ] * ( MAX + 1 ) NEW_LINE SieveOfEratosthenes ( ) NEW_LINE n , k = 5 , 2 NEW_LINE arr = [ 2 , 3 , 5 , 7 , 11 ] NEW_LINE solve ( arr , n , k ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void find_idx ( int arr [ ] , int n ) { Vector answer = new Vector ( ) ; int sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( sum == arr [ i ] ) { answer . add ( i ) ; } sum += arr [ i ] ; } if ( answer . size ( ) == 0 ) { System . out . println ( " - 1" ) ; return ; } for ( int i = answer . size ( ) - 1 ; i >= 0 ; i -- ) System . out . print ( answer . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 36 , 2 , 17 , 6 , 6 , 5 } ; int n = arr . length ; find_idx ( arr , n ) ; } }

def find_idx ( arr , n ) : NEW_LINE INDENT answer = [ ] NEW_LINE _sum = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( _sum == arr [ i ] ) : NEW_LINE INDENT answer . append ( i ) NEW_LINE DEDENT _sum += arr [ i ] NEW_LINE DEDENT if ( len ( answer ) == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT for i in range ( len ( answer ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( answer [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 36 , 2 , 17 , 6 , 6 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE find_idx ( arr , n ) NEW_LINE


import java . io . * ; class GFG { public static void floorAndCeil ( int arr [ ] , int x ) { int n = arr . length ; int fInd = - 1 , cInd = - 1 ; int fDist = Integer . MAX_VALUE , cDist = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= x && cDist > ( arr [ i ] - x ) ) { cInd = i ; cDist = arr [ i ] - x ; } if ( arr [ i ] <= x && fDist > ( x - arr [ i ] ) ) { fInd = i ; fDist = x - arr [ i ] ; } } if ( fDist == Integer . MAX_VALUE ) System . out . println ( " Floor ▁ doesn ' t ▁ exist ▁ " ) ; else System . out . println ( " Floor ▁ is ▁ " + arr [ fInd ] ) ; if ( cDist == Integer . MAX_VALUE ) System . out . println ( " Ceil ▁ doesn ' t ▁ exist ▁ " ) ; else System . out . println ( " Ceil ▁ is ▁ ▁ " + arr [ cInd ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 8 , 9 , 6 , 5 , 5 , 6 } ; int x = 7 ; floorAndCeil ( arr , x ) ; } }

import sys NEW_LINE def floorAndCeil ( arr , n , x ) : NEW_LINE INDENT fDist = sys . maxsize NEW_LINE cDist = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= x and cDist > ( arr [ i ] - x ) ) : NEW_LINE INDENT cInd = i NEW_LINE cDist = arr [ i ] - x NEW_LINE DEDENT if ( arr [ i ] <= x and fDist > ( x - arr [ i ] ) ) : NEW_LINE INDENT fInd = i NEW_LINE fDist = x - arr [ i ] NEW_LINE DEDENT DEDENT if ( fDist == sys . maxsize ) : NEW_LINE INDENT print ( " Floor ▁ doesn ' t ▁ exist ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Floor ▁ is ▁ " + str ( arr [ fInd ] ) ) NEW_LINE DEDENT if ( cDist == sys . maxsize ) : NEW_LINE INDENT print ( " Ceil ▁ doesn ' t ▁ exist ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Ceil ▁ is ▁ " + str ( arr [ cInd ] ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 8 , 9 , 6 , 5 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE x = 7 NEW_LINE floorAndCeil ( arr , n , x ) NEW_LINE DEDENT


public class GFG { static boolean isPrime ( int k ) { if ( k <= 1 ) return false ; for ( int i = 2 ; i < k ; i ++ ) if ( k % i == 0 ) return false ; return true ; } static int check ( int num , int k ) { int flag = 1 ; for ( int i = 2 ; i < k ; i ++ ) { if ( num % i == 0 ) flag = 0 ; } if ( flag == 1 ) { if ( num % k == 0 ) return 1 ; else return 0 ; } else return 0 ; } static int findCount ( int a , int b , int k ) { int count = 0 ; if ( ! isPrime ( k ) ) return 0 ; else { int ans ; for ( int i = a ; i <= b ; i ++ ) { ans = check ( i , k ) ; if ( ans == 1 ) count ++ ; else continue ; } } return count ; } public static void main ( String args [ ] ) { int a = 2020 , b = 6300 , k = 29 ; System . out . println ( findCount ( a , b , k ) ) ; } }

def isPrime ( k ) : NEW_LINE INDENT if ( k <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , k ) : NEW_LINE INDENT if ( k % i == 0 ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def check ( num , k ) : NEW_LINE INDENT flag = 1 NEW_LINE for i in range ( 2 , k ) : NEW_LINE INDENT if ( num % i == 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT if ( num % k == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findCount ( a , b , k ) : NEW_LINE INDENT count = 0 NEW_LINE if ( not isPrime ( k ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( a , b + 1 ) : NEW_LINE INDENT ans = check ( i , k ) NEW_LINE if ( ans == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2020 NEW_LINE b = 6300 NEW_LINE k = 29 NEW_LINE print ( findCount ( a , b , k ) ) NEW_LINE DEDENT


class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void countPrimePosition ( int arr [ ] ) { int c0 = 0 , c1 = 0 ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 && isPrime ( i ) ) c0 ++ ; if ( arr [ i ] == 1 && isPrime ( i ) ) c1 ++ ; } System . out . println ( " Number ▁ of ▁ 0s ▁ = ▁ " + c0 ) ; System . out . println ( " Number ▁ of ▁ 1s ▁ = ▁ " + c1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 1 , 0 , 1 } ; countPrimePosition ( arr ) ; } }

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def countPrimePosition ( arr ) : NEW_LINE INDENT c0 = 0 ; c1 = 0 ; NEW_LINE n = len ( arr ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 0 and isPrime ( i ) ) : NEW_LINE INDENT c0 += 1 ; NEW_LINE DEDENT if ( arr [ i ] == 1 and isPrime ( i ) ) : NEW_LINE INDENT c1 += 1 ; NEW_LINE DEDENT DEDENT print ( " Number ▁ of ▁ 0s ▁ = " , c0 ) ; NEW_LINE print ( " Number ▁ of ▁ 1s ▁ = " , c1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 1 ] ; NEW_LINE countPrimePosition ( arr ) ; NEW_LINE DEDENT


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

def avgOfFirstN ( n ) : NEW_LINE INDENT return ( float ) ( 1 + n ) / 2 ; NEW_LINE DEDENT n = 20 NEW_LINE print ( avgOfFirstN ( n ) ) NEW_LINE


class Solution { static long getSum ( int n , int d ) { long sum = 0 ; while ( d <= n ) { sum += d ; d += 10 ; } return sum ; } public static void main ( String args [ ] ) { int n = 30 , d = 3 ; System . out . print ( getSum ( n , d ) ) ; } }

def getSum ( n , d ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( d <= n ) : NEW_LINE INDENT sum += d NEW_LINE d += 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 30 NEW_LINE d = 3 NEW_LINE print ( getSum ( n , d ) ) NEW_LINE


import java . util . * ; class GFG1 { static int KthMinValAfterMconcatenate ( int [ ] A , int N , int M , int K ) { Arrays . sort ( A ) ; return ( A [ ( ( K - 1 ) / M ) ] ) ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 1 , 2 } ; int M = 3 , K = 4 ; int N = A . length ; System . out . println ( KthMinValAfterMconcatenate ( A , N , M , K ) ) ; } }

def KthMinValAfterMconcatenate ( A , N , M , K ) : NEW_LINE INDENT V = [ ] NEW_LINE for i in range ( 0 , M ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT V . append ( A [ j ] ) NEW_LINE DEDENT DEDENT V . sort ( ) NEW_LINE return V [ K - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 1 , 2 ] NEW_LINE M , K = 3 , 4 NEW_LINE N = len ( A ) NEW_LINE print ( KthMinValAfterMconcatenate ( A , N , M , K ) ) NEW_LINE DEDENT


class GFG { static String encryptString ( String str , int n ) { int i = 0 , cnt = 0 ; String encryptedStr = " " ; while ( i < n ) { cnt = i + 1 ; while ( cnt -- > 0 ) encryptedStr += str . charAt ( i ) ; i ++ ; } return encryptedStr ; } public static void main ( String [ ] args ) { String str = " geeks " ; int n = str . length ( ) ; System . out . println ( encryptString ( str , n ) ) ; } }

def encryptString ( string , n ) : NEW_LINE INDENT i , cnt = 0 , 0 NEW_LINE encryptedStr = " " NEW_LINE while i < n : NEW_LINE INDENT cnt = i + 1 NEW_LINE while cnt > 0 : NEW_LINE INDENT encryptedStr += string [ i ] NEW_LINE cnt -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return encryptedStr NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeks " NEW_LINE n = len ( string ) NEW_LINE print ( encryptString ( string , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static int LCMOfNeighbourFact ( int n ) { return factorial ( n + 1 ) ; } public static void main ( String args [ ] ) { int N = 5 ; System . out . println ( LCMOfNeighbourFact ( N ) ) ; } }

def factorial ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return n * factorial ( n - 1 ) NEW_LINE DEDENT def LCMOfNeighbourFact ( n ) : NEW_LINE INDENT return factorial ( n + 1 ) NEW_LINE DEDENT N = 5 NEW_LINE print ( LCMOfNeighbourFact ( N ) ) NEW_LINE


class GFG { static float Area ( float a ) { if ( a < 0 ) return - 1 ; float x = 0.464f * a ; float A = 0.70477f * ( float ) Math . pow ( x , 2 ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( String . format ( " % .5f " , Area ( a ) ) ) ; } }

import math as mt NEW_LINE def Area ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = 0.464 * a NEW_LINE A = 0.70477 * pow ( x , 2 ) NEW_LINE return A NEW_LINE DEDENT a = 5 NEW_LINE print ( Area ( a ) ) NEW_LINE


class GFG { static int sizeofInt = 8 ; static int toggleLastMBits ( int n , int m ) { int num = ( 1 << m ) - 1 ; return ( n ^ num ) ; } static int unsetLastMBits ( int n , int m ) { int num = ( 1 << ( sizeofInt * 8 - 1 ) ) - 1 ; num = toggleLastMBits ( num , m ) ; return ( n & num ) ; } public static void main ( String [ ] args ) { int n = 150 , m = 4 ; System . out . println ( unsetLastMBits ( n , m ) ) ; } }

import sys NEW_LINE def toggleLastMBits ( n , m ) : NEW_LINE INDENT num = ( 1 << m ) - 1 NEW_LINE return ( n ^ num ) NEW_LINE DEDENT def unsetLastMBits ( n , m ) : NEW_LINE INDENT num = ( 1 << ( sys . getsizeof ( int ) * 8 - 1 ) ) - 1 NEW_LINE num = toggleLastMBits ( num , m ) NEW_LINE return ( n & num ) NEW_LINE DEDENT n = 150 NEW_LINE m = 4 NEW_LINE print ( unsetLastMBits ( n , m ) ) NEW_LINE


class GFG { static int cntWays ( int n ) { if ( n % 2 == 1 ) { return 0 ; } else { return ( n - 2 ) / 4 ; } } public static void main ( String [ ] args ) { int n = 18 ; System . out . println ( cntWays ( n ) ) ; } }

def cntWays ( n ) : NEW_LINE INDENT if n % 2 == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return ( n - 2 ) // 4 NEW_LINE DEDENT DEDENT n = 18 NEW_LINE print ( cntWays ( n ) ) NEW_LINE


class GFG { static class Node { int data ; Node next ; } ; static Node addToEmpty ( Node head , int data ) { if ( head != null ) return head ; Node temp = new Node ( ) ; temp . data = data ; head = temp ; head . next = head ; return head ; } static Node addBegin ( Node head , int data ) { if ( head == null ) return addToEmpty ( head , data ) ; Node temp = new Node ( ) ; temp . data = data ; temp . next = head . next ; head . next = temp ; return head ; } static void traverse ( Node head ) { Node p ; if ( head == null ) { System . out . print ( " List ▁ is ▁ empty . " ) ; return ; } p = head ; do { System . out . print ( p . data + " ▁ " ) ; p = p . next ; } while ( p != head ) ; } static Node exchangeNodes ( Node head ) { Node p = head ; while ( p . next . next != head ) p = p . next ; p . next . next = head . next ; head . next = p . next ; p . next = head ; head = head . next ; return head ; } public static void main ( String args [ ] ) { int i ; Node head = null ; head = addToEmpty ( head , 6 ) ; for ( i = 5 ; i > 0 ; i -- ) head = addBegin ( head , i ) ; System . out . print ( " List ▁ Before : ▁ " ) ; traverse ( head ) ; System . out . println ( ) ; System . out . print ( " List ▁ After : ▁ " ) ; head = exchangeNodes ( head ) ; traverse ( head ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def addToEmpty ( head , data ) : NEW_LINE INDENT if ( head != None ) : NEW_LINE INDENT return head NEW_LINE DEDENT temp = Node ( data ) NEW_LINE temp . data = data NEW_LINE head = temp NEW_LINE head . next = head NEW_LINE return head NEW_LINE DEDENT def addBegin ( head , data ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return addToEmpty ( head , data ) NEW_LINE DEDENT temp = Node ( data ) NEW_LINE temp . data = data NEW_LINE temp . next = head . next NEW_LINE head . next = temp NEW_LINE return head NEW_LINE DEDENT def traverse ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT print ( " List ▁ is ▁ empty . " ) NEW_LINE return NEW_LINE DEDENT p = head NEW_LINE print ( p . data , end = " ▁ " ) NEW_LINE p = p . next NEW_LINE while ( p != head ) : NEW_LINE INDENT print ( p . data , end = " ▁ " ) NEW_LINE p = p . next NEW_LINE DEDENT DEDENT def exchangeNodes ( head ) : NEW_LINE INDENT p = head NEW_LINE while ( p . next . next != head ) : NEW_LINE INDENT p = p . next NEW_LINE DEDENT p . next . next = head . next NEW_LINE head . next = p . next NEW_LINE p . next = head NEW_LINE head = head . next NEW_LINE return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = addToEmpty ( head , 6 ) NEW_LINE for x in range ( 5 , 0 , - 1 ) : NEW_LINE INDENT head = addBegin ( head , x ) NEW_LINE DEDENT print ( " List ▁ Before : ▁ " , end = " " ) NEW_LINE traverse ( head ) NEW_LINE print ( ) NEW_LINE print ( " List ▁ After : ▁ " , end = " " ) NEW_LINE head = exchangeNodes ( head ) NEW_LINE traverse ( head ) NEW_LINE DEDENT


class Test { static void printCubes ( int a , int b ) { int acrt = ( int ) Math . cbrt ( a ) ; int bcrt = ( int ) Math . cbrt ( b ) ; for ( int i = acrt ; i <= bcrt ; i ++ ) if ( i * i * i >= a && i * i * i <= b ) System . out . print ( i * i * i + " ▁ " ) ; } public static void main ( String [ ] args ) { int a = 24 , b = 576 ; System . out . println ( " Perfect ▁ cubes ▁ in ▁ given ▁ range : " ) ; printCubes ( a , b ) ; } }

def cbrt ( n ) : NEW_LINE INDENT return ( int ) ( n ** ( 1. / 3 ) ) NEW_LINE DEDENT def printCubes ( a , b ) : NEW_LINE INDENT acrt = cbrt ( a ) NEW_LINE bcrt = cbrt ( b ) NEW_LINE for i in range ( acrt , bcrt + 1 ) : NEW_LINE INDENT if ( i * i * i >= a and i * i * i <= b ) : NEW_LINE INDENT print ( i * i * i , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT a = 24 NEW_LINE b = 576 NEW_LINE print ( " Perfect ▁ cubes ▁ in ▁ given ▁ range : " ) NEW_LINE printCubes ( a , b ) NEW_LINE


import java . io . * ; public class GFG { static int countWays ( int num ) { int [ ] dp = new int [ num + 1 ] ; int MOD = ( int ) 1E9 + 7 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= num ; ++ i ) { dp [ i ] = 0 ; for ( int j = 1 ; j <= 3 ; ++ j ) { if ( i - j == 0 ) dp [ i ] += 1 ; else if ( j == 1 ) dp [ i ] += dp [ i - j ] * 2 ; else if ( i - j > 0 ) dp [ i ] += dp [ i - j ] ; if ( dp [ i ] >= MOD ) dp [ i ] %= MOD ; } } return dp [ num ] ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( countWays ( n ) ) ; } }

def countWays ( num ) : NEW_LINE INDENT dp = [ 0 ] * ( num + 1 ) ; NEW_LINE MOD = 100000000 + 7 ; NEW_LINE dp [ 1 ] = 2 ; NEW_LINE for i in range ( 2 , num + 1 ) : NEW_LINE INDENT dp [ i ] = 0 ; NEW_LINE for j in range ( 1 , 4 ) : NEW_LINE INDENT if ( i - j == 0 ) : NEW_LINE INDENT dp [ i ] += 1 ; NEW_LINE DEDENT elif ( j == 1 ) : NEW_LINE INDENT dp [ i ] += dp [ i - j ] * 2 ; NEW_LINE DEDENT elif ( i - j > 0 ) : NEW_LINE INDENT dp [ i ] += dp [ i - j ] ; NEW_LINE DEDENT DEDENT if ( dp [ i ] >= MOD ) : NEW_LINE INDENT dp [ i ] %= MOD ; NEW_LINE DEDENT DEDENT return dp [ num ] ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( countWays ( n ) ) ; NEW_LINE


import java . util . * ; class GfG { static class Node { int data ; Node left = null ; Node right = null ; } static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = null ; temp . right = null ; return temp ; } static void largestValueInEachLevel ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; int nc , max ; q . add ( root ) ; while ( true ) { nc = q . size ( ) ; if ( nc == 0 ) break ; max = Integer . MIN_VALUE ; while ( nc != 0 ) { Node front = q . peek ( ) ; q . remove ( ) ; if ( max < front . data ) max = front . data ; if ( front . left != null ) q . add ( front . left ) ; if ( front . right != null ) q . add ( front . right ) ; nc -- ; } System . out . println ( max + " ▁ " ) ; } } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 4 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 7 ) ; largestValueInEachLevel ( root ) ; } }

INT_MIN = - 2147483648 NEW_LINE 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 largestValueInEachLevel ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE nc = 10 NEW_LINE max = 0 NEW_LINE q . append ( root ) NEW_LINE while ( 1 ) : NEW_LINE INDENT nc = len ( q ) NEW_LINE if ( nc == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT max = INT_MIN NEW_LINE while ( nc ) : NEW_LINE INDENT front = q [ 0 ] NEW_LINE q = q [ 1 : ] NEW_LINE if ( max < front . data ) : NEW_LINE INDENT max = front . data NEW_LINE DEDENT if ( front . left ) : NEW_LINE INDENT q . append ( front . left ) NEW_LINE DEDENT if ( front . right != None ) : NEW_LINE INDENT q . append ( front . right ) NEW_LINE DEDENT nc -= 1 NEW_LINE DEDENT print ( max , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 4 ) NEW_LINE root . left = newNode ( 9 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE largestValueInEachLevel ( root ) NEW_LINE DEDENT


class GFG { static void sortDiagonal ( int a [ ] [ ] , int M , int N ) { for ( int i = 0 ; i < M ; i ++ ) { int sm = a [ i ] [ i ] ; int pos = i ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( sm > a [ j ] [ j ] ) { sm = a [ j ] [ j ] ; pos = j ; } } swap ( a , i , i , pos , pos ) ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static void swap ( int [ ] [ ] a , int i , int i2 , int pos , int pos2 ) { int temp = a [ i ] [ i2 ] ; a [ i ] [ i2 ] = a [ pos ] [ pos2 ] ; a [ pos ] [ pos2 ] = temp ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 4 , 2 } , { 3 , 1 } } ; sortDiagonal ( a , 2 , 2 ) ; } }

def sortDiagonal ( a , M , N ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT sm = a [ i ] [ i ] NEW_LINE pos = i NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( sm > a [ j ] [ j ] ) : NEW_LINE INDENT sm = a [ j ] [ j ] NEW_LINE pos = j NEW_LINE DEDENT DEDENT a [ i ] [ i ] , a [ pos ] [ pos ] = a [ pos ] [ pos ] , a [ i ] [ i ] NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( a [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT a = [ [ 4 , 2 ] , [ 3 , 1 ] ] NEW_LINE sortDiagonal ( a , 2 , 2 ) NEW_LINE


class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int digitGCD ( int n ) { int gcd = 0 ; while ( n > 0 ) { gcd = __gcd ( n % 10 , gcd ) ; if ( gcd == 1 ) return 1 ; n = n / 10 ; } return gcd ; } public static void main ( String [ ] args ) { int n = 2448 ; System . out . print ( digitGCD ( n ) ) ; } }

def __gcd ( a , b ) : NEW_LINE INDENT return a if ( b == 0 ) else __gcd ( b , a % b ) NEW_LINE DEDENT def digitGCD ( n ) : NEW_LINE INDENT gcd = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT gcd = __gcd ( n % 10 , gcd ) NEW_LINE if ( gcd == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT return gcd NEW_LINE DEDENT n = 2448 NEW_LINE print ( digitGCD ( n ) ) NEW_LINE


class GFG { static void removeChar ( String s , char c ) { int j , count = 0 , n = s . length ( ) ; char [ ] t = s . toCharArray ( ) ; for ( int i = j = 0 ; i < n ; i ++ ) { if ( t [ i ] != c ) t [ j ++ ] = t [ i ] ; else count ++ ; } while ( count > 0 ) { t [ j ++ ] = ' \0' ; count -- ; } System . out . println ( t ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; removeChar ( s , ' g ' ) ; } }

def removeChar ( s , c ) : NEW_LINE INDENT counts = s . count ( c ) NEW_LINE s = list ( s ) NEW_LINE while counts : NEW_LINE INDENT s . remove ( c ) NEW_LINE counts -= 1 NEW_LINE DEDENT s = ' ' . join ( s ) NEW_LINE print ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE removeChar ( s , ' g ' ) NEW_LINE DEDENT


import java . io . * ; import java . util . HashMap ; class OddOccurrence { static int getOddOccurrence ( int arr [ ] , int n ) { HashMap < Integer , Integer > hmap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hmap . containsKey ( arr [ i ] ) ) { int val = hmap . get ( arr [ i ] ) ; hmap . put ( arr [ i ] , val + 1 ) ; } else hmap . put ( arr [ i ] , 1 ) ; } for ( Integer a : hmap . keySet ( ) ) { if ( hmap . get ( a ) % 2 != 0 ) return a ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = arr . length ; System . out . println ( getOddOccurrence ( arr , n ) ) ; } }

def getOddOccurrence ( arr , size ) : NEW_LINE INDENT Hash = dict ( ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT Hash [ arr [ i ] ] = Hash . get ( arr [ i ] , 0 ) + 1 ; NEW_LINE DEDENT for i in Hash : NEW_LINE INDENT if ( Hash [ i ] % 2 != 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getOddOccurrence ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static int xorOdd ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int xorArr = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( ( ( it . getValue ( ) ) % 2 ) == 1 ) { xorArr ^= it . getKey ( ) ; } } return xorArr ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 3 , 3 , 4 , 2 , 3 , 1 } ; int n = arr . length ; System . out . println ( xorOdd ( arr , n ) ) ; } }

def xorOdd ( arr , n ) : NEW_LINE INDENT m = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 ; NEW_LINE DEDENT xorArr = 0 ; NEW_LINE for key , value in m . items ( ) : NEW_LINE INDENT if ( value & 1 ) : NEW_LINE INDENT xorArr ^= key ; NEW_LINE DEDENT DEDENT return xorArr ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 3 , 3 , 4 , 2 , 3 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( xorOdd ( arr , n ) ) ; NEW_LINE DEDENT