import java . io . * ; class GFG { static int findSum ( int n ) { int ans = 0 , temp = 0 , num ; for ( int i = 1 ; i <= n && temp < n ; i ++ ) { temp = i - 1 ; num = 1 ; while ( temp < n ) { if ( temp + i <= n ) ans += ( i * num ) ; else ans += ( ( n - temp ) * num ) ; temp += i ; num ++ ; } } return ans ; } public static void main ( String [ ] args ) { int N = 2 ; System . out . println ( findSum ( N ) ) ; } }

def findSum ( n ) : NEW_LINE INDENT ans = 0 ; temp = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if temp < n : NEW_LINE INDENT temp = i - 1 NEW_LINE num = 1 NEW_LINE while temp < n : NEW_LINE INDENT if temp + i <= n : NEW_LINE INDENT ans += i * num NEW_LINE DEDENT else : NEW_LINE INDENT ans += ( n - temp ) * num NEW_LINE DEDENT temp += i NEW_LINE num += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT N = 2 NEW_LINE print ( findSum ( N ) ) NEW_LINE


import java . util . * ; class GFG { static int maxSubArraySum ( int arr [ ] , int size ) { int max_so_far = arr [ 0 ] ; int curr_max = arr [ 0 ] ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = Math . max ( arr [ i ] , curr_max + arr [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } static int lenOfLongSubarrWithGivenSum ( int arr [ ] , int n , int k ) { HashMap < Integer , Integer > um = new HashMap < Integer , Integer > ( ) ; int sum = 0 , maxLen = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == k ) maxLen = i + 1 ; if ( um . containsKey ( sum ) ) um . put ( sum , i ) ; if ( um . containsKey ( sum - k ) ) { if ( maxLen < ( i - um . get ( sum - k ) ) ) maxLen = i - um . get ( sum - k ) ; } } return maxLen ; } static int lenLongSubarrWithMaxSum ( int arr [ ] , int n ) { int maxSum = maxSubArraySum ( arr , n ) ; return lenOfLongSubarrWithGivenSum ( arr , n , maxSum ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , - 2 , - 1 , 3 , - 4 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ longest ▁ subarray ▁ " + " having ▁ maximum ▁ sum ▁ = ▁ " + lenLongSubarrWithMaxSum ( arr , n ) ) ; } }

def maxSubArraySum ( arr , size ) : NEW_LINE INDENT max_so_far = arr [ 0 ] NEW_LINE curr_max = arr [ 0 ] NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT curr_max = max ( arr [ i ] , curr_max + arr [ i ] ) NEW_LINE max_so_far = max ( max_so_far , curr_max ) NEW_LINE DEDENT return max_so_far NEW_LINE DEDENT def lenOfLongSubarrWithGivenSum ( arr , n , k ) : NEW_LINE INDENT um = dict ( ) NEW_LINE Sum , maxLen = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE if ( Sum == k ) : NEW_LINE INDENT maxLen = i + 1 NEW_LINE DEDENT if ( Sum not in um . keys ( ) ) : NEW_LINE INDENT um [ Sum ] = i NEW_LINE DEDENT if ( Sum in um . keys ( ) ) : NEW_LINE INDENT if ( ( Sum - k ) in um . keys ( ) and maxLen < ( i - um [ Sum - k ] ) ) : NEW_LINE INDENT maxLen = i - um [ Sum - k ] NEW_LINE DEDENT DEDENT DEDENT return maxLen NEW_LINE DEDENT def lenLongSubarrWithMaxSum ( arr , n ) : NEW_LINE INDENT maxSum = maxSubArraySum ( arr , n ) NEW_LINE return lenOfLongSubarrWithGivenSum ( arr , n , maxSum ) NEW_LINE DEDENT arr = [ 5 , - 2 , - 1 , 3 , - 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ of ▁ longest ▁ subarray ▁ having ▁ maximum ▁ sum ▁ = ▁ " , lenLongSubarrWithMaxSum ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { public static int squareFree ( int n ) { int cnt = 0 ; for ( int i = 1 ; ; i ++ ) { boolean isSqFree = true ; for ( int j = 2 ; j * j <= i ; j ++ ) { if ( i % ( j * j ) == 0 ) { isSqFree = false ; break ; } } if ( isSqFree == true ) { cnt ++ ; if ( cnt == n ) return i ; } } } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( " " + squareFree ( n ) ) ; } }

def squareFree ( n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE i = 1 ; NEW_LINE while ( True ) : NEW_LINE INDENT isSqFree = True ; NEW_LINE j = 2 ; NEW_LINE while ( j * j <= i ) : NEW_LINE INDENT if ( i % ( j * j ) == 0 ) : NEW_LINE INDENT isSqFree = False ; NEW_LINE break ; NEW_LINE DEDENT j += 1 ; NEW_LINE DEDENT if ( isSqFree == True ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE if ( cnt == n ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT n = 10 ; NEW_LINE print ( squareFree ( n ) ) ; NEW_LINE


class GFG { static int MAX_CHAR = 256 ; static int fact ( int n ) { return ( n <= 1 ) ? 1 : n * fact ( n - 1 ) ; } static void populateAndIncreaseCount ( int [ ] count , char [ ] str ) { int i ; for ( i = 0 ; i < str . length ; ++ i ) ++ count [ str [ i ] ] ; for ( i = 1 ; i < MAX_CHAR ; ++ i ) count [ i ] += count [ i - 1 ] ; } static void updatecount ( int [ ] count , char ch ) { int i ; for ( i = ch ; i < MAX_CHAR ; ++ i ) -- count [ i ] ; } static int findRank ( char [ ] str ) { int len = str . length ; int mul = fact ( len ) ; int rank = 1 , i ; int count [ ] = new int [ MAX_CHAR ] ; populateAndIncreaseCount ( count , str ) ; for ( i = 0 ; i < len ; ++ i ) { mul /= len - i ; rank += count [ str [ i ] - 1 ] * mul ; updatecount ( count , str [ i ] ) ; } return rank ; } public static void main ( String args [ ] ) { char str [ ] = " string " . toCharArray ( ) ; System . out . println ( findRank ( str ) ) ; } }

MAX_CHAR = 256 ; NEW_LINE count = [ 0 ] * ( MAX_CHAR + 1 ) ; NEW_LINE def fact ( n ) : NEW_LINE INDENT return 1 if ( n <= 1 ) else ( n * fact ( n - 1 ) ) ; NEW_LINE DEDENT def populateAndIncreaseCount ( str ) : NEW_LINE INDENT for i in range ( len ( str ) ) : NEW_LINE INDENT count [ ord ( str [ i ] ) ] += 1 ; NEW_LINE DEDENT for i in range ( 1 , MAX_CHAR ) : NEW_LINE INDENT count [ i ] += count [ i - 1 ] ; NEW_LINE DEDENT DEDENT def updatecount ( ch ) : NEW_LINE INDENT for i in range ( ord ( ch ) , MAX_CHAR ) : NEW_LINE INDENT count [ i ] -= 1 ; NEW_LINE DEDENT DEDENT def findRank ( str ) : NEW_LINE INDENT len1 = len ( str ) ; NEW_LINE mul = fact ( len1 ) ; NEW_LINE rank = 1 ; NEW_LINE populateAndIncreaseCount ( str ) ; NEW_LINE for i in range ( len1 ) : NEW_LINE INDENT mul = mul // ( len1 - i ) ; NEW_LINE rank += count [ ord ( str [ i ] ) - 1 ] * mul ; NEW_LINE updatecount ( str [ i ] ) ; NEW_LINE DEDENT return rank ; NEW_LINE DEDENT str = " string " ; NEW_LINE print ( findRank ( str ) ) ; NEW_LINE


class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; int digitSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) digitSum += ( str . charAt ( i ) - '0' ) ; return ( digitSum % 9 == 0 ) ; } public static void main ( String [ ] args ) { String str = "99333" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def check ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE digitSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT digitSum = digitSum + ( int ) ( st [ i ] ) NEW_LINE DEDENT return ( digitSum % 9 == 0 ) NEW_LINE DEDENT st = "99333" NEW_LINE if ( check ( st ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; public class GeeksforGeeks { public static void StarPattern ( int height ) { for ( int i = 0 ; i < height ; i ++ ) { for ( int j = height - 1 ; j > i ; j -- ) { System . out . print ( " * " ) ; } boolean printChar = false ; for ( int j = 0 ; j < ( ( i * 2 ) + 1 ) ; j ++ ) { if ( printChar ) { System . out . print ( " * " ) ; } else { System . out . print ( i + 1 ) ; } printChar = ! printChar ; } for ( int j = height - 1 ; j > i ; j -- ) { System . out . print ( " * " ) ; } System . out . println ( ) ; } } public static void main ( String args [ ] ) { int height = 7 ; StarPattern ( height ) ; } }

def StarPattern ( height ) : NEW_LINE INDENT for i in range ( height ) : NEW_LINE INDENT for j in range ( height - 1 , i , - 1 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT printChar = False NEW_LINE for j in range ( ( ( i * 2 ) + 1 ) ) : NEW_LINE INDENT if ( printChar ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( i + 1 , end = " " ) NEW_LINE DEDENT printChar = not printChar NEW_LINE DEDENT for j in range ( height - 1 , i , - 1 ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT height = 7 NEW_LINE StarPattern ( height ) NEW_LINE


import java . io . * ; class GFG { static void findValues ( int a , int b ) { if ( ( a - b ) % 2 == 1 ) { System . out . println ( " - 1" ) ; return ; } System . out . println ( ( ( a - b ) / 2 ) + " ▁ " + ( ( a + b ) / 2 ) ) ; } public static void main ( String [ ] args ) { int a = 12 , b = 8 ; findValues ( a , b ) ; } }

def findValues ( a , b ) : NEW_LINE INDENT if ( ( a - b ) % 2 == 1 ) : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE return ; NEW_LINE DEDENT print ( ( a - b ) // 2 , ( a + b ) // 2 ) ; NEW_LINE DEDENT a = 12 ; b = 8 ; NEW_LINE findValues ( a , b ) ; NEW_LINE


import java . io . * ; class GFG { static boolean isDvisibleBy12 ( String num ) { if ( num . length ( ) >= 3 ) { int d1 = ( int ) num . charAt ( num . length ( ) - 1 ) ; if ( d1 % 2 != 0 ) return false ; int d2 = ( int ) num . charAt ( num . length ( ) - 2 ) ; int sum = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) sum += num . charAt ( i ) ; return ( sum % 3 == 0 && ( d2 * 10 + d1 ) % 4 == 0 ) ; } else { int number = Integer . parseInt ( num ) ; return ( number % 12 == 0 ) ; } } public static void main ( String [ ] args ) { String num = "12244824607284961224" ; if ( isDvisibleBy12 ( num ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

import math NEW_LINE def isDvisibleBy12 ( num ) : NEW_LINE INDENT if ( len ( num ) >= 3 ) : NEW_LINE INDENT d1 = int ( num [ len ( num ) - 1 ] ) ; NEW_LINE if ( d1 % 2 != 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT d2 = int ( num [ len ( num ) - 2 ] ) NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , len ( num ) ) : NEW_LINE INDENT sum += int ( num [ i ] ) NEW_LINE DEDENT return ( sum % 3 == 0 and ( d2 * 10 + d1 ) % 4 == 0 ) NEW_LINE DEDENT else : NEW_LINE INDENT number = int ( num ) NEW_LINE return ( number % 12 == 0 ) NEW_LINE DEDENT DEDENT num = "12244824607284961224" NEW_LINE if ( isDvisibleBy12 ( num ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void count_ ( int count [ ] , int e ) { boolean hash [ ] = new boolean [ 10 ] ; while ( e > 0 ) { if ( hash [ e % 10 ] == false ) count [ e % 10 ] ++ ; hash [ e % 10 ] = true ; e /= 10 ; } } static void find_subsequence ( int arr [ ] , int n ) { int count [ ] = new int [ 10 ] ; for ( int i = 0 ; i < n ; i ++ ) { count_ ( count , arr [ i ] ) ; } int longest = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) longest = Math . max ( count [ i ] , longest ) ; System . out . print ( longest ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 11 , 12 , 23 , 74 , 13 } ; int n = arr . length ; find_subsequence ( arr , n ) ; } }

def count_ ( count , e ) : NEW_LINE INDENT hash = [ False ] * 10 NEW_LINE while ( e > 0 ) : NEW_LINE INDENT if ( hash [ e % 10 ] == False ) : NEW_LINE INDENT count [ e % 10 ] += 1 NEW_LINE DEDENT hash [ e % 10 ] = True NEW_LINE e //= 10 NEW_LINE DEDENT DEDENT def find_subsequence ( arr , n ) : NEW_LINE INDENT count = [ 0 ] * 10 NEW_LINE for i in range ( n ) : NEW_LINE INDENT count_ ( count , arr [ i ] ) NEW_LINE DEDENT longest = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT longest = max ( count [ i ] , longest ) NEW_LINE DEDENT print ( longest ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 11 , 12 , 23 , 74 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE find_subsequence ( arr , n ) NEW_LINE DEDENT


class GFG { static String findLongestPalindrome ( String str ) { int count [ ] = new int [ 256 ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { count [ str . charAt ( i ) ] ++ ; } String beg = " " , mid = " " , end = " " ; for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { if ( count [ ch ] % 2 == 1 ) { mid = String . valueOf ( ch ) ; count [ ch -- ] -- ; } else { for ( int i = 0 ; i < count [ ch ] / 2 ; i ++ ) { beg += ch ; } } } end = beg ; end = reverse ( end ) ; return beg + mid + end ; } static String reverse ( String str ) { String ans = " " ; char [ ] try1 = str . toCharArray ( ) ; for ( int i = try1 . length - 1 ; i >= 0 ; i -- ) { ans += try1 [ i ] ; } return ans ; } public static void main ( String [ ] args ) { String str = " abbaccd " ; System . out . println ( findLongestPalindrome ( str ) ) ; } }

def findLongestPalindrome ( strr ) : NEW_LINE INDENT count = [ 0 ] * 256 NEW_LINE for i in range ( len ( strr ) ) : NEW_LINE INDENT count [ ord ( strr [ i ] ) ] += 1 NEW_LINE DEDENT beg = " " NEW_LINE mid = " " NEW_LINE end = " " NEW_LINE ch = ord ( ' a ' ) NEW_LINE while ch <= ord ( ' z ' ) : NEW_LINE INDENT if ( count [ ch ] & 1 ) : NEW_LINE INDENT mid = ch NEW_LINE count [ ch ] -= 1 NEW_LINE ch -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( count [ ch ] // 2 ) : NEW_LINE INDENT beg += chr ( ch ) NEW_LINE DEDENT DEDENT ch += 1 NEW_LINE DEDENT end = beg NEW_LINE end = end [ : : - 1 ] NEW_LINE return beg + chr ( mid ) + end NEW_LINE DEDENT strr = " abbaccd " NEW_LINE print ( findLongestPalindrome ( strr ) ) NEW_LINE


import java . io . * ; class GFG { static int totalSumDivisibleByNum ( int n , int number ) { int firstnum = ( int ) Math . pow ( 10 , n - 1 ) ; int lastnum = ( int ) Math . pow ( 10 , n ) ; int sum = 0 ; for ( int i = firstnum ; i < lastnum ; i ++ ) if ( i % number == 0 ) sum += i ; return sum ; } public static void main ( String [ ] args ) { int n = 3 , num = 7 ; System . out . println ( totalSumDivisibleByNum ( n , num ) ) ; } }

def totalSumDivisibleByNum ( n , number ) : NEW_LINE INDENT firstnum = pow ( 10 , n - 1 ) NEW_LINE lastnum = pow ( 10 , n ) NEW_LINE sum = 0 NEW_LINE for i in range ( firstnum , lastnum ) : NEW_LINE INDENT if ( i % number == 0 ) : NEW_LINE INDENT sum += i NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT n = 3 ; num = 7 NEW_LINE print ( totalSumDivisibleByNum ( n , num ) ) NEW_LINE


class GFG { static int N = 3 ; static int maxPathSum ( int tri [ ] [ ] ) { int ans = 0 ; for ( int i = N - 2 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < N - i ; j ++ ) { if ( j - 1 >= 0 ) tri [ i ] [ j ] += Math . max ( tri [ i + 1 ] [ j ] , tri [ i + 1 ] [ j - 1 ] ) ; else tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; ans = Math . max ( ans , tri [ i ] [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int tri [ ] [ ] = { { 1 , 5 , 3 } , { 4 , 8 , 0 } , { 1 , 0 , 0 } } ; System . out . println ( maxPathSum ( tri ) ) ; } }

N = 3 NEW_LINE def maxPathSum ( tri ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( N - 2 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( 0 , N - i ) : NEW_LINE INDENT if ( j - 1 >= 0 ) : NEW_LINE INDENT tri [ i ] [ j ] += max ( tri [ i + 1 ] [ j ] , tri [ i + 1 ] [ j - 1 ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT tri [ i ] [ j ] += tri [ i + 1 ] [ j ] ; NEW_LINE DEDENT ans = max ( ans , tri [ i ] [ j ] ) ; NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT tri = [ [ 1 , 5 , 3 ] , [ 4 , 8 , 0 ] , [ 1 , 0 , 0 ] ] NEW_LINE print ( maxPathSum ( tri ) ) NEW_LINE


class GFG { static long squareDigitSum ( String s ) { long lengthN = s . length ( ) ; long result = ( lengthN / 9 ) * 81 + ( long ) Math . pow ( ( lengthN % 9 ) , 2 ) ; return result ; } public static void main ( String [ ] args ) { String s = "1111" ; System . out . println ( squareDigitSum ( s ) ) ; } }

def squareDigitSum ( num ) : NEW_LINE INDENT lengthN = len ( num ) NEW_LINE result = ( lengthN // 9 ) * 81 + ( lengthN % 9 ) ** 2 NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = "1111" NEW_LINE print ( squareDigitSum ( N ) ) NEW_LINE DEDENT


class GFG { static String even_or_odd ( String N ) { int len = N . length ( ) ; if ( N . charAt ( len - 1 ) == '0' || N . charAt ( len - 1 ) == '2' || N . charAt ( len - 1 ) == '4' || N . charAt ( len - 1 ) == '6' || N . charAt ( len - 1 ) == '8' || N . charAt ( len - 1 ) == ' A ' || N . charAt ( len - 1 ) == ' C ' || N . charAt ( len - 1 ) == ' E ' ) return ( " Even " ) ; else return ( " Odd " ) ; } public static void main ( String [ ] args ) { String N = " AB3454D " ; System . out . print ( even_or_odd ( N ) ) ; } }

def even_or_odd ( N ) : NEW_LINE INDENT l = len ( N ) NEW_LINE if ( N [ l - 1 ] == '0' or N [ l - 1 ] == '2' or N [ l - 1 ] == '4' or N [ l - 1 ] == '6' or N [ l - 1 ] == '8' or N [ l - 1 ] == ' A ' or N [ l - 1 ] == ' C ' or N [ l - 1 ] == ' E ' ) : NEW_LINE INDENT return ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( " Odd " ) NEW_LINE DEDENT DEDENT N = " AB3454D " NEW_LINE print ( even_or_odd ( N ) ) NEW_LINE


import java . util . Arrays ; import java . util . Collections ; class GFG { static int MAX_SIZE = 10 ; static void sortByRow ( Integer mat [ ] [ ] , int n , boolean ascending ) { for ( int i = 0 ; i < n ; i ++ ) { if ( ascending ) Arrays . sort ( mat [ i ] ) ; else Arrays . sort ( mat [ i ] , Collections . reverseOrder ( ) ) ; } } static void transpose ( Integer mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) { int temp = mat [ i ] [ j ] ; mat [ i ] [ j ] = mat [ j ] [ i ] ; mat [ j ] [ i ] = temp ; } } static void sortMatRowAndColWise ( Integer mat [ ] [ ] , int n ) { sortByRow ( mat , n , true ) ; transpose ( mat , n ) ; sortByRow ( mat , n , false ) ; transpose ( mat , n ) ; } static void printMat ( Integer mat [ ] [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 3 ; Integer mat [ ] [ ] = { { 3 , 2 , 1 } , { 9 , 8 , 7 } , { 6 , 5 , 4 } } ; System . out . print ( " Original ▁ Matrix : \n " ) ; printMat ( mat , n ) ; sortMatRowAndColWise ( mat , n ) ; System . out . print ( " \n Matrix ▁ After ▁ Sorting : \n " ) ; printMat ( mat , n ) ; } }

MAX_SIZE = 10 NEW_LINE def sortByRow ( mat , n , ascending ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( ascending ) : NEW_LINE INDENT mat [ i ] . sort ( ) NEW_LINE DEDENT else : NEW_LINE INDENT mat [ i ] . sort ( reverse = True ) NEW_LINE DEDENT DEDENT DEDENT def transpose ( mat , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT temp = mat [ i ] [ j ] NEW_LINE mat [ i ] [ j ] = mat [ j ] [ i ] NEW_LINE mat [ j ] [ i ] = temp NEW_LINE DEDENT DEDENT DEDENT def sortMatRowAndColWise ( mat , n ) : NEW_LINE INDENT sortByRow ( mat , n , True ) NEW_LINE transpose ( mat , n ) NEW_LINE sortByRow ( mat , n , False ) NEW_LINE transpose ( mat , n ) NEW_LINE DEDENT def printMat ( mat , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( mat [ i ] [ j ] , " ▁ " , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 3 NEW_LINE mat = [ [ 3 , 2 , 1 ] , [ 9 , 8 , 7 ] , [ 6 , 5 , 4 ] ] NEW_LINE print ( " Original ▁ Matrix : " ) NEW_LINE printMat ( mat , n ) NEW_LINE sortMatRowAndColWise ( mat , n ) NEW_LINE print ( " Matrix ▁ After ▁ Sorting : " ) NEW_LINE printMat ( mat , n ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { public static int minNumber ( int a [ ] , int n , int x ) { int l = 0 , h = 0 , e = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == x ) e ++ ; else if ( a [ i ] > x ) h ++ ; else if ( a [ i ] < x ) l ++ ; } int ans = 0 ; if ( l > h ) ans = l - h ; else if ( l < h ) ans = h - l - 1 ; return ans + 1 - e ; } public static void main ( String [ ] args ) { int x = 10 ; int a [ ] = { 10 , 20 , 30 } ; int n = a . length ; System . out . println ( minNumber ( a , n , x ) ) ; } }

def minNumber ( a , n , x ) : NEW_LINE INDENT l = 0 NEW_LINE h = 0 NEW_LINE e = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] == x : NEW_LINE INDENT e += 1 NEW_LINE DEDENT elif a [ i ] > x : NEW_LINE INDENT h += 1 NEW_LINE DEDENT elif a [ i ] < x : NEW_LINE INDENT l += 1 NEW_LINE DEDENT DEDENT ans = 0 ; NEW_LINE if l > h : NEW_LINE INDENT ans = l - h NEW_LINE DEDENT elif l < h : NEW_LINE INDENT ans = h - l - 1 ; NEW_LINE DEDENT return ans + 1 - e NEW_LINE DEDENT x = 10 NEW_LINE a = [ 10 , 20 , 30 ] NEW_LINE n = len ( a ) NEW_LINE print ( minNumber ( a , n , x ) ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GfG { public static int findremainder ( int arr [ ] , int len , int n ) { int mul = 1 ; for ( int i = 0 ; i < len ; i ++ ) mul = ( mul * ( arr [ i ] % n ) ) % n ; return mul % n ; } public static void main ( String argc [ ] ) { int [ ] arr = new int [ ] { 100 , 10 , 5 , 25 , 35 , 14 } ; int len = 6 ; int n = 11 ; System . out . println ( findremainder ( arr , len , n ) ) ; } }

def findremainder ( arr , lens , n ) : NEW_LINE INDENT mul = 1 NEW_LINE for i in range ( lens ) : NEW_LINE INDENT mul = ( mul * ( arr [ i ] % n ) ) % n NEW_LINE DEDENT return mul % n NEW_LINE DEDENT arr = [ 100 , 10 , 5 , 25 , 35 , 14 ] NEW_LINE lens = len ( arr ) NEW_LINE n = 11 NEW_LINE print ( findremainder ( arr , lens , n ) ) NEW_LINE


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

def hexDiagonal ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT d = 1.73 * a ; NEW_LINE return d ; NEW_LINE DEDENT a = 9 ; NEW_LINE print ( hexDiagonal ( a ) ) ; NEW_LINE


import java . io . * ; class GFG { static int MAX = 100 ; static void middleProduct ( int mat [ ] [ ] , int n ) { int row_prod = 1 , col_prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) { row_prod *= mat [ n / 2 ] [ i ] ; col_prod *= mat [ i ] [ n / 2 ] ; } System . out . print ( " Product ▁ of ▁ middle ▁ row ▁ = ▁ " + row_prod ) ; System . out . print ( " Product ▁ of ▁ middle ▁ column ▁ = ▁ " + col_prod ) ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 2 , 1 , 7 } , { 3 , 7 , 2 } , { 5 , 4 , 9 } } ; middleProduct ( mat , 3 ) ; } }

MAX = 100 NEW_LINE def middleProduct ( mat , n ) : NEW_LINE INDENT row_prod = 1 NEW_LINE col_prod = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT row_prod *= mat [ n // 2 ] [ i ] NEW_LINE col_prod *= mat [ i ] [ n // 2 ] NEW_LINE DEDENT print ( " Product ▁ of ▁ middle ▁ row ▁ = ▁ " , row_prod ) NEW_LINE print ( " Product ▁ of ▁ middle ▁ column ▁ = ▁ " , col_prod ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 2 , 1 , 7 ] , [ 3 , 7 , 2 ] , [ 5 , 4 , 9 ] ] NEW_LINE middleProduct ( mat , 3 ) NEW_LINE DEDENT


class GFG { static int oddFib ( int n ) { n = ( 3 * n + 1 ) / 2 ; int a = - 1 , b = 1 , c = 0 , i ; for ( i = 1 ; i <= n ; i ++ ) { c = a + b ; a = b ; b = c ; } return c ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( oddFib ( n ) ) ; } }

def oddFib ( n ) : NEW_LINE INDENT n = ( 3 * n + 1 ) // 2 NEW_LINE a = - 1 NEW_LINE b = 1 NEW_LINE c = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE DEDENT return c NEW_LINE DEDENT n = 4 NEW_LINE print ( oddFib ( n ) ) NEW_LINE


import java . io . * ; class GFG { static int m = 6 , n = 4 ; static int linearCheck ( int ar [ ] [ ] , int arr [ ] ) { for ( int i = 0 ; i < m ; i ++ ) { boolean matched = true ; for ( int j = 0 ; j < n ; j ++ ) { if ( ar [ i ] [ j ] != arr [ j ] ) { matched = false ; break ; } } if ( matched ) return i + 1 ; } return - 1 ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 0 , 0 , 1 , 0 } , { 10 , 9 , 22 , 23 } , { 40 , 40 , 40 , 40 } , { 43 , 44 , 55 , 68 } , { 81 , 73 , 100 , 132 } , { 100 , 75 , 125 , 133 } } ; int row [ ] = { 10 , 9 , 22 , 23 } ; System . out . println ( linearCheck ( mat , row ) ) ; } }

m , n = 6 , 4 ; NEW_LINE def linearCheck ( ar , arr ) : NEW_LINE INDENT for i in range ( m ) : NEW_LINE INDENT matched = True ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( ar [ i ] [ j ] != arr [ j ] ) : NEW_LINE INDENT matched = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( matched ) : NEW_LINE INDENT return i + 1 ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 0 , 0 , 1 , 0 ] , [ 10 , 9 , 22 , 23 ] , [ 40 , 40 , 40 , 40 ] , [ 43 , 44 , 55 , 68 ] , [ 81 , 73 , 100 , 132 ] , [ 100 , 75 , 125 , 133 ] ] ; NEW_LINE row = [ 10 , 9 , 22 , 23 ] ; NEW_LINE print ( linearCheck ( mat , row ) ) ; NEW_LINE DEDENT


class GfG { static int largestCoprime ( int N ) { if ( N == 6 ) return 1 ; else if ( N % 4 == 0 ) return ( N / 2 ) - 1 ; else if ( N % 2 == 0 ) return ( N / 2 ) - 2 ; else return ( ( N - 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int n = 50 ; System . out . println ( largestCoprime ( n ) ) ; } }

def largestCoprime ( N ) : NEW_LINE INDENT if N == 6 : NEW_LINE INDENT return 1 NEW_LINE DEDENT elif N % 4 == 0 : NEW_LINE INDENT return N // 2 - 1 NEW_LINE DEDENT elif N % 2 == 0 : NEW_LINE INDENT return N // 2 - 2 NEW_LINE DEDENT else : NEW_LINE INDENT return ( N - 1 ) // 2 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 50 NEW_LINE print ( largestCoprime ( n ) ) NEW_LINE DEDENT


class GFG { static int fastmod ( int base , int exp , int mod ) { if ( exp == 0 ) return 1 ; else if ( exp % 2 == 0 ) { int ans = fastmod ( base , exp / 2 , mod ) ; return ( ans % mod * ans % mod ) % mod ; } else return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; } static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int findPowerSum ( int n , int ar [ ] ) { final int mod = ( int ) 1e9 + 7 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int base = countSetBits ( ar [ i ] ) ; int exp = ar [ i ] ; sum += fastmod ( base , exp , mod ) ; sum %= mod ; } return sum ; } public static void main ( String [ ] args ) { int n = 3 ; int ar [ ] = { 1 , 2 , 3 } ; System . out . println ( findPowerSum ( n , ar ) ) ; } }

def fastmod ( base , exp , mod ) : NEW_LINE INDENT if ( exp == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT elif ( exp % 2 == 0 ) : NEW_LINE INDENT ans = fastmod ( base , exp / 2 , mod ) ; NEW_LINE return ( ans % mod * ans % mod ) % mod ; NEW_LINE DEDENT else : NEW_LINE INDENT return ( fastmod ( base , exp - 1 , mod ) % mod * base % mod ) % mod ; NEW_LINE DEDENT DEDENT def findPowerSum ( n , ar ) : NEW_LINE INDENT mod = int ( 1e9 ) + 7 ; NEW_LINE sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT base = bin ( ar [ i ] ) . count ( '1' ) ; NEW_LINE exp = ar [ i ] ; NEW_LINE sum += fastmod ( base , exp , mod ) ; NEW_LINE sum %= mod ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 ; NEW_LINE ar = [ 1 , 2 , 3 ] ; NEW_LINE print ( findPowerSum ( n , ar ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean isPresent ( int arr [ ] , int low , int high , int value ) { while ( low <= high ) { int mid = ( low + high ) / 2 ; if ( arr [ mid ] == value ) return true ; else if ( arr [ mid ] > value ) high = mid - 1 ; else low = mid + 1 ; } return false ; } static int countPairs ( int arr1 [ ] , int arr2 [ ] , int m , int n , int x ) { int count = 0 ; for ( int i = 0 ; i < m ; i ++ ) { int value = x - arr1 [ i ] ; if ( isPresent ( arr2 , 0 , n - 1 , value ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 3 , 5 , 7 } ; int arr2 [ ] = { 2 , 3 , 5 , 8 } ; int m = arr1 . length ; int n = arr2 . length ; int x = 10 ; System . out . println ( " Count ▁ = ▁ " + countPairs ( arr1 , arr2 , m , n , x ) ) ; } }

def isPresent ( arr , low , high , value ) : NEW_LINE INDENT while ( low <= high ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( arr [ mid ] == value ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( arr [ mid ] > value ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def countPairs ( arr1 , arr2 , m , n , x ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT value = x - arr1 [ i ] NEW_LINE if ( isPresent ( arr2 , 0 , n - 1 , value ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 1 , 3 , 5 , 7 ] NEW_LINE arr2 = [ 2 , 3 , 5 , 8 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE x = 10 NEW_LINE print ( " Count ▁ = ▁ " , countPairs ( arr1 , arr2 , m , n , x ) ) NEW_LINE DEDENT


import java . util . * ; class Main { static void printDistinct ( int arr [ ] ) { HashSet < Integer > set = new HashSet < > ( ) ; for ( int i = 0 ; i < arr . length ; i ++ ) { if ( ! set . contains ( arr [ i ] ) ) { set . add ( arr [ i ] ) ; System . out . print ( arr [ i ] + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 5 , 3 , 4 , 3 , 5 , 6 } ; printDistinct ( arr ) ; } }

def printDistinct ( arr , n ) : NEW_LINE INDENT s = dict ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] not in s . keys ( ) ) : NEW_LINE INDENT s [ arr [ i ] ] = arr [ i ] ; NEW_LINE print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT DEDENT arr = [ 10 , 5 , 3 , 4 , 3 , 5 , 6 ] ; NEW_LINE n = 7 ; NEW_LINE printDistinct ( arr , n ) ; NEW_LINE


import java . io . * ; class GFG { static float Mean ( float arr [ ] , int n ) { float sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + arr [ i ] ; return sum / n ; } static float meanAbsDevtion ( float arr [ ] , int n ) { float absSum = 0 ; for ( int i = 0 ; i < n ; i ++ ) absSum = absSum + Math . abs ( arr [ i ] - Mean ( arr , n ) ) ; return absSum / n ; } public static void main ( String [ ] args ) { float arr [ ] = { 10 , 15 , 15 , 17 , 18 , 21 } ; int n = arr . length ; System . out . println ( meanAbsDevtion ( arr , n ) ) ; } }

def Mean ( arr , n ) : NEW_LINE INDENT sm = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sm = sm + arr [ i ] NEW_LINE DEDENT return sm // n NEW_LINE DEDENT def meanAbsoluteDeviation ( arr , n ) : NEW_LINE INDENT absSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT absSum = absSum + abs ( arr [ i ] - Mean ( arr , n ) ) NEW_LINE DEDENT return absSum / n NEW_LINE DEDENT arr = [ 10 , 15 , 15 , 17 , 18 , 21 ] NEW_LINE n = len ( arr ) NEW_LINE print ( meanAbsoluteDeviation ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static int cntSubsets ( int [ ] arr , int n ) { int [ ] a = new int [ n ] ; int [ ] b = new int [ n ] ; a [ 0 ] = b [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; b [ i ] = a [ i - 1 ] ; } int result = a [ n - 1 ] + b [ n - 1 ] ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 5 , 7 } ; int n = arr . length ; System . out . println ( cntSubsets ( arr , n ) ) ; } }

def cntSubsets ( arr , n ) : NEW_LINE INDENT a = [ 0 ] * n NEW_LINE b = [ 0 ] * n ; NEW_LINE a [ 0 ] = b [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT a [ i ] = a [ i - 1 ] + b [ i - 1 ] ; NEW_LINE b [ i ] = a [ i - 1 ] ; NEW_LINE DEDENT result = a [ n - 1 ] + b [ n - 1 ] ; NEW_LINE return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 5 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( cntSubsets ( arr , n ) ) ; NEW_LINE DEDENT


class Segregate { void segregate0and1 ( int arr [ ] , int size ) { int left = 0 , right = size - 1 ; while ( left < right ) { while ( arr [ left ] == 0 && left < right ) left ++ ; while ( arr [ right ] == 1 && left < right ) right -- ; if ( left < right ) { arr [ left ] = 0 ; arr [ right ] = 1 ; left ++ ; right -- ; } } } public static void main ( String [ ] args ) { Segregate seg = new Segregate ( ) ; int arr [ ] = new int [ ] { 0 , 1 , 0 , 1 , 1 , 1 } ; int i , arr_size = arr . length ; seg . segregate0and1 ( arr , arr_size ) ; System . out . print ( " Array ▁ after ▁ segregation ▁ is ▁ " ) ; for ( i = 0 ; i < 6 ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }

def segregate0and1 ( arr , size ) : NEW_LINE INDENT left , right = 0 , size - 1 NEW_LINE while left < right : NEW_LINE INDENT while arr [ left ] == 0 and left < right : NEW_LINE INDENT left += 1 NEW_LINE DEDENT while arr [ right ] == 1 and left < right : NEW_LINE INDENT right -= 1 NEW_LINE DEDENT if left < right : NEW_LINE INDENT arr [ left ] = 0 NEW_LINE arr [ right ] = 1 NEW_LINE left += 1 NEW_LINE right -= 1 NEW_LINE DEDENT DEDENT return arr NEW_LINE DEDENT arr = [ 0 , 1 , 0 , 1 , 1 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE print ( " Array ▁ after ▁ segregation " ) NEW_LINE print ( segregate0and1 ( arr , arr_size ) ) NEW_LINE


import java . util . ArrayList ; class Interval { int buy , sell ; } class StockBuySell { void stockBuySell ( int price [ ] , int n ) { if ( n == 1 ) return ; int count = 0 ; ArrayList < Interval > sol = new ArrayList < Interval > ( ) ; int i = 0 ; while ( i < n - 1 ) { while ( ( i < n - 1 ) && ( price [ i + 1 ] <= price [ i ] ) ) i ++ ; if ( i == n - 1 ) break ; Interval e = new Interval ( ) ; e . buy = i ++ ; while ( ( i < n ) && ( price [ i ] >= price [ i - 1 ] ) ) i ++ ; e . sell = i - 1 ; sol . add ( e ) ; count ++ ; } if ( count == 0 ) System . out . println ( " There ▁ is ▁ no ▁ day ▁ when ▁ buying ▁ the ▁ stock ▁ " + " will ▁ make ▁ profit " ) ; else for ( int j = 0 ; j < count ; j ++ ) System . out . println ( " Buy ▁ on ▁ day : ▁ " + sol . get ( j ) . buy + " ▁ ▁ ▁ ▁ ▁ ▁ ▁ ▁ " + " Sell ▁ on ▁ day ▁ : ▁ " + sol . get ( j ) . sell ) ; return ; } public static void main ( String args [ ] ) { StockBuySell stock = new StockBuySell ( ) ; int price [ ] = { 100 , 180 , 260 , 310 , 40 , 535 , 695 } ; int n = price . length ; stock . stockBuySell ( price , n ) ; } }

def stockBuySell ( price , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return NEW_LINE DEDENT i = 0 NEW_LINE while ( i < ( n - 1 ) ) : NEW_LINE INDENT while ( ( i < ( n - 1 ) ) and ( price [ i + 1 ] <= price [ i ] ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( i == n - 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT buy = i NEW_LINE i += 1 NEW_LINE while ( ( i < n ) and ( price [ i ] >= price [ i - 1 ] ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT sell = i - 1 NEW_LINE print ( " Buy ▁ on ▁ day : ▁ " , buy , " \t " , " Sell ▁ on ▁ day : ▁ " , sell ) NEW_LINE DEDENT DEDENT price = [ 100 , 180 , 260 , 310 , 40 , 535 , 695 ] NEW_LINE n = len ( price ) NEW_LINE stockBuySell ( price , n ) NEW_LINE


import java . io . * ; class GFG { static int digitSum ( long n ) { int digSum = 0 ; while ( n > 0 ) { digSum += n % 10 ; n /= 10 ; } return digSum ; } public static long countInteger ( long n , long s ) { if ( n < s ) return 0 ; for ( long i = s ; i <= Math . min ( n , s + 163 ) ; i ++ ) if ( ( i - digitSum ( i ) ) > s ) return ( n - i + 1 ) ; return 0 ; } public static void main ( String args [ ] ) { long n = 1000 , s = 100 ; System . out . println ( countInteger ( n , s ) ) ; } }

def digitSum ( n ) : NEW_LINE INDENT digSum = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digSum += n % 10 NEW_LINE n //= 10 NEW_LINE DEDENT return digSum NEW_LINE DEDENT def countInteger ( n , s ) : NEW_LINE INDENT if ( n < s ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in range ( s , min ( n , s + 163 ) + 1 ) : NEW_LINE INDENT if ( ( i - digitSum ( i ) ) > s ) : NEW_LINE INDENT return ( n - i + 1 ) NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT n = 1000 NEW_LINE s = 100 NEW_LINE print ( countInteger ( n , s ) ) NEW_LINE


class GFG { static class Node { int data ; Node next ; Node prev ; } ; static Node sort ( Node head ) { if ( head == null || head . next == null ) { return head ; } Node front = head ; Node last = head ; Node res = new Node ( ) ; Node resEnd = res ; Node next ; while ( last . next != null ) { last = last . next ; } while ( front != last ) { if ( last . data <= front . data ) { resEnd . next = last ; next = last . prev ; last . prev . next = null ; last . prev = resEnd ; last = next ; resEnd = resEnd . next ; } else { resEnd . next = front ; next = front . next ; front . next = null ; front . prev = resEnd ; front = next ; resEnd = resEnd . next ; } } resEnd . next = front ; front . prev = resEnd ; return res . next ; } 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 ; } static void printList ( Node head ) { if ( head == null ) System . out . print ( " Doubly ▁ Linked ▁ list ▁ empty " ) ; while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 1 ) ; head = push ( head , 4 ) ; head = push ( head , 6 ) ; head = push ( head , 10 ) ; head = push ( head , 12 ) ; head = push ( head , 7 ) ; head = push ( head , 5 ) ; head = push ( head , 2 ) ; System . out . print ( " Original ▁ Doubly ▁ linked ▁ list : \n " ) ; printList ( head ) ; head = sort ( head ) ; System . out . print ( " \n Doubly ▁ linked ▁ list ▁ after ▁ sorting : \n " ) ; printList ( head ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . prev = None NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def sort ( head ) : NEW_LINE INDENT if head == None or head . next == None : NEW_LINE INDENT return head NEW_LINE DEDENT front = head NEW_LINE last = head NEW_LINE res = Node ( None ) NEW_LINE resEnd = res NEW_LINE while last . next != None : NEW_LINE INDENT last = last . next NEW_LINE DEDENT while front != last : NEW_LINE INDENT if last . data <= front . data : NEW_LINE INDENT resEnd . next = last NEW_LINE next = last . prev NEW_LINE last . prev . next = None NEW_LINE last . prev = resEnd NEW_LINE last = next NEW_LINE resEnd = resEnd . next NEW_LINE DEDENT else : NEW_LINE INDENT resEnd . next = front NEW_LINE next = front . next NEW_LINE front . next = None NEW_LINE front . prev = resEnd NEW_LINE front = next NEW_LINE resEnd = resEnd . next NEW_LINE DEDENT DEDENT resEnd . next = front NEW_LINE front . prev = resEnd NEW_LINE return res . next NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( 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 def printList ( head ) : NEW_LINE INDENT if head == None : NEW_LINE INDENT print ( " Doubly ▁ Linked ▁ list ▁ empty " ) NEW_LINE DEDENT while head != None : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 1 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 10 ) NEW_LINE head = push ( head , 12 ) NEW_LINE head = push ( head , 7 ) NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 2 ) NEW_LINE print ( " Original ▁ Doubly ▁ linked ▁ list : " ) NEW_LINE printList ( head ) NEW_LINE head = sort ( head ) NEW_LINE print ( " \n Doubly ▁ linked ▁ list ▁ after ▁ sorting : " ) NEW_LINE printList ( head ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int countMaxSetBits ( int left , int right ) { while ( ( left | ( left + 1 ) ) <= right ) left |= left + 1 ; return left ; } public static void main ( String [ ] args ) { int l = 1 ; int r = 5 ; System . out . println ( countMaxSetBits ( l , r ) ) ; l = 1 ; r = 10 ; System . out . println ( countMaxSetBits ( l , r ) ) ; } }

def countMaxSetBits ( left , right ) : NEW_LINE INDENT while ( left | ( left + 1 ) ) <= right : NEW_LINE INDENT left |= left + 1 NEW_LINE DEDENT return left NEW_LINE DEDENT l = 1 NEW_LINE r = 5 NEW_LINE print ( countMaxSetBits ( l , r ) ) NEW_LINE l = 1 NEW_LINE r = 10 NEW_LINE print ( countMaxSetBits ( l , r ) ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static int kPrimeFactor ( int n , int k ) { while ( n % 2 == 0 ) { k -- ; n = n / 2 ; if ( k == 0 ) return 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { while ( n % i == 0 ) { if ( k == 1 ) return i ; k -- ; n = n / i ; } } if ( n > 2 && k == 1 ) return n ; return - 1 ; } public static void main ( String args [ ] ) { int n = 12 , k = 3 ; System . out . println ( kPrimeFactor ( n , k ) ) ; n = 14 ; k = 3 ; System . out . println ( kPrimeFactor ( n , k ) ) ; } }

import math NEW_LINE def kPrimeFactor ( n , k ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT k = k - 1 NEW_LINE n = n / 2 NEW_LINE if ( k == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT DEDENT i = 3 NEW_LINE while i <= math . sqrt ( n ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT if ( k == 1 ) : NEW_LINE INDENT return i NEW_LINE DEDENT k = k - 1 NEW_LINE n = n / i NEW_LINE DEDENT i = i + 2 NEW_LINE DEDENT if ( n > 2 and k == 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT return - 1 NEW_LINE DEDENT n = 12 NEW_LINE k = 3 NEW_LINE print ( kPrimeFactor ( n , k ) ) NEW_LINE n = 14 NEW_LINE k = 3 NEW_LINE print ( kPrimeFactor ( n , k ) ) NEW_LINE


class GFG { static long sumAP ( long n , long d ) { n /= d ; return ( n ) * ( 1 + n ) * d / 2 ; } static long sumMultiples ( long n ) { n -- ; return sumAP ( n , 2 ) + sumAP ( n , 5 ) - sumAP ( n , 10 ) ; } public static void main ( String [ ] args ) { long n = 20 ; System . out . println ( sumMultiples ( n ) ) ; } }

def sumAP ( n , d ) : NEW_LINE INDENT n = int ( n / d ) ; NEW_LINE return ( n ) * ( 1 + n ) * ( d / 2 ) ; NEW_LINE DEDENT def sumMultiples ( n ) : NEW_LINE INDENT n -= 1 ; NEW_LINE return ( int ( sumAP ( n , 2 ) + sumAP ( n , 5 ) - sumAP ( n , 10 ) ) ) ; NEW_LINE DEDENT n = 20 ; NEW_LINE print ( sumMultiples ( n ) ) ; NEW_LINE


class GFG { static int findNumberOfEvenCells ( int n , int q [ ] [ ] , int size ) { int row [ ] = new int [ n ] ; int col [ ] = new int [ n ] ; for ( int i = 0 ; i < size ; i ++ ) { int x = q [ i ] [ 0 ] ; int y = q [ i ] [ 1 ] ; row [ x - 1 ] ++ ; col [ y - 1 ] ++ ; } int r1 = 0 , r2 = 0 ; int c1 = 0 , c2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( row [ i ] % 2 == 0 ) { r1 ++ ; } if ( row [ i ] % 2 == 1 ) { r2 ++ ; } if ( col [ i ] % 2 == 0 ) { c1 ++ ; } if ( col [ i ] % 2 == 1 ) { c2 ++ ; } } int count = r1 * c1 + r2 * c2 ; return count ; } public static void main ( String [ ] args ) { int n = 2 ; int q [ ] [ ] = { { 1 , 1 } , { 1 , 2 } , { 2 , 1 } } ; int size = q . length ; System . out . println ( findNumberOfEvenCells ( n , q , size ) ) ; } }

def findNumberOfEvenCells ( n , q , size ) : NEW_LINE INDENT row = [ 0 ] * n ; NEW_LINE col = [ 0 ] * n NEW_LINE for i in range ( size ) : NEW_LINE INDENT x = q [ i ] [ 0 ] ; NEW_LINE y = q [ i ] [ 1 ] ; NEW_LINE row [ x - 1 ] += 1 ; NEW_LINE col [ y - 1 ] += 1 ; NEW_LINE DEDENT r1 = 0 ; NEW_LINE r2 = 0 ; NEW_LINE c1 = 0 ; NEW_LINE c2 = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( row [ i ] % 2 == 0 ) : NEW_LINE INDENT r1 += 1 ; NEW_LINE DEDENT if ( row [ i ] % 2 == 1 ) : NEW_LINE INDENT r2 += 1 ; NEW_LINE DEDENT if ( col [ i ] % 2 == 0 ) : NEW_LINE INDENT c1 += 1 ; NEW_LINE DEDENT if ( col [ i ] % 2 == 1 ) : NEW_LINE INDENT c2 += 1 ; NEW_LINE DEDENT DEDENT count = r1 * c1 + r2 * c2 ; NEW_LINE return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE q = [ [ 1 , 1 ] , [ 1 , 2 ] , [ 2 , 1 ] ] ; NEW_LINE size = len ( q ) ; NEW_LINE print ( findNumberOfEvenCells ( n , q , size ) ) ; NEW_LINE DEDENT


class GFG { static final int max = 10000 ; static int dp [ ] = new int [ max ] ; static void initDP ( ) { for ( int i = 0 ; i < max ; i ++ ) dp [ i ] = - 1 ; } static int func ( int x ) { if ( x == 0 ) return 0 ; if ( dp [ x ] == - 1 ) dp [ x ] = x + func ( x / 2 ) ; return dp [ x ] ; } static void printFloor ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( func ( arr [ i ] ) + " ▁ " ) ; } public static void main ( String [ ] args ) { initDP ( ) ; int arr [ ] = { 8 , 6 } ; int n = arr . length ; printFloor ( arr , n ) ; } }

max = 10000 NEW_LINE dp = [ 0 ] * max NEW_LINE def initDP ( ) : NEW_LINE INDENT for i in range ( max ) : NEW_LINE INDENT dp [ i ] = - 1 NEW_LINE DEDENT DEDENT def func ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ x ] == - 1 ) : NEW_LINE INDENT dp [ x ] = x + func ( x // 2 ) NEW_LINE DEDENT return dp [ x ] NEW_LINE DEDENT def printFloor ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( func ( arr [ i ] ) , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT initDP ( ) NEW_LINE arr = [ 8 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE printFloor ( arr , n ) NEW_LINE DEDENT


import java . util . * ; import java . io . * ; class GFG { public static void sortSquares ( int arr [ ] ) { int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = arr [ i ] * arr [ i ] ; Arrays . sort ( arr ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 6 , - 3 , - 1 , 2 , 4 , 5 } ; int n = arr . length ; System . out . println ( " Before ▁ sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; sortSquares ( arr ) ; System . out . println ( " " ) ; System . out . println ( " After ▁ Sort ▁ " ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }

def sortSquare ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = arr [ i ] * arr [ i ] NEW_LINE DEDENT arr . sort ( ) NEW_LINE DEDENT arr = [ - 6 , - 3 , - 1 , 2 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Before ▁ sort " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " ) NEW_LINE sortSquare ( arr , n ) NEW_LINE print ( " After ▁ sort " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static float area ( float a , float b ) { if ( a < 0 || b < 0 ) return - 1 ; float A = a * b ; return A ; } public static void main ( String [ ] args ) { float a = 5 , b = 2 ; System . out . println ( area ( a , b ) ) ; } }

def area ( a , b ) : NEW_LINE INDENT if ( a < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT A = a * b NEW_LINE return A NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 5 NEW_LINE b = 2 NEW_LINE print ( area ( a , b ) ) NEW_LINE DEDENT


class GFG { static void printRoots ( long a , long b , long c ) { System . out . println ( 1 + " , ▁ " + c / ( a * 1.0 ) ) ; } public static void main ( String [ ] args ) { long a = 2 ; long b = 3 ; long c = - 5 ; printRoots ( a , b , c ) ; } }

def printRoots ( a , b , c ) : NEW_LINE INDENT print ( 1 , " , " , c / ( a * 1.0 ) ) NEW_LINE DEDENT a = 2 NEW_LINE b = 3 NEW_LINE c = - 5 NEW_LINE printRoots ( a , b , c ) NEW_LINE


import java . util . * ; class GFG { static Vector < Long > gen ( long n , Vector < Long > r ) { long a = r . get ( r . size ( ) - 1 ) ; a ++ ; for ( int i = 1 ; i <= n ; a += 2 , i ++ ) { r . add ( a ) ; } return r ; } static Vector < Long > conell ( long n ) { Vector < Long > res = new Vector < Long > ( ) ; long k = 1 ; res . add ( 0L ) ; while ( true ) { res = gen ( k , res ) ; k ++ ; int j = res . size ( ) - 1 ; while ( j != n && j + k > n ) { k -- ; } if ( j >= n ) { break ; } } res . remove ( 0 ) ; return res ; } public static void main ( String [ ] args ) { long n = 10 ; System . out . println ( " The ▁ first ▁ " + n + " ▁ terms ▁ are " ) ; Vector < Long > res = conell ( n ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) { System . out . print ( res . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; } }

def gen ( n , r ) : NEW_LINE INDENT a = r [ - 1 ] NEW_LINE a += 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT r . append ( a ) NEW_LINE a += 2 NEW_LINE DEDENT return r NEW_LINE DEDENT def conell ( n ) : NEW_LINE INDENT res = [ ] NEW_LINE k = 1 NEW_LINE res . append ( 0 ) NEW_LINE while 1 : NEW_LINE INDENT res = gen ( k , res ) NEW_LINE k += 1 NEW_LINE j = len ( res ) - 1 NEW_LINE while j != n and j + k > n : NEW_LINE INDENT k -= 1 NEW_LINE DEDENT if j >= n : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT res . remove ( res [ 0 ] ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE print ( " The ▁ first ▁ % d ▁ terms ▁ are " % n ) NEW_LINE res = conell ( n ) NEW_LINE for i in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT


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

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


import java . io . * ; class GFG { static int findTwoThreePrime ( int l , int r ) { if ( l == 1 ) l ++ ; int count = 0 ; for ( int i = l ; i <= r ; i ++ ) { int num = i ; while ( num % 2 == 0 ) num /= 2 ; while ( num % 3 == 0 ) num /= 3 ; if ( num == 1 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int l = 1 , r = 10 ; System . out . println ( findTwoThreePrime ( l , r ) ) ; } }

def findTwoThreePrime ( l , r ) : NEW_LINE INDENT if ( l == 1 ) : NEW_LINE INDENT l += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT num = i NEW_LINE while ( num % 2 == 0 ) : NEW_LINE INDENT num //= 2 ; NEW_LINE DEDENT while ( num % 3 == 0 ) : NEW_LINE INDENT num //= 3 NEW_LINE DEDENT if ( num == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 1 NEW_LINE r = 10 NEW_LINE print ( findTwoThreePrime ( l , r ) ) NEW_LINE DEDENT


class GFG { static void findNumbers ( int n ) { String ans = " " ; for ( int i = 0 ; i < n - 1 ; i ++ ) ans += '7' ; String even = ans + '6' ; String odd = ans + '7' ; System . out . println ( " Even ▁ : ▁ " + even ) ; System . out . println ( " Odd ▁ : ▁ " + odd ) ; } public static void main ( String args [ ] ) { int n = 4 ; findNumbers ( n ) ; } }

def findNumbers ( N ) : NEW_LINE INDENT ans = '7' * ( N - 1 ) NEW_LINE even = ans + '6' ; NEW_LINE odd = ans + '7' ; NEW_LINE print ( " Even ▁ : ▁ " , even ) ; NEW_LINE print ( " Odd ▁ : ▁ " , odd ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 ; NEW_LINE findNumbers ( n ) ; NEW_LINE DEDENT


class GFG { static int countNumber ( int N , int S ) { int countElements = 0 ; int currSum = 0 ; while ( currSum <= S ) { currSum += N ; N -- ; countElements ++ ; } return countElements ; } public static void main ( String [ ] args ) { int N , S ; N = 5 ; S = 11 ; int count = countNumber ( N , S ) ; System . out . println ( count ) ; } }

def countNumber ( N , S ) : NEW_LINE INDENT countElements = 0 ; NEW_LINE currentSum = 0 NEW_LINE currSum = 0 ; NEW_LINE while ( currSum <= S ) : NEW_LINE INDENT currSum += N ; NEW_LINE N = N - 1 ; NEW_LINE countElements = countElements + 1 ; NEW_LINE DEDENT return countElements ; NEW_LINE DEDENT N = 5 ; NEW_LINE S = 11 ; NEW_LINE count = countNumber ( N , S ) ; NEW_LINE print ( count ) ; NEW_LINE


def fib ( n ) : NEW_LINE INDENT F = [ [ 1 , 1 ] , [ 1 , 0 ] ] NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT power ( F , n - 1 ) NEW_LINE return F [ 0 ] [ 0 ] NEW_LINE DEDENT def multiply ( F , M ) : NEW_LINE INDENT x = ( F [ 0 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 0 ] ) NEW_LINE y = ( F [ 0 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 0 ] [ 1 ] * M [ 1 ] [ 1 ] ) NEW_LINE z = ( F [ 1 ] [ 0 ] * M [ 0 ] [ 0 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 0 ] ) NEW_LINE w = ( F [ 1 ] [ 0 ] * M [ 0 ] [ 1 ] + F [ 1 ] [ 1 ] * M [ 1 ] [ 1 ] ) NEW_LINE F [ 0 ] [ 0 ] = x NEW_LINE F [ 0 ] [ 1 ] = y NEW_LINE F [ 1 ] [ 0 ] = z NEW_LINE F [ 1 ] [ 1 ] = w NEW_LINE DEDENT def power ( F , n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT M = [ [ 1 , 1 ] , [ 1 , 0 ] ] ; NEW_LINE power ( F , n // 2 ) NEW_LINE multiply ( F , F ) NEW_LINE if ( n % 2 != 0 ) : NEW_LINE INDENT multiply ( F , M ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 9 NEW_LINE print ( fib ( n ) ) NEW_LINE DEDENT


class GFG { static boolean isprime ( int n ) { for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void LegendreConjecture ( int n ) { System . out . println ( " Primes ▁ in ▁ the ▁ range ▁ " + n * n + " ▁ and ▁ " + ( n + 1 ) * ( n + 1 ) + " ▁ are : " ) ; for ( int i = n * n ; i <= ( ( n + 1 ) * ( n + 1 ) ) ; i ++ ) { if ( isprime ( i ) ) System . out . println ( i ) ; } } public static void main ( String [ ] args ) { int n = 50 ; LegendreConjecture ( n ) ; } }

import math NEW_LINE def isprime ( n ) : NEW_LINE INDENT i = 2 NEW_LINE for i in range ( 2 , int ( ( math . sqrt ( n ) + 1 ) ) ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def LegendreConjecture ( n ) : NEW_LINE INDENT print ( " Primes ▁ in ▁ the ▁ range ▁ " , n * n , " ▁ and ▁ " , ( n + 1 ) * ( n + 1 ) , " ▁ are : " ) NEW_LINE for i in range ( n * n , ( ( ( n + 1 ) * ( n + 1 ) ) + 1 ) ) : NEW_LINE INDENT if ( isprime ( i ) ) : NEW_LINE INDENT print ( i ) NEW_LINE DEDENT DEDENT DEDENT n = 50 NEW_LINE LegendreConjecture ( n ) NEW_LINE


import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int findNumber ( int arr [ ] , int n ) { int ans = arr [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) ans = gcd ( ans , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == ans ) return ans ; return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 2 , 4 } ; int n = arr . length ; System . out . println ( findNumber ( arr , n ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def findNumber ( arr , n ) : NEW_LINE INDENT ans = arr [ 0 ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT ans = gcd ( ans , arr [ i ] ) NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == ans ) : NEW_LINE INDENT return ans NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 2 , 2 , 4 ] ; NEW_LINE n = len ( arr ) NEW_LINE print ( findNumber ( arr , n ) ) NEW_LINE


class GFG { static int countDivisibleSubseq ( String str , int n ) { int len = str . length ( ) ; int dp [ ] [ ] = new int [ len ] [ n ] ; dp [ 0 ] [ ( str . charAt ( 0 ) - '0' ) % n ] ++ ; for ( int i = 1 ; i < len ; i ++ ) { dp [ i ] [ ( str . charAt ( i ) - '0' ) % n ] ++ ; for ( int j = 0 ; j < n ; j ++ ) { dp [ i ] [ j ] += dp [ i - 1 ] [ j ] ; dp [ i ] [ ( j * 10 + ( str . charAt ( i ) - '0' ) ) % n ] += dp [ i - 1 ] [ j ] ; } } return dp [ len - 1 ] [ 0 ] ; } public static void main ( String [ ] args ) { String str = "1234" ; int n = 4 ; System . out . print ( countDivisibleSubseq ( str , n ) ) ; } }

def countDivisibleSubseq ( str , n ) : NEW_LINE INDENT l = len ( str ) NEW_LINE dp = [ [ 0 for x in range ( l ) ] for y in range ( n ) ] NEW_LINE dp [ 0 ] [ ( ord ( str [ 0 ] ) - ord ( '0' ) ) % n ] += 1 NEW_LINE for i in range ( 1 , l ) : NEW_LINE INDENT dp [ i ] [ ( ord ( str [ i ] ) - ord ( '0' ) ) % n ] += 1 NEW_LINE for j in range ( n ) : NEW_LINE INDENT dp [ i ] [ j ] += dp [ i - 1 ] [ j ] NEW_LINE dp [ i ] [ ( j * 10 + ( ord ( str [ i ] ) - ord ( '0' ) ) ) % n ] += dp [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT return dp [ l - 1 ] [ 0 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "1234" NEW_LINE n = 4 NEW_LINE print ( countDivisibleSubseq ( str , n ) ) NEW_LINE DEDENT


import java . util . Arrays ; public class GFG { static boolean sackRace ( int p1 , int s1 , int p2 , int s2 ) { return ( ( s1 > s2 && ( p2 - p1 ) % ( s1 - s2 ) == 0 ) || ( s2 > s1 && ( p1 - p2 ) % ( s2 - s1 ) == 0 ) ) ; } public static void main ( String args [ ] ) { int p1 = 4 , s1 = 4 , p2 = 8 , s2 = 2 ; if ( sackRace ( p1 , s1 , p2 , s2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def sackRace ( p1 , s1 , p2 , s2 ) : NEW_LINE INDENT return ( ( s1 > s2 and ( p2 - p1 ) % ( s1 - s2 ) == 0 ) or ( s2 > s1 and ( p1 - p2 ) % ( s2 - s1 ) == 0 ) ) NEW_LINE DEDENT p1 = 4 NEW_LINE s1 = 4 NEW_LINE p2 = 8 NEW_LINE s2 = 2 NEW_LINE if ( sackRace ( p1 , s1 , p2 , s2 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { public static int countIdenticalRows ( int mat [ ] [ ] ) { int count = 0 ; for ( int i = 0 ; i < mat . length ; i ++ ) { int first = mat [ i ] [ 0 ] ; boolean allSame = true ; for ( int j = 1 ; j < mat [ i ] . length ; j ++ ) { if ( mat [ i ] [ j ] != first ) { allSame = false ; break ; } } if ( allSame ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 2 } , { 2 , 2 , 2 } , { 5 , 5 , 2 } } ; System . out . print ( countIdenticalRows ( mat ) ) ; } }

def countIdenticalRows ( mat ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( mat ) ) : NEW_LINE INDENT first = mat [ i ] [ 0 ] NEW_LINE allSame = True NEW_LINE for j in range ( 1 , len ( mat [ i ] ) ) : NEW_LINE INDENT if ( mat [ i ] [ j ] != first ) : NEW_LINE INDENT allSame = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( allSame ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 1 , 1 , 2 ] , [ 2 , 2 , 2 ] , [ 5 , 5 , 2 ] ] NEW_LINE print ( countIdenticalRows ( mat ) ) NEW_LINE DEDENT


import java . util . * ; class Geeks { static int CountTheElements ( int arr [ ] , int n , int k ) { int counter = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == 0 ) counter ++ ; } return counter ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 6 , 7 , 12 , 14 , 18 } ; int n = arr . length ; int k = 3 ; System . out . println ( CountTheElements ( arr , n , k ) ) ; } }

def CountTheElements ( arr , n , k ) : NEW_LINE INDENT counter = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] % k == 0 ) : NEW_LINE INDENT counter = counter + 1 NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 6 , 7 , 12 , 14 , 18 ] ; NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( CountTheElements ( arr , n , k ) ) NEW_LINE DEDENT


class GFG { static void printSum ( int m [ ] , int s [ ] , int n ) { int total = 0 ; for ( int i = 0 ; i < n ; i ++ ) { total += s [ i ] ; total += ( m [ i ] * 60 ) ; } System . out . print ( total / 3600 + " ▁ : ▁ " ) ; total %= 3600 ; System . out . print ( total / 60 + " : ▁ " ) ; total %= 60 ; System . out . println ( total ) ; } public static void main ( String [ ] args ) { int m [ ] = { 0 , 2 , 3 , 2 , 1 } ; int s [ ] = { 45 , 31 , 11 , 27 , 28 } ; int n = m . length ; printSum ( m , s , n ) ; } }

def printSum ( m , s , n ) : NEW_LINE INDENT total = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT total += s [ i ] NEW_LINE total += ( m [ i ] * 60 ) NEW_LINE DEDENT print ( int ( total / 3600 ) , end = " ▁ : ▁ " ) NEW_LINE total %= 3600 NEW_LINE print ( int ( total / 60 ) , end = " : ▁ " ) NEW_LINE total %= 60 NEW_LINE print ( int ( total ) ) NEW_LINE DEDENT m = [ 0 , 2 , 3 , 2 , 1 ] NEW_LINE s = [ 45 , 31 , 11 , 27 , 28 ] NEW_LINE n = len ( m ) NEW_LINE printSum ( m , s , n ) NEW_LINE


import java . io . * ; class GFG { static long Odd_Sum ( int n ) { long total = ( n + 1 ) / 2 ; long odd = total * total ; return odd ; } static long Even_Sum ( int n ) { long total = ( n ) / 2 ; long even = total * ( total + 1 ) ; return even ; } static long sumLtoR ( int L , int R ) { long odd_sum , even_sum ; odd_sum = Odd_Sum ( R ) - Odd_Sum ( L - 1 ) ; even_sum = Even_Sum ( R ) - Even_Sum ( L - 1 ) ; return even_sum - odd_sum ; } public static void main ( String [ ] args ) { int L = 1 , R = 5 ; System . out . println ( sumLtoR ( L , R ) ) ; } }

def Odd_Sum ( n ) : NEW_LINE INDENT total = ( n + 1 ) // 2 NEW_LINE odd = total * total NEW_LINE return odd NEW_LINE DEDENT def Even_Sum ( n ) : NEW_LINE INDENT total = n // 2 NEW_LINE even = total * ( total + 1 ) NEW_LINE return even NEW_LINE DEDENT def sumLtoR ( L , R ) : NEW_LINE INDENT odd_sum = Odd_Sum ( R ) - Odd_Sum ( L - 1 ) NEW_LINE even_sum = Even_Sum ( R ) - Even_Sum ( L - 1 ) NEW_LINE return even_sum - odd_sum NEW_LINE DEDENT L = 1 ; R = 5 NEW_LINE print ( sumLtoR ( L , R ) ) NEW_LINE


import java . io . * ; class GFG { static void printArray ( int X , int d , int first_ele ) { while ( X > 0 ) { int count_ele = 0 , pow_of_two = 2 ; while ( X - pow_of_two + 1 >= 0 ) { count_ele ++ ; pow_of_two *= 2 ; } X = X - ( pow_of_two / 2 ) + 1 ; for ( int j = 0 ; j < count_ele ; j ++ ) System . out . print ( first_ele + " ▁ " ) ; first_ele += d ; } } public static void main ( String [ ] args ) { int d = 100 , X = 25 ; printArray ( X , d , 1 ) ; } }

def printArray ( X , d , first_ele ) : NEW_LINE INDENT while ( X > 0 ) : NEW_LINE INDENT count_ele , pow_of_two = 0 , 2 NEW_LINE while ( X - pow_of_two + 1 >= 0 ) : NEW_LINE INDENT count_ele += 1 NEW_LINE pow_of_two *= 2 NEW_LINE DEDENT X = X - ( pow_of_two / 2 ) + 1 NEW_LINE for j in range ( count_ele ) : NEW_LINE INDENT print ( first_ele , end = " ▁ " ) NEW_LINE DEDENT first_ele += d NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT d , X = 100 , 25 NEW_LINE printArray ( X , d , 1 ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class Pair { int first , second ; Pair ( int a , int b ) { first = a ; second = b ; } } public class GFG { static ArrayList < Pair > findSubArrays ( int [ ] arr , int n ) { HashMap < Integer , ArrayList < Integer > > map = new HashMap < > ( ) ; ArrayList < Pair > out = new ArrayList < > ( ) ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; if ( sum == 0 ) out . add ( new Pair ( 0 , i ) ) ; ArrayList < Integer > al = new ArrayList < > ( ) ; if ( map . containsKey ( sum ) ) { al = map . get ( sum ) ; for ( int it = 0 ; it < al . size ( ) ; it ++ ) { out . add ( new Pair ( al . get ( it ) + 1 , i ) ) ; } } al . add ( i ) ; map . put ( sum , al ) ; } return out ; } static void print ( ArrayList < Pair > out ) { for ( int i = 0 ; i < out . size ( ) ; i ++ ) { Pair p = out . get ( i ) ; System . out . println ( " Subarray ▁ found ▁ from ▁ Index ▁ " + p . first + " ▁ to ▁ " + p . second ) ; } } public static void main ( String args [ ] ) { int [ ] arr = { 6 , 3 , - 1 , - 3 , 4 , - 2 , 2 , 4 , 6 , - 12 , - 7 } ; int n = arr . length ; ArrayList < Pair > out = findSubArrays ( arr , n ) ; if ( out . size ( ) == 0 ) System . out . println ( " No ▁ subarray ▁ exists " ) ; else print ( out ) ; } }

def findSubArrays ( arr , n ) : NEW_LINE INDENT hashMap = { } NEW_LINE out = [ ] NEW_LINE sum1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum1 += arr [ i ] NEW_LINE if sum1 == 0 : NEW_LINE INDENT out . append ( ( 0 , i ) ) NEW_LINE DEDENT al = [ ] NEW_LINE if sum1 in hashMap : NEW_LINE INDENT al = hashMap . get ( sum1 ) NEW_LINE for it in range ( len ( al ) ) : NEW_LINE INDENT out . append ( ( al [ it ] + 1 , i ) ) NEW_LINE DEDENT DEDENT al . append ( i ) NEW_LINE hashMap [ sum1 ] = al NEW_LINE DEDENT return out NEW_LINE DEDENT def printOutput ( output ) : NEW_LINE INDENT for i in output : NEW_LINE INDENT print ( " Subarray ▁ found ▁ from ▁ Index ▁ " + str ( i [ 0 ] ) + " ▁ to ▁ " + str ( i [ 1 ] ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 6 , 3 , - 1 , - 3 , 4 , - 2 , 2 , 4 , 6 , - 12 , - 7 ] NEW_LINE n = len ( arr ) NEW_LINE out = findSubArrays ( arr , n ) NEW_LINE if ( len ( out ) == 0 ) : NEW_LINE INDENT print ( " No ▁ subarray ▁ exists " ) NEW_LINE DEDENT else : NEW_LINE INDENT printOutput ( out ) NEW_LINE DEDENT DEDENT


class GFG { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } static int minVal ( int a , int b ) { int setBits = countSetBits ( b ) ; int ans = 0 ; for ( int i = 30 ; i >= 0 ; i -- ) { int mask = 1 << i ; if ( ( a & mask ) > 0 && setBits > 0 ) { ans |= ( 1 << i ) ; setBits -- ; } } return ans ; } public static void main ( String [ ] args ) { int a = 3 , b = 5 ; System . out . println ( minVal ( a , b ) ) ; } }

def minVal ( a , b ) : NEW_LINE INDENT setBits = bin ( b ) . count ( '1' ) ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( 30 , - 1 , - 1 ) : NEW_LINE INDENT mask = ( 1 << i ) ; NEW_LINE s = ( a & mask ) ; NEW_LINE if ( s and setBits > 0 ) : NEW_LINE INDENT ans |= ( 1 << i ) ; NEW_LINE setBits -= 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 ; b = 5 ; NEW_LINE print ( minVal ( a , b ) ) ; NEW_LINE DEDENT


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

def getNthTerm ( N ) : NEW_LINE INDENT return 4 * pow ( N , 2 ) - 3 * N + 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 10 NEW_LINE print ( getNthTerm ( N ) ) NEW_LINE DEDENT


class GFG { static int getModulo ( int n , int d ) { return ( n & ( d - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 6 ; int d = 4 ; System . out . println ( n + " ▁ moduo ▁ " + d + " ▁ is ▁ " + getModulo ( n , d ) ) ; } }

def getModulo ( n , d ) : NEW_LINE INDENT return ( n & ( d - 1 ) ) NEW_LINE DEDENT n = 6 NEW_LINE d = 4 NEW_LINE print ( n , " moduo " , d , " is " , getModulo ( n , d ) ) NEW_LINE


class GFG { static void split ( int x , int n ) { if ( x < n ) System . out . print ( " - 1 ▁ " ) ; else if ( x % n == 0 ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ( x / n ) + " ▁ " ) ; } else { int zp = n - ( x % n ) ; int pp = x / n ; for ( int i = 0 ; i < n ; i ++ ) { if ( i >= zp ) System . out . print ( ( pp + 1 ) + " ▁ " ) ; else System . out . print ( pp + " ▁ " ) ; } } } public static void main ( String [ ] args ) { int x = 5 ; int n = 3 ; split ( x , n ) ; } }

def split ( x , n ) : NEW_LINE INDENT if ( x < n ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT elif ( x % n == 0 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( x // n , end = " ▁ " ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT zp = n - ( x % n ) NEW_LINE pp = x // n NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i >= zp ) : NEW_LINE INDENT print ( pp + 1 , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( pp , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT x = 5 NEW_LINE n = 3 NEW_LINE split ( x , n ) NEW_LINE


public class GFG { static int PowerOFPINnfactorial ( int n , int p ) { int ans = 0 ; int temp = p ; while ( temp <= n ) { ans += n / temp ; temp = temp * p ; } return ans ; } public static void main ( String [ ] args ) { System . out . println ( PowerOFPINnfactorial ( 4 , 2 ) ) ; } }

def PowerOFPINnfactorial ( n , p ) : NEW_LINE INDENT ans = 0 NEW_LINE temp = p NEW_LINE while ( temp <= n ) : NEW_LINE INDENT ans += n / temp NEW_LINE temp = temp * p NEW_LINE DEDENT return int ( ans ) NEW_LINE DEDENT print ( PowerOFPINnfactorial ( 4 , 2 ) ) NEW_LINE


class GFG { static int findNumberOfTimes ( String str1 , String str2 ) { int freq [ ] = new int [ 26 ] ; int l1 = str1 . length ( ) ; for ( int i = 0 ; i < l1 ; i ++ ) { freq [ str1 . charAt ( i ) - ' a ' ] += 1 ; } int l2 = str2 . length ( ) ; int count = Integer . MAX_VALUE ; for ( int i = 0 ; i < l2 ; i ++ ) { count = Math . min ( count , freq [ str2 . charAt ( i ) - ' a ' ] ) ; } return count ; } public static void main ( String [ ] args ) { String str1 = " foreeksgekseg " ; String str2 = " geeks " ; System . out . println ( findNumberOfTimes ( str1 , str2 ) ) ; } }

import sys NEW_LINE def findNumberOfTimes ( str1 , str2 ) : NEW_LINE INDENT freq = [ 0 ] * 26 NEW_LINE l1 = len ( str1 ) NEW_LINE for i in range ( l1 ) : NEW_LINE INDENT freq [ ord ( str1 [ i ] ) - ord ( " a " ) ] += 1 NEW_LINE DEDENT l2 = len ( str2 ) NEW_LINE count = sys . maxsize NEW_LINE for i in range ( l2 ) : NEW_LINE INDENT count = min ( count , freq [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] ) NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " foreeksgekseg " NEW_LINE str2 = " geeks " NEW_LINE print ( findNumberOfTimes ( str1 , str2 ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int maxNewElements ( int [ ] a , int n ) { int gcd = a [ 0 ] ; int mx = a [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { gcd = __gcd ( gcd , a [ i ] ) ; mx = Math . max ( mx , a [ i ] ) ; } int total_terms = mx / gcd ; return total_terms - n ; } public static void main ( String args [ ] ) { int a [ ] = { 4 , 6 , 10 } ; int n = a . length ; System . out . print ( maxNewElements ( a , n ) ) ; } }

from math import gcd NEW_LINE def maxNewElements ( a , n ) : NEW_LINE INDENT __gcd = a [ 0 ] NEW_LINE mx = a [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT __gcd = gcd ( __gcd , a [ i ] ) NEW_LINE mx = max ( mx , a [ i ] ) NEW_LINE DEDENT total_terms = mx / __gcd NEW_LINE return total_terms - n NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 6 , 10 ] NEW_LINE n = len ( a ) NEW_LINE print ( int ( maxNewElements ( a , n ) ) ) NEW_LINE DEDENT


class GFG { static int findS ( int s ) { int sum = 0 ; for ( int n = 1 ; sum < s ; n ++ ) { sum += n ; if ( sum == s ) return n ; } return - 1 ; } public static void main ( String [ ] args ) { int s = 15 ; int n = findS ( s ) ; if ( n == - 1 ) System . out . println ( " - 1" ) ; else System . out . println ( n ) ; } }

def findS ( s ) : NEW_LINE INDENT _sum = 0 NEW_LINE n = 1 NEW_LINE while ( _sum < s ) : NEW_LINE INDENT _sum += n NEW_LINE n += 1 NEW_LINE DEDENT n -= 1 NEW_LINE if _sum == s : NEW_LINE INDENT return n NEW_LINE DEDENT return - 1 NEW_LINE DEDENT s = 15 NEW_LINE n = findS ( s ) NEW_LINE if n == - 1 : NEW_LINE INDENT print ( " - 1" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int maxSumWO3Consec ( int arr [ ] , int n ) { int sum [ ] = new int [ n ] ; if ( n >= 1 ) sum [ 0 ] = arr [ 0 ] ; if ( n >= 2 ) sum [ 1 ] = arr [ 0 ] + arr [ 1 ] ; if ( n > 2 ) sum [ 2 ] = Math . max ( sum [ 1 ] , Math . max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) ; for ( int i = 3 ; i < n ; i ++ ) sum [ i ] = Math . max ( Math . max ( sum [ i - 1 ] , sum [ i - 2 ] + arr [ i ] ) , arr [ i ] + arr [ i - 1 ] + sum [ i - 3 ] ) ; return sum [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 100 , 1000 , 100 , 1000 , 1 } ; int n = arr . length ; System . out . println ( maxSumWO3Consec ( arr , n ) ) ; } }

def maxSumWO3Consec ( arr , n ) : NEW_LINE INDENT sum = [ 0 for k in range ( n ) ] NEW_LINE if n >= 1 : NEW_LINE INDENT sum [ 0 ] = arr [ 0 ] NEW_LINE DEDENT if n >= 2 : NEW_LINE INDENT sum [ 1 ] = arr [ 0 ] + arr [ 1 ] NEW_LINE DEDENT if n > 2 : NEW_LINE INDENT sum [ 2 ] = max ( sum [ 1 ] , max ( arr [ 1 ] + arr [ 2 ] , arr [ 0 ] + arr [ 2 ] ) ) NEW_LINE DEDENT for i in range ( 3 , n ) : NEW_LINE INDENT sum [ i ] = max ( max ( sum [ i - 1 ] , sum [ i - 2 ] + arr [ i ] ) , arr [ i ] + arr [ i - 1 ] + sum [ i - 3 ] ) NEW_LINE DEDENT return sum [ n - 1 ] NEW_LINE DEDENT arr = [ 100 , 1000 , 100 , 1000 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print maxSumWO3Consec ( arr , n ) NEW_LINE


class GFG { static void findPerm ( int Q [ ] , int n ) { int minval = 0 , qsum = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { qsum += Q [ i ] ; if ( qsum < minval ) minval = qsum ; } int [ ] P = new int [ n ] ; P [ 0 ] = 1 - minval ; boolean permFound = true ; for ( int i = 0 ; i < n - 1 ; i ++ ) { P [ i + 1 ] = P [ i ] + Q [ i ] ; if ( P [ i + 1 ] > n || P [ i + 1 ] < 1 ) { permFound = false ; break ; } } if ( permFound ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( P [ i ] + " ▁ " ) ; } } else { System . out . print ( - 1 ) ; } } public static void main ( String [ ] args ) { int Q [ ] = { - 2 , 1 } ; int n = 1 + Q . length ; findPerm ( Q , n ) ; } }

def findPerm ( Q , n ) : NEW_LINE INDENT minval = 0 ; qsum = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT qsum += Q [ i ] ; NEW_LINE if ( qsum < minval ) : NEW_LINE INDENT minval = qsum ; NEW_LINE DEDENT DEDENT P = [ 0 ] * n ; NEW_LINE P [ 0 ] = 1 - minval ; NEW_LINE permFound = True ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT P [ i + 1 ] = P [ i ] + Q [ i ] ; NEW_LINE if ( P [ i + 1 ] > n or P [ i + 1 ] < 1 ) : NEW_LINE INDENT permFound = False ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( permFound ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( P [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Q = [ - 2 , 1 ] ; NEW_LINE n = 1 + len ( Q ) ; NEW_LINE findPerm ( Q , n ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void CamelCase ( ArrayList < String > words , String pattern ) { Map < String , List < String > > map = new HashMap < String , List < String > > ( ) ; for ( int i = 0 ; i < words . size ( ) ; i ++ ) { String str = " " ; int l = words . get ( i ) . length ( ) ; for ( int j = 0 ; j < l ; j ++ ) { if ( words . get ( i ) . charAt ( j ) >= ' A ' && words . get ( i ) . charAt ( j ) <= ' Z ' ) { str += words . get ( i ) . charAt ( j ) ; map . put ( str , list ( map . get ( str ) , words . get ( i ) ) ) ; } } } boolean wordFound = false ; for ( Map . Entry < String , List < String > > it : map . entrySet ( ) ) { if ( it . getKey ( ) . equals ( pattern ) ) { wordFound = true ; for ( String s : it . getValue ( ) ) System . out . print ( s + " \n " ) ; } } if ( ! wordFound ) { System . out . print ( " No ▁ match ▁ found " ) ; } } private static List < String > list ( List < String > list , String str ) { List < String > temp = new ArrayList < String > ( ) ; if ( list != null ) temp . addAll ( list ) ; temp . add ( str ) ; return temp ; } public static void main ( String [ ] args ) { String arr [ ] = { " Hi " , " Hello " , " HelloWorld " , " HiTech " , " HiGeek " , " HiTechWorld " , " HiTechCity " , " HiTechLab " } ; ArrayList < String > words = new ArrayList < String > ( Arrays . asList ( arr ) ) ; String pattern = " HT " ; CamelCase ( words , pattern ) ; } }

def CamelCase ( words , pattern ) : NEW_LINE INDENT map = dict . fromkeys ( words , None ) ; NEW_LINE for i in range ( len ( words ) ) : NEW_LINE INDENT string = " " ; NEW_LINE l = len ( words [ i ] ) ; NEW_LINE for j in range ( l ) : NEW_LINE INDENT if ( words [ i ] [ j ] >= ' A ' and words [ i ] [ j ] <= ' Z ' ) : NEW_LINE INDENT string += words [ i ] [ j ] ; NEW_LINE if string not in map : NEW_LINE INDENT map [ string ] = [ words [ i ] ] NEW_LINE DEDENT elif map [ string ] is None : NEW_LINE INDENT map [ string ] = [ words [ i ] ] NEW_LINE DEDENT else : NEW_LINE INDENT map [ string ] . append ( words [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT wordFound = False ; NEW_LINE for key , value in map . items ( ) : NEW_LINE INDENT if ( key == pattern ) : NEW_LINE INDENT wordFound = True ; NEW_LINE for itt in value : NEW_LINE INDENT print ( itt ) ; NEW_LINE DEDENT DEDENT DEDENT if ( not wordFound ) : NEW_LINE INDENT print ( " No ▁ match ▁ found " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT words = [ " Hi " , " Hello " , " HelloWorld " , " HiTech " , " HiGeek " , " HiTechWorld " , " HiTechCity " , " HiTechLab " ] ; NEW_LINE pattern = " HT " ; NEW_LINE CamelCase ( words , pattern ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int val ( char c ) { if ( c >= '0' && c <= '9' ) return ( int ) c - '0' ; else return ( int ) c - ' A ' + 10 ; } static int toDeci ( String str , int base ) { int len = str . length ( ) ; int power = 1 ; int num = 0 ; int i ; for ( i = len - 1 ; i >= 0 ; i -- ) { if ( val ( str . charAt ( i ) ) >= base ) { System . out . println ( " Invalid ▁ Number " ) ; return - 1 ; } num += val ( str . charAt ( i ) ) * power ; power = power * base ; } return num ; } public static void main ( String [ ] args ) { String str = "11A " ; int base = 16 ; System . out . println ( " Decimal ▁ equivalent ▁ of ▁ " + str + " ▁ in ▁ base ▁ " + base + " ▁ is ▁ " + " ▁ " + toDeci ( str , base ) ) ; } }

def val ( c ) : NEW_LINE INDENT if c >= '0' and c <= '9' : NEW_LINE INDENT return ord ( c ) - ord ( '0' ) NEW_LINE DEDENT else : NEW_LINE INDENT return ord ( c ) - ord ( ' A ' ) + 10 ; NEW_LINE DEDENT DEDENT def toDeci ( str , base ) : NEW_LINE INDENT llen = len ( str ) NEW_LINE power = 1 NEW_LINE num = 0 NEW_LINE for i in range ( llen - 1 , - 1 , - 1 ) : NEW_LINE INDENT if val ( str [ i ] ) >= base : NEW_LINE INDENT print ( ' Invalid ▁ Number ' ) NEW_LINE return - 1 NEW_LINE DEDENT num += val ( str [ i ] ) * power NEW_LINE power = power * base NEW_LINE DEDENT return num NEW_LINE DEDENT strr = "11A " NEW_LINE base = 16 NEW_LINE print ( ' Decimal ▁ equivalent ▁ of ' , strr , ' in ▁ base ' , base , ' is ' , toDeci ( strr , base ) ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static int sumOfDigitsFrom1ToN ( int n ) { if ( n < 10 ) return ( n * ( n + 1 ) / 2 ) ; int d = ( int ) ( Math . log10 ( n ) ) ; int a [ ] = new int [ d + 1 ] ; a [ 0 ] = 0 ; a [ 1 ] = 45 ; for ( int i = 2 ; i <= d ; i ++ ) a [ i ] = a [ i - 1 ] * 10 + 45 * ( int ) ( Math . ceil ( Math . pow ( 10 , i - 1 ) ) ) ; int p = ( int ) ( Math . ceil ( Math . pow ( 10 , d ) ) ) ; int msd = n / p ; return ( msd * a [ d ] + ( msd * ( msd - 1 ) / 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToN ( n % p ) ) ; } public static void main ( String args [ ] ) { int n = 328 ; System . out . println ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ " + " from ▁ 1 ▁ to ▁ " + n + " ▁ is ▁ " + sumOfDigitsFrom1ToN ( n ) ) ; } }

import math NEW_LINE def sumOfDigitsFrom1ToN ( n ) : NEW_LINE INDENT if ( n < 10 ) : NEW_LINE INDENT return ( n * ( n + 1 ) / 2 ) NEW_LINE DEDENT d = ( int ) ( math . log10 ( n ) ) NEW_LINE a = [ 0 ] * ( d + 1 ) NEW_LINE a [ 0 ] = 0 NEW_LINE a [ 1 ] = 45 NEW_LINE for i in range ( 2 , d + 1 ) : NEW_LINE INDENT a [ i ] = a [ i - 1 ] * 10 + 45 * ( int ) ( math . ceil ( math . pow ( 10 , i - 1 ) ) ) NEW_LINE DEDENT p = ( int ) ( math . ceil ( math . pow ( 10 , d ) ) ) NEW_LINE msd = n // p NEW_LINE return ( int ) ( msd * a [ d ] + ( msd * ( msd - 1 ) // 2 ) * p + msd * ( 1 + n % p ) + sumOfDigitsFrom1ToN ( n % p ) ) NEW_LINE DEDENT n = 328 NEW_LINE print ( " Sum ▁ of ▁ digits ▁ in ▁ numbers ▁ from ▁ 1 ▁ to " , n , " is " , sumOfDigitsFrom1ToN ( n ) ) NEW_LINE


class GFG { static boolean checkPairs ( int l , int r ) { if ( ( l - r ) % 2 == 0 ) return false ; return true ; } public static void main ( String [ ] args ) { int l = 1 , r = 8 ; if ( checkPairs ( l , r ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def checkPairs ( l , r ) : NEW_LINE INDENT if ( l - r ) % 2 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l , r = 1 , 8 NEW_LINE if checkPairs ( l , r ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


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

def squarearea ( a , b ) : NEW_LINE INDENT if ( a < 0 or b < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT area = 4 * ( ( ( pow ( a , 2 ) + pow ( b , 2 ) ) / ( pow ( a , 2 ) * pow ( b , 2 ) ) ) ) NEW_LINE return area NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 4 NEW_LINE b = 2 NEW_LINE print ( squarearea ( a , b ) ) NEW_LINE DEDENT


class GFG { static int findCountOfPairs ( int a , int b , int n ) { int ans = 0 ; for ( int i = 1 ; i <= a ; i ++ ) { ans += b / n ; ans += ( i % n + b % n ) >= n ? 1 : 0 ; } return ans ; } public static void main ( String [ ] args ) { int a = 5 , b = 13 , n = 3 ; System . out . println ( findCountOfPairs ( a , b , n ) ) ; } }

def findCountOfPairs ( a , b , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , a + 1 ) : NEW_LINE INDENT ans += b // n NEW_LINE ans += 1 if ( i % n + b % n ) >= n else 0 NEW_LINE DEDENT return ans NEW_LINE DEDENT a = 5 ; b = 13 ; n = 3 NEW_LINE print ( findCountOfPairs ( a , b , n ) ) NEW_LINE


class GFG { static int minCost ( int N , int P , int Q ) { int cost = 0 ; while ( N > 0 ) { if ( ( N & 1 ) > 0 ) { cost += P ; N -- ; } else { int temp = N / 2 ; if ( temp * P > Q ) cost += Q ; else cost += P * temp ; N /= 2 ; } } return cost ; } public static void main ( String [ ] args ) { int N = 9 , P = 5 , Q = 1 ; System . out . println ( minCost ( N , P , Q ) ) ; } }

def minCost ( N , P , Q ) : NEW_LINE INDENT cost = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT if ( N & 1 ) : NEW_LINE INDENT cost += P NEW_LINE N -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp = N // 2 ; NEW_LINE if ( temp * P > Q ) : NEW_LINE INDENT cost += Q NEW_LINE DEDENT else : NEW_LINE INDENT cost += P * temp NEW_LINE DEDENT N //= 2 NEW_LINE DEDENT DEDENT return cost NEW_LINE DEDENT N = 9 NEW_LINE P = 5 NEW_LINE Q = 1 NEW_LINE print ( minCost ( N , P , Q ) ) NEW_LINE


import java . util . * ; class GFG { static void descOrder ( char [ ] s ) { Arrays . sort ( s ) ; reverse ( s ) ; } static void reverse ( char [ ] a ) { int i , n = a . length ; char t ; for ( i = 0 ; i < n / 2 ; i ++ ) { t = a [ i ] ; a [ i ] = a [ n - i - 1 ] ; a [ n - i - 1 ] = t ; } } public static void main ( String [ ] args ) { char [ ] s = " geeksforgeeks " . toCharArray ( ) ; descOrder ( s ) ; System . out . println ( String . valueOf ( s ) ) ; } }

def descOrder ( s ) : NEW_LINE INDENT s . sort ( reverse = True ) NEW_LINE str1 = ' ' . join ( s ) NEW_LINE print ( str1 ) NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT s = list ( ' geeksforgeeks ' ) NEW_LINE descOrder ( s ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


class GFG { static int negProdSubArr ( int arr [ ] , int n ) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } static int posProdSubArr ( int arr [ ] , int n ) { int total = ( n * ( n + 1 ) ) / 2 ; int cntNeg = negProdSubArr ( arr , n ) ; return ( total - cntNeg ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 4 , - 3 , 2 , - 5 } ; int n = arr . length ; System . out . println ( posProdSubArr ( arr , n ) ) ; } }

def negProdSubArr ( arr , n ) : NEW_LINE INDENT positive = 1 NEW_LINE negative = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = - 1 NEW_LINE DEDENT if ( i > 0 ) : NEW_LINE INDENT arr [ i ] *= arr [ i - 1 ] NEW_LINE DEDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT positive += 1 NEW_LINE DEDENT else : NEW_LINE INDENT negative += 1 NEW_LINE DEDENT DEDENT return ( positive * negative ) NEW_LINE DEDENT def posProdSubArr ( arr , n ) : NEW_LINE INDENT total = ( n * ( n + 1 ) ) / 2 ; NEW_LINE cntNeg = negProdSubArr ( arr , n ) ; NEW_LINE return ( total - cntNeg ) ; NEW_LINE DEDENT arr = [ 5 , - 4 , - 3 , 2 , - 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( posProdSubArr ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static boolean isPrime ( int n ) { int i ; if ( n == 1 ) { return false ; } for ( i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { return false ; } } return true ; } static boolean check_frequency ( char [ ] s ) { HashMap < Character , Integer > m = new HashMap < Character , Integer > ( ) ; for ( int i = 0 ; i < s . length ; i ++ ) { if ( m . containsKey ( s [ i ] ) ) { m . put ( s [ i ] , m . get ( s [ i ] ) + 1 ) ; } else { m . put ( s [ i ] , 1 ) ; } } for ( char ch = ' a ' ; ch <= ' z ' ; ch ++ ) { if ( m . get ( ch ) != null && m . get ( ch ) > 0 && ! isPrime ( m . get ( ch ) ) ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; if ( check_frequency ( s . toCharArray ( ) ) ) { System . out . println ( " Yes " ) ; } else { System . out . println ( " No " ) ; } } }

import math as mt NEW_LINE def isPrime ( n ) : NEW_LINE INDENT i = 2 NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , mt . ceil ( mt . sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def check_frequency ( s ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if s [ i ] in m . keys ( ) : NEW_LINE INDENT m [ s [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ s [ i ] ] = 1 NEW_LINE DEDENT DEDENT for ch in m : NEW_LINE INDENT if m [ ch ] > 0 and isPrime ( m [ ch ] ) == False : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT s = " geeksforgeeks " NEW_LINE if ( check_frequency ( s ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static String printString ( int n , String str ) { String str2 = " " ; int extraChar = n % 26 ; if ( extraChar >= 1 ) { for ( int i = 26 - ( extraChar + 1 ) ; i <= 25 ; i ++ ) { str2 += str . charAt ( i ) ; } } int countOfStr = n / 26 ; for ( int i = 1 ; i <= countOfStr ; i ++ ) { for ( int j = 0 ; j < 26 ; j ++ ) { str2 += str . charAt ( j ) ; } } return str2 ; } public static void main ( String [ ] args ) { int n = 30 ; String str = " zyxwvutsrqponmlkjihgfedcba " ; System . out . println ( printString ( n , str ) ) ; } }

def printString ( n , str ) : NEW_LINE INDENT str2 = " " NEW_LINE extraChar = n % 26 NEW_LINE if ( extraChar >= 1 ) : NEW_LINE INDENT for i in range ( 26 - ( extraChar + 1 ) , 26 ) : NEW_LINE INDENT str2 += str [ i ] NEW_LINE DEDENT DEDENT countOfStr = n // 26 NEW_LINE for i in range ( 1 , countOfStr + 1 ) : NEW_LINE INDENT for j in range ( 26 ) : NEW_LINE INDENT str2 += str [ j ] NEW_LINE DEDENT DEDENT return str2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 30 NEW_LINE str = " zyxwvutsrqponmlkjihgfedcba " NEW_LINE print ( printString ( n , str ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static int findY ( int x ) { if ( x > 2 ) return x - 2 ; return x + 2 ; } public static void main ( String [ ] args ) { int x = 5 ; System . out . println ( findY ( x ) ) ; } }

def findY ( x ) : NEW_LINE INDENT if ( x > 2 ) : NEW_LINE INDENT return x - 2 NEW_LINE DEDENT return x + 2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 5 NEW_LINE print ( findY ( x ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class data { int element ; int position ; } ; static data findMinElement ( int arr [ ] , int n ) { data result = new data ( ) ; int [ ] prefixSum = new int [ n ] ; int [ ] suffixSum = new int [ n ] ; prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; } suffixSum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) { suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] ; } int min = suffixSum [ 0 ] ; int pos = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( Math . abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < min ) { min = Math . abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) ; if ( suffixSum [ i + 1 ] < prefixSum [ i ] ) pos = i + 1 ; else pos = i ; } } result . element = min ; result . position = pos ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 1 , 2 , 3 , 4 } ; int n = arr . length ; data values ; values = findMinElement ( arr , n ) ; System . out . println ( " Minimum ▁ element ▁ : ▁ " + values . element + " \n Position ▁ : ▁ " + values . position ) ; } }

class Data : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . element = - 1 NEW_LINE self . position = - 1 NEW_LINE DEDENT DEDENT def findMinElement ( arr , n ) : NEW_LINE INDENT result = Data ( ) NEW_LINE prefixSum = [ 0 ] * n NEW_LINE suffixSum = [ 0 ] * n NEW_LINE prefixSum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] \ NEW_LINE DEDENT suffixSum [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] NEW_LINE DEDENT mini = suffixSum [ 0 ] NEW_LINE pos = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) < mini : NEW_LINE INDENT mini = abs ( suffixSum [ i + 1 ] - prefixSum [ i ] ) NEW_LINE if suffixSum [ i + 1 ] < prefixSum [ i ] : NEW_LINE INDENT pos = i + 1 NEW_LINE DEDENT else : NEW_LINE INDENT pos = i NEW_LINE DEDENT DEDENT DEDENT result . element = mini NEW_LINE result . position = pos NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE values = Data ( ) NEW_LINE values = findMinElement ( arr , n ) NEW_LINE print ( " Minimum ▁ element ▁ : " , values . element , " \n Position ▁ : " , values . position ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int [ ] orgazineInOrder ( int [ ] vec , int [ ] op , int n ) { int [ ] result = new int [ n ] ; Arrays . sort ( vec ) ; int i = 0 , j = n - 1 , k = 0 ; while ( i <= j && k <= n - 2 ) { if ( op [ k ] == ' < ' ) { result [ k ] = vec [ i ++ ] ; } else { result [ k ] = vec [ j -- ] ; } k ++ ; } result [ n - 1 ] = vec [ i ] ; return result ; } public static void main ( String [ ] args ) { int [ ] vec = { 8 , 2 , 7 , 1 , 5 , 9 } ; int [ ] op = { ' > ' , ' > ' , ' < ' , ' > ' , ' < ' } ; int [ ] result = orgazineInOrder ( vec , op , vec . length ) ; for ( int i = 0 ; i < result . length ; i ++ ) { System . out . print ( result [ i ] + " ▁ " ) ; } } }

def orgazineInOrder ( vec , op , n ) : NEW_LINE INDENT result = [ 0 ] * n ; NEW_LINE vec . sort ( ) ; NEW_LINE i = 0 ; NEW_LINE j = n - 1 ; NEW_LINE k = 0 ; NEW_LINE while ( i <= j and k <= n - 2 ) : NEW_LINE INDENT if ( op [ k ] == ' < ' ) : NEW_LINE INDENT result [ k ] = vec [ i ] ; NEW_LINE i += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT result [ k ] = vec [ j ] ; NEW_LINE j -= 1 ; NEW_LINE DEDENT k += 1 ; NEW_LINE DEDENT result [ n - 1 ] = vec [ i ] ; NEW_LINE return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT vec = [ 8 , 2 , 7 , 1 , 5 , 9 ] ; NEW_LINE op = [ ' > ' , ' > ' , ' < ' , ' > ' , ' < ' ] ; NEW_LINE result = orgazineInOrder ( vec , op , len ( vec ) ) ; NEW_LINE for i in range ( len ( result ) ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT


class GFG { static int sumOfTheSeries ( int n ) { int sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int k = 2 ; for ( int j = 1 ; j <= i ; j ++ ) { sum += k ; k += 2 ; } } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . printf ( " Sum ▁ = ▁ % d " , sumOfTheSeries ( n ) ) ; } }

def sumOfTheSeries ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n + 1 ) : NEW_LINE INDENT k = 2 NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT sum = sum + k ; NEW_LINE k = k + 2 NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT n = 5 NEW_LINE ans = sumOfTheSeries ( n ) ; NEW_LINE print ( ans ) NEW_LINE


import java . io . * ; class GFG { static int n = 5 ; static void printSumTricky ( int mat [ ] [ ] , int k ) { if ( k > n ) return ; int stripSum [ ] [ ] = new int [ n ] [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { int sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) sum += mat [ i ] [ j ] ; stripSum [ 0 ] [ j ] = sum ; for ( int i = 1 ; i < n - k + 1 ; i ++ ) { sum += ( mat [ i + k - 1 ] [ j ] - mat [ i - 1 ] [ j ] ) ; stripSum [ i ] [ j ] = sum ; } } for ( int i = 0 ; i < n - k + 1 ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < k ; j ++ ) sum += stripSum [ i ] [ j ] ; System . out . print ( sum + " ▁ " ) ; for ( int j = 1 ; j < n - k + 1 ; j ++ ) { sum += ( stripSum [ i ] [ j + k - 1 ] - stripSum [ i ] [ j - 1 ] ) ; System . out . print ( sum + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 , 4 } , { 5 , 5 , 5 , 5 , 5 } , } ; int k = 3 ; printSumTricky ( mat , k ) ; } }

def printSumTricky ( mat , k ) : NEW_LINE INDENT global n NEW_LINE if k > n : NEW_LINE INDENT return NEW_LINE DEDENT stripSum = [ [ None ] * n for i in range ( n ) ] NEW_LINE for j in range ( n ) : NEW_LINE INDENT Sum = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT Sum += mat [ i ] [ j ] NEW_LINE DEDENT stripSum [ 0 ] [ j ] = Sum NEW_LINE for i in range ( 1 , n - k + 1 ) : NEW_LINE INDENT Sum += ( mat [ i + k - 1 ] [ j ] - mat [ i - 1 ] [ j ] ) NEW_LINE stripSum [ i ] [ j ] = Sum NEW_LINE DEDENT DEDENT for i in range ( n - k + 1 ) : NEW_LINE INDENT Sum = 0 NEW_LINE for j in range ( k ) : NEW_LINE INDENT Sum += stripSum [ i ] [ j ] NEW_LINE DEDENT print ( Sum , end = " ▁ " ) NEW_LINE for j in range ( 1 , n - k + 1 ) : NEW_LINE INDENT Sum += ( stripSum [ i ] [ j + k - 1 ] - stripSum [ i ] [ j - 1 ] ) NEW_LINE print ( Sum , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE mat = [ [ 1 , 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 , 4 ] , [ 5 , 5 , 5 , 5 , 5 ] ] NEW_LINE k = 3 NEW_LINE printSumTricky ( mat , k ) NEW_LINE


import java . util . * ; class GFG { static int min ( int a , int b ) { return ( a > b ) ? b : a ; } static int minCostJumpsDP ( int [ ] A , int k ) { int size = A . length ; int [ ] x = new int [ size ] ; Arrays . fill ( x , Integer . MAX_VALUE ) ; x [ 0 ] = 0 ; for ( int i = 0 ; i < size ; i ++ ) { for ( int j = i + 1 ; j < i + k + 1 && j < size ; j ++ ) { x [ j ] = min ( x [ j ] , x [ i ] + Math . abs ( A [ i ] - A [ j ] ) ) ; } } return x [ size - 1 ] ; } public static void main ( String [ ] args ) { int [ ] input = { 83 , 26 , 37 , 35 , 33 , 35 , 56 } ; System . out . println ( minCostJumpsDP ( input , 3 ) ) ; } }

import sys NEW_LINE def minCostJumpsDP ( A , k ) : NEW_LINE INDENT size = len ( A ) NEW_LINE x = [ sys . maxsize ] * ( size ) NEW_LINE x [ 0 ] = 0 NEW_LINE for i in range ( size ) : NEW_LINE INDENT j = i + 1 NEW_LINE while j < i + k + 1 and j < size : NEW_LINE INDENT x [ j ] = min ( x [ j ] , x [ i ] + abs ( A [ i ] - A [ j ] ) ) NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return x [ size - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input_ = [ 83 , 26 , 37 , 35 , 33 , 35 , 56 ] NEW_LINE print ( minCostJumpsDP ( input_ , 3 ) ) NEW_LINE DEDENT


class GFG { static int N = 4 ; static int stepsRequired ( int arr [ ] [ ] ) { int result = 0 ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( i > j + 1 ) result += Math . abs ( arr [ i ] [ j ] ) ; } } return result ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 , 3 , 4 } , { 3 , 1 , 0 , 3 } , { 3 , 2 , 1 , 3 } , { - 3 , 4 , 2 , 1 } } ; System . out . println ( stepsRequired ( arr ) ) ; } }

N = 4 ; NEW_LINE def stepsRequired ( arr ) : NEW_LINE INDENT result = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( i > j + 1 ) : NEW_LINE INDENT result += abs ( arr [ i ] [ j ] ) ; NEW_LINE DEDENT DEDENT DEDENT return result ; NEW_LINE DEDENT arr = [ [ 1 , 2 , 3 , 4 ] , [ 3 , 1 , 0 , 3 ] , [ 3 , 2 , 1 , 3 ] , [ - 3 , 4 , 2 , 1 ] ] ; NEW_LINE print ( stepsRequired ( arr ) ) ; NEW_LINE


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

def totalBoxesRemoved ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE prev = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] > prev ) : NEW_LINE INDENT count += ( arr [ i ] - prev ) NEW_LINE arr [ i ] = prev NEW_LINE prev = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT prev = arr [ i ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 5 , 4 , 7 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( totalBoxesRemoved ( arr , n ) ) NEW_LINE