class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < size ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; if ( max_so_far < max_ending_here ) max_so_far = max_ending_here ; if ( max_ending_here < 0 ) max_ending_here = 0 ; } return max_so_far ; } static int maximizeSum ( int a [ ] , int n ) { int cnt = 0 ; int mini = Integer . MAX_VALUE ; int minSubarray = Integer . MAX_VALUE ; int sum = maxSubArraySum ( a , n ) ; int max_so_far = Integer . MIN_VALUE , max_ending_here = 0 ; for ( int i = 0 ; i < n ; i ++ ) { max_ending_here = max_ending_here + a [ i ] ; cnt ++ ; minSubarray = Math . min ( a [ i ] , minSubarray ) ; if ( sum == max_ending_here ) { if ( cnt == 1 ) mini = Math . min ( mini , 0 ) ; else mini = Math . min ( mini , minSubarray ) ; } if ( max_ending_here < 0 ) { max_ending_here = 0 ; cnt = 0 ; minSubarray = Integer . MAX_VALUE ; } } return sum - mini ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , - 2 , 3 } ; int n = a . length ; System . out . println ( maximizeSum ( a , n ) ) ; } }

import sys ; NEW_LINE def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = - ( sys . maxsize - 1 ) ; NEW_LINE max_ending_here = 0 ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] ; NEW_LINE if ( max_so_far < max_ending_here ) : NEW_LINE INDENT max_so_far = max_ending_here ; NEW_LINE DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 ; NEW_LINE DEDENT DEDENT return max_so_far ; NEW_LINE DEDENT def maximizeSum ( a , n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE mini = sys . maxsize ; NEW_LINE minSubarray = sys . maxsize ; NEW_LINE sum = maxSubArraySum ( a , n ) ; NEW_LINE max_so_far = - ( sys . maxsize - 1 ) ; NEW_LINE max_ending_here = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT max_ending_here = max_ending_here + a [ i ] ; NEW_LINE cnt += 1 ; NEW_LINE minSubarray = min ( a [ i ] , minSubarray ) ; NEW_LINE if ( sum == max_ending_here ) : NEW_LINE INDENT if ( cnt == 1 ) : NEW_LINE INDENT mini = min ( mini , 0 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT mini = min ( mini , minSubarray ) ; NEW_LINE DEDENT DEDENT if ( max_ending_here < 0 ) : NEW_LINE INDENT max_ending_here = 0 ; NEW_LINE cnt = 0 ; NEW_LINE minSubarray = sys . maxsize ; NEW_LINE DEDENT DEDENT return sum - mini ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 , - 2 , 3 ] ; NEW_LINE n = len ( a ) NEW_LINE print ( maximizeSum ( a , n ) ) ; NEW_LINE DEDENT


class GFG { static int findSum ( int [ ] arr , int n , int k ) { int ans = arr [ n - k - 1 ] - arr [ 0 ] ; for ( int i = 1 ; i <= k ; i ++ ) { ans = Math . min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 100 , 120 , 140 } ; int k = 2 ; int n = arr . length ; System . out . println ( findSum ( arr , n , k ) ) ; } }

def findSum ( arr , n , k ) : NEW_LINE INDENT ans = arr [ n - k - 1 ] - arr [ 0 ] ; NEW_LINE for i in range ( 1 , k + 1 ) : NEW_LINE INDENT ans = min ( arr [ n - 1 - ( k - i ) ] - arr [ i ] , ans ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 100 , 120 , 140 ] ; NEW_LINE k = 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findSum ( arr , n , k ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void longest ( int a [ ] , int n , int k ) { int [ ] freq = new int [ 7 ] ; int start = 0 , end = 0 , now = 0 , l = 0 ; for ( int i = 0 ; i < n ; i ++ ) { freq [ a [ i ] ] ++ ; if ( freq [ a [ i ] ] == 1 ) now ++ ; while ( now > k ) { freq [ a [ l ] ] -- ; if ( freq [ a [ l ] ] == 0 ) now -- ; l ++ ; } if ( i - l + 1 >= end - start + 1 ) { end = i ; start = l ; } } for ( int i = start ; i <= end ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int a [ ] = { 6 , 5 , 1 , 2 , 3 , 2 , 1 , 4 , 5 } ; int n = a . length ; int k = 3 ; longest ( a , n , k ) ; } }

def longest ( a , n , k ) : NEW_LINE INDENT freq = [ 0 ] * n NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE now = 0 NEW_LINE l = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ a [ i ] ] += 1 NEW_LINE if ( freq [ a [ i ] ] == 1 ) : NEW_LINE INDENT now += 1 NEW_LINE DEDENT while ( now > k ) : NEW_LINE INDENT freq [ a [ l ] ] -= 1 NEW_LINE if ( freq [ a [ l ] ] == 0 ) : NEW_LINE INDENT now -= 1 NEW_LINE DEDENT l += 1 NEW_LINE DEDENT if ( i - l + 1 >= end - start + 1 ) : NEW_LINE INDENT end = i NEW_LINE start = l NEW_LINE DEDENT DEDENT for i in range ( start , end + 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 6 , 5 , 1 , 2 , 3 , 2 , 1 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE longest ( a , n , k ) NEW_LINE DEDENT


class GFG { public static void printMultiples ( int N ) { for ( int i = 1 ; i <= N ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) { System . out . print ( ( i * j ) + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int N = 5 ; printMultiples ( N ) ; } }

def prMultiples ( N ) : NEW_LINE INDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( ( i * j ) , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE prMultiples ( N ) NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int maxEvenLenSum ( int arr [ ] , int n ) { if ( n < 2 ) return 0 ; int [ ] dp = new int [ n ] ; dp [ n - 1 ] = 0 ; dp [ n - 2 ] = arr [ n - 2 ] + arr [ n - 1 ] ; for ( int i = n - 3 ; i >= 0 ; i -- ) { dp [ i ] = arr [ i ] + arr [ i + 1 ] ; if ( dp [ i + 2 ] > 0 ) dp [ i ] += dp [ i + 2 ] ; } int maxSum = Arrays . stream ( dp ) . max ( ) . getAsInt ( ) ; return maxSum ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 9 , - 8 , 9 , 10 } ; int n = arr . length ; System . out . println ( maxEvenLenSum ( arr , n ) ) ; } }

def maxEvenLenSum ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT dp = [ 0 for i in range ( n ) ] NEW_LINE dp [ n - 1 ] = 0 NEW_LINE dp [ n - 2 ] = arr [ n - 2 ] + arr [ n - 1 ] NEW_LINE for i in range ( n - 3 , - 1 , - 1 ) : NEW_LINE INDENT dp [ i ] = arr [ i ] + arr [ i + 1 ] NEW_LINE if ( dp [ i + 2 ] > 0 ) : NEW_LINE INDENT dp [ i ] += dp [ i + 2 ] NEW_LINE DEDENT DEDENT maxSum = max ( dp ) NEW_LINE return maxSum NEW_LINE DEDENT arr = [ 8 , 9 , - 8 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxEvenLenSum ( arr , n ) ) NEW_LINE


import java . util . HashMap ; class Test { static int countDistictSubarray ( int arr [ ] , int n ) { HashMap < Integer , Integer > vis = new HashMap < Integer , Integer > ( ) { @ Override public Integer get ( Object key ) { if ( ! containsKey ( key ) ) return 0 ; return super . get ( key ) ; } } ; for ( int i = 0 ; i < n ; ++ i ) vis . put ( arr [ i ] , 1 ) ; int k = vis . size ( ) ; vis . clear ( ) ; int ans = 0 , right = 0 , window = 0 ; for ( int left = 0 ; left < n ; ++ left ) { while ( right < n && window < k ) { vis . put ( arr [ right ] , vis . get ( arr [ right ] ) + 1 ) ; if ( vis . get ( arr [ right ] ) == 1 ) ++ window ; ++ right ; } if ( window == k ) ans += ( n - right + 1 ) ; vis . put ( arr [ left ] , vis . get ( arr [ left ] ) - 1 ) ; if ( vis . get ( arr [ left ] ) == 0 ) -- window ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 1 , 3 , 2 , 3 } ; System . out . println ( countDistictSubarray ( arr , arr . length ) ) ; } }

def countDistictSubarray ( arr , n ) : NEW_LINE INDENT vis = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT vis [ arr [ i ] ] = 1 NEW_LINE DEDENT k = len ( vis ) NEW_LINE vid = dict ( ) NEW_LINE ans = 0 NEW_LINE right = 0 NEW_LINE window = 0 NEW_LINE for left in range ( n ) : NEW_LINE INDENT while ( right < n and window < k ) : NEW_LINE INDENT if arr [ right ] in vid . keys ( ) : NEW_LINE INDENT vid [ arr [ right ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT vid [ arr [ right ] ] = 1 NEW_LINE DEDENT if ( vid [ arr [ right ] ] == 1 ) : NEW_LINE INDENT window += 1 NEW_LINE DEDENT right += 1 NEW_LINE DEDENT if ( window == k ) : NEW_LINE INDENT ans += ( n - right + 1 ) NEW_LINE DEDENT vid [ arr [ left ] ] -= 1 NEW_LINE if ( vid [ arr [ left ] ] == 0 ) : NEW_LINE INDENT window -= 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 2 , 1 , 3 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countDistictSubarray ( arr , n ) ) NEW_LINE


class GFG { static int nth_term ( int a , int b , int n ) { int z = 0 ; if ( n % 6 == 1 ) z = a ; else if ( n % 6 == 2 ) z = b ; else if ( n % 6 == 3 ) z = b - a ; else if ( n % 6 == 4 ) z = - a ; else if ( n % 6 == 5 ) z = - b ; if ( n % 6 == 0 ) z = - ( b - a ) ; return z ; } public static void main ( String [ ] args ) { int a = 10 , b = 17 , n = 3 ; System . out . println ( nth_term ( a , b , n ) ) ; } }

def nth_term ( a , b , n ) : NEW_LINE INDENT z = 0 NEW_LINE if ( n % 6 == 1 ) : NEW_LINE INDENT z = a NEW_LINE DEDENT elif ( n % 6 == 2 ) : NEW_LINE INDENT z = b NEW_LINE DEDENT elif ( n % 6 == 3 ) : NEW_LINE INDENT z = b - a NEW_LINE DEDENT elif ( n % 6 == 4 ) : NEW_LINE INDENT z = - a NEW_LINE DEDENT elif ( n % 6 == 5 ) : NEW_LINE INDENT z = - b NEW_LINE DEDENT if ( n % 6 == 0 ) : NEW_LINE INDENT z = - ( b - a ) NEW_LINE DEDENT return z NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 10 NEW_LINE b = 17 NEW_LINE n = 3 NEW_LINE print ( nth_term ( a , b , n ) ) NEW_LINE DEDENT


public class GFG { static int totalDigits ( int n ) { int number_of_digits = 0 ; for ( int i = 1 ; i <= n ; i *= 10 ) number_of_digits += ( n - i + 1 ) ; return number_of_digits ; } public static void main ( String [ ] args ) { int n = 13 ; System . out . println ( totalDigits ( n ) ) ; } }

def totalDigits ( n ) : NEW_LINE INDENT number_of_digits = 0 ; NEW_LINE for i in range ( 1 , n , 10 ) : NEW_LINE INDENT number_of_digits = ( number_of_digits + ( n - i + 1 ) ) ; NEW_LINE DEDENT return number_of_digits ; NEW_LINE DEDENT n = 13 ; NEW_LINE s = totalDigits ( n ) + 1 ; NEW_LINE print ( s ) ; NEW_LINE


import java . io . * ; public class GFG { static void isSubsetAndZero ( int array [ ] , int length , int N ) { int arrAnd = array [ 0 ] ; for ( int i = 1 ; i < length ; i ++ ) { arrAnd = arrAnd & array [ i ] ; } if ( ( arrAnd & N ) == 0 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int array [ ] = { 1 , 2 , 4 } ; int length = array . length ; int N = 3 ; isSubsetAndZero ( array , length , N ) ; } }

def isSubsetAndZero ( array , length , N ) : NEW_LINE INDENT arrAnd = array [ 0 ] NEW_LINE for i in range ( 1 , length ) : NEW_LINE INDENT arrAnd = arrAnd & array [ i ] NEW_LINE DEDENT if ( ( arrAnd & N ) == 0 ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT array = [ 1 , 2 , 4 ] NEW_LINE length = len ( array ) NEW_LINE N = 3 NEW_LINE isSubsetAndZero ( array , length , N ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int maximumAbsolute ( int arr [ ] , int n ) { int mn = Integer . MAX_VALUE ; int mx = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( i > 0 && arr [ i ] == - 1 && arr [ i - 1 ] != - 1 ) { mn = Math . min ( mn , arr [ i - 1 ] ) ; mx = Math . max ( mx , arr [ i - 1 ] ) ; } if ( i < n - 1 && arr [ i ] == - 1 && arr [ i + 1 ] != - 1 ) { mn = Math . min ( mn , arr [ i + 1 ] ) ; mx = Math . max ( mx , arr [ i + 1 ] ) ; } } int common_integer = ( mn + mx ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == - 1 ) arr [ i ] = common_integer ; } int max_diff = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int diff = Math . abs ( arr [ i ] - arr [ i + 1 ] ) ; if ( diff > max_diff ) max_diff = diff ; } return max_diff ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , - 1 , 11 , - 1 , 3 , - 1 } ; int n = arr . length ; System . out . print ( maximumAbsolute ( arr , n ) ) ; } }

def maximumAbsolute ( arr , n ) : NEW_LINE INDENT mn = 10 ** 9 NEW_LINE mx = - 10 ** 9 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i > 0 and arr [ i ] == - 1 and arr [ i - 1 ] != - 1 ) : NEW_LINE INDENT mn = min ( mn , arr [ i - 1 ] ) NEW_LINE mx = max ( mx , arr [ i - 1 ] ) NEW_LINE DEDENT if ( i < n - 1 and arr [ i ] == - 1 and arr [ i + 1 ] != - 1 ) : NEW_LINE INDENT mn = min ( mn , arr [ i + 1 ] ) NEW_LINE mx = max ( mx , arr [ i + 1 ] ) NEW_LINE DEDENT DEDENT common_integer = ( mn + mx ) // 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == - 1 ) : NEW_LINE INDENT arr [ i ] = common_integer NEW_LINE DEDENT DEDENT max_diff = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT diff = abs ( arr [ i ] - arr [ i + 1 ] ) NEW_LINE if ( diff > max_diff ) : NEW_LINE INDENT max_diff = diff NEW_LINE DEDENT DEDENT return max_diff NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 1 , - 1 , 11 , - 1 , 3 , - 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maximumAbsolute ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void pattern ( int n ) { int i , j ; for ( i = n - 1 ; i >= 0 ; i -- ) { for ( j = n - 1 ; j > i ; j -- ) { System . out . print ( " ▁ " ) ; } System . out . print ( ( char ) ( i + 65 ) ) ; for ( j = 1 ; j < ( i * 2 ) ; j ++ ) System . out . print ( " ▁ " ) ; if ( i >= 1 ) System . out . print ( ( char ) ( i + 65 ) ) ; System . out . print ( " \n " ) ; } } public static void main ( String args [ ] ) { int n = 5 ; pattern ( n ) ; } }

import math as mt NEW_LINE def pattern ( n ) : NEW_LINE INDENT i = n - 1 NEW_LINE j = 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( n - 1 , i , - 1 ) : NEW_LINE INDENT print ( ' ▁ ' , end = ' ' ) NEW_LINE DEDENT print ( chr ( i + 65 ) , end = ' ' ) NEW_LINE for j in range ( 1 , i * 2 ) : NEW_LINE INDENT print ( ' ▁ ' , end = ' ' ) NEW_LINE DEDENT if ( i >= 1 ) : NEW_LINE INDENT print ( chr ( i + 65 ) , end = ' ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE pattern ( n ) NEW_LINE


import java . io . * ; class GFG { static int octalToDecimal ( int n ) { int num = n ; int dec_value = 0 ; int base = 1 ; int temp = num ; while ( temp > 0 ) { int last_digit = temp % 10 ; temp = temp / 10 ; dec_value += last_digit * base ; base = base * 8 ; } return dec_value ; } public static void main ( String [ ] args ) { int num = 67 ; System . out . println ( octalToDecimal ( num ) ) ; } }

def octalToDecimal ( n ) : NEW_LINE INDENT num = n ; NEW_LINE dec_value = 0 ; NEW_LINE base = 1 ; NEW_LINE temp = num ; NEW_LINE while ( temp ) : NEW_LINE INDENT last_digit = temp % 10 ; NEW_LINE temp = int ( temp / 10 ) ; NEW_LINE dec_value += last_digit * base ; NEW_LINE base = base * 8 ; NEW_LINE DEDENT return dec_value ; NEW_LINE DEDENT num = 67 ; NEW_LINE print ( octalToDecimal ( num ) ) ; NEW_LINE


class OddOccurance { int getOddOccurrence ( int ar [ ] , int ar_size ) { int i ; int res = 0 ; for ( i = 0 ; i < ar_size ; i ++ ) { res = res ^ ar [ i ] ; } return res ; } public static void main ( String [ ] args ) { OddOccurance occur = new OddOccurance ( ) ; int ar [ ] = new int [ ] { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = ar . length ; System . out . println ( occur . getOddOccurrence ( ar , n ) ) ; } }

def getOddOccurrence ( arr ) : NEW_LINE INDENT res = 0 NEW_LINE for element in arr : NEW_LINE INDENT res = res ^ element NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ] NEW_LINE print ( " % d " % getOddOccurrence ( arr ) ) NEW_LINE


public class GFG { public static int find_value ( int a [ ] , int n , int k ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += a [ i ] ; } return sum % k ; } public static void main ( String [ ] args ) { int n = 5 , k = 3 ; int a [ ] = { 12 , 4 , 13 , 0 , 5 } ; System . out . println ( find_value ( a , n , k ) ) ; } }

def find_value ( a , n , k ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT return sum % k NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , k = 5 , 3 ; NEW_LINE a = [ 12 , 4 , 13 , 0 , 5 ] ; NEW_LINE print ( find_value ( a , n , k ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static Vector < Integer > Diffs ( int [ ] a , int k ) { Vector < Integer > out = new Vector < Integer > ( ) ; int [ ] inc , dec ; inc = new int [ a . length ] ; dec = new int [ a . length ] ; int inc_sum = 0 ; int dec_sum = 0 ; for ( int i = 0 ; i < a . length ; ++ i ) { for ( int j = i - 1 ; j >= 0 && j > i - k && a [ j + 1 ] > a [ j ] ; -- j ) { ++ inc [ j ] ; ++ inc_sum ; } for ( int j = i - 1 ; j >= 0 && j > i - k && a [ j + 1 ] < a [ j ] ; -- j ) { ++ dec [ j ] ; ++ dec_sum ; } if ( i >= k - 1 ) { if ( i >= k ) { inc_sum -= inc [ i - k ] ; dec_sum -= dec [ i - k ] ; } out . add ( inc_sum - dec_sum ) ; } } return out ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 20 , 30 , 15 , 15 } ; Vector < Integer > out = Diffs ( arr , 3 ) ; for ( int n : out ) System . out . print ( n + " , ▁ " ) ; } }

def Diffs ( a , k ) : NEW_LINE INDENT out , inc , dec = [ ] , [ 0 ] * len ( a ) , [ 0 ] * len ( a ) NEW_LINE inc_sum , dec_sum = 0 , 0 NEW_LINE for i in range ( 0 , len ( a ) ) : NEW_LINE INDENT j = i - 1 NEW_LINE while ( j >= 0 and j > i - k and a [ j + 1 ] > a [ j ] ) : NEW_LINE INDENT inc [ j ] += 1 NEW_LINE inc_sum += 1 NEW_LINE j -= 1 NEW_LINE DEDENT j = i - 1 NEW_LINE while ( j >= 0 and j > i - k and a [ j + 1 ] < a [ j ] ) : NEW_LINE INDENT dec [ j ] += 1 NEW_LINE dec_sum += 1 NEW_LINE j -= 1 NEW_LINE DEDENT if i >= k - 1 : NEW_LINE INDENT if i >= k : NEW_LINE INDENT inc_sum -= inc [ i - k ] NEW_LINE dec_sum -= dec [ i - k ] NEW_LINE DEDENT out . append ( inc_sum - dec_sum ) NEW_LINE DEDENT DEDENT return out NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT out = Diffs ( [ 10 , 20 , 30 , 15 , 15 ] , 3 ) NEW_LINE for n in out : NEW_LINE INDENT print ( n , end = " , ▁ " ) NEW_LINE DEDENT DEDENT


public class Evaluate_BoolExp { static int evaluateBoolExpr ( StringBuffer s ) { int n = s . length ( ) ; for ( int i = 0 ; i < n ; i += 2 ) { if ( i + 1 < n && i + 2 < n ) { if ( s . charAt ( i + 1 ) == ' A ' ) { if ( s . charAt ( i + 2 ) == '0' || s . charAt ( i ) == 0 ) s . setCharAt ( i + 2 , '0' ) ; else s . setCharAt ( i + 2 , '1' ) ; } else if ( ( i + 1 ) < n && s . charAt ( i + 1 ) == ' B ' ) { if ( s . charAt ( i + 2 ) == '1' || s . charAt ( i ) == '1' ) s . setCharAt ( i + 2 , '1' ) ; else s . setCharAt ( i + 2 , '0' ) ; } else { if ( s . charAt ( i + 2 ) == s . charAt ( i ) ) s . setCharAt ( i + 2 , '0' ) ; else s . setCharAt ( i + 2 , '1' ) ; } } } return s . charAt ( n - 1 ) - '0' ; } public static void main ( String [ ] args ) { String s = "1C1B1B0A0" ; StringBuffer sb = new StringBuffer ( s ) ; System . out . println ( evaluateBoolExpr ( sb ) ) ; } }

import math as mt NEW_LINE def evaluateBoolExpr ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE for i in range ( 0 , n - 2 , 2 ) : NEW_LINE INDENT if ( s [ i + 1 ] == " A " ) : NEW_LINE INDENT if ( s [ i + 2 ] == "0" or s [ i ] == "0" ) : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT DEDENT elif ( s [ i + 1 ] == " B " ) : NEW_LINE INDENT if ( s [ i + 2 ] == "1" or s [ i ] == "1" ) : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( s [ i + 2 ] == s [ i ] ) : NEW_LINE INDENT s [ i + 2 ] = "0" NEW_LINE DEDENT else : NEW_LINE INDENT s [ i + 2 ] = "1" NEW_LINE DEDENT DEDENT DEDENT return ord ( s [ n - 1 ] ) - ord ( "0" ) NEW_LINE DEDENT s = "1C1B1B0A0" NEW_LINE string = [ s [ i ] for i in range ( len ( s ) ) ] NEW_LINE print ( evaluateBoolExpr ( string ) ) NEW_LINE


import java . io . * ; class GFG { static void printPair ( int g , int l ) { System . out . print ( g + " ▁ " + l ) ; } public static void main ( String [ ] args ) { int g = 3 , l = 12 ; printPair ( g , l ) ; } }

def printPair ( g , l ) : NEW_LINE INDENT print ( g , l ) NEW_LINE DEDENT g = 3 ; l = 12 ; NEW_LINE printPair ( g , l ) ; NEW_LINE


class GFG { static int no_of_ways ( int a [ ] , int n ) { int count_0 = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) count_0 ++ ; else count_1 ++ ; } if ( count_1 % 2 == 0 ) return count_0 ; else return count_1 ; } public static void main ( String [ ] args ) { int n = 4 ; int a1 [ ] = { 1 , 1 , 0 , 0 } ; System . out . println ( no_of_ways ( a1 , n ) ) ; n = 5 ; int a2 [ ] = { 1 , 1 , 1 , 0 , 0 } ; System . out . println ( no_of_ways ( a2 , n ) ) ; n = 5 ; int a3 [ ] = { 1 , 1 , 0 , 0 , 0 } ; System . out . println ( no_of_ways ( a3 , n ) ) ; n = 6 ; int a4 [ ] = { 1 , 1 , 1 , 0 , 0 , 0 } ; System . out . println ( no_of_ways ( a4 , n ) ) ; } }

