import java . util . * ; import java . lang . * ; import java . io . * ; public class Stringfun { String str = " Geeks ▁ for ▁ Geeks . " ; void words ( ) { int wCount = 0 , uCount = 0 , vCount = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { char c = str . charAt ( i ) ; switch ( c ) { case ' ▁ ' : case ' . ' : wCount ++ ; } switch ( c ) { case ' A ' : case ' E ' : case ' I ' : case ' O ' : case ' U ' : case ' a ' : case ' e ' : case ' i ' : case ' o ' : case ' u ' : vCount ++ ; } if ( c >= 65 && c <= 90 ) { uCount ++ ; } } System . out . println ( " Number ▁ of ▁ words ▁ = ▁ " + wCount ) ; System . out . println ( " Number ▁ of ▁ vowels ▁ = ▁ " + vCount ) ; System . out . println ( " Number ▁ of ▁ upper ▁ case ▁ characters ▁ = ▁ " + uCount ) ; } void frequency ( ) { TreeMap < Character , Integer > hmap = new TreeMap < Character , Integer > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { Integer c = hmap . get ( str . charAt ( i ) ) ; if ( hmap . get ( str . charAt ( i ) ) == null ) hmap . put ( str . charAt ( i ) , 1 ) ; else hmap . put ( str . charAt ( i ) , ++ c ) ; } for ( Map . Entry m : hmap . entrySet ( ) ) System . out . println ( " Character ▁ = ▁ " + m . getKey ( ) + " ▁ Frequency ▁ = ▁ " + m . getValue ( ) ) ; } public static void main ( String args [ ] ) throws IOException { Stringfun obj = new Stringfun ( ) ; obj . words ( ) ; obj . frequency ( ) ; } }

def words ( str ) : NEW_LINE INDENT wcount = vcount = ucount = i = 0 NEW_LINE while i < len ( str ) : NEW_LINE INDENT ch = str [ i ] NEW_LINE if ( ch == " ▁ " or ch == " . " ) : NEW_LINE INDENT wcount += 1 NEW_LINE DEDENT if ( ch == " a " or ch == " e " or ch == " i " or ch == ' o ' or ch == " u " ) : NEW_LINE INDENT vcount += 1 NEW_LINE DEDENT if ( ch == " A " or ch == " E " or ch == " I " or ch == ' O ' or ch == " U " ) : NEW_LINE INDENT vcount += 1 NEW_LINE DEDENT if ( ord ( ch ) >= 65 and ord ( ch ) <= 90 ) : NEW_LINE INDENT ucount += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT print ( " number ▁ of ▁ words ▁ = ▁ " , wcount ) NEW_LINE print ( " number ▁ of ▁ vowels ▁ = ▁ " , vcount ) NEW_LINE print ( " number ▁ of ▁ upper ▁ case ▁ characters ▁ = ▁ " , ucount ) NEW_LINE DEDENT def frequency ( str ) : NEW_LINE INDENT i = 1 NEW_LINE while i < 127 : NEW_LINE INDENT ch1 = chr ( i ) NEW_LINE c = 0 NEW_LINE j = 0 NEW_LINE while j < len ( str ) : NEW_LINE INDENT ch2 = str [ j ] NEW_LINE if ( ch1 == ch2 ) : NEW_LINE INDENT c += 1 NEW_LINE DEDENT j += 1 NEW_LINE DEDENT if c > 0 : NEW_LINE INDENT print ( " Character : " , ch1 + " ▁ Frequency : " , c ) NEW_LINE DEDENT i += 1 NEW_LINE DEDENT DEDENT s = " Geeks ▁ for ▁ Geeks . " NEW_LINE words ( s ) NEW_LINE frequency ( s ) NEW_LINE


class gfg { static boolean isValid ( String str , int len ) { for ( int i = 1 ; i < len ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) return false ; } return true ; } public static void main ( String [ ] args ) { String str = "0110" ; int len = str . length ( ) ; if ( isValid ( str , len ) ) System . out . println ( " Valid " ) ; else System . out . println ( " Invalid " ) ; } }

