import java . util . HashMap ; class GFG { public static int segments ( int n , int [ ] p , int m ) { HashMap < Integer , Integer > c = new HashMap < > ( ) ; c . put ( 0 , 1 ) ; boolean has = false ; int sum = 0 ; int ans = 0 ; for ( int r = 0 ; r < n ; r ++ ) { if ( p [ r ] < m ) sum -- ; else if ( p [ r ] > m ) sum ++ ; if ( p [ r ] == m ) has = true ; if ( has ) ans += ( c . get ( sum ) == null ? 0 : c . get ( sum ) ) + ( c . get ( sum - 1 ) == null ? 0 : c . get ( sum - 1 ) ) ; else c . put ( sum , c . get ( sum ) == null ? 1 : c . get ( sum ) + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { 2 , 4 , 5 , 3 , 1 } ; int n = a . length ; int m = 4 ; System . out . println ( segments ( n , a , m ) ) ; } }

def segments ( n , p , m ) : NEW_LINE INDENT c = dict ( ) NEW_LINE c [ 0 ] = 1 NEW_LINE has = False NEW_LINE Sum = 0 NEW_LINE ans = 0 NEW_LINE for r in range ( n ) : NEW_LINE INDENT if ( p [ r ] < m ) : NEW_LINE INDENT Sum -= 1 NEW_LINE DEDENT elif ( p [ r ] > m ) : NEW_LINE INDENT Sum += 1 NEW_LINE DEDENT if ( p [ r ] == m ) : NEW_LINE INDENT has = True NEW_LINE DEDENT if ( has ) : NEW_LINE INDENT if ( Sum in c . keys ( ) ) : NEW_LINE INDENT ans += c [ Sum ] NEW_LINE DEDENT if Sum - 1 in c . keys ( ) : NEW_LINE INDENT ans += c [ Sum - 1 ] NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT c [ Sum ] = c . get ( Sum , 0 ) + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 2 , 4 , 5 , 3 , 1 ] NEW_LINE n = len ( a ) NEW_LINE m = 4 NEW_LINE print ( segments ( n , a , m ) ) NEW_LINE


import java . util . * ; class solution { static int nthTerm ( int n ) { return 2 * ( int ) Math . pow ( n , 3 ) + ( int ) Math . pow ( n , 2 ) ; } public static void main ( String arr [ ] ) { int N = 3 ; System . out . println ( nthTerm ( N ) ) ; } }

def nthTerm ( n ) : NEW_LINE INDENT return 2 * pow ( n , 3 ) + pow ( n , 2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 3 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT


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

def numOfsubarrays ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT product = arr [ i ] NEW_LINE sum = arr [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( product == sum ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT product *= arr [ j ] NEW_LINE sum += arr [ j ] NEW_LINE DEDENT if ( product == sum ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( numOfsubarrays ( arr , n ) ) NEW_LINE


class GFG { static void findNthTerm ( int N ) { int ans = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( i % 2 == 0 ) { ans = ans + 6 ; } else { ans = ans + 2 ; } } System . out . print ( ans + " \n " ) ; } public static void main ( String [ ] args ) { int N = 3 ; findNthTerm ( N ) ; } }

def findNthTerm ( N ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT ans = ans + 6 NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans + 2 NEW_LINE DEDENT DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE findNthTerm ( N ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int uniqueNumbers ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int freq [ ] = new int [ n + 2 ] ; for ( int i = 0 ; i < n + 2 ; i ++ ) freq [ i ] = 0 ; for ( int x = 0 ; x < n ; x ++ ) { if ( freq [ arr [ x ] - 1 ] == 0 ) { freq [ arr [ x ] - 1 ] ++ ; } else if ( freq [ arr [ x ] ] == 0 ) { freq [ arr [ x ] ] ++ ; } else { freq [ arr [ x ] + 1 ] ++ ; } } int unique = 0 ; for ( int x = 0 ; x <= n + 1 ; x ++ ) { if ( freq [ x ] != 0 ) { unique ++ ; } } return unique ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 3 , 3 , 3 } ; int n = 4 ; int ans = uniqueNumbers ( arr , n ) ; System . out . println ( ans ) ; } }

def uniqueNumbers ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE freq = [ 0 ] * ( n + 2 ) NEW_LINE for val in arr : NEW_LINE INDENT if ( freq [ val - 1 ] == 0 ) : NEW_LINE INDENT freq [ val - 1 ] += 1 NEW_LINE DEDENT elif ( freq [ val ] == 0 ) : NEW_LINE INDENT freq [ val ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ val + 1 ] += 1 NEW_LINE DEDENT DEDENT unique = 0 NEW_LINE for val in freq : NEW_LINE INDENT if ( val > 0 ) : NEW_LINE INDENT unique += 1 NEW_LINE DEDENT DEDENT return unique NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 3 , 3 , 3 ] NEW_LINE n = 4 NEW_LINE print ( uniqueNumbers ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void KPrimeDivisors ( int N , int K ) { int maximum_split = 0 ; while ( N % 2 == 0 ) { maximum_split ++ ; N /= 2 ; } for ( int i = 3 ; i * i <= N ; i = i + 2 ) { while ( N % i == 0 ) { N = N / i ; maximum_split ++ ; } } if ( N > 2 ) maximum_split ++ ; if ( maximum_split < K ) { System . out . println ( " No " ) ; return ; } System . out . println ( " Yes " ) ; } public static void main ( String [ ] args ) { int N = 12 ; int K = 3 ; KPrimeDivisors ( N , K ) ; } }

import math as mt NEW_LINE def KPrimeDivisors ( n , k ) : NEW_LINE INDENT maximum_split = 0 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT maximum_split += 1 NEW_LINE n = n // 2 NEW_LINE DEDENT for i in range ( 3 , mt . ceil ( mt . sqrt ( n ) ) , 2 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT n = n / i ; NEW_LINE maximum_split += 1 NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT maximum_split += 1 NEW_LINE DEDENT if maximum_split < k : NEW_LINE INDENT print ( " No " ) NEW_LINE return NEW_LINE DEDENT print ( " Yes " ) NEW_LINE DEDENT N = 12 NEW_LINE K = 3 NEW_LINE KPrimeDivisors ( N , K ) NEW_LINE


class GFG { static int countSquares ( int m , int n ) { if ( n < m ) { int temp = m ; m = n ; n = temp ; } return m * ( m + 1 ) * ( 2 * m + 1 ) / 6 + ( n - m ) * m * ( m + 1 ) / 2 ; } public static void main ( String [ ] args ) { int m = 4 , n = 3 ; System . out . println ( " Count ▁ of ▁ squares ▁ is ▁ " + countSquares ( m , n ) ) ; } }

def countSquares ( m , n ) : NEW_LINE INDENT if ( n < m ) : NEW_LINE INDENT temp = m NEW_LINE m = n NEW_LINE n = temp NEW_LINE DEDENT return ( ( m * ( m + 1 ) * ( 2 * m + 1 ) / 6 + ( n - m ) * m * ( m + 1 ) / 2 ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 4 NEW_LINE n = 3 NEW_LINE print ( " Count ▁ of ▁ squares ▁ is ▁ " , countSquares ( m , n ) ) NEW_LINE DEDENT


import java . util . * ; class GfG { static int N = 100005 ; static boolean isprime [ ] = new boolean [ N ] ; static boolean can [ ] = new boolean [ N ] ; static ArrayList < Integer > SieveOfEratosthenes ( ) { for ( int a = 0 ; a < isprime . length ; a ++ ) { isprime [ a ] = true ; } for ( int p = 2 ; p * p < N ; p ++ ) { if ( isprime [ p ] == true ) { for ( int i = p * p ; i < N ; i += p ) isprime [ i ] = false ; } } ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; for ( int i = 2 ; i < N ; i ++ ) if ( isprime [ i ] ) primes . add ( i ) ; return primes ; } static int Prime_Numbers ( int n ) { ArrayList < Integer > primes = SieveOfEratosthenes ( ) ; for ( int i = 0 ; i < ( int ) ( primes . size ( ) ) - 1 ; i ++ ) if ( primes . get ( i ) + primes . get ( i + 1 ) + 1 < N ) can [ primes . get ( i ) + primes . get ( i + 1 ) + 1 ] = true ; int ans = 0 ; for ( int i = 2 ; i <= n ; i ++ ) { if ( can [ i ] && isprime [ i ] == true ) { ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( Prime_Numbers ( n ) ) ; } }

from math import sqrt ; NEW_LINE N = 100005 ; NEW_LINE isprime = [ True ] * N ; NEW_LINE can = [ False ] * N ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for p in range ( 2 , int ( sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( isprime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * p , N , p ) : NEW_LINE INDENT isprime [ i ] = False ; NEW_LINE DEDENT DEDENT DEDENT primes = [ ] ; NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( isprime [ i ] ) : NEW_LINE INDENT primes . append ( i ) ; NEW_LINE DEDENT DEDENT return primes ; NEW_LINE DEDENT def Prime_Numbers ( n ) : NEW_LINE INDENT primes = SieveOfEratosthenes ( ) ; NEW_LINE for i in range ( len ( primes ) - 1 ) : NEW_LINE INDENT if ( primes [ i ] + primes [ i + 1 ] + 1 < N ) : NEW_LINE INDENT can [ primes [ i ] + primes [ i + 1 ] + 1 ] = True ; NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( can [ i ] and isprime [ i ] ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 50 ; NEW_LINE print ( Prime_Numbers ( n ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static void Probability ( int A , int B ) { int C = 6 - Math . max ( A , B ) ; int gcd = __gcd ( C , 6 ) ; System . out . print ( C / gcd + " / " + 6 / gcd ) ; } public static void main ( String [ ] args ) { int A = 2 , B = 4 ; Probability ( A , B ) ; } }

from math import gcd NEW_LINE def Probability ( A , B ) : NEW_LINE INDENT C = 6 - max ( A , B ) NEW_LINE __gcd = gcd ( C , 6 ) NEW_LINE print ( C // __gcd , " / " , 6 // __gcd ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A , B = 2 , 4 NEW_LINE Probability ( A , B ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int R = 4 ; static int C = 4 ; static int first ( int arr [ ] , int low , int high ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == 0 || arr [ mid - 1 ] == 0 ) && arr [ mid ] == 1 ) return mid ; else if ( arr [ mid ] == 0 ) return first ( arr , ( mid + 1 ) , high ) ; else return first ( arr , low , ( mid - 1 ) ) ; } return - 1 ; } static void rowWith0s ( int mat [ ] [ ] ) { int max_row_index = 0 , max = Integer . MIN_VALUE ; int min_row_index = 0 , min = Integer . MAX_VALUE ; int i , index ; for ( i = 0 ; i < R ; i ++ ) { index = first ( mat [ i ] , 0 , C - 1 ) ; int cntZeroes = 0 ; if ( index == - 1 ) { cntZeroes = C ; } else { cntZeroes = index ; } if ( max < cntZeroes ) { max = cntZeroes ; max_row_index = i ; } if ( min > cntZeroes ) { min = cntZeroes ; min_row_index = i ; } } System . out . println ( " Row ▁ with ▁ min ▁ 0s : ▁ " + ( min_row_index + 1 ) ) ; System . out . println ( " Row ▁ with ▁ max ▁ 0s : ▁ " + ( max_row_index + 1 ) ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 0 , 1 } , { 0 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 } , { 0 , 0 , 0 , 0 } } ; rowWith0s ( mat ) ; } }

import sys NEW_LINE R = 4 NEW_LINE C = 4 NEW_LINE def first ( arr , low , high ) : NEW_LINE INDENT if ( high >= low ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 ; NEW_LINE if ( ( mid == 0 or arr [ mid - 1 ] == 0 ) and arr [ mid ] == 1 ) : NEW_LINE INDENT return mid ; NEW_LINE DEDENT elif ( arr [ mid ] == 0 ) : NEW_LINE INDENT return first ( arr , ( mid + 1 ) , high ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return first ( arr , low , ( mid - 1 ) ) ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT def rowWith0s ( mat ) : NEW_LINE INDENT row_index = 0 ; max = - ( sys . maxsize - 1 ) ; NEW_LINE min_row_index = 0 ; min = sys . maxsize ; NEW_LINE for i in range ( R ) : NEW_LINE INDENT index = first ( mat [ i ] , 0 , C - 1 ) ; NEW_LINE cntZeroes = 0 ; NEW_LINE if ( index == - 1 ) : NEW_LINE INDENT cntZeroes = C ; NEW_LINE DEDENT else : NEW_LINE INDENT cntZeroes = index ; NEW_LINE DEDENT if ( max < cntZeroes ) : NEW_LINE INDENT max = cntZeroes ; NEW_LINE max_row_index = i ; NEW_LINE DEDENT if ( min > cntZeroes ) : NEW_LINE INDENT min = cntZeroes ; NEW_LINE min_row_index = i ; NEW_LINE DEDENT DEDENT print ( " Row ▁ with ▁ min ▁ 0s : " , min_row_index + 1 ) ; NEW_LINE print ( " Row ▁ with ▁ max ▁ 0s : " , max_row_index + 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 0 , 0 , 0 , 1 ] , [ 0 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 ] , [ 0 , 0 , 0 , 0 ] ] ; NEW_LINE rowWith0s ( mat ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static void printPattern ( int n ) { int line_no = 1 ; int curr_star = 0 ; for ( line_no = 1 ; line_no <= n ; ) { if ( curr_star < line_no ) { System . out . print ( " * ▁ " ) ; curr_star ++ ; continue ; } if ( curr_star == line_no ) { System . out . println ( " " ) ; line_no ++ ; curr_star = 0 ; } } } public static void main ( String [ ] args ) { printPattern ( 7 ) ; } }

def printPattern ( n ) : NEW_LINE INDENT line_no = 1 NEW_LINE curr_star = 0 NEW_LINE line_no = 1 NEW_LINE while ( line_no <= n ) : NEW_LINE INDENT if ( curr_star < line_no ) : NEW_LINE INDENT print ( " * ▁ " , end = " " ) NEW_LINE curr_star += 1 NEW_LINE continue NEW_LINE DEDENT if ( curr_star == line_no ) : NEW_LINE INDENT print ( " " ) NEW_LINE line_no += 1 NEW_LINE curr_star = 0 NEW_LINE DEDENT DEDENT DEDENT printPattern ( 7 ) NEW_LINE


public class find { public static void main ( String args [ ] ) { int [ ] arr = { 1 , 5 , 7 , - 1 , 5 } ; int sum = 6 ; getPairsCount ( arr , sum ) ; } public static void getPairsCount ( int [ ] arr , int sum ) { int count = 0 ; for ( int i = 0 ; i < arr . length ; i ++ ) for ( int j = i + 1 ; j < arr . length ; j ++ ) if ( ( arr [ i ] + arr [ j ] ) == sum ) count ++ ; System . out . printf ( " Count ▁ of ▁ pairs ▁ is ▁ % d " , count ) ; } }

def getPairsCount ( arr , n , sum ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if arr [ i ] + arr [ j ] == sum : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 5 , 7 , - 1 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE sum = 6 NEW_LINE print ( " Count ▁ of ▁ pairs ▁ is " , getPairsCount ( arr , n , sum ) ) NEW_LINE


class GFG { static int maxPrimes ( int n ) { return n / 2 ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . println ( maxPrimes ( n ) ) ; } }

def maxPrmimes ( n ) : NEW_LINE INDENT return n // 2 NEW_LINE DEDENT n = 17 NEW_LINE print ( maxPrmimes ( n ) ) NEW_LINE


import java . util . * ; class GFG { static boolean oppositeSigns ( int x , int y ) { return ( ( x ^ y ) < 0 ) ; } static void partitionNegPos ( int arr [ ] , int n ) { Vector < Integer > a = new Vector < Integer > ( ) ; Vector < Integer > b = new Vector < Integer > ( ) ; a . add ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { if ( oppositeSigns ( a . get ( 0 ) , arr [ i ] ) ) b . add ( arr [ i ] ) ; else a . add ( arr [ i ] ) ; } for ( int i = 0 ; i < a . size ( ) ; i ++ ) System . out . print ( a . get ( i ) + " ▁ " ) ; System . out . println ( " " ) ; for ( int i = 0 ; i < b . size ( ) ; i ++ ) System . out . print ( b . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , - 2 , 6 , - 7 , 8 } ; int n = arr . length ; partitionNegPos ( arr , n ) ; } }

def oppositeSigns ( x , y ) : NEW_LINE INDENT return ( ( x ^ y ) < 0 ) NEW_LINE DEDENT def partitionNegPos ( arr , n ) : NEW_LINE INDENT a = [ ] NEW_LINE b = [ ] NEW_LINE a = a + [ arr [ 0 ] ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( oppositeSigns ( a [ 0 ] , arr [ i ] ) ) : NEW_LINE INDENT b = b + [ arr [ i ] ] NEW_LINE DEDENT else : NEW_LINE INDENT a = a + [ arr [ i ] ] NEW_LINE DEDENT DEDENT for i in range ( 0 , len ( a ) ) : NEW_LINE INDENT print ( a [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( " " ) NEW_LINE for i in range ( 0 , len ( b ) ) : NEW_LINE INDENT print ( b [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT arr = [ 1 , - 2 , 6 , - 7 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE partitionNegPos ( arr , n ) NEW_LINE


class GFG { static int [ ] pref = new int [ 100001 ] ; static int checkArmstrong ( int x ) { int n = String . valueOf ( x ) . length ( ) ; int sum1 = 0 ; int temp = x ; while ( temp > 0 ) { int digit = temp % 10 ; sum1 += Math . pow ( digit , n ) ; temp /= 10 ; } if ( sum1 == x ) return x ; return 0 ; } static void preCompute ( ) { for ( int i = 1 ; i < 100001 ; i ++ ) { pref [ i ] = pref [ i - 1 ] + checkArmstrong ( i ) ; } } static void printSum ( int L , int R ) { System . out . println ( pref [ R ] - pref [ L - 1 ] ) ; } static void printSumarmstrong ( int [ ] [ ] arr , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } public static void main ( String [ ] args ) { int Q = 2 ; int [ ] [ ] arr = { { 1 , 13 } , { 121 , 211 } } ; printSumarmstrong ( arr , Q ) ; } }

pref = [ 0 ] * 100001 NEW_LINE def checkArmstrong ( x ) : NEW_LINE INDENT n = len ( str ( x ) ) NEW_LINE sum1 = 0 NEW_LINE temp = x NEW_LINE while temp > 0 : NEW_LINE INDENT digit = temp % 10 NEW_LINE sum1 += digit ** n NEW_LINE temp //= 10 NEW_LINE DEDENT if sum1 == x : NEW_LINE INDENT return x NEW_LINE DEDENT return 0 NEW_LINE DEDENT def preCompute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + checkArmstrong ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT print ( pref [ R ] - pref [ L - 1 ] ) NEW_LINE DEDENT def printSumarmstrong ( arr , Q ) : NEW_LINE INDENT preCompute ( ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT Q = 2 NEW_LINE arr = [ [ 1 , 13 ] , [ 121 , 211 ] ] NEW_LINE printSumarmstrong ( arr , Q ) NEW_LINE


import java . io . * ; class GFG { static void findSurfaceArea ( float a , float h ) { float Area ; Area = 6 * a * h + 3 * ( float ) ( Math . sqrt ( 3 ) ) * a * a ; System . out . println ( " Surface ▁ Area : ▁ " + Area ) ; } static void findVolume ( float a , float h ) { float Volume ; Volume = 3 * ( float ) ( Math . sqrt ( 3 ) ) * a * a * h / 2 ; System . out . println ( " Volume : ▁ " + Volume ) ; } public static void main ( String [ ] args ) { float a = 5 , h = 10 ; findSurfaceArea ( a , h ) ; findVolume ( a , h ) ; } }

import math NEW_LINE def findSurfaceArea ( a , h ) : NEW_LINE INDENT Area = 0 ; NEW_LINE Area = ( 6 * a * h + 3 * math . sqrt ( 3 ) * a * a ) ; NEW_LINE print ( " Surface ▁ Area : " , round ( Area , 3 ) ) ; NEW_LINE DEDENT def findVolume ( a , h ) : NEW_LINE INDENT Volume = 0 ; NEW_LINE Volume = ( 3 * math . sqrt ( 3 ) * a * a * h / 2 ) ; NEW_LINE print ( " Volume : " , round ( Volume , 3 ) ) ; NEW_LINE DEDENT a = 5 ; NEW_LINE h = 10 ; NEW_LINE findSurfaceArea ( a , h ) ; NEW_LINE findVolume ( a , h ) ; NEW_LINE


import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = right = null ; } } class GFG { static int maxOfRightElement ( Node root ) { int res = Integer . MIN_VALUE ; if ( root == null ) return - 1 ; if ( root . right != null ) res = root . right . data ; return Math . max ( maxOfRightElement ( root . right ) , Math . max ( res , maxOfRightElement ( root . left ) ) ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 7 ) ; root . left = new Node ( 6 ) ; root . right = new Node ( 5 ) ; root . left . left = new Node ( 4 ) ; root . left . right = new Node ( 3 ) ; root . right . left = new Node ( 2 ) ; root . right . right = new Node ( 1 ) ; System . out . println ( maxOfRightElement ( root ) ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT temp = Node ( 0 ) NEW_LINE temp . data = data NEW_LINE temp . left = temp . right = None NEW_LINE return temp NEW_LINE DEDENT def maxOfRightElement ( root ) : NEW_LINE INDENT res = - 999999 NEW_LINE if ( root == None ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( root . right != None ) : NEW_LINE INDENT res = root . right . data NEW_LINE DEDENT return max ( maxOfRightElement ( root . right ) , res , maxOfRightElement ( root . left ) ) NEW_LINE DEDENT root = newNode ( 7 ) NEW_LINE root . left = newNode ( 6 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 2 ) NEW_LINE root . right . right = newNode ( 1 ) NEW_LINE print ( maxOfRightElement ( root ) ) NEW_LINE


import java . util . Arrays ; class GFG { static long noOfTriples ( long arr [ ] , int n ) { Arrays . sort ( arr ) ; long count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == arr [ 2 ] ) count ++ ; if ( arr [ 0 ] == arr [ 2 ] ) return ( count - 2 ) * ( count - 1 ) * ( count ) / 6 ; else if ( arr [ 1 ] == arr [ 2 ] ) return ( count - 1 ) * ( count ) / 2 ; return count ; } public static void main ( String arg [ ] ) { long arr [ ] = { 1 , 3 , 3 , 4 } ; int n = arr . length ; System . out . print ( noOfTriples ( arr , n ) ) ; } }

def noOfTriples ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] == arr [ 2 ] : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if arr [ 0 ] == arr [ 2 ] : NEW_LINE INDENT return ( count - 2 ) * ( count - 1 ) * ( count ) / 6 NEW_LINE DEDENT elif arr [ 1 ] == arr [ 2 ] : NEW_LINE INDENT return ( count - 1 ) * ( count ) / 2 NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ 1 , 3 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( noOfTriples ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { public static int indexOfFirstOne ( int arr [ ] , int low , int high ) { int mid = 0 ; while ( low <= high ) { mid = ( low + high ) / 2 ; if ( arr [ mid ] == 1 && ( mid == 0 || arr [ mid - 1 ] == 0 ) ) break ; else if ( arr [ mid ] == 1 ) high = mid - 1 ; else low = mid + 1 ; } return mid ; } public static int posOfFirstOne ( int arr [ ] ) { int l = 0 , h = 1 ; while ( arr [ h ] == 0 ) { l = h ; h = 2 * h ; } return indexOfFirstOne ( arr , l , h ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 1 , 1 , 1 , 1 } ; System . out . println ( " Index ▁ = ▁ " + posOfFirstOne ( arr ) ) ; } }

def indexOfFirstOne ( arr , low , high ) : NEW_LINE INDENT while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( arr [ mid ] == 1 and ( mid == 0 or arr [ mid - 1 ] == 0 ) ) : NEW_LINE INDENT break NEW_LINE DEDENT elif ( arr [ mid ] == 1 ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return mid NEW_LINE DEDENT def posOfFirstOne ( arr ) : NEW_LINE INDENT l = 0 NEW_LINE h = 1 NEW_LINE while ( arr [ h ] == 0 ) : NEW_LINE INDENT l = h NEW_LINE h = 2 * h NEW_LINE DEDENT return indexOfFirstOne ( arr , l , h ) NEW_LINE DEDENT arr = [ 0 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE print ( " Index ▁ = ▁ " , posOfFirstOne ( arr ) ) NEW_LINE


import java . io . * ; class GFG { static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; int l = 0 , r = n - 1 ; while ( l < m && r >= 0 ) { if ( ( arr1 [ l ] + arr2 [ r ] ) == x ) { l ++ ; r -- ; count ++ ; } else if ( ( arr1 [ l ] + arr2 [ r ] ) < x ) l ++ ; else r -- ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }

def countPairs ( arr1 , arr2 , m , n , x ) : NEW_LINE INDENT count , l , r = 0 , 0 , n - 1 NEW_LINE while ( l < m and r >= 0 ) : NEW_LINE INDENT if ( ( arr1 [ l ] + arr2 [ r ] ) == x ) : NEW_LINE INDENT l += 1 NEW_LINE r -= 1 NEW_LINE count += 1 NEW_LINE DEDENT elif ( ( arr1 [ l ] + arr2 [ r ] ) < x ) : NEW_LINE INDENT l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT r -= 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 3 , 5 , 7 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 8 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE x = 10 NEW_LINE print ( " Count ▁ = " , countPairs ( arr1 , arr2 , m , n , x ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static int Parts ( int Arr [ ] , int N ) { int factor = Arr [ 1 ] - Arr [ 0 ] ; for ( int i = 2 ; i < N ; i ++ ) { factor = __gcd ( factor , Arr [ i ] - Arr [ i - 1 ] ) ; } factor = __gcd ( factor , 360 - Arr [ N - 1 ] + Arr [ 0 ] ) ; int cuts = ( 360 / factor ) - N ; return cuts ; } public static void main ( String [ ] args ) { int Arr [ ] = { 0 , 1 } ; int N = Arr . length ; System . out . println ( Parts ( Arr , N ) ) ; } }

import math NEW_LINE def Parts ( Arr , N ) : NEW_LINE INDENT factor = Arr [ 1 ] - Arr [ 0 ] NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT factor = math . gcd ( factor , Arr [ i ] - Arr [ i - 1 ] ) NEW_LINE DEDENT factor = math . gcd ( factor , 360 - Arr [ N - 1 ] + Arr [ 0 ] ) NEW_LINE cuts = ( 360 // factor ) - N NEW_LINE return cuts NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Arr = [ 0 , 1 ] NEW_LINE N = len ( Arr ) NEW_LINE print ( Parts ( Arr , N ) ) NEW_LINE DEDENT


class GFG { static boolean sumSquare ( int n ) { for ( long i = 1 ; i * i <= n ; i ++ ) for ( long j = 1 ; j * j <= n ; j ++ ) if ( i * i + j * j == n ) { System . out . println ( i + " ^ 2 ▁ + ▁ " + j + " ^ 2" ) ; return true ; } return false ; } public static void main ( String args [ ] ) { int n = 25 ; if ( sumSquare ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def sumSquare ( n ) : NEW_LINE INDENT i = 1 NEW_LINE while i * i <= n : NEW_LINE INDENT j = 1 NEW_LINE while ( j * j <= n ) : NEW_LINE INDENT if ( i * i + j * j == n ) : NEW_LINE INDENT print ( i , " ^ 2 ▁ + ▁ " , j , " ^ 2" ) NEW_LINE return True NEW_LINE DEDENT j = j + 1 NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT return False NEW_LINE DEDENT n = 25 NEW_LINE if ( sumSquare ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static final int limit = 10000000 ; static int [ ] position = new int [ limit + 1 ] ; static void sieve ( ) { position [ 0 ] = - 1 ; position [ 1 ] = - 1 ; int pos = 0 ; for ( int i = 2 ; i <= limit ; i ++ ) { if ( position [ i ] == 0 ) { position [ i ] = ++ pos ; for ( int j = i * 2 ; j <= limit ; j += i ) position [ j ] = - 1 ; } } } public static void main ( String [ ] args ) { sieve ( ) ; int n = 11 ; System . out . print ( position [ n ] ) ; } }

limit = 1000000 NEW_LINE position = [ 0 ] * ( limit + 1 ) NEW_LINE def sieve ( ) : NEW_LINE INDENT position [ 0 ] = - 1 NEW_LINE position [ 1 ] = - 1 NEW_LINE pos = 0 NEW_LINE for i in range ( 2 , limit + 1 ) : NEW_LINE INDENT if ( position [ i ] == 0 ) : NEW_LINE INDENT pos += 1 NEW_LINE position [ i ] = pos NEW_LINE for j in range ( i * 2 , limit + 1 , i ) : NEW_LINE INDENT position [ j ] = - 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sieve ( ) NEW_LINE n = 11 NEW_LINE print ( position [ n ] ) NEW_LINE DEDENT


class PellNumber { public static int pell ( int n ) { if ( n <= 2 ) return n ; return 2 * pell ( n - 1 ) + pell ( n - 2 ) ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( pell ( n ) ) ; } }

def pell ( n ) : NEW_LINE INDENT if ( n <= 2 ) : NEW_LINE INDENT return n NEW_LINE DEDENT return ( 2 * pell ( n - 1 ) + pell ( n - 2 ) ) NEW_LINE DEDENT n = 4 ; NEW_LINE print ( pell ( n ) ) NEW_LINE


import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == b ) return a ; if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( ( ~ a & 1 ) == 1 ) { if ( ( b & 1 ) == 1 ) return gcd ( a >> 1 , b ) ; else return gcd ( a >> 1 , b >> 1 ) << 1 ; } if ( ( ~ b & 1 ) == 1 ) return gcd ( a , b >> 1 ) ; if ( a > b ) return gcd ( ( a - b ) >> 1 , b ) ; return gcd ( ( b - a ) >> 1 , a ) ; } public static void main ( String args [ ] ) { int a = 34 , b = 17 ; System . out . println ( " Gcd ▁ of ▁ given " + " numbers ▁ is ▁ " + gcd ( a , b ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == b ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( ( ~ a & 1 ) == 1 ) : NEW_LINE INDENT if ( ( b & 1 ) == 1 ) : NEW_LINE INDENT return gcd ( a >> 1 , b ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( gcd ( a >> 1 , b >> 1 ) << 1 ) NEW_LINE DEDENT DEDENT if ( ( ~ b & 1 ) == 1 ) : NEW_LINE INDENT return gcd ( a , b >> 1 ) NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return gcd ( ( a - b ) >> 1 , b ) NEW_LINE DEDENT return gcd ( ( b - a ) >> 1 , a ) NEW_LINE DEDENT a , b = 34 , 17 NEW_LINE print ( " Gcd ▁ of ▁ given ▁ numbers ▁ is ▁ " , gcd ( a , b ) ) NEW_LINE


import java . util . * ; class GFG { static void printSmall ( int arr [ ] , int asize , int n ) { int [ ] copy_arr = Arrays . copyOf ( arr , asize ) ; Arrays . sort ( copy_arr ) ; for ( int i = 0 ; i < asize ; ++ i ) { if ( Arrays . binarySearch ( copy_arr , 0 , n , arr [ i ] ) > - 1 ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 } ; int asize = arr . length ; int n = 5 ; printSmall ( arr , asize , n ) ; } }

def binary_search ( arr , low , high , ele ) : NEW_LINE INDENT while low < high : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if arr [ mid ] == ele : NEW_LINE INDENT return mid NEW_LINE DEDENT elif arr [ mid ] > ele : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def printSmall ( arr , asize , n ) : NEW_LINE INDENT copy_arr = arr . copy ( ) NEW_LINE copy_arr . sort ( ) NEW_LINE for i in range ( asize ) : NEW_LINE INDENT if binary_search ( copy_arr , low = 0 , high = n , ele = arr [ i ] ) > - 1 : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 5 , 8 , 9 , 6 , 7 , 3 , 4 , 2 , 0 ] NEW_LINE asize = len ( arr ) NEW_LINE n = 5 NEW_LINE printSmall ( arr , asize , n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static long fifthPowerSum ( int n ) { long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + ( i * i * i * i * i ) ; return sum ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( fifthPowerSum ( n ) ) ; } }

def fifthPowerSum ( n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sm = sm + ( i * i * i * i * i ) NEW_LINE DEDENT return sm NEW_LINE DEDENT n = 6 NEW_LINE print ( fifthPowerSum ( n ) ) NEW_LINE


import java . util . * ; public class GFG { final static int MAXN = 1000001 ; static int spf [ ] = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static int sumFactors ( int arr [ ] , int n ) { sieve ( ) ; Map map = new HashMap ( ) ; for ( int i = 0 ; i < MAXN ; ++ i ) map . put ( i , 0 ) ; for ( int i = 0 ; i < n ; ++ i ) map . put ( arr [ i ] , 1 ) ; int sum = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int num = arr [ i ] ; while ( num != 1 && ( int ) ( map . get ( spf [ num ] ) ) == 1 ) { num /= spf [ num ] ; } if ( num == 1 ) sum += arr [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 11 , 55 , 25 , 100 } ; int n = arr . length ; System . out . println ( sumFactors ( arr , n ) ) ; } }

from collections import defaultdict NEW_LINE MAXN = 1000001 NEW_LINE MAXN_sqrt = int ( MAXN ** ( 0.5 ) ) NEW_LINE spf = [ None ] * ( MAXN ) NEW_LINE def sieve ( ) : NEW_LINE INDENT spf [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAXN ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAXN , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT for i in range ( 3 , MAXN_sqrt ) : NEW_LINE INDENT if spf [ i ] == i : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE INDENT if spf [ j ] == j : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def sumFactors ( arr , n ) : NEW_LINE INDENT sieve ( ) NEW_LINE Map = defaultdict ( lambda : 0 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT Map [ arr [ i ] ] = 1 NEW_LINE DEDENT Sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT num = arr [ i ] NEW_LINE while num != 1 and Map [ spf [ num ] ] == 1 : NEW_LINE INDENT num = num // spf [ num ] NEW_LINE DEDENT if num == 1 : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE DEDENT DEDENT return Sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 11 , 55 , 25 , 100 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumFactors ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void findAndPrintOriginalArray ( int arr [ ] , int n ) { int arr_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) arr_sum += arr [ i ] ; arr_sum = arr_sum / ( n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr_sum - arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 14 , 12 , 13 , 11 } ; int n = arr . length ; findAndPrintOriginalArray ( arr , n ) ; } }

def findAndPrintOriginalArray ( arr , n ) : NEW_LINE INDENT arr_sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT arr_sum += arr [ i ] NEW_LINE DEDENT arr_sum = int ( arr_sum / ( n - 1 ) ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( ( arr_sum - arr [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 10 , 14 , 12 , 13 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE findAndPrintOriginalArray ( arr , n ) NEW_LINE


import java . util . Arrays ; class GFG { static int dp [ ] [ ] = new int [ 901 ] [ 8101 ] ; static int minimumNumberOfDigits ( int a , int b ) { if ( a > b || a < 0 || b < 0 || a > 900 || b > 8100 ) { return - 1 ; } if ( a == 0 && b == 0 ) { return 0 ; } if ( dp [ a ] [ b ] != - 1 ) { return dp [ a ] [ b ] ; } int ans = 101 ; for ( int i = 9 ; i >= 1 ; i -- ) { int k = minimumNumberOfDigits ( a - i , b - ( i * i ) ) ; if ( k != - 1 ) { ans = Math . min ( ans , k + 1 ) ; } } return dp [ a ] [ b ] = ans ; } static void printSmallestNumber ( int a , int b ) { for ( int [ ] row : dp ) { Arrays . fill ( row , - 1 ) ; } dp [ 0 ] [ 0 ] = 0 ; int k = minimumNumberOfDigits ( a , b ) ; if ( k == - 1 || k > 100 ) { System . out . println ( " - 1" ) ; } else { while ( a > 0 && b > 0 ) { for ( int i = 1 ; i <= 9 ; i ++ ) { if ( a >= i && b >= i * i && 1 + dp [ a - i ] [ b - i * i ] == dp [ a ] [ b ] ) { System . out . print ( i ) ; a -= i ; b -= i * i ; break ; } } } } } public static void main ( String args [ ] ) { int a = 18 , b = 162 ; printSmallestNumber ( a , b ) ; } }

dp = [ [ - 1 for i in range ( 8101 ) ] for i in range ( 901 ) ] NEW_LINE def minimumNumberOfDigits ( a , b ) : NEW_LINE INDENT if ( a > b or a < 0 or b < 0 or a > 900 or b > 8100 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT if ( a == 0 and b == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ a ] [ b ] != - 1 ) : NEW_LINE INDENT return dp [ a ] [ b ] NEW_LINE DEDENT ans = 101 NEW_LINE for i in range ( 9 , 0 , - 1 ) : NEW_LINE INDENT k = minimumNumberOfDigits ( a - i , b - ( i * i ) ) NEW_LINE if ( k != - 1 ) : NEW_LINE INDENT ans = min ( ans , k + 1 ) NEW_LINE DEDENT DEDENT dp [ a ] [ b ] = ans NEW_LINE return ans NEW_LINE DEDENT def printSmallestNumber ( a , b ) : NEW_LINE INDENT for i in range ( 901 ) : NEW_LINE INDENT for j in range ( 8101 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT dp [ 0 ] [ 0 ] = 0 NEW_LINE k = minimumNumberOfDigits ( a , b ) NEW_LINE if ( k == - 1 or k > 100 ) : NEW_LINE INDENT print ( - 1 , end = ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT while ( a > 0 and b > 0 ) : NEW_LINE INDENT for i in range ( 1 , 10 ) : NEW_LINE INDENT if ( a >= i and b >= i * i and 1 + dp [ a - i ] [ b - i * i ] == dp [ a ] [ b ] ) : NEW_LINE INDENT print ( i , end = ' ' ) NEW_LINE a -= i NEW_LINE b -= i * i NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 18 NEW_LINE b = 162 NEW_LINE printSmallestNumber ( a , b ) NEW_LINE DEDENT


import java . lang . * ; class GFG { static double PI = 3.14159265 ; public static double area_inscribed ( double P , double B , double H ) { return ( ( P + B - H ) * ( P + B - H ) * ( PI / 4 ) ) ; } public static void main ( String [ ] args ) { double P = 3 , B = 4 , H = 5 ; System . out . println ( area_inscribed ( P , B , H ) ) ; } }

PI = 3.14159265 NEW_LINE def area_inscribed ( P , B , H ) : NEW_LINE INDENT return ( ( P + B - H ) * ( P + B - H ) * ( PI / 4 ) ) NEW_LINE DEDENT P = 3 NEW_LINE B = 4 NEW_LINE H = 5 NEW_LINE print ( area_inscribed ( P , B , H ) ) NEW_LINE


import java . lang . * ; import java . util . * ; class GFG { public static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } public static int primorial ( int n ) { int p = 2 ; n -- ; for ( int i = 3 ; n != 0 ; i ++ ) { if ( isPrime ( i ) == true ) { p = p * i ; n -- ; } i ++ ; } return p ; } public static int findNextPrime ( int n ) { int nextPrime = n + 2 ; while ( true ) { if ( isPrime ( nextPrime ) == true ) break ; nextPrime ++ ; } return nextPrime ; } public static int fortunateNumber ( int n ) { int p = primorial ( n ) ; return findNextPrime ( p ) - p ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( fortunateNumber ( n ) ) ; } }

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : return False NEW_LINE if ( n <= 3 ) : return True NEW_LINE if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i += 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def primorial ( n ) : NEW_LINE INDENT p = 2 ; n -= 1 ; i = 3 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT p = p * i NEW_LINE n -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return p NEW_LINE DEDENT def findNextPrime ( n ) : NEW_LINE INDENT nextPrime = n + 2 NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPrime ( nextPrime ) ) : NEW_LINE INDENT break NEW_LINE DEDENT nextPrime += 1 NEW_LINE DEDENT return nextPrime NEW_LINE DEDENT def fortunateNumber ( n ) : NEW_LINE INDENT p = primorial ( n ) NEW_LINE return findNextPrime ( p ) - p NEW_LINE DEDENT n = 5 NEW_LINE print ( fortunateNumber ( n ) ) NEW_LINE


import java . util . Arrays ; class GFG { static int upper_bound ( int [ ] array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { final int mid = ( low + high ) / 2 ; if ( value >= array [ mid ] ) { low = mid + 1 ; } else { high = mid ; } } return low ; } static int lower_bound ( int [ ] array , int length , int value ) { int low = 0 ; int high = length ; while ( low < high ) { final int mid = ( low + high ) / 2 ; if ( value <= array [ mid ] ) { high = mid ; } else { low = mid + 1 ; } } return low ; } static int findSextuplets ( int [ ] arr , int n ) { int index = 0 ; int [ ] RHS = new int [ n * n * n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] != 0 ) { for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { RHS [ index ++ ] = arr [ i ] * ( arr [ j ] + arr [ k ] ) ; } } } } Arrays . sort ( RHS ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { for ( int k = 0 ; k < n ; k ++ ) { int val = arr [ i ] * arr [ j ] + arr [ k ] ; result += ( upper_bound ( RHS , index , val ) - lower_bound ( RHS , index , val ) ) ; } } } return result ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 } ; int n = arr . length ; System . out . println ( findSextuplets ( arr , n ) ) ; } }

def upper_bound ( array , length , value ) : NEW_LINE INDENT low = 0 ; NEW_LINE high = length ; NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = int ( ( low + high ) / 2 ) ; NEW_LINE if ( value >= array [ mid ] ) : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT high = mid ; NEW_LINE DEDENT DEDENT return low ; NEW_LINE DEDENT def lower_bound ( array , length , value ) : NEW_LINE INDENT low = 0 ; NEW_LINE high = length ; NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = int ( ( low + high ) / 2 ) ; NEW_LINE if ( value <= array [ mid ] ) : NEW_LINE INDENT high = mid ; NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT DEDENT return low ; NEW_LINE DEDENT def findSextuplets ( arr , n ) : NEW_LINE INDENT index = 0 ; NEW_LINE RHS = [ 0 ] * ( n * n * n + 1 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] != 0 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT for k in range ( n ) : NEW_LINE INDENT RHS [ index ] = arr [ i ] * ( arr [ j ] + arr [ k ] ) ; NEW_LINE index += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT RHS . sort ( ) ; NEW_LINE result = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT for k in range ( n ) : NEW_LINE INDENT val = arr [ i ] * arr [ j ] + arr [ k ] ; NEW_LINE result += ( upper_bound ( RHS , index , val ) - lower_bound ( RHS , index , val ) ) ; NEW_LINE DEDENT DEDENT DEDENT return result ; NEW_LINE DEDENT arr = [ 2 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findSextuplets ( arr , n ) ) ; NEW_LINE


public class MaximumAbsoluteDifference { private static int maxDistance ( int [ ] array ) { int max1 = Integer . MIN_VALUE ; int min1 = Integer . MAX_VALUE ; int max2 = Integer . MIN_VALUE ; int min2 = Integer . MAX_VALUE ; for ( int i = 0 ; i < array . length ; i ++ ) { max1 = Math . max ( max1 , array [ i ] + i ) ; min1 = Math . min ( min1 , array [ i ] + i ) ; max2 = Math . max ( max2 , array [ i ] - i ) ; min2 = Math . min ( min2 , array [ i ] - i ) ; } return Math . max ( max1 - min1 , max2 - min2 ) ; } public static void main ( String [ ] args ) { int [ ] array = { - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 } ; System . out . println ( maxDistance ( array ) ) ; } }

def maxDistance ( array ) : NEW_LINE INDENT max1 = - 2147483648 NEW_LINE min1 = + 2147483647 NEW_LINE max2 = - 2147483648 NEW_LINE min2 = + 2147483647 NEW_LINE for i in range ( len ( array ) ) : NEW_LINE INDENT max1 = max ( max1 , array [ i ] + i ) NEW_LINE min1 = min ( min1 , array [ i ] + i ) NEW_LINE max2 = max ( max2 , array [ i ] - i ) NEW_LINE min2 = min ( min2 , array [ i ] - i ) NEW_LINE DEDENT return max ( max1 - min1 , max2 - min2 ) NEW_LINE DEDENT array = [ - 70 , - 64 , - 6 , - 56 , 64 , 61 , - 57 , 16 , 48 , - 98 ] NEW_LINE print ( maxDistance ( array ) ) NEW_LINE


import java . util . Arrays ; class GFG { static int [ ] height = new int [ ] { 1 , 2 , 1 , 2 } ; public static int woodCollected ( int n , int m ) { int sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( height [ i ] - m <= 0 ) break ; sum += ( height [ i ] - m ) ; } return sum ; } public static int collectKWood ( int n , int k ) { Arrays . sort ( height ) ; int low = 0 , high = height [ n - 1 ] ; while ( low <= high ) { int mid = low + ( ( high - low ) / 2 ) ; int collected = woodCollected ( n , mid ) ; if ( collected == k ) return mid ; if ( collected > k ) low = mid + 1 ; else high = mid - 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int k = 2 ; int n = height . length ; System . out . print ( collectKWood ( n , k ) ) ; } }

def woodCollected ( height , n , m ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( height [ i ] - m <= 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT sum += ( height [ i ] - m ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def collectKWood ( height , n , k ) : NEW_LINE INDENT height = sorted ( height ) NEW_LINE low = 0 NEW_LINE high = height [ n - 1 ] NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = low + ( ( high - low ) // 2 ) NEW_LINE collected = woodCollected ( height , n , mid ) NEW_LINE if ( collected == k ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( collected > k ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT height = [ 1 , 2 , 1 , 2 ] NEW_LINE n = len ( height ) NEW_LINE k = 2 NEW_LINE print ( collectKWood ( height , n , k ) ) NEW_LINE


import java . io . * ; class GFG { static float Time ( float arr [ ] , int n , float Emptypipe ) { float fill = 0 ; for ( int i = 0 ; i < n ; i ++ ) fill += 1 / arr [ i ] ; fill = fill - ( 1 / ( float ) Emptypipe ) ; return 1 / fill ; } public static void main ( String [ ] args ) { float arr [ ] = { 12 , 14 } ; float Emptypipe = 30 ; int n = arr . length ; System . out . println ( ( int ) ( Time ( arr , n , Emptypipe ) ) + " ▁ Hours " ) ; } }

def Time ( arr , n , Emptypipe ) : NEW_LINE INDENT fill = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT fill += ( 1 / arr [ i ] ) NEW_LINE DEDENT fill = fill - ( 1 / float ( Emptypipe ) ) NEW_LINE return int ( 1 / fill ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 12 , 14 ] NEW_LINE Emptypipe = 30 NEW_LINE n = len ( arr ) NEW_LINE print ( ( Time ( arr , n , Emptypipe ) ) , " Hours " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; int ans = 1 ; while ( n % 2 == 0 ) { n /= 2 ; ans *= 2 ; } return ans ; } public static void main ( String [ ] args ) { int n = 36 ; int res = makeOdd ( n ) ; System . out . println ( res ) ; } }

def makeOdd ( n ) : NEW_LINE INDENT if ( n % 2 != 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT resul = 1 ; NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n = n / 2 ; NEW_LINE resul = resul * 2 ; NEW_LINE DEDENT return resul ; NEW_LINE DEDENT n = 36 ; NEW_LINE print ( makeOdd ( n ) ) ; NEW_LINE


class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static pair countSum ( int arr [ ] , int n ) { int result = 0 ; int count_odd , count_even ; count_odd = 0 ; count_even = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( arr [ i - 1 ] % 2 == 0 ) { count_even = count_even + count_even + 1 ; count_odd = count_odd + count_odd ; } else { int temp = count_even ; count_even = count_even + count_odd ; count_odd = count_odd + temp + 1 ; } } return new pair ( count_even , count_odd ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 } ; int n = arr . length ; pair ans = countSum ( arr , n ) ; System . out . print ( " EvenSum ▁ = ▁ " + ans . first ) ; System . out . print ( " ▁ OddSum ▁ = ▁ " + ans . second ) ; } }

def countSum ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE count_odd = 0 NEW_LINE count_even = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( arr [ i - 1 ] % 2 == 0 ) : NEW_LINE INDENT count_even = count_even + count_even + 1 NEW_LINE count_odd = count_odd + count_odd NEW_LINE DEDENT else : NEW_LINE INDENT temp = count_even NEW_LINE count_even = count_even + count_odd NEW_LINE count_odd = count_odd + temp + 1 NEW_LINE DEDENT DEDENT return ( count_even , count_odd ) NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 3 ] ; NEW_LINE n = len ( arr ) NEW_LINE count_even , count_odd = countSum ( arr , n ) ; NEW_LINE print ( " EvenSum ▁ = ▁ " , count_even , " ▁ OddSum ▁ = ▁ " , count_odd ) NEW_LINE


class GFG { static class Node { int data ; Node left , right ; boolean visited ; } static void postorder ( Node head ) { Node temp = head ; while ( temp != null && temp . visited == false ) { if ( temp . left != null && temp . left . visited == false ) temp = temp . left ; else if ( temp . right != null && temp . right . visited == false ) temp = temp . right ; else { System . out . printf ( " % d ▁ " , temp . data ) ; temp . visited = true ; temp = head ; } } } static Node newNode ( int data ) { Node node = new Node ( ) ; node . data = data ; node . left = null ; node . right = null ; node . visited = false ; return ( node ) ; } public static void main ( String [ ] args ) { Node root = newNode ( 8 ) ; root . left = newNode ( 3 ) ; root . right = newNode ( 10 ) ; root . left . left = newNode ( 1 ) ; root . left . right = newNode ( 6 ) ; root . left . right . left = newNode ( 4 ) ; root . left . right . right = newNode ( 7 ) ; root . right . right = newNode ( 14 ) ; root . right . right . left = newNode ( 13 ) ; postorder ( root ) ; } }

class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE self . visited = False NEW_LINE DEDENT DEDENT def postorder ( head ) : NEW_LINE INDENT temp = head NEW_LINE while ( temp and temp . visited == False ) : NEW_LINE INDENT if ( temp . left and temp . left . visited == False ) : NEW_LINE INDENT temp = temp . left NEW_LINE DEDENT elif ( temp . right and temp . right . visited == False ) : NEW_LINE INDENT temp = temp . right NEW_LINE DEDENT else : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp . visited = True NEW_LINE temp = head NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 8 ) NEW_LINE root . left = newNode ( 3 ) NEW_LINE root . right = newNode ( 10 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 6 ) NEW_LINE root . left . right . left = newNode ( 4 ) NEW_LINE root . left . right . right = newNode ( 7 ) NEW_LINE root . right . right = newNode ( 14 ) NEW_LINE root . right . right . left = newNode ( 13 ) NEW_LINE postorder ( root ) NEW_LINE DEDENT


public class GFG { static long findNumUtil ( long res , int a , int aCount , int b , int bCount , int n ) { if ( res > 1e11 ) return ( long ) 1e11 ; if ( aCount == bCount && res >= n ) return res ; return Math . min ( findNumUtil ( res * 10 + a , a , aCount + 1 , b , bCount , n ) , findNumUtil ( res * 10 + b , a , aCount , b , bCount + 1 , n ) ) ; } static int findNum ( int n , int a , int b ) { int result = 0 ; int aCount = 0 ; int bCount = 0 ; return ( int ) findNumUtil ( result , a , aCount , b , bCount , n ) ; } public static void main ( String args [ ] ) { int N = 4500 ; int A = 4 ; int B = 7 ; System . out . println ( findNum ( N , A , B ) ) ; } }

def findNumUtil ( res , a , aCount , b , bCount , n ) : NEW_LINE INDENT if ( res > 1e11 ) : NEW_LINE INDENT return 1e11 NEW_LINE DEDENT if ( aCount == bCount and res >= n ) : NEW_LINE INDENT return res NEW_LINE DEDENT return min ( findNumUtil ( res * 10 + a , a , aCount + 1 , b , bCount , n ) , findNumUtil ( res * 10 + b , a , aCount , b , bCount + 1 , n ) ) NEW_LINE DEDENT def findNum ( n , a , b ) : NEW_LINE INDENT result = 0 NEW_LINE aCount = 0 NEW_LINE bCount = 0 NEW_LINE return findNumUtil ( result , a , aCount , b , bCount , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4500 NEW_LINE A = 4 NEW_LINE B = 7 NEW_LINE print ( findNum ( N , A , B ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static float squareRoot ( int number , int precision ) { int start = 0 , end = number ; int mid ; double ans = 0.0 ; while ( start <= end ) { mid = ( start + end ) / 2 ; if ( mid * mid == number ) { ans = mid ; break ; } if ( mid * mid < number ) { start = mid + 1 ; ans = mid ; } else { end = mid - 1 ; } } double increment = 0.1 ; for ( int i = 0 ; i < precision ; i ++ ) { while ( ans * ans <= number ) { ans += increment ; } ans = ans - increment ; increment = increment / 10 ; } return ( float ) ans ; } public static void main ( String [ ] args ) { System . out . println ( squareRoot ( 50 , 3 ) ) ; System . out . println ( squareRoot ( 10 , 4 ) ) ; } }

def squareRoot ( number , precision ) : NEW_LINE INDENT start = 0 NEW_LINE end , ans = number , 1 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = int ( ( start + end ) / 2 ) NEW_LINE if ( mid * mid == number ) : NEW_LINE INDENT ans = mid NEW_LINE break NEW_LINE DEDENT if ( mid * mid < number ) : NEW_LINE INDENT start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT increment = 0.1 NEW_LINE for i in range ( 0 , precision ) : NEW_LINE INDENT while ( ans * ans <= number ) : NEW_LINE INDENT ans += increment NEW_LINE DEDENT ans = ans - increment NEW_LINE increment = increment / 10 NEW_LINE DEDENT return ans NEW_LINE DEDENT print ( round ( squareRoot ( 50 , 3 ) , 4 ) ) NEW_LINE print ( round ( squareRoot ( 10 , 4 ) , 4 ) ) NEW_LINE


class GFG { static int minJumps ( int arr [ ] , int n ) { int [ ] jumps = new int [ n ] ; int min ; jumps [ n - 1 ] = 0 ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( arr [ i ] == 0 ) jumps [ i ] = Integer . MAX_VALUE ; else if ( arr [ i ] >= n - i - 1 ) jumps [ i ] = 1 ; else { min = Integer . MAX_VALUE ; for ( int j = i + 1 ; j < n && j <= arr [ i ] + i ; j ++ ) { if ( min > jumps [ j ] ) min = jumps [ j ] ; } if ( min != Integer . MAX_VALUE ) jumps [ i ] = min + 1 ; else jumps [ i ] = min ; } } return jumps [ 0 ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 3 , 6 , 1 , 0 , 9 } ; int size = arr . length ; System . out . println ( " Minimum ▁ number ▁ of " + " ▁ jumps ▁ to ▁ reach ▁ end ▁ is ▁ " + minJumps ( arr , size ) ) ; } }

def minJumps ( arr , n ) : NEW_LINE INDENT jumps = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] == 0 ) : NEW_LINE INDENT jumps [ i ] = float ( ' inf ' ) NEW_LINE DEDENT elif ( arr [ i ] >= n - i - 1 ) : NEW_LINE INDENT jumps [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT min = float ( ' inf ' ) NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( j <= arr [ i ] + i ) : NEW_LINE INDENT if ( min > jumps [ j ] ) : NEW_LINE INDENT min = jumps [ j ] NEW_LINE DEDENT DEDENT DEDENT if ( min != float ( ' inf ' ) ) : NEW_LINE INDENT jumps [ i ] = min + 1 NEW_LINE DEDENT else : NEW_LINE INDENT jumps [ i ] = min NEW_LINE DEDENT DEDENT DEDENT return jumps [ 0 ] NEW_LINE DEDENT arr = [ 1 , 3 , 6 , 3 , 2 , 3 , 6 , 8 , 9 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( ' Minimum ▁ number ▁ of ▁ jumps ▁ to ▁ reach ' , ' end ▁ is ' , minJumps ( arr , n - 1 ) ) NEW_LINE


import java . io . * ; class GFG { static String findNumber ( int n , int d ) { String ans = " " ; if ( d != 10 ) { ans += Integer . toString ( d ) ; for ( int i = 1 ; i < n ; i ++ ) ans += '0' ; } else { if ( n == 1 ) ans += " Impossible " ; else { ans += '1' ; for ( int i = 1 ; i < n ; i ++ ) ans += '0' ; } } return ans ; } public static void main ( String [ ] args ) { int n = 12 , d = 3 ; System . out . println ( findNumber ( n , d ) ) ; } }

def findNumber ( n , d ) : NEW_LINE INDENT ans = " " NEW_LINE if ( d != 10 ) : NEW_LINE INDENT ans += str ( d ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT ans += " Impossible " NEW_LINE DEDENT else : NEW_LINE INDENT ans += '1' NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ans += '0' NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE d = 3 NEW_LINE print ( findNumber ( n , d ) ) NEW_LINE DEDENT


class GFG { static int MAX = 26 ; static void updateFreq ( String str , int freq [ ] ) { int len = str . length ( ) ; for ( int i = 0 ; i < len ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } } static int maxCount ( String str , String patt ) { int [ ] strFreq = new int [ MAX ] ; updateFreq ( str , strFreq ) ; int [ ] pattFreq = new int [ MAX ] ; updateFreq ( patt , pattFreq ) ; int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( pattFreq [ i ] == 0 ) continue ; ans = Math . min ( ans , strFreq [ i ] / pattFreq [ i ] ) ; } return ans ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; String patt = " geeks " ; System . out . print ( maxCount ( str , patt ) ) ; } }

MAX = 26 NEW_LINE def updateFreq ( strr , freq ) : NEW_LINE INDENT lenn = len ( strr ) NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT freq [ ord ( strr [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT DEDENT def maxCount ( strr , patt ) : NEW_LINE INDENT strrFreq = [ 0 for i in range ( MAX ) ] NEW_LINE updateFreq ( strr , strrFreq ) NEW_LINE pattFreq = [ 0 for i in range ( MAX ) ] NEW_LINE updateFreq ( patt , pattFreq ) NEW_LINE ans = 10 ** 9 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT if ( pattFreq [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT ans = min ( ans , strrFreq [ i ] // pattFreq [ i ] ) NEW_LINE DEDENT return ans NEW_LINE DEDENT strr = " geeksforgeeks " NEW_LINE patt = " geeks " NEW_LINE print ( maxCount ( strr , patt ) ) NEW_LINE


import java . lang . * ; import java . util . * ; class GFG { static int findPermutation ( int n ) { int len = ( int ) Math . ceil ( Math . log10 ( n ) ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( n % 2 != 0 ) { return n ; } else { n = ( n / 10 ) + ( n % 10 ) * ( int ) Math . pow ( 10 , len - i - 1 ) ; continue ; } } return - 1 ; } public static void main ( String args [ ] ) { int n = 132 ; System . out . println ( findPermutation ( n ) ) ; } }

from math import log10 , ceil , pow NEW_LINE def findPermutation ( n ) : NEW_LINE INDENT len = ceil ( log10 ( n ) ) NEW_LINE for i in range ( 0 , len , 1 ) : NEW_LINE INDENT if n % 2 != 0 : NEW_LINE INDENT return n NEW_LINE DEDENT else : NEW_LINE INDENT n = ( ( n / 10 ) + ( n % 10 ) * pow ( 10 , len - i - 1 ) ) NEW_LINE continue NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 132 NEW_LINE print ( int ( findPermutation ( n ) ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int find ( int a [ ] , int b [ ] , int k , int n1 , int n2 ) { LinkedHashSet < Integer > s = new LinkedHashSet < > ( ) ; for ( int i = 0 ; i < n2 ; i ++ ) s . add ( b [ i ] ) ; int missing = 0 ; for ( int i = 0 ; i < n1 ; i ++ ) { if ( ! s . contains ( a [ i ] ) ) missing ++ ; if ( missing == k ) return a [ i ] ; } return - 1 ; } public static void main ( String [ ] args ) { int a [ ] = { 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 15 } ; int b [ ] = { 4 , 10 , 6 , 8 , 12 } ; int n1 = a . length ; int n2 = b . length ; int k = 3 ; System . out . println ( find ( a , b , k , n1 , n2 ) ) ; } }

def find ( a , b , k , n1 , n2 ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( n2 ) : NEW_LINE INDENT s . add ( b [ i ] ) NEW_LINE DEDENT missing = 0 NEW_LINE for i in range ( n1 ) : NEW_LINE INDENT if a [ i ] not in s : NEW_LINE INDENT missing += 1 NEW_LINE DEDENT if missing == k : NEW_LINE INDENT return a [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT a = [ 0 , 2 , 4 , 6 , 8 , 10 , 12 , 14 , 15 ] NEW_LINE b = [ 4 , 10 , 6 , 8 , 12 ] NEW_LINE n1 = len ( a ) NEW_LINE n2 = len ( b ) NEW_LINE k = 3 NEW_LINE print ( find ( a , b , k , n1 , n2 ) ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; public class Stringfun { String str = " Geeks ▁ for ▁ Geeks . " ; void words ( ) { int wCount = 0 , uCount = 0 , vCount = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char c = str . charAt ( i ) ; switch ( c ) { case ' ▁ ' : case ' . ' : wCount ++ ; } switch ( c ) { case ' A ' : case ' E ' : case ' I ' : case ' O ' : case ' U ' : case ' a ' : case ' e ' : case ' i ' : case ' o ' : case ' u ' : vCount ++ ; } if ( c >= 65 && c <= 90 ) { uCount ++ ; } } System . out . println ( " Number ▁ of ▁ words ▁ = ▁ " + wCount ) ; System . out . println ( " Number ▁ of ▁ vowels ▁ = ▁ " + vCount ) ; System . out . println ( " Number ▁ of ▁ upper ▁ case ▁ characters ▁ = ▁ " + uCount ) ; } void frequency ( ) { TreeMap < Character , Integer > hmap = new TreeMap < Character , Integer > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { Integer c = hmap . get ( str . charAt ( i ) ) ; if ( hmap . get ( str . charAt ( i ) ) == null ) hmap . put ( str . charAt ( i ) , 1 ) ; else hmap . put ( str . charAt ( i ) , ++ c ) ; } for ( Map . Entry m : hmap . entrySet ( ) ) System . out . println ( " Character ▁ = ▁ " + m . getKey ( ) + " ▁ Frequency ▁ = ▁ " + m . getValue ( ) ) ; } public static void main ( String args [ ] ) throws IOException { Stringfun obj = new Stringfun ( ) ; obj . words ( ) ; obj . frequency ( ) ; } }

def words ( str ) : NEW_LINE INDENT wcount = vcount = ucount = i = 0 NEW_LINE while i < len ( str ) : NEW_LINE INDENT ch = str [ i ] NEW_LINE if ( ch == " ▁ " or ch == " . " ) : NEW_LINE INDENT wcount += 1 NEW_LINE DEDENT if ( ch == " a " or ch == " e " or ch == " i " or ch == ' o ' or ch == " u " ) : NEW_LINE INDENT vcount += 1 NEW_LINE DEDENT if ( ch == " A " or ch == " E " or ch == " I " or ch == ' O ' or ch == " U " ) : NEW_LINE INDENT vcount += 1 NEW_LINE DEDENT if ( ord ( ch ) >= 65 and ord ( ch ) <= 90 ) : NEW_LINE INDENT ucount += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT print ( " number ▁ of ▁ words ▁ = ▁ " , wcount ) NEW_LINE print ( " number ▁ of ▁ vowels ▁ = ▁ " , vcount ) NEW_LINE print ( " number ▁ of ▁ upper ▁ case ▁ characters ▁ = ▁ " , ucount ) NEW_LINE DEDENT def frequency ( str ) : NEW_LINE INDENT i = 1 NEW_LINE while i < 127 : NEW_LINE INDENT ch1 = chr ( i ) NEW_LINE c = 0 NEW_LINE j = 0 NEW_LINE while j < len ( str ) : NEW_LINE INDENT ch2 = str [ j ] NEW_LINE if ( ch1 == ch2 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if c > 0 : NEW_LINE INDENT print ( " Character : " , ch1 + " ▁ Frequency : " , c ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT s = " Geeks ▁ for ▁ Geeks . " NEW_LINE words ( s ) NEW_LINE frequency ( s ) NEW_LINE


class gfg { static boolean isValid ( String str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = "0110" ; int len = str . length ( ) ; if ( isValid ( str , len ) ) System . out . println ( " Valid " ) ; else System . out . println ( " Invalid " ) ; } }

def isValid ( string , length ) : NEW_LINE INDENT for i in range ( 1 , length ) : NEW_LINE INDENT if string [ i ] == string [ i - 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "0110" NEW_LINE length = len ( string ) NEW_LINE if isValid ( string , length ) : NEW_LINE INDENT print ( " Valid " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static boolean prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; ++ i ) if ( n % i == 0 ) return false ; return true ; } static int Min_Sum ( int n ) { if ( prime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; else { if ( prime ( n - 2 ) ) return 2 ; else return 3 ; } } public static void main ( String [ ] args ) { int n = 27 ; System . out . println ( Min_Sum ( n ) ) ; } }

from math import sqrt NEW_LINE def prime ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def Min_Sum ( n ) : NEW_LINE INDENT if ( prime ( n ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT if ( prime ( n - 2 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 27 NEW_LINE print ( Min_Sum ( n ) ) NEW_LINE DEDENT


class GFG { static String LexicographicalMaxString ( String str ) { String mx = " " ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( mx . compareTo ( str . substring ( i ) ) <= 0 ) { mx = str . substring ( i ) ; } } return mx ; } public static void main ( String [ ] args ) { String str = " ababaa " ; System . out . println ( LexicographicalMaxString ( str ) ) ; } }

def LexicographicalMaxString ( str ) : NEW_LINE INDENT mx = " " NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT mx = max ( mx , str [ i : ] ) NEW_LINE DEDENT return mx NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ababaa " NEW_LINE print ( LexicographicalMaxString ( str ) ) NEW_LINE DEDENT


import java . util . * ; class solution { static int nthTerm ( int n ) { return 5 * ( int ) Math . pow ( n , 2 ) - 5 * n ; } public static void main ( String arr [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }

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


import java . util . * ; class solution { void printArr ( int b [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + b [ i ] + " ▁ " ) ; } } void ModifiedArray ( int a [ ] , int n ) { int l = 0 , r = Integer . MAX_VALUE ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n / 2 ; i ++ ) { b [ i ] = Math . max ( l , a [ i ] - r ) ; b [ n - i - 1 ] = a [ i ] - b [ i ] ; l = b [ i ] ; r = b [ n - i - 1 ] ; } printArr ( b , n ) ; } public static void main ( String args [ ] ) { int a [ ] = { 5 , 6 } ; int n = a . length ; solution s = new solution ( ) ; s . ModifiedArray ( a , 2 * n ) ; } }

import sys NEW_LINE def printArr ( b , n ) : NEW_LINE INDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( b [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def ModifiedArray ( a , n ) : NEW_LINE INDENT l = 0 NEW_LINE r = sys . maxsize NEW_LINE b = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 0 , int ( n / 2 ) , 1 ) : NEW_LINE INDENT b [ i ] = max ( l , a [ i ] - r ) NEW_LINE b [ n - i - 1 ] = a [ i ] - b [ i ] NEW_LINE l = b [ i ] NEW_LINE r = b [ n - i - 1 ] NEW_LINE DEDENT printArr ( b , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 5 , 6 ] NEW_LINE n = len ( a ) NEW_LINE ModifiedArray ( a , 2 * n ) NEW_LINE DEDENT


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

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


class GFG { public static void main ( String [ ] args ) { long ans = 1 ; long mod = ( long ) 1000000007 * 120 ; for ( int i = 0 ; i < 5 ; i ++ ) ans = ( ans * ( 55555 - i ) ) % mod ; ans = ans / 120 ; System . out . println ( " Answer ▁ using " + " ▁ shortcut : ▁ " + ans ) ; } }

ans = 1 NEW_LINE mod = 1000000007 * 120 NEW_LINE for i in range ( 0 , 5 ) : NEW_LINE INDENT ans = ( ans * ( 55555 - i ) ) % mod NEW_LINE DEDENT ans = int ( ans / 120 ) NEW_LINE print ( " Answer ▁ using ▁ shortcut : ▁ " , ans ) NEW_LINE


public class GFG { static final int MAX_CHAR = 26 ; static int countSubstringWithEqualEnds ( String s ) { int result = 0 ; int n = s . length ( ) ; int [ ] count = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; return result ; } public static void main ( String args [ ] ) { String s = " abcab " ; System . out . println ( countSubstringWithEqualEnds ( s ) ) ; } }

MAX_CHAR = 26 ; NEW_LINE def countSubstringWithEqualEnds ( s ) : NEW_LINE INDENT result = 0 ; NEW_LINE n = len ( s ) ; NEW_LINE count = [ 0 ] * MAX_CHAR ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT s = " abcab " ; NEW_LINE print ( countSubstringWithEqualEnds ( s ) ) ; NEW_LINE


import java . util . * ; class GFG { static boolean isPalindrome ( int n ) { String str = String . valueOf ( n ) ; int s = 0 , e = str . length ( ) - 1 ; while ( s < e ) { if ( str . charAt ( s ) != str . charAt ( e ) ) { return false ; } s ++ ; e -- ; } return true ; } static void palindromicDivisors ( int n ) { Vector < Integer > PalindromDivisors = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( isPalindrome ( i ) ) { PalindromDivisors . add ( i ) ; } } else { if ( isPalindrome ( i ) ) { PalindromDivisors . add ( i ) ; } if ( isPalindrome ( n / i ) ) { PalindromDivisors . add ( n / i ) ; } } } } Collections . sort ( PalindromDivisors ) ; for ( int i = 0 ; i < PalindromDivisors . size ( ) ; i ++ ) { System . out . print ( PalindromDivisors . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 66 ; palindromicDivisors ( n ) ; } }

from math import sqrt ; NEW_LINE def isPalindrome ( n ) : NEW_LINE INDENT string = str ( n ) ; NEW_LINE s = 0 ; e = len ( string ) - 1 ; NEW_LINE while ( s < e ) : NEW_LINE INDENT if ( string [ s ] != string [ e ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT s += 1 ; NEW_LINE e -= 1 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def palindromicDivisors ( n ) : NEW_LINE INDENT PalindromDivisors = [ ] ; NEW_LINE for i in range ( 1 , int ( sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n // i == i ) : NEW_LINE INDENT if ( isPalindrome ( i ) ) : NEW_LINE INDENT PalindromDivisors . append ( i ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( isPalindrome ( i ) ) : NEW_LINE INDENT PalindromDivisors . append ( i ) ; NEW_LINE DEDENT if ( isPalindrome ( n // i ) ) : NEW_LINE INDENT PalindromDivisors . append ( n // i ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT PalindromDivisors . sort ( ) ; NEW_LINE for i in range ( len ( PalindromDivisors ) ) : NEW_LINE INDENT print ( PalindromDivisors [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 66 ; NEW_LINE palindromicDivisors ( n ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int factorial ( int f ) { int fact = 1 ; for ( int i = 2 ; i <= f ; i ++ ) fact *= ( int ) i ; return fact ; } static int findPermutation ( int N , int M ) { int permutation = factorial ( N + M - 1 ) / ( factorial ( N ) * factorial ( M - 1 ) ) ; return permutation ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; System . out . println ( findPermutation ( N , M ) ) ; } }

def factorial ( f ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 2 , f + 1 ) : NEW_LINE INDENT fact *= i NEW_LINE DEDENT return fact NEW_LINE DEDENT def findPermuatation ( N , M ) : NEW_LINE INDENT permutation = ( factorial ( N + M - 1 ) // ( factorial ( N ) * factorial ( M - 1 ) ) ) NEW_LINE return permutation NEW_LINE DEDENT N = 3 ; M = 3 NEW_LINE print ( findPermuatation ( N , M ) ) NEW_LINE


import java . util . * ; class GFG { static int findK ( int n , int k ) { ArrayList < Integer > a = new ArrayList < Integer > ( n ) ; for ( int i = 1 ; i < n ; i ++ ) if ( i % 2 == 1 ) a . add ( i ) ; for ( int i = 1 ; i < n ; i ++ ) if ( i % 2 == 0 ) a . add ( i ) ; return ( a . get ( k - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 3 ; System . out . println ( findK ( n , k ) ) ; } }

def findK ( n , k ) : NEW_LINE INDENT a = list ( ) NEW_LINE i = 1 NEW_LINE while i < n : NEW_LINE INDENT a . append ( i ) NEW_LINE i = i + 2 NEW_LINE DEDENT i = 2 NEW_LINE while i < n : NEW_LINE INDENT a . append ( i ) NEW_LINE i = i + 2 NEW_LINE DEDENT return ( a [ k - 1 ] ) NEW_LINE DEDENT n = 10 NEW_LINE k = 3 NEW_LINE print ( findK ( n , k ) ) NEW_LINE


import java . util . * ; class GFG { static void reArrange ( String words [ ] , int n ) { HashMap < String , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { freq . put ( words [ i ] , ( i + 1 ) ) ; } Arrays . sort ( words ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( freq . get ( words [ i ] ) + " ▁ " ) ; } public static void main ( String [ ] args ) { String words [ ] = { " live " , " place " , " travel " , " word " , " sky " } ; int n = words . length ; reArrange ( words , n ) ; } }

def reArrange ( words , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ words [ i ] ] = i + 1 NEW_LINE DEDENT words . sort ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( mp [ words [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT words = [ " live " , " place " , " travel " , " word " , " sky " ] NEW_LINE n = len ( words ) NEW_LINE reArrange ( words , n ) ; NEW_LINE


import java . util . * ; class GFG { static void CountTriangles ( int [ ] A ) { int n = A . length ; Arrays . sort ( A ) ; int count = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int l = 0 , r = i - 1 ; while ( l < r ) { if ( A [ l ] + A [ r ] > A [ i ] ) { count += r - l ; r -- ; } else { l ++ ; } } } System . out . print ( " No ▁ of ▁ possible ▁ solutions : ▁ " + count ) ; } public static void main ( String [ ] args ) { int [ ] A = { 4 , 3 , 5 , 7 , 6 } ; CountTriangles ( A ) ; } }

def CountTriangles ( A ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE A . sort ( ) ; NEW_LINE count = 0 ; NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT l = 0 ; NEW_LINE r = i - 1 ; NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( A [ l ] + A [ r ] > A [ i ] ) : NEW_LINE INDENT count += r - l ; NEW_LINE r -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT DEDENT DEDENT print ( " No ▁ of ▁ possible ▁ solutions : ▁ " , count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 3 , 5 , 7 , 6 ] ; NEW_LINE CountTriangles ( A ) ; NEW_LINE DEDENT


public class GFG { static void findNumbers ( int arr [ ] , int n ) { int sumN = ( n * ( n + 1 ) ) / 2 ; int sumSqN = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sum = 0 , sumSq = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; sumSq += Math . pow ( arr [ i ] , 2 ) ; } int B = ( ( ( sumSq - sumSqN ) / ( sum - sumN ) ) + sumN - sum ) / 2 ; int A = sum - sumN + B ; System . out . println ( " A ▁ = ▁ " + A + " \n B ▁ = ▁ " + B ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 } ; int n = arr . length ; findNumbers ( arr , n ) ; } }

import math NEW_LINE def findNumbers ( arr , n ) : NEW_LINE INDENT sumN = ( n * ( n + 1 ) ) / 2 ; NEW_LINE sumSqN = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; NEW_LINE sum = 0 ; NEW_LINE sumSq = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum = sum + arr [ i ] ; NEW_LINE sumSq = sumSq + ( math . pow ( arr [ i ] , 2 ) ) ; NEW_LINE DEDENT B = ( ( ( sumSq - sumSqN ) / ( sum - sumN ) ) + sumN - sum ) / 2 ; NEW_LINE A = sum - sumN + B ; NEW_LINE print ( " A ▁ = ▁ " , int ( A ) ) ; NEW_LINE print ( " B ▁ = ▁ " , int ( B ) ) ; NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findNumbers ( arr , n ) ; NEW_LINE


public class GFG { static int Rank ( int Edges , int Vertices ) { int result = 0 ; result = Edges - Vertices + 1 ; return result ; } public static void main ( String [ ] args ) { int Edges = 7 , Vertices = 5 ; System . out . println ( " Circuit ▁ Rank ▁ = ▁ " + Rank ( Edges , Vertices ) ) ; } }

def Rank ( Edges , Vertices ) : NEW_LINE INDENT result = Edges - Vertices + 1 NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Edges , Vertices = 7 , 5 NEW_LINE print ( " Circuit ▁ Rank ▁ = " , Rank ( Edges , Vertices ) ) NEW_LINE DEDENT


class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } public static void main ( String [ ] args ) { if ( isPowerful ( 20 ) ) System . out . print ( " YES \n " ) ; else System . out . print ( " NO \n " ) ; if ( isPowerful ( 27 ) ) System . out . print ( " YES \n " ) ; else System . out . print ( " NO \n " ) ; } }

import math NEW_LINE def isPowerful ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n = n // 2 NEW_LINE power = power + 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT for factor in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % factor == 0 ) : NEW_LINE INDENT n = n // factor NEW_LINE power = power + 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT return ( n == 1 ) NEW_LINE DEDENT print ( " YES " if isPowerful ( 20 ) else " NO " ) NEW_LINE print ( " YES " if isPowerful ( 27 ) else " NO " ) NEW_LINE


class GFG { static int valueofX ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ar [ i ] ; } if ( sum % n == 0 ) { return sum / n ; } else { int A = sum / n , B = sum / n + 1 ; int ValueA = 0 , ValueB = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ValueA += ( ar [ i ] - A ) * ( ar [ i ] - A ) ; ValueB += ( ar [ i ] - B ) * ( ar [ i ] - B ) ; } if ( ValueA < ValueB ) { return A ; } else { return B ; } } } public static void main ( String args [ ] ) { int n = 7 ; int arr [ ] = { 6 , 9 , 1 , 6 , 1 , 3 , 7 } ; System . out . println ( valueofX ( arr , n ) ) ; } }

def valueofX ( ar , n ) : NEW_LINE INDENT summ = sum ( ar ) NEW_LINE if ( summ % n == 0 ) : NEW_LINE INDENT return summ // n NEW_LINE DEDENT else : NEW_LINE INDENT A = summ // n NEW_LINE B = summ // n + 1 NEW_LINE ValueA = 0 NEW_LINE ValueB = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ValueA += ( ar [ i ] - A ) * ( ar [ i ] - A ) NEW_LINE ValueB += ( ar [ i ] - B ) * ( ar [ i ] - B ) NEW_LINE DEDENT if ( ValueA < ValueB ) : NEW_LINE INDENT return A NEW_LINE DEDENT else : NEW_LINE INDENT return B NEW_LINE DEDENT DEDENT DEDENT n = 7 NEW_LINE arr = [ 6 , 9 , 1 , 6 , 1 , 3 , 7 ] NEW_LINE print ( valueofX ( arr , n ) ) NEW_LINE


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

def ways ( n ) : NEW_LINE INDENT return n // 2 NEW_LINE DEDENT n = 2 NEW_LINE print ( ways ( n ) ) NEW_LINE


class GFG { static int product ( int a [ ] , int n ) { int ans = 1 ; int val = ( int ) Math . pow ( 2 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { ans *= ( int ) Math . pow ( a [ i ] , val ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 2 ; int a [ ] = { 3 , 7 } ; System . out . println ( product ( a , n ) ) ; } }

def product ( a , n ) : NEW_LINE INDENT ans = 1 NEW_LINE val = pow ( 2 , n - 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans *= pow ( a [ i ] , val ) NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 2 NEW_LINE a = [ 3 , 7 ] NEW_LINE print ( product ( a , n ) ) NEW_LINE


import java . io . * ; class GFG { static String getNumber ( String s ) { int number_of_digits = s . length ( ) ; int freq [ ] = new int [ 10 ] ; for ( int i = 0 ; i < number_of_digits ; i ++ ) { if ( s . charAt ( i ) == '1' || s . charAt ( i ) == '2' || s . charAt ( i ) == '3' || s . charAt ( i ) == '5' || s . charAt ( i ) == '7' ) { freq [ s . charAt ( i ) - 48 ] += 1 ; } if ( s . charAt ( i ) == '4' ) { freq [ 2 ] += 2 ; freq [ 3 ] ++ ; } if ( s . charAt ( i ) == '6' ) { freq [ 5 ] ++ ; freq [ 3 ] ++ ; } if ( s . charAt ( i ) == '8' ) { freq [ 7 ] ++ ; freq [ 2 ] += 3 ; } if ( s . charAt ( i ) == '9' ) { freq [ 7 ] ++ ; freq [ 3 ] += 2 ; freq [ 2 ] ++ ; } } String t = " " ; if ( freq [ 1 ] == number_of_digits || freq [ 0 ] == number_of_digits || ( freq [ 0 ] + freq [ 1 ] ) == number_of_digits ) { return s ; } else { for ( int i = 9 ; i >= 2 ; i -- ) { int ctr = freq [ i ] ; while ( ( ctr -- ) > 0 ) { t += ( char ) ( i + 48 ) ; } } return t ; } } public static void main ( String [ ] args ) { String s = "1280" ; System . out . println ( getNumber ( s ) ) ; } }

def getNumber ( s ) : NEW_LINE INDENT number_of_digits = len ( s ) ; NEW_LINE freq = [ 0 ] * 10 ; NEW_LINE for i in range ( number_of_digits ) : NEW_LINE INDENT if ( s [ i ] == '1' or s [ i ] == '2' or s [ i ] == '3' or s [ i ] == '5' or s [ i ] == '7' ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - 48 ] += 1 ; NEW_LINE DEDENT if ( s [ i ] == '4' ) : NEW_LINE INDENT freq [ 2 ] += 2 ; NEW_LINE freq [ 3 ] += 1 ; NEW_LINE DEDENT if ( s [ i ] == '6' ) : NEW_LINE INDENT freq [ 5 ] += 1 ; NEW_LINE freq [ 3 ] += 1 ; NEW_LINE DEDENT if ( s [ i ] == '8' ) : NEW_LINE INDENT freq [ 7 ] += 1 ; NEW_LINE freq [ 2 ] += 3 ; NEW_LINE DEDENT if ( s [ i ] == '9' ) : NEW_LINE INDENT freq [ 7 ] += 1 ; NEW_LINE freq [ 3 ] += 2 ; NEW_LINE freq [ 2 ] += 1 ; NEW_LINE DEDENT DEDENT t = " " ; NEW_LINE if ( freq [ 1 ] == number_of_digits or freq [ 0 ] == number_of_digits or ( freq [ 0 ] + freq [ 1 ] ) == number_of_digits ) : NEW_LINE INDENT return s ; NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 9 , 1 , - 1 ) : NEW_LINE INDENT ctr = freq [ i ] ; NEW_LINE while ( ctr > 0 ) : NEW_LINE INDENT t += chr ( i + 48 ) ; NEW_LINE ctr -= 1 ; NEW_LINE DEDENT DEDENT return t ; NEW_LINE DEDENT DEDENT s = "1280" ; NEW_LINE print ( getNumber ( s ) ) ; NEW_LINE


class GFG { static int getPassingCars ( int A [ ] , int n ) { int countOne = 0 , result = 0 ; while ( n >= 1 ) { if ( A [ n - 1 ] == 1 ) countOne ++ ; else result += countOne ; n -- ; } return result ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , 1 , 0 , 1 , 1 } ; int n = A . length ; System . out . println ( getPassingCars ( A , n ) ) ; } }

def getPassingCars ( A , n ) : NEW_LINE INDENT countOne = 0 ; result = 0 NEW_LINE while n >= 1 : NEW_LINE INDENT if A [ n - 1 ] == 1 : NEW_LINE INDENT countOne += 1 NEW_LINE DEDENT else : NEW_LINE INDENT result += countOne NEW_LINE DEDENT n -= 1 NEW_LINE DEDENT return result NEW_LINE DEDENT A = [ 0 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( A ) NEW_LINE print ( getPassingCars ( A , n ) ) NEW_LINE


class GFG { static final int R = 3 ; static final int C = 3 ; static final int MAX_K = 100 ; static int [ ] [ ] [ ] dp = new int [ R ] [ C ] [ MAX_K ] ; static int pathCountDPRecDP ( int [ ] [ ] mat , int m , int n , int k ) { if ( m < 0 || n < 0 ) return 0 ; if ( m == 0 && n == 0 ) return ( k == mat [ m ] [ n ] ? 1 : 0 ) ; if ( dp [ m ] [ n ] [ k ] != - 1 ) return dp [ m ] [ n ] [ k ] ; dp [ m ] [ n ] [ k ] = pathCountDPRecDP ( mat , m - 1 , n , k - mat [ m ] [ n ] ) + pathCountDPRecDP ( mat , m , n - 1 , k - mat [ m ] [ n ] ) ; return dp [ m ] [ n ] [ k ] ; } static int pathCountDP ( int [ ] [ ] mat , int k ) { for ( int i = 0 ; i < R ; i ++ ) for ( int j = 0 ; j < C ; j ++ ) for ( int l = 0 ; l < MAX_K ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; return pathCountDPRecDP ( mat , R - 1 , C - 1 , k ) ; } public static void main ( String [ ] args ) { int k = 12 ; int [ ] [ ] mat = new int [ ] [ ] { new int [ ] { 1 , 2 , 3 } , new int [ ] { 4 , 6 , 5 } , new int [ ] { 3 , 2 , 1 } } ; System . out . println ( pathCountDP ( mat , k ) ) ; } }

R = 3 NEW_LINE C = 3 NEW_LINE MAX_K = 1000 NEW_LINE def pathCountDPRecDP ( mat , m , n , k ) : NEW_LINE INDENT if m < 0 or n < 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif m == 0 and n == 0 : NEW_LINE INDENT return k == mat [ m ] [ n ] NEW_LINE DEDENT if ( dp [ m ] [ n ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ m ] [ n ] [ k ] NEW_LINE DEDENT dp [ m ] [ n ] [ k ] = ( pathCountDPRecDP ( mat , m - 1 , n , k - mat [ m ] [ n ] ) + pathCountDPRecDP ( mat , m , n - 1 , k - mat [ m ] [ n ] ) ) NEW_LINE return dp [ m ] [ n ] [ k ] NEW_LINE DEDENT def pathCountDP ( mat , k ) : NEW_LINE INDENT return pathCountDPRecDP ( mat , R - 1 , C - 1 , k ) NEW_LINE DEDENT k = 12 NEW_LINE dp = [ [ [ - 1 for col in range ( MAX_K ) ] for col in range ( C ) ] for row in range ( R ) ] NEW_LINE mat = [ [ 1 , 2 , 3 ] , [ 4 , 6 , 5 ] , [ 3 , 2 , 1 ] ] NEW_LINE print ( pathCountDP ( mat , k ) ) NEW_LINE


public class Main { static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; 1 << i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { int num = ( 1 << i ) + ( 1 << j ) ; if ( num <= n ) sum += num ; } } return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findSum ( n ) ) ; } }

def findSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE i = 1 NEW_LINE while ( ( 1 << i ) < n ) : NEW_LINE INDENT for j in range ( 0 , i ) : NEW_LINE INDENT num = ( 1 << i ) + ( 1 << j ) NEW_LINE if ( num <= n ) : NEW_LINE INDENT sum += num NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 10 NEW_LINE print ( findSum ( n ) ) NEW_LINE


import java . io . * ; public class GFG { static void printDiagonalSums ( int [ ] [ ] mat , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) principal += mat [ i ] [ j ] ; if ( ( i + j ) == ( n - 1 ) ) secondary += mat [ i ] [ j ] ; } } System . out . println ( " Principal ▁ Diagonal : " + principal ) ; System . out . println ( " Secondary ▁ Diagonal : " + secondary ) ; } static public void main ( String [ ] args ) { int [ ] [ ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; } }

MAX = 100 NEW_LINE def printDiagonalSums ( mat , n ) : NEW_LINE INDENT principal = 0 NEW_LINE secondary = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT principal += mat [ i ] [ j ] NEW_LINE DEDENT if ( ( i + j ) == ( n - 1 ) ) : NEW_LINE INDENT secondary += mat [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT print ( " Principal ▁ Diagonal : " , principal ) NEW_LINE print ( " Secondary ▁ Diagonal : " , secondary ) NEW_LINE DEDENT a = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] NEW_LINE printDiagonalSums ( a , 4 ) NEW_LINE


import java . io . * ; class GFG { static boolean isTriangular ( int num ) { if ( num < 0 ) return false ; int c = ( - 2 * num ) ; int b = 1 , a = 1 ; int d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return false ; float root1 = ( - b + ( float ) Math . sqrt ( d ) ) / ( 2 * a ) ; float root2 = ( - b - ( float ) Math . sqrt ( d ) ) / ( 2 * a ) ; if ( root1 > 0 && Math . floor ( root1 ) == root1 ) return true ; if ( root2 > 0 && Math . floor ( root2 ) == root2 ) return true ; return false ; } public static void main ( String [ ] args ) { int num = 55 ; if ( isTriangular ( num ) ) System . out . println ( " The ▁ number ▁ is " + " ▁ a ▁ triangular ▁ number " ) ; else System . out . println ( " The ▁ number ▁ " + " is ▁ NOT ▁ a ▁ triangular ▁ number " ) ; } }

import math NEW_LINE def isTriangular ( num ) : NEW_LINE INDENT if ( num < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT c = ( - 2 * num ) NEW_LINE b , a = 1 , 1 NEW_LINE d = ( b * b ) - ( 4 * a * c ) NEW_LINE if ( d < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT root1 = ( - b + math . sqrt ( d ) ) / ( 2 * a ) NEW_LINE root2 = ( - b - math . sqrt ( d ) ) / ( 2 * a ) NEW_LINE if ( root1 > 0 and math . floor ( root1 ) == root1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( root2 > 0 and math . floor ( root2 ) == root2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 55 NEW_LINE if ( isTriangular ( n ) ) : NEW_LINE INDENT print ( " The ▁ number ▁ is ▁ a ▁ triangular ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ number ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static Vector < Integer > find_idx ( int ar [ ] , int n ) { Vector < Integer > answer = new Vector < Integer > ( ) ; int sum = ar [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( sum == ar [ i ] ) { answer . add ( i + 1 ) ; } sum += ar [ i ] ; } return answer ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 3 , 6 , 3 , 15 , 5 } ; int n = ar . length ; Vector < Integer > ans = find_idx ( ar , n ) ; if ( ans . size ( ) != 0 ) { for ( int i : ans ) { System . out . print ( i + " ▁ " ) ; } } else { System . out . println ( " - 1" ) ; } } }

def find_idx ( ar , n ) : NEW_LINE INDENT answer = [ ] ; NEW_LINE sum = ar [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( sum == ar [ i ] ) : NEW_LINE INDENT answer . append ( i + 1 ) ; NEW_LINE DEDENT sum += ar [ i ] ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ar = [ 1 , 2 , 3 , 6 , 3 , 15 , 5 ] ; NEW_LINE n = len ( ar ) ; NEW_LINE ans = find_idx ( ar , n ) ; NEW_LINE if ( len ( ans ) != 0 ) : NEW_LINE INDENT for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int findEvenPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( ( ! ( ( A [ i ] & 1 ) > 0 ) ) ) count ++ ; return count * ( count - 1 ) / 2 ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = A . length ; System . out . println ( findEvenPair ( A , N ) ) ; } }

def findEvenPair ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( not ( A [ i ] & 1 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count * ( count - 1 ) // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 5 , 6 , 2 , 8 ] NEW_LINE N = len ( A ) NEW_LINE print ( findEvenPair ( A , N ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static void swap ( int [ ] a , int i , int j ) { int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } static void printArray ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 } ; int n = arr . length ; printArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && i % 2 == 1 ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < 0 && j % 2 == 0 ) { swap ( arr , i , j ) ; break ; } } } else if ( arr [ i ] < 0 && i % 2 == 0 ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] >= 0 && j % 2 == 1 ) { swap ( arr , i , j ) ; break ; } } } } printArray ( arr , n ) ; } }

def printArray ( a , n ) : NEW_LINE INDENT for i in a : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT arr = [ 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE printArray ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= 0 and i % 2 == 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] < 0 and j % 2 == 0 ) : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT elif ( arr [ i ] < 0 and i % 2 == 0 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] >= 0 and j % 2 == 1 ) : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT printArray ( arr , n ) ; NEW_LINE


class solution { static final int max = 4 ; static int countWays ( int index , int cnt , int dp [ ] [ ] , int n , int m , int k ) { if ( index == n ) { if ( cnt == k ) return 1 ; else return 0 ; } if ( dp [ index ] [ cnt ] != - 1 ) return dp [ index ] [ cnt ] ; int ans = 0 ; ans += countWays ( index + 1 , cnt , dp , n , m , k ) ; ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k ) ; return dp [ index ] [ cnt ] = ans ; } public static void main ( String args [ ] ) { int n = 3 , m = 3 , k = 2 ; int dp [ ] [ ] = new int [ n + 1 ] [ max ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) for ( int j = 0 ; j < max ; j ++ ) dp [ i ] [ j ] = - 1 ; System . out . println ( m * countWays ( 1 , 0 , dp , n , m , k ) ) ; } }

max = 4 NEW_LINE def countWays ( index , cnt , dp , n , m , k ) : NEW_LINE INDENT if ( index == n ) : NEW_LINE INDENT if ( cnt == k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( dp [ index ] [ cnt ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ cnt ] NEW_LINE DEDENT ans = 0 NEW_LINE ans += countWays ( index + 1 , cnt , dp , n , m , k ) NEW_LINE ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k ) NEW_LINE dp [ index ] [ cnt ] = ans NEW_LINE return dp [ index ] [ cnt ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE m = 3 NEW_LINE k = 2 NEW_LINE dp = [ [ - 1 for x in range ( n + 1 ) ] for y in range ( max ) ] NEW_LINE print ( m * countWays ( 1 , 0 , dp , n , m , k ) ) NEW_LINE DEDENT


public class GFG { static boolean isVowel ( char ch ) { ch = Character . toUpperCase ( ch ) ; return ( ch == ' A ' || ch == ' E ' || ch == ' I ' || ch == ' O ' || ch == ' U ' ) ; } static int countVowels ( String str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( isVowel ( str . charAt ( i ) ) ) ++ count ; return count ; } public static void main ( String args [ ] ) { String str = " abc ▁ de " ; System . out . println ( countVowels ( str ) ) ; } }

def isVowel ( ch ) : NEW_LINE INDENT return ch . upper ( ) in [ ' A ' , ' E ' , ' I ' , ' O ' , ' U ' ] NEW_LINE DEDENT def countVowels ( str ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if isVowel ( str [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT str = ' abc ▁ de ' NEW_LINE print ( countVowels ( str ) ) NEW_LINE


class GFG { static void convertToBinary ( int num , int a [ ] , int n ) { int pointer = n - 1 ; while ( num > 0 ) { a [ pointer ] = num % 2 ; num = num / 2 ; pointer -- ; } } static void checkforsum ( int a [ ] , int n ) { int sum1 = 0 ; int sum2 = 0 ; int mid = n / 2 ; for ( int i = 0 ; i < mid ; i ++ ) sum1 = sum1 + a [ i ] ; for ( int j = mid ; j < n ; j ++ ) sum2 = sum2 + a [ j ] ; if ( sum1 == sum2 ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] ) ; System . out . println ( ) ; } } static void print_seq ( int m ) { int n = ( 2 * m ) ; int a [ ] = new int [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { a [ j ] = 0 ; } for ( int i = 0 ; i < ( int ) Math . pow ( 2 , n ) ; i ++ ) { convertToBinary ( i , a , n ) ; checkforsum ( a , n ) ; } } public static void main ( String [ ] args ) { int m = 2 ; print_seq ( m ) ; } }

def convertToBinary ( num , a , n ) : NEW_LINE INDENT pointer = n - 1 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT a [ pointer ] = num % 2 NEW_LINE num = num // 2 NEW_LINE pointer -= 1 NEW_LINE DEDENT DEDENT def checkforsum ( a , n ) : NEW_LINE INDENT sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE mid = n // 2 NEW_LINE for i in range ( mid ) : NEW_LINE INDENT sum1 = sum1 + a [ i ] NEW_LINE DEDENT for j in range ( mid , n ) : NEW_LINE INDENT sum2 = sum2 + a [ j ] NEW_LINE DEDENT if ( sum1 == sum2 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def print_seq ( m ) : NEW_LINE INDENT n = ( 2 * m ) NEW_LINE a = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( pow ( 2 , n ) ) : NEW_LINE INDENT convertToBinary ( i , a , n ) NEW_LINE checkforsum ( a , n ) NEW_LINE DEDENT DEDENT m = 2 NEW_LINE print_seq ( m ) NEW_LINE


class GFG { static float ellipsearea ( float r ) { if ( r < 0 ) return - 1 ; float a = ( float ) ( ( 3.14f * r * r ) / 4 ) ; return a ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( ellipsearea ( r ) ) ; } }

def ellipsearea ( r ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT a = ( 3.14 * r * r ) / 4 ; NEW_LINE return a ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT r = 5 ; NEW_LINE print ( ellipsearea ( r ) ) ; NEW_LINE DEDENT


public class DivisibleParts { static void printTwoDivisibleParts ( String num , int f , int s ) { int N = num . length ( ) ; int [ ] prefixReminder = new int [ N + 1 ] ; int [ ] suffixReminder = new int [ N + 1 ] ; suffixReminder [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( num . charAt ( i - 1 ) - '0' ) ) % f ; prefixReminder [ N ] = 0 ; int base = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( num . charAt ( i ) - '0' ) * base ) % s ; base = ( base * 10 ) % s ; } for ( int i = 0 ; i < N ; i ++ ) { if ( prefixReminder [ i ] == 0 && suffixReminder [ i ] == 0 && num . charAt ( i ) != '0' ) { System . out . println ( num . substring ( 0 , i ) + " ▁ " + num . substring ( i ) ) ; return ; } } System . out . println ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { String num = "246904096" ; int f = 12345 ; int s = 1024 ; printTwoDivisibleParts ( num , f , s ) ; } }

def printTwoDivisibleParts ( num , f , s ) : NEW_LINE INDENT N = len ( num ) ; NEW_LINE prefixReminder = [ 0 ] * ( N + 1 ) ; NEW_LINE suffixReminder = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( ord ( num [ i - 1 ] ) - 48 ) ) % f ; NEW_LINE DEDENT base = 1 ; NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( ord ( num [ i ] ) - 48 ) * base ) % s ; NEW_LINE base = ( base * 10 ) % s ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( prefixReminder [ i ] == 0 and suffixReminder [ i ] == 0 and num [ i ] != '0' ) : NEW_LINE INDENT print ( num [ 0 : i ] , num [ i : N ] ) ; NEW_LINE return 0 ; NEW_LINE DEDENT DEDENT print ( " Not ▁ Possible " ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = "246904096" ; NEW_LINE f = 12345 ; NEW_LINE s = 1024 ; NEW_LINE printTwoDivisibleParts ( num , f , s ) ; NEW_LINE DEDENT


import java . io . * ; public class GFG { static int [ ] fact = new int [ ] { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; static boolean peterson ( int n ) { int num = n ; int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; sum += fact [ digit ] ; n = n / 10 ; } return ( sum == num ) ; } static public void main ( String [ ] args ) { int n = 145 ; if ( peterson ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

fact = [ 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 ] NEW_LINE def peterson ( n ) : NEW_LINE INDENT num = n NEW_LINE sum = 0 NEW_LINE while n > 0 : NEW_LINE INDENT digit = int ( n % 10 ) NEW_LINE sum += fact [ digit ] NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return ( sum == num ) NEW_LINE DEDENT n = 145 NEW_LINE print ( " Yes " if peterson ( n ) else " No " ) NEW_LINE