def no_of_ways ( a , n ) : NEW_LINE INDENT count_0 = 0 NEW_LINE count_1 = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT count_0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_1 += 1 NEW_LINE DEDENT DEDENT if ( count_1 % 2 == 0 ) : NEW_LINE INDENT return count_0 NEW_LINE DEDENT else : NEW_LINE INDENT return count_1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE a1 = [ 1 , 1 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a1 , n ) ) NEW_LINE n = 5 NEW_LINE a2 = [ 1 , 1 , 1 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a2 , n ) ) NEW_LINE n = 5 NEW_LINE a3 = [ 1 , 1 , 0 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a3 , n ) ) NEW_LINE n = 6 NEW_LINE a4 = [ 1 , 1 , 1 , 0 , 0 , 0 ] NEW_LINE print ( no_of_ways ( a4 , n ) ) NEW_LINE DEDENT


class FindNth { static int printNthElement ( int n ) { int arr [ ] = new int [ n + 1 ] ; arr [ 1 ] = 1 ; arr [ 2 ] = 2 ; for ( int i = 3 ; i <= n ; i ++ ) { if ( i % 2 != 0 ) arr [ i ] = arr [ i / 2 ] * 10 + 1 ; else arr [ i ] = arr [ ( i / 2 ) - 1 ] * 10 + 2 ; } return arr [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( printNthElement ( n ) ) ; } }

def printNthElement ( n ) : NEW_LINE INDENT arr = [ 0 ] * ( n + 1 ) ; NEW_LINE arr [ 1 ] = 1 NEW_LINE arr [ 2 ] = 2 NEW_LINE for i in range ( 3 , n + 1 ) : NEW_LINE INDENT if ( i % 2 != 0 ) : NEW_LINE INDENT arr [ i ] = arr [ i // 2 ] * 10 + 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = arr [ ( i // 2 ) - 1 ] * 10 + 2 NEW_LINE DEDENT DEDENT return arr [ n ] NEW_LINE DEDENT n = 5 NEW_LINE print ( printNthElement ( n ) ) NEW_LINE


import java . io . * ; class SmallPrime { static boolean isPrime ( int n ) { int i , c = 0 ; for ( i = 1 ; i < n / 2 ; i ++ ) { if ( n % i == 0 ) c ++ ; } if ( c == 1 ) { return true ; } else { return false ; } } static void findMinNum ( int arr [ ] , int n ) { int first = 0 , last = 0 , num , rev , i ; int hash [ ] = new int [ 10 ] ; for ( i = 0 ; i < n ; i ++ ) { hash [ arr [ i ] ] ++ ; } System . out . print ( " Minimum ▁ number : ▁ " ) ; for ( i = 0 ; i <= 9 ; i ++ ) { for ( int j = 0 ; j < hash [ i ] ; j ++ ) System . out . print ( i ) ; } System . out . println ( ) ; System . out . println ( ) ; for ( i = 0 ; i <= 9 ; i ++ ) { if ( hash [ i ] != 0 ) { first = i ; break ; } } for ( i = 9 ; i >= 0 ; i -- ) { if ( hash [ i ] != 0 ) { last = i ; break ; } } num = first * 10 + last ; rev = last * 10 + first ; System . out . print ( " Prime ▁ combinations : ▁ " ) ; if ( isPrime ( num ) && isPrime ( rev ) ) { System . out . println ( num + " ▁ " + rev ) ; } else if ( isPrime ( num ) ) { System . out . println ( num ) ; } else if ( isPrime ( rev ) ) { System . out . println ( rev ) ; } else { System . out . println ( " No ▁ combinations ▁ exist " ) ; } } public static void main ( String [ ] args ) { SmallPrime smallprime = new SmallPrime ( ) ; int arr [ ] = { 1 , 2 , 4 , 7 , 8 } ; smallprime . findMinNum ( arr , 5 ) ; } }

import math as mt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT i , c = 0 , 0 NEW_LINE for i in range ( 1 , n // 2 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT DEDENT if ( c == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findMinNum ( arr , n ) : NEW_LINE INDENT first , last = 0 , 0 NEW_LINE Hash = [ 0 for i in range ( 10 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT Hash [ arr [ i ] ] += 1 NEW_LINE DEDENT print ( " Minimum ▁ number : ▁ " , end = " " ) NEW_LINE for i in range ( 0 , 10 ) : NEW_LINE INDENT for j in range ( Hash [ i ] ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( Hash [ i ] != 0 ) : NEW_LINE INDENT first = i NEW_LINE break NEW_LINE DEDENT DEDENT for i in range ( 9 , - 1 , - 1 ) : NEW_LINE INDENT if ( Hash [ i ] != 0 ) : NEW_LINE INDENT last = i NEW_LINE break NEW_LINE DEDENT DEDENT num = first * 10 + last NEW_LINE rev = last * 10 + first NEW_LINE print ( " Prime ▁ combinations : ▁ " , end = " " ) NEW_LINE if ( isPrime ( num ) and isPrime ( rev ) ) : NEW_LINE INDENT print ( num , " ▁ " , rev ) NEW_LINE DEDENT elif ( isPrime ( num ) ) : NEW_LINE INDENT print ( num ) NEW_LINE DEDENT elif ( isPrime ( rev ) ) : NEW_LINE INDENT print ( rev ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ combinations ▁ exist " ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 4 , 7 , 8 ] NEW_LINE findMinNum ( arr , 5 ) NEW_LINE


import java . util . * ; class GFG { static String check ( int [ ] Arr , int n , int M , int K ) { int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( Arr [ i ] == 0 ) flag = 1 ; } if ( K % 2 != 0 && flag == 0 ) return " No " ; else return " Yes " ; } public static void main ( String args [ ] ) { int [ ] Arr = { 1 , 1 , 2 , 4 , 7 , 8 } ; int M = 5 ; int K = 6 ; int n = Arr . length ; System . out . println ( check ( Arr , n , M , K ) ) ; } }

def check ( Arr , n , M , K ) : NEW_LINE INDENT flag = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( Arr [ i ] == 0 ) : NEW_LINE INDENT flag = 1 NEW_LINE DEDENT DEDENT if ( K % 2 != 0 and flag == 0 ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT else : NEW_LINE INDENT return " Yes " ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Arr = [ 1 , 1 , 2 , 4 , 7 , 8 ] NEW_LINE M = 5 ; NEW_LINE K = 6 ; NEW_LINE n = len ( Arr ) ; NEW_LINE print ( check ( Arr , n , M , K ) ) NEW_LINE DEDENT


import java . util . Vector ; class Test { static final int MAXN = 100001 ; static int spf [ ] = new int [ MAXN ] ; static void sieve ( ) { spf [ 1 ] = 1 ; for ( int i = 2 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static Vector < Integer > getFactorization ( int x ) { Vector < Integer > ret = new Vector < > ( ) ; while ( x != 1 ) { ret . add ( spf [ x ] ) ; x = x / spf [ x ] ; } return ret ; } public static void main ( String args [ ] ) { sieve ( ) ; int x = 12246 ; System . out . print ( " prime ▁ factorization ▁ for ▁ " + x + " ▁ : ▁ " ) ; Vector < Integer > p = getFactorization ( x ) ; for ( int i = 0 ; i < p . size ( ) ; i ++ ) System . out . print ( p . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } }

import math as mt NEW_LINE MAXN = 100001 NEW_LINE spf = [ 0 for i in range ( MAXN ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT spf [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAXN ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAXN , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT for i in range ( 3 , mt . ceil ( mt . sqrt ( MAXN ) ) ) : NEW_LINE INDENT if ( spf [ i ] == i ) : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE INDENT if ( spf [ j ] == j ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def getFactorization ( x ) : NEW_LINE INDENT ret = list ( ) NEW_LINE while ( x != 1 ) : NEW_LINE INDENT ret . append ( spf [ x ] ) NEW_LINE x = x // spf [ x ] NEW_LINE DEDENT return ret NEW_LINE DEDENT sieve ( ) NEW_LINE x = 12246 NEW_LINE print ( " prime ▁ factorization ▁ for " , x , " : ▁ " , end = " " ) NEW_LINE p = getFactorization ( x ) NEW_LINE for i in range ( len ( p ) ) : NEW_LINE INDENT print ( p [ i ] , end = " ▁ " ) NEW_LINE DEDENT


class GFG { static class Node { int data ; Node next ; } ; static int getIntesectionNode ( Node head1 , Node head2 ) { Node current1 = head1 ; Node current2 = head2 ; if ( current1 == null || current2 == null ) return - 1 ; while ( current1 != null && current2 != null && current1 != current2 ) { current1 = current1 . next ; current2 = current2 . next ; if ( current1 == current2 ) return current1 . data ; if ( current1 == null ) current1 = head2 ; if ( current2 == null ) current2 = head1 ; } return current1 . data ; } public static void main ( String [ ] args ) { Node newNode ; Node head1 = new Node ( ) ; head1 . data = 10 ; Node head2 = new Node ( ) ; head2 . data = 3 ; newNode = new Node ( ) ; newNode . data = 6 ; head2 . next = newNode ; newNode = new Node ( ) ; newNode . data = 9 ; head2 . next . next = newNode ; newNode = new Node ( ) ; newNode . data = 15 ; head1 . next = newNode ; head2 . next . next . next = newNode ; newNode = new Node ( ) ; newNode . data = 30 ; head1 . next . next = newNode ; head1 . next . next . next = null ; System . out . print ( getIntesectionNode ( head1 , head2 ) ) ; } }

class new_Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def getIntesectionNode ( head1 , head2 ) : NEW_LINE INDENT current1 = head1 NEW_LINE current2 = head2 NEW_LINE if ( not current1 or not current2 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT while ( current1 and current2 and current1 != current2 ) : NEW_LINE INDENT current1 = current1 . next NEW_LINE current2 = current2 . next NEW_LINE if ( current1 == current2 ) : NEW_LINE INDENT return current1 . data NEW_LINE DEDENT if ( not current1 ) : NEW_LINE INDENT current1 = head2 NEW_LINE DEDENT if ( not current2 ) : NEW_LINE INDENT current2 = head1 NEW_LINE DEDENT DEDENT return current1 . data NEW_LINE DEDENT head1 = new_Node ( 10 ) NEW_LINE head2 = new_Node ( 3 ) NEW_LINE newNode = new_Node ( 6 ) NEW_LINE head2 . next = newNode NEW_LINE newNode = new_Node ( 9 ) NEW_LINE head2 . next . next = newNode NEW_LINE newNode = new_Node ( 15 ) NEW_LINE head1 . next = newNode NEW_LINE head2 . next . next . next = newNode NEW_LINE newNode = new_Node ( 30 ) NEW_LINE head1 . next . next = newNode NEW_LINE head1 . next . next . next = None NEW_LINE print ( getIntesectionNode ( head1 , head2 ) ) NEW_LINE


class GfG { static void reverse ( String str , int x ) { int n = ( str . length ( ) - x ) / 2 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( str . charAt ( i ) ) ; for ( int i = n + x - 1 ; i >= n ; i -- ) System . out . print ( str . charAt ( i ) ) ; for ( int i = n + x ; i < str . length ( ) ; i ++ ) System . out . print ( str . charAt ( i ) ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int x = 3 ; reverse ( str , x ) ; } }

def reverse ( str1 , x ) : NEW_LINE INDENT n = ( len ( str1 ) - x ) // 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT for i in range ( n + x - 1 , n - 1 , - 1 ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT for i in range ( n + x , len ( str1 ) ) : NEW_LINE INDENT print ( str1 [ i ] , end = " " ) NEW_LINE DEDENT DEDENT str1 = " geeksforgeeks " NEW_LINE x = 3 NEW_LINE reverse ( str1 , x ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static int countDigits ( int a , int b ) { int count = 0 ; int p = Math . abs ( a * b ) ; if ( p == 0 ) return 1 ; while ( p > 0 ) { count ++ ; p = p / 10 ; } return count ; } public static void main ( String args [ ] ) { int a = 33 ; int b = - 24 ; System . out . println ( " Number ▁ of ▁ digits ▁ = ▁ " + countDigits ( a , b ) ) ; } }

def countDigits ( a , b ) : NEW_LINE INDENT count = 0 NEW_LINE p = abs ( a * b ) NEW_LINE if ( p == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT while ( p > 0 ) : NEW_LINE INDENT count = count + 1 NEW_LINE p = p // 10 NEW_LINE DEDENT return count NEW_LINE DEDENT a = 33 NEW_LINE b = - 24 NEW_LINE print ( " Number ▁ of ▁ digits ▁ = ▁ " , countDigits ( a , b ) ) NEW_LINE


class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) { if ( n % i == 0 || n % ( i + 2 ) == 0 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { int n = 17 ; if ( isPrime ( n ) && ( n % 16 == 1 ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 17 NEW_LINE if ( isPrime ( n ) and ( n % 16 == 1 ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT


class GFG { static int minOperations ( int x , int y , int p , int q ) { if ( y % x != 0 ) return - 1 ; int d = y / x ; int a = 0 ; while ( d % p == 0 ) { d /= p ; a ++ ; } int b = 0 ; while ( d % q == 0 ) { d /= q ; b ++ ; } if ( d != 1 ) return - 1 ; return ( a + b ) ; } public static void main ( String [ ] args ) { int x = 12 , y = 2592 , p = 2 , q = 3 ; System . out . println ( minOperations ( x , y , p , q ) ) ; } }

def minOperations ( x , y , p , q ) : NEW_LINE INDENT if ( y % x != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT d = y // x NEW_LINE a = 0 NEW_LINE while ( d % p == 0 ) : NEW_LINE INDENT d //= p NEW_LINE a += 1 NEW_LINE DEDENT b = 0 NEW_LINE while ( d % q == 0 ) : NEW_LINE INDENT d //= q NEW_LINE b += 1 NEW_LINE DEDENT if ( d != 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( a + b ) NEW_LINE DEDENT x = 12 NEW_LINE y = 2592 NEW_LINE p = 2 NEW_LINE q = 3 NEW_LINE print ( minOperations ( x , y , p , q ) ) NEW_LINE


class Leet442 { public static void main ( String args [ ] ) { int numRay [ ] = { 0 , 4 , 3 , 2 , 7 , 8 , 2 , 3 , 1 } ; for ( int i = 0 ; i < numRay . length ; i ++ ) { numRay [ numRay [ i ] % numRay . length ] = numRay [ numRay [ i ] % numRay . length ] + numRay . length ; } System . out . println ( " The ▁ repeating ▁ elements ▁ are ▁ : ▁ " ) ; for ( int i = 0 ; i < numRay . length ; i ++ ) { if ( numRay [ i ] >= numRay . length * 2 ) { System . out . println ( i + " ▁ " ) ; } } } }

numRay = [ 0 , 4 , 3 , 2 , 7 , 8 , 2 , 3 , 1 ] ; NEW_LINE arr_size = len ( numRay ) ; NEW_LINE for i in range ( arr_size ) : NEW_LINE INDENT numRay [ numRay [ i ] % arr_size ] = numRay [ numRay [ i ] % arr_size ] + arr_size ; NEW_LINE DEDENT print ( " The ▁ repeating ▁ elements ▁ are ▁ : ▁ " ) ; NEW_LINE for i in range ( arr_size ) : NEW_LINE INDENT if ( numRay [ i ] >= arr_size * 2 ) : NEW_LINE INDENT print ( i , " ▁ " ) ; NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static boolean check ( int n ) { int m = n ; while ( n != 0 ) { int r = n % 10 ; if ( r > 0 ) if ( ( m % r ) != 0 ) return false ; n /= 10 ; } return true ; } static int count ( int l , int r ) { int ans = 0 ; for ( int i = l ; i <= r ; i ++ ) if ( check ( i ) ) ans += 1 ; return ans ; } public static void main ( String args [ ] ) { int l = 10 , r = 20 ; System . out . println ( count ( 10 , 20 ) ) ; } }

def check ( n ) : NEW_LINE INDENT m = n NEW_LINE while ( n != 0 ) : NEW_LINE INDENT r = n % 10 NEW_LINE if ( r > 0 ) : NEW_LINE INDENT if ( ( m % r ) != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = n // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT def count ( l , r ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT if ( check ( i ) ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT l = 10 NEW_LINE r = 20 NEW_LINE print ( count ( l , r ) ) NEW_LINE


class GFG { static int deleteFromStart ( int num , int n ) { int d = ( int ) Math . log10 ( num ) + 1 ; int rev_new_num = 0 ; for ( int i = 0 ; num != 0 ; i ++ ) { int digit = num % 10 ; num = num / 10 ; if ( i == ( d - n ) ) { continue ; } else { rev_new_num = ( rev_new_num * 10 ) + digit ; } } int new_num = 0 ; for ( int i = 0 ; rev_new_num != 0 ; i ++ ) { new_num = ( new_num * 10 ) + ( rev_new_num % 10 ) ; rev_new_num = rev_new_num / 10 ; } return new_num ; } static int deleteFromEnd ( int num , int n ) { int rev_new_num = 0 ; for ( int i = 1 ; num != 0 ; i ++ ) { int digit = num % 10 ; num = num / 10 ; if ( i == n ) { continue ; } else { rev_new_num = ( rev_new_num * 10 ) + digit ; } } int new_num = 0 ; for ( int i = 0 ; rev_new_num != 0 ; i ++ ) { new_num = ( new_num * 10 ) + ( rev_new_num % 10 ) ; rev_new_num = rev_new_num / 10 ; } return new_num ; } public static void main ( String [ ] args ) { int num = 1234 ; System . out . println ( " Number : ▁ " + num ) ; int n = 3 ; System . out . println ( " Digit ▁ to ▁ be ▁ deleted : ▁ " + n ) ; System . out . println ( " Number ▁ after ▁ " + n + " ▁ digit ▁ deleted ▁ from ▁ starting : ▁ " + deleteFromStart ( num , n ) ) ; System . out . println ( " Number ▁ after ▁ " + n + " ▁ digit ▁ deleted ▁ from ▁ ending : ▁ " + deleteFromEnd ( num , n ) ) ; } }

import math ; NEW_LINE def deleteFromStart ( num , n ) : NEW_LINE INDENT d = ( math . log10 ( num ) + 1 ) ; NEW_LINE rev_new_num = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( num != 0 ) : NEW_LINE INDENT digit = num % 10 ; NEW_LINE num = int ( num / 10 ) ; NEW_LINE if ( i != ( int ( d ) - n ) ) : NEW_LINE INDENT rev_new_num = ( ( rev_new_num * 10 ) + digit ) ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT new_num = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( rev_new_num != 0 ) : NEW_LINE INDENT new_num = ( ( new_num * 10 ) + ( rev_new_num % 10 ) ) ; NEW_LINE rev_new_num = int ( rev_new_num / 10 ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT return new_num ; NEW_LINE DEDENT def deleteFromEnd ( num , n ) : NEW_LINE INDENT rev_new_num = 0 ; NEW_LINE i = 1 ; NEW_LINE while ( num != 0 ) : NEW_LINE INDENT digit = num % 10 ; NEW_LINE num = int ( num / 10 ) ; NEW_LINE if ( i != n ) : NEW_LINE INDENT rev_new_num = ( ( rev_new_num * 10 ) + digit ) ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT new_num = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( rev_new_num != 0 ) : NEW_LINE INDENT new_num = ( ( new_num * 10 ) + ( rev_new_num % 10 ) ) ; NEW_LINE rev_new_num = int ( rev_new_num / 10 ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT return new_num ; NEW_LINE DEDENT num = 1234 ; NEW_LINE print ( " Number : " , num ) ; NEW_LINE n = 3 ; NEW_LINE print ( " Digit ▁ to ▁ be ▁ deleted : " , n ) ; NEW_LINE print ( " Number ▁ after " , n , " digit ▁ deleted ▁ from ▁ starting : " , deleteFromStart ( num , n ) ) ; NEW_LINE print ( " Number ▁ after " , n , " digit ▁ deleted ▁ from ▁ ending : " , deleteFromEnd ( num , n ) ) ; NEW_LINE


class GFG { static void printMatrix ( int n , int m ) { if ( n < 5 || m < 5 ) { System . out . print ( - 1 ) ; return ; } char [ ] s = " aeiou " . toCharArray ( ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { System . out . print ( s [ j % 5 ] + " ▁ " ) ; } System . out . println ( ) ; char c = s [ 0 ] ; for ( int k = 0 ; k < 4 ; k ++ ) { s [ k ] = s [ k + 1 ] ; } s [ 4 ] = c ; } } public static void main ( String [ ] args ) { int n = 5 , m = 5 ; printMatrix ( n , m ) ; } }

def printMatrix ( n , m ) : NEW_LINE INDENT if ( n < 5 or m < 5 ) : NEW_LINE INDENT print ( - 1 , end = " ▁ " ) ; NEW_LINE return ; NEW_LINE DEDENT s = " aeiou " ; NEW_LINE s = list ( s ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT print ( s [ j % 5 ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) NEW_LINE c = s [ 0 ] ; NEW_LINE for i in range ( 4 ) : NEW_LINE INDENT s [ i ] = s [ i + 1 ] ; NEW_LINE DEDENT s [ 4 ] = c ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; m = 5 ; NEW_LINE printMatrix ( n , m ) ; NEW_LINE DEDENT


class GFG { static int largestNum ( int a , int b ) { return a * ( ( a / b ) > 0 ? 1 : 0 ) + b * ( ( b / a ) > 0 ? 1 : 0 ) ; } public static void main ( String [ ] args ) { int a = 22 , b = 1231 ; System . out . print ( largestNum ( a , b ) ) ; } }

def largestNum ( a , b ) : NEW_LINE INDENT return a * ( bool ) ( a // b ) +   \ NEW_LINE INDENT b * ( bool ) ( b // a ) ; NEW_LINE DEDENT DEDENT a = 22 ; NEW_LINE b = 1231 ; NEW_LINE print ( largestNum ( a , b ) ) ; NEW_LINE


public class GFG { static int minimumMoves ( int n , int a [ ] , int s , int t ) { int i , x ; x = s ; for ( i = 1 ; i <= n ; i ++ ) { if ( x == t ) break ; x = a [ x ] ; } if ( x == t ) return i - 1 ; else return - 1 ; } public static void main ( String [ ] args ) { int s = 2 , t = 1 , i ; int a [ ] = { - 1 , 2 , 3 , 4 , 1 } ; int n = a . length ; System . out . println ( minimumMoves ( n , a , s , t ) ) ; } }

def minimumMoves ( n , a , s , t ) : NEW_LINE INDENT x = s NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if x == t : NEW_LINE INDENT return i - 1 NEW_LINE DEDENT x = a [ x ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s , t = 2 , 1 NEW_LINE a = [ - 1 , 2 , 3 , 4 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( minimumMoves ( n , a , s , t ) ) NEW_LINE DEDENT


class GFG { static int highestPower ( String str , int len ) { int ans = 0 ; for ( int i = len - 1 ; i >= 0 ; i -- ) { if ( str . charAt ( i ) == '0' ) ans ++ ; else break ; } return ans ; } public static void main ( String [ ] args ) { String str = "100100" ; int len = str . length ( ) ; System . out . println ( highestPower ( str , len ) ) ; } }

def highestPower ( str , lenngth ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( lenngth - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( str [ i ] == '0' ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT str = "100100" ; NEW_LINE lenngth = len ( str ) ; NEW_LINE print ( highestPower ( str , lenngth ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import java . util . * ; class GFG { static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; static void segmentedSieve ( ) { int n = 10000 ; int nNew = ( n - 2 ) / 2 ; boolean [ ] marked = new boolean [ nNew + 1 ] ; for ( int i = 1 ; i <= nNew ; i ++ ) for ( int j = i ; ( i + j + 2 * i * j ) <= nNew ; j ++ ) marked [ i + j + 2 * i * j ] = true ; primes . add ( 2 ) ; for ( int i = 1 ; i <= nNew ; i ++ ) if ( marked [ i ] == false ) primes . add ( 2 * i + 1 ) ; } static int Nfactors ( int a , int b , int n ) { segmentedSieve ( ) ; int result = 0 ; for ( int i = a ; i <= b ; i ++ ) { int tmp = ( int ) Math . sqrt ( i ) , copy = i ; int count = 0 ; for ( int j = 0 ; primes . get ( j ) <= tmp ; j ++ ) { if ( copy % primes . get ( j ) == 0 ) { count ++ ; while ( copy % primes . get ( j ) == 0 ) copy = copy / primes . get ( j ) ; } } if ( copy != 1 ) count ++ ; if ( count == n ) result ++ ; } return result ; } public static void main ( String [ ] args ) { int a = 1 , b = 100 , n = 3 ; System . out . println ( Nfactors ( a , b , n ) ) ; } }

import math NEW_LINE primes = [ ] ; NEW_LINE def segmentedSieve ( ) : NEW_LINE INDENT n = 10000 ; NEW_LINE nNew = int ( ( n - 2 ) / 2 ) ; NEW_LINE marked = [ False ] * ( nNew + 1 ) ; NEW_LINE for i in range ( 1 , nNew + 1 ) : NEW_LINE INDENT j = i ; NEW_LINE while ( ( i + j + 2 * i * j ) <= nNew ) : NEW_LINE INDENT marked [ i + j + 2 * i * j ] = True ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT primes . append ( 2 ) ; NEW_LINE for i in range ( 1 , nNew + 1 ) : NEW_LINE INDENT if ( marked [ i ] == False ) : NEW_LINE INDENT primes . append ( 2 * i + 1 ) ; NEW_LINE DEDENT DEDENT DEDENT def Nfactors ( a , b , n ) : NEW_LINE INDENT segmentedSieve ( ) ; NEW_LINE result = 0 ; NEW_LINE for i in range ( a , b + 1 ) : NEW_LINE INDENT tmp = math . sqrt ( i ) ; NEW_LINE copy = i ; NEW_LINE count = 0 ; NEW_LINE j = 0 ; NEW_LINE while ( primes [ j ] <= tmp ) : NEW_LINE INDENT if ( copy % primes [ j ] == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE while ( copy % primes [ j ] == 0 ) : NEW_LINE INDENT copy = ( copy // primes [ j ] ) ; NEW_LINE DEDENT DEDENT j += 1 ; NEW_LINE DEDENT if ( copy != 1 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT if ( count == n ) : NEW_LINE INDENT result += 1 ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT a = 1 ; NEW_LINE b = 100 ; NEW_LINE n = 3 ; NEW_LINE print ( Nfactors ( a , b , n ) ) ; NEW_LINE


public class GFG { static int maxval ( int a [ ] , int n ) { if ( n < 2 ) { return - 99999 ; } int max = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { int x = ( a [ i ] + i ) * ( a [ j ] + j ) ; if ( max < x ) { max = x ; } } } return max ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 5 , 3 , 1 , 10 } ; int len = arr . length ; System . out . println ( maxval ( arr , len ) ) ; } }

def maxval ( a , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT return - 99999 NEW_LINE DEDENT max = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT x = ( a [ i ] + i ) * ( a [ j ] + j ) NEW_LINE if max < x : NEW_LINE INDENT max = x NEW_LINE DEDENT DEDENT DEDENT return max NEW_LINE DEDENT print ( maxval ( [ 4 , 5 , 3 , 1 , 10 ] , 5 ) ) NEW_LINE


import java . util . HashMap ; class geeks { public static int nCr ( int n , int r ) { int res = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { res *= ( n - r + i ) ; res /= i ; } return res ; } public static int NoofSequences ( int N , int M ) { HashMap < Integer , Integer > prime = new HashMap < > ( ) ; for ( int i = 2 ; i <= Math . sqrt ( M ) ; i ++ ) { while ( M % i == 0 ) { if ( prime . get ( i ) == null ) prime . put ( i , 1 ) ; else { int x = prime . get ( i ) ; prime . put ( i , ++ x ) ; } M /= i ; } } if ( M > 1 ) { if ( prime . get ( M ) != null ) { int x = prime . get ( M ) ; prime . put ( M , ++ x ) ; } else prime . put ( M , 1 ) ; } int ans = 1 ; for ( HashMap . Entry < Integer , Integer > entry : prime . entrySet ( ) ) { ans *= ( nCr ( N + entry . getValue ( ) - 1 , N - 1 ) ) ; } return ans ; } public static void main ( String [ ] args ) { int N = 2 , M = 6 ; System . out . println ( NoofSequences ( N , M ) ) ; } }

def ncr ( n , r ) : NEW_LINE INDENT res = 1 NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE INDENT res *= ( n - r + i ) NEW_LINE res //= i NEW_LINE DEDENT return res NEW_LINE DEDENT def NoofSequences ( N , M ) : NEW_LINE INDENT prime = { } NEW_LINE for i in range ( 2 , int ( M ** ( .5 ) ) + 1 ) : NEW_LINE INDENT while ( M % i == 0 ) : NEW_LINE INDENT prime [ i ] = prime . get ( i , 0 ) + 1 NEW_LINE M //= i NEW_LINE DEDENT DEDENT if ( M > 1 ) : NEW_LINE INDENT prime [ M ] = prime . get ( M , 0 ) + 1 NEW_LINE DEDENT ans = 1 NEW_LINE for it in prime : NEW_LINE INDENT ans *= ( ncr ( N + prime [ it ] - 1 , N - 1 ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT N = 2 NEW_LINE M = 6 NEW_LINE print ( NoofSequences ( N , M ) ) NEW_LINE


class GFG { static final int MAX = 100000 ; static long prefix [ ] = new long [ MAX ] ; static void sieve_modified ( ) { for ( int i = 2 ; 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 long sumEvenFactors ( 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 . print ( sumEvenFactors ( l , r ) ) ; } }

def sieve_modified ( ) : NEW_LINE INDENT for i in range ( 2 , 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 sumEvenFactors ( L , R ) : NEW_LINE INDENT return ( prefix [ R ] - prefix [ L - 1 ] ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT MAX = 100000 NEW_LINE prefix = [ 0 ] * MAX NEW_LINE sieve_modified ( ) NEW_LINE l , r = 6 , 10 NEW_LINE print ( sumEvenFactors ( l , r ) ) NEW_LINE DEDENT


class GFG { static boolean isPerfectSquare ( double x ) { double sr = Math . sqrt ( x ) ; return ( ( sr - Math . floor ( sr ) ) == 0 ) ; } static boolean isSunnyNum ( int n ) { if ( isPerfectSquare ( n + 1 ) ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 3 ; if ( isSunnyNum ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

import math as mt NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT sr = mt . sqrt ( x ) NEW_LINE return ( ( sr - mt . floor ( sr ) ) == 0 ) NEW_LINE DEDENT def isSunnyNum ( n ) : NEW_LINE INDENT if ( isPerfectSquare ( n + 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 3 NEW_LINE if ( isSunnyNum ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class BinarySearch { static int countRotations ( int arr [ ] , int low , int high ) { if ( high < low ) return 0 ; if ( high == low ) return low ; int mid = low + ( high - low ) / 2 ; if ( mid < high && arr [ mid + 1 ] < arr [ mid ] ) return ( mid + 1 ) ; if ( mid > low && arr [ mid ] < arr [ mid - 1 ] ) return mid ; if ( arr [ high ] > arr [ mid ] ) return countRotations ( arr , low , mid - 1 ) ; return countRotations ( arr , mid + 1 , high ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 15 , 18 , 2 , 3 , 6 , 12 } ; int n = arr . length ; System . out . println ( countRotations ( arr , 0 , n - 1 ) ) ; } }

def countRotations ( arr , low , high ) : NEW_LINE INDENT if ( high < low ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( high == low ) : NEW_LINE INDENT return low NEW_LINE DEDENT mid = low + ( high - low ) / 2 ; NEW_LINE mid = int ( mid ) NEW_LINE if ( mid < high and arr [ mid + 1 ] < arr [ mid ] ) : NEW_LINE INDENT return ( mid + 1 ) NEW_LINE DEDENT if ( mid > low and arr [ mid ] < arr [ mid - 1 ] ) : NEW_LINE INDENT return mid NEW_LINE DEDENT if ( arr [ high ] > arr [ mid ] ) : NEW_LINE INDENT return countRotations ( arr , low , mid - 1 ) ; NEW_LINE DEDENT return countRotations ( arr , mid + 1 , high ) NEW_LINE DEDENT arr = [ 15 , 18 , 2 , 3 , 6 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countRotations ( arr , 0 , n - 1 ) ) NEW_LINE


import java . util . * ; class GFG { static boolean isPeak ( int arr [ ] , int n , int num , int i , int j ) { if ( i >= 0 && arr [ i ] > num ) { return false ; } if ( j < n && arr [ j ] > num ) { return false ; } return true ; } static boolean isTrough ( int arr [ ] , int n , int num , int i , int j ) { if ( i >= 0 && arr [ i ] < num ) { return false ; } if ( j < n && arr [ j ] < num ) { return false ; } return true ; } static void printPeaksTroughs ( int arr [ ] , int n ) { System . out . print ( " Peaks ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPeak ( arr , n , arr [ i ] , i - 1 , i + 1 ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } System . out . println ( " " ) ; System . out . print ( " Troughs ▁ : ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isTrough ( arr , n , arr [ i ] , i - 1 , i + 1 ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 10 , 5 , 7 , 4 , 3 , 5 } ; int n = arr . length ; printPeaksTroughs ( arr , n ) ; } }

def isPeak ( arr , n , num , i , j ) : NEW_LINE INDENT if ( i >= 0 and arr [ i ] > num ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( j < n and arr [ j ] > num ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def isTrough ( arr , n , num , i , j ) : NEW_LINE INDENT if ( i >= 0 and arr [ i ] < num ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( j < n and arr [ j ] < num ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def printPeaksTroughs ( arr , n ) : NEW_LINE INDENT print ( " Peaks ▁ : ▁ " , end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isPeak ( arr , n , arr [ i ] , i - 1 , i + 1 ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE print ( " Troughs ▁ : ▁ " , end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( isTrough ( arr , n , arr [ i ] , i - 1 , i + 1 ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 5 , 10 , 5 , 7 , 4 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE printPeaksTroughs ( arr , n ) NEW_LINE


class GFG { static int gcdFunc ( int a , int b ) { if ( b == 0 ) return a ; return gcdFunc ( b , a % b ) ; } static int findMinimumElements ( int [ ] a , int n ) { int [ ] b = new int [ n - 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { b [ i - 1 ] = a [ i ] - a [ i - 1 ] ; } int gcd = b [ 0 ] ; for ( int i = 0 ; i < n - 1 ; i ++ ) { gcd = gcdFunc ( gcd , b [ i ] ) ; } int ans = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { ans += ( b [ i ] / gcd ) - 1 ; } return ans ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 6 , 8 , 10 , 14 , 16 } ; int n1 = arr1 . length ; System . out . print ( findMinimumElements ( arr1 , n1 ) + " \n " ) ; } }

def gcdFunc ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcdFunc ( b , a % b ) NEW_LINE DEDENT def findMinimumElements ( a , n ) : NEW_LINE INDENT b = [ 0 ] * ( n - 1 ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT b [ i - 1 ] = a [ i ] - a [ i - 1 ] NEW_LINE DEDENT gcd = b [ 0 ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT gcd = gcdFunc ( gcd , b [ i ] ) NEW_LINE DEDENT ans = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT ans += ( b [ i ] // gcd ) - 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr1 = [ 1 , 6 , 8 , 10 , 14 , 16 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( findMinimumElements ( arr1 , n1 ) ) NEW_LINE


import java . math . * ; class GFG { public static int findGCD ( int a , int b ) { if ( b == 0 ) return a ; else return findGCD ( b , a % b ) ; } static void findNumbers ( int a , int b ) { int gcd = findGCD ( a , b ) ; System . out . println ( ( a / gcd ) + " ▁ " + ( b / gcd ) ) ; } public static void main ( String [ ] args ) { int a = 12 , b = 16 ; findNumbers ( a , b ) ; } }

from math import gcd NEW_LINE def findNumbers ( a , b ) : NEW_LINE INDENT __gcd = gcd ( a , b ) ; NEW_LINE print ( ( a // __gcd ) , ( b // __gcd ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 12 ; b = 16 ; NEW_LINE findNumbers ( a , b ) ; NEW_LINE DEDENT


class GFG { public static int indexOfFirstOne ( int arr [ ] , int low , int high ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == 1 && ( mid == 0 || arr [ mid - 1 ] == 0 ) ) return mid ; else if ( arr [ mid ] == 1 ) high = mid - 1 ; else low = mid + 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( indexOfFirstOne ( arr , 0 , n - 1 ) ) ; } }

def indexOfFirstOne ( arr , low , high ) : NEW_LINE INDENT while ( low <= high ) : NEW_LINE INDENT mid = int ( ( low + high ) / 2 ) NEW_LINE if ( arr [ mid ] == 1 and ( mid == 0 or arr [ mid - 1 ] == 0 ) ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( arr [ mid ] == 1 ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT arr = [ 0 , 0 , 0 , 0 , 0 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE ans = indexOfFirstOne ( arr , 0 , n - 1 ) NEW_LINE print ( ans ) NEW_LINE


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

def MinDeletion ( a , n ) : NEW_LINE INDENT map = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT map [ a [ i ] ] += 1 ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for key , value in map . items ( ) : NEW_LINE INDENT x = key ; NEW_LINE frequency = value ; NEW_LINE if ( x <= frequency ) : NEW_LINE INDENT ans += ( frequency - x ) ; NEW_LINE DEDENT else : NEW_LINE INDENT ans += frequency ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 3 , 2 , 3 , 4 , 4 , 4 , 4 , 5 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( MinDeletion ( a , n ) ) ; NEW_LINE DEDENT


class MinCellsDestination { static final int SIZE = 100 ; static int minCells ( int mat [ ] [ ] , int m , int n ) { int dp [ ] [ ] = new int [ m ] [ n ] ; for ( int i = 0 ; i < m ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) dp [ i ] [ j ] = Integer . MAX_VALUE ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( dp [ i ] [ j ] != Integer . MAX_VALUE && ( j + mat [ i ] [ j ] ) < n && ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) dp [ i ] [ j + mat [ i ] [ j ] ] = dp [ i ] [ j ] + 1 ; if ( dp [ i ] [ j ] != Integer . MAX_VALUE && ( i + mat [ i ] [ j ] ) < m && ( dp [ i ] [ j ] + 1 ) < dp [ i + mat [ i ] [ j ] ] [ j ] ) dp [ i + mat [ i ] [ j ] ] [ j ] = dp [ i ] [ j ] + 1 ; } } if ( dp [ m - 1 ] [ n - 1 ] != Integer . MAX_VALUE ) return dp [ m - 1 ] [ n - 1 ] ; return - 1 ; } public static void main ( String args [ ] ) { int mat [ ] [ ] = { { 2 , 3 , 2 , 1 , 4 } , { 3 , 2 , 5 , 8 , 2 } , { 1 , 1 , 2 , 2 , 1 } } ; int m = 3 , n = 5 ; System . out . println ( " Minimum ▁ number ▁ of ▁ cells " + " ▁ = ▁ " + minCells ( mat , m , n ) ) ; } }

SIZE = 100 NEW_LINE MAX = 10000000 NEW_LINE def minCells ( mat , m , n ) : NEW_LINE INDENT dp = [ [ MAX for i in range ( n ) ] for i in range ( m ) ] NEW_LINE dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( dp [ i ] [ j ] != MAX and ( j + mat [ i ] [ j ] ) < n and ( dp [ i ] [ j ] + 1 ) < dp [ i ] [ j + mat [ i ] [ j ] ] ) : NEW_LINE INDENT dp [ i ] [ j + mat [ i ] [ j ] ] = dp [ i ] [ j ] + 1 NEW_LINE DEDENT if ( dp [ i ] [ j ] != MAX and ( i + mat [ i ] [ j ] ) < m and ( dp [ i ] [ j ] + 1 ) < dp [ i + mat [ i ] [ j ] ] [ j ] ) : NEW_LINE INDENT dp [ i + mat [ i ] [ j ] ] [ j ] = dp [ i ] [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT if ( dp [ m - 1 ] [ n - 1 ] != MAX ) : NEW_LINE INDENT return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT return - 1 NEW_LINE DEDENT mat = [ [ 2 , 3 , 2 , 1 , 4 ] , [ 3 , 2 , 5 , 8 , 2 ] , [ 1 , 1 , 2 , 2 , 1 ] ] NEW_LINE m = 3 NEW_LINE n = 5 NEW_LINE print ( " Minimum ▁ number ▁ of ▁ cells ▁ = ▁ " , minCells ( mat , m , n ) ) NEW_LINE


class GFG { static int __gcd ( int a , int b ) { if ( a == 0 || b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static void coprime ( int a , int b ) { if ( __gcd ( a , b ) == 1 ) System . out . println ( " Co - Prime " ) ; else System . out . println ( " Not ▁ Co - Prime " ) ; } public static void main ( String [ ] args ) { int a = 5 , b = 6 ; coprime ( a , b ) ; a = 8 ; b = 16 ; coprime ( a , b ) ; } }

def __gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 or b == 0 ) : return 0 NEW_LINE if ( a == b ) : return a NEW_LINE if ( a > b ) : NEW_LINE INDENT return __gcd ( a - b , b ) NEW_LINE DEDENT return __gcd ( a , b - a ) NEW_LINE DEDENT def coprime ( a , b ) : NEW_LINE INDENT if ( __gcd ( a , b ) == 1 ) : NEW_LINE INDENT print ( " Co - Prime " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Co - Prime " ) NEW_LINE DEDENT DEDENT a = 5 ; b = 6 NEW_LINE coprime ( a , b ) NEW_LINE a = 8 ; b = 16 NEW_LINE coprime ( a , b ) NEW_LINE


class GFG { static int MAX = 26 ; public static boolean canBeMapped ( String s1 , int l1 , String s2 , int l2 ) { if ( l1 != l2 ) return false ; int [ ] freq1 = new int [ MAX ] ; int [ ] freq2 = new int [ MAX ] ; for ( int i = 0 ; i < l1 ; i ++ ) freq1 [ s1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < l2 ; i ++ ) freq2 [ s2 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX ; i ++ ) { if ( freq1 [ i ] == 0 ) continue ; boolean found = false ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( freq1 [ i ] == freq2 [ j ] ) { freq2 [ j ] = - 1 ; found = true ; break ; } } if ( ! found ) return false ; } return true ; } public static void main ( String [ ] args ) { String s1 = " axx " ; String s2 = " cbc " ; int l1 = s1 . length ( ) ; int l2 = s2 . length ( ) ; if ( canBeMapped ( s1 , l1 , s2 , l2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

MAX = 26 NEW_LINE def canBeMapped ( s1 , l1 , s2 , l2 ) : NEW_LINE INDENT if ( l1 != l2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT freq1 = [ 0 for i in range ( MAX ) ] NEW_LINE freq2 = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT freq1 [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( l2 ) : NEW_LINE INDENT freq2 [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( MAX ) : NEW_LINE INDENT if ( freq1 [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT found = False NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT if ( freq1 [ i ] == freq2 [ j ] ) : NEW_LINE INDENT freq2 [ j ] = - 1 NEW_LINE found = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( found == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s1 = " axx " NEW_LINE s2 = " cbc " NEW_LINE l1 = len ( s1 ) NEW_LINE l2 = len ( s2 ) NEW_LINE if ( canBeMapped ( s1 , l1 , s2 , l2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static int findQuadruples ( int a [ ] , int b [ ] , int c [ ] , int d [ ] , int x , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) for ( int k = 0 ; k < n ; k ++ ) for ( int l = 0 ; l < n ; l ++ ) if ( ( a [ i ] ^ b [ j ] ^ c [ k ] ^ d [ l ] ) == x ) count ++ ; return count ; } public static void main ( String [ ] args ) { int x = 3 ; int a [ ] = { 0 , 1 } ; int b [ ] = { 2 , 0 } ; int c [ ] = { 0 , 1 } ; int d [ ] = { 0 , 1 } ; int n = a . length ; System . out . println ( findQuadruples ( a , b , c , d , x , n ) ) ; } }

def findQuadruples ( a , b , c , d , x , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT for k in range ( n ) : NEW_LINE INDENT for l in range ( n ) : NEW_LINE INDENT if ( ( a [ i ] ^ b [ j ] ^ c [ k ] ^ d [ l ] ) == x ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return count NEW_LINE DEDENT x = 3 NEW_LINE a = [ 0 , 1 ] NEW_LINE b = [ 2 , 0 ] NEW_LINE c = [ 0 , 1 ] NEW_LINE d = [ 0 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( findQuadruples ( a , b , c , d , x , n ) ) NEW_LINE


class GFG { static int power ( int n , int r ) { int count = 0 ; for ( int i = r ; ( n / i ) >= 1 ; i = i * r ) count += n / i ; return count ; } public static void main ( String [ ] args ) { int n = 6 , r = 3 ; System . out . print ( power ( n , r ) ) ; } }

def power ( n , r ) : NEW_LINE INDENT count = 0 ; i = r NEW_LINE while ( ( n / i ) >= 1 ) : NEW_LINE INDENT count += n / i NEW_LINE i = i * r NEW_LINE DEDENT return int ( count ) NEW_LINE DEDENT n = 6 ; r = 3 NEW_LINE print ( power ( n , r ) ) NEW_LINE


import java . util . * ; class GFG { static int calculate_change ( int length , int breadth ) { int change = 0 ; change = length + breadth + ( ( length * breadth ) / 100 ) ; return change ; } public static void main ( String args [ ] ) { int cL = 20 ; int cB = - 10 ; int cA = calculate_change ( cL , cB ) ; System . out . println ( + cA ) ; } }

def calculate_change ( length , breadth ) : NEW_LINE INDENT change = 0 NEW_LINE change = length + breadth + ( ( length * breadth ) // 100 ) NEW_LINE return change NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT cL = 20 NEW_LINE cB = - 10 NEW_LINE cA = calculate_change ( cL , cB ) NEW_LINE print ( cA ) NEW_LINE DEDENT


class GFG { static int min_elimination ( int n , int [ ] arr ) { int count = 0 ; int prev_val = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { int curr_val = arr [ i ] ; if ( curr_val % 2 == prev_val % 2 ) count ++ ; prev_val = curr_val ; } return count ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 7 , 9 } ; int n = arr . length ; System . out . println ( min_elimination ( n , arr ) ) ; } }

def min_elimination ( n , arr ) : NEW_LINE INDENT count = 0 NEW_LINE prev_val = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr_val = arr [ i ] ; NEW_LINE if ( curr_val % 2 == prev_val % 2 ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT prev_val = curr_val NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( min_elimination ( n , arr ) ) ; NEW_LINE


class GFG { static boolean isDivisible ( int n ) { int d ; while ( ( n / 100 ) > 0 ) { d = n % 10 ; n /= 10 ; n = Math . abs ( n + ( d * 13 ) ) ; } return ( n % 43 == 0 ) ; } public static void main ( String [ ] args ) { int N = 2795 ; if ( isDivisible ( N ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def isDivisible ( n ) : NEW_LINE INDENT while n // 100 : NEW_LINE INDENT d = n % 10 NEW_LINE n //= 10 NEW_LINE n = abs ( n + ( d * 13 ) ) NEW_LINE DEDENT return ( n % 43 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2795 NEW_LINE if ( isDivisible ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class Gfg { static int getHeight ( int X ) { return ( 2 * X ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int X = 35 ; System . out . println ( getHeight ( X ) ) ; } }

def getHeight ( X ) : NEW_LINE INDENT return ( 2 * X ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT X = 35 NEW_LINE print ( getHeight ( X ) ) NEW_LINE DEDENT


public class GfG { public static String LedRequired ( String s , int led ) { int seg [ ] = { 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 } ; int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { count += seg [ ( int ) ( s . charAt ( i ) ) - 48 ] ; } if ( count <= led ) return " YES " ; else return " NO " ; } public static void main ( String [ ] args ) { String S = "123456789" ; int led = 20 ; System . out . println ( LedRequired ( S , led ) ) ; } }

seg = [ 6 , 2 , 5 , 5 , 4 , 5 , 6 , 3 , 7 , 6 ] NEW_LINE def LedRequired ( s , led ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT count += seg [ ord ( s [ i ] ) - 48 ] NEW_LINE DEDENT if ( count <= led ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT else : NEW_LINE INDENT return " NO " NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "123456789" NEW_LINE led = 20 NEW_LINE print ( LedRequired ( S , led ) ) NEW_LINE DEDENT


class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n != 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void main ( String [ ] args ) { int i = 3 ; System . out . print ( countSetBits ( i ) ) ; } }

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


class GFG { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return ( gcd ( b , a % b ) ) ; } static void findSmallest ( int a , int b ) { int lcm = ( a * b ) / gcd ( a , b ) ; System . out . print ( " x ▁ = ▁ " + lcm / a + " \n y ▁ = ▁ " + lcm / b ) ; } public static void main ( String [ ] args ) { int a = 25 , b = 35 ; findSmallest ( 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 findSmallest ( a , b ) : NEW_LINE INDENT lcm = ( a * b ) / gcd ( a , b ) NEW_LINE print ( " x ▁ = " , lcm / a , " \n y ▁ = ▁ " , lcm / b ) NEW_LINE DEDENT a = 25 NEW_LINE b = 35 NEW_LINE findSmallest ( a , b ) NEW_LINE


import java . util . * ; class Node { int data ; Node left , right ; public Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node root ; void printSpecificLevelOrderUtil ( Node root , Stack < Node > s ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root . left ) ; q . add ( root . right ) ; Node first = null , second = null ; while ( ! q . isEmpty ( ) ) { first = q . peek ( ) ; q . poll ( ) ; second = q . peek ( ) ; q . poll ( ) ; s . push ( second . left ) ; s . push ( first . right ) ; s . push ( second . right ) ; s . push ( first . left ) ; if ( first . left . left != null ) { q . add ( first . right ) ; q . add ( second . left ) ; q . add ( first . left ) ; q . add ( second . right ) ; } } } void printSpecificLevelOrder ( Node root ) { Stack < Node > s = new Stack < Node > ( ) ; s . push ( root ) ; if ( root . left != null ) { s . push ( root . right ) ; s . push ( root . left ) ; } if ( root . left . left != null ) printSpecificLevelOrderUtil ( root , s ) ; while ( ! s . empty ( ) ) { System . out . print ( s . peek ( ) . data + " ▁ " ) ; s . pop ( ) ; } } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; System . out . println ( " Specific ▁ Level ▁ Order ▁ Traversal ▁ " + " of ▁ Binary ▁ Tree ▁ is ▁ " ) ; tree . printSpecificLevelOrder ( tree . root ) ; } }

class Node : NEW_LINE INDENT s = [ ] NEW_LINE q = [ ] NEW_LINE elements = 0 NEW_LINE def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT def printSpecificLevelOrder ( self , root ) : NEW_LINE INDENT self . s . append ( root ) NEW_LINE prnt = self . s . pop ( 0 ) NEW_LINE self . q . append ( prnt . data ) NEW_LINE if prnt . right : NEW_LINE INDENT self . s . append ( root . right ) NEW_LINE DEDENT if prnt . left : NEW_LINE INDENT self . s . append ( root . left ) NEW_LINE DEDENT while ( len ( self . s ) > 0 ) : NEW_LINE INDENT first = self . s . pop ( 0 ) NEW_LINE self . q . append ( first . data ) NEW_LINE second = self . s . pop ( 0 ) NEW_LINE self . q . append ( second . data ) NEW_LINE if first . left and second . right and first . right and second . left : NEW_LINE INDENT self . s . append ( first . left ) NEW_LINE self . s . append ( second . right ) NEW_LINE self . s . append ( first . right ) NEW_LINE self . s . append ( second . left ) NEW_LINE DEDENT DEDENT for elements in reversed ( self . q ) : NEW_LINE INDENT print ( elements , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE print ( " Specific ▁ Level ▁ Order ▁ traversal ▁ of ▁ " " binary ▁ tree ▁ is " ) NEW_LINE root . printSpecificLevelOrder ( root ) NEW_LINE


class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int solve ( int arr [ ] , int n , int k ) { int minx = INT_MAX ; int i ; for ( i = 0 ; i < n ; i ++ ) { minx = Math . min ( minx , arr [ i ] ) ; } int decrements = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - minx ) % k != 0 ) { return - 1 ; } else { decrements += ( ( arr [ i ] - minx ) / k ) ; } } return decrements ; } public static void main ( String [ ] args ) { int n , k ; n = 3 ; k = 3 ; int arr [ ] = { 12 , 9 , 15 } ; System . out . println ( solve ( arr , n , k ) ) ; } }

import sys NEW_LINE def solve ( arr , n , k ) : NEW_LINE INDENT minx = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT minx = min ( minx , arr [ i ] ) ; NEW_LINE DEDENT decrements = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr [ i ] - minx ) % k != 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT decrements += ( ( arr [ i ] - minx ) // k ) ; NEW_LINE DEDENT DEDENT return decrements ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE k = 3 ; NEW_LINE arr = [ 12 , 9 , 15 ] ; NEW_LINE print ( solve ( arr , n , k ) ) ; NEW_LINE DEDENT


class GFG { static boolean check ( int H , int S ) { return H * H >= 4 * S ; } static int findPairs ( int H [ ] , int n , int S [ ] , int m ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < m ; j ++ ) { if ( check ( H [ i ] , S [ j ] ) ) count ++ ; } } return count ; } public static void main ( String args [ ] ) { int H [ ] = { 1 , 6 , 4 } ; int n = H . length ; int S [ ] = { 23 , 3 , 42 , 14 } ; int m = S . length ; System . out . println ( findPairs ( H , n , S , m ) ) ; } }

def check ( H , S ) : NEW_LINE INDENT return H * H >= 4 * S NEW_LINE DEDENT def findPairs ( H , n , S , m ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if check ( H [ i ] , S [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT H = [ 1 , 6 , 4 ] NEW_LINE n = len ( H ) NEW_LINE S = [ 23 , 3 , 42 , 14 ] NEW_LINE m = len ( S ) NEW_LINE print ( findPairs ( H , n , S , m ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean Prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static boolean checkSumPrime ( String str ) { int summ = 0 ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) summ += Math . abs ( str . charAt ( i - 1 ) - str . charAt ( i ) ) ; if ( Prime ( summ ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int num = 142 ; String str = "142" ; if ( checkSumPrime ( str ) ) System . out . println ( " Prime " ) ; else System . out . println ( " Not ▁ Prime " ) ; } }

import math as mt NEW_LINE def Prime ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , mt . ceil ( mt . sqrt ( n + 1 ) ) ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def checkSumPrime ( string ) : NEW_LINE INDENT summ = 0 NEW_LINE for i in range ( 1 , len ( string ) ) : NEW_LINE INDENT summ += abs ( int ( string [ i - 1 ] ) - int ( string [ i ] ) ) NEW_LINE DEDENT if Prime ( summ ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT num = 142 NEW_LINE string = str ( num ) NEW_LINE s = [ i for i in string ] NEW_LINE if checkSumPrime ( s ) : NEW_LINE INDENT print ( " Prime " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Prime \n " ) NEW_LINE DEDENT


class GFG { static void lexicoSmallestPermuatation ( int arr [ ] , int n ) { int cnt [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { ++ cnt [ arr [ i ] ] ; } int ele = 1 , replacement = 0 ; boolean vis [ ] = new boolean [ n + 1 ] ; for ( int i = 0 ; i < n ; ++ i ) { if ( cnt [ arr [ i ] ] == 1 ) { continue ; } while ( cnt [ ele ] > 0 ) { ++ ele ; } if ( ele > arr [ i ] && ! vis [ arr [ i ] ] ) { vis [ arr [ i ] ] = true ; } else { -- cnt [ arr [ i ] ] ; arr [ i ] = ele ; ++ replacement ; ++ ele ; } } System . out . print ( replacement + " \n " ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 3 , 2 } ; int sz = arr . length ; lexicoSmallestPermuatation ( arr , sz ) ; } }

def lexicoSmallestPermuatation ( arr , n ) : NEW_LINE INDENT cnt = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt [ arr [ i ] ] += 1 NEW_LINE DEDENT ele = 1 NEW_LINE replacement = 0 NEW_LINE vis = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( cnt [ arr [ i ] ] == 1 ) : NEW_LINE INDENT continue NEW_LINE DEDENT while ( cnt [ ele ] ) : NEW_LINE INDENT ele += 1 NEW_LINE DEDENT if ( ele > arr [ i ] and vis [ arr [ i ] ] == 0 ) : NEW_LINE INDENT vis [ arr [ i ] ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ arr [ i ] ] -= 1 NEW_LINE arr [ i ] = ele NEW_LINE replacement += 1 NEW_LINE ele += 1 NEW_LINE DEDENT DEDENT print ( replacement ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 3 , 2 ] NEW_LINE sz = len ( arr ) NEW_LINE lexicoSmallestPermuatation ( arr , sz ) NEW_LINE DEDENT


import java . io . * ; class GFG { static double areaOctagon ( double side ) { return ( float ) ( 2 * ( 1 + Math . sqrt ( 2 ) ) * side * side ) ; } public static void main ( String arg [ ] ) { double side = 4 ; System . out . print ( " Area ▁ of ▁ Regular ▁ Octagon ▁ = ▁ " + areaOctagon ( side ) ) ; } }

import math NEW_LINE def areaOctagon ( side ) : NEW_LINE INDENT return ( 2 * ( 1 + ( math . sqrt ( 2 ) ) ) * side * side ) NEW_LINE DEDENT side = 4 NEW_LINE print ( " Area ▁ of ▁ Regular ▁ Octagon ▁ = " , round ( areaOctagon ( side ) , 4 ) ) NEW_LINE


class GFG { static boolean isDivisible ( int [ ] arr , int n , int y ) { int d = 0 , i = 0 ; while ( i < n ) { while ( d < y && i < n ) d = d * 10 + arr [ i ++ ] ; d = d % y ; } if ( d == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 1 , 5 , 6 } ; int x = arr . length ; int y = 4 ; System . out . println ( isDivisible ( arr , x , y ) ? " Yes " : " No " ) ; } }

def isDivisible ( arr , n , y ) : NEW_LINE INDENT d , i = 0 , 0 NEW_LINE while i < n : NEW_LINE INDENT while d < y and i < n : NEW_LINE INDENT d = d * 10 + arr [ i ] NEW_LINE i += 1 NEW_LINE DEDENT d = d % y NEW_LINE DEDENT if d == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 5 , 6 ] NEW_LINE x = len ( arr ) NEW_LINE y = 4 NEW_LINE if ( isDivisible ( arr , x , y ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static int mod = 1000000007 ; static int [ ] ans = new int [ 100002 ] ; static int [ ] pref = new int [ 100002 ] ; static void preCompute ( ) { Arrays . fill ( ans , 1 ) ; for ( int i = 2 ; i <= 100000 / 2 ; i ++ ) { for ( int j = 2 * i ; j <= 100000 ; j += i ) { ans [ j ] = ( ans [ j ] * i ) % mod ; } } for ( int i = 1 ; i < 100002 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + ans [ i ] ; pref [ i ] %= mod ; } } static void printSum ( int L , int R ) { System . out . print ( pref [ R ] - pref [ L - 1 ] + " ▁ " ) ; } static void printSumProper ( int [ ] [ ] arr , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } public static void main ( String args [ ] ) { int Q = 2 ; int [ ] [ ] arr = { { 10 , 20 } , { 12 , 16 } } ; printSumProper ( arr , Q ) ; } }

mod = 1000000007 NEW_LINE ans = [ 1 ] * ( 100002 ) NEW_LINE pref = [ 0 ] * 100002 NEW_LINE def preCompute ( ) : NEW_LINE INDENT for i in range ( 2 , 100000 // 2 + 1 ) : NEW_LINE INDENT for j in range ( 2 * i , 100000 + 1 , i ) : NEW_LINE INDENT ans [ j ] = ( ans [ j ] * i ) % mod NEW_LINE DEDENT DEDENT for i in range ( 1 , 100002 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + ans [ i ] NEW_LINE pref [ i ] %= mod NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT print ( pref [ R ] - pref [ L - 1 ] , end = " ▁ " ) NEW_LINE DEDENT def printSumProper ( arr , Q ) : NEW_LINE INDENT preCompute ( ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Q = 2 NEW_LINE arr = [ [ 10 , 20 ] , [ 12 , 16 ] ] NEW_LINE printSumProper ( arr , Q ) NEW_LINE DEDENT


class GFG { static int __builtin_popcount ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static void countEvenOdd ( int arr [ ] , int n , int K ) { int even = 0 , odd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int x = __builtin_popcount ( arr [ i ] ) ; if ( x % 2 == 0 ) even ++ ; else odd ++ ; } int y ; y = __builtin_popcount ( K ) ; if ( ( y & 1 ) != 0 ) { System . out . println ( " Even ▁ = ▁ " + odd + " , ▁ Odd ▁ = ▁ " + even ) ; } else { System . out . println ( " Even ▁ = ▁ " + even + " , ▁ Odd ▁ = ▁ " + odd ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 15 , 9 , 8 , 8 } ; int K = 3 ; int n = arr . length ; countEvenOdd ( arr , n , K ) ; } }

def countEvenOdd ( arr , n , K ) : NEW_LINE INDENT even = 0 ; odd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = bin ( arr [ i ] ) . count ( '1' ) ; NEW_LINE if ( x % 2 == 0 ) : NEW_LINE INDENT even += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT odd += 1 ; NEW_LINE DEDENT DEDENT y = bin ( K ) . count ( '1' ) ; NEW_LINE if ( y & 1 ) : NEW_LINE INDENT print ( " Even ▁ = " , odd , " , ▁ Odd ▁ = " , even ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Even ▁ = " , even , " , ▁ Odd ▁ = " , odd ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 15 , 9 , 8 , 8 ] ; NEW_LINE K = 3 ; NEW_LINE n = len ( arr ) ; NEW_LINE countEvenOdd ( arr , n , K ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void remove ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } for ( int i = 0 ; i < n ; i ++ ) { if ( ( mp . containsKey ( arr [ i ] ) && mp . get ( arr [ i ] ) % 2 == 1 ) ) continue ; System . out . print ( arr [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 3 , 2 , 2 , 4 , 7 , 7 } ; int n = arr . length ; remove ( arr , n ) ; } }

def remove ( arr , n ) : NEW_LINE INDENT m = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( ( m [ arr [ i ] ] & 1 ) ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT print ( arr [ i ] , end = " , ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 3 , 3 , 2 , 2 , 4 , 7 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE remove ( arr , n ) ; NEW_LINE DEDENT


class GFG { static int reverseDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static int isPalindrome ( int n ) { int rev_n = reverseDigits ( n ) ; if ( rev_n == n ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { int n = 4562 ; System . out . println ( " Is " + n + " a ▁ Palindrome ▁ number ? ▁ - > ▁ " + ( isPalindrome ( n ) == 1 ? " true " : " false " ) ) ; n = 2002 ; System . out . println ( " Is " + n + " a ▁ Palindrome ▁ number ? ▁ - > ▁ " + ( isPalindrome ( n ) == 1 ? " true " : " false " ) ) ; } }

def reverseDigits ( num ) : NEW_LINE INDENT rev_num = 0 ; NEW_LINE while ( num > 0 ) : NEW_LINE INDENT rev_num = rev_num * 10 + num % 10 NEW_LINE num = num // 10 NEW_LINE DEDENT return rev_num NEW_LINE DEDENT def isPalindrome ( n ) : NEW_LINE INDENT rev_n = reverseDigits ( n ) ; NEW_LINE if ( rev_n == n ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4562 NEW_LINE if isPalindrome ( n ) == 1 : NEW_LINE INDENT print ( " Is " , n , " a ▁ Palindrome ▁ number ? ▁ - > " , True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Is " , n , " a ▁ Palindrome ▁ number ? ▁ - > " , False ) NEW_LINE DEDENT n = 2002 NEW_LINE if isPalindrome ( n ) == 1 : NEW_LINE INDENT print ( " Is " , n , " a ▁ Palindrome ▁ number ? ▁ - > " , True ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Is " , n , " a ▁ Palindrome ▁ number ? ▁ - > " , False ) NEW_LINE DEDENT DEDENT


class GFG { static int [ ] num_to_bits = new int [ ] { 0 , 1 , 1 , 2 , 1 , 2 , 2 , 3 , 1 , 2 , 2 , 3 , 2 , 3 , 3 , 4 } ; static int countSetBitsRec ( int num ) { int nibble = 0 ; if ( 0 == num ) return num_to_bits [ 0 ] ; nibble = num & 0xf ; return num_to_bits [ nibble ] + countSetBitsRec ( num >> 4 ) ; } public static void main ( String [ ] args ) { int num = 31 ; System . out . println ( countSetBitsRec ( num ) ) ; } }

num_to_bits = [ 0 , 1 , 1 , 2 , 1 , 2 , 2 , 3 , 1 , 2 , 2 , 3 , 2 , 3 , 3 , 4 ] ; NEW_LINE def countSetBitsRec ( num ) : NEW_LINE INDENT nibble = 0 ; NEW_LINE if ( 0 == num ) : NEW_LINE INDENT return num_to_bits [ 0 ] ; NEW_LINE DEDENT nibble = num & 0xf ; NEW_LINE return num_to_bits [ nibble ] + countSetBitsRec ( num >> 4 ) ; NEW_LINE DEDENT num = 31 ; NEW_LINE print ( countSetBitsRec ( num ) ) ; NEW_LINE


import java . io . * ; class GFG { public static void main ( String [ ] args ) { long n = 5 ; long fac1 = 1 ; for ( int i = 2 ; i <= n - 1 ; i ++ ) fac1 = fac1 * i ; long fac2 = fac1 * n ; long totalWays = fac1 * fac2 ; System . out . println ( totalWays ) ; } }

n = 5 NEW_LINE fac1 = 1 NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT fac1 = fac1 * i NEW_LINE DEDENT fac2 = fac1 * n NEW_LINE totalWays = fac1 * fac2 NEW_LINE print ( totalWays ) NEW_LINE


import java . io . * ; class GFG { static int findSplitPoint ( int arr [ ] , int n ) { int leftSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { leftSum += arr [ i ] ; int rightSum = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) rightSum += arr [ j ] ; if ( leftSum == rightSum ) return i + 1 ; } return - 1 ; } static void printTwoParts ( int arr [ ] , int n ) { int splitPoint = findSplitPoint ( arr , n ) ; if ( splitPoint == - 1 || splitPoint == n ) { System . out . println ( " Not ▁ Possible " ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) { if ( splitPoint == i ) System . out . println ( ) ; System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 5 } ; int n = arr . length ; printTwoParts ( arr , n ) ; } }

def findSplitPoint ( arr , n ) : NEW_LINE INDENT leftSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT leftSum += arr [ i ] NEW_LINE rightSum = 0 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT rightSum += arr [ j ] NEW_LINE DEDENT if ( leftSum == rightSum ) : NEW_LINE INDENT return i + 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def printTwoParts ( arr , n ) : NEW_LINE INDENT splitPo = findSplitPoint ( arr , n ) NEW_LINE if ( splitPo == - 1 or splitPo == n ) : NEW_LINE INDENT print ( " Not ▁ Possible " ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( splitPo == i ) : NEW_LINE INDENT print ( " " ) NEW_LINE DEDENT print ( str ( arr [ i ] ) + ' ▁ ' , end = ' ' ) NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE printTwoParts ( arr , n ) NEW_LINE


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

def weightedMean ( n ) : NEW_LINE INDENT return ( 2 * n + 1 ) / 3 NEW_LINE DEDENT n = 10 NEW_LINE print ( int ( weightedMean ( n ) ) ) NEW_LINE


import java . util . * ; class GFG { static void difference ( int arr [ ] , int n ) { int largest = arr [ 0 ] ; int i ; for ( i = 0 ; i < n ; i ++ ) { if ( largest < arr [ i ] ) largest = arr [ i ] ; } for ( i = 0 ; i < n ; i ++ ) arr [ i ] = largest - arr [ i ] ; for ( i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 9 , 3 , 2 } ; int n = arr . length ; difference ( arr , n ) ; } }

def difference ( arr , n ) : NEW_LINE INDENT largest = arr [ 0 ] ; NEW_LINE i = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( largest < arr [ i ] ) : NEW_LINE INDENT largest = arr [ i ] ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = largest - arr [ i ] ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 5 , 9 , 3 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE difference ( arr , n ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int nextPowerOf2 ( int n ) { n -- ; n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; n ++ ; return n ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( nextPowerOf2 ( n ) ) ; } }

def nextPowerOf2 ( n ) : NEW_LINE INDENT n -= 1 NEW_LINE n |= n >> 1 NEW_LINE n |= n >> 2 NEW_LINE n |= n >> 4 NEW_LINE n |= n >> 8 NEW_LINE n |= n >> 16 NEW_LINE n += 1 NEW_LINE return n NEW_LINE DEDENT n = 5 NEW_LINE print ( nextPowerOf2 ( n ) ) NEW_LINE


class GFG { static int minCost ( int arr [ ] , int cost [ ] , int n ) { int costThree = Integer . MAX_VALUE ; for ( int j = 0 ; j < n ; j ++ ) { int costI = Integer . MAX_VALUE ; int costK = Integer . MAX_VALUE ; for ( int i = 0 ; i < j ; i ++ ) { if ( arr [ i ] < arr [ j ] ) costI = Math . min ( costI , cost [ i ] ) ; } for ( int k = j + 1 ; k < n ; k ++ ) { if ( arr [ k ] > arr [ j ] ) costK = Math . min ( costK , cost [ k ] ) ; } if ( costI != Integer . MAX_VALUE && costK != Integer . MAX_VALUE ) { costThree = Math . min ( costThree , cost [ j ] + costI + costK ) ; } } if ( costThree == Integer . MAX_VALUE ) return - 1 ; return costThree ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 5 , 4 , 10 } ; int cost [ ] = { 40 , 30 , 20 , 10 , 40 } ; int n = arr . length ; System . out . println ( minCost ( arr , cost , n ) ) ; } }

def minCost ( arr , cost , n ) : NEW_LINE INDENT costThree = 10 ** 9 NEW_LINE for j in range ( n ) : NEW_LINE INDENT costI = 10 ** 9 NEW_LINE costK = 10 ** 9 NEW_LINE for i in range ( j ) : NEW_LINE INDENT if ( arr [ i ] < arr [ j ] ) : NEW_LINE INDENT costI = min ( costI , cost [ i ] ) NEW_LINE DEDENT DEDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT if ( arr [ k ] > arr [ j ] ) : NEW_LINE INDENT costK = min ( costK , cost [ k ] ) NEW_LINE DEDENT DEDENT if ( costI != 10 ** 9 and costK != 10 ** 9 ) : NEW_LINE INDENT costThree = min ( costThree , cost [ j ] + costI + costK ) NEW_LINE DEDENT DEDENT if ( costThree == 10 ** 9 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return costThree NEW_LINE DEDENT arr = [ 2 , 4 , 5 , 4 , 10 ] NEW_LINE cost = [ 40 , 30 , 20 , 10 , 40 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minCost ( arr , cost , n ) ) NEW_LINE


class GFG { static void printPattern ( char [ ] s , int n ) { System . out . println ( s ) ; int i = 0 , j = n - 1 ; while ( i < j ) { char c = s [ i ] ; s [ i ] = s [ j ] ; s [ j ] = c ; i ++ ; j -- ; } i = 0 ; j = n - 1 ; while ( j - i > 1 ) { s [ i ] = s [ j ] = ' * ' ; System . out . println ( s ) ; i ++ ; j -- ; } } public static void main ( String [ ] args ) { char [ ] s = " geeks " . toCharArray ( ) ; int n = s . length ; printPattern ( s , n ) ; } }

def printPattern ( s , n ) : NEW_LINE INDENT print ( ' ' . join ( s ) ) NEW_LINE i , j = 0 , n - 1 NEW_LINE while i < j : NEW_LINE INDENT s [ i ] , s [ j ] = s [ j ] , s [ i ] NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT i , j = 0 , n - 1 NEW_LINE while j - i > 1 : NEW_LINE INDENT s [ i ] , s [ j ] = ' * ' , ' * ' NEW_LINE print ( ' ' . join ( s ) ) NEW_LINE i += 1 NEW_LINE j -= 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " geeks " NEW_LINE n = len ( s ) NEW_LINE printPattern ( list ( s ) , n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printSeries ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { int num = i * ( i + 1 ) * ( i + 2 ) / 6 ; System . out . print ( num + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 10 ; printSeries ( n ) ; } }

def printSeries ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT num = i * ( i + 1 ) * ( i + 2 ) // 6 NEW_LINE print ( num , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE printSeries ( n ) NEW_LINE


import java . util . Arrays ; class GFG { static final int MAX = 1000001 ; static int numofAP ( int a [ ] , int n ) { int minarr = + 2147483647 ; int maxarr = - 2147483648 ; for ( int i = 0 ; i < n ; i ++ ) { minarr = Math . min ( minarr , a [ i ] ) ; maxarr = Math . max ( maxarr , a [ i ] ) ; } int dp [ ] = new int [ n ] ; int sum [ ] = new int [ MAX ] ; int ans = n + 1 ; for ( int d = ( minarr - maxarr ) ; d <= ( maxarr - minarr ) ; d ++ ) { Arrays . fill ( sum , 0 ) ; for ( int i = 0 ; i < n ; i ++ ) { dp [ i ] = 1 ; if ( a [ i ] - d >= 1 && a [ i ] - d <= 1000000 ) dp [ i ] += sum [ a [ i ] - d ] ; ans += dp [ i ] - 1 ; sum [ a [ i ] ] += dp [ i ] ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( numofAP ( arr , n ) ) ; } }

MAX = 1000001 NEW_LINE def numofAP ( a , n ) : NEW_LINE INDENT minarr = + 2147483647 NEW_LINE maxarr = - 2147483648 NEW_LINE for i in range ( n ) : NEW_LINE INDENT minarr = min ( minarr , a [ i ] ) NEW_LINE maxarr = max ( maxarr , a [ i ] ) NEW_LINE DEDENT dp = [ 0 for i in range ( n + 1 ) ] NEW_LINE ans = n + 1 NEW_LINE for d in range ( ( minarr - maxarr ) , ( maxarr - minarr ) + 1 ) : NEW_LINE INDENT sum = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT dp [ i ] = 1 NEW_LINE if ( a [ i ] - d >= 1 and a [ i ] - d <= 1000000 ) : NEW_LINE INDENT dp [ i ] += sum [ a [ i ] - d ] NEW_LINE DEDENT ans += dp [ i ] - 1 NEW_LINE sum [ a [ i ] ] += dp [ i ] NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( numofAP ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static boolean hasEvenNumberOfFactors ( int n ) { double root_n = Math . sqrt ( n ) ; if ( ( root_n * root_n ) == n ) return false ; return true ; } static void printStatusOfDoors ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { if ( hasEvenNumberOfFactors ( i ) ) System . out . print ( " closed " + " ▁ " ) ; else System . out . print ( " open " + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printStatusOfDoors ( n ) ; } }

import math NEW_LINE def hasEvenNumberOfFactors ( n ) : NEW_LINE INDENT root_n = math . sqrt ( n ) NEW_LINE if ( ( root_n * root_n ) == n ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def printStatusOfDoors ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( hasEvenNumberOfFactors ( i ) == True ) : NEW_LINE INDENT print ( " closed " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " open " , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 5 NEW_LINE printStatusOfDoors ( n ) NEW_LINE


import java . util . * ; class GFG { static void sortedAdjacentDifferences ( int arr [ ] , int n ) { int [ ] ans = new int [ n ] ; Arrays . sort ( arr ) ; int l = 0 , r = n - 1 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( i % 2 == 1 ) { ans [ i ] = arr [ l ] ; l ++ ; } else { ans [ i ] = arr [ r ] ; r -- ; } } for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ans [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 2 , 4 , 8 , 6 , 4 , 5 } ; int n = arr . length ; sortedAdjacentDifferences ( arr , n ) ; } }

def sortedAdjacentDifferences ( arr , n ) : NEW_LINE INDENT ans = [ 0 ] * n NEW_LINE arr = sorted ( arr ) NEW_LINE l = 0 NEW_LINE r = n - 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT ans [ i ] = arr [ l ] NEW_LINE l += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans [ i ] = arr [ r ] NEW_LINE r -= 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , - 2 , 4 , 8 , 6 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE sortedAdjacentDifferences ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void substringConversions ( String str , int k , int b ) { int i = 0 , sum = 0 , counter = k - 1 ; for ( i = 0 ; i < k ; i ++ ) { sum = ( int ) ( sum + ( ( str . charAt ( i ) - '0' ) * Math . pow ( b , counter ) ) ) ; counter -- ; } System . out . print ( sum + " ▁ " ) ; int prev = sum ; sum = 0 ; counter = 0 ; for ( ; i < str . length ( ) ; i ++ ) { sum = ( int ) ( prev - ( ( str . charAt ( i - k ) - '0' ) * Math . pow ( b , k - 1 ) ) ) ; sum = sum * b ; sum = sum + ( str . charAt ( i ) - '0' ) ; System . out . print ( sum + " ▁ " ) ; prev = sum ; counter ++ ; } } public static void main ( String [ ] args ) { String str = "12212" ; int b = 3 , k = 3 ; substringConversions ( str , b , k ) ; } }

import math as mt NEW_LINE def substringConversions ( str1 , k , b ) : NEW_LINE INDENT for i in range ( 0 , len ( str1 ) - k + 1 ) : NEW_LINE INDENT sub = str1 [ i : k + i ] NEW_LINE Sum = 0 NEW_LINE counter = 0 NEW_LINE for i in range ( len ( sub ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT Sum = ( Sum + ( ( ord ( sub [ i ] ) - ord ( '0' ) ) * pow ( b , counter ) ) ) NEW_LINE counter += 1 NEW_LINE DEDENT print ( Sum , end = " ▁ " ) NEW_LINE DEDENT DEDENT str1 = "12212" NEW_LINE b = 3 NEW_LINE k = 3 NEW_LINE substringConversions ( str1 , b , k ) NEW_LINE