import java . util . * ; class GFG { static int mod = 1000000007 ; static long power ( long a , long n ) { if ( n == 0 ) { return 1 ; } long p = power ( a , n / 2 ) % mod ; p = ( p * p ) % mod ; if ( n == 1 ) { p = ( p * a ) % mod ; } return p ; } static long computeSum ( int [ ] arr , int n ) { Arrays . sort ( arr ) ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { long maxs = ( power ( 2 , i ) - 1 + mod ) % mod ; maxs = ( maxs * arr [ i ] ) % mod ; long mins = ( power ( 2 , n - 1 - i ) - 1 + mod ) % mod ; mins = ( mins * arr [ i ] ) % mod ; long V = ( maxs - mins + mod ) % mod ; sum = ( sum + V ) % mod ; } return sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 3 , 1 } ; int n = arr . length ; System . out . println ( computeSum ( arr , n ) ) ; } }

def power ( a , n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT p = power ( a , n // 2 ) % mod NEW_LINE p = ( p * p ) % mod NEW_LINE if n & 1 == 1 : NEW_LINE INDENT p = ( p * a ) % mod NEW_LINE DEDENT return p NEW_LINE DEDENT def computeSum ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE Sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT maxs = ( power ( 2 , i ) - 1 + mod ) % mod NEW_LINE maxs = ( maxs * arr [ i ] ) % mod NEW_LINE mins = ( power ( 2 , n - 1 - i ) - 1 + mod ) % mod NEW_LINE mins = ( mins * arr [ i ] ) % mod NEW_LINE V = ( maxs - mins + mod ) % mod NEW_LINE Sum = ( Sum + V ) % mod NEW_LINE DEDENT return Sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mod = 1000000007 NEW_LINE arr = [ 4 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( computeSum ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; public class GFG { static int check ( int A [ ] , int B [ ] , int N ) { PriorityQueue < Integer > pq1 = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; PriorityQueue < Integer > pq2 = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; for ( int i = 0 ; i < N ; i ++ ) { pq1 . add ( A [ i ] ) ; } for ( int i = 0 ; i < N ; i ++ ) { pq2 . add ( B [ i ] ) ; } int c = 0 ; for ( int i = 0 ; i < N ; i ++ ) { if ( pq1 . peek ( ) . compareTo ( pq2 . peek ( ) ) == 1 ) { c ++ ; pq1 . poll ( ) ; pq2 . poll ( ) ; } else { if ( pq2 . size ( ) == 0 ) { break ; } pq2 . poll ( ) ; } } return ( c ) ; } public static void main ( String args [ ] ) { int A [ ] = { 10 , 3 , 7 , 5 , 8 } ; int B [ ] = { 8 , 6 , 2 , 5 , 9 } ; int N = A . length ; System . out . println ( check ( A , B , N ) ) ; } }

import heapq NEW_LINE def check ( A , B , N ) : NEW_LINE INDENT pq1 = [ ] NEW_LINE pq2 = [ ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT heapq . heappush ( pq1 , - A [ i ] ) NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT heapq . heappush ( pq2 , - B [ i ] ) NEW_LINE DEDENT c = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if - pq1 [ 0 ] > - pq2 [ 0 ] : NEW_LINE INDENT c += 1 NEW_LINE heapq . heappop ( pq1 ) NEW_LINE heapq . heappop ( pq2 ) NEW_LINE DEDENT else : NEW_LINE INDENT if len ( pq2 ) == 0 : NEW_LINE INDENT break NEW_LINE DEDENT heapq . heappop ( pq2 ) NEW_LINE DEDENT DEDENT return ( c ) NEW_LINE DEDENT A = [ 10 , 3 , 7 , 5 , 8 ] NEW_LINE B = [ 8 , 6 , 2 , 5 , 9 ] NEW_LINE N = len ( A ) NEW_LINE print ( check ( A , B , N ) ) NEW_LINE


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

import math NEW_LINE def numberofterm ( n , number ) : NEW_LINE INDENT firstnum = math . pow ( 10 , n - 1 ) NEW_LINE lastnum = math . pow ( 10 , n ) NEW_LINE count = 0 NEW_LINE for i in range ( int ( firstnum ) , int ( lastnum ) ) : NEW_LINE INDENT if ( i % number == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT n = 3 NEW_LINE num = 7 NEW_LINE print ( numberofterm ( n , num ) ) NEW_LINE


class GFG { static boolean canConvert ( int a , int b ) { while ( b > a ) { if ( b % 10 == 1 ) { b /= 10 ; continue ; } if ( b % 2 == 0 ) { b /= 2 ; continue ; } return false ; } if ( b == a ) return true ; return false ; } public static void main ( String [ ] args ) { int A = 2 , B = 82 ; if ( canConvert ( A , B ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def canConvert ( a , b ) : NEW_LINE INDENT while ( b > a ) : NEW_LINE INDENT if ( b % 10 == 1 ) : NEW_LINE INDENT b //= 10 ; NEW_LINE continue ; NEW_LINE DEDENT if ( b % 2 == 0 ) : NEW_LINE INDENT b /= 2 ; NEW_LINE continue ; NEW_LINE DEDENT return false ; NEW_LINE DEDENT if ( b == a ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 2 ; B = 82 ; NEW_LINE if ( canConvert ( A , B ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


class GFG { static int highestPower ( int n , int k ) { int i = 0 ; int a = ( int ) Math . pow ( n , i ) ; while ( a <= k ) { i += 1 ; a = ( int ) Math . pow ( n , i ) ; } return i - 1 ; } static int b [ ] = new int [ 50 ] ; static int PowerArray ( int n , int k ) { while ( k > 0 ) { int t = highestPower ( n , k ) ; if ( b [ t ] > 0 ) { System . out . print ( - 1 ) ; return 0 ; } else b [ t ] = 1 ; k -= Math . pow ( n , t ) ; } for ( int i = 0 ; i < 50 ; i ++ ) { if ( b [ i ] > 0 ) { System . out . print ( i + " , ▁ " ) ; } } return 0 ; } public static void main ( String [ ] args ) { int N = 3 ; int K = 40 ; PowerArray ( N , K ) ; } }

from math import pow NEW_LINE def highestPower ( n , k ) : NEW_LINE INDENT i = 0 NEW_LINE a = pow ( n , i ) NEW_LINE while ( a <= k ) : NEW_LINE INDENT i += 1 NEW_LINE a = pow ( n , i ) NEW_LINE DEDENT return i - 1 NEW_LINE DEDENT b = [ 0 for i in range ( 50 ) ] NEW_LINE def PowerArray ( n , k ) : NEW_LINE INDENT while ( k ) : NEW_LINE INDENT t = highestPower ( n , k ) NEW_LINE if ( b [ t ] ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return 0 NEW_LINE DEDENT else : NEW_LINE INDENT b [ t ] = 1 NEW_LINE DEDENT k -= pow ( n , t ) NEW_LINE DEDENT for i in range ( 50 ) : NEW_LINE INDENT if ( b [ i ] ) : NEW_LINE INDENT print ( i , end = ' , ▁ ' ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 3 NEW_LINE K = 40 NEW_LINE PowerArray ( N , K ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void findMaxGuests ( int arrl [ ] , int exit [ ] , int n ) { Arrays . sort ( arrl ) ; Arrays . sort ( exit ) ; int guests_in = 1 , max_guests = 1 , time = arrl [ 0 ] ; int i = 1 , j = 0 ; while ( i < n && j < n ) { if ( arrl [ i ] <= exit [ j ] ) { guests_in ++ ; if ( guests_in > max_guests ) { max_guests = guests_in ; time = arrl [ i ] ; } i ++ ; } else { guests_in -- ; j ++ ; } } System . out . println ( " Maximum ▁ Number ▁ of ▁ Guests ▁ = ▁ " + max_guests + " ▁ at ▁ time ▁ " + time ) ; } public static void main ( String [ ] args ) { int arrl [ ] = { 1 , 2 , 10 , 5 , 5 } ; int exit [ ] = { 4 , 5 , 12 , 9 , 12 } ; int n = arrl . length ; findMaxGuests ( arrl , exit , n ) ; } }

def findMaxGuests ( arrl , exit , n ) : NEW_LINE INDENT arrl . sort ( ) ; NEW_LINE exit . sort ( ) ; NEW_LINE guests_in = 1 ; NEW_LINE max_guests = 1 ; NEW_LINE time = arrl [ 0 ] ; NEW_LINE i = 1 ; NEW_LINE j = 0 ; NEW_LINE while ( i < n and j < n ) : NEW_LINE INDENT if ( arrl [ i ] <= exit [ j ] ) : NEW_LINE INDENT guests_in = guests_in + 1 ; NEW_LINE if ( guests_in > max_guests ) : NEW_LINE INDENT max_guests = guests_in ; NEW_LINE time = arrl [ i ] ; NEW_LINE DEDENT i = i + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT guests_in = guests_in - 1 ; NEW_LINE j = j + 1 ; NEW_LINE DEDENT DEDENT print ( " Maximum ▁ Number ▁ of ▁ Guests ▁ = " , max_guests , " at ▁ time " , time ) NEW_LINE DEDENT arrl = [ 1 , 2 , 10 , 5 , 5 ] ; NEW_LINE exit = [ 4 , 5 , 12 , 9 , 12 ] ; NEW_LINE n = len ( arrl ) ; NEW_LINE findMaxGuests ( arrl , exit , n ) ; NEW_LINE


class GFG { static long spellsCount ( String num ) { int n = num . length ( ) ; long result = 1 ; for ( int i = 0 ; i < n ; i ++ ) { int count = 1 ; while ( i < n - 1 && num . charAt ( i + 1 ) == num . charAt ( i ) ) { count ++ ; i ++ ; } result = result * ( long ) Math . pow ( 2 , count - 1 ) ; } return result ; } public static void main ( String [ ] args ) { String num = "11112" ; System . out . print ( spellsCount ( num ) ) ; } }

def spellsCount ( num ) : NEW_LINE INDENT n = len ( num ) ; NEW_LINE result = 1 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT count = 1 ; NEW_LINE while ( i < n - 1 and num [ i + 1 ] == num [ i ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT result = result * int ( pow ( 2 , count - 1 ) ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT num = "11112" ; NEW_LINE print ( spellsCount ( num ) ) ; NEW_LINE


public class Longest_common_prefix { static final int ALPHABET_SIZE = 26 ; static class TrieNode { TrieNode [ ] children = new TrieNode [ ALPHABET_SIZE ] ; boolean isLeaf ; public TrieNode ( ) { isLeaf = false ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) children [ i ] = null ; } } ; static TrieNode root ; static int indexs ; static void insert ( String key ) { int length = key . length ( ) ; int index ; TrieNode pCrawl = root ; for ( int level = 0 ; level < length ; level ++ ) { index = key . charAt ( level ) - ' a ' ; if ( pCrawl . children [ index ] == null ) pCrawl . children [ index ] = new TrieNode ( ) ; pCrawl = pCrawl . children [ index ] ; } pCrawl . isLeaf = true ; } static int countChildren ( TrieNode node ) { int count = 0 ; for ( int i = 0 ; i < ALPHABET_SIZE ; i ++ ) { if ( node . children [ i ] != null ) { count ++ ; indexs = i ; } } return ( count ) ; } static String walkTrie ( ) { TrieNode pCrawl = root ; indexs = 0 ; String prefix = " " ; while ( countChildren ( pCrawl ) == 1 && pCrawl . isLeaf == false ) { pCrawl = pCrawl . children [ indexs ] ; prefix += ( char ) ( ' a ' + indexs ) ; } return prefix ; } static void constructTrie ( String arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) insert ( arr [ i ] ) ; return ; } static String commonPrefix ( String arr [ ] , int n ) { root = new TrieNode ( ) ; constructTrie ( arr , n ) ; return walkTrie ( ) ; } public static void main ( String args [ ] ) { String arr [ ] = { " geeksforgeeks " , " geeks " , " geek " , " geezer " } ; int n = arr . length ; String ans = commonPrefix ( arr , n ) ; if ( ans . length ( ) != 0 ) System . out . println ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " + ans ) ; else System . out . println ( " There ▁ is ▁ no ▁ common ▁ prefix " ) ; } }

ALPHABET_SIZE = 26 NEW_LINE indexs = 0 NEW_LINE class TrieNode : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . isLeaf = False NEW_LINE self . children = [ None ] * ALPHABET_SIZE NEW_LINE DEDENT DEDENT def insert ( key , root ) : NEW_LINE INDENT pCrawl = root NEW_LINE for level in range ( len ( key ) ) : NEW_LINE INDENT index = ord ( key [ level ] ) - ord ( ' a ' ) NEW_LINE if pCrawl . children [ index ] == None : NEW_LINE INDENT pCrawl . children [ index ] = TrieNode ( ) NEW_LINE DEDENT pCrawl = pCrawl . children [ index ] NEW_LINE DEDENT pCrawl . isLeaf = True NEW_LINE DEDENT def constructTrie ( arr , n , root ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT insert ( arr [ i ] , root ) NEW_LINE DEDENT DEDENT def countChildren ( node ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( ALPHABET_SIZE ) : NEW_LINE INDENT if node . children [ i ] != None : NEW_LINE INDENT count += 1 NEW_LINE global indexs NEW_LINE indexs = i NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def walkTrie ( root ) : NEW_LINE INDENT pCrawl = root NEW_LINE prefix = " " NEW_LINE while ( countChildren ( pCrawl ) == 1 and pCrawl . isLeaf == False ) : NEW_LINE INDENT pCrawl = pCrawl . children [ indexs ] NEW_LINE prefix += chr ( 97 + indexs ) NEW_LINE DEDENT return prefix or - 1 NEW_LINE DEDENT def commonPrefix ( arr , n , root ) : NEW_LINE INDENT constructTrie ( arr , n , root ) NEW_LINE return walkTrie ( root ) NEW_LINE DEDENT n = 4 NEW_LINE arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ] NEW_LINE root = TrieNode ( ) NEW_LINE print ( commonPrefix ( arr , n , root ) ) NEW_LINE


class GFG { static int maxAND ( int L , int R ) { int maximum = L & R ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = Math . max ( maximum , ( i & j ) ) ; return maximum ; } public static void main ( String [ ] args ) { int L = 1 , R = 632 ; System . out . println ( maxAND ( L , R ) ) ; } }

def maxAND ( L , R ) : NEW_LINE INDENT maximum = L & R NEW_LINE for i in range ( L , R , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , R + 1 , 1 ) : NEW_LINE INDENT maximum = max ( maximum , ( i & j ) ) NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT L = 1 NEW_LINE R = 632 NEW_LINE print ( maxAND ( L , R ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int countSubArrays ( int arr [ ] , int n , int x ) { int len = 0 ; int count = 0 ; int binaryArr [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] <= x ) binaryArr [ i ] = 1 ; else binaryArr [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { if ( binaryArr [ i ] == 1 ) { int j ; for ( j = i + 1 ; j < n ; j ++ ) if ( binaryArr [ j ] != 1 ) break ; len = j - i ; count += ( len ) * ( len + 1 ) / 2 ; i = j ; } } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 7 , 8 , 2 , 3 , 9 } ; int x = 6 ; int n = arr . length ; System . out . println ( countSubArrays ( arr , n , x ) ) ; } }

def countSubArrays ( arr , n , x ) : NEW_LINE INDENT len = 0 NEW_LINE count = 0 NEW_LINE binaryArr = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] <= x ) : NEW_LINE INDENT binaryArr [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT binaryArr [ i ] = 0 NEW_LINE DEDENT DEDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( binaryArr [ i ] == 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT if ( binaryArr [ j ] != 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT len = j - i NEW_LINE count += ( len ) * ( int ) ( ( len + 1 ) / 2 ) NEW_LINE i = j NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 5 , 7 , 8 , 2 , 3 , 9 ] NEW_LINE x = 6 NEW_LINE n = len ( arr ) NEW_LINE print ( int ( countSubArrays ( arr , n , x ) ) ) NEW_LINE DEDENT


class GFG { static void alphabetPattern ( int N ) { int left = 0 , middle = N - 1 , right = N + 1 ; for ( int row = 0 ; row < 2 * N - 1 ; row ++ ) { if ( row < N ) System . out . print ( ++ left ) ; else System . out . print ( -- left ) ; for ( int col = 1 ; col < N - 1 ; col ++ ) { if ( row != N - 1 ) System . out . print ( " ▁ ▁ " ) ; else System . out . print ( " ▁ " + middle -- ) ; } if ( row < N ) System . out . print ( " ▁ " + -- right ) ; else System . out . print ( " ▁ " + ++ right ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int N = 4 ; alphabetPattern ( N ) ; } }

def alphabetPattern ( N ) : NEW_LINE INDENT left , middle , right = 0 , N - 1 , N + 1 NEW_LINE for row in range ( 0 , 2 * N - 1 ) : NEW_LINE INDENT if row < N : NEW_LINE INDENT left += 1 NEW_LINE print ( left , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT left -= 1 NEW_LINE print ( left , end = " " ) NEW_LINE DEDENT for col in range ( 1 , N - 1 ) : NEW_LINE INDENT if row != N - 1 : NEW_LINE INDENT print ( " ▁ " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ " + str ( middle ) , end = " " ) NEW_LINE middle -= 1 NEW_LINE DEDENT DEDENT if row < N : NEW_LINE INDENT right -= 1 NEW_LINE print ( " ▁ " + str ( right ) , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT right += 1 NEW_LINE print ( " ▁ " + str ( right ) , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE alphabetPattern ( N ) NEW_LINE DEDENT


class GFG { static int MAX = 64 ; static int maxOR ( int L , int R ) { if ( L == R ) { return L ; } int ans = 0 ; for ( int i = MAX - 1 ; i >= 0 ; i -- ) { int p , lbit , rbit ; p = 1 << i ; lbit = ( L >> i ) & 1 ; rbit = ( R >> i ) & 1 ; if ( ( rbit == 1 ) && ( lbit == 0 ) ) { ans += ( p << 1 ) - 1 ; break ; } if ( rbit == 1 ) { ans += p ; } } return ans ; } public static void main ( String [ ] args ) { int L = 4 , R = 5 ; System . out . println ( maxOR ( L , R ) ) ; } }

MAX = 64 ; NEW_LINE def maxOR ( L , R ) : NEW_LINE INDENT if ( L == R ) : NEW_LINE INDENT return L ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( MAX - 1 , - 1 , - 1 ) : NEW_LINE INDENT p = 1 << i ; NEW_LINE lbit = ( L >> i ) & 1 ; NEW_LINE rbit = ( R >> i ) & 1 ; NEW_LINE if ( ( rbit == 1 ) and ( lbit == 0 ) ) : NEW_LINE INDENT ans += ( p << 1 ) - 1 ; NEW_LINE break ; NEW_LINE DEDENT if ( rbit == 1 ) : NEW_LINE INDENT ans += p ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 4 ; R = 5 ; NEW_LINE print ( maxOR ( L , R ) ) ; NEW_LINE DEDENT


class GFG { static int submatrixXor ( int [ ] [ ] arr ) { int n = 3 ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { int top_left = ( i + 1 ) * ( j + 1 ) ; int bottom_right = ( n - i ) * ( n - j ) ; if ( ( top_left % 2 == 1 ) && ( bottom_right % 2 == 1 ) ) ans = ( ans ^ arr [ i ] [ j ] ) ; } } return ans ; } public static void main ( String [ ] args ) { int [ ] [ ] arr = { { 6 , 7 , 13 } , { 8 , 3 , 4 } , { 9 , 7 , 6 } } ; System . out . println ( submatrixXor ( arr ) ) ; } }

def submatrixXor ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT top_left = ( i + 1 ) * ( j + 1 ) NEW_LINE bottom_right = ( n - i ) * ( n - j ) NEW_LINE if ( top_left % 2 == 1 and bottom_right % 2 == 1 ) : NEW_LINE INDENT ans = ( ans ^ arr [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT n = 3 NEW_LINE arr = [ [ 6 , 7 , 13 ] , [ 8 , 3 , 4 ] , [ 9 , 7 , 6 ] ] NEW_LINE print ( submatrixXor ( arr , n ) ) NEW_LINE


public class GFG { static int count ( String s , int k ) { int n = s . length ( ) ; int d = 0 , i ; int count = 0 ; for ( i = 0 ; i < n ; i ++ ) d += s . charAt ( i ) ; if ( d % k == 0 ) count += 1 ; for ( i = k ; i < n ; i ++ ) { int prev = s . charAt ( i - k ) ; d -= prev ; d += s . charAt ( i ) ; if ( d % k == 0 ) count += 1 ; } return count ; } public static void main ( String [ ] args ) { String s = " bcgabc " ; int k = 3 ; int ans = count ( s , k ) ; System . out . println ( ans ) ; } }

def count ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE d , count = 0 , 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT d += ord ( s [ i ] ) NEW_LINE if ( d % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT prev = ord ( s [ i - k ] ) NEW_LINE d -= prev NEW_LINE d += ord ( s [ i ] ) NEW_LINE if ( d % k == 0 ) : NEW_LINE INDENT count += 1 NEW_LINE return count NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT s = " bcgabc " NEW_LINE k = 3 NEW_LINE ans = count ( s , k ) NEW_LINE print ( ans ) NEW_LINE


public class MatrixTraversal { private static void printMatrix ( int mat [ ] [ ] , int m , int n , int i , int j , int path [ ] , int idx ) { path [ idx ] = mat [ i ] [ j ] ; if ( i == m - 1 ) { for ( int k = j + 1 ; k < n ; k ++ ) { path [ idx + k - j ] = mat [ i ] [ k ] ; } for ( int l = 0 ; l < idx + n - j ; l ++ ) { System . out . print ( path [ l ] + " ▁ " ) ; } System . out . println ( ) ; return ; } if ( j == n - 1 ) { for ( int k = i + 1 ; k < m ; k ++ ) { path [ idx + k - i ] = mat [ k ] [ j ] ; } for ( int l = 0 ; l < idx + m - i ; l ++ ) { System . out . print ( path [ l ] + " ▁ " ) ; } System . out . println ( ) ; return ; } printMatrix ( mat , m , n , i + 1 , j , path , idx + 1 ) ; printMatrix ( mat , m , n , i , j + 1 , path , idx + 1 ) ; } public static void main ( String [ ] args ) { int m = 2 ; int n = 3 ; int mat [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } } ; int maxLengthOfPath = m + n - 1 ; printMatrix ( mat , m , n , 0 , 0 , new int [ maxLengthOfPath ] , 0 ) ; } }

def printAllPathsUtil ( mat , i , j , m , n , path , pi ) : NEW_LINE INDENT if ( i == m - 1 ) : NEW_LINE INDENT for k in range ( j , n ) : NEW_LINE INDENT path [ pi + k - j ] = mat [ i ] [ k ] NEW_LINE DEDENT for l in range ( pi + n - j ) : NEW_LINE INDENT print ( path [ l ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE return NEW_LINE DEDENT if ( j == n - 1 ) : NEW_LINE INDENT for k in range ( i , m ) : NEW_LINE INDENT path [ pi + k - i ] = mat [ k ] [ j ] NEW_LINE DEDENT for l in range ( pi + m - i ) : NEW_LINE INDENT print ( path [ l ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE return NEW_LINE DEDENT path [ pi ] = mat [ i ] [ j ] NEW_LINE printAllPathsUtil ( mat , i + 1 , j , m , n , path , pi + 1 ) NEW_LINE printAllPathsUtil ( mat , i , j + 1 , m , n , path , pi + 1 ) NEW_LINE DEDENT def printAllPaths ( mat , m , n ) : NEW_LINE INDENT path = [ 0 for i in range ( m + n ) ] NEW_LINE printAllPathsUtil ( mat , 0 , 0 , m , n , path , 0 ) NEW_LINE DEDENT mat = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] ] NEW_LINE printAllPaths ( mat , 2 , 3 ) NEW_LINE


import java . util . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void FillPrefixSuffix ( int prefix [ ] , int arr [ ] , int suffix [ ] , int n ) { prefix [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefix [ i ] = GCD ( prefix [ i - 1 ] , arr [ i ] ) ; suffix [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffix [ i ] = GCD ( suffix [ i + 1 ] , arr [ i ] ) ; } static int GCDoutsideRange ( int l , int r , int prefix [ ] , int suffix [ ] , int n ) { if ( l == 0 ) return suffix [ r + 1 ] ; if ( r == n - 1 ) return prefix [ l - 1 ] ; return GCD ( prefix [ l - 1 ] , suffix [ r + 1 ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 6 , 9 } ; int n = arr . length ; int prefix [ ] = new int [ n ] ; int suffix [ ] = new int [ n ] ; FillPrefixSuffix ( prefix , arr , suffix , n ) ; int l = 0 , r = 0 ; System . out . println ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; l = 1 ; r = 1 ; System . out . println ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; l = 1 ; r = 2 ; System . out . println ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) ; } }

def GCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return GCD ( b , a % b ) NEW_LINE DEDENT def FillPrefixSuffix ( prefix , arr , suffix , n ) : NEW_LINE INDENT prefix [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefix [ i ] = GCD ( prefix [ i - 1 ] , arr [ i ] ) NEW_LINE DEDENT suffix [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffix [ i ] = GCD ( suffix [ i + 1 ] , arr [ i ] ) NEW_LINE DEDENT DEDENT def GCDoutsideRange ( l , r , prefix , suffix , n ) : NEW_LINE INDENT if ( l == 0 ) : NEW_LINE INDENT return suffix [ r + 1 ] NEW_LINE DEDENT if ( r == n - 1 ) : NEW_LINE INDENT return prefix [ l - 1 ] NEW_LINE DEDENT return GCD ( prefix [ l - 1 ] , suffix [ r + 1 ] ) NEW_LINE DEDENT arr = [ 2 , 6 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE prefix = [ ] NEW_LINE suffix = [ ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT prefix . append ( 0 ) NEW_LINE suffix . append ( 0 ) NEW_LINE DEDENT FillPrefixSuffix ( prefix , arr , suffix , n ) NEW_LINE l = 0 NEW_LINE r = 0 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE l = 1 NEW_LINE r = 1 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE l = 1 NEW_LINE r = 2 NEW_LINE print ( GCDoutsideRange ( l , r , prefix , suffix , n ) ) NEW_LINE


import java . util . * ; class solution { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; else return gcd ( b , a % b ) ; } static int lcmOfArray ( int arr [ ] , int n ) { if ( n < 1 ) return 0 ; int lcm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) lcm = ( lcm * arr [ i ] ) / gcd ( lcm , arr [ i ] ) ; return lcm ; } static int minPerfectSquare ( int arr [ ] , int n ) { int minPerfectSq ; int lcm = lcmOfArray ( arr , n ) ; minPerfectSq = ( int ) lcm ; int cnt = 0 ; while ( lcm > 1 && lcm % 2 == 0 ) { cnt ++ ; lcm /= 2 ; } if ( cnt % 2 != 0 ) minPerfectSq *= 2 ; int i = 3 ; while ( lcm > 1 ) { cnt = 0 ; while ( lcm % i == 0 ) { cnt ++ ; lcm /= i ; } if ( cnt % 2 != 0 ) minPerfectSq *= i ; i += 2 ; } return minPerfectSq ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , 3 , 4 , 5 , 7 } ; int n = arr . length ; System . out . println ( minPerfectSquare ( arr , n ) ) ; } }

from math import gcd NEW_LINE def lcmOfArray ( arr , n ) : NEW_LINE INDENT if ( n < 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT lcm = arr [ 0 ] NEW_LINE for i in range ( 1 , n , 1 ) : NEW_LINE INDENT lcm = int ( ( lcm * arr [ i ] ) / gcd ( lcm , arr [ i ] ) ) NEW_LINE DEDENT return lcm NEW_LINE DEDENT def minPerfectSquare ( arr , n ) : NEW_LINE INDENT lcm = lcmOfArray ( arr , n ) NEW_LINE minPerfectSq = int ( lcm ) NEW_LINE cnt = 0 NEW_LINE while ( lcm > 1 and lcm % 2 == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE lcm /= 2 NEW_LINE DEDENT if ( cnt % 2 != 0 ) : NEW_LINE INDENT minPerfectSq *= 2 NEW_LINE DEDENT i = 3 NEW_LINE while ( lcm > 1 ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE while ( lcm % i == 0 ) : NEW_LINE INDENT cnt += 1 NEW_LINE lcm /= i NEW_LINE DEDENT if ( cnt % 2 != 0 ) : NEW_LINE INDENT minPerfectSq *= i NEW_LINE DEDENT i += 2 NEW_LINE DEDENT return minPerfectSq NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minPerfectSquare ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static int MAX_DIGITS = 20 ; static int isOctal ( int n ) { while ( n > 0 ) { if ( ( n % 10 ) >= 8 ) return 0 ; else n = n / 10 ; } return 1 ; } static int isPalindrome ( int n ) { int divide = ( isOctal ( n ) == 0 ) ? 8 : 10 ; int octal [ ] = new int [ MAX_DIGITS ] ; int i = 0 ; while ( n != 0 ) { octal [ i ++ ] = n % divide ; n = n / divide ; } for ( int j = i - 1 , k = 0 ; k <= j ; j -- , k ++ ) if ( octal [ j ] != octal [ k ] ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { int n = 97 ; if ( isPalindrome ( n ) > 0 ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

MAX_DIGITS = 20 ; NEW_LINE def isOctal ( n ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT if ( ( n % 10 ) >= 8 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT n = int ( n / 10 ) NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isPalindrome ( n ) : NEW_LINE INDENT divide = 8 if ( isOctal ( n ) == False ) else 10 NEW_LINE octal = [ ] NEW_LINE while ( n != 0 ) : NEW_LINE INDENT octal . append ( n % divide ) NEW_LINE n = int ( n / divide ) NEW_LINE DEDENT j = len ( octal ) - 1 NEW_LINE k = 0 NEW_LINE while ( k <= j ) : NEW_LINE INDENT if ( octal [ j ] != octal [ k ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT j -= 1 NEW_LINE k += 1 NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 97 ; NEW_LINE if ( isPalindrome ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


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

def printOrder ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( n // 2 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT for j in range ( n - 1 , n // 2 - 1 , - 1 ) : NEW_LINE INDENT print ( arr [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , - 1 ] NEW_LINE n = len ( arr ) NEW_LINE printOrder ( arr , n ) NEW_LINE DEDENT


class GFG { static int Profit ( int costPrice , int sellingPrice ) { int profit = ( sellingPrice - costPrice ) ; return profit ; } static int Loss ( int costPrice , int sellingPrice ) { int Loss = ( costPrice - sellingPrice ) ; return Loss ; } public static void main ( String [ ] args ) { int costPrice = 1500 , sellingPrice = 2000 ; if ( sellingPrice == costPrice ) System . out . println ( " No ▁ profit ▁ nor ▁ Loss " ) ; else if ( sellingPrice > costPrice ) System . out . println ( Profit ( costPrice , sellingPrice ) + " ▁ Profit ▁ " ) ; else System . out . println ( Loss ( costPrice , sellingPrice ) + " ▁ Loss ▁ " ) ; } }

def Profit ( costPrice , sellingPrice ) : NEW_LINE INDENT profit = ( sellingPrice - costPrice ) NEW_LINE return profit NEW_LINE DEDENT def Loss ( costPrice , sellingPrice ) : NEW_LINE INDENT Loss = ( costPrice - sellingPrice ) NEW_LINE return Loss NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT costPrice , sellingPrice = 1500 , 2000 NEW_LINE if sellingPrice == costPrice : NEW_LINE INDENT print ( " No ▁ profit ▁ nor ▁ Loss " ) NEW_LINE DEDENT elif sellingPrice > costPrice : NEW_LINE INDENT print ( Profit ( costPrice , sellingPrice ) , " Profit " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( Loss ( costPrice , sellingPrice ) , " Loss " ) NEW_LINE DEDENT DEDENT


class GFG { static int max_sum ( int arr [ ] , int n , int k ) { int dp [ ] = new int [ n ] ; dp [ 0 ] = Math . max ( 0 , arr [ 0 ] ) ; int i = 1 ; while ( i < k ) { dp [ i ] = Math . max ( dp [ i - 1 ] , arr [ i ] ) ; i ++ ; } i = k ; while ( i < n ) { dp [ i ] = Math . max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; i ++ ; } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , - 2 , 4 , 3 , 1 } ; int n = arr . length ; int k = 4 ; System . out . println ( max_sum ( arr , n , k ) ) ; } }

def max_sum ( arr , n , k ) : NEW_LINE INDENT dp = [ 0 ] * n ; NEW_LINE dp [ 0 ] = max ( 0 , arr [ 0 ] ) ; NEW_LINE i = 1 ; NEW_LINE while ( i < k ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i - 1 ] , arr [ i ] ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT i = k ; NEW_LINE while ( i < n ) : NEW_LINE INDENT dp [ i ] = max ( dp [ i - 1 ] , arr [ i ] + dp [ i - k ] ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT return dp [ n - 1 ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , - 2 , 4 , 3 , 1 ] ; NEW_LINE n = len ( arr ) NEW_LINE k = 4 ; NEW_LINE print ( max_sum ( arr , n , k ) ) ; NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; public class GfG { static int checkBit ( int pattern , int arr [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( ( pattern & arr [ i ] ) == pattern ) count ++ ; return count ; } static int maxAND ( int arr [ ] , int n ) { int res = 0 , count ; for ( int bit = 31 ; bit >= 0 ; bit -- ) { count = checkBit ( res | ( 1 << bit ) , arr , n ) ; if ( count >= 2 ) res |= ( 1 << bit ) ; } return res ; } public static void main ( String argc [ ] ) { int arr [ ] = { 4 , 8 , 6 , 2 } ; int n = arr . length ; System . out . println ( " Maximum ▁ AND ▁ Value ▁ = ▁ " + maxAND ( arr , n ) ) ; } }

def checkBit ( pattern , arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( pattern & arr [ i ] ) == pattern ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT def maxAND ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for bit in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT count = checkBit ( res | ( 1 << bit ) , arr , n ) NEW_LINE if ( count >= 2 ) : NEW_LINE INDENT res = res | ( 1 << bit ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT arr = [ 4 , 8 , 6 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ AND ▁ Value ▁ = ▁ " , maxAND ( arr , n ) ) NEW_LINE


class GFG { static final int N = 4 ; static int areSame ( int A [ ] [ ] , int B [ ] [ ] ) { int i , j ; for ( i = 0 ; i < N ; i ++ ) for ( j = 0 ; j < N ; j ++ ) if ( A [ i ] [ j ] != B [ i ] [ j ] ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { int A [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 } } ; int B [ ] [ ] = { { 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 } } ; if ( areSame ( A , B ) == 1 ) System . out . print ( " Matrices ▁ are ▁ identical " ) ; else System . out . print ( " Matrices ▁ are ▁ not ▁ identical " ) ; } }

N = 4 NEW_LINE def areSame ( A , B ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( A [ i ] [ j ] != B [ i ] [ j ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT DEDENT return 1 NEW_LINE DEDENT A = [ [ 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 ] ] NEW_LINE B = [ [ 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 ] ] NEW_LINE if ( areSame ( A , B ) == 1 ) : NEW_LINE INDENT print ( " Matrices ▁ are ▁ identical " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Matrices ▁ are ▁ not ▁ identical " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void sortMultiples ( int arr [ ] , int n , int x ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) { v . add ( arr [ i ] ) ; } } Collections . sort ( v , Collections . reverseOrder ( ) ) ; int j = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % x == 0 ) { arr [ i ] = v . get ( j ++ ) ; } } } static void printArray ( int arr [ ] , int N ) { for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 125 , 3 , 15 , 6 , 100 , 5 } ; int x = 5 ; int n = arr . length ; sortMultiples ( arr , n , x ) ; printArray ( arr , n ) ; } }

def sortMultiples ( arr , n , x ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % x == 0 ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v . sort ( reverse = True ) NEW_LINE j = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % x == 0 ) : NEW_LINE INDENT arr [ i ] = v [ j ] NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT def printArray ( arr , N ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 125 , 3 , 15 , 6 , 100 , 5 ] NEW_LINE x = 5 NEW_LINE n = len ( arr ) NEW_LINE sortMultiples ( arr , n , x ) NEW_LINE printArray ( arr , n ) NEW_LINE DEDENT


class GfG { static class Node { char data ; Node prev ; Node next ; } static Node head = null ; static void rotate ( int N ) { if ( N == 0 ) return ; Node current = head ; int count = 1 ; while ( count < N && current != null ) { current = current . next ; count ++ ; } if ( current == null ) return ; Node NthNode = current ; while ( current . next != null ) current = current . next ; current . next = head ; ( head ) . prev = current ; head = NthNode . next ; ( head ) . prev = null ; NthNode . next = null ; } static void push ( char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = ( head ) ; if ( ( head ) != null ) ( head ) . prev = new_node ; head = new_node ; } static void printList ( Node node ) { while ( node != null && node . next != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } if ( node != null ) System . out . print ( node . data ) ; } public static void main ( String [ ] args ) { push ( ' e ' ) ; push ( ' d ' ) ; push ( ' c ' ) ; push ( ' b ' ) ; push ( ' a ' ) ; int N = 2 ; System . out . println ( " Given ▁ linked ▁ list ▁ " ) ; printList ( head ) ; rotate ( N ) ; System . out . println ( ) ; System . out . println ( " Rotated ▁ Linked ▁ list ▁ " ) ; printList ( head ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , next = None , prev = None , data = None ) : NEW_LINE INDENT self . next = next NEW_LINE self . prev = prev NEW_LINE self . data = data NEW_LINE DEDENT DEDENT def push ( head , new_data ) : NEW_LINE INDENT new_node = Node ( data = new_data ) NEW_LINE new_node . next = head NEW_LINE new_node . prev = None NEW_LINE if head is not None : NEW_LINE INDENT head . prev = new_node NEW_LINE DEDENT head = new_node NEW_LINE return head NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT node = head NEW_LINE print ( " Given ▁ linked ▁ list " ) NEW_LINE while ( node is not None ) : NEW_LINE INDENT print ( node . data , end = " ▁ " ) , NEW_LINE last = node NEW_LINE node = node . next NEW_LINE DEDENT DEDENT def rotate ( start , N ) : NEW_LINE INDENT if N == 0 : NEW_LINE INDENT return NEW_LINE DEDENT current = start NEW_LINE count = 1 NEW_LINE while count < N and current != None : NEW_LINE INDENT current = current . next NEW_LINE count += 1 NEW_LINE DEDENT if current == None : NEW_LINE INDENT return NEW_LINE DEDENT NthNode = current NEW_LINE while current . next != None : NEW_LINE INDENT current = current . next NEW_LINE DEDENT current . next = start NEW_LINE start . prev = current NEW_LINE start = NthNode . next NEW_LINE start . prev = None NEW_LINE NthNode . next = None NEW_LINE return start NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT head = None NEW_LINE head = push ( head , ' e ' ) NEW_LINE head = push ( head , ' d ' ) NEW_LINE head = push ( head , ' c ' ) NEW_LINE head = push ( head , ' b ' ) NEW_LINE head = push ( head , ' a ' ) NEW_LINE printList ( head ) NEW_LINE print ( " \n " ) NEW_LINE N = 2 NEW_LINE head = rotate ( head , N ) NEW_LINE printList ( head ) NEW_LINE DEDENT


public class GFG { static boolean isComposite ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return false ; if ( n % 2 == 0 || n % 3 == 0 ) return true ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return true ; return false ; } static boolean isDigitSumOne ( int nm ) { while ( nm > 9 ) { int sum_digit = 0 ; while ( nm > 0 ) { int digit = nm % 10 ; sum_digit = sum_digit + digit ; nm = nm / 10 ; } nm = sum_digit ; } if ( nm == 1 ) return true ; else return false ; } static void printValidNums ( int l , int r ) { for ( int i = l ; i <= r ; i ++ ) { if ( isComposite ( i ) && isDigitSumOne ( i ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String arg [ ] ) { int l = 10 , r = 100 ; printValidNums ( l , r ) ; } }

def isComposite ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return False NEW_LINE DEDENT def isDigitSumOne ( nm ) : NEW_LINE INDENT while ( nm > 9 ) : NEW_LINE INDENT sum_digit = 0 NEW_LINE while ( nm != 0 ) : NEW_LINE INDENT digit = nm % 10 NEW_LINE sum_digit = sum_digit + digit NEW_LINE nm = nm // 10 NEW_LINE DEDENT nm = sum_digit NEW_LINE DEDENT if ( nm == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT def printValidNums ( m , n ) : NEW_LINE INDENT for i in range ( m , n + 1 ) : NEW_LINE INDENT if ( isComposite ( i ) and isDigitSumOne ( i ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT l = 10 NEW_LINE r = 100 NEW_LINE printValidNums ( m , n ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static int NO_OF_CHARS = 256 ; static boolean arePermutation ( char str1 [ ] , char str2 [ ] ) { int count1 [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count1 , 0 ) ; int count2 [ ] = new int [ NO_OF_CHARS ] ; Arrays . fill ( count2 , 0 ) ; int i ; for ( i = 0 ; i < str1 . length && i < str2 . length ; i ++ ) { count1 [ str1 [ i ] ] ++ ; count2 [ str2 [ i ] ] ++ ; } if ( str1 . length != str2 . length ) return false ; for ( i = 0 ; i < NO_OF_CHARS ; i ++ ) if ( count1 [ i ] != count2 [ i ] ) return false ; return true ; } public static void main ( String args [ ] ) { char str1 [ ] = ( " geeksforgeeks " ) . toCharArray ( ) ; char str2 [ ] = ( " forgeeksgeeks " ) . toCharArray ( ) ; if ( arePermutation ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

NO_OF_CHARS = 256 NEW_LINE def arePermutation ( str1 , str2 ) : NEW_LINE INDENT count1 = [ 0 ] * NO_OF_CHARS NEW_LINE count2 = [ 0 ] * NO_OF_CHARS NEW_LINE for i in str1 : NEW_LINE INDENT count1 [ ord ( i ) ] += 1 NEW_LINE DEDENT for i in str2 : NEW_LINE INDENT count2 [ ord ( i ) ] += 1 NEW_LINE DEDENT if len ( str1 ) != len ( str2 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT for i in xrange ( NO_OF_CHARS ) : NEW_LINE INDENT if count1 [ i ] != count2 [ i ] : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT str1 = " geeksforgeeks " NEW_LINE str2 = " forgeeksgeeks " NEW_LINE if arePermutation ( str1 , str2 ) : NEW_LINE INDENT print " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT print " No " NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static void majority ( int a [ ] , int p , int q , int size ) { int start = 0 , ends = q ; int [ ] arr = new int [ p ] ; int k = 0 ; while ( k < p ) { int one = 0 ; for ( int j = start ; j < ends ; j ++ ) { if ( a [ j ] == 1 ) { one ++ ; } } start = ends ; ends = ends + q ; arr [ k ] = one ; k ++ ; } start = 0 ; ends = q ; boolean found = false ; while ( ends > 0 ) { int dist_one = 0 ; for ( int i = 0 ; i < p ; i ++ ) if ( arr [ i ] > q / 2 ) dist_one ++ ; if ( dist_one > p / 2 ) { found = true ; System . out . println ( " Yes " ) ; return ; } start -- ; ends -- ; if ( start < 0 ) start = size + start ; int st = start , en = ends , l = 0 ; while ( en < size ) { if ( a [ st % size ] != a [ en % size ] ) { if ( a [ st % size ] == 1 ) arr [ l ] ++ ; else arr [ l ] -- ; } l ++ ; st = ( st + q ) ; en = en + q ; } } if ( found == false ) { System . out . println ( " No " ) ; } } public static void main ( String args [ ] ) { int p = 3 , q = 3 ; int n = p * q ; int a [ ] = { 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 } ; majority ( a , p , q , n ) ; } }

def majority ( a , p , q , size ) : NEW_LINE INDENT start = 0 NEW_LINE ends = q NEW_LINE arr = [ None ] * p NEW_LINE k = 0 NEW_LINE while ( k < p ) : NEW_LINE INDENT one = 0 NEW_LINE for j in range ( start , ends ) : NEW_LINE INDENT if ( a [ j ] == 1 ) : NEW_LINE INDENT one = one + 1 NEW_LINE DEDENT DEDENT start = ends NEW_LINE ends = ends + q NEW_LINE arr [ k ] = one NEW_LINE k = k + 1 NEW_LINE DEDENT start = 0 NEW_LINE ends = q NEW_LINE found = 0 NEW_LINE while ( ends > 0 ) : NEW_LINE INDENT dist_one = 0 NEW_LINE for i in range ( 0 , p ) : NEW_LINE INDENT if ( arr [ i ] > q / 2 ) : NEW_LINE INDENT dist_one = dist_one + 1 NEW_LINE DEDENT DEDENT if ( dist_one > p / 2 ) : NEW_LINE INDENT found = 1 NEW_LINE print ( " Yes " ) NEW_LINE return NEW_LINE DEDENT start = start - 1 NEW_LINE ends = ends - 1 NEW_LINE if ( start < 0 ) : NEW_LINE INDENT start = size + start NEW_LINE DEDENT st = start NEW_LINE en = ends NEW_LINE l = 0 NEW_LINE while ( en < size ) : NEW_LINE INDENT if ( a [ st % size ] != a [ en % size ] ) : NEW_LINE INDENT if ( a [ st % size ] == 1 ) : NEW_LINE INDENT arr [ l ] = arr [ l ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ l ] = arr [ l ] - 1 NEW_LINE DEDENT DEDENT l = l + 1 NEW_LINE st = ( st + q ) NEW_LINE en = en + q NEW_LINE DEDENT DEDENT if ( found == 0 ) : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT p = 3 NEW_LINE q = 3 NEW_LINE n = p * q NEW_LINE a = [ 0 , 0 , 1 , 1 , 0 , 1 , 1 , 0 , 0 ] NEW_LINE majority ( a , p , q , n ) NEW_LINE


class GFG { static int nextGreater ( int N ) { int power_of_2 = 1 , shift_count = 0 ; while ( true ) { if ( ( ( N >> shift_count ) & 1 ) % 2 == 0 ) break ; shift_count ++ ; power_of_2 = power_of_2 * 2 ; } return ( N + power_of_2 ) ; } public static void main ( String [ ] a ) { int N = 11 ; System . out . println ( " The ▁ next ▁ number ▁ is ▁ = ▁ " + nextGreater ( N ) ) ; } }

def nextGreater ( N ) : NEW_LINE INDENT power_of_2 = 1 ; NEW_LINE shift_count = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT if ( ( ( N >> shift_count ) & 1 ) % 2 == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT shift_count += 1 ; NEW_LINE power_of_2 = power_of_2 * 2 ; NEW_LINE DEDENT return ( N + power_of_2 ) ; NEW_LINE DEDENT N = 11 ; NEW_LINE print ( " The ▁ next ▁ number ▁ is ▁ = " , nextGreater ( N ) ) ; NEW_LINE


import java . io . * ; class GFG { static int count ( String s , int len ) { int MAX = 1000 ; int cur = 0 , dig = 0 ; int [ ] sum = new int [ MAX ] ; int [ ] [ ] dp = new int [ MAX ] [ 3 ] ; dp [ 0 ] [ 0 ] = 1 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = ( int ) ( s . charAt ( i - 1 ) ) - 48 ; cur += dig ; cur %= 3 ; sum [ i ] = cur ; dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] ; dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] ; dp [ i ] [ 2 ] = dp [ i - 1 ] [ 2 ] ; dp [ i ] [ sum [ i ] ] ++ ; } int ans = 0 , dprev = 0 , value = 0 , dprev2 = 0 ; for ( int i = 1 ; i <= len ; i ++ ) { dig = ( int ) ( s . charAt ( i - 1 ) ) - 48 ; if ( dig == 8 ) ans ++ ; if ( i - 2 >= 0 ) { dprev = ( int ) ( s . charAt ( i - 2 ) ) - 48 ; value = dprev * 10 + dig ; if ( ( value % 8 == 0 ) && ( value % 3 != 0 ) ) ans ++ ; } if ( i - 3 >= 0 ) { dprev2 = ( int ) ( s . charAt ( i - 3 ) ) - 48 ; dprev = ( int ) ( s . charAt ( i - 2 ) ) - 48 ; value = dprev2 * 100 + dprev * 10 + dig ; if ( value % 8 != 0 ) continue ; ans += ( i - 2 ) ; ans -= ( dp [ i - 3 ] [ sum [ i ] ] ) ; } } return ans ; } public static void main ( String [ ] args ) { String str = "6564525600" ; int len = str . length ( ) ; System . out . println ( count ( str , len ) ) ; } }

def count ( s , Len ) : NEW_LINE INDENT global MAX NEW_LINE cur = 0 NEW_LINE dig = 0 NEW_LINE Sum = [ 0 ] * MAX NEW_LINE dp = [ [ 0 , 0 , 0 ] for i in range ( MAX ) ] NEW_LINE dp [ 0 ] [ 0 ] = 1 NEW_LINE for i in range ( 1 , Len + 1 ) : NEW_LINE INDENT dig = int ( s [ i - 1 ] ) - 48 NEW_LINE cur += dig NEW_LINE cur %= 3 NEW_LINE Sum [ i ] = cur NEW_LINE dp [ i ] [ 0 ] = dp [ i - 1 ] [ 0 ] NEW_LINE dp [ i ] [ 1 ] = dp [ i - 1 ] [ 1 ] NEW_LINE dp [ i ] [ 2 ] = dp [ i - 1 ] [ 2 ] NEW_LINE dp [ i ] [ Sum [ i ] ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE dprev = 0 NEW_LINE value = 0 NEW_LINE dprev2 = 0 NEW_LINE for i in range ( 1 , Len + 1 ) : NEW_LINE INDENT dig = int ( s [ i - 1 ] ) - 48 NEW_LINE if dig == 8 : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT if i - 2 >= 0 : NEW_LINE INDENT dprev = int ( s [ i - 2 ] ) - 48 NEW_LINE value = dprev * 10 + dig NEW_LINE if ( value % 8 == 0 ) and ( value % 3 != 0 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if i - 3 >= 0 : NEW_LINE INDENT dprev2 = int ( s [ i - 3 ] ) - 48 NEW_LINE dprev = int ( s [ i - 2 ] ) - 48 NEW_LINE value = ( dprev2 * 100 + dprev * 10 + dig ) NEW_LINE if value % 8 != 0 : NEW_LINE INDENT continue NEW_LINE DEDENT ans += ( i - 2 ) NEW_LINE ans -= ( dp [ i - 3 ] [ Sum [ i ] ] ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT MAX = 1000 NEW_LINE Str = "6564525600" NEW_LINE Len = len ( Str ) NEW_LINE print ( count ( Str , Len ) ) NEW_LINE


import java . util . Arrays ; class GFG { static void printSortedLevels ( int arr [ ] , int n ) { int level = 0 ; for ( int i = 0 ; i < n ; level ++ ) { int cnt = ( int ) Math . pow ( 2 , level ) ; cnt -= 1 ; int j = Math . min ( i + cnt , n - 1 ) ; Arrays . sort ( arr , i , j + 1 ) ; while ( i <= j ) { System . out . print ( arr [ i ] + " ▁ " ) ; i ++ ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 4 , 9 , 2 , 1 } ; int n = arr . length ; printSortedLevels ( arr , n ) ; } }

from math import pow NEW_LINE def printSortedLevels ( arr , n ) : NEW_LINE INDENT level = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT cnt = int ( pow ( 2 , level ) ) NEW_LINE cnt -= 1 NEW_LINE j = min ( i + cnt , n - 1 ) NEW_LINE arr = arr [ : i ] + sorted ( arr [ i : j + 1 ] ) +   \ NEW_LINE INDENT arr [ j + 1 : ] NEW_LINE DEDENT while ( i <= j ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE DEDENT print ( ) NEW_LINE level += 1 NEW_LINE DEDENT DEDENT arr = [ 5 , 6 , 4 , 9 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE printSortedLevels ( arr , n ) NEW_LINE


import java . util . Arrays ; class GFG { static int arr [ ] = { 100 , 1000 , 100 , 1000 , 1 } ; static int sum [ ] = new int [ 10000 ] ; static int maxSumWO3Consec ( int n ) { if ( sum [ n ] != - 1 ) return sum [ n ] ; if ( n == 0 ) return sum [ n ] = 0 ; if ( n == 1 ) return sum [ n ] = arr [ 0 ] ; if ( n == 2 ) return sum [ n ] = arr [ 1 ] + arr [ 0 ] ; return sum [ n ] = Math . max ( Math . max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n - 1 ] ) , arr [ n - 2 ] + arr [ n - 1 ] + maxSumWO3Consec ( n - 3 ) ) ; } public static void main ( String [ ] args ) { int n = arr . length ; Arrays . fill ( sum , - 1 ) ; System . out . println ( maxSumWO3Consec ( n ) ) ; } }

arr = [ 100 , 1000 , 100 , 1000 , 1 ] NEW_LINE sum = [ - 1 ] * 10000 NEW_LINE def maxSumWO3Consec ( n ) : NEW_LINE INDENT if ( sum [ n ] != - 1 ) : NEW_LINE INDENT return sum [ n ] NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT sum [ n ] = 0 NEW_LINE return sum [ n ] NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT sum [ n ] = arr [ 0 ] NEW_LINE return sum [ n ] NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT sum [ n ] = arr [ 1 ] + arr [ 0 ] NEW_LINE return sum [ n ] NEW_LINE DEDENT sum [ n ] = max ( max ( maxSumWO3Consec ( n - 1 ) , maxSumWO3Consec ( n - 2 ) + arr [ n - 1 ] ) , arr [ n - 2 ] + arr [ n - 1 ] + maxSumWO3Consec ( n - 3 ) ) NEW_LINE return sum [ n ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = len ( arr ) NEW_LINE print ( maxSumWO3Consec ( n ) ) NEW_LINE DEDENT


import java . util . HashSet ; import java . util . Set ; public class GfG { static void findMissing ( int a [ ] , int b [ ] , int n , int m ) { HashSet < Integer > s = new HashSet < > ( ) ; for ( int i = 0 ; i < m ; i ++ ) s . add ( b [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( ! s . contains ( a [ i ] ) ) System . out . print ( a [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 6 , 3 , 4 , 5 } ; int b [ ] = { 2 , 4 , 3 , 1 , 0 } ; int n = a . length ; int m = b . length ; findMissing ( a , b , n , m ) ; } }

def findMissing ( a , b , n , m ) : NEW_LINE INDENT s = dict ( ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT s [ b [ i ] ] = 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if a [ i ] not in s . keys ( ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT a = [ 1 , 2 , 6 , 3 , 4 , 5 ] NEW_LINE b = [ 2 , 4 , 3 , 1 , 0 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE findMissing ( a , b , n , m ) NEW_LINE


class GfG { static long ispowerof2 ( long num ) { if ( ( num & ( num - 1 ) ) == 0 ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { long num = 549755813888L ; System . out . println ( ispowerof2 ( num ) ) ; } }

def ispowerof2 ( num ) : NEW_LINE INDENT if ( ( num & ( num - 1 ) ) == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = 549755813888 NEW_LINE print ( ispowerof2 ( num ) ) NEW_LINE DEDENT


class GFG { static int factorial ( int n ) { return ( n == 1 || n == 0 ) ? 1 : factorial ( n - 1 ) * n ; } static int gcdOfFactorial ( int [ ] arr , int n ) { int minm = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) minm = minm > arr [ i ] ? arr [ i ] : minm ; return factorial ( minm ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 9 , 12 , 122 , 34 , 15 } ; int n = arr . length ; System . out . println ( gcdOfFactorial ( arr , n ) ) ; } }

def factorial ( n ) : NEW_LINE INDENT if n == 1 or n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return factorial ( n - 1 ) * n NEW_LINE DEDENT DEDENT def gcdOfFactorial ( arr , n ) : NEW_LINE INDENT minm = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if minm > arr [ i ] : NEW_LINE INDENT minm = arr [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = minm NEW_LINE DEDENT DEDENT return factorial ( minm ) NEW_LINE DEDENT arr = [ 9 , 12 , 122 , 34 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( gcdOfFactorial ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static void specialNumber ( int n ) { if ( n < 10 || n > 99 ) System . out . println ( " Invalid ▁ Input ! ▁ " + " Number ▁ should ▁ have ▁ " + "2 ▁ digits ▁ only " ) ; else { int first = n / 10 ; int last = n % 10 ; int sum = first + last ; int pro = first * last ; if ( ( sum + pro ) == n ) { System . out . println ( n + " ▁ is ▁ a ▁ Special " + " ▁ Two - Digit ▁ Number " ) ; } else { System . out . println ( n + " ▁ is ▁ Not ▁ a ▁ Special " + " ▁ Two - Digit ▁ Number " ) ; } } } public static void main ( String args [ ] ) { int n = 59 ; specialNumber ( n ) ; } }

def specialNumber ( n ) : NEW_LINE INDENT if ( n < 10 or n > 99 ) : NEW_LINE INDENT print ( " Invalid ▁ Input ! ▁ Number " , " ▁ should ▁ have ▁ 2 ▁ digits ▁ only " ) NEW_LINE DEDENT else : NEW_LINE INDENT first = n // 10 NEW_LINE last = n % 10 NEW_LINE sum = first + last NEW_LINE pro = first * last NEW_LINE if ( ( sum + pro ) == n ) : NEW_LINE INDENT print ( n , " ▁ is ▁ a ▁ Special ▁ " , " Two - Digit ▁ Number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( n , " ▁ is ▁ Not ▁ a ▁ " , " Special ▁ Two - Digit ▁ Number " ) NEW_LINE DEDENT DEDENT DEDENT n = 59 NEW_LINE specialNumber ( n ) NEW_LINE


import java . util . * ; public class GFG { static int LongestFibSubseq ( int A [ ] , int n ) { TreeSet < Integer > S = new TreeSet < > ( ) ; for ( int t : A ) { S . add ( t ) ; } int maxLen = 0 , x , y ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = i + 1 ; j < n ; ++ j ) { x = A [ j ] ; y = A [ i ] + A [ j ] ; int length = 3 ; while ( S . contains ( y ) && ( y != S . last ( ) ) ) { int z = x + y ; x = y ; y = z ; maxLen = Math . max ( maxLen , ++ length ) ; } } } return maxLen >= 3 ? maxLen : 0 ; } public static void main ( String [ ] args ) { int A [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 } ; int n = A . length ; System . out . print ( LongestFibSubseq ( A , n ) ) ; } }

def LongestFibSubseq ( A , n ) : NEW_LINE INDENT S = set ( A ) NEW_LINE maxLen = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT x = A [ j ] NEW_LINE y = A [ i ] + A [ j ] NEW_LINE length = 2 NEW_LINE while y in S : NEW_LINE INDENT z = x + y NEW_LINE x = y NEW_LINE y = z NEW_LINE length += 1 NEW_LINE maxLen = max ( maxLen , length ) NEW_LINE DEDENT DEDENT DEDENT return maxLen if maxLen >= 3 else 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] NEW_LINE n = len ( A ) NEW_LINE print ( LongestFibSubseq ( A , n ) ) NEW_LINE DEDENT


class Geeks { static boolean isInGivenBase ( String str , int base ) { if ( base > 16 ) return false ; else if ( base <= 10 ) { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( ! ( str . charAt ( i ) >= '0' && str . charAt ( i ) < ( '0' + base ) ) ) return false ; } else { for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( ! ( ( str . charAt ( i ) >= '0' && str . charAt ( i ) < ( '0' + base ) ) || ( str . charAt ( i ) >= ' A ' && str . charAt ( i ) < ( ' A ' + base - 10 ) ) ) ) return false ; } return true ; } public static void main ( String args [ ] ) { String str = " AF87" ; if ( isInGivenBase ( str , 16 ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isInGivenBase ( Str , base ) : NEW_LINE INDENT if ( base > 16 ) : NEW_LINE INDENT return False NEW_LINE DEDENT elif ( base <= 10 ) : NEW_LINE INDENT for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( Str [ i ] . isnumeric ( ) and ( ord ( Str [ i ] ) >= ord ( '0' ) and ord ( Str [ i ] ) < ( ord ( '0' ) + base ) ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( Str [ i ] . isnumeric ( ) and ( ( ord ( Str [ i ] ) >= ord ( '0' ) and ord ( Str [ i ] ) < ( ord ( '0' ) + base ) ) or ( ord ( Str [ i ] ) >= ord ( ' A ' ) and ord ( Str [ i ] ) < ( ord ( ' A ' ) + base - 10 ) ) ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT Str = " AF87" NEW_LINE if ( isInGivenBase ( Str , 16 ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static int SumF ( int S [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( int ) ( S [ i ] * Math . pow ( 2 , n - i - 1 ) ) ; return sum ; } static int SumL ( int S [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum = sum + ( int ) ( S [ i ] * Math . pow ( 2 , i ) ) ; return sum ; } static int sumSetDiff ( int S [ ] , int n ) { return SumL ( S , n ) - SumF ( S , n ) ; } public static void main ( String arg [ ] ) { int n = 4 ; int S [ ] = { 5 , 2 , 9 , 6 } ; System . out . println ( sumSetDiff ( S , n ) ) ; } }

def SumF ( S , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( S [ i ] * pow ( 2 , n - i - 1 ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def SumL ( S , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum = sum + ( S [ i ] * pow ( 2 , i ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT def sumSetDiff ( S , n ) : NEW_LINE INDENT return SumL ( S , n ) - SumF ( S , n ) NEW_LINE DEDENT n = 4 NEW_LINE S = [ 5 , 2 , 9 , 6 ] NEW_LINE print ( sumSetDiff ( S , n ) ) NEW_LINE


public class GFG { static int smallestNumber ( int n ) { int num = 0 ; String s = " " ; int duplicate = n ; while ( n > 0 ) { s = ( char ) ( n % 10 + 48 ) + s ; n /= 10 ; } int index = - 1 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { int digit = s . charAt ( i ) - '0' ; if ( ( digit & 1 ) == 0 ) { index = i ; break ; } } if ( index == - 1 ) { return duplicate ; } for ( int i = 0 ; i < index ; i ++ ) { num = num * 10 + ( s . charAt ( i ) - '0' ) ; } num = num * 10 + ( s . charAt ( index ) - '0' + 1 ) ; for ( int i = index + 1 ; i < s . length ( ) ; i ++ ) { num = num * 10 + 1 ; } return num ; } static public void main ( String [ ] args ) { int N = 2397 ; System . out . println ( smallestNumber ( N ) ) ; } }

def smallestNumber ( n ) : NEW_LINE INDENT num = 0 NEW_LINE s = " " NEW_LINE duplicate = n NEW_LINE while ( n ) : NEW_LINE INDENT s = chr ( n % 10 + 48 ) + s NEW_LINE n //= 10 NEW_LINE DEDENT index = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT digit = ord ( s [ i ] ) - ord ( '0' ) NEW_LINE if ( ( digit & 1 ) == 0 ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return duplicate NEW_LINE DEDENT for i in range ( index ) : NEW_LINE INDENT num = num * 10 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT num = num * 10 + ( ord ( s [ index ] ) - ord ( '0' ) + 1 ) NEW_LINE for i in range ( index + 1 , len ( s ) ) : NEW_LINE INDENT num = num * 10 + 1 NEW_LINE DEDENT return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 2397 NEW_LINE print ( smallestNumber ( N ) ) NEW_LINE DEDENT


class GFG { static int find_greatest_divisor ( int l , int r ) { if ( l == r ) { return l ; } return 1 ; } public static void main ( String [ ] args ) { int l = 2 , r = 12 ; System . out . println ( find_greatest_divisor ( l , r ) ) ; } }

def find_greatest_divisor ( l , r ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT return l ; NEW_LINE DEDENT return 1 ; NEW_LINE DEDENT l = 2 ; NEW_LINE r = 12 ; NEW_LINE print ( find_greatest_divisor ( l , r ) ) ; NEW_LINE


import java . io . * ; class GFG { static void multiply ( int a [ ] [ ] , int b [ ] [ ] ) { int mul [ ] [ ] = new int [ 3 ] [ 3 ] ; for ( int i = 0 ; i < 3 ; i ++ ) { for ( int j = 0 ; j < 3 ; j ++ ) { mul [ i ] [ j ] = 0 ; for ( int k = 0 ; k < 3 ; k ++ ) mul [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; } } for ( int i = 0 ; i < 3 ; i ++ ) for ( int j = 0 ; j < 3 ; j ++ ) a [ i ] [ j ] = mul [ i ] [ j ] ; } static int power ( int F [ ] [ ] , int n ) { int M [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; if ( n == 1 ) return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; power ( F , n / 2 ) ; multiply ( F , F ) ; if ( n % 2 != 0 ) multiply ( F , M ) ; return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; } static int findNthTerm ( int n ) { int F [ ] [ ] = { { 1 , 1 , 1 } , { 1 , 0 , 0 } , { 0 , 1 , 0 } } ; return power ( F , n - 2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " F ( 5 ) ▁ is ▁ " + findNthTerm ( n ) ) ; } }

def multiply ( a , b ) : NEW_LINE INDENT mul = [ [ 0 for x in range ( 3 ) ] for y in range ( 3 ) ] ; NEW_LINE for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT mul [ i ] [ j ] = 0 ; NEW_LINE for k in range ( 3 ) : NEW_LINE INDENT mul [ i ] [ j ] += a [ i ] [ k ] * b [ k ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 3 ) : NEW_LINE INDENT for j in range ( 3 ) : NEW_LINE INDENT a [ i ] [ j ] = mul [ i ] [ j ] ; NEW_LINE DEDENT DEDENT return a ; NEW_LINE DEDENT def power ( F , n ) : NEW_LINE INDENT M = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; NEW_LINE if ( n == 1 ) : NEW_LINE INDENT return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; NEW_LINE DEDENT power ( F , int ( n / 2 ) ) ; NEW_LINE F = multiply ( F , F ) ; NEW_LINE if ( n % 2 != 0 ) : NEW_LINE INDENT F = multiply ( F , M ) ; NEW_LINE DEDENT return F [ 0 ] [ 0 ] + F [ 0 ] [ 1 ] ; NEW_LINE DEDENT def findNthTerm ( n ) : NEW_LINE INDENT F = [ [ 1 , 1 , 1 ] , [ 1 , 0 , 0 ] , [ 0 , 1 , 0 ] ] ; NEW_LINE return power ( F , n - 2 ) ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( " F ( 5 ) ▁ is " , findNthTerm ( n ) ) ; NEW_LINE


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

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


import java . util . * ; class GFG { static class node { int data ; node left ; node right ; } ; static node newNode ( int data ) { node temp = new node ( ) ; temp . data = data ; temp . left = null ; temp . right = null ; return temp ; } ; static int sumOfNodesAtNthLevel ( node root , int k ) { if ( root == null ) return 0 ; Queue < node > que = new LinkedList < > ( ) ; que . add ( root ) ; int level = 0 ; int sum = 0 ; int flag = 0 ; while ( ! que . isEmpty ( ) ) { int size = que . size ( ) ; while ( size -- > 0 ) { node ptr = que . peek ( ) ; que . remove ( ) ; if ( level == k ) { flag = 1 ; sum += ptr . data ; } else { if ( ptr . left != null ) que . add ( ptr . left ) ; if ( ptr . right != null ) que . add ( ptr . right ) ; } } level ++ ; if ( flag == 1 ) break ; } return sum ; } public static void main ( String [ ] args ) { node root = new node ( ) ; root = newNode ( 50 ) ; root . left = newNode ( 30 ) ; root . right = newNode ( 70 ) ; root . left . left = newNode ( 20 ) ; root . left . right = newNode ( 40 ) ; root . right . left = newNode ( 60 ) ; int level = 2 ; int result = sumOfNodesAtNthLevel ( root , level ) ; System . out . print ( result ) ; } }

class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data ; NEW_LINE self . left = None ; NEW_LINE self . right = None ; NEW_LINE DEDENT DEDENT def sumOfNodesAtNthLevel ( root , k ) : NEW_LINE INDENT if ( root == None ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT que = [ ] ; NEW_LINE que . append ( root ) ; NEW_LINE level = 0 ; NEW_LINE sum = 0 ; NEW_LINE flag = 0 ; NEW_LINE while ( len ( que ) != 0 ) : NEW_LINE INDENT size = len ( que ) ; NEW_LINE while ( size != 0 ) : NEW_LINE INDENT size -= 1 ; NEW_LINE ptr = que [ 0 ] ; NEW_LINE que . pop ( 0 ) ; NEW_LINE if ( level == k ) : NEW_LINE INDENT flag = 1 ; NEW_LINE sum += ptr . data ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( ptr . left ) : NEW_LINE INDENT que . append ( ptr . left ) ; NEW_LINE DEDENT if ( ptr . right ) : NEW_LINE INDENT que . append ( ptr . right ) ; NEW_LINE DEDENT DEDENT DEDENT level += 1 ; NEW_LINE if ( flag == 1 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT root = newNode ( 50 ) ; NEW_LINE root . left = newNode ( 30 ) ; NEW_LINE root . right = newNode ( 70 ) ; NEW_LINE root . left . left = newNode ( 20 ) ; NEW_LINE root . left . right = newNode ( 40 ) ; NEW_LINE root . right . left = newNode ( 60 ) ; NEW_LINE level = 2 ; NEW_LINE result = sumOfNodesAtNthLevel ( root , level ) ; NEW_LINE print ( result ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int cost ( String str ) { int len = str . length ( ) ; int res = 0 ; for ( int i = 0 , j = len - 1 ; i < j ; i ++ , j -- ) if ( str . charAt ( i ) != str . charAt ( j ) ) res += Math . min ( str . charAt ( i ) , str . charAt ( j ) ) - ' a ' + 1 ; return res ; } public static void main ( String [ ] args ) { String str = " abcdef " ; System . out . println ( cost ( str ) ) ; } }

def cost ( st ) : NEW_LINE INDENT l = len ( st ) NEW_LINE res = 0 NEW_LINE j = l - 1 NEW_LINE i = 0 NEW_LINE while ( i < j ) : NEW_LINE INDENT if ( st [ i ] != st [ j ] ) : NEW_LINE INDENT res += ( min ( ord ( st [ i ] ) , ord ( st [ j ] ) ) - ord ( ' a ' ) + 1 ) NEW_LINE DEDENT i = i + 1 NEW_LINE j = j - 1 NEW_LINE DEDENT return res NEW_LINE DEDENT st = " abcdef " ; NEW_LINE print ( cost ( st ) ) NEW_LINE


import java . util . * ; class GFG { static boolean isPrime ( int N ) { if ( N < 2 ) return false ; if ( N < 4 ) return true ; if ( ( N & 1 ) == 0 ) return false ; if ( N % 3 == 0 ) return false ; int curr = 5 , s = ( int ) Math . sqrt ( N ) ; while ( curr <= s ) { if ( N % curr == 0 ) return false ; curr += 2 ; if ( N % curr == 0 ) return false ; curr += 4 ; } return true ; } static boolean check ( int s , int p , int prefix_sum [ ] , int n ) { boolean satisfies = true ; for ( int i = 0 ; i < n ; i ++ ) { if ( i + s - 1 >= n ) break ; if ( prefix_sum [ i + s - 1 ] - ( i - 1 >= 0 ? prefix_sum [ i - 1 ] : 0 ) < p ) satisfies = false ; } return satisfies ; } static int minimumWindowSize ( int x , int y , int p ) { int [ ] prefix_sum = new int [ y - x + 1 ] ; for ( int i = x ; i <= y ; i ++ ) { if ( isPrime ( i ) ) prefix_sum [ i - x ] = 1 ; } for ( int i = 1 ; i < y - x + 1 ; i ++ ) prefix_sum [ i ] += prefix_sum [ i - 1 ] ; int low = 1 , high = y - x + 1 ; int mid ; while ( high - low > 1 ) { mid = ( low + high ) / 2 ; if ( check ( mid , p , prefix_sum , y - x + 1 ) ) { high = mid ; } else low = mid ; } if ( check ( low , p , prefix_sum , y - x + 1 ) ) return low ; return high ; } public static void main ( String [ ] args ) { int x = 12 ; int y = 42 ; int p = 3 ; System . out . print ( minimumWindowSize ( x , y , p ) ) ; } }

from math import sqrt NEW_LINE def isPrime ( N ) : NEW_LINE INDENT if ( N < 2 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( N < 4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( ( N & 1 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( N % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT curr = 5 NEW_LINE s = sqrt ( N ) NEW_LINE while ( curr <= s ) : NEW_LINE INDENT if ( N % curr == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT curr += 2 NEW_LINE if ( N % curr == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT curr += 4 NEW_LINE DEDENT return True NEW_LINE DEDENT def check ( s , p , prefix_sum , n ) : NEW_LINE INDENT satisfies = True NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i + s - 1 >= n ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( i - 1 >= 0 ) : NEW_LINE INDENT x = prefix_sum [ i - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT x = 0 NEW_LINE DEDENT if ( prefix_sum [ i + s - 1 ] - x < p ) : NEW_LINE INDENT satisfies = False NEW_LINE DEDENT DEDENT return satisfies NEW_LINE DEDENT def minimumWindowSize ( x , y , p ) : NEW_LINE INDENT prefix_sum = [ 0 ] * ( y - x + 1 ) NEW_LINE for i in range ( x , y + 1 ) : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT prefix_sum [ i - x ] = 1 NEW_LINE DEDENT DEDENT for i in range ( 1 , y - x + 1 ) : NEW_LINE INDENT prefix_sum [ i ] += prefix_sum [ i - 1 ] NEW_LINE DEDENT low = 1 NEW_LINE high = y - x + 1 NEW_LINE while ( high - low > 1 ) : NEW_LINE INDENT mid = ( low + high ) // 2 NEW_LINE if ( check ( mid , p , prefix_sum , y - x + 1 ) ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid NEW_LINE DEDENT DEDENT if ( check ( low , p , prefix_sum , y - x + 1 ) ) : NEW_LINE INDENT return low NEW_LINE DEDENT return high NEW_LINE DEDENT x = 12 NEW_LINE y = 42 NEW_LINE p = 3 NEW_LINE print ( minimumWindowSize ( x , y , p ) ) NEW_LINE


import java . io . * ; class GFG { static int complement ( int num ) { int i , len = 0 , temp , comp ; temp = num ; while ( true ) { len ++ ; num = num / 10 ; if ( Math . abs ( num ) == 0 ) break ; } num = temp ; comp = ( int ) Math . pow ( 10 , len ) - num ; return comp ; } public static void main ( String [ ] args ) { System . out . println ( complement ( 25 ) ) ; System . out . println ( complement ( 456 ) ) ; } }

import math NEW_LINE def complement ( num ) : NEW_LINE INDENT i = 0 ; NEW_LINE len = 0 ; NEW_LINE comp = 0 ; NEW_LINE temp = num ; NEW_LINE while ( 1 ) : NEW_LINE INDENT len += 1 ; NEW_LINE num = int ( num / 10 ) ; NEW_LINE if ( abs ( num ) == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT num = temp ; NEW_LINE comp = math . pow ( 10 , len ) - num ; NEW_LINE return int ( comp ) ; NEW_LINE DEDENT print ( complement ( 25 ) ) ; NEW_LINE print ( complement ( 456 ) ) ; NEW_LINE


import java . io . * ; class GFG { static boolean check ( String str ) { int n = str . length ( ) ; if ( n == 0 && n == 1 ) return false ; if ( n == 2 ) return ( ( ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 16 == 0 ) ; if ( n == 3 ) return ( ( ( str . charAt ( n - 3 ) - '0' ) * 100 + ( str . charAt ( n - 2 ) - '0' ) * 10 + ( str . charAt ( n - 1 ) - '0' ) ) % 16 == 0 ) ; int last = str . charAt ( n - 1 ) - '0' ; int second_last = str . charAt ( n - 2 ) - '0' ; int third_last = str . charAt ( n - 3 ) - '0' ; int fourth_last = str . charAt ( n - 4 ) - '0' ; return ( ( fourth_last * 1000 + third_last * 100 + second_last * 10 + last ) % 16 == 0 ) ; } public static void main ( String args [ ] ) { String str = "769528" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No ▁ " ) ; } }

def check ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE if ( n == 0 and n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n == 2 ) : NEW_LINE INDENT return ( ( int ) ( st [ n - 2 ] ) * 10 + ( ( int ) ( st [ n - 1 ] ) % 16 == 0 ) ) NEW_LINE DEDENT if ( n == 3 ) : NEW_LINE INDENT return ( ( ( int ) ( st [ n - 3 ] ) * 100 + ( int ) ( st [ n - 2 ] ) * 10 + ( int ) ( st [ n - 1 ] ) ) % 16 == 0 ) NEW_LINE DEDENT last = ( int ) ( st [ n - 1 ] ) NEW_LINE second_last = ( int ) ( st [ n - 2 ] ) NEW_LINE third_last = ( int ) ( st [ n - 3 ] ) NEW_LINE fourth_last = ( int ) ( st [ n - 4 ] ) NEW_LINE return ( ( fourth_last * 1000 + third_last * 100 + second_last * 10 + last ) % 16 == 0 ) NEW_LINE DEDENT st = "769528" NEW_LINE if ( check ( st ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . lang . * ; import java . util . * ; class GFG { static int countConsecutive ( int n ) { String s = Integer . toString ( n ) ; int count = 0 ; for ( int i = 0 ; i < s . length ( ) - 1 ; i ++ ) if ( s . charAt ( i ) == s . charAt ( i + 1 ) ) count ++ ; return count ; } public static void main ( String args [ ] ) { int n = 44522255 ; System . out . println ( countConsecutive ( n ) ) ; } }

def countConsecutive ( n ) : NEW_LINE INDENT s = str ( n ) NEW_LINE count = 0 NEW_LINE for i in range ( len ( s ) - 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 44522255 NEW_LINE print ( countConsecutive ( n ) ) NEW_LINE DEDENT


public class base_power { static int calculate ( int n , int power ) { int sum = 0 ; int bp = ( int ) Math . pow ( n , power ) ; while ( bp != 0 ) { int d = bp % 10 ; sum += d ; bp /= 10 ; } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; int power = 4 ; System . out . println ( calculate ( n , power ) ) ; } }

def calculate ( n , power ) : NEW_LINE INDENT return sum ( [ int ( i ) for i in str ( pow ( n , power ) ) ] ) NEW_LINE DEDENT n = 5 NEW_LINE power = 4 NEW_LINE print ( calculate ( n , power ) ) NEW_LINE


class GFG { static void swap ( int a , int b , int c , int d ) { a = a + b ; b = a - b ; a = a - b ; b = b + c ; c = b - c ; b = b - c ; c = c + d ; d = c - d ; c = c - d ; System . out . println ( " values ▁ after ▁ " + " swapping ▁ are ▁ : ▁ " ) ; System . out . println ( " a ▁ = ▁ " + a ) ; System . out . println ( " b ▁ = ▁ " + b ) ; System . out . println ( " c ▁ = ▁ " + c ) ; System . out . println ( " d ▁ = ▁ " + d ) ; } public static void main ( String [ ] args ) { int a = 1 ; int b = 2 ; int c = 3 ; int d = 4 ; System . out . println ( " values ▁ before ▁ " + " swapping ▁ are ▁ : ▁ " ) ; System . out . println ( " a ▁ = ▁ " + a ) ; System . out . println ( " b ▁ = ▁ " + b ) ; System . out . println ( " c ▁ = ▁ " + c ) ; System . out . println ( " d ▁ = ▁ " + d ) ; System . out . println ( " " ) ; swap ( a , b , c , d ) ; } }

def swap ( a , b , c , d ) : NEW_LINE INDENT a = a + b NEW_LINE b = a - b NEW_LINE a = a - b NEW_LINE b = b + c NEW_LINE c = b - c NEW_LINE b = b - c NEW_LINE c = c + d NEW_LINE d = c - d NEW_LINE c = c - d NEW_LINE print ( " values ▁ after ▁ swapping ▁ are ▁ : ▁ " ) NEW_LINE print ( " a ▁ = ▁ " , a ) NEW_LINE print ( " b ▁ = ▁ " , b ) NEW_LINE print ( " c ▁ = ▁ " , c ) NEW_LINE print ( " d ▁ = ▁ " , d ) NEW_LINE DEDENT a = 1 NEW_LINE b = 2 NEW_LINE c = 3 NEW_LINE d = 4 NEW_LINE print ( " values ▁ after ▁ swapping ▁ are ▁ : ▁ " ) NEW_LINE print ( " a ▁ = ▁ " , a ) NEW_LINE print ( " b ▁ = ▁ " , b ) NEW_LINE print ( " c ▁ = ▁ " , c ) NEW_LINE print ( " d ▁ = ▁ " , d ) NEW_LINE print ( " " ) NEW_LINE swap ( a , b , c , d ) NEW_LINE


class GFG { static int maxProfit ( int [ ] price , int n , int k ) { int [ ] [ ] profit = new int [ k + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= k ; i ++ ) profit [ i ] [ 0 ] = 0 ; for ( int j = 0 ; j <= n ; j ++ ) profit [ 0 ] [ j ] = 0 ; for ( int i = 1 ; i <= k ; i ++ ) { for ( int j = 1 ; j < n ; j ++ ) { int max_so_far = 0 ; for ( int m = 0 ; m < j ; m ++ ) max_so_far = Math . max ( max_so_far , price [ j ] - price [ m ] + profit [ i - 1 ] [ m ] ) ; profit [ i ] [ j ] = Math . max ( profit [ i ] [ j - 1 ] , max_so_far ) ; } } return profit [ k ] [ n - 1 ] ; } public static void main ( String [ ] args ) { int k = 2 ; int [ ] price = { 10 , 22 , 5 , 75 , 65 , 80 } ; int n = price . length ; System . out . println ( " Maximum ▁ profit ▁ is : ▁ " + maxProfit ( price , n , k ) ) ; } }

def maxProfit ( prices , n , k ) : NEW_LINE INDENT profit = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n ) ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 1 , k + 1 ) : NEW_LINE INDENT max_so_far = 0 NEW_LINE for l in range ( i ) : NEW_LINE INDENT max_so_far = max ( max_so_far , prices [ i ] - prices [ l ] + profit [ l ] [ j - 1 ] ) NEW_LINE DEDENT profit [ i ] [ j ] = max ( profit [ i - 1 ] [ j ] , max_so_far ) NEW_LINE DEDENT DEDENT return profit [ n - 1 ] [ k ] NEW_LINE DEDENT k = 2 NEW_LINE prices = [ 10 , 22 , 5 , 75 , 65 , 80 ] NEW_LINE n = len ( prices ) NEW_LINE print ( " Maximum ▁ profit ▁ is : " , maxProfit ( prices , n , k ) ) NEW_LINE


import java . util . * ; class GFG { static void block ( long x ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; System . out . print ( " Blocks ▁ for ▁ " + x + " ▁ : ▁ " ) ; while ( x > 0 ) { v . add ( ( int ) x % 2 ) ; x = x / 2 ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) { if ( v . get ( i ) == 1 ) { System . out . print ( i ) ; if ( i != v . size ( ) - 1 ) System . out . print ( " , ▁ " ) ; } } System . out . println ( ) ; } public static void main ( String args [ ] ) { block ( 71307 ) ; block ( 1213 ) ; block ( 29 ) ; block ( 100 ) ; } }

def block ( x ) : NEW_LINE INDENT v = [ ] NEW_LINE print ( " Blocks ▁ for ▁ % d ▁ : ▁ " % x , end = " " ) NEW_LINE while ( x > 0 ) : NEW_LINE INDENT v . append ( int ( x % 2 ) ) NEW_LINE x = int ( x / 2 ) NEW_LINE DEDENT for i in range ( 0 , len ( v ) ) : NEW_LINE INDENT if ( v [ i ] == 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE if ( i != len ( v ) - 1 ) : NEW_LINE INDENT print ( " , ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT print ( " \n " ) NEW_LINE DEDENT block ( 71307 ) NEW_LINE block ( 1213 ) NEW_LINE block ( 29 ) NEW_LINE block ( 100 ) NEW_LINE


class GFG { static int minIndex ( int arr [ ] , int n , int pos ) { int num = arr [ pos ] ; int i = pos - 1 ; while ( i >= 0 ) { if ( arr [ i ] != num ) break ; i -- ; } return i + 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 1 , 1 , 1 , 5 , 2 } ; int n = arr . length ; int pos = 4 ; System . out . println ( minIndex ( arr , n , pos ) ) ; } }

def minIndex ( arr , n , pos ) : NEW_LINE INDENT num = arr [ pos ] NEW_LINE i = pos - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( arr [ i ] != num ) : NEW_LINE INDENT break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT return i + 1 NEW_LINE DEDENT arr = [ 2 , 1 , 1 , 1 , 5 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE pos = 4 NEW_LINE print ( minIndex ( arr , n , pos ) ) NEW_LINE


import java . util . * ; class GFG { static boolean IsLarger ( int [ ] X , int [ ] Y , int n ) { for ( int i = 0 ; i < n ; i ++ ) { if ( X [ i ] < Y [ i ] ) { return false ; } } return true ; } static int solve ( int X [ ] , int Y [ ] , int n ) { int ans = 0 ; if ( IsLarger ( X , Y , n ) ) ans = 0 ; else { int d = Y [ 0 ] - X [ 0 ] ; for ( int i = 0 ; i < n ; i ++ ) X [ i ] += d ; if ( IsLarger ( X , Y , n ) ) ans = d ; else { ans = d + 1 ; } } return ans ; } public static void main ( String [ ] args ) { int X [ ] = { 2 , 3 , 6 , 9 } ; int Y [ ] = { 3 , 4 , 8 , 1 } ; int n = X . length ; System . out . println ( solve ( X , Y , n ) ) ; } }

def IsLarger ( X , Y , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( X [ i ] < Y [ i ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def solve ( X , Y , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE if ( IsLarger ( X , Y , n ) ) : NEW_LINE INDENT ans = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT d = Y [ 0 ] - X [ 0 ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT X [ i ] += d ; NEW_LINE DEDENT if ( IsLarger ( X , Y , n ) ) : NEW_LINE INDENT ans = d ; NEW_LINE DEDENT else : NEW_LINE INDENT ans = d + 1 ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = [ 2 , 3 , 6 , 9 ] ; NEW_LINE Y = [ 3 , 4 , 8 , 1 ] ; NEW_LINE n = len ( X ) ; NEW_LINE print ( solve ( X , Y , n ) ) ; NEW_LINE DEDENT


class ReverseDigits { static int reversDigits ( int num ) { boolean negativeFlag = false ; if ( num < 0 ) { negativeFlag = true ; num = - num ; } int prev_rev_num = 0 , rev_num = 0 ; while ( num != 0 ) { int curr_digit = num % 10 ; rev_num = ( rev_num * 10 ) + curr_digit ; if ( ( rev_num - curr_digit ) / 10 != prev_rev_num ) { System . out . println ( " WARNING ▁ OVERFLOWED ! ! ! " ) ; return 0 ; } prev_rev_num = rev_num ; num = num / 10 ; } return ( negativeFlag == true ) ? - rev_num : rev_num ; } public static void main ( String [ ] args ) { int num = 12345 ; System . out . println ( " Reverse ▁ of ▁ no . ▁ is ▁ " + reversDigits ( num ) ) ; num = 1000000045 ; System . out . println ( " Reverse ▁ of ▁ no . ▁ is ▁ " + reversDigits ( num ) ) ; } }

def reversDigits ( num ) : NEW_LINE INDENT negativeFlag = False NEW_LINE if ( num < 0 ) : NEW_LINE INDENT negativeFlag = True NEW_LINE num = - num NEW_LINE DEDENT prev_rev_num = 0 NEW_LINE rev_num = 0 NEW_LINE while ( num != 0 ) : NEW_LINE INDENT curr_digit = num % 10 NEW_LINE rev_num = ( rev_num * 10 ) + curr_digit NEW_LINE if ( rev_num >= 2147483647 or rev_num <= - 2147483648 ) : NEW_LINE INDENT rev_num = 0 NEW_LINE DEDENT if ( ( rev_num - curr_digit ) // 10 != prev_rev_num ) : NEW_LINE INDENT print ( " WARNING ▁ OVERFLOWED ! ! ! " ) NEW_LINE return 0 NEW_LINE DEDENT prev_rev_num = rev_num NEW_LINE num = num // 10 NEW_LINE DEDENT return - rev_num if ( negativeFlag == True ) else rev_num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT num = 12345 NEW_LINE print ( " Reverse ▁ of ▁ no . ▁ is ▁ " , reversDigits ( num ) ) NEW_LINE num = 1000000045 NEW_LINE print ( " Reverse ▁ of ▁ no . ▁ is ▁ " , reversDigits ( num ) ) NEW_LINE DEDENT


class GFG { static int replaceDigit ( int x , int d1 , int d2 ) { int result = 0 , multiply = 1 ; while ( x % 10 > 0 ) { int remainder = x % 10 ; if ( remainder == d1 ) result = result + d2 * multiply ; else result = result + remainder * multiply ; multiply *= 10 ; x = x / 10 ; } return result ; } public static void main ( String [ ] args ) { int x = 645 , d1 = 6 , d2 = 5 ; System . out . println ( replaceDigit ( x , d1 , d2 ) ) ; } }

def replaceDigit ( x , d1 , d2 ) : NEW_LINE INDENT result = 0 ; NEW_LINE multiply = 1 ; NEW_LINE while ( x % 10 > 0 ) : NEW_LINE INDENT remainder = x % 10 ; NEW_LINE if ( remainder == d1 ) : NEW_LINE INDENT result = ( result + d2 * multiply ) ; NEW_LINE DEDENT else : NEW_LINE INDENT result = ( result + remainder * multiply ) ; NEW_LINE DEDENT multiply *= 10 ; NEW_LINE x = int ( x / 10 ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT x = 645 ; NEW_LINE d1 = 6 ; NEW_LINE d2 = 5 ; NEW_LINE print ( replaceDigit ( x , d1 , d2 ) ) ; NEW_LINE


class GFG { static void shortestLength ( int n , int x [ ] , int y [ ] ) { int answer = 0 ; int i = 0 ; while ( n != 0 && i < x . length ) { if ( x [ i ] + y [ i ] > answer ) answer = x [ i ] + y [ i ] ; i ++ ; } System . out . println ( " Length ▁ - > ▁ " + answer ) ; System . out . println ( " Path ▁ - > ▁ " + " ( ▁ 1 , ▁ " + answer + " ▁ ) " + " and ▁ ( ▁ " + answer + " , ▁ 1 ▁ ) " ) ; } public static void main ( String [ ] args ) { int n = 4 ; int x [ ] = new int [ ] { 1 , 4 , 2 , 1 } ; int y [ ] = new int [ ] { 4 , 1 , 1 , 2 } ; shortestLength ( n , x , y ) ; } }

def shortestLength ( n , x , y ) : NEW_LINE INDENT answer = 0 NEW_LINE i = 0 NEW_LINE while n > 0 : NEW_LINE INDENT if ( x [ i ] + y [ i ] > answer ) : NEW_LINE INDENT answer = x [ i ] + y [ i ] NEW_LINE DEDENT i += 1 NEW_LINE n -= 1 NEW_LINE DEDENT print ( " Length ▁ - > ▁ " + str ( answer ) ) NEW_LINE print ( " Path ▁ - > ▁ " + " ( ▁ 1 , ▁ " + str ( answer ) + " ▁ ) " + " and ▁ ( ▁ " + str ( answer ) + " , ▁ 1 ▁ ) " ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE x = [ 1 , 4 , 2 , 1 ] NEW_LINE y = [ 4 , 1 , 1 , 2 ] NEW_LINE shortestLength ( n , x , y ) NEW_LINE DEDENT


class GFG { static int R = 5 ; static int C = 5 ; static int findMaxCock ( int ar [ ] [ ] ) { if ( R < 3 || C < 3 ) return - 1 ; int max_sum = Integer . MIN_VALUE ; for ( int i = 0 ; i < R - 2 ; i ++ ) { for ( int j = 0 ; j < C - 2 ; j ++ ) { int sum = ( ar [ i ] [ j ] + ar [ i ] [ j + 2 ] ) + ( ar [ i + 1 ] [ j + 1 ] ) + ( ar [ i + 2 ] [ j ] + ar [ i + 2 ] [ j + 1 ] + ar [ i + 2 ] [ j + 2 ] ) ; max_sum = Math . max ( max_sum , sum ) ; } } return max_sum ; } public static void main ( String [ ] args ) { int ar [ ] [ ] = { { 0 , 3 , 0 , 6 , 0 } , { 0 , 1 , 1 , 0 , 0 } , { 1 , 1 , 1 , 0 , 0 } , { 0 , 0 , 2 , 0 , 1 } , { 0 , 2 , 0 , 1 , 3 } } ; System . out . println ( findMaxCock ( ar ) ) ; } }

import sys NEW_LINE R = 5 NEW_LINE C = 5 NEW_LINE def findMaxCock ( ar ) : NEW_LINE INDENT if ( R < 3 or C < 3 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT max_sum = - sys . maxsize - 1 NEW_LINE for i in range ( R - 2 ) : NEW_LINE INDENT for j in range ( C - 2 ) : NEW_LINE INDENT sum = ( ( ar [ i ] [ j ] + ar [ i ] [ j + 2 ] ) + ( ar [ i + 1 ] [ j + 1 ] ) + ( ar [ i + 2 ] [ j ] + ar [ i + 2 ] [ j + 1 ] + ar [ i + 2 ] [ j + 2 ] ) ) NEW_LINE max_sum = max ( max_sum , sum ) NEW_LINE DEDENT DEDENT return max_sum ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT ar = [ [ 0 , 3 , 0 , 6 , 0 ] , [ 0 , 1 , 1 , 0 , 0 ] , [ 1 , 1 , 1 , 0 , 0 ] , [ 0 , 0 , 2 , 0 , 1 ] , [ 0 , 2 , 0 , 1 , 3 ] ] NEW_LINE print ( findMaxCock ( ar ) ) NEW_LINE DEDENT


class GFG { static int numLen ( int K ) { if ( K % 2 == 0 || K % 5 == 0 ) { return - 1 ; } int number = 0 ; int len = 1 ; for ( len = 1 ; len <= K ; len ++ ) { number = number * 10 + 1 ; if ( ( number % K == 0 ) ) { return len ; } } return - 1 ; } public static void main ( String [ ] args ) { int K = 7 ; System . out . println ( numLen ( K ) ) ; } }

def numLen ( K ) : NEW_LINE INDENT if ( K % 2 == 0 or K % 5 == 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT number = 0 ; NEW_LINE len = 1 ; NEW_LINE for len in range ( 1 , K + 1 ) : NEW_LINE INDENT number = number * 10 + 1 ; NEW_LINE if ( ( number % K == 0 ) ) : NEW_LINE INDENT return len ; NEW_LINE DEDENT DEDENT return - 1 ; NEW_LINE DEDENT K = 7 ; NEW_LINE print ( numLen ( K ) ) ; NEW_LINE


import java . io . * ; class GFG { private static int minBroadcastRange ( int [ ] houses , int [ ] towers ) { int n = houses . length ; int m = towers . length ; int leftTower = Integer . MIN_VALUE ; int rightTower = towers [ 0 ] ; int j = 0 , k = 0 ; int min_range = 0 ; while ( j < n ) { if ( houses [ j ] < rightTower ) { int left = houses [ j ] - leftTower ; int right = rightTower - houses [ j ] ; int local_max = left < right ? left : right ; if ( local_max > min_range ) min_range = local_max ; j ++ ; } else { leftTower = towers [ k ] ; if ( k < m - 1 ) { k ++ ; rightTower = towers [ k ] ; } else rightTower = Integer . MAX_VALUE ; } } return min_range ; } public static void main ( String [ ] args ) { int [ ] a = { 12 , 13 , 11 , 80 } ; int [ ] b = { 4 , 6 , 15 , 60 } ; int max = minBroadcastRange ( a , b ) ; System . out . println ( max ) ; } }

import sys NEW_LINE def minBroadcastRange ( houses , towers , n , m ) : NEW_LINE INDENT leftTower = - sys . maxsize - 1 NEW_LINE rightTower = towers [ 0 ] NEW_LINE j , k = 0 , 0 NEW_LINE min_range = 0 NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( houses [ j ] < rightTower ) : NEW_LINE INDENT left = houses [ j ] - leftTower NEW_LINE right = rightTower - houses [ j ] NEW_LINE if left < right : NEW_LINE INDENT local_max = left NEW_LINE DEDENT else : NEW_LINE INDENT local_max = right NEW_LINE DEDENT if ( local_max > min_range ) : NEW_LINE INDENT min_range = local_max NEW_LINE DEDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT leftTower = towers [ k ] NEW_LINE if ( k < m - 1 ) : NEW_LINE INDENT k += 1 NEW_LINE rightTower = towers [ k ] NEW_LINE DEDENT else : NEW_LINE INDENT rightTower = sys . maxsize NEW_LINE DEDENT DEDENT DEDENT return min_range NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 12 , 13 , 11 , 80 ] NEW_LINE b = [ 4 , 6 , 15 , 60 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE max = minBroadcastRange ( a , b , n , m ) NEW_LINE print ( max ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; public class GFG { static List < Integer > v = new ArrayList < Integer > ( ) ; static void printSequence ( int n , int s , int k ) { if ( s == 0 ) { for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( v . get ( i ) + " ▁ " ) ; System . out . println ( ) ; return ; } for ( int i = 0 ; i < k ; i ++ ) { v . add ( n + i ) ; printSequence ( n + i , s - 1 , k ) ; v . remove ( v . size ( ) - 1 ) ; } for ( int i = 1 ; i < k ; i ++ ) { v . add ( n - i ) ; printSequence ( n - i , s - 1 , k ) ; v . remove ( v . size ( ) - 1 ) ; } } static void wrapper ( int n , int s , int k ) { v . add ( n ) ; printSequence ( n , s - 1 , k ) ; } public static void main ( String args [ ] ) { int n = 5 , s = 3 , k = 2 ; wrapper ( n , s , k ) ; } }

def printSequence ( v , n , s , k ) : NEW_LINE INDENT if ( s == 0 ) : NEW_LINE INDENT for i in range ( 0 , len ( v ) ) : NEW_LINE INDENT print ( " { } ▁ " . format ( v [ i ] ) , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE return ; NEW_LINE DEDENT for i in range ( 0 , k ) : NEW_LINE INDENT v . append ( n + i ) NEW_LINE printSequence ( v , n + i , s - 1 , k ) NEW_LINE v . pop ( ) NEW_LINE DEDENT for i in range ( 1 , k ) : NEW_LINE INDENT v . append ( n - i ) NEW_LINE printSequence ( v , n - i , s - 1 , k ) NEW_LINE v . pop ( ) NEW_LINE DEDENT DEDENT def wrapper ( n , s , k ) : NEW_LINE INDENT v = [ ] NEW_LINE v . append ( n ) NEW_LINE printSequence ( v , n , s - 1 , k ) NEW_LINE DEDENT n = 5 ; s = 3 ; k = 2 ; NEW_LINE wrapper ( n , s , k ) ; NEW_LINE


import java . io . * ; class GFG { static int rev ( int n , int temp ) { if ( n == 0 ) return temp ; temp = ( temp * 10 ) + ( n % 10 ) ; return rev ( n / 10 , temp ) ; } public static void main ( String [ ] args ) { int n = 121 ; int temp = rev ( n , 0 ) ; if ( temp == n ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }

def rev ( n , temp ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return temp ; NEW_LINE DEDENT temp = ( temp * 10 ) + ( n % 10 ) ; NEW_LINE return rev ( n / 10 , temp ) ; NEW_LINE DEDENT n = 121 ; NEW_LINE temp = rev ( n , 0 ) ; NEW_LINE if ( temp != n ) : NEW_LINE INDENT print ( " yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) ; NEW_LINE DEDENT


public class GFG { static int NumberOfRectangles ( int n , int m ) { if ( n % 2 == 0 ) return ( n / 2 ) * m ; else if ( m % 2 == 0 ) return ( m / 2 ) * n ; return ( n * m - 1 ) / 2 ; } public static void main ( String args [ ] ) { int n = 3 , m = 3 ; System . out . println ( NumberOfRectangles ( n , m ) ) ; } }

def NumberOfRectangles ( n , m ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return ( n / 2 ) * m NEW_LINE DEDENT elif ( m % 2 == 0 ) : NEW_LINE INDENT return ( m // 2 ) * n NEW_LINE DEDENT return ( n * m - 1 ) // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE m = 3 NEW_LINE print ( NumberOfRectangles ( n , m ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static String decToBin ( int n ) { if ( n == 0 ) return "0" ; String bin = " " ; while ( n > 0 ) { bin = ( ( n & 1 ) == 0 ? '0' : '1' ) + bin ; n >>= 1 ; } return bin ; } public static void main ( String [ ] args ) { int n = 38 ; System . out . println ( decToBin ( n ) ) ; } }

def decToBin ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return "0" ; NEW_LINE DEDENT bin = " " ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT if ( n & 1 == 0 ) : NEW_LINE INDENT bin = '0' + bin ; NEW_LINE DEDENT else : NEW_LINE INDENT bin = '1' + bin ; NEW_LINE DEDENT n = n >> 1 ; NEW_LINE DEDENT return bin ; NEW_LINE DEDENT n = 38 ; NEW_LINE print ( decToBin ( n ) ) ; NEW_LINE


import java . util . * ; class GFG { static int maxSubarrayLength ( int [ ] A , int N ) { int maxLen = 0 ; int curr_sum = 0 ; HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( A [ i ] % 2 == 0 ) curr_sum -= 1 ; else curr_sum += 1 ; if ( curr_sum == 0 ) maxLen = Math . max ( maxLen , i + 1 ) ; if ( hash . containsKey ( curr_sum ) ) maxLen = Math . max ( maxLen , i - hash . get ( curr_sum ) ) ; else { hash . put ( curr_sum , i ) ; } } return maxLen ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 4 , 7 , 8 , 9 , 2 , 11 , 0 , 2 , 13 } ; int n = arr . length ; System . out . print ( maxSubarrayLength ( arr , n ) ) ; } }

def maxSubarrayLength ( A , N ) : NEW_LINE INDENT maxLen = 0 ; NEW_LINE curr_sum = 0 ; NEW_LINE hash = { } ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] % 2 == 0 ) : NEW_LINE INDENT curr_sum -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT curr_sum += 1 ; NEW_LINE DEDENT if ( curr_sum == 0 ) : NEW_LINE INDENT maxLen = max ( maxLen , i + 1 ) ; NEW_LINE DEDENT if curr_sum in hash : NEW_LINE INDENT maxLen = max ( maxLen , i - hash [ curr_sum ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT hash [ curr_sum ] = i ; NEW_LINE DEDENT DEDENT return maxLen ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 12 , 4 , 7 , 8 , 9 , 2 , 11 , 0 , 2 , 13 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( maxSubarrayLength ( arr , n ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static void translate ( char str [ ] ) { int len = str . length ; if ( len < 2 ) return ; int i = 0 ; int j = 0 ; while ( j < len - 1 ) { if ( str [ j ] == ' A ' && str [ j + 1 ] == ' B ' ) { j = j + 2 ; str [ i ++ ] = ' C ' ; continue ; } str [ i ++ ] = str [ j ++ ] ; } if ( j == len - 1 ) str [ i ++ ] = str [ j ] ; str [ i ] = ' ▁ ' ; str [ len - 1 ] = ' ▁ ' ; } public static void main ( String args [ ] ) { String st = " helloABworldABGfG " ; char str [ ] = st . toCharArray ( ) ; translate ( str ) ; System . out . println ( " The ▁ modified ▁ string ▁ is ▁ : " ) ; System . out . println ( str ) ; } }

def translate ( st ) : NEW_LINE INDENT l = len ( st ) NEW_LINE if ( l < 2 ) : NEW_LINE INDENT return NEW_LINE DEDENT i = 0 NEW_LINE j = 0 NEW_LINE while ( j < l - 1 ) : NEW_LINE INDENT if ( st [ j ] == ' A ' and st [ j + 1 ] == ' B ' ) : NEW_LINE INDENT j += 2 NEW_LINE st [ i ] = ' C ' NEW_LINE i += 1 NEW_LINE continue NEW_LINE DEDENT st [ i ] = st [ j ] NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT if ( j == l - 1 ) : NEW_LINE INDENT st [ i ] = st [ j ] NEW_LINE i += 1 NEW_LINE DEDENT st [ i ] = ' ▁ ' NEW_LINE st [ l - 1 ] = ' ▁ ' NEW_LINE DEDENT st = list ( " helloABworldABGfG " ) NEW_LINE translate ( st ) NEW_LINE print ( " The ▁ modified ▁ string ▁ is ▁ : " ) NEW_LINE print ( ' ' . join ( st ) ) NEW_LINE


import java . util . Scanner ; class GFG { public static void main ( String [ ] args ) { Scanner sc = new Scanner ( System . in ) ; int a , b , i , j , flag ; System . out . printf ( " Enter ▁ lower ▁ bound ▁ of ▁ the ▁ interval : ▁ " ) ; a = sc . nextInt ( ) ; System . out . printf ( " \n Enter ▁ upper ▁ bound ▁ of ▁ the ▁ interval : ▁ " ) ; b = sc . nextInt ( ) ; System . out . printf ( " \n Prime ▁ numbers ▁ between ▁ % d ▁ and ▁ % d ▁ are : ▁ " , a , b ) ; if ( a == 1 ) { System . out . println ( a ) ; a ++ ; if ( b >= 2 ) { System . out . println ( a ) ; a ++ ; } } if ( a == 2 ) System . out . println ( a ) ; if ( a % 2 == 0 ) a ++ ; for ( i = a ; i <= b ; i = i + 2 ) { flag = 1 ; for ( j = 2 ; j * j <= i ; ++ j ) { if ( i % j == 0 ) { flag = 0 ; break ; } } if ( flag == 1 ) System . out . println ( i ) ; } } }

if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a , b , i , j = 0 , 0 , 0 , 0 NEW_LINE print ( " Enter ▁ lower ▁ bound ▁ of ▁ the ▁ interval : " , end = " " ) NEW_LINE a = int ( input ( ) ) NEW_LINE print ( a ) NEW_LINE print ( " Enter ▁ upper ▁ bound ▁ of ▁ the ▁ interval : " , end = " " ) NEW_LINE b = int ( input ( ) ) NEW_LINE print ( b ) NEW_LINE print ( " Prime ▁ numbers ▁ between " , a , " and " , b , " are : " , end = " " ) NEW_LINE if ( a == 1 ) : NEW_LINE INDENT print ( a , end = " ▁ " ) NEW_LINE a += 1 NEW_LINE if ( b >= 2 ) : NEW_LINE INDENT print ( a , end = " ▁ " ) NEW_LINE a += 1 NEW_LINE DEDENT DEDENT if ( a == 2 ) : NEW_LINE INDENT print ( a , end = " ▁ " ) NEW_LINE DEDENT if ( a % 2 == 0 ) : NEW_LINE INDENT a += 1 NEW_LINE DEDENT for i in range ( a , b + 1 , 2 ) : NEW_LINE INDENT flag = 1 NEW_LINE j = 2 NEW_LINE while ( j * j <= i ) : NEW_LINE INDENT if ( i % j == 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE break NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if ( flag == 1 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT


class GfG { static String K_String ( String s , int k ) { int n = s . length ( ) ; int fre [ ] = new int [ 26 ] ; for ( int i = 0 ; i < n ; i ++ ) fre [ s . charAt ( i ) - ' a ' ] ++ ; String str = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( fre [ i ] % k == 0 ) { int x = fre [ i ] / k ; while ( x != 0 ) { str += ( char ) ( i + ' a ' ) ; x -- ; } } else { return " - 1" ; } } return str ; } public static void main ( String [ ] args ) { String s = " aabb " ; int k = 2 ; System . out . println ( K_String ( s , k ) ) ; } }

def K_String ( s , k ) : NEW_LINE INDENT n = len ( s ) NEW_LINE fre = [ 0 ] * 26 NEW_LINE for i in range ( n ) : NEW_LINE INDENT fre [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT str = " " NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT if ( fre [ i ] % k == 0 ) : NEW_LINE INDENT x = fre [ i ] // k NEW_LINE while ( x ) : NEW_LINE INDENT str += chr ( i + ord ( ' a ' ) ) NEW_LINE x -= 1 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return " - 1" NEW_LINE DEDENT DEDENT return str NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aabb " NEW_LINE k = 2 NEW_LINE print ( K_String ( s , k ) ) NEW_LINE DEDENT