import java . io . * ; import java . util . Arrays ; class GFG { static int countTripletsLessThanL ( int n , int L , int [ ] arr ) { Arrays . sort ( arr ) ; int ways = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { int mostDistantDistance = arr [ k ] - arr [ i ] ; if ( mostDistantDistance <= L ) { ways ++ ; } } } } return ways ; } static public void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int L = 3 ; int ans = countTripletsLessThanL ( n , L , arr ) ; System . out . println ( " Total ▁ Number ▁ of ▁ ways ▁ = ▁ " + ans ) ; } }

def countTripletsLessThanL ( n , L , arr ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE ways = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT mostDistantDistance = arr [ k ] - arr [ i ] NEW_LINE if ( mostDistantDistance <= L ) : NEW_LINE INDENT ways += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT return ways NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE L = 3 NEW_LINE ans = countTripletsLessThanL ( n , L , arr ) NEW_LINE print ( " Total ▁ Number ▁ of ▁ ways ▁ = " , ans ) NEW_LINE DEDENT


class GFG { static void findPair ( int arr [ ] , int n ) { if ( n < 2 ) { System . out . print ( " - 1" ) ; return ; } int i , first , second ; first = second = - 1 ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > first ) { second = first ; first = arr [ i ] ; } else if ( arr [ i ] > second ) { second = arr [ i ] ; } } System . out . println ( " n ▁ = ▁ " + first + " ▁ and ▁ r ▁ = ▁ " + second ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 } ; int n = arr . length ; findPair ( arr , n ) ; } }

def findPair ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT i = 0 NEW_LINE first = - 1 NEW_LINE second = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT second = first NEW_LINE first = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] > second ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT print ( " n ▁ = " , first , " and ▁ r ▁ = " , second ) NEW_LINE DEDENT arr = [ 0 , 2 , 3 , 4 , 1 , 6 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE findPair ( arr , n ) NEW_LINE


import java . math . * ; class GFG { public static void checkType ( int arr [ ] , int n ) { if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] <= arr [ n - 1 ] ) System . out . println ( " Increasing " ) ; else if ( arr [ 0 ] >= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) System . out . println ( " Decreasing " ) ; else if ( arr [ 0 ] <= arr [ 1 ] && arr [ n - 2 ] >= arr [ n - 1 ] ) System . out . println ( " Increasing ▁ then ▁ decreasing " ) ; else System . out . println ( " Decreasing ▁ then ▁ increasing " ) ; } public static void main ( String [ ] args ) { int [ ] arr = new int [ ] { 1 , 2 , 3 , 4 } ; int n = arr . length ; checkType ( arr , n ) ; } }

def checkType ( arr , n ) : NEW_LINE INDENT if ( arr [ 0 ] <= arr [ 1 ] and arr [ n - 2 ] <= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Increasing " ) ; NEW_LINE DEDENT elif ( arr [ 0 ] >= arr [ 1 ] and arr [ n - 2 ] >= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Decreasing " ) ; NEW_LINE DEDENT elif ( arr [ 0 ] <= arr [ 1 ] and arr [ n - 2 ] >= arr [ n - 1 ] ) : NEW_LINE INDENT print ( " Increasing ▁ then ▁ decreasing " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Decreasing ▁ then ▁ increasing " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE checkType ( arr , n ) ; NEW_LINE DEDENT


class GFG { static int countsubarray ( int array [ ] , int n , int k ) { int count = 0 ; int i , j , mul ; for ( i = 0 ; i < n ; i ++ ) { if ( array [ i ] <= k ) count ++ ; mul = array [ i ] ; for ( j = i + 1 ; j < n ; j ++ ) { mul = mul * array [ j ] ; if ( mul <= k ) count ++ ; else break ; } } return count ; } public static void main ( String args [ ] ) { int array [ ] = { 1 , 2 , 3 , 4 } ; int k = 10 ; int size = array . length ; int count = countsubarray ( array , size , k ) ; System . out . print ( count ) ; } }

def countsubarray ( array , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if array [ i ] <= k : NEW_LINE INDENT count += 1 NEW_LINE DEDENT mul = array [ i ] NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT mul = mul * array [ j ] NEW_LINE if mul <= k : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT array = [ 1 , 2 , 3 , 4 ] NEW_LINE k = 10 NEW_LINE size = len ( array ) NEW_LINE count = countsubarray ( array , size , k ) ; NEW_LINE print ( count , end = " ▁ " ) NEW_LINE


import java . io . * ; import java . util . * ; public class GFG { static int MaxSumDifference ( Integer [ ] a , int n ) { List < Integer > finalSequence = new ArrayList < Integer > ( ) ; Arrays . sort ( a ) ; for ( int i = 0 ; i < n / 2 ; ++ i ) { finalSequence . add ( a [ i ] ) ; finalSequence . add ( a [ n - i - 1 ] ) ; } if ( n % 2 != 0 ) finalSequence . add ( a [ n / 2 ] ) ; int MaximumSum = 0 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { MaximumSum = MaximumSum + Math . abs ( finalSequence . get ( i ) - finalSequence . get ( i + 1 ) ) ; } MaximumSum = MaximumSum + Math . abs ( finalSequence . get ( n - 1 ) - finalSequence . get ( 0 ) ) ; return MaximumSum ; } public static void main ( String args [ ] ) { Integer [ ] a = { 1 , 2 , 4 , 8 } ; int n = a . length ; System . out . print ( MaxSumDifference ( a , n ) ) ; } }

import numpy as np NEW_LINE class GFG : NEW_LINE INDENT def MaxSumDifference ( a , n ) : NEW_LINE INDENT np . sort ( a ) ; NEW_LINE j = 0 NEW_LINE finalSequence = [ 0 for x in range ( n ) ] NEW_LINE for i in range ( 0 , int ( n / 2 ) ) : NEW_LINE INDENT finalSequence [ j ] = a [ i ] NEW_LINE finalSequence [ j + 1 ] = a [ n - i - 1 ] NEW_LINE j = j + 2 NEW_LINE DEDENT if ( n % 2 != 0 ) : NEW_LINE INDENT finalSequence [ n - 1 ] = a [ n / 2 ] NEW_LINE DEDENT MaximumSum = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT MaximumSum = ( MaximumSum + abs ( finalSequence [ i ] - finalSequence [ i + 1 ] ) ) NEW_LINE DEDENT MaximumSum = ( MaximumSum + abs ( finalSequence [ n - 1 ] - finalSequence [ 0 ] ) ) ; NEW_LINE print ( MaximumSum ) NEW_LINE DEDENT DEDENT a = [ 1 , 2 , 4 , 8 ] NEW_LINE n = len ( a ) NEW_LINE GFG . MaxSumDifference ( a , n ) ; NEW_LINE


import java . util . Arrays ; class GFG { static final int maximum = 1000 ; static int lcs ( String X , String Y , int m , int n , int dp [ ] [ ] ) { if ( m == 0 || n == 0 ) { return 0 ; } if ( dp [ m - 1 ] [ n - 1 ] != - 1 ) { return dp [ m - 1 ] [ n - 1 ] ; } if ( X . charAt ( m - 1 ) == Y . charAt ( n - 1 ) ) { dp [ m - 1 ] [ n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 , dp ) ; return dp [ m - 1 ] [ n - 1 ] ; } else { dp [ m - 1 ] [ n - 1 ] = Math . max ( lcs ( X , Y , m , n - 1 , dp ) , lcs ( X , Y , m - 1 , n , dp ) ) ; return dp [ m - 1 ] [ n - 1 ] ; } } public static void main ( String [ ] args ) { String X = " AGGTAB " ; String Y = " GXTXAYB " ; int m = X . length ( ) ; int n = Y . length ( ) ; int dp [ ] [ ] = new int [ m ] [ maximum ] ; for ( int [ ] row : dp ) { Arrays . fill ( row , - 1 ) ; } System . out . println ( " Length ▁ of ▁ LCS : ▁ " + lcs ( X , Y , m , n , dp ) ) ; } }

maximum = 1000 NEW_LINE def lcs ( X , Y , m , n , dp ) : NEW_LINE INDENT if ( m == 0 or n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ m - 1 ] [ n - 1 ] != - 1 ) : NEW_LINE INDENT return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT if ( X [ m - 1 ] == Y [ n - 1 ] ) : NEW_LINE INDENT dp [ m - 1 ] [ n - 1 ] = 1 + lcs ( X , Y , m - 1 , n - 1 , dp ) NEW_LINE return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ m - 1 ] [ n - 1 ] = max ( lcs ( X , Y , m , n - 1 , dp ) , lcs ( X , Y , m - 1 , n , dp ) ) NEW_LINE return dp [ m - 1 ] [ n - 1 ] NEW_LINE DEDENT DEDENT X = " AGGTAB " NEW_LINE Y = " GXTXAYB " NEW_LINE m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE dp = [ [ - 1 for i in range ( maximum ) ] for i in range ( m ) ] NEW_LINE print ( " Length ▁ of ▁ LCS : " , lcs ( X , Y , m , n , dp ) ) NEW_LINE


import java . util . HashMap ; class GFG { static String smallestFreq ( String S1 , String S2 ) { HashMap < String , Integer > banned = new HashMap < > ( ) ; for ( int i = 0 ; i < S2 . length ( ) ; i ++ ) { String s = " " ; while ( i < S2 . length ( ) && S2 . charAt ( i ) != ' ▁ ' ) s += S2 . charAt ( i ++ ) ; banned . put ( s , banned . get ( s ) == null ? 1 : banned . get ( s ) + 1 ) ; } HashMap < String , Integer > result = new HashMap < > ( ) ; String ans = " " ; int freq = 0 ; for ( int i = 0 ; i < S1 . length ( ) ; i ++ ) { String s = " " ; while ( i < S1 . length ( ) && S1 . charAt ( i ) != ' ▁ ' ) s += S1 . charAt ( i ++ ) ; if ( banned . get ( s ) == null ) { result . put ( s , result . get ( s ) == null ? 1 : result . get ( s ) + 1 ) ; if ( result . get ( s ) > freq || ( result . get ( s ) == freq && s . compareTo ( ans ) < 0 ) ) { ans = s ; freq = result . get ( s ) ; } } } return ans ; } public static void main ( String [ ] args ) { String S1 = " geeks ▁ for ▁ geeks ▁ is ▁ best ▁ place ▁ to ▁ learn " ; String S2 = " bad ▁ place " ; System . out . println ( smallestFreq ( S1 , S2 ) ) ; } }

