class GFG { static int findSum ( int [ ] arr , int n , int left , int right ) { int k = right - left ; int d = arr [ 1 ] - arr [ 0 ] ; int ans = arr [ left - 1 ] * ( k + 1 ) ; ans = ans + ( d * ( k * ( k + 1 ) ) ) / 2 ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 } ; int queries = 3 ; int q [ ] [ ] = { { 2 , 4 } , { 2 , 6 } , { 5 , 6 } } ; int n = arr . length ; for ( int i = 0 ; i < queries ; i ++ ) System . out . print ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) + " \n " ) ; } }

def findSum ( arr , n , left , right ) : NEW_LINE INDENT k = right - left ; NEW_LINE d = arr [ 1 ] - arr [ 0 ] ; NEW_LINE ans = arr [ left - 1 ] * ( k + 1 ) ; NEW_LINE ans = ans + ( d * ( k * ( k + 1 ) ) ) // 2 ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 4 , 6 , 8 , 10 , 12 , 14 , 16 ] ; NEW_LINE queries = 3 ; NEW_LINE q = [ [ 2 , 4 ] , [ 2 , 6 ] , [ 5 , 6 ] ] ; NEW_LINE n = len ( arr ) ; NEW_LINE for i in range ( queries ) : NEW_LINE INDENT print ( findSum ( arr , n , q [ i ] [ 0 ] , q [ i ] [ 1 ] ) ) ; NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static void Find_Sequence ( int [ ] arr , int n ) { Vector < Integer > inc_arr = new Vector < > ( ) , dec_arr = new Vector < > ( ) ; int flag = 0 ; long inc = - 1 , dec = ( long ) 1e7 ; for ( int i = 0 ; i < n ; i ++ ) { if ( inc < arr [ i ] && arr [ i ] < dec ) { if ( arr [ i ] < arr [ i + 1 ] ) { inc = arr [ i ] ; inc_arr . add ( arr [ i ] ) ; } else { dec = arr [ i ] ; dec_arr . add ( arr [ i ] ) ; } } else if ( inc < arr [ i ] ) { inc = arr [ i ] ; inc_arr . add ( arr [ i ] ) ; } else if ( dec > arr [ i ] ) { dec = arr [ i ] ; dec_arr . add ( arr [ i ] ) ; } else { System . out . println ( - 1 ) ; flag = 1 ; break ; } } if ( flag == 0 ) { for ( int i : inc_arr ) System . out . print ( i + " ▁ " ) ; System . out . println ( ) ; for ( int i : dec_arr ) System . out . print ( i + " ▁ " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int [ ] arr = { 5 , 1 , 3 , 6 , 8 , 2 , 9 , 0 , 10 } ; int n = arr . length ; Find_Sequence ( arr , n ) ; } }

def Find_Sequence ( array , n ) : NEW_LINE INDENT inc_arr , dec_arr = [ ] , [ ] NEW_LINE inc , dec = - 1 , 1e7 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if inc < array [ i ] < dec : NEW_LINE INDENT if array [ i ] < array [ i + 1 ] : NEW_LINE INDENT inc = array [ i ] NEW_LINE inc_arr . append ( array [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT dec = array [ i ] NEW_LINE dec_arr . append ( array [ i ] ) NEW_LINE DEDENT DEDENT elif inc < array [ i ] : NEW_LINE INDENT inc = array [ i ] NEW_LINE inc_arr . append ( array [ i ] ) NEW_LINE DEDENT elif dec > array [ i ] : NEW_LINE INDENT dec = array [ i ] NEW_LINE dec_arr . append ( array [ i ] ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' - 1' ) NEW_LINE break NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( inc_arr , dec_arr ) NEW_LINE DEDENT DEDENT arr = [ 5 , 1 , 3 , 6 , 8 , 2 , 9 , 0 , 10 ] NEW_LINE n = len ( arr ) NEW_LINE Find_Sequence ( arr , n ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class Gfg { static void Digits ( int n ) { int largest = 0 ; int smallest = 9 ; while ( n != 0 ) { int r = n % 10 ; largest = Math . max ( r , largest ) ; smallest = Math . min ( r , smallest ) ; n = n / 10 ; } System . out . println ( largest + " ▁ " + smallest ) ; } public static void main ( String [ ] args ) throws java . lang . Exception { int n = 2346 ; Digits ( n ) ; } }

def Digits ( n ) : NEW_LINE INDENT largest = 0 NEW_LINE smallest = 9 NEW_LINE while ( n ) : NEW_LINE INDENT r = n % 10 NEW_LINE largest = max ( r , largest ) NEW_LINE smallest = min ( r , smallest ) NEW_LINE n = n // 10 NEW_LINE DEDENT print ( largest , smallest ) NEW_LINE DEDENT n = 2346 NEW_LINE Digits ( n ) NEW_LINE


import java . io . * ; class Series { static int sumOfSeries ( int n ) { return n * ( n + 1 ) * ( 6 * n * n * n + 9 * n * n + n - 1 ) / 30 ; } public static void main ( String [ ] args ) { int n = 4 ; System . out . println ( sumOfSeries ( n ) ) ; } }

def sumOfSeries ( n ) : NEW_LINE INDENT return n * ( n + 1 ) * ( 6 * n * n * n + 9 * n * n + n - 1 ) / 30 NEW_LINE DEDENT n = 4 NEW_LINE print sumOfSeries ( n ) NEW_LINE


class GFG { static int p = 1000000007 ; static long power ( long x , long y ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( y % 2 == 1 ) res = ( res * x ) % p ; y = y >> 1 ; x = ( x * x ) % p ; } return res % p ; } public static void main ( String [ ] args ) { long n = 3 ; long x = ( power ( 3 , n ) % p + 1 ) % p ; x = ( x - power ( 2 , n + 1 ) + p ) % p ; x = ( x * power ( 2 , p - 2 ) ) % p ; System . out . println ( x ) ; } }

p = 1000000007 NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res % p NEW_LINE DEDENT n = 3 NEW_LINE x = ( power ( 3 , n ) % p + 1 ) % p NEW_LINE x = ( x - power ( 2 , n + 1 ) + p ) % p NEW_LINE x = ( x * power ( 2 , p - 2 ) ) % p NEW_LINE print ( x ) NEW_LINE


import java . util . Arrays ; class GFG { static void printKMissing ( int [ ] arr , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < n && arr [ i ] <= 0 ) i ++ ; int count = 0 , curr = 1 ; while ( count < k && i < n ) { if ( arr [ i ] != curr ) { System . out . print ( curr + " ▁ " ) ; count ++ ; } else i ++ ; curr ++ ; } while ( count < k ) { System . out . print ( curr + " ▁ " ) ; curr ++ ; count ++ ; } } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 3 , 4 } ; int n = arr . length ; int k = 3 ; printKMissing ( arr , n , k ) ; } }

def printKMissing ( arr , n , k ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE i = 0 NEW_LINE while ( i < n and arr [ i ] <= 0 ) : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT count = 0 NEW_LINE curr = 1 NEW_LINE while ( count < k and i < n ) : NEW_LINE INDENT if ( arr [ i ] != curr ) : NEW_LINE INDENT print ( str ( curr ) + " ▁ " , end = ' ' ) NEW_LINE count = count + 1 NEW_LINE DEDENT else : NEW_LINE INDENT i = i + 1 NEW_LINE DEDENT curr = curr + 1 NEW_LINE DEDENT while ( count < k ) : NEW_LINE INDENT print ( str ( curr ) + " ▁ " , end = ' ' ) NEW_LINE curr = curr + 1 NEW_LINE count = count + 1 NEW_LINE DEDENT DEDENT arr = [ 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE printKMissing ( arr , n , k ) ; NEW_LINE


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

import math as mt NEW_LINE def countPairs ( arr , n ) : NEW_LINE INDENT mp = dict ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in mp . keys ( ) : NEW_LINE INDENT mp [ arr [ i ] ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT mp [ arr [ i ] ] = 1 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for it in mp : NEW_LINE INDENT count = mp [ it ] NEW_LINE ans += ( count * ( count - 1 ) ) // 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 1 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countPairs ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static int countTrees ( int n ) { int BT [ ] = new int [ n + 1 ] ; for ( int i = 0 ; i <= n ; i ++ ) BT [ i ] = 0 ; BT [ 0 ] = BT [ 1 ] = 1 ; for ( int i = 2 ; i <= n ; ++ i ) for ( int j = 0 ; j < i ; j ++ ) BT [ i ] += BT [ j ] * BT [ i - j - 1 ] ; return BT [ n ] ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( " Total ▁ Possible ▁ " + " Binary ▁ Trees ▁ are ▁ : ▁ " + countTrees ( n ) ) ; } }

def countTrees ( n ) : NEW_LINE INDENT BT = [ 0 ] * ( n + 1 ) NEW_LINE BT [ 0 ] = BT [ 1 ] = 1 NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT for j in range ( i ) : NEW_LINE INDENT BT [ i ] += BT [ j ] * BT [ i - j - 1 ] NEW_LINE DEDENT DEDENT return BT [ n ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( " Total ▁ Possible ▁ Binary ▁ Trees ▁ are ▁ : ▁ " , countTrees ( n ) ) NEW_LINE DEDENT


import java . util . Vector ; public class GFG { static int max_val ( int [ ] arr ) { int i ; int max = arr [ 0 ] ; for ( i = 1 ; i < arr . length ; i ++ ) { if ( arr [ i ] > max ) { max = arr [ i ] ; } } return max ; } static void compositeSumProduct ( int arr [ ] , int n , int k ) { int max_val = max_val ( arr ) ; Vector < Boolean > prime = new Vector < > ( ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) { prime . add ( i , Boolean . TRUE ) ; } for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime . get ( p ) == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) { prime . add ( i , Boolean . FALSE ) ; } } } int sum = 0 , product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ! prime . get ( arr [ i ] ) && arr [ i ] % k == 0 ) { sum += arr [ i ] ; product *= arr [ i ] ; } } System . out . println ( " Sum ▁ of ▁ composite ▁ numbers ▁ " + " divisible ▁ by ▁ k ▁ is ▁ " + sum ) ; System . out . println ( " \n Product ▁ of ▁ composite ▁ numbers " + " ▁ divisible ▁ by ▁ k ▁ is ▁ " + product ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; int k = 2 ; compositeSumProduct ( arr , n , k ) ; } }

