import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int totalSum ( int a [ ] , int n ) { int i , sum = 0 , sum1 = 0 , j ; for ( i = 0 ; i < n ; i ++ ) { sum1 = 0 ; for ( j = i ; j < n ; j ++ ) { sum1 = ( sum1 | a [ j ] ) ; sum = sum + sum1 ; } } return sum ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = a . length ; System . out . println ( totalSum ( a , n ) ) ; } }

def totalSum ( a , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sum1 = ( sum1 | a [ j ] ) ; NEW_LINE sum = sum + sum1 ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT a = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( totalSum ( a , n ) ) ; NEW_LINE


class GFG { static int triplets ( int N ) { return ( ( N + 1 ) * ( N + 2 ) ) / 2 ; } public static void main ( String [ ] args ) { int N = 50 ; System . out . println ( triplets ( N ) ) ; } }

def triplets ( N ) : NEW_LINE INDENT return ( ( N + 1 ) * ( N + 2 ) ) // 2 ; NEW_LINE DEDENT N = 50 ; NEW_LINE print ( triplets ( N ) ) NEW_LINE


class GFG { static int largestPrimeFactor ( int n ) { int max = - 1 ; while ( n % 2 == 0 ) { max = 2 ; n >>= 1 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { max = i ; n = n / i ; } } if ( n > 2 ) max = n ; return max ; } static boolean checkUnusual ( int n ) { int factor = largestPrimeFactor ( n ) ; if ( factor > Math . sqrt ( n ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 14 ; if ( checkUnusual ( n ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

from math import sqrt NEW_LINE def largestPrimeFactor ( n ) : NEW_LINE INDENT max = - 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT max = 2 ; NEW_LINE n >>= 1 ; NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT max = i ; NEW_LINE n = n / i ; NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT max = n NEW_LINE DEDENT return max NEW_LINE DEDENT def checkUnusual ( n ) : NEW_LINE INDENT factor = largestPrimeFactor ( n ) NEW_LINE if factor > sqrt ( n ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 14 NEW_LINE if checkUnusual ( n ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


import java . util . * ; public class OrderOfCharactersForPattern { public static boolean followsPattern ( String str , String pattern ) { Set < Character > patternSet = neHashSet < > ( ) ; for ( int i = 0 ; i < pattern . length ( ) ; i ++ ) patternSet . add ( pattern . charAt ( i ) ) ; StringBuilder modifiedString = new StringBuilder ( str ) ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) if ( ! patternSet . contains ( modifiedString . charAt ( i ) ) ) modifiedString . deleteCharAt ( i ) ; for ( int i = modifiedString . length ( ) - 1 ; i > 0 ; i -- ) if ( modifiedString . charAt ( i ) == modifiedString . charAt ( i - 1 ) ) modifiedString . deleteCharAt ( i ) ; if ( pattern . length ( ) != modifiedString . length ( ) ) return false ; for ( int i = 0 ; i < pattern . length ( ) ; i ++ ) if ( pattern . charAt ( i ) != modifiedString . charAt ( i ) ) return false ; return true ; } int main ( ) { String str = " engineers ▁ rock " ; String pattern = " er " ; System . out . println ( " Expected : ▁ true , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " egr " ; System . out . println ( " Expected : ▁ false , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " gsr " ; System . out . println ( " Expected : ▁ false , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " eger " ; System . out . println ( " Expected : ▁ true , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; return 0 ; } }

def followsPattern ( string , pattern ) : NEW_LINE INDENT patternSet = set ( ) NEW_LINE for i in range ( len ( pattern ) ) : NEW_LINE INDENT patternSet . add ( pattern [ i ] ) NEW_LINE DEDENT modifiedString = string NEW_LINE for i in range ( len ( string ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if not modifiedString [ i ] in patternSet : NEW_LINE INDENT modifiedString = modifiedString [ : i ] +   \ NEW_LINE INDENT modifiedString [ i + 1 : ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( len ( modifiedString ) - 1 , 0 , - 1 ) : NEW_LINE INDENT if modifiedString [ i ] == modifiedString [ i - 1 ] : NEW_LINE INDENT modifiedString = modifiedString [ : i ] +   \ NEW_LINE INDENT modifiedString [ i + 1 : ] NEW_LINE DEDENT DEDENT DEDENT if len ( pattern ) != len ( modifiedString ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( len ( pattern ) ) : NEW_LINE INDENT if pattern [ i ] != modifiedString [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " engineers ▁ rock " NEW_LINE pattern = " er " NEW_LINE print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " egr " NEW_LINE print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " gsr " NEW_LINE print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " eger " NEW_LINE print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int MAX = 100000 ; static void countEleLessThanOrEqual ( int arr1 [ ] , int m , int arr2 [ ] , int n ) { int hash [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr2 [ i ] ] ++ ; for ( int i = 1 ; i < MAX ; i ++ ) { hash [ i ] = hash [ i ] + hash [ i - 1 ] ; } for ( int i = 0 ; i < m ; i ++ ) { System . out . print ( hash [ arr1 [ i ] ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 , 4 , 7 , 9 } ; int arr2 [ ] = { 0 , 1 , 2 , 1 , 1 , 4 } ; int m , n ; m = arr1 . length ; n = arr2 . length ; countEleLessThanOrEqual ( arr1 , m , arr2 , n ) ; } }

MAX = 100000 NEW_LINE def countEleLessThanOrEqual ( arr1 , m , arr2 , n ) : NEW_LINE INDENT hash = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ arr2 [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , MAX , 1 ) : NEW_LINE INDENT hash [ i ] = hash [ i ] + hash [ i - 1 ] NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT print ( hash [ arr1 [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 2 , 3 , 4 , 7 , 9 ] NEW_LINE arr2 = [ 0 , 1 , 2 , 1 , 1 , 4 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE countEleLessThanOrEqual ( arr1 , m , arr2 , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static long pref [ ] = new long [ 100010 ] ; static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static int isNonFibonacci ( int n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 0 ; else return n ; } static void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) ; } } static void printSum ( int L , int R ) { int sum = ( int ) ( pref [ R ] - pref [ L - 1 ] ) ; System . out . print ( sum + " ▁ " ) ; } public static void main ( String [ ] args ) { compute ( ) ; int Q = 2 ; int arr [ ] [ ] = { { 1 , 5 } , { 6 , 10 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } }

from math import sqrt NEW_LINE pref = [ 0 ] * 100010 NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( sqrt ( x ) ) NEW_LINE if ( s * s == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isNonFibonacci ( n ) : NEW_LINE INDENT x = 5 * n * n NEW_LINE if ( isPerfectSquare ( x + 4 ) or isPerfectSquare ( x - 4 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return n NEW_LINE DEDENT DEDENT def compute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT sum = pref [ R ] - pref [ L - 1 ] NEW_LINE print ( sum , end = " ▁ " ) NEW_LINE DEDENT compute ( ) NEW_LINE Q = 2 NEW_LINE arr = [ [ 1 , 5 ] , [ 6 , 10 ] ] NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT


class GFG { static int trace ( int n , int m ) { int A [ ] [ ] = new int [ n ] [ m ] ; int B [ ] [ ] = new int [ n ] [ m ] ; int C [ ] [ ] = new int [ n ] [ m ] ; int cnt = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) { A [ i ] [ j ] = cnt ; cnt ++ ; } cnt = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) { B [ j ] [ i ] = cnt ; cnt ++ ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( i == j ) sum += C [ i ] [ j ] ; return sum ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; System . out . println ( trace ( N , M ) ) ; } }

def trace ( n , m ) : NEW_LINE INDENT A = [ [ 0 for x in range ( m ) ] for y in range ( n ) ] ; NEW_LINE B = [ [ 0 for x in range ( m ) ] for y in range ( n ) ] ; NEW_LINE C = [ [ 0 for x in range ( m ) ] for y in range ( n ) ] ; NEW_LINE cnt = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT A [ i ] [ j ] = cnt ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT cnt = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT B [ j ] [ i ] = cnt ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT C [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ; NEW_LINE DEDENT DEDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT sum += C [ i ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT return sum ; NEW_LINE DEDENT N = 3 ; NEW_LINE M = 3 ; NEW_LINE print ( trace ( N , M ) ) ; NEW_LINE


import java . io . * ; class GFG { static int nCr ( int n , int k ) { int C [ ] [ ] = new int [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } static double resultOfAllSubsets ( int arr [ ] , int N ) { double result = 0.0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; for ( int n = 1 ; n <= N ; n ++ ) result += ( double ) ( sum * ( nCr ( N - 1 , n - 1 ) ) ) / n ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 7 } ; int N = arr . length ; System . out . println ( resultOfAllSubsets ( arr , N ) ) ; } }

def nCr ( n , k ) : NEW_LINE INDENT C = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] NEW_LINE DEDENT def resultOfAllSubsets ( arr , N ) : NEW_LINE INDENT result = 0.0 NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT for n in range ( 1 , N + 1 ) : NEW_LINE INDENT result += ( sum * ( nCr ( N - 1 , n - 1 ) ) ) / n NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE print ( resultOfAllSubsets ( arr , N ) ) NEW_LINE


import java . io . * ; class GFG { static void solveWordWrap ( int arr [ ] , int n , int k ) { int i , j ; int currlen ; int cost ; int dp [ ] = new int [ n ] ; int ans [ ] = new int [ n ] ; dp [ n - 1 ] = 0 ; ans [ n - 1 ] = n - 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { currlen = - 1 ; dp [ i ] = Integer . MAX_VALUE ; for ( j = i ; j < n ; j ++ ) { currlen += ( arr [ j ] + 1 ) ; if ( currlen > k ) break ; if ( j == n - 1 ) cost = 0 ; else cost = ( k - currlen ) * ( k - currlen ) + dp [ j + 1 ] ; if ( cost < dp [ i ] ) { dp [ i ] = cost ; ans [ i ] = j ; } } } i = 0 ; while ( i < n ) { System . out . print ( ( i + 1 ) + " ▁ " + ( ans [ i ] + 1 ) + " ▁ " ) ; i = ans [ i ] + 1 ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 5 } ; int n = arr . length ; int M = 6 ; solveWordWrap ( arr , n , M ) ; } }

import sys NEW_LINE def solveWordWrap ( arr , n , k ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE ans = [ 0 ] * n NEW_LINE dp [ n - 1 ] = 0 NEW_LINE ans [ n - 1 ] = n - 1 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT currlen = - 1 NEW_LINE dp [ i ] = sys . maxsize NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT currlen += ( arr [ j ] + 1 ) NEW_LINE if ( currlen > k ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( j == n - 1 ) : NEW_LINE INDENT cost = 0 NEW_LINE DEDENT else : NEW_LINE INDENT cost = ( ( k - currlen ) * ( k - currlen ) + dp [ j + 1 ] ) NEW_LINE DEDENT if ( cost < dp [ i ] ) : NEW_LINE INDENT dp [ i ] = cost NEW_LINE ans [ i ] = j NEW_LINE DEDENT DEDENT DEDENT i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT print ( i + 1 , ans [ i ] + 1 , end = " ▁ " ) NEW_LINE i = ans [ i ] + 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE M = 6 NEW_LINE solveWordWrap ( arr , n , M ) NEW_LINE DEDENT


import java . util . Arrays ; import java . io . * ; class GFG { static int N = 20 ; static int [ ] fact = new int [ N ] ; static void pre ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } static int CountPermutation ( int a [ ] , int n ) { int ways = 1 ; Arrays . sort ( a ) ; int size = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 4 , 4 , 2 , 4 } ; int n = a . length ; pre ( ) ; System . out . println ( CountPermutation ( a , n ) ) ; } }

N = 20 NEW_LINE fact = [ 0 ] * N ; NEW_LINE def pre ( ) : NEW_LINE INDENT fact [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT fact [ i ] = i * fact [ i - 1 ] ; NEW_LINE DEDENT DEDENT def CountPermutation ( a , n ) : NEW_LINE INDENT ways = 1 ; NEW_LINE a . sort ( ) ; NEW_LINE size = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] == a [ i - 1 ] ) : NEW_LINE INDENT size += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ways *= fact [ size ] ; NEW_LINE size = 1 ; NEW_LINE DEDENT DEDENT ways *= fact [ size ] ; NEW_LINE return ways ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 4 , 4 , 2 , 4 ] ; NEW_LINE n = len ( a ) ; NEW_LINE pre ( ) ; NEW_LINE print ( CountPermutation ( a , n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int [ ] fact = new int [ 14 ] ; static int size = 1 ; static void preCompute ( int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; fact [ i - 1 ] <= N ; i ++ ) { fact [ i ] = ( fact [ i - 1 ] * i ) ; size ++ ; } } static void findMin ( int N ) { preCompute ( N ) ; int originalN = N ; Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int i = size - 1 ; i >= 0 ; i -- ) { while ( N >= fact [ i ] ) { N -= fact [ i ] ; ans . add ( fact [ i ] ) ; } } System . out . print ( ans . size ( ) + " \n " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 27 ; findMin ( n ) ; } }

fact = [ 0 ] * 14 NEW_LINE size = 1 NEW_LINE def preCompute ( N ) : NEW_LINE INDENT global size NEW_LINE fact [ 0 ] = 1 NEW_LINE i = 1 NEW_LINE while fact [ i - 1 ] <= N : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE size += 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def findMin ( N ) : NEW_LINE INDENT preCompute ( N ) NEW_LINE originalN = N NEW_LINE ans = [ ] NEW_LINE for i in range ( size - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( N >= fact [ i ] ) : NEW_LINE INDENT N -= fact [ i ] NEW_LINE ans . append ( fact [ i ] ) NEW_LINE DEDENT DEDENT print ( len ( ans ) ) NEW_LINE for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 27 NEW_LINE findMin ( n ) NEW_LINE


import java . text . * ; class GFG { static double f ( double x , double y ) { double v = y - 2 * x * x + 1 ; return v ; } static double predict ( double x , double y , double h ) { double y1p = y + h * f ( x , y ) ; return y1p ; } static double correct ( double x , double y , double x1 , double y1 , double h ) { double e = 0.00001 ; double y1c = y1 ; do { y1 = y1c ; y1c = y + 0.5 * h * ( f ( x , y ) + f ( x1 , y1 ) ) ; } while ( Math . abs ( y1c - y1 ) > e ) ; return y1c ; } static void printFinalValues ( double x , double xn , double y , double h ) { while ( x < xn ) { double x1 = x + h ; double y1p = predict ( x , y , h ) ; double y1c = correct ( x , y , x1 , y1p , h ) ; x = x1 ; y = y1c ; } DecimalFormat df = new DecimalFormat ( " # . # # # # # " ) ; System . out . println ( " The ▁ final ▁ value ▁ of ▁ y ▁ at ▁ x ▁ = ▁ " + x + " ▁ is ▁ : ▁ " + df . format ( y ) ) ; } public static void main ( String [ ] args ) { double x = 0 , y = 0.5 ; double xn = 1 ; double h = 0.2 ; printFinalValues ( x , xn , y , h ) ; } }

def f ( x , y ) : NEW_LINE INDENT v = y - 2 * x * x + 1 ; NEW_LINE return v ; NEW_LINE DEDENT def predict ( x , y , h ) : NEW_LINE INDENT y1p = y + h * f ( x , y ) ; NEW_LINE return y1p ; NEW_LINE DEDENT def correct ( x , y , x1 , y1 , h ) : NEW_LINE INDENT e = 0.00001 ; NEW_LINE y1c = y1 ; NEW_LINE while ( abs ( y1c - y1 ) > e + 1 ) : NEW_LINE INDENT y1 = y1c ; NEW_LINE y1c = y + 0.5 * h * ( f ( x , y ) + f ( x1 , y1 ) ) ; NEW_LINE DEDENT return y1c ; NEW_LINE DEDENT def printFinalValues ( x , xn , y , h ) : NEW_LINE INDENT while ( x < xn ) : NEW_LINE INDENT x1 = x + h ; NEW_LINE y1p = predict ( x , y , h ) ; NEW_LINE y1c = correct ( x , y , x1 , y1p , h ) ; NEW_LINE x = x1 ; NEW_LINE y = y1c ; NEW_LINE DEDENT print ( " The ▁ final ▁ value ▁ of ▁ y ▁ at ▁ x ▁ = " , int ( x ) , " is ▁ : " , y ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 0 ; y = 0.5 ; NEW_LINE xn = 1 ; NEW_LINE h = 0.2 ; NEW_LINE printFinalValues ( x , xn , y , h ) ; NEW_LINE DEDENT


class gfg { static int findMinSwaps ( int arr [ ] , int n ) { int noOfZeroes [ ] = new int [ n ] ; int i , count = 0 ; noOfZeroes [ n - 1 ] = 1 - arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) { noOfZeroes [ i ] = noOfZeroes [ i + 1 ] ; if ( arr [ i ] == 0 ) noOfZeroes [ i ] ++ ; } for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) count += noOfZeroes [ i ] ; } return count ; } public static void main ( String args [ ] ) { int ar [ ] = { 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 } ; System . out . println ( findMinSwaps ( ar , ar . length ) ) ; } }

def findMinSwaps ( arr , n ) : NEW_LINE INDENT noOfZeroes = [ 0 ] * n NEW_LINE count = 0 NEW_LINE noOfZeroes [ n - 1 ] = 1 - arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT noOfZeroes [ i ] = noOfZeroes [ i + 1 ] NEW_LINE if ( arr [ i ] == 0 ) : NEW_LINE INDENT noOfZeroes [ i ] = noOfZeroes [ i ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT count = count + noOfZeroes [ i ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinSwaps ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static void evaluate ( int n ) { if ( n == 1 || n == 2 ) System . out . println ( " No ▁ Pythagoras ▁ " + " Triplet ▁ exists " ) ; else if ( n % 2 == 0 ) { int var = 1 * n * n / 4 ; System . out . print ( " Pythagoras ▁ Triplets ▁ " + " exist ▁ i . e . ▁ " ) ; System . out . print ( n + " ▁ " ) ; System . out . print ( var - 1 + " ▁ " ) ; System . out . println ( var + 1 + " ▁ " ) ; } else if ( n % 2 != 0 ) { int var = 1 * n * n + 1 ; System . out . print ( " Pythagoras ▁ Triplets ▁ " + " exist ▁ i . e . ▁ " ) ; System . out . print ( n + " ▁ " ) ; System . out . print ( var / 2 - 1 + " ▁ " ) ; System . out . println ( var / 2 + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 22 ; evaluate ( n ) ; } }

def evaluate ( n ) : NEW_LINE INDENT if ( n == 1 or n == 2 ) : NEW_LINE INDENT print ( " No ▁ Pythagoras " + " ▁ Triplet ▁ exists " ) ; NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT var = n * n / 4 ; NEW_LINE print ( " Pythagoras ▁ Triplets " + " ▁ exist ▁ i . e . ▁ " , end = " " ) ; NEW_LINE print ( int ( n ) , " ▁ " , int ( var - 1 ) , " ▁ " , int ( var + 1 ) ) ; NEW_LINE DEDENT elif ( n % 2 != 0 ) : NEW_LINE INDENT var = n * n + 1 ; NEW_LINE print ( " Pythagoras ▁ Triplets ▁ " + " exist ▁ i . e . ▁ " , end = " " ) ; NEW_LINE print ( int ( n ) , " ▁ " , int ( var / 2 - 1 ) , " ▁ " , int ( var / 2 ) ) ; NEW_LINE DEDENT DEDENT n = 22 ; NEW_LINE evaluate ( n ) ; NEW_LINE


import java . util . * ; class GFG { static void min_visible_bottles ( int [ ] arr , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } ans = Math . max ( ans , mp . get ( arr [ i ] ) ) ; } System . out . print ( " Minimum ▁ number ▁ of ▁ " + " Visible ▁ Bottles ▁ are : ▁ " + ans + " \n " ) ; } public static void main ( String [ ] args ) { int n = 8 ; int arr [ ] = { 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 } ; min_visible_bottles ( arr , n ) ; } }

def min_visible_bottles ( arr , n ) : NEW_LINE INDENT m = dict ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] = m . get ( arr [ i ] , 0 ) + 1 NEW_LINE ans = max ( ans , m [ arr [ i ] ] ) NEW_LINE DEDENT print ( " Minimum ▁ number ▁ of " , " Visible ▁ Bottles ▁ are : ▁ " , ans ) NEW_LINE DEDENT n = 8 NEW_LINE arr = [ 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 ] NEW_LINE min_visible_bottles ( arr , n ) NEW_LINE


class GFG { static int findMinimumSubsequences ( String A , String B ) { int numberOfSubsequences = 1 ; int sizeOfB = B . length ( ) ; int sizeOfA = A . length ( ) ; int inf = 1000000 ; int [ ] [ ] next = new int [ 26 ] [ sizeOfB ] ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < sizeOfB ; j ++ ) { next [ i ] [ j ] = inf ; } } for ( int i = 0 ; i < sizeOfB ; i ++ ) { next [ B . charAt ( i ) - ' a ' ] [ i ] = i ; } for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = sizeOfB - 2 ; j >= 0 ; j -- ) { if ( next [ i ] [ j ] == inf ) { next [ i ] [ j ] = next [ i ] [ j + 1 ] ; } } } int pos = 0 ; int i = 0 ; while ( i < sizeOfA ) { if ( pos == 0 && next [ A . charAt ( i ) - ' a ' ] [ pos ] == inf ) { numberOfSubsequences = - 1 ; break ; } else if ( pos < sizeOfB && next [ A . charAt ( i ) - ' a ' ] [ pos ] < inf ) { int nextIndex = next [ A . charAt ( i ) - ' a ' ] [ pos ] + 1 ; pos = nextIndex ; i ++ ; } else { numberOfSubsequences ++ ; pos = 0 ; } } return numberOfSubsequences ; } public static void main ( String [ ] args ) { String A = " aacbe " ; String B = " aceab " ; System . out . print ( findMinimumSubsequences ( A , B ) ) ; } }

def findMinimumSubsequences ( A , B ) : NEW_LINE INDENT numberOfSubsequences = 1 NEW_LINE sizeOfB = len ( B ) NEW_LINE sizeOfA = len ( A ) NEW_LINE inf = 1000000 NEW_LINE next = [ [ inf for i in range ( sizeOfB ) ] for i in range ( 26 ) ] NEW_LINE for i in range ( sizeOfB ) : NEW_LINE INDENT next [ ord ( B [ i ] ) - ord ( ' a ' ) ] [ i ] = i NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT for j in range ( sizeOfB - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( next [ i ] [ j ] == inf ) : NEW_LINE INDENT next [ i ] [ j ] = next [ i ] [ j + 1 ] NEW_LINE DEDENT DEDENT DEDENT pos = 0 NEW_LINE i = 0 NEW_LINE while ( i < sizeOfA ) : NEW_LINE INDENT if ( pos == 0 and next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] == inf ) : NEW_LINE INDENT numberOfSubsequences = - 1 NEW_LINE break NEW_LINE DEDENT elif ( pos < sizeOfB and next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] < inf ) : NEW_LINE INDENT nextIndex = next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] + 1 NEW_LINE pos = nextIndex NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT numberOfSubsequences += 1 NEW_LINE pos = 0 NEW_LINE DEDENT DEDENT return numberOfSubsequences NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " aacbe " NEW_LINE B = " aceab " NEW_LINE print ( findMinimumSubsequences ( A , B ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printRoots ( int n ) { double theta = 3.14 * 2 / n ; for ( int k = 0 ; k < n ; k ++ ) { double real = Math . cos ( k * theta ) ; double img = Math . sin ( k * theta ) ; System . out . println ( real ) ; if ( img >= 0 ) System . out . println ( " ▁ + ▁ i ▁ " ) ; else System . out . println ( " ▁ - ▁ i ▁ " ) ; System . out . println ( Math . abs ( img ) ) ; } } public static void main ( String [ ] args ) { printRoots ( 1 ) ; printRoots ( 2 ) ; printRoots ( 3 ) ; } }

import math NEW_LINE def printRoots ( n ) : NEW_LINE INDENT theta = math . pi * 2 / n NEW_LINE for k in range ( 0 , n ) : NEW_LINE INDENT real = math . cos ( k * theta ) NEW_LINE img = math . sin ( k * theta ) NEW_LINE print ( real , end = " ▁ " ) NEW_LINE if ( img >= 0 ) : NEW_LINE INDENT print ( " ▁ + ▁ i ▁ " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ - ▁ i ▁ " , end = " ▁ " ) NEW_LINE DEDENT print ( abs ( img ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT printRoots ( 1 ) NEW_LINE printRoots ( 2 ) NEW_LINE printRoots ( 3 ) NEW_LINE DEDENT


class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int min_element ( int [ ] A , int N ) { int min = A [ 0 ] ; int i ; for ( i = 1 ; i < N ; i ++ ) { if ( min > A [ i ] ) { min = A [ i ] ; } } return min ; } static int countPartitions ( int [ ] A , int N ) { int count = 0 ; int i , j ; for ( i = 0 ; i < N ; i ++ ) { int min_elem = min_element ( A , N ) ; if ( min_elem == INT_MAX ) break ; count ++ ; for ( j = 0 ; j < N ; j ++ ) { if ( A [ j ] % min_elem == 0 ) A [ j ] = INT_MAX ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 } ; int N = arr . length ; System . out . println ( countPartitions ( arr , N ) ) ; } }

import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def countPartitions ( A , N ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT min_elem = min ( A ) ; NEW_LINE if ( min_elem == INT_MAX ) : NEW_LINE INDENT break ; NEW_LINE DEDENT count += 1 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] % min_elem == 0 ) : NEW_LINE INDENT A [ i ] = INT_MAX ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( countPartitions ( arr , N ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int [ ] pref = new int [ 100001 ] ; static int checkPalindrome ( int num ) { String str = String . valueOf ( num ) ; int l = 0 , r = str . length ( ) - 1 ; while ( l < r ) { if ( str . charAt ( l ) != str . charAt ( r ) ) { return 0 ; } l ++ ; r -- ; } return num ; } static void preCompute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + checkPalindrome ( i ) ; } } static void printSum ( int L , int R ) { System . out . print ( pref [ R ] - pref [ L - 1 ] + " \n " ) ; } static void printSumPalindromic ( 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 [ ] [ ] = { { 10 , 13 } , { 12 , 21 } } ; printSumPalindromic ( arr , Q ) ; } }

pref = [ 0 ] * 100001 NEW_LINE def checkPalindrome ( num ) : NEW_LINE INDENT strr = str ( num ) NEW_LINE l = 0 NEW_LINE r = len ( strr ) - 1 NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( strr [ l ] != strr [ r ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT return num NEW_LINE DEDENT def preCompute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + checkPalindrome ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT print ( pref [ R ] - pref [ L - 1 ] ) NEW_LINE DEDENT def printSumPalindromic ( 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 = [ [ 10 , 13 ] , [ 12 , 21 ] ] NEW_LINE printSumPalindromic ( arr , Q ) NEW_LINE


class GFG { static int right = 2 ; static int left = 4 ; static int [ ] [ ] dp = new int [ left ] [ right ] ; static int findSubarraySum ( int ind , int flips , int n , int [ ] a , int k ) { if ( flips > k ) return ( int ) ( - 1e9 ) ; if ( ind == n ) return 0 ; if ( dp [ ind ] [ flips ] != - 1 ) return dp [ ind ] [ flips ] ; int ans = 0 ; ans = Math . max ( 0 , a [ ind ] + findSubarraySum ( ind + 1 , flips , n , a , k ) ) ; ans = Math . max ( ans , - a [ ind ] + findSubarraySum ( ind + 1 , flips + 1 , n , a , k ) ) ; return dp [ ind ] [ flips ] = ans ; } static int findMaxSubarraySum ( int [ ] a , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < k + 1 ; j ++ ) dp [ i ] [ j ] = - 1 ; int ans = ( int ) ( - 1e9 ) ; for ( int i = 0 ; i < n ; i ++ ) ans = Math . max ( ans , findSubarraySum ( i , 0 , n , a , k ) ) ; return ans ; } public static void main ( String [ ] args ) { int [ ] a = { - 1 , - 2 , - 100 , - 10 } ; int n = a . length ; int k = 1 ; System . out . println ( findMaxSubarraySum ( a , n , k ) ) ; } }

import numpy as np NEW_LINE right = 3 ; NEW_LINE left = 6 ; NEW_LINE dp = np . ones ( ( left , right ) ) NEW_LINE dp = - 1 * dp NEW_LINE def findSubarraySum ( ind , flips , n , a , k ) : NEW_LINE INDENT if ( flips > k ) : NEW_LINE INDENT return - 1e9 ; NEW_LINE DEDENT if ( ind == n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ ind ] [ flips ] != - 1 ) : NEW_LINE INDENT return dp [ ind ] [ flips ] ; NEW_LINE DEDENT ans = 0 ; NEW_LINE ans = max ( 0 , a [ ind ] + findSubarraySum ( ind + 1 , flips , n , a , k ) ) ; NEW_LINE ans = max ( ans , - a [ ind ] + findSubarraySum ( ind + 1 , flips + 1 , n , a , k ) ) ; NEW_LINE dp [ ind ] [ flips ] = ans ; NEW_LINE return dp [ ind ] [ flips ] ; NEW_LINE DEDENT def findMaxSubarraySum ( a , n , k ) : NEW_LINE INDENT ans = - 1e9 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = max ( ans , findSubarraySum ( i , 0 , n , a , k ) ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ - 1 , - 2 , - 100 , - 10 ] ; NEW_LINE n = len ( a ) ; NEW_LINE k = 1 ; NEW_LINE print ( findMaxSubarraySum ( a , n , k ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static boolean checkconv ( int a [ ] , int b [ ] , int n ) { int [ ] c = new int [ n ] ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = b [ i ] - a [ i ] ; } Vector < pair > idxs = new Vector < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( c [ i ] != 0 ) idxs . add ( new pair ( i , c [ i ] ) ) ; } for ( int i = 0 ; i < idxs . size ( ) - 1 ; i ++ ) { if ( idxs . get ( i + 1 ) . first - idxs . get ( i ) . first != 1 || idxs . get ( i + 1 ) . second != idxs . get ( i ) . second ) { flag = 1 ; break ; } } return flag == 1 ? false : true ; } static int diffofarrays ( int a [ ] , int b [ ] , int n ) { int [ ] c = new int [ n ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = b [ i ] - a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( c [ i ] != 0 ) { ans = c [ i ] ; break ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 7 , 1 , 4 , 0 , 2 , 2 } ; int B [ ] = { 3 , 7 , 3 , 6 , 2 , 2 , 2 } ; int arr_size = A . length ; if ( checkconv ( A , B , arr_size ) ) { System . out . print ( diffofarrays ( A , B , arr_size ) + " \n " ) ; } else System . out . print ( " NA " + " \n " ) ; } }

def checkconv ( a , b , n ) : NEW_LINE INDENT c = [ 0 ] * n ; flag = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ i ] = b [ i ] - a [ i ] ; NEW_LINE DEDENT idxs = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( c [ i ] != 0 ) : NEW_LINE INDENT idxs . append ( ( i , c [ i ] ) ) ; NEW_LINE DEDENT DEDENT for i in range ( len ( idxs ) - 1 ) : NEW_LINE INDENT if ( idxs [ i + 1 ] [ 0 ] - idxs [ i ] [ 0 ] != 1 or idxs [ i + 1 ] [ 1 ] != idxs [ i ] [ 1 ] ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT return not flag ; NEW_LINE DEDENT def diffofarrays ( a , b , n ) : NEW_LINE INDENT c = [ 0 ] * n ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ i ] = b [ i ] - a [ i ] ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( c [ i ] != 0 ) : NEW_LINE INDENT ans = c [ i ] ; NEW_LINE break ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 7 , 1 , 4 , 0 , 2 , 2 ] ; NEW_LINE B = [ 3 , 7 , 3 , 6 , 2 , 2 , 2 ] ; NEW_LINE arr_size = len ( A ) ; NEW_LINE if ( checkconv ( A , B , arr_size ) ) : NEW_LINE INDENT print ( diffofarrays ( A , B , arr_size ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NA " ) ; NEW_LINE DEDENT DEDENT


public class GFG { public static int getRightMin ( int arr [ ] , int n ) { int min = arr [ 0 ] ; int rightMin = 0 ; int i = 1 ; while ( i < n ) { if ( arr [ i ] == min ) rightMin = i ; i *= 2 ; } i = rightMin + 1 ; while ( i < n && arr [ i ] == min ) { rightMin = i ; i ++ ; } return rightMin ; } public static int getLeftMax ( int arr [ ] , int n ) { int max = arr [ n - 1 ] ; int leftMax = n - 1 ; int i = n - 2 ; while ( i > 0 ) { if ( arr [ i ] == max ) leftMax = i ; i /= 2 ; } i = leftMax - 1 ; while ( i >= 0 && arr [ i ] == max ) { leftMax = i ; i -- ; } return leftMax ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 } ; int n = arr . length ; System . out . println ( " Minimum ▁ left ▁ : ▁ " + 0 ) ; System . out . println ( " Minimum ▁ right ▁ : ▁ " + getRightMin ( arr , n ) ) ; System . out . println ( " Maximum ▁ left ▁ : ▁ " + getLeftMax ( arr , n ) ) ; System . out . println ( " Maximum ▁ right ▁ : ▁ " + ( n - 1 ) ) ; } }

def getRightMin ( arr , n ) : NEW_LINE INDENT min = arr [ 0 ] NEW_LINE rightMin = 0 NEW_LINE i = 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] == min ) : NEW_LINE INDENT rightMin = i NEW_LINE DEDENT i *= 2 NEW_LINE DEDENT i = rightMin + 1 NEW_LINE while ( i < n and arr [ i ] == min ) : NEW_LINE INDENT rightMin = i NEW_LINE i += 1 NEW_LINE DEDENT return rightMin NEW_LINE DEDENT def getLeftMax ( arr , n ) : NEW_LINE INDENT max = arr [ n - 1 ] NEW_LINE leftMax = n - 1 NEW_LINE i = n - 2 NEW_LINE while ( i > 0 ) : NEW_LINE INDENT if ( arr [ i ] == max ) : NEW_LINE INDENT leftMax = i NEW_LINE DEDENT i = int ( i / 2 ) NEW_LINE DEDENT i = leftMax - 1 NEW_LINE while ( i >= 0 and arr [ i ] == max ) : NEW_LINE INDENT leftMax = i NEW_LINE i -= 1 NEW_LINE DEDENT return leftMax NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ left ▁ : " , 0 ) NEW_LINE print ( " Minimum ▁ right ▁ : " , getRightMin ( arr , n ) ) NEW_LINE print ( " Maximum ▁ left ▁ : " , getLeftMax ( arr , n ) ) NEW_LINE print ( " Maximum ▁ right ▁ : " , ( n - 1 ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int MaxNumbers ( int a [ ] , int n ) { int [ ] fre = { 0 , 0 , 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] %= 3 ; fre [ a [ i ] ] ++ ; } int ans = fre [ 0 ] ; int k = Math . min ( fre [ 1 ] , fre [ 2 ] ) ; ans += k ; fre [ 1 ] -= k ; fre [ 2 ] -= k ; ans += fre [ 1 ] / 3 + fre [ 2 ] / 3 ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 } ; int n = a . length ; System . out . println ( MaxNumbers ( a , n ) ) ; } }

def MaxNumbers ( a , n ) : NEW_LINE INDENT fre = [ 0 for i in range ( 3 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] %= 3 NEW_LINE fre [ a [ i ] ] += 1 NEW_LINE DEDENT ans = fre [ 0 ] NEW_LINE k = min ( fre [ 1 ] , fre [ 2 ] ) NEW_LINE ans += k NEW_LINE fre [ 1 ] -= k NEW_LINE fre [ 2 ] -= k NEW_LINE ans += fre [ 1 ] // 3 + fre [ 2 ] // 3 NEW_LINE return ans NEW_LINE DEDENT a = [ 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 ] NEW_LINE n = len ( a ) NEW_LINE print ( MaxNumbers ( a , n ) ) NEW_LINE


class GFG { static void findPoint ( int x1 , int y1 , int x2 , int y2 ) { System . out . println ( " ( " + ( int ) ( 2 * x2 - x1 ) + " , " + ( int ) ( 2 * y2 - y1 ) + " ▁ ) " ) ; } public static void main ( String args [ ] ) { int x1 = 0 , y1 = 0 , x2 = 1 , y2 = 1 ; findPoint ( x1 , y1 , x2 , y2 ) ; } }

def findPoint ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT print ( " ( " , 2 * x2 - x1 , " , " , 2 * y2 - y1 , " ) " ) ; NEW_LINE DEDENT x1 = 0 ; NEW_LINE y1 = 0 ; NEW_LINE x2 = 1 ; NEW_LINE y2 = 1 ; NEW_LINE findPoint ( x1 , y1 , x2 , y2 ) ; NEW_LINE


class shortest_path { static int INF = Integer . MAX_VALUE , N = 4 ; static int minCostRec ( int cost [ ] [ ] , int s , int d ) { if ( s == d || s + 1 == d ) return cost [ s ] [ d ] ; int min = cost [ s ] [ d ] ; for ( int i = s + 1 ; i < d ; i ++ ) { int c = minCostRec ( cost , s , i ) + minCostRec ( cost , i , d ) ; if ( c < min ) min = c ; } return min ; } static int minCost ( int cost [ ] [ ] ) { return minCostRec ( cost , 0 , N - 1 ) ; } public static void main ( String args [ ] ) { int cost [ ] [ ] = { { 0 , 15 , 80 , 90 } , { INF , 0 , 40 , 50 } , { INF , INF , 0 , 70 } , { INF , INF , INF , 0 } } ; System . out . println ( " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ " + N + " ▁ is ▁ " + minCost ( cost ) ) ; } }

global N NEW_LINE N = 4 NEW_LINE def minCostRec ( cost , s , d ) : NEW_LINE INDENT if s == d or s + 1 == d : NEW_LINE INDENT return cost [ s ] [ d ] NEW_LINE DEDENT min = cost [ s ] [ d ] NEW_LINE for i in range ( s + 1 , d ) : NEW_LINE INDENT c = minCostRec ( cost , s , i ) + minCostRec ( cost , i , d ) NEW_LINE if c < min : NEW_LINE INDENT min = c NEW_LINE DEDENT DEDENT return min NEW_LINE DEDENT def minCost ( cost ) : NEW_LINE INDENT return minCostRec ( cost , 0 , N - 1 ) NEW_LINE DEDENT cost = [ [ 0 , 15 , 80 , 90 ] , [ float ( " inf " ) , 0 , 40 , 50 ] , [ float ( " inf " ) , float ( " inf " ) , 0 , 70 ] , [ float ( " inf " ) , float ( " inf " ) , float ( " inf " ) , 0 ] ] NEW_LINE print " The ▁ Minimum ▁ cost ▁ to ▁ reach ▁ station ▁ % d ▁ is ▁ % d " %   \ NEW_LINE INDENT ( N , minCost ( cost ) ) NEW_LINE DEDENT


class GFG { static String getSubSeq ( String s , int n ) { String res = " " ; int cr = 0 ; while ( cr < n ) { char mx = s . charAt ( cr ) ; for ( int i = cr + 1 ; i < n ; i ++ ) { mx = ( char ) Math . max ( mx , s . charAt ( i ) ) ; } int lst = cr ; for ( int i = cr ; i < n ; i ++ ) { if ( s . charAt ( i ) == mx ) { res += s . charAt ( i ) ; lst = i ; } } cr = lst + 1 ; } return res ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; int n = s . length ( ) ; System . out . println ( getSubSeq ( s , n ) ) ; } }

def getSubSeq ( s , n ) : NEW_LINE INDENT res = " " NEW_LINE cr = 0 NEW_LINE while ( cr < n ) : NEW_LINE INDENT mx = s [ cr ] NEW_LINE for i in range ( cr + 1 , n ) : NEW_LINE INDENT mx = max ( mx , s [ i ] ) NEW_LINE DEDENT lst = cr NEW_LINE for i in range ( cr , n ) : NEW_LINE INDENT if ( s [ i ] == mx ) : NEW_LINE INDENT res += s [ i ] NEW_LINE lst = i NEW_LINE DEDENT DEDENT cr = lst + 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE n = len ( s ) NEW_LINE print ( getSubSeq ( s , n ) ) NEW_LINE DEDENT


import java . util . Vector ; class GFG { static int getsum ( int a ) { int r = 0 , sum = 0 ; while ( a > 0 ) { r = a % 10 ; sum = sum + r ; a = a / 10 ; } return sum ; } static void value ( int a , int b , int c ) { int co = 0 , p = 0 ; int no , r = 0 , x = 0 , q = 0 , w = 0 ; Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 1 ; i < 82 ; i ++ ) { no = ( int ) Math . pow ( i , a ) ; no = b * no + c ; if ( no > 0 && no < 1000000000 ) { x = getsum ( no ) ; if ( x == i ) { q ++ ; v . add ( no ) ; w ++ ; } } } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { System . out . print ( v . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int a = 2 , b = 2 , c = - 1 ; value ( a , b , c ) ; } }

def getsum ( a ) : NEW_LINE INDENT r = 0 NEW_LINE sum = 0 NEW_LINE while ( a > 0 ) : NEW_LINE INDENT r = a % 10 NEW_LINE sum = sum + r NEW_LINE a = a // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def value ( a , b , c ) : NEW_LINE INDENT x = 0 NEW_LINE q = 0 NEW_LINE w = 0 NEW_LINE v = [ ] NEW_LINE for i in range ( 1 , 82 ) : NEW_LINE INDENT no = pow ( i , a ) NEW_LINE no = b * no + c NEW_LINE if ( no > 0 and no < 1000000000 ) : NEW_LINE INDENT x = getsum ( no ) NEW_LINE if ( x == i ) : NEW_LINE INDENT q += 1 NEW_LINE v . append ( no ) NEW_LINE w += 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( len ( v ) ) : NEW_LINE INDENT print ( v [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2 NEW_LINE b = 2 NEW_LINE c = - 1 NEW_LINE value ( a , b , c ) NEW_LINE DEDENT


class GFG { static final int MAX = 1000 ; static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int func ( int ind , int g , int dp [ ] [ ] , int n , int a [ ] ) { if ( ind == n ) { if ( g == 1 ) return 1 ; else return 0 ; } if ( dp [ ind ] [ g ] != - 1 ) return dp [ ind ] [ g ] ; int ans = func ( ind + 1 , g , dp , n , a ) + func ( ind + 1 , gcd ( a [ ind ] , g ) , dp , n , a ) ; return dp [ ind ] [ g ] = ans ; } static int countSubsequences ( int a [ ] , int n ) { int dp [ ] [ ] = new int [ n ] [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) dp [ i ] [ j ] = - 1 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) count += func ( i + 1 , a [ i ] , dp , n , a ) ; return count ; } public static void main ( String args [ ] ) { int a [ ] = { 3 , 4 , 8 , 16 } ; int n = a . length ; System . out . println ( countSubsequences ( a , n ) ) ; } }

MAX = 1000 NEW_LINE def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def func ( ind , g , dp , n , a ) : NEW_LINE INDENT if ( ind == n ) : NEW_LINE INDENT if ( g == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( dp [ ind ] [ g ] != - 1 ) : NEW_LINE INDENT return dp [ ind ] [ g ] NEW_LINE DEDENT ans = ( func ( ind + 1 , g , dp , n , a ) + func ( ind + 1 , gcd ( a [ ind ] , g ) , dp , n , a ) ) NEW_LINE dp [ ind ] [ g ] = ans NEW_LINE return dp [ ind ] [ g ] NEW_LINE DEDENT def countSubsequences ( a , n ) : NEW_LINE INDENT dp = [ [ - 1 for i in range ( MAX ) ] for i in range ( n ) ] NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT count += func ( i + 1 , a [ i ] , dp , n , a ) NEW_LINE DEDENT return count NEW_LINE DEDENT a = [ 3 , 4 , 8 , 16 ] NEW_LINE n = len ( a ) NEW_LINE print ( countSubsequences ( a , n ) ) NEW_LINE


class GFG { static class Node { int data ; Node next ; } ; static Node reverse ( Node head , int k ) { Node prev = null ; Node curr = head ; Node temp = null ; Node tail = null ; Node newHead = null ; Node join = null ; int t = 0 ; while ( curr != null ) { t = k ; join = curr ; prev = null ; while ( curr != null && t -- != 0 ) { temp = curr . next ; curr . next = prev ; prev = curr ; curr = temp ; } if ( ( newHead == null ) ) newHead = prev ; if ( tail != null ) tail . next = prev ; tail = join ; } return newHead ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 9 ) ; head = push ( head , 8 ) ; head = push ( head , 7 ) ; head = push ( head , 6 ) ; head = push ( head , 5 ) ; head = push ( head , 4 ) ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; int k = 3 ; System . out . print ( " Given ▁ linked ▁ list ▁ \n " ) ; printList ( head ) ; head = reverse ( head , k ) ; System . out . print ( " \n Reversed ▁ Linked ▁ list ▁ \n " ) ; printList ( head ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def reverse ( head , k ) : NEW_LINE INDENT prev = None NEW_LINE curr = head NEW_LINE temp = None NEW_LINE tail = None NEW_LINE newHead = None NEW_LINE join = None NEW_LINE t = 0 NEW_LINE while ( curr ) : NEW_LINE INDENT t = k NEW_LINE join = curr NEW_LINE prev = None NEW_LINE while ( curr and t > 0 ) : NEW_LINE INDENT temp = curr . next NEW_LINE curr . next = prev NEW_LINE prev = curr NEW_LINE curr = temp NEW_LINE t = t - 1 NEW_LINE DEDENT if ( newHead == None ) : NEW_LINE INDENT newHead = prev NEW_LINE DEDENT if ( tail != None ) : NEW_LINE INDENT tail . next = prev NEW_LINE DEDENT tail = join NEW_LINE DEDENT return newHead NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = head_ref NEW_LINE head_ref = new_node NEW_LINE return head_ref NEW_LINE DEDENT def prList ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , end = " ▁ " ) NEW_LINE node = node . next NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 9 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 7 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 3 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 1 ) NEW_LINE k = 3 NEW_LINE print ( " Given ▁ linked ▁ list ▁ " ) NEW_LINE prList ( head ) NEW_LINE head = reverse ( head , k ) NEW_LINE print ( " \n Reversed ▁ Linked ▁ list ▁ " ) NEW_LINE prList ( head ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int n = 3 ; static class Pair { int first , second ; Pair ( int a , int b ) { first = a ; second = b ; } } static int minSteps ( int arr [ ] [ ] ) { boolean v [ ] [ ] = new boolean [ n ] [ n ] ; Queue < Pair > q = new LinkedList < Pair > ( ) ; q . add ( new Pair ( 0 , 0 ) ) ; int depth = 0 ; while ( q . size ( ) != 0 ) { int x = q . size ( ) ; while ( x -- > 0 ) { Pair y = q . peek ( ) ; int i = y . first , j = y . second ; q . remove ( ) ; if ( v [ i ] [ j ] ) continue ; if ( i == n - 1 && j == n - 1 ) return depth ; v [ i ] [ j ] = true ; if ( i + arr [ i ] [ j ] < n ) q . add ( new Pair ( i + arr [ i ] [ j ] , j ) ) ; if ( j + arr [ i ] [ j ] < n ) q . add ( new Pair ( i , j + arr [ i ] [ j ] ) ) ; } depth ++ ; } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; System . out . println ( minSteps ( arr ) ) ; } }

n = 3 NEW_LINE def minSteps ( arr ) : NEW_LINE INDENT v = [ [ 0 for i in range ( n ) ] for j in range ( n ) ] NEW_LINE q = [ [ 0 , 0 ] ] NEW_LINE depth = 0 NEW_LINE while ( len ( q ) != 0 ) : NEW_LINE INDENT x = len ( q ) NEW_LINE while ( x > 0 ) : NEW_LINE INDENT y = q [ 0 ] NEW_LINE i = y [ 0 ] NEW_LINE j = y [ 1 ] NEW_LINE q . remove ( q [ 0 ] ) NEW_LINE x -= 1 NEW_LINE if ( v [ i ] [ j ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( i == n - 1 and j == n - 1 ) : NEW_LINE INDENT return depth NEW_LINE DEDENT v [ i ] [ j ] = 1 NEW_LINE if ( i + arr [ i ] [ j ] < n ) : NEW_LINE INDENT q . append ( [ i + arr [ i ] [ j ] , j ] ) NEW_LINE DEDENT if ( j + arr [ i ] [ j ] < n ) : NEW_LINE INDENT q . append ( [ i , j + arr [ i ] [ j ] ] ) NEW_LINE DEDENT DEDENT depth += 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ] NEW_LINE print ( minSteps ( arr ) ) NEW_LINE DEDENT


import java . util . Arrays ; class MinSum { static long findMinSum ( long a [ ] , long b [ ] , long n ) { Arrays . sort ( a ) ; Arrays . sort ( b ) ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + Math . abs ( a [ i ] - b [ i ] ) ; return sum ; } public static void main ( String [ ] args ) { long a [ ] = { 4 , 1 , 8 , 7 } ; long b [ ] = { 2 , 3 , 6 , 5 } ; int n = a . length ; System . out . println ( findMinSum ( a , b , n ) ) ; } }

def findMinSum ( a , b , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE b . sort ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + abs ( a [ i ] - b [ i ] ) NEW_LINE DEDENT return sum NEW_LINE DEDENT a = [ 4 , 1 , 8 , 7 ] NEW_LINE b = [ 2 , 3 , 6 , 5 ] NEW_LINE n = len ( a ) NEW_LINE print ( findMinSum ( a , b , n ) ) NEW_LINE


import java . io . * ; class GFG { static int MAX = 100 ; static int binomialCoeff ( int n , int k ) { int C [ ] = new int [ k + 1 ] ; C [ 0 ] = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = Math . min ( i , k ) ; j > 0 ; j -- ) C [ j ] = C [ j ] + C [ j - 1 ] ; } return C [ k ] ; } static int sumOfproduct ( int n ) { return binomialCoeff ( 2 * n , n - 1 ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . println ( sumOfproduct ( n ) ) ; } }

MAX = 100 ; NEW_LINE def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ 0 ] * ( k + 1 ) ; NEW_LINE C [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) , 0 , - 1 ) : NEW_LINE INDENT C [ j ] = C [ j ] + C [ j - 1 ] ; NEW_LINE DEDENT DEDENT return C [ k ] ; NEW_LINE DEDENT def sumOfproduct ( n ) : NEW_LINE INDENT return binomialCoeff ( 2 * n , n - 1 ) ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( sumOfproduct ( n ) ) ; NEW_LINE


class GFG { static int findPairs ( int arr [ ] , int n , int x ) { int l = 0 , r = n - 1 ; int result = 0 ; while ( l < r ) { if ( arr [ l ] + arr [ r ] < x ) { result += ( r - l ) ; l ++ ; } else r -- ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = arr . length ; int x = 7 ; System . out . print ( findPairs ( arr , n , x ) ) ; } }

def findPairs ( arr , n , x ) : NEW_LINE INDENT l = 0 ; r = n - 1 NEW_LINE result = 0 NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( arr [ l ] + arr [ r ] < x ) : NEW_LINE INDENT result += ( r - l ) NEW_LINE l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT r -= 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE x = 7 NEW_LINE print ( findPairs ( arr , n , x ) ) NEW_LINE


import java . util . * ; class GFG { static Vector < String > preProcess ( int n ) { Vector < String > preProcessedCubes = new Vector < > ( ) ; for ( int i = 1 ; i * i * i <= n ; i ++ ) { int iThCube = i * i * i ; String cubeString = String . valueOf ( iThCube ) ; preProcessedCubes . add ( cubeString ) ; } return preProcessedCubes ; } static String findLargestCubeUtil ( String num , Vector < String > preProcessedCubes ) { Collections . reverse ( preProcessedCubes ) ; int totalCubes = preProcessedCubes . size ( ) ; for ( int i = 0 ; i < totalCubes ; i ++ ) { String currCube = preProcessedCubes . get ( i ) ; int digitsInCube = currCube . length ( ) ; int index = 0 ; int digitsInNumber = num . length ( ) ; for ( int j = 0 ; j < digitsInNumber ; j ++ ) { if ( num . charAt ( j ) == currCube . charAt ( index ) ) { index ++ ; } if ( digitsInCube == index ) { return currCube ; } } } return " Not ▁ Possible " ; } static void findLargestCube ( int n ) { Vector < String > preProcessedCubes = preProcess ( n ) ; String num = String . valueOf ( n ) ; String ans = findLargestCubeUtil ( num , preProcessedCubes ) ; System . out . println ( " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from ▁ " + n + " ▁ is ▁ " + ans ) ; } public static void main ( String [ ] args ) { int n ; n = 4125 ; findLargestCube ( n ) ; n = 876 ; findLargestCube ( n ) ; } }

import math as mt NEW_LINE def preProcess ( n ) : NEW_LINE INDENT preProcessedCubes = list ( ) NEW_LINE for i in range ( 1 , mt . ceil ( n ** ( 1. / 3. ) ) ) : NEW_LINE INDENT iThCube = i ** 3 NEW_LINE cubeString = str ( iThCube ) NEW_LINE preProcessedCubes . append ( cubeString ) NEW_LINE DEDENT return preProcessedCubes NEW_LINE DEDENT def findLargestCubeUtil ( num , preProcessedCubes ) : NEW_LINE INDENT preProcessedCubes = preProcessedCubes [ : : - 1 ] NEW_LINE totalCubes = len ( preProcessedCubes ) NEW_LINE for i in range ( totalCubes ) : NEW_LINE INDENT currCube = preProcessedCubes [ i ] NEW_LINE digitsInCube = len ( currCube ) NEW_LINE index = 0 NEW_LINE digitsInNumber = len ( num ) NEW_LINE for j in range ( digitsInNumber ) : NEW_LINE INDENT if ( num [ j ] == currCube [ index ] ) : NEW_LINE INDENT index += 1 NEW_LINE DEDENT if ( digitsInCube == index ) : NEW_LINE INDENT return currCube NEW_LINE DEDENT DEDENT DEDENT return " Not ▁ Possible " NEW_LINE DEDENT def findLargestCube ( n ) : NEW_LINE INDENT preProcessedCubes = preProcess ( n ) NEW_LINE num = str ( n ) NEW_LINE ans = findLargestCubeUtil ( num , preProcessedCubes ) NEW_LINE print ( " Largest ▁ Cube ▁ that ▁ can ▁ be ▁ formed ▁ from " , n , " is " , ans ) NEW_LINE DEDENT n = 4125 NEW_LINE findLargestCube ( n ) NEW_LINE n = 876 NEW_LINE findLargestCube ( n ) NEW_LINE


class GFG { static final int M = 3 ; static final int N = 3 ; static void rotateMatrix ( int matrix [ ] [ ] , int k ) { int temp [ ] = new int [ M ] ; k = k % M ; for ( int i = 0 ; i < N ; i ++ ) { for ( int t = 0 ; t < M - k ; t ++ ) temp [ t ] = matrix [ i ] [ t ] ; for ( int j = M - k ; j < M ; j ++ ) matrix [ i ] [ j - M + k ] = matrix [ i ] [ j ] ; for ( int j = k ; j < M ; j ++ ) matrix [ i ] [ j ] = temp [ j - k ] ; } } static void displayMatrix ( int matrix [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int matrix [ ] [ ] = { { 12 , 23 , 34 } , { 45 , 56 , 67 } , { 78 , 89 , 91 } } ; int k = 2 ; rotateMatrix ( matrix , k ) ; displayMatrix ( matrix ) ; } }

M = 3 NEW_LINE N = 3 NEW_LINE matrix = [ [ 12 , 23 , 34 ] , [ 45 , 56 , 67 ] , [ 78 , 89 , 91 ] ] NEW_LINE def rotateMatrix ( k ) : NEW_LINE INDENT global M , N , matrix NEW_LINE temp = [ 0 ] * M NEW_LINE k = k % M NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for t in range ( 0 , M - k ) : NEW_LINE INDENT temp [ t ] = matrix [ i ] [ t ] NEW_LINE DEDENT for j in range ( M - k , M ) : NEW_LINE INDENT matrix [ i ] [ j - M + k ] = matrix [ i ] [ j ] NEW_LINE DEDENT for j in range ( k , M ) : NEW_LINE INDENT matrix [ i ] [ j ] = temp [ j - k ] NEW_LINE DEDENT DEDENT DEDENT def displayMatrix ( ) : NEW_LINE INDENT global M , N , matrix NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT for j in range ( 0 , M ) : NEW_LINE INDENT print ( " { } ▁ " . format ( matrix [ i ] [ j ] ) , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT k = 2 NEW_LINE rotateMatrix ( k ) NEW_LINE displayMatrix ( ) NEW_LINE


class GFG { static int findMinOperations ( String S ) { S = reverse ( S ) ; int n = S . length ( ) ; int dp [ ] [ ] = new int [ n + 1 ] [ 2 ] ; if ( S . charAt ( 0 ) == '0' ) { dp [ 0 ] [ 0 ] = 0 ; } else { dp [ 0 ] [ 0 ] = 1 ; } dp [ 0 ] [ 1 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( S . charAt ( i ) == '0' ) { dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] = 1 + Math . min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] ) ; } else { dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 0 ] = 1 + Math . min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) ; } } return dp [ n - 1 ] [ 0 ] ; } static String reverse ( String input ) { char [ ] temparray = input . toCharArray ( ) ; int left , right = 0 ; right = temparray . length - 1 ; for ( left = 0 ; left < right ; left ++ , right -- ) { char temp = temparray [ left ] ; temparray [ left ] = temparray [ right ] ; temparray [ right ] = temp ; } return String . valueOf ( temparray ) ; } public static void main ( String [ ] args ) { String S = "100" ; System . out . println ( findMinOperations ( S ) ) ; S = "111" ; System . out . println ( findMinOperations ( S ) ) ; } }

def findMinOperations ( S ) : NEW_LINE INDENT S = S [ : : - 1 ] NEW_LINE n = len ( S ) NEW_LINE dp = [ [ 0 ] * 2 ] * ( n + 1 ) NEW_LINE if ( S [ 0 ] == '0' ) : NEW_LINE INDENT dp [ 0 ] [ 0 ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ 0 ] [ 0 ] = 1 NEW_LINE DEDENT dp [ 0 ] [ 1 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( S [ i ] == '0' ) : NEW_LINE INDENT dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] NEW_LINE dp [ i ] [ 1 ] = 1 + min ( dp [ i - 1 ] [ 1 ] , dp [ i - 1 ] [ 0 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ; NEW_LINE dp [ i ] [ 0 ] = 1 + min ( dp [ i - 1 ] [ 0 ] , dp [ i - 1 ] [ 1 ] ) NEW_LINE DEDENT DEDENT return dp [ n - 1 ] [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "100" NEW_LINE print ( findMinOperations ( S ) ) NEW_LINE S = "111" ; NEW_LINE print ( findMinOperations ( S ) ) NEW_LINE DEDENT


public class GFG { static void EvenOddAbsoluteDifference ( int arr [ ] , int n ) { int even = 0 ; int odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) even = Math . abs ( even - arr [ i ] ) ; else odd = Math . abs ( odd - arr [ i ] ) ; } System . out . println ( " Even ▁ Index ▁ absolute ▁ difference ▁ : ▁ " + even ) ; System . out . println ( " Odd ▁ Index ▁ absolute ▁ difference ▁ : ▁ " + odd ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; EvenOddAbsoluteDifference ( arr , n ) ; } }

def EvenOddAbsoluteDifference ( arr , n ) : NEW_LINE INDENT even = 0 NEW_LINE odd = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT even = abs ( even - arr [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT odd = abs ( odd - arr [ i ] ) ; NEW_LINE DEDENT DEDENT print ( " Even ▁ Index ▁ absolute ▁ difference ▁ : " , even ) NEW_LINE print ( " Odd ▁ Index ▁ absolute ▁ difference ▁ : " , odd ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE EvenOddAbsoluteDifference ( arr , n ) NEW_LINE DEDENT


import java . util . Arrays ; import java . util . HashMap ; public class anagramPairCount { public static void main ( String [ ] args ) { subString ( " kkkk " ) ; } static void subString ( String s ) { HashMap < String , Integer > map = new HashMap < > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { for ( int j = i ; j < s . length ( ) ; j ++ ) { char [ ] valC = s . substring ( i , j + 1 ) . toCharArray ( ) ; Arrays . sort ( valC ) ; String val = new String ( valC ) ; if ( map . containsKey ( val ) ) map . put ( val , map . get ( val ) + 1 ) ; else map . put ( val , 1 ) ; } } int anagramPairCount = 0 ; for ( String key : map . keySet ( ) ) { int n = map . get ( key ) ; anagramPairCount += ( n * ( n - 1 ) ) / 2 ; } System . out . println ( anagramPairCount ) ; } }

def countOfAnagramSubstring ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT sb = ' ' NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sb = ' ' . join ( sorted ( sb + s [ j ] ) ) NEW_LINE mp [ sb ] = mp . get ( sb , 0 ) NEW_LINE mp [ sb ] += 1 NEW_LINE DEDENT DEDENT anas = 0 NEW_LINE for k , v in mp . items ( ) : NEW_LINE INDENT anas += ( v * ( v - 1 ) ) // 2 NEW_LINE DEDENT return anas NEW_LINE DEDENT s = " xyyx " NEW_LINE print ( countOfAnagramSubstring ( s ) ) NEW_LINE


import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static Vector < pair > FindPoints ( int n ) { Vector < pair > v = new Vector < pair > ( ) ; v . add ( new pair ( 0 , 0 ) ) ; v . add ( new pair ( 0 , n ) ) ; v . add ( new pair ( n , 0 ) ) ; v . add ( new pair ( n , n ) ) ; if ( n % 2 == 0 ) v . add ( new pair ( n / 2 , n / 2 ) ) ; return v ; } public static void main ( String [ ] args ) { int N = 8 ; Vector < pair > v = FindPoints ( N ) ; for ( pair i : v ) { System . out . print ( " ( " + i . first + " , ▁ " + i . second + " ) ▁ " ) ; } } }

def FindPoints ( n ) : NEW_LINE INDENT v = [ ] ; NEW_LINE v . append ( [ 0 , 0 ] ) ; NEW_LINE v . append ( [ 0 , n ] ) ; NEW_LINE v . append ( [ n , 0 ] ) ; NEW_LINE v . append ( [ n , n ] ) ; NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT v . append ( [ n // 2 , n // 2 ] ) ; NEW_LINE DEDENT return v ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 ; NEW_LINE v = FindPoints ( N ) ; NEW_LINE for element in v : NEW_LINE INDENT print ( " ( " , element [ 0 ] , " , " , element [ 1 ] , " ) " , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT


import java . io . * ; import java . util . * ; class GFG { static int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += Math . log10 ( i ) ; return ( int ) ( Math . floor ( digits ) ) + 1 ; } public static void main ( String [ ] args ) { System . out . println ( findDigits ( 1 ) ) ; System . out . println ( findDigits ( 5 ) ) ; System . out . println ( findDigits ( 10 ) ) ; System . out . println ( findDigits ( 120 ) ) ; } }

import math NEW_LINE def findDigits ( n ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( n <= 1 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT digits = 0 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT digits += math . log10 ( i ) ; NEW_LINE DEDENT return math . floor ( digits ) + 1 ; NEW_LINE DEDENT print ( findDigits ( 1 ) ) ; NEW_LINE print ( findDigits ( 5 ) ) ; NEW_LINE print ( findDigits ( 10 ) ) ; NEW_LINE print ( findDigits ( 120 ) ) ; NEW_LINE


class GFG { static void maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 , start = 0 , end = 0 , s = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here += a [ i ] ; if ( max_so_far < max_ending_here ) { max_so_far = max_ending_here ; start = s ; end = i ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; s = i + 1 ; } } System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + max_so_far ) ; System . out . println ( " Starting ▁ index ▁ " + start ) ; System . out . println ( " Ending ▁ index ▁ " + end ) ; } public static void main ( String [ ] args ) { int a [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . length ; maxSubArraySum ( a , n ) ; } }

from sys import maxsize NEW_LINE def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = - maxsize - 1 NEW_LINE max_ending_here = 0 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE s = 0 NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT max_ending_here += a [ i ] NEW_LINE if max_so_far < max_ending_here : NEW_LINE INDENT max_so_far = max_ending_here NEW_LINE start = s NEW_LINE end = i NEW_LINE DEDENT if max_ending_here < 0 : NEW_LINE INDENT max_ending_here = 0 NEW_LINE s = i + 1 NEW_LINE DEDENT DEDENT print ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ % d " % ( max_so_far ) ) NEW_LINE print ( " Starting ▁ Index ▁ % d " % ( start ) ) NEW_LINE print ( " Ending ▁ Index ▁ % d " % ( end ) ) NEW_LINE DEDENT a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE maxSubArraySum ( a , len ( a ) ) NEW_LINE


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 MaxLCM ( int n ) { if ( n % 2 != 0 ) System . out . print ( n + " ▁ " + ( n - 1 ) + " ▁ " + ( n - 2 ) ) ; else if ( __gcd ( n , ( n - 3 ) ) == 1 ) System . out . print ( n + " ▁ " + ( n - 1 ) + " ▁ " + ( n - 3 ) ) ; else System . out . print ( ( n - 1 ) + " ▁ " + ( n - 2 ) + " ▁ " + ( n - 3 ) ) ; } public static void main ( String [ ] args ) { int n = 12 ; MaxLCM ( n ) ; } }

from math import gcd NEW_LINE def MaxLCM ( n ) : NEW_LINE INDENT if ( n % 2 != 0 ) : NEW_LINE INDENT print ( n , ( n - 1 ) , ( n - 2 ) ) NEW_LINE DEDENT elif ( gcd ( n , ( n - 3 ) ) == 1 ) : NEW_LINE INDENT print ( n , ( n - 1 ) , ( n - 3 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ( n - 1 ) , ( n - 2 ) , ( n - 3 ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE MaxLCM ( n ) NEW_LINE DEDENT


import java . util . LinkedList ; import java . util . Queue ; import java . util . Stack ; public class Reverse_k_element_queue { static Queue < Integer > queue ; static void reverseQueueFirstKElements ( int k ) { if ( queue . isEmpty ( ) == true || k > queue . size ( ) ) return ; if ( k <= 0 ) return ; Stack < Integer > stack = new Stack < Integer > ( ) ; for ( int i = 0 ; i < k ; i ++ ) { stack . push ( queue . peek ( ) ) ; queue . remove ( ) ; } while ( ! stack . empty ( ) ) { queue . add ( stack . peek ( ) ) ; stack . pop ( ) ; } for ( int i = 0 ; i < queue . size ( ) - k ; i ++ ) { queue . add ( queue . peek ( ) ) ; queue . remove ( ) ; } } static void Print ( ) { while ( ! queue . isEmpty ( ) ) { System . out . print ( queue . peek ( ) + " ▁ " ) ; queue . remove ( ) ; } } public static void main ( String args [ ] ) { queue = new LinkedList < Integer > ( ) ; queue . add ( 10 ) ; queue . add ( 20 ) ; queue . add ( 30 ) ; queue . add ( 40 ) ; queue . add ( 50 ) ; queue . add ( 60 ) ; queue . add ( 70 ) ; queue . add ( 80 ) ; queue . add ( 90 ) ; queue . add ( 100 ) ; int k = 5 ; reverseQueueFirstKElements ( k ) ; Print ( ) ; } }

from queue import Queue NEW_LINE def reverseQueueFirstKElements ( k , Queue ) : NEW_LINE INDENT if ( Queue . empty ( ) == True or k > Queue . qsize ( ) ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( k <= 0 ) : NEW_LINE INDENT return NEW_LINE DEDENT Stack = [ ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT Stack . append ( Queue . queue [ 0 ] ) NEW_LINE Queue . get ( ) NEW_LINE DEDENT while ( len ( Stack ) != 0 ) : NEW_LINE INDENT Queue . put ( Stack [ - 1 ] ) NEW_LINE Stack . pop ( ) NEW_LINE DEDENT for i in range ( Queue . qsize ( ) - k ) : NEW_LINE INDENT Queue . put ( Queue . queue [ 0 ] ) NEW_LINE Queue . get ( ) NEW_LINE DEDENT DEDENT def Print ( Queue ) : NEW_LINE INDENT while ( not Queue . empty ( ) ) : NEW_LINE INDENT print ( Queue . queue [ 0 ] , end = " ▁ " ) NEW_LINE Queue . get ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Queue = Queue ( ) NEW_LINE Queue . put ( 10 ) NEW_LINE Queue . put ( 20 ) NEW_LINE Queue . put ( 30 ) NEW_LINE Queue . put ( 40 ) NEW_LINE Queue . put ( 50 ) NEW_LINE Queue . put ( 60 ) NEW_LINE Queue . put ( 70 ) NEW_LINE Queue . put ( 80 ) NEW_LINE Queue . put ( 90 ) NEW_LINE Queue . put ( 100 ) NEW_LINE k = 5 NEW_LINE reverseQueueFirstKElements ( k , Queue ) NEW_LINE Print ( Queue ) NEW_LINE DEDENT


class GFG { static boolean productSumDivisible ( int n , int size ) { int sum = 0 , product = 1 ; while ( n > 0 ) { if ( size % 2 == 0 ) { product *= n % 10 ; } else { sum += n % 10 ; } n = n / 10 ; size -- ; } if ( product % sum == 0 ) { return true ; } return false ; } public static void main ( String [ ] args ) { int n = 1234 ; int len = 4 ; if ( productSumDivisible ( n , len ) ) { System . out . println ( " TRUE " ) ; } else { System . out . println ( " FALSE " ) ; } } }

def productSumDivisible ( n , size ) : NEW_LINE INDENT sum = 0 NEW_LINE product = 1 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( size % 2 == 0 ) : NEW_LINE INDENT product *= n % 10 NEW_LINE DEDENT else : NEW_LINE INDENT sum += n % 10 NEW_LINE DEDENT n = n // 10 NEW_LINE size -= 1 NEW_LINE DEDENT if ( product % sum == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1234 NEW_LINE len = 4 NEW_LINE if ( productSumDivisible ( n , len ) ) : NEW_LINE INDENT print ( " TRUE " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " FALSE " ) NEW_LINE DEDENT DEDENT


public class AAA { static int get_last_two_digit ( long N ) { if ( N <= 10 ) { long ans = 0 , fac = 1 ; for ( int i = 1 ; i <= N ; i ++ ) { fac = fac * i ; ans += fac ; } return ( int ) ans % 100 ; } else return 13 ; } public static void main ( String [ ] args ) { long N = 1 ; for ( N = 1 ; N <= 10 ; N ++ ) System . out . println ( " For ▁ N ▁ = ▁ " + N + " ▁ : ▁ " + get_last_two_digit ( N ) ) ; } }

def get_last_two_digit ( N ) : NEW_LINE INDENT if N <= 10 : NEW_LINE INDENT ans = 0 NEW_LINE fac = 1 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT fac = fac * i NEW_LINE ans += fac NEW_LINE DEDENT ans = ans % 100 NEW_LINE return ans NEW_LINE DEDENT else : NEW_LINE INDENT return 13 NEW_LINE DEDENT DEDENT N = 1 NEW_LINE for N in range ( 1 , 11 ) : NEW_LINE INDENT print ( " For ▁ N ▁ = ▁ " , N , " : ▁ " , get_last_two_digit ( N ) , sep = ' ▁ ' ) NEW_LINE DEDENT


public class GFG { static boolean isPerfectSquare ( int n ) { for ( int sum = 0 , i = 1 ; sum < n ; i += 2 ) { sum += i ; if ( sum == n ) return true ; } return false ; } public static void main ( String args [ ] ) { if ( isPerfectSquare ( 35 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " NO " ) ; if ( isPerfectSquare ( 49 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isPerfectSquare ( n ) : NEW_LINE INDENT i = 1 NEW_LINE the_sum = 0 NEW_LINE while the_sum < n : NEW_LINE INDENT the_sum += i NEW_LINE if the_sum == n : NEW_LINE INDENT return True NEW_LINE DEDENT i += 2 NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT print ( ' Yes ' ) if isPerfectSquare ( 35 ) else print ( ' NO ' ) NEW_LINE print ( ' Yes ' ) if isPerfectSquare ( 49 ) else print ( ' NO ' ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int MAXN = 100005 ; static int spf [ ] = new int [ MAXN ] ; static int sum_digits [ ] = new int [ MAXN ] ; static boolean isValid [ ] = new boolean [ MAXN ] ; static int ans [ ] = new int [ MAXN ] ; static void Smallest_prime_factor ( ) { for ( int i = 1 ; 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 += 2 ) if ( spf [ i ] == i ) for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } static int Digit_Sum ( int copy ) { int d = 0 ; while ( copy > 0 ) { d += copy % 10 ; copy /= 10 ; } return d ; } static void Sum_Of_All_Digits ( ) { for ( int n = 2 ; n < MAXN ; n ++ ) { sum_digits [ n ] = sum_digits [ n / spf [ n ] ] + Digit_Sum ( spf [ n ] ) ; if ( Digit_Sum ( n ) == sum_digits [ n ] ) isValid [ n ] = true ; } for ( int n = 2 ; n < MAXN ; n ++ ) { if ( isValid [ n ] ) ans [ n ] = 1 ; ans [ n ] += ans [ n - 1 ] ; } } public static void main ( String [ ] args ) { Smallest_prime_factor ( ) ; Sum_Of_All_Digits ( ) ; int l , r ; l = 2 ; r = 3 ; System . out . println ( " Valid ▁ numbers ▁ in ▁ the ▁ range ▁ " + l + " ▁ " + r + " ▁ are ▁ " + ( ans [ r ] - ans [ l - 1 ] ) ) ; l = 2 ; r = 10 ; System . out . println ( " Valid ▁ numbers ▁ in ▁ the ▁ range ▁ " + l + " ▁ " + r + " ▁ are ▁ " + ( ans [ r ] - ans [ l - 1 ] ) ) ; } }

MAXN = 100005 NEW_LINE spf = [ 0 ] * MAXN NEW_LINE sum_digits = [ 0 ] * MAXN NEW_LINE isValid = [ 0 ] * MAXN NEW_LINE ans = [ 0 ] * MAXN NEW_LINE def Smallest_prime_factor ( ) : NEW_LINE INDENT for i in range ( 1 , 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 i = 3 NEW_LINE while i * i <= MAXN : 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 i += 2 NEW_LINE DEDENT DEDENT def Digit_Sum ( copy ) : NEW_LINE INDENT d = 0 NEW_LINE while ( copy ) : NEW_LINE INDENT d += copy % 10 NEW_LINE copy //= 10 NEW_LINE DEDENT return d NEW_LINE DEDENT def Sum_Of_All_Digits ( ) : NEW_LINE INDENT for n in range ( 2 , MAXN ) : NEW_LINE INDENT sum_digits [ n ] = ( sum_digits [ n // spf [ n ] ] + Digit_Sum ( spf [ n ] ) ) NEW_LINE if ( Digit_Sum ( n ) == sum_digits [ n ] ) : NEW_LINE INDENT isValid [ n ] = True NEW_LINE DEDENT DEDENT for n in range ( 2 , MAXN ) : NEW_LINE INDENT if ( isValid [ n ] ) : NEW_LINE INDENT ans [ n ] = 1 NEW_LINE DEDENT ans [ n ] += ans [ n - 1 ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Smallest_prime_factor ( ) NEW_LINE Sum_Of_All_Digits ( ) NEW_LINE l = 2 NEW_LINE r = 3 NEW_LINE print ( " Valid ▁ numbers ▁ in ▁ the ▁ range " , l , r , " are " , ans [ r ] - ans [ l - 1 ] ) NEW_LINE l = 2 NEW_LINE r = 10 NEW_LINE print ( " Valid ▁ numbers ▁ in ▁ the ▁ range " , l , r , " are " , ans [ r ] - ans [ l - 1 ] ) NEW_LINE DEDENT


class Test { static int sumOfDigit ( int K ) { int sod = 0 ; while ( K != 0 ) { sod += K % 10 ; K /= 10 ; } return sod ; } static int totalNumbersWithSpecificDifference ( int N , int diff ) { int low = 1 , high = N ; while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( mid - sumOfDigit ( mid ) < diff ) low = mid + 1 ; else high = mid - 1 ; } return ( N - high ) ; } public static void main ( String args [ ] ) { int N = 13 ; int diff = 2 ; System . out . println ( totalNumbersWithSpecificDifference ( N , diff ) ) ; } }

def sumOfDigit ( K ) : NEW_LINE INDENT sod = 0 NEW_LINE while ( K ) : NEW_LINE INDENT sod = sod + K % 10 NEW_LINE K = K // 10 NEW_LINE DEDENT return sod NEW_LINE DEDENT def totalNumbersWithSpecificDifference ( N , diff ) : NEW_LINE INDENT low = 1 NEW_LINE high = N NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( mid - sumOfDigit ( mid ) < diff ) : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT return ( N - high ) NEW_LINE DEDENT N = 13 NEW_LINE diff = 2 NEW_LINE print ( totalNumbersWithSpecificDifference ( N , diff ) ) NEW_LINE


import java . util . * ; class GFG { static int N = 1000001 ; static Vector < Integer > [ ] v = new Vector [ N ] ; static void preprocess ( ) { for ( int i = 1 ; i < N ; i ++ ) { int num = i ; for ( int j = 2 ; j <= Math . sqrt ( num ) ; j ++ ) { if ( num % j == 0 ) { v [ i ] . add ( j ) ; while ( num % j == 0 ) { num = num / j ; } } } if ( num > 2 ) v [ i ] . add ( num ) ; } } static int query ( int number , int n ) { return v [ number ] . get ( n - 1 ) ; } public static void main ( String [ ] args ) { for ( int i = 0 ; i < N ; i ++ ) v [ i ] = new Vector < Integer > ( ) ; preprocess ( ) ; int number = 6 , n = 1 ; System . out . print ( query ( number , n ) + " \n " ) ; number = 210 ; n = 3 ; System . out . print ( query ( number , n ) + " \n " ) ; number = 210 ; n = 2 ; System . out . print ( query ( number , n ) + " \n " ) ; number = 60 ; n = 2 ; System . out . print ( query ( number , n ) + " \n " ) ; } }

from math import sqrt , ceil NEW_LINE N = 10001 NEW_LINE v = [ [ ] for i in range ( N ) ] NEW_LINE def preprocess ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT num = i NEW_LINE for j in range ( 2 , ceil ( sqrt ( num ) ) + 1 ) : NEW_LINE INDENT if ( num % j == 0 ) : NEW_LINE INDENT v [ i ] . append ( j ) NEW_LINE while ( num % j == 0 ) : NEW_LINE INDENT num = num // j NEW_LINE DEDENT DEDENT DEDENT if ( num > 2 ) : NEW_LINE INDENT v [ i ] . append ( num ) NEW_LINE DEDENT DEDENT DEDENT def query ( number , n ) : NEW_LINE INDENT return v [ number ] [ n - 1 ] NEW_LINE DEDENT preprocess ( ) NEW_LINE number = 6 NEW_LINE n = 1 NEW_LINE print ( query ( number , n ) ) NEW_LINE number = 210 NEW_LINE n = 3 NEW_LINE print ( query ( number , n ) ) NEW_LINE number = 210 NEW_LINE n = 2 NEW_LINE print ( query ( number , n ) ) NEW_LINE number = 60 NEW_LINE n = 2 NEW_LINE print ( query ( number , n ) ) NEW_LINE


import java . util . Collections ; import java . util . Vector ; class GFG { static void findfactors ( int n ) { Vector < Integer > vec = new Vector < Integer > ( ) ; for ( int i = 1 ; i * i <= n ; i ++ ) { if ( n % i == 0 ) { vec . add ( i ) ; vec . add ( n / i ) ; } } Collections . sort ( vec ) ; System . out . println ( " All ▁ the ▁ factors ▁ are ▁ - > ▁ " ) ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { System . out . print ( vec . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; int maxProduct = 1 ; boolean flag = true ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { for ( int j = i ; j < vec . size ( ) ; j ++ ) { for ( int k = j ; k < vec . size ( ) ; k ++ ) { int y = n - vec . get ( i ) - vec . get ( j ) - vec . get ( k ) ; if ( y <= 0 ) { break ; } if ( n % y == 0 ) { flag = false ; maxProduct = Math . max ( vec . get ( i ) * vec . get ( j ) * vec . get ( k ) * y , maxProduct ) ; } } } } if ( flag == false ) { System . out . println ( " Product ▁ is ▁ - > ▁ " + maxProduct ) ; } else { System . out . println ( " Not ▁ possible " ) ; } } public static void main ( String [ ] args ) { int n ; n = 50 ; findfactors ( n ) ; } }

from math import * NEW_LINE def findfactors ( n ) : NEW_LINE INDENT vec = [ ] NEW_LINE for i in range ( 1 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT vec . append ( i ) NEW_LINE vec . append ( n // i ) NEW_LINE DEDENT DEDENT vec . sort ( ) NEW_LINE print ( " All ▁ the ▁ factors ▁ are ▁ - > ▁ " , end = " " ) NEW_LINE for i in range ( len ( vec ) ) : NEW_LINE INDENT print ( vec [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE maxProduct = 1 NEW_LINE flag = 1 NEW_LINE for i in range ( 0 , len ( vec ) ) : NEW_LINE INDENT for j in range ( i , len ( vec ) ) : NEW_LINE INDENT for k in range ( j , len ( vec ) ) : NEW_LINE INDENT y = n - vec [ i ] - vec [ j ] - vec [ k ] NEW_LINE if y <= 0 : NEW_LINE INDENT break NEW_LINE DEDENT if n % y == 0 : NEW_LINE INDENT flag = 0 NEW_LINE maxProduct = max ( vec [ i ] * vec [ j ] * vec [ k ] * y , maxProduct ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if flag == 0 : NEW_LINE INDENT print ( " Product ▁ is ▁ - ▁ > " , maxProduct ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 50 NEW_LINE findfactors ( n ) NEW_LINE DEDENT


class GFG { static int sumOfDigits ( int x ) { int sum = 0 ; while ( x != 0 ) { sum += x % 10 ; x = x / 10 ; } return sum ; } static int countNumbers ( int l , int r ) { int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { if ( i % 2 == 0 && sumOfDigits ( i ) % 3 == 0 ) count ++ ; } return count ; } public static void main ( String args [ ] ) { int l = 1000 , r = 6000 ; System . out . println ( countNumbers ( l , r ) ) ; } }

def sumOfDigits ( x ) : NEW_LINE INDENT sum = 0 NEW_LINE while x != 0 : NEW_LINE INDENT sum += x % 10 NEW_LINE x = x // 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT def countNumbers ( l , r ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if i % 2 == 0 and sumOfDigits ( i ) % 3 == 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT l = 1000 ; r = 6000 NEW_LINE print ( countNumbers ( l , r ) ) NEW_LINE


class GFG { public static void balancedString ( String str ) { int count = 0 , i ; int n = str . length ( ) ; for ( i = 0 ; i < n ; i ++ ) { if ( str . charAt ( i ) == ' ( ' ) { System . out . print ( str . charAt ( i ) ) ; count ++ ; } else if ( str . charAt ( i ) == ' ) ' && count != 0 ) { System . out . print ( str . charAt ( i ) ) ; count -- ; } else if ( str . charAt ( i ) != ' ) ' ) System . out . print ( str . charAt ( i ) ) ; } if ( count != 0 ) for ( i = 0 ; i < count ; i ++ ) System . out . print ( " ) " ) ; } public static void main ( String args [ ] ) { String str = " gau ) ra ) v ( ku ( mar ( rajput ) ) " ; balancedString ( str ) ; } }

def balancedString ( str ) : NEW_LINE INDENT count , i = 0 , 0 NEW_LINE n = len ( str ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( str [ i ] == ' ( ' ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE count += 1 NEW_LINE DEDENT elif ( str [ i ] == ' ) ' and count != 0 ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE count -= 1 NEW_LINE DEDENT elif ( str [ i ] != ' ) ' ) : NEW_LINE INDENT print ( str [ i ] , end = " " ) NEW_LINE DEDENT DEDENT if ( count != 0 ) : NEW_LINE INDENT for i in range ( count ) : NEW_LINE INDENT print ( " ) " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " gau ) ra ) v ( ku ( mar ( rajput ) ) " NEW_LINE balancedString ( str ) NEW_LINE DEDENT


public class GFG { static String findNthNo ( int n ) { String res = " " ; while ( n >= 1 ) { if ( ( n & 1 ) == 1 ) { res = res + "4" ; n = ( n - 1 ) / 2 ; } else { res = res + "7" ; n = ( n - 2 ) / 2 ; } } StringBuilder sb = new StringBuilder ( res ) ; sb . reverse ( ) ; return new String ( sb ) ; } public static void main ( String args [ ] ) { int n = 13 ; System . out . print ( findNthNo ( n ) ) ; } }

def reverse ( s ) : NEW_LINE INDENT if len ( s ) == 0 : NEW_LINE INDENT return s NEW_LINE DEDENT else : NEW_LINE INDENT return reverse ( s [ 1 : ] ) + s [ 0 ] NEW_LINE DEDENT DEDENT def findNthNo ( n ) : NEW_LINE INDENT res = " " ; NEW_LINE while ( n >= 1 ) : NEW_LINE INDENT if ( n & 1 ) : NEW_LINE INDENT res = res + "4" ; NEW_LINE n = ( int ) ( ( n - 1 ) / 2 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT res = res + "7" ; NEW_LINE n = ( int ) ( ( n - 2 ) / 2 ) ; NEW_LINE DEDENT DEDENT return reverse ( res ) ; NEW_LINE DEDENT n = 13 ; NEW_LINE print ( findNthNo ( n ) ) ; NEW_LINE


class GFG { static int sumOfTheSeries ( int n ) { int sum_n = ( n * ( n + 1 ) / 2 ) ; int sum_sq_n = ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ; return ( sum_n + sum_sq_n ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . printf ( " Sum ▁ = ▁ % d " , sumOfTheSeries ( n ) ) ; } }

def sumOfTheSeries ( n ) : NEW_LINE INDENT sum_n = int ( ( n * ( n + 1 ) / 2 ) ) ; NEW_LINE sum_sq_n = int ( ( n * ( n + 1 ) / 2 ) * ( 2 * n + 1 ) / 3 ) NEW_LINE return ( sum_n + sum_sq_n ) ; NEW_LINE DEDENT n = 5 NEW_LINE ans = sumOfTheSeries ( n ) NEW_LINE print ( ans ) NEW_LINE


class RearrangeArray { void rightrotate ( int arr [ ] , int n , int outofplace , int cur ) { int tmp = arr [ cur ] ; for ( int i = cur ; i > outofplace ; i -- ) arr [ i ] = arr [ i - 1 ] ; arr [ outofplace ] = tmp ; } void rearrange ( int arr [ ] , int n ) { int outofplace = - 1 ; for ( int index = 0 ; index < n ; index ++ ) { if ( outofplace >= 0 ) { if ( ( ( arr [ index ] >= 0 ) && ( arr [ outofplace ] < 0 ) ) || ( ( arr [ index ] < 0 ) && ( arr [ outofplace ] >= 0 ) ) ) { rightrotate ( arr , n , outofplace , index ) ; if ( index - outofplace > 2 ) outofplace = outofplace + 2 ; else outofplace = - 1 ; } } if ( outofplace == - 1 ) { if ( ( ( arr [ index ] >= 0 ) && ( ( index & 0x01 ) == 0 ) ) || ( ( arr [ index ] < 0 ) && ( index & 0x01 ) == 1 ) ) outofplace = index ; } } } void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { RearrangeArray rearrange = new RearrangeArray ( ) ; int arr [ ] = { - 5 , - 2 , 5 , 2 , 4 , 7 , 1 , 8 , 0 , - 8 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ " ) ; rearrange . printArray ( arr , n ) ; rearrange . rearrange ( arr , n ) ; System . out . println ( " RearrangeD ▁ array ▁ is ▁ " ) ; rearrange . printArray ( arr , n ) ; } }

def rightRotate ( arr , n , outOfPlace , cur ) : NEW_LINE INDENT temp = arr [ cur ] NEW_LINE for i in range ( cur , outOfPlace , - 1 ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] NEW_LINE DEDENT arr [ outOfPlace ] = temp NEW_LINE return arr NEW_LINE DEDENT def rearrange ( arr , n ) : NEW_LINE INDENT outOfPlace = - 1 NEW_LINE for index in range ( n ) : NEW_LINE INDENT if ( outOfPlace >= 0 ) : NEW_LINE INDENT if ( ( arr [ index ] >= 0 and arr [ outOfPlace ] < 0 ) or ( arr [ index ] < 0 and arr [ outOfPlace ] >= 0 ) ) : NEW_LINE INDENT arr = rightRotate ( arr , n , outOfPlace , index ) NEW_LINE if ( index - outOfPlace > 2 ) : NEW_LINE INDENT outOfPlace += 2 NEW_LINE DEDENT else : NEW_LINE INDENT outOfPlace = - 1 NEW_LINE DEDENT DEDENT DEDENT if ( outOfPlace == - 1 ) : NEW_LINE INDENT if ( ( arr [ index ] >= 0 and index % 2 == 0 ) or ( arr [ index ] < 0 and index % 2 == 1 ) ) : NEW_LINE INDENT outOfPlace = index NEW_LINE DEDENT DEDENT DEDENT return arr NEW_LINE DEDENT arr = [ - 5 , - 2 , 5 , 2 , 4 , 7 , 1 , 8 , 0 , - 8 ] NEW_LINE print ( " Given ▁ Array ▁ is : " ) NEW_LINE print ( arr ) NEW_LINE print ( " \n Rearranged ▁ array ▁ is : " ) NEW_LINE print ( rearrange ( arr , len ( arr ) ) ) NEW_LINE


import java . util . Arrays ; class Test { static int maxModValue ( int arr [ ] , int n ) { int ans = 0 ; Arrays . sort ( arr ) ; for ( int j = n - 2 ; j >= 0 ; -- j ) { if ( ans >= arr [ j ] ) break ; if ( arr [ j ] == arr [ j + 1 ] ) continue ; for ( int i = 2 * arr [ j ] ; i <= arr [ n - 1 ] + arr [ j ] ; i += arr [ j ] ) { int ind = Arrays . binarySearch ( arr , i ) ; if ( ind < 0 ) ind = Math . abs ( ind + 1 ) ; else { while ( arr [ ind ] == i ) { ind -- ; if ( ind == 0 ) { ind = - 1 ; break ; } } ind ++ ; } ans = Math . max ( ans , arr [ ind - 1 ] % arr [ j ] ) ; } } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 4 , 5 , 9 , 11 } ; System . out . println ( maxModValue ( arr , arr . length ) ) ; } }

def maxModValue ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE arr = sorted ( arr ) NEW_LINE for j in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( ans >= arr [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( arr [ j ] == arr [ j + 1 ] ) : NEW_LINE INDENT continue NEW_LINE DEDENT i = 2 * arr [ j ] NEW_LINE while ( i <= arr [ n - 1 ] + arr [ j ] ) : NEW_LINE INDENT ind = 0 NEW_LINE for k in arr : NEW_LINE INDENT if k >= i : NEW_LINE INDENT ind = arr . index ( k ) NEW_LINE DEDENT DEDENT ans = max ( ans , arr [ ind - 1 ] % arr [ j ] ) NEW_LINE i += arr [ j ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 4 , 5 , 9 , 11 ] NEW_LINE n = 5 NEW_LINE print ( maxModValue ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } static Node getDeepestLeftLeafNode ( Node root ) { if ( root == null ) return null ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; Node result = null ; while ( ! q . isEmpty ( ) ) { Node temp = q . peek ( ) ; q . remove ( ) ; if ( temp . left != null ) { q . add ( temp . left ) ; if ( temp . left . left == null && temp . left . right == null ) result = temp . left ; } if ( temp . right != null ) q . add ( temp . right ) ; } return result ; } public static void main ( String [ ] args ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . right . left = newNode ( 5 ) ; root . right . right = newNode ( 6 ) ; root . right . left . right = newNode ( 7 ) ; root . right . right . right = newNode ( 8 ) ; root . right . left . right . left = newNode ( 9 ) ; root . right . right . right . right = newNode ( 10 ) ; Node result = getDeepestLeftLeafNode ( root ) ; if ( result != null ) System . out . println ( " Deepest ▁ Left ▁ Leaf ▁ Node ▁ : : ▁ " + result . data ) ; else System . out . println ( " No ▁ result , ▁ " + " left ▁ leaf ▁ not ▁ found " ) ; } }

_MIN = - 2147483648 NEW_LINE _MAX = 2147483648 NEW_LINE class newnode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getDeepestLeftLeafNode ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return None NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE result = None NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE if ( not temp . left . left and not temp . left . right ) : NEW_LINE INDENT result = temp . left NEW_LINE DEDENT DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newnode ( 1 ) NEW_LINE root . left = newnode ( 2 ) NEW_LINE root . right = newnode ( 3 ) NEW_LINE root . left . Left = newnode ( 4 ) NEW_LINE root . right . left = newnode ( 5 ) NEW_LINE root . right . right = newnode ( 6 ) NEW_LINE root . right . left . right = newnode ( 7 ) NEW_LINE root . right . right . right = newnode ( 8 ) NEW_LINE root . right . left . right . left = newnode ( 9 ) NEW_LINE root . right . right . right . right = newnode ( 10 ) NEW_LINE result = getDeepestLeftLeafNode ( root ) NEW_LINE if result : NEW_LINE INDENT print ( " Deepest ▁ Left ▁ Leaf ▁ Node ▁ : : " , result . data ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ result , ▁ Left ▁ leaf ▁ not ▁ found " ) NEW_LINE DEDENT DEDENT


import java . util . * ; import java . lang . * ; class GFG { static int MAX_CHAR = 26 ; public static void commonCharacters ( String str [ ] , int n ) { Boolean [ ] prim = new Boolean [ MAX_CHAR ] ; Arrays . fill ( prim , new Boolean ( true ) ) ; for ( int i = 0 ; i < n ; i ++ ) { Boolean [ ] sec = new Boolean [ MAX_CHAR ] ; Arrays . fill ( sec , new Boolean ( false ) ) ; for ( int j = 0 ; j < str [ i ] . length ( ) ; j ++ ) { if ( prim [ str [ i ] . charAt ( j ) - ' a ' ] ) sec [ str [ i ] . charAt ( j ) - ' a ' ] = true ; } System . arraycopy ( sec , 0 , prim , 0 , MAX_CHAR ) ; } for ( int i = 0 ; i < 26 ; i ++ ) if ( prim [ i ] ) { System . out . print ( Character . toChars ( i + 97 ) ) ; System . out . print ( " ▁ " ) ; } } public static void main ( String [ ] args ) { String str [ ] = { " geeksforgeeks " , " gemkstones " , " acknowledges " , " aguelikes " } ; int n = str . length ; commonCharacters ( str , n ) ; } }

MAX_CHAR = 26 NEW_LINE def commonCharacters ( strings , n ) : NEW_LINE INDENT prim = [ True ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT sec = [ False ] * MAX_CHAR NEW_LINE for j in range ( len ( strings [ i ] ) ) : NEW_LINE INDENT if ( prim [ ord ( strings [ i ] [ j ] ) - ord ( ' a ' ) ] ) : NEW_LINE INDENT sec [ ord ( strings [ i ] [ j ] ) - ord ( ' a ' ) ] = True NEW_LINE DEDENT DEDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT prim [ i ] = sec [ i ] NEW_LINE DEDENT DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( prim [ i ] ) : NEW_LINE INDENT print ( " % c ▁ " % ( i + ord ( ' a ' ) ) , end = " " ) NEW_LINE DEDENT DEDENT DEDENT strings = [ " geeksforgeeks " , " gemkstones " , " acknowledges " , " aguelikes " ] NEW_LINE n = len ( strings ) NEW_LINE commonCharacters ( strings , n ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int [ ] solve ( int [ ] arr , int n ) { int [ ] b = new int [ n ] ; int p = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { b [ p ] = arr [ i -- ] ; if ( i >= 0 ) b [ n - 1 - p ] = arr [ i ] ; p ++ ; } return b ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int [ ] b = solve ( arr , n ) ; System . out . println ( Arrays . toString ( b ) ) ; } }

def solve ( arr , n ) : NEW_LINE INDENT b = [ 0 for i in range ( n ) ] NEW_LINE p = 0 NEW_LINE i = n - 1 NEW_LINE while i >= 0 : NEW_LINE INDENT b [ p ] = arr [ i ] NEW_LINE i -= 1 NEW_LINE if ( i >= 0 ) : NEW_LINE INDENT b [ n - 1 - p ] = arr [ i ] NEW_LINE DEDENT p += 1 NEW_LINE i -= 1 NEW_LINE DEDENT return b NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE b = solve ( arr , n ) NEW_LINE print ( b ) NEW_LINE


class CkeckSorted { static int arraySortedOrNot ( int arr [ ] , int n ) { if ( n == 1 || n == 0 ) return 1 ; if ( arr [ n - 1 ] < arr [ n - 2 ] ) return 0 ; return arraySortedOrNot ( arr , n - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 20 , 23 , 23 , 45 , 78 , 88 } ; int n = arr . length ; if ( arraySortedOrNot ( arr , n ) != 0 ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def arraySortedOrNot ( arr ) : NEW_LINE INDENT n = len ( arr ) NEW_LINE if n == 1 or n == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return arr [ 0 ] <= arr [ 1 ] and arraySortedOrNot ( arr [ 1 : ] ) NEW_LINE DEDENT arr = [ 20 , 23 , 23 , 45 , 78 , 88 ] NEW_LINE if arraySortedOrNot ( arr ) : print ( " Yes " ) NEW_LINE else : print ( " No " ) NEW_LINE


class GFG { static int MAX = 128 ; static boolean isVowel ( char x ) { return ( x == ' a ' || x == ' e ' || x == ' i ' || x == ' o ' || x == ' u ' || x == ' A ' || x == ' E ' || x == ' I ' || x == ' O ' || x == ' U ' ) ; } static int KDistinctVowel ( String s , int k ) { int n = s . length ( ) ; int [ ] c = new int [ MAX ] ; int result = - 1 ; for ( int i = 0 , j = - 1 ; i < n ; ++ i ) { char x = s . charAt ( i ) ; if ( isVowel ( x ) ) { if ( ++ c [ x ] == 1 ) { -- k ; } } while ( k < 0 ) { x = s . charAt ( ++ j ) ; if ( isVowel ( x ) ) { if ( -- c [ x ] == 0 ) { ++ k ; } } } if ( k == 0 ) { result = Math . max ( result , i - j ) ; } } return result ; } public static void main ( String [ ] args ) { String s = " tHeracEBetwEEntheTwo " ; int k = 1 ; System . out . println ( KDistinctVowel ( s , k ) ) ; } }

MAX = 128 NEW_LINE def isVowel ( x ) : NEW_LINE INDENT return ( x == ' a ' or x == ' e ' or x == ' i ' or x == ' o ' or x == ' u ' or x == ' A ' or x == ' E ' or x == ' I ' or x == ' O ' or x == ' U ' ) NEW_LINE DEDENT def KDistinctVowel ( c , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE c = [ 0 for i in range ( MAX ) ] NEW_LINE result = - 1 NEW_LINE j = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = s [ i ] NEW_LINE if isVowel ( x ) : NEW_LINE INDENT c [ ord ( x ) ] += 1 NEW_LINE if c [ ord ( x ) ] == 1 : NEW_LINE INDENT k -= 1 NEW_LINE DEDENT DEDENT while k < 0 : NEW_LINE INDENT j += 1 NEW_LINE x = s [ j ] NEW_LINE if isVowel ( x ) : NEW_LINE INDENT c [ ord ( x ) ] -= 1 NEW_LINE k += 1 NEW_LINE DEDENT DEDENT if k == 0 : NEW_LINE INDENT result = max ( result , i - j ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT s = " tHeracEBetwEEntheTwo " NEW_LINE k = 1 NEW_LINE print ( KDistinctVowel ( s , k ) ) NEW_LINE


import java . util . Arrays ; class GFG { static void maxPerimeter ( int arr [ ] , int n ) { arr = arrRevSort ( arr ) ; int maxi = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( arr [ i ] < arr [ i + 1 ] + arr [ i + 2 ] ) { maxi = Math . max ( maxi , arr [ i ] + arr [ i + 1 ] + arr [ i + 2 ] ) ; break ; } } if ( maxi > 0 ) { System . out . println ( " Maximum ▁ Perimeter ▁ is : ▁ " + maxi ) ; } else { System . out . println ( " Triangle ▁ formation ▁ is ▁ not ▁ possible . " ) ; } } static int [ ] arrRevSort ( int [ ] arr ) { Arrays . sort ( arr , 0 , arr . length ) ; int j = arr . length - 1 ; for ( int i = 0 ; i < arr . length / 2 ; i ++ , j -- ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } return arr ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 6 , 1 , 6 , 5 , 8 , 4 } ; maxPerimeter ( arr1 , 6 ) ; int arr2 [ ] = { 2 , 20 , 7 , 55 , 1 , 33 , 12 , 4 } ; maxPerimeter ( arr2 , 8 ) ; int arr3 [ ] = { 33 , 6 , 20 , 1 , 8 , 12 , 5 , 55 , 4 , 9 } ; maxPerimeter ( arr3 , 10 ) ; } }

def maxPerimeter ( arr ) : NEW_LINE INDENT maxi = 0 NEW_LINE n = len ( arr ) NEW_LINE arr . sort ( reverse = True ) NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT if arr [ i ] < ( arr [ i + 1 ] + arr [ i + 2 ] ) : NEW_LINE INDENT maxi = max ( maxi , arr [ i ] + arr [ i + 1 ] + arr [ i + 2 ] ) NEW_LINE break NEW_LINE DEDENT DEDENT if ( maxi == 0 ) : NEW_LINE INDENT return " Triangle ▁ formation ▁ is ▁ not ▁ possible " NEW_LINE DEDENT else : NEW_LINE INDENT return " Maximum ▁ Perimeter ▁ is : ▁ " + str ( maxi ) NEW_LINE DEDENT DEDENT def main ( ) : NEW_LINE INDENT arr1 = [ 6 , 1 , 6 , 5 , 8 , 4 ] NEW_LINE a = maxPerimeter ( arr1 ) NEW_LINE print ( a ) NEW_LINE arr2 = [ 2 , 20 , 7 , 55 , 1 , 33 , 12 , 4 ] NEW_LINE a = maxPerimeter ( arr2 ) NEW_LINE print ( a ) NEW_LINE arr3 = [ 33 , 6 , 20 , 1 , 8 , 12 , 5 , 55 , 4 , 9 ] NEW_LINE a = maxPerimeter ( arr3 ) NEW_LINE print ( a ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import java . io . * ; class GFG { static long hexadecagonalNum ( long n ) { return ( ( 14 * n * n ) - 12 * n ) / 2 ; } public static void main ( String [ ] args ) { long n = 5 ; System . out . println ( n + " th ▁ " + " Hexadecagonal ▁ number ▁ : ▁ " + hexadecagonalNum ( n ) ) ; n = 9 ; System . out . println ( n + " th ▁ " + " Hexadecagonal ▁ number ▁ : ▁ " + hexadecagonalNum ( n ) ) ; } }

def hexadecagonalNum ( n ) : NEW_LINE INDENT return ( ( 14 * n * n ) - 12 * n ) // 2 NEW_LINE DEDENT n = 5 NEW_LINE print ( " % sth ▁ Hexadecagonal ▁ number ▁ : ▁ " % n , hexadecagonalNum ( n ) ) NEW_LINE n = 9 NEW_LINE print ( " % sth ▁ Hexadecagonal ▁ number ▁ : ▁ " % n , hexadecagonalNum ( n ) ) NEW_LINE