class GFG { static long findDigits ( int n , int b ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double M_PI = 3.141592 ; double M_E = 2.7182 ; double x = ( ( n * Math . log10 ( n / M_E ) + Math . log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( Math . log10 ( b ) ) ; return ( long ) ( Math . floor ( x ) + 1 ) ; } public static void main ( String [ ] args ) { System . out . print ( findDigits ( 4 , 16 ) + " \n " ) ; System . out . print ( findDigits ( 5 , 8 ) + " \n " ) ; System . out . print ( findDigits ( 12 , 16 ) + " \n " ) ; System . out . print ( findDigits ( 19 , 13 ) + " \n " ) ; } }

from math import log10 , floor NEW_LINE def findDigits ( n , b ) : NEW_LINE INDENT if ( n < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT M_PI = 3.141592 NEW_LINE M_E = 2.7182 NEW_LINE if ( n <= 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT x = ( ( n * log10 ( n / M_E ) + log10 ( 2 * M_PI * n ) / 2.0 ) ) / ( log10 ( b ) ) NEW_LINE return floor ( x ) + 1 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( findDigits ( 4 , 16 ) ) NEW_LINE print ( findDigits ( 5 , 8 ) ) NEW_LINE print ( findDigits ( 12 , 16 ) ) NEW_LINE print ( findDigits ( 19 , 13 ) ) NEW_LINE DEDENT


class GFG { static final int N = 100 ; static int dp [ ] [ ] = new int [ N ] [ 3 ] ; static int v [ ] [ ] = new int [ N ] [ 3 ] ; static int findCnt ( String s , int i , int r ) { if ( i == s . length ( ) ) { if ( r == 0 ) return 1 ; else return 0 ; } if ( v [ i ] [ r ] == 1 ) return dp [ i ] [ r ] ; v [ i ] [ r ] = 1 ; dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( s . charAt ( i ) - '0' ) ) % 3 ) + findCnt ( s , i + 1 , r ) ; return dp [ i ] [ r ] ; } public static void main ( String [ ] args ) { String s = "11" ; System . out . print ( findCnt ( s , 0 , 0 ) - 1 ) ; } }

import numpy as np NEW_LINE N = 100 NEW_LINE dp = np . zeros ( ( N , 3 ) ) ; NEW_LINE v = np . zeros ( ( N , 3 ) ) ; NEW_LINE def findCnt ( s , i , r ) : NEW_LINE INDENT if ( i == len ( s ) ) : NEW_LINE INDENT if ( r == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT if ( v [ i ] [ r ] ) : NEW_LINE INDENT return dp [ i ] [ r ] ; NEW_LINE DEDENT v [ i ] [ r ] = 1 ; NEW_LINE dp [ i ] [ r ] = findCnt ( s , i + 1 , ( r * 2 + ( ord ( s [ i ] ) - ord ( '0' ) ) ) % 3 ) +   \ NEW_LINE INDENT findCnt ( s , i + 1 , r ) ; NEW_LINE DEDENT return dp [ i ] [ r ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "11" ; NEW_LINE print ( findCnt ( s , 0 , 0 ) - 1 ) ; NEW_LINE DEDENT


class GFG { static void check ( int n ) { if ( n % 2 == 1 && n > 5 ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } public static void main ( String [ ] args ) { int a = 3 ; int b = 7 ; check ( a ) ; check ( b ) ; } }

def check ( n ) : NEW_LINE INDENT if n % 2 == 1 and n > 5 : NEW_LINE INDENT print ( ' YES ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' NO ' ) NEW_LINE DEDENT DEDENT def main ( ) : NEW_LINE INDENT a = 3 NEW_LINE b = 7 NEW_LINE check ( a ) NEW_LINE check ( b ) NEW_LINE DEDENT main ( ) NEW_LINE


import java . io . * ; class GFG { static int MAX = 100001 ; static int prefix [ ] = new int [ MAX ] ; static void sieve_modified ( ) { for ( int i = 1 ; i < MAX ; i += 2 ) { for ( int j = i ; j < MAX ; j += i ) prefix [ j ] += i ; } for ( int i = 1 ; i < MAX ; i ++ ) prefix [ i ] += prefix [ i - 1 ] ; } static int sumOddFactors ( int L , int R ) { return ( prefix [ R ] - prefix [ L - 1 ] ) ; } public static void main ( String [ ] args ) { sieve_modified ( ) ; int l = 6 , r = 10 ; System . out . println ( sumOddFactors ( l , r ) ) ; } }

MAX = 100001 ; NEW_LINE prefix = [ 0 ] * MAX ; NEW_LINE def sieve_modified ( ) : NEW_LINE INDENT for i in range ( 1 , MAX , 2 ) : NEW_LINE INDENT for j in range ( i , MAX , i ) : NEW_LINE INDENT prefix [ j ] += i ; NEW_LINE DEDENT DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT prefix [ i ] += prefix [ i - 1 ] ; NEW_LINE DEDENT DEDENT def sumOddFactors ( L , R ) : NEW_LINE INDENT return ( prefix [ R ] - prefix [ L - 1 ] ) ; NEW_LINE DEDENT sieve_modified ( ) ; NEW_LINE l = 6 ; NEW_LINE r = 10 ; NEW_LINE print ( sumOddFactors ( l , r ) ) ; NEW_LINE


class GFG { static String countnndSay ( int n ) { if ( n == 1 ) return "1" ; if ( n == 2 ) return "11" ; String str = "11" ; for ( int i = 3 ; i <= n ; i ++ ) { str += ' $ ' ; int len = str . length ( ) ; int cnt = 1 ; String tmp = " " ; char [ ] arr = str . toCharArray ( ) ; for ( int j = 1 ; j < len ; j ++ ) { if ( arr [ j ] != arr [ j - 1 ] ) { tmp += cnt + 0 ; tmp += arr [ j - 1 ] ; cnt = 1 ; } else cnt ++ ; } str = tmp ; } return str ; } public static void main ( String [ ] args ) { int N = 3 ; System . out . println ( countnndSay ( N ) ) ; } }

def countnndSay ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return "1" NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return "11" NEW_LINE DEDENT s = "11" NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT s += ' $ ' NEW_LINE l = len ( s ) NEW_LINE cnt = 1 NEW_LINE tmp = " " NEW_LINE for j in range ( 1 , l ) : NEW_LINE INDENT if ( s [ j ] != s [ j - 1 ] ) : NEW_LINE INDENT tmp += str ( cnt + 0 ) NEW_LINE tmp += s [ j - 1 ] NEW_LINE cnt = 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 1 NEW_LINE DEDENT DEDENT s = tmp NEW_LINE DEDENT return s ; NEW_LINE DEDENT N = 3 NEW_LINE print ( countnndSay ( N ) ) NEW_LINE


import java . util . * ; class GFG { static void disp ( int row_no , int block ) { System . out . print ( row_no * block ) ; } static int row ( int ht , int h ) { return ht / h ; } static void calculate ( int l , int w , int h , int a , int ht ) { int no_block = ( 4 * a ) / l ; int row_no ; if ( h < w ) row_no = row ( ht , w ) ; else row_no = row ( ht , h ) ; disp ( row_no , no_block ) ; } public static void main ( String [ ] args ) { int l = 50 , w = 20 , h = 35 ; int a = 700 ; int ht = 140 ; calculate ( l , w , h , a , ht ) ; } }

def disp ( row_no , block ) : NEW_LINE INDENT print ( row_no * block ) NEW_LINE DEDENT def row ( ht , h ) : NEW_LINE INDENT return ht // h NEW_LINE DEDENT def calculate ( l , w , h , a , ht ) : NEW_LINE INDENT no_block = ( 4 * a ) // l NEW_LINE if ( h < w ) : NEW_LINE INDENT row_no = row ( ht , w ) NEW_LINE DEDENT else : NEW_LINE INDENT row_no = row ( ht , h ) NEW_LINE DEDENT disp ( row_no , no_block ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT l = 50 NEW_LINE w = 20 NEW_LINE h = 35 NEW_LINE a = 700 NEW_LINE ht = 140 NEW_LINE calculate ( l , w , h , a , ht ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void fib ( int f [ ] , int N ) { f [ 1 ] = 1 ; f [ 2 ] = 1 ; for ( int i = 3 ; i <= N ; i ++ ) f [ i ] = f [ i - 1 ] + f [ i - 2 ] ; } static void fiboTriangle ( int n ) { int N = n * ( n + 1 ) / 2 ; int f [ ] = new int [ N + 1 ] ; fib ( f , N ) ; int fiboNum = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) System . out . print ( f [ fiboNum ++ ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String args [ ] ) { int n = 5 ; fiboTriangle ( n ) ; } }

def fib ( f , N ) : NEW_LINE INDENT f [ 1 ] = 1 NEW_LINE f [ 2 ] = 1 NEW_LINE for i in range ( 3 , N + 1 ) : NEW_LINE INDENT f [ i ] = f [ i - 1 ] + f [ i - 2 ] NEW_LINE DEDENT DEDENT def fiboTriangle ( n ) : NEW_LINE INDENT N = n * ( n + 1 ) // 2 NEW_LINE f = [ 0 ] * ( N + 1 ) NEW_LINE fib ( f , N ) NEW_LINE fiboNum = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT fiboNum = fiboNum + 1 NEW_LINE print ( f [ fiboNum ] , " ▁ " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE fiboTriangle ( n ) NEW_LINE


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

def cubeSum ( n ) : NEW_LINE INDENT return ( n * n * ( 2 * n * n - 1 ) ) NEW_LINE DEDENT print ( cubeSum ( 4 ) ) NEW_LINE


import java . util . * ; class GFG { static void prime_seive ( int MAX , int [ ] isprime , Vector < Integer > prime ) { int i = 2 ; while ( i * i <= MAX ) { if ( isprime [ i ] == 1 ) { prime . add ( i ) ; for ( int j = i * 2 ; j < MAX ; j += i ) { isprime [ j ] = 0 ; } } i += 1 ; } } static int printNearest ( int N ) { int MAX = ( int ) 1e6 ; int [ ] isprime = new int [ MAX ] ; for ( int i = 2 ; i < MAX ; i ++ ) isprime [ i ] = 1 ; Vector < Integer > prime = new Vector < Integer > ( ) ; int i = 0 ; prime_seive ( MAX , isprime , prime ) ; while ( isprime [ N ] == 0 ) { N += prime . get ( i ) ; i += 1 ; } return N ; } public static void main ( String [ ] args ) { int N = 8 ; System . out . printf ( " % d " , printNearest ( N ) ) ; } }

def prime_seive ( MAX , isprime , prime ) : NEW_LINE INDENT i = 2 NEW_LINE while ( i * i <= MAX ) : NEW_LINE INDENT if ( isprime [ i ] == 1 ) : NEW_LINE INDENT prime . append ( i ) NEW_LINE for j in range ( i * 2 , MAX , i ) : NEW_LINE INDENT isprime [ j ] = 0 NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def printNearest ( N ) : NEW_LINE INDENT MAX = 10 ** 6 NEW_LINE isprime = [ 1 ] * MAX NEW_LINE isprime [ 0 ] = isprime [ 1 ] = 0 NEW_LINE prime = [ ] NEW_LINE i = 0 NEW_LINE prime_seive ( MAX , isprime , prime ) NEW_LINE while not isprime [ N ] : NEW_LINE INDENT N += prime [ i ] NEW_LINE i += 1 NEW_LINE DEDENT return N NEW_LINE DEDENT N = 8 NEW_LINE print ( printNearest ( N ) ) NEW_LINE


import java . util . * ; class GFG { static int sumKRepeating ( int arr [ ] , int n , int k ) { int sum = 0 ; Vector < Boolean > visited = new Vector < Boolean > ( ) ; for ( int i = 0 ; i < n ; i ++ ) visited . add ( false ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited . get ( i ) == true ) continue ; int count = 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { count ++ ; visited . add ( j , true ) ; } } if ( count == k ) sum += arr [ i ] ; } return sum ; } public static void main ( String args [ ] ) { int arr [ ] = { 9 , 9 , 10 , 11 , 8 , 8 , 9 , 8 } ; int n = arr . length ; int k = 3 ; System . out . println ( sumKRepeating ( arr , n , k ) ) ; } }

def sumKRepeating ( arr , n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE visited = [ False for i in range ( n ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( visited [ i ] == True ) : NEW_LINE INDENT continue NEW_LINE DEDENT count = 1 NEW_LINE for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE visited [ j ] = True NEW_LINE DEDENT DEDENT if ( count == k ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 9 , 10 , 11 , 8 , 8 , 9 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( sumKRepeating ( arr , n , k ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void midpoint ( int x1 , int x2 , int y1 , int y2 ) { System . out . print ( ( x1 + x2 ) / 2 + " ▁ , ▁ " + ( y1 + y2 ) / 2 ) ; } public static void main ( String [ ] args ) { int x1 = - 1 , y1 = 2 ; int x2 = 3 , y2 = - 6 ; midpoint ( x1 , x2 , y1 , y2 ) ; } }

def midpoint ( x1 , x2 , y1 , y2 ) : NEW_LINE INDENT print ( ( x1 + x2 ) // 2 , " ▁ , ▁ " , ( y1 + y2 ) // 2 ) NEW_LINE DEDENT x1 , y1 , x2 , y2 = - 1 , 2 , 3 , - 6 NEW_LINE midpoint ( x1 , x2 , y1 , y2 ) NEW_LINE


class GFG { static int bankNotes ( int A , int B , int S , int N ) { int numerator = S - ( B * N ) ; int denominator = A - B ; if ( numerator % denominator == 0 ) return ( numerator / denominator ) ; return - 1 ; } public static void main ( String [ ] args ) { int A = 1 , B = 2 , S = 7 , N = 5 ; System . out . print ( bankNotes ( A , B , S , N ) ) ; } }

def bankNotes ( A , B , S , N ) : NEW_LINE INDENT numerator = S - ( B * N ) NEW_LINE denominator = A - B NEW_LINE if ( numerator % denominator == 0 ) : NEW_LINE INDENT return ( numerator // denominator ) NEW_LINE DEDENT return - 1 NEW_LINE DEDENT A , B , S , N = 1 , 2 , 7 , 5 NEW_LINE print ( bankNotes ( A , B , S , N ) ) NEW_LINE


class GFG { public static void main ( String [ ] args ) { int matrix [ ] [ ] = new int [ 5 ] [ 5 ] , row_index , column_index , x = 0 , size = 5 ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { matrix [ row_index ] [ column_index ] = ++ x ; } } System . out . printf ( " The ▁ matrix ▁ is \n " ) ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { System . out . printf ( " % d\t " , matrix [ row_index ] [ column_index ] ) ; } System . out . printf ( " \n " ) ; } System . out . printf ( " \n Main ▁ diagonal ▁ elements ▁ are : \n " ) ; for ( row_index = 0 ; row_index < size ; row_index ++ ) { for ( column_index = 0 ; column_index < size ; column_index ++ ) { if ( row_index == column_index ) { System . out . printf ( " % d , ▁ " , matrix [ row_index ] [ column_index ] ) ; } } } } }

if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT matrix = [ [ 0 for i in range ( 5 ) ] for j in range ( 5 ) ] NEW_LINE x , size = 0 , 5 ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT x += 1 ; NEW_LINE matrix [ row_index ] [ column_index ] = x ; NEW_LINE DEDENT DEDENT print ( " The ▁ matrix ▁ is " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " \t " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT print ( " \n Main ▁ diagonal ▁ elements ▁ are : " ) ; NEW_LINE for row_index in range ( size ) : NEW_LINE INDENT for column_index in range ( size ) : NEW_LINE INDENT if ( row_index == column_index ) : NEW_LINE INDENT print ( matrix [ row_index ] [ column_index ] , end = " \t " ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT


import java . util . * ; class GFG { static int make_permutation ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) ans += Math . abs ( i + 1 - arr [ i ] ) ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 3 , 8 , 1 , 1 } ; int n = arr . length ; System . out . print ( make_permutation ( arr , n ) ) ; } }

def make_permutation ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += abs ( i + 1 - arr [ i ] ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 3 , 8 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( make_permutation ( arr , n ) ) ; NEW_LINE DEDENT


class GFG { final static int NO_OF_CHARS = 256 ; static int max_distinct_char ( String str , int n ) { int count [ ] = new int [ NO_OF_CHARS ] ; for ( int i = 0 ; i < n ; i ++ ) { count [ str . charAt ( i ) ] ++ ; } int max_distinct = 0 ; for ( int i = 0 ; i < NO_OF_CHARS ; i ++ ) { if ( count [ i ] != 0 ) { max_distinct ++ ; } } return max_distinct ; } static int smallesteSubstr_maxDistictChar ( String str ) { int n = str . length ( ) ; int max_distinct = max_distinct_char ( str , n ) ; int minl = n ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { String subs = null ; if ( i < j ) subs = str . substring ( i , j ) ; else subs = str . substring ( j , i ) ; int subs_lenght = subs . length ( ) ; int sub_distinct_char = max_distinct_char ( subs , subs_lenght ) ; if ( subs_lenght < minl && max_distinct == sub_distinct_char ) { minl = subs_lenght ; } } } return minl ; } static public void main ( String [ ] args ) { String str = " AABBBCBB " ; int len = smallesteSubstr_maxDistictChar ( str ) ; System . out . println ( " ▁ The ▁ length ▁ of ▁ the ▁ smallest ▁ substring " + " ▁ consisting ▁ of ▁ maximum ▁ distinct ▁ " + " characters ▁ : ▁ " + len ) ; } }

NO_OF_CHARS = 256 NEW_LINE def max_distinct_char ( str , n ) : NEW_LINE INDENT count = [ 0 ] * NO_OF_CHARS NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ ord ( str [ i ] ) ] += 1 NEW_LINE DEDENT max_distinct = 0 NEW_LINE for i in range ( NO_OF_CHARS ) : NEW_LINE INDENT if ( count [ i ] != 0 ) : NEW_LINE INDENT max_distinct += 1 NEW_LINE DEDENT DEDENT return max_distinct NEW_LINE DEDENT def smallesteSubstr_maxDistictChar ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE max_distinct = max_distinct_char ( str , n ) NEW_LINE minl = n NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT subs = str [ i : j ] NEW_LINE subs_lenght = len ( subs ) NEW_LINE sub_distinct_char = max_distinct_char ( subs , subs_lenght ) NEW_LINE if ( subs_lenght < minl and max_distinct == sub_distinct_char ) : NEW_LINE INDENT minl = subs_lenght NEW_LINE DEDENT DEDENT DEDENT return minl NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " AABBBCBB " NEW_LINE l = smallesteSubstr_maxDistictChar ( str ) ; NEW_LINE print ( " The ▁ length ▁ of ▁ the ▁ smallest ▁ substring " , " consisting ▁ of ▁ maximum ▁ distinct " , " characters ▁ : " , l ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static int maxHist ( int R , int C , int row [ ] ) { Stack < Integer > result = new Stack < Integer > ( ) ; int top_val ; int max_area = 0 ; int area = 0 ; int i = 0 ; while ( i < C ) { if ( result . empty ( ) || row [ result . peek ( ) ] <= row [ i ] ) result . push ( i ++ ) ; else { top_val = row [ result . peek ( ) ] ; result . pop ( ) ; area = top_val * i ; if ( ! result . empty ( ) ) area = top_val * ( i - result . peek ( ) - 1 ) ; max_area = Math . max ( area , max_area ) ; } } while ( ! result . empty ( ) ) { top_val = row [ result . peek ( ) ] ; result . pop ( ) ; area = top_val * i ; if ( ! result . empty ( ) ) area = top_val * ( i - result . peek ( ) - 1 ) ; max_area = Math . max ( area , max_area ) ; } return max_area ; } static int maxRectangle ( int R , int C , int A [ ] [ ] ) { int result = maxHist ( R , C , A [ 0 ] ) ; for ( int i = 1 ; i < R ; i ++ ) { for ( int j = 0 ; j < C ; j ++ ) if ( A [ i ] [ j ] == 1 ) A [ i ] [ j ] += A [ i - 1 ] [ j ] ; result = Math . max ( result , maxHist ( R , C , A [ i ] ) ) ; } return result ; } public static void main ( String [ ] args ) { int R = 4 ; int C = 4 ; int A [ ] [ ] = { { 0 , 1 , 1 , 0 } , { 1 , 1 , 1 , 1 } , { 1 , 1 , 1 , 1 } , { 1 , 1 , 0 , 0 } , } ; System . out . print ( " Area ▁ of ▁ maximum ▁ rectangle ▁ is ▁ " + maxRectangle ( R , C , A ) ) ; } }

R = 4 NEW_LINE C = 4 NEW_LINE INDENT result = [ ] NEW_LINE top_val = 0 NEW_LINE max_area = 0 NEW_LINE area = 0 NEW_LINE i = 0 NEW_LINE while ( i < C ) : NEW_LINE INDENT if ( len ( result ) == 0 ) or ( row [ result [ 0 ] ] <= row [ i ] ) : NEW_LINE INDENT result . append ( i ) NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT top_val = row [ result [ 0 ] ] NEW_LINE result . pop ( 0 ) NEW_LINE area = top_val * i NEW_LINE if ( len ( result ) ) : NEW_LINE INDENT area = top_val * ( i - result [ 0 ] - 1 ) NEW_LINE DEDENT max_area = max ( area , max_area ) NEW_LINE DEDENT DEDENT while ( len ( result ) ) : NEW_LINE INDENT top_val = row [ result [ 0 ] ] NEW_LINE result . pop ( 0 ) NEW_LINE area = top_val * i NEW_LINE if ( len ( result ) ) : NEW_LINE INDENT area = top_val * ( i - result [ 0 ] - 1 ) NEW_LINE DEDENT max_area = max ( area , max_area ) NEW_LINE DEDENT return max_area NEW_LINE DEDENT def maxRectangle ( A ) : NEW_LINE INDENT result = maxHist ( A [ 0 ] ) NEW_LINE for i in range ( 1 , R ) : NEW_LINE INDENT for j in range ( C ) : NEW_LINE INDENT if ( A [ i ] [ j ] ) : NEW_LINE INDENT A [ i ] [ j ] += A [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT result = max ( result , maxHist ( A [ i ] ) ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ [ 0 , 1 , 1 , 0 ] , [ 1 , 1 , 1 , 1 ] , [ 1 , 1 , 1 , 1 ] , [ 1 , 1 , 0 , 0 ] ] NEW_LINE print ( " Area ▁ of ▁ maximum ▁ rectangle ▁ is " , maxRectangle ( A ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean hasCrest ( int arr [ ] , int n , int L , int R ) { int [ ] present = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { present [ i ] = 0 ; } for ( int i = 1 ; i <= n - 2 ; i ++ ) { if ( ( arr [ i ] <= arr [ i + 1 ] ) && ( arr [ i ] <= arr [ i - 1 ] ) ) { present [ i ] = 1 ; } } for ( int i = 1 ; i < n ; i ++ ) { present [ i ] += present [ i - 1 ] ; } if ( present [ L ] == present [ R - 1 ] ) return true ; return false ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 } ; int N = arr . length ; int L = 2 ; int R = 6 ; if ( hasCrest ( arr , N , L , R ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def hasCrest ( arr , n , L , R ) : NEW_LINE INDENT present = [ 0 ] * n ; NEW_LINE for i in range ( 1 , n - 2 + 1 ) : NEW_LINE INDENT if ( ( arr [ i ] <= arr [ i + 1 ] ) and ( arr [ i ] <= arr [ i - 1 ] ) ) : NEW_LINE INDENT present [ i ] = 1 ; NEW_LINE DEDENT DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT present [ i ] += present [ i - 1 ] ; NEW_LINE DEDENT if ( present [ L ] == present [ R - 1 ] ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 1 , 3 , 5 , 12 , 11 , 7 , 9 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE L = 2 ; NEW_LINE R = 6 ; NEW_LINE if ( hasCrest ( arr , N , L , R ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


class GfG { static int findAnswer ( int X , int N ) { return ( N - 1 ) * 9 + X ; } public static void main ( String [ ] args ) { int X = 7 , N = 43 ; System . out . println ( findAnswer ( X , N ) ) ; } }

def findAnswer ( X , N ) : NEW_LINE INDENT return ( N - 1 ) * 9 + X ; NEW_LINE DEDENT X = 7 ; NEW_LINE N = 43 ; NEW_LINE print ( findAnswer ( X , N ) ) ; NEW_LINE


import java . util . * ; class GFG { static List < Integer > getPerfectcubes ( int n ) { List < Integer > perfectcubes = new ArrayList < Integer > ( ) ; int current = 1 ; int i = 1 ; while ( current <= n ) { perfectcubes . add ( current ) ; i += 1 ; current = ( int ) ( Math . pow ( i , 3 ) ) ; } return perfectcubes ; } static int maxPairSum ( int [ ] arr ) { int n = arr . length ; int max = 0 ; int secondMax = 0 ; if ( arr [ 0 ] > arr [ 1 ] ) { max = arr [ 0 ] ; secondMax = arr [ 1 ] ; } else { max = arr [ 1 ] ; secondMax = arr [ 0 ] ; } for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] > max ) { secondMax = max ; max = arr [ i ] ; } else if ( arr [ i ] > secondMax ) { secondMax = arr [ i ] ; } } return ( max + secondMax ) ; } static int countPairsWith ( int n , List < Integer > perfectcubes , List < Integer > nums ) { int count = 0 ; for ( int i = 0 ; i < perfectcubes . size ( ) ; i ++ ) { int temp = perfectcubes . get ( i ) - n ; if ( temp > n && ( nums . contains ( temp ) ) ) count += 1 ; } return count ; } static int countPairs ( int [ ] arr ) { int n = arr . length ; int max = maxPairSum ( arr ) ; List < Integer > perfectcubes = getPerfectcubes ( max ) ; List < Integer > nums = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { nums . add ( arr [ i ] ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { count += countPairsWith ( arr [ i ] , perfectcubes , nums ) ; } return count ; } public static void main ( String [ ] agrs ) { int [ ] arr = { 2 , 6 , 18 , 9 , 999 , 1 } ; System . out . print ( countPairs ( arr ) ) ; } }

def getPerfectcubes ( n ) : NEW_LINE INDENT perfectcubes = [ ] ; NEW_LINE current = 1 ; NEW_LINE i = 1 ; NEW_LINE while ( current <= n ) : NEW_LINE INDENT perfectcubes . append ( current ) ; NEW_LINE i += 1 ; NEW_LINE current = int ( pow ( i , 3 ) ) ; NEW_LINE DEDENT return perfectcubes ; NEW_LINE DEDENT def maxPairSum ( arr ) : NEW_LINE INDENT n = len ( arr ) ; NEW_LINE max = 0 ; NEW_LINE secondMax = 0 ; NEW_LINE if ( arr [ 0 ] > arr [ 1 ] ) : NEW_LINE INDENT max = arr [ 0 ] ; NEW_LINE secondMax = arr [ 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT max = arr [ 1 ] ; NEW_LINE secondMax = arr [ 0 ] ; NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i ] > max ) : NEW_LINE INDENT secondMax = max ; NEW_LINE max = arr [ i ] ; NEW_LINE DEDENT elif ( arr [ i ] > secondMax ) : NEW_LINE INDENT secondMax = arr [ i ] ; NEW_LINE DEDENT DEDENT return ( max + secondMax ) ; NEW_LINE DEDENT def countPairsWith ( n , perfectcubes , nums ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( len ( perfectcubes ) ) : NEW_LINE INDENT temp = perfectcubes [ i ] - n ; NEW_LINE if ( temp > n and ( temp in nums ) ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT def countPairs ( arr ) : NEW_LINE INDENT n = len ( arr ) ; NEW_LINE max = maxPairSum ( arr ) ; NEW_LINE perfectcubes = getPerfectcubes ( max ) ; NEW_LINE nums = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT nums . append ( arr [ i ] ) ; NEW_LINE DEDENT count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT count += countPairsWith ( arr [ i ] , perfectcubes , nums ) ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT arr = [ 2 , 6 , 18 , 9 , 999 , 1 ] ; NEW_LINE print ( countPairs ( arr ) ) ; NEW_LINE


import java . io . * ; class GFG { static void getLongestSeq ( int a [ ] , int n ) { int maxIdx = 0 , maxLen = 0 , currLen = 0 , currIdx = 0 ; for ( int k = 0 ; k < n ; k ++ ) { if ( a [ k ] > 0 ) { currLen ++ ; if ( currLen == 1 ) currIdx = k ; } else { if ( currLen > maxLen ) { maxLen = currLen ; maxIdx = currIdx ; } currLen = 0 ; } } if ( maxLen > 0 ) { System . out . print ( " Length ▁ " + maxLen ) ; System . out . print ( " , starting ▁ index ▁ " + maxIdx ) ; } else System . out . println ( " No ▁ positive ▁ sequence ▁ detected . " ) ; return ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , - 3 , 2 , 3 , 4 , - 6 , 1 , 2 , 3 , 4 , 5 , - 8 , 5 , 6 } ; int n = arr . length ; getLongestSeq ( arr , n ) ; } }

def getLongestSeq ( a , n ) : NEW_LINE INDENT maxIdx = 0 NEW_LINE maxLen = 0 NEW_LINE currLen = 0 NEW_LINE currIdx = 0 NEW_LINE for k in range ( n ) : NEW_LINE INDENT if a [ k ] > 0 : NEW_LINE INDENT currLen += 1 NEW_LINE if currLen == 1 : NEW_LINE INDENT currIdx = k NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if currLen > maxLen : NEW_LINE INDENT maxLen = currLen NEW_LINE maxIdx = currIdx NEW_LINE DEDENT currLen = 0 NEW_LINE DEDENT DEDENT if maxLen > 0 : NEW_LINE INDENT print ( ' Index ▁ : ▁ ' , maxIdx , ' , Length ▁ : ▁ ' , maxLen , ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ positive ▁ sequence ▁ detected . " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , - 3 , 2 , 3 , 4 , - 6 , 1 , 2 , 3 , 4 , 5 , - 8 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE getLongestSeq ( arr , n ) NEW_LINE


import java . util . * ; class GFG { static int minimumflip ( int mat [ ] [ ] , int n ) { int flip = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < i ; j ++ ) if ( mat [ i ] [ j ] != mat [ j ] [ i ] ) flip ++ ; return flip ; } public static void main ( String [ ] args ) { int n = 3 ; int mat [ ] [ ] = { { 0 , 0 , 1 } , { 1 , 1 , 1 } , { 1 , 0 , 0 } } ; System . out . println ( minimumflip ( mat , n ) ) ; } }

N = 3 NEW_LINE def minimumflip ( mat , n ) : NEW_LINE INDENT flip = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if mat [ i ] [ j ] != mat [ j ] [ i ] : NEW_LINE INDENT flip += 1 NEW_LINE DEDENT DEDENT DEDENT return flip NEW_LINE DEDENT n = 3 NEW_LINE mat = [ [ 0 , 0 , 1 ] , [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] ] NEW_LINE print ( minimumflip ( mat , n ) ) NEW_LINE


public class GFG { static double sumProductDifference ( int a , int b , int c , int d , int e ) { double rootSum = ( double ) ( - 1 * b ) / a ; double rootProduct = ( double ) e / a ; return Math . abs ( rootSum - rootProduct ) ; } public static void main ( String args [ ] ) { System . out . println ( sumProductDifference ( 8 , 4 , 6 , 4 , 1 ) ) ; } }

def sumProductDifference ( a , b , c , d , e ) : NEW_LINE INDENT rootSum = ( - 1 * b ) / a NEW_LINE rootProduct = e / a NEW_LINE return abs ( rootSum - rootProduct ) NEW_LINE DEDENT print ( sumProductDifference ( 8 , 4 , 6 , 4 , 1 ) ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; public class GFG { static double heptdiagonal ( double a ) { if ( a < 0 ) return - 1 ; double d = 1.802 * a ; return d ; } public static void main ( String [ ] args ) { int a = 6 ; System . out . println ( heptdiagonal ( a ) ) ; } }

def heptdiagonal ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT d = 1.802 * a NEW_LINE return round ( d , 3 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 6 NEW_LINE print ( heptdiagonal ( a ) ) NEW_LINE DEDENT


class GFG { static boolean strmatch ( char txt [ ] , char pat [ ] , int n , int m ) { if ( m == 0 ) return ( n == 0 ) ; int i = 0 , j = 0 , index_txt = - 1 , index_pat = - 1 ; while ( i < n ) { if ( j < m && txt [ i ] == pat [ j ] ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' ? ' ) { i ++ ; j ++ ; } else if ( j < m && pat [ j ] == ' * ' ) { index_txt = i ; index_pat = j ; j ++ ; } else if ( index_pat != - 1 ) { j = index_pat + 1 ; i = index_txt + 1 ; index_txt ++ ; } else { return false ; } } while ( j < m && pat [ j ] == ' * ' ) { j ++ ; } if ( j == m ) { return true ; } return false ; } public static void main ( String [ ] args ) { char str [ ] = " baaabab " . toCharArray ( ) ; char pattern [ ] = " * * * * * ba * * * * * ab " . toCharArray ( ) ; if ( strmatch ( str , pattern , str . length , pattern . length ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; char pattern2 [ ] = " a * * * * * ab " . toCharArray ( ) ; if ( strmatch ( str , pattern2 , str . length , pattern2 . length ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def stringmatch ( txt , pat , n , m ) : NEW_LINE INDENT if ( m == 0 ) : NEW_LINE INDENT return ( n == 0 ) NEW_LINE DEDENT i = 0 NEW_LINE j = 0 NEW_LINE index_txt = - 1 NEW_LINE index_pat = - 1 NEW_LINE while ( i < n - 2 ) : NEW_LINE INDENT if ( j < m and txt [ i ] == pat [ j ] ) : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( j < m and pat [ j ] == ' ? ' ) : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT elif ( j < m and pat [ j ] == ' * ' ) : NEW_LINE INDENT index_txt = i NEW_LINE index_pat = j NEW_LINE j += 1 NEW_LINE DEDENT elif ( index_pat != - 1 ) : NEW_LINE INDENT j = index_pat + 1 NEW_LINE i = index_txt + 1 NEW_LINE index_txt += 1 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT while ( j < m and pat [ j ] == ' * ' ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( j == m ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT strr = " baaabab " NEW_LINE pattern = " * * * * * ba * * * * * ab " NEW_LINE if ( stringmatch ( strr , pattern , len ( strr ) , len ( pattern ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT pattern2 = " a * * * * * ab " ; NEW_LINE if ( stringmatch ( strr , pattern2 , len ( strr ) , len ( pattern2 ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static void compute ( long S , long X ) { long A = ( S - X ) / 2 ; int a = 0 , b = 0 ; final int LONG_FIELD_SIZE = 8 ; for ( int i = 0 ; i < 8 * LONG_FIELD_SIZE ; i ++ ) { long Xi = ( X & ( 1 << i ) ) ; long Ai = ( A & ( 1 << i ) ) ; if ( Xi == 0 && Ai == 0 ) { } else if ( Xi == 0 && Ai > 0 ) { a = ( ( 1 << i ) | a ) ; b = ( ( 1 << i ) | b ) ; } else if ( Xi > 0 && Ai == 0 ) { a = ( ( 1 << i ) | a ) ; } else { System . out . println ( " Not ▁ Possible " ) ; return ; } } System . out . println ( " a ▁ = ▁ " + a + " \n b ▁ = ▁ " + b ) ; } public static void main ( String [ ] args ) { long S = 17 , X = 13 ; compute ( S , X ) ; } }

def compute ( S , X ) : NEW_LINE INDENT A = ( S - X ) // 2 NEW_LINE a = 0 NEW_LINE b = 0 NEW_LINE for i in range ( 64 ) : NEW_LINE INDENT Xi = ( X & ( 1 << i ) ) NEW_LINE Ai = ( A & ( 1 << i ) ) NEW_LINE if ( Xi == 0 and Ai == 0 ) : NEW_LINE INDENT pass NEW_LINE DEDENT elif ( Xi == 0 and Ai > 0 ) : NEW_LINE INDENT a = ( ( 1 << i ) | a ) NEW_LINE b = ( ( 1 << i ) | b ) NEW_LINE DEDENT elif ( Xi > 0 and Ai == 0 ) : NEW_LINE INDENT a = ( ( 1 << i ) | a ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT DEDENT print ( " a ▁ = ▁ " , a ) NEW_LINE print ( " b ▁ = " , b ) NEW_LINE DEDENT S = 17 NEW_LINE X = 13 NEW_LINE compute ( S , X ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GfG { public static int sumofFactors ( int n ) { if ( n % 2 != 0 ) return 0 ; int res = 1 ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; if ( i == 2 && count == 1 ) curr_sum = 0 ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } public static void main ( String argc [ ] ) { int n = 18 ; System . out . println ( sumofFactors ( n ) ) ; } }

import math NEW_LINE def sumofFactors ( n ) : NEW_LINE INDENT if ( n % 2 != 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT res = 1 NEW_LINE for i in range ( 2 , ( int ) ( math . sqrt ( n ) ) + 1 ) : NEW_LINE INDENT count = 0 NEW_LINE curr_sum = 1 NEW_LINE curr_term = 1 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE n = n // i NEW_LINE if ( i == 2 and count == 1 ) : NEW_LINE INDENT curr_sum = 0 NEW_LINE DEDENT curr_term = curr_term * i NEW_LINE curr_sum = curr_sum + curr_term NEW_LINE DEDENT res = res * curr_sum NEW_LINE DEDENT if ( n >= 2 ) : NEW_LINE INDENT res = res * ( 1 + n ) NEW_LINE DEDENT return res NEW_LINE DEDENT n = 18 NEW_LINE print ( sumofFactors ( n ) ) NEW_LINE


class GFG { static void findNextNumber ( int n ) { int h [ ] = new int [ 10 ] ; int i = 0 , msb = n , rem = 0 ; int next_num = - 1 , count = 0 ; while ( msb > 9 ) { rem = msb % 10 ; h [ rem ] = 1 ; msb /= 10 ; count ++ ; } h [ msb ] = 1 ; count ++ ; for ( i = msb + 1 ; i < 10 ; i ++ ) { if ( h [ i ] == 0 ) { next_num = i ; break ; } } if ( next_num == - 1 ) { for ( i = 1 ; i < msb ; i ++ ) { if ( h [ i ] == 0 ) { next_num = i ; count ++ ; break ; } } } if ( next_num > 0 ) { for ( i = 0 ; i < 10 ; i ++ ) { if ( h [ i ] == 0 ) { msb = i ; break ; } } for ( i = 1 ; i < count ; i ++ ) { next_num = ( ( next_num * 10 ) + msb ) ; } if ( next_num > n ) System . out . print ( next_num + " \n " ) ; else System . out . print ( " Not ▁ Possible ▁ \n " ) ; } else { System . out . print ( " Not ▁ Possible ▁ \n " ) ; } } public static void main ( String [ ] args ) { int n = 2019 ; findNextNumber ( n ) ; } }

def findNextNumber ( n ) : NEW_LINE INDENT h = [ 0 for i in range ( 10 ) ] NEW_LINE i = 0 NEW_LINE msb = n NEW_LINE rem = 0 NEW_LINE next_num = - 1 NEW_LINE count = 0 NEW_LINE while ( msb > 9 ) : NEW_LINE INDENT rem = msb % 10 NEW_LINE h [ rem ] = 1 NEW_LINE msb //= 10 NEW_LINE count += 1 NEW_LINE DEDENT h [ msb ] = 1 NEW_LINE count += 1 NEW_LINE for i in range ( msb + 1 , 10 , 1 ) : NEW_LINE INDENT if ( h [ i ] == 0 ) : NEW_LINE INDENT next_num = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( next_num == - 1 ) : NEW_LINE INDENT for i in range ( 1 , msb , 1 ) : NEW_LINE INDENT if ( h [ i ] == 0 ) : NEW_LINE INDENT next_num = i NEW_LINE count += 1 NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT if ( next_num > 0 ) : NEW_LINE INDENT for i in range ( 0 , 10 , 1 ) : NEW_LINE INDENT if ( h [ i ] == 0 ) : NEW_LINE INDENT msb = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( 1 , count , 1 ) : NEW_LINE INDENT next_num = ( ( next_num * 10 ) + msb ) NEW_LINE DEDENT if ( next_num > n ) : NEW_LINE INDENT print ( next_num ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 2019 NEW_LINE findNextNumber ( n ) NEW_LINE DEDENT


class GFG { static class node { char data ; node next ; } ; static node add ( char data ) { node newnode = new node ( ) ; newnode . data = data ; newnode . next = null ; return newnode ; } static void print ( node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ - > ▁ " ) ; head = head . next ; } System . out . print ( " null " ) ; } static boolean isSpecialChar ( char ch ) { if ( ch >= ' a ' && ch <= ' z ' ) return false ; if ( ch >= ' A ' && ch <= ' Z ' ) return false ; if ( ch >= '0' && ch <= '9' ) return false ; return true ; } static node removeFromLL ( node head ) { node curr = head ; node prev = head ; while ( curr != null && isSpecialChar ( curr . data ) ) { node temp = curr ; head = curr . next ; curr = curr . next ; temp = null ; } prev = head ; while ( curr != null ) { while ( curr != null && isSpecialChar ( curr . data ) ) { node temp = curr ; prev . next = curr . next ; curr = curr . next ; temp = null ; } if ( curr == null ) break ; prev = curr ; curr = curr . next ; } return head ; } public static void main ( String [ ] args ) { node head = null ; head = add ( ' ( ' ) ; head . next = add ( ' G ' ) ; head . next . next = add ( ' E ' ) ; head . next . next . next = add ( ' E ' ) ; head . next . next . next . next = add ( ' * ' ) ; head . next . next . next . next . next = add ( ' K ' ) ; head . next . next . next . next . next . next = add ( ' S ' ) ; head . next . next . next . next . next . next . next = add ( ' * ' ) ; head = removeFromLL ( head ) ; print ( 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 add ( data ) : NEW_LINE INDENT newnode = Node ( data ) NEW_LINE newnode . data = data NEW_LINE newnode . next = None NEW_LINE return newnode NEW_LINE DEDENT def printlist ( head ) : NEW_LINE INDENT while ( head != None ) : NEW_LINE INDENT print ( head . data , end = " ▁ - > ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT print ( " None " ) NEW_LINE DEDENT def isSpecialChar ( ch ) : NEW_LINE INDENT if ( ch >= ' a ' and ch <= ' z ' ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( ch >= ' A ' and ch <= ' Z ' ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( ch >= '0' and ch <= '9' ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def removeFromLL ( head ) : NEW_LINE INDENT curr = head NEW_LINE prev = head NEW_LINE while ( curr != None and isSpecialChar ( curr . data ) ) : NEW_LINE INDENT temp = curr NEW_LINE head = curr . next NEW_LINE curr = curr . next NEW_LINE temp = None NEW_LINE DEDENT prev = head NEW_LINE while ( curr != None ) : NEW_LINE INDENT while ( curr != None and isSpecialChar ( curr . data ) ) : NEW_LINE INDENT temp = curr NEW_LINE prev . next = curr . next NEW_LINE curr = curr . next NEW_LINE temp = None NEW_LINE DEDENT if ( curr == None ) : NEW_LINE INDENT break NEW_LINE DEDENT prev = curr NEW_LINE curr = curr . next NEW_LINE DEDENT return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = add ( ' ( ' ) NEW_LINE head . next = add ( ' G ' ) NEW_LINE head . next . next = add ( ' E ' ) NEW_LINE head . next . next . next = add ( ' E ' ) NEW_LINE head . next . next . next . next = add ( ' * ' ) NEW_LINE head . next . next . next . next . next = add ( ' K ' ) NEW_LINE head . next . next . next . next . next . next = add ( ' S ' ) NEW_LINE head . next . next . next . next . next . next . next = add ( ' * ' ) NEW_LINE head = removeFromLL ( head ) NEW_LINE printlist ( head ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class GFG { public static void preCompute ( int arr [ ] , int n , int pre [ ] ) { pre [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) pre [ i ] = arr [ i ] + pre [ i - 1 ] ; } public static int rangeSum ( int i , int j , int pre [ ] ) { if ( i == 0 ) return pre [ j ] ; return pre [ j ] - pre [ i - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int pre [ ] = new int [ n ] ; preCompute ( arr , n , pre ) ; System . out . println ( rangeSum ( 1 , 3 , pre ) ) ; System . out . println ( rangeSum ( 2 , 4 , pre ) ) ; } }

def find_ans ( ar , j , k ) : NEW_LINE INDENT l = len ( ar ) NEW_LINE for i in range ( 1 , l ) : NEW_LINE INDENT ar [ i ] = ar [ i ] + ar [ i - 1 ] NEW_LINE DEDENT print ( ar [ k ] - ar [ j - 1 ] ) NEW_LINE return ; NEW_LINE DEDENT pr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE ar = pr [ : ] NEW_LINE find_ans ( ar , 1 , 3 ) NEW_LINE ar = pr [ : ] NEW_LINE find_ans ( ar , 2 , 4 ) NEW_LINE


import java . util . * ; class GFG { static final int N = 1000005 ; static boolean prime [ ] = new boolean [ N ] ; static void sieve ( ) { Arrays . fill ( prime , true ) ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int i = 2 ; i < N ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * 2 ; j < N ; j += i ) { prime [ j ] = false ; } } } } static int sumTruncatablePrimes ( int n ) { int sum = 0 ; for ( int i = 2 ; i < n ; i ++ ) { int num = i ; boolean flag = true ; while ( num > 0 ) { if ( ! prime [ num ] ) { flag = false ; break ; } num /= 10 ; } num = i ; int power = 10 ; while ( num / power > 0 ) { if ( ! prime [ num % power ] ) { flag = false ; break ; } power *= 10 ; } if ( flag ) { sum += i ; } } return sum ; } public static void main ( String [ ] args ) { int n = 25 ; sieve ( ) ; System . out . println ( sumTruncatablePrimes ( n ) ) ; } }

N = 1000005 NEW_LINE prime = [ True for i in range ( N ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE prime [ 0 ] = False NEW_LINE for i in range ( 2 , N ) : NEW_LINE INDENT if ( prime [ i ] == True ) : NEW_LINE INDENT for j in range ( i * 2 , N , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def sumTruncatablePrimes ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT num = i NEW_LINE flag = True NEW_LINE while ( num ) : NEW_LINE INDENT if ( prime [ num ] == False ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT num //= 10 NEW_LINE DEDENT num = i NEW_LINE power = 10 NEW_LINE while ( num // power ) : NEW_LINE INDENT if ( prime [ num % power ] == False ) : NEW_LINE INDENT flag = False NEW_LINE break NEW_LINE DEDENT power *= 10 NEW_LINE DEDENT if ( flag == True ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 25 NEW_LINE sieve ( ) NEW_LINE print ( sumTruncatablePrimes ( n ) ) NEW_LINE


import java . util . * ; class geeks { public static int fibonacciArray ( int [ ] arr , int n ) { Set < Integer > s = new HashSet < Integer > ( ) ; int a = 1 , b = 1 ; int c ; s . add ( a ) ; if ( n > 2 ) s . add ( b ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) { c = a + b ; s . add ( c ) ; a = b ; b = c ; } for ( int i = 0 ; i < n ; i ++ ) { if ( s . contains ( arr [ i ] ) ) s . remove ( arr [ i ] ) ; } return s . size ( ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 3 , 1 , 21 , 4 , 2 , 1 , 8 , 9 } ; int n = arr . length ; System . out . print ( fibonacciArray ( arr , n ) ) ; } }

def fibonacciArray ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE a , b = 1 , 1 NEW_LINE s . add ( a ) NEW_LINE if n >= 2 : NEW_LINE INDENT s . add ( b ) NEW_LINE DEDENT for i in range ( 0 , n - 2 ) : NEW_LINE INDENT c = a + b NEW_LINE s . add ( c ) NEW_LINE a , b = b , c NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] in s : NEW_LINE INDENT s . remove ( arr [ i ] ) NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 1 , 21 , 4 , 2 , 1 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( fibonacciArray ( arr , n ) ) NEW_LINE DEDENT


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

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


class GFG { static int MAX = 100005 ; static int spf [ ] = new int [ MAX ] ; static void sieve ( ) { spf [ 0 ] = 1 ; spf [ 1 ] = - 1 ; for ( int i = 2 ; i * i < MAX ; i ++ ) { if ( spf [ i ] == 0 ) { for ( int j = i * 2 ; j < MAX ; j += i ) { if ( spf [ j ] == 0 ) { spf [ j ] = i ; } } } } for ( int i = 2 ; i < MAX ; i ++ ) { if ( spf [ i ] != 1 ) spf [ i ] = i ; } } static int smallestInteger ( int [ ] arr , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = __gcd ( gcd , arr [ i ] ) ; return spf [ gcd ] ; } static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { sieve ( ) ; int arr [ ] = { 2 , 4 , 8 } ; int n = arr . length ; System . out . println ( smallestInteger ( arr , n ) ) ; } }

MAX = 10005 ; NEW_LINE spf = [ 0 ] * MAX ; NEW_LINE def sieve ( ) : NEW_LINE INDENT spf [ 0 ] = 1 ; NEW_LINE spf [ 1 ] = - 1 ; NEW_LINE i = 2 ; NEW_LINE while ( i * i < MAX ) : NEW_LINE INDENT if ( spf [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i * 2 , MAX , i ) : NEW_LINE INDENT if ( spf [ j ] == 0 ) : NEW_LINE INDENT spf [ j ] = i ; NEW_LINE DEDENT DEDENT DEDENT i += 1 ; NEW_LINE DEDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT if ( spf [ i ] == 0 ) : NEW_LINE INDENT spf [ i ] = i ; NEW_LINE DEDENT DEDENT DEDENT def __gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT return __gcd ( b , a % b ) ; NEW_LINE DEDENT def smallestInteger ( arr , n ) : NEW_LINE INDENT gcd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT gcd = __gcd ( gcd , arr [ i ] ) ; NEW_LINE DEDENT return spf [ gcd ] ; NEW_LINE DEDENT sieve ( ) ; NEW_LINE arr = [ 2 , 4 , 8 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( smallestInteger ( arr , n ) ) ; NEW_LINE


class GFG { static void findNums ( int n ) { if ( n <= 11 ) { if ( n == 8 ) System . out . print ( "4 ▁ 4" ) ; if ( n == 10 ) System . out . print ( "4 ▁ 6" ) ; else System . out . print ( " - 1" ) ; return ; } if ( n % 2 == 0 ) System . out . print ( "4 ▁ " + ( n - 4 ) ) ; else System . out . print ( "9 ▁ " + ( n - 9 ) ) ; } public static void main ( String args [ ] ) { int n = 13 ; findNums ( n ) ; } }

def findNums ( n ) : NEW_LINE INDENT if ( n <= 11 ) : NEW_LINE INDENT if ( n == 8 ) : NEW_LINE INDENT print ( "4 ▁ 4" , end = " ▁ " ) NEW_LINE DEDENT if ( n == 10 ) : NEW_LINE INDENT print ( "4 ▁ 6" , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - 1" , end = " ▁ " ) NEW_LINE DEDENT DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT print ( "4 ▁ " , ( n - 4 ) , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( "9 ▁ " , n - 9 , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 13 NEW_LINE findNums ( n ) NEW_LINE


import java . util . * ; class GFG { static void solve ( int n , int k ) { int mini = 0 ; int x1 = 1 ; int [ ] a = new int [ k ] ; for ( int i = 1 ; i <= k ; i ++ ) { mini += x1 ; a [ i - 1 ] = x1 ; x1 += 1 ; } if ( n < mini ) { System . out . print ( " - 1" ) ; return ; } int rem = n - mini ; int cnt = rem / k ; rem = rem % k ; for ( int i = 0 ; i < k ; i ++ ) a [ i ] += cnt ; for ( int i = k - 1 ; i > 0 && rem > 0 ; i -- ) { int xx = a [ i - 1 ] * 2 ; int left = xx - a [ i ] ; if ( rem >= left ) { a [ i ] = xx ; rem -= left ; } else { a [ i ] += rem ; rem = 0 ; } } int sum = a [ 0 ] ; for ( int i = 1 ; i < k ; i ++ ) { if ( a [ i ] > 2 * a [ i - 1 ] ) { System . out . print ( " - 1" ) ; return ; } sum += a [ i ] ; } if ( sum != n ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 26 , k = 6 ; solve ( n , k ) ; } }

def solve ( n , k ) : NEW_LINE INDENT mini = 0 NEW_LINE x1 = 1 NEW_LINE a = [ 0 for i in range ( k ) ] NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT mini += x1 NEW_LINE a [ i - 1 ] = x1 NEW_LINE x1 += 1 NEW_LINE DEDENT if ( n < mini ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return NEW_LINE DEDENT rem = n - mini NEW_LINE cnt = int ( rem / k ) NEW_LINE rem = rem % k NEW_LINE for i in range ( k ) : NEW_LINE INDENT a [ i ] += cnt NEW_LINE DEDENT i = k - 1 NEW_LINE while ( i > 0 and rem > 0 ) : NEW_LINE INDENT xx = a [ i - 1 ] * 2 NEW_LINE left = xx - a [ i ] NEW_LINE if ( rem >= left ) : NEW_LINE INDENT a [ i ] = xx NEW_LINE rem -= left NEW_LINE DEDENT else : NEW_LINE INDENT a [ i ] += rem NEW_LINE rem = 0 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT sum = a [ 0 ] NEW_LINE for i in range ( 1 , k ) : NEW_LINE INDENT if ( a [ i ] > 2 * a [ i - 1 ] ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return NEW_LINE DEDENT sum += a [ i ] NEW_LINE DEDENT if ( sum != n ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 26 NEW_LINE k = 6 NEW_LINE solve ( n , k ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static int countTriplets ( int [ ] arr , int n , int a , int b ) { int ans = 0 ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ i ] + arr [ j ] + arr [ k ] >= a && arr [ i ] + arr [ j ] + arr [ k ] <= b ) { ans ++ ; } } } } return ans ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 } ; int n = arr . length ; int a = 8 , b = 16 ; System . out . println ( " " + countTriplets ( arr , n , a , b ) ) ; } }

def countTriplets ( arr , n , a , b ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( ( arr [ i ] + arr [ j ] + arr [ k ] >= a ) and ( arr [ i ] + arr [ j ] + arr [ k ] <= b ) ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 7 , 5 , 3 , 8 , 4 , 1 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE a = 8 ; b = 16 NEW_LINE print ( countTriplets ( arr , n , a , b ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static int N = 4 ; static void diagonalBoundarySum ( int arr [ ] [ ] ) { int requiredSum = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i == j || ( i + j ) == N - 1 ) { requiredSum += arr [ i ] [ j ] ; } else if ( i == 0 || j == 0 || i == N - 1 || j == N - 1 ) { requiredSum += arr [ i ] [ j ] ; } } } System . out . println ( requiredSum ) ; } public static void main ( String args [ ] ) { int arr [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 1 , 2 , 3 , 4 } , { 1 , 2 , 3 , 4 } , { 1 , 2 , 3 , 4 } } ; diagonalBoundarySum ( arr ) ; } }

N = 4 ; NEW_LINE def diagonalBoundarySum ( arr ) : NEW_LINE INDENT requiredSum = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( i == j or ( i + j ) == N - 1 ) : NEW_LINE INDENT requiredSum += arr [ i ] [ j ] ; NEW_LINE DEDENT elif ( i == 0 or j == 0 or i == N - 1 or j == N - 1 ) : NEW_LINE INDENT requiredSum += arr [ i ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT print ( requiredSum ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ [ 1 , 2 , 3 , 4 ] , [ 1 , 2 , 3 , 4 ] , [ 1 , 2 , 3 , 4 ] , [ 1 , 2 , 3 , 4 ] ] ; NEW_LINE diagonalBoundarySum ( arr ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { public static void subArraySum ( int [ ] arr , int n , int sum ) { int cur_sum = 0 ; int start = 0 ; int end = - 1 ; HashMap < Integer , Integer > hashMap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { cur_sum = cur_sum + arr [ i ] ; if ( cur_sum - sum == 0 ) { start = 0 ; end = i ; break ; } if ( hashMap . containsKey ( cur_sum - sum ) ) { start = hashMap . get ( cur_sum - sum ) + 1 ; end = i ; break ; } hashMap . put ( cur_sum , i ) ; } if ( end == - 1 ) { System . out . println ( " No ▁ subarray ▁ with ▁ given ▁ sum ▁ exists " ) ; } else { System . out . println ( " Sum ▁ found ▁ between ▁ indexes ▁ " + start + " ▁ to ▁ " + end ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 2 , - 2 , - 20 , 10 } ; int n = arr . length ; int sum = - 10 ; subArraySum ( arr , n , sum ) ; } }

def subArraySum ( arr , n , Sum ) : NEW_LINE INDENT Map = { } NEW_LINE curr_sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT curr_sum = curr_sum + arr [ i ] NEW_LINE if curr_sum == Sum : NEW_LINE INDENT print ( " Sum ▁ found ▁ between ▁ indexes ▁ 0 ▁ to " , i ) NEW_LINE return NEW_LINE DEDENT if ( curr_sum - Sum ) in Map : NEW_LINE INDENT print ( " Sum ▁ found ▁ between ▁ indexes " ,   \ Map [ curr_sum - Sum ] + 1 , " to " , i ) NEW_LINE return NEW_LINE DEDENT Map [ curr_sum ] = i NEW_LINE DEDENT print ( " No ▁ subarray ▁ with ▁ given ▁ sum ▁ exists " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 2 , - 2 , - 20 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE Sum = - 10 NEW_LINE subArraySum ( arr , n , Sum ) NEW_LINE DEDENT


class GFG { static String previousNumber ( String num ) { int n = num . length ( ) ; if ( num . compareTo ( "1" ) == 0 ) { return "0" ; } int i ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( num . charAt ( i ) == '1' ) { num = num . substring ( 0 , i ) + '0' + num . substring ( i + 1 ) ; break ; } else { num = num . substring ( 0 , i ) + '1' + num . substring ( i + 1 ) ; } } if ( i == 0 ) { return num . substring ( 1 , n - 1 ) ; } return num ; } public static void main ( String [ ] args ) { String num = "10110" ; System . out . print ( " Binary ▁ representation ▁ of ▁ previous ▁ number ▁ = ▁ " + previousNumber ( num ) ) ; } }

def previousNumber ( num1 ) : NEW_LINE INDENT n = len ( num1 ) ; NEW_LINE num = list ( num1 ) ; NEW_LINE if ( num1 == "1" ) : NEW_LINE INDENT return "0" ; NEW_LINE DEDENT i = n - 1 ; NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( num [ i ] == '1' ) : NEW_LINE INDENT num [ i ] = '0' ; NEW_LINE break ; NEW_LINE DEDENT else : NEW_LINE INDENT num [ i ] = '1' ; NEW_LINE DEDENT i -= 1 ; NEW_LINE DEDENT if ( i == 0 ) : NEW_LINE INDENT return num [ 1 : n ] ; NEW_LINE DEDENT return ' ' . join ( num ) ; NEW_LINE DEDENT num = "10110" ; NEW_LINE print ( " Binary ▁ representation ▁ of ▁ previous ▁ number ▁ = " , previousNumber ( num ) ) ; NEW_LINE


import java . io . * ; class GFG { public static String prefixSuffix ( String a , String b , int l ) { String prefix = a . substring ( 0 , l ) ; int lb = b . length ( ) ; String suffix = b . substring ( lb - l ) ; return ( prefix + suffix ) ; } public static void main ( String args [ ] ) throws IOException { String a = " remuneration " , b = " acquiesce " ; int l = 5 ; System . out . println ( prefixSuffix ( a , b , l ) ) ; } }

def GetPrefixSuffix ( a , b , l ) : NEW_LINE INDENT prefix = a [ : l ] ; NEW_LINE lb = len ( b ) ; NEW_LINE suffix = b [ lb - l : ] ; NEW_LINE return ( prefix + suffix ) ; NEW_LINE DEDENT a = " remuneration " ; NEW_LINE b = " acquiesce " ; NEW_LINE l = 5 ; NEW_LINE print ( GetPrefixSuffix ( a , b , l ) ) ; NEW_LINE


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

def seriessum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum += i * ( i + 1 ) / 2 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 4 NEW_LINE print ( seriessum ( n ) ) NEW_LINE


import java . util . * ; class solution { static int count ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int cnt = 0 ; int i = 0 , j = 1 ; while ( i < n && j < n ) { j = ( j <= i ) ? ( i + 1 ) : j ; while ( j < n && ( arr [ j ] - arr [ i ] ) < k ) j ++ ; cnt += ( n - j ) ; i ++ ; } return cnt ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int k = 2 ; System . out . println ( count ( arr , n , k ) ) ; } }

def count ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE cnt = 0 ; NEW_LINE i = 0 ; j = 1 ; NEW_LINE while ( i < n and j < n ) : NEW_LINE INDENT if j <= i : NEW_LINE INDENT j = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT j = j NEW_LINE DEDENT while ( j < n and ( arr [ j ] - arr [ i ] ) < k ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT cnt += ( n - j ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( count ( arr , n , k ) ) ; NEW_LINE DEDENT


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

def countIntersections ( n ) : NEW_LINE INDENT return n * ( n - 1 ) // 2 NEW_LINE DEDENT n = 3 NEW_LINE print ( countIntersections ( n ) ) NEW_LINE


import java . util . * ; class GfG { static boolean isSlopeGood ( double slope , int arr [ ] , int n ) { Set < Double > setOfLines = new HashSet < Double > ( ) ; for ( int i = 0 ; i < n ; i ++ ) setOfLines . add ( arr [ i ] - slope * ( i ) ) ; return setOfLines . size ( ) == 2 ; } static boolean checkForParallel ( int arr [ ] , int n ) { boolean slope1 = isSlopeGood ( arr [ 1 ] - arr [ 0 ] , arr , n ) ; boolean slope2 = isSlopeGood ( arr [ 2 ] - arr [ 1 ] , arr , n ) ; boolean slope3 = isSlopeGood ( ( arr [ 2 ] - arr [ 0 ] ) / 2 , arr , n ) ; return ( slope1 == true || slope2 == true || slope3 == true ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 6 , 3 , 8 , 5 } ; int n = arr . length ; if ( checkForParallel ( arr , n ) == true ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } }

def isSlopeGood ( slope , arr , n ) : NEW_LINE INDENT setOfLines = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT setOfLines [ arr [ i ] - slope * ( i ) ] = 1 NEW_LINE DEDENT return len ( setOfLines ) == 2 NEW_LINE DEDENT def checkForParallel ( arr , n ) : NEW_LINE INDENT slope1 = isSlopeGood ( arr [ 1 ] - arr [ 0 ] , arr , n ) NEW_LINE slope2 = isSlopeGood ( arr [ 2 ] - arr [ 1 ] , arr , n ) NEW_LINE slope3 = isSlopeGood ( ( arr [ 2 ] - arr [ 0 ] ) // 2 , arr , n ) NEW_LINE return ( slope1 or slope2 or slope3 ) NEW_LINE DEDENT arr = [ 1 , 6 , 3 , 8 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE if checkForParallel ( arr , n ) : NEW_LINE INDENT print ( 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( 0 ) NEW_LINE DEDENT


class GFG { static char flip ( char c ) { return ( c == '0' ) ? '1' : '0' ; } static void printOneAndTwosComplement ( String bin ) { int n = bin . length ( ) ; int i ; String ones = " " , twos = " " ; ones = twos = " " ; for ( i = 0 ; i < n ; i ++ ) { ones += flip ( bin . charAt ( i ) ) ; } twos = ones ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( ones . charAt ( i ) == '1' ) { twos = twos . substring ( 0 , i ) + '0' + twos . substring ( i + 1 ) ; } else { twos = twos . substring ( 0 , i ) + '1' + twos . substring ( i + 1 ) ; break ; } } if ( i == - 1 ) { twos = '1' + twos ; } System . out . println ( "1 ' s ▁ complement : ▁ " + ones ) ; ; System . out . println ( "2 ' s ▁ complement : ▁ " + twos ) ; } public static void main ( String [ ] args ) { String bin = "1100" ; printOneAndTwosComplement ( bin ) ; } }

def flip ( c ) : NEW_LINE INDENT return '1' if ( c == '0' ) else '0' NEW_LINE DEDENT def printOneAndTwosComplement ( bin ) : NEW_LINE INDENT n = len ( bin ) NEW_LINE ones = " " NEW_LINE twos = " " NEW_LINE for i in range ( n ) : NEW_LINE INDENT ones += flip ( bin [ i ] ) NEW_LINE DEDENT ones = list ( ones . strip ( " " ) ) NEW_LINE twos = list ( ones ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( ones [ i ] == '1' ) : NEW_LINE INDENT twos [ i ] = '0' NEW_LINE DEDENT else : NEW_LINE INDENT twos [ i ] = '1' NEW_LINE break NEW_LINE DEDENT DEDENT i -= 1 NEW_LINE if ( i == - 1 ) : NEW_LINE INDENT twos . insert ( 0 , '1' ) NEW_LINE DEDENT print ( "1 ' s ▁ complement : ▁ " , * ones , sep = " " ) NEW_LINE print ( "2 ' s ▁ complement : ▁ " , * twos , sep = " " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT bin = "1100" NEW_LINE printOneAndTwosComplement ( bin . strip ( " " ) ) NEW_LINE DEDENT


class DegreeOfVertex { static class Graph { int v , e ; int [ ] [ ] dir ; Graph ( int v , int e ) { this . v = v ; this . e = e ; dir = new int [ v ] [ ] ; for ( int i = 0 ; i < v ; i ++ ) dir [ i ] = new int [ v ] ; } } static Graph createGraph ( int v , int e ) { Graph G = new Graph ( v , e ) ; G . dir [ 0 ] [ 1 ] = 1 ; G . dir [ 0 ] [ 2 ] = 1 ; G . dir [ 0 ] [ 3 ] = 1 ; G . dir [ 1 ] [ 0 ] = 1 ; G . dir [ 1 ] [ 3 ] = 1 ; G . dir [ 2 ] [ 0 ] = 1 ; G . dir [ 2 ] [ 3 ] = 1 ; G . dir [ 3 ] [ 0 ] = 1 ; G . dir [ 3 ] [ 1 ] = 1 ; G . dir [ 3 ] [ 2 ] = 1 ; return G ; } static int findDegree ( Graph G , int ver ) { int degree = 0 ; for ( int i = 0 ; i < G . v ; i ++ ) { if ( G . dir [ ver ] [ i ] == 1 ) degree ++ ; } return degree ; } public static void main ( String [ ] args ) { int vertices = 4 ; int edges = 5 ; Graph G = createGraph ( vertices , edges ) ; int ver = 0 ; int degree = findDegree ( G , ver ) ; System . out . println ( degree ) ; } }

class Graph : NEW_LINE INDENT v = None NEW_LINE e = None NEW_LINE diri = [ ] NEW_LINE def __init__ ( self , v , e ) : NEW_LINE INDENT self . v = v NEW_LINE self . e = e NEW_LINE self . diri = [ [ 0 for i in range ( v ) ] for j in range ( v ) ] NEW_LINE DEDENT DEDENT def createGraph ( v , e ) : NEW_LINE INDENT G = Graph ( v , e ) NEW_LINE G . diri [ 0 ] [ 1 ] = 1 NEW_LINE G . diri [ 0 ] [ 2 ] = 1 NEW_LINE G . diri [ 0 ] [ 3 ] = 1 NEW_LINE G . diri [ 1 ] [ 0 ] = 1 NEW_LINE G . diri [ 1 ] [ 3 ] = 1 NEW_LINE G . diri [ 2 ] [ 0 ] = 1 NEW_LINE G . diri [ 2 ] [ 3 ] = 1 NEW_LINE G . diri [ 3 ] [ 0 ] = 1 NEW_LINE G . diri [ 3 ] [ 1 ] = 1 NEW_LINE G . diri [ 3 ] [ 2 ] = 1 NEW_LINE return G NEW_LINE DEDENT def findDegree ( G , ver ) : NEW_LINE INDENT degree = 0 NEW_LINE for i in range ( G . v ) : NEW_LINE INDENT if G . diri [ ver ] [ i ] == 1 : NEW_LINE INDENT degree += 1 NEW_LINE DEDENT DEDENT return degree NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT vertices = 4 NEW_LINE edges = 5 NEW_LINE G = createGraph ( vertices , edges ) NEW_LINE ver = 0 NEW_LINE degree = findDegree ( G , ver ) NEW_LINE print ( degree ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int count ( int S [ ] , int m , int n ) { if ( n == 0 ) return 1 ; if ( n < 0 ) return 0 ; if ( m <= 0 && n >= 1 ) return 0 ; return count ( S , m - 1 , n ) + count ( S , m , n - S [ m - 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int m = arr . length ; System . out . println ( count ( arr , m , 4 ) ) ; } }

def count ( S , m , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n < 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( m <= 0 and n >= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return count ( S , m - 1 , n ) + count ( S , m , n - S [ m - 1 ] ) ; NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE m = len ( arr ) NEW_LINE print ( count ( arr , m , 4 ) ) NEW_LINE


class GFG { static float knapSack ( int W , float wt [ ] , float val [ ] , int n ) { float maxratio = Integer . MIN_VALUE ; int maxindex = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( val [ i ] / wt [ i ] ) > maxratio ) { maxratio = ( val [ i ] / wt [ i ] ) ; maxindex = i ; } } return ( W * maxratio ) ; } public static void main ( String [ ] args ) { float val [ ] = { 14 , 27 , 44 , 19 } ; float wt [ ] = { 6 , 7 , 9 , 8 } ; int n = val . length ; int W = 50 ; System . out . println ( knapSack ( W , wt , val , n ) ) ; } }

import sys NEW_LINE def knapSack ( W , wt , val , n ) : NEW_LINE INDENT maxratio = - sys . maxsize - 1 ; NEW_LINE maxindex = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( val [ i ] / wt [ i ] ) > maxratio ) : NEW_LINE INDENT maxratio = ( val [ i ] / wt [ i ] ) ; NEW_LINE maxindex = i ; NEW_LINE DEDENT DEDENT return ( W * maxratio ) ; NEW_LINE DEDENT val = [ 14 , 27 , 44 , 19 ] ; NEW_LINE wt = [ 6 , 7 , 9 , 8 ] ; NEW_LINE n = len ( val ) ; NEW_LINE W = 50 ; NEW_LINE print ( knapSack ( W , wt , val , n ) ) ; NEW_LINE


class GFG { static String replaceRemove ( char [ ] s , char X , char Y , char Z ) { int start = 0 , end = s . length - 1 ; while ( start <= end ) { if ( s [ start ] == Z ) { while ( end >= 0 && s [ end ] == Z ) { end -- ; } char temp ; if ( end > start ) { temp = s [ start ] ; s [ start ] = s [ end ] ; s [ end ] = temp ; if ( s [ start ] == X ) s [ start ] = Y ; start ++ ; } } else { if ( s [ start ] == X ) s [ start ] = Y ; start ++ ; } } String new_s = new String ( s ) ; while ( new_s . length ( ) > 0 && new_s . charAt ( new_s . length ( ) - 1 ) == Z ) { new_s = new_s . substring ( 0 , new_s . length ( ) - 1 ) ; } return new_s ; } public static void main ( String [ ] args ) { String str = " batman " ; char X = ' a ' , Y = ' d ' , Z = ' b ' ; str = replaceRemove ( str . toCharArray ( ) , X , Y , Z ) ; if ( str . length ( ) == 0 ) { System . out . println ( - 1 ) ; } else { System . out . println ( str ) ; } } }

def replaceRemove ( s , X , Y , Z ) : NEW_LINE INDENT s = list ( s ) ; NEW_LINE start = 0 ; NEW_LINE end = len ( s ) - 1 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT if ( s [ start ] == Z ) : NEW_LINE INDENT while ( end >= 0 and s [ end ] == Z ) : NEW_LINE INDENT end -= 1 ; NEW_LINE DEDENT if ( end > start ) : NEW_LINE INDENT s [ start ] , s [ end ] = s [ end ] , s [ start ] NEW_LINE if ( s [ start ] == X ) : NEW_LINE INDENT s [ start ] = Y ; NEW_LINE DEDENT start += 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( s [ start ] == X ) : NEW_LINE INDENT s [ start ] = Y ; NEW_LINE DEDENT start += 1 ; NEW_LINE DEDENT DEDENT while ( len ( s ) > 0 and s [ len ( s ) - 1 ] == Z ) : NEW_LINE INDENT s . pop ( ) ; NEW_LINE DEDENT return " " . join ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " batman " ; NEW_LINE X = ' a ' ; Y = ' d ' ; Z = ' b ' ; NEW_LINE string = replaceRemove ( string , X , Y , Z ) ; NEW_LINE if ( len ( string ) == 0 ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( string ) ; NEW_LINE DEDENT DEDENT


public class GFG { final static int MAX = 100005 ; static void precompute ( int prefix [ ] , int suffix [ ] ) { boolean prime [ ] = new boolean [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) { prime [ i ] = true ; } for ( int i = 2 ; i * i < MAX ; i ++ ) { if ( prime [ i ] ) { for ( int j = i + i ; j < MAX ; j += i ) { prime [ j ] = false ; } } } prefix [ 1 ] = 1 ; suffix [ MAX - 1 ] = ( int ) 1e9 + 7 ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( prime [ i ] ) { prefix [ i ] = i ; } else { prefix [ i ] = prefix [ i - 1 ] ; } } for ( int i = MAX - 2 ; i > 1 ; i -- ) { if ( prime [ i ] ) { suffix [ i ] = i ; } else { suffix [ i ] = suffix [ i + 1 ] ; } } } static int query ( int prefix [ ] , int suffix [ ] , int L , int R ) { if ( prefix [ R ] < L || suffix [ L ] > R ) { return 0 ; } else { return prefix [ R ] - suffix [ L ] ; } } public static void main ( String [ ] args ) { int q = 3 ; int L [ ] = { 2 , 2 , 24 } ; int R [ ] = { 5 , 2 , 28 } ; int prefix [ ] = new int [ MAX ] , suffix [ ] = new int [ MAX ] ; precompute ( prefix , suffix ) ; for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( query ( prefix , suffix , L [ i ] , R [ i ] ) ) ; } } }

from math import sqrt NEW_LINE MAX = 100005 NEW_LINE def precompute ( prefix , suffix ) : NEW_LINE INDENT prime = [ True for i in range ( MAX ) ] NEW_LINE k = int ( sqrt ( MAX ) ) NEW_LINE for i in range ( 2 , k , 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT for j in range ( i + i , MAX , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT prefix [ 1 ] = 1 NEW_LINE suffix [ MAX - 1 ] = int ( 1e9 + 7 ) NEW_LINE for i in range ( 2 , MAX , 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT prefix [ i ] = i NEW_LINE DEDENT else : NEW_LINE INDENT prefix [ i ] = prefix [ i - 1 ] NEW_LINE DEDENT DEDENT i = MAX - 2 NEW_LINE while ( i > 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT suffix [ i ] = i NEW_LINE DEDENT else : NEW_LINE INDENT suffix [ i ] = suffix [ i + 1 ] NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT DEDENT def query ( prefix , suffix , L , R ) : NEW_LINE INDENT if ( prefix [ R ] < L or suffix [ L ] > R ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return prefix [ R ] - suffix [ L ] NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = 3 NEW_LINE L = [ 2 , 2 , 24 ] NEW_LINE R = [ 5 , 2 , 28 ] NEW_LINE prefix = [ 0 for i in range ( MAX ) ] NEW_LINE suffix = [ 0 for i in range ( MAX ) ] NEW_LINE precompute ( prefix , suffix ) NEW_LINE for i in range ( 0 , q , 1 ) : NEW_LINE INDENT print ( query ( prefix , suffix , L [ i ] , R [ i ] ) ) NEW_LINE DEDENT DEDENT


class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } static void flip ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; a = lcm / a ; b = lcm / b ; System . out . print ( ( a - 1 ) + " ▁ " + ( b - 1 ) ) ; } public static void main ( String [ ] args ) { int a = 10 ; int b = 5 ; flip ( a , b ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT def flip ( a , b ) : NEW_LINE INDENT lcm = ( a * b ) // gcd ( a , b ) NEW_LINE a = lcm // a NEW_LINE b = lcm // b NEW_LINE print ( a - 1 , b - 1 ) NEW_LINE DEDENT a = 10 NEW_LINE b = 5 NEW_LINE flip ( a , b ) NEW_LINE


import java . util . Arrays ; class GFG { static void normalSieve ( int n ) { boolean prime [ ] = new boolean [ n / 2 ] ; Arrays . fill ( prime , false ) ; for ( int i = 3 ; i * i < n ; i += 2 ) { if ( prime [ i / 2 ] == false ) for ( int j = i * i ; j < n ; j += i * 2 ) prime [ j / 2 ] = true ; } System . out . print ( "2 ▁ " ) ; for ( int i = 3 ; i < n ; i += 2 ) if ( prime [ i / 2 ] == false ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 100 ; normalSieve ( n ) ; } }

def normalSieve ( n ) : NEW_LINE INDENT prime = [ 0 ] * int ( n / 2 ) ; NEW_LINE i = 3 ; NEW_LINE while ( i * i < n ) : NEW_LINE INDENT if ( prime [ int ( i / 2 ) ] == 0 ) : NEW_LINE INDENT j = i * i ; NEW_LINE while ( j < n ) : NEW_LINE INDENT prime [ int ( j / 2 ) ] = 1 ; NEW_LINE j += i * 2 ; NEW_LINE DEDENT DEDENT i += 2 ; NEW_LINE DEDENT print ( 2 , end = " ▁ " ) ; NEW_LINE i = 3 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( prime [ int ( i / 2 ) ] == 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT i += 2 ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 100 ; NEW_LINE normalSieve ( n ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean checkPalindrome ( String s ) { for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( s . length ( ) - i - 1 ) ) return false ; } return true ; } static boolean CheckOdd ( String s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i ++ ) { String x = " " ; for ( int j = i ; j < n ; j ++ ) { x += s . charAt ( j ) ; if ( x . length ( ) % 2 == 0 && checkPalindrome ( x ) == true ) return false ; } } return true ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; if ( CheckOdd ( s ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }

def checkPalindrome ( s ) : NEW_LINE INDENT for i in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ i ] != s [ len ( s ) - i - 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def CheckOdd ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = " " NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT x += s [ j ] NEW_LINE if ( len ( x ) % 2 == 0 and checkPalindrome ( x ) == True ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE if ( CheckOdd ( s ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT


class GFG { static int countStrings ( int n ) { int a [ ] = new int [ n ] , b [ ] = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } from 2 ^ n return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] ; } public static void main ( String args [ ] ) { System . out . println ( countStrings ( 5 ) ) ; } }

def countStrings ( n ) : NEW_LINE INDENT a = [ 0 ] * n NEW_LINE b = [ 0 ] * n NEW_LINE a [ 0 ] = b [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = a [ i - 1 ] + b [ i - 1 ] NEW_LINE b [ i ] = a [ i - 1 ] NEW_LINE DEDENT return ( 1 << n ) - a [ n - 1 ] - b [ n - 1 ] NEW_LINE DEDENT print ( countStrings ( 5 ) ) NEW_LINE


public class GFG { static final int MAX = 256 ; static int freq [ ] [ ] ; static void preCalculate ( String str , int n ) { freq [ ( int ) str . charAt ( 0 ) ] [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { char ch = str . charAt ( i ) ; for ( int j = 0 ; j < MAX ; j ++ ) { char charToUpdate = ( char ) j ; if ( charToUpdate == ch ) freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 ; else freq [ j ] [ i ] = freq [ j ] [ i - 1 ] ; } } } static int getFrequency ( char ch , int l , int r ) { if ( l == 0 ) return freq [ ( int ) ch ] [ r ] ; else return ( freq [ ( int ) ch ] [ r ] - freq [ ( int ) ch ] [ l - 1 ] ) ; } static String lastNonRepeating ( String str , int n , int l , int r ) { for ( int i = r ; i >= l ; i -- ) { char ch = str . charAt ( i ) ; if ( getFrequency ( ch , l , r ) == 1 ) return ( " " + ch ) ; } return " - 1" ; } public static void main ( String [ ] args ) { String str = " GeeksForGeeks " ; int n = str . length ( ) ; int queries [ ] [ ] = { { 2 , 9 } , { 2 , 3 } , { 0 , 12 } } ; int q = queries . length ; freq = new int [ MAX ] [ n ] ; preCalculate ( str , n ) ; for ( int i = 0 ; i < q ; i ++ ) { System . out . println ( lastNonRepeating ( str , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) ; } } }

MAX = 256 NEW_LINE freq = [ [ 0 for i in range ( 256 ) ] for j in range ( 1000 ) ] NEW_LINE def preCalculate ( string , n ) : NEW_LINE INDENT freq [ ord ( string [ 0 ] ) ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT ch = string [ i ] NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT charToUpdate = chr ( j ) NEW_LINE if charToUpdate == ch : NEW_LINE INDENT freq [ j ] [ i ] = freq [ j ] [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT freq [ j ] [ i ] = freq [ j ] [ i - 1 ] NEW_LINE DEDENT DEDENT DEDENT DEDENT def getFrequency ( ch , l , r ) : NEW_LINE INDENT if l == 0 : NEW_LINE INDENT return freq [ ord ( ch ) ] [ r ] NEW_LINE DEDENT else : NEW_LINE INDENT return ( freq [ ord ( ch ) ] [ r ] - freq [ ord ( ch ) ] [ l - 1 ] ) NEW_LINE DEDENT DEDENT def lastNonRepeating ( string , n , l , r ) : NEW_LINE INDENT for i in range ( r , l - 1 , - 1 ) : NEW_LINE INDENT ch = string [ i ] NEW_LINE if getFrequency ( ch , l , r ) == 1 : NEW_LINE INDENT return ch NEW_LINE DEDENT DEDENT return " - 1" NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " GeeksForGeeks " NEW_LINE n = len ( string ) NEW_LINE queries = [ ( 2 , 9 ) , ( 2 , 3 ) , ( 0 , 12 ) ] NEW_LINE q = len ( queries ) NEW_LINE preCalculate ( string , n ) NEW_LINE for i in range ( q ) : NEW_LINE INDENT print ( lastNonRepeating ( string , n , queries [ i ] [ 0 ] , queries [ i ] [ 1 ] ) ) NEW_LINE DEDENT DEDENT


public class GFG { static void findGreater ( int a , int b ) { double x = ( double ) a * ( double ) ( Math . log ( ( double ) ( b ) ) ) ; double y = ( double ) b * ( double ) ( Math . log ( ( double ) ( a ) ) ) ; if ( y > x ) { System . out . println ( " a ^ b ▁ is ▁ greater " ) ; } else if ( y < x ) { System . out . println ( " b ^ a ▁ is ▁ greater " ) ; } else { System . out . println ( " Both ▁ are ▁ equal " ) ; } } public static void main ( String [ ] args ) { int a = 3 , b = 5 , c = 2 , d = 4 ; findGreater ( a , b ) ; findGreater ( c , d ) ; } }

import math NEW_LINE def findGreater ( a , b ) : NEW_LINE INDENT x = a * ( math . log ( b ) ) ; NEW_LINE y = b * ( math . log ( a ) ) ; NEW_LINE if ( y > x ) : NEW_LINE INDENT print ( " a ^ b ▁ is ▁ greater " ) ; NEW_LINE DEDENT elif ( y < x ) : NEW_LINE INDENT print ( " b ^ a ▁ is ▁ greater " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Both ▁ are ▁ equal " ) ; NEW_LINE DEDENT DEDENT a = 3 ; NEW_LINE b = 5 ; NEW_LINE c = 2 ; NEW_LINE d = 4 ; NEW_LINE findGreater ( a , b ) ; NEW_LINE findGreater ( c , d ) ; NEW_LINE


class GFG { static void printFun ( int test ) { if ( test < 1 ) return ; else { System . out . printf ( " % d ▁ " , test ) ; printFun ( test - 1 ) ; System . out . printf ( " % d ▁ " , test ) ; return ; } } public static void main ( String [ ] args ) { int test = 3 ; printFun ( test ) ; } }

def printFun ( test ) : NEW_LINE INDENT if ( test < 1 ) : NEW_LINE INDENT return NEW_LINE DEDENT else : NEW_LINE INDENT print ( test , end = " ▁ " ) NEW_LINE printFun ( test - 1 ) NEW_LINE print ( test , end = " ▁ " ) NEW_LINE return NEW_LINE DEDENT DEDENT test = 3 NEW_LINE printFun ( test ) NEW_LINE


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

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


import java . util . Scanner ; class Pattern { void display ( int n ) { int sp = n / 2 , st = 1 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= sp ; j ++ ) System . out . print ( " ▁ " ) ; int count = st / 2 + 1 ; for ( int k = 1 ; k <= st ; k ++ ) { System . out . print ( count ) ; if ( k <= st / 2 ) count -- ; else count ++ ; } System . out . println ( ) ; if ( i <= n / 2 ) { sp = sp - 1 ; st = st + 2 ; } else { sp = sp + 1 ; st = st - 2 ; } } } public static void main ( String [ ] args ) { Pattern p = new Pattern ( ) ; int n = 7 ; p . display ( n ) ; } }

def display ( n ) : NEW_LINE INDENT sp = n // 2 NEW_LINE st = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , sp + 1 ) : NEW_LINE INDENT print ( " ▁ " , end = ' ▁ ' ) NEW_LINE DEDENT count = st // 2 + 1 NEW_LINE for k in range ( 1 , st + 1 ) : NEW_LINE INDENT print ( count , end = ' ▁ ' ) NEW_LINE if ( k <= ( st // 2 ) ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( ) NEW_LINE if ( i <= n // 2 ) : NEW_LINE INDENT sp -= 1 NEW_LINE st += 2 NEW_LINE DEDENT else : NEW_LINE INDENT sp += 1 NEW_LINE st -= 2 NEW_LINE DEDENT DEDENT DEDENT n = 7 NEW_LINE display ( n ) NEW_LINE


import java . util . * ; class GfG { static boolean validateStackSequence ( int pushed [ ] , int popped [ ] , int len ) { int j = 0 ; Stack < Integer > st = new Stack < > ( ) ; for ( int i = 0 ; i < len ; i ++ ) { st . push ( pushed [ i ] ) ; while ( ! st . empty ( ) && j < len && st . peek ( ) == popped [ j ] ) { st . pop ( ) ; j ++ ; } } return j == len ; } public static void main ( String [ ] args ) { int pushed [ ] = { 1 , 2 , 3 , 4 , 5 } ; int popped [ ] = { 4 , 5 , 3 , 2 , 1 } ; int len = pushed . length ; System . out . println ( ( validateStackSequence ( pushed , popped , len ) ? " True " : " False " ) ) ; } }

def validateStackSequence ( pushed , popped ) : NEW_LINE INDENT j = 0 NEW_LINE stack = [ ] NEW_LINE for x in pushed : NEW_LINE INDENT stack . append ( x ) NEW_LINE while stack and j < len ( popped ) and stack [ - 1 ] == popped [ j ] : NEW_LINE INDENT stack . pop ( ) NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT return j == len ( popped ) NEW_LINE DEDENT pushed = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE popped = [ 4 , 5 , 3 , 2 , 1 ] NEW_LINE print ( validateStackSequence ( pushed , popped ) ) NEW_LINE


class GFG { static boolean isprime ( int x ) { for ( int i = 2 ; i * i <= x ; i ++ ) if ( x % i == 0 ) return false ; return true ; } static boolean isNSqMinusnMSqPrime ( int m , int n ) { if ( n - m == 1 && isprime ( m + n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int m = 13 , n = 16 ; if ( isNSqMinusnMSqPrime ( m , n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def isprime ( x ) : NEW_LINE INDENT for i in range ( 2 , math . sqrt ( x ) ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def isNSqMinusnMSqPrime ( m , n ) : NEW_LINE INDENT if ( n - m == 1 and isprime ( m + n ) ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT m = 13 ; NEW_LINE n = 16 ; NEW_LINE if ( isNSqMinusnMSqPrime ( m , n ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT


public class Improve { static int calculateSquareSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = ( fibo [ 0 ] * fibo [ 0 ] ) + ( fibo [ 1 ] * fibo [ 1 ] ) ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; sum += ( fibo [ i ] * fibo [ i ] ) ; } return sum ; } public static void main ( String args [ ] ) { int n = 6 ; System . out . println ( " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " + calculateSquareSum ( n ) ) ; } }

def calculateSquareSum ( n ) : NEW_LINE INDENT fibo = [ 0 ] * ( n + 1 ) ; NEW_LINE if ( n <= 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT fibo [ 0 ] = 0 ; NEW_LINE fibo [ 1 ] = 1 ; NEW_LINE sum = ( ( fibo [ 0 ] * fibo [ 0 ] ) + ( fibo [ 1 ] * fibo [ 1 ] ) ) ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fibo [ i ] = ( fibo [ i - 1 ] + fibo [ i - 2 ] ) ; NEW_LINE sum += ( fibo [ i ] * fibo [ i ] ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 6 ; NEW_LINE print ( " Sum ▁ of ▁ squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : " , calculateSquareSum ( n ) ) ; NEW_LINE


import java . io . * ; class GFG { static int binarySearchCount ( int arr [ ] , int n , int key ) { int left = 0 ; int right = n - 1 ; int count = 0 ; while ( left <= right ) { int mid = ( right + left ) / 2 ; if ( arr [ mid ] <= key ) { count = mid + 1 ; left = mid + 1 ; } else right = mid - 1 ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 11 , 11 , 16 } ; int key = 11 ; int n = arr . length ; System . out . println ( binarySearchCount ( arr , n , key ) ) ; } }

def binarySearchCount ( arr , n , key ) : NEW_LINE INDENT left = 0 NEW_LINE right = n - 1 NEW_LINE count = 0 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = int ( ( right + left ) / 2 ) NEW_LINE if ( arr [ mid ] <= key ) : NEW_LINE INDENT count = mid + 1 NEW_LINE left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT right = mid - 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 11 , 11 , 16 ] NEW_LINE key = 11 NEW_LINE n = len ( arr ) NEW_LINE print ( binarySearchCount ( arr , n , key ) ) NEW_LINE


import java . util . * ; class GFG { static final int MAX_SIZE = 100005 ; static int [ ] fib = new int [ MAX_SIZE + 1 ] ; static void fibonacci ( ) { int i ; fib [ 0 ] = 0 ; fib [ 1 ] = 1 ; for ( i = 2 ; i <= MAX_SIZE ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static void printLevel ( int level ) { int left_index = ( int ) Math . pow ( 2 , level - 1 ) ; int right_index = ( int ) ( Math . pow ( 2 , level ) - 1 ) ; for ( int i = left_index ; i <= right_index ; i ++ ) { System . out . print ( fib [ i - 1 ] + " ▁ " ) ; } System . out . println ( ) ; } public static void main ( String [ ] args ) { fibonacci ( ) ; int K = 4 ; printLevel ( K ) ; } }

MAX_SIZE = 100005 NEW_LINE fib = [ 0 ] * ( MAX_SIZE + 1 ) NEW_LINE def fibonacci ( ) : NEW_LINE INDENT fib [ 0 ] = 0 NEW_LINE fib [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX_SIZE + 1 ) : NEW_LINE INDENT fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] NEW_LINE DEDENT DEDENT def printLevel ( level ) : NEW_LINE INDENT left_index = pow ( 2 , level - 1 ) NEW_LINE right_index = pow ( 2 , level ) - 1 NEW_LINE for i in range ( left_index , right_index + 1 ) : NEW_LINE INDENT print ( fib [ i - 1 ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT fibonacci ( ) NEW_LINE K = 4 NEW_LINE printLevel ( K ) NEW_LINE


public class GFG { static long fun ( int marks [ ] , int n ) { long dp [ ] = new long [ n ] ; int temp ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( marks [ i ] > marks [ i + 1 ] ) { temp = i ; while ( true ) { if ( ( marks [ temp ] > marks [ temp + 1 ] ) && temp >= 0 ) { if ( dp [ temp ] > dp [ temp + 1 ] ) { temp -= 1 ; continue ; } else { dp [ temp ] = dp [ temp + 1 ] + 1 ; temp -= 1 ; } } else break ; } } else if ( marks [ i ] < marks [ i + 1 ] ) dp [ i + 1 ] = dp [ i ] + 1 ; } int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += dp [ i ] ; return sum ; } public static void main ( String args [ ] ) { int n = 6 ; int marks [ ] = { 1 , 4 , 5 , 2 , 2 , 1 } ; System . out . println ( fun ( marks , n ) ) ; } }

def fun ( marks , n ) : NEW_LINE INDENT dp = [ 1 for i in range ( 0 , n ) ] NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if marks [ i ] > marks [ i + 1 ] : NEW_LINE INDENT temp = i NEW_LINE while True : NEW_LINE INDENT if marks [ temp ] > marks [ temp + 1 ] and temp >= 0 : NEW_LINE INDENT if dp [ temp ] > dp [ temp + 1 ] : NEW_LINE INDENT temp -= 1 NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT dp [ temp ] = dp [ temp + 1 ] + 1 NEW_LINE temp -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT elif marks [ i ] < marks [ i + 1 ] : NEW_LINE INDENT dp [ i + 1 ] = dp [ i ] + 1 NEW_LINE DEDENT DEDENT return ( sum ( dp ) ) NEW_LINE DEDENT n = 6 NEW_LINE marks = [ 1 , 4 , 5 , 2 , 2 , 1 ] NEW_LINE print ( fun ( marks , n ) ) NEW_LINE


import java . io . * ; class GFG { 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 countArray ( int N , int K ) { return binomialCoeff ( K - 1 , N - 1 ) ; } public static void main ( String [ ] args ) { int N = 2 , K = 3 ; System . out . println ( countArray ( N , K ) ) ; } }

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 countArray ( N , K ) : NEW_LINE INDENT return binomialCoeff ( K - 1 , N - 1 ) ; NEW_LINE DEDENT N = 2 ; NEW_LINE K = 3 ; NEW_LINE print ( countArray ( N , K ) ) ; NEW_LINE


import java . util . * ; class GFG { static int fib ( int n ) { double phi = ( 1 + Math . sqrt ( 5 ) ) / 2 ; return ( int ) Math . round ( Math . pow ( phi , n ) / Math . sqrt ( 5 ) ) ; } static int calculateSum ( int l , int r ) { int sum = fib ( r + 2 ) - fib ( l + 1 ) ; return sum ; } static int sumFibonacci ( int k ) { int l = ( k * ( k - 1 ) ) / 2 ; int r = l + k ; int sum = calculateSum ( l , r - 1 ) ; return sum ; } public static void main ( String args [ ] ) { int k = 3 ; System . out . println ( sumFibonacci ( k ) ) ; } }

import math NEW_LINE MAX = 1000000 NEW_LINE def fib ( n ) : NEW_LINE INDENT phi = ( 1 + math . sqrt ( 5 ) ) / 2 NEW_LINE return round ( pow ( phi , n ) / math . sqrt ( 5 ) ) NEW_LINE DEDENT def calculateSum ( l , r ) : NEW_LINE INDENT sum = fib ( r + 2 ) - fib ( l + 1 ) NEW_LINE return sum NEW_LINE DEDENT def sumFibonacci ( k ) : NEW_LINE INDENT l = ( k * ( k - 1 ) ) / 2 NEW_LINE r = l + k NEW_LINE sum = calculateSum ( l , r - 1 ) NEW_LINE return sum NEW_LINE DEDENT k = 3 NEW_LINE print ( sumFibonacci ( k ) ) NEW_LINE


import java . util . * ; class GFG { static int get_permutation ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int result = 0 ; for ( int i = 0 ; i < n ; i ++ ) { result += Math . abs ( arr [ i ] - ( i + 1 ) ) ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . length ; System . out . print ( get_permutation ( arr , n ) ) ; } }

def get_permutation ( arr , n ) : NEW_LINE INDENT arr = sorted ( arr ) NEW_LINE result = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT result += abs ( arr [ i ] - ( i + 1 ) ) NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( get_permutation ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void countPair ( int arr [ ] , int n , int k ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = ( arr [ i ] + k ) % k ; } int hash [ ] = new int [ k ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } for ( int i = 0 ; i < k ; i ++ ) { cnt += ( hash [ i ] * ( hash [ i ] - 1 ) ) / 2 ; } System . out . print ( cnt + " \n " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int k = 2 ; int n = arr . length ; countPair ( arr , n , k ) ; } }

def countPair ( arr , n , k ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] = ( arr [ i ] + k ) % k ; NEW_LINE DEDENT hash = [ 0 ] * k ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ arr [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( k ) : NEW_LINE INDENT cnt += ( hash [ i ] * ( hash [ i ] - 1 ) ) / 2 ; NEW_LINE DEDENT print ( int ( cnt ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE k = 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE countPair ( arr , n , k ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static Vector < Integer > findPrime ( int MAX ) { boolean pm [ ] = new boolean [ MAX + 1 ] ; for ( int i = 0 ; i < pm . length ; i ++ ) pm [ i ] = true ; pm [ 0 ] = pm [ 1 ] = false ; for ( int i = 2 ; i <= MAX ; i ++ ) if ( pm [ i ] ) for ( int j = 2 * i ; j <= MAX ; j += i ) pm [ j ] = false ; Vector < Integer > prime = new Vector < Integer > ( ) ; for ( int i = 0 ; i <= MAX ; i ++ ) if ( pm [ i ] ) prime . add ( i ) ; return prime ; } static int max_element ( int arr [ ] ) { int max = arr [ 0 ] ; for ( int i = 0 ; i < arr . length ; i ++ ) max = Math . max ( max , arr [ i ] ) ; return max ; } static int findSmallest ( int arr [ ] , int n ) { int MAX = max_element ( arr ) ; Vector < Integer > prime = findPrime ( MAX ) ; Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) s . add ( arr [ i ] ) ; long ans = - 1 ; for ( int i = 0 ; i < prime . size ( ) ; i ++ ) { if ( ! s . contains ( prime . get ( i ) ) ) { ans = ( prime . get ( i ) ) ; break ; } } return ( int ) ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 0 , 1 , 2 , 7 } ; int n = arr . length ; if ( findSmallest ( arr , n ) == - 1 ) System . out . print ( " No ▁ prime ▁ number ▁ missing " ) ; else System . out . print ( findSmallest ( arr , n ) ) ; } }

def findPrime ( MAX ) : NEW_LINE INDENT pm = [ True ] * ( MAX + 1 ) NEW_LINE pm [ 0 ] , pm [ 1 ] = False , False NEW_LINE for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if pm [ i ] == True : NEW_LINE INDENT for j in range ( 2 * i , MAX + 1 , i ) : NEW_LINE INDENT pm [ j ] = False NEW_LINE DEDENT DEDENT DEDENT prime = [ ] NEW_LINE for i in range ( 0 , MAX + 1 ) : NEW_LINE INDENT if pm [ i ] == True : NEW_LINE INDENT prime . append ( i ) NEW_LINE DEDENT DEDENT return prime NEW_LINE DEDENT def findSmallest ( arr , n ) : NEW_LINE INDENT MAX = max ( arr ) NEW_LINE prime = findPrime ( MAX ) NEW_LINE s = set ( ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT ans = - 1 NEW_LINE for i in range ( 0 , len ( prime ) ) : NEW_LINE INDENT if prime [ i ] not in s : NEW_LINE INDENT ans = prime [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 0 , 1 , 2 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE if ( findSmallest ( arr , n ) == - 1 ) : NEW_LINE INDENT print ( " No ▁ prime ▁ number ▁ missing " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( findSmallest ( arr , n ) ) NEW_LINE DEDENT DEDENT


class GFG { static double pentdiagonal ( double a ) { if ( a < 0 ) return - 1 ; double d = 1.22 * a ; return d ; } static public void main ( String args [ ] ) { double a = 6 ; System . out . println ( pentdiagonal ( a ) ) ; } }

def pentdiagonal ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT d = 1.22 * a NEW_LINE return d NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 6 NEW_LINE print ( pentdiagonal ( a ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int StrictlyPositiveXor ( int [ ] A , int N ) { int allxor = 0 ; boolean checkallzero = true ; for ( int i = 0 ; i < N ; i += 1 ) { allxor ^= A [ i ] ; if ( A [ i ] > 0 ) checkallzero = false ; } if ( allxor != 0 ) return N ; if ( checkallzero ) return - 1 ; int l = N , r = - 1 ; for ( int i = 0 ; i < N ; i += 1 ) { if ( A [ i ] > 0 ) { l = i + 1 ; break ; } } for ( int i = N - 1 ; i >= 0 ; i -= 1 ) { if ( A [ i ] > 0 ) { r = i + 1 ; break ; } } return Math . max ( N - l , r - 1 ) ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 0 , 0 , 1 } ; int N = A . length ; System . out . print ( StrictlyPositiveXor ( A , N ) ) ; } }

def StrictlyPositiveXor ( A , N ) : NEW_LINE INDENT allxor = 0 ; NEW_LINE checkallzero = True ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT allxor ^= A [ i ] ; NEW_LINE if ( A [ i ] > 0 ) : NEW_LINE INDENT checkallzero = False ; NEW_LINE DEDENT DEDENT if ( allxor != 0 ) : NEW_LINE INDENT return N ; NEW_LINE DEDENT if ( checkallzero ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT l = N ; r = - 1 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] > 0 ) : NEW_LINE INDENT l = i + 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( A [ i ] > 0 ) : NEW_LINE INDENT r = i + 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT return max ( N - l , r - 1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 0 , 0 , 1 ] ; NEW_LINE N = len ( A ) ; NEW_LINE print ( StrictlyPositiveXor ( A , N ) ) ; NEW_LINE DEDENT


class GFG { public static int search ( int arr [ ] , int x ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == x ) return i ; } return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 10 , 40 } ; int x = 10 ; int result = search ( arr , x ) ; if ( result == - 1 ) System . out . print ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; else System . out . print ( " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }

def search ( arr , n , x ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == x ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 10 , 40 ] ; NEW_LINE x = 10 ; NEW_LINE n = len ( arr ) ; NEW_LINE result = search ( arr , n , x ) NEW_LINE if ( result == - 1 ) : NEW_LINE INDENT print ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Element ▁ is ▁ present ▁ at ▁ index " , result ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int calculateSum ( int n ) { int a1 = 1 , a2 = 2 ; int r = 2 ; int d = 1 ; return ( n ) * ( 2 * a1 + ( n - 1 ) * d ) / 2 + a2 * ( int ) ( Math . pow ( r , n ) - 1 ) / ( r - 1 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }

def calculateSum ( n ) : NEW_LINE INDENT a1 = 1 ; NEW_LINE a2 = 2 ; NEW_LINE r = 2 ; NEW_LINE d = 1 ; NEW_LINE return ( ( n ) * ( 2 * a1 + ( n - 1 ) * d ) / 2 + a2 * ( pow ( r , n ) - 1 ) / ( r - 1 ) ) ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( " Sum ▁ = " , int ( calculateSum ( n ) ) ) NEW_LINE