import java . util . * ; class GFG { static int maxLen = 30 ; static int findCnt ( int arr [ ] , int n , int k ) { int ans = 0 ; for ( int i = 1 ; i < n ; i ++ ) { arr [ i ] += arr [ i - 1 ] ; if ( arr [ i ] > k || arr [ i ] < - 1 * k ) ans ++ ; } if ( arr [ 0 ] > k || arr [ 0 ] < - 1 * k ) ans ++ ; Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) ans += n - upper_bound ( arr , 0 , n , arr [ i ] + k ) ; return ans ; } static int upper_bound ( int [ ] a , int low , int high , int element ) { while ( low < high ) { int middle = low + ( high - low ) / 2 ; if ( a [ middle ] > element ) high = middle ; else low = middle + 1 ; } return low ; } public static void main ( String [ ] args ) { int arr [ ] = { - 1 , 4 , - 5 , 6 } ; int n = arr . length ; int k = 0 ; System . out . println ( findCnt ( arr , n , k ) ) ; } }

from bisect import bisect as upper_bound NEW_LINE maxLen = 30 NEW_LINE def findCnt ( arr , n , k ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT arr [ i ] += arr [ i - 1 ] NEW_LINE if ( arr [ i ] > k or arr [ i ] < - 1 * k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT if ( arr [ 0 ] > k or arr [ 0 ] < - 1 * k ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT arr = sorted ( arr ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans += n - upper_bound ( arr , arr [ i ] + k ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ - 1 , 4 , - 5 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE k = 0 NEW_LINE print ( findCnt ( arr , n , k ) ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { public static void printPairs ( int arr [ ] , int n ) { Vector < Integer > v = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( Math . abs ( arr [ i ] ) == Math . abs ( arr [ j ] ) ) v . add ( Math . abs ( arr [ i ] ) ) ; if ( v . size ( ) == 0 ) return ; Collections . sort ( v ) ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( - v . get ( i ) + " ▁ " + v . get ( i ) ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 } ; int n = arr . length ; printPairs ( arr , n ) ; } }

def printPairs ( arr , n ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( abs ( arr [ i ] ) == abs ( arr [ j ] ) ) : NEW_LINE INDENT v . append ( abs ( arr [ i ] ) ) NEW_LINE DEDENT DEDENT DEDENT if ( len ( v ) == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT v . sort ( ) NEW_LINE for i in range ( len ( v ) ) : NEW_LINE INDENT print ( - v [ i ] , " " , v [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 8 , 9 , - 4 , 1 , - 1 , - 8 , - 9 ] NEW_LINE n = len ( arr ) NEW_LINE printPairs ( arr , n ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; class GFG { public static int sumOfSeries ( int n ) { int x = ( n * ( n + 1 ) / 2 ) ; return x * x ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( sumOfSeries ( n ) ) ; } }

def sumOfSeries ( n ) : NEW_LINE INDENT x = ( n * ( n + 1 ) / 2 ) NEW_LINE return ( int ) ( x * x ) NEW_LINE DEDENT n = 5 NEW_LINE print ( sumOfSeries ( n ) ) NEW_LINE


public class GfG { static int two_factor ( int n ) { int twocount = 0 ; while ( n % 2 == 0 ) { twocount ++ ; n = n / 2 ; } return twocount ; } static int five_factor ( int n ) { int fivecount = 0 ; while ( n % 5 == 0 ) { fivecount ++ ; n = n / 5 ; } return fivecount ; } static int find_con_zero ( int arr [ ] , int n ) { int twocount = 0 ; int fivecount = 0 ; for ( int i = 0 ; i < n ; i ++ ) { twocount += two_factor ( arr [ i ] ) ; fivecount += five_factor ( arr [ i ] ) ; } if ( twocount < fivecount ) return twocount ; else return fivecount ; } public static void main ( String s [ ] ) { int arr [ ] = { 100 , 10 , 5 , 25 , 35 , 14 } ; int n = 6 ; System . out . println ( find_con_zero ( arr , n ) ) ; } }

def two_factor ( n ) : NEW_LINE INDENT twocount = 0 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT twocount += 1 NEW_LINE n = int ( n / 2 ) NEW_LINE DEDENT return twocount NEW_LINE DEDENT def five_factor ( n ) : NEW_LINE INDENT fivecount = 0 NEW_LINE while n % 5 == 0 : NEW_LINE INDENT fivecount += 1 NEW_LINE n = int ( n / 5 ) NEW_LINE DEDENT return fivecount NEW_LINE DEDENT def find_con_zero ( arr , n ) : NEW_LINE INDENT twocount = 0 NEW_LINE fivecount = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT twocount += two_factor ( arr [ i ] ) NEW_LINE fivecount += five_factor ( arr [ i ] ) NEW_LINE DEDENT if twocount < fivecount : NEW_LINE INDENT return twocount NEW_LINE DEDENT else : NEW_LINE INDENT return fivecount NEW_LINE DEDENT DEDENT arr = [ 100 , 10 , 5 , 25 , 35 , 14 ] NEW_LINE n = 6 NEW_LINE print ( find_con_zero ( arr , n ) ) NEW_LINE


class GFG { static void ReplaceElements ( int arr [ ] , int n ) { int prod = 1 ; for ( int i = 0 ; i < n ; ++ i ) { prod *= arr [ i ] ; } for ( int i = 0 ; i < n ; ++ i ) { arr [ i ] = prod / arr [ i ] ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 3 , 5 , 7 } ; int n = arr . length ; ReplaceElements ( arr , n ) ; for ( int i = 0 ; i < n ; ++ i ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( " " ) ; } }

def ReplaceElements ( arr , n ) : NEW_LINE INDENT prod = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT prod *= arr [ i ] NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = prod // arr [ i ] NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 3 , 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE ReplaceElements ( arr , n ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT


import java . io . * ; class GFG { static boolean isPowerOfTwo ( int x ) { return ( x > 0 && ( ! ( ( x & ( x - 1 ) ) > 0 ) ) ) ; } static void Count_pairs ( int a [ ] , int n ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( isPowerOfTwo ( a [ i ] ) ) count ++ ; } int ans = ( count * ( count - 1 ) ) / 2 ; System . out . println ( ans ) ; } public static void main ( String [ ] args ) { int a [ ] = { 2 , 5 , 8 , 16 , 128 } ; int n = a . length ; Count_pairs ( a , n ) ; } }

def isPowerOfTwo ( x ) : NEW_LINE INDENT return ( x and ( not ( x & ( x - 1 ) ) ) ) NEW_LINE DEDENT def Count_pairs ( a , n ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if isPowerOfTwo ( a [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT ans = ( count * ( count - 1 ) ) / 2 NEW_LINE print ( ans ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 2 , 5 , 8 , 16 , 128 ] NEW_LINE n = len ( a ) NEW_LINE Count_pairs ( a , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void right_left ( int a [ ] , int n ) { Map < Integer , Integer > total = new HashMap < > ( ) ; Map < Integer , Integer > left = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) total . put ( a [ i ] , total . get ( a [ i ] ) == null ? 1 : total . get ( a [ i ] ) + 1 ) ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( ( total . get ( a [ i ] ) - 1 - ( 2 * ( left . containsKey ( a [ i ] ) == true ? left . get ( a [ i ] ) : 0 ) ) ) + " ▁ " ) ; left . put ( a [ i ] , left . get ( a [ i ] ) == null ? 1 : left . get ( a [ i ] ) + 1 ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 2 , 1 } ; int n = a . length ; right_left ( a , n ) ; } }

def right_left ( a , n ) : NEW_LINE INDENT total = dict . fromkeys ( a , 0 ) ; NEW_LINE left = dict . fromkeys ( a , 0 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] not in total : NEW_LINE INDENT total [ a [ i ] ] = 1 NEW_LINE DEDENT total [ a [ i ] ] += 1 ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( total [ a [ i ] ] - 1 - ( 2 * left [ a [ i ] ] ) , end = " ▁ " ) ; NEW_LINE left [ a [ i ] ] += 1 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 3 , 2 , 1 ] ; NEW_LINE n = len ( a ) ; NEW_LINE right_left ( a , n ) ; NEW_LINE DEDENT


class GFG { static int getMinimumSum ( int arr [ ] , int n ) { int ans = Integer . MAX_VALUE ; for ( int i = 0 ; i < n - 2 ; i ++ ) { for ( int j = i + 1 ; j < n - 1 ; j ++ ) { for ( int k = j + 1 ; k < n ; k ++ ) { ans = Math . min ( ans , arr [ i ] + arr [ j ] + arr [ k ] ) ; } } } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , - 1 , 5 , - 2 } ; int n = arr . length ; System . out . print ( getMinimumSum ( arr , n ) + " \n " ) ; } }

import sys NEW_LINE def getMinimumSum ( arr , n ) : NEW_LINE INDENT ans = sys . maxsize ; NEW_LINE for i in range ( n - 2 ) : NEW_LINE INDENT for j in range ( i + 1 , n - 1 ) : NEW_LINE INDENT for k in range ( j + 1 , n ) : NEW_LINE INDENT ans = min ( ans , arr [ i ] + arr [ j ] + arr [ k ] ) ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 , 5 , - 1 , 5 , - 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( getMinimumSum ( arr , n ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int merge ( int arr [ ] , int temp [ ] , int left , int mid , int right ) { int inv_count = 0 ; int i = left ; int j = mid ; int k = left ; while ( ( i <= mid - 1 ) && ( j <= right ) ) { if ( arr [ i ] <= arr [ j ] ) temp [ k ++ ] = arr [ i ++ ] ; else { temp [ k ++ ] = arr [ j ++ ] ; inv_count = inv_count + ( mid - i ) ; } } while ( i <= mid - 1 ) temp [ k ++ ] = arr [ i ++ ] ; while ( j <= right ) temp [ k ++ ] = arr [ j ++ ] ; for ( i = left ; i <= right ; i ++ ) arr [ i ] = temp [ i ] ; return inv_count ; } static int _mergeSort ( int arr [ ] , int temp [ ] , int left , int right ) { int mid , inv_count = 0 ; if ( right > left ) { mid = ( right + left ) / 2 ; inv_count = _mergeSort ( arr , temp , left , mid ) ; inv_count += _mergeSort ( arr , temp , mid + 1 , right ) ; inv_count += merge ( arr , temp , left , mid + 1 , right ) ; } return inv_count ; } static int countPairs ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) arr [ i ] = i * arr [ i ] ; int temp [ ] = new int [ n ] ; return _mergeSort ( arr , temp , 0 , n - 1 ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 0 , 10 , 2 , 4 , 1 , 6 } ; int n = arr . length ; System . out . print ( " Count ▁ of ▁ Pairs ▁ : ▁ " + countPairs ( arr , n ) ) ; } }

def merge ( arr , temp , left , mid , right ) : NEW_LINE INDENT inv_count = 0 NEW_LINE i = left NEW_LINE j = mid NEW_LINE k = left NEW_LINE while ( ( i <= mid - 1 ) and ( j <= right ) ) : NEW_LINE INDENT if ( arr [ i ] <= arr [ j ] ) : NEW_LINE INDENT temp [ k ] = arr [ i ] NEW_LINE i += 1 NEW_LINE k += 1 NEW_LINE DEDENT else : NEW_LINE INDENT temp [ k ] = arr [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE inv_count = inv_count + ( mid - i ) NEW_LINE DEDENT DEDENT while ( i <= mid - 1 ) : NEW_LINE INDENT temp [ k ] = arr [ i ] NEW_LINE i += 1 NEW_LINE k += 1 NEW_LINE DEDENT while ( j <= right ) : NEW_LINE INDENT temp [ k ] = arr [ j ] NEW_LINE k += 1 NEW_LINE j += 1 NEW_LINE DEDENT for i in range ( left , right + 1 ) : NEW_LINE INDENT arr [ i ] = temp [ i ] NEW_LINE DEDENT return inv_count NEW_LINE DEDENT def _mergeSort ( arr , temp , left , right ) : NEW_LINE INDENT inv_count = 0 NEW_LINE if ( right > left ) : NEW_LINE INDENT mid = ( right + left ) // 2 NEW_LINE inv_count = _mergeSort ( arr , temp , left , mid ) NEW_LINE inv_count += _mergeSort ( arr , temp , mid + 1 , right ) NEW_LINE inv_count += merge ( arr , temp , left , mid + 1 , right ) NEW_LINE DEDENT return inv_count NEW_LINE DEDENT def countPairs ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT arr [ i ] = i * arr [ i ] NEW_LINE DEDENT temp = [ 0 ] * n NEW_LINE return _mergeSort ( arr , temp , 0 , n - 1 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 0 , 10 , 2 , 4 , 1 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Count ▁ of ▁ Pairs ▁ : ▁ " , countPairs ( arr , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int MAX_COUNT = 10000 ; static List < String > vec = new LinkedList < String > ( ) ; static void generateNumbersUtil ( ) { Queue < String > q = new LinkedList < String > ( ) ; q . add ( "9" ) ; for ( int count = MAX_COUNT ; count > 0 ; count -- ) { String s1 = q . peek ( ) ; q . remove ( ) ; vec . add ( s1 ) ; String s2 = s1 ; q . add ( s1 + "0" ) ; q . add ( s2 + "9" ) ; } } static String findSmallestMultiple ( int n ) { for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { if ( Integer . parseInt ( vec . get ( i ) ) % n == 0 ) { return vec . get ( i ) ; } } return " " ; } public static void main ( String [ ] args ) { generateNumbersUtil ( ) ; int n = 7 ; System . out . println ( findSmallestMultiple ( n ) ) ; } }

from queue import Queue NEW_LINE def generateNumbersUtil ( ) : NEW_LINE INDENT global vec NEW_LINE q = Queue ( ) NEW_LINE q . put ( "9" ) NEW_LINE for count in range ( MAX_COUNT , - 1 , - 1 ) : NEW_LINE INDENT s1 = q . queue [ 0 ] NEW_LINE q . get ( ) NEW_LINE vec . append ( s1 ) NEW_LINE s2 = s1 NEW_LINE s1 += "0" NEW_LINE q . put ( s1 ) NEW_LINE s2 += "9" NEW_LINE q . put ( s2 ) NEW_LINE DEDENT DEDENT def findSmallestMultiple ( n ) : NEW_LINE INDENT global vec NEW_LINE for i in range ( len ( vec ) ) : NEW_LINE INDENT if ( int ( vec [ i ] ) % n == 0 ) : NEW_LINE INDENT return vec [ i ] NEW_LINE DEDENT DEDENT DEDENT MAX_COUNT = 10000 NEW_LINE vec = [ ] NEW_LINE generateNumbersUtil ( ) NEW_LINE n = 7 NEW_LINE print ( findSmallestMultiple ( n ) ) NEW_LINE


import java . io . * ; class GFG { static boolean isEqualBlock ( int n ) { int first_bit = n % 2 ; int first_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit && n > 0 ) { n = n / 2 ; first_count ++ ; } if ( n == 0 ) return false ; while ( n > 0 ) { first_bit = n % 2 ; int curr_count = 1 ; n = n / 2 ; while ( n % 2 == first_bit ) { n = n / 2 ; curr_count ++ ; } if ( curr_count != first_count ) return false ; } return true ; } public static void main ( String [ ] args ) { int n = 51 ; if ( isEqualBlock ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isEqualBlock ( n ) : NEW_LINE INDENT first_bit = n % 2 NEW_LINE first_count = 1 NEW_LINE n = n // 2 NEW_LINE while n % 2 == first_bit and n > 0 : NEW_LINE INDENT n = n // 2 NEW_LINE first_count += 1 NEW_LINE DEDENT if n == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT while n > 0 : NEW_LINE INDENT first_bit = n % 2 NEW_LINE curr_count = 1 NEW_LINE n = n // 2 NEW_LINE while n % 2 == first_bit : NEW_LINE INDENT n = n // 2 NEW_LINE curr_count += 1 NEW_LINE DEDENT if curr_count != first_count : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 51 NEW_LINE if isEqualBlock ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class solution { static int countNumbers ( int N ) { return ( int ) Math . sqrt ( N ) - 1 ; } public static void main ( String args [ ] ) { int N = 36 ; System . out . println ( countNumbers ( N ) ) ; } }

import math NEW_LINE def countNumbers ( N ) : NEW_LINE INDENT return int ( math . sqrt ( N ) ) - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 36 NEW_LINE print ( countNumbers ( N ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int phi ( int n ) { int result = n ; for ( int p = 2 ; p * p <= n ; ++ p ) { if ( n % p == 0 ) { while ( n % p == 0 ) n /= p ; result -= result / p ; } } if ( n > 1 ) result -= result / n ; return 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 = int ( n / p ) ; NEW_LINE DEDENT result -= int ( result / p ) ; NEW_LINE DEDENT p += 1 ; NEW_LINE DEDENT if ( n > 1 ) : NEW_LINE INDENT result -= int ( result / n ) ; NEW_LINE DEDENT return result ; NEW_LINE DEDENT for n in range ( 1 , 11 ) : NEW_LINE INDENT print ( " phi ( " , n , " ) ▁ = " , phi ( n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int productDiagonals ( int arr [ ] [ ] , int n ) { int product = 1 ; for ( int i = 0 ; i < n ; i ++ ) { product = product * arr [ i ] [ i ] ; product = product * arr [ i ] [ n - i - 1 ] ; } if ( n % 2 == 1 ) { product = product / arr [ n / 2 ] [ n / 2 ] ; } return product ; } public static void main ( String [ ] args ) { int arr1 [ ] [ ] = { { 1 , 2 , 3 , 4 } , { 5 , 6 , 7 , 8 } , { 9 , 7 , 4 , 2 } , { 2 , 2 , 2 , 1 } } ; System . out . print ( productDiagonals ( arr1 , 4 ) + " \n " ) ; int arr2 [ ] [ ] = { { 2 , 1 , 2 , 1 , 2 } , { 1 , 2 , 1 , 2 , 1 } , { 2 , 1 , 2 , 1 , 2 } , { 1 , 2 , 1 , 2 , 1 } , { 2 , 1 , 2 , 1 , 2 } } ; System . out . print ( productDiagonals ( arr2 , 5 ) + " \n " ) ; } }

def productDiagonals ( arr , n ) : NEW_LINE INDENT product = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT product = product * arr [ i ] [ i ] ; NEW_LINE product = product * arr [ i ] [ n - i - 1 ] ; NEW_LINE DEDENT if ( n % 2 == 1 ) : NEW_LINE INDENT product = product // arr [ n // 2 ] [ n // 2 ] ; NEW_LINE DEDENT return product ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ [ 1 , 2 , 3 , 4 ] , [ 5 , 6 , 7 , 8 ] , [ 9 , 7 , 4 , 2 ] , [ 2 , 2 , 2 , 1 ] ] ; NEW_LINE print ( productDiagonals ( arr1 , 4 ) ) ; NEW_LINE arr2 = [ [ 2 , 1 , 2 , 1 , 2 ] , [ 1 , 2 , 1 , 2 , 1 ] , [ 2 , 1 , 2 , 1 , 2 ] , [ 1 , 2 , 1 , 2 , 1 ] , [ 2 , 1 , 2 , 1 , 2 ] ] ; NEW_LINE print ( productDiagonals ( arr2 , 5 ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static void findSurpasser ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ j ] > arr [ i ] ) count ++ ; System . out . print ( count + " ▁ " ) ; } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; System . out . println ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 7 , 5 , 3 , 0 , 8 , 1 } ; int n = arr . length ; System . out . println ( " Given ▁ array ▁ is ▁ " ) ; printArray ( arr , n ) ; System . out . println ( " Surpasser ▁ Count ▁ of " + " ▁ array ▁ is ▁ " ) ; findSurpasser ( arr , n ) ; } }

def findSurpasser ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT count = 0 ; NEW_LINE for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( arr [ j ] > arr [ i ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count , end = " ▁ " ) NEW_LINE DEDENT DEDENT def printArray ( arr , n ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , 7 , 5 , 3 , 0 , 8 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Given ▁ array ▁ is " ) NEW_LINE printArray ( arr , n ) NEW_LINE print ( " \n Surpasser ▁ Count ▁ of ▁ array ▁ is " ) ; NEW_LINE findSurpasser ( arr , n ) NEW_LINE


import java . io . * ; import java . util . * ; public class Simstrings { static final int MAX_CHAR = 26 ; static void printCommon ( String s1 , String s2 ) { int [ ] a1 = new int [ MAX_CHAR ] ; int [ ] a2 = new int [ MAX_CHAR ] ; int length1 = s1 . length ( ) ; int length2 = s2 . length ( ) ; for ( int i = 0 ; i < length1 ; i ++ ) a1 [ s1 . charAt ( i ) - ' a ' ] += 1 ; for ( int i = 0 ; i < length2 ; i ++ ) a2 [ s2 . charAt ( i ) - ' a ' ] += 1 ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) { if ( a1 [ i ] != 0 && a2 [ i ] != 0 ) { for ( int j = 0 ; j < Math . min ( a1 [ i ] , a2 [ i ] ) ; j ++ ) System . out . print ( ( ( char ) ( i + ' a ' ) ) ) ; } } } public static void main ( String [ ] args ) throws IOException { String s1 = " geeksforgeeks " , s2 = " practiceforgeeks " ; printCommon ( s1 , s2 ) ; } }

MAX_CHAR = 26 NEW_LINE def printCommon ( s1 , s2 ) : NEW_LINE INDENT a1 = [ 0 for i in range ( MAX_CHAR ) ] NEW_LINE a2 = [ 0 for i in range ( MAX_CHAR ) ] NEW_LINE length1 = len ( s1 ) NEW_LINE length2 = len ( s2 ) NEW_LINE for i in range ( 0 , length1 ) : NEW_LINE INDENT a1 [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 0 , length2 ) : NEW_LINE INDENT a2 [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 0 , MAX_CHAR ) : NEW_LINE INDENT if ( a1 [ i ] != 0 and a2 [ i ] != 0 ) : NEW_LINE INDENT for j in range ( 0 , min ( a1 [ i ] , a2 [ i ] ) ) : NEW_LINE INDENT ch = chr ( ord ( ' a ' ) + i ) NEW_LINE print ( ch , end = ' ' ) NEW_LINE DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " geeksforgeeks " NEW_LINE s2 = " practiceforgeeks " NEW_LINE printCommon ( s1 , s2 ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int pairAndSum ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int k = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) k ++ ; } ans += ( 1 << i ) * ( k * ( k - 1 ) / 2 ) ; } return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 5 , 10 , 15 } ; int n = arr . length ; System . out . println ( pairAndSum ( arr , n ) ) ; } }

def pairAndSum ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , 32 ) : NEW_LINE INDENT k = 0 NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( arr [ j ] & ( 1 << i ) ) ) : NEW_LINE INDENT k = k + 1 NEW_LINE DEDENT DEDENT ans = ans + ( 1 << i ) * ( k * ( k - 1 ) // 2 ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 5 , 10 , 15 ] NEW_LINE n = len ( arr ) NEW_LINE print ( pairAndSum ( arr , n ) ) NEW_LINE


class GFG { static int fib [ ] = new int [ 100005 ] ; static void computeFibonacci ( ) { fib [ 0 ] = 1 ; fib [ 1 ] = 1 ; for ( int i = 2 ; i < 100005 ; i ++ ) { fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; } } static int countString ( String str ) { int ans = 1 ; int cnt = 1 ; for ( int i = 1 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) == str . charAt ( i - 1 ) ) { cnt ++ ; } else { ans = ans * fib [ cnt ] ; cnt = 1 ; } } ans = ans * fib [ cnt ] ; return ans ; } public static void main ( String [ ] args ) { String str = " abdllldefkkkk " ; computeFibonacci ( ) ; System . out . println ( countString ( str ) ) ; } }

fib = [ 0 ] * 100005 ; NEW_LINE def computeFibonacci ( ) : NEW_LINE INDENT fib [ 0 ] = 1 ; NEW_LINE fib [ 1 ] = 1 ; NEW_LINE for i in range ( 2 , 100005 ) : NEW_LINE INDENT fib [ i ] = fib [ i - 1 ] + fib [ i - 2 ] ; NEW_LINE DEDENT DEDENT def countString ( string ) : NEW_LINE INDENT ans = 1 ; NEW_LINE cnt = 1 ; NEW_LINE for i in range ( 1 , len ( string ) ) : NEW_LINE INDENT if ( string [ i ] == string [ i - 1 ] ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ans = ans * fib [ cnt ] ; NEW_LINE cnt = 1 ; NEW_LINE DEDENT DEDENT ans = ans * fib [ cnt ] ; NEW_LINE return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " abdllldefkkkk " ; NEW_LINE computeFibonacci ( ) ; NEW_LINE print ( countString ( string ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static void alter ( long x , long y ) { while ( true ) { if ( x == 0 || y == 0 ) break ; if ( x >= 2 * y ) x = x % ( 2 * y ) ; else if ( y >= 2 * x ) y = y % ( 2 * x ) ; else break ; } System . out . println ( " X ▁ = ▁ " + x + " , ▁ " + " Y ▁ = ▁ " + y ) ; } public static void main ( String [ ] args ) { long x = 12 , y = 5 ; alter ( x , y ) ; } }

import math as mt NEW_LINE def alter ( x , y ) : NEW_LINE INDENT while ( True ) : NEW_LINE INDENT if ( x == 0 or y == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( x >= 2 * y ) : NEW_LINE INDENT x = x % ( 2 * y ) NEW_LINE DEDENT elif ( y >= 2 * x ) : NEW_LINE INDENT y = y % ( 2 * x ) NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT print ( " X ▁ = " , x , " , ▁ " , " Y ▁ = " , y ) NEW_LINE DEDENT x , y = 12 , 5 NEW_LINE alter ( x , y ) NEW_LINE


import java . util . HashSet ; public class GFG { static int getPairs ( int a [ ] , int n ) { HashSet < Integer > visited1 = new HashSet < Integer > ( ) ; int un [ ] = new int [ n ] ; un [ n - 1 ] = 0 ; int count = 0 ; for ( int i = n - 1 ; i > 0 ; i -- ) { if ( visited1 . contains ( a [ i ] ) ) un [ i - 1 ] = count ; else un [ i - 1 ] = ++ count ; visited1 . add ( a [ i ] ) ; } HashSet < Integer > visited2 = new HashSet < Integer > ( ) ; int answer = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { if ( visited2 . contains ( a [ i ] ) ) continue ; answer += un [ i ] ; visited2 . add ( a [ i ] ) ; } return answer ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 } ; int n = a . length ; System . out . println ( getPairs ( a , n ) ) ; } }

def getPairs ( a , n ) : NEW_LINE INDENT visited1 = set ( ) NEW_LINE un = [ 0 ] * n NEW_LINE un [ n - 1 ] = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( a [ i ] in visited1 ) : NEW_LINE INDENT un [ i - 1 ] = count NEW_LINE DEDENT else : NEW_LINE INDENT count += 1 NEW_LINE un [ i - 1 ] = count NEW_LINE DEDENT visited1 . add ( a [ i ] ) NEW_LINE DEDENT visited2 = set ( ) NEW_LINE answer = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT if ( a [ i ] in visited2 ) : NEW_LINE INDENT continue NEW_LINE DEDENT answer += un [ i ] NEW_LINE visited2 . add ( a [ i ] ) NEW_LINE DEDENT return answer NEW_LINE DEDENT a = [ 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 ] NEW_LINE n = len ( a ) NEW_LINE print ( getPairs ( a , n ) ) NEW_LINE


import java . util . * ; class GFG { public static void findFibSubset ( Integer [ ] x ) { Integer max = Collections . max ( Arrays . asList ( x ) ) ; List < Integer > fib = new ArrayList < Integer > ( ) ; List < Integer > result = new ArrayList < Integer > ( ) ; Integer a = 0 ; Integer b = 1 ; while ( b < max ) { Integer c = a + b ; a = b ; b = c ; fib . add ( c ) ; } for ( Integer i = 0 ; i < x . length ; i ++ ) { if ( fib . contains ( x [ i ] ) ) { result . add ( x [ i ] ) ; } } System . out . println ( result ) ; } public static void main ( String args [ ] ) { Integer [ ] a = { 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 } ; findFibSubset ( a ) ; } }

def findFibSubset ( arr , n ) : NEW_LINE INDENT m = max ( arr ) NEW_LINE a = 0 NEW_LINE b = 1 NEW_LINE hash = [ ] NEW_LINE hash . append ( a ) NEW_LINE hash . append ( b ) NEW_LINE while ( b < m ) : NEW_LINE INDENT c = a + b NEW_LINE a = b NEW_LINE b = c NEW_LINE hash . append ( b ) NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if arr [ i ] in hash : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 2 , 8 , 5 , 20 , 1 , 40 , 13 , 23 ] NEW_LINE n = len ( arr ) NEW_LINE findFibSubset ( arr , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int c ; static int HailstoneNumbers ( int N ) { System . out . print ( N + " ▁ " ) ; if ( N == 1 && c == 0 ) { return c ; } else if ( N == 1 && c != 0 ) { c ++ ; return c ; } else if ( N % 2 == 0 ) { c ++ ; HailstoneNumbers ( N / 2 ) ; } else if ( N % 2 != 0 ) { c ++ ; HailstoneNumbers ( 3 * N + 1 ) ; } return c ; } public static void main ( String [ ] args ) { int N = 7 ; int x ; x = HailstoneNumbers ( N ) ; System . out . println ( ) ; System . out . println ( " Number ▁ of ▁ Steps : ▁ " + x ) ; } }

def HailstoneNumbers ( N , c ) : NEW_LINE INDENT print ( N , end = " ▁ " ) ; NEW_LINE if ( N == 1 and c == 0 ) : NEW_LINE INDENT return c ; NEW_LINE DEDENT elif ( N == 1 and c != 0 ) : NEW_LINE INDENT c = c + 1 ; NEW_LINE DEDENT elif ( N % 2 == 0 ) : NEW_LINE INDENT c = c + 1 ; NEW_LINE c = HailstoneNumbers ( int ( N / 2 ) , c ) ; NEW_LINE DEDENT elif ( N % 2 != 0 ) : NEW_LINE INDENT c = c + 1 ; NEW_LINE c = HailstoneNumbers ( 3 * N + 1 , c ) ; NEW_LINE DEDENT return c ; NEW_LINE DEDENT N = 7 ; NEW_LINE x = HailstoneNumbers ( N , 0 ) ; NEW_LINE print ( " \n Number ▁ of ▁ Steps : ▁ " , x ) ; NEW_LINE


class GFG { static int countFactor ( int P , int X ) { if ( X < P ) return 0 ; return ( X / P + countFactor ( P , X / P ) ) ; } static int findSmallestX ( int Y ) { int low = 0 , high = 5 * Y ; int N = 0 ; while ( low <= high ) { int mid = ( high + low ) / 2 ; if ( countFactor ( 5 , mid ) < Y ) { low = mid + 1 ; } else { N = mid ; high = mid - 1 ; } } return N ; } public static void main ( String args [ ] ) { int Y = 10 ; System . out . println ( findSmallestX ( Y ) ) ; } }

def countFactor ( P , X ) : NEW_LINE INDENT if ( X < P ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT return ( X // P + countFactor ( P , X // P ) ) ; NEW_LINE DEDENT def findSmallestX ( Y ) : NEW_LINE INDENT low = 0 ; NEW_LINE high = 5 * Y ; NEW_LINE N = 0 ; NEW_LINE while ( low <= high ) : NEW_LINE INDENT mid = ( high + low ) // 2 ; NEW_LINE if ( countFactor ( 5 , mid ) < Y ) : NEW_LINE INDENT low = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT N = mid ; NEW_LINE high = mid - 1 ; NEW_LINE DEDENT DEDENT return N ; NEW_LINE DEDENT Y = 10 ; NEW_LINE print ( findSmallestX ( Y ) ) ; NEW_LINE


class GFG { static Node head ; static class Node { int data ; Node next ; } ; static Node 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 ; return head = head_ref ; } static int sumOfNodes ( Node head ) { Node ptr = head ; int sum = 0 ; while ( ptr != null ) { sum += ptr . data ; ptr = ptr . next ; } return sum ; } public static void main ( String [ ] args ) { push ( head , 7 ) ; push ( head , 6 ) ; push ( head , 8 ) ; push ( head , 4 ) ; push ( head , 1 ) ; System . out . println ( " Sum ▁ of ▁ nodes ▁ = ▁ " + sumOfNodes ( 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 push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = head_ref NEW_LINE head_ref = new_node NEW_LINE return head_ref NEW_LINE DEDENT def sumOfNodes ( head ) : NEW_LINE INDENT ptr = head NEW_LINE sum = 0 NEW_LINE while ( ptr != None ) : NEW_LINE INDENT sum = sum + ptr . data NEW_LINE ptr = ptr . next NEW_LINE DEDENT return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE head = push ( head , 7 ) NEW_LINE head = push ( head , 6 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 1 ) NEW_LINE print ( " Sum ▁ of ▁ nodes ▁ = " , sumOfNodes ( head ) ) NEW_LINE DEDENT


import java . util . HashSet ; class CP { public static void main ( String [ ] args ) { int [ ] A = { 11 , 15 , 7 , 19 } ; int N = A . length ; HashSet < Integer > set = new HashSet < > ( ) ; int i , j , res ; for ( i = 0 ; i < N ; ++ i ) for ( j = i , res = Integer . MAX_VALUE ; j < N ; ++ j ) { res &= A [ j ] ; set . add ( res ) ; } System . out . println ( set ) ; } }

A = [ 11 , 15 , 7 , 19 ] NEW_LINE N = len ( A ) NEW_LINE Set = set ( ) NEW_LINE for i in range ( 0 , N ) : NEW_LINE INDENT res = 2147483647 NEW_LINE for j in range ( i , N ) : NEW_LINE INDENT res &= A [ j ] NEW_LINE Set . add ( res ) NEW_LINE DEDENT DEDENT print ( Set ) NEW_LINE


class GFG { static void ans ( int n ) { if ( n == 1 ) System . out . println ( "1" ) ; else System . out . println ( "0" ) ; } public static void main ( String [ ] args ) { int n = 2 ; ans ( n ) ; } }

def ans ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( "1" , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( "0" , end = " " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 2 ; NEW_LINE ans ( n ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static void subsetBitwiseORk ( int arr [ ] , int n , int k ) { ArrayList < Integer > v = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( arr [ i ] | k ) == k ) { v . add ( arr [ i ] ) ; } } int ans = 0 ; for ( int i = 0 ; i < v . size ( ) ; i ++ ) ans = ans | v . get ( i ) ; if ( ans != k ) { System . out . println ( " Subset ▁ does " + " ▁ not ▁ exist " ) ; return ; } for ( int i = 0 ; i < v . size ( ) ; i ++ ) System . out . print ( v . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int k = 3 ; int arr [ ] = { 1 , 4 , 2 } ; int n = arr . length ; subsetBitwiseORk ( arr , n , k ) ; } }

def subsetBitwiseORk ( arr , n , k ) : NEW_LINE INDENT v = [ ] NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( ( arr [ i ] | k ) == k ) : NEW_LINE INDENT v . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT ans = 0 NEW_LINE for i in range ( 0 , len ( v ) ) : NEW_LINE INDENT ans |= v [ i ] NEW_LINE DEDENT if ( ans != k ) : NEW_LINE INDENT print ( " Subset ▁ does ▁ not ▁ exist \n " ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , len ( v ) ) : NEW_LINE INDENT print ( " { } ▁ " . format ( v [ i ] ) , end = " " ) NEW_LINE DEDENT DEDENT k = 3 NEW_LINE arr = [ 1 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE subsetBitwiseORk ( arr , n , k ) NEW_LINE


class GFG { static void findTheNumber ( int n ) { if ( n == 1 ) { System . out . print ( " Impossible " + " \n " ) ; return ; } for ( int i = 0 ; i < n - 1 ; i ++ ) { System . out . print ( "5" ) ; } System . out . print ( "4" ) ; } public static void main ( String [ ] args ) { int n = 12 ; findTheNumber ( n ) ; } }

def findTheNumber ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT print ( " Impossible " ) NEW_LINE return NEW_LINE DEDENT for i in range ( n - 1 ) : NEW_LINE INDENT print ( "5" , end = " " ) NEW_LINE DEDENT print ( "4" ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 12 NEW_LINE findTheNumber ( n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int N = 100005 ; static int [ ] lpf = new int [ N ] ; static void least_prime_factor ( ) { for ( int i = 2 ; i < N ; i ++ ) if ( lpf [ i ] % 2 != 1 ) for ( int j = i ; j < N ; j += i ) if ( lpf [ j ] % 2 != 0 ) lpf [ j ] = i ; } static void Mobius ( int n ) { int [ ] mobius = new int [ N ] ; for ( int i = 1 ; i < N ; i ++ ) { if ( i == 1 ) mobius [ i ] = 1 ; else { if ( lpf [ i / lpf [ i ] ] == lpf [ i ] ) mobius [ i ] = 0 ; else mobius [ i ] = - 1 * mobius [ i / lpf [ i ] ] ; } } for ( int i = 1 ; i <= n ; i ++ ) System . out . print ( mobius [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 5 ; Arrays . fill ( lpf , - 1 ) ; least_prime_factor ( ) ; Mobius ( n ) ; } }

N = 100005 NEW_LINE lpf = [ 0 ] * N ; NEW_LINE def least_prime_factor ( ) : NEW_LINE INDENT for i in range ( 2 , N ) : NEW_LINE INDENT if ( not lpf [ i ] ) : NEW_LINE INDENT for j in range ( i , N , i ) : NEW_LINE INDENT if ( not lpf [ j ] ) : NEW_LINE INDENT lpf [ j ] = i ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def Mobius ( n ) : NEW_LINE INDENT mobius = [ 0 ] * N ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT if ( i == 1 ) : NEW_LINE INDENT mobius [ i ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT if ( lpf [ i // lpf [ i ] ] == lpf [ i ] ) : NEW_LINE INDENT mobius [ i ] = 0 ; NEW_LINE DEDENT else : NEW_LINE INDENT mobius [ i ] = - 1 * mobius [ i // lpf [ i ] ] ; NEW_LINE DEDENT DEDENT DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( mobius [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE least_prime_factor ( ) ; NEW_LINE Mobius ( n ) ; NEW_LINE DEDENT


class GFG { static String isDivisible ( int n ) { int temp = n ; while ( n > 0 ) { int k = n % 10 ; if ( temp % k == 0 ) { return " YES " ; } n /= 10 ; } return " NO " ; } public static void main ( String [ ] args ) { int n = 9876543 ; System . out . println ( isDivisible ( n ) ) ; } }

def isDivisible ( n ) : NEW_LINE INDENT temp = n NEW_LINE while ( n ) : NEW_LINE INDENT k = n % 10 NEW_LINE if ( temp % k == 0 ) : NEW_LINE INDENT return " YES " NEW_LINE DEDENT n /= 10 ; NEW_LINE DEDENT return " NO " NEW_LINE DEDENT n = 9876543 NEW_LINE print ( isDivisible ( n ) ) NEW_LINE


import java . io . * ; import java . util . * ; class GFG { static Queue < Integer > q = new LinkedList < Integer > ( ) ; static boolean checkSorted ( int n ) { Stack < Integer > st = new Stack < Integer > ( ) ; int expected = 1 ; int fnt ; while ( q . size ( ) != 0 ) { fnt = q . peek ( ) ; q . poll ( ) ; if ( fnt == expected ) expected ++ ; else { if ( st . size ( ) == 0 ) { st . push ( fnt ) ; } else if ( st . size ( ) != 0 && st . peek ( ) < fnt ) { return false ; } else st . push ( fnt ) ; } while ( st . size ( ) != 0 && st . peek ( ) == expected ) { st . pop ( ) ; expected ++ ; } } if ( expected - 1 == n && st . size ( ) == 0 ) return true ; return false ; } public static void main ( String args [ ] ) { q . add ( 5 ) ; q . add ( 1 ) ; q . add ( 2 ) ; q . add ( 3 ) ; q . add ( 4 ) ; int n = q . size ( ) ; if ( checkSorted ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

from queue import Queue NEW_LINE def checkSorted ( n , q ) : NEW_LINE INDENT st = [ ] NEW_LINE expected = 1 NEW_LINE fnt = None NEW_LINE while ( not q . empty ( ) ) : NEW_LINE INDENT fnt = q . queue [ 0 ] NEW_LINE q . get ( ) NEW_LINE if ( fnt == expected ) : NEW_LINE INDENT expected += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( len ( st ) == 0 ) : NEW_LINE INDENT st . append ( fnt ) NEW_LINE DEDENT elif ( len ( st ) != 0 and st [ - 1 ] < fnt ) : NEW_LINE INDENT return False NEW_LINE DEDENT else : NEW_LINE INDENT st . append ( fnt ) NEW_LINE DEDENT DEDENT while ( len ( st ) != 0 and st [ - 1 ] == expected ) : NEW_LINE INDENT st . pop ( ) NEW_LINE expected += 1 NEW_LINE DEDENT DEDENT if ( expected - 1 == n and len ( st ) == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = Queue ( ) NEW_LINE q . put ( 5 ) NEW_LINE q . put ( 1 ) NEW_LINE q . put ( 2 ) NEW_LINE q . put ( 3 ) NEW_LINE q . put ( 4 ) NEW_LINE n = q . qsize ( ) NEW_LINE if checkSorted ( n , q ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT


class GFG { public static double fact [ ] ; public static double probability ( int k , int n ) { double ans = 0 ; for ( int i = k ; i <= n ; ++ i ) ans += fact [ n ] / ( fact [ i ] * fact [ n - i ] ) ; ans = ans / ( 1 << n ) ; return ans ; } public static void precompute ( ) { fact [ 0 ] = fact [ 1 ] = 1 ; for ( int i = 2 ; i < 20 ; ++ i ) fact [ i ] = fact [ i - 1 ] * i ; } public static void main ( String [ ] args ) { fact = new double [ 100 ] ; precompute ( ) ; System . out . println ( probability ( 2 , 3 ) ) ; System . out . println ( probability ( 3 , 6 ) ) ; System . out . println ( probability ( 12 , 18 ) ) ; } }

MAX = 21 NEW_LINE fact = [ 0 ] * MAX NEW_LINE def probability ( k , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( k , n + 1 ) : NEW_LINE INDENT ans += fact [ n ] / ( fact [ i ] * fact [ n - i ] ) NEW_LINE DEDENT ans = ans / ( 1 << n ) NEW_LINE return ans NEW_LINE DEDENT def precompute ( ) : NEW_LINE INDENT fact [ 0 ] = 1 NEW_LINE fact [ 1 ] = 1 NEW_LINE for i in range ( 2 , 20 ) : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT precompute ( ) NEW_LINE print ( probability ( 2 , 3 ) ) NEW_LINE print ( probability ( 3 , 6 ) ) NEW_LINE print ( probability ( 12 , 18 ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int digSum ( int n ) { if ( n == 0 ) return 0 ; return ( n % 9 == 0 ) ? 9 : ( n % 9 ) ; } static int powerDigitSum ( int a , int n ) { int res = 1 ; while ( n > 0 ) { if ( n % 2 == 1 ) { res = res * digSum ( a ) ; res = digSum ( res ) ; } a = digSum ( digSum ( a ) * digSum ( a ) ) ; n /= 2 ; } return res ; } public static void main ( String args [ ] ) { int a = 9 , n = 4 ; System . out . print ( powerDigitSum ( a , n ) ) ; } }

def digSum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif n % 9 == 0 : NEW_LINE INDENT return 9 NEW_LINE DEDENT else : NEW_LINE INDENT return n % 9 NEW_LINE DEDENT DEDENT def powerDigitSum ( a , n ) : NEW_LINE INDENT res = 1 NEW_LINE while ( n ) : NEW_LINE INDENT if n % 2 == 1 : NEW_LINE INDENT res = res * digSum ( a ) NEW_LINE res = digSum ( res ) NEW_LINE DEDENT a = digSum ( digSum ( a ) * digSum ( a ) ) NEW_LINE n //= 2 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , n = 9 , 4 NEW_LINE print ( powerDigitSum ( a , n ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int maxResult ( int n , int a , int b , int c ) { int maxVal = 0 ; for ( int i = 0 ; i <= n ; i += a ) for ( int j = 0 ; j <= n - i ; j += b ) { float z = ( n - ( i + j ) ) / c ; if ( Math . floor ( z ) == Math . ceil ( z ) ) { int x = i / a ; int y = j / b ; maxVal = Math . max ( maxVal , x + y + ( int ) z ) ; } } return maxVal ; } public static void main ( String args [ ] ) { int n = 10 , a = 5 , b = 3 , c = 4 ; System . out . println ( maxResult ( n , a , b , c ) ) ; } }

from math import * ; NEW_LINE def maxResult ( n , a , b , c ) : NEW_LINE INDENT maxVal = 0 ; NEW_LINE for i in range ( 0 , n + 1 , a ) : NEW_LINE INDENT for j in range ( 0 , n - i + 1 , b ) : NEW_LINE INDENT z = ( n - ( i + j ) ) / c ; NEW_LINE if ( floor ( z ) == ceil ( z ) ) : NEW_LINE INDENT x = i // a ; NEW_LINE y = j // b ; NEW_LINE maxVal = max ( maxVal , x + y + int ( z ) ) ; NEW_LINE DEDENT DEDENT DEDENT return maxVal ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 10 NEW_LINE a = 5 NEW_LINE b = 3 NEW_LINE c = 4 NEW_LINE print ( maxResult ( n , a , b , c ) ) ; NEW_LINE DEDENT


class GFG { static int RSF ( int n ) { while ( n >= 10 ) { int x = n ; int l = 0 ; while ( n > 0 ) { n = n / 10 ; l ++ ; } int a [ ] = new int [ l ] ; int i = l - 1 ; while ( x > 0 ) { a [ i ] = x % 10 ; x = x / 10 ; i -- ; } for ( int j = 0 ; j < l - 1 ; j ++ ) { n = n * 10 + Math . abs ( a [ j ] - a [ j + 1 ] ) ; } } return n ; } public static void main ( String [ ] arg ) { int n = 6972 ; int ans = RSF ( n ) ; System . out . println ( ans ) ; } }

def RSF ( n ) : NEW_LINE INDENT while ( n >= 10 ) : NEW_LINE INDENT x = n ; NEW_LINE l = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT n = n // 10 ; NEW_LINE l += 1 ; NEW_LINE DEDENT a = [ 0 ] * l ; NEW_LINE i = l - 1 ; NEW_LINE while ( x > 0 ) : NEW_LINE INDENT a [ i ] = x % 10 ; NEW_LINE x = x // 10 ; NEW_LINE i -= 1 ; NEW_LINE DEDENT for j in range ( 0 , l - 1 ) : NEW_LINE INDENT n = n * 10 + abs ( a [ j ] - a [ j + 1 ] ) ; NEW_LINE DEDENT DEDENT return n ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 614 ; NEW_LINE ans = RSF ( n ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { public static void rearrange ( int [ ] arr ) { if ( arr == null || arr . length % 2 == 1 ) return ; int currIdx = ( arr . length - 1 ) / 2 ; while ( currIdx > 0 ) { int count = currIdx , swapIdx = currIdx ; while ( count -- > 0 ) { int temp = arr [ swapIdx + 1 ] ; arr [ swapIdx + 1 ] = arr [ swapIdx ] ; arr [ swapIdx ] = temp ; swapIdx ++ ; } currIdx -- ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 3 , 5 , 2 , 4 , 6 } ; rearrange ( arr ) ; for ( int i = 0 ; i < arr . length ; i ++ ) System . out . print ( " ▁ " + arr [ i ] ) ; } }

arr = [ 1 , 3 , 5 , 2 , 4 , 6 ] NEW_LINE def rearrange ( n ) : NEW_LINE INDENT global arr NEW_LINE if ( n % 2 == 1 ) : NEW_LINE INDENT return NEW_LINE DEDENT currIdx = int ( ( n - 1 ) / 2 ) NEW_LINE while ( currIdx > 0 ) : NEW_LINE INDENT count = currIdx NEW_LINE swapIdx = currIdx NEW_LINE while ( count > 0 ) : NEW_LINE INDENT temp = arr [ swapIdx + 1 ] NEW_LINE arr [ swapIdx + 1 ] = arr [ swapIdx ] NEW_LINE arr [ swapIdx ] = temp NEW_LINE swapIdx = swapIdx + 1 NEW_LINE count = count - 1 NEW_LINE DEDENT currIdx = currIdx - 1 NEW_LINE DEDENT DEDENT n = len ( arr ) NEW_LINE rearrange ( n ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( " { } ▁ " . format ( arr [ i ] ) , end = " " ) NEW_LINE DEDENT


public class GFG { static boolean isPalindrome ( int n ) { int divisor = 1 ; while ( n / divisor >= 10 ) divisor *= 10 ; while ( n != 0 ) { int leading = n / divisor ; int trailing = n % 10 ; if ( leading != trailing ) return false ; n = ( n % divisor ) / 10 ; divisor = divisor / 100 ; } return true ; } public static void main ( String args [ ] ) { if ( isPalindrome ( 1001 ) ) System . out . println ( " Yes , ▁ it ▁ is ▁ Palindrome " ) ; else System . out . println ( " No , ▁ not ▁ Palindrome " ) ; } }

def isPalindrome ( n ) : NEW_LINE INDENT divisor = 1 NEW_LINE while ( n / divisor >= 10 ) : NEW_LINE INDENT divisor *= 10 NEW_LINE DEDENT while ( n != 0 ) : NEW_LINE INDENT leading = n // divisor NEW_LINE trailing = n % 10 NEW_LINE if ( leading != trailing ) : NEW_LINE INDENT return False NEW_LINE DEDENT n = ( n % divisor ) // 10 NEW_LINE divisor = divisor / 100 NEW_LINE DEDENT return True NEW_LINE DEDENT if ( isPalindrome ( 1001 ) ) : NEW_LINE INDENT print ( ' Yes , ▁ it ▁ is ▁ palindrome ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' No , ▁ not ▁ palindrome ' ) NEW_LINE DEDENT


import java . io . * ; class GFG { static boolean isEven ( int arr [ ] , int n , int r ) { if ( r % 2 == 0 ) { if ( arr [ n - 1 ] % 2 == 0 ) return true ; } else { int oddCount = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( arr [ i ] % 2 != 0 ) oddCount ++ ; } if ( oddCount % 2 == 0 ) return true ; } return false ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 0 } ; int n = arr . length ; int r = 2 ; if ( isEven ( arr , n , r ) ) System . out . println ( " Even " ) ; else System . out . println ( " Odd " ) ; } }

def isEven ( arr , n , r ) : NEW_LINE INDENT if ( r % 2 == 0 ) : NEW_LINE INDENT if ( arr [ n - 1 ] % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT oddCount = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] % 2 != 0 ) : NEW_LINE INDENT oddCount += 1 NEW_LINE DEDENT DEDENT if ( oddCount % 2 == 0 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE r = 2 NEW_LINE if ( isEven ( arr , n , r ) ) : NEW_LINE INDENT print ( " Even " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Odd " ) NEW_LINE DEDENT DEDENT


public class GFG { static int max = 100 ; static void countSubsequence ( String s , int n ) { int cntG = 0 , cntF = 0 , result = 0 , C = 0 ; for ( int i = 0 ; i < n ; i ++ ) { switch ( s . charAt ( i ) ) { case ' G ' : cntG ++ ; result += C ; break ; case ' F ' : cntF ++ ; C += cntG ; break ; default : continue ; } } System . out . println ( result ) ; } public static void main ( String args [ ] ) { String s = " GFGFG " ; int n = s . length ( ) ; countSubsequence ( s , n ) ; } }

MAX = 100 NEW_LINE def countSubsequence ( s , n ) : NEW_LINE INDENT cntG = 0 NEW_LINE cntF = 0 NEW_LINE result = 0 NEW_LINE C = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( s [ i ] == ' G ' ) : NEW_LINE INDENT cntG += 1 NEW_LINE result += C NEW_LINE continue NEW_LINE DEDENT if ( s [ i ] == ' F ' ) : NEW_LINE INDENT cntF += 1 NEW_LINE C += cntG NEW_LINE continue NEW_LINE DEDENT else : NEW_LINE INDENT continue NEW_LINE DEDENT DEDENT print ( result ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " GFGFG " NEW_LINE n = len ( s ) NEW_LINE countSubsequence ( s , n ) NEW_LINE DEDENT


public class GFG { static String newString ( String s ) { int l = s . length ( ) ; int freq [ ] = new int [ 26 ] ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] += 1 ; } String ans = " " ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < freq [ i ] ; j ++ ) { ans += ( char ) ( 97 + i ) ; } } return ans ; } public static void main ( String args [ ] ) { String s = " aabab " ; System . out . println ( newString ( s ) ) ; } }

def newString ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE freq = [ 0 ] * ( 26 ) NEW_LINE for i in range ( 0 , l ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT ans = " " NEW_LINE for i in range ( 0 , 26 ) : NEW_LINE INDENT for j in range ( 0 , freq [ i ] ) : NEW_LINE INDENT ans += chr ( 97 + i ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " aabab " NEW_LINE print ( newString ( s ) ) NEW_LINE DEDENT


public class Demo { public static int longestSubarray ( int [ ] input ) { int prev = - 1 ; int current , next ; int prevCount = 0 , currentCount = 1 ; int longest = 1 ; current = input [ 0 ] ; for ( int i = 1 ; i < input . length ; i ++ ) { next = input [ i ] ; if ( next == current ) { currentCount ++ ; } else if ( next == prev ) { prevCount += currentCount ; prev = current ; current = next ; currentCount = 1 ; } else { longest = Math . max ( longest , currentCount + prevCount ) ; prev = current ; prevCount = currentCount ; current = next ; currentCount = 1 ; } } return Math . max ( longest , currentCount + prevCount ) ; } public static void main ( String [ ] args ) { int [ ] input = { 5 , 5 , 6 , 7 , 6 } ; System . out . println ( longestSubarray ( input ) ) ; } }

def longestSubarray ( input , length ) : NEW_LINE INDENT prev = - 1 NEW_LINE prevCount = 0 NEW_LINE currentCount = 1 NEW_LINE longest = 1 NEW_LINE current = input [ 0 ] NEW_LINE for i in range ( 1 , length ) : NEW_LINE INDENT next = input [ i ] NEW_LINE if next == current : NEW_LINE INDENT currentCount += 1 NEW_LINE DEDENT elif next == prev : NEW_LINE INDENT prevCount += currentCount NEW_LINE prev = current NEW_LINE current = next NEW_LINE currentCount = 1 NEW_LINE DEDENT else : NEW_LINE INDENT longest = max ( longest , currentCount + prevCount ) NEW_LINE prev = current NEW_LINE prevCount = currentCount NEW_LINE current = next NEW_LINE currentCount = 1 NEW_LINE DEDENT DEDENT return max ( longest , currentCount + prevCount ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT input = [ 5 , 5 , 6 , 7 , 6 ] NEW_LINE n = len ( input ) NEW_LINE print ( longestSubarray ( input , n ) ) NEW_LINE DEDENT


class GFG { static int MAX = 10 ; static int lcs ( int [ ] [ ] [ ] dp , int [ ] arr1 , int n , int [ ] arr2 , int m , int k ) { if ( k < 0 ) return - 10000000 ; if ( n < 0 || m < 0 ) return 0 ; int ans = dp [ n ] [ m ] [ k ] ; if ( ans != - 1 ) return ans ; try { ans = Math . max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) ) ; if ( arr1 [ n - 1 ] == arr2 [ m - 1 ] ) ans = Math . max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k ) ) ; ans = Math . max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k - 1 ) ) ; } catch ( Exception e ) { } return ans ; } public static void main ( String [ ] args ) { int k = 1 ; int [ ] arr1 = { 1 , 2 , 3 , 4 , 5 } ; int [ ] arr2 = { 5 , 3 , 1 , 4 , 2 } ; int n = arr1 . length ; int m = arr2 . length ; int [ ] [ ] [ ] dp = new int [ MAX ] [ MAX ] [ MAX ] ; for ( int i = 0 ; i < MAX ; i ++ ) for ( int j = 0 ; j < MAX ; j ++ ) for ( int l = 0 ; l < MAX ; l ++ ) dp [ i ] [ j ] [ l ] = - 1 ; System . out . println ( lcs ( dp , arr1 , n , arr2 , m , k ) ) ; } }

MAX = 10 NEW_LINE def lcs ( dp , arr1 , n , arr2 , m , k ) : NEW_LINE INDENT if k < 0 : NEW_LINE INDENT return - ( 10 ** 7 ) NEW_LINE DEDENT if n < 0 or m < 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT ans = dp [ n ] [ m ] [ k ] NEW_LINE if ans != - 1 : NEW_LINE INDENT return ans NEW_LINE DEDENT ans = max ( lcs ( dp , arr1 , n - 1 , arr2 , m , k ) , lcs ( dp , arr1 , n , arr2 , m - 1 , k ) ) NEW_LINE if arr1 [ n - 1 ] == arr2 [ m - 1 ] : NEW_LINE INDENT ans = max ( ans , 1 + lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k ) ) NEW_LINE DEDENT ans = max ( ans , lcs ( dp , arr1 , n - 1 , arr2 , m - 1 , k - 1 ) ) NEW_LINE return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT k = 1 NEW_LINE arr1 = [ 1 , 2 , 3 , 4 , 5 ] NEW_LINE arr2 = [ 5 , 3 , 1 , 4 , 2 ] NEW_LINE n = len ( arr1 ) NEW_LINE m = len ( arr2 ) NEW_LINE dp = [ [ [ - 1 for i in range ( MAX ) ] for j in range ( MAX ) ] for k in range ( MAX ) ] NEW_LINE print ( lcs ( dp , arr1 , n , arr2 , m , k ) ) NEW_LINE DEDENT


class IsDivisible { static boolean check ( String str ) { int n = str . length ( ) ; if ( n == 0 ) return false ; if ( n == 1 ) return ( ( str . charAt ( 0 ) - '0' ) % 4 == 0 ) ; int last = str . charAt ( n - 1 ) - '0' ; int second_last = str . charAt ( n - 2 ) - '0' ; return ( ( second_last * 10 + last ) % 4 == 0 ) ; } public static void main ( String [ ] args ) { String str = "76952" ; if ( check ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def check ( st ) : NEW_LINE INDENT n = len ( st ) NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return ( ( st [ 0 ] - '0' ) % 4 == 0 ) NEW_LINE DEDENT last = ( int ) ( st [ n - 1 ] ) NEW_LINE second_last = ( int ) ( st [ n - 2 ] ) NEW_LINE return ( ( second_last * 10 + last ) % 4 == 0 ) NEW_LINE DEDENT st = "76952" NEW_LINE if ( check ( st ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ " ) NEW_LINE DEDENT


import java . util . LinkedHashSet ; class GFG { static int repeated_digit ( int n ) { LinkedHashSet < Integer > s = new LinkedHashSet < > ( ) ; while ( n != 0 ) { int d = n % 10 ; if ( s . contains ( d ) ) { return 0 ; } s . add ( d ) ; n = n / 10 ; } return 1 ; } static int calculate ( int L , int R ) { int answer = 0 ; for ( int i = L ; i < R + 1 ; ++ i ) { answer = answer + repeated_digit ( i ) ; } return answer ; } public static void main ( String [ ] args ) { int L = 1 , R = 100 ; System . out . println ( calculate ( L , R ) ) ; } }

def repeated_digit ( n ) : NEW_LINE INDENT a = [ ] NEW_LINE while n != 0 : NEW_LINE INDENT d = n % 10 NEW_LINE if d in a : NEW_LINE INDENT return 0 NEW_LINE DEDENT a . append ( d ) NEW_LINE n = n // 10 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def calculate ( L , R ) : NEW_LINE INDENT answer = 0 NEW_LINE for i in range ( L , R + 1 ) : NEW_LINE INDENT answer = answer + repeated_digit ( i ) NEW_LINE DEDENT return answer NEW_LINE DEDENT L = 1 NEW_LINE R = 100 NEW_LINE print ( calculate ( L , R ) ) NEW_LINE


class GFG { static void last_digit ( int X , int Y ) { System . out . print ( X % Y ) ; } public static void main ( String [ ] args ) { int X = 55 , Y = 3 ; last_digit ( X , Y ) ; } }

def last_digit ( X , Y ) : NEW_LINE INDENT print ( X % Y ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = 55 ; Y = 3 ; NEW_LINE last_digit ( X , Y ) ; NEW_LINE DEDENT


class GFG { static int index ( int i ) { return 1 + ( i >> 31 ) - ( - i >> 31 ) ; } static void check ( int n ) { String s [ ] = { " negative " , " zero " , " positive " } ; int val = index ( n ) ; System . out . println ( n + " ▁ is ▁ " + s [ val ] ) ; } public static void main ( String [ ] args ) { check ( 30 ) ; check ( - 20 ) ; check ( 0 ) ; } }

def index ( i ) : NEW_LINE INDENT return 1 + ( i >> 31 ) - ( - i >> 31 ) NEW_LINE DEDENT def check ( n ) : NEW_LINE INDENT s = " negative " , " zero " , " positive " NEW_LINE val = index ( n ) NEW_LINE print ( n , " is " , s [ val ] ) NEW_LINE DEDENT check ( 30 ) NEW_LINE check ( - 20 ) NEW_LINE check ( 0 ) NEW_LINE


import java . util . * ; class GFG { static int minimalLength ( int a [ ] , int n ) { int [ ] [ ] dp = new int [ n + 1 ] [ n + 1 ] ; int [ ] dp1 = new int [ n ] ; int i , j , k ; for ( i = 0 ; i < n + 1 ; i ++ ) { for ( j = 0 ; j < n + 1 ; j ++ ) { dp [ i ] [ j ] = - 1 ; } } for ( int size = 1 ; size <= n ; size ++ ) { for ( i = 0 ; i < n - size + 1 ; i ++ ) { j = i + size - 1 ; if ( i == j ) dp [ i ] [ j ] = a [ i ] ; else { for ( k = i ; k < j ; k ++ ) { if ( dp [ i ] [ k ] != - 1 && dp [ i ] [ k ] == dp [ k + 1 ] [ j ] ) dp [ i ] [ j ] = dp [ i ] [ k ] + 1 ; } } } } for ( i = 0 ; i < n ; i ++ ) dp1 [ i ] = ( int ) 1e7 ; for ( i = 0 ; i < n ; i ++ ) { for ( j = 0 ; j <= i ; j ++ ) { if ( dp [ j ] [ i ] != - 1 ) { if ( j == 0 ) dp1 [ i ] = 1 ; else dp1 [ i ] = Math . min ( dp1 [ i ] , dp1 [ j - 1 ] + 1 ) ; } } } return dp1 [ n - 1 ] ; } public static void main ( String [ ] args ) { int n = 7 ; int a [ ] = { 3 , 3 , 4 , 4 , 4 , 3 , 3 } ; System . out . print ( minimalLength ( a , n ) ) ; } }

import numpy as np NEW_LINE def minimalLength ( a , n ) : NEW_LINE INDENT dp = np . ones ( ( n + 1 , n + 1 ) ) * - 1 ; NEW_LINE dp1 = [ 0 ] * n ; NEW_LINE for size in range ( 1 , n + 1 ) : NEW_LINE INDENT for i in range ( n - size + 1 ) : NEW_LINE INDENT j = i + size - 1 ; NEW_LINE if ( i == j ) : NEW_LINE INDENT dp [ i ] [ j ] = a [ i ] ; NEW_LINE DEDENT else : NEW_LINE INDENT for k in range ( i , j ) : NEW_LINE INDENT if ( dp [ i ] [ k ] != - 1 and dp [ i ] [ k ] == dp [ k + 1 ] [ j ] ) : NEW_LINE INDENT dp [ i ] [ j ] = dp [ i ] [ k ] + 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT dp1 [ i ] = int ( 1e7 ) ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT if ( dp [ j ] [ i ] != - 1 ) : NEW_LINE INDENT if ( j == 0 ) : NEW_LINE INDENT dp1 [ i ] = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT dp1 [ i ] = min ( dp1 [ i ] , dp1 [ j - 1 ] + 1 ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT return dp1 [ n - 1 ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 7 ; NEW_LINE a = [ 3 , 3 , 4 , 4 , 4 , 3 , 3 ] ; NEW_LINE print ( minimalLength ( a , n ) ) ; NEW_LINE DEDENT


public class GFG { static int countX ( int n ) { String binary = Integer . toBinaryString ( n ) ; int count = 0 ; for ( int i = 0 ; i < binary . length ( ) ; i ++ ) { if ( binary . charAt ( i ) == '1' ) count ++ ; } int answer = ( int ) Math . pow ( 2 , count ) ; return answer ; } public static void main ( String args [ ] ) { int n = 5 ; int answer = countX ( n ) ; System . out . println ( answer ) ; } }

def countX ( n ) : NEW_LINE INDENT binary = " { 0 : b } " . format ( n ) NEW_LINE count = 0 NEW_LINE for i in range ( len ( binary ) ) : NEW_LINE INDENT if ( binary [ i ] == '1' ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT answer = int ( pow ( 2 , count ) ) NEW_LINE return answer NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE answer = countX ( n ) NEW_LINE print ( answer ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void MinimumMaximumPairs ( int n , int m ) { int max_pairs = ( ( n - m + 1 ) * ( n - m ) ) / 2 ; int min_pairs = m * ( ( ( n - m ) / m + 1 ) * ( ( n - m ) / m ) ) / 2 + ( int ) Math . ceil ( ( double ) ( ( n - m ) / ( double ) ( m ) ) ) * ( ( n - m ) % m ) ; System . out . println ( " Minimum ▁ no . ▁ of ▁ pairs ▁ = ▁ " + min_pairs ) ; System . out . println ( " Maximum ▁ no . ▁ of ▁ pairs ▁ = ▁ " + max_pairs ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 2 ; MinimumMaximumPairs ( n , m ) ; } }

from math import ceil NEW_LINE def MinimumMaximumPairs ( n , m ) : NEW_LINE INDENT max_pairs = ( ( n - m + 1 ) * ( n - m ) ) // 2 ; NEW_LINE min_pairs = ( m * ( ( ( n - m ) // m + 1 ) * ( ( n - m ) // m ) ) // 2 + ceil ( ( n - m ) / ( m ) ) * ( ( n - m ) % m ) ) NEW_LINE print ( " Minimum ▁ no . ▁ of ▁ pairs ▁ = ▁ " , min_pairs ) NEW_LINE print ( " Maximum ▁ no . ▁ of ▁ pairs ▁ = ▁ " , max_pairs ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , m = 5 , 2 NEW_LINE MinimumMaximumPairs ( n , m ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void isPrime ( int N ) { boolean isPrime = true ; int [ ] arr = { 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 } ; if ( N < 2 ) { isPrime = false ; } if ( N % 2 == 0 || N % 3 == 0 || N % 5 == 0 ) { isPrime = false ; } for ( int i = 0 ; i < Math . sqrt ( N ) ; i += 30 ) { for ( int c : arr ) { if ( c > Math . sqrt ( N ) ) { break ; } else { if ( N % ( c + i ) == 0 ) { isPrime = false ; break ; } } if ( ! isPrime ) break ; } } if ( isPrime ) System . out . println ( " Prime ▁ Number " ) ; else System . out . println ( " Not ▁ a ▁ Prime ▁ Number " ) ; } public static void main ( String args [ ] ) { int N = 121 ; isPrime ( N ) ; } }

import math NEW_LINE def isPrime ( N ) : NEW_LINE INDENT isPrime = True ; NEW_LINE arr = [ 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 ] NEW_LINE if ( N < 2 ) : NEW_LINE INDENT isPrime = False NEW_LINE DEDENT if ( N % 2 == 0 or N % 3 == 0 or N % 5 == 0 ) : NEW_LINE INDENT isPrime = False NEW_LINE DEDENT for i in range ( 0 , int ( math . sqrt ( N ) ) , 30 ) : NEW_LINE INDENT for c in arr : NEW_LINE INDENT if ( c > int ( math . sqrt ( N ) ) ) : NEW_LINE INDENT break NEW_LINE DEDENT else : NEW_LINE INDENT if ( N % ( c + i ) == 0 ) : NEW_LINE INDENT isPrime = False NEW_LINE break NEW_LINE DEDENT DEDENT if ( not isPrime ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if ( isPrime ) : NEW_LINE INDENT print ( " Prime ▁ Number " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Not ▁ a ▁ Prime ▁ Number " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 121 NEW_LINE isPrime ( N ) NEW_LINE DEDENT


import java . util . * ; class GFG { static String minInt ( String str ) { String s = " " ; for ( int i = 0 ; i < str . length ( ) ; i ++ ) { if ( str . charAt ( i ) >= '5' ) { s += ( char ) ( ( '9' - str . charAt ( i ) ) + '0' ) ; } else { s += str . charAt ( i ) ; } } if ( str . charAt ( 0 ) == '0' ) s += '9' ; return s ; } public static void main ( String [ ] args ) { String str = "589" ; System . out . println ( minInt ( str ) ) ; } }

def minInt ( str1 ) : NEW_LINE INDENT for i in range ( len ( str1 ) ) : NEW_LINE INDENT if ( str1 [ i ] >= 5 ) : NEW_LINE INDENT str1 [ i ] = ( 9 - str1 [ i ] ) NEW_LINE DEDENT DEDENT if ( str1 [ 0 ] == 0 ) : NEW_LINE INDENT str1 [ 0 ] = 9 NEW_LINE DEDENT temp = " " NEW_LINE for i in str1 : NEW_LINE INDENT temp += str ( i ) NEW_LINE DEDENT return temp NEW_LINE DEDENT str1 = "589" NEW_LINE str1 = [ int ( i ) for i in str1 ] NEW_LINE print ( minInt ( str1 ) ) NEW_LINE


class GFG { static int times ( int [ ] steps , int n ) { int current_level = 0 ; int previous_level = 0 ; int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { previous_level = current_level ; current_level = current_level + steps [ i ] ; if ( ( previous_level < 0 && current_level >= 0 ) || ( previous_level > 0 && current_level <= 0 ) ) { count ++ ; } } return count ; } public static void main ( String [ ] args ) { int steps [ ] = { 1 , - 1 , 0 , 0 , 1 , 1 , - 3 , 2 } ; int n = steps . length ; System . out . println ( times ( steps , n ) ) ; } }

def times ( steps , n ) : NEW_LINE INDENT current_level = 0 NEW_LINE previous_level = 0 NEW_LINE count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT previous_level = current_level NEW_LINE current_level = current_level + steps [ i ] NEW_LINE if ( ( previous_level < 0 and current_level >= 0 ) or ( previous_level > 0 and current_level <= 0 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT steps = [ 1 , - 1 , 0 , 0 , 1 , 1 , - 3 , 2 ] NEW_LINE n = len ( steps ) NEW_LINE print ( times ( steps , n ) ) NEW_LINE


import java . util . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static void makePairs ( Vector < Integer > pairs , int a , int b ) { for ( int i = a ; i <= b ; i ++ ) { int sumOfDigits = 0 , k = i ; while ( k > 0 ) { sumOfDigits += k % 10 ; k /= 10 ; } if ( sumOfDigits <= 162 ) pairs . add ( sumOfDigits ) ; } } static int countCoPrime ( int a , int b ) { Vector < Integer > pairs = new Vector < Integer > ( ) ; makePairs ( pairs , a , b ) ; int count = 0 ; for ( int i = 0 ; i < pairs . size ( ) ; i ++ ) for ( int j = i + 1 ; j < pairs . size ( ) ; j ++ ) if ( GCD ( pairs . get ( i ) , pairs . get ( j ) ) == 1 ) count ++ ; return count ; } public static void main ( String args [ ] ) { int a = 12 , b = 15 ; System . out . println ( countCoPrime ( a , b ) ) ; } }

from math import gcd NEW_LINE def makePairs ( pairs , a , b ) : NEW_LINE INDENT for i in range ( a , b + 1 , 1 ) : NEW_LINE INDENT sumOfDigits = 0 NEW_LINE k = i NEW_LINE while ( k > 0 ) : NEW_LINE INDENT sumOfDigits += k % 10 NEW_LINE k = int ( k / 10 ) NEW_LINE DEDENT if ( sumOfDigits <= 162 ) : NEW_LINE INDENT pairs . append ( sumOfDigits ) NEW_LINE DEDENT DEDENT DEDENT def countCoPrime ( a , b ) : NEW_LINE INDENT pairs = [ ] NEW_LINE makePairs ( pairs , a , b ) NEW_LINE count = 0 NEW_LINE for i in range ( 0 , len ( pairs ) , 1 ) : NEW_LINE INDENT for j in range ( i + 1 , len ( pairs ) , 1 ) : NEW_LINE INDENT if ( gcd ( pairs [ i ] , pairs [ j ] ) == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = 12 NEW_LINE b = 15 NEW_LINE print ( countCoPrime ( a , b ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static void print ( ArrayList < Integer > a , int n ) { for ( int i = 0 ; i <= n ; i ++ ) System . out . print ( a . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } static void sort ( ArrayList < Integer > a , int n ) { for ( int i = n ; i >= 0 ; i -- ) for ( int j = n ; j > n - i ; j -- ) if ( a . get ( j ) > a . get ( j - 1 ) ) { int tempswap = a . get ( j ) ; a . remove ( j ) ; a . add ( j , a . get ( j - 1 ) ) ; a . remove ( j - 1 ) ; a . add ( j - 1 , tempswap ) ; } print ( a , n ) ; } public static void main ( String [ ] args ) { int n = 6 ; ArrayList < Integer > a = new ArrayList < Integer > ( ) ; a . add ( 2 ) ; a . add ( 4 ) ; a . add ( 3 ) ; a . add ( 2 ) ; a . add ( 4 ) ; a . add ( 5 ) ; a . add ( 3 ) ; sort ( a , n ) ; } }

def print1 ( a , n ) : NEW_LINE INDENT for i in range ( 0 , n + 1 ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT def sort ( a , n ) : NEW_LINE INDENT for i in range ( n , 0 , - 1 ) : NEW_LINE INDENT for j in range ( n , n - i , - 1 ) : NEW_LINE INDENT if ( a [ j ] > a [ j - 1 ] ) : NEW_LINE INDENT a [ j ] , a [ j - 1 ] = a [ j - 1 ] , a [ j ] NEW_LINE DEDENT DEDENT DEDENT print1 ( a , n ) NEW_LINE DEDENT n = 7 NEW_LINE a = [ 2 , 4 , 3 , 2 , 4 , 5 , 3 ] NEW_LINE sort ( a , n - 1 ) NEW_LINE


public class GFG { public static boolean isPalindrome ( float num ) { String s = String . valueOf ( num ) ; int low = 0 ; int high = s . length ( ) - 1 ; while ( low < high ) { if ( s . charAt ( low ) != s . charAt ( high ) ) return false ; low ++ ; high -- ; } return true ; } public static void main ( String args [ ] ) { float n = 123.321f ; if ( isPalindrome ( n ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def isPalindrome ( num ) : NEW_LINE INDENT s = str ( num ) NEW_LINE low = 0 NEW_LINE high = len ( s ) - 1 NEW_LINE while ( low < high ) : NEW_LINE INDENT if ( s [ low ] != s [ high ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT low += 1 NEW_LINE high -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT n = 123.321 NEW_LINE if ( isPalindrome ( n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


class GFG { public static int solve ( int [ ] A , int n ) { int [ ] [ ] dp = new int [ 2000 ] [ 2000 ] ; int flag = 1 ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += A [ i ] ; for ( int i = - sum ; i <= sum ; i ++ ) { try { dp [ 0 ] [ i ] = Integer . MAX_VALUE ; } catch ( Exception e ) { } } dp [ 0 ] [ 0 ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j <= sum ; j ++ ) { try { dp [ flag ] [ j ] = Integer . MAX_VALUE ; if ( j - A [ i - 1 ] <= sum && j - A [ i - 1 ] >= - sum ) dp [ flag ] [ j ] = dp [ flag ^ 1 ] [ j - A [ i - 1 ] ] ; if ( j + A [ i - 1 ] <= sum && j + A [ i - 1 ] >= - sum && dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] != Integer . MAX_VALUE ) dp [ flag ] [ j ] = Math . min ( dp [ flag ] [ j ] , dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] + 1 ) ; } catch ( Exception e ) { } } flag = flag ^ 1 ; } for ( int i = 0 ; i <= sum ; i ++ ) { if ( dp [ flag ^ 1 ] [ i ] != Integer . MAX_VALUE ) return dp [ flag ^ 1 ] [ i ] ; } return n - 1 ; } public static void main ( String [ ] args ) { int [ ] arr = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 } ; int n = arr . length ; System . out . println ( solve ( arr , n ) ) ; } }

def solve ( A , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 2000 ) ] for i in range ( 2000 ) ] NEW_LINE flag = 1 NEW_LINE sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += A [ i ] NEW_LINE DEDENT for i in range ( - sum , sum + 1 ) : NEW_LINE INDENT dp [ 0 ] [ i ] = 10 ** 9 NEW_LINE DEDENT dp [ 0 ] [ 0 ] = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( - sum , sum + 1 ) : NEW_LINE INDENT dp [ flag ] [ j ] = 10 ** 9 NEW_LINE if ( j - A [ i - 1 ] <= sum and j - A [ i - 1 ] >= - sum ) : NEW_LINE INDENT dp [ flag ] [ j ] = dp [ flag ^ 1 ] [ j - A [ i - 1 ] ] NEW_LINE DEDENT if ( j + A [ i - 1 ] <= sum and j + A [ i - 1 ] >= - sum and dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] != 10 ** 9 ) : NEW_LINE INDENT dp [ flag ] [ j ] = min ( dp [ flag ] [ j ] , dp [ flag ^ 1 ] [ j + A [ i - 1 ] ] + 1 ) NEW_LINE DEDENT DEDENT flag = flag ^ 1 NEW_LINE DEDENT for i in range ( sum + 1 ) : NEW_LINE INDENT if ( dp [ flag ^ 1 ] [ i ] != 10 ** 9 ) : NEW_LINE INDENT return dp [ flag ^ 1 ] [ i ] NEW_LINE DEDENT DEDENT return n - 1 NEW_LINE DEDENT arr = [ 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 ] NEW_LINE n = len ( arr ) NEW_LINE print ( solve ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static void recaman ( int n ) { int arr [ ] = new int [ n ] ; arr [ 0 ] = 0 ; System . out . print ( arr [ 0 ] + " ▁ , " ) ; for ( int i = 1 ; i < n ; i ++ ) { int curr = arr [ i - 1 ] - i ; int j ; for ( j = 0 ; j < i ; j ++ ) { if ( ( arr [ j ] == curr ) || curr < 0 ) { curr = arr [ i - 1 ] + i ; break ; } } arr [ i ] = curr ; System . out . print ( arr [ i ] + " , ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 17 ; recaman ( n ) ; } }

def recaman ( n ) : NEW_LINE INDENT arr = [ 0 ] * n NEW_LINE arr [ 0 ] = 0 NEW_LINE print ( arr [ 0 ] , end = " , ▁ " ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT curr = arr [ i - 1 ] - i NEW_LINE for j in range ( 0 , i ) : NEW_LINE INDENT if ( ( arr [ j ] == curr ) or curr < 0 ) : NEW_LINE INDENT curr = arr [ i - 1 ] + i NEW_LINE break NEW_LINE DEDENT DEDENT arr [ i ] = curr NEW_LINE print ( arr [ i ] , end = " , ▁ " ) NEW_LINE DEDENT DEDENT n = 17 NEW_LINE recaman ( n ) NEW_LINE


class GFG { static void find_composite_nos ( int n ) { System . out . println ( 9 * n + " ▁ " + 8 * n ) ; } public static void main ( String [ ] args ) { int n = 4 ; find_composite_nos ( n ) ; } }

def find_composite_nos ( n ) : NEW_LINE INDENT print ( 9 * n , 8 * n ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 ; NEW_LINE find_composite_nos ( n ) ; NEW_LINE DEDENT


public class GFG { static char encodedChar ( String str , int k ) { String expand = " " ; String temp = " " ; int freq = 0 ; for ( int i = 0 ; i < str . length ( ) ; ) { temp = " " ; freq = 0 ; while ( i < str . length ( ) && str . charAt ( i ) >= ' a ' && str . charAt ( i ) <= ' z ' ) { temp += str . charAt ( i ) ; i ++ ; } while ( i < str . length ( ) && str . charAt ( i ) >= '1' && str . charAt ( i ) <= '9' ) { freq = freq * 10 + str . charAt ( i ) - '0' ; i ++ ; } for ( int j = 1 ; j <= freq ; j ++ ) expand += temp ; } if ( freq == 0 ) expand += temp ; return expand . charAt ( k - 1 ) ; } public static void main ( String args [ ] ) { String str = " ab4c12ed3" ; int k = 21 ; System . out . println ( encodedChar ( str , k ) ) ; } }

def encodedChar ( str , k ) : NEW_LINE INDENT expand = " " NEW_LINE freq = 0 NEW_LINE i = 0 NEW_LINE while ( i < len ( str ) ) : NEW_LINE INDENT temp = " " NEW_LINE freq = 0 NEW_LINE while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( ' a ' ) and ord ( str [ i ] ) <= ord ( ' z ' ) ) : NEW_LINE INDENT temp += str [ i ] NEW_LINE i += 1 NEW_LINE DEDENT while ( i < len ( str ) and ord ( str [ i ] ) >= ord ( '1' ) and ord ( str [ i ] ) <= ord ( '9' ) ) : NEW_LINE INDENT freq = freq * 10 + ord ( str [ i ] ) - ord ( '0' ) NEW_LINE i += 1 NEW_LINE DEDENT for j in range ( 1 , freq + 1 , 1 ) : NEW_LINE INDENT expand += temp NEW_LINE DEDENT DEDENT if ( freq == 0 ) : NEW_LINE INDENT expand += temp NEW_LINE DEDENT return expand [ k - 1 ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " ab4c12ed3" NEW_LINE k = 21 NEW_LINE print ( encodedChar ( str , k ) ) NEW_LINE DEDENT


class RepeatElement { void printRepeating ( int arr [ ] , int size ) { int count [ ] = new int [ size ] ; int i ; System . out . println ( " Repeated ▁ elements ▁ are ▁ : ▁ " ) ; for ( i = 0 ; i < size ; i ++ ) { if ( count [ arr [ i ] ] == 1 ) System . out . print ( arr [ i ] + " ▁ " ) ; else count [ arr [ i ] ] ++ ; } } public static void main ( String [ ] args ) { RepeatElement repeat = new RepeatElement ( ) ; int arr [ ] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . length ; repeat . printRepeating ( arr , arr_size ) ; } }

def printRepeating ( arr , size ) : NEW_LINE INDENT count = [ 0 ] * size NEW_LINE print ( " ▁ Repeating ▁ elements ▁ are ▁ " , end = " " ) NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT if ( count [ arr [ i ] ] == 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT count [ arr [ i ] ] = count [ arr [ i ] ] + 1 NEW_LINE DEDENT DEDENT DEDENT arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printRepeating ( arr , arr_size ) NEW_LINE


class GFG { static double solve ( int s ) { double area = ( 1.732 * Math . pow ( s , 2 ) ) / 8 ; return area ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . println ( solve ( n ) ) ; } }

def solve ( s ) : NEW_LINE INDENT area = ( 1.732 * s ** 2 ) / 8 NEW_LINE return area NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 14 NEW_LINE print ( solve ( n ) ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static class node { int data ; node left , right ; node ( int data ) { this . data = data ; this . left = this . right = null ; } } static int getLevel ( node root , int data ) { Queue < node > q = new LinkedList < > ( ) ; int level = 1 ; q . add ( root ) ; q . add ( null ) ; while ( ! q . isEmpty ( ) ) { node temp = q . poll ( ) ; if ( temp == null ) { if ( q . peek ( ) != null ) { q . add ( null ) ; } level += 1 ; } else { if ( temp . data == data ) { return level ; } if ( temp . left != null ) { q . add ( temp . left ) ; } if ( temp . right != null ) { q . add ( temp . right ) ; } } } return 0 ; } public static void main ( String [ ] args ) { node root = new node ( 20 ) ; root . left = new node ( 10 ) ; root . right = new node ( 30 ) ; root . left . left = new node ( 5 ) ; root . left . right = new node ( 15 ) ; root . left . right . left = new node ( 12 ) ; root . right . left = new node ( 25 ) ; root . right . right = new node ( 40 ) ; int level = getLevel ( root , 30 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 30 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 30 ▁ not ▁ found " ) ; level = getLevel ( root , 12 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 12 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 12 ▁ not ▁ found " ) ; level = getLevel ( root , 25 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 25 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 25 ▁ not ▁ found " ) ; level = getLevel ( root , 27 ) ; if ( level != 0 ) System . out . println ( " level ▁ of ▁ node ▁ 27 ▁ is ▁ " + level ) ; else System . out . println ( " node ▁ 27 ▁ not ▁ found " ) ; } }

_MIN = - 2147483648 NEW_LINE _MAX = 2147483648 NEW_LINE class getnode : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def getlevel ( root , data ) : NEW_LINE INDENT q = [ ] NEW_LINE level = 1 NEW_LINE q . append ( root ) NEW_LINE q . append ( None ) NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( temp == None ) : NEW_LINE INDENT if len ( q ) == 0 : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( q [ 0 ] != None ) : NEW_LINE INDENT q . append ( None ) NEW_LINE DEDENT level += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( temp . data == data ) : NEW_LINE INDENT return level NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = getnode ( 20 ) NEW_LINE root . left = getnode ( 10 ) NEW_LINE root . right = getnode ( 30 ) NEW_LINE root . left . left = getnode ( 5 ) NEW_LINE root . left . right = getnode ( 15 ) NEW_LINE root . left . right . left = getnode ( 12 ) NEW_LINE root . right . left = getnode ( 25 ) NEW_LINE root . right . right = getnode ( 40 ) NEW_LINE level = getlevel ( root , 30 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 30 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 30 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 12 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 12 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 12 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 25 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 25 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 25 ▁ not ▁ found " ) NEW_LINE DEDENT level = getlevel ( root , 27 ) NEW_LINE if level != 0 : NEW_LINE INDENT print ( " level ▁ of ▁ node ▁ 27 ▁ is " , level ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " node ▁ 27 ▁ not ▁ found " ) NEW_LINE DEDENT DEDENT


class RepeatElement { void printRepeating ( int arr [ ] , int size ) { int S = 0 ; int P = 1 ; int x , y ; int D ; int n = size - 2 , i ; for ( i = 0 ; i < size ; i ++ ) { S = S + arr [ i ] ; P = P * arr [ i ] ; } S = S - n * ( n + 1 ) / 2 ; P = P / fact ( n ) ; D = ( int ) Math . sqrt ( S * S - 4 * P ) ; x = ( D + S ) / 2 ; y = ( S - D ) / 2 ; System . out . println ( " The ▁ two ▁ repeating ▁ elements ▁ are ▁ : " ) ; System . out . print ( x + " ▁ " + y ) ; } int fact ( int n ) { return ( n == 0 ) ? 1 : n * fact ( n - 1 ) ; } public static void main ( String [ ] args ) { RepeatElement repeat = new RepeatElement ( ) ; int arr [ ] = { 4 , 2 , 4 , 5 , 2 , 3 , 1 } ; int arr_size = arr . length ; repeat . printRepeating ( arr , arr_size ) ; } }

import math NEW_LINE def printRepeating ( arr , size ) : NEW_LINE INDENT S = 0 ; NEW_LINE P = 1 ; NEW_LINE n = size - 2 NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT S = S + arr [ i ] NEW_LINE P = P * arr [ i ] NEW_LINE DEDENT S = S - n * ( n + 1 ) // 2 NEW_LINE P = P // fact ( n ) NEW_LINE D = math . sqrt ( S * S - 4 * P ) NEW_LINE x = ( D + S ) // 2 NEW_LINE y = ( S - D ) // 2 NEW_LINE print ( " The ▁ two ▁ Repeating ▁ elements ▁ are ▁ " , ( int ) ( x ) , " ▁ & ▁ " , ( int ) ( y ) ) NEW_LINE DEDENT def fact ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return ( n * fact ( n - 1 ) ) NEW_LINE DEDENT DEDENT arr = [ 4 , 2 , 4 , 5 , 2 , 3 , 1 ] NEW_LINE arr_size = len ( arr ) NEW_LINE printRepeating ( arr , arr_size ) NEW_LINE


class GFG { static int minimumAdjacentDifference ( int a [ ] , int n , int k ) { int minDiff = Integer . MAX_VALUE ; for ( int i = 0 ; i <= k ; i ++ ) { int maxDiff = Integer . MIN_VALUE ; for ( int j = 0 ; j < n - k - 1 ; j ++ ) { for ( int p = i ; p <= i + j ; p ++ ) { maxDiff = Math . max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; } } minDiff = Math . min ( minDiff , maxDiff ) ; } return minDiff ; } public static void main ( String [ ] args ) { int n = 5 ; int k = 2 ; int [ ] a = { 3 , 7 , 8 , 10 , 14 } ; System . out . println ( minimumAdjacentDifference ( a , n , k ) ) ; } }

import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE INT_MIN = - ( sys . maxsize - 1 ) ; NEW_LINE def minimumAdjacentDifference ( a , n , k ) : NEW_LINE INDENT minDiff = INT_MAX ; NEW_LINE for i in range ( k + 1 ) : NEW_LINE INDENT maxDiff = INT_MIN ; NEW_LINE for j in range ( n - k - 1 ) : NEW_LINE INDENT for p in range ( i , i + j + 1 ) : NEW_LINE INDENT maxDiff = max ( maxDiff , a [ p + 1 ] - a [ p ] ) ; NEW_LINE DEDENT DEDENT minDiff = min ( minDiff , maxDiff ) ; NEW_LINE DEDENT return minDiff ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE k = 2 ; NEW_LINE a = [ 3 , 7 , 8 , 10 , 14 ] ; NEW_LINE print ( minimumAdjacentDifference ( a , n , k ) ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static int setBitNumber ( int n ) { if ( n == 0 ) return 0 ; int msb = 0 ; while ( n != 0 ) { n = n / 2 ; msb ++ ; } return ( 1 << msb ) ; } public static void main ( String [ ] args ) { int n = 0 ; System . out . println ( setBitNumber ( n ) ) ; } }

def setBitNumber ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT msb = 0 ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT n = int ( n / 2 ) ; NEW_LINE msb += 1 ; NEW_LINE DEDENT return ( 1 << msb ) ; NEW_LINE DEDENT n = 0 ; NEW_LINE print ( setBitNumber ( n ) ) ; NEW_LINE


class GFG { static int countSetBits ( int n ) { n ++ ; int powerOf2 = 2 ; int cnt = n / 2 ; while ( powerOf2 <= n ) { int totalPairs = n / powerOf2 ; cnt += ( totalPairs / 2 ) * powerOf2 ; cnt += ( totalPairs % 2 == 1 ) ? ( n % powerOf2 ) : 0 ; powerOf2 <<= 1 ; } return cnt ; } public static void main ( String [ ] args ) { int n = 14 ; System . out . println ( countSetBits ( n ) ) ; } }

def countSetBits ( n ) : NEW_LINE INDENT n += 1 ; NEW_LINE powerOf2 = 2 ; NEW_LINE cnt = n // 2 ; NEW_LINE while ( powerOf2 <= n ) : NEW_LINE INDENT totalPairs = n // powerOf2 ; NEW_LINE cnt += ( totalPairs // 2 ) * powerOf2 ; NEW_LINE if ( totalPairs & 1 ) : NEW_LINE INDENT cnt += ( n % powerOf2 ) NEW_LINE DEDENT else : NEW_LINE INDENT cnt += 0 NEW_LINE DEDENT powerOf2 <<= 1 ; NEW_LINE DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 14 ; NEW_LINE print ( countSetBits ( n ) ) ; NEW_LINE DEDENT


import java . lang . * ; import java . lang . Math ; import java . io . * ; class GFG { static double getValue ( double n ) { double result = ( Math . floor ( Math . log ( n + 1 ) / Math . log ( 2 ) ) ) ; return Math . pow ( 2 , result ) ; } public static void main ( String [ ] args ) { double n = 9 ; System . out . println ( getValue ( n ) ) ; n = 1025 ; System . out . println ( getValue ( n ) ) ; } }

import math NEW_LINE def getValue ( n ) : NEW_LINE INDENT result = int ( math . floor ( math . log ( n + 1 ) / math . log ( 2 ) ) ) NEW_LINE return int ( math . pow ( 2 , result ) ) NEW_LINE DEDENT n = 9 NEW_LINE print ( getValue ( n ) ) NEW_LINE n = 1025 NEW_LINE print ( getValue ( n ) ) NEW_LINE


import java . io . * ; class GFG { static void printLargestDivisible ( int n , int a [ ] ) { int i , c0 = 0 , c5 = 0 ; for ( i = 0 ; i < n ; i ++ ) { if ( a [ i ] == 0 ) c0 ++ ; else c5 ++ ; } c5 = ( int ) Math . floor ( c5 / 9 ) * 9 ; if ( c0 == 0 ) System . out . print ( - 1 ) ; else if ( c5 == 0 ) System . out . println ( 0 ) ; else { for ( i = 0 ; i < c5 ; i ++ ) System . out . print ( 5 ) ; for ( i = 0 ; i < c0 ; i ++ ) System . out . print ( 0 ) ; } } public static void main ( String [ ] args ) { int a [ ] = { 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 0 , 5 , 5 } ; int n = a . length ; printLargestDivisible ( n , a ) ; } }

from math import * NEW_LINE def printLargestDivisible ( n , a ) : NEW_LINE INDENT c0 , c5 = 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] == 0 : NEW_LINE INDENT c0 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT c5 += 1 NEW_LINE DEDENT DEDENT c5 = floor ( c5 / 9 ) * 9 NEW_LINE if c0 == 0 : NEW_LINE INDENT print ( - 1 , end = " " ) NEW_LINE DEDENT elif c5 == 0 : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT for i in range ( c5 ) : NEW_LINE INDENT print ( 5 , end = " " ) NEW_LINE DEDENT for i in range ( c0 ) : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 5 , 5 , 5 , 5 , 5 , 5 , 5 , 5 , 0 , 5 , 5 ] NEW_LINE n = len ( a ) NEW_LINE printLargestDivisible ( n , a ) NEW_LINE DEDENT


import java . util . * ; class GFG { static void sortWithRemainderP ( int arr [ ] , int n , int k , int p ) { Vector < Integer > v1 = new Vector < Integer > ( ) ; Vector < Integer > v2 = new Vector < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] % k == p ) { v1 . add ( i ) ; v2 . add ( arr [ i ] ) ; } } Collections . sort ( v2 ) ; for ( int i = 0 ; i < v1 . size ( ) ; i ++ ) arr [ v1 . get ( i ) ] = v2 . get ( i ) ; for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 255 , 16 , 2 , 4 , 0 } ; int n = arr . length ; int k = 2 ; int p = 0 ; sortWithRemainderP ( arr , n , k , p ) ; } }

def sortWithRemainderP ( arr , n , k , p ) : NEW_LINE INDENT v1 = [ ] NEW_LINE v2 = [ ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] % k == p ) : NEW_LINE INDENT v1 . append ( i ) NEW_LINE v2 . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT v2 . sort ( reverse = False ) NEW_LINE for i in range ( 0 , len ( v1 ) , 1 ) : NEW_LINE INDENT arr [ v1 [ i ] ] = v2 [ i ] NEW_LINE DEDENT for i in range ( 0 , n , 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 8 , 255 , 16 , 2 , 4 , 0 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE p = 0 NEW_LINE sortWithRemainderP ( arr , n , k , p ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void checkCollision ( int a , int b , int c , int x , int y , int radius ) { double dist = ( Math . abs ( a * x + b * y + c ) ) / Math . sqrt ( a * a + b * b ) ; if ( radius == dist ) System . out . println ( " Touch " ) ; else if ( radius > dist ) System . out . println ( " Intersect " ) ; else System . out . println ( " Outside " ) ; } public static void main ( String [ ] args ) { int radius = 5 ; int x = 0 , y = 0 ; int a = 3 , b = 4 , c = 25 ; checkCollision ( a , b , c , x , y , radius ) ; } }

import math NEW_LINE def checkCollision ( a , b , c , x , y , radius ) : NEW_LINE INDENT dist = ( ( abs ( a * x + b * y + c ) ) / math . sqrt ( a * a + b * b ) ) NEW_LINE if ( radius == dist ) : NEW_LINE INDENT print ( " Touch " ) NEW_LINE DEDENT elif ( radius > dist ) : NEW_LINE INDENT print ( " Intersect " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Outside " ) NEW_LINE DEDENT DEDENT radius = 5 NEW_LINE x = 0 NEW_LINE y = 0 NEW_LINE a = 3 NEW_LINE b = 4 NEW_LINE c = 25 NEW_LINE checkCollision ( a , b , c , x , y , radius ) NEW_LINE


import java . util . * ; class GFG { static int countSubArrayProductLessThanK ( ArrayList < Integer > a , long k ) { int n = a . size ( ) ; long p = 1 ; int res = 0 ; for ( int start = 0 , end = 0 ; end < n ; end ++ ) { p *= a . get ( end ) ; while ( start < end && p >= k ) p /= a . get ( start ++ ) ; if ( p < k ) { int len = end - start + 1 ; res += len ; } } return res ; } public static void main ( String [ ] args ) { ArrayList < Integer > al = new ArrayList < Integer > ( ) ; al . add ( 1 ) ; al . add ( 2 ) ; al . add ( 3 ) ; al . add ( 4 ) ; System . out . println ( countSubArrayProductLessThanK ( al , 10 ) ) ; ArrayList < Integer > al1 = new ArrayList < Integer > ( ) ; al1 . add ( 1 ) ; al1 . add ( 9 ) ; al1 . add ( 2 ) ; al1 . add ( 8 ) ; al1 . add ( 6 ) ; al1 . add ( 4 ) ; al1 . add ( 3 ) ; System . out . println ( countSubArrayProductLessThanK ( al1 , 100 ) ) ; ArrayList < Integer > al2 = new ArrayList < Integer > ( ) ; al2 . add ( 5 ) ; al2 . add ( 3 ) ; al2 . add ( 2 ) ; System . out . println ( countSubArrayProductLessThanK ( al2 , 16 ) ) ; ArrayList < Integer > al3 = new ArrayList < Integer > ( ) ; al3 . add ( 100 ) ; al3 . add ( 200 ) ; System . out . println ( countSubArrayProductLessThanK ( al3 , 100 ) ) ; ArrayList < Integer > al4 = new ArrayList < Integer > ( ) ; al4 . add ( 100 ) ; al4 . add ( 200 ) ; System . out . println ( countSubArrayProductLessThanK ( al3 , 101 ) ) ; } }

def countSubArrayProductLessThanK ( a , k ) : NEW_LINE INDENT n = len ( a ) NEW_LINE p = 1 NEW_LINE res = 0 NEW_LINE start = 0 NEW_LINE end = 0 NEW_LINE while ( end < n ) : NEW_LINE INDENT p *= a [ end ] NEW_LINE while ( start < end and p >= k ) : NEW_LINE INDENT p = int ( p // a [ start ] ) NEW_LINE start += 1 NEW_LINE DEDENT if ( p < k ) : NEW_LINE INDENT l = end - start + 1 NEW_LINE res += l NEW_LINE DEDENT end += 1 NEW_LINE DEDENT return res NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( countSubArrayProductLessThanK ( [ 1 , 2 , 3 , 4 ] , 10 ) ) NEW_LINE print ( countSubArrayProductLessThanK ( [ 1 , 9 , 2 , 8 , 6 , 4 , 3 ] , 100 ) ) NEW_LINE print ( countSubArrayProductLessThanK ( [ 5 , 3 , 2 ] , 16 ) ) NEW_LINE print ( countSubArrayProductLessThanK ( [ 100 , 200 ] , 100 ) ) NEW_LINE print ( countSubArrayProductLessThanK ( [ 100 , 200 ] , 101 ) ) NEW_LINE DEDENT


import java . lang . * ; import java . util . * ; class GFG { static void countDigit ( int n ) { int temp = n , sum = 0 , product = 1 ; while ( temp != 0 ) { int d = temp % 10 ; temp /= 10 ; if ( d > 0 && n % d == 0 ) { sum += d ; product *= d ; } } System . out . print ( " Sum ▁ = ▁ " + sum ) ; System . out . print ( " \n Product ▁ = ▁ " + product ) ; } public static void main ( String args [ ] ) { int n = 1012 ; countDigit ( n ) ; } }

def countDigit ( n ) : NEW_LINE INDENT temp = n NEW_LINE sum = 0 NEW_LINE product = 1 NEW_LINE while ( temp != 0 ) : NEW_LINE INDENT d = temp % 10 NEW_LINE temp //= 10 NEW_LINE if ( d > 0 and n % d == 0 ) : NEW_LINE INDENT sum += d NEW_LINE product *= d NEW_LINE DEDENT DEDENT print ( " Sum ▁ = " , sum ) NEW_LINE print ( " Product ▁ = " , product ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 1012 NEW_LINE countDigit ( n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static final int INT_BITS = 32 ; static int maxSubarrayXOR ( int set [ ] , int n ) { int index = 0 ; for ( int i = INT_BITS - 1 ; i >= 0 ; i -- ) { int maxInd = index ; int maxEle = Integer . MIN_VALUE ; for ( int j = index ; j < n ; j ++ ) { if ( ( set [ j ] & ( 1 << i ) ) != 0 && set [ j ] > maxEle ) { maxEle = set [ j ] ; maxInd = j ; } } if ( maxEle == - 2147483648 ) continue ; int temp = set [ index ] ; set [ index ] = set [ maxInd ] ; set [ maxInd ] = temp ; maxInd = index ; for ( int j = 0 ; j < n ; j ++ ) { if ( j != maxInd && ( set [ j ] & ( 1 << i ) ) != 0 ) set [ j ] = set [ j ] ^ set [ maxInd ] ; } index ++ ; } int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) res ^= set [ i ] ; return res ; } public static void main ( String arg [ ] ) { int set [ ] = { 9 , 8 , 5 } ; int n = set . length ; System . out . print ( " Max ▁ subset ▁ XOR ▁ is ▁ " ) ; System . out . print ( maxSubarrayXOR ( set , n ) ) ; } }

INT_BITS = 32 NEW_LINE def maxSubarrayXOR ( set , n ) : NEW_LINE INDENT index = 0 NEW_LINE for i in range ( INT_BITS - 1 , - 1 , - 1 ) : NEW_LINE INDENT maxInd = index NEW_LINE maxEle = - 2147483648 NEW_LINE for j in range ( index , n ) : NEW_LINE INDENT if ( ( set [ j ] & ( 1 << i ) ) != 0 and set [ j ] > maxEle ) : NEW_LINE INDENT maxEle = set [ j ] NEW_LINE maxInd = j NEW_LINE DEDENT DEDENT if ( maxEle == - 2147483648 ) : NEW_LINE INDENT continue NEW_LINE DEDENT temp = set [ index ] NEW_LINE set [ index ] = set [ maxInd ] NEW_LINE set [ maxInd ] = temp NEW_LINE maxInd = index NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( j != maxInd and ( set [ j ] & ( 1 << i ) ) != 0 ) : NEW_LINE INDENT set [ j ] = set [ j ] ^ set [ maxInd ] NEW_LINE DEDENT DEDENT index = index + 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT res = res ^ set [ i ] NEW_LINE DEDENT return res NEW_LINE DEDENT set = [ 9 , 8 , 5 ] NEW_LINE n = len ( set ) NEW_LINE print ( " Max ▁ subset ▁ XOR ▁ is ▁ " , end = " " ) NEW_LINE print ( maxSubarrayXOR ( set , n ) ) NEW_LINE


import java . util . * ; class Geeks { static float maxVol ( float P , float A ) { float l = ( float ) ( P - Math . sqrt ( P * P - 24 * A ) ) / 12 ; float V = ( float ) ( l * ( A / 2.0 - l * ( P / 4.0 - l ) ) ) ; return V ; } public static void main ( String args [ ] ) { float P = 20 , A = 16 ; System . out . println ( maxVol ( P , A ) ) ; } }

from math import sqrt NEW_LINE def maxVol ( P , A ) : NEW_LINE INDENT l = ( P - sqrt ( P * P - 24 * A ) ) / 12 NEW_LINE V = l * ( A / 2.0 - l * ( P / 4.0 - l ) ) NEW_LINE return V NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT P = 20 NEW_LINE A = 16 NEW_LINE print ( maxVol ( P , A ) ) NEW_LINE DEDENT


class GFG { static void SubarraysWithSumS ( int n , int k , int s ) { for ( int i = 0 ; i < k ; i ++ ) System . out . print ( s + " ▁ " ) ; for ( int i = k ; i < n ; i ++ ) System . out . print ( s + 1 + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 4 , k = 2 , s = 3 ; SubarraysWithSumS ( n , k , s ) ; } }

def SubarraysWithSumS ( n , k , s ) : NEW_LINE INDENT for i in range ( k ) : NEW_LINE INDENT print ( s , end = " ▁ " ) NEW_LINE DEDENT for i in range ( k , n ) : NEW_LINE INDENT print ( s + 1 , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 4 NEW_LINE k = 2 NEW_LINE s = 3 NEW_LINE SubarraysWithSumS ( n , k , s ) NEW_LINE


import java . util . * ; import java . lang . * ; class GFG { static int sum_of_series ( int n ) { int result = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { if ( i % 2 == 0 ) result = result - ( int ) Math . pow ( i , 2 ) ; else result = result + ( int ) Math . pow ( i , 2 ) ; } return result ; } public static void main ( String args [ ] ) { int n = 3 ; System . out . println ( sum_of_series ( n ) ) ; n = 10 ; System . out . println ( sum_of_series ( n ) ) ; } }

def sum_of_series ( n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT result = result - pow ( i , 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT result = result + pow ( i , 2 ) NEW_LINE DEDENT DEDENT return result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE n = 10 NEW_LINE print ( sum_of_series ( n ) ) NEW_LINE DEDENT


import java . util . * ; public class ACE { static int gcd ( int a , int b ) { return b == 0 ? a : gcd ( b , a % b ) ; } static int find_LCM ( int x , int y ) { return ( x * y ) / gcd ( x , y ) ; } static int CountPairs ( int n , int m , int A , int B ) { int cnt = 0 ; int lcm = find_LCM ( A , B ) ; for ( int i = 1 ; i <= n ; i ++ ) cnt += ( m + ( i % lcm ) ) / lcm ; return cnt ; } public static void main ( String [ ] args ) { int n = 60 , m = 90 , A = 5 , B = 10 ; System . out . println ( CountPairs ( n , m , A , B ) ) ; } }

from math import gcd NEW_LINE def find_LCM ( x , y ) : NEW_LINE INDENT return ( x * y ) // gcd ( x , y ) NEW_LINE DEDENT def CountPairs ( n , m , A , B ) : NEW_LINE INDENT cnt = 0 NEW_LINE lcm = find_LCM ( A , B ) NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT cnt += ( m + ( i % lcm ) ) // lcm NEW_LINE DEDENT return cnt NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n , m , A , B = 60 , 90 , 5 , 10 NEW_LINE print ( CountPairs ( n , m , A , B ) ) NEW_LINE DEDENT


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 ; } public static void main ( String [ ] args ) { int num = 4562 ; System . out . println ( " Reverse ▁ of ▁ no . ▁ is ▁ " + reversDigits ( num ) ) ; } }

n = 4562 ; NEW_LINE rev = 0 NEW_LINE while ( n > 0 ) : NEW_LINE INDENT a = n % 10 NEW_LINE rev = rev * 10 + a NEW_LINE n = n / 10 NEW_LINE DEDENT print ( rev ) NEW_LINE


import java . util . * ; class GFG { static boolean [ ] Prime ; static void computePrime ( int N ) { Prime = new boolean [ N + 1 ] ; Arrays . fill ( Prime , true ) ; Prime [ 0 ] = Prime [ 1 ] = false ; for ( int i = 2 ; i * i <= N ; i ++ ) { if ( Prime [ i ] ) { for ( int j = i * i ; j < N ; j += i ) { Prime [ j ] = false ; } } } } static int countTwinPairs ( int arr [ ] , int n ) { int maxE = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; computePrime ( maxE ) ; int count = 0 ; int freq [ ] = new int [ maxE + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( Prime [ arr [ i ] ] ) { if ( arr [ i ] + 2 < freq . length && freq [ arr [ i ] + 2 ] > 0 && Prime [ arr [ i ] + 2 ] ) { count ++ ; } } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 4 , 3 , 5 , 7 } ; int n = arr . length ; System . out . print ( countTwinPairs ( arr , n ) ) ; } }

def computePrime ( N ) : NEW_LINE INDENT Prime = [ True ] * ( N + 1 ) NEW_LINE Prime [ 0 ] = False NEW_LINE Prime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i <= N : NEW_LINE INDENT if ( Prime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , N , i ) : NEW_LINE INDENT Prime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT return Prime NEW_LINE DEDENT def countTwinPairs ( arr , n ) : NEW_LINE INDENT maxE = max ( arr ) NEW_LINE Prime = computePrime ( maxE ) NEW_LINE count = 0 NEW_LINE freq = [ 0 ] * ( maxE + 2 ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 NEW_LINE DEDENT arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( Prime [ arr [ i ] ] ) : NEW_LINE INDENT if ( ( arr [ i ] + 2 ) <= ( maxE ) and freq [ arr [ i ] + 2 ] > 0 and Prime [ arr [ i ] + 2 ] ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 3 , 5 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countTwinPairs ( arr , n ) ) NEW_LINE DEDENT


class GFG { static int log_a_to_base_b ( int a , int b ) { return ( int ) ( Math . log ( a ) / Math . log ( b ) ) ; } public static void main ( String [ ] args ) { int a = 3 ; int b = 2 ; System . out . println ( log_a_to_base_b ( a , b ) ) ; a = 256 ; b = 4 ; System . out . println ( log_a_to_base_b ( a , b ) ) ; } }

from math import log NEW_LINE def log_a_to_base_b ( a , b ) : NEW_LINE INDENT return log ( a ) // log ( b ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 ; NEW_LINE b = 2 ; NEW_LINE print ( log_a_to_base_b ( a , b ) ) ; NEW_LINE a = 256 ; NEW_LINE b = 4 ; NEW_LINE print ( log_a_to_base_b ( a , b ) ) ; NEW_LINE DEDENT