from collections import defaultdict NEW_LINE def smallestFreq ( S1 , S2 ) : NEW_LINE INDENT banned = defaultdict ( lambda : 0 ) NEW_LINE i = 0 NEW_LINE while i < len ( S2 ) : NEW_LINE INDENT s = " " NEW_LINE while i < len ( S2 ) and S2 [ i ] != ' ▁ ' : NEW_LINE INDENT s += S2 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE banned [ s ] += 1 NEW_LINE DEDENT result = defaultdict ( lambda : 0 ) NEW_LINE ans = " " NEW_LINE freq = 0 NEW_LINE i = 0 NEW_LINE while i < len ( S1 ) : NEW_LINE INDENT s = " " NEW_LINE while i < len ( S1 ) and S1 [ i ] != ' ▁ ' : NEW_LINE INDENT s += S1 [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i += 1 NEW_LINE if banned [ s ] == 0 : NEW_LINE INDENT result [ s ] += 1 NEW_LINE if ( result [ s ] > freq or ( result [ s ] == freq and s < ans ) ) : NEW_LINE INDENT ans = s NEW_LINE freq = result [ s ] NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S1 = " geeks ▁ for ▁ geeks ▁ is ▁ best ▁ place ▁ to ▁ learn " NEW_LINE S2 = " bad ▁ place " NEW_LINE print ( smallestFreq ( S1 , S2 ) ) NEW_LINE DEDENT


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

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


class GFG { static String X , Y ; static int lcs ( int i , int j , int count ) { if ( i == 0 || j == 0 ) { return count ; } if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { count = lcs ( i - 1 , j - 1 , count + 1 ) ; } count = Math . max ( count , Math . max ( lcs ( i , j - 1 , 0 ) , lcs ( i - 1 , j , 0 ) ) ) ; return count ; } public static void main ( String [ ] args ) { int n , m ; X = " abcdxyz " ; Y = " xyzabcd " ; n = X . length ( ) ; m = Y . length ( ) ; System . out . println ( lcs ( n , m , 0 ) ) ; } }

def lcs ( i , j , count ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT return count NEW_LINE DEDENT if ( X [ i - 1 ] == Y [ j - 1 ] ) : NEW_LINE INDENT count = lcs ( i - 1 , j - 1 , count + 1 ) NEW_LINE DEDENT count = max ( count , max ( lcs ( i , j - 1 , 0 ) , lcs ( i - 1 , j , 0 ) ) ) NEW_LINE return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = " abcdxyz " NEW_LINE Y = " xyzabcd " NEW_LINE n = len ( X ) NEW_LINE m = len ( Y ) NEW_LINE print ( lcs ( n , m , 0 ) ) NEW_LINE DEDENT


class GFG { static boolean isPower ( int x , int y ) { int res1 = ( int ) Math . log ( y ) / ( int ) Math . log ( x ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } public static void main ( String args [ ] ) { if ( isPower ( 27 , 729 ) ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } }

import math NEW_LINE def isPower ( x , y ) : NEW_LINE INDENT res1 = math . log ( y ) // math . log ( x ) ; NEW_LINE res2 = math . log ( y ) / math . log ( x ) ; NEW_LINE return 1 if ( res1 == res2 ) else 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( isPower ( 27 , 729 ) ) ; NEW_LINE DEDENT


class GFG { static int maxProdSum ( int arr [ ] , int n ) { int leftArraySum = 0 , maxProduct = 0 ; for ( int i = 0 ; i < n ; i ++ ) { leftArraySum += arr [ i ] ; int rightArraySum = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) { rightArraySum += arr [ j ] ; } int k = leftArraySum * rightArraySum ; if ( k > maxProduct ) { maxProduct = k ; } } return maxProduct ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 10 , 1 , 7 , 2 , 9 } ; int n = arr . length ; System . out . print ( maxProdSum ( arr , n ) ) ; } }

def maxProdSum ( arr , n ) : NEW_LINE INDENT leftArraySum = 0 ; NEW_LINE maxProduct = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT leftArraySum += arr [ i ] ; NEW_LINE rightArraySum = 0 ; NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT rightArraySum += arr [ j ] ; NEW_LINE DEDENT k = leftArraySum * rightArraySum ; NEW_LINE if ( k > maxProduct ) : NEW_LINE INDENT maxProduct = k ; NEW_LINE DEDENT DEDENT return maxProduct ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 10 , 1 , 7 , 2 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxProdSum ( arr , n ) ) ; NEW_LINE DEDENT


class GFG { static String minWord = " " , maxWord = " " ; static void minMaxLengthWords ( String input ) { int len = input . length ( ) ; int si = 0 , ei = 0 ; int min_length = len , min_start_index = 0 , max_length = 0 , max_start_index = 0 ; while ( ei <= len ) { if ( ei < len && input . charAt ( ei ) != ' ▁ ' ) { ei ++ ; } else { int curr_length = ei - si ; if ( curr_length < min_length ) { min_length = curr_length ; min_start_index = si ; } if ( curr_length > max_length ) { max_length = curr_length ; max_start_index = si ; } ei ++ ; si = ei ; } } minWord = input . substring ( min_start_index , min_start_index + min_length ) ; maxWord = input . substring ( max_start_index , max_length ) ; } public static void main ( String [ ] args ) { String a = " GeeksforGeeks ▁ A ▁ Computer ▁ Science ▁ portal ▁ for ▁ Geeks " ; minMaxLengthWords ( a ) ; System . out . print ( " Minimum ▁ length ▁ word : ▁ " + minWord + " \n Maximum ▁ length ▁ word : ▁ " + maxWord ) ; } }

def minMaxLengthWords ( inp ) : NEW_LINE INDENT length = len ( inp ) NEW_LINE si = ei = 0 NEW_LINE min_length = length NEW_LINE min_start_index = max_length = max_start_index = 0 NEW_LINE while ei <= length : NEW_LINE INDENT if ( ei < length ) and ( inp [ ei ] != " ▁ " ) : NEW_LINE INDENT ei += 1 NEW_LINE DEDENT else : NEW_LINE INDENT curr_length = ei - si NEW_LINE if curr_length < min_length : NEW_LINE INDENT min_length = curr_length NEW_LINE min_start_index = si NEW_LINE DEDENT if curr_length > max_length : NEW_LINE INDENT max_length = curr_length NEW_LINE max_start_index = si NEW_LINE DEDENT ei += 1 NEW_LINE si = ei NEW_LINE DEDENT DEDENT minWord = inp [ min_start_index : min_start_index + min_length ] NEW_LINE maxWord = inp [ max_start_index : max_length ] NEW_LINE print ( " Minimum ▁ length ▁ word : ▁ " , minWord ) NEW_LINE print ( " Maximum ▁ length ▁ word : ▁ " , maxWord ) NEW_LINE DEDENT a = " GeeksforGeeks ▁ A ▁ Computer ▁ Science ▁ portal ▁ for ▁ Geeks " NEW_LINE minMaxLengthWords ( a ) NEW_LINE


import java . util . * ; class GFG { static void findMaximumBefore ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int currAns = - 1 ; for ( int j = i - 1 ; j >= 0 ; j -- ) { if ( arr [ j ] > currAns && arr [ j ] < arr [ i ] ) { currAns = arr [ j ] ; } } System . out . print ( currAns + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 7 , 6 , 8 , 5 } ; int n = arr . length ; findMaximumBefore ( arr , n ) ; } }

def findMaximumBefore ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT currAns = - 1 NEW_LINE for j in range ( i - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ j ] > currAns and arr [ j ] < arr [ i ] ) : NEW_LINE INDENT currAns = arr [ j ] NEW_LINE DEDENT DEDENT print ( currAns , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 7 , 6 , 8 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE findMaximumBefore ( arr , n ) NEW_LINE DEDENT


import java . util . Arrays ; import java . util . Collections ; class GFG { static int minimumCostOfBreaking ( Integer X [ ] , Integer Y [ ] , int m , int n ) { int res = 0 ; Arrays . sort ( X , Collections . reverseOrder ( ) ) ; Arrays . sort ( Y , Collections . reverseOrder ( ) ) ; int hzntl = 1 , vert = 1 ; int i = 0 , j = 0 ; while ( i < m && j < n ) { if ( X [ i ] > Y [ j ] ) { res += X [ i ] * vert ; hzntl ++ ; i ++ ; } else { res += Y [ j ] * hzntl ; vert ++ ; j ++ ; } } int total = 0 ; while ( i < m ) total += X [ i ++ ] ; res += total * vert ; total = 0 ; while ( j < n ) total += Y [ j ++ ] ; res += total * hzntl ; return res ; } public static void main ( String arg [ ] ) { int m = 6 , n = 4 ; Integer X [ ] = { 2 , 1 , 3 , 1 , 4 } ; Integer Y [ ] = { 4 , 1 , 2 } ; System . out . print ( minimumCostOfBreaking ( X , Y , m - 1 , n - 1 ) ) ; } }

def minimumCostOfBreaking ( X , Y , m , n ) : NEW_LINE INDENT res = 0 NEW_LINE X . sort ( reverse = True ) NEW_LINE Y . sort ( reverse = True ) NEW_LINE hzntl = 1 ; vert = 1 NEW_LINE i = 0 ; j = 0 NEW_LINE while ( i < m and j < n ) : NEW_LINE INDENT if ( X [ i ] > Y [ j ] ) : NEW_LINE INDENT res += X [ i ] * vert NEW_LINE hzntl += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT res += Y [ j ] * hzntl NEW_LINE vert += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT total = 0 NEW_LINE while ( i < m ) : NEW_LINE INDENT total += X [ i ] NEW_LINE i += 1 NEW_LINE DEDENT res += total * vert NEW_LINE total = 0 NEW_LINE while ( j < n ) : NEW_LINE INDENT total += Y [ j ] NEW_LINE j += 1 NEW_LINE DEDENT res += total * hzntl NEW_LINE return res NEW_LINE DEDENT m = 6 ; n = 4 NEW_LINE X = [ 2 , 1 , 3 , 1 , 4 ] NEW_LINE Y = [ 4 , 1 , 2 ] NEW_LINE print ( minimumCostOfBreaking ( X , Y , m - 1 , n - 1 ) ) NEW_LINE


public class GFG { public static int getSum ( int arr [ ] , int p ) { return arr . length ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 8 } ; int p = 7 ; System . out . print ( getSum ( arr , p ) ) ; } }

def getSum ( arr , p ) : NEW_LINE INDENT return len ( arr ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 8 ] NEW_LINE p = 7 NEW_LINE print ( getSum ( arr , p ) ) NEW_LINE DEDENT


class GFG { static int [ ] solve ( int n ) { int low = 1 , high = ( int ) 1e4 , x = n , p = 0 ; while ( low <= high ) { int mid = ( low + high ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( x - sum >= 1 ) { p = mid ; low = mid + 1 ; } else { high = mid - 1 ; } } int start = 1 , end = ( int ) 1e4 , y = 1 , q = 0 ; while ( start <= end ) { int mid = ( start + end ) / 2 ; int sum = ( mid * ( mid + 1 ) ) / 2 ; if ( y + sum <= n ) { q = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } x = x - ( p * ( p + 1 ) ) / 2 ; y = y + ( q * ( q + 1 ) ) / 2 ; int r = x ; int c = q + 1 - n + y ; int ans [ ] = { r , c } ; return ans ; } public static void main ( String [ ] args ) { int n = 5 ; int [ ] p = solve ( n ) ; System . out . println ( p [ 0 ] + " ▁ " + p [ 1 ] ) ; } }

def solve ( n ) : NEW_LINE INDENT low = 1 NEW_LINE high = 10 ** 4 NEW_LINE x , p = n , 0 NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE sum = ( mid * ( mid + 1 ) ) // 2 NEW_LINE if ( x - sum >= 1 ) : NEW_LINE INDENT p = mid NEW_LINE low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT DEDENT start , end , y , q = 1 , 10 ** 4 , 1 , 0 NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE sum = ( mid * ( mid + 1 ) ) // 2 NEW_LINE if ( y + sum <= n ) : NEW_LINE INDENT q = mid NEW_LINE start = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT DEDENT x = x - ( p * ( p + 1 ) ) // 2 NEW_LINE y = y + ( q * ( q + 1 ) ) // 2 NEW_LINE r = x NEW_LINE c = q + 1 - n + y NEW_LINE return r , c NEW_LINE DEDENT n = 5 NEW_LINE r , c = solve ( n ) NEW_LINE print ( r , c ) NEW_LINE


class GFG { static int maxNormalSubString ( char [ ] P , char [ ] Q , int K , int N ) { if ( K == 0 ) return 0 ; int count = 0 ; int left = 0 , right = 0 ; int ans = 0 ; while ( right < N ) { while ( right < N && count <= K ) { int pos = P [ right ] - ' a ' ; if ( Q [ pos ] == '0' ) { if ( count + 1 > K ) break ; else count ++ ; } right ++ ; if ( count <= K ) ans = Math . max ( ans , right - left ) ; } while ( left < right ) { int pos = P [ left ] - ' a ' ; left ++ ; if ( Q [ pos ] == '0' ) count -- ; if ( count < K ) break ; } } return ans ; } public static void main ( String [ ] args ) { String P = " giraffe " , Q = "01111001111111111011111111" ; int K = 2 ; int N = P . length ( ) ; System . out . print ( maxNormalSubString ( P . toCharArray ( ) , Q . toCharArray ( ) , K , N ) ) ; } }

def maxNormalSubstring ( P , Q , K , N ) : NEW_LINE INDENT if ( K == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT count = 0 NEW_LINE left , right = 0 , 0 NEW_LINE ans = 0 NEW_LINE while ( right < N ) : NEW_LINE INDENT while ( right < N and count <= K ) : NEW_LINE INDENT pos = ord ( P [ right ] ) - ord ( ' a ' ) NEW_LINE if ( Q [ pos ] == '0' ) : NEW_LINE INDENT if ( count + 1 > K ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT right += 1 NEW_LINE if ( count <= K ) : NEW_LINE INDENT ans = max ( ans , right - left ) NEW_LINE DEDENT DEDENT while ( left < right ) : NEW_LINE INDENT pos = ord ( P [ left ] ) - ord ( ' a ' ) NEW_LINE left += 1 NEW_LINE if ( Q [ pos ] == '0' ) : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT if ( count < K ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if ( __name__ == " _ _ main _ _ " ) : NEW_LINE INDENT P = " giraffe " NEW_LINE Q = "01111001111111111011111111" NEW_LINE K = 2 NEW_LINE N = len ( P ) NEW_LINE print ( maxNormalSubstring ( P , Q , K , N ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int getPrime ( int arr [ ] , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; Vector < Boolean > prime = new Vector < > ( max_val + 1 ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime . add ( i , Boolean . TRUE ) ; prime . add ( 1 , Boolean . FALSE ) ; prime . add ( 2 , Boolean . FALSE ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime . get ( p ) == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime . add ( i , Boolean . FALSE ) ; } } int maximum = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime . get ( arr [ i ] ) ) { maximum = Math . max ( maximum , arr [ i ] ) ; } } return maximum ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 10 , 15 , 7 , 6 , 8 , 13 } ; int n = arr . length ; System . out . println ( getPrime ( arr , n ) ) ; } }

from math import sqrt NEW_LINE def getPrime ( arr , n ) : NEW_LINE INDENT max_val = arr [ 0 ] NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] > max_val ) : NEW_LINE INDENT max_val = arr [ i ] NEW_LINE DEDENT DEDENT prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( 2 , int ( sqrt ( max_val ) ) + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT maximum = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT maximum = max ( maximum , arr [ i ] ) NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 10 , 15 , 7 , 6 , 8 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getPrime ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class Node { int data ; Node left , right ; } ; static int heightoftree ( Node root ) { if ( root == null ) return 0 ; int left = heightoftree ( root . left ) ; int right = heightoftree ( root . right ) ; return ( ( left > right ? left : right ) + 1 ) ; } static void printPerLevelMinimum ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; q . add ( null ) ; int min = Integer . MAX_VALUE ; int level = 0 ; while ( q . isEmpty ( ) == false ) { Node node = q . peek ( ) ; q . remove ( ) ; if ( node == null ) { System . out . print ( " level ▁ " + level + " ▁ min ▁ is ▁ = ▁ " + min + " \n " ) ; if ( q . isEmpty ( ) ) break ; q . add ( null ) ; level ++ ; min = Integer . MAX_VALUE ; continue ; } if ( min > node . data ) min = node . data ; if ( node . left != null ) { q . add ( node . left ) ; } if ( node . right != null ) { q . add ( node . right ) ; } } } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . left = temp . right = null ; return temp ; } public static void main ( String [ ] args ) { Node root = newNode ( 7 ) ; root . left = newNode ( 6 ) ; root . right = newNode ( 5 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 3 ) ; root . right . left = newNode ( 2 ) ; root . right . right = newNode ( 1 ) ; System . out . print ( " Every ▁ Level ▁ minimum ▁ is " + " \n " ) ; printPerLevelMinimum ( root ) ; } }

