class GFG { static String findLargest ( int N ) { String largest = " " ; for ( int i = 0 ; i < N ; i ++ ) largest += ' F ' ; return largest ; } static String findSmallest ( int N ) { String smallest = "1" ; for ( int i = 0 ; i < N - 1 ; i ++ ) smallest += '0' ; return smallest ; } static void print ( int largest ) { System . out . println ( " Largest : ▁ " + findLargest ( largest ) ) ; System . out . println ( " Smallest : ▁ " + findSmallest ( largest ) ) ; } public static void main ( String [ ] args ) { int N = 4 ; print ( N ) ; } }

def findLargest ( N ) : NEW_LINE INDENT largest = ' F ' * N NEW_LINE return largest ; NEW_LINE DEDENT def findSmallest ( N ) : NEW_LINE INDENT smallest = '1' + '0' * ( N - 1 ) NEW_LINE return smallest ; NEW_LINE DEDENT def printAns ( largest ) : NEW_LINE INDENT print ( " Largest : ▁ " , findLargest ( largest ) ) ; NEW_LINE print ( " Smallest : ▁ " , findSmallest ( largest ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; NEW_LINE printAns ( N ) ; NEW_LINE DEDENT


class GFG { static int TotalWays ( int n , int s , int k ) { int [ ] dp = new int [ n ] ; dp [ s - 1 ] = 1 ; for ( int i = s ; i < n ; i ++ ) { int idx = Math . max ( s - 1 , i - k ) ; for ( int j = idx ; j < i ; j ++ ) { dp [ i ] += dp [ j ] ; } } return dp [ n - 1 ] ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 2 ; int s = 2 ; System . out . print ( " Total ▁ Ways ▁ = ▁ " + TotalWays ( n , s , k ) ) ; } }

def TotalWays ( n , s , k ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE dp [ s - 1 ] = 1 NEW_LINE for i in range ( s , n ) : NEW_LINE INDENT idx = max ( s - 1 , i - k ) NEW_LINE for j in range ( idx , i ) : NEW_LINE INDENT dp [ i ] += dp [ j ] NEW_LINE DEDENT DEDENT return dp [ n - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE k = 2 NEW_LINE s = 2 NEW_LINE print ( " Total ▁ Ways ▁ = ▁ " , TotalWays ( n , s , k ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static int PointInKSquares ( int n , int a [ ] , int k ) { Arrays . sort ( a ) ; return a [ n - k ] ; } public static void main ( String [ ] args ) { int k = 2 ; int [ ] a = { 1 , 2 , 3 , 4 } ; int n = a . length ; int x = PointInKSquares ( n , a , k ) ; System . out . println ( " ( " + x + " , ▁ " + x + " ) " ) ; } }

def PointInKSquares ( n , a , k ) : NEW_LINE INDENT a . sort ( ) NEW_LINE return a [ n - k ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 2 NEW_LINE a = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( a ) NEW_LINE x = PointInKSquares ( n , a , k ) NEW_LINE print ( " ( " , x , " , " , x , " ) " ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static String printVertically ( String s ) { String result = " " ; HashMap < Integer , String > map = new HashMap < > ( ) ; Scanner sc = new Scanner ( s ) ; int max = Integer . MIN_VALUE ; while ( sc . hasNext ( ) ) { String st = sc . next ( ) ; max = Math . max ( max , st . length ( ) ) ; } sc = new Scanner ( s ) ; while ( sc . hasNext ( ) ) { String st = sc . next ( ) ; for ( int i = 0 ; i < st . length ( ) ; i ++ ) { char ch = st . charAt ( i ) ; map . put ( i , map . getOrDefault ( i , " " ) + ch ) ; } for ( int i = st . length ( ) ; i < max ; i ++ ) { map . put ( i , map . getOrDefault ( i , " " ) + " $ " ) ; } } for ( int i = 0 ; i < max ; i ++ ) { String a = map . get ( i ) ; result += a + " ▁ " ; } return result ; } public static void main ( String [ ] args ) { String st = " ge ▁ ek ▁ sfor ▁ ge ▁ eks " ; System . out . println ( printVertically ( st ) ) ; } }

def printVertically ( s ) : NEW_LINE INDENT s = s . split ( " ▁ " ) NEW_LINE result = " " NEW_LINE map = dict ( ) NEW_LINE mx = 0 NEW_LINE for i in s : NEW_LINE INDENT mx = max ( mx , len ( i ) ) NEW_LINE DEDENT n = mx NEW_LINE i = 0 NEW_LINE while ( i < mx ) : NEW_LINE INDENT for j in s : NEW_LINE INDENT if i >= len ( j ) : NEW_LINE INDENT map [ i ] = map . get ( i , " " ) + " $ " NEW_LINE DEDENT else : NEW_LINE INDENT map [ i ] = map . get ( i , " " ) + j [ i ] NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT for j in range ( mx ) : NEW_LINE INDENT result += map [ j ] + " ▁ " NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT st = " ge ▁ ek ▁ sfor ▁ ge ▁ eks " NEW_LINE print ( printVertically ( st ) ) NEW_LINE DEDENT


class GFG { static int getMaximumSumWithLCMN ( int N ) { int sum = 0 ; int LIM = ( int ) Math . sqrt ( N ) ; for ( int i = 1 ; i <= LIM ; i ++ ) { if ( N % i == 0 ) { if ( i == ( N / i ) ) sum += i ; else sum += ( i + N / i ) ; } } return sum ; } public static void main ( String [ ] args ) { int N = 12 ; System . out . println ( getMaximumSumWithLCMN ( N ) ) ; } }

import math NEW_LINE def getMaximumSumWithLCMN ( N ) : NEW_LINE INDENT sum = 0 NEW_LINE LIM = int ( math . sqrt ( N ) ) NEW_LINE for i in range ( 1 , LIM + 1 ) : NEW_LINE INDENT if ( N % i == 0 ) : NEW_LINE INDENT if ( i == ( N // i ) ) : NEW_LINE INDENT sum = sum + i NEW_LINE DEDENT else : NEW_LINE INDENT sum = sum + ( i + N // i ) NEW_LINE DEDENT DEDENT DEDENT return sum NEW_LINE DEDENT N = 12 NEW_LINE print ( getMaximumSumWithLCMN ( N ) ) NEW_LINE


public class GFG { static final int MAX_CHAR = 26 ; static boolean arekAnagrams ( String str1 , String str2 , int k ) { int n = str1 . length ( ) ; if ( str2 . length ( ) != n ) return false ; int [ ] count1 = new int [ MAX_CHAR ] ; int [ ] count2 = new int [ MAX_CHAR ] ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) count1 [ str1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) count2 [ str2 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) if ( count1 [ i ] > count2 [ i ] ) count = count + Math . abs ( count1 [ i ] - count2 [ i ] ) ; return ( count <= k ) ; } public static void main ( String args [ ] ) { String str1 = " anagram " ; String str2 = " grammar " ; int k = 2 ; if ( arekAnagrams ( str1 , str2 , k ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

MAX_CHAR = 26 NEW_LINE def arekAnagrams ( str1 , str2 , k ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE if ( len ( str2 ) != n ) : NEW_LINE INDENT return False NEW_LINE DEDENT count1 = [ 0 ] * MAX_CHAR NEW_LINE count2 = [ 0 ] * MAX_CHAR NEW_LINE for i in range ( n ) : NEW_LINE INDENT count1 [ ord ( str1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT count2 [ ord ( str2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( MAX_CHAR ) : NEW_LINE INDENT if ( count1 [ i ] > count2 [ i ] ) : NEW_LINE INDENT count = count + abs ( count1 [ i ] - count2 [ i ] ) NEW_LINE DEDENT DEDENT return ( count <= k ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " anagram " NEW_LINE str2 = " grammar " NEW_LINE k = 2 NEW_LINE if ( arekAnagrams ( str1 , str2 , k ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static void findNumbers ( int n , int b ) { double even = 0 , odd = 0 ; if ( b % 2 == 0 ) { even = Math . pow ( b , n ) - 2 ; odd = Math . pow ( b , n ) - 1 ; } else { even = Math . pow ( b , n ) - 1 ; odd = Math . pow ( b , n ) - 2 ; } System . out . println ( " Even ▁ Number ▁ = ▁ " + ( int ) even ) ; System . out . print ( " Odd ▁ Number ▁ = ▁ " + ( int ) odd ) ; } public static void main ( String [ ] args ) { int N = 2 , B = 5 ; findNumbers ( N , B ) ; } }

def findNumbers ( n , b ) : NEW_LINE INDENT even = 0 ; NEW_LINE odd = 0 ; NEW_LINE if ( b % 2 == 0 ) : NEW_LINE INDENT even = pow ( b , n ) - 2 ; NEW_LINE odd = pow ( b , n ) - 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT even = pow ( b , n ) - 1 ; NEW_LINE odd = pow ( b , n ) - 2 ; NEW_LINE DEDENT print ( " Even ▁ Number ▁ = ▁ " , int ( even ) ) ; NEW_LINE print ( " Odd ▁ Number ▁ = ▁ " , int ( odd ) ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 ; NEW_LINE B = 5 ; NEW_LINE findNumbers ( N , B ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void count_unique ( HashSet < Integer > s , int n ) { if ( s . contains ( n ) ) return ; s . add ( n ) ; n += 1 ; while ( n % 10 == 0 ) { n = n / 10 ; } count_unique ( s , n ) ; } public static void main ( String [ ] args ) { int n = 10 ; HashSet < Integer > s = new HashSet < > ( ) ; count_unique ( s , n ) ; System . out . println ( s . size ( ) ) ; } }

def count_unique ( s , n ) : NEW_LINE INDENT if ( s . count ( n ) ) : NEW_LINE INDENT return ; NEW_LINE DEDENT s . append ( n ) ; NEW_LINE n += 1 ; NEW_LINE while ( n % 10 == 0 ) : NEW_LINE INDENT n = n // 10 ; NEW_LINE DEDENT count_unique ( s , n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE s = [ ] NEW_LINE count_unique ( s , n ) NEW_LINE print ( len ( s ) ) NEW_LINE DEDENT


class Main { static int findMaximum ( int arr [ ] , int low , int high ) { int max = arr [ low ] ; int i ; for ( i = low ; i <= high ; i ++ ) { if ( arr [ i ] > max ) max = arr [ i ] ; } return max ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 30 , 40 , 50 , 60 , 70 , 23 , 20 } ; int n = arr . length ; System . out . println ( " The ▁ maximum ▁ element ▁ is ▁ " + findMaximum ( arr , 0 , n - 1 ) ) ; } }

def findMaximum ( arr , low , high ) : NEW_LINE INDENT max = arr [ low ] NEW_LINE i = low NEW_LINE for i in range ( high + 1 ) : NEW_LINE INDENT if arr [ i ] > max : NEW_LINE INDENT max = arr [ i ] NEW_LINE DEDENT DEDENT return max NEW_LINE DEDENT arr = [ 1 , 30 , 40 , 50 , 60 , 70 , 23 , 20 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ maximum ▁ element ▁ is ▁ % d " % findMaximum ( arr , 0 , n - 1 ) ) NEW_LINE


import java . io . * ; class GFG { static int countOddSquares ( int n , int m ) { return ( int ) Math . pow ( m , 0.5 ) - ( int ) Math . pow ( n - 1 , 0.5 ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 100 ; System . out . println ( " Count ▁ is ▁ " + ( ( m - n + 1 ) - countOddSquares ( n , m ) ) ) ; } }

def countOddSquares ( n , m ) : NEW_LINE INDENT return ( int ( pow ( m , 0.5 ) ) - int ( pow ( n - 1 , 0.5 ) ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; m = 100 ; NEW_LINE print ( " Count ▁ is " , ( m - n + 1 ) - countOddSquares ( n , m ) ) NEW_LINE DEDENT


public class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void findRatio ( int m , int n ) { int Am = m - 5 , An = n - 5 ; int numerator = Am / GCD ( Am , An ) ; int denominator = An / GCD ( Am , An ) ; System . out . println ( numerator + " / " + denominator ) ; } public static void main ( String args [ ] ) { int m = 10 , n = 20 ; findRatio ( m , n ) ; } }

from fractions import gcd NEW_LINE def findRatio ( m , n ) : NEW_LINE INDENT Am = m - 5 NEW_LINE An = n - 5 NEW_LINE numerator = Am // ( gcd ( Am , An ) ) NEW_LINE denominator = An // ( gcd ( Am , An ) ) NEW_LINE print ( numerator , ' / ' , denominator ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m = 10 NEW_LINE n = 20 NEW_LINE findRatio ( m , n ) NEW_LINE DEDENT


class GFG { static void nDigitPerfectSquares ( int n ) { int smallest = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n - 1 ) ) ) , 2 ) ; System . out . print ( smallest + " ▁ " ) ; int largest = ( int ) Math . pow ( Math . ceil ( Math . sqrt ( Math . pow ( 10 , n ) ) ) - 1 , 2 ) ; System . out . print ( largest ) ; } public static void main ( String args [ ] ) { int n = 4 ; nDigitPerfectSquares ( n ) ; } }

import math NEW_LINE def nDigitPerfectSquares ( n ) : NEW_LINE INDENT print ( pow ( math . ceil ( math . sqrt ( pow ( 10 , n - 1 ) ) ) , 2 ) , end = " ▁ " ) ; NEW_LINE print ( pow ( math . ceil ( math . sqrt ( pow ( 10 , n ) ) ) - 1 , 2 ) ) ; NEW_LINE DEDENT n = 4 ; NEW_LINE nDigitPerfectSquares ( n ) ; NEW_LINE


class GFG { public static boolean isMagic ( int n ) { int sum = 0 ; while ( n > 0 || sum > 9 ) { if ( n == 0 ) { n = sum ; sum = 0 ; } sum += n % 10 ; n /= 10 ; } return ( sum == 1 ) ; } public static void main ( String args [ ] ) { int n = 1234 ; if ( isMagic ( n ) ) System . out . println ( " Magic ▁ Number " ) ; else System . out . println ( " Not ▁ a ▁ magic ▁ Number " ) ; } }

def isMagic ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE while ( n > 0 or sum > 9 ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT n = sum ; NEW_LINE sum = 0 ; NEW_LINE DEDENT sum = sum + n % 10 ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return True if ( sum == 1 ) else False ; NEW_LINE DEDENT n = 1234 ; NEW_LINE if ( isMagic ( n ) ) : NEW_LINE INDENT print ( " Magic ▁ Number " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ magic ▁ Number " ) ; NEW_LINE DEDENT


class GFG { static long countPairs ( int arr [ ] , int n ) { long count = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] * arr [ j ] == arr [ i ] + arr [ j ] ) count ++ ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 0 , 3 , 2 , 0 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }

def countPairs ( arr , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ i ] * arr [ j ] == arr [ i ] + arr [ j ] ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 0 , 3 , 2 , 0 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countPairs ( arr , n ) ) ; NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int nextPerfectSquare ( int N ) { int nextN = ( int ) Math . floor ( Math . sqrt ( N ) ) + 1 ; return nextN * nextN ; } public static void main ( String args [ ] ) { int n = 35 ; System . out . println ( nextPerfectSquare ( n ) ) ; } }

import math NEW_LINE def nextPerfectSquare ( N ) : NEW_LINE INDENT nextN = math . floor ( math . sqrt ( N ) ) + 1 NEW_LINE return nextN * nextN NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 35 NEW_LINE print ( nextPerfectSquare ( N ) ) NEW_LINE DEDENT


class Node { int data ; Node left , right ; public Node ( int item ) { data = item ; left = right = null ; } } public class BinaryTree { Node root ; boolean isBST ( ) { return isBSTUtil ( root , Integer . MIN_VALUE , Integer . MAX_VALUE ) ; } boolean isBSTUtil ( Node node , int min , int max ) { if ( node == null ) return true ; if ( node . data < min || node . data > max ) return false ; return ( isBSTUtil ( node . left , min , node . data - 1 ) && isBSTUtil ( node . right , node . data + 1 , max ) ) ; } public static void main ( String args [ ] ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 4 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 5 ) ; tree . root . left . left = new Node ( 1 ) ; tree . root . left . right = new Node ( 3 ) ; if ( tree . isBST ( ) ) System . out . println ( " IS ▁ BST " ) ; else System . out . println ( " Not ▁ a ▁ BST " ) ; } }

INT_MAX = 4294967296 NEW_LINE INT_MIN = - 4294967296 NEW_LINE class Node : 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 isBST ( node ) : NEW_LINE INDENT return ( isBSTUtil ( node , INT_MIN , INT_MAX ) ) NEW_LINE DEDENT def isBSTUtil ( node , mini , maxi ) : NEW_LINE INDENT if node is None : NEW_LINE INDENT return True NEW_LINE DEDENT if node . data < mini or node . data > maxi : NEW_LINE INDENT return False NEW_LINE DEDENT return ( isBSTUtil ( node . left , mini , node . data - 1 ) and isBSTUtil ( node . right , node . data + 1 , maxi ) ) NEW_LINE DEDENT root = Node ( 4 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 5 ) NEW_LINE root . left . left = Node ( 1 ) NEW_LINE root . left . right = Node ( 3 ) NEW_LINE if ( isBST ( root ) ) : NEW_LINE INDENT print " Is ▁ BST " NEW_LINE DEDENT else : NEW_LINE INDENT print " Not ▁ a ▁ BST " NEW_LINE DEDENT


class GFG { static void printValueOfPi ( int N ) { double pi = 2 * Math . acos ( 0.0 ) ; System . out . println ( pi ) ; } public static void main ( String [ ] args ) { int N = 4 ; printValueOfPi ( N ) ; } }

from math import acos NEW_LINE def printValueOfPi ( N ) : NEW_LINE INDENT pi = round ( 2 * acos ( 0.0 ) , N ) ; NEW_LINE print ( pi ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 ; NEW_LINE printValueOfPi ( N ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int __gcd ( int a , int b ) { if ( a == 0 || b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static int find_Gcd ( int crr [ ] , int n ) { int i ; int result = crr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) result = __gcd ( crr [ i ] , result ) ; return result ; } static void profitRatio ( int amountArr [ ] , int timeTrr [ ] , int n ) { int i ; int crr [ ] = new int [ n ] ; for ( i = 0 ; i < n ; i ++ ) crr [ i ] = amountArr [ i ] * timeTrr [ i ] ; int Share = find_Gcd ( crr , n ) ; for ( i = 0 ; i < n - 1 ; i ++ ) System . out . print ( crr [ i ] / Share + " ▁ : ▁ " ) ; System . out . print ( crr [ i ] / Share ) ; } public static void main ( String [ ] args ) { int amountArr [ ] = { 5000 , 6000 , 1000 } ; int timeTrr [ ] = { 6 , 6 , 12 } ; int n = amountArr . length ; profitRatio ( amountArr , timeTrr , n ) ; } }

def __gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 or b == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT return a ; NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return __gcd ( a - b , b ) ; NEW_LINE DEDENT return __gcd ( a , b - a ) ; NEW_LINE DEDENT def find_Gcd ( crr , n ) : NEW_LINE INDENT result = crr [ 0 ] ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT result = __gcd ( crr [ i ] , result ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT def profitRatio ( amountArr , timeTrr , n ) : NEW_LINE INDENT i = 0 ; NEW_LINE crr = [ 0 ] * n ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT crr [ i ] = amountArr [ i ] * timeTrr [ i ] ; NEW_LINE DEDENT Share = find_Gcd ( crr , n ) ; NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT print ( int ( crr [ i ] / Share ) , end = " ▁ : ▁ " ) ; NEW_LINE DEDENT print ( int ( crr [ i + 1 ] / Share ) ) ; NEW_LINE DEDENT amountArr = [ 5000 , 6000 , 1000 ] ; NEW_LINE timeTrr = [ 6 , 6 , 12 ] ; NEW_LINE n = len ( amountArr ) ; NEW_LINE profitRatio ( amountArr , timeTrr , n ) ; NEW_LINE


import java . util . Arrays ; import java . util . Collections ; class GFG { public static int kthgroupsum ( int k ) { int cur = ( k * ( k - 1 ) ) + 1 ; int sum = 0 ; while ( k -- > 0 ) { sum += cur ; cur += 2 ; } return sum ; } public static void main ( String [ ] args ) { int k = 3 ; System . out . print ( kthgroupsum ( k ) ) ; } }

def kthgroupsum ( k ) : NEW_LINE INDENT cur = int ( ( k * ( k - 1 ) ) + 1 ) NEW_LINE sum = 0 NEW_LINE while k : NEW_LINE INDENT sum += cur NEW_LINE cur += 2 NEW_LINE k = k - 1 NEW_LINE DEDENT return sum NEW_LINE DEDENT k = 3 NEW_LINE print ( kthgroupsum ( k ) ) NEW_LINE


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

def pattern ( ) : NEW_LINE INDENT k = 0 NEW_LINE spaces = 1 NEW_LINE n = 7 NEW_LINE for i in range ( n , 0 , - 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT if ( i != n ) : NEW_LINE INDENT for k in range ( 1 , spaces + 1 ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT spaces = spaces + 2 NEW_LINE DEDENT for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT if ( j != n ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT spaces = spaces - 4 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , i + 1 ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT if ( i != n ) : NEW_LINE INDENT for k in range ( 1 , spaces + 1 ) : NEW_LINE INDENT print ( end = " ▁ " ) NEW_LINE DEDENT spaces = spaces - 2 NEW_LINE DEDENT for j in range ( i , 0 , - 1 ) : NEW_LINE INDENT if ( j != n ) : NEW_LINE INDENT print ( i , end = " " ) NEW_LINE DEDENT DEDENT print ( ) NEW_LINE DEDENT DEDENT pattern ( ) NEW_LINE


class GFG { static void printPermutation ( int n , int k ) { if ( k >= n || ( n % 2 == 0 && k == 0 ) ) { System . out . print ( - 1 ) ; return ; } int per [ ] = new int [ n + 1 ] , i ; for ( i = 1 ; i <= n ; i ++ ) { per [ i ] = i ; } int cnt = n - 1 ; i = 2 ; while ( i < n ) { if ( cnt - 1 > k ) { swap ( per , i , i + 1 ) ; cnt -= 2 ; } else if ( cnt - 1 == k ) { swap ( per , 1 , i ) ; cnt -- ; } else { break ; } i += 2 ; } for ( i = 1 ; i <= n ; i ++ ) { System . out . print ( per [ i ] + " ▁ " ) ; } } static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } public static void main ( String [ ] args ) { int n = 4 , k = 3 ; printPermutation ( n , k ) ; } }

def printPermutation ( n , k ) : NEW_LINE INDENT if ( k >= n or ( n % 2 == 0 and k == 0 ) ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE return ; NEW_LINE DEDENT per = [ 0 ] * ( n + 1 ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT per [ i ] = i ; NEW_LINE DEDENT cnt = n - 1 ; NEW_LINE i = 2 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( cnt - 1 > k ) : NEW_LINE INDENT t = per [ i ] ; NEW_LINE per [ i ] = per [ i + 1 ] ; NEW_LINE per [ i + 1 ] = t ; NEW_LINE cnt -= 2 ; NEW_LINE DEDENT elif ( cnt - 1 == k ) : NEW_LINE INDENT t = per [ 1 ] ; NEW_LINE per [ 1 ] = per [ i ] ; NEW_LINE per [ i ] = t ; NEW_LINE cnt -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT i += 2 ; NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( per [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT n = 4 ; NEW_LINE k = 3 ; NEW_LINE printPermutation ( n , k ) ; NEW_LINE


class GFG { static int MAX = 100001 ; static int [ ] prime = new int [ MAX + 1 ] ; static int [ ] countdiv = new int [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 2 ; i * i <= MAX ; ++ i ) { if ( prime [ i ] == 0 ) for ( int j = i * 2 ; j <= MAX ; j += i ) prime [ j ] = i ; } for ( int i = 1 ; i < MAX ; ++ i ) if ( prime [ i ] == 0 ) prime [ i ] = i ; } static int largestGCDSubsequence ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; ++ i ) { int element = arr [ i ] ; while ( element > 1 ) { int div = prime [ element ] ; ++ countdiv [ div ] ; ans = Math . max ( ans , countdiv [ div ] ) ; while ( element % div == 0 ) element /= div ; } } return ans ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int arr [ ] = { 10 , 15 , 7 , 25 , 9 , 35 } ; int size = arr . length ; System . out . println ( largestGCDSubsequence ( arr , size ) ) ; } }

import math as mt NEW_LINE MAX = 100001 NEW_LINE prime = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE countdiv = [ 0 for i in range ( MAX + 1 ) ] NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for i in range ( 2 , mt . ceil ( mt . sqrt ( MAX + 1 ) ) ) : NEW_LINE INDENT if ( prime [ i ] == 0 ) : NEW_LINE INDENT for j in range ( i * 2 , MAX + 1 , i ) : NEW_LINE INDENT prime [ j ] = i NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , MAX ) : NEW_LINE INDENT if ( prime [ i ] == 0 ) : NEW_LINE INDENT prime [ i ] = i NEW_LINE DEDENT DEDENT DEDENT def largestGCDSubsequence ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT element = arr [ i ] NEW_LINE while ( element > 1 ) : NEW_LINE INDENT div = prime [ element ] NEW_LINE countdiv [ div ] += 1 NEW_LINE ans = max ( ans , countdiv [ div ] ) NEW_LINE while ( element % div == 0 ) : NEW_LINE INDENT element = element // div NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT SieveOfEratosthenes ( ) NEW_LINE arr = [ 10 , 15 , 7 , 25 , 9 , 35 ] NEW_LINE size = len ( arr ) NEW_LINE print ( largestGCDSubsequence ( arr , size ) ) NEW_LINE


import java . util . HashSet ; import java . util . Set ; public class GFG { static int distinct_year ( String str ) { String str2 = " " ; Set < String > uniqueDates = new HashSet < > ( ) ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( Character . isDigit ( str . charAt ( i ) ) ) { str2 += ( str . charAt ( i ) ) ; } if ( str . charAt ( i ) == ' - ' ) { str2 = " " ; } if ( str2 . length ( ) == 4 ) { uniqueDates . add ( str2 ) ; str2 = " " ; } } return uniqueDates . size ( ) ; } static public void main ( String [ ] args ) { String str = " UN ▁ was ▁ established ▁ on ▁ 24-10-1945 . " + " India ▁ got ▁ freedom ▁ on ▁ 15-08-1947 . " ; System . out . println ( distinct_year ( str ) ) ; } }

def distinct_years ( str ) : NEW_LINE INDENT str2 = " " NEW_LINE uniqueDates = set ( ) NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT if str [ i ] . isdigit ( ) : NEW_LINE INDENT str2 += str [ i ] NEW_LINE DEDENT if str [ i ] == ' - ' : NEW_LINE INDENT str2 = " " NEW_LINE DEDENT if len ( str2 ) == 4 : NEW_LINE INDENT uniqueDates . add ( str2 ) NEW_LINE str2 = " " NEW_LINE DEDENT DEDENT return len ( uniqueDates ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str =   " UN was established on 24 - 10 - 1945. \ NEW_LINE INDENT India got freedom on 15 - 0 8 - 1947. " NEW_LINE DEDENT print ( distinct_years ( str ) ) NEW_LINE DEDENT


class GFG { static int distinct ( int [ ] arr , int n ) { int count = 0 ; if ( n == 1 ) return 1 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( i == 0 ) { if ( arr [ i ] != arr [ i + 1 ] ) count += 1 ; } else { if ( arr [ i ] != arr [ i + 1 ] || arr [ i ] != arr [ i - 1 ] ) count += 1 ; } } if ( arr [ n - 1 ] != arr [ n - 2 ] ) count += 1 ; return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 0 , 0 , 0 , 1 , 0 } ; int n = arr . length ; System . out . println ( distinct ( arr , n ) ) ; } }

def distinct ( arr ) : NEW_LINE INDENT count = 0 NEW_LINE if len ( arr ) == 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT for i in range ( 0 , len ( arr ) - 1 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT elif ( i > 0 & i < len ( arr ) - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ i + 1 ] or arr [ i ] != arr [ i - 1 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT if ( arr [ len ( arr ) - 1 ] != arr [ len ( arr ) - 2 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT return count NEW_LINE DEDENT arr = [ 0 , 0 , 0 , 0 , 0 , 1 , 0 ] NEW_LINE print ( distinct ( arr ) ) NEW_LINE


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

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


import java . io . * ; class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n <= 3 ) return true ; if ( n % 2 == 0 || n % 3 == 0 ) return false ; for ( int i = 5 ; i * i <= n ; i = i + 6 ) if ( n % i == 0 || n % ( i + 2 ) == 0 ) return false ; return true ; } static void printPrime ( int n ) { for ( int i = 2 ; i <= n ; i ++ ) { if ( isPrime ( i ) ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 7 ; printPrime ( n ) ; } }

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n <= 3 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n % 2 == 0 or n % 3 == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = 5 NEW_LINE while ( i * i <= n ) : NEW_LINE INDENT if ( n % i == 0 or n % ( i + 2 ) == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT i = i + 6 NEW_LINE DEDENT return True NEW_LINE DEDENT def printPrime ( n ) : NEW_LINE INDENT for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if isPrime ( i ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 7 NEW_LINE printPrime ( n ) NEW_LINE


import java . util . * ; class GFG { static boolean isAnagram ( String s1 , String s2 ) { s1 = sortString ( s1 ) ; s2 = sortString ( s2 ) ; return ( s1 . equals ( s2 ) ) ; } public static String sortString ( String inputString ) { char tempArray [ ] = inputString . toCharArray ( ) ; Arrays . sort ( tempArray ) ; return new String ( tempArray ) ; } static int CountSteps ( char [ ] s1 , char [ ] s2 , int size ) { int i = 0 , j = 0 ; int result = 0 ; while ( i < size ) { j = i ; while ( s1 [ j ] != s2 [ i ] ) { j += 1 ; } while ( i < j ) { char temp = s1 [ j ] ; s1 [ j ] = s1 [ j - 1 ] ; s1 [ j - 1 ] = temp ; j -= 1 ; result += 1 ; } i += 1 ; } return result ; } public static void main ( String [ ] args ) { String s1 = " abcd " ; String s2 = " cdab " ; int size = s2 . length ( ) ; if ( isAnagram ( s1 , s2 ) ) System . out . println ( CountSteps ( s1 . toCharArray ( ) , s2 . toCharArray ( ) , size ) ) ; else System . out . println ( - 1 ) ; } }

def isAnagram ( s1 , s2 ) : NEW_LINE INDENT s1 = list ( s1 ) ; NEW_LINE s2 = list ( s2 ) ; NEW_LINE s1 = s1 . sort ( ) ; NEW_LINE s2 = s2 . sort ( ) ; NEW_LINE if ( s1 == s2 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT return 0 ; NEW_LINE DEDENT def CountSteps ( s1 , s2 , size ) : NEW_LINE INDENT s1 = list ( s1 ) ; NEW_LINE s2 = list ( s2 ) ; NEW_LINE i = 0 ; NEW_LINE j = 0 ; NEW_LINE result = 0 ; NEW_LINE while ( i < size ) : NEW_LINE INDENT j = i ; NEW_LINE while ( s1 [ j ] != s2 [ i ] ) : NEW_LINE INDENT j += 1 ; NEW_LINE DEDENT while ( i < j ) : NEW_LINE INDENT temp = s1 [ j ] ; NEW_LINE s1 [ j ] = s1 [ j - 1 ] ; NEW_LINE s1 [ j - 1 ] = temp ; NEW_LINE j -= 1 ; NEW_LINE result += 1 ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " abcd " ; NEW_LINE s2 = " cdab " ; NEW_LINE size = len ( s2 ) ; NEW_LINE if ( isAnagram ( s1 , s2 ) ) : NEW_LINE INDENT print ( CountSteps ( s1 , s2 , size ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT DEDENT


public class GFG { static int CHARS = 26 ; static boolean isValidString ( String str ) { int freq [ ] = new int [ CHARS ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { freq [ str . charAt ( i ) - ' a ' ] ++ ; } int i , freq1 = 0 , count_freq1 = 0 ; for ( i = 0 ; i < CHARS ; i ++ ) { if ( freq [ i ] != 0 ) { freq1 = freq [ i ] ; count_freq1 = 1 ; break ; } } int j , freq2 = 0 , count_freq2 = 0 ; for ( j = i + 1 ; j < CHARS ; j ++ ) { if ( freq [ j ] != 0 ) { if ( freq [ j ] == freq1 ) { count_freq1 ++ ; } else { count_freq2 = 1 ; freq2 = freq [ j ] ; break ; } } } for ( int k = j + 1 ; k < CHARS ; k ++ ) { if ( freq [ k ] != 0 ) { if ( freq [ k ] == freq1 ) { count_freq1 ++ ; } if ( freq [ k ] == freq2 ) { count_freq2 ++ ; } else { return false ; } } if ( count_freq1 > 1 && count_freq2 > 1 ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = " abcbc " ; if ( isValidString ( str ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

CHARS = 26 NEW_LINE def isValidString ( str ) : NEW_LINE INDENT freq = [ 0 ] * CHARS NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT freq1 = 0 NEW_LINE count_freq1 = 0 NEW_LINE for i in range ( CHARS ) : NEW_LINE INDENT if ( freq [ i ] != 0 ) : NEW_LINE INDENT freq1 = freq [ i ] NEW_LINE count_freq1 = 1 NEW_LINE break NEW_LINE DEDENT DEDENT freq2 = 0 NEW_LINE count_freq2 = 0 NEW_LINE for j in range ( i + 1 , CHARS ) : NEW_LINE INDENT if ( freq [ j ] != 0 ) : NEW_LINE INDENT if ( freq [ j ] == freq1 ) : NEW_LINE INDENT count_freq1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count_freq2 = 1 NEW_LINE freq2 = freq [ j ] NEW_LINE break NEW_LINE DEDENT DEDENT DEDENT for k in range ( j + 1 , CHARS ) : NEW_LINE INDENT if ( freq [ k ] != 0 ) : NEW_LINE INDENT if ( freq [ k ] == freq1 ) : NEW_LINE INDENT count_freq1 += 1 NEW_LINE DEDENT if ( freq [ k ] == freq2 ) : NEW_LINE INDENT count_freq2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( count_freq1 > 1 and count_freq2 > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " abcbc " NEW_LINE if ( isValidString ( str ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static int n = 4 ; static int count ( int arr [ ] [ ] ) { int diag1 = 0 , diag2 = 0 ; int row = 0 , col = 0 , count = 0 ; for ( int i = 0 , j = n - 1 ; i < n ; i ++ , j -- ) { diag1 += arr [ i ] [ i ] ; diag2 += arr [ i ] [ j ] ; } for ( int i = 0 ; i < n ; i ++ ) { row = 0 ; col = 0 ; for ( int j = 0 ; j < n ; j ++ ) { row = row + arr [ i ] [ j ] ; } for ( int j = 0 ; j < n ; j ++ ) { col = col + arr [ j ] [ i ] ; } if ( ( row == diag1 ) || ( row == diag2 ) ) count ++ ; if ( ( col == diag1 ) || ( col == diag2 ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 7 , 2 , 3 , 5 } , { 4 , 5 , 6 , 3 } , { 7 , 9 , 10 , 12 } , { 1 , 5 , 4 , 3 } } ; System . out . println ( count ( arr ) ) ; } }

n = 4 NEW_LINE def count ( arr ) : NEW_LINE INDENT diag1 = 0 ; diag2 = 0 ; row = 0 NEW_LINE col = 0 ; count = 0 ; j = n - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT diag1 += arr [ i ] [ i ] NEW_LINE diag2 += arr [ i ] [ j ] NEW_LINE j -= 1 NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT row = 0 ; col = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT row += arr [ i ] [ j ] NEW_LINE DEDENT for j in range ( n ) : NEW_LINE INDENT col += arr [ j ] [ i ] NEW_LINE DEDENT if ( ( row == diag1 ) or ( row == diag2 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( ( col == diag1 ) or ( col == diag2 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ [ 7 , 2 , 3 , 5 ] , [ 4 , 5 , 6 , 3 ] , [ 7 , 9 , 10 , 12 ] , [ 1 , 5 , 4 , 3 ] ] NEW_LINE print ( count ( arr ) ) NEW_LINE


class GFG { static final int MAX = 1000000 ; static boolean [ ] prime = new boolean [ MAX + 1 ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i <= MAX ; i ++ ) prime [ i ] = true ; prime [ 1 ] = false ; prime [ 0 ] = false ; for ( int p = 2 ; p * p <= MAX ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= MAX ; i += p ) prime [ i ] = false ; } } } static void solve ( int [ ] arr , int n , int k ) { int c = 0 ; long sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ arr [ i ] ] ) { c ++ ; if ( c % k == 0 ) { sum += arr [ i ] ; c = 0 ; } } } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { SieveOfEratosthenes ( ) ; int n = 5 , k = 2 ; int [ ] arr = { 2 , 3 , 5 , 7 , 11 } ; solve ( arr , n , k ) ; } }

def SieveOfEratosthenes ( ) : NEW_LINE INDENT prime [ 1 ] = False NEW_LINE prime [ 0 ] = False NEW_LINE p = 2 NEW_LINE while p * p <= MAX : NEW_LINE INDENT if prime [ p ] == True : NEW_LINE INDENT for i in range ( p * 2 , MAX + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT DEDENT def solve ( arr , n , k ) : NEW_LINE INDENT c = 0 NEW_LINE Sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT c += 1 NEW_LINE if c % k == 0 : NEW_LINE INDENT Sum += arr [ i ] NEW_LINE c = 0 NEW_LINE DEDENT DEDENT DEDENT print ( Sum ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT MAX = 1000000 NEW_LINE prime = [ True ] * ( MAX + 1 ) NEW_LINE SieveOfEratosthenes ( ) NEW_LINE n , k = 5 , 2 NEW_LINE arr = [ 2 , 3 , 5 , 7 , 11 ] NEW_LINE solve ( arr , n , k ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void find_idx ( int arr [ ] , int n ) { Vector answer = new Vector ( ) ; int sum = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( sum == arr [ i ] ) { answer . add ( i ) ; } sum += arr [ i ] ; } if ( answer . size ( ) == 0 ) { System . out . println ( " - 1" ) ; return ; } for ( int i = answer . size ( ) - 1 ; i >= 0 ; i -- ) System . out . print ( answer . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 36 , 2 , 17 , 6 , 6 , 5 } ; int n = arr . length ; find_idx ( arr , n ) ; } }

def find_idx ( arr , n ) : NEW_LINE INDENT answer = [ ] NEW_LINE _sum = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( _sum == arr [ i ] ) : NEW_LINE INDENT answer . append ( i ) NEW_LINE DEDENT _sum += arr [ i ] NEW_LINE DEDENT if ( len ( answer ) == 0 ) : NEW_LINE INDENT print ( - 1 ) NEW_LINE return NEW_LINE DEDENT for i in range ( len ( answer ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT print ( answer [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 36 , 2 , 17 , 6 , 6 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE find_idx ( arr , n ) NEW_LINE


import java . io . * ; class GFG { public static void floorAndCeil ( int arr [ ] , int x ) { int n = arr . length ; int fInd = - 1 , cInd = - 1 ; int fDist = Integer . MAX_VALUE , cDist = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] >= x && cDist > ( arr [ i ] - x ) ) { cInd = i ; cDist = arr [ i ] - x ; } if ( arr [ i ] <= x && fDist > ( x - arr [ i ] ) ) { fInd = i ; fDist = x - arr [ i ] ; } } if ( fDist == Integer . MAX_VALUE ) System . out . println ( " Floor ▁ doesn ' t ▁ exist ▁ " ) ; else System . out . println ( " Floor ▁ is ▁ " + arr [ fInd ] ) ; if ( cDist == Integer . MAX_VALUE ) System . out . println ( " Ceil ▁ doesn ' t ▁ exist ▁ " ) ; else System . out . println ( " Ceil ▁ is ▁ ▁ " + arr [ cInd ] ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 6 , 8 , 9 , 6 , 5 , 5 , 6 } ; int x = 7 ; floorAndCeil ( arr , x ) ; } }

import sys NEW_LINE def floorAndCeil ( arr , n , x ) : NEW_LINE INDENT fDist = sys . maxsize NEW_LINE cDist = sys . maxsize NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] >= x and cDist > ( arr [ i ] - x ) ) : NEW_LINE INDENT cInd = i NEW_LINE cDist = arr [ i ] - x NEW_LINE DEDENT if ( arr [ i ] <= x and fDist > ( x - arr [ i ] ) ) : NEW_LINE INDENT fInd = i NEW_LINE fDist = x - arr [ i ] NEW_LINE DEDENT DEDENT if ( fDist == sys . maxsize ) : NEW_LINE INDENT print ( " Floor ▁ doesn ' t ▁ exist ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Floor ▁ is ▁ " + str ( arr [ fInd ] ) ) NEW_LINE DEDENT if ( cDist == sys . maxsize ) : NEW_LINE INDENT print ( " Ceil ▁ doesn ' t ▁ exist ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Ceil ▁ is ▁ " + str ( arr [ cInd ] ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 6 , 8 , 9 , 6 , 5 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE x = 7 NEW_LINE floorAndCeil ( arr , n , x ) NEW_LINE DEDENT


public class GFG { static boolean isPrime ( int k ) { if ( k <= 1 ) return false ; for ( int i = 2 ; i < k ; i ++ ) if ( k % i == 0 ) return false ; return true ; } static int check ( int num , int k ) { int flag = 1 ; for ( int i = 2 ; i < k ; i ++ ) { if ( num % i == 0 ) flag = 0 ; } if ( flag == 1 ) { if ( num % k == 0 ) return 1 ; else return 0 ; } else return 0 ; } static int findCount ( int a , int b , int k ) { int count = 0 ; if ( ! isPrime ( k ) ) return 0 ; else { int ans ; for ( int i = a ; i <= b ; i ++ ) { ans = check ( i , k ) ; if ( ans == 1 ) count ++ ; else continue ; } } return count ; } public static void main ( String args [ ] ) { int a = 2020 , b = 6300 , k = 29 ; System . out . println ( findCount ( a , b , k ) ) ; } }

def isPrime ( k ) : NEW_LINE INDENT if ( k <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , k ) : NEW_LINE INDENT if ( k % i == 0 ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def check ( num , k ) : NEW_LINE INDENT flag = 1 NEW_LINE for i in range ( 2 , k ) : NEW_LINE INDENT if ( num % i == 0 ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( flag == 1 ) : NEW_LINE INDENT if ( num % k == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def findCount ( a , b , k ) : NEW_LINE INDENT count = 0 NEW_LINE if ( not isPrime ( k ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( a , b + 1 ) : NEW_LINE INDENT ans = check ( i , k ) NEW_LINE if ( ans == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2020 NEW_LINE b = 6300 NEW_LINE k = 29 NEW_LINE print ( findCount ( a , b , k ) ) NEW_LINE DEDENT


class GFG { static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) { if ( n % i == 0 ) return false ; } return true ; } static void countPrimePosition ( int arr [ ] ) { int c0 = 0 , c1 = 0 ; int n = arr . length ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 0 && isPrime ( i ) ) c0 ++ ; if ( arr [ i ] == 1 && isPrime ( i ) ) c1 ++ ; } System . out . println ( " Number ▁ of ▁ 0s ▁ = ▁ " + c0 ) ; System . out . println ( " Number ▁ of ▁ 1s ▁ = ▁ " + c1 ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 1 , 0 , 1 , 0 , 1 } ; countPrimePosition ( arr ) ; } }

def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def countPrimePosition ( arr ) : NEW_LINE INDENT c0 = 0 ; c1 = 0 ; NEW_LINE n = len ( arr ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == 0 and isPrime ( i ) ) : NEW_LINE INDENT c0 += 1 ; NEW_LINE DEDENT if ( arr [ i ] == 1 and isPrime ( i ) ) : NEW_LINE INDENT c1 += 1 ; NEW_LINE DEDENT DEDENT print ( " Number ▁ of ▁ 0s ▁ = " , c0 ) ; NEW_LINE print ( " Number ▁ of ▁ 1s ▁ = " , c1 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 0 , 1 , 0 , 1 ] ; NEW_LINE countPrimePosition ( arr ) ; NEW_LINE DEDENT


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

def avgOfFirstN ( n ) : NEW_LINE INDENT return ( float ) ( 1 + n ) / 2 ; NEW_LINE DEDENT n = 20 NEW_LINE print ( avgOfFirstN ( n ) ) NEW_LINE


class Solution { static long getSum ( int n , int d ) { long sum = 0 ; while ( d <= n ) { sum += d ; d += 10 ; } return sum ; } public static void main ( String args [ ] ) { int n = 30 , d = 3 ; System . out . print ( getSum ( n , d ) ) ; } }

def getSum ( n , d ) : NEW_LINE INDENT sum = 0 NEW_LINE while ( d <= n ) : NEW_LINE INDENT sum += d NEW_LINE d += 10 NEW_LINE DEDENT return sum NEW_LINE DEDENT n = 30 NEW_LINE d = 3 NEW_LINE print ( getSum ( n , d ) ) NEW_LINE


import java . util . * ; class GFG1 { static int KthMinValAfterMconcatenate ( int [ ] A , int N , int M , int K ) { Arrays . sort ( A ) ; return ( A [ ( ( K - 1 ) / M ) ] ) ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 1 , 2 } ; int M = 3 , K = 4 ; int N = A . length ; System . out . println ( KthMinValAfterMconcatenate ( A , N , M , K ) ) ; } }

def KthMinValAfterMconcatenate ( A , N , M , K ) : NEW_LINE INDENT V = [ ] NEW_LINE for i in range ( 0 , M ) : NEW_LINE INDENT for j in range ( 0 , N ) : NEW_LINE INDENT V . append ( A [ j ] ) NEW_LINE DEDENT DEDENT V . sort ( ) NEW_LINE return V [ K - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 1 , 2 ] NEW_LINE M , K = 3 , 4 NEW_LINE N = len ( A ) NEW_LINE print ( KthMinValAfterMconcatenate ( A , N , M , K ) ) NEW_LINE DEDENT


class GFG { static String encryptString ( String str , int n ) { int i = 0 , cnt = 0 ; String encryptedStr = " " ; while ( i < n ) { cnt = i + 1 ; while ( cnt -- > 0 ) encryptedStr += str . charAt ( i ) ; i ++ ; } return encryptedStr ; } public static void main ( String [ ] args ) { String str = " geeks " ; int n = str . length ( ) ; System . out . println ( encryptString ( str , n ) ) ; } }

def encryptString ( string , n ) : NEW_LINE INDENT i , cnt = 0 , 0 NEW_LINE encryptedStr = " " NEW_LINE while i < n : NEW_LINE INDENT cnt = i + 1 NEW_LINE while cnt > 0 : NEW_LINE INDENT encryptedStr += string [ i ] NEW_LINE cnt -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return encryptedStr NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeks " NEW_LINE n = len ( string ) NEW_LINE print ( encryptString ( string , n ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int factorial ( int n ) { if ( n == 0 ) return 1 ; return n * factorial ( n - 1 ) ; } static int LCMOfNeighbourFact ( int n ) { return factorial ( n + 1 ) ; } public static void main ( String args [ ] ) { int N = 5 ; System . out . println ( LCMOfNeighbourFact ( N ) ) ; } }

def factorial ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return n * factorial ( n - 1 ) NEW_LINE DEDENT def LCMOfNeighbourFact ( n ) : NEW_LINE INDENT return factorial ( n + 1 ) NEW_LINE DEDENT N = 5 NEW_LINE print ( LCMOfNeighbourFact ( N ) ) NEW_LINE


class GFG { static float Area ( float a ) { if ( a < 0 ) return - 1 ; float x = 0.464f * a ; float A = 0.70477f * ( float ) Math . pow ( x , 2 ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 ; System . out . println ( String . format ( " % .5f " , Area ( a ) ) ) ; } }

import math as mt NEW_LINE def Area ( a ) : NEW_LINE INDENT if ( a < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT x = 0.464 * a NEW_LINE A = 0.70477 * pow ( x , 2 ) NEW_LINE return A NEW_LINE DEDENT a = 5 NEW_LINE print ( Area ( a ) ) NEW_LINE


class GFG { static int sizeofInt = 8 ; static int toggleLastMBits ( int n , int m ) { int num = ( 1 << m ) - 1 ; return ( n ^ num ) ; } static int unsetLastMBits ( int n , int m ) { int num = ( 1 << ( sizeofInt * 8 - 1 ) ) - 1 ; num = toggleLastMBits ( num , m ) ; return ( n & num ) ; } public static void main ( String [ ] args ) { int n = 150 , m = 4 ; System . out . println ( unsetLastMBits ( n , m ) ) ; } }

import sys NEW_LINE def toggleLastMBits ( n , m ) : NEW_LINE INDENT num = ( 1 << m ) - 1 NEW_LINE return ( n ^ num ) NEW_LINE DEDENT def unsetLastMBits ( n , m ) : NEW_LINE INDENT num = ( 1 << ( sys . getsizeof ( int ) * 8 - 1 ) ) - 1 NEW_LINE num = toggleLastMBits ( num , m ) NEW_LINE return ( n & num ) NEW_LINE DEDENT n = 150 NEW_LINE m = 4 NEW_LINE print ( unsetLastMBits ( n , m ) ) NEW_LINE


class GFG { static int cntWays ( int n ) { if ( n % 2 == 1 ) { return 0 ; } else { return ( n - 2 ) / 4 ; } } public static void main ( String [ ] args ) { int n = 18 ; System . out . println ( cntWays ( n ) ) ; } }

def cntWays ( n ) : NEW_LINE INDENT if n % 2 == 1 : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return ( n - 2 ) // 4 NEW_LINE DEDENT DEDENT n = 18 NEW_LINE print ( cntWays ( n ) ) NEW_LINE


class GFG { static class Node { int data ; Node next ; } ; static Node addToEmpty ( Node head , int data ) { if ( head != null ) return head ; Node temp = new Node ( ) ; temp . data = data ; head = temp ; head . next = head ; return head ; } static Node addBegin ( Node head , int data ) { if ( head == null ) return addToEmpty ( head , data ) ; Node temp = new Node ( ) ; temp . data = data ; temp . next = head . next ; head . next = temp ; return head ; } static void traverse ( Node head ) { Node p ; if ( head == null ) { System . out . print ( " List ▁ is ▁ empty . " ) ; return ; } p = head ; do { System . out . print ( p . data + " ▁ " ) ; p = p . next ; } while ( p != head ) ; } static Node exchangeNodes ( Node head ) { Node p = head ; while ( p . next . next != head ) p = p . next ; p . next . next = head . next ; head . next = p . next ; p . next = head ; head = head . next ; return head ; } public static void main ( String args [ ] ) { int i ; Node head = null ; head = addToEmpty ( head , 6 ) ; for ( i = 5 ; i > 0 ; i -- ) head = addBegin ( head , i ) ; System . out . print ( " List ▁ Before : ▁ " ) ; traverse ( head ) ; System . out . println ( ) ; System . out . print ( " List ▁ After : ▁ " ) ; head = exchangeNodes ( head ) ; traverse ( head ) ; } }

import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def addToEmpty ( head , data ) : NEW_LINE INDENT if ( head != None ) : NEW_LINE INDENT return head NEW_LINE DEDENT temp = Node ( data ) NEW_LINE temp . data = data NEW_LINE head = temp NEW_LINE head . next = head NEW_LINE return head NEW_LINE DEDENT def addBegin ( head , data ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return addToEmpty ( head , data ) NEW_LINE DEDENT temp = Node ( data ) NEW_LINE temp . data = data NEW_LINE temp . next = head . next NEW_LINE head . next = temp NEW_LINE return head NEW_LINE DEDENT def traverse ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT print ( " List ▁ is ▁ empty . " ) NEW_LINE return NEW_LINE DEDENT p = head NEW_LINE print ( p . data , end = " ▁ " ) NEW_LINE p = p . next NEW_LINE while ( p != head ) : NEW_LINE INDENT print ( p . data , end = " ▁ " ) NEW_LINE p = p . next NEW_LINE DEDENT DEDENT def exchangeNodes ( head ) : NEW_LINE INDENT p = head NEW_LINE while ( p . next . next != head ) : NEW_LINE INDENT p = p . next NEW_LINE DEDENT p . next . next = head . next NEW_LINE head . next = p . next NEW_LINE p . next = head NEW_LINE head = head . next NEW_LINE return head NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = addToEmpty ( head , 6 ) NEW_LINE for x in range ( 5 , 0 , - 1 ) : NEW_LINE INDENT head = addBegin ( head , x ) NEW_LINE DEDENT print ( " List ▁ Before : ▁ " , end = " " ) NEW_LINE traverse ( head ) NEW_LINE print ( ) NEW_LINE print ( " List ▁ After : ▁ " , end = " " ) NEW_LINE head = exchangeNodes ( head ) NEW_LINE traverse ( head ) NEW_LINE DEDENT


class Test { static void printCubes ( int a , int b ) { int acrt = ( int ) Math . cbrt ( a ) ; int bcrt = ( int ) Math . cbrt ( b ) ; for ( int i = acrt ; i <= bcrt ; i ++ ) if ( i * i * i >= a && i * i * i <= b ) System . out . print ( i * i * i + " ▁ " ) ; } public static void main ( String [ ] args ) { int a = 24 , b = 576 ; System . out . println ( " Perfect ▁ cubes ▁ in ▁ given ▁ range : " ) ; printCubes ( a , b ) ; } }

def cbrt ( n ) : NEW_LINE INDENT return ( int ) ( n ** ( 1. / 3 ) ) NEW_LINE DEDENT def printCubes ( a , b ) : NEW_LINE INDENT acrt = cbrt ( a ) NEW_LINE bcrt = cbrt ( b ) NEW_LINE for i in range ( acrt , bcrt + 1 ) : NEW_LINE INDENT if ( i * i * i >= a and i * i * i <= b ) : NEW_LINE INDENT print ( i * i * i , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT a = 24 NEW_LINE b = 576 NEW_LINE print ( " Perfect ▁ cubes ▁ in ▁ given ▁ range : " ) NEW_LINE printCubes ( a , b ) NEW_LINE


import java . io . * ; public class GFG { static int countWays ( int num ) { int [ ] dp = new int [ num + 1 ] ; int MOD = ( int ) 1E9 + 7 ; dp [ 1 ] = 2 ; for ( int i = 2 ; i <= num ; ++ i ) { dp [ i ] = 0 ; for ( int j = 1 ; j <= 3 ; ++ j ) { if ( i - j == 0 ) dp [ i ] += 1 ; else if ( j == 1 ) dp [ i ] += dp [ i - j ] * 2 ; else if ( i - j > 0 ) dp [ i ] += dp [ i - j ] ; if ( dp [ i ] >= MOD ) dp [ i ] %= MOD ; } } return dp [ num ] ; } static public void main ( String [ ] args ) { int n = 3 ; System . out . println ( countWays ( n ) ) ; } }

def countWays ( num ) : NEW_LINE INDENT dp = [ 0 ] * ( num + 1 ) ; NEW_LINE MOD = 100000000 + 7 ; NEW_LINE dp [ 1 ] = 2 ; NEW_LINE for i in range ( 2 , num + 1 ) : NEW_LINE INDENT dp [ i ] = 0 ; NEW_LINE for j in range ( 1 , 4 ) : NEW_LINE INDENT if ( i - j == 0 ) : NEW_LINE INDENT dp [ i ] += 1 ; NEW_LINE DEDENT elif ( j == 1 ) : NEW_LINE INDENT dp [ i ] += dp [ i - j ] * 2 ; NEW_LINE DEDENT elif ( i - j > 0 ) : NEW_LINE INDENT dp [ i ] += dp [ i - j ] ; NEW_LINE DEDENT DEDENT if ( dp [ i ] >= MOD ) : NEW_LINE INDENT dp [ i ] %= MOD ; NEW_LINE DEDENT DEDENT return dp [ num ] ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( countWays ( n ) ) ; NEW_LINE


import java . util . * ; class GfG { static class Node { int data ; Node left = null ; Node right = null ; } static Node newNode ( int val ) { Node temp = new Node ( ) ; temp . data = val ; temp . left = null ; temp . right = null ; return temp ; } static void largestValueInEachLevel ( Node root ) { if ( root == null ) return ; Queue < Node > q = new LinkedList < Node > ( ) ; int nc , max ; q . add ( root ) ; while ( true ) { nc = q . size ( ) ; if ( nc == 0 ) break ; max = Integer . MIN_VALUE ; while ( nc != 0 ) { Node front = q . peek ( ) ; q . remove ( ) ; if ( max < front . data ) max = front . data ; if ( front . left != null ) q . add ( front . left ) ; if ( front . right != null ) q . add ( front . right ) ; nc -- ; } System . out . println ( max + " ▁ " ) ; } } public static void main ( String [ ] args ) { Node root = null ; root = newNode ( 4 ) ; root . left = newNode ( 9 ) ; root . right = newNode ( 2 ) ; root . left . left = newNode ( 3 ) ; root . left . right = newNode ( 5 ) ; root . right . right = newNode ( 7 ) ; largestValueInEachLevel ( root ) ; } }

INT_MIN = - 2147483648 NEW_LINE class newNode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def largestValueInEachLevel ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return NEW_LINE DEDENT q = [ ] NEW_LINE nc = 10 NEW_LINE max = 0 NEW_LINE q . append ( root ) NEW_LINE while ( 1 ) : NEW_LINE INDENT nc = len ( q ) NEW_LINE if ( nc == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT max = INT_MIN NEW_LINE while ( nc ) : NEW_LINE INDENT front = q [ 0 ] NEW_LINE q = q [ 1 : ] NEW_LINE if ( max < front . data ) : NEW_LINE INDENT max = front . data NEW_LINE DEDENT if ( front . left ) : NEW_LINE INDENT q . append ( front . left ) NEW_LINE DEDENT if ( front . right != None ) : NEW_LINE INDENT q . append ( front . right ) NEW_LINE DEDENT nc -= 1 NEW_LINE DEDENT print ( max , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 4 ) NEW_LINE root . left = newNode ( 9 ) NEW_LINE root . right = newNode ( 2 ) NEW_LINE root . left . left = newNode ( 3 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE largestValueInEachLevel ( root ) NEW_LINE DEDENT


class GFG { static void sortDiagonal ( int a [ ] [ ] , int M , int N ) { for ( int i = 0 ; i < M ; i ++ ) { int sm = a [ i ] [ i ] ; int pos = i ; for ( int j = i + 1 ; j < N ; j ++ ) { if ( sm > a [ j ] [ j ] ) { sm = a [ j ] [ j ] ; pos = j ; } } swap ( a , i , i , pos , pos ) ; } for ( int i = 0 ; i < M ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( a [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } static void swap ( int [ ] [ ] a , int i , int i2 , int pos , int pos2 ) { int temp = a [ i ] [ i2 ] ; a [ i ] [ i2 ] = a [ pos ] [ pos2 ] ; a [ pos ] [ pos2 ] = temp ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 4 , 2 } , { 3 , 1 } } ; sortDiagonal ( a , 2 , 2 ) ; } }

def sortDiagonal ( a , M , N ) : NEW_LINE INDENT for i in range ( M ) : NEW_LINE INDENT sm = a [ i ] [ i ] NEW_LINE pos = i NEW_LINE for j in range ( i + 1 , N ) : NEW_LINE INDENT if ( sm > a [ j ] [ j ] ) : NEW_LINE INDENT sm = a [ j ] [ j ] NEW_LINE pos = j NEW_LINE DEDENT DEDENT a [ i ] [ i ] , a [ pos ] [ pos ] = a [ pos ] [ pos ] , a [ i ] [ i ] NEW_LINE DEDENT for i in range ( M ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( a [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT a = [ [ 4 , 2 ] , [ 3 , 1 ] ] NEW_LINE sortDiagonal ( a , 2 , 2 ) NEW_LINE


class GFG { static int __gcd ( int a , int b ) { return b == 0 ? a : __gcd ( b , a % b ) ; } static int digitGCD ( int n ) { int gcd = 0 ; while ( n > 0 ) { gcd = __gcd ( n % 10 , gcd ) ; if ( gcd == 1 ) return 1 ; n = n / 10 ; } return gcd ; } public static void main ( String [ ] args ) { int n = 2448 ; System . out . print ( digitGCD ( n ) ) ; } }

def __gcd ( a , b ) : NEW_LINE INDENT return a if ( b == 0 ) else __gcd ( b , a % b ) NEW_LINE DEDENT def digitGCD ( n ) : NEW_LINE INDENT gcd = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT gcd = __gcd ( n % 10 , gcd ) NEW_LINE if ( gcd == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT n = n // 10 NEW_LINE DEDENT return gcd NEW_LINE DEDENT n = 2448 NEW_LINE print ( digitGCD ( n ) ) NEW_LINE


class GFG { static void removeChar ( String s , char c ) { int j , count = 0 , n = s . length ( ) ; char [ ] t = s . toCharArray ( ) ; for ( int i = j = 0 ; i < n ; i ++ ) { if ( t [ i ] != c ) t [ j ++ ] = t [ i ] ; else count ++ ; } while ( count > 0 ) { t [ j ++ ] = ' \0' ; count -- ; } System . out . println ( t ) ; } public static void main ( String [ ] args ) { String s = " geeksforgeeks " ; removeChar ( s , ' g ' ) ; } }

def removeChar ( s , c ) : NEW_LINE INDENT counts = s . count ( c ) NEW_LINE s = list ( s ) NEW_LINE while counts : NEW_LINE INDENT s . remove ( c ) NEW_LINE counts -= 1 NEW_LINE DEDENT s = ' ' . join ( s ) NEW_LINE print ( s ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE removeChar ( s , ' g ' ) NEW_LINE DEDENT


import java . io . * ; import java . util . HashMap ; class OddOccurrence { static int getOddOccurrence ( int arr [ ] , int n ) { HashMap < Integer , Integer > hmap = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( hmap . containsKey ( arr [ i ] ) ) { int val = hmap . get ( arr [ i ] ) ; hmap . put ( arr [ i ] , val + 1 ) ; } else hmap . put ( arr [ i ] , 1 ) ; } for ( Integer a : hmap . keySet ( ) ) { if ( hmap . get ( a ) % 2 != 0 ) return a ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 } ; int n = arr . length ; System . out . println ( getOddOccurrence ( arr , n ) ) ; } }

def getOddOccurrence ( arr , size ) : NEW_LINE INDENT Hash = dict ( ) NEW_LINE for i in range ( size ) : NEW_LINE INDENT Hash [ arr [ i ] ] = Hash . get ( arr [ i ] , 0 ) + 1 ; NEW_LINE DEDENT for i in Hash : NEW_LINE INDENT if ( Hash [ i ] % 2 != 0 ) : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 4 , 5 , 2 , 4 , 3 , 5 , 2 , 4 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getOddOccurrence ( arr , n ) ) NEW_LINE


import java . util . * ; class GFG { static int xorOdd ( int arr [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } } int xorArr = 0 ; for ( Map . Entry < Integer , Integer > it : mp . entrySet ( ) ) { if ( ( ( it . getValue ( ) ) % 2 ) == 1 ) { xorArr ^= it . getKey ( ) ; } } return xorArr ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 1 , 3 , 3 , 4 , 2 , 3 , 1 } ; int n = arr . length ; System . out . println ( xorOdd ( arr , n ) ) ; } }

def xorOdd ( arr , n ) : NEW_LINE INDENT m = dict . fromkeys ( arr , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] += 1 ; NEW_LINE DEDENT xorArr = 0 ; NEW_LINE for key , value in m . items ( ) : NEW_LINE INDENT if ( value & 1 ) : NEW_LINE INDENT xorArr ^= key ; NEW_LINE DEDENT DEDENT return xorArr ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 1 , 3 , 3 , 4 , 2 , 3 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( xorOdd ( arr , n ) ) ; NEW_LINE DEDENT


import java . lang . * ; class GFG { static float findWater ( int i , int j , float X ) { if ( j > i ) { System . out . println ( " Incorrect ▁ Input " ) ; System . exit ( 0 ) ; } int ll = Math . round ( ( i * ( i + 1 ) ) ) ; float [ ] glass = new float [ ll + 2 ] ; int index = 0 ; glass [ index ] = X ; for ( int row = 1 ; row <= i ; ++ row ) { for ( int col = 1 ; col <= row ; ++ col , ++ index ) { X = glass [ index ] ; glass [ index ] = ( X >= 1.0f ) ? 1.0f : X ; X = ( X >= 1.0f ) ? ( X - 1 ) : 0.0f ; glass [ index + row ] += X / 2 ; glass [ index + row + 1 ] += X / 2 ; } } return glass [ ( int ) ( i * ( i - 1 ) / 2 + j - 1 ) ] ; } public static void main ( String [ ] args ) { int i = 2 , j = 2 ; float X = 2.0f ; System . out . println ( " Amount ▁ of ▁ water ▁ in ▁ jth ▁ " + " glass ▁ of ▁ ith ▁ row ▁ is : ▁ " + findWater ( i , j , X ) ) ; } }

def findWater ( i , j , X ) : NEW_LINE INDENT if ( j > i ) : NEW_LINE INDENT print ( " Incorrect ▁ Input " ) ; NEW_LINE return ; NEW_LINE DEDENT glass = [ 0 ] * int ( i * ( i + 1 ) / 2 ) ; NEW_LINE index = 0 ; NEW_LINE glass [ index ] = X ; NEW_LINE for row in range ( 1 , i ) : NEW_LINE INDENT for col in range ( 1 , row + 1 ) : NEW_LINE INDENT X = glass [ index ] ; NEW_LINE glass [ index ] = 1.0 if ( X >= 1.0 ) else X ; NEW_LINE X = ( X - 1 ) if ( X >= 1.0 ) else 0.0 ; NEW_LINE glass [ index + row ] += ( X / 2 ) ; NEW_LINE glass [ index + row + 1 ] += ( X / 2 ) ; NEW_LINE index += 1 ; NEW_LINE DEDENT DEDENT return glass [ int ( i * ( i - 1 ) / 2 + j - 1 ) ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT i = 2 ; NEW_LINE j = 2 ; NEW_LINE X = 2.0 ; NEW_LINE res = repr ( findWater ( i , j , X ) ) ; NEW_LINE print ( " Amount ▁ of ▁ water ▁ in ▁ jth ▁ glass ▁ of ▁ ith ▁ row ▁ is : " , res . ljust ( 8 , '0' ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static double cyl ( double r , double h ) { if ( r < 0 && h < 0 ) return - 1 ; double R = ( 2 * r ) / 3 ; double H = ( 2 * h ) / 3 ; double V = 3.14 * Math . pow ( R , 2 ) * H ; return V ; } public static void main ( String [ ] args ) { double r = 4 , h = 8 ; System . out . println ( cyl ( r , h ) ) ; } }

import math NEW_LINE def cyl ( r , h ) : NEW_LINE INDENT if ( r < 0 and h < 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT R = ( 2 * r ) / 3 NEW_LINE H = ( 2 * h ) / 3 NEW_LINE V = 3.14 * math . pow ( R , 2 ) * H NEW_LINE return V NEW_LINE DEDENT r = 4 ; h = 8 ; NEW_LINE print ( cyl ( r , h ) , " \n " ) NEW_LINE


import java . io . * ; class GFG { static int countBits ( int n ) { int count = 0 ; while ( n > 0 ) { count ++ ; n >>= 1 ; } return count ; } public static void main ( String [ ] args ) { int n = 32 ; System . out . println ( " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = ▁ " + countBits ( n ) ) ; } }

def countBits ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT count += 1 ; NEW_LINE n >>= 1 ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT n = 32 ; NEW_LINE print ( " Minimum ▁ value ▁ of ▁ K ▁ is ▁ = " , countBits ( n ) ) ; NEW_LINE


class GFG { static void minAdjDifference ( int arr [ ] , int n ) { if ( n < 2 ) return ; int res = Math . abs ( arr [ 1 ] - arr [ 0 ] ) ; for ( int i = 2 ; i < n ; i ++ ) res = Math . min ( res , Math . abs ( arr [ i ] - arr [ i - 1 ] ) ) ; res = Math . min ( res , Math . abs ( arr [ n - 1 ] - arr [ 0 ] ) ) ; System . out . print ( " Min ▁ Difference ▁ = ▁ " + res ) ; } public static void main ( String arg [ ] ) { int a [ ] = { 10 , 12 , 13 , 15 , 10 } ; int n = a . length ; minAdjDifference ( a , n ) ; } }

def minAdjDifference ( arr , n ) : NEW_LINE INDENT if ( n < 2 ) : return NEW_LINE res = abs ( arr [ 1 ] - arr [ 0 ] ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT res = min ( res , abs ( arr [ i ] - arr [ i - 1 ] ) ) NEW_LINE DEDENT res = min ( res , abs ( arr [ n - 1 ] - arr [ 0 ] ) ) NEW_LINE print ( " Min ▁ Difference ▁ = ▁ " , res ) NEW_LINE DEDENT a = [ 10 , 12 , 13 , 15 , 10 ] NEW_LINE n = len ( a ) NEW_LINE minAdjDifference ( a , n ) NEW_LINE


import java . io . * ; class GFG { static int findMinCost ( int arr [ ] , int n , int choc_cost ) { int choc_buy = arr [ 0 ] ; int curr_choc = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int choc = arr [ i ] - arr [ i + 1 ] ; curr_choc += choc ; if ( curr_choc < 0 ) { choc_buy += Math . abs ( curr_choc ) ; curr_choc = 0 ; } } return choc_buy * choc_cost ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 6 , 11 , 4 , 7 , 1 } ; int n = arr . length ; int p = 5 ; System . out . println ( findMinCost ( arr , n , p ) ) ; } }

def findMinCost ( arr , n , choc_cost ) : NEW_LINE INDENT choc_buy = arr [ 0 ] NEW_LINE curr_choc = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT choc = arr [ i ] - arr [ i + 1 ] NEW_LINE curr_choc += choc NEW_LINE if ( curr_choc < 0 ) : NEW_LINE INDENT choc_buy += abs ( curr_choc ) NEW_LINE curr_choc = 0 NEW_LINE DEDENT DEDENT return choc_buy * choc_cost NEW_LINE DEDENT arr = [ 10 , 6 , 11 , 4 , 7 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE p = 5 NEW_LINE print ( findMinCost ( arr , n , p ) ) NEW_LINE


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

def countSub ( arr , n ) : NEW_LINE INDENT count = [ 0 for i in range ( 10 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( arr [ i ] - 1 , - 1 , - 1 ) : NEW_LINE INDENT count [ arr [ i ] ] += count [ j ] NEW_LINE DEDENT count [ arr [ i ] ] += 1 NEW_LINE DEDENT result = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT result += count [ i ] NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 3 , 2 , 4 , 5 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countSub ( arr , n ) ) NEW_LINE


class GFG { static int MAX = 100 ; static void spiralFill ( int m , int n , int a [ ] [ ] ) { int val = 1 ; int k = 0 , l = 0 ; while ( k < m && l < n ) { for ( int i = l ; i < n ; ++ i ) { a [ k ] [ i ] = val ++ ; } k ++ ; for ( int i = k ; i < m ; ++ i ) { a [ i ] [ n - 1 ] = val ++ ; } n -- ; if ( k < m ) { for ( int i = n - 1 ; i >= l ; -- i ) { a [ m - 1 ] [ i ] = val ++ ; } m -- ; } if ( l < n ) { for ( int i = m - 1 ; i >= k ; -- i ) { a [ i ] [ l ] = val ++ ; } l ++ ; } } } public static void main ( String [ ] args ) { int m = 4 , n = 4 ; int a [ ] [ ] = new int [ MAX ] [ MAX ] ; spiralFill ( m , n , a ) ; for ( int i = 0 ; i < m ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) { System . out . print ( a [ i ] [ j ] + " ▁ " ) ; } System . out . println ( " " ) ; } } }

def spiralFill ( m , n , a ) : NEW_LINE INDENT val = 1 NEW_LINE k , l = 0 , 0 NEW_LINE while ( k < m and l < n ) : NEW_LINE INDENT for i in range ( l , n ) : NEW_LINE INDENT a [ k ] [ i ] = val NEW_LINE val += 1 NEW_LINE DEDENT k += 1 NEW_LINE for i in range ( k , m ) : NEW_LINE INDENT a [ i ] [ n - 1 ] = val NEW_LINE val += 1 NEW_LINE DEDENT n -= 1 NEW_LINE if ( k < m ) : NEW_LINE INDENT for i in range ( n - 1 , l - 1 , - 1 ) : NEW_LINE INDENT a [ m - 1 ] [ i ] = val NEW_LINE val += 1 NEW_LINE DEDENT m -= 1 NEW_LINE DEDENT if ( l < n ) : NEW_LINE INDENT for i in range ( m - 1 , k - 1 , - 1 ) : NEW_LINE INDENT a [ i ] [ l ] = val NEW_LINE val += 1 NEW_LINE DEDENT l += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT m , n = 4 , 4 NEW_LINE a = [ [ 0 for j in range ( m ) ] for i in range ( n ) ] NEW_LINE spiralFill ( m , n , a ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( a [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ' ' ) NEW_LINE DEDENT DEDENT


class GFG { static class Node { int data ; Node next ; } ; static Node circular ( Node head ) { Node start = head ; while ( head . next != null ) head = head . next ; head . next = start ; return start ; } static Node push ( Node head , int data ) { Node newNode = new Node ( ) ; newNode . data = data ; newNode . next = ( head ) ; ( head ) = newNode ; return head ; } static void displayList ( Node node ) { Node start = node ; while ( node . next != start ) { System . out . print ( " ▁ " + node . data ) ; node = node . next ; } System . out . print ( " ▁ " + node . data ) ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 15 ) ; head = push ( head , 14 ) ; head = push ( head , 13 ) ; head = push ( head , 22 ) ; head = push ( head , 17 ) ; circular ( head ) ; System . out . print ( " Display ▁ list : ▁ \n " ) ; displayList ( head ) ; } }

import sys 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 push ( head , data ) : NEW_LINE INDENT if not head : NEW_LINE INDENT return Node ( data ) NEW_LINE DEDENT newNode = Node ( data ) NEW_LINE newNode . next = head NEW_LINE head = newNode NEW_LINE return head NEW_LINE DEDENT def circular ( head ) : NEW_LINE INDENT start = head NEW_LINE while ( head . next is not None ) : NEW_LINE INDENT head = head . next NEW_LINE DEDENT head . next = start NEW_LINE return start NEW_LINE DEDENT def displayList ( node ) : NEW_LINE INDENT start = node NEW_LINE while ( node . next is not start ) : NEW_LINE INDENT print ( " { } ▁ " . format ( node . data ) , end = " " ) NEW_LINE node = node . next NEW_LINE DEDENT print ( " { } ▁ " . format ( node . data ) , end = " " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 15 ) NEW_LINE head = push ( head , 14 ) NEW_LINE head = push ( head , 13 ) NEW_LINE head = push ( head , 22 ) NEW_LINE head = push ( head , 17 ) NEW_LINE circular ( head ) NEW_LINE print ( " Display ▁ List : " ) NEW_LINE displayList ( head ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int findAnswer ( int n , int [ ] arr ) { Arrays . sort ( arr ) ; int sum = 0 ; for ( int i = 0 ; i < n / 2 ; ++ i ) { sum += ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ; } return sum ; } public static void main ( String [ ] args ) { int [ ] arr = { 53 , 28 , 143 , 5 } ; int n = arr . length ; System . out . println ( findAnswer ( n , arr ) ) ; } }

def findAnswer ( n , arr ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE sum = 0 NEW_LINE for i in range ( int ( n / 2 ) ) : NEW_LINE INDENT sum += ( ( arr [ i ] + arr [ n - i - 1 ] ) * ( arr [ i ] + arr [ n - i - 1 ] ) ) NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 53 , 28 , 143 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findAnswer ( n , arr ) ) NEW_LINE DEDENT


public class GFG { static int minSwaps ( int arr [ ] , int n ) { int numberOfOnes = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) numberOfOnes ++ ; } int x = numberOfOnes ; int count_ones = 0 , maxOnes ; for ( int i = 0 ; i < x ; i ++ ) { if ( arr [ i ] == 1 ) count_ones ++ ; } maxOnes = count_ones ; for ( int i = 1 ; i <= n - x ; i ++ ) { if ( arr [ i - 1 ] == 1 ) count_ones -- ; if ( arr [ i + x - 1 ] == 1 ) count_ones ++ ; if ( maxOnes < count_ones ) maxOnes = count_ones ; } int numberOfZeroes = x - maxOnes ; return numberOfZeroes ; } public static void main ( String args [ ] ) { int a [ ] = new int [ ] { 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 , 1 } ; int n = a . length ; System . out . println ( minSwaps ( a , n ) ) ; } }

def minSwaps ( arr , n ) : NEW_LINE INDENT numberOfOnes = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT numberOfOnes = numberOfOnes + 1 NEW_LINE DEDENT DEDENT x = numberOfOnes NEW_LINE count_ones = 0 NEW_LINE maxOnes = 0 NEW_LINE for i in range ( 0 , x ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT count_ones = count_ones + 1 NEW_LINE DEDENT DEDENT maxOnes = count_ones NEW_LINE for i in range ( 1 , ( n - x + 1 ) ) : NEW_LINE INDENT if ( arr [ i - 1 ] == 1 ) : NEW_LINE INDENT count_ones = count_ones - 1 NEW_LINE DEDENT if ( arr [ i + x - 1 ] == 1 ) : NEW_LINE INDENT count_ones = count_ones + 1 NEW_LINE DEDENT if ( maxOnes < count_ones ) : NEW_LINE INDENT maxOnes = count_ones NEW_LINE DEDENT DEDENT numberOfZeroes = x - maxOnes NEW_LINE return numberOfZeroes NEW_LINE DEDENT a = [ 0 , 0 , 1 , 0 , 1 , 1 , 0 , 0 , 1 ] NEW_LINE n = 9 NEW_LINE print ( minSwaps ( a , n ) ) NEW_LINE


class GFG { static void printRepeatingEven ( int arr [ ] , int n ) { long _xor = 0L ; long pos ; for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; _xor ^= pos ; } for ( int i = 0 ; i < n ; ++ i ) { pos = 1 << arr [ i ] ; if ( ! ( ( pos & _xor ) != 0 ) ) { System . out . print ( arr [ i ] + " ▁ " ) ; _xor ^= pos ; } } } public static void main ( String args [ ] ) { int arr [ ] = { 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 } ; int n = arr . length ; printRepeatingEven ( arr , n ) ; } }

def printRepeatingEven ( arr , n ) : NEW_LINE INDENT axor = 0 ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT pos = 1 << arr [ i ] ; NEW_LINE axor ^= pos ; NEW_LINE DEDENT for i in range ( 0 , n - 1 ) : NEW_LINE INDENT pos = 1 << arr [ i ] ; NEW_LINE if ( not ( pos & axor ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE axor ^= pos ; NEW_LINE DEDENT DEDENT DEDENT arr = [ 9 , 12 , 23 , 10 , 12 , 12 , 15 , 23 , 14 , 12 , 15 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE printRepeatingEven ( arr , n ) ; NEW_LINE


import java . io . * ; class GFG { static int binomialCoeff ( int n , int k ) { int [ ] [ ] C = new int [ n + 1 ] [ k + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } static int maxcoefficientvalue ( int n ) { if ( n % 2 == 0 ) return binomialCoeff ( n , n / 2 ) ; else return binomialCoeff ( n , ( n + 1 ) / 2 ) ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( maxcoefficientvalue ( n ) ) ; } }

def binomialCoeff ( n , k ) : NEW_LINE INDENT C = [ [ 0 for x in range ( k + 1 ) ] for y in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] ; NEW_LINE DEDENT def maxcoefficientvalue ( n ) : NEW_LINE INDENT if ( n % 2 == 0 ) : NEW_LINE INDENT return binomialCoeff ( n , int ( n / 2 ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT return binomialCoeff ( n , int ( ( n + 1 ) / 2 ) ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 4 ; NEW_LINE print ( maxcoefficientvalue ( n ) ) ; NEW_LINE DEDENT


class GFG { static boolean canBeBalanced ( String s , int n ) { int count = 0 , count_1 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' ( ' ) count ++ ; else count -- ; count_1 = Math . min ( count_1 , count ) ; } if ( count_1 >= - 1 && count == 0 ) return true ; return false ; } public static void main ( String [ ] args ) { String s = " ( ) ) ( ) ( " ; int n = s . length ( ) ; if ( canBeBalanced ( s , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def canBeBalanced ( s , n ) : NEW_LINE INDENT count = 0 NEW_LINE count_1 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' ( ' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT count -= 1 NEW_LINE DEDENT count_1 = min ( count_1 , count ) NEW_LINE DEDENT if ( count_1 >= - 1 and count == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT s = " ( ) ) ( ) ( " NEW_LINE n = len ( s ) NEW_LINE if ( canBeBalanced ( s , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


public class MergeTwoSortedArrays { private static int nextGap ( int gap ) { if ( gap <= 1 ) return 0 ; return ( gap / 2 ) + ( gap % 2 ) ; } private static void merge ( int [ ] arr1 , int [ ] arr2 , int n , int m ) { int i , j , gap = n + m ; for ( gap = nextGap ( gap ) ; gap > 0 ; gap = nextGap ( gap ) ) { for ( i = 0 ; i + gap < n ; i ++ ) if ( arr1 [ i ] > arr1 [ i + gap ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr1 [ i + gap ] ; arr1 [ i + gap ] = temp ; } for ( j = gap > n ? gap - n : 0 ; i < n && j < m ; i ++ , j ++ ) if ( arr1 [ i ] > arr2 [ j ] ) { int temp = arr1 [ i ] ; arr1 [ i ] = arr2 [ j ] ; arr2 [ j ] = temp ; } if ( j < m ) { for ( j = 0 ; j + gap < m ; j ++ ) if ( arr2 [ j ] > arr2 [ j + gap ] ) { int temp = arr2 [ j ] ; arr2 [ j ] = arr2 [ j + gap ] ; arr2 [ j + gap ] = temp ; } } } } public static void main ( String [ ] args ) { int [ ] a1 = { 10 , 27 , 38 , 43 , 82 } ; int [ ] a2 = { 3 , 9 } ; merge ( a1 , a2 , a1 . length , a2 . length ) ; System . out . print ( " First ▁ Array : ▁ " ) ; for ( int i = 0 ; i < a1 . length ; i ++ ) { System . out . print ( a1 [ i ] + " ▁ " ) ; } System . out . println ( ) ; System . out . print ( " Second ▁ Array : ▁ " ) ; for ( int i = 0 ; i < a2 . length ; i ++ ) { System . out . print ( a2 [ i ] + " ▁ " ) ; } } }

def nextGap ( gap ) : NEW_LINE INDENT if ( gap <= 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( gap // 2 ) + ( gap % 2 ) NEW_LINE DEDENT def merge ( arr1 , arr2 , n , m ) : NEW_LINE INDENT gap = n + m NEW_LINE gap = nextGap ( gap ) NEW_LINE while gap > 0 : NEW_LINE INDENT i = 0 NEW_LINE while i + gap < n : NEW_LINE INDENT if ( arr1 [ i ] > arr1 [ i + gap ] ) : NEW_LINE INDENT arr1 [ i ] , arr1 [ i + gap ] = arr1 [ i + gap ] , arr1 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE DEDENT j = gap - n if gap > n else 0 NEW_LINE while i < n and j < m : NEW_LINE INDENT if ( arr1 [ i ] > arr2 [ j ] ) : NEW_LINE INDENT arr1 [ i ] , arr2 [ j ] = arr2 [ j ] , arr1 [ i ] NEW_LINE DEDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT if ( j < m ) : NEW_LINE INDENT j = 0 NEW_LINE while j + gap < m : NEW_LINE INDENT if ( arr2 [ j ] > arr2 [ j + gap ] ) : NEW_LINE INDENT arr2 [ j ] , arr2 [ j + gap ] = arr2 [ j + gap ] , arr2 [ j ] NEW_LINE DEDENT j += 1 NEW_LINE DEDENT DEDENT gap = nextGap ( gap ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a1 = [ 10 , 27 , 38 , 43 , 82 ] NEW_LINE a2 = [ 3 , 9 ] NEW_LINE n = len ( a1 ) NEW_LINE m = len ( a2 ) NEW_LINE merge ( a1 , a2 , n , m ) NEW_LINE print ( " First ▁ Array : ▁ " , end = " " ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( a1 [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE print ( " Second ▁ Array : ▁ " , end = " " ) NEW_LINE for i in range ( m ) : NEW_LINE INDENT print ( a2 [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT


class GFG { static int isSubstring ( String s1 , String s2 ) { int M = s1 . length ( ) ; int N = s2 . length ( ) ; for ( int i = 0 ; i <= N - M ; i ++ ) { int j ; for ( j = 0 ; j < M ; j ++ ) if ( s2 . charAt ( i + j ) != s1 . charAt ( j ) ) break ; if ( j == M ) return i ; } return - 1 ; } public static void main ( String args [ ] ) { String s1 = " for " ; String s2 = " geeksforgeeks " ; int res = isSubstring ( s1 , s2 ) ; if ( res == - 1 ) System . out . println ( " Not ▁ present " ) ; else System . out . println ( " Present ▁ at ▁ index ▁ " + res ) ; } }

def isSubstring ( s1 , s2 ) : NEW_LINE INDENT M = len ( s1 ) NEW_LINE N = len ( s2 ) NEW_LINE for i in range ( N - M + 1 ) : NEW_LINE INDENT for j in range ( M ) : NEW_LINE INDENT if ( s2 [ i + j ] != s1 [ j ] ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT if j + 1 == M : NEW_LINE INDENT return i NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " for " NEW_LINE s2 = " geeksforgeeks " NEW_LINE res = isSubstring ( s1 , s2 ) NEW_LINE if res == - 1 : NEW_LINE INDENT print ( " Not ▁ present " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Present ▁ at ▁ index ▁ " + str ( res ) ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static boolean check ( String s ) { for ( int i = 0 ; i + 1 < s . length ( ) ; ++ i ) { if ( Math . abs ( s . charAt ( i ) - s . charAt ( i + 1 ) ) == 1 ) { return false ; } } return true ; } static void Rearrange ( String str ) { String odd = " " , even = " " ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { if ( str . charAt ( i ) % 2 == 0 ) { even += str . charAt ( i ) ; } else { odd += str . charAt ( i ) ; } } odd = sort ( odd ) ; even = sort ( even ) ; if ( check ( odd + even ) ) { System . out . print ( odd + even ) ; } else if ( check ( even + odd ) ) { System . out . print ( even + odd ) ; } else { System . out . print ( - 1 ) ; } } public static String sort ( String inputString ) { char tempArray [ ] = inputString . toCharArray ( ) ; Arrays . sort ( tempArray ) ; return new String ( tempArray ) ; } public static void main ( String [ ] args ) { String str = " aabcd " ; Rearrange ( str ) ; } }

def check ( s ) : NEW_LINE INDENT for i in range ( len ( s ) - 1 ) : NEW_LINE INDENT if ( abs ( ord ( s [ i ] ) - ord ( s [ i + 1 ] ) ) == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def Rearrange ( Str ) : NEW_LINE INDENT odd , even = " " , " " NEW_LINE for i in range ( len ( Str ) ) : NEW_LINE INDENT if ( ord ( Str [ i ] ) % 2 == 0 ) : NEW_LINE INDENT even += Str [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT odd += Str [ i ] NEW_LINE DEDENT DEDENT odd = sorted ( odd ) NEW_LINE even = sorted ( even ) NEW_LINE if ( check ( odd + even ) ) : NEW_LINE INDENT print ( " " . join ( odd + even ) ) NEW_LINE DEDENT elif ( check ( even + odd ) ) : NEW_LINE INDENT print ( " " . join ( even + odd ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( - 1 ) NEW_LINE DEDENT DEDENT Str = " aabcd " NEW_LINE Rearrange ( Str ) NEW_LINE


class GFG { static int [ ] swap ( int [ ] arr , int i , int j ) { int temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; return arr ; } static void sort_shell_metzner ( int arr [ ] , int n ) { int i , j , k , l , m , temp ; m = n ; while ( m > 0 ) { m /= 2 ; k = n - m ; j = 0 ; do { i = j ; do { l = i + m ; if ( l < n && arr [ i ] > arr [ l ] ) { swap ( arr , i , l ) ; i -= m ; } else { break ; } } while ( i >= 0 ) ; j ++ ; } while ( j <= k ) ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 0 , - 2 , 8 , 5 , 1 } ; int n = arr . length ; sort_shell_metzner ( arr , n ) ; printArray ( arr , n ) ; } }

def sort_shell_metzner ( arr , n ) : NEW_LINE INDENT m = n ; NEW_LINE while ( m > 0 ) : NEW_LINE INDENT m //= 2 NEW_LINE k = n - m NEW_LINE j = 0 NEW_LINE while ( j < k ) : NEW_LINE INDENT i = j NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT l = i + m NEW_LINE if ( arr [ i ] > arr [ l ] ) : NEW_LINE INDENT arr [ i ] , arr [ l ] = arr [ l ] , arr [ i ] NEW_LINE i -= m NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 0 , - 2 , 8 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE sort_shell_metzner ( arr , n ) NEW_LINE printArray ( arr , n ) NEW_LINE DEDENT


class Test { static int arr [ ] = new int [ ] { 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 } ; static void findZeroes ( int m ) { int wL = 0 , wR = 0 ; int bestL = 0 , bestWindow = 0 ; int zeroCount = 0 ; while ( wR < arr . length ) { if ( zeroCount <= m ) { if ( arr [ wR ] == 0 ) zeroCount ++ ; wR ++ ; } if ( zeroCount > m ) { if ( arr [ wL ] == 0 ) zeroCount -- ; wL ++ ; } if ( ( wR - wL > bestWindow ) && ( zeroCount <= m ) ) { bestWindow = wR - wL ; bestL = wL ; } } for ( int i = 0 ; i < bestWindow ; i ++ ) { if ( arr [ bestL + i ] == 0 ) System . out . print ( bestL + i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int m = 2 ; System . out . println ( " Indexes ▁ of ▁ zeroes ▁ to ▁ be ▁ flipped ▁ are ▁ " ) ; findZeroes ( m ) ; } }

def findZeroes ( arr , n , m ) : NEW_LINE INDENT wL = wR = 0 NEW_LINE bestL = bestWindow = 0 NEW_LINE zeroCount = 0 NEW_LINE while wR < n : NEW_LINE INDENT if zeroCount <= m : NEW_LINE INDENT if arr [ wR ] == 0 : NEW_LINE INDENT zeroCount += 1 NEW_LINE DEDENT wR += 1 NEW_LINE DEDENT if zeroCount > m : NEW_LINE INDENT if arr [ wL ] == 0 : NEW_LINE INDENT zeroCount -= 1 NEW_LINE DEDENT wL += 1 NEW_LINE DEDENT if ( wR - wL > bestWindow ) and ( zeroCount <= m ) : NEW_LINE INDENT bestWindow = wR - wL NEW_LINE bestL = wL NEW_LINE DEDENT DEDENT for i in range ( 0 , bestWindow ) : NEW_LINE INDENT if arr [ bestL + i ] == 0 : NEW_LINE INDENT print ( bestL + i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 1 , 0 , 0 , 1 , 1 , 0 , 1 , 0 , 1 , 1 ] NEW_LINE m = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( " Indexes ▁ of ▁ zeroes ▁ to ▁ be ▁ flipped ▁ are " , end = " ▁ " ) NEW_LINE findZeroes ( arr , n , m ) NEW_LINE


import java . io . * ; import java . util . * ; import java . lang . Math ; class Multipy { static void modify ( int arr [ ] , int n ) { if ( n <= 1 ) return ; int prev = arr [ 0 ] ; arr [ 0 ] = arr [ 0 ] * arr [ 1 ] ; for ( int i = 1 ; i < n - 1 ; i ++ ) { int curr = arr [ i ] ; arr [ i ] = prev * arr [ i + 1 ] ; prev = curr ; } arr [ n - 1 ] = prev * arr [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 5 , 6 } ; int n = arr . length ; modify ( arr , n ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } }

def modify ( arr , n ) : NEW_LINE INDENT if n <= 1 : NEW_LINE INDENT return NEW_LINE DEDENT prev = arr [ 0 ] NEW_LINE arr [ 0 ] = arr [ 0 ] * arr [ 1 ] NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT curr = arr [ i ] ; NEW_LINE arr [ i ] = prev * arr [ i + 1 ] NEW_LINE prev = curr NEW_LINE DEDENT arr [ n - 1 ] = prev * arr [ n - 1 ] NEW_LINE DEDENT arr = [ 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE modify ( arr , n ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT


import java . util . * ; class GfG { static int countPairs ( int A1 [ ] , int A2 [ ] , int n1 , int n2 , int K ) { int res = 0 ; Map < Integer , Integer > m = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n1 ; ++ i ) { if ( m . containsKey ( A1 [ i ] ) ) m . put ( A1 [ i ] , m . get ( A1 [ i ] ) + 1 ) ; else m . put ( A1 [ i ] , 1 ) ; } for ( int i = 0 ; i < n2 ; ++ i ) { int temp = K - A2 [ i ] ; if ( m . containsKey ( temp ) && m . get ( temp ) != 0 ) { res ++ ; m . put ( temp , m . get ( A1 [ i ] ) - 1 ) ; } } return res ; } public static void main ( String [ ] args ) { int A1 [ ] = { 1 , 1 , 3 , 4 , 5 , 6 , 6 } ; int A2 [ ] = { 1 , 4 , 4 , 5 , 7 } , K = 10 ; int n1 = A1 . length ; int n2 = A2 . length ; System . out . println ( countPairs ( A1 , A2 , n1 , n2 , K ) ) ; } }

def countPairs ( A1 , A2 , n1 , n2 , K ) : NEW_LINE INDENT res = 0 NEW_LINE m = dict ( ) NEW_LINE for i in range ( 0 , n1 ) : NEW_LINE INDENT if A1 [ i ] not in m . keys ( ) : NEW_LINE INDENT m [ A1 [ i ] ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT m [ A1 [ i ] ] = m [ A1 [ i ] ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n2 ) : NEW_LINE INDENT temp = K - A2 [ i ] NEW_LINE if temp in m . keys ( ) : NEW_LINE INDENT res = res + 1 NEW_LINE m [ temp ] = m [ temp ] - 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT A1 = [ 1 , 1 , 3 , 4 , 5 , 6 , 6 ] NEW_LINE A2 = [ 1 , 4 , 4 , 5 , 7 ] NEW_LINE K = 10 NEW_LINE n1 = len ( A1 ) NEW_LINE n2 = len ( A2 ) NEW_LINE print ( countPairs ( A1 , A2 , n1 , n2 , K ) ) NEW_LINE


import java . io . * ; class GFG { static int MAX = 50 ; static void remove_row_col ( int arr [ ] [ ] , int n , int x ) { for ( int i = x ; i < n ; i ++ ) { for ( int j = x ; j < n ; j ++ ) { System . out . print ( arr [ i ] [ j ] + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 3 ; int arr [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int x = 1 ; remove_row_col ( arr , n , x ) ; } }

def remove_row_col ( arr , n , x ) : NEW_LINE INDENT for i in range ( x , n ) : NEW_LINE INDENT for j in range ( x , n ) : NEW_LINE INDENT print ( arr [ i ] [ j ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE MAX = 50 NEW_LINE arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE x = 1 NEW_LINE remove_row_col ( arr , n , x ) NEW_LINE DEDENT


class GFG { public static boolean isDivisible ( int num , int x , int y ) { while ( num % x == 0 || num % y == 0 ) { if ( num % x == 0 ) num /= x ; if ( num % y == 0 ) num /= y ; } if ( num > 1 ) return false ; return true ; } public static int _gcd ( int a , int b ) { while ( a != b ) { if ( a > b ) a = a - b ; else b = b - a ; } return a ; } public static boolean isPossible ( int [ ] arr , int n , int x , int y ) { int gcd = arr [ 0 ] ; for ( int i = 1 ; i < n ; i ++ ) gcd = _gcd ( gcd , arr [ i ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! isDivisible ( arr [ i ] / gcd , x , y ) ) return false ; } return true ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 6 , 8 } ; int n = arr . length ; int x = 2 , y = 3 ; if ( isPossible ( arr , n , x , y ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

from math import gcd as __gcd NEW_LINE def isDivisible ( num , x , y ) : NEW_LINE INDENT while ( num % x == 0 or num % y == 0 ) : NEW_LINE INDENT if ( num % x == 0 ) : NEW_LINE INDENT num //= x NEW_LINE DEDENT if ( num % y == 0 ) : NEW_LINE INDENT num //= y NEW_LINE DEDENT DEDENT if ( num > 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT def isPossible ( arr , n , x , y ) : NEW_LINE INDENT gcd = arr [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT gcd = __gcd ( gcd , arr [ i ] ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( isDivisible ( arr [ i ] // gcd , x , y ) == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 2 , 4 , 6 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE x = 2 NEW_LINE y = 3 NEW_LINE if ( isPossible ( arr , n , x , y ) == True ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int factorial ( int n ) { int result = 1 ; for ( int i = 1 ; i <= n ; i ++ ) result = result * i ; return result ; } static int nCr ( int n , int r ) { return factorial ( n ) / ( factorial ( r ) * factorial ( n - r ) ) ; } static int calculate_result ( int n ) { int result = 2 * nCr ( ( n - 2 ) , ( n / 2 - 1 ) ) ; return result ; } public static void main ( String [ ] args ) { int a = 2 , b = 4 ; System . out . println ( calculate_result ( 2 * a ) ) ; System . out . print ( calculate_result ( 2 * b ) ) ; } }

def factorial ( n ) : NEW_LINE INDENT result = 1 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT result *= i NEW_LINE DEDENT return result NEW_LINE DEDENT def nCr ( n , r ) : NEW_LINE INDENT return ( factorial ( n ) // ( factorial ( r ) * factorial ( n - r ) ) ) NEW_LINE DEDENT def calculate_result ( n ) : NEW_LINE INDENT result = 2 * nCr ( ( n - 2 ) , ( n // 2 - 1 ) ) NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 2 , 4 NEW_LINE print ( calculate_result ( 2 * a ) ) NEW_LINE print ( calculate_result ( 2 * b ) ) NEW_LINE DEDENT


class GFG { static int MAX_CHAR = 26 ; static void sortString ( String str ) { int charCount [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { charCount [ str . charAt ( i ) - ' a ' ] ++ ; } for ( int i = MAX_CHAR - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < charCount [ i ] ; j ++ ) { System . out . print ( ( char ) ( ' a ' + i ) ) ; } } } public static void main ( String [ ] args ) { String s = " alkasingh " ; sortString ( s ) ; } }

MAX_CHAR = 26 ; NEW_LINE def sortString ( str ) : NEW_LINE INDENT charCount = [ 0 ] * MAX_CHAR ; NEW_LINE for i in range ( len ( str ) ) : NEW_LINE INDENT charCount [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( MAX_CHAR - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( charCount [ i ] ) : NEW_LINE INDENT print ( chr ( 97 + i ) , end = " " ) ; NEW_LINE DEDENT DEDENT DEDENT s = " alkasingh " ; NEW_LINE sortString ( s ) ; NEW_LINE


class GFG { static double average ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return sum / n ; } public static void main ( String [ ] args ) { int arr [ ] = { 10 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } ; int n = arr . length ; System . out . println ( average ( arr , n ) ) ; } }

def average ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT return sum / n ; NEW_LINE DEDENT arr = [ 10 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( average ( arr , n ) ) NEW_LINE


import java . util . HashSet ; class geeks { public static int countTotalDistinct ( String str ) { int cnt = 0 ; HashSet < String > items = new HashSet < > ( ) ; for ( int i = 0 ; i < str . length ( ) ; ++ i ) { String temp = " " ; HashSet < Character > ans = new HashSet < > ( ) ; for ( int j = i ; j < str . length ( ) ; ++ j ) { temp = temp + str . charAt ( j ) ; ans . add ( str . charAt ( j ) ) ; if ( ! items . contains ( temp ) ) { items . add ( temp ) ; cnt += ans . size ( ) ; } } } return cnt ; } public static void main ( String [ ] args ) { String str = " ABCA " ; System . out . println ( countTotalDistinct ( str ) ) ; } }

def countTotalDistinct ( string ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE items = set ( ) ; NEW_LINE for i in range ( len ( string ) ) : NEW_LINE INDENT temp = " " ; NEW_LINE ans = set ( ) ; NEW_LINE for j in range ( i , len ( string ) ) : NEW_LINE INDENT temp = temp + string [ j ] ; NEW_LINE ans . add ( string [ j ] ) ; NEW_LINE if temp not in items : NEW_LINE INDENT items . add ( temp ) ; NEW_LINE cnt += len ( ans ) ; NEW_LINE DEDENT DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " ABCA " ; NEW_LINE print ( countTotalDistinct ( string ) ) ; NEW_LINE DEDENT


class solution { static int oddEquivalent ( String s , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( s . charAt ( i ) == '1' ) count ++ ; } return count ; } public static void main ( String ar [ ] ) { String s = "1011011" ; int n = s . length ( ) ; System . out . println ( oddEquivalent ( s , n ) ) ; } }

def oddEquivalent ( s , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( s [ i ] == '1' ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "1011011" NEW_LINE n = len ( s ) NEW_LINE print ( oddEquivalent ( s , n ) ) NEW_LINE DEDENT


public class GFG { static void printNumbers ( int n , int d ) { String st = " " + d ; char ch = st . charAt ( 0 ) ; for ( int i = 0 ; i <= n ; i ++ ) { st = " " ; st = st + i ; if ( i == d || st . indexOf ( ch ) >= 0 ) System . out . print ( i + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 100 , d = 5 ; printNumbers ( n , d ) ; } }

def index ( st , ch ) : NEW_LINE INDENT for i in range ( len ( st ) ) : NEW_LINE INDENT if ( st [ i ] == ch ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def printNumbers ( n , d ) : NEW_LINE INDENT st = " " + str ( d ) NEW_LINE ch = st [ 0 ] NEW_LINE for i in range ( 0 , n + 1 , 1 ) : NEW_LINE INDENT st = " " NEW_LINE st = st + str ( i ) NEW_LINE if ( i == d or index ( st , ch ) >= 0 ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 100 NEW_LINE d = 5 NEW_LINE printNumbers ( n , d ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int countRectangles ( int radius ) { int rectangles = 0 ; int diameter = 2 * radius ; int diameterSquare = diameter * diameter ; for ( int a = 1 ; a < 2 * radius ; a ++ ) { for ( int b = 1 ; b < 2 * radius ; b ++ ) { int diagnalLengthSquare = ( a * a + b * b ) ; if ( diagnalLengthSquare <= diameterSquare ) { rectangles ++ ; } } } return rectangles ; } public static void main ( String [ ] args ) { int radius = 2 ; int totalRectangles ; totalRectangles = countRectangles ( radius ) ; System . out . println ( totalRectangles + " ▁ rectangles ▁ can ▁ be ▁ " + " cut ▁ from ▁ a ▁ circle ▁ of " + " ▁ Radius ▁ " + radius ) ; } }

def countRectangles ( radius ) : NEW_LINE INDENT rectangles = 0 NEW_LINE diameter = 2 * radius NEW_LINE diameterSquare = diameter * diameter NEW_LINE for a in range ( 1 , 2 * radius ) : NEW_LINE INDENT for b in range ( 1 , 2 * radius ) : NEW_LINE INDENT diagnalLengthSquare = ( a * a + b * b ) NEW_LINE if ( diagnalLengthSquare <= diameterSquare ) : NEW_LINE INDENT rectangles += 1 NEW_LINE DEDENT DEDENT DEDENT return rectangles NEW_LINE DEDENT radius = 2 NEW_LINE totalRectangles = countRectangles ( radius ) NEW_LINE print ( totalRectangles , " rectangles ▁ can ▁ be " , " cut ▁ from ▁ a ▁ circle ▁ of ▁ Radius " , radius ) NEW_LINE