import java . util . * ; class GFG { static boolean isShuffledSubString ( String A , String B ) { int n = A . length ( ) ; int m = B . length ( ) ; if ( n > m ) { return false ; } else { A = sort ( A ) ; for ( int i = 0 ; i < m ; i ++ ) { if ( i + n - 1 >= m ) return false ; String str = " " ; for ( int j = 0 ; j < n ; j ++ ) str += B . charAt ( i + j ) ; str = sort ( str ) ; if ( str . equals ( A ) ) return true ; } } return false ; } static String sort ( String inputString ) { char tempArray [ ] = inputString . toCharArray ( ) ; Arrays . sort ( tempArray ) ; return String . valueOf ( tempArray ) ; } public static void main ( String [ ] args ) { String str1 = " geekforgeeks " ; String str2 = " ekegorfkeegsgeek " ; boolean a = isShuffledSubString ( str1 , str2 ) ; if ( a ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; System . out . println ( ) ; } }

def isShuffledSubstring ( A , B ) : NEW_LINE INDENT n = len ( A ) NEW_LINE m = len ( B ) NEW_LINE if ( n > m ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT A = sorted ( A ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( i + n - 1 >= m ) : NEW_LINE INDENT return False NEW_LINE DEDENT Str = " " NEW_LINE for j in range ( n ) : NEW_LINE INDENT Str += ( B [ i + j ] ) NEW_LINE DEDENT Str = sorted ( Str ) NEW_LINE if ( Str == A ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT Str1 = " geekforgeeks " NEW_LINE Str2 = " ekegorfkeegsgeek " NEW_LINE a = isShuffledSubstring ( Str1 , Str2 ) NEW_LINE if ( a ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int findTileCount ( int x , int y , int l , int r ) { int lcm = ( x * y ) / __gcd ( x , y ) ; int countl = ( l - 1 ) / lcm ; int countr = r / lcm ; return countr - countl ; } static int __gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } public static void main ( String [ ] args ) { int x = 2 , y = 3 , l = 6 , r = 18 ; System . out . println ( findTileCount ( x , y , l , r ) ) ; } }

from math import gcd NEW_LINE def findTileCount ( x , y , l , r ) : NEW_LINE INDENT lcm = ( x * y ) // gcd ( x , y ) NEW_LINE count1 = ( l - 1 ) // lcm NEW_LINE countr = r // lcm NEW_LINE return countr - count1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y , l , r = 2 , 3 , 6 , 18 NEW_LINE print ( findTileCount ( x , y , l , r ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 6 ; static int subsetGraph ( int C [ ] [ ] ) { HashSet < Integer > vertices = new HashSet < > ( ) ; for ( int i = 0 ; i < N ; ++ i ) { vertices . add ( i ) ; } while ( ! vertices . isEmpty ( ) ) { if ( vertices . size ( ) == 1 ) { return 1 ; } boolean someone_removed = false ; for ( int x : vertices ) { HashSet < Integer > values = new HashSet < > ( ) ; for ( int y : vertices ) { if ( y != x ) { values . add ( C [ x ] [ y ] ) ; } } if ( values . size ( ) == 1 ) { vertices . remove ( x ) ; someone_removed = true ; break ; } } if ( ! someone_removed ) { break ; } } return ( vertices . size ( ) ) ; } public static void main ( String [ ] args ) { int C [ ] [ ] = { { 0 , 9 , 2 , 4 , 7 , 8 } , { 9 , 0 , 9 , 9 , 7 , 9 } , { 2 , 9 , 0 , 3 , 7 , 6 } , { 4 , 9 , 3 , 0 , 7 , 1 } , { 7 , 7 , 7 , 7 , 0 , 7 } , { 8 , 9 , 6 , 1 , 7 , 0 } } ; System . out . println ( subsetGraph ( C ) ) ; } }

def subsetGraph ( C ) : NEW_LINE INDENT global N NEW_LINE vertices = set ( ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT vertices . add ( i ) NEW_LINE DEDENT while ( len ( vertices ) != 0 ) : NEW_LINE INDENT if ( len ( vertices ) == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT someone_removed = False NEW_LINE for x in vertices : NEW_LINE INDENT values = set ( ) NEW_LINE for y in vertices : NEW_LINE INDENT if ( y != x ) : NEW_LINE INDENT values . add ( C [ x ] [ y ] ) NEW_LINE DEDENT DEDENT if ( len ( values ) == 1 ) : NEW_LINE INDENT vertices . remove ( x ) NEW_LINE someone_removed = True NEW_LINE break NEW_LINE DEDENT DEDENT if ( not someone_removed ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return len ( vertices ) NEW_LINE DEDENT N = 6 NEW_LINE C = [ [ 0 , 9 , 2 , 4 , 7 , 8 ] , [ 9 , 0 , 9 , 9 , 7 , 9 ] , [ 2 , 9 , 0 , 3 , 7 , 6 ] , [ 4 , 9 , 3 , 0 , 7 , 1 ] , [ 7 , 7 , 7 , 7 , 0 , 7 ] , [ 8 , 9 , 6 , 1 , 7 , 0 ] ] NEW_LINE print ( subsetGraph ( C ) ) NEW_LINE


class GFG { int binarySearch ( int arr [ ] , int l , int r , int x ) { if ( r >= l ) { int mid = l + ( r - l ) / 2 ; if ( arr [ mid ] == x ) return mid ; if ( mid > l && arr [ mid - 1 ] == x ) return ( mid - 1 ) ; if ( mid < r && arr [ mid + 1 ] == x ) return ( mid + 1 ) ; if ( arr [ mid ] > x ) return binarySearch ( arr , l , mid - 2 , x ) ; return binarySearch ( arr , mid + 2 , r , x ) ; } return - 1 ; } public static void main ( String args [ ] ) { GFG ob = new GFG ( ) ; int arr [ ] = { 3 , 2 , 10 , 4 , 40 } ; int n = arr . length ; int x = 4 ; int result = ob . binarySearch ( arr , 0 , n - 1 , x ) ; if ( result == - 1 ) System . out . println ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) ; else System . out . println ( " Element ▁ is ▁ present ▁ at ▁ index ▁ " + result ) ; } }

def binarySearch ( arr , l , r , x ) : NEW_LINE INDENT if ( r >= l ) : NEW_LINE INDENT mid = int ( l + ( r - l ) / 2 ) NEW_LINE if ( arr [ mid ] == x ) : return mid NEW_LINE if ( mid > l and arr [ mid - 1 ] == x ) : NEW_LINE INDENT return ( mid - 1 ) NEW_LINE DEDENT if ( mid < r and arr [ mid + 1 ] == x ) : NEW_LINE INDENT return ( mid + 1 ) NEW_LINE DEDENT if ( arr [ mid ] > x ) : NEW_LINE INDENT return binarySearch ( arr , l , mid - 2 , x ) NEW_LINE DEDENT return binarySearch ( arr , mid + 2 , r , x ) NEW_LINE DEDENT return - 1 NEW_LINE DEDENT arr = [ 3 , 2 , 10 , 4 , 40 ] NEW_LINE n = len ( arr ) NEW_LINE x = 4 NEW_LINE result = binarySearch ( arr , 0 , n - 1 , x ) NEW_LINE if ( result == - 1 ) : NEW_LINE INDENT print ( " Element ▁ is ▁ not ▁ present ▁ in ▁ array " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Element ▁ is ▁ present ▁ at ▁ index " , result ) NEW_LINE DEDENT


import java . lang . Math ; class UglyNumber { int getNthUglyNo ( int n ) { int ugly [ ] = new int [ n ] ; int i2 = 0 , i3 = 0 , i5 = 0 ; int next_multiple_of_2 = 2 ; int next_multiple_of_3 = 3 ; int next_multiple_of_5 = 5 ; int next_ugly_no = 1 ; ugly [ 0 ] = 1 ; for ( int i = 1 ; i < n ; i ++ ) { next_ugly_no = Math . min ( next_multiple_of_2 , Math . min ( next_multiple_of_3 , next_multiple_of_5 ) ) ; ugly [ i ] = next_ugly_no ; if ( next_ugly_no == next_multiple_of_2 ) { i2 = i2 + 1 ; next_multiple_of_2 = ugly [ i2 ] * 2 ; } if ( next_ugly_no == next_multiple_of_3 ) { i3 = i3 + 1 ; next_multiple_of_3 = ugly [ i3 ] * 3 ; } if ( next_ugly_no == next_multiple_of_5 ) { i5 = i5 + 1 ; next_multiple_of_5 = ugly [ i5 ] * 5 ; } } return next_ugly_no ; } public static void main ( String args [ ] ) { int n = 150 ; UglyNumber obj = new UglyNumber ( ) ; System . out . println ( obj . getNthUglyNo ( n ) ) ; } }

def getNthUglyNo ( n ) : NEW_LINE INDENT ugly = [ 0 ] * n NEW_LINE ugly [ 0 ] = 1 NEW_LINE i2 = i3 = i5 = 0 NEW_LINE next_multiple_of_2 = 2 NEW_LINE next_multiple_of_3 = 3 NEW_LINE next_multiple_of_5 = 5 NEW_LINE for l in range ( 1 , n ) : NEW_LINE INDENT ugly [ l ] = min ( next_multiple_of_2 , next_multiple_of_3 , next_multiple_of_5 ) NEW_LINE if ugly [ l ] == next_multiple_of_2 : NEW_LINE INDENT i2 += 1 NEW_LINE next_multiple_of_2 = ugly [ i2 ] * 2 NEW_LINE DEDENT if ugly [ l ] == next_multiple_of_3 : NEW_LINE INDENT i3 += 1 NEW_LINE next_multiple_of_3 = ugly [ i3 ] * 3 NEW_LINE DEDENT if ugly [ l ] == next_multiple_of_5 : NEW_LINE INDENT i5 += 1 NEW_LINE next_multiple_of_5 = ugly [ i5 ] * 5 NEW_LINE DEDENT DEDENT return ugly [ - 1 ] NEW_LINE DEDENT def main ( ) : NEW_LINE INDENT n = 150 NEW_LINE print getNthUglyNo ( n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT main ( ) NEW_LINE DEDENT


class GFG { static boolean isMultipleof5 ( int n ) { while ( n > 0 ) n = n - 5 ; if ( n == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { int n = 19 ; if ( isMultipleof5 ( n ) == true ) System . out . printf ( " % d ▁ is ▁ multiple ▁ of ▁ 5 \n " , n ) ; else System . out . printf ( " % d ▁ is ▁ not ▁ a ▁ multiple ▁ of ▁ 5 \n " , n ) ; } }

def isMultipleof5 ( n ) : NEW_LINE INDENT while ( n > 0 ) : NEW_LINE INDENT n = n - 5 NEW_LINE DEDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 0 NEW_LINE DEDENT i = 19 NEW_LINE if ( isMultipleof5 ( i ) == 1 ) : NEW_LINE INDENT print ( i , " is ▁ multiple ▁ of ▁ 5" ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( i , " is ▁ not ▁ a ▁ multiple ▁ of ▁ 5" ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int fnMod ( int n ) { if ( n % 5 == 1 ) return 4 ; else return 0 ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( fnMod ( n ) ) ; n = 11 ; System . out . println ( fnMod ( n ) ) ; } }

def fnMod ( n ) : NEW_LINE INDENT if ( n % 5 == 1 ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT n = 10 NEW_LINE print ( fnMod ( n ) ) NEW_LINE n = 11 NEW_LINE print ( fnMod ( n ) ) NEW_LINE


class GFG { static double Probability ( double p [ ] , int n ) { double [ ] [ ] dp = new double [ n + 1 ] [ n + 1 ] ; dp [ 0 ] [ 0 ] = 1.0 ; for ( int i = 1 ; i <= n ; i += 1 ) { for ( int j = 0 ; j <= i ; j += 1 ) { if ( j == 0 ) dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) ; else dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 ] [ j - 1 ] * p [ i ] ; } } double ans = 0.0 ; for ( int i = ( n + 1 ) / 2 ; i <= n ; i += 1 ) ans += dp [ n ] [ i ] ; return ans ; } public static void main ( String [ ] args ) { double p [ ] = { 0.0 , 0.3 , 0.4 , 0.7 } ; int n = p . length - 1 ; System . out . println ( Probability ( p , n ) ) ; } }

import numpy as np NEW_LINE def Probability ( p , n ) : NEW_LINE INDENT dp = np . zeros ( ( n + 1 , n + 1 ) ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = 0.0 NEW_LINE DEDENT DEDENT dp [ 0 ] [ 0 ] = 1.0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) ; NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = ( dp [ i - 1 ] [ j ] * ( 1.0 - p [ i ] ) + dp [ i - 1 ] [ j - 1 ] * p [ i ] ) ; NEW_LINE DEDENT DEDENT DEDENT ans = 0.0 ; NEW_LINE for i in range ( ( n + 1 ) // 2 , n + 1 ) : NEW_LINE INDENT ans += dp [ n ] [ i ] ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT p = [ 0.0 , 0.3 , 0.4 , 0.7 ] ; NEW_LINE n = len ( p ) - 1 ; NEW_LINE print ( Probability ( p , n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int findElement ( int [ ] arr , int [ ] [ ] ranges , int rotations , int index ) { for ( int i = rotations - 1 ; i >= 0 ; i -- ) { int left = ranges [ i ] [ 0 ] ; int right = ranges [ i ] [ 1 ] ; if ( left <= index && right >= index ) { if ( index == left ) index = right ; else index -- ; } } return arr [ index ] ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 4 , 5 } ; int rotations = 2 ; int [ ] [ ] ranges = { { 0 , 2 } , { 0 , 3 } } ; int index = 1 ; System . out . println ( findElement ( arr , ranges , rotations , index ) ) ; } }

def findElement ( arr , ranges , rotations , index ) : NEW_LINE INDENT for i in range ( rotations - 1 , - 1 , - 1 ) : NEW_LINE INDENT left = ranges [ i ] [ 0 ] NEW_LINE right = ranges [ i ] [ 1 ] NEW_LINE if ( left <= index and right >= index ) : NEW_LINE INDENT if ( index == left ) : NEW_LINE INDENT index = right NEW_LINE DEDENT else : NEW_LINE INDENT index = index - 1 NEW_LINE DEDENT DEDENT DEDENT return arr [ index ] NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE rotations = 2 NEW_LINE ranges = [ [ 0 , 2 ] , [ 0 , 3 ] ] NEW_LINE index = 1 NEW_LINE print ( findElement ( arr , ranges , rotations , index ) ) NEW_LINE


class GFG { static int getMissingNo ( int a [ ] , int n ) { int total = 1 ; for ( int i = 2 ; i <= ( n + 1 ) ; i ++ ) { total += i ; total -= a [ i - 2 ] ; } return total ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 2 , 3 , 5 } ; System . out . println ( getMissingNo ( arr , arr . length ) ) ; } }

def getMissingNo ( a , n ) : NEW_LINE INDENT i , total = 0 , 1 NEW_LINE for i in range ( 2 , n + 2 ) : NEW_LINE INDENT total += i NEW_LINE total -= a [ i - 2 ] NEW_LINE DEDENT return total NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 5 ] NEW_LINE print ( getMissingNo ( arr , len ( arr ) ) ) NEW_LINE


import java . util . * ; class GFG { static int MAX = 100001 ; static ArrayList < Integer > p = new ArrayList < Integer > ( ) ; static void sieve ( ) { int [ ] isPrime = new int [ MAX + 1 ] ; for ( int i = 2 ; i <= MAX ; i ++ ) { if ( isPrime [ i ] == 0 ) { p . add ( i ) ; for ( int j = 2 ; i * j <= MAX ; j ++ ) isPrime [ i * j ] = 1 ; } } } static int phi ( int n ) { int res = n ; for ( int i = 0 ; p . get ( i ) * p . get ( i ) <= n ; i ++ ) { if ( n % p . get ( i ) == 0 ) { res -= ( res / p . get ( i ) ) ; while ( n % p . get ( i ) == 0 ) n /= p . get ( i ) ; } } if ( n > 1 ) res -= ( res / n ) ; return res ; } public static void main ( String [ ] args ) { sieve ( ) ; System . out . println ( phi ( 11 ) ) ; System . out . println ( phi ( 21 ) ) ; System . out . println ( phi ( 31 ) ) ; System . out . println ( phi ( 41 ) ) ; System . out . println ( phi ( 51 ) ) ; System . out . println ( phi ( 61 ) ) ; System . out . println ( phi ( 91 ) ) ; System . out . println ( phi ( 101 ) ) ; } }

MAX = 100001 ; NEW_LINE p = [ ] ; NEW_LINE def sieve ( ) : NEW_LINE INDENT isPrime = [ 0 ] * ( MAX + 1 ) ; NEW_LINE for i in range ( 2 , MAX + 1 ) : NEW_LINE INDENT if ( isPrime [ i ] == 0 ) : NEW_LINE INDENT p . append ( i ) ; NEW_LINE j = 2 ; NEW_LINE while ( i * j <= MAX ) : NEW_LINE INDENT isPrime [ i * j ] = 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def phi ( n ) : NEW_LINE INDENT res = n ; NEW_LINE i = 0 ; NEW_LINE while ( p [ i ] * p [ i ] <= n ) : NEW_LINE INDENT if ( n % p [ i ] == 0 ) : NEW_LINE INDENT res -= int ( res / p [ i ] ) ; NEW_LINE while ( n % p [ i ] == 0 ) : NEW_LINE INDENT n = int ( n / p [ i ] ) ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT if ( n > 1 ) : NEW_LINE INDENT res -= int ( res / n ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT sieve ( ) ; NEW_LINE print ( phi ( 11 ) ) ; NEW_LINE print ( phi ( 21 ) ) ; NEW_LINE print ( phi ( 31 ) ) ; NEW_LINE print ( phi ( 41 ) ) ; NEW_LINE print ( phi ( 51 ) ) ; NEW_LINE print ( phi ( 61 ) ) ; NEW_LINE print ( phi ( 91 ) ) ; NEW_LINE print ( phi ( 101 ) ) ; NEW_LINE


class GFG { static StringBuilder findMaximumNum ( StringBuilder str , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( k < 1 ) break ; if ( str . charAt ( i ) != '9' ) { str . setCharAt ( i , '9' ) ; k -- ; } } return str ; } public static void main ( String [ ] args ) { StringBuilder str = new StringBuilder ( "569431" ) ; int n = str . length ( ) ; int k = 3 ; System . out . println ( findMaximumNum ( str , n , k ) ) ; } }

def findMaximumNum ( st , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( k < 1 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( st [ i ] != '9' ) : NEW_LINE INDENT st = st [ 0 : i ] + '9' + st [ i + 1 : ] NEW_LINE k -= 1 NEW_LINE DEDENT DEDENT return st NEW_LINE DEDENT st = "569431" NEW_LINE n = len ( st ) NEW_LINE k = 3 NEW_LINE print ( findMaximumNum ( st , n , k ) ) NEW_LINE


public class GFG { static int calculateSum ( int n ) { int r1 = 2 , r2 = 3 ; int a1 = 1 , a2 = 1 ; return ( int ) ( a1 * ( Math . pow ( r1 , n ) - 1 ) / ( r1 - 1 ) + a2 * ( Math . pow ( r2 , n ) - 1 ) / ( r2 - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( " Sum ▁ = ▁ " + calculateSum ( n ) ) ; } }

from math import * NEW_LINE def calculateSum ( n ) : NEW_LINE INDENT r1 , r2 = 2 , 3 NEW_LINE a1 , a2 = 1 , 1 NEW_LINE return ( a1 * ( pow ( r1 , n ) - 1 ) // ( r1 - 1 ) + a2 * ( pow ( r2 , n ) - 1 ) // ( r2 - 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( " SUM ▁ = ▁ " , calculateSum ( n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void Divide ( int a , int b ) { long dividend = ( long ) a ; long divisor = ( long ) b ; long sign = ( dividend < 0 ) ^ ( divisor < 0 ) ? - 1 : 1 ; dividend = Math . abs ( dividend ) ; divisor = Math . abs ( divisor ) ; if ( divisor == 0 ) { System . out . println ( " Cannot ▁ Divide ▁ by ▁ 0" ) ; return ; } if ( dividend == 0 ) { System . out . println ( a + " ▁ / ▁ " + b + " ▁ is ▁ equal ▁ to ▁ : ▁ " + 0 ) ; return ; } if ( divisor == 1 ) { System . out . println ( a + " ▁ / ▁ " + b + " ▁ is ▁ equal ▁ to ▁ : ▁ " + sign * dividend ) ; return ; } System . out . println ( a + " ▁ / ▁ " + b + " ▁ is ▁ equal ▁ to ▁ : ▁ " + Math . floor ( sign * ( Math . exp ( Math . log ( dividend ) - Math . log ( divisor ) ) ) ) ) ; } public static void main ( String [ ] args ) { int a = 10 , b = 5 ; Divide ( a , b ) ; a = 49 ; b = - 7 ; Divide ( a , b ) ; } }

import math NEW_LINE def Divide ( a , b ) : NEW_LINE INDENT dividend = a ; NEW_LINE divisor = b ; NEW_LINE sign = - 1 if ( ( dividend < 0 ) ^ ( divisor < 0 ) ) else 1 ; NEW_LINE dividend = abs ( dividend ) ; NEW_LINE divisor = abs ( divisor ) ; NEW_LINE if ( divisor == 0 ) : NEW_LINE INDENT print ( " Cannot ▁ Divide ▁ by ▁ 0" ) ; NEW_LINE DEDENT if ( dividend == 0 ) : NEW_LINE INDENT print ( a , " / " , b , " is ▁ equal ▁ to ▁ : " , 0 ) ; NEW_LINE DEDENT if ( divisor == 1 ) : NEW_LINE INDENT print ( a , " / " , b , " is ▁ equal ▁ to ▁ : " , ( sign * dividend ) ) ; NEW_LINE DEDENT print ( a , " / " , b , " is ▁ equal ▁ to ▁ : " , math . floor ( sign * math . exp ( math . log ( dividend ) - math . log ( divisor ) ) ) ) ; NEW_LINE DEDENT a = 10 ; NEW_LINE b = 5 ; NEW_LINE Divide ( a , b ) ; NEW_LINE a = 49 ; NEW_LINE b = - 7 ; NEW_LINE Divide ( a , b ) ; NEW_LINE


class GFG { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; this . next = null ; } } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ - > ▁ " ) ; node = node . next ; } System . out . print ( " NULL " ) ; } static int cntNodes ( Node node ) { if ( node == null ) return 0 ; return ( 1 + cntNodes ( node . next ) ) ; } static void updateList ( Node head , int m ) { int cnt = cntNodes ( head ) ; if ( cnt != m && m < cnt ) { int skip = cnt - m ; Node prev = null ; Node curr = head ; while ( skip > 0 ) { prev = curr ; curr = curr . next ; skip -- ; } prev . next = null ; Node tempHead = head ; head = curr ; while ( curr . next != null ) curr = curr . next ; curr . next = tempHead ; } printList ( head ) ; } public static void main ( String [ ] args ) { Node head = new Node ( 4 ) ; head . next = new Node ( 5 ) ; head . next . next = new Node ( 6 ) ; head . next . next . next = new Node ( 1 ) ; head . next . next . next . next = new Node ( 2 ) ; head . next . next . next . next . next = new Node ( 3 ) ; int m = 3 ; updateList ( head , m ) ; } }

class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def printList ( node ) : NEW_LINE INDENT while ( node != None ) : NEW_LINE INDENT print ( node . data , " - > " , end = " ▁ " ) NEW_LINE node = node . next NEW_LINE DEDENT print ( " NULL " ) NEW_LINE DEDENT def cntNodes ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + cntNodes ( node . next ) ) NEW_LINE DEDENT def updateList ( head , m ) : NEW_LINE INDENT cnt = cntNodes ( head ) NEW_LINE if ( cnt != m and m < cnt ) : NEW_LINE INDENT skip = cnt - m NEW_LINE prev = None NEW_LINE curr = head NEW_LINE while ( skip > 0 ) : NEW_LINE INDENT prev = curr NEW_LINE curr = curr . next NEW_LINE skip -= 1 NEW_LINE DEDENT prev . next = None NEW_LINE tempHead = head NEW_LINE head = curr NEW_LINE while ( curr . next != None ) : NEW_LINE INDENT curr = curr . next NEW_LINE DEDENT curr . next = tempHead NEW_LINE DEDENT printList ( head ) NEW_LINE DEDENT head = newNode ( 4 ) NEW_LINE head . next = newNode ( 5 ) NEW_LINE head . next . next = newNode ( 6 ) NEW_LINE head . next . next . next = newNode ( 1 ) NEW_LINE head . next . next . next . next = newNode ( 2 ) NEW_LINE head . next . next . next . next . next = newNode ( 3 ) NEW_LINE m = 3 NEW_LINE updateList ( head , m ) NEW_LINE


import java . io . * ; class GFG { static void findNormal ( int A , int x , int y ) { int dif = A - x * 2 ; if ( y == ( 2 * x - x * x ) ) { if ( dif < 0 ) System . out . print ( ( 0 - dif ) + " y ▁ = ▁ " + " x " + ( ( 0 - x ) + ( y * dif ) ) ) ; else if ( dif > 0 ) System . out . print ( dif + " y ▁ = ▁ " + " - x + " + ( x + dif * y ) ) ; else System . out . print ( " x ▁ = ▁ " + x ) ; } else System . out . println ( " Not ▁ possible " ) ; } public static void main ( String [ ] args ) { int A = 2 , x = 2 , y = 0 ; findNormal ( A , x , y ) ; ; } }

def findNormal ( A , x , y ) : NEW_LINE INDENT dif = A - x * 2 NEW_LINE if ( y == ( 2 * x - x * x ) ) : NEW_LINE INDENT if ( dif < 0 ) : NEW_LINE INDENT print ( 0 - dif , " y ▁ = " , " x " , ( 0 - x ) + ( y * dif ) ) NEW_LINE DEDENT elif ( dif > 0 ) : NEW_LINE INDENT print ( dif , " y ▁ = " , " - ▁ x ▁ + " , x + dif * y ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " x ▁ = " , x ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " Not ▁ possible " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = 2 NEW_LINE x = 2 NEW_LINE y = 0 NEW_LINE findNormal ( A , x , y ) NEW_LINE DEDENT


class GFG { static int XorAscii ( String str , int len ) { int ans = ( str . charAt ( 0 ) ) ; for ( int i = 1 ; i < len ; i ++ ) { ans = ( ans ^ ( ( str . charAt ( i ) ) ) ) ; } return ans ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int len = str . length ( ) ; System . out . print ( XorAscii ( str , len ) + " \n " ) ; str = " GfG " ; len = str . length ( ) ; System . out . print ( XorAscii ( str , len ) ) ; } }

def XorAscii ( str1 , len1 ) : NEW_LINE INDENT ans = ord ( str1 [ 0 ] ) NEW_LINE for i in range ( 1 , len1 ) : NEW_LINE INDENT ans = ( ans ^ ( ord ( str1 [ i ] ) ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT str1 = " geeksforgeeks " NEW_LINE len1 = len ( str1 ) NEW_LINE print ( XorAscii ( str1 , len1 ) ) NEW_LINE str1 = " GfG " NEW_LINE len1 = len ( str1 ) NEW_LINE print ( XorAscii ( str1 , len1 ) ) NEW_LINE


class GFG { static int findGCDSum ( int n , int a [ ] ) { int GCDSum = 0 ; int tempGCD = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { tempGCD = 0 ; for ( int k = i ; k <= j ; k ++ ) { tempGCD = __gcd ( tempGCD , a [ k ] ) ; } GCDSum += tempGCD ; } } return GCDSum ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int totalSum = findGCDSum ( n , a ) ; System . out . print ( totalSum + " \n " ) ; } }

def findGCDSum ( n , a ) : NEW_LINE INDENT GCDSum = 0 ; NEW_LINE tempGCD = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT tempGCD = 0 ; NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT tempGCD = __gcd ( tempGCD , a [ k ] ) ; NEW_LINE DEDENT GCDSum += tempGCD ; NEW_LINE DEDENT DEDENT return GCDSum ; NEW_LINE DEDENT def __gcd ( a , b ) : NEW_LINE INDENT return a if ( b == 0 ) else __gcd ( b , a % b ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 ; NEW_LINE a = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE totalSum = findGCDSum ( n , a ) ; NEW_LINE print ( totalSum ) ; NEW_LINE DEDENT


class GFG { static int maxLen = 30 ; static int [ ] seg = new int [ 3 * maxLen ] ; static int build ( int l , int r , int in , int [ ] arr ) { if ( l == r ) return seg [ in ] = arr [ l ] ; int mid = ( l + r ) / 2 ; return seg [ in ] = __gcd ( build ( l , mid , 2 * in + 1 , arr ) , build ( mid + 1 , r , 2 * in + 2 , arr ) ) ; } static int query ( int l , int r , int l1 , int r1 , int in ) { if ( l1 <= l && r <= r1 ) return seg [ in ] ; if ( l > r1 || r < l1 ) return 0 ; int mid = ( l + r ) / 2 ; return __gcd ( query ( l , mid , l1 , r1 , 2 * in + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * in + 2 ) ) ; } static int findLen ( int [ ] arr , int n ) { build ( 0 , n - 1 , 0 , arr ) ; int i = 0 , j = 0 ; int ans = Integer . MAX_VALUE ; while ( i < n ) { while ( j < n && query ( 0 , n - 1 , i , j , 0 ) != 1 ) j ++ ; if ( j == n ) break ; ans = Math . min ( ( j - i + 1 ) , ans ) ; i ++ ; j = Math . max ( j , i ) ; } if ( ans == Integer . MAX_VALUE ) return - 1 ; else return ans ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 2 , 2 } ; int n = arr . length ; System . out . println ( findLen ( arr , n ) ) ; } }

from math import gcd as __gcd NEW_LINE maxLen = 30 NEW_LINE seg = [ 0 for i in range ( 3 * maxLen ) ] NEW_LINE def build ( l , r , inn , arr ) : NEW_LINE INDENT if ( l == r ) : NEW_LINE INDENT seg [ inn ] = arr [ l ] NEW_LINE return seg [ inn ] NEW_LINE DEDENT mid = ( l + r ) // 2 NEW_LINE seg [ inn ] = __gcd ( build ( l , mid , 2 * inn + 1 , arr ) , build ( mid + 1 , r , 2 * inn + 2 , arr ) ) NEW_LINE return seg [ inn ] NEW_LINE DEDENT def query ( l , r , l1 , r1 , inn ) : NEW_LINE INDENT if ( l1 <= l and r <= r1 ) : NEW_LINE INDENT return seg [ inn ] NEW_LINE DEDENT if ( l > r1 or r < l1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT mid = ( l + r ) // 2 NEW_LINE x = __gcd ( query ( l , mid , l1 , r1 , 2 * inn + 1 ) , query ( mid + 1 , r , l1 , r1 , 2 * inn + 2 ) ) NEW_LINE return x NEW_LINE DEDENT def findLen ( arr , n ) : NEW_LINE INDENT build ( 0 , n - 1 , 0 , arr ) NEW_LINE i = 0 NEW_LINE j = 0 NEW_LINE ans = 10 ** 9 NEW_LINE while ( i < n ) : NEW_LINE INDENT while ( j < n and query ( 0 , n - 1 , i , j , 0 ) != 1 ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( j == n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT ans = minn ( ( j - i + 1 ) , ans ) NEW_LINE i += 1 NEW_LINE j = max ( j , i ) NEW_LINE DEDENT if ( ans == 10 ** 9 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ans NEW_LINE DEDENT DEDENT arr = [ 2 , 2 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findLen ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static void CheckSubarray ( int arr [ ] , int N ) { int presum [ ] = new int [ N + 1 ] ; for ( int i = 1 ; i <= N ; i += 1 ) { presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] ; } for ( int i = 1 ; i <= N ; i += 1 ) { for ( int j = i ; j <= N ; j += 1 ) { if ( ( presum [ j ] - presum [ i - 1 ] ) % N == 0 ) { System . out . print ( ( i - 1 ) + " ▁ " + ( j - 1 ) ) ; return ; } } } System . out . print ( - 1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 7 , 5 , 3 , 7 } ; int N = arr . length ; CheckSubarray ( arr , N ) ; } }

def CheckSubarray ( arr , N ) : NEW_LINE INDENT presum = [ 0 for i in range ( N + 1 ) ] NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT presum [ i ] = presum [ i - 1 ] + arr [ i - 1 ] NEW_LINE DEDENT for i in range ( 1 , N + 1 ) : NEW_LINE INDENT for j in range ( i , N + 1 ) : NEW_LINE INDENT if ( ( presum [ j ] - presum [ i - 1 ] ) % N == 0 ) : NEW_LINE INDENT print ( i - 1 , j - 1 ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT print ( " - 1" ) NEW_LINE DEDENT arr = [ 7 , 5 , 3 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE CheckSubarray ( arr , N ) NEW_LINE


class GFG { static double [ ] find_Centroid ( double v [ ] [ ] ) { double [ ] ans = new double [ 2 ] ; int n = v . length ; double signedArea = 0 ; for ( int i = 0 ; i < n ; i ++ ) { double x0 = v [ i ] [ 0 ] , y0 = v [ i ] [ 1 ] ; double x1 = v [ ( i + 1 ) % n ] [ 0 ] , y1 = v [ ( i + 1 ) % n ] [ 1 ] ; double A = ( x0 * y1 ) - ( x1 * y0 ) ; signedArea += A ; ans [ 0 ] += ( x0 + x1 ) * A ; ans [ 1 ] += ( y0 + y1 ) * A ; } signedArea *= 0.5 ; ans [ 0 ] = ( ans [ 0 ] ) / ( 6 * signedArea ) ; ans [ 1 ] = ( ans [ 1 ] ) / ( 6 * signedArea ) ; return ans ; } public static void main ( String [ ] args ) { double vp [ ] [ ] = { { 1 , 2 } , { 3 , - 4 } , { 6 , - 7 } } ; double [ ] ans = find_Centroid ( vp ) ; System . out . println ( ans [ 0 ] + " ▁ " + ans [ 1 ] ) ; } }

def find_Centroid ( v ) : NEW_LINE INDENT ans = [ 0 , 0 ] NEW_LINE n = len ( v ) NEW_LINE signedArea = 0 NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT x0 = v [ i ] [ 0 ] NEW_LINE y0 = v [ i ] [ 1 ] NEW_LINE x1 = v [ ( i + 1 ) % n ] [ 0 ] NEW_LINE y1 = v [ ( i + 1 ) % n ] [ 1 ] NEW_LINE A = ( x0 * y1 ) - ( x1 * y0 ) NEW_LINE signedArea += A NEW_LINE ans [ 0 ] += ( x0 + x1 ) * A NEW_LINE ans [ 1 ] += ( y0 + y1 ) * A NEW_LINE DEDENT signedArea *= 0.5 NEW_LINE ans [ 0 ] = ( ans [ 0 ] ) / ( 6 * signedArea ) NEW_LINE ans [ 1 ] = ( ans [ 1 ] ) / ( 6 * signedArea ) NEW_LINE return ans NEW_LINE DEDENT vp = [ [ 1 , 2 ] , [ 3 , - 4 ] , [ 6 , - 7 ] ] NEW_LINE ans = find_Centroid ( vp ) NEW_LINE print ( round ( ans [ 0 ] , 12 ) , ans [ 1 ] ) NEW_LINE


import java . io . * ; class GFG { static boolean checkDivisibility ( int n , int digit ) { return ( digit != 0 && n % digit == 0 ) ; } static boolean allDigitsDivide ( int n ) { int temp = n ; while ( temp > 0 ) { int digit = n % 10 ; if ( ( checkDivisibility ( n , digit ) ) == false ) return false ; temp /= 10 ; } return true ; } public static void main ( String args [ ] ) { int n = 128 ; if ( allDigitsDivide ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def checkDivisibility ( n , digit ) : NEW_LINE INDENT return ( digit != 0 and n % digit == 0 ) NEW_LINE DEDENT def allDigitsDivide ( n ) : NEW_LINE INDENT temp = n NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT digit = n % 10 NEW_LINE if ( ( checkDivisibility ( n , digit ) ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT temp = temp // 10 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 128 NEW_LINE if ( allDigitsDivide ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int countDivisibles ( int A , int B , int M ) { int counter = 0 ; for ( int i = A ; i <= B ; i ++ ) if ( i % M == 0 ) counter ++ ; return counter ; } public static void main ( String [ ] args ) { int A = 30 , B = 100 , M = 30 ; System . out . println ( countDivisibles ( A , B , M ) ) ; } }

def countDivisibles ( A , B , M ) : NEW_LINE INDENT counter = 0 ; NEW_LINE for i in range ( A , B ) : NEW_LINE INDENT if ( i % M == 0 ) : NEW_LINE INDENT counter = counter + 1 NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT A = 30 NEW_LINE B = 100 NEW_LINE M = 30 NEW_LINE print ( countDivisibles ( A , B , M ) ) NEW_LINE


class GFG { static int findMaxProduct ( int arr [ ] , int n ) { int i ; int ans = Integer . MIN_VALUE ; int maxval = 1 ; int minval = 1 ; int prevMax ; for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) { maxval = maxval * arr [ i ] ; minval = Math . min ( 1 , minval * arr [ i ] ) ; } else if ( arr [ i ] == 0 ) { minval = 1 ; maxval = 0 ; } else if ( arr [ i ] < 0 ) { prevMax = maxval ; maxval = minval * arr [ i ] ; minval = prevMax * arr [ i ] ; } ans = Math . max ( ans , maxval ) ; if ( maxval <= 0 ) { maxval = 1 ; } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , - 4 , 0 , - 2 } ; int n = arr . length ; System . out . println ( findMaxProduct ( arr , n ) ) ; } }

def findMaxProduct ( arr , n ) : NEW_LINE INDENT ans = - float ( ' inf ' ) NEW_LINE maxval = 1 NEW_LINE minval = 1 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if arr [ i ] > 0 : NEW_LINE INDENT maxval = maxval * arr [ i ] NEW_LINE minval = min ( 1 , minval * arr [ i ] ) NEW_LINE DEDENT elif arr [ i ] == 0 : NEW_LINE INDENT minval = 1 NEW_LINE maxval = 0 NEW_LINE DEDENT elif arr [ i ] < 0 : NEW_LINE INDENT prevMax = maxval NEW_LINE maxval = minval * arr [ i ] NEW_LINE minval = prevMax * arr [ i ] NEW_LINE DEDENT ans = max ( ans , maxval ) NEW_LINE if maxval <= 0 : NEW_LINE INDENT maxval = 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , - 4 , 0 , - 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMaxProduct ( arr , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int setmiddlebits ( int n ) { n |= n >> 1 ; n |= n >> 2 ; n |= n >> 4 ; n |= n >> 8 ; n |= n >> 16 ; return ( n >> 1 ) ^ 1 ; } static int togglemiddlebits ( int n ) { if ( n == 1 ) return 1 ; return n ^ setmiddlebits ( n ) ; } public static void main ( String [ ] args ) { int n = 9 ; System . out . println ( togglemiddlebits ( n ) ) ; } }

def setmiddlebits ( n ) : NEW_LINE INDENT n |= n >> 1 ; NEW_LINE n |= n >> 2 ; NEW_LINE n |= n >> 4 ; NEW_LINE n |= n >> 8 ; NEW_LINE n |= n >> 16 ; NEW_LINE return ( n >> 1 ) ^ 1 NEW_LINE DEDENT def togglemiddlebits ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return n ^ setmiddlebits ( n ) NEW_LINE DEDENT n = 9 NEW_LINE print ( togglemiddlebits ( n ) ) NEW_LINE


import java . util . * ; class GFG { static boolean CompareAlphanumeric ( char [ ] str1 , char [ ] str2 ) { int i , j ; i = 0 ; j = 0 ; int len1 = str1 . length ; int len2 = str2 . length ; while ( i <= len1 && j <= len2 ) { while ( i < len1 && ( ! ( ( str1 [ i ] >= ' a ' && str1 [ i ] <= ' z ' ) || ( str1 [ i ] >= ' A ' && str1 [ i ] <= ' Z ' ) || ( str1 [ i ] >= '0' && str1 [ i ] <= '9' ) ) ) ) { i ++ ; } while ( j < len2 && ( ! ( ( str2 [ j ] >= ' a ' && str2 [ j ] <= ' z ' ) || ( str2 [ j ] >= ' A ' && str2 [ j ] <= ' Z ' ) || ( str2 [ j ] >= '0' && str2 [ j ] <= '9' ) ) ) ) { j ++ ; } if ( i == len1 && j == len2 ) { return true ; } else if ( str1 [ i ] != str2 [ j ] ) { return false ; } else { i ++ ; j ++ ; } } return false ; } static void CompareAlphanumericUtil ( String str1 , String str2 ) { boolean res ; res = CompareAlphanumeric ( str1 . toCharArray ( ) , str2 . toCharArray ( ) ) ; if ( res == true ) { System . out . println ( " Equal " ) ; } else { System . out . println ( " Unequal " ) ; } } public static void main ( String [ ] args ) { String str1 , str2 ; str1 = " Ram , ▁ Shyam " ; str2 = " ▁ Ram ▁ - ▁ Shyam . " ; CompareAlphanumericUtil ( str1 , str2 ) ; str1 = " abc123" ; str2 = "123abc " ; CompareAlphanumericUtil ( str1 , str2 ) ; } }

def CompareAlphanumeric ( str1 , str2 ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE len1 = len ( str1 ) NEW_LINE len2 = len ( str2 ) NEW_LINE while ( i <= len1 and j <= len2 ) : NEW_LINE INDENT while ( i < len1 and ( ( ( str1 [ i ] >= ' a ' and str1 [ i ] <= ' z ' ) or ( str1 [ i ] >= ' A ' and str1 [ i ] <= ' Z ' ) or ( str1 [ i ] >= '0' and str1 [ i ] <= '9' ) ) == False ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT while ( j < len2 and ( ( ( str2 [ j ] >= ' a ' and str2 [ j ] <= ' z ' ) or ( str2 [ j ] >= ' A ' and str2 [ j ] <= ' Z ' ) or ( str2 [ j ] >= '0' and str2 [ j ] <= '9' ) ) == False ) ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT if ( i == len1 and j == len2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT elif ( str1 [ i ] != str2 [ j ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def CompareAlphanumericUtil ( str1 , str2 ) : NEW_LINE INDENT res = CompareAlphanumeric ( str1 , str2 ) NEW_LINE if ( res == True ) : NEW_LINE INDENT print ( " Equal " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Unequal " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " Ram , ▁ Shyam " NEW_LINE str2 = " ▁ Ram ▁ - ▁ Shyam . " NEW_LINE CompareAlphanumericUtil ( str1 , str2 ) NEW_LINE str1 = " abc123" NEW_LINE str2 = "123abc " NEW_LINE CompareAlphanumericUtil ( str1 , str2 ) NEW_LINE DEDENT


class GFG { static final int n = 5 ; static void printSumSimple ( int mat [ ] [ ] , int k ) { if ( k > n ) return ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { for ( int j = 0 ; j < n - k + 1 ; j ++ ) { int sum = 0 ; for ( int p = i ; p < k + i ; p ++ ) for ( int q = j ; q < k + j ; q ++ ) sum += mat [ p ] [ q ] ; System . out . print ( sum + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String arg [ ] ) { int mat [ ] [ ] = { { 1 , 1 , 1 , 1 , 1 } , { 2 , 2 , 2 , 2 , 2 } , { 3 , 3 , 3 , 3 , 3 } , { 4 , 4 , 4 , 4 , 4 } , { 5 , 5 , 5 , 5 , 5 } } ; int k = 3 ; printSumSimple ( mat , k ) ; } }

n = 5 NEW_LINE def printSumSimple ( mat , k ) : NEW_LINE INDENT if ( k > n ) : NEW_LINE INDENT return NEW_LINE DEDENT for i in range ( n - k + 1 ) : NEW_LINE INDENT for j in range ( n - k + 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for p in range ( i , k + i ) : NEW_LINE INDENT for q in range ( j , k + j ) : NEW_LINE INDENT sum += mat [ p ] [ q ] NEW_LINE DEDENT DEDENT print ( sum , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT mat = [ [ 1 , 1 , 1 , 1 , 1 ] , [ 2 , 2 , 2 , 2 , 2 ] , [ 3 , 3 , 3 , 3 , 3 ] , [ 4 , 4 , 4 , 4 , 4 ] , [ 5 , 5 , 5 , 5 , 5 ] ] NEW_LINE k = 3 NEW_LINE printSumSimple ( mat , k ) NEW_LINE DEDENT


class Test { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } public static void main ( String [ ] args ) { int num = 5 ; System . out . println ( " Factorial ▁ of ▁ " + num + " ▁ is ▁ " + factorial ( 5 ) ) ; } }

def factorial ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return n * factorial ( n - 1 ) NEW_LINE DEDENT num = 5 ; NEW_LINE print ( " Factorial ▁ of " , num , " is " , factorial ( num ) ) NEW_LINE


import java . io . * ; import java . util . * ; public class GFG { static int N = 4 ; static void shiftMatrixByK ( int [ ] [ ] mat , int k ) { if ( k > N ) { System . out . print ( " Shifting ▁ is " + " ▁ not ▁ possible " ) ; return ; } int j = 0 ; while ( j < N ) { for ( int i = k ; i < N ; i ++ ) System . out . print ( mat [ j ] [ i ] + " ▁ " ) ; for ( int i = 0 ; i < k ; i ++ ) System . out . print ( mat [ j ] [ i ] + " ▁ " ) ; System . out . println ( ) ; j ++ ; } } public static void main ( String args [ ] ) { int [ ] [ ] mat = new int [ ] [ ] { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 10 , 11 , 12 } , { 13 , 14 , 15 , 16 } } ; int k = 2 ; shiftMatrixByK ( mat , k ) ; } }

N = 4 NEW_LINE def shiftMatrixByK ( mat , k ) : NEW_LINE INDENT if ( k > N ) : NEW_LINE INDENT print ( " shifting ▁ is " " ▁ not ▁ possible " ) NEW_LINE return NEW_LINE DEDENT j = 0 NEW_LINE while ( j < N ) : NEW_LINE INDENT for i in range ( k , N ) : NEW_LINE INDENT print ( " { } ▁ " . format ( mat [ j ] [ i ] ) , end = " " ) NEW_LINE DEDENT for i in range ( 0 , k ) : NEW_LINE INDENT print ( " { } ▁ " . format ( mat [ j ] [ i ] ) , end = " " ) NEW_LINE DEDENT print ( " " ) NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT mat = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 10 , 11 , 12 ] , [ 13 , 14 , 15 , 16 ] ] NEW_LINE k = 2 NEW_LINE shiftMatrixByK ( mat , k ) NEW_LINE


class GFG { static double numberOfDigits ( double n ) { if ( n == 1 ) return 1 ; double d = ( n * Math . log10 ( 1.6180339887498948 ) ) - ( ( Math . log10 ( 5 ) ) / 2 ) ; return Math . ceil ( d ) ; } public static void main ( String [ ] args ) { double i ; for ( i = 1 ; i <= 10 ; i ++ ) System . out . println ( " Number ▁ of ▁ Digits ▁ in ▁ F ( " + i + " ) ▁ - ▁ " + numberOfDigits ( i ) ) ; } }

import math NEW_LINE phi = ( 1 + 5 ** .5 ) / 2 NEW_LINE def numberOfDig ( n ) : NEW_LINE INDENT if n == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return math . ceil ( ( n * math . log10 ( phi ) - .5 * math . log10 ( 5 ) ) ) NEW_LINE DEDENT // Driver Code NEW_LINE for i in range ( 1 , 11 ) : NEW_LINE INDENT print ( " Number ▁ of ▁ Digits ▁ in ▁ F ( " + str ( i ) + " ) ▁ - ▁ " + str ( numberOfDig ( i ) ) ) NEW_LINE DEDENT


class GFG { static void positiveNegativeZero ( int [ ] arr ) { int len = arr . length ; float positiveCount = 0 ; float negativeCount = 0 ; float zeroCount = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( arr [ i ] > 0 ) { positiveCount ++ ; } else if ( arr [ i ] < 0 ) { negativeCount ++ ; } else if ( arr [ i ] == 0 ) { zeroCount ++ ; } } System . out . printf ( " % 1.4f ▁ " , positiveCount / len ) ; System . out . printf ( " % 1.4f ▁ " , negativeCount / len ) ; System . out . printf ( " % 1.4f ▁ " , zeroCount / len ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int [ ] a1 = { 2 , - 1 , 5 , 6 , 0 , - 3 } ; positiveNegativeZero ( a1 ) ; int [ ] a2 = { 4 , 0 , - 2 , - 9 , - 7 , 1 } ; positiveNegativeZero ( a2 ) ; } }

def positiveNegativeZero ( arr ) : NEW_LINE INDENT length = len ( arr ) ; NEW_LINE positiveCount = 0 ; NEW_LINE negativeCount = 0 ; NEW_LINE zeroCount = 0 ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT positiveCount += 1 ; NEW_LINE DEDENT elif ( arr [ i ] < 0 ) : NEW_LINE INDENT negativeCount += 1 ; NEW_LINE DEDENT elif ( arr [ i ] == 0 ) : NEW_LINE INDENT zeroCount += 1 ; NEW_LINE DEDENT DEDENT print ( " { 0 : . 4f } " . format ( ( positiveCount / length ) ) , end = " ▁ " ) ; NEW_LINE print ( " % 1.4f ▁ " % ( negativeCount / length ) , end = " ▁ " ) ; NEW_LINE print ( " % 1.4f ▁ " % ( zeroCount / length ) , end = " ▁ " ) ; NEW_LINE print ( ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a1 = [ 2 , - 1 , 5 , 6 , 0 , - 3 ] ; NEW_LINE positiveNegativeZero ( a1 ) ; NEW_LINE a2 = [ 4 , 0 , - 2 , - 9 , - 7 , 1 ] ; NEW_LINE positiveNegativeZero ( a2 ) ; NEW_LINE DEDENT


public class GFG { static int maxAlternateSum ( int arr [ ] , int n ) { if ( n == 1 ) return arr [ 0 ] ; int dec [ ] = new int [ n ] ; int inc [ ] = new int [ n ] ; dec [ 0 ] = inc [ 0 ] = arr [ 0 ] ; int flag = 0 ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ j ] > arr [ i ] ) { dec [ i ] = Math . max ( dec [ i ] , inc [ j ] + arr [ i ] ) ; flag = 1 ; } else if ( arr [ j ] < arr [ i ] && flag == 1 ) inc [ i ] = Math . max ( inc [ i ] , dec [ j ] + arr [ i ] ) ; } } int result = Integer . MIN_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( result < inc [ i ] ) result = inc [ i ] ; if ( result < dec [ i ] ) result = dec [ i ] ; } return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 2 , 3 , 5 , 7 , 9 , 10 } ; System . out . println ( " Maximum ▁ sum ▁ = ▁ " + maxAlternateSum ( arr , arr . length ) ) ; } }

def maxAlternateSum ( arr , n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return arr [ 0 ] NEW_LINE DEDENT dec = [ 0 for i in range ( n + 1 ) ] NEW_LINE inc = [ 0 for i in range ( n + 1 ) ] NEW_LINE dec [ 0 ] = inc [ 0 ] = arr [ 0 ] NEW_LINE flag = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT if ( arr [ j ] > arr [ i ] ) : NEW_LINE INDENT dec [ i ] = max ( dec [ i ] , inc [ j ] + arr [ i ] ) NEW_LINE flag = 1 NEW_LINE DEDENT elif ( arr [ j ] < arr [ i ] and flag == 1 ) : NEW_LINE INDENT inc [ i ] = max ( inc [ i ] , dec [ j ] + arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT result = - 2147483648 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( result < inc [ i ] ) : NEW_LINE INDENT result = inc [ i ] NEW_LINE DEDENT if ( result < dec [ i ] ) : NEW_LINE INDENT result = dec [ i ] NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT arr = [ 8 , 2 , 3 , 5 , 7 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Maximum ▁ sum ▁ = ▁ " , maxAlternateSum ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static int countSol ( int coeff [ ] , int start , int end , int rhs ) { if ( rhs == 0 ) return 1 ; int result = 0 ; for ( int i = start ; i <= end ; i ++ ) if ( coeff [ i ] <= rhs ) result += countSol ( coeff , i , end , rhs - coeff [ i ] ) ; return result ; } public static void main ( String [ ] args ) { int coeff [ ] = { 2 , 2 , 5 } ; int rhs = 4 ; int n = coeff . length ; System . out . println ( countSol ( coeff , 0 , n - 1 , rhs ) ) ; } }

def countSol ( coeff , start , end , rhs ) : NEW_LINE INDENT if ( rhs == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT result = 0 NEW_LINE for i in range ( start , end + 1 ) : NEW_LINE INDENT if ( coeff [ i ] <= rhs ) : NEW_LINE INDENT result += countSol ( coeff , i , end , rhs - coeff [ i ] ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT coeff = [ 2 , 2 , 5 ] NEW_LINE rhs = 4 NEW_LINE n = len ( coeff ) NEW_LINE print ( countSol ( coeff , 0 , n - 1 , rhs ) ) NEW_LINE


import java . io . * ; public class GFG { static void vowelOrConsonant ( char x ) { if ( x == ' a ' || x == ' e ' || x == ' i ' || x == ' o ' || x == ' u ' || x == ' A ' || x == ' E ' || x == ' I ' || x == ' O ' || x == ' U ' ) System . out . println ( " Vowel " ) ; else System . out . println ( " Consonant " ) ; } static public void main ( String [ ] args ) { vowelOrConsonant ( ' c ' ) ; vowelOrConsonant ( ' E ' ) ; } }

def vowelOrConsonant ( x ) : NEW_LINE INDENT if ( x == ' a ' or x == ' e ' or x == ' i ' or x == ' o ' or x == ' u ' or x == ' A ' or x == ' E ' or x == ' I ' or x == ' O ' or x == ' U ' ) : NEW_LINE INDENT print ( " Vowel " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Consonant " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT vowelOrConsonant ( ' c ' ) NEW_LINE vowelOrConsonant ( ' E ' ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int sumOfRange ( int a , int b ) { int i = ( a * ( a + 1 ) ) >> 1 ; int j = ( b * ( b + 1 ) ) >> 1 ; return ( i - j ) ; } static int sumofproduct ( int n ) { int sum = 0 ; int root = ( int ) Math . sqrt ( n ) ; for ( int i = 1 ; i <= root ; i ++ ) { int up = n / i ; int low = Math . max ( n / ( i + 1 ) , root ) ; sum += ( i * sumOfRange ( up , low ) ) ; sum += ( i * ( n / i ) ) ; } return sum ; } static public void main ( String [ ] args ) { int n = 10 ; System . out . println ( sumofproduct ( n ) ) ; } }

import math NEW_LINE def sumOfRange ( a , b ) : NEW_LINE INDENT i = ( a * ( a + 1 ) ) >> 1 ; NEW_LINE j = ( b * ( b + 1 ) ) >> 1 ; NEW_LINE return ( i - j ) ; NEW_LINE DEDENT def sumofproduct ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE root = int ( math . sqrt ( n ) ) ; NEW_LINE for i in range ( 1 , root + 1 ) : NEW_LINE INDENT up = int ( n / i ) ; NEW_LINE low = max ( int ( n / ( i + 1 ) ) , root ) ; NEW_LINE sum += ( i * sumOfRange ( up , low ) ) ; NEW_LINE sum += ( i * int ( n / i ) ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 10 ; NEW_LINE print ( sumofproduct ( n ) ) ; NEW_LINE


public class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static boolean isPossible ( int N ) { if ( isPrime ( N ) && isPrime ( N - 2 ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 13 ; if ( isPossible ( n ) == true ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

import math NEW_LINE def isPrime ( n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return False NEW_LINE DEDENT if n == 2 : NEW_LINE INDENT return True NEW_LINE DEDENT if n % 2 == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT if n % i == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def isPossible ( n ) : NEW_LINE INDENT if isPrime ( n ) and isPrime ( n - 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT n = 13 NEW_LINE if isPossible ( n ) == True : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { static int maxSum ( int arr [ ] , int n , int k ) { int max_sum = Integer . MIN_VALUE ; for ( int i = 0 ; i < n - k + 1 ; i ++ ) { int current_sum = 0 ; for ( int j = 0 ; j < k ; j ++ ) current_sum = current_sum + arr [ i + j ] ; max_sum = Math . max ( current_sum , max_sum ) ; } return max_sum ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 2 , 10 , 2 , 3 , 1 , 0 , 20 } ; int k = 4 ; int n = arr . length ; System . out . println ( maxSum ( arr , n , k ) ) ; } }

import sys NEW_LINE INT_MIN = - sys . maxsize - 1 NEW_LINE def maxSum ( arr , n , k ) : NEW_LINE INDENT max_sum = INT_MIN NEW_LINE for i in range ( n - k + 1 ) : NEW_LINE INDENT current_sum = 0 NEW_LINE for j in range ( k ) : NEW_LINE INDENT current_sum = current_sum + arr [ i + j ] NEW_LINE DEDENT max_sum = max ( current_sum , max_sum ) NEW_LINE DEDENT return max_sum NEW_LINE DEDENT arr = [ 1 , 4 , 2 , 10 , 2 , 3 , 1 , 0 , 20 ] NEW_LINE k = 4 NEW_LINE n = len ( arr ) NEW_LINE print ( maxSum ( arr , n , k ) ) NEW_LINE


import java . util . * ; class GFG { static float getPercentile ( int rank , int students ) { float result = ( float ) ( students - rank ) / students * 100 ; return result ; } public static void main ( String [ ] args ) { int your_rank = 805 ; int total_students = 97481 ; System . out . print ( getPercentile ( your_rank , total_students ) ) ; } }

def getPercentile ( rank , students ) : NEW_LINE INDENT result = ( students - rank ) / students * 100 ; NEW_LINE return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT your_rank = 805 ; NEW_LINE total_students = 97481 ; NEW_LINE print ( getPercentile ( your_rank , total_students ) ) ; NEW_LINE DEDENT


import java . util . Arrays ; public class GFG { private static int mergeAndCount ( int [ ] arr , int l , int m , int r ) { int [ ] left = Arrays . copyOfRange ( arr , l , m + 1 ) ; int [ ] right = Arrays . copyOfRange ( arr , m + 1 , r + 1 ) ; int i = 0 , j = 0 , k = l , swaps = 0 ; while ( i < left . length && j < right . length ) { if ( left [ i ] <= right [ j ] ) arr [ k ++ ] = left [ i ++ ] ; else { arr [ k ++ ] = right [ j ++ ] ; swaps += ( m + 1 ) - ( l + i ) ; } } while ( i < left . length ) arr [ k ++ ] = left [ i ++ ] ; while ( j < right . length ) arr [ k ++ ] = right [ j ++ ] ; return swaps ; } private static int mergeSortAndCount ( int [ ] arr , int l , int r ) { int count = 0 ; if ( l < r ) { int m = ( l + r ) / 2 ; count += mergeSortAndCount ( arr , l , m ) ; count += mergeSortAndCount ( arr , m + 1 , r ) ; count += mergeAndCount ( arr , l , m , r ) ; } return count ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 20 , 6 , 4 , 5 } ; System . out . println ( mergeSortAndCount ( arr , 0 , arr . length - 1 ) ) ; } }

def mergeSort ( arr , n ) : NEW_LINE INDENT temp_arr = [ 0 ] * n NEW_LINE return _mergeSort ( arr , temp_arr , 0 , n - 1 ) NEW_LINE DEDENT def _mergeSort ( arr , temp_arr , left , right ) : NEW_LINE INDENT inv_count = 0 NEW_LINE if left < right : NEW_LINE INDENT mid = ( left + right ) // 2 NEW_LINE inv_count += _mergeSort ( arr , temp_arr , left , mid ) NEW_LINE inv_count += _mergeSort ( arr , temp_arr , mid + 1 , right ) NEW_LINE inv_count += merge ( arr , temp_arr , left , mid , right ) NEW_LINE DEDENT return inv_count NEW_LINE DEDENT def merge ( arr , temp_arr , left , mid , right ) : NEW_LINE INDENT i = left NEW_LINE j = mid + 1 NEW_LINE k = left NEW_LINE inv_count = 0 NEW_LINE while i <= mid and j <= right : NEW_LINE INDENT if arr [ i ] <= arr [ j ] : NEW_LINE INDENT temp_arr [ k ] = arr [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp_arr [ k ] = arr [ j ] NEW_LINE inv_count += ( mid - i + 1 ) NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT while i <= mid : NEW_LINE INDENT temp_arr [ k ] = arr [ i ] NEW_LINE k += 1 NEW_LINE i += 1 NEW_LINE DEDENT while j <= right : NEW_LINE INDENT temp_arr [ k ] = arr [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT for loop_var in range ( left , right + 1 ) : NEW_LINE INDENT arr [ loop_var ] = temp_arr [ loop_var ] NEW_LINE DEDENT return inv_count NEW_LINE DEDENT arr = [ 1 , 20 , 6 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE result = mergeSort ( arr , n ) NEW_LINE print ( " Number ▁ of ▁ inversions ▁ are " , result ) NEW_LINE


import java . io . * ; class GFG { static int getLeftmostBit ( int n ) { int m = 0 ; while ( n > 1 ) { n = n >> 1 ; m ++ ; } return m ; } static int getNextLeftmostBit ( int n , int m ) { int temp = 1 << m ; while ( n < temp ) { temp = temp >> 1 ; m -- ; } return m ; } static int countSetBits ( int n ) { int m = getLeftmostBit ( n ) ; return countSetBits ( n , m ) ; } static int countSetBits ( int n , int m ) { if ( n == 0 ) return 0 ; m = getNextLeftmostBit ( n , m ) ; if ( n == ( ( int ) 1 << ( m + 1 ) ) - 1 ) return ( int ) ( m + 1 ) * ( 1 << m ) ; n = n - ( 1 << m ) ; return ( n + 1 ) + countSetBits ( n ) + m * ( 1 << ( m - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 17 ; System . out . println ( " Total ▁ set ▁ bit ▁ count ▁ is ▁ " + countSetBits ( n ) ) ; } }

def getLeftmostBit ( n ) : NEW_LINE INDENT m = 0 NEW_LINE while ( n > 1 ) : NEW_LINE INDENT n = n >> 1 NEW_LINE m += 1 NEW_LINE DEDENT return m NEW_LINE DEDENT def getNextLeftmostBit ( n , m ) : NEW_LINE INDENT temp = 1 << m NEW_LINE while ( n < temp ) : NEW_LINE INDENT temp = temp >> 1 NEW_LINE m -= 1 NEW_LINE DEDENT return m NEW_LINE DEDENT def countSetBits ( n ) : NEW_LINE INDENT m = getLeftmostBit ( n ) NEW_LINE return _countSetBits ( n , m ) NEW_LINE DEDENT def _countSetBits ( n , m ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT m = getNextLeftmostBit ( n , m ) NEW_LINE if ( n == ( 1 << ( m + 1 ) ) - 1 ) : NEW_LINE INDENT return ( ( m + 1 ) * ( 1 << m ) ) NEW_LINE DEDENT n = n - ( 1 << m ) NEW_LINE return ( n + 1 ) + countSetBits ( n ) + m * ( 1 << ( m - 1 ) ) NEW_LINE DEDENT n = 17 NEW_LINE print ( " Total ▁ set ▁ bit ▁ count ▁ is " , countSetBits ( n ) ) NEW_LINE


import java . util . * ; class GFG { static int Count_subarray ( int arr [ ] , int n ) { int subarray_sum , remaining_sum , count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { subarray_sum = 0 ; remaining_sum = 0 ; for ( int k = i ; k <= j ; k ++ ) { subarray_sum += arr [ k ] ; } for ( int l = 0 ; l < i ; l ++ ) { remaining_sum += arr [ l ] ; } for ( int l = j + 1 ; l < n ; l ++ ) { remaining_sum += arr [ l ] ; } if ( subarray_sum > remaining_sum ) { count += 1 ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 9 , 12 , 6 } ; int n = arr . length ; System . out . print ( Count_subarray ( arr , n ) ) ; } }

def Count_subarray ( arr , n ) : NEW_LINE INDENT subarray_sum , remaining_sum , count = 0 , 0 , 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT subarray_sum = 0 ; NEW_LINE remaining_sum = 0 ; NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT subarray_sum += arr [ k ] ; NEW_LINE DEDENT for l in range ( i ) : NEW_LINE INDENT remaining_sum += arr [ l ] ; NEW_LINE DEDENT for l in range ( j + 1 , n ) : NEW_LINE INDENT remaining_sum += arr [ l ] ; NEW_LINE DEDENT if ( subarray_sum > remaining_sum ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 9 , 12 , 6 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( Count_subarray ( arr , n ) ) ; NEW_LINE DEDENT


class GFG { static int calculatePower ( int b , int e ) { int ans = 1 ; while ( e > 0 ) { if ( e % 2 == 1 ) ans = ans * b ; e = e / 2 ; b = b * b ; } return ans ; } static int CountSubSet ( int arr [ ] , int n , int X ) { int count = 0 , checkX = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == X ) { checkX = 1 ; break ; } } if ( checkX == 1 ) count = calculatePower ( 2 , n - 1 ) ; else count = 0 ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 6 , 7 } ; int X = 5 ; int n = arr . length ; System . out . println ( CountSubSet ( arr , n , X ) ) ; } }

def calculatePower ( b , e ) : NEW_LINE INDENT ans = 1 ; NEW_LINE while ( e > 0 ) : NEW_LINE INDENT if ( e % 2 == 1 ) : NEW_LINE INDENT ans = ans * b ; NEW_LINE DEDENT e = e // 2 ; NEW_LINE b = b * b ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT def CountSubSet ( arr , n , X ) : NEW_LINE INDENT count = 0 ; checkX = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == X ) : NEW_LINE INDENT checkX = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT if ( checkX == 1 ) : NEW_LINE INDENT count = calculatePower ( 2 , n - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT count = 0 ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 5 , 6 , 7 ] ; NEW_LINE X = 5 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( CountSubSet ( arr , n , X ) ) ; NEW_LINE DEDENT


import java . util . * ; class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } class BinaryTree { Node constructTreeUtil ( int post [ ] , int n ) { Node root = new Node ( post [ n - 1 ] ) ; Stack < Node > s = new Stack < > ( ) ; s . push ( root ) ; for ( int i = n - 2 ; i >= 0 ; -- i ) { Node x = new Node ( post [ i ] ) ; Node temp = null ; while ( ! s . isEmpty ( ) && post [ i ] < s . peek ( ) . data ) temp = s . pop ( ) ; if ( temp != null ) temp . left = x ; else s . peek ( ) . right = x ; s . push ( x ) ; } return root ; } Node constructTree ( int post [ ] , int size ) { return constructTreeUtil ( post , size ) ; } void printInorder ( Node node ) { if ( node == null ) return ; printInorder ( node . left ) ; System . out . print ( node . data + " ▁ " ) ; printInorder ( node . right ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; int post [ ] = new int [ ] { 1 , 7 , 5 , 50 , 40 , 10 } ; int size = post . length ; Node root = tree . constructTree ( post , size ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree : " ) ; tree . printInorder ( root ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data = 0 ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def constructTreeUtil ( post , n ) : NEW_LINE INDENT root = Node ( post [ n - 1 ] ) NEW_LINE s = [ ] NEW_LINE s . append ( root ) NEW_LINE i = n - 2 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT x = Node ( post [ i ] ) NEW_LINE temp = None NEW_LINE while ( len ( s ) > 0 and post [ i ] < s [ - 1 ] . data ) : NEW_LINE INDENT temp = s [ - 1 ] NEW_LINE s . pop ( ) NEW_LINE DEDENT if ( temp != None ) : NEW_LINE INDENT temp . left = x NEW_LINE DEDENT else : NEW_LINE INDENT s [ - 1 ] . right = x NEW_LINE DEDENT s . append ( x ) NEW_LINE i = i - 1 NEW_LINE DEDENT return root NEW_LINE DEDENT def constructTree ( post , size ) : NEW_LINE INDENT return constructTreeUtil ( post , size ) NEW_LINE DEDENT def printInorder ( node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return NEW_LINE DEDENT printInorder ( node . left ) NEW_LINE print ( node . data , end = " ▁ " ) NEW_LINE printInorder ( node . right ) NEW_LINE DEDENT post = [ 1 , 7 , 5 , 50 , 40 , 10 ] NEW_LINE size = len ( post ) NEW_LINE root = constructTree ( post , size ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ the ▁ constructed ▁ tree : " ) NEW_LINE printInorder ( root ) NEW_LINE


import java . util . * ; class GFG { static int gcd ( int a , int b ) { return b > 0 ? gcd ( b , a % b ) : a ; } static int countGCD ( int L , int R , int g ) { L = ( L + g - 1 ) / g ; R = R / g ; int ans = 0 ; for ( int i = L ; i <= R ; i ++ ) for ( int j = L ; j <= R ; j ++ ) if ( gcd ( i , j ) == 1 ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int L = 1 , R = 11 , g = 5 ; System . out . println ( countGCD ( L , R , g ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT return gcd ( b , a % b ) if b > 0 else a NEW_LINE DEDENT def countGCD ( L , R , g ) : NEW_LINE INDENT L = ( L + g - 1 ) // g NEW_LINE R = R // g NEW_LINE ans = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT for j in range ( L , R + 1 ) : NEW_LINE INDENT if ( gcd ( i , j ) == 1 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT L = 1 NEW_LINE R = 11 NEW_LINE g = 5 NEW_LINE print ( countGCD ( L , R , g ) ) NEW_LINE


class GFG { static int MAX = 64 ; static int getBitmask ( char [ ] s ) { int temp = 0 ; for ( int j = 0 ; j < s . length ; j ++ ) { switch ( s [ j ] ) { case ' s ' : temp = temp | ( 1 ) ; break ; case ' t ' : temp = temp | ( 2 ) ; break ; case ' r ' : temp = temp | ( 4 ) ; break ; case ' i ' : temp = temp | ( 8 ) ; break ; case ' n ' : temp = temp | ( 16 ) ; break ; case ' g ' : temp = temp | ( 32 ) ; break ; default : break ; } } return temp ; } static int countPairs ( String arr [ ] , int n ) { int [ ] bitMask = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) bitMask [ getBitmask ( arr [ i ] . toCharArray ( ) ) ] ++ ; int cnt = 0 ; for ( int i = 0 ; i < MAX ; i ++ ) { for ( int j = i ; j < MAX ; j ++ ) { if ( ( i | j ) == ( MAX - 1 ) ) { if ( i == j ) cnt += ( ( bitMask [ i ] * bitMask [ i ] - 1 ) / 2 ) ; else cnt += ( bitMask [ i ] * bitMask [ j ] ) ; } } } return cnt ; } public static void main ( String [ ] args ) { String arr [ ] = { " strrr " , " strring " , " gstrin " } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }

MAX = 64 NEW_LINE def getBitmask ( s ) : NEW_LINE INDENT temp = 0 NEW_LINE for j in range ( len ( s ) ) : NEW_LINE INDENT if ( s [ j ] == ' s ' ) : NEW_LINE INDENT temp = temp | 1 NEW_LINE DEDENT elif ( s [ j ] == ' t ' ) : NEW_LINE INDENT temp = temp | 2 NEW_LINE DEDENT elif ( s [ j ] == ' r ' ) : NEW_LINE INDENT temp = temp | 4 NEW_LINE DEDENT elif ( s [ j ] == ' i ' ) : NEW_LINE INDENT temp = temp | 8 NEW_LINE DEDENT elif ( s [ j ] == ' n ' ) : NEW_LINE INDENT temp = temp | 16 NEW_LINE DEDENT elif ( s [ j ] == ' g ' ) : NEW_LINE INDENT temp = temp | 32 NEW_LINE DEDENT DEDENT return temp NEW_LINE DEDENT def countPairs ( arr , n ) : NEW_LINE INDENT bitMask = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT bitMask [ getBitmask ( arr [ i ] ) ] += 1 NEW_LINE DEDENT cnt = 0 NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT for j in range ( i , MAX ) : NEW_LINE INDENT if ( ( i | j ) == ( MAX - 1 ) ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT cnt += ( ( bitMask [ i ] * bitMask [ i ] - 1 ) // 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT cnt += ( bitMask [ i ] * bitMask [ j ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT return cnt NEW_LINE DEDENT arr = [ " strrr " , " strring " , " gstrin " ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE


class GFG { static int numberOfCuts ( int M , int N ) { int result = 0 ; result = ( M - 1 ) * ( N - 1 ) ; return result ; } public static void main ( String args [ ] ) { int M = 4 , N = 4 ; int Cuts = numberOfCuts ( M , N ) ; System . out . println ( " Maximum ▁ cuts ▁ = ▁ " + Cuts ) ; } }

def numberOfCuts ( M , N ) : NEW_LINE INDENT result = 0 NEW_LINE result = ( M - 1 ) * ( N - 1 ) NEW_LINE return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M , N = 4 , 4 NEW_LINE Cuts = numberOfCuts ( M , N ) NEW_LINE print ( " Maximum ▁ cuts ▁ = ▁ " , Cuts ) NEW_LINE DEDENT


import java . util . Arrays ; class Test { static int subsetPairNotDivisibleByK ( int arr [ ] , int N , int K ) { int f [ ] = new int [ K ] ; Arrays . fill ( f , 0 ) ; for ( int i = 0 ; i < N ; i ++ ) f [ arr [ i ] % K ] ++ ; if ( K % 2 == 0 ) f [ K / 2 ] = Math . min ( f [ K / 2 ] , 1 ) ; int res = Math . min ( f [ 0 ] , 1 ) ; for ( int i = 1 ; i <= K / 2 ; i ++ ) res += Math . max ( f [ i ] , f [ K - i ] ) ; return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 7 , 2 , 9 , 1 } ; int N = arr . length ; int K = 3 ; System . out . print ( subsetPairNotDivisibleByK ( arr , N , K ) ) ; } }

def subsetPairNotDivisibleByK ( arr , N , K ) : NEW_LINE INDENT f = [ 0 for i in range ( K ) ] NEW_LINE for i in range ( N ) : NEW_LINE INDENT f [ arr [ i ] % K ] += 1 NEW_LINE DEDENT if ( K % 2 == 0 ) : NEW_LINE INDENT f [ K // 2 ] = min ( f [ K // 2 ] , 1 ) NEW_LINE DEDENT res = min ( f [ 0 ] , 1 ) NEW_LINE for i in range ( 1 , ( K // 2 ) + 1 ) : NEW_LINE INDENT res += max ( f [ i ] , f [ K - i ] ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 3 , 7 , 2 , 9 , 1 ] NEW_LINE N = len ( arr ) NEW_LINE K = 3 NEW_LINE print ( subsetPairNotDivisibleByK ( arr , N , K ) ) NEW_LINE


class GFG { static boolean SieveOfEratosthenes ( int n , boolean isPrime [ ] ) { isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p <= n ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * p ; i <= n ; i += p ) isPrime [ i ] = false ; } } return false ; } static void findPrimePair ( int n ) { boolean isPrime [ ] = new boolean [ n + 1 ] ; SieveOfEratosthenes ( n , isPrime ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPrime [ i ] && isPrime [ n - i ] ) { System . out . print ( i + " ▁ " + ( n - i ) ) ; return ; } } } public static void main ( String [ ] args ) { int n = 74 ; findPrimePair ( n ) ; } }

def SieveOfEratosthenes ( n , isPrime ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT isPrime [ i ] = True NEW_LINE DEDENT p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT i = p * p NEW_LINE while ( i <= n ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE i += p NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def findPrimePair ( n ) : NEW_LINE INDENT isPrime = [ 0 ] * ( n + 1 ) NEW_LINE SieveOfEratosthenes ( n , isPrime ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( isPrime [ i ] and isPrime [ n - i ] ) : NEW_LINE INDENT print ( i , ( n - i ) ) NEW_LINE return NEW_LINE DEDENT DEDENT DEDENT n = 74 NEW_LINE findPrimePair ( n ) NEW_LINE


import java . util . * ; class GFG { static int calculateSum ( int n ) { if ( n % 2 == 1 ) return ( n + 1 ) / 2 ; return - n / 2 ; } public static void main ( String ar [ ] ) { int n = 8 ; System . out . println ( calculateSum ( n ) ) ; } }

def solve_sum ( n ) : NEW_LINE INDENT if ( n % 2 == 1 ) : NEW_LINE INDENT return ( n + 1 ) / 2 NEW_LINE DEDENT return - n / 2 NEW_LINE DEDENT n = 8 NEW_LINE print ( int ( solve_sum ( n ) ) ) NEW_LINE


import java . io . * ; class GFG { static int maxSubArraySum ( int a [ ] , int size ) { int max_so_far = a [ 0 ] ; int curr_max = a [ 0 ] ; for ( int i = 1 ; i < size ; i ++ ) { curr_max = Math . max ( a [ i ] , curr_max + a [ i ] ) ; max_so_far = Math . max ( max_so_far , curr_max ) ; } return max_so_far ; } public static void main ( String [ ] args ) { int a [ ] = { - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 } ; int n = a . length ; int max_sum = maxSubArraySum ( a , n ) ; System . out . println ( " Maximum ▁ contiguous ▁ sum ▁ is ▁ " + max_sum ) ; } }

def maxSubArraySum ( a , size ) : NEW_LINE INDENT max_so_far = a [ 0 ] NEW_LINE curr_max = a [ 0 ] NEW_LINE for i in range ( 1 , size ) : NEW_LINE INDENT curr_max = max ( a [ i ] , curr_max + a [ i ] ) NEW_LINE max_so_far = max ( max_so_far , curr_max ) NEW_LINE DEDENT return max_so_far NEW_LINE DEDENT a = [ - 2 , - 3 , 4 , - 1 , - 2 , 1 , 5 , - 3 ] NEW_LINE print " Maximum ▁ contiguous ▁ sum ▁ is " , maxSubArraySum ( a , len ( a ) ) NEW_LINE


class GFG { static int numberSequence ( int n ) { int num = ( int ) ( Math . pow ( 4 , n ) - Math . pow ( 2 , n ) ) - 1 ; return num ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( numberSequence ( n ) ) ; } }

def numberSequence ( n ) : NEW_LINE INDENT num = pow ( 4 , n ) - pow ( 2 , n ) - 1 NEW_LINE return num NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE print ( numberSequence ( n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static long removeRecur ( long n ) { long prev_digit = n % 10 ; long pow = 10 ; long res = prev_digit ; while ( n > 0 ) { long curr_digit = n % 10 ; if ( curr_digit != prev_digit ) { res += curr_digit * pow ; prev_digit = curr_digit ; pow *= 10 ; } n = n / 10 ; } return res ; } public static void main ( String [ ] args ) { long n = 12224 ; System . out . println ( removeRecur ( n ) ) ; } }

def removeRecur ( n ) : NEW_LINE INDENT prev_digit = n % 10 NEW_LINE pow = 10 NEW_LINE res = prev_digit NEW_LINE while ( n ) : NEW_LINE INDENT curr_digit = n % 10 NEW_LINE if ( curr_digit != prev_digit ) : NEW_LINE INDENT res += curr_digit * pow NEW_LINE prev_digit = curr_digit NEW_LINE pow *= 10 NEW_LINE DEDENT n = int ( n / 10 ) NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12224 NEW_LINE print ( removeRecur ( n ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static float rainDayProbability ( int a [ ] , int n ) { float count = 0 , m ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 1 ) count ++ ; } m = count / n ; return m ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 } ; int n = a . length ; System . out . print ( rainDayProbability ( a , n ) ) ; } }

def rainDayProbability ( a , n ) : NEW_LINE INDENT count = a . count ( 1 ) NEW_LINE m = count / n NEW_LINE return m NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 0 , 1 , 0 , 1 , 1 , 1 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( rainDayProbability ( a , n ) ) NEW_LINE DEDENT


class GFG { static double expect ( double m , double n ) { double ans = 0.0 , i ; for ( i = m ; i > 0 ; i -- ) ans += ( Math . pow ( i / m , n ) - Math . pow ( ( i - 1 ) / m , n ) ) * i ; return ans ; } public static void main ( String [ ] args ) { double m = 6 , n = 3 ; System . out . println ( String . format ( " % .5f " , expect ( m , n ) ) ) ; } }

def expect ( m , n ) : NEW_LINE INDENT ans = 0.0 NEW_LINE i = m NEW_LINE while ( i ) : NEW_LINE INDENT ans += ( pow ( i / m , n ) - pow ( ( i - 1 ) / m , n ) ) * i NEW_LINE i -= 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT m , n = 6 , 3 NEW_LINE print ( expect ( m , n ) ) NEW_LINE DEDENT


public class GFG { static int findElement ( int arr [ ] , int n ) { int [ ] prefixSum = new int [ n ] ; prefixSum [ 0 ] = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] ; int [ ] suffixSum = new int [ n ] ; suffixSum [ n - 1 ] = arr [ n - 1 ] ; for ( int i = n - 2 ; i >= 0 ; i -- ) suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) if ( prefixSum [ i ] == suffixSum [ i ] ) return arr [ i ] ; return - 1 ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 4 , 2 , 5 } ; int n = arr . length ; System . out . println ( findElement ( arr , n ) ) ; } }

def findElement ( arr , n ) : NEW_LINE INDENT prefixSum = [ 0 ] * n NEW_LINE prefixSum [ 0 ] = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT prefixSum [ i ] = prefixSum [ i - 1 ] + arr [ i ] NEW_LINE DEDENT suffixSum = [ 0 ] * n NEW_LINE suffixSum [ n - 1 ] = arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT suffixSum [ i ] = suffixSum [ i + 1 ] + arr [ i ] NEW_LINE DEDENT for i in range ( 1 , n - 1 , 1 ) : NEW_LINE INDENT if prefixSum [ i ] == suffixSum [ i ] : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 4 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findElement ( arr , n ) ) NEW_LINE DEDENT


class GFG { static void find_numbers ( int N ) { if ( N == 1 ) { System . out . print ( - 1 ) ; } else { System . out . print ( N + " ▁ " + ( N + 1 ) + " ▁ " + ( N * ( N + 1 ) ) ) ; } } public static void main ( String [ ] args ) { int N = 5 ; find_numbers ( N ) ; } }

def find_numbers ( N ) : NEW_LINE INDENT if ( N == 1 ) : NEW_LINE INDENT print ( - 1 , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( N , N + 1 , N * ( N + 1 ) ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE find_numbers ( N ) ; NEW_LINE DEDENT


import java . io . * ; import java . math . * ; import java . text . * ; import java . util . * ; import java . util . regex . * ; class GFG { static int summingSeries ( long n ) { int S = 0 ; for ( i = 1 ; i <= n ; i ++ ) S += i * i - ( i - 1 ) * ( i - 1 ) ; return S ; } public static void main ( String [ ] args ) { int n = 100 ; System . out . println ( " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " + summingSeries ( n ) ) ; } }

def summingSeries ( n ) : NEW_LINE INDENT S = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT S += i * i - ( i - 1 ) * ( i - 1 ) NEW_LINE DEDENT return S NEW_LINE DEDENT n = 100 NEW_LINE print ( " The ▁ sum ▁ of ▁ n ▁ term ▁ is : ▁ " , summingSeries ( n ) , sep = " " ) NEW_LINE


class GFG { static int Log2n ( int n ) { return ( n > 1 ) ? 1 + Log2n ( n / 2 ) : 0 ; } static boolean isPowerOfTwo ( int n ) { return n > 0 && ( ( n & ( n - 1 ) ) == 0 ) ; } static int findPosition ( int n ) { if ( ! isPowerOfTwo ( n ) ) return - 1 ; return Log2n ( n ) + 1 ; } public static void main ( String [ ] args ) { int n = 0 ; int pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number ▁ " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 12 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number ▁ " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; n = 128 ; pos = findPosition ( n ) ; if ( pos == - 1 ) System . out . println ( " n ▁ = ▁ " + n + " , ▁ Invalid ▁ number ▁ " ) ; else System . out . println ( " n ▁ = ▁ " + n + " , ▁ Position ▁ " + pos ) ; } }

def Log2n ( n ) : NEW_LINE INDENT if ( n > 1 ) : NEW_LINE INDENT return ( 1 + Log2n ( n / 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def isPowerOfTwo ( n ) : NEW_LINE INDENT return n and ( not ( n & ( n - 1 ) ) ) NEW_LINE DEDENT def findPosition ( n ) : NEW_LINE INDENT if ( not isPowerOfTwo ( n ) ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return Log2n ( n ) + 1 NEW_LINE DEDENT n = 0 NEW_LINE pos = findPosition ( n ) NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT print ( " n ▁ = " , n , " , ▁ Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = ▁ " , n , " , ▁ Position ▁ " , pos ) NEW_LINE DEDENT n = 12 NEW_LINE pos = findPosition ( n ) NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT print ( " n ▁ = " , n , " , ▁ Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = ▁ " , n , " , ▁ Position ▁ " , pos ) NEW_LINE DEDENT n = 128 NEW_LINE pos = findPosition ( n ) NEW_LINE if ( pos == - 1 ) : NEW_LINE INDENT print ( " n ▁ = ▁ " , n , " , ▁ Invalid ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " n ▁ = ▁ " , n , " , ▁ Position ▁ " , pos ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int maximize ( int A1 [ ] , int A2 [ ] , int n , int x , int y ) { int [ ] c = new int [ n ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = A2 [ i ] - A1 [ i ] ; sum += A1 [ i ] ; } int temp ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( c [ i ] < c [ i + 1 ] ) { temp = c [ i ] ; c [ i ] = c [ i + 1 ] ; c [ i + 1 ] = temp ; } } int maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { sum += c [ i ] ; if ( i + 1 >= ( n - x ) ) maxi = Math . max ( sum , maxi ) ; } return maxi ; } public static void main ( String args [ ] ) { int A1 [ ] = { 1 , 2 , 3 , 4 , 5 } ; int A2 [ ] = { 5 , 4 , 3 , 2 , 1 } ; int n = 5 ; int x = 3 , y = 3 ; System . out . println ( maximize ( A1 , A2 , n , x , y ) ) ; } }

def maximize ( A1 , A2 , n , x , y ) : NEW_LINE INDENT c = [ 0 for i in range ( n ) ] NEW_LINE Sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ i ] = A2 [ i ] - A1 [ i ] NEW_LINE Sum += A1 [ i ] NEW_LINE DEDENT c . sort ( ) NEW_LINE c = c [ : : - 1 ] NEW_LINE maxi = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT Sum += c [ i ] NEW_LINE if ( i + 1 >= ( n - x ) ) : NEW_LINE INDENT maxi = max ( Sum , maxi ) NEW_LINE DEDENT DEDENT return maxi NEW_LINE DEDENT A1 = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE A2 = [ 5 , 4 , 3 , 2 , 1 ] NEW_LINE n = 5 NEW_LINE x , y = 3 , 3 NEW_LINE print ( maximize ( A1 , A2 , n , x , y ) ) NEW_LINE


class GFG { static int max = 100 ; public static void main ( String [ ] args ) { long [ ] arr = new long [ max ] ; arr [ 0 ] = 0 ; arr [ 1 ] = 1 ; int i = 0 ; for ( i = 2 ; i < max ; i ++ ) arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] ; for ( i = 1 ; i < max - 1 ; i ++ ) { if ( ( arr [ i ] % 10 == 0 ) && ( arr [ i + 1 ] % 10 == 1 ) ) break ; } System . out . println ( " Sequence ▁ is ▁ repeating ▁ after ▁ index ▁ " + i ) ; } }

if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT max = 100 NEW_LINE arr = [ 0 for i in range ( max ) ] NEW_LINE arr [ 0 ] = 0 NEW_LINE arr [ 1 ] = 1 NEW_LINE for i in range ( 2 , max ) : NEW_LINE INDENT arr [ i ] = arr [ i - 1 ] + arr [ i - 2 ] NEW_LINE DEDENT for i in range ( 1 , max - 1 ) : NEW_LINE INDENT if ( ( arr [ i ] % 10 == 0 ) and ( arr [ i + 1 ] % 10 == 1 ) ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( " Sequence ▁ is ▁ repeating ▁ after ▁ index " , i ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void maxLengthSubSeq ( int a [ ] , int n ) { int temp [ ] = new int [ n ] ; int print [ ] = new int [ n ] ; int y = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int j = 0 ; int x = 0 ; temp [ j ++ ] = a [ x ] ; x = a [ x ] + x ; while ( x < n ) { temp [ j ++ ] = a [ x ] ; x = a [ x ] + x ; } if ( y < j ) { for ( int k = 0 ; k < j ; k ++ ) { print [ k ] = temp [ k ] ; y = j ; } } } for ( int i = 0 ; i < y ; i ++ ) System . out . print ( print [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = a . length ; maxLengthSubSeq ( a , n ) ; } }

def maxLengthSubSeq ( a , n ) : NEW_LINE INDENT temp = [ 0 for i in range ( n ) ] NEW_LINE print1 = [ 0 for i in range ( n ) ] NEW_LINE y = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT j = 0 NEW_LINE x = 0 NEW_LINE temp [ j ] = a [ x ] NEW_LINE j += 1 NEW_LINE x = a [ x ] + x NEW_LINE while ( x < n ) : NEW_LINE INDENT temp [ j ] = a [ x ] NEW_LINE j += 1 NEW_LINE x = a [ x ] + x NEW_LINE DEDENT if ( y < j ) : NEW_LINE INDENT for k in range ( 0 , j , 1 ) : NEW_LINE INDENT print1 [ k ] = temp [ k ] NEW_LINE y = j NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , y , 1 ) : NEW_LINE INDENT print ( print1 [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE maxLengthSubSeq ( a , n ) NEW_LINE DEDENT


class GFG { static double areaCuboid ( double l , double h , double w ) { return ( l * h * w ) ; } static double surfaceAreaCuboid ( double l , double h , double w ) { return ( 2 * l * w + 2 * w * h + 2 * l * h ) ; } public static void main ( String [ ] args ) { double l = 1 ; double h = 5 ; double w = 7 ; System . out . println ( " Area ▁ = ▁ " + areaCuboid ( l , h , w ) ) ; System . out . println ( " Total ▁ Surface ▁ Area ▁ = ▁ " + surfaceAreaCuboid ( l , h , w ) ) ; } }

def volumeCuboid ( l , h , w ) : NEW_LINE INDENT return ( l * h * w ) NEW_LINE DEDENT def surfaceAreaCuboid ( l , h , w ) : NEW_LINE INDENT return ( 2 * l * w + 2 * w * h + 2 * l * h ) NEW_LINE DEDENT l = 1 NEW_LINE h = 5 NEW_LINE w = 7 NEW_LINE print ( " Volume ▁ = " , volumeCuboid ( l , h , w ) ) NEW_LINE print ( " Total ▁ Surface ▁ Area ▁ = " , surfaceAreaCuboid ( l , h , w ) ) NEW_LINE


class GFG { static boolean oppositeSigns ( int x , int y ) { return ( ( x ^ y ) < 0 ) ; } public static void main ( String [ ] args ) { int x = 100 , y = - 100 ; if ( oppositeSigns ( x , y ) == true ) System . out . println ( " Signs ▁ are ▁ opposite " ) ; else System . out . println ( " Signs ▁ are ▁ not ▁ opposite " ) ; } }

def oppositeSigns ( x , y ) : NEW_LINE INDENT return ( ( x ^ y ) < 0 ) ; NEW_LINE DEDENT x = 100 NEW_LINE y = 1 NEW_LINE if ( oppositeSigns ( x , y ) == True ) : NEW_LINE INDENT print " Signs ▁ are ▁ opposite " NEW_LINE DEDENT else : NEW_LINE INDENT print " Signs ▁ are ▁ not ▁ opposite " NEW_LINE DEDENT


class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static void noOfSquares ( int x1 , int y1 , int x2 , int y2 ) { int dx = Math . abs ( x2 - x1 ) ; int dy = Math . abs ( y2 - y1 ) ; int ans = dx + dy - __gcd ( dx , dy ) ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int x1 = 1 , y1 = 1 , x2 = 4 , y2 = 3 ; noOfSquares ( x1 , y1 , x2 , y2 ) ; } }

from math import gcd NEW_LINE def noOfSquares ( x1 , y1 , x2 , y2 ) : NEW_LINE INDENT dx = abs ( x2 - x1 ) ; NEW_LINE dy = abs ( y2 - y1 ) ; NEW_LINE ans = dx + dy - gcd ( dx , dy ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x1 = 1 ; y1 = 1 ; x2 = 4 ; y2 = 3 ; NEW_LINE noOfSquares ( x1 , y1 , x2 , y2 ) ; NEW_LINE DEDENT


public class Optimal_BST2 { static int optimalSearchTree ( int keys [ ] , int freq [ ] , int n ) { int cost [ ] [ ] = new int [ n + 1 ] [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) cost [ i ] [ i ] = freq [ i ] ; for ( int L = 2 ; L <= n ; L ++ ) { for ( int i = 0 ; i <= n - L + 1 ; i ++ ) { int j = i + L - 1 ; cost [ i ] [ j ] = Integer . MAX_VALUE ; for ( int r = i ; r <= j ; r ++ ) { int c = ( ( r > i ) ? cost [ i ] [ r - 1 ] : 0 ) + ( ( r < j ) ? cost [ r + 1 ] [ j ] : 0 ) + sum ( freq , i , j ) ; if ( c < cost [ i ] [ j ] ) cost [ i ] [ j ] = c ; } } } return cost [ 0 ] [ n - 1 ] ; } static int sum ( int freq [ ] , int i , int j ) { int s = 0 ; for ( int k = i ; k <= j ; k ++ ) { if ( k >= freq . length ) continue ; s += freq [ k ] ; } return s ; } public static void main ( String [ ] args ) { int keys [ ] = { 10 , 12 , 20 } ; int freq [ ] = { 34 , 8 , 50 } ; int n = keys . length ; System . out . println ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is ▁ " + optimalSearchTree ( keys , freq , n ) ) ; } }

INT_MAX = 2147483647 NEW_LINE def optimalSearchTree ( keys , freq , n ) : NEW_LINE INDENT cost = [ [ 0 for x in range ( n ) ] for y in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT cost [ i ] [ i ] = freq [ i ] NEW_LINE DEDENT for L in range ( 2 , n + 1 ) : NEW_LINE INDENT for i in range ( n - L + 2 ) : NEW_LINE INDENT j = i + L - 1 NEW_LINE if i >= n or j >= n : NEW_LINE INDENT break NEW_LINE DEDENT cost [ i ] [ j ] = INT_MAX NEW_LINE for r in range ( i , j + 1 ) : NEW_LINE INDENT c = 0 NEW_LINE if ( r > i ) : NEW_LINE INDENT c += cost [ i ] [ r - 1 ] NEW_LINE DEDENT if ( r < j ) : NEW_LINE INDENT c += cost [ r + 1 ] [ j ] NEW_LINE DEDENT c += sum ( freq , i , j ) NEW_LINE if ( c < cost [ i ] [ j ] ) : NEW_LINE INDENT cost [ i ] [ j ] = c NEW_LINE DEDENT DEDENT DEDENT DEDENT return cost [ 0 ] [ n - 1 ] NEW_LINE DEDENT def sum ( freq , i , j ) : NEW_LINE INDENT s = 0 NEW_LINE for k in range ( i , j + 1 ) : NEW_LINE INDENT s += freq [ k ] NEW_LINE DEDENT return s NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT keys = [ 10 , 12 , 20 ] NEW_LINE freq = [ 34 , 8 , 50 ] NEW_LINE n = len ( keys ) NEW_LINE print ( " Cost ▁ of ▁ Optimal ▁ BST ▁ is " , optimalSearchTree ( keys , freq , n ) ) NEW_LINE DEDENT


class GFG { static float normal ( float m , float n ) { float N = ( float ) ( ( Math . abs ( m ) * Math . abs ( n ) ) / Math . sqrt ( ( Math . abs ( m ) * Math . abs ( m ) ) + ( Math . abs ( n ) * Math . abs ( n ) ) ) ) ; return N ; } public static void main ( String [ ] args ) { float m = - 5 , n = 3 ; System . out . println ( normal ( m , n ) ) ; } }

import math ; NEW_LINE def normal ( m , n ) : NEW_LINE INDENT N = ( ( abs ( m ) * abs ( n ) ) / math . sqrt ( ( abs ( m ) * abs ( m ) ) + ( abs ( n ) * abs ( n ) ) ) ) ; NEW_LINE return N ; NEW_LINE DEDENT m = - 5 ; n = 3 ; NEW_LINE print ( normal ( m , n ) ) ; NEW_LINE


class GFG { static int mod = 1000000007 ; static int [ ] [ ] dp = new int [ 55 ] [ 55 ] ; static int NoofWays ( int face , int throwsVal , int sum ) { if ( sum == 0 && throwsVal == 0 ) { return 1 ; } if ( sum < 0 || throwsVal == 0 ) { return 0 ; } if ( dp [ throwsVal ] [ sum ] != - 1 ) { return dp [ throwsVal ] [ sum ] ; } int ans = 0 ; for ( int i = 1 ; i <= face ; i ++ ) { ans += NoofWays ( face , throwsVal - 1 , sum - i ) ; } return dp [ throwsVal ] [ sum ] = ans ; } public static void main ( String [ ] args ) { int faces = 6 , throwsVal = 3 , sum = 12 ; for ( int i = 0 ; i < 55 ; i ++ ) { for ( int j = 0 ; j < 55 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } System . out . println ( NoofWays ( faces , throwsVal , sum ) ) ; } }

import numpy as np NEW_LINE mod = 1000000007 ; NEW_LINE dp = np . zeros ( ( 55 , 55 ) ) ; NEW_LINE def NoofWays ( face , throws , sum ) : NEW_LINE INDENT if ( sum == 0 and throws == 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT if ( sum < 0 or throws == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ throws ] [ sum ] != - 1 ) : NEW_LINE INDENT return dp [ throws ] [ sum ] ; NEW_LINE DEDENT ans = 0 ; NEW_LINE for i in range ( 1 , face + 1 ) : NEW_LINE INDENT ans += NoofWays ( face , throws - 1 , sum - i ) ; NEW_LINE DEDENT dp [ throws ] [ sum ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT faces = 6 ; throws = 3 ; sum = 12 ; NEW_LINE for i in range ( 55 ) : NEW_LINE INDENT for j in range ( 55 ) : NEW_LINE INDENT dp [ i ] [ j ] = - 1 NEW_LINE DEDENT DEDENT print ( NoofWays ( faces , throws , sum ) ) ; NEW_LINE DEDENT


class Test { static int gcd ( int a , int b ) { if ( b == 0 ) return a ; return gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int a = 98 , b = 56 ; System . out . println ( " GCD ▁ of ▁ " + a + " ▁ and ▁ " + b + " ▁ is ▁ " + gcd ( a , b ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return gcd ( b , a % b ) NEW_LINE DEDENT a = 98 NEW_LINE b = 56 NEW_LINE if ( gcd ( a , b ) ) : NEW_LINE INDENT print ( ' GCD ▁ of ' , a , ' and ' , b , ' is ' , gcd ( a , b ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' not ▁ found ' ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX = 100 , n ; static int [ ] store = new int [ MAX ] ; static int [ ] [ ] graph = new int [ MAX ] [ MAX ] ; static int [ ] d = new int [ MAX ] ; static boolean is_clique ( int b ) { for ( int i = 1 ; i < b ; i ++ ) { for ( int j = i + 1 ; j < b ; j ++ ) if ( graph [ store [ i ] ] [ store [ j ] ] == 0 ) return false ; } return true ; } static int maxCliques ( int i , int l ) { int max_ = 0 ; for ( int j = i + 1 ; j <= n ; j ++ ) { store [ l ] = j ; if ( is_clique ( l + 1 ) ) { max_ = Math . max ( max_ , l ) ; max_ = Math . max ( max_ , maxCliques ( j , l + 1 ) ) ; } } return max_ ; } public static void main ( String [ ] args ) { int [ ] [ ] edges = { { 1 , 2 } , { 2 , 3 } , { 3 , 1 } , { 4 , 3 } , { 4 , 1 } , { 4 , 2 } } ; int size = edges . length ; n = 4 ; for ( int i = 0 ; i < size ; i ++ ) { graph [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = 1 ; graph [ edges [ i ] [ 1 ] ] [ edges [ i ] [ 0 ] ] = 1 ; d [ edges [ i ] [ 0 ] ] ++ ; d [ edges [ i ] [ 1 ] ] ++ ; } System . out . print ( maxCliques ( 0 , 1 ) ) ; } }

MAX = 100 ; NEW_LINE n = 0 ; NEW_LINE store = [ 0 ] * MAX ; NEW_LINE graph = [ [ 0 for i in range ( MAX ) ] for j in range ( MAX ) ] ; NEW_LINE d = [ 0 ] * MAX ; NEW_LINE def is_clique ( b ) : NEW_LINE INDENT for i in range ( 1 , b ) : NEW_LINE INDENT for j in range ( i + 1 , b ) : NEW_LINE INDENT if ( graph [ store [ i ] ] [ store [ j ] ] == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT DEDENT return True ; NEW_LINE DEDENT def maxCliques ( i , l ) : NEW_LINE INDENT max_ = 0 ; NEW_LINE for j in range ( i + 1 , n + 1 ) : NEW_LINE INDENT store [ l ] = j ; NEW_LINE if ( is_clique ( l + 1 ) ) : NEW_LINE INDENT max_ = max ( max_ , l ) ; NEW_LINE max_ = max ( max_ , maxCliques ( j , l + 1 ) ) ; NEW_LINE DEDENT DEDENT return max_ ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT edges = [ [ 1 , 2 ] , [ 2 , 3 ] , [ 3 , 1 ] , [ 4 , 3 ] , [ 4 , 1 ] , [ 4 , 2 ] ] ; NEW_LINE size = len ( edges ) ; NEW_LINE n = 4 ; NEW_LINE for i in range ( size ) : NEW_LINE INDENT graph [ edges [ i ] [ 0 ] ] [ edges [ i ] [ 1 ] ] = 1 ; NEW_LINE graph [ edges [ i ] [ 1 ] ] [ edges [ i ] [ 0 ] ] = 1 ; NEW_LINE d [ edges [ i ] [ 0 ] ] += 1 ; NEW_LINE d [ edges [ i ] [ 1 ] ] += 1 ; NEW_LINE DEDENT print ( maxCliques ( 0 , 1 ) ) ; NEW_LINE DEDENT


import java . util . Arrays ; class GFG { static int countDistinct ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( i < n - 1 && arr [ i ] == arr [ i + 1 ] ) { i ++ ; } res ++ ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 } ; int n = arr . length ; System . out . println ( countDistinct ( arr , n ) ) ; } }

def countDistinct ( arr , n ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE res = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT while ( i < n - 1 and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT i += 1 ; NEW_LINE DEDENT res += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT arr = [ 6 , 10 , 5 , 4 , 9 , 120 , 4 , 6 , 10 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countDistinct ( arr , n ) ) ; NEW_LINE


import java . io . * ; class GFG { static int MAXN = 100 ; static int maxValue ( int n , int m , int x [ ] , int y [ ] , int a [ ] ) { int c [ ] [ ] = new int [ MAXN ] [ MAXN ] ; for ( int i = 0 ; i < m ; ++ i ) { for ( int p = 0 ; p < n ; ++ p ) { for ( int q = 0 ; q < n ; ++ q ) { int dx = Math . abs ( p - x [ i ] ) ; int dy = Math . abs ( q - y [ i ] ) ; int d = Math . max ( dx , dy ) ; c [ p ] [ q ] += Math . max ( 0 , a [ i ] - d ) ; } } } int res = 0 ; for ( int i = 0 ; i < n ; ++ i ) { for ( int j = 0 ; j < n ; ++ j ) { res = Math . max ( res , c [ i ] [ j ] ) ; } } return res ; } public static void main ( String [ ] args ) { int n = 10 ; int m = 2 ; int x [ ] = { 3 , 7 } ; int y [ ] = { 3 , 7 } ; int a [ ] = { 4 , 3 } ; System . out . println ( maxValue ( n , m , x , y , a ) ) ; } }

MAXN = 100 NEW_LINE def maxValue ( n , m , x , y , a ) : NEW_LINE INDENT c = [ [ 0 for x in range ( MAXN ) ] for y in range ( MAXN ) ] NEW_LINE for i in range ( m ) : NEW_LINE INDENT for p in range ( n ) : NEW_LINE INDENT for q in range ( n ) : NEW_LINE INDENT dx = abs ( p - x [ i ] ) NEW_LINE dy = abs ( q - y [ i ] ) NEW_LINE d = max ( dx , dy ) NEW_LINE c [ p ] [ q ] += max ( 0 , a [ i ] - d ) NEW_LINE DEDENT DEDENT DEDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT res = max ( res , c [ i ] [ j ] ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE m = 2 NEW_LINE x = [ 3 , 7 ] NEW_LINE y = [ 3 , 7 ] NEW_LINE a = [ 4 , 3 ] NEW_LINE print ( maxValue ( n , m , x , y , a ) ) NEW_LINE DEDENT


class GFG { static int numberOfPermutations ( int a [ ] , int n ) { int sumEven , sumOdd , c = 0 ; do { sumEven = sumOdd = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) { sumOdd += a [ i ] ; } else { sumEven += a [ i ] ; } } if ( sumOdd == sumEven ) { c ++ ; } } while ( next_permutation ( a ) ) ; return c ; } static boolean next_permutation ( int [ ] p ) { for ( int a = p . length - 2 ; a >= 0 ; -- a ) { if ( p [ a ] < p [ a + 1 ] ) { for ( int b = p . length - 1 ; ; -- b ) { if ( p [ b ] > p [ a ] ) { int t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; for ( ++ a , b = p . length - 1 ; a < b ; ++ a , -- b ) { t = p [ a ] ; p [ a ] = p [ b ] ; p [ b ] = t ; } return true ; } } } } return false ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; System . out . println ( numberOfPermutations ( a , n ) ) ; } }

def next_permutation ( arr ) : NEW_LINE INDENT arrCount = len ( arr ) ; NEW_LINE i = arrCount - 1 ; NEW_LINE while ( i > 0 and arr [ i ] <= arr [ i - 1 ] ) : NEW_LINE INDENT i -= 1 ; NEW_LINE DEDENT if ( i <= 0 ) : NEW_LINE INDENT return [ False , arr ] ; NEW_LINE DEDENT pivotIndex = i - 1 ; NEW_LINE j = arrCount - 1 ; NEW_LINE while ( arr [ j ] <= arr [ pivotIndex ] ) : NEW_LINE INDENT j -= 1 ; NEW_LINE DEDENT temp = arr [ pivotIndex ] ; NEW_LINE arr [ pivotIndex ] = arr [ j ] ; NEW_LINE arr [ j ] = temp ; NEW_LINE j = arrCount - 1 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT temp = arr [ i ] ; NEW_LINE arr [ i ] = arr [ j ] ; NEW_LINE arr [ j ] = temp ; NEW_LINE i += 1 ; NEW_LINE j -= 1 ; NEW_LINE DEDENT return [ True , arr ] ; NEW_LINE DEDENT def numberOfPermutations ( a , n ) : NEW_LINE INDENT sumEven = 0 ; NEW_LINE sumOdd = 0 ; NEW_LINE c = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT sumEven = 0 ; NEW_LINE sumOdd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i % 2 ) : NEW_LINE INDENT sumOdd += a [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT sumEven += a [ i ] ; NEW_LINE DEDENT DEDENT if ( sumOdd == sumEven ) : NEW_LINE INDENT c += 1 ; NEW_LINE DEDENT xx = next_permutation ( a ) ; NEW_LINE if ( xx [ 0 ] == False ) : NEW_LINE INDENT break ; NEW_LINE DEDENT a = xx [ 1 ] ; NEW_LINE DEDENT return c ; NEW_LINE DEDENT a = [ 1 , 2 , 3 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( numberOfPermutations ( a , n ) ) ; NEW_LINE


class GFG { static int minOperations ( int A , int B ) { if ( A > B ) { A = A + B ; B = A - B ; A = A - B ; } B = B / __gcd ( A , B ) ; return B - 1 ; } static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } public static void main ( String [ ] args ) { int A = 7 , B = 15 ; System . out . print ( minOperations ( A , B ) + " \n " ) ; } }

import math NEW_LINE def minOperations ( A , B ) : NEW_LINE INDENT if ( A > B ) : NEW_LINE INDENT swap ( A , B ) NEW_LINE DEDENT B = B // math . gcd ( A , B ) ; NEW_LINE return B - 1 NEW_LINE DEDENT A = 7 NEW_LINE B = 15 NEW_LINE print ( minOperations ( A , B ) ) NEW_LINE


class GFG { static int MAX = 10 ; static int [ ] [ ] [ ] F = new int [ MAX ] [ MAX ] [ MAX ] ; static int noOfAssignments ( String s , int n , int i , int c_x , int c_y ) { if ( F [ i ] [ c_x ] [ c_y ] != - 1 ) return F [ i ] [ c_x ] [ c_y ] ; if ( i == n ) { F [ i ] [ c_x ] [ c_y ] = ( c_x == 0 && c_y == 0 ) ? 1 : 0 ; return F [ i ] [ c_x ] [ c_y ] ; } if ( s . charAt ( i ) == ' ( ' ) { F [ i ] [ c_x ] [ c_y ] = noOfAssignments ( s , n , i + 1 , c_x + 1 , c_y ) + noOfAssignments ( s , n , i + 1 , c_x , c_y + 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } F [ i ] [ c_x ] [ c_y ] = 0 ; if ( c_x != 0 ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( s , n , i + 1 , c_x - 1 , c_y ) ; if ( c_y != 0 ) F [ i ] [ c_x ] [ c_y ] += noOfAssignments ( s , n , i + 1 , c_x , c_y - 1 ) ; return F [ i ] [ c_x ] [ c_y ] ; } public static void main ( String [ ] args ) { String s = " ( ( ) ) " ; int n = s . length ( ) ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int k = 0 ; k < MAX ; k ++ ) F [ i ] [ j ] [ k ] = - 1 ; System . out . println ( noOfAssignments ( s , n , 0 , 0 , 0 ) ) ; } }

MAX = 10 NEW_LINE F = [ [ [ - 1 for i in range ( MAX ) ] for j in range ( MAX ) ] for k in range ( MAX ) ] NEW_LINE def noOfAssignments ( S , n , i , c_x , c_y ) : NEW_LINE INDENT if F [ i ] [ c_x ] [ c_y ] != - 1 : NEW_LINE INDENT return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if i == n : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] = not c_x and not c_y NEW_LINE return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if S [ i ] == ' ( ' : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] =   \ NEW_LINE INDENT noOfAssignments ( S , n , i + 1 , c_x + 1 , c_y ) +   \ NEW_LINE noOfAssignments ( S , n , i + 1 , c_x , c_y + 1 ) NEW_LINE DEDENT return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT F [ i ] [ c_x ] [ c_y ] = 0 NEW_LINE if c_x : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] +=   \ NEW_LINE INDENT noOfAssignments ( S , n , i + 1 , c_x - 1 , c_y ) NEW_LINE DEDENT DEDENT if c_y : NEW_LINE INDENT F [ i ] [ c_x ] [ c_y ] +=   \ NEW_LINE INDENT noOfAssignments ( S , n , i + 1 , c_x , c_y - 1 ) NEW_LINE DEDENT DEDENT return F [ i ] [ c_x ] [ c_y ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = " ( ( ) ) " NEW_LINE n = len ( S ) NEW_LINE print ( noOfAssignments ( S , n , 0 , 0 , 0 ) ) NEW_LINE DEDENT


public class GFG { static boolean isVowel ( char c ) { c = Character . toLowerCase ( c ) ; if ( c == ' a ' || c == ' e ' || c == ' i ' || c == ' o ' || c == ' u ' ) return true ; return false ; } static String firstXvowels ( String s , int x ) { String result = " " ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) { if ( isVowel ( s . charAt ( i ) ) ) result += s . charAt ( i ) ; if ( result . length ( ) == x ) { return result ; } } return " - 1" ; } public static void main ( String [ ] args ) { String str = " GeeksForGeeks " ; int x = 3 ; System . out . println ( firstXvowels ( str , x ) ) ; } }

def isVowel ( c ) : NEW_LINE INDENT c = c . lower ( ) NEW_LINE if ( c == ' a ' or c == ' e ' or c == ' i ' or c == ' o ' or c == ' u ' ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def firstXvowels ( s , x ) : NEW_LINE INDENT result = " " NEW_LINE for i in range ( 0 , len ( s ) , 1 ) : NEW_LINE INDENT if ( isVowel ( s [ i ] ) ) : NEW_LINE INDENT result += s [ i ] NEW_LINE DEDENT if ( len ( result ) == x ) : NEW_LINE INDENT return result NEW_LINE DEDENT DEDENT return " - 1" NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " GeeksForGeeks " NEW_LINE x = 3 NEW_LINE print ( firstXvowels ( str , x ) ) NEW_LINE DEDENT


class GFG { static int countSum ( int N , int L , int R ) { if ( L > R ) { return 0 ; } if ( N == 1 ) { return R - L + 1 ; } if ( N > 1 ) { return ( N - 2 ) * ( R - L ) + 1 ; } return 0 ; } public static void main ( String [ ] args ) { int N = 4 , L = 4 , R = 6 ; System . out . print ( countSum ( N , L , R ) ) ; } }

def countSum ( N , L , R ) : NEW_LINE INDENT if ( L > R ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( N == 1 ) : NEW_LINE INDENT return R - L + 1 ; NEW_LINE DEDENT if ( N > 1 ) : NEW_LINE INDENT return ( N - 2 ) * ( R - L ) + 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N , L , R = 4 , 4 , 6 ; NEW_LINE print ( countSum ( N , L , R ) ) ; NEW_LINE DEDENT