from queue import Queue NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = self . right = None NEW_LINE DEDENT DEDENT def heightoftree ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT left = heightoftree ( root . left ) NEW_LINE right = heightoftree ( root . right ) NEW_LINE if left > right : NEW_LINE INDENT return left + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return right + 1 NEW_LINE DEDENT DEDENT def printPerLevelMinimum ( root ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return NEW_LINE DEDENT q = Queue ( ) NEW_LINE q . put ( root ) NEW_LINE q . put ( None ) NEW_LINE Min = 9999999999999 NEW_LINE level = 0 NEW_LINE while ( q . empty ( ) == False ) : NEW_LINE INDENT node = q . queue [ 0 ] NEW_LINE q . get ( ) NEW_LINE if ( node == None ) : NEW_LINE INDENT print ( " level " , level , " min ▁ is ▁ = " , Min ) NEW_LINE if ( q . empty ( ) ) : NEW_LINE INDENT break NEW_LINE DEDENT q . put ( None ) NEW_LINE level += 1 NEW_LINE Min = 999999999999 NEW_LINE continue NEW_LINE DEDENT if ( Min > node . data ) : NEW_LINE INDENT Min = node . data NEW_LINE DEDENT if ( node . left != None ) : NEW_LINE INDENT q . put ( node . left ) NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT q . put ( node . right ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 7 ) NEW_LINE root . left = newNode ( 6 ) NEW_LINE root . right = newNode ( 5 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 3 ) NEW_LINE root . right . left = newNode ( 2 ) NEW_LINE root . right . right = newNode ( 1 ) NEW_LINE print ( " Every ▁ Level ▁ minimum ▁ is " ) NEW_LINE printPerLevelMinimum ( root ) NEW_LINE DEDENT


import java . util . * ; class GFG { static final int N = 10000 ; static int n , m ; static int [ ] dp = new int [ N ] ; static int minOperations ( int k ) { if ( k <= 0 || k >= 10000 ) return 1000000000 ; if ( k == m ) return 0 ; dp [ k ] = dp [ k ] ; if ( dp [ k ] != - 1 ) return dp [ k ] ; dp [ k ] = 1000000000 ; dp [ k ] = 1 + Math . min ( minOperations ( 2 * k ) , minOperations ( k - 1 ) ) ; return dp [ k ] ; } public static void main ( String [ ] args ) { n = 4 ; m = 6 ; Arrays . fill ( dp , - 1 ) ; System . out . println ( minOperations ( n ) ) ; } }

N = 1000 NEW_LINE dp = [ - 1 ] * N NEW_LINE def minOperations ( k ) : NEW_LINE INDENT if ( k <= 0 or k >= 1000 ) : NEW_LINE INDENT return 1e9 NEW_LINE DEDENT if ( k == m ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT dp [ k ] = dp [ k ] NEW_LINE if ( dp [ k ] != - 1 ) : NEW_LINE INDENT return dp [ k ] NEW_LINE DEDENT dp [ k ] = 1e9 NEW_LINE dp [ k ] = 1 + min ( minOperations ( 2 * k ) , minOperations ( k - 1 ) ) NEW_LINE return dp [ k ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE m = 6 NEW_LINE print ( minOperations ( n ) ) NEW_LINE DEDENT


import java . io . * ; class MaxSumPath { static int maxSum ( int [ ] [ ] mat , int n ) { if ( n == 1 ) return mat [ 0 ] [ 0 ] ; int dp [ ] [ ] = new int [ n ] [ n ] ; int maxSum = Integer . MIN_VALUE , max ; for ( int j = 0 ; j < n ; j ++ ) dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < n ; j ++ ) { max = Integer . MIN_VALUE ; if ( ( ( j - 1 ) >= 0 ) && ( max < dp [ i + 1 ] [ j - 1 ] ) ) max = dp [ i + 1 ] [ j - 1 ] ; if ( ( ( j + 1 ) < n ) && ( max < dp [ i + 1 ] [ j + 1 ] ) ) max = dp [ i + 1 ] [ j + 1 ] ; dp [ i ] [ j ] = mat [ i ] [ j ] + max ; } } for ( int j = 0 ; j < n ; j ++ ) if ( maxSum < dp [ 0 ] [ j ] ) maxSum = dp [ 0 ] [ j ] ; return maxSum ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 5 , 6 , 1 , 7 } , { - 2 , 10 , 8 , - 1 } , { 3 , - 7 , - 9 , 11 } , { 12 , - 4 , 2 , 6 } } ; int n = 4 ; System . out . println ( " Maximum ▁ Sum ▁ = ▁ " + maxSum ( mat , n ) ) ; } }

SIZE = 10 NEW_LINE INT_MIN = - 10000000 NEW_LINE def maxSum ( mat , n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return mat [ 0 ] [ 0 ] NEW_LINE DEDENT dp = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE maxSum = INT_MIN NEW_LINE for j in range ( n ) : NEW_LINE INDENT dp [ n - 1 ] [ j ] = mat [ n - 1 ] [ j ] NEW_LINE DEDENT for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT maxi = INT_MIN NEW_LINE if ( ( ( ( j - 1 ) >= 0 ) and ( maxi < dp [ i + 1 ] [ j - 1 ] ) ) ) : NEW_LINE INDENT maxi = dp [ i + 1 ] [ j - 1 ] NEW_LINE DEDENT if ( ( ( ( j + 1 ) < n ) and ( maxi < dp [ i + 1 ] [ j + 1 ] ) ) ) : NEW_LINE INDENT maxi = dp [ i + 1 ] [ j + 1 ] NEW_LINE DEDENT dp [ i ] [ j ] = mat [ i ] [ j ] + maxi NEW_LINE DEDENT DEDENT for j in range ( n ) : NEW_LINE INDENT if ( maxSum < dp [ 0 ] [ j ] ) : NEW_LINE INDENT maxSum = dp [ 0 ] [ j ] NEW_LINE DEDENT DEDENT return maxSum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT mat = [ [ 5 , 6 , 1 , 7 ] , [ - 2 , 10 , 8 , - 1 ] , [ 3 , - 7 , - 9 , 11 ] , [ 12 , - 4 , 2 , 6 ] ] NEW_LINE n = 4 NEW_LINE print ( " Maximum ▁ Sum = " , maxSum ( mat , n ) ) NEW_LINE DEDENT


class GFG { static int countPaths ( int m , int n ) { int [ ] [ ] dp = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 1 ; i <= m ; i ++ ) { for ( int j = 1 ; j <= n ; j ++ ) { if ( i == 1 || j == 1 ) dp [ i ] [ j ] = 1 ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ; } } return dp [ m ] [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; int m = 5 ; System . out . println ( countPaths ( n , m ) ) ; } }

def countPaths ( m , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( m + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( 1 , m + 1 ) : NEW_LINE INDENT for j in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i == 1 or j == 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] + dp [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ m ] [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE m = 5 NEW_LINE print ( countPaths ( n , m ) ) NEW_LINE DEDENT


class GFG { public static int findMaxGCD ( int arr [ ] , int n ) { int high = 0 ; for ( int i = 0 ; i < n ; i ++ ) high = Math . max ( high , arr [ i ] ) ; int count [ ] = new int [ high + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) count [ arr [ i ] ] ++ ; int counter = 0 ; for ( int i = high ; i >= 1 ; i -- ) { int j = i ; while ( j <= high ) { if ( count [ j ] > 0 ) counter += count [ j ] ; j += i ; if ( counter == 2 ) return i ; } counter = 0 ; } return 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 8 , 8 , 12 } ; int n = arr . length ; System . out . println ( findMaxGCD ( arr , n ) ) ; } }

def findMaxGCD ( arr , n ) : NEW_LINE INDENT high = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT high = max ( high , arr [ i ] ) NEW_LINE DEDENT count = [ 0 ] * ( high + 1 ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT counter = 0 NEW_LINE for i in range ( high , 0 , - 1 ) : NEW_LINE INDENT j = i NEW_LINE while ( j <= high ) : NEW_LINE INDENT if ( count [ j ] > 0 ) : NEW_LINE INDENT counter += count [ j ] NEW_LINE DEDENT j += i NEW_LINE if ( counter == 2 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT counter = 0 NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 4 , 8 , 8 , 12 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxGCD ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static int calculateEvenSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ 2 * n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; int sum = 0 ; for ( int i = 2 ; i <= 2 * n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum += fibo [ i ] ; } return sum ; } public static void main ( String [ ] args ) { int n = 8 ; System . out . println ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + + calculateEvenSum ( n ) ) ; } }

def calculateEvenSum ( n ) : NEW_LINE INDENT if n <= 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT fibo = [ 0 ] * ( 2 * n + 1 ) NEW_LINE fibo [ 0 ] , fibo [ 1 ] = 0 , 1 NEW_LINE sum = 0 NEW_LINE for i in range ( 2 , 2 * n + 1 ) : NEW_LINE INDENT fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] NEW_LINE if i % 2 == 0 : NEW_LINE INDENT sum += fibo [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 NEW_LINE print ( " Even ▁ indexed ▁ Fibonacci ▁ Sum ▁ upto " , n , " terms : " , calculateEvenSum ( n ) ) NEW_LINE DEDENT


class GFG { static void printPattern ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= i ; j ++ ) System . out . print ( i ) ; System . out . print ( " \n " ) ; } for ( int i = n - 1 ; i > 0 ; i -- ) { for ( int j = i ; j > 0 ; j -- ) System . out . print ( i ) ; System . out . print ( " \n " ) ; } } public static void main ( String arg [ ] ) { int n = 8 ; printPattern ( n ) ; } }