def isValid ( string , length ) : NEW_LINE INDENT for i in range ( 1 , length ) : NEW_LINE INDENT if string [ i ] == string [ i - 1 ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "0110" NEW_LINE length = len ( string ) NEW_LINE if isValid ( string , length ) : NEW_LINE INDENT print ( " Valid " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static boolean prime ( int n ) { if ( n == 1 ) return false ; for ( int i = 2 ; i * i <= n ; ++ i ) if ( n % i == 0 ) return false ; return true ; } static int Min_Sum ( int n ) { if ( prime ( n ) ) return 1 ; if ( n % 2 == 0 ) return 2 ; else { if ( prime ( n - 2 ) ) return 2 ; else return 3 ; } } public static void main ( String [ ] args ) { int n = 27 ; System . out . println ( Min_Sum ( n ) ) ; } }

from math import sqrt NEW_LINE def prime ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 , 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def Min_Sum ( n ) : NEW_LINE INDENT if ( prime ( n ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT if ( prime ( n - 2 ) ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT else : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 27 NEW_LINE print ( Min_Sum ( n ) ) NEW_LINE DEDENT


class GFG { static String LexicographicalMaxString ( String str ) { String mx = " " ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( mx . compareTo ( str . substring ( i ) ) <= 0 ) { mx = str . substring ( i ) ; } } return mx ; } public static void main ( String [ ] args ) { String str = " ababaa " ; System . out . println ( LexicographicalMaxString ( str ) ) ; } }

def LexicographicalMaxString ( str ) : NEW_LINE INDENT mx = " " NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT mx = max ( mx , str [ i : ] ) NEW_LINE DEDENT return mx NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ababaa " NEW_LINE print ( LexicographicalMaxString ( str ) ) NEW_LINE DEDENT


import java . util . * ; class solution { static int nthTerm ( int n ) { return 5 * ( int ) Math . pow ( n , 2 ) - 5 * n ; } public static void main ( String arr [ ] ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }

def nthTerm ( n ) : NEW_LINE INDENT return 5 * pow ( n , 2 ) - 5 * n NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE


import java . util . * ; class solution { void printArr ( int b [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( " ▁ " + b [ i ] + " ▁ " ) ; } } void ModifiedArray ( int a [ ] , int n ) { int l = 0 , r = Integer . MAX_VALUE ; int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n / 2 ; i ++ ) { b [ i ] = Math . max ( l , a [ i ] - r ) ; b [ n - i - 1 ] = a [ i ] - b [ i ] ; l = b [ i ] ; r = b [ n - i - 1 ] ; } printArr ( b , n ) ; } public static void main ( String args [ ] ) { int a [ ] = { 5 , 6 } ; int n = a . length ; solution s = new solution ( ) ; s . ModifiedArray ( a , 2 * n ) ; } }

import sys NEW_LINE def printArr ( b , n ) : NEW_LINE INDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( b [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def ModifiedArray ( a , n ) : NEW_LINE INDENT l = 0 NEW_LINE r = sys . maxsize NEW_LINE b = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 0 , int ( n / 2 ) , 1 ) : NEW_LINE INDENT b [ i ] = max ( l , a [ i ] - r ) NEW_LINE b [ n - i - 1 ] = a [ i ] - b [ i ] NEW_LINE l = b [ i ] NEW_LINE r = b [ n - i - 1 ] NEW_LINE DEDENT printArr ( b , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 5 , 6 ] NEW_LINE n = len ( a ) NEW_LINE ModifiedArray ( a , 2 * n ) NEW_LINE DEDENT


import java . io . * ; class countSetBits { static int countSetBits ( int n ) { int count = 0 ; while ( n > 0 ) { count += n & 1 ; n >>= 1 ; } return count ; } public static void main ( String args [ ] ) { int i = 9 ; System . out . println ( countSetBits ( i ) ) ; } }

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


class GFG { public static void main ( String [ ] args ) { long ans = 1 ; long mod = ( long ) 1000000007 * 120 ; for ( int i = 0 ; i < 5 ; i ++ ) ans = ( ans * ( 55555 - i ) ) % mod ; ans = ans / 120 ; System . out . println ( " Answer ▁ using " + " ▁ shortcut : ▁ " + ans ) ; } }

ans = 1 NEW_LINE mod = 1000000007 * 120 NEW_LINE for i in range ( 0 , 5 ) : NEW_LINE INDENT ans = ( ans * ( 55555 - i ) ) % mod NEW_LINE DEDENT ans = int ( ans / 120 ) NEW_LINE print ( " Answer ▁ using ▁ shortcut : ▁ " , ans ) NEW_LINE


public class GFG { static final int MAX_CHAR = 26 ; static int countSubstringWithEqualEnds ( String s ) { int result = 0 ; int n = s . length ( ) ; int [ ] count = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < n ; i ++ ) count [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; return result ; } public static void main ( String args [ ] ) { String s = " abcab " ; System . out . println ( countSubstringWithEqualEnds ( s ) ) ; } }

MAX_CHAR = 26 ; NEW_LINE def countSubstringWithEqualEnds ( s ) : NEW_LINE INDENT result = 0 ; NEW_LINE n = len ( s ) ; NEW_LINE count = [ 0 ] * MAX_CHAR ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( MAX_CHAR ) : NEW_LINE INDENT result += ( count [ i ] * ( count [ i ] + 1 ) / 2 ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT s = " abcab " ; NEW_LINE print ( countSubstringWithEqualEnds ( s ) ) ; NEW_LINE


import java . util . * ; class GFG { static boolean isPalindrome ( int n ) { String str = String . valueOf ( n ) ; int s = 0 , e = str . length ( ) - 1 ; while ( s < e ) { if ( str . charAt ( s ) != str . charAt ( e ) ) { return false ; } s ++ ; e -- ; } return true ; } static void palindromicDivisors ( int n ) { Vector < Integer > PalindromDivisors = new Vector < Integer > ( ) ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) { if ( isPalindrome ( i ) ) { PalindromDivisors . add ( i ) ; } } else { if ( isPalindrome ( i ) ) { PalindromDivisors . add ( i ) ; } if ( isPalindrome ( n / i ) ) { PalindromDivisors . add ( n / i ) ; } } } } Collections . sort ( PalindromDivisors ) ; for ( int i = 0 ; i < PalindromDivisors . size ( ) ; i ++ ) { System . out . print ( PalindromDivisors . get ( i ) + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 66 ; palindromicDivisors ( n ) ; } }

from math import sqrt ; NEW_LINE def isPalindrome ( n ) : NEW_LINE INDENT string = str ( n ) ; NEW_LINE s = 0 ; e = len ( string ) - 1 ; NEW_LINE while ( s < e ) : NEW_LINE INDENT if ( string [ s ] != string [ e ] ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT s += 1 ; NEW_LINE e -= 1 ; NEW_LINE DEDENT return True ; NEW_LINE DEDENT def palindromicDivisors ( n ) : NEW_LINE INDENT PalindromDivisors = [ ] ; NEW_LINE for i in range ( 1 , int ( sqrt ( n ) ) ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT if ( n // i == i ) : NEW_LINE INDENT if ( isPalindrome ( i ) ) : NEW_LINE INDENT PalindromDivisors . append ( i ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT if ( isPalindrome ( i ) ) : NEW_LINE INDENT PalindromDivisors . append ( i ) ; NEW_LINE DEDENT if ( isPalindrome ( n // i ) ) : NEW_LINE INDENT PalindromDivisors . append ( n // i ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT PalindromDivisors . sort ( ) ; NEW_LINE for i in range ( len ( PalindromDivisors ) ) : NEW_LINE INDENT print ( PalindromDivisors [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 66 ; NEW_LINE palindromicDivisors ( n ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int factorial ( int f ) { int fact = 1 ; for ( int i = 2 ; i <= f ; i ++ ) fact *= ( int ) i ; return fact ; } static int findPermutation ( int N , int M ) { int permutation = factorial ( N + M - 1 ) / ( factorial ( N ) * factorial ( M - 1 ) ) ; return permutation ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; System . out . println ( findPermutation ( N , M ) ) ; } }

def factorial ( f ) : NEW_LINE INDENT fact = 1 NEW_LINE for i in range ( 2 , f + 1 ) : NEW_LINE INDENT fact *= i NEW_LINE DEDENT return fact NEW_LINE DEDENT def findPermuatation ( N , M ) : NEW_LINE INDENT permutation = ( factorial ( N + M - 1 ) // ( factorial ( N ) * factorial ( M - 1 ) ) ) NEW_LINE return permutation NEW_LINE DEDENT N = 3 ; M = 3 NEW_LINE print ( findPermuatation ( N , M ) ) NEW_LINE


import java . util . * ; class GFG { static int findK ( int n , int k ) { ArrayList < Integer > a = new ArrayList < Integer > ( n ) ; for ( int i = 1 ; i < n ; i ++ ) if ( i % 2 == 1 ) a . add ( i ) ; for ( int i = 1 ; i < n ; i ++ ) if ( i % 2 == 0 ) a . add ( i ) ; return ( a . get ( k - 1 ) ) ; } public static void main ( String [ ] args ) { int n = 10 , k = 3 ; System . out . println ( findK ( n , k ) ) ; } }

def findK ( n , k ) : NEW_LINE INDENT a = list ( ) NEW_LINE i = 1 NEW_LINE while i < n : NEW_LINE INDENT a . append ( i ) NEW_LINE i = i + 2 NEW_LINE DEDENT i = 2 NEW_LINE while i < n : NEW_LINE INDENT a . append ( i ) NEW_LINE i = i + 2 NEW_LINE DEDENT return ( a [ k - 1 ] ) NEW_LINE DEDENT n = 10 NEW_LINE k = 3 NEW_LINE print ( findK ( n , k ) ) NEW_LINE


import java . util . * ; class GFG { static void reArrange ( String words [ ] , int n ) { HashMap < String , Integer > freq = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { freq . put ( words [ i ] , ( i + 1 ) ) ; } Arrays . sort ( words ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( freq . get ( words [ i ] ) + " ▁ " ) ; } public static void main ( String [ ] args ) { String words [ ] = { " live " , " place " , " travel " , " word " , " sky " } ; int n = words . length ; reArrange ( words , n ) ; } }

def reArrange ( words , n ) : NEW_LINE INDENT mp = { } NEW_LINE for i in range ( n ) : NEW_LINE INDENT mp [ words [ i ] ] = i + 1 NEW_LINE DEDENT words . sort ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( mp [ words [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT words = [ " live " , " place " , " travel " , " word " , " sky " ] NEW_LINE n = len ( words ) NEW_LINE reArrange ( words , n ) ; NEW_LINE


import java . util . * ; class GFG { static void CountTriangles ( int [ ] A ) { int n = A . length ; Arrays . sort ( A ) ; int count = 0 ; for ( int i = n - 1 ; i >= 1 ; i -- ) { int l = 0 , r = i - 1 ; while ( l < r ) { if ( A [ l ] + A [ r ] > A [ i ] ) { count += r - l ; r -- ; } else { l ++ ; } } } System . out . print ( " No ▁ of ▁ possible ▁ solutions : ▁ " + count ) ; } public static void main ( String [ ] args ) { int [ ] A = { 4 , 3 , 5 , 7 , 6 } ; CountTriangles ( A ) ; } }

def CountTriangles ( A ) : NEW_LINE INDENT n = len ( A ) ; NEW_LINE A . sort ( ) ; NEW_LINE count = 0 ; NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT l = 0 ; NEW_LINE r = i - 1 ; NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( A [ l ] + A [ r ] > A [ i ] ) : NEW_LINE INDENT count += r - l ; NEW_LINE r -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT l += 1 ; NEW_LINE DEDENT DEDENT DEDENT print ( " No ▁ of ▁ possible ▁ solutions : ▁ " , count ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 4 , 3 , 5 , 7 , 6 ] ; NEW_LINE CountTriangles ( A ) ; NEW_LINE DEDENT


public class GFG { static void findNumbers ( int arr [ ] , int n ) { int sumN = ( n * ( n + 1 ) ) / 2 ; int sumSqN = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; int sum = 0 , sumSq = 0 , i ; for ( i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; sumSq += Math . pow ( arr [ i ] , 2 ) ; } int B = ( ( ( sumSq - sumSqN ) / ( sum - sumN ) ) + sumN - sum ) / 2 ; int A = sum - sumN + B ; System . out . println ( " A ▁ = ▁ " + A + " \n B ▁ = ▁ " + B ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 } ; int n = arr . length ; findNumbers ( arr , n ) ; } }

import math NEW_LINE def findNumbers ( arr , n ) : NEW_LINE INDENT sumN = ( n * ( n + 1 ) ) / 2 ; NEW_LINE sumSqN = ( n * ( n + 1 ) * ( 2 * n + 1 ) ) / 6 ; NEW_LINE sum = 0 ; NEW_LINE sumSq = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum = sum + arr [ i ] ; NEW_LINE sumSq = sumSq + ( math . pow ( arr [ i ] , 2 ) ) ; NEW_LINE DEDENT B = ( ( ( sumSq - sumSqN ) / ( sum - sumN ) ) + sumN - sum ) / 2 ; NEW_LINE A = sum - sumN + B ; NEW_LINE print ( " A ▁ = ▁ " , int ( A ) ) ; NEW_LINE print ( " B ▁ = ▁ " , int ( B ) ) ; NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 3 , 4 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE findNumbers ( arr , n ) ; NEW_LINE


public class GFG { static int Rank ( int Edges , int Vertices ) { int result = 0 ; result = Edges - Vertices + 1 ; return result ; } public static void main ( String [ ] args ) { int Edges = 7 , Vertices = 5 ; System . out . println ( " Circuit ▁ Rank ▁ = ▁ " + Rank ( Edges , Vertices ) ) ; } }

def Rank ( Edges , Vertices ) : NEW_LINE INDENT result = Edges - Vertices + 1 NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT Edges , Vertices = 7 , 5 NEW_LINE print ( " Circuit ▁ Rank ▁ = " , Rank ( Edges , Vertices ) ) NEW_LINE DEDENT


class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } public static void main ( String [ ] args ) { if ( isPowerful ( 20 ) ) System . out . print ( " YES \n " ) ; else System . out . print ( " NO \n " ) ; if ( isPowerful ( 27 ) ) System . out . print ( " YES \n " ) ; else System . out . print ( " NO \n " ) ; } }

import math NEW_LINE def isPowerful ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n = n // 2 NEW_LINE power = power + 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT for factor in range ( 3 , int ( math . sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % factor == 0 ) : NEW_LINE INDENT n = n // factor NEW_LINE power = power + 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT return ( n == 1 ) NEW_LINE DEDENT print ( " YES " if isPowerful ( 20 ) else " NO " ) NEW_LINE print ( " YES " if isPowerful ( 27 ) else " NO " ) NEW_LINE


class GFG { static int valueofX ( int ar [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum = sum + ar [ i ] ; } if ( sum % n == 0 ) { return sum / n ; } else { int A = sum / n , B = sum / n + 1 ; int ValueA = 0 , ValueB = 0 ; for ( int i = 0 ; i < n ; i ++ ) { ValueA += ( ar [ i ] - A ) * ( ar [ i ] - A ) ; ValueB += ( ar [ i ] - B ) * ( ar [ i ] - B ) ; } if ( ValueA < ValueB ) { return A ; } else { return B ; } } } public static void main ( String args [ ] ) { int n = 7 ; int arr [ ] = { 6 , 9 , 1 , 6 , 1 , 3 , 7 } ; System . out . println ( valueofX ( arr , n ) ) ; } }

def valueofX ( ar , n ) : NEW_LINE INDENT summ = sum ( ar ) NEW_LINE if ( summ % n == 0 ) : NEW_LINE INDENT return summ // n NEW_LINE DEDENT else : NEW_LINE INDENT A = summ // n NEW_LINE B = summ // n + 1 NEW_LINE ValueA = 0 NEW_LINE ValueB = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ValueA += ( ar [ i ] - A ) * ( ar [ i ] - A ) NEW_LINE ValueB += ( ar [ i ] - B ) * ( ar [ i ] - B ) NEW_LINE DEDENT if ( ValueA < ValueB ) : NEW_LINE INDENT return A NEW_LINE DEDENT else : NEW_LINE INDENT return B NEW_LINE DEDENT DEDENT DEDENT n = 7 NEW_LINE arr = [ 6 , 9 , 1 , 6 , 1 , 3 , 7 ] NEW_LINE print ( valueofX ( arr , n ) ) NEW_LINE


class GFG { static int ways ( int n ) { return n / 2 ; } public static void main ( String args [ ] ) { int n = 2 ; System . out . println ( ways ( n ) ) ; } }

def ways ( n ) : NEW_LINE INDENT return n // 2 NEW_LINE DEDENT n = 2 NEW_LINE print ( ways ( n ) ) NEW_LINE


class GFG { static int product ( int a [ ] , int n ) { int ans = 1 ; int val = ( int ) Math . pow ( 2 , n - 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { ans *= ( int ) Math . pow ( a [ i ] , val ) ; } return ans ; } public static void main ( String [ ] args ) { int n = 2 ; int a [ ] = { 3 , 7 } ; System . out . println ( product ( a , n ) ) ; } }

def product ( a , n ) : NEW_LINE INDENT ans = 1 NEW_LINE val = pow ( 2 , n - 1 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans *= pow ( a [ i ] , val ) NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 2 NEW_LINE a = [ 3 , 7 ] NEW_LINE print ( product ( a , n ) ) NEW_LINE


import java . io . * ; class GFG { static String getNumber ( String s ) { int number_of_digits = s . length ( ) ; int freq [ ] = new int [ 10 ] ; for ( int i = 0 ; i < number_of_digits ; i ++ ) { if ( s . charAt ( i ) == '1' || s . charAt ( i ) == '2' || s . charAt ( i ) == '3' || s . charAt ( i ) == '5' || s . charAt ( i ) == '7' ) { freq [ s . charAt ( i ) - 48 ] += 1 ; } if ( s . charAt ( i ) == '4' ) { freq [ 2 ] += 2 ; freq [ 3 ] ++ ; } if ( s . charAt ( i ) == '6' ) { freq [ 5 ] ++ ; freq [ 3 ] ++ ; } if ( s . charAt ( i ) == '8' ) { freq [ 7 ] ++ ; freq [ 2 ] += 3 ; } if ( s . charAt ( i ) == '9' ) { freq [ 7 ] ++ ; freq [ 3 ] += 2 ; freq [ 2 ] ++ ; } } String t = " " ; if ( freq [ 1 ] == number_of_digits || freq [ 0 ] == number_of_digits || ( freq [ 0 ] + freq [ 1 ] ) == number_of_digits ) { return s ; } else { for ( int i = 9 ; i >= 2 ; i -- ) { int ctr = freq [ i ] ; while ( ( ctr -- ) > 0 ) { t += ( char ) ( i + 48 ) ; } } return t ; } } public static void main ( String [ ] args ) { String s = "1280" ; System . out . println ( getNumber ( s ) ) ; } }

def getNumber ( s ) : NEW_LINE INDENT number_of_digits = len ( s ) ; NEW_LINE freq = [ 0 ] * 10 ; NEW_LINE for i in range ( number_of_digits ) : NEW_LINE INDENT if ( s [ i ] == '1' or s [ i ] == '2' or s [ i ] == '3' or s [ i ] == '5' or s [ i ] == '7' ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - 48 ] += 1 ; NEW_LINE DEDENT if ( s [ i ] == '4' ) : NEW_LINE INDENT freq [ 2 ] += 2 ; NEW_LINE freq [ 3 ] += 1 ; NEW_LINE DEDENT if ( s [ i ] == '6' ) : NEW_LINE INDENT freq [ 5 ] += 1 ; NEW_LINE freq [ 3 ] += 1 ; NEW_LINE DEDENT if ( s [ i ] == '8' ) : NEW_LINE INDENT freq [ 7 ] += 1 ; NEW_LINE freq [ 2 ] += 3 ; NEW_LINE DEDENT if ( s [ i ] == '9' ) : NEW_LINE INDENT freq [ 7 ] += 1 ; NEW_LINE freq [ 3 ] += 2 ; NEW_LINE freq [ 2 ] += 1 ; NEW_LINE DEDENT DEDENT t = " " ; NEW_LINE if ( freq [ 1 ] == number_of_digits or freq [ 0 ] == number_of_digits or ( freq [ 0 ] + freq [ 1 ] ) == number_of_digits ) : NEW_LINE INDENT return s ; NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( 9 , 1 , - 1 ) : NEW_LINE INDENT ctr = freq [ i ] ; NEW_LINE while ( ctr > 0 ) : NEW_LINE INDENT t += chr ( i + 48 ) ; NEW_LINE ctr -= 1 ; NEW_LINE DEDENT DEDENT return t ; NEW_LINE DEDENT DEDENT s = "1280" ; NEW_LINE print ( getNumber ( s ) ) ; NEW_LINE


class GFG { static int getPassingCars ( int A [ ] , int n ) { int countOne = 0 , result = 0 ; while ( n >= 1 ) { if ( A [ n - 1 ] == 1 ) countOne ++ ; else result += countOne ; n -- ; } return result ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , 1 , 0 , 1 , 1 } ; int n = A . length ; System . out . println ( getPassingCars ( A , n ) ) ; } }

def getPassingCars ( A , n ) : NEW_LINE INDENT countOne = 0 ; result = 0 NEW_LINE while n >= 1 : NEW_LINE INDENT if A [ n - 1 ] == 1 : NEW_LINE INDENT countOne += 1 NEW_LINE DEDENT else : NEW_LINE INDENT result += countOne NEW_LINE DEDENT n -= 1 NEW_LINE DEDENT return result NEW_LINE DEDENT A = [ 0 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( A ) NEW_LINE print ( getPassingCars ( A , n ) ) NEW_LINE


class GFG { static final int R = 3 ; static final int C = 3 ; static final int MAX_K = 100 ; static int [ ] [ ] [ ] dp = new int [ R ] [ C ] [ MAX_K ] ; static int pathCountDPRecDP ( int [ ] [ ] mat , int m , int n , int k ) { if ( m < 0 || n < 0 ) return 0 ; if ( m == 0 && n == 0 ) return ( k == mat [ m ] [ n ] ? 1 : 0 ) ; if ( dp [ m ] [ n ] [ k ] != - 1 ) return dp [ m ] [ n ] [ k ] ; dp [ m ] [ n ] [ k ] = pathCountDPRecDP ( mat , m - 1 , n , k - mat [ m ] [ n ] ) + pathCountDPRecDP ( mat , m , n - 1 , k - mat [ m ] [ n ] ) ; return dp [ m ] [ n ] [ k ] ; } static int pathCountDP ( int [ ] [ ] mat , int k ) { for ( int i = 0 ; i < R ; i ++ ) for ( int j = 0 ; j < C ; j ++ ) for ( int l = 0 ; l < MAX_K ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; return pathCountDPRecDP ( mat , R - 1 , C - 1 , k ) ; } public static void main ( String [ ] args ) { int k = 12 ; int [ ] [ ] mat = new int [ ] [ ] { new int [ ] { 1 , 2 , 3 } , new int [ ] { 4 , 6 , 5 } , new int [ ] { 3 , 2 , 1 } } ; System . out . println ( pathCountDP ( mat , k ) ) ; } }

R = 3 NEW_LINE C = 3 NEW_LINE MAX_K = 1000 NEW_LINE def pathCountDPRecDP ( mat , m , n , k ) : NEW_LINE INDENT if m < 0 or n < 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif m == 0 and n == 0 : NEW_LINE INDENT return k == mat [ m ] [ n ] NEW_LINE DEDENT if ( dp [ m ] [ n ] [ k ] != - 1 ) : NEW_LINE INDENT return dp [ m ] [ n ] [ k ] NEW_LINE DEDENT dp [ m ] [ n ] [ k ] = ( pathCountDPRecDP ( mat , m - 1 , n , k - mat [ m ] [ n ] ) + pathCountDPRecDP ( mat , m , n - 1 , k - mat [ m ] [ n ] ) ) NEW_LINE return dp [ m ] [ n ] [ k ] NEW_LINE DEDENT def pathCountDP ( mat , k ) : NEW_LINE INDENT return pathCountDPRecDP ( mat , R - 1 , C - 1 , k ) NEW_LINE DEDENT k = 12 NEW_LINE dp = [ [ [ - 1 for col in range ( MAX_K ) ] for col in range ( C ) ] for row in range ( R ) ] NEW_LINE mat = [ [ 1 , 2 , 3 ] , [ 4 , 6 , 5 ] , [ 3 , 2 , 1 ] ] NEW_LINE print ( pathCountDP ( mat , k ) ) NEW_LINE


public class Main { static int findSum ( int n ) { int sum = 0 ; for ( int i = 1 ; 1 << i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { int num = ( 1 << i ) + ( 1 << j ) ; if ( num <= n ) sum += num ; } } return sum ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( findSum ( n ) ) ; } }

def findSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE i = 1 NEW_LINE while ( ( 1 << i ) < n ) : NEW_LINE INDENT for j in range ( 0 , i ) : NEW_LINE INDENT num = ( 1 << i ) + ( 1 << j ) NEW_LINE if ( num <= n ) : NEW_LINE INDENT sum += num NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 10 NEW_LINE print ( findSum ( n ) ) NEW_LINE


import java . io . * ; public class GFG { static void printDiagonalSums ( int [ ] [ ] mat , int n ) { int principal = 0 , secondary = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { if ( i == j ) principal += mat [ i ] [ j ] ; if ( ( i + j ) == ( n - 1 ) ) secondary += mat [ i ] [ j ] ; } } System . out . println ( " Principal ▁ Diagonal : " + principal ) ; System . out . println ( " Secondary ▁ Diagonal : " + secondary ) ; } static public void main ( String [ ] args ) { int [ ] [ ] a = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } } ; printDiagonalSums ( a , 4 ) ; } }

MAX = 100 NEW_LINE def printDiagonalSums ( mat , n ) : NEW_LINE INDENT principal = 0 NEW_LINE secondary = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT principal += mat [ i ] [ j ] NEW_LINE DEDENT if ( ( i + j ) == ( n - 1 ) ) : NEW_LINE INDENT secondary += mat [ i ] [ j ] NEW_LINE DEDENT DEDENT DEDENT print ( " Principal ▁ Diagonal : " , principal ) NEW_LINE print ( " Secondary ▁ Diagonal : " , secondary ) NEW_LINE DEDENT a = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] ] NEW_LINE printDiagonalSums ( a , 4 ) NEW_LINE


import java . io . * ; class GFG { static boolean isTriangular ( int num ) { if ( num < 0 ) return false ; int c = ( - 2 * num ) ; int b = 1 , a = 1 ; int d = ( b * b ) - ( 4 * a * c ) ; if ( d < 0 ) return false ; float root1 = ( - b + ( float ) Math . sqrt ( d ) ) / ( 2 * a ) ; float root2 = ( - b - ( float ) Math . sqrt ( d ) ) / ( 2 * a ) ; if ( root1 > 0 && Math . floor ( root1 ) == root1 ) return true ; if ( root2 > 0 && Math . floor ( root2 ) == root2 ) return true ; return false ; } public static void main ( String [ ] args ) { int num = 55 ; if ( isTriangular ( num ) ) System . out . println ( " The ▁ number ▁ is " + " ▁ a ▁ triangular ▁ number " ) ; else System . out . println ( " The ▁ number ▁ " + " is ▁ NOT ▁ a ▁ triangular ▁ number " ) ; } }

import math NEW_LINE def isTriangular ( num ) : NEW_LINE INDENT if ( num < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT c = ( - 2 * num ) NEW_LINE b , a = 1 , 1 NEW_LINE d = ( b * b ) - ( 4 * a * c ) NEW_LINE if ( d < 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT root1 = ( - b + math . sqrt ( d ) ) / ( 2 * a ) NEW_LINE root2 = ( - b - math . sqrt ( d ) ) / ( 2 * a ) NEW_LINE if ( root1 > 0 and math . floor ( root1 ) == root1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( root2 > 0 and math . floor ( root2 ) == root2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 55 NEW_LINE if ( isTriangular ( n ) ) : NEW_LINE INDENT print ( " The ▁ number ▁ is ▁ a ▁ triangular ▁ number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " The ▁ number ▁ is ▁ NOT ▁ a ▁ triangular ▁ number " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static Vector < Integer > find_idx ( int ar [ ] , int n ) { Vector < Integer > answer = new Vector < Integer > ( ) ; int sum = ar [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) { if ( sum == ar [ i ] ) { answer . add ( i + 1 ) ; } sum += ar [ i ] ; } return answer ; } public static void main ( String [ ] args ) { int ar [ ] = { 1 , 2 , 3 , 6 , 3 , 15 , 5 } ; int n = ar . length ; Vector < Integer > ans = find_idx ( ar , n ) ; if ( ans . size ( ) != 0 ) { for ( int i : ans ) { System . out . print ( i + " ▁ " ) ; } } else { System . out . println ( " - 1" ) ; } } }

def find_idx ( ar , n ) : NEW_LINE INDENT answer = [ ] ; NEW_LINE sum = ar [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( sum == ar [ i ] ) : NEW_LINE INDENT answer . append ( i + 1 ) ; NEW_LINE DEDENT sum += ar [ i ] ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT ar = [ 1 , 2 , 3 , 6 , 3 , 15 , 5 ] ; NEW_LINE n = len ( ar ) ; NEW_LINE ans = find_idx ( ar , n ) ; NEW_LINE if ( len ( ans ) != 0 ) : NEW_LINE INDENT for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int findEvenPair ( int A [ ] , int N ) { int count = 0 ; for ( int i = 0 ; i < N ; i ++ ) if ( ( ! ( ( A [ i ] & 1 ) > 0 ) ) ) count ++ ; return count * ( count - 1 ) / 2 ; } public static void main ( String [ ] args ) { int A [ ] = { 5 , 6 , 2 , 8 } ; int N = A . length ; System . out . println ( findEvenPair ( A , N ) ) ; } }

def findEvenPair ( A , N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( not ( A [ i ] & 1 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count * ( count - 1 ) // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 5 , 6 , 2 , 8 ] NEW_LINE N = len ( A ) NEW_LINE print ( findEvenPair ( A , N ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static void swap ( int [ ] a , int i , int j ) { int temp = a [ i ] ; a [ i ] = a [ j ] ; a [ j ] = temp ; } static void printArray ( int [ ] a , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 } ; int n = arr . length ; printArray ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= 0 && i % 2 == 1 ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] < 0 && j % 2 == 0 ) { swap ( arr , i , j ) ; break ; } } } else if ( arr [ i ] < 0 && i % 2 == 0 ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ j ] >= 0 && j % 2 == 1 ) { swap ( arr , i , j ) ; break ; } } } } printArray ( arr , n ) ; } }

def printArray ( a , n ) : NEW_LINE INDENT for i in a : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT arr = [ 1 , - 3 , 5 , 6 , - 3 , 6 , 7 , - 4 , 9 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE printArray ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= 0 and i % 2 == 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] < 0 and j % 2 == 0 ) : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT elif ( arr [ i ] < 0 and i % 2 == 0 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] >= 0 and j % 2 == 1 ) : NEW_LINE INDENT arr [ i ] , arr [ j ] = arr [ j ] , arr [ i ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT DEDENT printArray ( arr , n ) ; NEW_LINE


class solution { static final int max = 4 ; static int countWays ( int index , int cnt , int dp [ ] [ ] , int n , int m , int k ) { if ( index == n ) { if ( cnt == k ) return 1 ; else return 0 ; } if ( dp [ index ] [ cnt ] != - 1 ) return dp [ index ] [ cnt ] ; int ans = 0 ; ans += countWays ( index + 1 , cnt , dp , n , m , k ) ; ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k ) ; return dp [ index ] [ cnt ] = ans ; } public static void main ( String args [ ] ) { int n = 3 , m = 3 , k = 2 ; int dp [ ] [ ] = new int [ n + 1 ] [ max ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) for ( int j = 0 ; j < max ; j ++ ) dp [ i ] [ j ] = - 1 ; System . out . println ( m * countWays ( 1 , 0 , dp , n , m , k ) ) ; } }

max = 4 NEW_LINE def countWays ( index , cnt , dp , n , m , k ) : NEW_LINE INDENT if ( index == n ) : NEW_LINE INDENT if ( cnt == k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT if ( dp [ index ] [ cnt ] != - 1 ) : NEW_LINE INDENT return dp [ index ] [ cnt ] NEW_LINE DEDENT ans = 0 NEW_LINE ans += countWays ( index + 1 , cnt , dp , n , m , k ) NEW_LINE ans += ( m - 1 ) * countWays ( index + 1 , cnt + 1 , dp , n , m , k ) NEW_LINE dp [ index ] [ cnt ] = ans NEW_LINE return dp [ index ] [ cnt ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE m = 3 NEW_LINE k = 2 NEW_LINE dp = [ [ - 1 for x in range ( n + 1 ) ] for y in range ( max ) ] NEW_LINE print ( m * countWays ( 1 , 0 , dp , n , m , k ) ) NEW_LINE DEDENT


public class GFG { static boolean isVowel ( char ch ) { ch = Character . toUpperCase ( ch ) ; return ( ch == ' A ' || ch == ' E ' || ch == ' I ' || ch == ' O ' || ch == ' U ' ) ; } static int countVowels ( String str ) { int count = 0 ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) if ( isVowel ( str . charAt ( i ) ) ) ++ count ; return count ; } public static void main ( String args [ ] ) { String str = " abc ▁ de " ; System . out . println ( countVowels ( str ) ) ; } }

def isVowel ( ch ) : NEW_LINE INDENT return ch . upper ( ) in [ ' A ' , ' E ' , ' I ' , ' O ' , ' U ' ] NEW_LINE DEDENT def countVowels ( str ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if isVowel ( str [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT str = ' abc ▁ de ' NEW_LINE print ( countVowels ( str ) ) NEW_LINE


class GFG { static void convertToBinary ( int num , int a [ ] , int n ) { int pointer = n - 1 ; while ( num > 0 ) { a [ pointer ] = num % 2 ; num = num / 2 ; pointer -- ; } } static void checkforsum ( int a [ ] , int n ) { int sum1 = 0 ; int sum2 = 0 ; int mid = n / 2 ; for ( int i = 0 ; i < mid ; i ++ ) sum1 = sum1 + a [ i ] ; for ( int j = mid ; j < n ; j ++ ) sum2 = sum2 + a [ j ] ; if ( sum1 == sum2 ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] ) ; System . out . println ( ) ; } } static void print_seq ( int m ) { int n = ( 2 * m ) ; int a [ ] = new int [ n ] ; for ( int j = 0 ; j < n ; j ++ ) { a [ j ] = 0 ; } for ( int i = 0 ; i < ( int ) Math . pow ( 2 , n ) ; i ++ ) { convertToBinary ( i , a , n ) ; checkforsum ( a , n ) ; } } public static void main ( String [ ] args ) { int m = 2 ; print_seq ( m ) ; } }

def convertToBinary ( num , a , n ) : NEW_LINE INDENT pointer = n - 1 NEW_LINE while ( num > 0 ) : NEW_LINE INDENT a [ pointer ] = num % 2 NEW_LINE num = num // 2 NEW_LINE pointer -= 1 NEW_LINE DEDENT DEDENT def checkforsum ( a , n ) : NEW_LINE INDENT sum1 = 0 NEW_LINE sum2 = 0 NEW_LINE mid = n // 2 NEW_LINE for i in range ( mid ) : NEW_LINE INDENT sum1 = sum1 + a [ i ] NEW_LINE DEDENT for j in range ( mid , n ) : NEW_LINE INDENT sum2 = sum2 + a [ j ] NEW_LINE DEDENT if ( sum1 == sum2 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] , end = " " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT def print_seq ( m ) : NEW_LINE INDENT n = ( 2 * m ) NEW_LINE a = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( pow ( 2 , n ) ) : NEW_LINE INDENT convertToBinary ( i , a , n ) NEW_LINE checkforsum ( a , n ) NEW_LINE DEDENT DEDENT m = 2 NEW_LINE print_seq ( m ) NEW_LINE


class GFG { static float ellipsearea ( float r ) { if ( r < 0 ) return - 1 ; float a = ( float ) ( ( 3.14f * r * r ) / 4 ) ; return a ; } public static void main ( String [ ] args ) { float r = 5 ; System . out . println ( ellipsearea ( r ) ) ; } }

def ellipsearea ( r ) : NEW_LINE INDENT if ( r < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT a = ( 3.14 * r * r ) / 4 ; NEW_LINE return a ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT r = 5 ; NEW_LINE print ( ellipsearea ( r ) ) ; NEW_LINE DEDENT


public class DivisibleParts { static void printTwoDivisibleParts ( String num , int f , int s ) { int N = num . length ( ) ; int [ ] prefixReminder = new int [ N + 1 ] ; int [ ] suffixReminder = new int [ N + 1 ] ; suffixReminder [ 0 ] = 0 ; for ( int i = 1 ; i < N ; i ++ ) suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( num . charAt ( i - 1 ) - '0' ) ) % f ; prefixReminder [ N ] = 0 ; int base = 1 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( num . charAt ( i ) - '0' ) * base ) % s ; base = ( base * 10 ) % s ; } for ( int i = 0 ; i < N ; i ++ ) { if ( prefixReminder [ i ] == 0 && suffixReminder [ i ] == 0 && num . charAt ( i ) != '0' ) { System . out . println ( num . substring ( 0 , i ) + " ▁ " + num . substring ( i ) ) ; return ; } } System . out . println ( " Not ▁ Possible " ) ; } public static void main ( String [ ] args ) { String num = "246904096" ; int f = 12345 ; int s = 1024 ; printTwoDivisibleParts ( num , f , s ) ; } }

def printTwoDivisibleParts ( num , f , s ) : NEW_LINE INDENT N = len ( num ) ; NEW_LINE prefixReminder = [ 0 ] * ( N + 1 ) ; NEW_LINE suffixReminder = [ 0 ] * ( N + 1 ) ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT suffixReminder [ i ] = ( suffixReminder [ i - 1 ] * 10 + ( ord ( num [ i - 1 ] ) - 48 ) ) % f ; NEW_LINE DEDENT base = 1 ; NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT prefixReminder [ i ] = ( prefixReminder [ i + 1 ] + ( ord ( num [ i ] ) - 48 ) * base ) % s ; NEW_LINE base = ( base * 10 ) % s ; NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( prefixReminder [ i ] == 0 and suffixReminder [ i ] == 0 and num [ i ] != '0' ) : NEW_LINE INDENT print ( num [ 0 : i ] , num [ i : N ] ) ; NEW_LINE return 0 ; NEW_LINE DEDENT DEDENT print ( " Not ▁ Possible " ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT num = "246904096" ; NEW_LINE f = 12345 ; NEW_LINE s = 1024 ; NEW_LINE printTwoDivisibleParts ( num , f , s ) ; NEW_LINE DEDENT


import java . io . * ; public class GFG { static int [ ] fact = new int [ ] { 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 } ; static boolean peterson ( int n ) { int num = n ; int sum = 0 ; while ( n > 0 ) { int digit = n % 10 ; sum += fact [ digit ] ; n = n / 10 ; } return ( sum == num ) ; } static public void main ( String [ ] args ) { int n = 145 ; if ( peterson ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

fact = [ 1 , 1 , 2 , 6 , 24 , 120 , 720 , 5040 , 40320 , 362880 ] NEW_LINE def peterson ( n ) : NEW_LINE INDENT num = n NEW_LINE sum = 0 NEW_LINE while n > 0 : NEW_LINE INDENT digit = int ( n % 10 ) NEW_LINE sum += fact [ digit ] NEW_LINE n = int ( n / 10 ) NEW_LINE DEDENT return ( sum == num ) NEW_LINE DEDENT n = 145 NEW_LINE print ( " Yes " if peterson ( n ) else " No " ) NEW_LINE


import java . util . * ; class GFG { static int MAX = 50002 ; static ArrayList < Integer > primes = new ArrayList < Integer > ( ) ; public static void sieve ( ) { boolean isPrime [ ] = new boolean [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) isPrime [ i ] = true ; for ( int p = 2 ; p * p < MAX ; p ++ ) { if ( isPrime [ p ] == true ) { for ( int i = p * 2 ; i < MAX ; i += p ) isPrime [ i ] = false ; } } for ( int p = 2 ; p < MAX ; p ++ ) if ( isPrime [ p ] == true ) primes . add ( p ) ; } public static int power ( int x , int y ) { int count = 0 ; int z = y ; while ( x >= z ) { count += ( x / z ) ; z *= y ; } return count ; } public static int modMult ( int a , int b , int mod ) { int res = 0 ; a = a % mod ; while ( b > 0 ) { if ( b % 2 == 1 ) res = ( res + a ) % mod ; a = ( a * 2 ) % mod ; b /= 2 ; } return res % mod ; } public static int countWays ( int n , int m ) { int ans = 1 ; for ( int i = 1 ; i < primes . size ( ) ; i ++ ) { int powers = power ( n , primes . get ( i ) ) ; if ( powers == 0 ) break ; ans = modMult ( ans , powers + 1 , m ) % m ; } if ( ( ( ans - 1 ) % m ) < 0 ) return ( ans - 1 + m ) % m ; else return ( ans - 1 ) % m ; } public static void main ( String [ ] args ) { sieve ( ) ; int n = 4 , m = 7 ; System . out . println ( countWays ( n , m ) ) ; } }

MAX = 50002 ; NEW_LINE primes = [ ] NEW_LINE def sieve ( ) : NEW_LINE INDENT isPrime = [ True ] * ( MAX ) NEW_LINE p = 2 NEW_LINE while p * p < MAX : NEW_LINE INDENT if ( isPrime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , MAX , p ) : NEW_LINE INDENT isPrime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT for p in range ( 2 , MAX ) : NEW_LINE INDENT if ( isPrime [ p ] ) : NEW_LINE INDENT primes . append ( p ) NEW_LINE DEDENT DEDENT DEDENT def power ( x , y ) : NEW_LINE INDENT count = 0 NEW_LINE z = y NEW_LINE while ( x >= z ) : NEW_LINE INDENT count += ( x // z ) NEW_LINE z *= y NEW_LINE DEDENT return count NEW_LINE DEDENT def modMult ( a , b , mod ) : NEW_LINE INDENT res = 0 NEW_LINE a = a % mod NEW_LINE while ( b > 0 ) : NEW_LINE INDENT if ( b % 2 == 1 ) : NEW_LINE INDENT res = ( res + a ) % mod NEW_LINE DEDENT a = ( a * 2 ) % mod NEW_LINE b //= 2 NEW_LINE DEDENT return res % mod NEW_LINE DEDENT def countWays ( n , m ) : NEW_LINE INDENT ans = 1 NEW_LINE for i in range ( 1 , len ( primes ) ) : NEW_LINE INDENT powers = power ( n , primes [ i ] ) NEW_LINE if ( powers == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT ans = modMult ( ans , powers + 1 , m ) % m NEW_LINE DEDENT if ( ( ( ans - 1 ) % m ) < 0 ) : NEW_LINE INDENT return ( ans - 1 + m ) % m NEW_LINE DEDENT else : NEW_LINE INDENT return ( ans - 1 ) % m NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sieve ( ) NEW_LINE n = 4 NEW_LINE m = 7 NEW_LINE print ( countWays ( n , m ) ) NEW_LINE DEDENT


import java . util . Vector ; class GFG { static Vector < Integer > deleteElement ( int A [ ] , int L , int R , int N ) { Vector < Integer > B = new Vector < > ( ) ; for ( int i = 0 ; i < N ; i ++ ) { if ( i <= L || i >= R ) { B . add ( A [ i ] ) ; } } return B ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 5 , 3 , 4 , 9 , 3 , 1 , 6 , 3 , 11 , 12 , 3 } ; int L = 2 , R = 7 ; int n = A . length ; Vector < Integer > res = deleteElement ( A , L , R , n ) ; for ( Integer x : res ) { System . out . print ( x + " ▁ " ) ; } } }

def deleteElement ( A , L , R , N ) : NEW_LINE INDENT B = [ ] NEW_LINE for i in range ( 0 , N , 1 ) : NEW_LINE INDENT if ( i <= L or i >= R ) : NEW_LINE INDENT B . append ( A [ i ] ) NEW_LINE DEDENT DEDENT return B NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = [ 3 , 5 , 3 , 4 , 9 , 3 , 1 , 6 , 3 , 11 , 12 , 3 ] NEW_LINE L = 2 NEW_LINE R = 7 NEW_LINE n = len ( A ) NEW_LINE res = deleteElement ( A , L , R , n ) NEW_LINE for i in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT


class GFG { static int swapNibbles ( int x ) { return ( ( x & 0x0F ) << 4 | ( x & 0xF0 ) >> 4 ) ; } public static void main ( String arg [ ] ) { int x = 100 ; System . out . print ( swapNibbles ( x ) ) ; } }

def swapNibbles ( x ) : NEW_LINE INDENT return ( ( x & 0x0F ) << 4 | ( x & 0xF0 ) >> 4 ) NEW_LINE DEDENT x = 100 NEW_LINE print ( swapNibbles ( x ) ) NEW_LINE


import java . util . HashMap ; class GFG { public static int minOperations ( int [ ] a , int n , int K ) { HashMap < Integer , Boolean > map = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( a [ i ] ) ) return 0 ; map . put ( a [ i ] , true ) ; } int [ ] b = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) b [ i ] = a [ i ] | K ; map . clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) map . put ( b [ i ] , true ) ; } for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( a [ i ] ) ) return 1 ; } map . clear ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( map . containsKey ( b [ i ] ) ) return 2 ; map . put ( b [ i ] , true ) ; } return - 1 ; } public static void main ( String [ ] args ) { int K = 3 ; int [ ] a = { 1 , 9 , 4 , 3 } ; int n = a . length ; System . out . println ( minOperations ( a , n , K ) ) ; } }

def minOperations ( a , n , K ) : NEW_LINE INDENT map = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( map [ a [ i ] ] ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT map [ a [ i ] ] = True ; NEW_LINE DEDENT b = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT b [ i ] = a [ i ] | K ; NEW_LINE DEDENT map . clear ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] != b [ i ] ) : NEW_LINE INDENT map [ b [ i ] ] = True ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT if a [ i ] not in map : NEW_LINE INDENT pass NEW_LINE DEDENT elif ( map [ a [ i ] ] ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT DEDENT map . clear ( ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( map [ b [ i ] ] ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT map [ b [ i ] ] = true ; NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT K = 3 ; NEW_LINE a = [ 1 , 9 , 4 , 3 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( minOperations ( a , n , K ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static long kBitDifferencePairs ( int arr [ ] , int n , int k ) { int MAX = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; long [ ] count = new long [ MAX + 1 ] ; Arrays . fill ( count , 0 ) ; for ( int i = 0 ; i < n ; ++ i ) ++ count [ arr [ i ] ] ; long ans = 0 ; if ( k == 0 ) { for ( int i = 0 ; i <= MAX ; ++ i ) ans += ( count [ i ] * ( count [ i ] - 1 ) ) / 2 ; return ans ; } for ( int i = 0 ; i <= MAX ; ++ i ) { if ( count [ i ] == 0 ) continue ; for ( int j = i + 1 ; j <= MAX ; ++ j ) { if ( Integer . bitCount ( i ^ j ) == k ) ans += count [ i ] * count [ j ] ; } } return ans ; } public static void main ( String [ ] args ) { int k = 2 ; int arr [ ] = { 2 , 4 , 1 , 3 , 1 } ; int n = arr . length ; System . out . println ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ = ▁ " + kBitDifferencePairs ( arr , n , k ) ) ; k = 3 ; System . out . println ( " Total ▁ pairs ▁ for ▁ k ▁ = ▁ " + k + " ▁ are ▁ = ▁ " + kBitDifferencePairs ( arr , n , k ) ) ; } }

def kBitDifferencePairs ( arr , n , k ) : NEW_LINE INDENT MAX = max ( arr ) NEW_LINE count = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT ans = 0 NEW_LINE if ( k == 0 ) : NEW_LINE INDENT for i in range ( MAX + 1 ) : NEW_LINE INDENT ans += ( count [ i ] * ( count [ i ] - 1 ) ) // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT for i in range ( MAX + 1 ) : NEW_LINE INDENT if ( count [ i ] == 0 ) : NEW_LINE INDENT continue NEW_LINE DEDENT for j in range ( i + 1 , MAX + 1 ) : NEW_LINE INDENT if ( bin ( i ^ j ) . count ( '1' ) == k ) : NEW_LINE INDENT ans += count [ i ] * count [ j ] NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT k = 2 NEW_LINE arr = [ 2 , 4 , 1 , 3 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Total ▁ pairs ▁ for ▁ k ▁ = " , k , " are " , kBitDifferencePairs ( arr , n , k ) ) NEW_LINE k = 3 NEW_LINE print ( " Total ▁ pairs ▁ for ▁ k ▁ = " , k , " are " , kBitDifferencePairs ( arr , n , k ) ) NEW_LINE


import java . io . * ; class GFG { static int makeOdd ( int n ) { if ( n % 2 != 0 ) return 1 ; int i ; for ( i = 2 ; i <= n ; i ++ ) if ( ( n % i == 0 ) && ( ( n / i ) % 2 == 1 ) ) break ; return i ; } public static void main ( String [ ] args ) { int n = 36 ; int res = makeOdd ( n ) ; System . out . println ( res ) ; } }

def makeOdd ( n ) : NEW_LINE INDENT if n % 2 != 0 : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( n % i == 0 and ( int ) ( n / i ) % 2 == 1 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT DEDENT n = 36 ; NEW_LINE print ( makeOdd ( n ) ) ; NEW_LINE


class GFG { static int getPairs ( int a [ ] ) { int count = 0 ; for ( int i = 0 ; i < a . length ; i ++ ) { for ( int j = 0 ; j < a . length ; j ++ ) { if ( a [ i ] < a [ j ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 4 , 3 , 1 } ; System . out . println ( getPairs ( a ) ) ; } }

def getPairs ( a ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( len ( a ) ) : NEW_LINE INDENT for j in range ( len ( a ) ) : NEW_LINE INDENT if ( a [ i ] < a [ j ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 4 , 3 , 1 ] NEW_LINE print ( getPairs ( a ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void findBalance ( int x , float bal ) { if ( x % 10 == 0 && ( ( float ) x + 1.50 ) <= bal ) { System . out . printf ( " % .2f \n " , bal - x - 1.50 ) ; } else { System . out . printf ( " % .2f \n " , bal ) ; } } public static void main ( String [ ] args ) { int x = 50 ; float bal = ( float ) 100.50 ; findBalance ( x , bal ) ; } }

def findBalance ( x , bal ) : NEW_LINE INDENT if ( x % 10 == 0 and ( x + 1.50 ) <= bal ) : NEW_LINE INDENT print ( round ( bal - x - 1.50 , 2 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( round ( bal , 2 ) ) NEW_LINE DEDENT DEDENT x = 50 NEW_LINE bal = 100.50 NEW_LINE findBalance ( x , bal ) NEW_LINE


class Geeks { static int countways ( int n ) { if ( n == 1 ) return 0 ; else if ( n % 2 == 0 ) return 1 + countways ( n / 2 ) ; else return 1 + Math . min ( countways ( n - 1 ) , countways ( n + 1 ) ) ; } public static void main ( String args [ ] ) { int n = 15 ; System . out . println ( countways ( n ) ) ; } }

def countways ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT return 1 + countways ( n / 2 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return 1 + min ( countways ( n - 1 ) , countways ( n + 1 ) ) ; NEW_LINE DEDENT DEDENT n = 15 ; NEW_LINE print ( countways ( n ) ) ; NEW_LINE


class GFG { static class pair { double F , S ; public pair ( double F , double S ) { this . F = F ; this . S = S ; } public pair ( ) { } } static double minDistance ( pair A , pair B , pair E ) { pair AB = new pair ( ) ; AB . F = B . F - A . F ; AB . S = B . S - A . S ; pair BE = new pair ( ) ; BE . F = E . F - B . F ; BE . S = E . S - B . S ; pair AE = new pair ( ) ; AE . F = E . F - A . F ; AE . S = E . S - A . S ; double AB_BE , AB_AE ; AB_BE = ( AB . F * BE . F + AB . S * BE . S ) ; AB_AE = ( AB . F * AE . F + AB . S * AE . S ) ; double reqAns = 0 ; if ( AB_BE > 0 ) { double y = E . S - B . S ; double x = E . F - B . F ; reqAns = Math . sqrt ( x * x + y * y ) ; } else if ( AB_AE < 0 ) { double y = E . S - A . S ; double x = E . F - A . F ; reqAns = Math . sqrt ( x * x + y * y ) ; } else { double x1 = AB . F ; double y1 = AB . S ; double x2 = AE . F ; double y2 = AE . S ; double mod = Math . sqrt ( x1 * x1 + y1 * y1 ) ; reqAns = Math . abs ( x1 * y2 - y1 * x2 ) / mod ; } return reqAns ; } public static void main ( String [ ] args ) { pair A = new pair ( 0 , 0 ) ; pair B = new pair ( 2 , 0 ) ; pair E = new pair ( 1 , 1 ) ; System . out . print ( ( int ) minDistance ( A , B , E ) ) ; } }

from math import sqrt NEW_LINE def minDistance ( A , B , E ) : NEW_LINE INDENT AB = [ None , None ] ; NEW_LINE AB [ 0 ] = B [ 0 ] - A [ 0 ] ; NEW_LINE AB [ 1 ] = B [ 1 ] - A [ 1 ] ; NEW_LINE BE = [ None , None ] ; NEW_LINE BE [ 0 ] = E [ 0 ] - B [ 0 ] ; NEW_LINE BE [ 1 ] = E [ 1 ] - B [ 1 ] ; NEW_LINE AE = [ None , None ] ; NEW_LINE AE [ 0 ] = E [ 0 ] - A [ 0 ] ; NEW_LINE AE [ 1 ] = E [ 1 ] - A [ 1 ] ; NEW_LINE AB_BE = AB [ 0 ] * BE [ 0 ] + AB [ 1 ] * BE [ 1 ] ; NEW_LINE AB_AE = AB [ 0 ] * AE [ 0 ] + AB [ 1 ] * AE [ 1 ] ; NEW_LINE reqAns = 0 ; NEW_LINE if ( AB_BE > 0 ) : NEW_LINE INDENT y = E [ 1 ] - B [ 1 ] ; NEW_LINE x = E [ 0 ] - B [ 0 ] ; NEW_LINE reqAns = sqrt ( x * x + y * y ) ; NEW_LINE DEDENT elif ( AB_AE < 0 ) : NEW_LINE INDENT y = E [ 1 ] - A [ 1 ] ; NEW_LINE x = E [ 0 ] - A [ 0 ] ; NEW_LINE reqAns = sqrt ( x * x + y * y ) ; NEW_LINE DEDENT else : NEW_LINE INDENT x1 = AB [ 0 ] ; NEW_LINE y1 = AB [ 1 ] ; NEW_LINE x2 = AE [ 0 ] ; NEW_LINE y2 = AE [ 1 ] ; NEW_LINE mod = sqrt ( x1 * x1 + y1 * y1 ) ; NEW_LINE reqAns = abs ( x1 * y2 - y1 * x2 ) / mod ; NEW_LINE DEDENT return reqAns ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 0 , 0 ] ; NEW_LINE B = [ 2 , 0 ] ; NEW_LINE E = [ 1 , 1 ] ; NEW_LINE print ( minDistance ( A , B , E ) ) ; NEW_LINE DEDENT


class GFG { static void findNthNum ( int N ) { int bit_L = 1 , last_num = 0 ; while ( bit_L * ( bit_L + 1 ) / 2 < N ) { last_num = last_num + bit_L ; bit_L ++ ; } int bit_R = N - last_num - 1 ; System . out . print ( ( 1 << bit_L ) + ( 1 << bit_R ) + " \n " ) ; } public static void main ( String [ ] args ) { int N = 13 ; findNthNum ( N ) ; } }

def findNthNum ( N ) : NEW_LINE INDENT bit_L = 1 ; NEW_LINE last_num = 0 ; NEW_LINE while ( bit_L * ( bit_L + 1 ) / 2 < N ) : NEW_LINE INDENT last_num = last_num + bit_L ; NEW_LINE bit_L += 1 ; NEW_LINE DEDENT bit_R = N - last_num - 1 ; NEW_LINE print ( ( 1 << bit_L ) + ( 1 << bit_R ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 13 ; NEW_LINE findNthNum ( N ) ; NEW_LINE DEDENT


class GFG { static int solve ( int [ ] arr , int n ) { int k = arr . length ; int mp [ ] = new int [ n + 2 ] ; for ( int i = 0 ; i <= n ; i ++ ) { mp [ i ] = 0 ; } for ( int i = 0 ; i < k ; i ++ ) { mp [ arr [ i ] ] = 1 ; } int leftSegment = arr [ 0 ] - 1 ; for ( int i = 1 ; i < k ; i ++ ) { leftSegment = Math . max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) ; } int rightSegment = n - arr [ k - 1 ] ; int maxSegment = Math . max ( leftSegment , rightSegment ) ; int tim ; if ( ( maxSegment & 1 ) == 1 ) tim = ( maxSegment / 2 ) + 1 ; else tim = maxSegment / 2 ; return tim ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 1 , 4 } ; System . out . println ( solve ( arr , N ) ) ; } }

def solve ( arr , n ) : NEW_LINE INDENT k = len ( arr ) NEW_LINE mp = [ False for i in range ( n + 2 ) ] NEW_LINE for i in range ( k ) : NEW_LINE INDENT mp [ arr [ i ] ] = True NEW_LINE DEDENT leftSegment = arr [ 0 ] - 1 NEW_LINE for i in range ( 1 , k ) : NEW_LINE INDENT leftSegment = max ( leftSegment , arr [ i ] - arr [ i - 1 ] - 1 ) NEW_LINE DEDENT rightSegment = n - arr [ k - 1 ] NEW_LINE maxSegment = max ( leftSegment , rightSegment ) ; NEW_LINE tim = 0 NEW_LINE if ( maxSegment & 1 ) : NEW_LINE INDENT tim = ( maxSegment // 2 ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT tim = maxSegment // 2 NEW_LINE DEDENT return tim NEW_LINE DEDENT N = 5 NEW_LINE arr = [ 1 , 4 ] NEW_LINE print ( solve ( arr , N ) ) NEW_LINE


import java . io . * ; class GFG { static int findNthTerm ( int x , int y , int n ) { int [ ] f = new int [ 6 ] ; f [ 0 ] = x ; f [ 1 ] = y ; for ( int i = 2 ; i <= 5 ; i ++ ) f [ i ] = f [ i - 1 ] - f [ i - 2 ] ; return f [ n % 6 ] ; } public static void main ( String args [ ] ) { int x = 2 , y = 3 , n = 3 ; System . out . println ( findNthTerm ( x , y , n ) ) ; } }

def findNthTerm ( x , y , n ) : NEW_LINE INDENT f = [ 0 ] * 6 NEW_LINE f [ 0 ] = x NEW_LINE f [ 1 ] = y NEW_LINE for i in range ( 2 , 6 ) : NEW_LINE INDENT f [ i ] = f [ i - 1 ] - f [ i - 2 ] NEW_LINE DEDENT return f [ n % 6 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x , y , n = 2 , 3 , 3 NEW_LINE print ( findNthTerm ( x , y , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static class Node { int data ; Node next ; } static boolean isCircular ( Node head ) { if ( head == null ) return true ; Node node = head . next ; while ( node != null && node != head ) node = node . next ; return ( node == head ) ; } static Node newNode ( int data ) { Node temp = new Node ( ) ; temp . data = data ; temp . next = null ; return temp ; } public static void main ( String args [ ] ) { Node head = newNode ( 1 ) ; head . next = newNode ( 2 ) ; head . next . next = newNode ( 3 ) ; head . next . next . next = newNode ( 4 ) ; System . out . print ( isCircular ( head ) ? " Yes \n " : " No \n " ) ; head . next . next . next . next = head ; System . out . print ( isCircular ( head ) ? " Yes \n " : " No \n " ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT class LinkedList : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT DEDENT def Circular ( head ) : NEW_LINE INDENT if head == None : NEW_LINE INDENT return True NEW_LINE DEDENT node = head . next NEW_LINE i = 0 NEW_LINE while ( ( node is not None ) and ( node is not head ) ) : NEW_LINE INDENT i = i + 1 NEW_LINE node = node . next NEW_LINE DEDENT return ( node == head ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT llist = LinkedList ( ) NEW_LINE llist . head = Node ( 1 ) NEW_LINE second = Node ( 2 ) NEW_LINE third = Node ( 3 ) NEW_LINE fourth = Node ( 4 ) NEW_LINE llist . head . next = second ; NEW_LINE second . next = third ; NEW_LINE third . next = fourth NEW_LINE if ( Circular ( llist . head ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT fourth . next = llist . head NEW_LINE if ( Circular ( llist . head ) ) : NEW_LINE INDENT print ( ' Yes ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No ' ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static int countDigit ( long n ) { return ( int ) Math . floor ( Math . log10 ( n ) + 1 ) ; } public static void main ( String [ ] args ) { long n = 345289467 ; System . out . print ( " Number ▁ of ▁ digits ▁ : ▁ " + countDigit ( n ) ) ; } }

import math NEW_LINE def countDigit ( n ) : NEW_LINE INDENT return math . floor ( math . log ( n , 10 ) + 1 ) NEW_LINE DEDENT n = 345289467 NEW_LINE print ( " Number ▁ of ▁ digits ▁ : ▁ % ▁ d " % ( countDigit ( n ) ) ) NEW_LINE


class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void findgcd ( int n , int x , int y ) { int g = gcd ( x , y ) ; for ( int i = 0 ; i < g ; i ++ ) System . out . print ( n ) ; } public static void main ( String [ ] args ) { int n = 123 , x = 5 , y = 2 ; findgcd ( n , x , y ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT return gcd ( b % a , a ) NEW_LINE DEDENT def findgcd ( n , x , y ) : NEW_LINE INDENT g = gcd ( x , y ) NEW_LINE for i in range ( g ) : NEW_LINE INDENT print ( n ) NEW_LINE DEDENT DEDENT n = 123 NEW_LINE x = 5 NEW_LINE y = 2 NEW_LINE findgcd ( n , x , y ) NEW_LINE


import java . io . * ; import java . math . * ; class GFG { static int sumofoddFactors ( int n ) { int res = 1 ; while ( n % 2 == 0 ) n = n / 2 ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i ++ ) { int count = 0 , curr_sum = 1 ; int curr_term = 1 ; while ( n % i == 0 ) { count ++ ; n = n / i ; curr_term *= i ; curr_sum += curr_term ; } res *= curr_sum ; } if ( n >= 2 ) res *= ( 1 + n ) ; return res ; } public static void main ( String args [ ] ) throws IOException { int n = 30 ; System . out . println ( sumofoddFactors ( n ) ) ; } }

import math NEW_LINE def sumofoddFactors ( n ) : NEW_LINE INDENT res = 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT n = n // 2 NEW_LINE DEDENT for i in range ( 3 , int ( math . sqrt ( n ) + 1 ) ) : NEW_LINE INDENT count = 0 NEW_LINE curr_sum = 1 NEW_LINE curr_term = 1 NEW_LINE while n % i == 0 : NEW_LINE INDENT count += 1 NEW_LINE n = n // i NEW_LINE curr_term *= i NEW_LINE curr_sum += curr_term NEW_LINE DEDENT res *= curr_sum NEW_LINE DEDENT if n >= 2 : NEW_LINE INDENT res *= ( 1 + n ) NEW_LINE DEDENT return res NEW_LINE DEDENT n = 30 NEW_LINE print ( sumofoddFactors ( n ) ) NEW_LINE


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

def check ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE oddDigSum = 0 NEW_LINE evenDigSum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT oddDigSum = oddDigSum + ( ( int ) ( st [ i ] ) ) NEW_LINE DEDENT else : NEW_LINE INDENT evenDigSum = evenDigSum + ( ( int ) ( st [ i ] ) ) NEW_LINE DEDENT DEDENT return ( ( oddDigSum - evenDigSum ) % 11 == 0 ) NEW_LINE DEDENT st = "76945" NEW_LINE if ( check ( st ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int numberOfMinutes ( int S , int S1 ) { int Min = 0 ; Min = ( int ) ( ( ( S - S1 ) / Math . floor ( S ) ) * 60 ) ; return Min ; } public static void main ( String [ ] args ) { int S = 30 , S1 = 10 ; System . out . println ( numberOfMinutes ( S , S1 ) + " ▁ min " ) ; } }

import math NEW_LINE def numberOfMinutes ( S , S1 ) : NEW_LINE INDENT Min = 0 ; NEW_LINE Min = ( ( S - S1 ) / math . floor ( S ) ) * 60 ; NEW_LINE return int ( Min ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S , S1 = 30 , 10 ; NEW_LINE print ( numberOfMinutes ( S , S1 ) , " min " ) ; NEW_LINE DEDENT


class Test { static float findArea ( float a , float b , float c ) { if ( a < 0 || b < 0 || c < 0 || ( a + b <= c ) || a + c <= b || b + c <= a ) { System . out . println ( " Not ▁ a ▁ valid ▁ triangle " ) ; System . exit ( 0 ) ; } float s = ( a + b + c ) / 2 ; return ( float ) Math . sqrt ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ; } public static void main ( String [ ] args ) { float a = 3.0f ; float b = 4.0f ; float c = 5.0f ; System . out . println ( " Area ▁ is ▁ " + findArea ( a , b , c ) ) ; } }

def findArea ( a , b , c ) : NEW_LINE INDENT if ( a < 0 or b < 0 or c < 0 or ( a + b <= c ) or ( a + c <= b ) or ( b + c <= a ) ) : NEW_LINE INDENT print ( ' Not ▁ a ▁ valid ▁ trianglen ' ) NEW_LINE return NEW_LINE DEDENT s = ( a + b + c ) / 2 NEW_LINE area = ( s * ( s - a ) * ( s - b ) * ( s - c ) ) ** 0.5 NEW_LINE print ( ' Area ▁ of ▁ a ▁ traingle ▁ is ▁ % f ' % area ) NEW_LINE DEDENT a = 3.0 NEW_LINE b = 4.0 NEW_LINE c = 5.0 NEW_LINE findArea ( a , b , c ) NEW_LINE


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

import math as mt NEW_LINE def largestNumber ( n ) : NEW_LINE INDENT s = " " NEW_LINE duplicate = n NEW_LINE while ( n > 0 ) : NEW_LINE INDENT s = chr ( n % 10 + 48 ) + s NEW_LINE n = n // 10 NEW_LINE DEDENT index = - 1 NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT if ( ( ord ( s [ i ] ) - ord ( '0' ) ) % 2 & 1 ) : NEW_LINE INDENT index = i NEW_LINE break NEW_LINE DEDENT DEDENT if ( index == - 1 ) : NEW_LINE INDENT return duplicate NEW_LINE DEDENT num = 0 NEW_LINE for i in range ( index ) : NEW_LINE INDENT num = num * 10 + ( ord ( s [ i ] ) - ord ( '0' ) ) NEW_LINE DEDENT num = num * 10 + ( ord ( s [ index ] ) - ord ( '0' ) - 1 ) NEW_LINE for i in range ( index + 1 , len ( s ) ) : NEW_LINE INDENT num = num * 10 + 8 NEW_LINE DEDENT return num NEW_LINE DEDENT N = 24578 NEW_LINE print ( largestNumber ( N ) ) NEW_LINE


class GFG { static void generateSequence ( int n , int g ) { for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( i * g + " ▁ " ) ; ; } public static void main ( String [ ] args ) { int n = 6 , g = 5 ; generateSequence ( n , g ) ; } }

def generateSequence ( n , g ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( i * g , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , g = 6 , 5 NEW_LINE generateSequence ( n , g ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int findCount ( String s , int n , int a , int b ) { int res = 0 ; char [ ] s1 = s . toCharArray ( ) ; for ( int i = 0 ; i < b ; i ++ ) { if ( i == a ) { if ( s1 [ n - i - 1 ] != '1' ) res += 1 ; } else { if ( s1 [ n - i - 1 ] != '0' ) res += 1 ; } } return res ; } static public void main ( String [ ] args ) { String str = "1001011001" ; int N = str . length ( ) ; int A = 3 , B = 6 ; System . out . println ( findCount ( str , N , A , B ) ) ; } }

def findCount ( s , n , a , b ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( b ) : NEW_LINE INDENT if ( i == a ) : NEW_LINE INDENT res += ( s [ n - i - 1 ] != '1' ) NEW_LINE DEDENT else : NEW_LINE INDENT res += ( s [ n - i - 1 ] != '0' ) NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = "1001011001" NEW_LINE N = len ( str ) NEW_LINE A = 3 NEW_LINE B = 6 NEW_LINE print ( findCount ( str , N , A , B ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int getProduct ( int n ) { int product = 1 ; while ( n != 0 ) { product = product * ( n % 10 ) ; n = n / 10 ; } return product ; } public static void main ( String [ ] args ) { int n = 4513 ; System . out . println ( getProduct ( n ) ) ; } }

def getProduct ( n ) : NEW_LINE INDENT product = 1 NEW_LINE while ( n != 0 ) : NEW_LINE INDENT product = product * ( n % 10 ) NEW_LINE n = n // 10 NEW_LINE DEDENT return product NEW_LINE DEDENT n = 4513 NEW_LINE print ( getProduct ( n ) ) NEW_LINE


import java . util . * ; class GFG { static void sieve ( boolean [ ] primes , int x ) { primes [ 1 ] = true ; for ( int i = 2 ; i * i <= x ; i ++ ) { if ( primes [ i ] == false ) { for ( int j = 2 ; j * i <= x ; j ++ ) primes [ i * j ] = true ; } } } static int nDivisors ( boolean [ ] primes , int x , int a , int b , int n ) { int result = 0 ; ArrayList < Integer > v = new ArrayList < Integer > ( ) ; for ( int i = 2 ; i <= x ; i ++ ) if ( primes [ i ] == false ) v . add ( i ) ; for ( int i = a ; i <= b ; i ++ ) { int temp = i ; int total = 1 ; int j = 0 ; for ( int k = v . get ( j ) ; k * k <= temp ; k = v . get ( ++ j ) ) { int count = 0 ; while ( temp % k == 0 ) { count ++ ; temp = temp / k ; } total = total * ( count + 1 ) ; } if ( temp != 1 ) total = total * 2 ; if ( total == n ) result ++ ; } return result ; } static int countNDivisors ( int a , int b , int n ) { int x = ( int ) Math . sqrt ( b ) + 1 ; boolean [ ] primes = new boolean [ x + 1 ] ; sieve ( primes , x ) ; return nDivisors ( primes , x , a , b , n ) ; } public static void main ( String [ ] args ) { int a = 1 , b = 7 , n = 2 ; System . out . println ( countNDivisors ( a , b , n ) ) ; } }

import math ; NEW_LINE def sieve ( primes , x ) : NEW_LINE INDENT primes [ 1 ] = False ; NEW_LINE i = 2 ; NEW_LINE while ( i * i <= x ) : NEW_LINE INDENT if ( primes [ i ] == True ) : NEW_LINE INDENT j = 2 ; NEW_LINE while ( j * i <= x ) : NEW_LINE INDENT primes [ i * j ] = False ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT DEDENT def nDivisors ( primes , x , a , b , n ) : NEW_LINE INDENT result = 0 ; NEW_LINE v = [ ] ; NEW_LINE for i in range ( 2 , x + 1 ) : NEW_LINE INDENT if ( primes [ i ] ) : NEW_LINE INDENT v . append ( i ) ; NEW_LINE DEDENT DEDENT for i in range ( a , b + 1 ) : NEW_LINE INDENT temp = i ; NEW_LINE total = 1 ; NEW_LINE j = 0 ; NEW_LINE k = v [ j ] ; NEW_LINE while ( k * k <= temp ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( temp % k == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE temp = int ( temp / k ) ; NEW_LINE DEDENT total = total * ( count + 1 ) ; NEW_LINE j += 1 ; NEW_LINE k = v [ j ] ; NEW_LINE DEDENT if ( temp != 1 ) : NEW_LINE INDENT total = total * 2 ; NEW_LINE DEDENT if ( total == n ) : NEW_LINE INDENT result += 1 ; NEW_LINE DEDENT DEDENT return result ; NEW_LINE DEDENT def countNDivisors ( a , b , n ) : NEW_LINE INDENT x = int ( math . sqrt ( b ) + 1 ) ; NEW_LINE primes = [ True ] * ( x + 1 ) ; NEW_LINE sieve ( primes , x ) ; NEW_LINE return nDivisors ( primes , x , a , b , n ) ; NEW_LINE DEDENT a = 1 ; NEW_LINE b = 7 ; NEW_LINE n = 2 ; NEW_LINE print ( countNDivisors ( a , b , n ) ) ; NEW_LINE


import java . io . * ; class GFG { static int countEvenSum ( int arr [ ] , int n ) { int result = 0 ; for ( int i = 0 ; i <= n - 1 ; i ++ ) { int sum = 0 ; for ( int j = i ; j <= n - 1 ; j ++ ) { sum = sum + arr [ j ] ; if ( sum % 2 == 0 ) result ++ ; } } return ( result ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 3 , 4 , 1 } ; int n = arr . length ; System . out . print ( " The ▁ Number ▁ of ▁ Subarrays " + " ▁ with ▁ even ▁ sum ▁ is ▁ " ) ; System . out . println ( countEvenSum ( arr , n ) ) ; } }

def countEvenSum ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT sum = 0 NEW_LINE for j in range ( i , n , 1 ) : NEW_LINE INDENT sum = sum + arr [ j ] NEW_LINE if ( sum % 2 == 0 ) : NEW_LINE INDENT result = result + 1 NEW_LINE DEDENT DEDENT DEDENT return ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 3 , 4 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ Number ▁ of ▁ Subarrays " , " with ▁ even ▁ sum ▁ is " , countEvenSum ( arr , n ) ) NEW_LINE DEDENT


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

def pattern ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 2 * n ) : NEW_LINE INDENT if i > ( n - j + 1 ) : NEW_LINE INDENT print ( " " , end = ' ▁ ' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = ' ' ) ; NEW_LINE DEDENT if i + n - 1 > j : NEW_LINE INDENT print ( " " , end = ' ▁ ' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = ' ' ) ; NEW_LINE DEDENT DEDENT print ( " " ) ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , 2 * n ) : NEW_LINE INDENT if i < j : NEW_LINE INDENT print ( " " , end = ' ▁ ' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = ' ' ) ; NEW_LINE DEDENT if i < 2 * n - j : NEW_LINE INDENT print ( " " , end = ' ▁ ' ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " * " , end = ' ' ) ; NEW_LINE DEDENT DEDENT print ( " " ) ; NEW_LINE DEDENT DEDENT pattern ( 7 ) ; NEW_LINE


class GFG { static int sum ( int arr [ ] , int l , int r ) { int s = 0 ; for ( int i = l ; i <= r ; i ++ ) { s += arr [ i ] ; } return s ; } static int dp [ ] [ ] [ ] = new int [ 101 ] [ 101 ] [ 101 ] ; static int solve ( int arr [ ] , int l , int r , int k ) { if ( r - l + 1 <= k ) return sum ( arr , l , r ) ; if ( dp [ l ] [ r ] [ k ] != 0 ) return dp [ l ] [ r ] [ k ] ; int sum_ = sum ( arr , l , r ) ; int len_r = ( r - l + 1 ) - k ; int len = ( r - l + 1 ) ; int ans = 0 ; for ( int i = 0 ; i < len - len_r + 1 ; i ++ ) { int sum_sub = sum ( arr , i + l , i + l + len_r - 1 ) ; ans = Math . max ( ans , ( sum_ - sum_sub ) + ( sum_sub - solve ( arr , i + l , i + l + len_r - 1 , k ) ) ) ; } dp [ l ] [ r ] [ k ] = ans ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 15 , 20 , 9 , 2 , 5 } , k = 2 ; int n = arr . length ; System . out . println ( solve ( arr , 0 , n - 1 , k ) ) ; } }

import numpy as np NEW_LINE def Sum ( arr , l , r ) : NEW_LINE INDENT s = 0 ; NEW_LINE for i in range ( l , r + 1 ) : NEW_LINE INDENT s += arr [ i ] ; NEW_LINE DEDENT return s ; NEW_LINE DEDENT dp = np . zeros ( ( 101 , 101 , 101 ) ) ; NEW_LINE def solve ( arr , l , r , k ) : NEW_LINE INDENT if ( r - l + 1 <= k ) : NEW_LINE INDENT return Sum ( arr , l , r ) ; NEW_LINE DEDENT if ( dp [ l ] [ r ] [ k ] ) : NEW_LINE INDENT return dp [ l ] [ r ] [ k ] ; NEW_LINE DEDENT sum_ = Sum ( arr , l , r ) ; NEW_LINE len_r = ( r - l + 1 ) - k ; NEW_LINE length = ( r - l + 1 ) ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( length - len_r + 1 ) : NEW_LINE INDENT sum_sub = Sum ( arr , i + l , i + l + len_r - 1 ) ; NEW_LINE ans = max ( ans , ( sum_ - sum_sub ) + ( sum_sub - solve ( arr , i + l , i + l + len_r - 1 , k ) ) ) ; NEW_LINE DEDENT dp [ l ] [ r ] [ k ] = ans ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 10 , 15 , 20 , 9 , 2 , 5 ] ; k = 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( solve ( arr , 0 , n - 1 , k ) ) ; NEW_LINE DEDENT


class GFG { static double nthRoot ( int A , int N ) { double xPre = Math . random ( ) % 10 ; double eps = 0.001 ; double delX = 2147483647 ; double xK = 0.0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } public static void main ( String [ ] args ) { int N = 4 ; int A = 81 ; double nthRootValue = nthRoot ( A , N ) ; System . out . println ( " Nth ▁ root ▁ is ▁ " + Math . round ( nthRootValue * 1000.0 ) / 1000.0 ) ; } }

import math NEW_LINE import random NEW_LINE def nthRoot ( A , N ) : NEW_LINE INDENT xPre = random . randint ( 1 , 101 ) % 10 NEW_LINE eps = 0.001 NEW_LINE delX = 2147483647 NEW_LINE xK = 0.0 NEW_LINE while ( delX > eps ) : NEW_LINE INDENT xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N NEW_LINE delX = abs ( xK - xPre ) NEW_LINE xPre = xK ; NEW_LINE DEDENT return xK NEW_LINE DEDENT N = 4 NEW_LINE A = 81 NEW_LINE nthRootValue = nthRoot ( A , N ) NEW_LINE print ( " Nth ▁ root ▁ is ▁ " , nthRootValue ) NEW_LINE


import java . util . * ; class GFG { static class pair { int first , second ; pair ( ) { first = 0 ; second = 0 ; } } static void printIndices ( int n , pair a [ ] ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( a [ i ] . second + " ▁ " ) ; System . out . println ( ) ; } static class sort implements Comparator < pair > { public int compare ( pair a , pair b ) { return a . first < b . first ? - 1 : 1 ; } } static void printPermutations ( int n , int a [ ] , int k ) { pair arr [ ] = new pair [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { arr [ i ] = new pair ( ) ; arr [ i ] . first = a [ i ] ; arr [ i ] . second = i ; } Arrays . sort ( arr , new sort ( ) ) ; int count = 1 ; for ( int i = 1 ; i < n ; i ++ ) if ( arr [ i ] . first == arr [ i - 1 ] . first ) count ++ ; if ( count < k ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 0 ; i < k - 1 ; i ++ ) { printIndices ( n , arr ) ; for ( int j = 1 ; j < n ; j ++ ) { if ( arr [ j ] . first == arr [ j - 1 ] . first ) { pair t = arr [ j ] ; arr [ j ] = arr [ j - 1 ] ; arr [ j - 1 ] = t ; break ; } } } printIndices ( n , arr ) ; } public static void main ( String arsg [ ] ) { int a [ ] = { 1 , 3 , 3 , 1 } ; int n = a . length ; int k = 3 ; printPermutations ( n , a , k ) ; } }

def printIndices ( n , a ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( a [ i ] [ 1 ] , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE DEDENT def printPermutations ( n , a , k ) : NEW_LINE INDENT arr = [ [ 0 , 0 ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT arr [ i ] [ 0 ] = a [ i ] NEW_LINE arr [ i ] [ 1 ] = i NEW_LINE DEDENT arr . sort ( reverse = False ) NEW_LINE count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] [ 0 ] == arr [ i - 1 ] [ 0 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count < k ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return NEW_LINE DEDENT for i in range ( k - 1 ) : NEW_LINE INDENT printIndices ( n , arr ) NEW_LINE for j in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ j ] [ 0 ] == arr [ j - 1 ] [ 0 ] ) : NEW_LINE INDENT temp = arr [ j ] NEW_LINE arr [ j ] = arr [ j - 1 ] NEW_LINE arr [ j - 1 ] = temp NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT printIndices ( n , arr ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 3 , 3 , 1 ] NEW_LINE n = len ( a ) NEW_LINE k = 3 NEW_LINE printPermutations ( n , a , k ) NEW_LINE DEDENT


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

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


import java . util . * ; class GFG { static int M = 4 ; static int N = 5 ; static void printCommonElements ( int mat [ ] [ ] ) { Map < Integer , Integer > mp = new HashMap < > ( ) ; for ( int j = 0 ; j < N ; j ++ ) mp . put ( mat [ 0 ] [ j ] , 1 ) ; for ( int i = 1 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) { if ( mp . get ( mat [ i ] [ j ] ) != null && mp . get ( mat [ i ] [ j ] ) == i ) { mp . put ( mat [ i ] [ j ] , i + 1 ) ; if ( i == M - 1 ) System . out . print ( mat [ i ] [ j ] + " ▁ " ) ; } } } } public static void main ( String [ ] args ) { int mat [ ] [ ] = { { 1 , 2 , 1 , 4 , 8 } , { 3 , 7 , 8 , 5 , 1 } , { 8 , 7 , 7 , 3 , 1 } , { 8 , 1 , 2 , 7 , 9 } , } ; printCommonElements ( mat ) ; } }

M = 4 NEW_LINE N = 5 NEW_LINE def printCommonElements ( mat ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for j in range ( N ) : NEW_LINE INDENT mp [ mat [ 0 ] [ j ] ] = 1 NEW_LINE DEDENT for i in range ( 1 , M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT if ( mat [ i ] [ j ] in mp . keys ( ) and mp [ mat [ i ] [ j ] ] == i ) : NEW_LINE INDENT mp [ mat [ i ] [ j ] ] = i + 1 NEW_LINE if i == M - 1 : NEW_LINE INDENT print ( mat [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT mat = [ [ 1 , 2 , 1 , 4 , 8 ] , [ 3 , 7 , 8 , 5 , 1 ] , [ 8 , 7 , 7 , 3 , 1 ] , [ 8 , 1 , 2 , 7 , 9 ] ] NEW_LINE printCommonElements ( mat ) NEW_LINE


class GFG { static void longestString ( String str1 , String str2 ) { int count1 [ ] = new int [ 26 ] , count2 [ ] = new int [ 26 ] ; for ( int i = 0 ; i < str1 . length ( ) ; i ++ ) { count1 [ str1 . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < str2 . length ( ) ; i ++ ) { count2 [ str2 . charAt ( i ) - ' a ' ] ++ ; } String result = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 1 ; j <= Math . min ( count1 [ i ] , count2 [ i ] ) ; j ++ ) { result += ( char ) ( ' a ' + i ) ; } } System . out . println ( result ) ; } public static void main ( String [ ] args ) { String str1 = " geeks " , str2 = " cake " ; longestString ( str1 , str2 ) ; } }

def longestString ( str1 , str2 ) : NEW_LINE INDENT count1 = [ 0 ] * 26 NEW_LINE count2 = [ 0 ] * 26 NEW_LINE for i in range ( len ( str1 ) ) : NEW_LINE INDENT count1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( len ( str2 ) ) : NEW_LINE INDENT count2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT result = " " NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT for j in range ( 1 , min ( count1 [ i ] , count2 [ i ] ) + 1 ) : NEW_LINE INDENT result = result + chr ( ord ( ' a ' ) + i ) NEW_LINE DEDENT DEDENT print ( result ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str1 = " geeks " NEW_LINE str2 = " cake " NEW_LINE longestString ( str1 , str2 ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 2005 ; static Set < Integer > ans = new LinkedHashSet < Integer > ( ) ; static void dfs ( int num , int left , int ct ) { if ( ct >= 15 ) return ; if ( left == 0 ) ans . add ( num ) ; for ( int i = 0 ; i <= Math . min ( left , 9 ) ; i ++ ) dfs ( num * 10 + i , left - i , ct + 1 ) ; } static int getKthNum ( int m , int k ) { dfs ( 0 , m , 0 ) ; int ct = 0 ; for ( int it : ans ) { ct ++ ; if ( ct == k ) { return it ; } } return - 1 ; } public static void main ( String [ ] args ) { int m = 5 , k = 3 ; System . out . println ( getKthNum ( m , k ) ) ; } }

N = 2005 NEW_LINE ans = dict ( ) NEW_LINE def dfs ( n , left , ct ) : NEW_LINE INDENT if ( ct >= 15 ) : NEW_LINE INDENT return NEW_LINE DEDENT if ( left == 0 ) : NEW_LINE INDENT ans [ n ] = 1 NEW_LINE DEDENT for i in range ( min ( left , 9 ) + 1 ) : NEW_LINE INDENT dfs ( n * 10 + i , left - i , ct + 1 ) NEW_LINE DEDENT DEDENT def getKthNum ( m , k ) : NEW_LINE INDENT dfs ( 0 , m , 0 ) NEW_LINE c = 0 NEW_LINE for it in sorted ( ans . keys ( ) ) : NEW_LINE INDENT c += 1 NEW_LINE if ( c == k ) : NEW_LINE INDENT return it NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT m = 5 NEW_LINE k = 3 NEW_LINE print ( getKthNum ( m , k ) ) NEW_LINE


public class GFG { static class Node { int data ; Node next ; Node ( int data ) { this . data = data ; } } static Node modularNode ( Node head , int k ) { if ( k <= 0 || head == null ) return null ; int i = 1 ; Node modularNode = null ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( i % k == 0 ) modularNode = temp ; i ++ ; } return modularNode ; } public static void main ( String [ ] args ) { Node head = new Node ( 1 ) ; head . next = new Node ( 2 ) ; head . next . next = new Node ( 3 ) ; head . next . next . next = new Node ( 4 ) ; head . next . next . next . next = new Node ( 5 ) ; int k = 2 ; Node answer = modularNode ( head , k ) ; System . out . print ( " Modular ▁ node ▁ is ▁ " ) ; if ( answer != null ) System . out . println ( answer . data ) ; else System . out . println ( " null " ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def newNode ( data ) : NEW_LINE INDENT new_node = Node ( data ) NEW_LINE new_node . data = data NEW_LINE new_node . next = None NEW_LINE return new_node NEW_LINE DEDENT def modularNode ( head , k ) : NEW_LINE INDENT if ( k <= 0 or head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT i = 1 NEW_LINE modularNode = None NEW_LINE temp = head NEW_LINE while ( temp != None ) : NEW_LINE INDENT if ( i % k == 0 ) : NEW_LINE INDENT modularNode = temp NEW_LINE DEDENT i = i + 1 NEW_LINE temp = temp . next NEW_LINE DEDENT return modularNode NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = newNode ( 1 ) NEW_LINE head . next = newNode ( 2 ) NEW_LINE head . next . next = newNode ( 3 ) NEW_LINE head . next . next . next = newNode ( 4 ) NEW_LINE head . next . next . next . next = newNode ( 5 ) NEW_LINE k = 2 NEW_LINE answer = modularNode ( head , k ) NEW_LINE print ( " Modular ▁ node ▁ is " , end = ' ▁ ' ) NEW_LINE if ( answer != None ) : NEW_LINE INDENT print ( answer . data , end = ' ▁ ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " None " ) NEW_LINE DEDENT DEDENT


class GFG { static int findMax ( int arr [ ] , int n ) { boolean [ ] divisible = new boolean [ n ] ; int res = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( divisible [ i ] ) continue ; int cnt = 0 ; for ( int j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] % arr [ i ] ) == 0 ) { divisible [ j ] = true ; cnt ++ ; } } res = Math . max ( res , cnt ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; System . out . println ( findMax ( arr , n ) ) ; } }

def findMax ( arr , n ) : NEW_LINE INDENT divisible = [ False ] * n ; NEW_LINE res = 0 ; NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( divisible [ i ] ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT cnt = 0 ; NEW_LINE for j in range ( i ) : NEW_LINE INDENT if ( ( arr [ j ] % arr [ i ] ) == 0 ) : NEW_LINE INDENT divisible [ j ] = True ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT res = max ( res , cnt ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findMax ( arr , n ) ) ; NEW_LINE DEDENT


class GFG { static int MAX = 10 ; static String getMaxEven ( char [ ] str , int len ) { int [ ] freq = new int [ MAX ] ; int i , minEvenDigit = MAX , minDigit = MAX ; for ( i = 0 ; i < len ; i ++ ) { int digit = str [ i ] - '0' ; freq [ digit ] ++ ; if ( digit % 2 == 0 ) minEvenDigit = Math . min ( digit , minEvenDigit ) ; minDigit = Math . min ( digit , minDigit ) ; } if ( minEvenDigit == MAX ) return " - 1" ; freq [ minEvenDigit ] -- ; freq [ minDigit ] -- ; i = 0 ; for ( int j = MAX - 1 ; j >= 0 ; j -- ) { for ( int k = 0 ; k < freq [ j ] ; k ++ ) str [ i ++ ] = ( char ) ( j + '0' ) ; if ( j == minEvenDigit ) str [ i ++ ] = ( char ) ( minDigit + '0' ) ; } str [ i - 1 ] = ( char ) ( minEvenDigit + '0' ) ; return String . valueOf ( str ) ; } public static void main ( String [ ] args ) { char [ ] str = "1023422" . toCharArray ( ) ; int len = str . length ; System . out . println ( getMaxEven ( str , len ) ) ; } }

MAX = 10 NEW_LINE def getMaxEven ( string , length ) : NEW_LINE INDENT string = list ( string ) NEW_LINE freq = [ 0 ] * MAX NEW_LINE minEvenDigit = MAX ; NEW_LINE minDigit = MAX ; NEW_LINE for i in range ( length ) : NEW_LINE INDENT digit = ord ( string [ i ] ) - ord ( '0' ) ; NEW_LINE freq [ digit ] += 1 ; NEW_LINE if ( digit % 2 == 0 ) : NEW_LINE INDENT minEvenDigit = min ( digit , minEvenDigit ) ; NEW_LINE DEDENT minDigit = min ( digit , minDigit ) ; NEW_LINE DEDENT if ( minEvenDigit == MAX ) : NEW_LINE INDENT return " - 1" ; NEW_LINE DEDENT freq [ minEvenDigit ] -= 1 ; NEW_LINE freq [ minDigit ] -= 1 ; NEW_LINE i = 0 ; NEW_LINE for j in range ( MAX - 1 , - 1 , - 1 ) : NEW_LINE INDENT for k in range ( freq [ j ] ) : NEW_LINE INDENT string [ i ] = chr ( j + ord ( '0' ) ) ; NEW_LINE i += 1 NEW_LINE DEDENT if ( j == minEvenDigit ) : NEW_LINE INDENT string [ i ] = chr ( minDigit + ord ( '0' ) ) ; NEW_LINE i += 1 NEW_LINE DEDENT DEDENT return " " . join ( string ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = "1023422" ; NEW_LINE length = len ( string ) ; NEW_LINE print ( getMaxEven ( string , length ) ) ; NEW_LINE DEDENT


class GFG { static char maxRepeating ( String str ) { int n = str . length ( ) ; int count = 0 ; char res = str . charAt ( 0 ) ; int cur_count = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( i < n - 1 && str . charAt ( i ) == str . charAt ( i + 1 ) ) cur_count ++ ; else { if ( cur_count > count ) { count = cur_count ; res = str . charAt ( i ) ; } cur_count = 1 ; } } return res ; } public static void main ( String args [ ] ) { String str = " aaaabbaaccde " ; System . out . println ( maxRepeating ( str ) ) ; } }

def maxRepeating ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE count = 0 NEW_LINE res = str [ 0 ] NEW_LINE cur_count = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i < n - 1 and str [ i ] == str [ i + 1 ] ) : NEW_LINE INDENT cur_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if cur_count > count : NEW_LINE INDENT count = cur_count NEW_LINE res = str [ i ] NEW_LINE DEDENT cur_count = 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " aaaabbaaccde " NEW_LINE print ( maxRepeating ( str ) ) NEW_LINE DEDENT


class GFG { static boolean isSquare ( int num ) { int root = ( int ) Math . sqrt ( num ) ; return ( root * root ) == num ; } static boolean isCube ( int num ) { int root = ( int ) Math . cbrt ( num ) ; return ( root * root * root ) == num ; } static int countSC ( int N ) { int count = 0 ; for ( int i = 1 ; i <= N ; i ++ ) { if ( isSquare ( i ) ) count ++ ; else if ( isCube ( i ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int N = 20 ; System . out . println ( " Number ▁ of ▁ squares ▁ " + " and ▁ cubes ▁ is ▁ " + countSC ( N ) ) ; } }

def isSquare ( num ) : NEW_LINE INDENT root = int ( num ** ( 1 / 2 ) ) NEW_LINE return ( root * root ) == num NEW_LINE DEDENT def isCube ( num ) : NEW_LINE INDENT root = int ( num ** ( 1 / 3 ) ) NEW_LINE return ( root * root * root ) == num NEW_LINE DEDENT def countSC ( N ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT if isSquare ( i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT elif isCube ( i ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 20 NEW_LINE print ( " Number ▁ of ▁ squares ▁ and ▁ cubes ▁ is ▁ " , countSC ( N ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void intersection ( int a [ ] , int b [ ] , int n , int m ) { int i = 0 , j = 0 ; while ( i < n && j < m ) { if ( a [ i ] > b [ j ] ) { j ++ ; } else if ( b [ j ] > a [ i ] ) { i ++ ; } else { System . out . print ( a [ i ] + " ▁ " ) ; i ++ ; j ++ ; } } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 } ; int b [ ] = { 3 , 3 , 5 } ; int n = a . length ; int m = b . length ; intersection ( a , b , n , m ) ; } }

def intersection ( a , b , n , m ) : NEW_LINE INDENT i = 0 NEW_LINE j = 0 NEW_LINE while ( i < n and j < m ) : NEW_LINE INDENT if ( a [ i ] > b [ j ] ) : NEW_LINE INDENT j += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( b [ j ] > a [ i ] ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 , 3 , 4 , 5 , 5 , 6 ] NEW_LINE b = [ 3 , 3 , 5 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE intersection ( a , b , n , m ) NEW_LINE DEDENT


class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int findReachable ( int [ ] arr , int D , int A , int B , int n ) { int gcd_AB = GCD ( A , B ) ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] - D ) % gcd_AB == 0 ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; int D = 4 , A = 4 , B = 6 ; System . out . println ( findReachable ( arr , D , A , B , n ) ) ; } }

def GCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT return GCD ( b , a % b ) ; NEW_LINE DEDENT def findReachable ( arr , D , A , B , n ) : NEW_LINE INDENT gcd_AB = GCD ( A , B ) ; NEW_LINE count = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ( arr [ i ] - D ) % gcd_AB == 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT arr = [ 4 , 5 , 6 , 7 , 8 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE D = 4 ; A = 4 ; B = 6 ; NEW_LINE print ( findReachable ( arr , D , A , B , n ) ) ; NEW_LINE


import java . util . * ; class GfG { static class Node { int data ; Node left , right ; } static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . left = null ; newNode . right = null ; return newNode ; } static boolean isFullBinaryTree ( Node root ) { if ( root == null ) return true ; Queue < Node > q = new LinkedList < Node > ( ) ; q . add ( root ) ; while ( ! q . isEmpty ( ) ) { Node node = q . peek ( ) ; q . remove ( ) ; if ( node . left == null && node . right == null ) continue ; if ( node . left == null || node . right == null ) return false ; q . add ( node . left ) ; q . add ( node . right ) ; } return true ; } public static void main ( String [ ] args ) { Node root = getNode ( 1 ) ; root . left = getNode ( 2 ) ; root . right = getNode ( 3 ) ; root . left . left = getNode ( 4 ) ; root . left . right = getNode ( 5 ) ; if ( isFullBinaryTree ( root ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

class getNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def isFullBinaryTree ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return True NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE while ( not len ( q ) ) : NEW_LINE INDENT node = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( node . left == None and node . right == None ) : NEW_LINE INDENT continue NEW_LINE DEDENT if ( node . left == None or node . right == None ) : NEW_LINE INDENT return False NEW_LINE DEDENT q . append ( node . left ) NEW_LINE q . append ( node . right ) NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getNode ( 1 ) NEW_LINE root . left = getNode ( 2 ) NEW_LINE root . right = getNode ( 3 ) NEW_LINE root . left . left = getNode ( 4 ) NEW_LINE root . left . right = getNode ( 5 ) NEW_LINE if ( isFullBinaryTree ( root ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static void tower ( int n , char sourcePole , char destinationPole , char auxiliaryPole ) { if ( 0 == n ) return ; tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) ; System . out . printf ( " Move ▁ the ▁ disk ▁ % d ▁ from ▁ % c ▁ to ▁ % c \n " , n , sourcePole , destinationPole ) ; tower ( n - 1 , auxiliaryPole , destinationPole , sourcePole ) ; } public static void main ( String [ ] args ) { tower ( 3 , ' S ' , ' D ' , ' A ' ) ; } }

def tower ( n , sourcePole , destinationPole , auxiliaryPole ) : NEW_LINE INDENT if ( 0 == n ) : NEW_LINE INDENT return NEW_LINE DEDENT tower ( n - 1 , sourcePole , auxiliaryPole , destinationPole ) NEW_LINE print ( " Move ▁ the ▁ disk " , sourcePole , " from " , sourcePole , " to " , destinationPole ) NEW_LINE tower ( n - 1 , auxiliaryPole , destinationPole , sourcePole ) NEW_LINE DEDENT tower ( 3 , ' S ' , ' D ' , ' A ' ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static long fourthPowerSum ( int n ) { long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) sum = sum + ( i * i * i * i ) ; return sum ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( fourthPowerSum ( n ) ) ; } }

import math NEW_LINE def fourthPowerSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT sum = sum + ( i * i * i * i ) NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 6 NEW_LINE print ( fourthPowerSum ( n ) ) NEW_LINE


import java . io . * ; import java . lang . * ; class GFG { public static int nthTerm ( int N ) { return Math . abs ( N * ( ( N - 1 ) * ( N - 3 ) * ( N - 5 ) ) ) ; } public static void main ( String [ ] args ) { int N = 6 ; System . out . println ( nthTerm ( N ) ) ; } }

def nthTerm ( N ) : NEW_LINE INDENT return ( abs ( N * ( ( N - 1 ) * ( N - 3 ) * ( N - 5 ) ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 6 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; import java . lang . * ; class GFG { static void getAverageCountArray ( int [ ] a , int x , int N ) { HashMap < Integer , Integer > map = new HashMap < Integer , Integer > ( ) ; int [ ] avg = new int [ N ] ; int val , av ; for ( int i = 0 ; i < N ; i ++ ) { if ( ! map . containsKey ( a [ i ] ) ) { map . put ( a [ i ] , 1 ) ; } else { val = map . get ( a [ i ] ) ; val ++ ; map . remove ( a [ i ] ) ; map . put ( a [ i ] , val ) ; } } for ( int i = 0 ; i < N ; i ++ ) { av = ( a [ i ] + x ) / 2 ; if ( map . containsKey ( av ) ) { val = map . get ( av ) ; avg [ i ] = val ; } } for ( int i = 0 ; i < N ; i ++ ) { System . out . print ( avg [ i ] + " ▁ " ) ; } } public static void main ( String args [ ] ) { int [ ] a = { 2 , 0 , 4 , 6 , 2 } ; int x = 2 ; int N = a . length ; getAverageCountArray ( a , x , N ) ; } }

def getAverageCountArray ( a , x , N ) : NEW_LINE INDENT map = { } NEW_LINE avg = [ 0 ] * N NEW_LINE for i in range ( N ) : NEW_LINE INDENT if a [ i ] not in map : NEW_LINE INDENT map [ a [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT map [ a [ i ] ] += 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT av = ( a [ i ] + x ) // 2 NEW_LINE if av in map : NEW_LINE INDENT val = map [ av ] NEW_LINE avg [ i ] = val NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( avg [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 0 , 4 , 6 , 2 ] NEW_LINE x = 2 NEW_LINE N = len ( a ) NEW_LINE getAverageCountArray ( a , x , N ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; if ( b == 0 ) return a ; int k ; for ( k = 0 ; ( ( a | b ) & 1 ) == 0 ; ++ k ) { a >>= 1 ; b >>= 1 ; } while ( ( a & 1 ) == 0 ) a >>= 1 ; do { while ( ( b & 1 ) == 0 ) b >>= 1 ; if ( a > b ) { int temp = a ; a = b ; b = temp ; } b = ( b - a ) ; } while ( b != 0 ) ; return a << k ; } public static void main ( String args [ ] ) { int a = 34 , b = 17 ; System . out . println ( " Gcd ▁ of ▁ given ▁ " + " numbers ▁ is ▁ " + gcd ( a , b ) ) ; } }

def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT k = 0 NEW_LINE while ( ( ( a | b ) & 1 ) == 0 ) : NEW_LINE INDENT a = a >> 1 NEW_LINE b = b >> 1 NEW_LINE k = k + 1 NEW_LINE DEDENT while ( ( a & 1 ) == 0 ) : NEW_LINE INDENT a = a >> 1 NEW_LINE DEDENT while ( b != 0 ) : NEW_LINE INDENT while ( ( b & 1 ) == 0 ) : NEW_LINE INDENT b = b >> 1 NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT temp = a NEW_LINE a = b NEW_LINE b = temp NEW_LINE DEDENT b = ( b - a ) NEW_LINE DEDENT return ( a << k ) NEW_LINE DEDENT a = 34 NEW_LINE b = 17 NEW_LINE print ( " Gcd ▁ of ▁ given ▁ numbers ▁ is ▁ " , gcd ( a , b ) ) NEW_LINE


import java . io . * ; class GFG { static int summation ( int n ) { int sum ; sum = ( int ) ( Math . pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ; return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( summation ( n ) ) ; } }

import math NEW_LINE def summation ( n ) : NEW_LINE INDENT return int ( ( pow ( 10 , n + 1 ) - 10 - ( 9 * n ) ) / 81 ) ; NEW_LINE DEDENT print ( summation ( 5 ) ) ; NEW_LINE