from math import sqrt , ceil NEW_LINE def compositeSumProduct ( arr , n , k ) : NEW_LINE INDENT max_val = arr [ 0 ] ; NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT if ( max_val < arr [ i ] ) : NEW_LINE INDENT max_val = arr [ i ] NEW_LINE DEDENT DEDENT prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE k = int ( sqrt ( max_val ) ) NEW_LINE for p in range ( 2 , k + 1 , 1 ) : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for i in range ( p * 2 , max_val , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT sum = 0 NEW_LINE product = 1 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] == False and arr [ i ] % k == 0 ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE product *= arr [ i ] NEW_LINE DEDENT DEDENT print ( " Sum ▁ of ▁ composite ▁ numbers " , " divisible ▁ by ▁ k ▁ is " , sum ) NEW_LINE print ( " Product ▁ of ▁ composite ▁ numbers " , " divisible ▁ by ▁ k ▁ is " , product ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE compositeSumProduct ( arr , n , k ) NEW_LINE DEDENT


class GFG { static final int MAX = 32768 ; static int [ ] arr = new int [ 2 * MAX ] ; static void gouldSequence ( ) { arr [ 0 ] = 1 ; int i = 1 ; int p = 1 ; while ( i <= MAX ) { int j = 0 ; while ( j < i ) { arr [ i + j ] = 2 * arr [ j ] ; j ++ ; } i = ( 1 << p ) ; p ++ ; } } static void printSequence ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { gouldSequence ( ) ; int n = 16 ; printSequence ( n ) ; } }

MAX = 32768 NEW_LINE arr = [ None ] * ( 2 * MAX ) NEW_LINE def gouldSequence ( ) : NEW_LINE INDENT arr [ 0 ] = 1 NEW_LINE i = 1 NEW_LINE p = 1 NEW_LINE while i <= MAX : NEW_LINE INDENT j = 0 NEW_LINE while j < i : NEW_LINE INDENT arr [ i + j ] = 2 * arr [ j ] NEW_LINE j += 1 NEW_LINE DEDENT i = ( 1 << p ) NEW_LINE p += 1 NEW_LINE DEDENT DEDENT def printSequence ( n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT gouldSequence ( ) NEW_LINE n = 16 NEW_LINE printSequence ( n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int [ ] Findpermutation ( int n ) { int [ ] a = new int [ n + 1 ] ; a [ 1 ] = n ; for ( int i = 2 ; i <= n ; i ++ ) a [ i ] = i - 1 ; return a ; } public static void main ( String [ ] args ) { int n = 8 ; int [ ] v = Findpermutation ( n ) ; for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( v [ i ] + " ▁ " ) ; } }

def Findpermutation ( n ) : NEW_LINE INDENT a = [ 0 ] * ( n + 1 ) ; NEW_LINE a [ 1 ] = n ; NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT a [ i ] = i - 1 ; NEW_LINE DEDENT return a ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 ; NEW_LINE v = Findpermutation ( n ) ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( v [ i ] , end = ' ▁ ' ) ; NEW_LINE DEDENT DEDENT


class GFG { static class Node { int data ; Node next , prev ; } ; static Node getNode ( int data ) { Node newNode = new Node ( ) ; newNode . data = data ; return newNode ; } static Node insertEnd ( Node head , Node new_node ) { if ( head == null ) { new_node . next = new_node . prev = new_node ; head = new_node ; return head ; } Node last = ( head ) . prev ; new_node . next = head ; ( head ) . prev = new_node ; new_node . prev = last ; last . next = new_node ; return head ; } static Node reverse ( Node head ) { if ( head == null ) return null ; Node new_head = null ; Node last = head . prev ; Node curr = last , prev ; while ( curr . prev != last ) { prev = curr . prev ; new_head = insertEnd ( new_head , curr ) ; curr = prev ; } new_head = insertEnd ( new_head , curr ) ; return new_head ; } static void display ( Node head ) { if ( head == null ) return ; Node temp = head ; System . out . print ( " Forward ▁ direction : ▁ " ) ; while ( temp . next != head ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . next ; } System . out . print ( temp . data + " ▁ " ) ; Node last = head . prev ; temp = last ; System . out . print ( " \n Backward ▁ direction : ▁ " ) ; while ( temp . prev != last ) { System . out . print ( temp . data + " ▁ " ) ; temp = temp . prev ; } System . out . print ( temp . data + " ▁ " ) ; } public static void main ( String args [ ] ) { Node head = null ; head = insertEnd ( head , getNode ( 1 ) ) ; head = insertEnd ( head , getNode ( 2 ) ) ; head = insertEnd ( head , getNode ( 3 ) ) ; head = insertEnd ( head , getNode ( 4 ) ) ; head = insertEnd ( head , getNode ( 5 ) ) ; System . out . print ( " Current ▁ list : \n " ) ; display ( head ) ; head = reverse ( head ) ; System . out . print ( " \n \n Reversed ▁ list : \n " ) ; display ( 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 getNode ( data ) : NEW_LINE INDENT newNode = Node ( data ) NEW_LINE newNode . data = data NEW_LINE return newNode NEW_LINE DEDENT def insertEnd ( head , new_node ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT new_node . next = new_node NEW_LINE new_node . prev = new_node NEW_LINE head = new_node NEW_LINE return head NEW_LINE DEDENT last = head . prev NEW_LINE new_node . next = head NEW_LINE head . prev = new_node NEW_LINE new_node . prev = last NEW_LINE last . next = new_node NEW_LINE return head NEW_LINE DEDENT def reverse ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT new_head = None NEW_LINE last = head . prev NEW_LINE curr = last NEW_LINE while ( curr . prev != last ) : NEW_LINE INDENT prev = curr . prev NEW_LINE new_head = insertEnd ( new_head , curr ) NEW_LINE curr = prev NEW_LINE DEDENT new_head = insertEnd ( new_head , curr ) NEW_LINE return new_head NEW_LINE DEDENT def display ( head ) : NEW_LINE INDENT if ( head == None ) : NEW_LINE INDENT return NEW_LINE DEDENT temp = head NEW_LINE print ( " Forward ▁ direction : ▁ " , end = " " ) NEW_LINE while ( temp . next != head ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . next NEW_LINE DEDENT print ( temp . data ) NEW_LINE last = head . prev NEW_LINE temp = last NEW_LINE print ( " Backward ▁ direction : ▁ " , end = " " ) NEW_LINE while ( temp . prev != last ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . prev NEW_LINE DEDENT print ( temp . data ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = insertEnd ( head , getNode ( 1 ) ) NEW_LINE head = insertEnd ( head , getNode ( 2 ) ) NEW_LINE head = insertEnd ( head , getNode ( 3 ) ) NEW_LINE head = insertEnd ( head , getNode ( 4 ) ) NEW_LINE head = insertEnd ( head , getNode ( 5 ) ) NEW_LINE print ( " Current ▁ list : " ) NEW_LINE display ( head ) NEW_LINE head = reverse ( head ) NEW_LINE print ( " \n Reversed ▁ list : " ) NEW_LINE display ( head ) NEW_LINE DEDENT


class GFG { static int maxSize = 50 ; static double dp [ ] = new double [ maxSize ] ; static int v [ ] = new int [ maxSize ] ; static double expectedSteps ( int x ) { if ( x == 0 ) return 0 ; if ( x <= 5 ) return 6 ; if ( v [ x ] == 1 ) return dp [ x ] ; v [ x ] = 1 ; dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 ; return dp [ x ] ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( expectedSteps ( n - 1 ) ) ; } }

maxSize = 50 NEW_LINE dp = [ 0 ] * maxSize NEW_LINE v = [ 0 ] * maxSize NEW_LINE def expectedSteps ( x ) : NEW_LINE INDENT if ( x == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( x <= 5 ) : NEW_LINE INDENT return 6 NEW_LINE DEDENT if ( v [ x ] ) : NEW_LINE INDENT return dp [ x ] NEW_LINE DEDENT v [ x ] = 1 NEW_LINE dp [ x ] = 1 + ( expectedSteps ( x - 1 ) + expectedSteps ( x - 2 ) + expectedSteps ( x - 3 ) + expectedSteps ( x - 4 ) + expectedSteps ( x - 5 ) + expectedSteps ( x - 6 ) ) / 6 NEW_LINE return dp [ x ] NEW_LINE DEDENT n = 10 NEW_LINE print ( round ( expectedSteps ( n - 1 ) , 5 ) ) NEW_LINE


import java . util . * ; class GFG { static int Hosoya ( int n , int m ) { if ( ( n == 0 && m == 0 ) || ( n == 1 && m == 0 ) || ( n == 1 && m == 1 ) || ( n == 2 && m == 1 ) ) return 1 ; if ( n > m ) return Hosoya ( n - 1 , m ) + Hosoya ( n - 2 , m ) ; else if ( m == n ) return Hosoya ( n - 1 , m - 1 ) + Hosoya ( n - 2 , m - 2 ) ; else return 0 ; } static void printHosoya ( int n ) { for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) System . out . print ( Hosoya ( i , j ) + " ▁ " ) ; System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; printHosoya ( n ) ; } }

def Hosoya ( n , m ) : NEW_LINE INDENT if ( ( n == 0 and m == 0 ) or ( n == 1 and m == 0 ) or ( n == 1 and m == 1 ) or ( n == 2 and m == 1 ) ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if n > m : NEW_LINE INDENT return Hosoya ( n - 1 , m ) NEW_LINE INDENT + Hosoya ( n - 2 , m ) NEW_LINE DEDENT DEDENT elif m == n : NEW_LINE INDENT return Hosoya ( n - 1 , m - 1 ) NEW_LINE INDENT + Hosoya ( n - 2 , m - 2 ) NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def printHosoya ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT print ( Hosoya ( i , j ) , end = " ▁ " ) NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE printHosoya ( n ) NEW_LINE


public class AQES { static int LowerInsertionPoint ( int arr [ ] , int n , int X ) { if ( X < arr [ 0 ] ) return 0 ; else if ( X > arr [ n - 1 ] ) return n ; int lowerPnt = 0 ; int i = 1 ; while ( i < n && arr [ i ] < X ) { lowerPnt = i ; i = i * 2 ; } while ( lowerPnt < n && arr [ lowerPnt ] < X ) lowerPnt ++ ; return lowerPnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 4 , 5 , 6 , 7 , 9 } ; int n = arr . length ; int X = 4 ; System . out . println ( LowerInsertionPoint ( arr , n , X ) ) ; } }

def LowerInsertionPoint ( arr , n , X ) : NEW_LINE INDENT if ( X < arr [ 0 ] ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT elif ( X > arr [ n - 1 ] ) : NEW_LINE INDENT return n NEW_LINE DEDENT lowerPnt = 0 NEW_LINE i = 1 NEW_LINE while ( i < n and arr [ i ] < X ) : NEW_LINE INDENT lowerPnt = i NEW_LINE i = i * 2 NEW_LINE DEDENT while ( lowerPnt < n and arr [ lowerPnt ] < X ) : NEW_LINE INDENT lowerPnt += 1 NEW_LINE DEDENT return lowerPnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 4 , 5 , 6 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE X = 4 NEW_LINE print ( LowerInsertionPoint ( arr , n , X ) ) NEW_LINE DEDENT


class GFG { static void maximumSuffix ( String s , int n , int k ) { int i = s . length ( ) - 1 ; int arr [ ] = new int [ 26 ] ; String suffix = " " ; while ( i > - 1 ) { int index = s . charAt ( i ) - ' a ' ; if ( arr [ index ] < n ) { arr [ index ] ++ ; suffix += s . charAt ( i ) ; i -- ; continue ; } if ( k == 0 ) break ; k -- ; i -- ; } suffix = reverse ( suffix ) ; System . out . print ( suffix ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String str = " iahagafedcba " ; int n = 1 , k = 2 ; maximumSuffix ( str , n , k ) ; } }

def maximumSuffix ( s , n , k ) : NEW_LINE INDENT i = len ( s ) - 1 NEW_LINE arr = [ 0 for i in range ( 26 ) ] NEW_LINE suffix = " " NEW_LINE while ( i > - 1 ) : NEW_LINE INDENT index = ord ( s [ i ] ) - ord ( ' a ' ) ; NEW_LINE if ( arr [ index ] < n ) : NEW_LINE INDENT arr [ index ] += 1 NEW_LINE suffix += s [ i ] NEW_LINE i -= 1 NEW_LINE continue NEW_LINE DEDENT if ( k == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT k -= 1 NEW_LINE i -= 1 NEW_LINE DEDENT suffix = suffix [ : : - 1 ] NEW_LINE print ( suffix ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " iahagafedcba " NEW_LINE n = 1 NEW_LINE k = 2 NEW_LINE maximumSuffix ( str , n , k ) NEW_LINE DEDENT


class GFG { static int countHexadecimal ( int L , int R ) { int count = 0 ; for ( int i = L ; i <= R ; i ++ ) { if ( i >= 10 && i <= 15 ) count ++ ; else if ( i > 15 ) { int k = i ; while ( k != 0 ) { if ( k % 16 >= 10 ) count ++ ; k = k / 16 ; } } } return count ; } public static void main ( String args [ ] ) { int L = 5 , R = 100 ; System . out . print ( countHexadecimal ( L , R ) ) ; } }

def countHexadecimal ( L , R ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT if ( i >= 10 and i <= 15 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT elif ( i > 15 ) : NEW_LINE INDENT k = i ; NEW_LINE while ( k != 0 ) : NEW_LINE INDENT if ( k % 16 >= 10 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT k = k // 16 ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT L = 5 ; R = 100 ; NEW_LINE print ( countHexadecimal ( L , R ) ) ; NEW_LINE DEDENT


public class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = null ; right = null ; } } static void updateParent ( int parent [ ] , Node node ) { if ( node == null ) return ; if ( node . left != null ) parent [ node . left . data ] = node . data ; if ( node . right != null ) parent [ node . right . data ] = node . data ; updateParent ( parent , node . left ) ; updateParent ( parent , node . right ) ; } static int getMaxXor ( Node root , int n , int val , int x ) { int parent [ ] = new int [ n + 1 ] ; updateParent ( parent , root ) ; int max = x ^ val ; val = parent [ val ] ; while ( val != 0 ) { max = Math . max ( max , x ^ val ) ; val = parent [ val ] ; } return max ; } public static void main ( String [ ] args ) { int n = 6 ; Node root = new Node ( 1 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 3 ) ; root . left . left = new Node ( 4 ) ; root . right . right = new Node ( 5 ) ; root . right . right . left = new Node ( 6 ) ; int val = 6 , x = 4 ; System . out . println ( getMaxXor ( root , n , val , x ) ) ; } }

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 updateParent ( parent , node ) : NEW_LINE INDENT if ( node == None ) : NEW_LINE INDENT return parent NEW_LINE DEDENT if ( node . left != None ) : NEW_LINE INDENT parent [ node . left . data ] = node . data NEW_LINE DEDENT if ( node . right != None ) : NEW_LINE INDENT parent [ node . right . data ] = node . data NEW_LINE DEDENT parent = updateParent ( parent , node . left ) NEW_LINE parent = updateParent ( parent , node . right ) NEW_LINE return parent NEW_LINE DEDENT def getMaxXor ( root , n , val , x ) : NEW_LINE INDENT parent = [ 0 ] * ( n + 1 ) NEW_LINE parent = updateParent ( parent , root ) NEW_LINE maximum = x ^ val NEW_LINE val = parent [ val ] NEW_LINE while ( val != 0 ) : NEW_LINE INDENT maximum = max ( maximum , x ^ val ) NEW_LINE val = parent [ val ] NEW_LINE DEDENT return maximum NEW_LINE DEDENT n = 6 NEW_LINE root = Node ( 1 ) NEW_LINE root . left = Node ( 2 ) NEW_LINE root . right = Node ( 3 ) NEW_LINE root . left . left = Node ( 4 ) NEW_LINE root . right . right = Node ( 5 ) NEW_LINE root . right . right . left = Node ( 6 ) NEW_LINE val = 6 NEW_LINE x = 4 NEW_LINE print ( getMaxXor ( root , n , val , x ) ) NEW_LINE


class GFG { static int number_of_ways ( int n ) { int [ ] includes_3 = new int [ n + 1 ] ; int [ ] not_includes_3 = new int [ n + 1 ] ; includes_3 [ 3 ] = 1 ; not_includes_3 [ 1 ] = 1 ; not_includes_3 [ 2 ] = 2 ; not_includes_3 [ 3 ] = 3 ; for ( int i = 4 ; i <= n ; i ++ ) { includes_3 [ i ] = includes_3 [ i - 1 ] + includes_3 [ i - 2 ] + not_includes_3 [ i - 3 ] ; not_includes_3 [ i ] = not_includes_3 [ i - 1 ] + not_includes_3 [ i - 2 ] ; } return includes_3 [ n ] ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . print ( number_of_ways ( n ) ) ; } }

def number_of_ways ( n ) : NEW_LINE INDENT includes_3 = [ 0 ] * ( n + 1 ) NEW_LINE not_includes_3 = [ 0 ] * ( n + 1 ) NEW_LINE includes_3 [ 3 ] = 1 NEW_LINE not_includes_3 [ 1 ] = 1 NEW_LINE not_includes_3 [ 2 ] = 2 NEW_LINE not_includes_3 [ 3 ] = 3 NEW_LINE for i in range ( 4 , n + 1 ) : NEW_LINE INDENT includes_3 [ i ] = includes_3 [ i - 1 ] +   \ NEW_LINE INDENT includes_3 [ i - 2 ] +   \ NEW_LINE not_includes_3 [ i - 3 ] NEW_LINE DEDENT not_includes_3 [ i ] = not_includes_3 [ i - 1 ] +   \ NEW_LINE INDENT not_includes_3 [ i - 2 ] NEW_LINE DEDENT DEDENT return includes_3 [ n ] NEW_LINE DEDENT n = 7 NEW_LINE print ( number_of_ways ( n ) ) NEW_LINE


class GFG { static int equivalentBase4 ( String bin ) { if ( bin . compareTo ( "00" ) == 0 ) return 0 ; if ( bin . compareTo ( "01" ) == 0 ) return 1 ; if ( bin . compareTo ( "10" ) == 0 ) return 2 ; return 3 ; } static String isDivisibleBy5 ( String bin ) { int l = bin . length ( ) ; if ( l % 2 != 0 ) bin = '0' + bin ; int odd_sum = 0 , even_sum = 0 ; int isOddDigit = 1 ; for ( int i = 0 ; i < bin . length ( ) ; i += 2 ) { if ( isOddDigit != 0 ) odd_sum += equivalentBase4 ( bin . substring ( i , i + 2 ) ) ; else even_sum += equivalentBase4 ( bin . substring ( i , i + 2 ) ) ; isOddDigit ^= 1 ; } if ( Math . abs ( odd_sum - even_sum ) % 5 == 0 ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { String bin = "10000101001" ; System . out . println ( isDivisibleBy5 ( bin ) ) ; } }

def equivalentBase4 ( bin ) : NEW_LINE INDENT if ( bin == "00" ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( bin == "01" ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT if ( bin == "10" ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT if ( bin == "11" ) : NEW_LINE INDENT return 3 NEW_LINE DEDENT DEDENT def isDivisibleBy5 ( bin ) : NEW_LINE INDENT l = len ( bin ) NEW_LINE if ( ( l % 2 ) == 1 ) : NEW_LINE INDENT bin = '0' + bin NEW_LINE DEDENT odd_sum = 0 NEW_LINE even_sum = 0 NEW_LINE isOddDigit = 1 NEW_LINE for i in range ( 0 , len ( bin ) , 2 ) : NEW_LINE INDENT if ( isOddDigit ) : NEW_LINE INDENT odd_sum += equivalentBase4 ( bin [ i : i + 2 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT even_sum += equivalentBase4 ( bin [ i : i + 2 ] ) NEW_LINE DEDENT isOddDigit = isOddDigit ^ 1 NEW_LINE DEDENT if ( abs ( odd_sum - even_sum ) % 5 == 0 ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT else : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT bin = "10000101001" NEW_LINE print ( isDivisibleBy5 ( bin ) ) NEW_LINE DEDENT


class GFG { static final int DP_s = 9 ; static int getNumMonotone ( int len ) { int [ ] [ ] DP = new int [ len ] [ DP_s ] ; for ( int i = 0 ; i < DP_s ; ++ i ) DP [ 0 ] [ i ] = i + 1 ; for ( int i = 0 ; i < len ; ++ i ) DP [ i ] [ 0 ] = 1 ; for ( int i = 1 ; i < len ; ++ i ) for ( int j = 1 ; j < DP_s ; ++ j ) DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ] ; return DP [ len - 1 ] [ DP_s - 1 ] ; } public static void main ( String [ ] args ) { System . out . println ( getNumMonotone ( 10 ) ) ; } }

DP_s = 9 NEW_LINE def getNumMonotone ( ln ) : NEW_LINE INDENT DP = [ [ 0 ] * DP_s for i in range ( ln ) ] NEW_LINE for i in range ( DP_s ) : NEW_LINE INDENT DP [ 0 ] [ i ] = i + 1 NEW_LINE DEDENT for i in range ( ln ) : NEW_LINE INDENT DP [ i ] [ 0 ] = 1 NEW_LINE DEDENT for i in range ( 1 , ln ) : NEW_LINE INDENT for j in range ( 1 , DP_s ) : NEW_LINE INDENT DP [ i ] [ j ] = DP [ i - 1 ] [ j ] + DP [ i ] [ j - 1 ] NEW_LINE DEDENT DEDENT return DP [ ln - 1 ] [ DP_s - 1 ] NEW_LINE DEDENT print ( getNumMonotone ( 10 ) ) NEW_LINE


import java . util . * ; class GFG { static int lis ( int [ ] arr , int n ) { int max = 0 ; int [ ] lst = new int [ n ] ; Arrays . fill ( lst , 1 ) ; for ( int i = 1 ; i < n ; i ++ ) { for ( int j = 0 ; j < i ; j ++ ) { if ( arr [ i ] > arr [ j ] && lst [ i ] < lst [ j ] + 1 ) lst [ i ] = lst [ j ] + 1 ; } } for ( int i = 0 ; i < n ; i ++ ) if ( max < lst [ i ] ) max = lst [ i ] ; return max ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = arr . length ; System . out . println ( " Length ▁ of ▁ lis ▁ is ▁ " + lis ( arr , n ) ) ; } }

def lis ( arr , n ) : NEW_LINE INDENT i , j , maxm = 0 , 0 , 0 NEW_LINE lst = [ 1 for s in range ( n ) ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT for j in range ( 0 , i ) : NEW_LINE INDENT if ( arr [ i ] > arr [ j ] and lst [ i ] < lst [ j ] + 1 ) : NEW_LINE INDENT lst [ i ] = lst [ j ] + 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if maxm < lst [ i ] : NEW_LINE INDENT maxm = lst [ i ] NEW_LINE DEDENT DEDENT return maxm NEW_LINE DEDENT arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Length ▁ of ▁ lst ▁ is " , lis ( arr , n ) ) NEW_LINE


class GFG { static int findSubsequence ( int arr [ ] , int n ) { int len = 1 ; int tmp ; int i , j , d ; int [ ] [ ] dp = new int [ n ] [ 10 ] ; int [ ] cnt = new int [ 10 ] ; int locMax ; tmp = arr [ 0 ] ; while ( tmp > 0 ) { dp [ 0 ] [ tmp % 10 ] = 1 ; tmp /= 10 ; } for ( i = 1 ; i < n ; i ++ ) { tmp = arr [ i ] ; locMax = 1 ; for ( int x = 0 ; x < 10 ; x ++ ) cnt [ x ] = 0 ; while ( tmp > 0 ) { cnt [ tmp % 10 ] = 1 ; tmp /= 10 ; } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i ] [ d ] = 1 ; for ( j = 0 ; j < i ; j ++ ) { dp [ i ] [ d ] = Math . max ( dp [ i ] [ d ] , dp [ j ] [ d ] + 1 ) ; locMax = Math . max ( dp [ i ] [ d ] , locMax ) ; } } } for ( d = 0 ; d <= 9 ; d ++ ) { if ( cnt [ d ] > 0 ) { dp [ i ] [ d ] = locMax ; } } len = Math . max ( len , locMax ) ; } return len ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 12 , 44 , 29 , 33 , 96 , 89 } ; int n = arr . length ; System . out . println ( findSubsequence ( arr , n ) ) ; } }

def findSubsequence ( arr , n ) : NEW_LINE INDENT Len = 1 NEW_LINE tmp = 0 NEW_LINE i , j , d = 0 , 0 , 0 NEW_LINE dp = [ [ 0 for i in range ( 10 ) ] for i in range ( n ) ] NEW_LINE cnt = [ 0 for i in range ( 10 ) ] NEW_LINE locMax = 0 NEW_LINE tmp = arr [ 0 ] NEW_LINE while ( tmp > 0 ) : NEW_LINE INDENT dp [ 0 ] [ tmp % 10 ] = 1 NEW_LINE tmp //= 10 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT tmp = arr [ i ] NEW_LINE locMax = 1 NEW_LINE cnt = [ 0 for i in range ( 10 ) ] NEW_LINE while ( tmp > 0 ) : NEW_LINE INDENT cnt [ tmp % 10 ] = 1 NEW_LINE tmp //= 10 NEW_LINE DEDENT for d in range ( 10 ) : NEW_LINE INDENT if ( cnt [ d ] ) : NEW_LINE INDENT dp [ i ] [ d ] = 1 NEW_LINE for j in range ( i ) : NEW_LINE INDENT dp [ i ] [ d ] = max ( dp [ i ] [ d ] , dp [ j ] [ d ] + 1 ) NEW_LINE locMax = max ( dp [ i ] [ d ] , locMax ) NEW_LINE DEDENT DEDENT DEDENT for d in range ( 10 ) : NEW_LINE INDENT if ( cnt [ d ] ) : NEW_LINE INDENT dp [ i ] [ d ] = locMax NEW_LINE DEDENT DEDENT Len = max ( Len , locMax ) NEW_LINE DEDENT return Len NEW_LINE DEDENT arr = [ 1 , 12 , 44 , 29 , 33 , 96 , 89 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findSubsequence ( arr , n ) ) NEW_LINE


public class GFG { static int reversDigits ( int num ) { int rev_num = 0 ; while ( num > 0 ) { rev_num = rev_num * 10 + num % 10 ; num = num / 10 ; } return rev_num ; } static void icanobifNumbers ( int N ) { int first = 0 , second = 1 ; if ( N == 1 ) System . out . print ( first ) ; else if ( N == 2 ) System . out . print ( first + " ▁ " + second ) ; else { System . out . print ( first + " ▁ " + second + " ▁ " ) ; for ( int i = 3 ; i <= N ; i ++ ) { int x = reversDigits ( first ) ; int y = reversDigits ( second ) ; System . out . print ( x + y + " ▁ " ) ; int temp = second ; second = x + y ; first = temp ; } } } public static void main ( String [ ] args ) { int N = 12 ; icanobifNumbers ( N ) ; } }

def reversedigit ( num ) : NEW_LINE INDENT rev_num = 0 NEW_LINE while num > 0 : NEW_LINE INDENT rev_num = rev_num * 10 + num % 10 NEW_LINE num = num // 10 NEW_LINE DEDENT return rev_num NEW_LINE DEDENT def icanobifNumbers ( N ) : NEW_LINE INDENT first = 0 NEW_LINE second = 1 NEW_LINE if N == 1 : NEW_LINE INDENT print ( first ) NEW_LINE DEDENT elif N == 2 : NEW_LINE INDENT print ( first , second ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( first , second , end = " ▁ " ) NEW_LINE for i in range ( 3 , N + 1 ) : NEW_LINE INDENT x = reversedigit ( first ) NEW_LINE y = reversedigit ( second ) NEW_LINE print ( x + y , end = " ▁ " ) NEW_LINE temp = second NEW_LINE second = x + y NEW_LINE first = temp NEW_LINE DEDENT DEDENT DEDENT N = 12 NEW_LINE icanobifNumbers ( N ) NEW_LINE


class GFG { static final int MAX = 1000 ; static int max_prime [ ] = new int [ MAX ] ; static int min_prime [ ] = new int [ MAX ] ; static void sieve ( int n ) { for ( int i = 2 ; i < n ; ++ i ) { if ( min_prime [ i ] > 0 ) { continue ; } min_prime [ i ] = i ; max_prime [ i ] = i ; int j = i + i ; while ( j < n ) { if ( min_prime [ j ] == 0 ) { min_prime [ j ] = i ; } max_prime [ j ] = i ; j += i ; } } } static int findCost ( int A , int B , int C , int X ) { sieve ( MAX ) ; int N , M ; if ( X == 1 ) { return - 1 ; } int min_cost = C + X ; int cost_for_prime = A ; N = max_prime [ X ] ; if ( N != X ) { M = X / N ; cost_for_prime += M ; min_cost = Math . min ( min_cost , cost_for_prime ) ; } M = min_prime [ X ] ; N = X / M ; if ( N != min_prime [ N ] ) { int cost_for_comp = B + M ; min_cost = Math . min ( min_cost , cost_for_comp ) ; } return min_cost ; } public static void main ( String [ ] args ) { int A = 7 , B = 11 , C = 2 , X = 20 ; System . out . println ( findCost ( A , B , C , X ) ) ; } }

MAX = 10000 ; NEW_LINE max_prime = [ 0 ] * MAX ; NEW_LINE min_prime = [ 0 ] * MAX ; NEW_LINE def sieve ( n ) : NEW_LINE INDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( min_prime [ i ] > 0 ) : NEW_LINE INDENT continue ; NEW_LINE DEDENT min_prime [ i ] = i ; NEW_LINE max_prime [ i ] = i ; NEW_LINE j = i + i ; NEW_LINE while ( j < n ) : NEW_LINE INDENT if ( min_prime [ j ] == 0 ) : NEW_LINE INDENT min_prime [ j ] = i ; NEW_LINE DEDENT max_prime [ j ] = i ; NEW_LINE j += i ; NEW_LINE DEDENT DEDENT DEDENT def findCost ( A , B , C , X ) : NEW_LINE INDENT sieve ( MAX ) ; NEW_LINE if ( X == 1 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT min_cost = C + X ; NEW_LINE cost_for_prime = A ; NEW_LINE N = max_prime [ X ] ; NEW_LINE if ( N != X ) : NEW_LINE INDENT M = X // N ; NEW_LINE cost_for_prime += M ; NEW_LINE min_cost = min ( min_cost , cost_for_prime ) ; NEW_LINE DEDENT M = min_prime [ X ] ; NEW_LINE N = X // M ; NEW_LINE if ( N != min_prime [ N ] ) : NEW_LINE INDENT cost_for_comp = B + M ; NEW_LINE min_cost = min ( min_cost , cost_for_comp ) ; NEW_LINE DEDENT return min_cost ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 7 ; B = 11 ; C = 2 ; X = 20 ; NEW_LINE print ( findCost ( A , B , C , X ) ) ; NEW_LINE DEDENT


import java . util . * ; import java . io . * ; class GFG { static int binarySearch ( int arr [ ] , int low , int high , int x ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( x == arr [ mid ] ) return mid ; if ( x > arr [ mid ] ) return binarySearch ( arr , ( mid + 1 ) , high , x ) ; else return binarySearch ( arr , low , ( mid - 1 ) , x ) ; } return - 1 ; } static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 , i ; Arrays . sort ( arr ) ; for ( i = 0 ; i < n - 1 ; i ++ ) if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != - 1 ) count ++ ; return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }

def binarySearch ( arr , low , high , x ) : NEW_LINE INDENT if ( high >= low ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if x == arr [ mid ] : NEW_LINE INDENT return ( mid ) NEW_LINE DEDENT elif ( x > arr [ mid ] ) : NEW_LINE INDENT return binarySearch ( arr , ( mid + 1 ) , high , x ) NEW_LINE DEDENT else : NEW_LINE INDENT return binarySearch ( arr , low , ( mid - 1 ) , x ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def countPairsWithDiffK ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE arr . sort ( ) NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT if ( binarySearch ( arr , i + 1 , n - 1 , arr [ i ] + k ) != - 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( arr , n , k ) ) NEW_LINE


import java . io . * ; public class gfg { public int digitsum ( int n ) { int r = n % 9 ; if ( r == 0 ) return 9 ; else return r ; } public int find ( int n , int d ) { int minimum = 10 ; for ( int i = 0 ; i < 9 ; i ++ ) { int current = ( n + i * d ) ; minimum = Math . min ( minimum , digitsum ( current ) ) ; } return minimum ; } } class geek { public static void main ( String [ ] args ) { gfg g = new gfg ( ) ; int n = 2546 , d = 124 ; System . out . println ( " Minimum ▁ possible ▁ digitsum ▁ is ▁ : ▁ " + ( g . find ( n , d ) ) ) ; } }

def digitsum ( n ) : NEW_LINE INDENT r = n % 9 ; NEW_LINE if ( r == 0 ) : NEW_LINE INDENT return 9 ; NEW_LINE DEDENT else : NEW_LINE INDENT return r ; NEW_LINE DEDENT DEDENT def find ( n , d ) : NEW_LINE INDENT minimum = 10 ; NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT current = ( n + i * d ) ; NEW_LINE minimum = min ( minimum , digitsum ( current ) ) ; NEW_LINE DEDENT return minimum ; NEW_LINE DEDENT n = 2546 ; NEW_LINE d = 124 ; NEW_LINE print ( " Minimum ▁ possible ▁ digitsum ▁ is ▁ : " , find ( n , d ) ) ; NEW_LINE


class GFG { static int countSubSeq ( int i , int sum , int cnt , int a [ ] , int n ) { if ( i == n ) { if ( sum == 0 && cnt > 0 ) { return 1 ; } else { return 0 ; } } int ans = 0 ; ans += countSubSeq ( i + 1 , sum , cnt , a , n ) ; ans += countSubSeq ( i + 1 , sum + a [ i ] , cnt + 1 , a , n ) ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { - 1 , 2 , - 2 , 1 } ; int n = a . length ; System . out . println ( countSubSeq ( 0 , 0 , 0 , a , n ) ) ; } }

def countSubSeq ( i , Sum , cnt , a , n ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT if ( Sum == 0 and cnt > 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE ans += countSubSeq ( i + 1 , Sum , cnt , a , n ) NEW_LINE ans += countSubSeq ( i + 1 , Sum + a [ i ] , cnt + 1 , a , n ) NEW_LINE return ans NEW_LINE DEDENT a = [ - 1 , 2 , - 2 , 1 ] NEW_LINE n = len ( a ) NEW_LINE print ( countSubSeq ( 0 , 0 , 0 , a , n ) ) NEW_LINE


class GFG { static int countNumbers ( int x , int n ) { if ( x > n ) return 0 ; return 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ; } public static void main ( String [ ] args ) { int n = 120 ; System . out . println ( countNumbers ( 1 , n ) ) ; } }

def countNumbers ( x , n ) : NEW_LINE INDENT if x > n : NEW_LINE INDENT return 0 NEW_LINE DEDENT return ( 1 + countNumbers ( x * 10 , n ) + countNumbers ( x * 10 + 1 , n ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 120 ; NEW_LINE print ( countNumbers ( 1 , n ) ) ; NEW_LINE DEDENT


import java . util . Arrays ; public class GFG { static int solution ( int [ ] A ) { int n = A . length ; boolean [ ] present = new boolean [ n + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { if ( A [ i ] > 0 && A [ i ] <= n ) present [ A [ i ] ] = true ; } for ( int i = 1 ; i <= n ; i ++ ) if ( ! present [ i ] ) return i ; return n + 1 ; } public static void main ( String [ ] args ) { int A [ ] = { 0 , 10 , 2 , - 10 , - 20 } ; System . out . println ( solution ( A ) ) ; } }

def solution ( A ) : NEW_LINE INDENT m = max ( A ) NEW_LINE if m < 1 : NEW_LINE INDENT return 1 NEW_LINE DEDENT if len ( A ) == 1 : NEW_LINE INDENT return 2 if A [ 0 ] == 1 else 1 NEW_LINE DEDENT l = [ 0 ] * m NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT if A [ i ] > 0 : NEW_LINE INDENT if l [ A [ i ] - 1 ] != 1 : NEW_LINE INDENT l [ A [ i ] - 1 ] = 1 NEW_LINE DEDENT DEDENT DEDENT for i in range ( len ( l ) ) : NEW_LINE INDENT if l [ i ] == 0 : NEW_LINE INDENT return i + 1 NEW_LINE DEDENT DEDENT return i + 2 NEW_LINE DEDENT A = [ 0 , 10 , 2 , - 10 , - 20 ] NEW_LINE print ( solution ( A ) ) NEW_LINE


import java . io . * ; class GFG { static void printUpperHalf ( String str ) { char first , second ; int pos = 0 ; for ( int i = 1 ; i <= 4 ; i ++ ) { first = str . charAt ( pos ) ; second = str . charAt ( pos + 1 ) ; pos += 2 ; for ( int j = 4 - i ; j >= 1 ; j -- ) System . out . print ( " ▁ " ) ; System . out . print ( first ) ; for ( int j = 1 ; j < i ; j ++ ) System . out . print ( " - - " ) ; System . out . println ( second ) ; } } static void printLowerHalf ( String str ) { char first , second ; int pos = 0 ; for ( int i = 1 ; i <= 4 ; i ++ ) { first = str . charAt ( pos ) ; second = str . charAt ( pos + 1 ) ; pos += 2 ; for ( int j = 1 ; j < i ; j ++ ) System . out . print ( " ▁ " ) ; System . out . print ( first ) ; for ( int j = 4 - i ; j >= 1 ; j -- ) System . out . print ( " - - " ) ; System . out . println ( second ) ; } } static void printDNA ( String str [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int x = i % 6 ; if ( x % 2 == 0 ) printUpperHalf ( str [ x ] ) ; else printLowerHalf ( str [ x ] ) ; } } public static void main ( String [ ] args ) { int n = 8 ; String DNA [ ] = { " ATTAATTA " , " TAGCTAGC " , " CGCGATAT " , " TAATATGC " , " ATCGTACG " , " CGTAGCAT " } ; printDNA ( DNA , n ) ; } }

import math NEW_LINE def printUpperHalf ( str ) : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE pos = 0 NEW_LINE for i in range ( 1 , 5 ) : NEW_LINE INDENT first = str [ pos ] NEW_LINE second = str [ pos + 1 ] NEW_LINE pos += 2 NEW_LINE for j in range ( 4 - i , 0 , - 1 ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT print ( first , end = " " ) NEW_LINE for j in range ( 1 , i ) : NEW_LINE INDENT print ( " - - " , end = " " ) NEW_LINE DEDENT print ( second ) NEW_LINE DEDENT DEDENT def printLowerHalf ( str ) : NEW_LINE INDENT first = 0 NEW_LINE second = 0 NEW_LINE pos = 0 NEW_LINE for i in range ( 1 , 5 ) : NEW_LINE INDENT first = str [ pos ] NEW_LINE second = str [ pos + 1 ] NEW_LINE pos += 2 NEW_LINE for j in range ( 1 , i ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT print ( first , end = " " ) NEW_LINE for j in range ( 4 - i , 0 , - 1 ) : NEW_LINE INDENT print ( " - - " , end = " " ) NEW_LINE DEDENT print ( second ) NEW_LINE DEDENT DEDENT def printDNA ( str , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT x = i % 6 NEW_LINE if ( x % 2 == 0 ) : NEW_LINE INDENT printUpperHalf ( str [ x ] ) NEW_LINE DEDENT else : NEW_LINE INDENT printLowerHalf ( str [ x ] ) NEW_LINE DEDENT DEDENT DEDENT n = 8 NEW_LINE DNA = [ " ATTAATTA " , " TAGCTAGC " , " CGCGATAT " , " TAATATGC " , " ATCGTACG " , " CGTAGCAT " ] NEW_LINE printDNA ( DNA , n ) NEW_LINE


import java . util . Arrays ; import java . util . Collections ; public class GFG { static void twoWaySort ( Integer arr [ ] , int n ) { int l = 0 , r = n - 1 ; int k = 0 ; while ( l < r ) { while ( arr [ l ] % 2 != 0 ) { l ++ ; k ++ ; } while ( arr [ r ] % 2 == 0 && l < r ) r -- ; if ( l < r ) { int temp = arr [ l ] ; arr [ l ] = arr [ r ] ; arr [ r ] = temp ; } } Arrays . sort ( arr , 0 , k , Collections . reverseOrder ( ) ) ; Arrays . sort ( arr , k , n ) ; } public static void main ( String [ ] args ) { Integer arr [ ] = { 1 , 3 , 2 , 7 , 5 , 4 } ; twoWaySort ( arr , arr . length ) ; System . out . println ( Arrays . toString ( arr ) ) ; } }

def two_way_sort ( arr , arr_len ) : NEW_LINE INDENT l , r = 0 , arr_len - 1 NEW_LINE k = 0 NEW_LINE while ( l < r ) : NEW_LINE INDENT while ( arr [ l ] % 2 != 0 ) : NEW_LINE INDENT l += 1 NEW_LINE k += 1 NEW_LINE DEDENT while ( arr [ r ] % 2 == 0 and l < r ) : NEW_LINE INDENT r -= 1 NEW_LINE DEDENT if ( l < r ) : NEW_LINE INDENT arr [ l ] , arr [ r ] = arr [ r ] , arr [ l ] NEW_LINE DEDENT DEDENT odd = arr [ : k ] NEW_LINE even = arr [ k : ] NEW_LINE odd . sort ( reverse = True ) NEW_LINE even . sort ( ) NEW_LINE odd . extend ( even ) NEW_LINE return odd NEW_LINE DEDENT arr_len = 6 NEW_LINE arr = [ 1 , 3 , 2 , 7 , 5 , 4 ] NEW_LINE result = two_way_sort ( arr , arr_len ) NEW_LINE for i in result : NEW_LINE INDENT print ( str ( i ) + " ▁ " ) , NEW_LINE DEDENT


class GFG { static class Node { int data ; Node left , right ; Node ( int data ) { this . data = data ; left = right = null ; } } ; static boolean isBSTUtil ( Node root , int prev ) { if ( root != null ) { if ( ! isBSTUtil ( root . left , prev ) ) return false ; if ( root . data <= prev ) return false ; prev = root . data ; return isBSTUtil ( root . right , prev ) ; } return true ; } static boolean isBST ( Node root ) { int prev = Integer . MIN_VALUE ; return isBSTUtil ( root , prev ) ; } public static void main ( String [ ] args ) { Node root = new Node ( 5 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 15 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 4 ) ; if ( isBST ( root ) ) System . out . print ( " Is ▁ BST " ) ; else System . out . print ( " Not ▁ a ▁ BST " ) ; } }

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 isBSTUtil ( root , prev ) : NEW_LINE INDENT if ( root ) : NEW_LINE INDENT if ( not isBSTUtil ( root . left , prev ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( root . data <= prev ) : NEW_LINE INDENT return False NEW_LINE DEDENT prev = root . data NEW_LINE return isBSTUtil ( root . right , prev ) NEW_LINE DEDENT return True NEW_LINE DEDENT def isBST ( root ) : NEW_LINE INDENT prev = - ( 2 ** 32 ) NEW_LINE return isBSTUtil ( root , prev ) NEW_LINE DEDENT root = newNode ( 5 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 15 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 4 ) 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 int percent ( int a , int b ) { float result = 0 ; result = ( ( b - a ) * 100 ) / a ; return ( int ) result ; } public static void main ( String [ ] args ) { int a = 20 , b = 25 ; System . out . println ( percent ( a , b ) + " % " ) ; } }

def percent ( a , b ) : NEW_LINE INDENT result = int ( ( ( b - a ) * 100 ) / a ) NEW_LINE return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 20 , 25 NEW_LINE print ( percent ( a , b ) , " % " ) NEW_LINE DEDENT


class GFG { public static void main ( String args [ ] ) { int n = 5 ; int a [ ] [ ] = { { 1 , 2 , 3 , 4 , 5 } , { 5 , 6 , 7 , 8 , 6 } , { 9 , 5 , 6 , 8 , 7 } , { 2 , 3 , 5 , 6 , 8 } , { 1 , 2 , 3 , 4 , 5 } } ; int convergingele , mid ; int i , j ; if ( n % 2 == 0 ) { System . out . printf ( " NULL \n " ) ; } else { mid = n / 2 ; convergingele = a [ mid ] [ mid ] ; System . out . printf ( " % d \n " , convergingele ) ; } } }

n = 5 NEW_LINE a = [ [ 1 , 2 , 3 , 4 , 5 ] , [ 5 , 6 , 7 , 8 , 6 ] , [ 9 , 5 , 6 , 8 , 7 ] , [ 2 , 3 , 5 , 6 , 8 ] , [ 1 , 2 , 3 , 4 , 5 ] ] NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT print ( " NULL " ) NEW_LINE DEDENT else : NEW_LINE INDENT mid = n // 2 NEW_LINE convergingele = a [ mid ] [ mid ] NEW_LINE print ( convergingele ) NEW_LINE DEDENT


class GFG { static void moveSpaceInFront ( char str [ ] ) { int i = str . length - 1 ; for ( int j = i ; j >= 0 ; j -- ) if ( str [ j ] != ' ▁ ' ) { char c = str [ i ] ; str [ i ] = str [ j ] ; str [ j ] = c ; i -- ; } } public static void main ( String [ ] args ) { char str [ ] = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " . toCharArray ( ) ; moveSpaceInFront ( str ) ; System . out . println ( String . valueOf ( str ) ) ; } }

def moveSpaceInFront ( s ) : NEW_LINE INDENT i = len ( s ) - 1 ; NEW_LINE for j in range ( i , - 1 , - 1 ) : NEW_LINE INDENT if ( s [ j ] != ' ▁ ' ) : NEW_LINE INDENT s = swap ( s , i , j ) ; NEW_LINE i -= 1 ; NEW_LINE DEDENT DEDENT return s ; NEW_LINE DEDENT def swap ( c , i , j ) : NEW_LINE INDENT c = list ( c ) NEW_LINE c [ i ] , c [ j ] = c [ j ] , c [ i ] NEW_LINE return ' ' . join ( c ) NEW_LINE DEDENT s = " Hey ▁ there , ▁ it ' s ▁ GeeksforGeeks " ; NEW_LINE s = moveSpaceInFront ( s ) ; NEW_LINE print ( s ) ; NEW_LINE


import java . lang . Math ; public class GfG { public static boolean is_possible ( String s ) { int l = s . length ( ) ; int one = 0 , zero = 0 ; for ( int i = 0 ; i < l ; i ++ ) { if ( s . charAt ( i ) == '0' ) zero ++ ; else one ++ ; } if ( l % 2 == 0 ) return ( one == zero ) ; else return ( Math . abs ( one - zero ) == 1 ) ; } public static void main ( String [ ] args ) { String s = "100110" ; if ( is_possible ( s ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def is_possible ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE one = 0 NEW_LINE zero = 0 NEW_LINE for i in range ( 0 , l ) : NEW_LINE INDENT if ( s [ i ] == '0' ) : NEW_LINE INDENT zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT one += 1 NEW_LINE DEDENT DEDENT if ( l % 2 == 0 ) : NEW_LINE INDENT return ( one == zero ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( abs ( one - zero ) == 1 ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "100110" NEW_LINE if ( is_possible ( s ) ) : 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 phi ( int n ) { float result = n ; for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) n /= p ; result *= ( 1.0 - ( 1.0 / ( float ) p ) ) ; } } if ( n > 1 ) result *= ( 1.0 - ( 1.0 / ( float ) n ) ) ; return ( int ) result ; } public static void main ( String args [ ] ) { int n ; for ( n = 1 ; n <= 10 ; n ++ ) System . out . println ( " phi ( " + n + " ) ▁ = ▁ " + phi ( n ) ) ; } }

def phi ( n ) : NEW_LINE INDENT result = n NEW_LINE p = 2 NEW_LINE while ( p * p <= n ) : NEW_LINE INDENT if ( n % p == 0 ) : NEW_LINE INDENT while ( n % p == 0 ) : NEW_LINE INDENT n = n // p NEW_LINE DEDENT result = result * ( 1.0 - ( 1.0 / ( float ) ( p ) ) ) NEW_LINE DEDENT p = p + 1 NEW_LINE DEDENT if ( n > 1 ) : NEW_LINE INDENT result = result * ( 1.0 - ( 1.0 / ( float ) ( n ) ) ) NEW_LINE DEDENT return ( int ) ( result ) NEW_LINE DEDENT for n in range ( 1 , 11 ) : NEW_LINE INDENT print ( " phi ( " , n , " ) ▁ = ▁ " , phi ( n ) ) NEW_LINE DEDENT


import java . util . * ; public class GFG { static int maxLength ( int arr [ ] , int N ) { int product = 1 , len = 0 ; for ( int i = 0 ; i < N ; i ++ ) { product *= arr [ i ] ; } if ( product >= 0 ) { return N ; } for ( int i = 0 ; i < N ; i ++ ) { if ( arr [ i ] < 0 ) { len = Math . max ( len , Math . max ( N - i - 1 , i ) ) ; } } return len ; } public static void main ( String args [ ] ) { int arr [ ] = { - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 } ; int N = arr . length ; System . out . println ( maxLength ( arr , N ) ) ; } }

def maxLength ( arr , N ) : NEW_LINE INDENT product = 1 NEW_LINE Len = 0 NEW_LINE for i in arr : NEW_LINE INDENT product *= i NEW_LINE DEDENT if ( product >= 0 ) : NEW_LINE INDENT return N NEW_LINE DEDENT for i in range ( N ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT Len = max ( Len , max ( N - i - 1 , i ) ) NEW_LINE DEDENT DEDENT return Len NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 1 , 1 , 1 , - 2 , 3 , 2 , - 1 ] NEW_LINE N = len ( arr ) NEW_LINE print ( maxLength ( arr , N ) ) NEW_LINE DEDENT


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

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


import java . util . * ; class GFG { static void minSet ( ArrayList < Integer > nums ) { ArrayList < Integer > A = new ArrayList < Integer > ( ) ; Collections . sort ( nums ) ; int sum = 0 ; for ( int i = 0 ; i < nums . size ( ) ; i ++ ) sum += nums . get ( i ) ; int temp = 0 ; for ( int i = nums . size ( ) - 1 ; i >= 0 && temp <= sum / 2 ; i -- ) { A . add ( nums . get ( i ) ) ; temp += nums . get ( i ) ; } for ( int i = 0 ; i < A . size ( ) ; i ++ ) System . out . print ( A . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { ArrayList < Integer > gfg = new ArrayList < Integer > ( ) ; gfg . add ( 7 ) ; gfg . add ( 6 ) ; gfg . add ( 13 ) ; gfg . add ( 13 ) ; gfg . add ( 12 ) ; gfg . add ( 11 ) ; minSet ( gfg ) ; } }

def minSet ( nums ) : NEW_LINE INDENT A = [ ] NEW_LINE nums . sort ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( 0 , len ( nums ) ) : NEW_LINE INDENT sum += nums [ i ] NEW_LINE DEDENT temp = 0 NEW_LINE for i in range ( len ( nums ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( temp > sum / 2 ) : NEW_LINE INDENT break NEW_LINE DEDENT A . append ( nums [ i ] ) NEW_LINE temp += nums [ i ] NEW_LINE DEDENT for i in range ( 0 , len ( A ) ) : NEW_LINE INDENT print ( A [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT DEDENT vec = [ 7 , 6 , 13 , 13 , 12 , 11 ] NEW_LINE minSet ( vec ) ; NEW_LINE


import java . util . * ; class GFG { static boolean checkPair ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) { sum += arr [ i ] ; } if ( sum % 2 != 0 ) { return false ; } sum = sum / 2 ; HashSet < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { int val = sum - arr [ i ] ; if ( s . contains ( val ) && val == ( int ) s . toArray ( ) [ s . size ( ) - 1 ] ) { System . out . printf ( " Pair ▁ elements ▁ are ▁ % d ▁ and ▁ % d \n " , arr [ i ] , val ) ; return true ; } s . add ( arr [ i ] ) ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 11 , 5 , 1 , 4 , 7 } ; int n = arr . length ; if ( checkPair ( arr , n ) == false ) { System . out . printf ( " No ▁ pair ▁ found " ) ; } } }

def checkPair ( arr , n ) : NEW_LINE INDENT s = set ( ) NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT if sum % 2 != 0 : NEW_LINE INDENT return False NEW_LINE DEDENT sum = sum / 2 NEW_LINE for i in range ( n ) : NEW_LINE INDENT val = sum - arr [ i ] NEW_LINE if arr [ i ] not in s : NEW_LINE INDENT s . add ( arr [ i ] ) NEW_LINE DEDENT if val in s : NEW_LINE INDENT print ( " Pair ▁ elements ▁ are " , arr [ i ] , " and " , int ( val ) ) NEW_LINE DEDENT DEDENT DEDENT arr = [ 2 , 11 , 5 , 1 , 4 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE if checkPair ( arr , n ) == False : NEW_LINE INDENT print ( " No ▁ pair ▁ found " ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; public class GFG { static int missingK ( int [ ] a , int k , int n ) { int difference = 0 , ans = 0 , count = k ; boolean flag = false ; for ( int i = 0 ; i < n - 1 ; i ++ ) { difference = 0 ; if ( ( a [ i ] + 1 ) != a [ i + 1 ] ) { difference += ( a [ i + 1 ] - a [ i ] ) - 1 ; if ( difference >= count ) { ans = a [ i ] + count ; flag = true ; break ; } else count -= difference ; } } if ( flag ) return ans ; else return - 1 ; } public static void main ( String args [ ] ) { int [ ] a = { 1 , 5 , 11 , 19 } ; int k = 11 ; int n = a . length ; int missing = missingK ( a , k , n ) ; System . out . print ( missing ) ; } }

def missingK ( a , k , n ) : NEW_LINE INDENT difference = 0 NEW_LINE ans = 0 NEW_LINE count = k NEW_LINE flag = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT difference = 0 NEW_LINE if ( ( a [ i ] + 1 ) != a [ i + 1 ] ) : NEW_LINE INDENT difference += ( a [ i + 1 ] - a [ i ] ) - 1 NEW_LINE if ( difference >= count ) : NEW_LINE INDENT ans = a [ i ] + count NEW_LINE flag = 1 NEW_LINE break NEW_LINE DEDENT else : NEW_LINE INDENT count -= difference NEW_LINE DEDENT DEDENT DEDENT if ( flag ) : NEW_LINE INDENT return ans NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT a = [ 1 , 5 , 11 , 19 ] NEW_LINE k = 11 NEW_LINE n = len ( a ) NEW_LINE missing = missingK ( a , k , n ) NEW_LINE print ( missing ) NEW_LINE


import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node head ; static void push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head_ref ; head_ref = new_node ; head = head_ref ; } static void uniqueElements ( Node head ) { HashMap < Integer , Integer > hash = new HashMap < Integer , Integer > ( ) ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( hash . containsKey ( temp . data ) ) { hash . put ( temp . data , hash . get ( temp . data ) + 1 ) ; } else { hash . put ( temp . data , 1 ) ; } } int count = 0 ; for ( Node temp = head ; temp != null ; temp = temp . next ) { if ( hash . get ( temp . data ) == 1 ) { System . out . print ( temp . data + " ▁ " ) ; count ++ ; } } if ( count == 0 ) System . out . print ( " ▁ No ▁ Unique ▁ Elements ▁ " ) ; } public static void main ( String [ ] args ) { head = null ; push ( head , 5 ) ; push ( head , 4 ) ; push ( head , 3 ) ; push ( head , 5 ) ; push ( head , 3 ) ; push ( head , 2 ) ; push ( head , 4 ) ; push ( head , 4 ) ; push ( head , 1 ) ; uniqueElements ( head ) ; } }

import sys NEW_LINE 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 push ( head , data ) : NEW_LINE INDENT if not head : NEW_LINE INDENT return Node ( data ) NEW_LINE DEDENT temp = Node ( data ) NEW_LINE temp . next = head NEW_LINE head = temp NEW_LINE return head NEW_LINE DEDENT def uniqueElements ( head ) : NEW_LINE INDENT _map = { } NEW_LINE temp = head NEW_LINE while ( temp ) : NEW_LINE INDENT d = temp . data NEW_LINE if d in _map : NEW_LINE INDENT _map [ d ] = _map . get ( d ) + 1 NEW_LINE DEDENT else : NEW_LINE INDENT _map [ d ] = 1 NEW_LINE DEDENT temp = temp . next NEW_LINE DEDENT count = 0 NEW_LINE for i in _map : NEW_LINE INDENT if _map . get ( i ) == 1 : NEW_LINE INDENT count += 1 NEW_LINE print ( " { } ▁ " . format ( i ) , end = " " ) NEW_LINE DEDENT DEDENT if count == 0 : NEW_LINE INDENT print ( " No ▁ Unique ▁ Elements " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 3 ) NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 3 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 1 ) NEW_LINE uniqueElements ( head ) NEW_LINE DEDENT


import java . util . * ; class GFG { public static boolean check_elements ( int arr [ ] , int n , int A , int B ) { int range = B - A ; for ( int i = 0 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i ] ) >= A && Math . abs ( arr [ i ] ) <= B ) { int z = Math . abs ( arr [ i ] ) - A ; if ( arr [ z ] > 0 ) { arr [ z ] = arr [ z ] * - 1 ; } } } int count = 0 ; for ( int i = 0 ; i <= range && i < n ; i ++ ) { if ( arr [ i ] > 0 ) return false ; else count ++ ; } if ( count != ( range + 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 4 , 5 , 2 , 7 , 8 , 3 } ; int n = arr . length ; int A = 2 , B = 5 ; if ( check_elements ( arr , n , A , B ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def check_elements ( arr , n , A , B ) : NEW_LINE INDENT rangeV = B - A NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( abs ( arr [ i ] ) >= A and abs ( arr [ i ] ) <= B ) : NEW_LINE INDENT z = abs ( arr [ i ] ) - A NEW_LINE if ( arr [ z ] > 0 ) : NEW_LINE INDENT arr [ z ] = arr [ z ] * - 1 NEW_LINE DEDENT DEDENT DEDENT count = 0 NEW_LINE for i in range ( 0 , rangeV + 1 ) : NEW_LINE INDENT if i >= n : NEW_LINE INDENT break NEW_LINE DEDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT if ( count != ( rangeV + 1 ) ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT arr = [ 1 , 4 , 5 , 2 , 7 , 8 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE A = 2 NEW_LINE B = 5 NEW_LINE if ( check_elements ( arr , n , A , B ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; public class LychrelNumberTest { private static int MAX_ITERATIONS = 20 ; private static boolean isLychrel ( long number ) { for ( int i = 0 ; i < MAX_ITERATIONS ; i ++ ) { number = number + reverse ( number ) ; if ( isPalindrome ( number ) ) return false ; } return true ; } private static boolean isPalindrome ( final long number ) { return number == reverse ( number ) ; } private static long reverse ( long number ) { long reverse = 0 ; while ( number > 0 ) { long remainder = number % 10 ; reverse = ( reverse * 10 ) + remainder ; number = number / 10 ; } return reverse ; } public static void main ( String [ ] args ) { long number = 295 ; System . out . println ( number + " ▁ is ▁ lychrel ? ▁ " + isLychrel ( number ) ) ; } }

MAX_ITERATIONS = 20 ; NEW_LINE def isLychrel ( number ) : NEW_LINE INDENT for i in range ( MAX_ITERATIONS ) : NEW_LINE INDENT number = number + reverse ( number ) ; NEW_LINE if ( isPalindrome ( number ) ) : NEW_LINE INDENT return " false " ; NEW_LINE DEDENT DEDENT return " true " ; NEW_LINE DEDENT def isPalindrome ( number ) : NEW_LINE INDENT return number == reverse ( number ) ; NEW_LINE DEDENT def reverse ( number ) : NEW_LINE INDENT reverse = 0 ; NEW_LINE while ( number > 0 ) : NEW_LINE INDENT remainder = number % 10 ; NEW_LINE reverse = ( reverse * 10 ) + remainder ; NEW_LINE number = int ( number / 10 ) ; NEW_LINE DEDENT return reverse ; NEW_LINE DEDENT number = 295 ; NEW_LINE print ( number , " ▁ is ▁ lychrel ? ▁ " , isLychrel ( number ) ) ; NEW_LINE


class GFG { static int remainingArea ( int N , int M , int K ) { while ( K -- > 0 && N > 0 && M > 0 ) { if ( N > M ) N = N - M ; else M = M - N ; } if ( N > 0 && M > 0 ) return N * M ; else return 0 ; } public static void main ( String [ ] args ) { int N = 5 , M = 3 , K = 2 ; System . out . println ( remainingArea ( N , M , K ) ) ; } }

def remainingArea ( N , M , K ) : NEW_LINE INDENT while ( K > 0 and N > 0 and M > 0 ) : NEW_LINE INDENT if ( N > M ) : NEW_LINE INDENT N = N - M ; NEW_LINE DEDENT else : NEW_LINE INDENT M = M - N ; NEW_LINE DEDENT K = K - 1 ; NEW_LINE DEDENT if ( N > 0 and M > 0 ) : NEW_LINE INDENT return N * M ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT N = 5 ; NEW_LINE M = 3 ; NEW_LINE K = 2 ; NEW_LINE print ( remainingArea ( N , M , K ) ) ; NEW_LINE


public class GFG { static int maxZeros ( int N ) { int maxm = - 1 ; int cnt = 0 ; while ( N != 0 ) { if ( ( N & 1 ) == 0 ) { cnt ++ ; N >>= 1 ; maxm = Math . max ( maxm , cnt ) ; } else { maxm = Math . max ( maxm , cnt ) ; cnt = 0 ; N >>= 1 ; } } return maxm ; } public static void main ( String args [ ] ) { int N = 14 ; System . out . println ( maxZeros ( N ) ) ; } }

def maxZeros ( N ) : NEW_LINE INDENT maxm = - 1 NEW_LINE cnt = 0 NEW_LINE while ( N ) : NEW_LINE INDENT if ( not ( N & 1 ) ) : NEW_LINE INDENT cnt += 1 NEW_LINE N >>= 1 NEW_LINE maxm = max ( maxm , cnt ) NEW_LINE DEDENT else : NEW_LINE INDENT maxm = max ( maxm , cnt ) NEW_LINE cnt = 0 NEW_LINE N >>= 1 NEW_LINE DEDENT DEDENT return maxm NEW_LINE DEDENT N = 14 NEW_LINE print ( maxZeros ( N ) ) NEW_LINE


class GFG { static int min ( int x , int y , int z ) { return Math . min ( Math . min ( x , y ) , z ) ; } static int editDistDP ( String str1 , String str2 , int m , int n ) { int dp [ ] [ ] = new int [ m + 1 ] [ n + 1 ] ; for ( int i = 0 ; i <= m ; i ++ ) { for ( int j = 0 ; j <= n ; j ++ ) { if ( i == 0 ) dp [ i ] [ j ] = j ; else if ( j == 0 ) dp [ i ] [ j ] = i ; else if ( str1 . charAt ( i - 1 ) == str2 . charAt ( j - 1 ) ) dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] ; else dp [ i ] [ j ] = 1 + min ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) ; } } return dp [ m ] [ n ] ; } static boolean areKDistant ( String str1 , String str2 , int k ) { int m = str1 . length ( ) ; int n = str2 . length ( ) ; if ( Math . abs ( m - n ) > k ) return false ; return ( editDistDP ( str1 , str2 , m , n ) <= k ) ; } public static void main ( String [ ] args ) { String str1 = " geek " ; String str2 = " gks " ; int k = 3 ; if ( areKDistant ( str1 , str2 , k ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def minn ( x , y , z ) : NEW_LINE INDENT return min ( min ( x , y ) , z ) NEW_LINE DEDENT def editDistDP ( str1 , str2 , m , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( n + 1 ) ] for j in range ( m + 1 ) ] NEW_LINE for i in range ( m + 1 ) : NEW_LINE INDENT for j in range ( n + 1 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = j NEW_LINE DEDENT elif ( j == 0 ) : NEW_LINE INDENT dp [ i ] [ j ] = i NEW_LINE DEDENT elif ( str1 [ i - 1 ] == str2 [ j - 1 ] ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i - 1 ] [ j - 1 ] NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = 1 + minn ( dp [ i ] [ j - 1 ] , dp [ i - 1 ] [ j ] , dp [ i - 1 ] [ j - 1 ] ) NEW_LINE DEDENT DEDENT DEDENT return dp [ m ] [ n ] NEW_LINE DEDENT def areKDistant ( str1 , str2 , k ) : NEW_LINE INDENT m = len ( str1 ) NEW_LINE n = len ( str2 ) NEW_LINE if ( abs ( m - n ) > k ) : NEW_LINE INDENT return False NEW_LINE DEDENT return ( editDistDP ( str1 , str2 , m , n ) <= k ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " geek " NEW_LINE str2 = " gks " NEW_LINE k = 3 NEW_LINE if areKDistant ( str1 , str2 , k ) : 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 trailingZeroes ( int n ) { int cnt = 0 ; while ( n > 0 ) { n /= 5 ; cnt += n ; } return cnt ; } static void binarySearch ( int n ) { int low = 0 ; int high = 1000000 ; while ( low < high ) { int mid = ( low + high ) / 2 ; int count = trailingZeroes ( mid ) ; if ( count < n ) low = mid + 1 ; else high = mid ; } int result [ ] = new int [ 1000 ] ; int k = 0 ; while ( trailingZeroes ( low ) == n ) { result [ k ] = low ; k ++ ; low ++ ; } for ( int i = 0 ; i < k ; i ++ ) System . out . print ( result [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int n = 3 ; binarySearch ( n ) ; } }

def trailingZeroes ( n ) : NEW_LINE INDENT cnt = 0 NEW_LINE while n > 0 : NEW_LINE INDENT n = int ( n / 5 ) NEW_LINE cnt += n NEW_LINE DEDENT return cnt NEW_LINE DEDENT def binarySearch ( n ) : NEW_LINE INDENT low = 0 NEW_LINE high = 1e6 NEW_LINE while low < high : NEW_LINE INDENT mid = int ( ( low + high ) / 2 ) NEW_LINE count = trailingZeroes ( mid ) NEW_LINE if count < n : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT high = mid NEW_LINE DEDENT DEDENT result = list ( ) NEW_LINE while trailingZeroes ( low ) == n : NEW_LINE INDENT result . append ( low ) NEW_LINE low += 1 NEW_LINE DEDENT for i in range ( len ( result ) ) : NEW_LINE INDENT print ( result [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 2 NEW_LINE binarySearch ( n ) NEW_LINE


import java . io . * ; class GFG { static int sumDigits ( int no ) { return no == 0 ? 0 : no % 10 + sumDigits ( no / 10 ) ; } public static void main ( String [ ] args ) { System . out . println ( sumDigits ( 687 ) ) ; } }

def sumDigits ( no ) : NEW_LINE INDENT return 0 if no == 0 else int ( no % 10 ) + sumDigits ( int ( no / 10 ) ) NEW_LINE DEDENT print ( sumDigits ( 687 ) ) NEW_LINE


import java . util . * ; import java . lang . * ; public class GfG { public static int min_noOf_operation ( int arr [ ] , int n , int k ) { int noOfSubtraction ; int res = 0 ; for ( int i = 1 ; i < n ; i ++ ) { noOfSubtraction = 0 ; if ( arr [ i ] > arr [ i - 1 ] ) { noOfSubtraction = ( arr [ i ] - arr [ i - 1 ] ) / k ; if ( ( arr [ i ] - arr [ i - 1 ] ) % k != 0 ) noOfSubtraction ++ ; arr [ i ] = arr [ i ] - k * noOfSubtraction ; } res = res + noOfSubtraction ; } return res ; } public static void main ( String argc [ ] ) { int arr = { 1 , 1 , 2 , 3 } ; int N = 4 ; int k = 5 ; System . out . println ( min_noOf_operation ( arr , N , k ) ) ; } }

def min_noOf_operation ( arr , n , k ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT noOfSubtraction = 0 NEW_LINE if ( arr [ i ] > arr [ i - 1 ] ) : NEW_LINE INDENT noOfSubtraction = ( arr [ i ] - arr [ i - 1 ] ) / k ; NEW_LINE if ( ( arr [ i ] - arr [ i - 1 ] ) % k != 0 ) : NEW_LINE INDENT noOfSubtraction += 1 NEW_LINE DEDENT arr [ i ] = arr [ i ] - k * noOfSubtraction NEW_LINE DEDENT res = res + noOfSubtraction NEW_LINE DEDENT return int ( res ) NEW_LINE DEDENT arr = [ 1 , 1 , 2 , 3 ] NEW_LINE N = len ( arr ) NEW_LINE k = 5 NEW_LINE print ( min_noOf_operation ( arr , N , k ) ) NEW_LINE


class GFG { static boolean isKthBitSet ( int n , int k ) { if ( ( n >> ( k - 1 ) ) == 1 ) return true ; return false ; } static int setKthBit ( int n , int k ) { return ( ( 1 << ( k - 1 ) ) | n ) ; } static boolean allBitsAreSet ( int n ) { if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } static boolean bitsAreInAltOrder ( int n ) { int num = n ^ ( n >> 1 ) ; return allBitsAreSet ( num ) ; } static boolean bitsAreInAltPatrnInGivenRange ( int n , int l , int r ) { int num , left_shift ; if ( isKthBitSet ( n , r ) ) { num = n ; left_shift = r ; } else { num = setKthBit ( n , ( r + 1 ) ) ; left_shift = r + 1 ; } num = num & ( ( 1 << left_shift ) - 1 ) ; num = num >> ( l - 1 ) ; return bitsAreInAltOrder ( num ) ; } public static void main ( String [ ] args ) { int n = 18 ; int l = 1 , r = 3 ; if ( bitsAreInAltPatrnInGivenRange ( n , l , r ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isKthBitSet ( n , k ) : NEW_LINE INDENT if ( ( n >> ( k - 1 ) ) & 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def setKthBit ( n , k ) : NEW_LINE INDENT return ( ( 1 << ( k - 1 ) ) | n ) NEW_LINE DEDENT def allBitsAreSet ( n ) : NEW_LINE INDENT if ( ( ( n + 1 ) & n ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def bitsAreInAltOrder ( n ) : NEW_LINE INDENT num = n ^ ( n >> 1 ) NEW_LINE return allBitsAreSet ( num ) NEW_LINE DEDENT def bitsAreInAltPatrnInGivenRange ( n , l , r ) : NEW_LINE INDENT if ( isKthBitSet ( n , r ) ) : NEW_LINE INDENT num = n NEW_LINE left_shift = r NEW_LINE DEDENT else : NEW_LINE INDENT num = setKthBit ( n , ( r + 1 ) ) NEW_LINE left_shift = r + 1 NEW_LINE DEDENT num = num & ( ( 1 << left_shift ) - 1 ) NEW_LINE num = num >> ( l - 1 ) NEW_LINE return bitsAreInAltOrder ( num ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 18 NEW_LINE l = 1 NEW_LINE r = 3 NEW_LINE if ( bitsAreInAltPatrnInGivenRange ( n , l , r ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { static int usingBinarySearch ( int start , int end , int N , int S ) { if ( start >= end ) return start ; int mid = start + ( end - start ) / 2 ; int totalSum = ( N * ( N + 1 ) ) / 2 ; int midSum = ( mid * ( mid + 1 ) ) / 2 ; if ( ( totalSum - midSum ) <= S ) { return usingBinarySearch ( start , mid , N , S ) ; } return usingBinarySearch ( mid + 1 , end , N , S ) ; } public static void main ( String [ ] args ) { int N , S ; N = 5 ; S = 11 ; System . out . println ( N - usingBinarySearch ( 1 , N , N , S ) + 1 ) ; } }

def usingBinarySearch ( start , end , N , S ) : NEW_LINE INDENT if ( start >= end ) : NEW_LINE INDENT return start ; NEW_LINE DEDENT mid = start + ( end - start ) // 2 ; NEW_LINE totalSum = ( N * ( N + 1 ) ) // 2 ; NEW_LINE midSum = ( mid * ( mid + 1 ) ) // 2 ; NEW_LINE if ( ( totalSum - midSum ) <= S ) : NEW_LINE INDENT return usingBinarySearch ( start , mid , N , S ) ; NEW_LINE DEDENT return usingBinarySearch ( mid + 1 , end , N , S ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE S = 11 ; NEW_LINE print ( N - usingBinarySearch ( 1 , N , N , S ) + 1 ) ; NEW_LINE DEDENT


class GFG { static int getSteps ( int arr [ ] , int n ) { int count = 0 ; int modulus [ ] = new int [ 4 ] ; int sum = 0 ; int i ; for ( i = 0 ; i < n ; i ++ ) { int mod = arr [ i ] % 4 ; sum += mod ; modulus [ mod ] ++ ; } if ( sum % 4 != 0 ) { return - 1 ; } else { if ( modulus [ 1 ] > modulus [ 3 ] ) { count += modulus [ 3 ] ; } else { count += modulus [ 1 ] ; } modulus [ 1 ] -= count ; modulus [ 3 ] -= count ; modulus [ 2 ] += modulus [ 1 ] / 2 ; modulus [ 2 ] += modulus [ 3 ] / 2 ; count += modulus [ 1 ] / 2 ; count += modulus [ 3 ] / 2 ; count += modulus [ 2 ] / 2 ; return count ; } } public static void main ( String [ ] args ) { int n = 7 ; int arr [ ] = { 1 , 2 , 3 , 1 , 2 , 3 , 8 } ; int count = getSteps ( arr , n ) ; System . out . printf ( " % d " , count ) ; } }

def getSteps ( arr , n ) : NEW_LINE INDENT count = 0 NEW_LINE modulus = [ 0 for i in range ( 4 ) ] NEW_LINE Sum = 0 NEW_LINE i = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT mod = arr [ i ] % 4 NEW_LINE Sum += mod NEW_LINE modulus [ mod ] += 1 NEW_LINE DEDENT if ( Sum % 4 != 0 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( modulus [ 1 ] > modulus [ 3 ] ) : NEW_LINE INDENT count += modulus [ 3 ] NEW_LINE DEDENT else : NEW_LINE INDENT count += modulus [ 1 ] NEW_LINE DEDENT modulus [ 1 ] -= count NEW_LINE modulus [ 3 ] -= count NEW_LINE modulus [ 2 ] += modulus [ 1 ] // 2 NEW_LINE modulus [ 2 ] += modulus [ 3 ] // 2 NEW_LINE count += modulus [ 1 ] // 2 NEW_LINE count += modulus [ 3 ] // 2 NEW_LINE count += modulus [ 2 ] // 2 NEW_LINE return count NEW_LINE DEDENT DEDENT n = 7 NEW_LINE arr = [ 1 , 2 , 3 , 1 , 2 , 3 , 8 ] NEW_LINE count = getSteps ( arr , n ) NEW_LINE print ( count ) NEW_LINE


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { public static boolean orthogonality ( int x1 , int y1 , int x2 , int y2 , int r1 , int r2 ) { int dsquare = ( x1 - x2 ) * ( x1 - x2 ) + ( y1 - y2 ) * ( y1 - y2 ) ; if ( dsquare == r1 * r1 + r2 * r2 ) return true ; else return false ; } public static void main ( String [ ] args ) throws java . lang . Exception { int x1 = 4 , y1 = 3 ; int x2 = 0 , y2 = 1 ; int r1 = 2 , r2 = 4 ; boolean f = orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) ; if ( f ) System . out . println ( " Given ▁ circles ▁ are ▁ orthogonal . " ) ; else System . out . println ( " Given ▁ circles ▁ are ▁ not ▁ orthogonal . " ) ; } }

def orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) : NEW_LINE INDENT dsquare = ( x1 - x2 ) * ( x1 - x2 ) +   \ NEW_LINE INDENT ( y1 - y2 ) * ( y1 - y2 ) ; NEW_LINE DEDENT if ( dsquare == r1 * r1 + r2 * r2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT x1 , y1 = 4 , 3 NEW_LINE x2 , y2 = 0 , 1 NEW_LINE r1 , r2 = 2 , 4 NEW_LINE f = orthogonality ( x1 , y1 , x2 , y2 , r1 , r2 ) NEW_LINE if ( f ) : NEW_LINE INDENT print ( " Given ▁ circles ▁ are ▁ orthogonal . " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Given ▁ circles ▁ are ▁ not ▁ orthogonal . " ) NEW_LINE DEDENT


import java . util . * ; class GFG { static boolean isAnyNotPalindrome ( String s ) { Set < Character > unique = new HashSet < Character > ( ) ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) unique . add ( s . charAt ( i ) ) ; if ( unique . size ( ) > 1 ) return true ; else return false ; } public static void main ( String [ ] args ) { String s = " aaaaab " ; if ( isAnyNotPalindrome ( s ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }

def isAnyNotPalindrome ( s ) : NEW_LINE INDENT unique = set ( ) NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT unique . add ( s [ i ] ) NEW_LINE DEDENT if ( len ( unique ) > 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " aaaaab " NEW_LINE if ( isAnyNotPalindrome ( s ) ) : 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 mod ( String num , int a ) { int res = 0 ; for ( int i = 0 ; i < num . length ( ) ; i ++ ) res = ( res * 10 + ( int ) num . charAt ( i ) - '0' ) % a ; return res ; } public static void main ( String [ ] args ) { String num = "12316767678678" ; System . out . println ( mod ( num , 10 ) ) ; } }

def mod ( num , a ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 0 , len ( num ) ) : NEW_LINE INDENT res = ( res * 10 + int ( num [ i ] ) ) % a ; NEW_LINE DEDENT return res NEW_LINE DEDENT num = "12316767678678" ; NEW_LINE print ( mod ( num , 10 ) ) NEW_LINE


import java . io . * ; class GFG { static int solve ( String test , int MOD ) { int size = test . length ( ) ; int total = 0 ; for ( int i = 0 ; i <= 8 ; i ++ ) { int x = i ; int y = i + 1 ; String newtest = " " ; for ( int j = 0 ; j < size ; j ++ ) { if ( test . charAt ( j ) == x + 48 || test . charAt ( j ) == y + 48 ) { newtest += test . charAt ( j ) ; } } if ( newtest . length ( ) > 0 ) { int size1 = newtest . length ( ) ; int [ ] prefix = new int [ size1 ] ; for ( int j = 0 ; j < size1 ; j ++ ) { prefix [ j ] = 0 ; if ( newtest . charAt ( j ) == y + 48 ) { prefix [ j ] ++ ; } } for ( int j = 1 ; j < size1 ; j ++ ) { prefix [ j ] += prefix [ j - 1 ] ; } int count = 0 ; int firstcount = 0 ; int ss = 0 ; int prev = 0 ; for ( int j = 0 ; j < size1 ; j ++ ) { if ( newtest . charAt ( j ) == x + 48 ) { count ++ ; firstcount ++ ; } else { ss += count * count ; int pairsum = ( firstcount * firstcount - ss ) / 2 ; int temp = pairsum ; pairsum -= prev ; prev = temp ; int secondway = prefix [ size1 - 1 ] ; if ( j != 0 ) secondway -= prefix [ j - 1 ] ; int answer = count * ( count - 1 ) * secondway * ( secondway - 1 ) ; answer /= 4 ; answer += ( pairsum * secondway * ( secondway - 1 ) ) / 2 ; total += answer ; count = 0 ; } } } } return total ; } public static void main ( String [ ] args ) { String test = "13134422" ; int MOD = 1000000007 ; System . out . println ( solve ( test , MOD ) ) ; } }

MOD = 1000000007 NEW_LINE def solve ( test ) : NEW_LINE INDENT size = len ( test ) NEW_LINE total = 0 NEW_LINE for i in range ( 9 ) : NEW_LINE INDENT x = i NEW_LINE y = i + 1 NEW_LINE newtest = " " NEW_LINE for j in range ( size ) : NEW_LINE INDENT if ( ord ( test [ j ] ) == x + 48 or ord ( test [ j ] ) == y + 48 ) : NEW_LINE INDENT newtest += test [ j ] NEW_LINE DEDENT DEDENT if ( len ( newtest ) > 0 ) : NEW_LINE INDENT size1 = len ( newtest ) NEW_LINE prefix = [ 0 for i in range ( size1 ) ] NEW_LINE for j in range ( size1 ) : NEW_LINE INDENT if ( ord ( newtest [ j ] ) == y + 48 ) : NEW_LINE INDENT prefix [ j ] += 1 NEW_LINE DEDENT DEDENT for j in range ( 1 , size1 ) : NEW_LINE INDENT prefix [ j ] += prefix [ j - 1 ] NEW_LINE DEDENT count = 0 NEW_LINE firstcount = 0 NEW_LINE ss = 0 NEW_LINE prev = 0 NEW_LINE for j in range ( size1 ) : NEW_LINE INDENT if ( ord ( newtest [ j ] ) == x + 48 ) : NEW_LINE INDENT count += 1 NEW_LINE firstcount += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ss += count * count NEW_LINE pairsum = ( firstcount * firstcount - ss ) // 2 NEW_LINE temp = pairsum NEW_LINE pairsum -= prev NEW_LINE prev = temp NEW_LINE secondway = prefix [ size1 - 1 ] NEW_LINE if ( j != 0 ) : NEW_LINE INDENT secondway -= prefix [ j - 1 ] NEW_LINE DEDENT answer = count * ( count - 1 ) * secondway * ( secondway - 1 ) NEW_LINE answer //= 4 NEW_LINE answer += ( pairsum * secondway * ( secondway - 1 ) ) // 2 NEW_LINE total += answer NEW_LINE count = 0 NEW_LINE DEDENT DEDENT DEDENT DEDENT return total NEW_LINE DEDENT test = "13134422" NEW_LINE print ( solve ( test ) ) NEW_LINE


import java . io . * ; class GFG { static void KthCharacter ( int m , int n , int k ) { int distance = ( int ) Math . pow ( 2 , n ) ; int Block_number = k / distance ; int remaining = k % distance ; int s [ ] = new int [ 32 ] ; int x = 0 ; for ( ; m > 0 ; x ++ ) { s [ x ] = m % 2 ; m = m / 2 ; } int root = s [ x - 1 - Block_number ] ; if ( remaining == 0 ) { System . out . println ( root ) ; return ; } Boolean flip = true ; while ( remaining > 1 ) { if ( ( remaining & 1 ) > 0 ) { flip = ! flip ; } remaining = remaining >> 1 ; } if ( flip ) { System . out . println ( ( root > 0 ) ? 0 : 1 ) ; } else { System . out . println ( root ) ; } } public static void main ( String [ ] args ) { int m = 5 , k = 5 , n = 3 ; KthCharacter ( m , n , k ) ; } }

def KthCharacter ( m , n , k ) : NEW_LINE INDENT distance = pow ( 2 , n ) NEW_LINE Block_number = int ( k / distance ) NEW_LINE remaining = k % distance NEW_LINE s = [ 0 ] * 32 NEW_LINE x = 0 NEW_LINE while ( m > 0 ) : NEW_LINE INDENT s [ x ] = m % 2 NEW_LINE m = int ( m / 2 ) NEW_LINE x += 1 NEW_LINE DEDENT root = s [ x - 1 - Block_number ] NEW_LINE if ( remaining == 0 ) : NEW_LINE INDENT print ( root ) NEW_LINE return NEW_LINE DEDENT flip = True NEW_LINE while ( remaining > 1 ) : NEW_LINE INDENT if ( remaining & 1 ) : NEW_LINE INDENT flip = not ( flip ) NEW_LINE DEDENT remaining = remaining >> 1 NEW_LINE DEDENT if ( flip ) : NEW_LINE INDENT print ( not ( root ) ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( root ) NEW_LINE DEDENT DEDENT m = 5 NEW_LINE k = 5 NEW_LINE n = 3 NEW_LINE KthCharacter ( m , n , k ) NEW_LINE


import java . io . * ; class GFG { static boolean areVowelsInOrder ( String s ) { int n = s . length ( ) ; char c = ( char ) 64 ; for ( int i = 1 ; i < n ; i ++ ) { if ( s . charAt ( i ) == ' a ' || s . charAt ( i ) == ' e ' || s . charAt ( i ) == ' i ' || s . charAt ( i ) == ' o ' || s . charAt ( i ) == ' u ' ) { if ( s . charAt ( i ) < c ) return false ; else { c = s . charAt ( i ) ; } } } return true ; } public static void main ( String [ ] args ) { String s = " aabbbddeecc " ; if ( areVowelsInOrder ( s ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def areVowelsInOrder ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE c = chr ( 64 ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( s [ i ] == ' a ' or s [ i ] == ' e ' or s [ i ] == ' i ' or s [ i ] == ' o ' or s [ i ] == ' u ' ) : NEW_LINE INDENT if s [ i ] < c : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT c = s [ i ] NEW_LINE DEDENT DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aabbbddeecc " NEW_LINE if areVowelsInOrder ( s ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GfG { static int n = 3 ; static void preComputeXor ( int arr [ ] [ ] , int prefix_xor [ ] [ ] ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n ; j ++ ) { if ( j == 0 ) prefix_xor [ i ] [ j ] = arr [ i ] [ j ] ; else prefix_xor [ i ] [ j ] = ( prefix_xor [ i ] [ j - 1 ] ^ arr [ i ] [ j ] ) ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 1 ; j < n ; j ++ ) prefix_xor [ j ] [ i ] = ( prefix_xor [ j - 1 ] [ i ] ^ prefix_xor [ j ] [ i ] ) ; } static int ansQuerie ( int prefix_xor [ ] [ ] , int x1 , int y1 , int x2 , int y2 ) { int xor_1 = 0 , xor_2 = 0 , xor_3 = 0 ; if ( x1 != 0 ) xor_1 = prefix_xor [ x1 - 1 ] [ y2 ] ; if ( y1 != 0 ) xor_2 = prefix_xor [ x2 ] [ y1 - 1 ] ; if ( x1 != 0 && y1 != 0 ) xor_3 = prefix_xor [ x1 - 1 ] [ y1 - 1 ] ; return ( ( prefix_xor [ x2 ] [ y2 ] ^ xor_1 ) ^ ( xor_2 ^ xor_3 ) ) ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = new int [ ] [ ] { { 1 , 2 , 3 } , { 4 , 5 , 6 } , { 7 , 8 , 9 } } ; int prefix_xor [ ] [ ] = new int [ n ] [ n ] ; preComputeXor ( arr , prefix_xor ) ; System . out . println ( ansQuerie ( prefix_xor , 1 , 1 , 2 , 2 ) ) ; System . out . println ( ansQuerie ( prefix_xor , 1 , 2 , 2 , 2 ) ) ; } }

n = 3 NEW_LINE def preComputeXor ( arr , prefix_xor ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT prefix_xor [ i ] [ j ] = arr [ i ] [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT prefix_xor [ i ] [ j ] = ( prefix_xor [ i ] [ j - 1 ] ^ arr [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( 1 , n ) : NEW_LINE INDENT prefix_xor [ j ] [ i ] = ( prefix_xor [ j - 1 ] [ i ] ^ prefix_xor [ j ] [ i ] ) NEW_LINE DEDENT DEDENT DEDENT def ansQuerie ( prefix_xor , x1 , y1 , x2 , y2 ) : NEW_LINE INDENT xor_1 , xor_2 , xor_3 = 0 , 0 , 0 NEW_LINE if ( x1 != 0 ) : NEW_LINE INDENT xor_1 = prefix_xor [ x1 - 1 ] [ y2 ] NEW_LINE DEDENT if ( y1 != 0 ) : NEW_LINE INDENT xor_2 = prefix_xor [ x2 ] [ y1 - 1 ] NEW_LINE DEDENT if ( x1 != 0 and y1 != 0 ) : NEW_LINE INDENT xor_3 = prefix_xor [ x1 - 1 ] [ y1 - 1 ] NEW_LINE DEDENT return ( ( prefix_xor [ x2 ] [ y2 ] ^ xor_1 ) ^ ( xor_2 ^ xor_3 ) ) NEW_LINE DEDENT arr = [ [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ] NEW_LINE prefix_xor = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE preComputeXor ( arr , prefix_xor ) NEW_LINE print ( ansQuerie ( prefix_xor , 1 , 1 , 2 , 2 ) ) NEW_LINE print ( ansQuerie ( prefix_xor , 1 , 2 , 2 , 2 ) ) NEW_LINE


import java . io . * ; class GFG { static long power ( int x , long y ) { long res = 1 ; while ( y > 0 ) { if ( y == 1 ) res = ( res * x ) ; y = y >> 1 ; x = ( x * x ) ; } return res ; } static long solve ( int L ) { int n = L / 2 + 1 ; long ans = power ( n , n - 2 ) ; return ans ; } public static void main ( String [ ] args ) { int L = 6 ; System . out . println ( solve ( L ) ) ; } }

def power ( x , y ) : NEW_LINE INDENT res = 1 ; NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y % 2 == 1 ) : NEW_LINE INDENT res = ( res * x ) ; NEW_LINE DEDENT y = int ( y ) >> 1 ; NEW_LINE x = ( x * x ) ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def solve ( L ) : NEW_LINE INDENT n = L / 2 + 1 ; NEW_LINE ans = power ( n , n - 2 ) ; NEW_LINE return int ( ans ) ; NEW_LINE DEDENT L = 6 ; NEW_LINE print ( solve ( L ) ) ; NEW_LINE


import java . util . * ; class GFG { static int maxN = 300 ; static int maxM = 300 ; static int dp [ ] [ ] = new int [ maxN ] [ maxM ] ; static boolean v [ ] [ ] = new boolean [ maxN ] [ maxM ] ; static int findLen ( int [ ] arr , int i , int curr , int n , int m ) { if ( i == n ) { if ( curr == m ) return 0 ; else return - 1 ; } if ( v [ i ] [ curr ] ) return dp [ i ] [ curr ] ; v [ i ] [ curr ] = true ; int l = findLen ( arr , i + 1 , curr , n , m ) ; int r = findLen ( arr , i + 1 , curr & arr [ i ] , n , m ) ; dp [ i ] [ curr ] = l ; if ( r != - 1 ) dp [ i ] [ curr ] = Math . max ( dp [ i ] [ curr ] , r + 1 ) ; return dp [ i ] [ curr ] ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 7 , 2 , 3 } ; int n = arr . length ; int m = 3 ; int ans = findLen ( arr , 0 , ( ( 1 << 8 ) - 1 ) , n , m ) ; if ( ans == - 1 ) System . out . print ( 0 ) ; else System . out . print ( ans ) ; } }

import numpy as np NEW_LINE maxN = 20 NEW_LINE maxM = 256 NEW_LINE dp = np . zeros ( ( maxN , maxM ) ) ; NEW_LINE v = np . zeros ( ( maxN , maxM ) ) ; NEW_LINE def findLen ( arr , i , curr , n , m ) : NEW_LINE INDENT if ( i == n ) : NEW_LINE INDENT if ( curr == m ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT DEDENT if ( v [ i ] [ curr ] ) : NEW_LINE INDENT return dp [ i ] [ curr ] ; NEW_LINE DEDENT v [ i ] [ curr ] = 1 ; NEW_LINE l = findLen ( arr , i + 1 , curr , n , m ) ; NEW_LINE r = findLen ( arr , i + 1 , curr & arr [ i ] , n , m ) ; NEW_LINE dp [ i ] [ curr ] = l ; NEW_LINE if ( r != - 1 ) : NEW_LINE INDENT dp [ i ] [ curr ] = max ( dp [ i ] [ curr ] , r + 1 ) ; NEW_LINE DEDENT return dp [ i ] [ curr ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 7 , 2 , 3 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE m = 3 ; NEW_LINE ans = findLen ( arr , 0 , ( ( 1 << 8 ) - 1 ) , n , m ) ; NEW_LINE if ( ans == - 1 ) : NEW_LINE INDENT print ( 0 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( ans ) ; NEW_LINE DEDENT DEDENT


class GFG { static int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 } ; static int multiply ( ) { int pro = 1 ; for ( int i = 0 ; i < arr . length ; i ++ ) pro = pro * arr [ i ] ; return pro ; } public static void main ( String [ ] args ) { System . out . println ( multiply ( ) ) ; } }

def multiply ( array , n ) : NEW_LINE INDENT pro = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT pro = pro * array [ i ] NEW_LINE DEDENT return pro NEW_LINE DEDENT array = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( array ) NEW_LINE print ( multiply ( array , n ) ) NEW_LINE


import java . util . * ; class GFG { static void segregate0and1 ( int arr [ ] ) { int type0 = 0 ; int type1 = arr . length - 1 ; while ( type0 < type1 ) { if ( arr [ type0 ] == 1 ) { arr [ type1 ] = arr [ type1 ] + arr [ type0 ] ; arr [ type0 ] = arr [ type1 ] - arr [ type0 ] ; arr [ type1 ] = arr [ type1 ] - arr [ type0 ] ; type1 -- ; } else { type0 ++ ; } } } public static void main ( String [ ] args ) { int [ ] array = { 0 , 1 , 0 , 1 , 1 , 1 } ; segregate0and1 ( array ) ; for ( int a : array ) { System . out . print ( a + " ▁ " ) ; } } }

def segregate0and1 ( arr , size ) : NEW_LINE INDENT type0 = 0 NEW_LINE type1 = size - 1 NEW_LINE while ( type0 < type1 ) : NEW_LINE INDENT if ( arr [ type0 ] == 1 ) : NEW_LINE INDENT ( arr [ type0 ] , arr [ type1 ] ) = ( arr [ type1 ] , arr [ type0 ] ) NEW_LINE type1 -= 1 NEW_LINE DEDENT else : NEW_LINE INDENT type0 += 1 NEW_LINE DEDENT DEDENT DEDENT arr = [ 0 , 1 , 0 , 1 , 1 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE segregate0and1 ( arr , arr_size ) NEW_LINE print ( " Array ▁ after ▁ segregation ▁ is " , end = " ▁ " ) NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class GfG { public static int [ ] fix ( int [ ] A ) { Set < Integer > s = new HashSet < Integer > ( ) ; for ( int i = 0 ; i < A . length ; i ++ ) { s . add ( A [ i ] ) ; } for ( int i = 0 ; i < A . length ; i ++ ) { if ( s . contains ( i ) ) A [ i ] = i ; else A [ i ] = - 1 ; } return A ; } public static void main ( String [ ] args ) { int A [ ] = { - 1 , - 1 , 6 , 1 , 9 , 3 , 2 , - 1 , 4 , - 1 } ; System . out . println ( Arrays . toString ( fix ( A ) ) ) ; } }

def fix ( A ) : NEW_LINE INDENT s = set ( ) NEW_LINE for i in range ( len ( A ) ) : NEW_LINE INDENT s . add ( A [ i ] ) NEW_LINE DEDENT for i in range ( len ( A ) ) : NEW_LINE INDENT if i in s : NEW_LINE INDENT A [ i ] = i NEW_LINE DEDENT else : NEW_LINE INDENT A [ i ] = - 1 NEW_LINE DEDENT DEDENT return A NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ - 1 , - 1 , 6 , 1 , 9 , 3 , 2 , - 1 , 4 , - 1 ] NEW_LINE print ( fix ( A ) ) NEW_LINE DEDENT


class GFG { static int MAX = 26 ; static void performQueries ( String str , int q , int type [ ] , char ch [ ] , int freq [ ] ) { int n = str . length ( ) ; int [ ] [ ] L = new int [ MAX ] [ n ] ; int [ ] [ ] F = new int [ MAX ] [ n ] ; int [ ] cnt = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { int k = str . charAt ( i ) - ' a ' ; cnt [ k ] ++ ; for ( int j = 0 ; j < MAX ; j ++ ) { if ( k == j ) { L [ j ] [ cnt [ j ] ] = i ; F [ j ] [ cnt [ j ] ] = i ; } else L [ j ] [ cnt [ j ] ] = L [ j ] [ cnt [ j ] ] + 1 ; } } for ( int i = 0 ; i < q ; i ++ ) { if ( type [ i ] == 1 ) { System . out . print ( L [ ch [ i ] - ' a ' ] [ freq [ i ] ] ) ; } else { System . out . print ( F [ ch [ i ] - ' a ' ] [ freq [ i ] ] ) ; } System . out . print ( " \n " ) ; } } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int type [ ] = { 1 , 2 } ; char ch [ ] = { ' e ' , ' k ' } ; int freq [ ] = { 2 , 2 } ; int q = type . length ; performQueries ( str , q , type , ch , freq ) ; } }

import numpy as np NEW_LINE MAX = 26 ; NEW_LINE def performQueries ( string , q , type_arr , ch , freq ) : NEW_LINE INDENT n = len ( string ) ; NEW_LINE L = np . zeros ( ( MAX , n ) ) ; NEW_LINE F = np . zeros ( ( MAX , n ) ) ; NEW_LINE cnt = [ 0 ] * MAX ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT k = ord ( string [ i ] ) - ord ( ' a ' ) ; NEW_LINE cnt [ k ] += 1 ; NEW_LINE for j in range ( MAX ) : NEW_LINE INDENT if ( k == j ) : NEW_LINE INDENT L [ j ] [ cnt [ j ] ] = i ; NEW_LINE F [ j ] [ cnt [ j ] ] = i ; NEW_LINE DEDENT else : NEW_LINE INDENT L [ j ] [ cnt [ j ] ] = L [ j ] [ cnt [ j ] ] + 1 ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( q ) : NEW_LINE INDENT if ( type_arr [ i ] == 1 ) : NEW_LINE INDENT print ( L [ ord ( ch [ i ] ) - ord ( ' a ' ) ] [ freq [ i ] ] , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( F [ ord ( ch [ i ] ) - ord ( ' a ' ) ] [ freq [ i ] ] , end = " " ) ; NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE type_arr = [ 1 , 2 ] ; NEW_LINE ch = [ ' e ' , ' k ' ] ; NEW_LINE freq = [ 2 , 2 ] ; NEW_LINE q = len ( type_arr ) ; NEW_LINE performQueries ( string , q , type_arr , ch , freq ) ; NEW_LINE DEDENT


class GFG { static void find ( int [ ] arr , int length , int s ) { for ( int i = 1 ; i <= ( Math . pow ( 2 , length ) ) ; i ++ ) { int sum = 0 ; for ( int j = 0 ; j < length ; j ++ ) if ( ( ( i >> j ) & 1 ) % 2 == 1 ) sum += arr [ j ] ; if ( sum == s ) { System . out . println ( " YES " ) ; return ; } } System . out . println ( " NO " ) ; } public static void main ( String [ ] args ) { int sum = 5 ; int [ ] array = { - 1 , 2 , 4 , 121 } ; int length = array . length ; find ( array , length , sum ) ; } }

from itertools import combinations NEW_LINE def find ( lst , n ) : NEW_LINE INDENT print ( ' YES ' if [ 1 for r in range ( 1 , len ( lst ) + 1 ) for subset in combinations ( lst , r ) if sum ( subset ) == n ] else ' NO ' ) NEW_LINE DEDENT find ( [ - 1 , 2 , 4 , 121 ] , 5 ) NEW_LINE


import java . util . * ; class GFG { static int no_of_characters ( int M ) { int k = 1 ; while ( true ) { if ( ( int ) Math . pow ( 2 , k + 1 ) - 2 < M ) k ++ ; else break ; } return k ; } static void print_string ( int M ) { int k , num , N ; k = no_of_characters ( M ) ; N = M - ( ( int ) Math . pow ( 2 , k ) - 2 ) ; while ( k > 0 ) { num = ( int ) Math . pow ( 2 , k - 1 ) ; if ( num >= N ) System . out . print ( " A " ) ; else { System . out . print ( " B " ) ; N -= num ; } k -- ; } System . out . println ( ) ; } public static void main ( String args [ ] ) { int M ; M = 30 ; print_string ( M ) ; M = 55 ; print_string ( M ) ; M = 100 ; print_string ( M ) ; } }

from math import pow NEW_LINE def no_of_characters ( M ) : NEW_LINE INDENT k = 1 NEW_LINE while ( True ) : NEW_LINE INDENT if ( pow ( 2 , k + 1 ) - 2 < M ) : NEW_LINE INDENT k += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return k NEW_LINE DEDENT def print_string ( M ) : NEW_LINE INDENT k = no_of_characters ( M ) NEW_LINE N = M - ( pow ( 2 , k ) - 2 ) NEW_LINE while ( k > 0 ) : NEW_LINE INDENT num = pow ( 2 , k - 1 ) NEW_LINE if ( num >= N ) : NEW_LINE INDENT print ( " A " , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " B " , end = " " ) NEW_LINE N -= num NEW_LINE DEDENT k -= 1 NEW_LINE DEDENT print ( " \n " , end = " " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT M = 30 ; NEW_LINE print_string ( M ) NEW_LINE M = 55 NEW_LINE print_string ( M ) NEW_LINE M = 100 NEW_LINE print_string ( M ) NEW_LINE DEDENT


class Gfg { static int cassini ( int n ) { return ( n & 1 ) != 0 ? - 1 : 1 ; } public static void main ( String args [ ] ) { int n = 5 ; System . out . println ( cassini ( n ) ) ; } }

def cassini ( n ) : NEW_LINE INDENT return - 1 if ( n & 1 ) else 1 NEW_LINE DEDENT n = 5 NEW_LINE print ( cassini ( n ) ) NEW_LINE