def printPattern ( n ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT n = 8 NEW_LINE printPattern ( n ) NEW_LINE


class GFGq { static int max_Element = ( int ) ( 1e6 + 5 ) ; static int sum1 [ ] = new int [ max_Element ] , sum2 [ ] = new int [ max_Element ] , sum3 [ ] = new int [ max_Element ] ; static void precomputation ( int arr [ ] , int n ) { for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum1 [ j ] += i ; for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum2 [ j ] += ( sum1 [ j ] - i ) * i ; for ( int i = 1 ; i < max_Element ; i ++ ) sum2 [ i ] /= 2 ; for ( int i = 1 ; i < max_Element ; i ++ ) for ( int j = i ; j < max_Element ; j += i ) sum3 [ j ] += i * ( sum2 [ j ] - i * ( sum1 [ j ] - i ) ) ; for ( int i = 1 ; i < max_Element ; i ++ ) sum3 [ i ] /= 3 ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( sum3 [ arr [ i ] ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 9 , 5 , 6 } ; int n = arr . length ; precomputation ( arr , n ) ; } }

max_Element = 100005 NEW_LINE sum1 = [ 0 for i in range ( max_Element ) ] NEW_LINE sum2 = [ 0 for i in range ( max_Element ) ] NEW_LINE sum3 = [ 0 for i in range ( max_Element ) ] NEW_LINE def precomputation ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT for j in range ( i , max_Element , i ) : NEW_LINE INDENT sum1 [ j ] += i NEW_LINE DEDENT DEDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT for j in range ( i , max_Element , i ) : NEW_LINE INDENT sum2 [ j ] += ( sum1 [ j ] - i ) * i NEW_LINE DEDENT DEDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT sum2 [ i ] = int ( sum2 [ i ] / 2 ) NEW_LINE DEDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT for j in range ( i , max_Element , i ) : NEW_LINE INDENT sum3 [ j ] += i * ( sum2 [ j ] - i * ( sum1 [ j ] - i ) ) NEW_LINE DEDENT DEDENT for i in range ( 1 , max_Element , 1 ) : NEW_LINE INDENT sum3 [ i ] = int ( sum3 [ i ] / 3 ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( sum3 [ arr [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 9 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE precomputation ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 3 ; static int M = 3 ; static int sumMaxOccurring ( int arr [ ] [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < M ; j ++ ) { if ( mp . containsKey ( arr [ i ] [ j ] ) ) { mp . put ( arr [ i ] [ j ] , mp . get ( arr [ i ] [ j ] ) + 1 ) ; } else { mp . put ( arr [ i ] [ j ] , 1 ) ; } } } int sum = 0 ; int maxFreq = Integer . MIN_VALUE ; for ( Map . Entry < Integer , Integer > itr : mp . entrySet ( ) ) { if ( itr . getValue ( ) > maxFreq ) { maxFreq = itr . getValue ( ) ; } } for ( Map . Entry < Integer , Integer > itr : mp . entrySet ( ) ) { if ( itr . getValue ( ) == maxFreq ) { sum += ( itr . getKey ( ) ) * ( itr . getValue ( ) ) ; } } return sum ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 3 } , { 1 , 3 , 2 } , { 1 , 5 , 6 } } ; System . out . println ( sumMaxOccurring ( mat ) ) ; } }

import sys NEW_LINE N = 3 NEW_LINE M = 3 NEW_LINE def sumMaxOccuring ( arr ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if arr [ i ] [ j ] in mp : NEW_LINE INDENT mp [ arr [ i ] [ j ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] [ j ] ] = 1 NEW_LINE DEDENT DEDENT DEDENT s = 0 NEW_LINE maxFreq = - sys . maxsize NEW_LINE for i in mp : NEW_LINE INDENT if mp [ i ] > maxFreq : NEW_LINE INDENT maxFreq = mp [ i ] NEW_LINE DEDENT DEDENT for i in mp : NEW_LINE INDENT if mp [ i ] == maxFreq : NEW_LINE INDENT s += i * mp [ i ] NEW_LINE DEDENT DEDENT return s NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 1 , 2 , 3 ] , [ 1 , 3 , 2 ] , [ 1 , 5 , 6 ] ] NEW_LINE print ( sumMaxOccuring ( mat ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int sumDigitSquare ( int n ) { int sq = 0 ; while ( n != 0 ) { int digit = n % 10 ; sq += digit * digit ; n = n / 10 ; } return sq ; } static boolean isHappy ( int n ) { while ( true ) { if ( n == 1 ) return true ; n = sumDigitSquare ( n ) ; if ( n == 4 ) return false ; } } public static void main ( String args [ ] ) { int n = 23 ; if ( isHappy ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def sumDigitSquare ( n ) : NEW_LINE INDENT sq = 0 NEW_LINE while ( n ) : NEW_LINE INDENT digit = n % 10 NEW_LINE sq = sq + digit * digit NEW_LINE n = n // 10 NEW_LINE DEDENT return sq NEW_LINE DEDENT def isHappy ( n ) : NEW_LINE INDENT while ( 1 ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT n = sumDigitSquare ( n ) NEW_LINE if ( n == 4 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT n = 23 NEW_LINE if ( isHappy ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


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

def centeredTetrahedralNumber ( n ) : NEW_LINE INDENT return ( 2 * n + 1 ) * ( n * n + n + 3 ) // 3 NEW_LINE DEDENT n = 6 NEW_LINE print ( centeredTetrahedralNumber ( n ) ) NEW_LINE


public class GFG { public static void main ( String args [ ] ) { String binaryNumber = "1001" ; System . out . println ( Integer . parseInt ( binaryNumber , 2 ) ) ; } }

n = input ( ) NEW_LINE s = int ( n , 2 ) NEW_LINE print ( s ) NEW_LINE


class GFG { static int countTriplets ( int arr [ ] , int n ) { int [ ] freq = new int [ 100 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( freq [ arr [ i ] + arr [ j ] ] > 0 ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int n = 4 ; int arr [ ] = { 1 , 5 , 3 , 2 } ; System . out . print ( countTriplets ( arr , n ) ) ; } }

def countTriplets ( arr , n ) : NEW_LINE INDENT freq = [ 0 for i in range ( 100 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT if ( freq [ arr [ i ] + arr [ j ] ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 NEW_LINE arr = [ 1 , 5 , 3 , 2 ] NEW_LINE print ( countTriplets ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void result ( String s , int n ) { HashSet < String > st = new HashSet < String > ( ) ; for ( int i = 0 ; i < ( int ) s . length ( ) ; i ++ ) { String ans = " " ; for ( int j = i ; j < ( int ) s . length ( ) ; j ++ ) { ans += s . charAt ( j ) ; if ( ans . length ( ) == n ) { st . add ( ans ) ; break ; } } } for ( String it : st ) System . out . print ( it + " ▁ " ) ; } public static void main ( String [ ] args ) { String s = " abca " ; int n = 3 ; result ( s , n ) ; } }

def result ( s , n ) : NEW_LINE INDENT st = set ( ) ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT ans = " " ; NEW_LINE for j in range ( i , len ( s ) ) : NEW_LINE INDENT ans += s [ j ] ; NEW_LINE if ( len ( ans ) == n ) : NEW_LINE INDENT st . add ( ans ) ; NEW_LINE break ; NEW_LINE DEDENT DEDENT DEDENT for it in st : NEW_LINE INDENT print ( it , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abca " ; NEW_LINE n = 3 ; NEW_LINE result ( s , n ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int search ( int searchspace [ ] , int s , int e , int key ) { int ans = - 1 ; while ( s <= e ) { int mid = ( s + e ) / 2 ; if ( searchspace [ mid ] - key >= 0 ) { ans = mid ; e = mid - 1 ; } else { s = mid + 1 ; } } return ans ; } static int longestSubarray ( int [ ] a , int n ) { int SuffixSum [ ] = new int [ n + 1 ] ; SuffixSum [ n ] = 0 ; for ( int i = n - 1 ; i >= 0 ; -- i ) { SuffixSum [ i ] = SuffixSum [ i + 1 ] + a [ i ] ; } int ans = 0 ; int searchspace [ ] = new int [ n ] ; int index [ ] = new int [ n ] ; int j = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( ( j == 0 ) || SuffixSum [ i ] > searchspace [ j - 1 ] ) { searchspace [ j ] = SuffixSum [ i ] ; index [ j ] = i ; j ++ ; } int idx = search ( searchspace , 0 , j - 1 , SuffixSum [ i + 1 ] ) ; if ( idx != - 1 ) ans = Math . max ( ans , i - index [ idx ] + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { int [ ] a = { - 1 , 4 , - 2 , - 5 , 6 , - 8 } ; int n = a . length ; System . out . println ( longestSubarray ( a , n ) ) ; } }

import math as mt NEW_LINE def search ( searchspace , s , e , key ) : NEW_LINE INDENT ans = - 1 NEW_LINE while s <= e : NEW_LINE INDENT mid = ( s + e ) // 2 NEW_LINE if searchspace [ mid ] - key >= 0 : NEW_LINE INDENT ans = mid NEW_LINE e = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT s = mid + 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT def longestSubarray ( a , n ) : NEW_LINE INDENT SuffixSum = [ 0 for i in range ( n + 1 ) ] NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT SuffixSum [ i ] = SuffixSum [ i + 1 ] + a [ i ] NEW_LINE DEDENT ans = 0 NEW_LINE searchspace = [ 0 for i in range ( n ) ] NEW_LINE index = [ 0 for i in range ( n ) ] NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if j == 0 or ( SuffixSum [ i ] > searchspace [ j - 1 ] ) : NEW_LINE INDENT searchspace [ j ] = SuffixSum [ i ] NEW_LINE index [ j ] = i NEW_LINE j += 1 NEW_LINE DEDENT idx = search ( searchspace , 0 , j - 1 , SuffixSum [ i + 1 ] ) NEW_LINE if idx != - 1 : NEW_LINE INDENT ans = max ( ans , i - index [ idx ] + 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ - 1 , 4 , - 2 , - 5 , 6 , - 8 ] NEW_LINE n = len ( a ) NEW_LINE print ( longestSubarray ( a , n ) ) NEW_LINE


public class GFG { static boolean isDivisiblePalindrome ( int n ) { int hash [ ] = new int [ 10 ] ; int digitSum = 0 ; while ( n != 0 ) { digitSum += n % 10 ; hash [ n % 10 ] ++ ; n /= 10 ; } if ( digitSum % 3 != 0 ) return false ; int oddCount = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( hash [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount > 1 ) return false ; else return true ; } public static void main ( String [ ] args ) { int n = 34734 ; System . out . print ( isDivisiblePalindrome ( n ) ) ; } }

def isDivisiblePalindrome ( n ) : NEW_LINE INDENT hash = [ 0 ] * 10 NEW_LINE digitSum = 0 NEW_LINE while ( n ) : NEW_LINE INDENT digitSum += n % 10 NEW_LINE hash [ n % 10 ] += 1 NEW_LINE n //= 10 NEW_LINE DEDENT if ( digitSum % 3 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT oddCount = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( hash [ i ] % 2 != 0 ) : NEW_LINE INDENT oddCount += 1 NEW_LINE DEDENT DEDENT if ( oddCount > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT n = 34734 NEW_LINE if ( isDivisiblePalindrome ( n ) ) : NEW_LINE INDENT print ( " True " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " False " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int dp [ ] [ ] = new int [ 1000 ] [ 1000 ] ; static boolean isprime ( long num ) { int i ; if ( num <= 1 ) return false ; for ( i = 2 ; i * i <= num ; i ++ ) { if ( num % i == 0 ) { return false ; } } return true ; } static long convert ( String s , int i , int j ) { long temp = 0 ; int k ; for ( k = i ; k <= j ; k ++ ) { temp = temp * 10 + ( s . charAt ( k ) - '0' ) ; } return temp ; } static int solve ( String s , int i , int j ) { int k ; long num = convert ( s , i , j ) ; if ( isprime ( num ) ) { return 0 ; } if ( i == j && isprime ( num ) ) return 0 ; if ( i == j && isprime ( num ) == false ) return Integer . MAX_VALUE ; if ( dp [ i ] [ j ] != 0 ) return dp [ i ] [ j ] ; int ans = Integer . MAX_VALUE ; for ( k = i ; k < j ; k ++ ) { int left = solve ( s , i , k ) ; if ( left == Integer . MAX_VALUE ) { continue ; } int right = solve ( s , k + 1 , j ) ; if ( right == Integer . MAX_VALUE ) { continue ; } ans = Math . min ( ans , 1 + left + right ) ; } return dp [ i ] [ j ] = ans ; } public static void main ( String [ ] args ) { String s = "2352" ; int n = s . length ( ) ; int cuts = solve ( s , 0 , n - 1 ) ; if ( cuts != Integer . MAX_VALUE ) { System . out . print ( cuts ) ; } else { System . out . print ( " Not ▁ Possible " ) ; } } }

import numpy as np ; NEW_LINE import sys NEW_LINE dp = np . zeros ( ( 1000 , 1000 ) ) ; NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def isprime ( num ) : NEW_LINE INDENT if ( num <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , int ( num ** ( 1 / 2 ) ) + 1 ) : NEW_LINE INDENT if ( num % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def convert ( s , i , j ) : NEW_LINE INDENT temp = 0 ; NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT temp = temp * 10 + ( ord ( s [ k ] ) - ord ( '0' ) ) ; NEW_LINE DEDENT return temp ; NEW_LINE DEDENT def solve ( s , i , j ) : NEW_LINE INDENT num = convert ( s , i , j ) ; NEW_LINE if ( isprime ( num ) ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i == j and isprime ( num ) ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i == j and isprime ( num ) == False ) : NEW_LINE INDENT return INT_MAX ; NEW_LINE DEDENT if ( dp [ i ] [ j ] ) : NEW_LINE INDENT return dp [ i ] [ j ] ; NEW_LINE DEDENT ans = INT_MAX ; NEW_LINE for k in range ( i , j ) : NEW_LINE INDENT left = solve ( s , i , k ) ; NEW_LINE if ( left == INT_MAX ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT right = solve ( s , k + 1 , j ) ; NEW_LINE if ( right == INT_MAX ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT ans = min ( ans , 1 + left + right ) ; NEW_LINE DEDENT dp [ i ] [ j ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "2352" ; NEW_LINE n = len ( s ) ; NEW_LINE cuts = solve ( s , 0 , n - 1 ) ; NEW_LINE if ( cuts != INT_MAX ) : NEW_LINE INDENT print ( cuts ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ Possible " ) ; NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static int countWays ( String str , int m ) { HashSet < Character > set = new HashSet < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) set . add ( str . charAt ( i ) ) ; int result = 0 ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { HashMap < Character , Integer > first_map = new HashMap < Character , Integer > ( ) ; HashMap < Character , Integer > second_map = new HashMap < Character , Integer > ( ) ; for ( int j = 0 ; j < i ; j ++ ) { if ( first_map . containsKey ( str . charAt ( j ) ) ) first_map . put ( str . charAt ( j ) , ( first_map . get ( str . charAt ( j ) ) + 1 ) ) ; else first_map . put ( str . charAt ( j ) , 1 ) ; } for ( int k = i ; k < str . length ( ) ; k ++ ) { if ( second_map . containsKey ( str . charAt ( k ) ) ) second_map . put ( str . charAt ( k ) , ( second_map . get ( str . charAt ( k ) ) + 1 ) ) ; else second_map . put ( str . charAt ( k ) , 1 ) ; } Iterator itr = set . iterator ( ) ; int total_count = 0 ; while ( itr . hasNext ( ) ) { int first_count = 0 , second_count = 0 ; char ch = ( char ) itr . next ( ) ; if ( first_map . containsKey ( ch ) ) first_count = first_map . get ( ch ) ; if ( second_map . containsKey ( ch ) ) second_count = second_map . get ( ch ) ; if ( first_count == second_count && first_count != 0 ) total_count += 1 ; } if ( total_count >= m ) result += 1 ; } return result ; } public static void main ( String [ ] args ) { String str = " aabbccaa " ; int m = 2 ; System . out . println ( countWays ( str , m ) ) ; } }

from collections import defaultdict NEW_LINE def countWays ( string , m ) : NEW_LINE INDENT Set = set ( ) NEW_LINE for i in range ( 0 , len ( string ) ) : NEW_LINE INDENT Set . add ( string [ i ] ) NEW_LINE DEDENT result = 0 NEW_LINE for i in range ( 1 , len ( string ) ) : NEW_LINE INDENT first_map = defaultdict ( lambda : 0 ) NEW_LINE second_map = defaultdict ( lambda : 0 ) NEW_LINE for j in range ( 0 , i ) : NEW_LINE INDENT first_map [ string [ j ] ] += 1 NEW_LINE DEDENT for k in range ( i , len ( string ) ) : NEW_LINE INDENT second_map [ string [ k ] ] += 1 NEW_LINE DEDENT total_count = 0 NEW_LINE for ch in Set : NEW_LINE INDENT first_count , second_count = 0 , 0 NEW_LINE if ch in first_map : NEW_LINE INDENT first_count = first_map [ ch ] NEW_LINE DEDENT if ch in second_map : NEW_LINE INDENT second_count = second_map [ ch ] NEW_LINE DEDENT if first_count == second_count and first_count != 0 : NEW_LINE INDENT total_count += 1 NEW_LINE DEDENT DEDENT if total_count >= m : NEW_LINE INDENT result += 1 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " aabbccaa " NEW_LINE m = 2 NEW_LINE print ( countWays ( string , m ) ) NEW_LINE DEDENT


import java . util . ArrayList ; import java . util . List ; class GFG { static boolean canFormPalindrome ( String str ) { List < Character > list = new ArrayList < Character > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( list . contains ( str . charAt ( i ) ) ) list . remove ( ( Character ) str . charAt ( i ) ) ; else list . add ( str . charAt ( i ) ) ; } if ( str . length ( ) % 2 == 0 && list . isEmpty ( ) || ( str . length ( ) % 2 == 1 && list . size ( ) == 1 ) ) return true ; else return false ; } public static void main ( String args [ ] ) { if ( canFormPalindrome ( " geeksforgeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; if ( canFormPalindrome ( " geeksogeeks " ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def canFormPalindrome ( strr ) : NEW_LINE INDENT listt = [ ] NEW_LINE for i in range ( len ( strr ) ) : NEW_LINE INDENT if ( strr [ i ] in listt ) : NEW_LINE INDENT listt . remove ( strr [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT listt . append ( strr [ i ] ) NEW_LINE DEDENT DEDENT if ( len ( strr ) % 2 == 0 and len ( listt ) == 0 or   \ ( len ( strr ) % 2 == 1 and len ( listt ) == 1 ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( canFormPalindrome ( " geeksforgeeks " ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT if ( canFormPalindrome ( " geeksogeeks " ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int LongestSubarray ( int [ ] arr , int n , int k ) { int [ ] arr2 = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) arr2 [ i ] = arr [ i ] % k ; int current_length , max_length = 0 ; int j ; for ( int i = 0 ; i < n ; ) { current_length = 1 ; for ( j = i + 1 ; j < n ; j ++ ) { if ( arr2 [ j ] == arr2 [ i ] ) current_length ++ ; else break ; } max_length = Math . max ( max_length , current_length ) ; i = j ; } return max_length ; } public static void main ( String [ ] args ) { int [ ] arr = { 4 , 9 , 7 , 18 , 29 , 11 } ; int n = arr . length ; int k = 11 ; System . out . println ( LongestSubarray ( arr , n , k ) ) ; } }

def LongestSubarray ( arr , n , k ) : NEW_LINE INDENT arr2 = [ 0 ] * n NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr2 [ i ] = arr [ i ] % k NEW_LINE DEDENT max_length = 0 NEW_LINE i = 0 NEW_LINE while i < n : NEW_LINE INDENT current_length = 1 NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr2 [ j ] == arr2 [ i ] ) : NEW_LINE INDENT current_length += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT max_length = max ( max_length , current_length ) NEW_LINE i = j NEW_LINE i += 1 NEW_LINE DEDENT return max_length NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 9 , 7 , 18 , 29 , 11 ] NEW_LINE n = len ( arr ) NEW_LINE k = 11 NEW_LINE print ( LongestSubarray ( arr , n , k ) ) NEW_LINE DEDENT


public class GFG { static void find_volume ( double area , double h ) { double Volume = ( area * h ) ; System . out . println ( " Volume : ▁ " + Volume ) ; } static void find_Surface_area ( double area , double a , double h ) { double Surface_area = ( 2 * area ) + ( 8 * a * h ) ; System . out . println ( " Surface ▁ area : ▁ " + Surface_area ) ; } public static void main ( String [ ] args ) { double h = 1 ; double a = 6 ; double d = 2 ; double area = 2 * a * d ; find_Surface_area ( area , a , h ) ; find_volume ( area , h ) ; } }

import math NEW_LINE def find_volume ( area , h ) : NEW_LINE INDENT Volume = ( area * h ) NEW_LINE print ( " Volume : ▁ " , end = " ▁ " ) NEW_LINE print ( Volume ) NEW_LINE DEDENT def find_Surface_area ( area , a , h ) : NEW_LINE INDENT Surface_area = ( 2 * area ) + ( 8 * a * h ) NEW_LINE print ( " Surface ▁ area : ▁ " , end = " ▁ " ) NEW_LINE print ( Surface_area ) NEW_LINE DEDENT h = 1 NEW_LINE a = 6 NEW_LINE d = 2 NEW_LINE area = 2 * a * d NEW_LINE find_Surface_area ( area , a , h ) NEW_LINE find_volume ( area , h ) NEW_LINE


import java . util . * ; class GFG { static int xor_all_elements ( int n ) { switch ( n & 3 ) { case 0 : return n ; case 1 : return 1 ; case 2 : return n + 1 ; } return 0 ; } static Vector < Integer > findArray ( int xorr [ ] , int n ) { Vector < Integer > arr = new Vector < Integer > ( ) ; int xor_all = xor_all_elements ( n ) ; int xor_adjacent = 0 ; for ( int i = 0 ; i < n - 1 ; i += 2 ) { xor_adjacent = xor_adjacent ^ xorr [ i ] ; } int last_element = xor_all ^ xor_adjacent ; arr . add ( last_element ) ; for ( int i = n - 2 ; i >= 0 ; i -- ) { last_element = xorr [ i ] ^ last_element ; arr . add ( last_element ) ; } return arr ; } public static void main ( String [ ] args ) { Vector < Integer > arr = new Vector < Integer > ( ) ; int xorr [ ] = { 7 , 5 , 3 , 7 } ; int n = 5 ; arr = findArray ( xorr , n ) ; for ( int i = n - 1 ; i >= 0 ; i -- ) { System . out . print ( arr . get ( i ) + " ▁ " ) ; } } }

def xor_all_elements ( n ) : NEW_LINE INDENT if n & 3 == 0 : NEW_LINE INDENT return n NEW_LINE DEDENT elif n & 3 == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif n & 3 == 2 : NEW_LINE INDENT return n + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findArray ( xorr , n ) : NEW_LINE INDENT arr = [ ] NEW_LINE xor_all = xor_all_elements ( n ) NEW_LINE xor_adjacent = 0 NEW_LINE for i in range ( 0 , n - 1 , 2 ) : NEW_LINE INDENT xor_adjacent = xor_adjacent ^ xorr [ i ] NEW_LINE DEDENT last_element = xor_all ^ xor_adjacent NEW_LINE arr . append ( last_element ) NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT last_element = xorr [ i ] ^ last_element NEW_LINE arr . append ( last_element ) NEW_LINE DEDENT return arr NEW_LINE DEDENT xorr = [ 7 , 5 , 3 , 7 ] NEW_LINE n = 5 NEW_LINE arr = findArray ( xorr , n ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT


class GFG { static void result ( int N ) { for ( int num = 0 ; num < N ; num ++ ) { if ( num % 3 == 0 && num % 5 == 0 ) System . out . print ( num + " ▁ " ) ; } } public static void main ( String [ ] args ) { int N = 100 ; result ( N ) ; } }

def result ( N ) : NEW_LINE INDENT for num in range ( N ) : NEW_LINE INDENT if num % 3 == 0 and num % 5 == 0 : NEW_LINE INDENT print ( str ( num ) + " ▁ " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT pass NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 100 NEW_LINE result ( N ) NEW_LINE DEDENT


import java . util . Arrays ; public class GFG { static boolean strmatch ( String str , String pattern , int n , int m ) { if ( m == 0 ) return ( n == 0 ) ; boolean [ ] [ ] lookup = new boolean [ n + 1 ] [ m + 1 ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) Arrays . fill ( lookup [ i ] , false ) ; lookup [ 0 ] [ 0 ] = true ; for ( int j = 1 ; j <= m ; j ++ ) if ( pattern . charAt ( j - 1 ) == ' * ' ) lookup [ 0 ] [ j ] = lookup [ 0 ] [ j - 1 ] ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= m ; j ++ ) { if ( pattern . charAt ( j - 1 ) == ' * ' ) lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] || lookup [ i - 1 ] [ j ] ; else if ( pattern . charAt ( j - 1 ) == ' ? ' || str . charAt ( i - 1 ) == pattern . charAt ( j - 1 ) ) lookup [ i ] [ j ] = lookup [ i - 1 ] [ j - 1 ] ; else lookup [ i ] [ j ] = false ; } } return lookup [ n ] [ m ] ; } public static void main ( String args [ ] ) { String str = " baaabab " ; String pattern = " * * * * * ba * * * * * ab " ; if ( strmatch ( str , pattern , str . length ( ) , pattern . length ( ) ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def strrmatch ( strr , pattern , n , m ) : NEW_LINE INDENT if ( m == 0 ) : NEW_LINE INDENT return ( n == 0 ) NEW_LINE DEDENT lookup = [ [ False for i in range ( m + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE lookup [ 0 ] [ 0 ] = True NEW_LINE for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( pattern [ j - 1 ] == ' * ' ) : NEW_LINE INDENT lookup [ 0 ] [ j ] = lookup [ 0 ] [ j - 1 ] NEW_LINE DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , m + 1 ) : NEW_LINE INDENT if ( pattern [ j - 1 ] == ' * ' ) : NEW_LINE INDENT lookup [ i ] [ j ] = lookup [ i ] [ j - 1 ] or lookup [ i - 1 ] [ j ] NEW_LINE DEDENT elif ( pattern [ j - 1 ] == ' ? ' or strr [ i - 1 ] == pattern [ j - 1 ] ) : NEW_LINE INDENT lookup [ i ] [ j ] = lookup [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT lookup [ i ] [ j ] = False NEW_LINE DEDENT DEDENT DEDENT return lookup [ n ] [ m ] NEW_LINE DEDENT strr = " baaabab " NEW_LINE pattern = " * * * * * ba * * * * * ab " NEW_LINE if ( strrmatch ( strr , pattern , len ( strr ) , len ( pattern ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . HashMap ; import java . util . Map ; public class GFG { static int countWords ( String str [ ] , int n ) { HashMap < String , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( str [ i ] ) ) { int get = m . get ( str [ i ] ) ; m . put ( str [ i ] , get + 1 ) ; } else { m . put ( str [ i ] , 1 ) ; } } int res = 0 ; for ( Map . Entry < String , Integer > it : m . entrySet ( ) ) { if ( it . getValue ( ) == 2 ) res ++ ; } return res ; } public static void main ( String args [ ] ) { String s [ ] = { " hate " , " love " , " peace " , " love " , " peace " , " hate " , " love " , " peace " , " love " , " peace " } ; int n = s . length ; System . out . println ( countWords ( s , n ) ) ; } }

def countWords ( stri , n ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ stri [ i ] ] = m . get ( stri [ i ] , 0 ) + 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in m . values ( ) : NEW_LINE INDENT if i == 2 : NEW_LINE INDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT s = [ " hate " , " love " , " peace " , " love " , " peace " , " hate " , " love " , " peace " , " love " , " peace " ] NEW_LINE n = len ( s ) NEW_LINE print ( countWords ( s , n ) ) NEW_LINE


import java . util . * ; class GFG { static int MAX = 100 ; static int [ ] [ ] [ ] [ ] dp = new int [ MAX ] [ MAX ] [ MAX ] [ 2 ] ; static int countPathsUtil ( int i , int j , int k , int d ) { if ( i < 0 || j < 0 ) return 0 ; if ( i == 0 && j == 0 ) return 1 ; if ( k == 0 ) { if ( d == 0 && i == 0 ) return 1 ; if ( d == 1 && j == 0 ) return 1 ; return 0 ; } if ( dp [ i ] [ j ] [ k ] [ d ] != - 1 ) return dp [ i ] [ j ] [ k ] [ d ] ; if ( d == 0 ) return dp [ i ] [ j ] [ k ] [ d ] = countPathsUtil ( i , j - 1 , k , d ) + countPathsUtil ( i - 1 , j , k - 1 , d == 1 ? 0 : 1 ) ; return dp [ i ] [ j ] [ k ] [ d ] = countPathsUtil ( i - 1 , j , k , d ) + countPathsUtil ( i , j - 1 , k - 1 , d == 1 ? 0 : 1 ) ; } static int countPaths ( int i , int j , int k ) { if ( i == 0 && j == 0 ) return 1 ; for ( int p = 0 ; p < MAX ; p ++ ) { for ( int q = 0 ; q < MAX ; q ++ ) { for ( int r = 0 ; r < MAX ; r ++ ) for ( int s = 0 ; s < 2 ; s ++ ) dp [ p ] [ q ] [ r ] [ s ] = - 1 ; } } return countPathsUtil ( i - 1 , j , k , 1 ) + countPathsUtil ( i , j - 1 , k , 0 ) ; } public static void main ( String [ ] args ) { int m = 3 , n = 3 , k = 2 ; System . out . println ( " Number ▁ of ▁ paths ▁ is ▁ " + countPaths ( m - 1 , n - 1 , k ) ) ; } }

MAX = 100 NEW_LINE dp = [ [ [ [ - 1 for col in range ( 2 ) ] for col in range ( MAX ) ] for row in range ( MAX ) ] for row in range ( MAX ) ] NEW_LINE def countPathsUtil ( i , j , k , d ) : NEW_LINE INDENT if ( i < 0 or j < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( i == 0 and j == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( k == 0 ) : NEW_LINE INDENT if ( d == 0 and i == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( d == 1 and j == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if ( dp [ i ] [ j ] [ k ] [ d ] != - 1 ) : NEW_LINE INDENT return dp [ i ] [ j ] [ k ] [ d ] NEW_LINE DEDENT if ( d == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] [ k ] [ d ] = countPathsUtil ( i , j - 1 , k , d ) +   \ NEW_LINE INDENT countPathsUtil ( i - 1 , j , k - 1 , not d ) NEW_LINE DEDENT return dp [ i ] [ j ] [ k ] [ d ] NEW_LINE DEDENT dp [ i ] [ j ] [ k ] [ d ] = countPathsUtil ( i - 1 , j , k , d ) +   \ NEW_LINE INDENT countPathsUtil ( i , j - 1 , k - 1 , not d ) NEW_LINE DEDENT return dp [ i ] [ j ] [ k ] [ d ] NEW_LINE DEDENT def countPaths ( i , j , k ) : NEW_LINE INDENT if ( i == 0 and j == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return countPathsUtil ( i - 1 , j , k , 1 ) + \ NEW_LINE INDENT countPathsUtil ( i , j - 1 , k , 0 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 3 NEW_LINE n = 3 NEW_LINE k = 2 NEW_LINE print ( " Number ▁ of ▁ paths ▁ is " , countPaths ( m - 1 , n - 1 , k ) ) NEW_LINE DEDENT


public void push ( int new_data ) { Node new_Node = new Node ( new_data ) ; new_Node . next = head ; new_Node . prev = null ; if ( head != null ) head . prev = new_Node ; head = new_Node ; }

def push ( self , new_data ) : NEW_LINE INDENT new_node = Node ( data = new_data ) NEW_LINE new_node . next = self . head NEW_LINE new_node . prev = None NEW_LINE if self . head is not None : NEW_LINE INDENT self . head . prev = new_node NEW_LINE DEDENT self . head = new_node NEW_LINE DEDENT


class GFG { static int editDistanceWith2Ops ( String X , String Y ) { int m = X . length ( ) , n = Y . length ( ) ; int L [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 || j == 0 ) { L [ i ] [ j ] = 0 ; } else if ( X . charAt ( i - 1 ) == Y . charAt ( j - 1 ) ) { L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 ; } else { L [ i ] [ j ] = Math . max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) ; } } } int lcs = L [ m ] [ n ] ; return ( m - lcs ) + ( n - lcs ) ; } public static void main ( String [ ] args ) { String X = " abc " , Y = " acd " ; System . out . println ( editDistanceWith2Ops ( X , Y ) ) ; } }

def editDistanceWith2Ops ( X , Y ) : NEW_LINE INDENT m = len ( X ) NEW_LINE n = len ( Y ) NEW_LINE L = [ [ 0 for x in range ( m + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT if ( i == 0 or j == 0 ) : NEW_LINE INDENT L [ i ] [ j ] = 0 NEW_LINE DEDENT elif ( X [ i - 1 ] == Y [ j - 1 ] ) : NEW_LINE INDENT L [ i ] [ j ] = L [ i - 1 ] [ j - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT L [ i ] [ j ] = max ( L [ i - 1 ] [ j ] , L [ i ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT lcs = L [ m ] [ n ] NEW_LINE return ( m - lcs ) + ( n - lcs ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = " abc " NEW_LINE Y = " acd " NEW_LINE print ( editDistanceWith2Ops ( X , Y ) ) NEW_LINE DEDENT


import java . io . * ; public class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) { return a ; } else { return __gcd ( b , a % b ) ; } } static int LCM ( int x , int y , int z ) { int ans = ( ( x * y ) / ( __gcd ( x , y ) ) ) ; return ( ( z * ans ) / ( __gcd ( ans , z ) ) ) ; } static int findDivisible ( int n , int x , int y , int z ) { int lcm = LCM ( x , y , z ) ; int ndigitnumber = ( int ) Math . pow ( 10 , n - 1 ) ; int reminder = ndigitnumber % lcm ; if ( reminder == 0 ) return ndigitnumber ; ndigitnumber += lcm - reminder ; if ( ndigitnumber < Math . pow ( 10 , n ) ) return ndigitnumber ; else return 0 ; } static public void main ( String [ ] args ) { int n = 4 , x = 2 , y = 3 , z = 5 ; int res = findDivisible ( n , x , y , z ) ; if ( res != 0 ) System . out . println ( res ) ; else System . out . println ( " Not ▁ possible " ) ; } }

from fractions import gcd NEW_LINE import math NEW_LINE def LCM ( x , y , z ) : NEW_LINE INDENT ans = int ( ( x * y ) / ( gcd ( x , y ) ) ) NEW_LINE return int ( ( z * ans ) / ( gcd ( ans , z ) ) ) NEW_LINE DEDENT def findDivisible ( n , x , y , z ) : NEW_LINE INDENT lcm = LCM ( x , y , z ) NEW_LINE ndigitnumber = math . pow ( 10 , n - 1 ) NEW_LINE reminder = ndigitnumber % lcm NEW_LINE if reminder == 0 : NEW_LINE INDENT return ndigitnumber NEW_LINE DEDENT ndigitnumber += lcm - reminder NEW_LINE if ndigitnumber < math . pow ( 10 , n ) : NEW_LINE INDENT return int ( ndigitnumber ) NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT n = 4 NEW_LINE x = 2 NEW_LINE y = 3 NEW_LINE z = 5 NEW_LINE res = findDivisible ( n , x , y , z ) NEW_LINE if res != 0 : NEW_LINE INDENT print ( res ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT


class GFG { static boolean isPerfectCube ( int x ) { int cr = ( int ) Math . cbrt ( x ) ; return ( cr * cr * cr == x ) ; } static void canBePerfectCube ( int N , int K ) { if ( isPerfectCube ( N + K ) || isPerfectCube ( N - K ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } public static void main ( String [ ] args ) { int N = 7 ; int K = 1 ; canBePerfectCube ( N , K ) ; N = 5 ; K = 4 ; canBePerfectCube ( N , K ) ; N = 7 ; K = 2 ; canBePerfectCube ( N , K ) ; } }

def isPerfectCube ( x ) : NEW_LINE INDENT cr = int ( x ** ( 1 / 3 ) ) ; NEW_LINE return ( cr * cr * cr == x ) ; NEW_LINE DEDENT def canBePerfectCube ( N , K ) : NEW_LINE INDENT if ( isPerfectCube ( N + K ) or isPerfectCube ( N - K ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 7 ; K = 1 ; NEW_LINE canBePerfectCube ( N , K ) ; NEW_LINE N = 5 ; K = 4 ; NEW_LINE canBePerfectCube ( N , K ) ; NEW_LINE N = 7 ; K = 2 ; NEW_LINE canBePerfectCube ( N , K ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int invertBits ( int n ) { int x = ( int ) ( Math . log ( n ) / Math . log ( 2 ) ) ; int m = 1 << x ; m = m | m - 1 ; n = n ^ m ; return n ; } public static void main ( String [ ] args ) { int n = 20 ; System . out . print ( invertBits ( n ) ) ; } }

import math NEW_LINE def invertBits ( n ) : NEW_LINE INDENT x = int ( math . log ( n , 2 ) ) NEW_LINE m = 1 << x NEW_LINE m = m | m - 1 NEW_LINE n = n ^ m NEW_LINE return n NEW_LINE DEDENT n = 20 NEW_LINE print ( invertBits ( n ) ) NEW_LINE


import java . io . * ; class GFG { static float squarearea ( float r ) { if ( r < 0 ) return - 1 ; float a = 4 * ( float ) ( Math . pow ( r , 2 ) / 5 ) ; return a ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( squarearea ( r ) ) ; } }

def squarearea ( r ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT a = 4 * ( pow ( r , 2 ) / 5 ) NEW_LINE return a NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT r = 5 NEW_LINE print ( int ( squarearea ( r ) ) ) NEW_LINE DEDENT


class GFG { static int MinStep ( int a [ ] , int n ) { int positive = 0 , negative = 0 , zero = 0 ; int step = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) { zero ++ ; } else if ( a [ i ] < 0 ) { negative ++ ; step = step + ( - 1 - a [ i ] ) ; } else { positive ++ ; step = step + ( a [ i ] - 1 ) ; } } if ( negative % 2 == 0 ) { step = step + zero ; } else { if ( zero > 0 ) { step = step + zero ; } else { step = step + 2 ; } } return step ; } public static void main ( String [ ] args ) { int a [ ] = { 0 , - 2 , - 1 , - 3 , 4 } ; int n = a . length ; System . out . println ( MinStep ( a , n ) ) ; } }

def MinStep ( a , n ) : NEW_LINE INDENT positive = 0 ; NEW_LINE negative = 0 ; NEW_LINE zero = 0 ; NEW_LINE step = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] == 0 ) : NEW_LINE INDENT zero += 1 ; NEW_LINE DEDENT elif ( a [ i ] < 0 ) : NEW_LINE INDENT negative += 1 ; NEW_LINE step = step + ( - 1 - a [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT positive += 1 ; NEW_LINE step = step + ( a [ i ] - 1 ) ; NEW_LINE DEDENT DEDENT if ( negative % 2 == 0 ) : NEW_LINE INDENT step = step + zero ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( zero > 0 ) : NEW_LINE INDENT step = step + zero ; NEW_LINE DEDENT else : NEW_LINE INDENT step = step + 2 ; NEW_LINE DEDENT DEDENT return step ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 0 , - 2 , - 1 , - 3 , 4 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( MinStep ( a , n ) ) ; NEW_LINE DEDENT


class GFG { static int countLattice ( int r ) { if ( r <= 0 ) return 0 ; int result = 4 ; for ( int x = 1 ; x < r ; x ++ ) { int ySquare = r * r - x * x ; int y = ( int ) Math . sqrt ( ySquare ) ; if ( y * y == ySquare ) result += 4 ; } return result ; } public static void main ( String arg [ ] ) { int r = 5 ; System . out . println ( countLattice ( r ) ) ; } }

import math NEW_LINE def countLattice ( r ) : NEW_LINE INDENT if ( r <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT result = 4 NEW_LINE for x in range ( 1 , r ) : NEW_LINE INDENT ySquare = r * r - x * x NEW_LINE y = int ( math . sqrt ( ySquare ) ) NEW_LINE if ( y * y == ySquare ) : NEW_LINE INDENT result += 4 NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT r = 5 NEW_LINE print ( countLattice ( r ) ) NEW_LINE


class GFG { final static int TEN = 10 ; static int digitSum ( int n ) { int sum = 0 ; while ( n > 0 ) { sum += n % TEN ; n /= TEN ; } return sum ; } static int getNthTerm ( int n ) { int sum = digitSum ( n ) ; if ( sum % TEN == 0 ) return ( n * TEN ) ; int extra = TEN - ( sum % TEN ) ; return ( ( n * TEN ) + extra ) ; } static void firstNTerms ( int n ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( getNthTerm ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 10 ; firstNTerms ( n ) ; } }

TEN = 10 NEW_LINE def digitSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT sum += n % TEN NEW_LINE n //= TEN NEW_LINE DEDENT return sum NEW_LINE DEDENT def getNthTerm ( n ) : NEW_LINE INDENT sum = digitSum ( n ) NEW_LINE if ( sum % TEN == 0 ) : NEW_LINE INDENT return ( n * TEN ) NEW_LINE DEDENT extra = TEN - ( sum % TEN ) NEW_LINE return ( ( n * TEN ) + extra ) NEW_LINE DEDENT def firstNTerms ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( getNthTerm ( i ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 10 NEW_LINE firstNTerms ( n ) NEW_LINE


class GFG { static double sum ( double a , double b , double c , double d , double x ) { double ans = ( x * ( a + b ) * ( c - d ) ) / ( ( a * d ) - ( b * c ) ) ; return ans ; } public static void main ( String [ ] args ) { double a = 1 , b = 2 , c = 9 , d = 13 , x = 5 ; System . out . println ( sum ( a , b , c , d , x ) ) ; } }

def sum ( a , b , c , d , x ) : NEW_LINE INDENT ans = ( ( x * ( a + b ) * ( c - d ) ) / ( ( a * d ) - ( b * c ) ) ) ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b , c , d , x = 1 , 2 , 9 , 13 , 5 ; NEW_LINE print ( sum ( a , b , c , d , x ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void findSumPairs ( int a [ ] , int n ) { Map < Integer , Integer > mpp = new HashMap < > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { mpp . put ( a [ i ] + a [ j ] , mpp . get ( a [ i ] + a [ j ] ) == null ? 1 : mpp . get ( a [ i ] + a [ j ] ) + 1 ) ; } } int occur = 0 ; for ( Map . Entry < Integer , Integer > entry : mpp . entrySet ( ) ) { if ( entry . getValue ( ) > occur ) { occur = entry . getValue ( ) ; } } for ( Map . Entry < Integer , Integer > entry : mpp . entrySet ( ) ) { if ( entry . getValue ( ) == occur ) System . out . println ( entry . getKey ( ) ) ; } } public static void main ( String args [ ] ) { int a [ ] = { 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 } ; int n = a . length ; findSumPairs ( a , n ) ; } }

def findSumPairs ( a , n ) : NEW_LINE INDENT mpp = { i : 0 for i in range ( 21 ) } NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT mpp [ a [ i ] + a [ j ] ] += 1 NEW_LINE DEDENT DEDENT occur = 0 NEW_LINE for key , value in mpp . items ( ) : NEW_LINE INDENT if ( value > occur ) : NEW_LINE INDENT occur = value NEW_LINE DEDENT DEDENT for key , value in mpp . items ( ) : NEW_LINE INDENT if ( value == occur ) : NEW_LINE INDENT print ( key ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 8 , 3 , 11 , 4 , 9 , 2 , 7 ] NEW_LINE n = len ( a ) NEW_LINE findSumPairs ( a , n ) NEW_LINE DEDENT


class GFG { static int value ( char x ) { return ( int ) ( x - ' a ' ) ; } static String maximumProduct ( String str , int n ) { String answer = " " , curr = " " ; long maxProduct = 0 , product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product *= 1L * value ( str . charAt ( i ) ) ; curr += str . charAt ( i ) ; if ( product >= maxProduct ) { maxProduct = product ; answer = curr ; } if ( product == 0 ) { product = 1 ; curr = " " ; } } return answer ; } public static void main ( String [ ] args ) { String str = " sdtfakdhdahdzz " ; int n = str . length ( ) ; System . out . print ( maximumProduct ( str , n ) + " \n " ) ; } }

def value ( x ) : NEW_LINE INDENT return ( ord ( x ) - ord ( ' a ' ) ) NEW_LINE DEDENT def maximumProduct ( strr , n ) : NEW_LINE INDENT answer = " " NEW_LINE curr = " " NEW_LINE maxProduct = 0 NEW_LINE product = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT product *= value ( strr [ i ] ) NEW_LINE curr += strr [ i ] NEW_LINE if ( product >= maxProduct ) : NEW_LINE INDENT maxProduct = product NEW_LINE answer = curr NEW_LINE DEDENT if ( product == 0 ) : NEW_LINE INDENT product = 1 NEW_LINE curr = " " NEW_LINE DEDENT DEDENT return answer NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT strr = " sdtfakdhdahdzz " NEW_LINE n = len ( strr ) NEW_LINE print ( maximumProduct ( strr , n ) ) NEW_LINE DEDENT


class GFG { static boolean checkDivisibility ( String num ) { int length = num . length ( ) ; if ( length == 1 && num . charAt ( 0 ) == '0' ) return true ; if ( length % 3 == 1 ) { num += "00" ; length += 2 ; } else if ( length % 3 == 2 ) { num += "0" ; length += 1 ; } int sum = 0 , p = 1 ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int group = 0 ; group += num . charAt ( i -- ) - '0' ; group += ( num . charAt ( i -- ) - '0' ) * 10 ; group += ( num . charAt ( i ) - '0' ) * 100 ; sum = sum + group * p ; p *= ( - 1 ) ; } sum = Math . abs ( sum ) ; return ( sum % 13 == 0 ) ; } public static void main ( String [ ] args ) { String number = "83959092724" ; if ( checkDivisibility ( number ) ) System . out . println ( number + " ▁ is ▁ divisible ▁ by ▁ 13 . " ) ; else System . out . println ( number + " ▁ is ▁ not ▁ divisibe ▁ by ▁ 13 . " ) ; } }

def checkDivisibility ( num ) : NEW_LINE INDENT length = len ( num ) NEW_LINE if ( length == 1 and num [ 0 ] == '0' ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( length % 3 == 1 ) : NEW_LINE INDENT num = str ( num ) + "00" NEW_LINE length += 2 NEW_LINE DEDENT elif ( length % 3 == 2 ) : NEW_LINE INDENT num = str ( num ) + "0" NEW_LINE length += 1 NEW_LINE DEDENT sum = 0 NEW_LINE p = 1 NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT group = 0 NEW_LINE group += ord ( num [ i ] ) - ord ( '0' ) NEW_LINE i -= 1 NEW_LINE group += ( ord ( num [ i ] ) - ord ( '0' ) ) * 10 NEW_LINE i -= 1 NEW_LINE group += ( ord ( num [ i ] ) - ord ( '0' ) ) * 100 NEW_LINE sum = sum + group * p NEW_LINE p *= ( - 1 ) NEW_LINE DEDENT sum = abs ( sum ) NEW_LINE return ( sum % 13 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT number = "83959092724" NEW_LINE if ( checkDivisibility ( number ) ) : NEW_LINE INDENT print ( number , " is ▁ divisible ▁ by ▁ 13 . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( number , " is ▁ not ▁ divisibe ▁ by ▁ 13 . " ) NEW_LINE DEDENT DEDENT


class Sol { static class Node { int data ; Node next ; Node prev ; } ; static void reversePrint ( Node head_ref ) { Node tail = head_ref ; while ( tail . next != null ) { tail = tail . next ; } while ( tail != head_ref ) { System . out . print ( tail . data + " ▁ " ) ; tail = tail . prev ; } System . out . println ( tail . data ) ; } static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head_ref ) ; if ( ( head_ref ) != null ) ( head_ref ) . prev = new_node ; ( head_ref ) = new_node ; return head_ref ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 2 ) ; head = push ( head , 4 ) ; head = push ( head , 8 ) ; head = push ( head , 10 ) ; System . out . print ( " Linked ▁ List ▁ elements ▁ in ▁ reverse ▁ order ▁ : ▁ " ) ; reversePrint ( head ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def reversePrint ( head_ref ) : NEW_LINE INDENT tail = head_ref NEW_LINE while ( tail . next != None ) : NEW_LINE INDENT tail = tail . next NEW_LINE DEDENT while ( tail != head_ref ) : NEW_LINE INDENT print ( tail . data , end = " ▁ " ) NEW_LINE tail = tail . prev NEW_LINE DEDENT print ( tail . data ) NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . data = new_data NEW_LINE new_node . prev = None NEW_LINE new_node . next = head_ref NEW_LINE if ( head_ref != None ) : NEW_LINE INDENT head_ref . prev = new_node NEW_LINE DEDENT head_ref = new_node NEW_LINE return head_ref NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 10 ) NEW_LINE print ( " Linked ▁ List ▁ elements ▁ in ▁ reverse ▁ order ▁ : ▁ " ) NEW_LINE reversePrint ( head ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; public class GfG { public static int kthdigit ( int a , int b , int k ) { int p = ( int ) Math . pow ( a , b ) ; int count = 0 ; while ( p > 0 && count < k ) { int rem = p % 10 ; count ++ ; if ( count == k ) return rem ; p = p / 10 ; } return 0 ; } public static void main ( String argc [ ] ) { int a = 5 , b = 2 ; int k = 1 ; System . out . println ( kthdigit ( a , b , k ) ) ; } }

def kthdigit ( a , b , k ) : NEW_LINE INDENT p = a ** b NEW_LINE count = 0 NEW_LINE while ( p > 0 and count < k ) : NEW_LINE INDENT rem = p % 10 NEW_LINE count = count + 1 NEW_LINE if ( count == k ) : NEW_LINE INDENT return rem NEW_LINE DEDENT p = p / 10 ; NEW_LINE DEDENT DEDENT a = 5 NEW_LINE b = 2 NEW_LINE k = 1 NEW_LINE ans = kthdigit ( a , b , k ) NEW_LINE print ( ans ) NEW_LINE


import java . util . * ; class GFG { static boolean isDivisible ( int n ) { int d ; while ( ( n / 100 ) > 0 ) { d = n % 10 ; n /= 10 ; n = Math . abs ( n - ( d * 3 ) ) ; } return ( n % 31 == 0 ) ; } public static void main ( String [ ] args ) { int N = 1922 ; 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 * 3 ) ) NEW_LINE DEDENT return ( n % 31 == 0 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 1922 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 . io . * ; class GFG { static int countDecodingDP ( char digits [ ] , int n ) { int count [ ] = new int [ n + 1 ] ; count [ 0 ] = 1 ; count [ 1 ] = 1 ; if ( digits [ 0 ] == '0' ) return 0 ; for ( int i = 2 ; i <= n ; i ++ ) { count [ i ] = 0 ; if ( digits [ i - 1 ] > '0' ) count [ i ] = count [ i - 1 ] ; if ( digits [ i - 2 ] == '1' || ( digits [ i - 2 ] == '2' && digits [ i - 1 ] < '7' ) ) count [ i ] += count [ i - 2 ] ; } return count [ n ] ; } public static void main ( String [ ] args ) { char digits [ ] = { '1' , '2' , '3' , '4' } ; int n = digits . length ; System . out . println ( " Count ▁ is ▁ " + countDecodingDP ( digits , n ) ) ; } }

def countDecodingDP ( digits , n ) : NEW_LINE INDENT count = [ 0 ] * ( n + 1 ) ; NEW_LINE count [ 0 ] = 1 ; NEW_LINE count [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT count [ i ] = 0 ; NEW_LINE if ( digits [ i - 1 ] > '0' ) : NEW_LINE INDENT count [ i ] = count [ i - 1 ] ; NEW_LINE DEDENT if ( digits [ i - 2 ] == '1' or ( digits [ i - 2 ] == '2' and digits [ i - 1 ] < '7' ) ) : NEW_LINE INDENT count [ i ] += count [ i - 2 ] ; NEW_LINE DEDENT DEDENT return count [ n ] ; NEW_LINE DEDENT digits = "1234" ; NEW_LINE n = len ( digits ) ; NEW_LINE print ( " Count ▁ is " , countDecodingDP ( digits , n ) ) ; NEW_LINE


class GFG { static int maxSum ( int arr [ ] , int n , int k ) { if ( n < k ) { return - 1 ; } int res = 0 ; for ( int i = 0 ; i < k ; i ++ ) res += arr [ i ] ; int curr_sum = res ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; res = Math . max ( res , curr_sum ) ; } return res ; } static int solve ( int arr [ ] , int n , int k ) { int max_len = 0 , l = 0 , r = n , m ; while ( l <= r ) { m = ( l + r ) / 2 ; if ( maxSum ( arr , n , m ) > k ) r = m - 1 ; else { l = m + 1 ; max_len = m ; } } return max_len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = arr . length ; int k = 10 ; System . out . println ( solve ( arr , n , k ) ) ; } }

def maxSum ( arr , n , k ) : NEW_LINE INDENT if ( n < k ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT res = 0 ; NEW_LINE for i in range ( k ) : NEW_LINE INDENT res += arr [ i ] ; NEW_LINE DEDENT curr_sum = res ; NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT curr_sum += arr [ i ] - arr [ i - k ] ; NEW_LINE res = max ( res , curr_sum ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def solve ( arr , n , k ) : NEW_LINE INDENT max_len = 0 ; l = 0 ; r = n ; NEW_LINE while ( l <= r ) : NEW_LINE INDENT m = ( l + r ) // 2 ; NEW_LINE if ( maxSum ( arr , n , m ) > k ) : NEW_LINE INDENT r = m - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l = m + 1 ; NEW_LINE max_len = m ; NEW_LINE DEDENT DEDENT return max_len ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 10 ; NEW_LINE print ( solve ( arr , n , k ) ) ; NEW_LINE DEDENT


class GFG { static int calculateLeaps ( int n ) { if ( n == 0 ) return 1 ; return ( 1 << ( n - 1 ) ) ; } public static void main ( String [ ] args ) { System . out . println ( calculateLeaps ( 4 ) ) ; } }

def calculateLeaps ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return ( 1 << ( n - 1 ) ) ; NEW_LINE DEDENT print ( calculateLeaps ( 4 ) ) ; NEW_LINE


class GFG { static int MAX_INT = 1000000 ; static int solve ( int [ ] array , int N ) { int [ ] [ ] pos = new int [ 5 ] [ 10000 ] ; int [ ] pref = new int [ 5 ] ; if ( array [ 0 ] == 0 ) { pref [ 0 ] = 1 ; pos [ 0 ] [ pos [ 0 ] . length - 1 ] = 0 ; } int ans = MAX_INT ; for ( int i = 1 ; i < N ; i ++ ) { if ( array [ i ] == 0 ) { pref [ 0 ] ++ ; pos [ 0 ] [ pos [ 0 ] . length - 1 ] = i ; } else { if ( pref [ array [ i ] - 1 ] > 0 ) { pref [ array [ i ] ] ++ ; pos [ array [ i ] ] [ pos [ array [ i ] ] . length - 1 ] = i ; if ( array [ i ] == 4 ) { int end = i ; int start = i ; for ( int j = 3 ; j >= 0 ; j -- ) { int s = 0 ; int e = pos [ j ] . length - 1 ; int temp = - 1 ; while ( s <= e ) { int m = ( s + e ) / 2 ; if ( pos [ j ] [ m ] <= start ) { temp = pos [ j ] [ m ] ; s = m + 1 ; } else e = m - 1 ; } start = temp ; } ans = Math . min ( ans , end - start + 1 ) ; } } } } return ans ; } public static void main ( String [ ] args ) { int [ ] array = { 0 , 1 , 2 , 3 , 4 , 2 , 0 , 3 , 4 } ; int N = array . length ; System . out . println ( solve ( array , N ) ) ; } }

MAX_INT = 1000000 NEW_LINE def solve ( Array , N ) : NEW_LINE INDENT pos = [ [ ] for i in range ( 5 ) ] NEW_LINE pref = [ 0 for i in range ( 5 ) ] NEW_LINE if ( Array [ 0 ] == 0 ) : NEW_LINE INDENT pref [ 0 ] = 1 NEW_LINE pos [ 0 ] . append ( 0 ) NEW_LINE DEDENT ans = MAX_INT NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( Array [ i ] == 0 ) : NEW_LINE INDENT pref [ 0 ] += 1 NEW_LINE pos [ 0 ] . append ( i ) NEW_LINE DEDENT else : NEW_LINE INDENT if ( pref [ Array [ i ] - 1 ] > 0 ) : NEW_LINE INDENT pref [ Array [ i ] ] += 1 NEW_LINE pos [ Array [ i ] ] . append ( i ) NEW_LINE if ( Array [ i ] == 4 ) : NEW_LINE INDENT end = i NEW_LINE start = i NEW_LINE for j in range ( 3 , - 1 , - 1 ) : NEW_LINE INDENT s = 0 NEW_LINE e = len ( pos [ j ] ) - 1 NEW_LINE temp = - 1 NEW_LINE while ( s <= e ) : NEW_LINE INDENT m = ( s + e ) // 2 NEW_LINE if ( pos [ j ] [ m ] <= start ) : NEW_LINE INDENT temp = pos [ j ] [ m ] NEW_LINE s = m + 1 NEW_LINE DEDENT else : NEW_LINE INDENT e = m - 1 NEW_LINE DEDENT DEDENT start = temp NEW_LINE DEDENT ans = min ( ans , end - start + 1 ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT Array = [ 0 , 1 , 2 , 3 , 4 , 2 , 0 , 3 , 4 ] NEW_LINE N = len ( Array ) NEW_LINE print ( solve ( Array , N ) ) NEW_LINE


class CLG { static float hexagonside ( float a ) { if ( a < 0 ) return - 1 ; float x = a / 3 ; return x ; } public static void main ( String [ ] args ) { float a = 6 ; System . out . println ( hexagonside ( a ) ) ; } }

def hexagonside ( a ) : NEW_LINE INDENT if a < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = a // 3 NEW_LINE return x NEW_LINE DEDENT a = 6 NEW_LINE print ( hexagonside ( a ) ) NEW_LINE


import java . io . * ; class GFG { public static int averageValue ( String s ) { int sum_char = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { sum_char += ( int ) s . charAt ( i ) ; } return sum_char / s . length ( ) ; } public static void main ( String [ ] args ) { String s = " GeeksforGeeks " ; System . out . println ( averageValue ( s ) ) ; } }

def averageValue ( s ) : NEW_LINE INDENT sum_char = 0 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT sum_char += ord ( s [ i ] ) NEW_LINE DEDENT return sum_char // len ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " GeeksforGeeks " NEW_LINE print ( averageValue ( s ) ) NEW_LINE DEDENT


class GfG { static int findMinSum ( int arr [ ] , int n ) { int occ = n - 1 , sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] * ( int ) Math . pow ( 2 , occ ) ; occ -- ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 4 , 5 } ; int n = arr . length ; System . out . println ( findMinSum ( arr , n ) ) ; } }

def findMinSum ( arr , n ) : NEW_LINE INDENT occ = n - 1 NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += arr [ i ] * pow ( 2 , occ ) NEW_LINE occ -= 1 NEW_LINE DEDENT return Sum NEW_LINE DEDENT arr = [ 1 , 2 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinSum ( arr , n ) ) NEW_LINE


class GFG { static boolean isSellingPossible ( int n , int a [ ] ) { int i , c25 = 0 , c50 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 25 ) c25 ++ ; else if ( a [ i ] == 50 ) { c50 ++ ; if ( c25 == 0 ) break ; c25 -- ; } else { if ( c50 > 0 && c25 > 0 ) { c50 -- ; c25 -- ; } else if ( c25 >= 3 ) c25 -= 3 ; else break ; } } if ( i == n ) return true ; else return false ; } public static void main ( String [ ] args ) { int a [ ] = { 25 , 25 , 50 , 100 } ; int n = a . length ; if ( isSellingPossible ( n , a ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

def isSellingPossible ( n , a ) : NEW_LINE INDENT c25 = 0 ; NEW_LINE c50 = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( a [ i ] == 25 ) : NEW_LINE INDENT c25 += 1 ; NEW_LINE DEDENT elif ( a [ i ] == 50 ) : NEW_LINE INDENT c50 += 1 ; NEW_LINE if ( c25 == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT c25 -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( c50 > 0 and c25 > 0 ) : NEW_LINE INDENT c50 -= 1 ; NEW_LINE c25 -= 1 ; NEW_LINE DEDENT elif ( c25 >= 3 ) : NEW_LINE INDENT c25 -= 3 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT if ( i == n ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT a = [ 25 , 25 , 50 , 100 ] ; NEW_LINE n = len ( a ) ; NEW_LINE if ( isSellingPossible ( n , a ) ) : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int findTrailingZeros ( int n ) { int count = 0 ; for ( int i = 5 ; n / i >= 1 ; i *= 5 ) count += n / i ; return count ; } public static void main ( String [ ] args ) { int n = 100 ; System . out . println ( " Count ▁ of ▁ trailing ▁ 0s ▁ in ▁ " + n + " ! ▁ is ▁ " + findTrailingZeros ( n ) ) ; } }

def findTrailingZeros ( n ) : NEW_LINE INDENT count = 0 NEW_LINE i = 5 NEW_LINE while ( n / i >= 1 ) : NEW_LINE INDENT count += int ( n / i ) NEW_LINE i *= 5 NEW_LINE DEDENT return int ( count ) NEW_LINE DEDENT n = 100 NEW_LINE print ( " Count ▁ of ▁ trailing ▁ 0s ▁ " + " in ▁ 100 ! ▁ is " , findTrailingZeros ( n ) ) NEW_LINE


class GFG { static int findMinNumber ( int n ) { int count = 0 , ans = 1 ; while ( n % 2 == 0 ) { count ++ ; n /= 2 ; } if ( count % 2 == 1 ) ans *= 2 ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { count = 0 ; while ( n % i == 0 ) { count ++ ; n /= i ; } if ( count % 2 == 1 ) ans *= i ; } if ( n > 2 ) ans *= n ; return ans ; } public static void main ( String [ ] args ) { int n = 72 ; System . out . println ( findMinNumber ( n ) ) ; } }

import math NEW_LINE def findMinNumber ( n ) : NEW_LINE INDENT count = 0 NEW_LINE ans = 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT count += 1 NEW_LINE n //= 2 NEW_LINE DEDENT if count % 2 is not 0 : NEW_LINE INDENT ans *= 2 NEW_LINE DEDENT for i in range ( 3 , ( int ) ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT count = 0 NEW_LINE while n % i == 0 : NEW_LINE INDENT count += 1 NEW_LINE n //= i NEW_LINE DEDENT if count % 2 is not 0 : NEW_LINE INDENT ans *= i NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT ans *= n NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 72 NEW_LINE print ( findMinNumber ( n ) ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int check ( int n ) { int sum = 0 ; while ( n != 0 ) { sum += n % 10 ; n = n / 10 ; } if ( sum % 7 == 0 ) return 1 ; else return 0 ; } public static void main ( String args [ ] ) { int n = 25 ; String s = ( check ( n ) == 1 ) ? " YES " : " NO " ; System . out . println ( s ) ; } }

def check ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE while n != 0 : NEW_LINE INDENT sum += n % 10 NEW_LINE n = n // 10 NEW_LINE DEDENT if sum % 7 == 0 : 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 = 25 NEW_LINE print ( ( " YES " ) if check ( n ) == 1 else print ( " NO " ) ) NEW_LINE DEDENT


class GFG { static int N = 100010 ; static int [ ] id = new int [ 100010 ] ; static int [ ] sz = new int [ 100010 ] ; static int Root ( int idx ) { int i = idx ; while ( i != id [ i ] ) { id [ i ] = id [ id [ i ] ] ; i = id [ i ] ; } return i ; } static void Union ( int a , int b ) { int i = Root ( a ) ; int j = Root ( b ) ; if ( i != j ) { if ( sz [ i ] >= sz [ j ] ) { id [ j ] = i ; sz [ i ] += sz [ j ] ; sz [ j ] = 0 ; } else { id [ i ] = j ; sz [ j ] += sz [ i ] ; sz [ i ] = 0 ; } } } static int minChange ( int n , int a [ ] , int b [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { id [ i ] = i ; sz [ i ] = 1 ; } for ( int i = 0 ; i < n ; ++ i ) if ( Root ( a [ i ] ) != Root ( b [ i ] ) ) Union ( a [ i ] , b [ i ] ) ; int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) if ( id [ i ] == i ) ans += ( sz [ i ] - 1 ) ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 1 , 1 , 3 , 5 } , b [ ] = { 1 , 2 , 2 , 4 , 5 } ; int n = a . length ; System . out . println ( minChange ( n , a , b ) ) ; } }

N = 100010 NEW_LINE ID = [ 0 for i in range ( N ) ] NEW_LINE sz = [ 0 for i in range ( N ) ] NEW_LINE def Root ( idx ) : NEW_LINE INDENT i = idx NEW_LINE while i != ID [ i ] : NEW_LINE INDENT ID [ i ] , i = ID [ ID [ i ] ] , ID [ i ] NEW_LINE DEDENT return i NEW_LINE DEDENT def Union ( a , b ) : NEW_LINE INDENT i , j = Root ( a ) , Root ( b ) NEW_LINE if i != j : NEW_LINE INDENT if sz [ i ] >= sz [ j ] : NEW_LINE INDENT ID [ j ] = i NEW_LINE sz [ i ] += sz [ j ] NEW_LINE sz [ j ] = 0 NEW_LINE DEDENT else : NEW_LINE INDENT ID [ i ] = j NEW_LINE sz [ j ] += sz [ i ] NEW_LINE sz [ i ] = 0 NEW_LINE DEDENT DEDENT DEDENT def minChange ( n , a , b ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT ID [ i ] = i NEW_LINE sz [ i ] = 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if Root ( a [ i ] ) != Root ( b [ i ] ) : NEW_LINE INDENT Union ( a [ i ] , b [ i ] ) NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ID [ i ] == i : NEW_LINE INDENT ans += ( sz [ i ] - 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT a = [ 2 , 1 , 1 , 3 , 5 ] NEW_LINE b = [ 1 , 2 , 2 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE print ( minChange ( n , a , b ) ) NEW_LINE


public class Diagonals { static int numberOfDiagonals ( int n ) { return n * ( n - 3 ) / 2 ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( n + " ▁ sided ▁ convex ▁ polygon ▁ have ▁ " ) ; System . out . println ( numberOfDiagonals ( n ) + " ▁ diagonals " ) ; } }

def numberOfDiagonals ( n ) : NEW_LINE INDENT return n * ( n - 3 ) / 2 NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT n = 5 NEW_LINE print ( n , " ▁ sided ▁ convex ▁ polygon ▁ have ▁ " ) NEW_LINE print ( numberOfDiagonals ( n ) , " ▁ diagonals " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static void reachTheNums ( int nums [ ] , int k , int d1 , int d2 , int n ) { int i ; int ans [ ] = new int [ n ] ; int gcd = __gcd ( d1 , d2 ) ; for ( i = 0 ; i < n ; i ++ ) { int x = nums [ i ] - k ; if ( x % gcd == 0 ) ans [ i ] = 1 ; else ans [ i ] = 0 ; } for ( i = 0 ; i < n ; i ++ ) System . out . print ( ans [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int nums [ ] = { 9 , 4 } ; int n = nums . length ; int k = 8 ; int d1 = 3 , d2 = 2 ; reachTheNums ( nums , k , d1 , d2 , n ) ; } }

import math as mt NEW_LINE def reachTheNums ( nums , k , d1 , d2 , n ) : NEW_LINE INDENT ans = [ 0 for i in range ( n ) ] NEW_LINE gcd = mt . gcd ( d1 , d2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT x = nums [ i ] - k NEW_LINE if ( x % gcd == 0 ) : NEW_LINE INDENT ans [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT ans [ i ] = 0 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT nums = [ 9 , 4 ] NEW_LINE n = len ( nums ) NEW_LINE k = 8 NEW_LINE d1 , d2 = 3 , 2 NEW_LINE reachTheNums ( nums , k , d1 , d2 , n ) NEW_LINE