import java . util . * ; class GFG { public static int sumOfPairs ( int n ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; int sum = 0 ; while ( n > 0 ) { v . add ( n % 2 ) ; n = n / 2 ; } Collections . reverse ( v ) ; for ( int i = 0 ; i < v . size ( ) - 1 ; i ++ ) { for ( int j = i + 1 ; j < v . size ( ) ; j ++ ) { if ( v . get ( i ) == 0 && v . get ( j ) == 1 ) sum += 1 ; if ( v . get ( i ) == 1 && v . get ( j ) == 1 ) sum += 3 ; if ( v . get ( i ) == 1 && v . get ( j ) == 0 ) sum += 2 ; } } return sum ; } public static void main ( String [ ] args ) { int N = 5 ; System . out . print ( sumOfPairs ( N ) ) ; } }

def sumofPairs ( n ) : NEW_LINE INDENT v = [ ] NEW_LINE sum = 0 NEW_LINE while n > 0 : NEW_LINE INDENT v . append ( n % 2 ) NEW_LINE n = n // 2 NEW_LINE DEDENT v . reverse ( ) NEW_LINE for i in range ( len ( v ) - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , len ( v ) ) : NEW_LINE INDENT if v [ i ] == 0 and v [ j ] == 1 : NEW_LINE INDENT sum += 1 NEW_LINE DEDENT if v [ i ] == 1 and v [ j ] == 1 : NEW_LINE INDENT sum += 3 NEW_LINE DEDENT if v [ i ] == 1 and v [ j ] == 0 : NEW_LINE INDENT sum += 2 NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 NEW_LINE print ( sumofPairs ( N ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class Point { int x , y ; public Point ( int x , int y ) { this . x = x ; this . y = y ; } } ; static int findmin ( Point p [ ] , int n ) { int a = 0 , b = 0 , c = 0 , d = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( p [ i ] . x <= 0 ) a ++ ; else if ( p [ i ] . x >= 0 ) b ++ ; if ( p [ i ] . y >= 0 ) c ++ ; else if ( p [ i ] . y <= 0 ) d ++ ; } return Math . min ( Math . min ( a , b ) , Math . min ( c , d ) ) ; } public static void main ( String [ ] args ) { Point p [ ] = { new Point ( 1 , 1 ) , new Point ( 2 , 2 ) , new Point ( - 1 , - 1 ) , new Point ( - 2 , 2 ) } ; int n = p . length ; System . out . println ( findmin ( p , n ) ) ; } }

def findmin ( p , n ) : NEW_LINE INDENT a , b , c , d = 0 , 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( p [ i ] [ 0 ] <= 0 ) : NEW_LINE INDENT a += 1 NEW_LINE DEDENT elif ( p [ i ] [ 0 ] >= 0 ) : NEW_LINE INDENT b += 1 NEW_LINE DEDENT if ( p [ i ] [ 1 ] >= 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT elif ( p [ i ] [ 1 ] <= 0 ) : NEW_LINE INDENT d += 1 NEW_LINE DEDENT DEDENT return min ( [ a , b , c , d ] ) NEW_LINE DEDENT p = [ [ 1 , 1 ] , [ 2 , 2 ] , [ - 1 , - 1 ] , [ - 2 , 2 ] ] NEW_LINE n = len ( p ) NEW_LINE print ( findmin ( p , n ) ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static void main ( String args [ ] ) { int N = 6 ; int Even = N / 2 ; int Odd = N - Even ; System . out . println ( Even * Odd ) ; } }

N = 6 NEW_LINE Even = N // 2 NEW_LINE Odd = N - Even NEW_LINE print ( Even * Odd ) NEW_LINE


class GfG { static String strBitwiseAND ( String [ ] arr , int n ) { String res = " " ; int smallest_size = Integer . MAX_VALUE ; int largest_size = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { StringBuilder temp = new StringBuilder ( ) ; temp . append ( arr [ i ] ) ; arr [ i ] = temp . reverse ( ) . toString ( ) ; smallest_size = Math . min ( smallest_size , arr [ i ] . length ( ) ) ; largest_size = Math . max ( largest_size , arr [ i ] . length ( ) ) ; } for ( int i = 0 ; i < smallest_size ; i ++ ) { boolean all_ones = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( arr [ j ] . charAt ( i ) == '0' ) { all_ones = false ; break ; } } res += ( all_ones ? '1' : '0' ) ; } for ( int i = 0 ; i < largest_size - smallest_size ; i ++ ) res += '0' ; StringBuilder temp = new StringBuilder ( ) ; temp . append ( res ) ; res = temp . reverse ( ) . toString ( ) ; return res ; } public static void main ( String [ ] args ) { String arr [ ] = { "101" , "110110" , "111" } ; int n = arr . length ; System . out . println ( strBitwiseAND ( arr , n ) ) ; } }

import sys ; NEW_LINE def strBitwiseAND ( arr , n ) : NEW_LINE INDENT res = " " NEW_LINE smallest_size = sys . maxsize ; NEW_LINE largest_size = - ( sys . maxsize - 1 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = arr [ i ] [ : : - 1 ] ; NEW_LINE smallest_size = min ( smallest_size , len ( arr [ i ] ) ) ; NEW_LINE largest_size = max ( largest_size , len ( arr [ i ] ) ) ; NEW_LINE DEDENT for i in range ( smallest_size ) : NEW_LINE INDENT all_ones = True ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr [ j ] [ i ] == '0' ) : NEW_LINE INDENT all_ones = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if all_ones : NEW_LINE INDENT res += '1' ; NEW_LINE DEDENT else : NEW_LINE INDENT res += '0' ; NEW_LINE DEDENT DEDENT for i in range ( largest_size - smallest_size ) : NEW_LINE INDENT res += '0' ; NEW_LINE DEDENT res = res [ : : - 1 ] ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ "101" , "110110" , "111" ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( strBitwiseAND ( arr , n ) ) ; NEW_LINE DEDENT


import java . io . * ; import java . math . * ; public class GFG { static int sumofFactors ( int n ) { int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n > 2 ) res *= ( 1 + n ) ; return res ; } public static void main ( String args [ ] ) { int n = 30 ; System . out . println ( sumofFactors ( n ) ) ; } }

import math as m NEW_LINE def sumofFactors ( n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 2 , int ( m . sqrt ( n ) + 1 ) ) : NEW_LINE INDENT curr_sum = 1 NEW_LINE curr_term = 1 NEW_LINE while n % i == 0 : NEW_LINE INDENT n = n / i ; NEW_LINE curr_term = curr_term * i ; NEW_LINE curr_sum += curr_term ; NEW_LINE DEDENT res = res * curr_sum NEW_LINE DEDENT if n > 2 : NEW_LINE INDENT res = res * ( 1 + n ) NEW_LINE DEDENT return res ; NEW_LINE DEDENT sum = sumofFactors ( 30 ) NEW_LINE print ( " Sum ▁ of ▁ all ▁ divisors ▁ is : ▁ " , sum ) NEW_LINE


class GFG { static int countDecoding ( char [ ] digits , int n ) { if ( n == 0 || n == 1 ) return 1 ; if ( digits [ 0 ] == '0' ) return 0 ; int count = 0 ; if ( digits [ n - 1 ] > '0' ) count = countDecoding ( digits , n - 1 ) ; if ( digits [ n - 2 ] == '1' || ( digits [ n - 2 ] == '2' && digits [ n - 1 ] < '7' ) ) count += countDecoding ( digits , n - 2 ) ; return count ; } public static void main ( String [ ] args ) { char digits [ ] = { '1' , '2' , '3' , '4' } ; int n = digits . length ; System . out . printf ( " Count ▁ is ▁ % d " , countDecoding ( digits , n ) ) ; } }

def numDecodings ( s : str ) -> int : NEW_LINE INDENT return numDecodingsHelper ( s , len ( s ) ) ; NEW_LINE DEDENT def numDecodingsHelper ( s : str , n : int ) -> int : NEW_LINE INDENT if n == 0 or n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT count = 0 NEW_LINE if s [ n - 1 ] > "0" : NEW_LINE INDENT count = numDecodingsHelper ( s , n - 1 ) NEW_LINE DEDENT if ( s [ n - 2 ] == '1' or ( s [ n - 2 ] == '2' and s [ n - 1 ] < '7' ) ) : NEW_LINE INDENT count += numDecodingsHelper ( s , n - 2 ) NEW_LINE DEDENT return count NEW_LINE DEDENT digits = "1234" NEW_LINE print ( " Count ▁ is ▁ " , numDecodings ( digits ) ) NEW_LINE


class GFG { static int countDistinct ( int arr [ ] , int n ) { int res = 1 ; for ( int i = 1 ; i < n ; i ++ ) { int j = 0 ; for ( j = 0 ; j < i ; j ++ ) if ( arr [ i ] == arr [ j ] ) break ; if ( i == j ) res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 } ; int n = arr . length ; System . out . println ( countDistinct ( arr , n ) ) ; } }

def countDistinct ( arr , n ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT j = 0 NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if ( i == j + 1 ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 12 , 10 , 9 , 45 , 2 , 10 , 10 , 45 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countDistinct ( arr , n ) ) NEW_LINE


class GFG { static String get_maximum ( char [ ] s , int a [ ] ) { int n = s . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( s [ i ] - '0' < a [ s [ i ] - '0' ] ) { int j = i ; while ( j < n && ( s [ j ] - '0' <= a [ s [ j ] - '0' ] ) ) { s [ j ] = ( char ) ( '0' + a [ s [ j ] - '0' ] ) ; j ++ ; } return String . valueOf ( s ) ; } } return String . valueOf ( s ) ; } public static void main ( String [ ] args ) { String s = "1337" ; int a [ ] = { 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 } ; System . out . println ( get_maximum ( s . toCharArray ( ) , a ) ) ; } }

def get_maximum ( s , a ) : NEW_LINE INDENT s = list ( s ) NEW_LINE n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ord ( s [ i ] ) - ord ( '0' ) < a [ ord ( s [ i ] ) - ord ( '0' ) ] ) : NEW_LINE INDENT j = i NEW_LINE while ( j < n and ( ord ( s [ j ] ) - ord ( '0' ) <= a [ ord ( s [ j ] ) - ord ( '0' ) ] ) ) : NEW_LINE INDENT s [ j ] = chr ( ord ( '0' ) + a [ ord ( s [ j ] ) - ord ( '0' ) ] ) NEW_LINE j += 1 NEW_LINE DEDENT return " " . join ( s ) ; NEW_LINE DEDENT DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "1337" NEW_LINE a = [ 0 , 1 , 2 , 5 , 4 , 6 , 6 , 3 , 1 , 9 ] NEW_LINE print ( get_maximum ( s , a ) ) NEW_LINE DEDENT


import java . io . * ; public class GFG { static int maxDiff ( int [ ] arr , int n ) { int SubsetSum_1 = 0 , SubsetSum_2 = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { boolean isSingleOccurance = true ; for ( int j = i + 1 ; j <= n - 1 ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { isSingleOccurance = false ; arr [ i ] = arr [ j ] = 0 ; break ; } } if ( isSingleOccurance ) { if ( arr [ i ] > 0 ) SubsetSum_1 += arr [ i ] ; else SubsetSum_2 += arr [ i ] ; } } return Math . abs ( SubsetSum_1 - SubsetSum_2 ) ; } static public void main ( String [ ] args ) { int [ ] arr = { 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 } ; int n = arr . length ; System . out . println ( " Maximum ▁ Difference ▁ = ▁ " + maxDiff ( arr , n ) ) ; } }

import math NEW_LINE def maxDiff ( arr , n ) : NEW_LINE INDENT SubsetSum_1 = 0 NEW_LINE SubsetSum_2 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT isSingleOccurance = True NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT isSingleOccurance = False NEW_LINE arr [ i ] = arr [ j ] = 0 NEW_LINE break NEW_LINE DEDENT DEDENT if ( isSingleOccurance == True ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT SubsetSum_1 += arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT SubsetSum_2 += arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return abs ( SubsetSum_1 - SubsetSum_2 ) NEW_LINE DEDENT arr = [ 4 , 2 , - 3 , 3 , - 2 , - 2 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ Difference ▁ = ▁ { } " . format ( maxDiff ( arr , n ) ) ) NEW_LINE


class GFG { static long binomialCoeff ( int n , int k ) { long res = 1 ; if ( k > n - k ) k = n - k ; for ( int i = 0 ; i < k ; ++ i ) { res *= ( n - i ) ; res /= ( i + 1 ) ; } return res ; } static long catalan ( int n ) { long c = binomialCoeff ( 2 * n , n ) ; return c / ( n + 1 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( catalan ( n ) ) ; } }

def binomialCoeff ( n , k ) : NEW_LINE INDENT res = 1 ; NEW_LINE if ( k > n - k ) : NEW_LINE INDENT k = n - k ; NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT res *= ( n - i ) ; NEW_LINE res /= ( i + 1 ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def catalan ( n ) : NEW_LINE INDENT c = binomialCoeff ( 2 * n , n ) ; NEW_LINE return int ( c / ( n + 1 ) ) ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( catalan ( n ) ) ; NEW_LINE


class GFG { static void makePolygon ( double a ) { double n = 360 / ( 180 - a ) ; if ( n == ( int ) n ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { double a = 90 ; makePolygon ( a ) ; } }

def makePolygon ( a ) : NEW_LINE INDENT n = 360 / ( 180 - a ) NEW_LINE if n == int ( n ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 90 NEW_LINE makePolygon ( a ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean isPrime ( int x ) { for ( int i = 2 ; i * i <= x ; i ++ ) { if ( x % i == 0 ) return false ; } return true ; } static int minimumCost ( int n ) { if ( isPrime ( n ) ) return 1 ; if ( n % 2 == 1 && isPrime ( n - 2 ) ) return 2 ; if ( n % 2 == 0 ) return 2 ; return 3 ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( minimumCost ( n ) ) ; } }

from math import sqrt NEW_LINE def isPrime ( x ) : NEW_LINE INDENT for i in range ( 2 , int ( sqrt ( x ) ) + 1 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT return 1 ; NEW_LINE DEDENT def minimumCost ( n ) : NEW_LINE INDENT if ( isPrime ( n ) ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( n % 2 == 1 and isPrime ( n - 2 ) ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT return 3 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 6 ; NEW_LINE print ( minimumCost ( n ) ) ; NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static int [ ] pref = new int [ 100010 ] ; static int isPerfectCube ( int x ) { double cr = Math . round ( Math . cbrt ( x ) ) ; if ( cr * cr * cr == ( double ) x ) return x ; return 0 ; } static void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isPerfectCube ( i ) ; } } static void printSum ( int L , int R ) { long sum = pref [ R ] - pref [ L - 1 ] ; System . out . print ( sum + " ▁ " ) ; } public static void main ( String [ ] args ) { compute ( ) ; int Q = 4 ; int [ ] [ ] arr = { { 1 , 10 } , { 1 , 100 } , { 2 , 25 } , { 4 , 50 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } }

pref = [ 0 ] * 100010 ; NEW_LINE def isPerfectCube ( x ) : NEW_LINE INDENT cr = round ( x ** ( 1 / 3 ) ) ; NEW_LINE rslt = x if ( cr * cr * cr == x ) else 0 ; NEW_LINE return rslt ; NEW_LINE DEDENT def compute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + isPerfectCube ( i ) ; NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT sum = pref [ R ] - pref [ L - 1 ] ; NEW_LINE print ( sum , end = " ▁ " ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT compute ( ) ; NEW_LINE Q = 4 ; NEW_LINE arr = [ [ 1 , 10 ] , [ 1 , 100 ] , [ 2 , 25 ] , [ 4 , 50 ] ] ; NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; NEW_LINE DEDENT DEDENT


class GFG { static void print_asterisk ( int asterisk ) { if ( asterisk == 0 ) return ; System . out . print ( " * ▁ " ) ; print_asterisk ( asterisk - 1 ) ; } static void print_space ( int space ) { if ( space == 0 ) return ; System . out . print ( " ▁ " ) ; System . out . print ( " ▁ " ) ; print_space ( space - 1 ) ; } static void pattern_upper ( int n , int num ) { if ( n == 0 ) return ; print_asterisk ( num - n + 1 ) ; print_space ( 2 * n - 1 ) ; print_asterisk ( num - n + 1 ) ; System . out . println ( ) ; pattern_upper ( n - 1 , num ) ; } static void pattern_lower ( int n , int num ) { if ( n == 0 ) return ; print_asterisk ( n ) ; print_space ( 2 * ( num - n ) + 1 ) ; print_asterisk ( n ) ; System . out . println ( ) ; pattern_lower ( n - 1 , num ) ; } static void pattern ( int n , int num ) { pattern_upper ( n , num ) ; pattern_lower ( n - 1 , num ) ; } public static void main ( String [ ] args ) { int n = 5 ; pattern ( n , n ) ; } }

def print_asterisk ( asterisk ) : NEW_LINE INDENT if ( asterisk == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT print ( " * ▁ " , end = " " ) ; NEW_LINE print_asterisk ( asterisk - 1 ) ; NEW_LINE DEDENT def print_space ( space ) : NEW_LINE INDENT if ( space == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT print ( " ▁ " , end = " " ) ; NEW_LINE print ( " ▁ " , end = " " ) ; NEW_LINE print_space ( space - 1 ) ; NEW_LINE DEDENT def pattern_upper ( n , num ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT print_asterisk ( num - n + 1 ) ; NEW_LINE print_space ( 2 * n - 1 ) ; NEW_LINE print_asterisk ( num - n + 1 ) ; NEW_LINE print ( ) ; NEW_LINE pattern_upper ( n - 1 , num ) ; NEW_LINE DEDENT def pattern_lower ( n , num ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT print_asterisk ( n ) ; NEW_LINE print_space ( 2 * ( num - n ) + 1 ) ; NEW_LINE print_asterisk ( n ) ; NEW_LINE print ( ) ; NEW_LINE pattern_lower ( n - 1 , num ) ; NEW_LINE DEDENT def pattern ( n , num ) : NEW_LINE INDENT pattern_upper ( n , num ) ; NEW_LINE pattern_lower ( n - 1 , num ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE pattern ( n , n ) ; NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static void printDistinct ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) i ++ ; System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; int n = arr . length ; printDistinct ( arr , n ) ; } }

def printDistinct ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i < n - 1 and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT while ( i < n - 1 and ( arr [ i ] == arr [ i + 1 ] ) ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE printDistinct ( arr , n ) ; NEW_LINE


class Main { static int kth ( int arr1 [ ] , int arr2 [ ] , int m , int n , int k ) { int [ ] sorted1 = new int [ m + n ] ; int i = 0 , j = 0 , d = 0 ; while ( i < m && j < n ) { if ( arr1 [ i ] < arr2 [ j ] ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; else sorted1 [ d ++ ] = arr2 [ j ++ ] ; } while ( i < m ) sorted1 [ d ++ ] = arr1 [ i ++ ] ; while ( j < n ) sorted1 [ d ++ ] = arr2 [ j ++ ] ; return sorted1 [ k - 1 ] ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 3 , 6 , 7 , 9 } ; int arr2 [ ] = { 1 , 4 , 8 , 10 } ; int k = 5 ; System . out . print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) ; } }

def kth ( arr1 , arr2 , m , n , k ) : NEW_LINE INDENT sorted1 = [ 0 ] * ( m + n ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE d = 0 NEW_LINE while ( i < m and j < n ) : NEW_LINE INDENT if ( arr1 [ i ] < arr2 [ j ] ) : NEW_LINE INDENT sorted1 [ d ] = arr1 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT sorted1 [ d ] = arr2 [ j ] NEW_LINE j += 1 NEW_LINE DEDENT d += 1 NEW_LINE DEDENT while ( i < m ) : NEW_LINE INDENT sorted1 [ d ] = arr1 [ i ] NEW_LINE d += 1 NEW_LINE i += 1 NEW_LINE DEDENT while ( j < n ) : NEW_LINE INDENT sorted1 [ d ] = arr2 [ j ] NEW_LINE d += 1 NEW_LINE j += 1 NEW_LINE DEDENT return sorted1 [ k - 1 ] NEW_LINE DEDENT arr1 = [ 2 , 3 , 6 , 7 , 9 ] NEW_LINE arr2 = [ 1 , 4 , 8 , 10 ] NEW_LINE k = 5 ; NEW_LINE print ( kth ( arr1 , arr2 , 5 , 4 , k ) ) NEW_LINE


import java . io . * ; class GFG { static int findLastIndex ( String str , Character x ) { for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) if ( str . charAt ( i ) == x ) return i ; return - 1 ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; Character x = ' e ' ; int index = findLastIndex ( str , x ) ; if ( index == - 1 ) System . out . println ( " Character ▁ not ▁ found " ) ; else System . out . println ( " Last ▁ index ▁ is ▁ " + index ) ; } }

def findLastIndex ( str , x ) : NEW_LINE INDENT for i in range ( len ( str ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] == x ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT str = " geeksforgeeks " NEW_LINE x = ' e ' NEW_LINE index = findLastIndex ( str , x ) NEW_LINE if ( index == - 1 ) : NEW_LINE INDENT print ( " Character ▁ not ▁ found " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Last ▁ index ▁ is ▁ " , index ) NEW_LINE DEDENT


class GFG { static int MAX = 10001 ; static void sieveOfEratosthenes ( int [ ] s ) { boolean [ ] prime = new boolean [ MAX + 1 ] ; for ( int i = 2 ; i <= MAX ; i += 2 ) s [ i ] = 2 ; for ( int i = 3 ; i <= MAX ; i += 2 ) { if ( prime [ i ] == false ) { s [ i ] = i ; for ( int j = i ; j * i <= MAX ; j += 2 ) { if ( prime [ i * j ] == false ) { prime [ i * j ] = true ; s [ i * j ] = i ; } } } } } static int kPrimeFactor ( int n , int k , int [ ] s ) { while ( n > 1 ) { if ( k == 1 ) return s [ n ] ; k -- ; n /= s [ n ] ; } return - 1 ; } public static void main ( String [ ] args ) { int [ ] s = new int [ MAX + 1 ] ; sieveOfEratosthenes ( s ) ; int n = 12 , k = 3 ; System . out . println ( kPrimeFactor ( n , k , s ) ) ; n = 14 ; k = 3 ; System . out . println ( kPrimeFactor ( n , k , s ) ) ; } }

MAX = 10001 NEW_LINE def sieveOfEratosthenes ( s ) : NEW_LINE INDENT prime = [ False for i in range ( MAX + 1 ) ] NEW_LINE for i in range ( 2 , MAX + 1 , 2 ) : NEW_LINE INDENT s [ i ] = 2 ; NEW_LINE DEDENT for i in range ( 3 , MAX , 2 ) : NEW_LINE INDENT if ( prime [ i ] == False ) : NEW_LINE INDENT s [ i ] = i NEW_LINE for j in range ( i , MAX + 1 , 2 ) : NEW_LINE INDENT if j * j > MAX : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ i * j ] == False ) : NEW_LINE INDENT prime [ i * j ] = True NEW_LINE s [ i * j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def kPrimeFactor ( n , k , s ) : NEW_LINE INDENT while ( n > 1 ) : NEW_LINE INDENT if ( k == 1 ) : NEW_LINE INDENT return s [ n ] NEW_LINE DEDENT k -= 1 NEW_LINE n //= s [ n ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT s = [ - 1 for i in range ( MAX + 1 ) ] NEW_LINE sieveOfEratosthenes ( s ) NEW_LINE n = 12 NEW_LINE k = 3 NEW_LINE print ( kPrimeFactor ( n , k , s ) ) NEW_LINE n = 14 NEW_LINE k = 3 NEW_LINE print ( kPrimeFactor ( n , k , s ) ) NEW_LINE


import java . io . * ; import java . util . * ; public class GFG { static List < Integer > power = new ArrayList < Integer > ( ) ; static void nextPower ( Integer N ) { Integer carry = 0 ; for ( int i = 0 ; i < power . size ( ) ; i ++ ) { Integer prod = ( power . get ( i ) * N ) + carry ; power . set ( i , prod % 10 ) ; carry = prod / 10 ; } while ( carry >= 1 ) { power . add ( carry % 10 ) ; carry = carry / 10 ; } } static void printPowerNumber ( int X , int N ) { power . add ( 1 ) ; List < Integer > res = new ArrayList < Integer > ( ) ; for ( int i = 1 ; i <= X ; i ++ ) { nextPower ( N ) ; res . add ( power . get ( power . size ( ) - 1 ) ) ; res . add ( power . get ( 0 ) ) ; } for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . print ( res . get ( i ) ) ; } public static void main ( String args [ ] ) { Integer N = 19 , X = 4 ; printPowerNumber ( X , N ) ; } }

power = [ ] NEW_LINE def nextPower ( N ) : NEW_LINE INDENT global power NEW_LINE carry = 0 NEW_LINE for i in range ( 0 , len ( power ) ) : NEW_LINE INDENT prod = ( power [ i ] * N ) + carry NEW_LINE power [ i ] = prod % 10 NEW_LINE carry = ( int ) ( prod / 10 ) NEW_LINE DEDENT while ( carry ) : NEW_LINE INDENT power . append ( carry % 10 ) NEW_LINE carry = ( int ) ( carry / 10 ) NEW_LINE DEDENT DEDENT def printPowerNumber ( X , N ) : NEW_LINE INDENT global power NEW_LINE power . append ( 1 ) NEW_LINE res = [ ] NEW_LINE for i in range ( 1 , X + 1 ) : NEW_LINE INDENT nextPower ( N ) NEW_LINE res . append ( power [ - 1 ] ) NEW_LINE res . append ( power [ 0 ] ) NEW_LINE DEDENT for i in range ( 0 , len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " " ) NEW_LINE DEDENT DEDENT N = 19 NEW_LINE X = 4 NEW_LINE printPowerNumber ( X , N ) NEW_LINE


import java . util . * ; class GFG { static void findMostOccurringChar ( Vector < String > str ) { int [ ] hash = new int [ 26 ] ; for ( int i = 0 ; i < str . size ( ) ; i ++ ) { for ( int j = 0 ; j < str . get ( i ) . length ( ) ; j ++ ) { hash [ str . get ( i ) . charAt ( j ) - 97 ] ++ ; } } int max = 0 ; for ( int i = 0 ; i < 26 ; i ++ ) { max = hash [ i ] > hash [ max ] ? i : max ; } System . out . print ( ( char ) ( max + 97 ) + " \n " ) ; } public static void main ( String [ ] args ) { Vector < String > str = new Vector < String > ( ) ; str . add ( " animal " ) ; str . add ( " zebra " ) ; str . add ( " lion " ) ; str . add ( " giraffe " ) ; findMostOccurringChar ( str ) ; } }

def findMostOccurringChar ( string ) : NEW_LINE INDENT hash = [ 0 ] * 26 ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT for j in range ( len ( string [ i ] ) ) : NEW_LINE INDENT hash [ ord ( string [ i ] [ j ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT DEDENT max = 0 ; NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT max = i if hash [ i ] > hash [ max ] else max ; NEW_LINE DEDENT print ( ( chr ) ( max + 97 ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = [ ] ; NEW_LINE string . append ( " animal " ) ; NEW_LINE string . append ( " zebra " ) ; NEW_LINE string . append ( " lion " ) ; NEW_LINE string . append ( " giraffe " ) ; NEW_LINE findMostOccurringChar ( string ) ; NEW_LINE DEDENT


import java . util . * ; import java . io . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { int res = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { int curr_sum = 0 ; for ( int j = 0 ; j < n ; j ++ ) { int index = ( i + j ) % n ; curr_sum += j * arr [ index ] ; } res = Math . max ( res , curr_sum ) ; } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 3 , 1 , 2 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }

import sys NEW_LINE def maxSum ( arr , n ) : NEW_LINE INDENT res = - sys . maxsize NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT curr_sum = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT index = int ( ( i + j ) % n ) NEW_LINE curr_sum += j * arr [ index ] NEW_LINE DEDENT res = max ( res , curr_sum ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 8 , 3 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSum ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; static void SieveofEratosthenes ( int n ) { boolean [ ] visited = new boolean [ n * n + 2 ] ; for ( int i = 2 ; i <= n + 1 ; i ++ ) if ( ! visited [ i ] ) { for ( int j = i * i ; j <= n + 1 ; j += i ) visited [ j ] = true ; primes . add ( i ) ; } } static boolean specialPrimeNumbers ( int n , int k ) { SieveofEratosthenes ( n ) ; int count = 0 ; for ( int i = 0 ; i < primes . size ( ) ; i ++ ) { for ( int j = 0 ; j < i - 1 ; j ++ ) { if ( primes . get ( j ) + primes . get ( j + 1 ) + 1 == primes . get ( i ) ) { count ++ ; break ; } } if ( count == k ) return true ; } return false ; } public static void main ( String [ ] args ) { int n = 27 , k = 2 ; if ( specialPrimeNumbers ( n , k ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

primes = [ ] ; NEW_LINE def SieveofEratosthenes ( n ) : NEW_LINE INDENT visited = [ False ] * ( n + 2 ) ; NEW_LINE for i in range ( 2 , n + 2 ) : NEW_LINE INDENT if ( visited [ i ] == False ) : NEW_LINE INDENT for j in range ( i * i , n + 2 , i ) : NEW_LINE INDENT visited [ j ] = True ; NEW_LINE DEDENT primes . append ( i ) ; NEW_LINE DEDENT DEDENT DEDENT def specialPrimeNumbers ( n , k ) : NEW_LINE INDENT SieveofEratosthenes ( n ) ; NEW_LINE count = 0 ; NEW_LINE for i in range ( len ( primes ) ) : NEW_LINE INDENT for j in range ( i - 1 ) : NEW_LINE INDENT if ( primes [ j ] + primes [ j + 1 ] + 1 == primes [ i ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( count == k ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT n = 27 ; NEW_LINE k = 2 ; NEW_LINE if ( specialPrimeNumbers ( n , k ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int setBits ( int n ) { int count = 0 ; while ( n > 0 ) { n = n & ( n - 1 ) ; count ++ ; } return count ; } static int countPairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int setbits_x = setBits ( a [ i ] ) ; for ( int j = i + 1 ; j < n ; j ++ ) { int setbits_y = setBits ( a [ j ] ) ; int setbits_xor_xy = setBits ( a [ i ] ^ a [ j ] ) ; if ( setbits_x + setbits_y == setbits_xor_xy ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 3 , 4 , 5 , 6 } ; int n = a . length ; System . out . println ( countPairs ( a , n ) ) ; } }

def setBits ( n ) : NEW_LINE INDENT count = 0 NEW_LINE while ( n ) : NEW_LINE INDENT n = n & ( n - 1 ) NEW_LINE count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT def countPairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n - 1 , 1 ) : NEW_LINE INDENT setbits_x = setBits ( a [ i ] ) NEW_LINE for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT setbits_y = setBits ( a [ j ] ) NEW_LINE setbits_xor_xy = setBits ( a [ i ] ^ a [ j ] ) ; NEW_LINE if ( setbits_x + setbits_y == setbits_xor_xy ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( a ) NEW_LINE print ( countPairs ( a , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int distinctCount ( int arr [ ] , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) s . add ( Math . abs ( arr [ i ] ) ) ; return s . size ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }

def distinctCount ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT s . add ( abs ( arr [ i ] ) ) NEW_LINE DEDENT return len ( s ) NEW_LINE DEDENT arr = [ - 2 , - 1 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Count ▁ of ▁ absolute ▁ distinct ▁ values : " , distinctCount ( arr , n ) ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static int countSubseq ( int arr [ ] , int n ) { int count = 0 ; 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 ) ; for ( Map . Entry < Integer , Integer > entry : mp . entrySet ( ) ) { if ( entry . getKey ( ) == 0 ) count += Math . pow ( 2 , entry . getValue ( ) ) - 1 ; else count += Math . pow ( 2 , entry . getValue ( ) - 1 ) ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 , 5 , 6 } ; int n = arr . length ; System . out . println ( countSubseq ( arr , n ) ) ; } }

def countSubseq ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE mp = { } NEW_LINE for x in arr : NEW_LINE INDENT if x in mp . keys ( ) : NEW_LINE INDENT mp [ x ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ x ] = 1 NEW_LINE DEDENT DEDENT for i in mp . keys ( ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT count += pow ( 2 , mp [ i ] ) - 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += pow ( 2 , mp [ i ] - 1 ) NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 2 , 2 , 2 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countSubseq ( arr , n ) ) NEW_LINE


class GFG { static void bin ( int n ) { if ( n > 1 ) bin ( n / 2 ) ; System . out . print ( n % 2 ) ; } public static void main ( String [ ] args ) { bin ( 7 ) ; System . out . println ( ) ; bin ( 4 ) ; } }

def bin ( n ) : NEW_LINE INDENT if n > 1 : NEW_LINE INDENT bin ( n // 2 ) NEW_LINE DEDENT print ( n % 2 , end = " " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT bin ( 7 ) NEW_LINE print ( ) NEW_LINE bin ( 4 ) NEW_LINE DEDENT


class GFG { public static void main ( String [ ] args ) { char i ; System . out . printf ( " The ▁ Alphabets ▁ from ▁ A ▁ to ▁ Z ▁ are : ▁ \n " ) ; for ( i = ' A ' ; i <= ' Z ' ; i ++ ) { System . out . printf ( " % c ▁ " , i ) ; } } }

if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT i = chr ; NEW_LINE print ( " The ▁ Alphabets ▁ from ▁ A ▁ to ▁ Z ▁ are : ▁ " ) ; NEW_LINE for i in range ( ord ( ' A ' ) , ord ( ' Z ' ) + 1 ) : NEW_LINE INDENT print ( chr ( i ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT


class GFG { static void findSubsequence ( int arr [ ] , int n ) { int sign [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) sign [ i ] = 1 ; else sign [ i ] = - 1 ; } int k = 0 ; int result [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { int cur = arr [ i ] ; int j = i ; while ( j < n && sign [ i ] == sign [ j ] ) { cur = Math . max ( cur , arr [ j ] ) ; ++ j ; } result [ k ++ ] = cur ; i = j - 1 ; } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; System . out . print ( " \n " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 } ; int n = arr . length ; findSubsequence ( arr , n ) ; } }

def findSubsequence ( arr , n ) : NEW_LINE INDENT sign = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT sign [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT sign [ i ] = - 1 NEW_LINE DEDENT DEDENT k = 0 NEW_LINE result = [ 0 ] * n NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT cur = arr [ i ] NEW_LINE j = i NEW_LINE while ( j < n and sign [ i ] == sign [ j ] ) : NEW_LINE INDENT cur = max ( cur , arr [ j ] ) NEW_LINE j += 1 NEW_LINE DEDENT result [ k ] = cur NEW_LINE k += 1 NEW_LINE i = j - 1 NEW_LINE i += 1 NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 4 , 9 , 4 , 11 , - 5 , - 17 , 9 , - 3 , - 5 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE findSubsequence ( arr , n ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class GFG { static void nearestGcd ( int [ ] arr , int n ) { for ( int i = 0 ; i < n ; ++ i ) { int closest = - 1 ; for ( int j = i - 1 , k = i + 1 ; j > 0 || k <= n ; -- j , ++ k ) { if ( j >= 0 && __gcd ( arr [ i ] , arr [ j ] ) > 1 ) { closest = j + 1 ; break ; } if ( k < n && __gcd ( arr [ i ] , arr [ k ] ) > 1 ) { closest = k + 1 ; break ; } } System . out . print ( closest + " ▁ " ) ; } } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 2 , 9 , 4 , 3 , 13 } ; int n = arr . length ; nearestGcd ( arr , n ) ; } }

import math NEW_LINE def nearestGcd ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT closest = - 1 NEW_LINE j = i - 1 NEW_LINE k = i + 1 NEW_LINE while j > 0 or k <= n : NEW_LINE INDENT if ( j >= 0 and math . gcd ( arr [ i ] , arr [ j ] ) > 1 ) : NEW_LINE INDENT closest = j + 1 NEW_LINE break NEW_LINE DEDENT if ( k < n and math . gcd ( arr [ i ] , arr [ k ] ) > 1 ) : NEW_LINE INDENT closest = k + 1 NEW_LINE break NEW_LINE DEDENT k += 1 NEW_LINE j -= 1 NEW_LINE DEDENT print ( closest , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 9 , 4 , 3 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE nearestGcd ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void printDiamond ( int n ) { int space = n - 1 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < space ; j ++ ) System . out . print ( " ▁ " ) ; for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( " * ▁ " ) ; System . out . print ( " \n " ) ; space -- ; } space = 0 ; for ( int i = n ; i > 0 ; i -- ) { for ( int j = 0 ; j < space ; j ++ ) System . out . print ( " ▁ " ) ; for ( int j = 0 ; j < i ; j ++ ) System . out . print ( " * ▁ " ) ; System . out . print ( " \n " ) ; space ++ ; } } public static void main ( String [ ] args ) { printDiamond ( 5 ) ; } }

def Diamond ( rows ) : NEW_LINE INDENT n = 0 NEW_LINE for i in range ( 1 , rows + 1 ) : NEW_LINE INDENT for j in range ( 1 , ( rows - i ) + 1 ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT while n != ( 2 * i - 1 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE n = n + 1 NEW_LINE DEDENT n = 0 NEW_LINE print ( ) NEW_LINE DEDENT k = 1 NEW_LINE n = 1 NEW_LINE for i in range ( 1 , rows ) : NEW_LINE INDENT for j in range ( 1 , k + 1 ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT k = k + 1 NEW_LINE while n <= ( 2 * ( rows - i ) - 1 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE n = n + 1 NEW_LINE DEDENT n = 1 NEW_LINE print ( ) NEW_LINE DEDENT DEDENT rows = 5 NEW_LINE Diamond ( rows ) NEW_LINE


import java . util . * ; class GFG { static void SieveOfEratosthenes ( boolean prime [ ] , int p_size ) { prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= p_size ; p ++ ) { if ( prime [ p ] ) { for ( int i = p * 2 ; i <= p_size ; i += p ) prime [ i ] = false ; } } } static int gcdPrimeFreq ( int arr [ ] , int n ) { boolean [ ] prime = new boolean [ n + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) prime [ i ] = true ; SieveOfEratosthenes ( prime , n + 1 ) ; int i , j ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( 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 gcd = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( prime [ it . getValue ( ) ] ) { gcd = __gcd ( gcd , it . getKey ( ) ) ; } } return gcd ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static public void main ( String [ ] arg ) { int arr [ ] = { 5 , 4 , 6 , 5 , 4 , 6 } ; int n = arr . length ; System . out . println ( gcdPrimeFreq ( arr , n ) ) ; } }

from math import sqrt , gcd NEW_LINE def SieveOfEratosthenes ( prime , p_size ) : NEW_LINE INDENT prime [ 0 ] = False ; NEW_LINE prime [ 1 ] = False ; NEW_LINE for p in range ( 2 , int ( sqrt ( p_size ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( 2 * p , p_size , p ) : NEW_LINE INDENT prime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def gcdPrimeFreq ( arr , n ) : NEW_LINE INDENT prime = [ True ] * ( n + 1 ) ; NEW_LINE SieveOfEratosthenes ( prime , n + 1 ) ; NEW_LINE m = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 ; NEW_LINE DEDENT __gcd = 0 ; NEW_LINE for key , value in m . items ( ) : NEW_LINE INDENT if ( prime [ value ] ) : NEW_LINE INDENT __gcd = gcd ( __gcd , key ) ; NEW_LINE DEDENT DEDENT return __gcd ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 4 , 6 , 5 , 4 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( gcdPrimeFreq ( arr , n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static final int N = 10005 ; static void SieveOfEratosthenes ( Vector < Boolean > composite ) { for ( int i = 0 ; i < N ; i ++ ) { composite . add ( i , false ) ; } for ( int p = 2 ; p * p < N ; p ++ ) { if ( ! composite . get ( p ) ) { for ( int i = p * 2 ; i < N ; i += p ) { composite . add ( i , true ) ; } } } } static int sumOfElements ( int arr [ ] , int n ) { Vector < Boolean > composite = new Vector < Boolean > ( ) ; for ( int i = 0 ; i < N ; i ++ ) composite . add ( false ) ; SieveOfEratosthenes ( composite ) ; 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 sum = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( composite . get ( it . getValue ( ) ) ) { sum += ( it . getKey ( ) ) ; } } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 1 , 1 , 3 , 3 , 2 , 4 } ; int n = arr . length ; System . out . print ( sumOfElements ( arr , n ) ) ; } }

N = 100005 NEW_LINE def SieveOfEratosthenes ( composite ) : NEW_LINE INDENT for p in range ( 2 , N ) : NEW_LINE INDENT if p * p > N : NEW_LINE INDENT break NEW_LINE DEDENT if ( composite [ p ] == False ) : NEW_LINE INDENT for i in range ( 2 * p , N , p ) : NEW_LINE INDENT composite [ i ] = True NEW_LINE DEDENT DEDENT DEDENT DEDENT def sumOfElements ( arr , n ) : NEW_LINE INDENT composite = [ False ] * N NEW_LINE SieveOfEratosthenes ( composite ) NEW_LINE m = dict ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] = m . get ( arr [ i ] , 0 ) + 1 NEW_LINE DEDENT sum = 0 NEW_LINE for it in m : NEW_LINE INDENT if ( composite [ m [ it ] ] ) : NEW_LINE INDENT sum += ( it ) NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 1 , 1 , 3 , 3 , 2 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumOfElements ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; import java . lang . Math ; class LIS { static int CeilIndex ( int A [ ] , int l , int r , int key ) { while ( r - l > 1 ) { int m = l + ( r - l ) / 2 ; if ( A [ m ] >= key ) r = m ; else l = m ; } return r ; } static int LongestIncreasingSubsequenceLength ( int A [ ] , int size ) { int [ ] tailTable = new int [ size ] ; int len ; tailTable [ 0 ] = A [ 0 ] ; len = 1 ; for ( int i = 1 ; i < size ; i ++ ) { if ( A [ i ] < tailTable [ 0 ] ) tailTable [ 0 ] = A [ i ] ; else if ( A [ i ] > tailTable [ len - 1 ] ) tailTable [ len ++ ] = A [ i ] ; else tailTable [ CeilIndex ( tailTable , - 1 , len - 1 , A [ i ] ) ] = A [ i ] ; } return len ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 5 , 3 , 7 , 11 , 8 , 10 , 13 , 6 } ; int n = A . length ; System . out . println ( " Length ▁ of ▁ Longest ▁ Increasing ▁ Subsequence ▁ is ▁ " + LongestIncreasingSubsequenceLength ( A , n ) ) ; } }

def CeilIndex ( A , l , r , key ) : NEW_LINE INDENT while ( r - l > 1 ) : NEW_LINE INDENT m = l + ( r - l ) // 2 NEW_LINE if ( A [ m ] >= key ) : NEW_LINE INDENT r = m NEW_LINE DEDENT else : NEW_LINE INDENT l = m NEW_LINE DEDENT DEDENT return r NEW_LINE DEDENT def LongestIncreasingSubsequenceLength ( A , size ) : NEW_LINE INDENT tailTable = [ 0 for i in range ( size + 1 ) ] NEW_LINE len = 0 NEW_LINE tailTable [ 0 ] = A [ 0 ] NEW_LINE len = 1 NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT if ( A [ i ] < tailTable [ 0 ] ) : NEW_LINE INDENT tailTable [ 0 ] = A [ i ] NEW_LINE DEDENT elif ( A [ i ] > tailTable [ len - 1 ] ) : NEW_LINE INDENT tailTable [ len ] = A [ i ] NEW_LINE len += 1 NEW_LINE DEDENT else : NEW_LINE INDENT tailTable [ CeilIndex ( tailTable , - 1 , len - 1 , A [ i ] ) ] = A [ i ] NEW_LINE DEDENT DEDENT return len NEW_LINE DEDENT A = [ 2 , 5 , 3 , 7 , 11 , 8 , 10 , 13 , 6 ] NEW_LINE n = len ( A ) NEW_LINE print ( " Length ▁ of ▁ Longest ▁ Increasing ▁ Subsequence ▁ is ▁ " , LongestIncreasingSubsequenceLength ( A , n ) ) NEW_LINE


class GFG { static int subsetCount ( int arr [ ] , int n ) { return 1 << n ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 } ; int n = A . length ; System . out . println ( subsetCount ( A , n ) ) ; } }

import math NEW_LINE def subsetCount ( arr , n ) : NEW_LINE INDENT return 1 << n NEW_LINE DEDENT A = [ 1 , 2 , 3 ] NEW_LINE n = len ( A ) NEW_LINE print ( subsetCount ( A , n ) ) NEW_LINE


import java . util . * ; class GFG { static int cntSubSeq ( int arr [ ] , int n ) { int pos_count = 0 ; int neg_count = 0 ; int result ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos_count ++ ; if ( arr [ i ] < 0 ) neg_count ++ ; } result = ( int ) Math . pow ( 2 , pos_count ) ; if ( neg_count > 0 ) result *= Math . pow ( 2 , neg_count - 1 ) ; result -= 1 ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , - 3 , - 1 , 4 } ; int n = arr . length ; System . out . print ( cntSubSeq ( arr , n ) ) ; } }

import math NEW_LINE def cntSubSeq ( arr , n ) : NEW_LINE INDENT pos_count = 0 ; NEW_LINE neg_count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT pos_count += 1 NEW_LINE DEDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT neg_count += 1 NEW_LINE DEDENT DEDENT result = int ( math . pow ( 2 , pos_count ) ) NEW_LINE if ( neg_count > 0 ) : NEW_LINE INDENT result *= int ( math . pow ( 2 , neg_count - 1 ) ) NEW_LINE DEDENT result -= 1 NEW_LINE return result NEW_LINE DEDENT arr = [ 2 , - 3 , - 1 , 4 ] NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntSubSeq ( arr , n ) ) NEW_LINE


class GFG { static boolean existsTriplet ( int a [ ] , int b [ ] , int c [ ] , int x , int l1 , int l2 , int l3 ) { if ( l2 <= l1 && l2 <= l3 ) { swap ( l2 , l1 ) ; swap ( a , b ) ; } else if ( l3 <= l1 && l3 <= l2 ) { swap ( l3 , l1 ) ; swap ( a , c ) ; } for ( int i = 0 ; i < l1 ; i ++ ) { int j = 0 , k = l3 - 1 ; while ( j < l2 && k >= 0 ) { if ( a [ i ] + b [ j ] + c [ k ] == x ) return true ; if ( a [ i ] + b [ j ] + c [ k ] < x ) j ++ ; else k -- ; } } return false ; } private static void swap ( int x , int y ) { int temp = x ; x = y ; y = temp ; } private static void swap ( int [ ] x , int [ ] y ) { int [ ] temp = x ; x = y ; y = temp ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 7 , 8 , 10 , 15 } ; int b [ ] = { 1 , 6 , 7 , 8 } ; int c [ ] = { 4 , 5 , 5 } ; int l1 = a . length ; int l2 = b . length ; int l3 = c . length ; int x = 14 ; if ( existsTriplet ( a , b , c , x , l1 , l2 , l3 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def existsTriplet ( a , b , c , x , l1 , l2 , l3 ) : NEW_LINE INDENT if ( l2 <= l1 and l2 <= l3 ) : NEW_LINE INDENT l1 , l2 = l2 , l1 NEW_LINE a , b = b , a NEW_LINE DEDENT elif ( l3 <= l1 and l3 <= l2 ) : NEW_LINE INDENT l1 , l3 = l3 , l1 NEW_LINE a , c = c , a NEW_LINE DEDENT for i in range ( l1 ) : NEW_LINE INDENT j = 0 NEW_LINE k = l3 - 1 NEW_LINE while ( j < l2 and k >= 0 ) : NEW_LINE INDENT if ( a [ i ] + b [ j ] + c [ k ] == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( a [ i ] + b [ j ] + c [ k ] < x ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT k -= 1 NEW_LINE DEDENT DEDENT DEDENT return False NEW_LINE DEDENT a = [ 2 , 7 , 8 , 10 , 15 ] NEW_LINE b = [ 1 , 6 , 7 , 8 ] NEW_LINE c = [ 4 , 5 , 5 ] NEW_LINE l1 = len ( a ) NEW_LINE l2 = len ( b ) NEW_LINE l3 = len ( c ) NEW_LINE x = 14 NEW_LINE if ( existsTriplet ( a , b , c , x , l1 , l2 , l3 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class Solution { static int max_element ( int A [ ] ) { int max = Integer . MIN_VALUE ; for ( int i = 0 ; i < A . length ; i ++ ) if ( max < A [ i ] ) max = A [ i ] ; return max ; } static void SumDivPrime ( int A [ ] , int n ) { int max_val = ( max_element ( A ) ) + 1 ; boolean prime [ ] = new boolean [ max_val + 1 ] ; for ( int i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( prime [ A [ i ] ] ) sum += A [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { if ( prime [ A [ i ] ] && sum % A [ i ] == 0 ) { System . out . println ( " YES " ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String args [ ] ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = A . length ; SumDivPrime ( A , n ) ; } }

import math NEW_LINE def SumDivPrime ( A , n ) : NEW_LINE INDENT max_val = max ( A ) + 1 NEW_LINE prime = [ True ] * ( max_val + 1 ) NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( math . sqrt ( max_val ) ) + 1 ) : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ A [ i ] ] : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ A [ i ] ] and sum % A [ i ] == 0 : NEW_LINE INDENT print ( " YES " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " NO " ) NEW_LINE DEDENT A = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( A ) NEW_LINE SumDivPrime ( A , n ) NEW_LINE


class GFG { static void printNcR ( int n , int r ) { long p = 1 , k = 1 ; if ( n - r < r ) { r = n - r ; } if ( r != 0 ) { while ( r > 0 ) { p *= n ; k *= r ; long m = __gcd ( p , k ) ; p /= m ; k /= m ; n -- ; r -- ; } } else { p = 1 ; } System . out . println ( p ) ; } static long __gcd ( long n1 , long n2 ) { long gcd = 1 ; for ( int i = 1 ; i <= n1 && i <= n2 ; ++ i ) { if ( n1 % i == 0 && n2 % i == 0 ) { gcd = i ; } } return gcd ; } public static void main ( String [ ] args ) { int n = 50 , r = 25 ; printNcR ( n , r ) ; } }

from math import * NEW_LINE def printNcR ( n , r ) : NEW_LINE INDENT p = 1 NEW_LINE k = 1 NEW_LINE if ( n - r < r ) : NEW_LINE INDENT r = n - r NEW_LINE DEDENT if ( r != 0 ) : NEW_LINE INDENT while ( r ) : NEW_LINE INDENT p *= n NEW_LINE k *= r NEW_LINE m = gcd ( p , k ) NEW_LINE p //= m NEW_LINE k //= m NEW_LINE n -= 1 NEW_LINE r -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT p = 1 NEW_LINE DEDENT print ( p ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 50 NEW_LINE r = 25 NEW_LINE printNcR ( n , r ) NEW_LINE DEDENT


import java . util . Arrays ; import java . io . * ; class GFG { static int CalculateMax ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int min_sum = arr [ 0 ] + arr [ 1 ] ; int max_sum = arr [ n - 1 ] + arr [ n - 2 ] ; return ( Math . abs ( max_sum - min_sum ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 7 , 1 , 11 } ; int n = arr . length ; System . out . println ( CalculateMax ( arr , n ) ) ; } }

def CalculateMax ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE min_sum = arr [ 0 ] + arr [ 1 ] NEW_LINE max_sum = arr [ n - 1 ] + arr [ n - 2 ] NEW_LINE return abs ( max_sum - min_sum ) NEW_LINE DEDENT arr = [ 6 , 7 , 1 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE print ( CalculateMax ( arr , n ) ) NEW_LINE


class GFG { static long sumOfDigits ( long n ) { long sum = 0 ; while ( n > 0 ) { sum += n % 10 ; n /= 10 ; } return sum ; } static long countDigits ( long num , long limit ) { long left = 1 , right = num , result = 0 ; while ( left <= right ) { long mid = ( right + left ) / 2 ; if ( ( mid - sumOfDigits ( mid ) ) >= limit ) { result = num - mid + 1 ; right = mid - 1 ; } else { left = mid + 1 ; } } return result ; } public static void main ( String [ ] args ) { long N = 1546300 , L = 30651 ; System . out . println ( countDigits ( N , L ) ) ; } }

def sumOfDigits ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += n % 10 NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def countDigits ( num , limit ) : NEW_LINE INDENT left = 1 NEW_LINE right = num NEW_LINE result = 0 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = int ( ( right + left ) / 2 ) NEW_LINE if ( ( mid - sumOfDigits ( mid ) ) >= limit ) : NEW_LINE INDENT result = num - mid + 1 NEW_LINE right = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 1546300 NEW_LINE L = 30651 NEW_LINE print ( countDigits ( N , L ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class Node { int data ; Node left , right ; Node ( int val ) { data = val ; left = null ; right = null ; } } ; static void spiralOrder ( Node root ) { Deque < Node > d = new LinkedList < Node > ( ) ; d . addLast ( root ) ; int dir = 0 ; while ( d . size ( ) > 0 ) { int size = d . size ( ) ; while ( size -- > 0 ) { if ( dir == 0 ) { Node temp = d . peekLast ( ) ; d . pollLast ( ) ; if ( temp . right != null ) d . addFirst ( temp . right ) ; if ( temp . left != null ) d . addFirst ( temp . left ) ; System . out . print ( temp . data + " ▁ " ) ; } else { Node temp = d . peekFirst ( ) ; d . pollFirst ( ) ; if ( temp . left != null ) d . addLast ( temp . left ) ; if ( temp . right != null ) d . addLast ( temp . right ) ; System . out . print ( temp . data + " ▁ " ) ; } } System . out . println ( ) ; dir = 1 - dir ; } } public static void main ( String args [ ] ) { Node root = new Node ( 10 ) ; root . left = new Node ( 20 ) ; root . right = new Node ( 30 ) ; root . left . left = new Node ( 40 ) ; root . left . right = new Node ( 60 ) ; spiralOrder ( root ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , val ) : NEW_LINE INDENT self . data = val ; NEW_LINE self . left = None ; NEW_LINE self . right = None ; NEW_LINE DEDENT DEDENT def spiralOrder ( root ) : NEW_LINE INDENT d = [ ] ; NEW_LINE d . append ( root ) ; NEW_LINE direct = 0 ; NEW_LINE while ( len ( d ) != 0 ) : NEW_LINE INDENT size = len ( d ) ; NEW_LINE while ( size ) : NEW_LINE INDENT size -= 1 ; NEW_LINE if ( direct == 0 ) : NEW_LINE INDENT temp = d . pop ( ) ; NEW_LINE if ( temp . right ) : NEW_LINE INDENT d . insert ( 0 , temp . right ) ; NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT d . insert ( 0 , temp . left ) ; NEW_LINE DEDENT print ( temp . data , end = " ▁ " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT temp = d [ 0 ] ; NEW_LINE d . pop ( 0 ) ; NEW_LINE if ( temp . left ) : NEW_LINE INDENT d . append ( temp . left ) ; NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT d . append ( temp . right ) ; NEW_LINE DEDENT print ( temp . data , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT print ( ) NEW_LINE direct = 1 - direct ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = Node ( 10 ) ; NEW_LINE root . left = Node ( 20 ) ; NEW_LINE root . right = Node ( 30 ) ; NEW_LINE root . left . left = Node ( 40 ) ; NEW_LINE root . left . right = Node ( 60 ) ; NEW_LINE spiralOrder ( root ) ; NEW_LINE DEDENT


class GFG { public static boolean strcheck ( String s1 , String s2 ) { if ( s1 . compareTo ( s2 ) != 0 ) return false ; return true ; } public static boolean check ( String s ) { if ( s . length ( ) >= 10 ) return true ; for ( int i = 1 ; i < s . length ( ) ; i ++ ) { for ( int j = i + 1 ; j < s . length ( ) ; j ++ ) { for ( int k = j + 1 ; k < s . length ( ) ; k ++ ) { String s1 = " " , s2 = " " , s3 = " " , s4 = " " ; try { s1 = s . substring ( 0 , i ) ; s2 = s . substring ( i , j - i ) ; s3 = s . substring ( j , k - j ) ; s4 = s . substring ( k , s . length ( ) - k ) ; } catch ( StringIndexOutOfBoundsException e ) { } if ( strcheck ( s1 , s2 ) && strcheck ( s1 , s3 ) && strcheck ( s1 , s4 ) && strcheck ( s2 , s3 ) && strcheck ( s2 , s4 ) && strcheck ( s3 , s4 ) ) return true ; } } } return false ; } public static void main ( String [ ] args ) { String str = " aaabb " ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def check ( s ) : NEW_LINE INDENT if ( len ( s ) >= 10 ) : NEW_LINE INDENT return True NEW_LINE DEDENT for i in range ( 1 , len ( s ) ) : NEW_LINE INDENT for j in range ( i + 1 , len ( s ) ) : NEW_LINE INDENT for k in range ( j + 1 , len ( s ) ) : NEW_LINE INDENT s1 = s [ 0 : i ] NEW_LINE s2 = s [ i : j - i ] NEW_LINE s3 = s [ j : k - j ] NEW_LINE s4 = s [ k : len ( s ) - k ] NEW_LINE if ( s1 != s2 and s1 != s3 and s1 != s4 and s2 != s3 and s2 != s4 and s3 != s4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aaabb " NEW_LINE print ( " Yes " ) if ( check ( str ) ) else print ( " NO " ) NEW_LINE DEDENT


static int mod ( int a , int m ) { return ( a % m + m ) % m ; }

def mod ( a , m ) : NEW_LINE INDENT return ( a % m + m ) % m NEW_LINE DEDENT


import static java . lang . Math . * ; class GFG { static int sumofsquare ( int n ) { int [ ] [ ] C = new int [ n + 1 ] [ n + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= min ( i , n ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } int sum = 0 ; for ( i = 0 ; i <= n ; i ++ ) sum += ( C [ n ] [ i ] * C [ n ] [ i ] ) ; return sum ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( sumofsquare ( n ) ) ; } }

def sumofsquare ( n ) : NEW_LINE INDENT C = [ [ 0 for i in range ( n + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT for j in range ( 0 , min ( i , n ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT sum = sum + ( C [ n ] [ i ] * C [ n ] [ i ] ) NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 4 NEW_LINE print ( sumofsquare ( n ) , end = " \n " ) NEW_LINE


import java . io . * ; class GFG { static double Area ( int b1 , int b2 , int h ) { return ( ( b1 + b2 ) / 2 ) * h ; } public static void main ( String [ ] args ) { int base1 = 8 , base2 = 10 , height = 6 ; double area = Area ( base1 , base2 , height ) ; System . out . println ( " Area ▁ is : ▁ " + area ) ; } }

def Area ( b1 , b2 , h ) : NEW_LINE INDENT return ( ( b1 + b2 ) / 2 ) * h NEW_LINE DEDENT base1 = 8 ; base2 = 10 ; height = 6 NEW_LINE area = Area ( base1 , base2 , height ) NEW_LINE print ( " Area ▁ is : " , area ) NEW_LINE


class GFG { static int MAXN = 100000 ; static int MOD = 1000000007 ; static long fact [ ] = new long [ MAXN ] ; static void factorial ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < MAXN ; i ++ ) fact [ i ] = ( fact [ i - 1 ] * i ) % MOD ; } static long power ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long x , long p ) { return power ( x , p - 2 , p ) ; } static long countDifferentNumbers ( long arr [ ] , long P ) { factorial ( ) ; long res = 0 , X = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) X += arr [ i ] ; res = fact [ ( int ) X ] ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( arr [ i ] > 1 ) res = ( res * modInverse ( fact [ ( int ) arr [ i ] ] , P ) ) % P ; } return res ; } public static void main ( String [ ] args ) { long arr [ ] = { 1 , 0 , 2 , 0 , 0 , 7 , 4 , 0 , 0 , 3 } ; System . out . println ( countDifferentNumbers ( arr , MOD ) ) ; } }

MAXN = 100000 NEW_LINE MOD = 1000000007 NEW_LINE fact = [ 0 ] * MAXN ; NEW_LINE def factorial ( ) : NEW_LINE INDENT fact [ 0 ] = 1 NEW_LINE for i in range ( 1 , MAXN ) : NEW_LINE INDENT fact [ i ] = ( fact [ i - 1 ] * i ) % MOD NEW_LINE DEDENT DEDENT def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 ; NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def modInverse ( x , p ) : NEW_LINE INDENT return power ( x , p - 2 , p ) NEW_LINE DEDENT def countDifferentNumbers ( arr , P ) : NEW_LINE INDENT factorial ( ) ; NEW_LINE res = 0 ; X = 0 ; NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT X += arr [ i ] NEW_LINE DEDENT res = fact [ X ] NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( arr [ i ] > 1 ) : NEW_LINE INDENT res = ( res * modInverse ( fact [ arr [ i ] ] , P ) ) % P ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 2 , 0 , 0 , 7 , 4 , 0 , 0 , 3 ] NEW_LINE print ( countDifferentNumbers ( arr , MOD ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int count_pairs ( int a [ ] , int b [ ] , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) odd1 ++ ; else even1 ++ ; } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) odd2 ++ ; else even2 ++ ; } int pairs = Math . min ( odd1 , odd2 ) + Math . min ( even1 , even2 ) ; return pairs ; } public static void main ( String [ ] args ) { int a [ ] = { 9 , 14 , 6 , 2 , 11 } ; int b [ ] = { 8 , 4 , 7 , 20 } ; int n = a . length ; int m = b . length ; System . out . println ( count_pairs ( a , b , n , m ) ) ; } }

def count_pairs ( a , b , n , m ) : NEW_LINE INDENT odd1 = 0 NEW_LINE even1 = 0 NEW_LINE odd2 = 0 NEW_LINE even2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 == 1 ) : NEW_LINE INDENT odd1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even1 += 1 NEW_LINE DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT if ( b [ i ] % 2 == 1 ) : NEW_LINE INDENT odd2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even2 += 1 NEW_LINE DEDENT DEDENT pairs = min ( odd1 , odd2 ) + min ( even1 , even2 ) NEW_LINE return pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 9 , 14 , 6 , 2 , 11 ] NEW_LINE b = [ 8 , 4 , 7 , 20 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( count_pairs ( a , b , n , m ) ) NEW_LINE DEDENT


class GFG { static int maxTeams ( int N1 , int N2 ) { int count = 0 ; while ( N1 > 0 && N2 > 0 && N1 + N2 >= 3 ) { if ( N1 > N2 ) { N1 -= 2 ; N2 -= 1 ; } else { N1 -= 1 ; N2 -= 2 ; } count ++ ; } return count ; } public static void main ( String [ ] args ) { int N1 = 4 , N2 = 5 ; System . out . println ( maxTeams ( N1 , N2 ) ) ; } }

def maxTeams ( N1 , N2 ) : NEW_LINE INDENT count = 0 NEW_LINE while ( N1 > 0 and N2 > 0 and N1 + N2 >= 3 ) : NEW_LINE INDENT if ( N1 > N2 ) : NEW_LINE INDENT N1 -= 2 NEW_LINE N2 -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT N1 -= 1 NEW_LINE N2 -= 2 NEW_LINE DEDENT count = count + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT N1 = 4 NEW_LINE N2 = 5 NEW_LINE print ( maxTeams ( N1 , N2 ) ) NEW_LINE


import java . util . * ; class solution { static void printExpansion ( String str ) { String suff = " " ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) { suff = suff + str . charAt ( i ) ; System . out . print ( suff ) ; } } public static void main ( String args [ ] ) { String str = " geeks " ; printExpansion ( str ) ; } }

def printExpansion ( str ) : NEW_LINE INDENT suff = " " NEW_LINE for i in range ( len ( str ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT suff = suff + str [ i ] NEW_LINE print ( suff , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeks " NEW_LINE printExpansion ( str ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class GFG { static void printArr ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } static void generateArr ( int arr [ ] , int n ) { boolean flip = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] & 1 ) != 0 ) { if ( flip ^= true ) System . out . print ( ( int ) ( Math . ceil ( arr [ i ] / 2.0 ) ) + " ▁ " ) ; else System . out . print ( ( int ) ( Math . floor ( arr [ i ] / 2.0 ) ) + " ▁ " ) ; } else { System . out . print ( arr [ i ] / 2 + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , - 5 , - 7 , 9 , 2 , - 2 } ; int n = arr . length ; generateArr ( arr , n ) ; } }

from math import ceil , floor NEW_LINE def printArr ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def generateArr ( arr , n ) : NEW_LINE INDENT flip = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] & 1 ) : NEW_LINE INDENT flip ^= True NEW_LINE if ( flip ) : NEW_LINE INDENT print ( int ( ceil ( ( arr [ i ] ) / 2 ) ) , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( int ( floor ( ( arr [ i ] ) / 2 ) ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( int ( arr [ i ] / 2 ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 3 , - 5 , - 7 , 9 , 2 , - 2 ] NEW_LINE n = len ( arr ) NEW_LINE generateArr ( arr , n ) NEW_LINE


import java . util . * ; class GFG { static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; return __gcd ( b % a , a ) ; } static int minimumMoves ( int A [ ] , int N ) { int one = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( A [ i ] == 1 ) one ++ ; if ( one != 0 ) return N - one ; int minimum = Integer . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { int g = A [ i ] ; for ( int j = i + 1 ; j < N ; j ++ ) { g = __gcd ( A [ j ] , g ) ; if ( g == 1 ) { minimum = Math . min ( minimum , j - i ) ; break ; } } } if ( minimum == Integer . MAX_VALUE ) return - 1 ; else return N + minimum - 1 ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 4 , 3 , 9 } ; int N = A . length ; System . out . print ( minimumMoves ( A , N ) ) ; } }

def __gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return __gcd ( b % a , a ) NEW_LINE DEDENT def minimumMoves ( A , N ) : NEW_LINE INDENT one = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] == 1 ) : NEW_LINE INDENT one += 1 NEW_LINE DEDENT DEDENT if ( one != 0 ) : NEW_LINE INDENT return N - one NEW_LINE DEDENT minimum = + 2147483647 NEW_LINE for i in range ( N ) : NEW_LINE INDENT g = A [ i ] NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT g = __gcd ( A [ j ] , g ) NEW_LINE if ( g == 1 ) : NEW_LINE INDENT minimum = min ( minimum , j - i ) NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( minimum == + 2147483647 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return N + minimum - 1 ; NEW_LINE DEDENT DEDENT A = [ 2 , 4 , 3 , 9 ] NEW_LINE N = len ( A ) NEW_LINE print ( minimumMoves ( A , N ) ) NEW_LINE


class GFG { static int check_digits ( int n ) { while ( n != 0 ) { if ( ( n % 10 ) % 2 != 0 ) return 0 ; n /= 10 ; } return 1 ; } static int smallest_number ( int n ) { for ( int i = n ; ; i ++ ) if ( check_digits ( i ) != 0 ) return i ; } public static void main ( String [ ] args ) { int N = 2397 ; System . out . println ( smallest_number ( N ) ) ; } }

def check_digits ( n ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT if ( ( n % 10 ) % 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT n = int ( n / 10 ) NEW_LINE DEDENT return 1 NEW_LINE DEDENT def smallest_number ( n ) : NEW_LINE INDENT for i in range ( n , 2401 ) : NEW_LINE INDENT if ( check_digits ( i ) == 1 ) : NEW_LINE INDENT return ( i ) NEW_LINE DEDENT DEDENT DEDENT N = 2397 NEW_LINE print ( str ( smallest_number ( N ) ) ) NEW_LINE


class GFG { static double mulmod ( long a , long b , long mod ) { long res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } static long findProduct ( long N ) { long product = 1 , fact = 1 ; long MOD = ( long ) ( 1e9 + 7 ) ; for ( int i = 1 ; i <= N ; i ++ ) { fact = ( long ) mulmod ( fact , i , MOD ) ; product = ( long ) mulmod ( product , fact , MOD ) ; if ( product == 0 ) return 0 ; } return product ; } public static void main ( String [ ] args ) { long N = 3 ; System . out . println ( findProduct ( N ) ) ; N = 5 ; System . out . println ( findProduct ( N ) ) ; } }

def mulmod ( a , b , mod ) : NEW_LINE INDENT res = 0 NEW_LINE a = a % mod NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( b % 2 == 1 ) : NEW_LINE INDENT res = ( res + a ) % mod NEW_LINE DEDENT a = ( a * 2 ) % mod NEW_LINE b //= 2 NEW_LINE DEDENT return res % mod NEW_LINE DEDENT def findProduct ( N ) : NEW_LINE INDENT product = 1 ; fact = 1 NEW_LINE MOD = 1e9 + 7 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fact = mulmod ( fact , i , MOD ) NEW_LINE product = mulmod ( product , fact , MOD ) NEW_LINE if not product : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return int ( product ) NEW_LINE DEDENT N = 3 NEW_LINE print ( findProduct ( N ) ) NEW_LINE N = 5 NEW_LINE print ( findProduct ( N ) ) NEW_LINE


class GFG { static long binomialCoeff ( long n , long k ) { if ( k == 0 || k == n ) return 1 ; return binomialCoeff ( n - 1 , k - 1 ) + binomialCoeff ( n - 1 , k ) ; } static long fact ( long n ) { if ( n >= 1 ) return n * fact ( n - 1 ) ; else return 1 ; } static boolean isVowel ( char ch ) { if ( ch == ' a ' || ch == ' e ' || ch == ' i ' || ch == ' o ' || ch == ' u ' ) { return true ; } return false ; } static long countWords ( String s , int p , int q ) { long countc = 0 , countv = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) countv ++ ; else countc ++ ; } long a = binomialCoeff ( countc , p ) ; long b = binomialCoeff ( countv , q ) ; long c = fact ( p + q ) ; long ans = ( a * b ) * c ; return ans ; } public static void main ( String [ ] args ) { String s = " crackathon " ; int p = 4 , q = 3 ; System . out . println ( countWords ( s , p , q ) ) ; } }

def binomialCoeff ( n , k ) : NEW_LINE INDENT if ( k == 0 or k == n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return binomialCoeff ( n - 1 , k - 1 ) +   \ NEW_LINE INDENT binomialCoeff ( n - 1 , k ) NEW_LINE DEDENT DEDENT def fact ( n ) : NEW_LINE INDENT if ( n >= 1 ) : NEW_LINE INDENT return n * fact ( n - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT def isVowel ( ch ) : NEW_LINE INDENT if ( ch == ' a ' or ch == ' e ' or ch == ' i ' or ch == ' o ' or ch == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def countWords ( s , p , q ) : NEW_LINE INDENT countc = 0 NEW_LINE countv = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT countv += 1 NEW_LINE DEDENT else : NEW_LINE INDENT countc += 1 NEW_LINE DEDENT DEDENT a = binomialCoeff ( countc , p ) NEW_LINE b = binomialCoeff ( countv , q ) NEW_LINE c = fact ( p + q ) NEW_LINE ans = ( a * b ) * c NEW_LINE return ans NEW_LINE DEDENT s = " crackathon " NEW_LINE p = 4 NEW_LINE q = 3 NEW_LINE print ( countWords ( s , p , q ) ) NEW_LINE


import java . util . Arrays ; class GFG { static int minNumber ( int a [ ] , int n , int x ) { Arrays . sort ( a ) ; int k ; for ( k = 0 ; a [ ( n ) / 2 ] != x ; k ++ ) { a [ n ++ ] = x ; Arrays . sort ( a ) ; } return k ; } public static void main ( String [ ] args ) { int x = 10 ; int a [ ] = { 10 , 20 , 30 } ; int n = 3 ; System . out . println ( minNumber ( a , n - 1 , x ) ) ; } }

def minNumber ( a , n , x ) : NEW_LINE INDENT a . sort ( reverse = False ) NEW_LINE k = 0 NEW_LINE while ( a [ int ( ( n - 1 ) / 2 ) ] != x ) : NEW_LINE INDENT a [ n - 1 ] = x NEW_LINE n += 1 NEW_LINE a . sort ( reverse = False ) NEW_LINE k += 1 NEW_LINE DEDENT return k NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 10 NEW_LINE a = [ 10 , 20 , 30 ] NEW_LINE n = 3 NEW_LINE print ( minNumber ( a , n , x ) ) NEW_LINE DEDENT


class GFG { static int reverse ( int num ) { int rev = 0 ; while ( num > 0 ) { rev = rev * 10 + num % 10 ; num = num / 10 ; } return rev ; } static boolean isPalindrome ( int num ) { if ( num == reverse ( num ) ) return true ; return false ; } static void printPalindromes ( int d ) { if ( d <= 0 ) return ; int smallest = ( int ) Math . pow ( 10 , d - 1 ) ; int largest = ( int ) Math . pow ( 10 , d ) - 1 ; for ( int i = smallest ; i <= largest ; i ++ ) { if ( isPalindrome ( i ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int d = 2 ; printPalindromes ( d ) ; } }

def reverse ( num ) : NEW_LINE INDENT rev = 0 ; NEW_LINE while ( num > 0 ) : NEW_LINE INDENT rev = rev * 10 + num % 10 ; NEW_LINE num = num // 10 ; NEW_LINE DEDENT return rev ; NEW_LINE DEDENT def isPalindrome ( num ) : NEW_LINE INDENT if ( num == reverse ( num ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def printPalindromes ( d ) : NEW_LINE INDENT if ( d <= 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT smallest = pow ( 10 , d - 1 ) ; NEW_LINE largest = pow ( 10 , d ) - 1 ; NEW_LINE for i in range ( smallest , largest + 1 ) : NEW_LINE INDENT if ( isPalindrome ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT d = 2 ; NEW_LINE printPalindromes ( d ) ; NEW_LINE


import java . util . * ; class solution { static void Alphabet_N_Pattern ( int N ) { int index , side_index , size ; int Right = 1 , Left = 1 , Diagonal = 2 ; for ( index = 0 ; index < N ; index ++ ) { System . out . print ( Left ++ ) ; for ( side_index = 0 ; side_index < 2 * ( index ) ; side_index ++ ) System . out . print ( " ▁ " ) ; if ( index != 0 && index != N - 1 ) System . out . print ( Diagonal ++ ) ; else System . out . print ( " ▁ " ) ; for ( side_index = 0 ; side_index < 2 * ( N - index - 1 ) ; side_index ++ ) System . out . print ( " ▁ " ) ; System . out . print ( Right ++ ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { int Size = 6 ; Alphabet_N_Pattern ( Size ) ; } }

def Alphabet_N_Pattern ( N ) : NEW_LINE INDENT Right = 1 NEW_LINE Left = 1 NEW_LINE Diagonal = 2 NEW_LINE for index in range ( N ) : NEW_LINE INDENT print ( Left , end = " " ) NEW_LINE Left += 1 NEW_LINE for side_index in range ( 0 , 2 * ( index ) , 1 ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT if ( index != 0 and index != N - 1 ) : NEW_LINE INDENT print ( Diagonal , end = " " ) NEW_LINE Diagonal += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT for side_index in range ( 0 , 2 * ( N - index - 1 ) , 1 ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT print ( Right , end = " " ) NEW_LINE Right += 1 NEW_LINE print ( " \n " , end = " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Size = 6 NEW_LINE Alphabet_N_Pattern ( Size ) NEW_LINE DEDENT


import java . io . * ; import java . math . * ; class GFG { static int getSum ( int n ) { int sum = 0 ; while ( n != 0 ) { sum = sum + n % 10 ; n = n / 10 ; } return sum ; } static int largestDigitSumdivisior ( int n ) { int res = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { res = Math . max ( res , getSum ( i ) ) ; res = Math . max ( res , getSum ( n / i ) ) ; } } return res ; } public static void main ( String args [ ] ) { int n = 14 ; System . out . println ( largestDigitSumdivisior ( n ) ) ; } }

import math NEW_LINE def getSum ( n ) : NEW_LINE INDENT sm = 0 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT sm = sm + n % 10 NEW_LINE n = n // 10 NEW_LINE DEDENT return sm NEW_LINE DEDENT def largestDigitSumdivisior ( n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 1 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT res = max ( res , getSum ( i ) ) NEW_LINE res = max ( res , getSum ( n // i ) ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT n = 14 NEW_LINE print ( largestDigitSumdivisior ( n ) ) NEW_LINE


class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static int countNumbers ( int X , int Y , int N ) { int divisibleByX = N / X ; int divisibleByY = N / Y ; int LCM = ( X * Y ) / gcd ( X , Y ) ; int divisibleByLCM = N / LCM ; int divisibleByXorY = divisibleByX + divisibleByY - divisibleByLCM ; int divisibleByXnotY = divisibleByXorY - divisibleByY ; return divisibleByXnotY ; } public static void main ( String [ ] args ) { int X = 2 , Y = 3 , N = 10 ; System . out . println ( countNumbers ( X , Y , N ) ) ; } }

from math import gcd NEW_LINE def countNumbers ( X , Y , N ) : NEW_LINE INDENT divisibleByX = int ( N / X ) NEW_LINE divisibleByY = int ( N / Y ) NEW_LINE LCM = int ( ( X * Y ) / gcd ( X , Y ) ) NEW_LINE divisibleByLCM = int ( N / LCM ) NEW_LINE divisibleByXorY = ( divisibleByX + divisibleByY - divisibleByLCM ) NEW_LINE divisibleByXnotY = ( divisibleByXorY - divisibleByY ) NEW_LINE return divisibleByXnotY NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 2 NEW_LINE Y = 3 NEW_LINE N = 10 NEW_LINE print ( countNumbers ( X , Y , N ) ) NEW_LINE DEDENT


import java . lang . * ; class GFG { public static boolean ispalindrome ( int x ) { int ans = 0 ; int temp = x ; while ( temp > 0 ) { ans = 10 * ans + temp % 10 ; temp = temp / 10 ; } return ans == x ; } public static int SuperPalindromes ( int L , int R ) { int LIMIT = 100000 ; int ans = 0 ; for ( int i = 0 ; i < LIMIT ; i ++ ) { String s = Integer . toString ( i ) ; StringBuilder rs = new StringBuilder ( ) ; rs . append ( s . substring ( 0 , Math . max ( 1 , s . length ( ) - 1 ) ) ) ; String srs = rs . reverse ( ) . toString ( ) ; String p = s + srs ; int p_sq = ( int ) ( Math . pow ( Integer . parseInt ( p ) , 2 ) ) ; if ( p_sq > R ) { break ; } if ( p_sq >= L && ispalindrome ( p_sq ) ) { ans = ans + 1 ; } } for ( int i = 0 ; i < LIMIT ; i ++ ) { String s = Integer . toString ( i ) ; StringBuilder rs = new StringBuilder ( ) ; rs . append ( s ) ; rs = rs . reverse ( ) ; String p = s + rs ; int p_sq = ( int ) ( Math . pow ( Integer . parseInt ( p ) , 2 ) ) ; if ( p_sq > R ) { break ; } if ( p_sq >= L && ispalindrome ( p_sq ) ) { ans = ans + 1 ; } } return ans ; } public static void main ( String [ ] args ) { String L = "4" ; String R = "1000" ; System . out . println ( SuperPalindromes ( Integer . parseInt ( L ) , Integer . parseInt ( R ) ) ) ; } }

def ispalindrome ( x ) : NEW_LINE INDENT ans , temp = 0 , x NEW_LINE while temp > 0 : NEW_LINE INDENT ans = 10 * ans + temp % 10 NEW_LINE temp = temp // 10 NEW_LINE DEDENT return ans == x NEW_LINE DEDENT def SuperPalindromes ( L , R ) : NEW_LINE INDENT L , R = int ( L ) , int ( R ) NEW_LINE LIMIT = 100000 NEW_LINE ans = 0 NEW_LINE for i in range ( LIMIT ) : NEW_LINE INDENT s = str ( i ) NEW_LINE p = s + s [ - 2 : : - 1 ] NEW_LINE p_sq = int ( p ) ** 2 NEW_LINE if p_sq > R : NEW_LINE INDENT break NEW_LINE DEDENT if p_sq >= L and ispalindrome ( p_sq ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE DEDENT DEDENT for i in range ( LIMIT ) : NEW_LINE INDENT s = str ( i ) NEW_LINE p = s + s [ : : - 1 ] NEW_LINE p_sq = int ( p ) ** 2 NEW_LINE if p_sq > R : NEW_LINE INDENT break NEW_LINE DEDENT if p_sq >= L and ispalindrome ( p_sq ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT L = "4" NEW_LINE R = "1000" NEW_LINE print ( SuperPalindromes ( L , R ) ) NEW_LINE


import java . util . Arrays ; import java . util . Collections ; import java . util . List ; class GFG { static int getMax ( int [ ] A ) { int max = Integer . MIN_VALUE ; for ( int i : A ) { max = Math . max ( max , i ) ; } return max ; } static int calculateAND ( int arr [ ] , int n ) { int max_val = getMax ( arr ) ; boolean prime [ ] = new boolean [ max_val + 1 ] ; int i ; for ( i = 0 ; i < max_val + 1 ; i ++ ) prime [ i ] = true ; prime [ 0 ] = false ; prime [ 1 ] = false ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } int S1 = 0 , S2 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { S1 += arr [ i ] ; } else if ( arr [ i ] != 1 ) { S2 += arr [ i ] ; } } return ( S1 & S2 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 4 , 6 , 7 } ; int n = arr . length ; System . out . println ( calculateAND ( arr , n ) ) ; } }

def calculateAND ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , max_val + 1 ) : NEW_LINE INDENT if p * p >= max_val : NEW_LINE INDENT break NEW_LINE DEDENT if ( prime [ p ] ) : NEW_LINE INDENT for i in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT S1 = 0 NEW_LINE S2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT S1 += arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] != 1 ) : NEW_LINE INDENT S2 += arr [ i ] NEW_LINE DEDENT DEDENT return ( S1 & S2 ) NEW_LINE DEDENT arr = [ 3 , 4 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( calculateAND ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static int circle ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; int radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; if ( distSq == radSumSq ) return 1 ; else if ( distSq > radSumSq ) return - 1 ; else return 0 ; } public static void main ( String [ ] args ) { int x1 = - 10 , y1 = 8 ; int x2 = 14 , y2 = - 24 ; int r1 = 30 , r2 = 10 ; int t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( t == 1 ) System . out . println ( " Circle ▁ touch ▁ to " + " ▁ each ▁ other . " ) ; else if ( t < 0 ) System . out . println ( " Circle ▁ not ▁ touch " + " ▁ to ▁ each ▁ other . " ) ; else System . out . println ( " Circle ▁ intersect " + " ▁ to ▁ each ▁ other . " ) ; } }

def circle ( x1 , y1 , x2 , y2 , r1 , r2 ) : NEW_LINE INDENT distSq = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; NEW_LINE radSumSq = ( r1 + r2 ) * ( r1 + r2 ) ; NEW_LINE if ( distSq == radSumSq ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif ( distSq > radSumSq ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT x1 = - 10 NEW_LINE y1 = 8 NEW_LINE x2 = 14 NEW_LINE y2 = - 24 NEW_LINE r1 = 30 NEW_LINE r2 = 10 NEW_LINE t = circle ( x1 , y1 , x2 , y2 , r1 , r2 ) NEW_LINE if ( t == 1 ) : NEW_LINE INDENT print ( " Circle ▁ touch ▁ to ▁ each ▁ other . " ) NEW_LINE DEDENT elif ( t < 0 ) : NEW_LINE INDENT print ( " Circle ▁ not ▁ touch ▁ to ▁ each ▁ other . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Circle ▁ intersect ▁ to ▁ each ▁ other . " ) NEW_LINE DEDENT


class GFG { static int findLength ( int [ ] arr , int n ) { int x = 0 ; for ( int i = 0 ; i < n ; i ++ ) x = Math . max ( x , arr [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != x ) continue ; int j = i ; while ( arr [ j ] == x ) { j ++ ; if ( j >= n ) break ; } ans = Math . max ( ans , j - i ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 } ; int n = arr . length ; System . out . println ( findLength ( arr , n ) ) ; } }

def findLength ( arr , n ) : NEW_LINE INDENT x = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = max ( x , arr [ i ] ) ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != x ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT j = i ; NEW_LINE while ( arr [ j ] == x ) : NEW_LINE INDENT j += 1 ; NEW_LINE if j >= n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT ans = max ( ans , j - i ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findLength ( arr , n ) ) ; NEW_LINE DEDENT


class GFG { static int max ( int x , int y ) { return ( x > y ) ? x : y ; } static int lps ( char seq [ ] , int i , int j ) { if ( i == j ) { return 1 ; } if ( seq [ i ] == seq [ j ] && i + 1 == j ) { return 2 ; } if ( seq [ i ] == seq [ j ] ) { return lps ( seq , i + 1 , j - 1 ) + 2 ; } return max ( lps ( seq , i , j - 1 ) , lps ( seq , i + 1 , j ) ) ; } public static void main ( String [ ] args ) { String seq = " GEEKSFORGEEKS " ; int n = seq . length ( ) ; System . out . printf ( " The ▁ length ▁ of ▁ the ▁ LPS ▁ is ▁ % d " , lps ( seq . toCharArray ( ) , 0 , n - 1 ) ) ; } }

def max ( x , y ) : NEW_LINE INDENT if ( x > y ) : NEW_LINE INDENT return x NEW_LINE DEDENT return y NEW_LINE DEDENT def lps ( seq , i , j ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( seq [ i ] == seq [ j ] and i + 1 == j ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( seq [ i ] == seq [ j ] ) : NEW_LINE INDENT return lps ( seq , i + 1 , j - 1 ) + 2 NEW_LINE DEDENT return max ( lps ( seq , i , j - 1 ) , lps ( seq , i + 1 , j ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT seq = " GEEKSFORGEEKS " NEW_LINE n = len ( seq ) NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ LPS ▁ is " , lps ( seq , 0 , n - 1 ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static long doublefactorial ( long n ) { if ( n == 0 || n == 1 ) return 1 ; return n * doublefactorial ( n - 2 ) ; } static public void main ( String [ ] args ) { System . out . println ( " Double ▁ factorial " + " ▁ is ▁ " + doublefactorial ( 5 ) ) ; } }

def doublefactorial ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return n * doublefactorial ( n - 2 ) ; NEW_LINE DEDENT print ( " Double ▁ factorial ▁ is " , doublefactorial ( 5 ) ) ; NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static final int MAX = 10000 ; static int prefix [ ] = new int [ MAX + 1 ] ; static void buildPrefix ( ) { boolean prime [ ] = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i < MAX + 1 ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } prefix [ 0 ] = prefix [ 1 ] = 0 ; for ( int p = 2 ; p <= MAX ; p ++ ) { prefix [ p ] = prefix [ p - 1 ] ; if ( prime [ p ] == true ) prefix [ p ] += p ; } } static int sumPrimeRange ( int L , int R ) { buildPrefix ( ) ; return prefix [ R ] - prefix [ L - 1 ] ; } public static void main ( String args [ ] ) { int L = 10 , R = 20 ; System . out . println ( sumPrimeRange ( L , R ) ) ; } }

from math import sqrt NEW_LINE MAX = 10000 NEW_LINE prefix = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE def buildPrefix ( ) : NEW_LINE INDENT prime = [ True for i in range ( MAX + 1 ) ] NEW_LINE for p in range ( 2 , int ( sqrt ( MAX ) ) + 1 , 1 ) : 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 DEDENT prefix [ 0 ] = 0 NEW_LINE prefix [ 1 ] = 0 NEW_LINE for p in range ( 2 , MAX + 1 , 1 ) : NEW_LINE INDENT prefix [ p ] = prefix [ p - 1 ] NEW_LINE if ( prime [ p ] ) : NEW_LINE INDENT prefix [ p ] += p NEW_LINE DEDENT DEDENT DEDENT def sumPrimeRange ( L , R ) : NEW_LINE INDENT buildPrefix ( ) NEW_LINE return prefix [ R ] - prefix [ L - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 10 NEW_LINE R = 20 NEW_LINE print ( sumPrimeRange ( L , R ) ) NEW_LINE DEDENT


public class GFG { static int nthTerm ( int n ) { return 3 * ( int ) Math . pow ( n , 2 ) - 4 * n + 2 ; } public static void main ( String args [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }

def nthTerm ( n ) : NEW_LINE INDENT return 3 * pow ( n , 2 ) - 4 * n + 2 NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE


import java . util . * ; class GFG { static long subarrayXor ( int arr [ ] , int n , int m ) { long ans = 0 ; int [ ] xorArr = new int [ n ] ; HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; xorArr [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) xorArr [ i ] = xorArr [ i - 1 ] ^ arr [ i ] ; for ( int i = 0 ; i < n ; i ++ ) { int tmp = m ^ xorArr [ i ] ; ans = ans + ( mp . containsKey ( tmp ) == false ? 0 : ( ( long ) mp . get ( tmp ) ) ) ; if ( xorArr [ i ] == m ) ans ++ ; if ( mp . containsKey ( xorArr [ i ] ) ) mp . put ( xorArr [ i ] , mp . get ( xorArr [ i ] ) + 1 ) ; else mp . put ( xorArr [ i ] , 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 2 , 6 , 4 } ; int n = arr . length ; int m = 6 ; System . out . print ( " Number ▁ of ▁ subarrays ▁ having ▁ given ▁ XOR ▁ is ▁ " + subarrayXor ( arr , n , m ) ) ; } }

def subarrayXor ( arr , n , m ) : NEW_LINE INDENT ans = 0 NEW_LINE xorArr = [ 0 for _ in range ( n ) ] NEW_LINE mp = dict ( ) NEW_LINE xorArr [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT xorArr [ i ] = xorArr [ i - 1 ] ^ arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT tmp = m ^ xorArr [ i ] NEW_LINE if tmp in mp . keys ( ) : NEW_LINE INDENT ans = ans + ( mp [ tmp ] ) NEW_LINE DEDENT if ( xorArr [ i ] == m ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT mp [ xorArr [ i ] ] = mp . get ( xorArr [ i ] , 0 ) + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 4 , 2 , 2 , 6 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE m = 6 NEW_LINE print ( " Number ▁ of ▁ subarrays ▁ having ▁ given ▁ XOR ▁ is " , subarrayXor ( arr , n , m ) ) NEW_LINE


import java . io . * ; class GFG { static int minToggle ( int arr [ ] , int n ) { int zero [ ] = new int [ n + 1 ] ; zero [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; ++ i ) { if ( arr [ i - 1 ] == 0 ) zero [ i ] = zero [ i - 1 ] + 1 ; else zero [ i ] = zero [ i - 1 ] ; } int ans = n ; for ( int i = 1 ; i <= n ; ++ i ) ans = Math . min ( ans , i - zero [ i ] + zero [ n ] - zero [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 , 1 , 1 , 0 } ; int n = arr . length ; System . out . println ( minToggle ( arr , n ) ) ; } }

def minToggle ( arr , n ) : NEW_LINE INDENT zero = [ 0 for i in range ( n + 1 + 1 ) ] NEW_LINE zero [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] == 0 ) : NEW_LINE INDENT zero [ i ] = zero [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT zero [ i ] = zero [ i - 1 ] NEW_LINE DEDENT DEDENT ans = n NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = min ( ans , i - zero [ i ] + zero [ n ] - zero [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 0 , 1 , 1 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minToggle ( arr , n ) ) NEW_LINE


class GFG { static float sqroot ( float s ) { int pSq = 0 ; int N = 0 ; for ( int i = ( int ) ( s ) ; i > 0 ; i -- ) { for ( int j = 1 ; j < i ; j ++ ) { if ( j * j == i ) { pSq = i ; N = j ; break ; } } if ( pSq > 0 ) break ; } float d = s - pSq ; float P = d / ( 2.0f * N ) ; float A = N + P ; float sqrt_of_s = A - ( ( P * P ) / ( 2.0f * A ) ) ; return sqrt_of_s ; } public static void main ( String [ ] args ) { float num = 9.2345f ; float sqroot_of_num = sqroot ( num ) ; System . out . print ( " Square ▁ root ▁ of ▁ " + num + " ▁ = ▁ " + Math . round ( sqroot_of_num * 100000.0 ) / 100000.0 ) ; } }

def sqroot ( s ) : NEW_LINE INDENT pSq = 0 ; NEW_LINE N = 0 ; NEW_LINE for i in range ( int ( s ) , 0 , - 1 ) : NEW_LINE INDENT for j in range ( 1 , i ) : NEW_LINE INDENT if ( j * j == i ) : NEW_LINE INDENT pSq = i ; NEW_LINE N = j ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( pSq > 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT d = s - pSq ; NEW_LINE P = d / ( 2.0 * N ) ; NEW_LINE A = N + P ; NEW_LINE sqrt_of_s = A - ( ( P * P ) / ( 2.0 * A ) ) ; NEW_LINE return sqrt_of_s ; NEW_LINE DEDENT num = 9.2345 ; NEW_LINE sqroot_of_num = sqroot ( num ) ; NEW_LINE print ( " Square ▁ root ▁ of ▁ " , num , " = " , round ( ( sqroot_of_num * 100000.0 ) / 100000.0 , 5 ) ) ; NEW_LINE


import java . util . * ; class GFG { static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = 0 ; r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } static boolean isPalindrome ( String r ) { String p = r ; p = reverse ( p ) ; return ( r . equals ( p ) ) ; } static boolean CheckStr ( String s ) { int l = s . length ( ) ; int i ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substring ( 0 , i ) ) ) { break ; } } if ( i == ( l + 1 ) ) { return false ; } i = 2 ; for ( i = 2 ; i <= l ; i ++ ) { if ( isPalindrome ( s . substring ( l - i , l ) ) ) { return true ; } } return false ; } public static void main ( String args [ ] ) { String s = " abccbarfgdbd " ; if ( CheckStr ( s ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }

def isPalindrome ( r ) : NEW_LINE INDENT p = r [ : : - 1 ] NEW_LINE return r == p NEW_LINE DEDENT def CheckStr ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE i = 0 NEW_LINE for i in range ( 2 , l + 1 ) : NEW_LINE INDENT if isPalindrome ( s [ 0 : i ] ) == True : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if i == ( l + 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , l + 1 ) : NEW_LINE INDENT if isPalindrome ( s [ l - i : l ] ) == True : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abccbarfgdbd " NEW_LINE if CheckStr ( s ) == True : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


class GFG { static long cal_minimum_time ( long X ) { long t = 0 ; long sum = 0 ; while ( sum < X ) { t ++ ; sum = sum + t ; } return t ; } public static void main ( String [ ] args ) { long n = 6 ; long ans = cal_minimum_time ( n ) ; System . out . println ( " The ▁ minimum ▁ time ▁ required ▁ is ▁ : ▁ " + ans ) ; } }

def cal_minimum_time ( X ) : NEW_LINE INDENT t = 0 NEW_LINE sum = 0 NEW_LINE while ( sum < X ) : NEW_LINE INDENT t = t + 1 NEW_LINE sum = sum + t ; NEW_LINE DEDENT return t ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 6 NEW_LINE ans = cal_minimum_time ( n ) NEW_LINE print ( " The ▁ minimum ▁ time ▁ required ▁ is ▁ : " , ans ) NEW_LINE DEDENT


class GFG { static int N = 1000000 ; static long phi [ ] = new long [ N + 5 ] ; static void computeTotient ( ) { for ( int i = 1 ; i <= N ; i ++ ) phi [ i ] = i ; for ( int p = 2 ; p <= N ; p ++ ) { if ( phi [ p ] == p ) { phi [ p ] = p - 1 ; for ( int i = 2 * p ; i <= N ; i += p ) { phi [ i ] = ( phi [ i ] / p ) * ( p - 1 ) ; } } } } static long power ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1 ) > 0 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static long calculate ( long x , long k , long mod ) { long arr [ ] = new long [ N ] ; long count = 0 ; while ( mod > 1 ) { arr [ ( int ) count ++ ] = mod ; mod = phi [ ( int ) mod ] ; } long result = 1 ; long loop = count + 1 ; arr [ ( int ) count ] = 1 ; for ( int i = ( int ) Math . min ( k , loop ) - 1 ; i >= 0 ; i -- ) result = power ( x , result , arr [ i ] ) ; return result ; } public static void main ( String args [ ] ) { computeTotient ( ) ; long x = 3 , k = 2 , m = 3 ; System . out . println ( calculate ( x , k , m ) ) ; } }

N = 1000000 NEW_LINE phi = [ 0 for i in range ( N + 5 ) ] NEW_LINE def computeTotient ( ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT phi [ i ] = i NEW_LINE DEDENT for p in range ( 2 , N + 1 ) : NEW_LINE INDENT if ( phi [ p ] == p ) : NEW_LINE INDENT phi [ p ] = p - 1 NEW_LINE for i in range ( 2 * p , N + 1 , p ) : NEW_LINE INDENT phi [ i ] = ( phi [ i ] // p ) * ( p - 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def calculate ( x , k , mod ) : NEW_LINE INDENT arr = [ 0 for i in range ( N ) ] NEW_LINE count = 0 NEW_LINE while ( mod > 1 ) : NEW_LINE INDENT arr [ count ] = mod NEW_LINE count += 1 NEW_LINE mod = phi [ mod ] NEW_LINE DEDENT result = 1 NEW_LINE loop = count + 1 NEW_LINE arr [ count ] = 1 NEW_LINE for i in range ( min ( k , loop ) , - 1 , - 1 ) : NEW_LINE INDENT result = power ( x , result , arr [ i ] ) NEW_LINE DEDENT return result NEW_LINE DEDENT computeTotient ( ) NEW_LINE x = 3 NEW_LINE k = 2 NEW_LINE m = 3 NEW_LINE print ( calculate ( x , k , m ) ) NEW_LINE


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

def nthOdd ( n ) : NEW_LINE INDENT return ( 2 * n - 1 ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 10 NEW_LINE print ( nthOdd ( n ) ) NEW_LINE DEDENT


class GFG { static class Node { int data ; Node next ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = null ; return temp ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } static void replaceNodes ( Node head ) { Node p = head ; Node x = null ; int count = 0 ; int i ; while ( p != null ) { count = 0 ; i = 0 ; x = p ; while ( x != null ) { if ( x . data > p . data ) count ++ ; x = x . next ; } p . data = count ; p = p . next ; } } public static void main ( String [ ] args ) { Node head = newNode ( 10 ) ; head . next = newNode ( 12 ) ; head . next . next = newNode ( 5 ) ; head . next . next . next = newNode ( 40 ) ; head . next . next . next . next = newNode ( 21 ) ; head . next . next . next . next . next = newNode ( 70 ) ; head . next . next . next . next . next . next = newNode ( 1 ) ; head . next . next . next . next . next . next . next = newNode ( 49 ) ; head . next . next . next . next . next . next . next . next = newNode ( 37 ) ; replaceNodes ( head ) ; printList ( head ) ; } }

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 printList ( node ) : NEW_LINE INDENT while node != None : NEW_LINE INDENT print ( node . data , end = " ▁ " ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT def replaceNodes ( head ) : NEW_LINE INDENT p = head NEW_LINE x = None NEW_LINE count = 0 NEW_LINE while p != None : NEW_LINE INDENT count = 0 NEW_LINE x = p NEW_LINE while x != None : NEW_LINE INDENT if x . data > p . data : NEW_LINE INDENT count += 1 NEW_LINE DEDENT x = x . next NEW_LINE DEDENT p . data = count NEW_LINE p = p . next NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT head = Node ( 10 ) NEW_LINE head . next = Node ( 12 ) NEW_LINE head . next . next = Node ( 5 ) NEW_LINE head . next . next . next = Node ( 40 ) NEW_LINE head . next . next . next . next = Node ( 21 ) NEW_LINE head . next . next . next . next . next = Node ( 70 ) NEW_LINE head . next . next . next . next . next . next = Node ( 1 ) NEW_LINE head . next . next . next . next . next . next . next = Node ( 49 ) NEW_LINE head . next . next . next . next . next . next . next . next = Node ( 37 ) NEW_LINE replaceNodes ( head ) NEW_LINE printList ( head ) NEW_LINE DEDENT