class GFG { static void thirdLargest ( int arr [ ] , int arr_size ) { if ( arr_size < 3 ) { System . out . printf ( " ▁ Invalid ▁ Input ▁ " ) ; return ; } int first = arr [ 0 ] ; for ( int i = 1 ; i < arr_size ; i ++ ) if ( arr [ i ] > first ) first = arr [ i ] ; int second = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > second && arr [ i ] < first ) second = arr [ i ] ; int third = Integer . MIN_VALUE ; for ( int i = 0 ; i < arr_size ; i ++ ) if ( arr [ i ] > third && arr [ i ] < second ) third = arr [ i ] ; System . out . printf ( " The ▁ third ▁ Largest ▁ " + " element ▁ is ▁ % d \n " , third ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 12 , 13 , 1 , 10 , 34 , 16 } ; int n = arr . length ; thirdLargest ( arr , n ) ; } }

import sys NEW_LINE def thirdLargest ( arr , arr_size ) : NEW_LINE INDENT if ( arr_size < 3 ) : NEW_LINE INDENT print ( " ▁ Invalid ▁ Input ▁ " ) NEW_LINE return NEW_LINE DEDENT first = arr [ 0 ] NEW_LINE for i in range ( 1 , arr_size ) : NEW_LINE INDENT if ( arr [ i ] > first ) : NEW_LINE INDENT first = arr [ i ] NEW_LINE DEDENT DEDENT second = - sys . maxsize NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT if ( arr [ i ] > second and arr [ i ] < first ) : NEW_LINE INDENT second = arr [ i ] NEW_LINE DEDENT DEDENT third = - sys . maxsize NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT if ( arr [ i ] > third and arr [ i ] < second ) : NEW_LINE INDENT third = arr [ i ] NEW_LINE DEDENT DEDENT print ( " The ▁ Third ▁ Largest " , " element ▁ is " , third ) NEW_LINE DEDENT arr = [ 12 , 13 , 1 , 10 , 34 , 16 ] NEW_LINE n = len ( arr ) NEW_LINE thirdLargest ( arr , n ) NEW_LINE


import java . util . * ; class GFG { static int countOp ( int arr [ ] , int n , int k ) { int operations = 0 ; for ( int i = 1 ; i < n ; i ++ ) { if ( Math . abs ( arr [ i ] - arr [ i - 1 ] ) > k ) { int absDiff = Math . abs ( arr [ i ] - arr [ i - 1 ] ) ; int currOp = absDiff - k ; if ( arr [ i ] < arr [ i - 1 ] ) arr [ i ] += currOp ; else arr [ i ] -= currOp ; operations += currOp ; } } return operations ; } static public void main ( String [ ] arg ) { int arr [ ] = { 1 , 2 , 5 , 9 } ; int n = arr . length ; int k = 2 ; System . out . println ( countOp ( arr , n , k ) ) ; } }

def countOp ( arr , n , k ) : NEW_LINE INDENT operations = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( abs ( arr [ i ] - arr [ i - 1 ] ) > k ) : NEW_LINE INDENT absDiff = abs ( arr [ i ] - arr [ i - 1 ] ) ; NEW_LINE currOp = absDiff - k ; NEW_LINE if ( arr [ i ] < arr [ i - 1 ] ) : NEW_LINE INDENT arr [ i ] += currOp ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] -= currOp ; NEW_LINE DEDENT operations += currOp ; NEW_LINE DEDENT DEDENT return operations ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 5 , 9 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE k = 2 ; NEW_LINE print ( countOp ( arr , n , k ) ) ; NEW_LINE DEDENT


class GFG { static int countNumbers ( int l , int r ) { return ( ( r / 6 ) - ( l - 1 ) / 6 ) ; } public static void main ( String [ ] args ) { int l = 1000 , r = 6000 ; System . out . println ( countNumbers ( l , r ) ) ; } }

def countNumbers ( l , r ) : NEW_LINE INDENT return ( ( r // 6 ) - ( l - 1 ) // 6 ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 1000 ; r = 6000 ; NEW_LINE print ( countNumbers ( l , r ) ) ; NEW_LINE DEDENT


import java . io . * ; public class GFG { static boolean isPrime ( int a [ ] , int lower , int upper ) { int n = 0 ; for ( int i = lower - 1 ; i <= upper - 1 ; i ++ ) n += a [ i ] ; if ( n <= 1 ) return false ; for ( int i = 2 ; i < n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 , 5 , 5 , 4 , 7 , 8 , 9 } ; int lower = 3 , upper = 6 ; if ( isPrime ( a , lower , upper ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }

def isPrime ( a , lower , upper ) : NEW_LINE INDENT n = 0 NEW_LINE for i in range ( lower - 1 , upper ) : NEW_LINE INDENT n = n + a [ i ] NEW_LINE DEDENT if ( n <= 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , n ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT a = [ 1 , 2 , 3 , 5 , 5 , 4 , 7 , 8 , 9 ] NEW_LINE lower = 3 NEW_LINE upper = 6 NEW_LINE if ( isPrime ( a , lower , upper ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT


import java . io . * ; class GFG { static String search ( int arr [ ] , int n , int x ) { if ( arr [ n - 1 ] == x ) return " Found " ; int backup = arr [ n - 1 ] ; arr [ n - 1 ] = x ; for ( int i = 0 ; ; i ++ ) { if ( arr [ i ] == x ) { arr [ n - 1 ] = backup ; if ( i < n - 1 ) return " Found " ; return " Not ▁ Found " ; } } } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 1 , 5 , 8 } ; int n = arr . length ; int x = 1 ; System . out . println ( search ( arr , n , x ) ) ; } }

def search ( arr , n , x ) : NEW_LINE INDENT if ( arr [ n - 1 ] == x ) : NEW_LINE INDENT return " Found " NEW_LINE DEDENT backup = arr [ n - 1 ] NEW_LINE arr [ n - 1 ] = x NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] == x ) : NEW_LINE INDENT arr [ n - 1 ] = backup NEW_LINE if ( i < n - 1 ) : NEW_LINE INDENT return " Found " NEW_LINE DEDENT return " Not ▁ Found " NEW_LINE DEDENT i = i + 1 NEW_LINE DEDENT DEDENT arr = [ 4 , 6 , 1 , 5 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE x = 1 NEW_LINE print ( search ( arr , n , x ) ) NEW_LINE


class GFG { static int CountOfOddSum ( int a [ ] [ ] , int n ) { int [ ] [ ] dp = new int [ n ] [ 2 ] ; int [ ] [ ] cnt = new int [ n ] [ 2 ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < 2 ; j ++ ) { if ( a [ i ] [ j ] % 2 == 0 ) { cnt [ i ] [ 0 ] ++ ; } else { cnt [ i ] [ 1 ] ++ ; } } } dp [ 0 ] [ 0 ] = cnt [ 0 ] [ 0 ] ; dp [ 0 ] [ 1 ] = cnt [ 0 ] [ 1 ] ; for ( int i = 1 ; i < n ; i ++ ) { dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 0 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 1 ] ) ; dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 1 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 0 ] ) ; } return dp [ n - 1 ] [ 1 ] ; } public static void main ( String [ ] args ) { int a [ ] [ ] = { { 1 , 2 } , { 3 , 6 } } ; int n = a . length ; int ans = CountOfOddSum ( a , n ) ; System . out . println ( ans ) ; } }

def CountOfOddSum ( a , n ) : NEW_LINE INDENT dp = [ [ 0 for i in range ( 2 ) ] for i in range ( n ) ] NEW_LINE cnt = [ [ 0 for i in range ( 2 ) ] for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( 2 ) : NEW_LINE INDENT if ( a [ i ] [ j ] % 2 == 0 ) : NEW_LINE INDENT cnt [ i ] [ 0 ] += 1 NEW_LINE DEDENT else : NEW_LINE INDENT cnt [ i ] [ 1 ] += 1 NEW_LINE DEDENT DEDENT DEDENT dp [ 0 ] [ 0 ] = cnt [ 0 ] [ 0 ] NEW_LINE dp [ 0 ] [ 1 ] = cnt [ 0 ] [ 1 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT dp [ i ] [ 0 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 0 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 1 ] ) NEW_LINE dp [ i ] [ 1 ] = ( dp [ i - 1 ] [ 0 ] * cnt [ i ] [ 1 ] + dp [ i - 1 ] [ 1 ] * cnt [ i ] [ 0 ] ) NEW_LINE DEDENT return dp [ n - 1 ] [ 1 ] NEW_LINE DEDENT a = [ [ 1 , 2 ] , [ 3 , 6 ] ] NEW_LINE n = len ( a ) NEW_LINE ans = CountOfOddSum ( a , n ) NEW_LINE print ( ans ) NEW_LINE


class sol { static boolean isPossible ( int l , int m , int x , int y ) { if ( l * m == x + y ) return true ; return false ; } public static void main ( String args [ ] ) { int l = 3 , m = 2 , x = 4 , y = 2 ; if ( isPossible ( l , m , x , y ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }

def isPossible ( l , m , x , y ) : NEW_LINE INDENT if ( l * m == x + y ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 3 ; m = 2 ; x = 4 ; y = 2 ; NEW_LINE if ( isPossible ( l , m , x , y ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT


import java . io . * ; public class GFG { static int intersection ( int n ) { return n * ( n - 1 ) ; } public static void main ( String [ ] args ) throws IOException { System . out . println ( intersection ( 3 ) ) ; } }

def intersection ( n ) : NEW_LINE INDENT return n * ( n - 1 ) ; NEW_LINE DEDENT print ( intersection ( 3 ) ) NEW_LINE


public class GFG { static int Race ( int B , int C ) { int result = 0 ; result = ( ( C * 100 ) / B ) ; return 100 - result ; } public static void main ( String [ ] args ) { int B = 10 ; int C = 28 ; B = 100 - B ; C = 100 - C ; System . out . println ( Race ( B , C ) + " ▁ meters " ) ; } }

def Race ( B , C ) : NEW_LINE INDENT result = 0 ; NEW_LINE result = ( ( C * 100 ) // B ) NEW_LINE return 100 - result NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT B = 10 NEW_LINE C = 28 NEW_LINE B = 100 - B ; NEW_LINE C = 100 - C ; NEW_LINE print ( str ( Race ( B , C ) ) + " ▁ meters " ) NEW_LINE DEDENT


class Test { static int arr [ ] = new int [ ] { 3 , 7 , 90 , 20 , 10 , 50 , 40 } ; static void findMinAvgSubarray ( int n , int k ) { if ( n < k ) return ; int res_index = 0 ; int curr_sum = 0 ; for ( int i = 0 ; i < k ; i ++ ) curr_sum += arr [ i ] ; int min_sum = curr_sum ; for ( int i = k ; i < n ; i ++ ) { curr_sum += arr [ i ] - arr [ i - k ] ; if ( curr_sum < min_sum ) { min_sum = curr_sum ; res_index = ( i - k + 1 ) ; } } System . out . println ( " Subarray ▁ between ▁ [ " + res_index + " , ▁ " + ( res_index + k - 1 ) + " ] ▁ has ▁ minimum ▁ average " ) ; } public static void main ( String [ ] args ) { int k = 3 ; findMinAvgSubarray ( arr . length , k ) ; } }

def findMinAvgSubarray ( arr , n , k ) : NEW_LINE INDENT if ( n < k ) : return 0 NEW_LINE res_index = 0 NEW_LINE curr_sum = 0 NEW_LINE for i in range ( k ) : NEW_LINE INDENT curr_sum += arr [ i ] NEW_LINE DEDENT min_sum = curr_sum NEW_LINE for i in range ( k , n ) : NEW_LINE INDENT curr_sum += arr [ i ] - arr [ i - k ] NEW_LINE if ( curr_sum < min_sum ) : NEW_LINE INDENT min_sum = curr_sum NEW_LINE res_index = ( i - k + 1 ) NEW_LINE DEDENT DEDENT print ( " Subarray ▁ between ▁ [ " , res_index , " , ▁ " , ( res_index + k - 1 ) , " ] ▁ has ▁ minimum ▁ average " ) NEW_LINE DEDENT arr = [ 3 , 7 , 90 , 20 , 10 , 50 , 40 ] NEW_LINE k = 3 NEW_LINE n = len ( arr ) NEW_LINE findMinAvgSubarray ( arr , n , k ) NEW_LINE


class GFG { static String isEvenOrOdd ( int L , int R ) { int oddCount = ( R - L ) / 2 ; if ( R % 2 == 1 || L % 2 == 1 ) oddCount ++ ; if ( oddCount % 2 == 0 ) return " Even " ; else return " Odd " ; } public static void main ( String [ ] args ) { int L = 5 , R = 15 ; System . out . println ( isEvenOrOdd ( L , R ) ) ; } }

def isEvenOrOdd ( L , R ) : NEW_LINE INDENT oddCount = ( R - L ) / 2 NEW_LINE if ( R % 2 == 1 or L % 2 == 1 ) : NEW_LINE INDENT oddCount = oddCount + 1 NEW_LINE DEDENT if ( oddCount % 2 == 0 ) : NEW_LINE INDENT return " Even " NEW_LINE DEDENT else : NEW_LINE INDENT return " Odd " NEW_LINE DEDENT DEDENT L = 5 NEW_LINE R = 15 NEW_LINE print ( isEvenOrOdd ( L , R ) ) ; NEW_LINE


class GFG { static double nthRoot ( long A , long N ) { double xPre = 7 ; double eps = 1e-3 ; double delX = Integer . MAX_VALUE ; double xK = 0 ; while ( delX > eps ) { xK = ( ( N - 1.0 ) * xPre + ( double ) A / Math . pow ( xPre , N - 1 ) ) / ( double ) N ; delX = Math . abs ( xK - xPre ) ; xPre = xK ; } return xK ; } static boolean check ( long no , int k ) { double kth_root = nthRoot ( no , k ) ; long num = ( long ) kth_root ; if ( Math . abs ( num - kth_root ) < 1e-4 ) return true ; return false ; } static void printExpo ( long arr [ ] , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) { if ( check ( arr [ i ] , k ) ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int K = 6 ; long arr [ ] = { 46656 , 64 , 256 , 729 , 16 , 1000 } ; int n = arr . length ; printExpo ( arr , n , K ) ; } }

def nthRoot ( A , N ) : NEW_LINE INDENT xPre = 7 NEW_LINE eps = 1e-3 NEW_LINE delX = 10 ** 9 NEW_LINE xK = 0 NEW_LINE while ( delX > eps ) : NEW_LINE INDENT xK = ( ( N - 1.0 ) * xPre + A / pow ( xPre , N - 1 ) ) / N NEW_LINE delX = abs ( xK - xPre ) NEW_LINE xPre = xK NEW_LINE DEDENT return xK NEW_LINE DEDENT def check ( no , k ) : NEW_LINE INDENT kth_root = nthRoot ( no , k ) NEW_LINE num = int ( kth_root ) NEW_LINE if ( abs ( num - kth_root ) < 1e-4 ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def printExpo ( arr , n , k ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( check ( arr [ i ] , k ) ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT K = 6 NEW_LINE arr = [ 46656 , 64 , 256 , 729 , 16 , 1000 ] NEW_LINE n = len ( arr ) NEW_LINE printExpo ( arr , n , K ) NEW_LINE DEDENT


import java . util . * ; class GFG { static String integer_to_string ( int n , int base ) { String str = " " ; while ( n > 0 ) { int digit = n % base ; n /= base ; str += ( char ) ( digit + '0' ) ; } return str ; } static int isPalindrome ( int i , int k ) { int temp = i ; int m = 0 ; while ( temp > 0 ) { m = temp % 10 + m * 10 ; temp /= 10 ; } if ( m == i ) { String str = integer_to_string ( m , k ) ; StringBuilder sb = new StringBuilder ( str ) ; String str1 = sb . reverse ( ) . toString ( ) ; if ( str . equals ( str1 ) ) { return i ; } } return 0 ; } static void sumPalindrome ( int n , int k ) { int sum = 0 ; for ( int i = 1 ; i < n ; i ++ ) { sum += isPalindrome ( i , k ) ; } System . out . println ( " Total ▁ sum ▁ is ▁ " + sum ) ; } public static void main ( String [ ] args ) { int n = 100 ; int k = 2 ; sumPalindrome ( n , k ) ; } }

def integer_to_string ( n , base ) : NEW_LINE INDENT str = " " ; NEW_LINE while ( n > 0 ) : NEW_LINE INDENT digit = n % base ; NEW_LINE n = int ( n / base ) ; NEW_LINE str = chr ( digit + ord ( '0' ) ) + str ; NEW_LINE DEDENT return str ; NEW_LINE DEDENT def isPalindrome ( i , k ) : NEW_LINE INDENT temp = i ; NEW_LINE m = 0 ; NEW_LINE while ( temp > 0 ) : NEW_LINE INDENT m = ( temp % 10 ) + ( m * 10 ) ; NEW_LINE temp = int ( temp / 10 ) ; NEW_LINE DEDENT if ( m == i ) : NEW_LINE INDENT str = integer_to_string ( m , k ) ; NEW_LINE str1 = str ; NEW_LINE if ( str [ : : - 1 ] == str1 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return 0 ; NEW_LINE DEDENT def sumPalindrome ( n , k ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += isPalindrome ( i , k ) ; NEW_LINE DEDENT print ( " Total ▁ sum ▁ is " , sum ) ; NEW_LINE DEDENT n = 100 ; NEW_LINE k = 2 ; NEW_LINE sumPalindrome ( n , k ) ; NEW_LINE


class GFG { static void printSeriesSum ( int N ) { double sum = 0 ; int a = 1 ; int cnt = 0 ; boolean flag = true ; sum += a ; while ( cnt < N ) { int nextElement ; if ( flag == true ) { nextElement = a * 2 ; sum += nextElement ; flag = ! flag ; } else { nextElement = a * 3 / 2 ; sum += nextElement ; flag = ! flag ; } a = nextElement ; cnt ++ ; } System . out . println ( sum ) ; } public static void main ( String [ ] args ) { int N = 8 ; printSeriesSum ( N ) ; } }

def printSeriesSum ( N ) : NEW_LINE INDENT sum = 0 ; NEW_LINE a = 1 ; NEW_LINE cnt = 0 ; NEW_LINE flag = True ; NEW_LINE sum += a ; NEW_LINE while ( cnt < N ) : NEW_LINE INDENT nextElement = None ; NEW_LINE if ( flag ) : NEW_LINE INDENT nextElement = a * 2 ; NEW_LINE sum += nextElement ; NEW_LINE flag = not flag ; NEW_LINE DEDENT else : NEW_LINE INDENT nextElement = a * ( 3 / 2 ) ; NEW_LINE sum += nextElement ; NEW_LINE flag = not flag ; NEW_LINE DEDENT a = nextElement ; NEW_LINE cnt += 1 NEW_LINE DEDENT print ( sum ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 8 ; NEW_LINE printSeriesSum ( N ) ; NEW_LINE DEDENT


public class ReplaceDigits { static int replace0with5 ( int number ) { return number += calculateAddedValue ( number ) ; } private static int calculateAddedValue ( int number ) { int result = 0 ; int decimalPlace = 1 ; if ( number == 0 ) { result += ( 5 * decimalPlace ) ; } while ( number > 0 ) { if ( number % 10 == 0 ) result += ( 5 * decimalPlace ) ; number /= 10 ; decimalPlace *= 10 ; } return result ; } public static void main ( String [ ] args ) { System . out . print ( replace0with5 ( 1020 ) ) ; } }

def replace0with5 ( number ) : NEW_LINE INDENT number += calculateAddedValue ( number ) NEW_LINE return number NEW_LINE DEDENT def calculateAddedValue ( number ) : NEW_LINE INDENT result = 0 NEW_LINE decimalPlace = 1 NEW_LINE if ( number == 0 ) : NEW_LINE INDENT result += ( 5 * decimalPlace ) NEW_LINE DEDENT while ( number > 0 ) : NEW_LINE INDENT if ( number % 10 == 0 ) : NEW_LINE INDENT result += ( 5 * decimalPlace ) NEW_LINE DEDENT number //= 10 NEW_LINE decimalPlace *= 10 NEW_LINE DEDENT return result NEW_LINE DEDENT print ( replace0with5 ( 1020 ) ) NEW_LINE


import java . util . * ; class GFG { static class Node { int elementID ; int stepsToeliminate ; public Node ( int elementID , int stepsToeliminate ) { super ( ) ; this . elementID = elementID ; this . stepsToeliminate = stepsToeliminate ; } } ; static void minSteps ( int arr [ ] , int N ) { Stack < Node > s = new Stack < Node > ( ) ; s . add ( new Node ( 0 , - 1 ) ) ; int maxStepsToeliminate = - 1 ; for ( int i = 1 ; i < N ; i ++ ) { int stepsToeliminate = 1 ; while ( ! s . isEmpty ( ) ) { if ( arr [ s . peek ( ) . elementID ] >= arr [ i ] ) { stepsToeliminate = Math . max ( stepsToeliminate , s . peek ( ) . stepsToeliminate + 1 ) ; s . pop ( ) ; } else { break ; } } if ( s . isEmpty ( ) ) { stepsToeliminate = - 1 ; } maxStepsToeliminate = Math . max ( maxStepsToeliminate , stepsToeliminate ) ; s . add ( new Node ( i , stepsToeliminate ) ) ; } System . out . print ( ( maxStepsToeliminate < 0 ? 0 : maxStepsToeliminate ) + " \n " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 1 , 7 , 5 } ; int size = arr . length ; minSteps ( arr , size ) ; } }

def minSteps ( arr , N ) : NEW_LINE INDENT s = [ ] ; NEW_LINE s . append ( ( 0 , - 1 ) ) ; NEW_LINE maxStepsToeliminate = - 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT stepsToeliminate = 1 ; NEW_LINE while ( len ( s ) != 0 ) : NEW_LINE INDENT if ( arr [ s [ - 1 ] [ 0 ] ] >= arr [ i ] ) : NEW_LINE INDENT stepsToeliminate = max ( stepsToeliminate , s [ - 1 ] [ 1 ] + 1 ) ; NEW_LINE s . pop ( ) ; NEW_LINE DEDENT else : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT if ( len ( s ) == 0 ) : NEW_LINE INDENT stepsToeliminate = - 1 ; NEW_LINE DEDENT maxStepsToeliminate = max ( maxStepsToeliminate , stepsToeliminate ) ; NEW_LINE s . append ( ( i , stepsToeliminate ) ) ; NEW_LINE DEDENT print ( 0 if ( maxStepsToeliminate < 0 ) else maxStepsToeliminate ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 7 , 5 ] ; NEW_LINE size = len ( arr ) ; NEW_LINE minSteps ( arr , size ) ; NEW_LINE DEDENT


class GFG { static final double EPSILON = 0.001 ; static double func ( double x ) { return x * x * x - x * x + 2 ; } static double derivFunc ( double x ) { return 3 * x * x - 2 * x ; } static void newtonRaphson ( double x ) { double h = func ( x ) / derivFunc ( x ) ; while ( Math . abs ( h ) >= EPSILON ) { h = func ( x ) / derivFunc ( x ) ; x = x - h ; } System . out . print ( " The ▁ value ▁ of ▁ the " + " ▁ root ▁ is ▁ : ▁ " + Math . round ( x * 100.0 ) / 100.0 ) ; } public static void main ( String [ ] args ) { double x0 = - 20 ; newtonRaphson ( x0 ) ; } }

def func ( x ) : NEW_LINE INDENT return x * x * x - x * x + 2 NEW_LINE DEDENT def derivFunc ( x ) : NEW_LINE INDENT return 3 * x * x - 2 * x NEW_LINE DEDENT def newtonRaphson ( x ) : NEW_LINE INDENT h = func ( x ) / derivFunc ( x ) NEW_LINE while abs ( h ) >= 0.0001 : NEW_LINE INDENT h = func ( x ) / derivFunc ( x ) NEW_LINE x = x - h NEW_LINE DEDENT print ( " The ▁ value ▁ of ▁ the ▁ root ▁ is ▁ : ▁ " , " % .4f " % x ) NEW_LINE DEDENT x0 = - 20 NEW_LINE newtonRaphson ( x0 ) NEW_LINE


import java . util . * ; class GFG { static int minimalSteps ( String s , int n ) { int [ ] dp = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dp [ i ] = Integer . MAX_VALUE ; String s1 = " " , s2 = " " ; dp [ 0 ] = 1 ; s1 += s . charAt ( 0 ) ; for ( int i = 1 ; i < n ; i ++ ) { s1 += s . charAt ( i ) ; s2 = s . substring ( i + 1 , i + 1 ) ; dp [ i ] = Math . min ( dp [ i ] , dp [ i - 1 ] + 1 ) ; if ( s1 == s2 ) dp [ i * 2 + 1 ] = Math . min ( dp [ i ] + 1 , dp [ i * 2 + 1 ] ) ; } return dp [ n - 1 ] ; } public static void main ( String args [ ] ) { String s = " aaaaaaaa " ; int n = s . length ( ) ; System . out . println ( minimalSteps ( s , n ) / 2 ) ; } }

INT_MAX = 100000000 NEW_LINE def minimalSteps ( s , n ) : NEW_LINE INDENT dp = [ INT_MAX for i in range ( n ) ] NEW_LINE s1 = " " NEW_LINE s2 = " " NEW_LINE dp [ 0 ] = 1 NEW_LINE s1 += s [ 0 ] NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT s1 += s [ i ] NEW_LINE s2 = s [ i + 1 : i + 1 + i + 1 ] NEW_LINE dp [ i ] = min ( dp [ i ] , dp [ i - 1 ] + 1 ) NEW_LINE if ( s1 == s2 ) : NEW_LINE INDENT dp [ i * 2 + 1 ] = min ( dp [ i ] + 1 , dp [ i * 2 + 1 ] ) NEW_LINE DEDENT DEDENT return dp [ n - 1 ] NEW_LINE DEDENT s = " aaaaaaaa " NEW_LINE n = len ( s ) NEW_LINE print ( minimalSteps ( s , n ) ) NEW_LINE


public class minFlips { static int findFlips ( String str , int n ) { char last = ' ▁ ' ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( last != str . charAt ( i ) ) res ++ ; last = str . charAt ( i ) ; } return res / 2 ; } public static void main ( String [ ] args ) { String str = "00011110001110" ; int n = str . length ( ) ; System . out . println ( findFlips ( str , n ) ) ; } }

def findFlips ( str , n ) : NEW_LINE INDENT last = ' ▁ ' NEW_LINE res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( last != str [ i ] ) : NEW_LINE INDENT res += 1 NEW_LINE DEDENT last = str [ i ] NEW_LINE DEDENT return res // 2 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "00011110001110" NEW_LINE n = len ( str ) NEW_LINE print ( findFlips ( str , n ) ) NEW_LINE DEDENT


class Node { int data ; Node left , right ; Node ( int item ) { data = item ; left = right = null ; } } class BinaryTree { Node root ; void printPaths ( Node node ) { int path [ ] = new int [ 1000 ] ; printPathsRecur ( node , path , 0 ) ; } void printPathsRecur ( Node node , int path [ ] , int pathLen ) { if ( node == null ) return ; path [ pathLen ] = node . data ; pathLen ++ ; if ( node . left == null && node . right == null ) printArray ( path , pathLen ) ; else { printPathsRecur ( node . left , path , pathLen ) ; printPathsRecur ( node . right , path , pathLen ) ; } } void printArray ( int ints [ ] , int len ) { int i ; for ( i = 0 ; i < len ; i ++ ) System . out . print ( ints [ i ] + " ▁ " ) ; System . out . println ( " " ) ; } public static void main ( String [ ] args ) { BinaryTree tree = new BinaryTree ( ) ; tree . root = new Node ( 1 ) ; tree . root . left = new Node ( 2 ) ; tree . root . right = new Node ( 3 ) ; tree . root . left . left = new Node ( 4 ) ; tree . root . left . right = new Node ( 5 ) ; tree . printPaths ( tree . root ) ; } }

class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . right = None NEW_LINE self . left = None NEW_LINE DEDENT DEDENT def printRoute ( stack , root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return NEW_LINE DEDENT stack . append ( root . data ) NEW_LINE if ( root . left == None and root . right == None ) : NEW_LINE INDENT print ( ' ▁ ' . join ( [ str ( i ) for i in stack ] ) ) NEW_LINE DEDENT printRoute ( stack , root . left ) NEW_LINE printRoute ( stack , root . right ) NEW_LINE stack . pop ( ) NEW_LINE DEDENT root = Node ( 1 ) ; NEW_LINE root . left = Node ( 2 ) ; NEW_LINE root . right = Node ( 3 ) ; NEW_LINE root . left . left = Node ( 4 ) ; NEW_LINE root . left . right = Node ( 5 ) ; NEW_LINE printRoute ( [ ] , root ) NEW_LINE


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

def countStr ( n ) : NEW_LINE INDENT return ( 1 + ( n * 2 ) + ( n * ( ( n * n ) - 1 ) // 2 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( countStr ( n ) ) NEW_LINE DEDENT


class GFG { static int a = 0 , b = 0 ; static int isPrime ( int x ) { int s = ( int ) Math . sqrt ( x ) ; for ( int i = 2 ; i <= s ; i ++ ) if ( x % i == 0 ) return 0 ; return 1 ; } static void Num ( int x ) { for ( int i = 2 ; i <= x / 2 ; i ++ ) { if ( isPrime ( i ) != 0 && isPrime ( x - i ) != 0 ) { a = i ; b = x - i ; return ; } } } static void generate ( int n ) { if ( n <= 7 ) System . out . println ( " Impossible " + " ▁ to ▁ form " ) ; if ( n % 2 != 0 ) { Num ( n - 5 ) ; System . out . println ( "2 ▁ 3 ▁ " + a + " ▁ " + b ) ; } else { Num ( n - 4 ) ; System . out . println ( "2 ▁ 2 ▁ " + a + " ▁ " + b ) ; } } public static void main ( String [ ] args ) { int n = 28 ; generate ( n ) ; } }

import math ; NEW_LINE def isPrime ( x ) : NEW_LINE INDENT s = int ( math . sqrt ( x ) ) NEW_LINE for i in range ( 2 , s + 1 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT return 1 NEW_LINE DEDENT def Num ( x ) : NEW_LINE INDENT ab = [ 0 ] * 2 NEW_LINE for i in range ( 2 , int ( x / 2 ) + 1 ) : NEW_LINE INDENT if ( isPrime ( i ) != 0 and isPrime ( x - i ) != 0 ) : NEW_LINE INDENT ab [ 0 ] = i NEW_LINE ab [ 1 ] = x - i NEW_LINE return ab NEW_LINE DEDENT DEDENT DEDENT def generate ( n ) : NEW_LINE INDENT if ( n <= 7 ) : NEW_LINE INDENT print ( " Impossible ▁ to ▁ form " ) NEW_LINE DEDENT if ( n % 2 != 0 ) : NEW_LINE INDENT ab = Num ( n - 5 ) NEW_LINE print ( "2 ▁ 3" , ab [ 0 ] , ab [ 1 ] ) NEW_LINE DEDENT else : NEW_LINE INDENT ab = Num ( n - 4 ) NEW_LINE print ( "2 ▁ 2" , ab [ 0 ] , ab [ 1 ] ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 28 NEW_LINE generate ( n ) NEW_LINE DEDENT


class GFG { static int countSubStr ( String str , int len ) { int ans = 0 ; for ( int i = 0 ; i < len ; i ++ ) { if ( str . charAt ( i ) == '0' ) ans += ( i + 1 ) ; } return ans ; } public static void main ( String [ ] args ) { String str = "10010" ; int len = str . length ( ) ; System . out . println ( countSubStr ( str , len ) ) ; } }

def countSubStr ( strr , lenn ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( lenn ) : NEW_LINE INDENT if ( strr [ i ] == '0' ) : NEW_LINE INDENT ans += ( i + 1 ) NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT strr = "10010" NEW_LINE lenn = len ( strr ) NEW_LINE print ( countSubStr ( strr , lenn ) ) NEW_LINE


import java . util . * ; class GFG { static void sieve ( int n , boolean [ ] prime ) { for ( int i = 2 ; i <= n ; i ++ ) prime [ i ] = true ; prime [ 0 ] = prime [ 1 ] = false ; for ( int i = 2 ; i <= n ; i ++ ) { if ( prime [ i ] ) { for ( int j = i * i ; j <= n ; j += i ) { prime [ j ] = false ; } } } } static int sumOfSubset ( int a [ ] , int n ) { int maxi = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; boolean [ ] prime = new boolean [ maxi + 1 ] ; sieve ( maxi , prime ) ; int times = ( int ) Math . pow ( 2 , n - 1 ) ; int sum = 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( prime [ a [ i ] ] ) sum = ( int ) ( sum * ( Math . pow ( a [ i ] , times ) ) ) ; } return sum ; } public static void main ( String [ ] args ) { int a [ ] = { 3 , 7 } ; int n = a . length ; System . out . println ( sumOfSubset ( a , n ) ) ; } }

prime = [ True for i in range ( 100 ) ] NEW_LINE def sieve ( n , prime ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT prime [ i ] = True NEW_LINE DEDENT prime [ 0 ] = prime [ 1 ] = False NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT if ( prime [ i ] ) : NEW_LINE INDENT for j in range ( 2 * i , n + 1 , i ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT DEDENT DEDENT def SumOfSubset ( a , n ) : NEW_LINE INDENT maxi = max ( a ) NEW_LINE sieve ( maxi , prime ) NEW_LINE times = pow ( 2 , n - 1 ) NEW_LINE Sum = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ a [ i ] ] ) : NEW_LINE INDENT Sum = Sum * ( pow ( a [ i ] , times ) ) NEW_LINE DEDENT DEDENT return Sum NEW_LINE DEDENT a = [ 3 , 7 ] NEW_LINE n = len ( a ) NEW_LINE print ( SumOfSubset ( a , n ) ) NEW_LINE


class RepeatElement { void printRepeating ( int arr [ ] , int size ) { int i , j ; System . out . println ( " Repeated ▁ Elements ▁ are ▁ : " ) ; for ( i = 0 ; i < size ; i ++ ) { for ( j = i + 1 ; j < size ; j ++ ) { if ( arr [ i ] == arr [ j ] ) System . out . print ( 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 print ( " Repeating ▁ elements ▁ are ▁ " , end = ' ' ) NEW_LINE for i in range ( 0 , size ) : NEW_LINE INDENT for j in range ( i + 1 , size ) : NEW_LINE INDENT if arr [ i ] == arr [ j ] : NEW_LINE INDENT print ( arr [ i ] , end = ' ▁ ' ) NEW_LINE DEDENT 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 MaximumDiffrence { int maxDiff ( int arr [ ] , int arr_size ) { int max_diff = arr [ 1 ] - arr [ 0 ] ; int i , j ; for ( i = 0 ; i < arr_size ; i ++ ) { for ( j = i + 1 ; j < arr_size ; j ++ ) { if ( arr [ j ] - arr [ i ] > max_diff ) max_diff = arr [ j ] - arr [ i ] ; } } return max_diff ; } public static void main ( String [ ] args ) { MaximumDiffrence maxdif = new MaximumDiffrence ( ) ; int arr [ ] = { 1 , 2 , 90 , 10 , 110 } ; System . out . println ( " Maximum ▁ differnce ▁ is ▁ " + maxdif . maxDiff ( arr , 5 ) ) ; } }

def maxDiff ( arr , arr_size ) : NEW_LINE INDENT max_diff = arr [ 1 ] - arr [ 0 ] NEW_LINE for i in range ( 0 , arr_size ) : NEW_LINE INDENT for j in range ( i + 1 , arr_size ) : NEW_LINE INDENT if ( arr [ j ] - arr [ i ] > max_diff ) : NEW_LINE INDENT max_diff = arr [ j ] - arr [ i ] NEW_LINE DEDENT DEDENT DEDENT return max_diff NEW_LINE DEDENT arr = [ 1 , 2 , 90 , 10 , 110 ] NEW_LINE size = len ( arr ) NEW_LINE print ( " Maximum ▁ difference ▁ is " , maxDiff ( arr , size ) ) NEW_LINE


class GFG { static void fibonacci ( double n ) { double fib ; for ( double i = 0 ; i < n ; i ++ ) { fib = ( Math . pow ( ( 1 + Math . sqrt ( 5 ) ) , i ) - Math . pow ( ( 1 - Math . sqrt ( 5 ) ) , i ) ) / ( Math . pow ( 2 , i ) * Math . sqrt ( 5 ) ) ; System . out . print ( ( int ) fib + " ▁ " ) ; } } public static void main ( String [ ] args ) { double n = 8 ; fibonacci ( n ) ; } }

import math NEW_LINE def fibonacci ( n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT fib = ( ( pow ( ( 1 + math . sqrt ( 5 ) ) , i ) - pow ( ( 1 - math . sqrt ( 5 ) ) , i ) ) / ( pow ( 2 , i ) * math . sqrt ( 5 ) ) ) ; NEW_LINE print ( int ( fib ) , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT n = 8 ; NEW_LINE fibonacci ( n ) ; NEW_LINE


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

def term ( n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT ans = ans + i NEW_LINE DEDENT return ans NEW_LINE DEDENT n = 4 NEW_LINE print ( term ( n ) ) NEW_LINE


import java . io . * ; class Gfg { static int N = 3 ; static void swapDiagonal ( int matrix [ ] [ ] ) { for ( int i = 0 ; i < N ; i ++ ) { int temp = matrix [ i ] [ i ] ; matrix [ i ] [ i ] = matrix [ i ] [ N - i - 1 ] ; matrix [ i ] [ N - i - 1 ] = temp ; } } public static void main ( String arg [ ] ) { int matrix [ ] [ ] = { { 0 , 1 , 2 } , { 3 , 4 , 5 } , { 6 , 7 , 8 } } ; swapDiagonal ( matrix ) ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = 0 ; j < N ; j ++ ) System . out . print ( matrix [ i ] [ j ] + " ▁ " ) ; System . out . println ( ) ; } } }

N = 3 NEW_LINE def swapDiagonal ( matrix ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT matrix [ i ] [ i ] , matrix [ i ] [ N - i - 1 ] =   \ NEW_LINE INDENT matrix [ i ] [ N - i - 1 ] , matrix [ i ] [ i ] NEW_LINE DEDENT DEDENT DEDENT matrix = [ [ 0 , 1 , 2 ] , [ 3 , 4 , 5 ] , [ 6 , 7 , 8 ] ] NEW_LINE swapDiagonal ( matrix ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT for j in range ( N ) : NEW_LINE INDENT print ( matrix [ i ] [ j ] , end = ' ▁ ' ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT


import java . io . * ; import java . util . * ; class GFG { static void findTriplets ( int [ ] arr , int n , int sum ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { int l = i + 1 ; int r = n - 1 ; int x = arr [ i ] ; while ( l < r ) { if ( x + arr [ l ] + arr [ r ] == sum ) { System . out . println ( x + " ▁ " + arr [ l ] + " ▁ " + arr [ r ] ) ; l ++ ; r -- ; } else if ( x + arr [ l ] + arr [ r ] < sum ) l ++ ; else r -- ; } } } public static void main ( String args [ ] ) { int [ ] arr = new int [ ] { 0 , - 1 , 2 , - 3 , 1 } ; int sum = - 2 ; int n = arr . length ; findTriplets ( arr , n , sum ) ; } }

def findTriplets ( arr , n , sum ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT l = i + 1 ; NEW_LINE r = n - 1 ; NEW_LINE x = arr [ i ] ; NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( x + arr [ l ] + arr [ r ] == sum ) : NEW_LINE INDENT print ( x , arr [ l ] , arr [ r ] ) ; NEW_LINE l = l + 1 ; NEW_LINE r = r - 1 ; NEW_LINE DEDENT elif ( x + arr [ l ] + arr [ r ] < sum ) : NEW_LINE INDENT l = l + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT r = r - 1 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT arr = [ 0 , - 1 , 2 , - 3 , 1 ] ; NEW_LINE sum = - 2 ; NEW_LINE n = len ( arr ) ; NEW_LINE findTriplets ( arr , n , sum ) ; NEW_LINE


import java . util . * ; class GfG { static class Node { int data ; Node next ; } static Node head = null ; static Node Reverse ( Node head , int k ) { Stack < Node > mystack = new Stack < Node > ( ) ; Node current = head ; Node prev = null ; while ( current != null ) { int count = 0 ; while ( current != null && count < k ) { mystack . push ( current ) ; current = current . next ; count ++ ; } while ( mystack . size ( ) > 0 ) { if ( prev == null ) { prev = mystack . peek ( ) ; head = prev ; mystack . pop ( ) ; } else { prev . next = mystack . peek ( ) ; prev = prev . next ; mystack . pop ( ) ; } } } prev . next = null ; return head ; } static void push ( int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = head ; head = new_node ; } static void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } public static void main ( String [ ] args ) { push ( 9 ) ; push ( 8 ) ; push ( 7 ) ; push ( 6 ) ; push ( 5 ) ; push ( 4 ) ; push ( 3 ) ; push ( 2 ) ; push ( 1 ) ; System . out . println ( " Given ▁ linked ▁ list ▁ " ) ; printList ( head ) ; head = Reverse ( head , 3 ) ; System . out . println ( ) ; System . out . println ( " Reversed ▁ Linked ▁ list ▁ " ) ; printList ( head ) ; } }

class Node ( object ) : NEW_LINE INDENT __slots__ = ' data ' , ' next ' NEW_LINE def __init__ ( self , data = None , next = None ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT def __repr__ ( self ) : NEW_LINE INDENT return repr ( self . data ) NEW_LINE DEDENT DEDENT class LinkedList ( object ) : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . head = None NEW_LINE DEDENT def __repr__ ( self ) : NEW_LINE INDENT nodes = [ ] NEW_LINE curr = self . head NEW_LINE while curr : NEW_LINE INDENT nodes . append ( repr ( curr ) ) NEW_LINE curr = curr . next NEW_LINE DEDENT return ' [ ' + ' , ▁ ' . join ( nodes ) + ' ] ' NEW_LINE DEDENT def prepend ( self , data ) : NEW_LINE INDENT self . head = Node ( data = data , next = self . head ) NEW_LINE DEDENT def reverse ( self , k = 1 ) : NEW_LINE INDENT if self . head is None : NEW_LINE INDENT return NEW_LINE DEDENT curr = self . head NEW_LINE prev = None NEW_LINE new_stack = [ ] NEW_LINE while curr is not None : NEW_LINE INDENT val = 0 NEW_LINE while curr is not None and val < k : NEW_LINE INDENT new_stack . append ( curr . data ) NEW_LINE curr = curr . next NEW_LINE val += 1 NEW_LINE DEDENT while new_stack : NEW_LINE INDENT if prev is None : NEW_LINE INDENT prev = Node ( new_stack . pop ( ) ) NEW_LINE self . head = prev NEW_LINE DEDENT else : NEW_LINE INDENT prev . next = Node ( new_stack . pop ( ) ) NEW_LINE prev = prev . next NEW_LINE DEDENT DEDENT DEDENT prev . next = None NEW_LINE return self . head NEW_LINE DEDENT DEDENT llist = LinkedList ( ) NEW_LINE llist . prepend ( 9 ) NEW_LINE llist . prepend ( 8 ) NEW_LINE llist . prepend ( 7 ) NEW_LINE llist . prepend ( 6 ) NEW_LINE llist . prepend ( 5 ) NEW_LINE llist . prepend ( 4 ) NEW_LINE llist . prepend ( 3 ) NEW_LINE llist . prepend ( 2 ) NEW_LINE llist . prepend ( 1 ) NEW_LINE print ( " Given ▁ linked ▁ list " ) NEW_LINE print ( llist ) NEW_LINE llist . head = llist . reverse ( 3 ) NEW_LINE print ( " Reversed ▁ Linked ▁ list " ) NEW_LINE print ( llist ) NEW_LINE


import java . io . * ; class GFG { static int distinctCount ( int arr [ ] , int n ) { int count = n ; int i = 0 , j = n - 1 , sum = 0 ; while ( i < j ) { while ( i != j && arr [ i ] == arr [ i + 1 ] ) { count -- ; i ++ ; } while ( i != j && arr [ j ] == arr [ j - 1 ] ) { count -- ; j -- ; } if ( i == j ) break ; sum = arr [ i ] + arr [ j ] ; if ( sum == 0 ) { count -- ; i ++ ; j -- ; } else if ( sum < 0 ) i ++ ; else j -- ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { - 2 , - 1 , 0 , 1 , 1 } ; int n = arr . length ; System . out . println ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " + distinctCount ( arr , n ) ) ; } }

def distinctCount ( arr , n ) : NEW_LINE INDENT count = n ; NEW_LINE i = 0 ; j = n - 1 ; sum = 0 ; NEW_LINE while ( i < j ) : NEW_LINE INDENT while ( i != j and arr [ i ] == arr [ i + 1 ] ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE i = i + 1 ; NEW_LINE DEDENT while ( i != j and arr [ j ] == arr [ j - 1 ] ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT if ( i == j ) : NEW_LINE INDENT break ; NEW_LINE DEDENT sum = arr [ i ] + arr [ j ] ; NEW_LINE if ( sum == 0 ) : NEW_LINE INDENT count = count - 1 ; NEW_LINE i = i + 1 ; NEW_LINE j = j - 1 ; NEW_LINE DEDENT elif ( sum < 0 ) : NEW_LINE INDENT i = i + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT j = j - 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT arr = [ - 2 , - 1 , 0 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( " Count ▁ of ▁ absolute ▁ distinct ▁ values ▁ : ▁ " , distinctCount ( arr , n ) ) ; NEW_LINE


import java . io . * ; class Combination { static void combinationUtil ( int arr [ ] , int data [ ] , int start , int end , int index , int r ) { if ( index == r ) { for ( int j = 0 ; j < r ; j ++ ) System . out . print ( data [ j ] + " ▁ " ) ; System . out . println ( " " ) ; return ; } for ( int i = start ; i <= end && end - i + 1 >= r - index ; i ++ ) { data [ index ] = arr [ i ] ; combinationUtil ( arr , data , i + 1 , end , index + 1 , r ) ; } } static void printCombination ( int arr [ ] , int n , int r ) { int data [ ] = new int [ r ] ; combinationUtil ( arr , data , 0 , n - 1 , 0 , r ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 } ; int r = 3 ; int n = arr . length ; printCombination ( arr , n , r ) ; } }

def printCombination ( arr , n , r ) : NEW_LINE INDENT data = [ 0 ] * r ; NEW_LINE combinationUtil ( arr , data , 0 , n - 1 , 0 , r ) ; NEW_LINE DEDENT def combinationUtil ( arr , data , start , end , index , r ) : NEW_LINE INDENT if ( index == r ) : NEW_LINE INDENT for j in range ( r ) : NEW_LINE INDENT print ( data [ j ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE return ; NEW_LINE DEDENT i = start ; NEW_LINE while ( i <= end and end - i + 1 >= r - index ) : NEW_LINE INDENT data [ index ] = arr [ i ] ; NEW_LINE combinationUtil ( arr , data , i + 1 , end , index + 1 , r ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT DEDENT arr = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE r = 3 ; NEW_LINE n = len ( arr ) ; NEW_LINE printCombination ( arr , n , r ) ; NEW_LINE


import java . io . * ; class GFG { static void halfsquare ( int n , int x , int y ) { int half = n / 2 ; if ( ( half == x || half == x - 1 ) && ( half == y || half == y - 1 ) ) System . out . println ( " NO " ) ; else System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int n = 100 ; int x = 51 , y = 100 ; halfsquare ( n , x , y ) ; } }

def halfsquare ( n , x , y ) : NEW_LINE INDENT half = n // 2 NEW_LINE if ( ( half == x or half == x - 1 ) and ( half == y or half == y - 1 ) ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 100 NEW_LINE x , y = 51 , 100 NEW_LINE halfsquare ( n , x , y ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int mostFrequent ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; int max_count = 1 , res = arr [ 0 ] ; int curr_count = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] == arr [ i - 1 ] ) curr_count ++ ; else { if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ i - 1 ] ; } curr_count = 1 ; } } if ( curr_count > max_count ) { max_count = curr_count ; res = arr [ n - 1 ] ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 5 , 2 , 1 , 3 , 2 , 1 } ; int n = arr . length ; System . out . println ( mostFrequent ( arr , n ) ) ; } }

def mostFrequent ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE max_count = 1 ; res = arr [ 0 ] ; curr_count = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] == arr [ i - 1 ] ) : NEW_LINE INDENT curr_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT if ( curr_count > max_count ) : NEW_LINE INDENT max_count = curr_count NEW_LINE res = arr [ i - 1 ] NEW_LINE DEDENT curr_count = 1 NEW_LINE DEDENT DEDENT if ( curr_count > max_count ) : NEW_LINE INDENT max_count = curr_count NEW_LINE res = arr [ n - 1 ] NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 1 , 5 , 2 , 1 , 3 , 2 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( mostFrequent ( arr , n ) ) NEW_LINE


import java . util . Arrays ; import java . io . * ; class GFG { static int items ( int n , int a [ ] ) { Arrays . sort ( a ) ; int z = a [ n - 1 ] ; int x = 1 ; int s = 0 ; for ( int i = 0 ; i < n - 1 ; i ++ ) { s += a [ i ] ; if ( s <= z ) x += 1 ; else break ; } return x ; } public static void main ( String [ ] args ) { int n = 5 ; int a [ ] = { 5 , 3 , 1 , 5 , 6 } ; System . out . println ( items ( n , a ) ) ; } }

def items ( n , a ) : NEW_LINE INDENT a . sort ( ) NEW_LINE z = a [ n - 1 ] NEW_LINE x = 1 NEW_LINE s = 0 NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT s += a [ i ] NEW_LINE if ( s <= z ) : NEW_LINE INDENT x += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT n = 5 NEW_LINE a = [ 5 , 3 , 1 , 5 , 6 ] NEW_LINE print ( items ( n , a ) ) NEW_LINE


import java . util . * ; class GFG { static int leastValue ( int P , int A , int N , int a [ ] ) { int ans = - 1 ; float tmp = Float . MAX_VALUE ; for ( int i = 0 ; i < N ; i ++ ) { float t = ( float ) ( P - a [ i ] * 0.006 ) ; if ( Math . abs ( t - A ) < tmp ) { tmp = Math . abs ( t - A ) ; ans = i ; } } return a [ ans ] ; } public static void main ( String [ ] args ) { int N = 2 , P = 12 , A = 2005 ; int a [ ] = { 1000 , 2000 } ; System . out . println ( leastValue ( P , A , N , a ) ) ; } }

def leastValue ( P , A , N , a ) : NEW_LINE INDENT ans = - 1 NEW_LINE tmp = float ( ' inf ' ) NEW_LINE for i in range ( N ) : NEW_LINE INDENT t = P - a [ i ] * 0.006 NEW_LINE if abs ( t - A ) < tmp : NEW_LINE INDENT tmp = abs ( t - A ) NEW_LINE ans = i NEW_LINE DEDENT DEDENT return a [ ans ] NEW_LINE DEDENT N , P , A = 2 , 12 , 5 NEW_LINE a = [ 1000 , 2000 ] NEW_LINE print ( leastValue ( P , A , N , a ) ) NEW_LINE


class GFG { static int getSingle ( int arr [ ] , int n ) { int ones = 0 , twos = 0 ; int common_bit_mask ; for ( int i = 0 ; i < n ; i ++ ) { twos = twos | ( ones & arr [ i ] ) ; ones = ones ^ arr [ i ] ; common_bit_mask = ~ ( ones & twos ) ; ones &= common_bit_mask ; twos &= common_bit_mask ; } return ones ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 3 , 2 , 3 } ; int n = arr . length ; System . out . println ( " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " + getSingle ( arr , n ) ) ; } }

def getSingle ( arr , n ) : NEW_LINE INDENT ones = 0 NEW_LINE twos = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT twos = twos | ( ones & arr [ i ] ) NEW_LINE ones = ones ^ arr [ i ] NEW_LINE common_bit_mask = ~ ( ones & twos ) NEW_LINE ones &= common_bit_mask NEW_LINE twos &= common_bit_mask NEW_LINE DEDENT return ones NEW_LINE DEDENT arr = [ 3 , 3 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " The ▁ element ▁ with ▁ single ▁ occurrence ▁ is ▁ " , getSingle ( arr , n ) ) NEW_LINE


import java . io . * ; import java . math . * ; public class GFG { int getMinNum ( int a , int b , int c ) { if ( c < a || c > b ) { return c ; } int x = ( ( b / c ) * c ) + c ; return x ; } public static void main ( String args [ ] ) { int a = 2 ; int b = 4 ; int c = 4 ; GFG g = new GFG ( ) ; System . out . println ( g . getMinNum ( a , b , c ) ) ; } }

def getMinNum ( a , b , c ) : NEW_LINE INDENT if ( c < a or c > b ) : NEW_LINE INDENT return c NEW_LINE DEDENT x = ( ( b // c ) * c ) + c NEW_LINE return x NEW_LINE DEDENT a , b , c = 2 , 4 , 4 NEW_LINE print ( getMinNum ( a , b , c ) ) NEW_LINE


class GFG { static class Point { int x , y ; } static boolean doOverlap ( Point l1 , Point r1 , Point l2 , Point r2 ) { if ( l1 . x >= r2 . x || l2 . x >= r1 . x ) { return false ; } if ( l1 . y <= r2 . y || l2 . y <= r1 . y ) { return false ; } return true ; } public static void main ( String [ ] args ) { Point l1 = new Point ( ) , r1 = new Point ( ) , l2 = new Point ( ) , r2 = new Point ( ) ; l1 . x = 0 ; l1 . y = 10 ; r1 . x = 10 ; r1 . y = 0 ; l2 . x = 5 ; l2 . y = 5 ; r2 . x = 15 ; r2 . y = 0 ; if ( doOverlap ( l1 , r1 , l2 , r2 ) ) { System . out . println ( " Rectangles ▁ Overlap " ) ; } else { System . out . println ( " Rectangles ▁ Don ' t ▁ Overlap " ) ; } } }

class Point : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT def doOverlap ( l1 , r1 , l2 , r2 ) : NEW_LINE INDENT if ( l1 . x >= r2 . x or l2 . x >= r1 . x ) : NEW_LINE INDENT return False NEW_LINE DEDENT if ( l1 . y <= r2 . y or l2 . y <= r1 . y ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l1 = Point ( 0 , 10 ) NEW_LINE r1 = Point ( 10 , 0 ) NEW_LINE l2 = Point ( 5 , 5 ) NEW_LINE r2 = Point ( 15 , 0 ) NEW_LINE if ( doOverlap ( l1 , r1 , l2 , r2 ) ) : NEW_LINE INDENT print ( " Rectangles ▁ Overlap " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Rectangles ▁ Don ' t ▁ Overlap " ) NEW_LINE DEDENT DEDENT


import java . util . * ; class GFG { static String solve ( int n , char x , char y ) { int length = ( int ) Math . ceil ( Math . log ( n + 2 ) / Math . log ( 2 ) ) - 1 ; int rank = n - ( 1 << length ) + 1 ; String left = " " , right = " " ; for ( int i = length - 1 ; i >= 0 ; i -- ) { int mask = ( 1 << i ) ; int bit = mask & rank ; if ( bit > 0 ) { left += y ; right += y ; } else { left += x ; right += x ; } } StringBuilder sb = new StringBuilder ( right ) ; sb . reverse ( ) ; right = sb . toString ( ) ; String res = left + right ; return res ; } public static void main ( String [ ] args ) { int n = 23 ; char x = '4' , y = '5' ; String ans = solve ( n , x , y ) ; System . out . println ( ans ) ; } }

from math import ceil , log2 NEW_LINE def solve ( n , x , y ) : NEW_LINE INDENT length = ceil ( log2 ( n + 2 ) ) - 1 ; NEW_LINE rank = n - ( 1 << length ) + 1 ; NEW_LINE left = " " ; right = " " ; NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT mask = ( 1 << i ) ; NEW_LINE bit = ( mask & rank ) ; NEW_LINE if ( bit ) : NEW_LINE INDENT left += y ; NEW_LINE right += y ; NEW_LINE DEDENT else : NEW_LINE INDENT left += x ; NEW_LINE right += x ; NEW_LINE DEDENT DEDENT right = right [ : : - 1 ] ; NEW_LINE res = left + right ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 23 ; NEW_LINE x = '4' ; NEW_LINE y = '5' ; NEW_LINE ans = solve ( n , x , y ) ; NEW_LINE print ( ans ) ; NEW_LINE DEDENT


import java . io . * ; class GFG { static float Area ( float a , float b ) { if ( a < 0 && b < 0 ) return - 1 ; float h = ( float ) Math . sqrt ( ( ( Math . pow ( a , 2 ) + Math . pow ( b , 2 ) ) / ( Math . pow ( a , 2 ) * Math . pow ( b , 2 ) ) ) ) ; float A = ( float ) ( 0.70477 * Math . pow ( h , 2 ) ) ; return A ; } public static void main ( String [ ] args ) { float a = 5 , b = 4 ; System . out . println ( Area ( a , b ) ) ; } }

import math ; NEW_LINE def Area ( a , b ) : NEW_LINE INDENT if ( a < 0 and b < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT h = math . sqrt ( ( ( pow ( a , 2 ) + pow ( b , 2 ) ) / ( pow ( a , 2 ) * pow ( b , 2 ) ) ) ) ; NEW_LINE A = 0.70477 * pow ( h , 2 ) ; NEW_LINE return A ; NEW_LINE DEDENT a = 5 ; NEW_LINE b = 4 ; NEW_LINE print ( round ( Area ( a , b ) , 7 ) ) ; NEW_LINE


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

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


import java . io . * ; class GFG { static void printReverseFloyd ( int n ) { int curr_val = n * ( n + 1 ) / 2 ; for ( int i = n ; i >= 1 ; i -- ) { for ( int j = i ; j >= 1 ; j -- ) { System . out . printf ( " % 2d ▁ ▁ " , curr_val -- ) ; } System . out . println ( " " ) ; } } public static void main ( String [ ] args ) { int n = 7 ; printReverseFloyd ( n ) ; } }

def printReverseFloyd ( n ) : NEW_LINE INDENT curr_val = int ( n * ( n + 1 ) / 2 ) NEW_LINE for i in range ( n + 1 , 1 , - 1 ) : NEW_LINE INDENT for j in range ( i , 1 , - 1 ) : NEW_LINE INDENT print ( curr_val , end = " ▁ ▁ " ) NEW_LINE curr_val -= 1 NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT n = 7 NEW_LINE printReverseFloyd ( n ) NEW_LINE


public class Discriminant { static int disc ( int a , int b , int c ) { int dis = ( int ) Math . pow ( b , 2 ) - ( 4 * a * c ) ; return dis ; } public static void main ( String args [ ] ) { int a = 20 ; int b = 30 ; int c = 10 ; int discriminant = disc ( a , b , c ) ; if ( discriminant > 0 ) { System . out . println ( " Discriminant ▁ is ▁ " + discriminant + " ▁ which ▁ is ▁ Positive " ) ; System . out . println ( " Hence ▁ Two ▁ Solutions " ) ; } else if ( discriminant == 0 ) { System . out . println ( " Discriminant ▁ is ▁ " + discriminant + " ▁ which ▁ is ▁ Zero " ) ; System . out . println ( " Hence ▁ One ▁ Solution " ) ; } else { System . out . println ( " Discriminant ▁ is ▁ " + discriminant + " ▁ which ▁ is ▁ Negative " ) ; System . out . println ( " Hence ▁ No ▁ Real ▁ Solutions " ) ; } } }

def discriminant ( a , b , c ) : NEW_LINE INDENT discriminant = ( b ** 2 ) - ( 4 * a * c ) NEW_LINE if discriminant > 0 : NEW_LINE INDENT print ( ' Discriminant ▁ is ' , discriminant , " which ▁ is ▁ Positive " ) NEW_LINE print ( ' Hence ▁ Two ▁ Solutions ' ) NEW_LINE DEDENT elif discriminant == 0 : NEW_LINE INDENT print ( ' Discriminant ▁ is ' , discriminant , " which ▁ is ▁ Zero " ) NEW_LINE print ( ' Hence ▁ One ▁ Solution ' ) NEW_LINE DEDENT elif discriminant < 0 : NEW_LINE INDENT print ( ' Discriminant ▁ is ' , discriminant , " which ▁ is ▁ Negative " ) NEW_LINE print ( ' Hence ▁ No ▁ Real ▁ Solutions ' ) NEW_LINE DEDENT DEDENT a = 20 NEW_LINE b = 30 NEW_LINE c = 10 NEW_LINE discriminant ( a , b , c ) NEW_LINE


import java . io . * ; public class GFG { void sum ( int [ ] arr , int n ) { int i = n ; if ( arr [ i ] < 9 ) { arr [ i ] = arr [ i ] + 1 ; return ; } arr [ i ] = 0 ; i -- ; sum ( arr , i ) ; if ( arr [ 0 ] > 0 ) System . out . print ( arr [ 0 ] + " , ▁ " ) ; for ( i = 1 ; i <= n ; i ++ ) { System . out . print ( arr [ i ] ) ; if ( i < n ) System . out . print ( " , ▁ " ) ; } } static public void main ( String [ ] args ) { GFG obj = new GFG ( ) ; int n = 4 ; int [ ] arr = { 0 , 1 , 9 , 3 , 9 } ; obj . sum ( arr , n ) ; } }

def sum ( arr , n ) : NEW_LINE INDENT i = n NEW_LINE if ( arr [ i ] < 9 ) : NEW_LINE INDENT arr [ i ] = arr [ i ] + 1 NEW_LINE return NEW_LINE DEDENT arr [ i ] = 0 NEW_LINE i -= 1 NEW_LINE sum ( arr , i ) NEW_LINE if ( arr [ 0 ] > 0 ) : NEW_LINE INDENT print ( arr [ 0 ] , " , ▁ " , end = " " ) NEW_LINE DEDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT print ( arr [ i ] , end = " " ) NEW_LINE if ( i < n ) : NEW_LINE INDENT print ( " , ▁ " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT n = 4 NEW_LINE arr = [ 0 , 1 , 9 , 3 , 9 ] NEW_LINE sum ( arr , n ) NEW_LINE


import java . util . * ; class GFG { public static void main ( String [ ] args ) { int arr [ ] = { 4 , 2 , 12 , 13 , 5 , 19 } ; int n = arr . length ; int k = 3 ; primeSumAndProduct ( arr , n , k ) ; } static boolean [ ] SieveOfEratosthenes ( int max_val ) { boolean [ ] prime = new boolean [ max_val + 1 ] ; for ( int i = 0 ; i <= max_val ; i ++ ) prime [ i ] = true ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime [ p ] == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime [ i ] = false ; } } return prime ; } static void primeSumAndProduct ( int arr [ ] , int n , int k ) { int max_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) max_val = Math . max ( max_val , arr [ i ] ) ; boolean [ ] prime = SieveOfEratosthenes ( max_val ) ; prime [ 0 ] = false ; prime [ 1 ] = false ; PriorityQueue < Integer > maxHeap = new PriorityQueue < Integer > ( Collections . reverseOrder ( ) ) ; PriorityQueue < Integer > minHeap = new PriorityQueue < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( prime [ arr [ i ] ] ) { minHeap . add ( arr [ i ] ) ; maxHeap . add ( arr [ i ] ) ; } long minProduct = 1 , maxProduct = 1 , minSum = 0 , maxSum = 0 ; while ( k > 0 ) { k -- ; minProduct *= minHeap . peek ( ) ; maxProduct *= maxHeap . peek ( ) ; minSum += minHeap . peek ( ) ; maxSum += maxHeap . peek ( ) ; minHeap . remove ( ) ; maxHeap . remove ( ) ; } System . out . println ( " Sum ▁ of ▁ k - minimum ▁ prime ▁ numbers ▁ is ▁ " + minSum ) ; System . out . println ( " Sum ▁ of ▁ k - maximum ▁ prime ▁ numbers ▁ is ▁ " + maxSum ) ; System . out . println ( " Product ▁ of ▁ k - minimum ▁ prime ▁ numbers ▁ is ▁ " + minProduct ) ; System . out . println ( " Product ▁ of ▁ k - maximum ▁ prime ▁ numbers ▁ is ▁ " + maxProduct ) ; } }

import heapq NEW_LINE def SieveOfEratosthenes ( max_val ) : NEW_LINE INDENT prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE p = 2 NEW_LINE while p * p <= max_val : NEW_LINE INDENT if ( prime [ p ] == True ) : NEW_LINE INDENT for j in range ( 2 * p , max_val + 1 , p ) : NEW_LINE INDENT prime [ j ] = False NEW_LINE DEDENT DEDENT p += 1 NEW_LINE DEDENT return prime NEW_LINE DEDENT def primeSumAndProduct ( arr , n , k ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = SieveOfEratosthenes ( max_val ) NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE Heap = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( prime [ arr [ i ] ] ) : NEW_LINE INDENT Heap . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT minProduct = 1 NEW_LINE maxProduct = 1 NEW_LINE minSum = 0 NEW_LINE maxSum = 0 NEW_LINE min_k = heapq . nsmallest ( k , Heap ) NEW_LINE max_k = heapq . nlargest ( k , Heap ) NEW_LINE minSum = sum ( min_k ) NEW_LINE maxSum = sum ( max_k ) NEW_LINE for val in min_k : NEW_LINE INDENT minProduct *= val NEW_LINE DEDENT for val in max_k : NEW_LINE INDENT maxProduct *= val NEW_LINE DEDENT print ( " Sum ▁ of ▁ k - minimum ▁ prime ▁ numbers ▁ is " , minSum ) NEW_LINE print ( " Sum ▁ of ▁ k - maximum ▁ prime ▁ numbers ▁ is " , maxSum ) NEW_LINE print ( " Product ▁ of ▁ k - minimum ▁ prime ▁ numbers ▁ is " , minProduct ) NEW_LINE print ( " Product ▁ of ▁ k - maximum ▁ prime ▁ numbers ▁ is " , maxProduct ) NEW_LINE DEDENT arr = [ 4 , 2 , 12 , 13 , 5 , 19 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE primeSumAndProduct ( arr , n , k ) NEW_LINE


class GFG { static class Data { double x , y ; public Data ( double x , double y ) { super ( ) ; this . x = x ; this . y = y ; } } ; static double inv_interpolate ( Data [ ] d , int n , double y ) { double x = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { double xi = d [ i ] . x ; for ( j = 0 ; j < n ; j ++ ) { if ( j != i ) { xi = xi * ( y - d [ j ] . y ) / ( d [ i ] . y - d [ j ] . y ) ; } } x += xi ; } return x ; } public static void main ( String [ ] args ) { Data [ ] d = { new Data ( 1.27 , 2.3 ) , new Data ( 2.25 , 2.95 ) , new Data ( 2.5 , 3.5 ) , new Data ( 3.6 , 5.1 ) } ; int n = 4 ; double y = 4.5 ; System . out . printf ( " Value ▁ of ▁ x ▁ at ▁ y ▁ = ▁ 4.5 ▁ : ▁ % .5f " , inv_interpolate ( d , n , y ) ) ; } }

class Data : NEW_LINE INDENT def __init__ ( self , x , y ) : NEW_LINE INDENT self . x = x NEW_LINE self . y = y NEW_LINE DEDENT DEDENT def inv_interpolate ( d : list , n : int , y : float ) -> float : NEW_LINE INDENT x = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT xi = d [ i ] . x NEW_LINE for j in range ( n ) : NEW_LINE INDENT if j != i : NEW_LINE INDENT xi = ( xi * ( y - d [ j ] . y ) / ( d [ i ] . y - d [ j ] . y ) ) NEW_LINE DEDENT DEDENT x += xi NEW_LINE DEDENT return x NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT d = [ Data ( 1.27 , 2.3 ) , Data ( 2.25 , 2.95 ) , Data ( 2.5 , 3.5 ) , Data ( 3.6 , 5.1 ) ] NEW_LINE n = 4 NEW_LINE y = 4.5 NEW_LINE print ( " Value ▁ of ▁ x ▁ at ▁ y ▁ = ▁ 4.5 ▁ : " , round ( inv_interpolate ( d , n , y ) , 5 ) ) NEW_LINE DEDENT


import java . util . * ; class GFG { static int nx = 0 , ny = 0 ; static boolean check ( String s , int p , int q ) { for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( s . charAt ( i ) == ' x ' ) nx ++ ; else ny ++ ; } if ( ( nx * ny ) != ( p + q ) ) return true ; else return false ; } public static String smallestPermutation ( String s , int p , int q ) { if ( check ( s , p , q ) == true ) { return " Impossible " ; } char tempArray [ ] = s . toCharArray ( ) ; Arrays . sort ( tempArray ) ; String str = new String ( tempArray ) ; int a = nx * ny , b = 0 , i = 0 , j = 0 ; if ( a == p && b == q ) { return str ; } while ( 1 > 0 ) { for ( i = 0 ; i < str . length ( ) - 1 ; ++ i ) { if ( str . charAt ( i ) == ' x ' && str . charAt ( i + 1 ) == ' y ' ) break ; } for ( j = i ; j < str . length ( ) - 1 ; j ++ ) { if ( str . charAt ( j ) == ' x ' && str . charAt ( j + 1 ) == ' y ' ) { StringBuilder sb = new StringBuilder ( str ) ; sb . setCharAt ( j , str . charAt ( j + 1 ) ) ; sb . setCharAt ( j + 1 , str . charAt ( j ) ) ; str = sb . toString ( ) ; a -- ; b ++ ; if ( a == p && b == q ) { return str ; } } } } } public static void main ( String [ ] args ) { String s = " yxxyx " ; int p = 3 , q = 3 ; System . out . print ( smallestPermutation ( s , p , q ) ) ; } }

def check ( s , p , q ) : NEW_LINE INDENT global nx NEW_LINE global ny NEW_LINE for i in range ( 0 , len ( s ) ) : NEW_LINE INDENT if s [ i ] == ' x ' : NEW_LINE INDENT nx += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ny += 1 NEW_LINE DEDENT DEDENT if nx * ny != p + q : NEW_LINE INDENT return 1 NEW_LINE DEDENT else : NEW_LINE INDENT return 0 NEW_LINE DEDENT DEDENT def smallestPermutation ( s , p , q ) : NEW_LINE INDENT if check ( s , p , q ) == 1 : NEW_LINE INDENT return " Impossible " NEW_LINE DEDENT s = sorted ( s ) NEW_LINE a , b , i = nx * ny , 0 , 0 NEW_LINE if a == p and b == q : NEW_LINE INDENT return ' ' . join ( s ) NEW_LINE DEDENT while True : NEW_LINE INDENT for i in range ( 0 , len ( s ) - 1 ) : NEW_LINE INDENT if s [ i ] == ' x ' and s [ i + 1 ] == ' y ' : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT for j in range ( i , len ( s ) - 1 ) : NEW_LINE INDENT if s [ j ] == ' x ' and s [ j + 1 ] == ' y ' : NEW_LINE INDENT s [ j ] , s [ j + 1 ] = s [ j + 1 ] , s [ j ] NEW_LINE a -= 1 NEW_LINE b += 1 NEW_LINE if a == p and b == q : NEW_LINE INDENT return ' ' . join ( s ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT nx , ny = 0 , 0 NEW_LINE s = " yxxyx " NEW_LINE p , q = 3 , 3 NEW_LINE print ( smallestPermutation ( s , p , q ) ) NEW_LINE DEDENT


import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static int totalSum ( int a [ ] , int n ) { int i , sum = 0 , sum1 = 0 , j ; for ( i = 0 ; i < n ; i ++ ) { sum1 = 0 ; for ( j = i ; j < n ; j ++ ) { sum1 = ( sum1 | a [ j ] ) ; sum = sum + sum1 ; } } return sum ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 } ; int n = a . length ; System . out . println ( totalSum ( a , n ) ) ; } }

def totalSum ( a , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT sum1 = ( sum1 | a [ j ] ) ; NEW_LINE sum = sum + sum1 ; NEW_LINE DEDENT DEDENT return sum ; NEW_LINE DEDENT a = [ 1 , 2 , 3 , 4 , 5 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( totalSum ( a , n ) ) ; NEW_LINE


class GFG { static int triplets ( int N ) { return ( ( N + 1 ) * ( N + 2 ) ) / 2 ; } public static void main ( String [ ] args ) { int N = 50 ; System . out . println ( triplets ( N ) ) ; } }

def triplets ( N ) : NEW_LINE INDENT return ( ( N + 1 ) * ( N + 2 ) ) // 2 ; NEW_LINE DEDENT N = 50 ; NEW_LINE print ( triplets ( N ) ) NEW_LINE


class GFG { static int largestPrimeFactor ( int n ) { int max = - 1 ; while ( n % 2 == 0 ) { max = 2 ; n >>= 1 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { max = i ; n = n / i ; } } if ( n > 2 ) max = n ; return max ; } static boolean checkUnusual ( int n ) { int factor = largestPrimeFactor ( n ) ; if ( factor > Math . sqrt ( n ) ) { return true ; } else { return false ; } } public static void main ( String [ ] args ) { int n = 14 ; if ( checkUnusual ( n ) ) { System . out . println ( " YES " ) ; } else { System . out . println ( " NO " ) ; } } }

from math import sqrt NEW_LINE def largestPrimeFactor ( n ) : NEW_LINE INDENT max = - 1 NEW_LINE while n % 2 == 0 : NEW_LINE INDENT max = 2 ; NEW_LINE n >>= 1 ; NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( n ) ) + 1 , 2 ) : NEW_LINE INDENT while n % i == 0 : NEW_LINE INDENT max = i ; NEW_LINE n = n / i ; NEW_LINE DEDENT DEDENT if n > 2 : NEW_LINE INDENT max = n NEW_LINE DEDENT return max NEW_LINE DEDENT def checkUnusual ( n ) : NEW_LINE INDENT factor = largestPrimeFactor ( n ) NEW_LINE if factor > sqrt ( n ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 14 NEW_LINE if checkUnusual ( n ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT


import java . util . * ; public class OrderOfCharactersForPattern { public static boolean followsPattern ( String str , String pattern ) { Set < Character > patternSet = neHashSet < > ( ) ; for ( int i = 0 ; i < pattern . length ( ) ; i ++ ) patternSet . add ( pattern . charAt ( i ) ) ; StringBuilder modifiedString = new StringBuilder ( str ) ; for ( int i = str . length ( ) - 1 ; i >= 0 ; i -- ) if ( ! patternSet . contains ( modifiedString . charAt ( i ) ) ) modifiedString . deleteCharAt ( i ) ; for ( int i = modifiedString . length ( ) - 1 ; i > 0 ; i -- ) if ( modifiedString . charAt ( i ) == modifiedString . charAt ( i - 1 ) ) modifiedString . deleteCharAt ( i ) ; if ( pattern . length ( ) != modifiedString . length ( ) ) return false ; for ( int i = 0 ; i < pattern . length ( ) ; i ++ ) if ( pattern . charAt ( i ) != modifiedString . charAt ( i ) ) return false ; return true ; } int main ( ) { String str = " engineers ▁ rock " ; String pattern = " er " ; System . out . println ( " Expected : ▁ true , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " egr " ; System . out . println ( " Expected : ▁ false , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " gsr " ; System . out . println ( " Expected : ▁ false , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; str = " engineers ▁ rock " ; pattern = " eger " ; System . out . println ( " Expected : ▁ true , ▁ Actual : ▁ " + followsPattern ( str , pattern ) ) ; return 0 ; } }

def followsPattern ( string , pattern ) : NEW_LINE INDENT patternSet = set ( ) NEW_LINE for i in range ( len ( pattern ) ) : NEW_LINE INDENT patternSet . add ( pattern [ i ] ) NEW_LINE DEDENT modifiedString = string NEW_LINE for i in range ( len ( string ) - 1 , - 1 , - 1 ) : NEW_LINE INDENT if not modifiedString [ i ] in patternSet : NEW_LINE INDENT modifiedString = modifiedString [ : i ] +   \ NEW_LINE INDENT modifiedString [ i + 1 : ] NEW_LINE DEDENT DEDENT DEDENT for i in range ( len ( modifiedString ) - 1 , 0 , - 1 ) : NEW_LINE INDENT if modifiedString [ i ] == modifiedString [ i - 1 ] : NEW_LINE INDENT modifiedString = modifiedString [ : i ] +   \ NEW_LINE INDENT modifiedString [ i + 1 : ] NEW_LINE DEDENT DEDENT DEDENT if len ( pattern ) != len ( modifiedString ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( len ( pattern ) ) : NEW_LINE INDENT if pattern [ i ] != modifiedString [ i ] : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " engineers ▁ rock " NEW_LINE pattern = " er " NEW_LINE print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " egr " NEW_LINE print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " gsr " NEW_LINE print ( " Expected : ▁ false , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE string = " engineers ▁ rock " NEW_LINE pattern = " eger " NEW_LINE print ( " Expected : ▁ true , ▁ Actual : " , followsPattern ( string , pattern ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int MAX = 100000 ; static void countEleLessThanOrEqual ( int arr1 [ ] , int m , int arr2 [ ] , int n ) { int hash [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) hash [ arr2 [ i ] ] ++ ; for ( int i = 1 ; i < MAX ; i ++ ) { hash [ i ] = hash [ i ] + hash [ i - 1 ] ; } for ( int i = 0 ; i < m ; i ++ ) { System . out . print ( hash [ arr1 [ i ] ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 , 4 , 7 , 9 } ; int arr2 [ ] = { 0 , 1 , 2 , 1 , 1 , 4 } ; int m , n ; m = arr1 . length ; n = arr2 . length ; countEleLessThanOrEqual ( arr1 , m , arr2 , n ) ; } }

MAX = 100000 NEW_LINE def countEleLessThanOrEqual ( arr1 , m , arr2 , n ) : NEW_LINE INDENT hash = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ arr2 [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , MAX , 1 ) : NEW_LINE INDENT hash [ i ] = hash [ i ] + hash [ i - 1 ] NEW_LINE DEDENT for i in range ( m ) : NEW_LINE INDENT print ( hash [ arr1 [ i ] ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 1 , 2 , 3 , 4 , 7 , 9 ] NEW_LINE arr2 = [ 0 , 1 , 2 , 1 , 1 , 4 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE countEleLessThanOrEqual ( arr1 , m , arr2 , n ) NEW_LINE DEDENT


import java . util . * ; class GFG { static long pref [ ] = new long [ 100010 ] ; static boolean isPerfectSquare ( int x ) { int s = ( int ) Math . sqrt ( x ) ; return ( s * s == x ) ; } static int isNonFibonacci ( int n ) { if ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) return 0 ; else return n ; } static void compute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) ; } } static void printSum ( int L , int R ) { int sum = ( int ) ( pref [ R ] - pref [ L - 1 ] ) ; System . out . print ( sum + " ▁ " ) ; } public static void main ( String [ ] args ) { compute ( ) ; int Q = 2 ; int arr [ ] [ ] = { { 1 , 5 } , { 6 , 10 } } ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } }

from math import sqrt NEW_LINE pref = [ 0 ] * 100010 NEW_LINE def isPerfectSquare ( x ) : NEW_LINE INDENT s = int ( sqrt ( x ) ) NEW_LINE if ( s * s == x ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT def isNonFibonacci ( n ) : NEW_LINE INDENT x = 5 * n * n NEW_LINE if ( isPerfectSquare ( x + 4 ) or isPerfectSquare ( x - 4 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return n NEW_LINE DEDENT DEDENT def compute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + isNonFibonacci ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT sum = pref [ R ] - pref [ L - 1 ] NEW_LINE print ( sum , end = " ▁ " ) NEW_LINE DEDENT compute ( ) NEW_LINE Q = 2 NEW_LINE arr = [ [ 1 , 5 ] , [ 6 , 10 ] ] NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT


class GFG { static int trace ( int n , int m ) { int A [ ] [ ] = new int [ n ] [ m ] ; int B [ ] [ ] = new int [ n ] [ m ] ; int C [ ] [ ] = new int [ n ] [ m ] ; int cnt = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) { A [ i ] [ j ] = cnt ; cnt ++ ; } cnt = 1 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) { B [ j ] [ i ] = cnt ; cnt ++ ; } for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) C [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ; int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < m ; j ++ ) if ( i == j ) sum += C [ i ] [ j ] ; return sum ; } public static void main ( String [ ] args ) { int N = 3 , M = 3 ; System . out . println ( trace ( N , M ) ) ; } }

def trace ( n , m ) : NEW_LINE INDENT A = [ [ 0 for x in range ( m ) ] for y in range ( n ) ] ; NEW_LINE B = [ [ 0 for x in range ( m ) ] for y in range ( n ) ] ; NEW_LINE C = [ [ 0 for x in range ( m ) ] for y in range ( n ) ] ; NEW_LINE cnt = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT A [ i ] [ j ] = cnt ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT cnt = 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT B [ j ] [ i ] = cnt ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT C [ i ] [ j ] = A [ i ] [ j ] + B [ i ] [ j ] ; NEW_LINE DEDENT DEDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( m ) : NEW_LINE INDENT if ( i == j ) : NEW_LINE INDENT sum += C [ i ] [ j ] ; NEW_LINE DEDENT DEDENT DEDENT return sum ; NEW_LINE DEDENT N = 3 ; NEW_LINE M = 3 ; NEW_LINE print ( trace ( N , M ) ) ; NEW_LINE


import java . io . * ; class GFG { static int nCr ( int n , int k ) { int C [ ] [ ] = new int [ n + 1 ] [ k + 1 ] ; int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] ; } } return C [ n ] [ k ] ; } static double resultOfAllSubsets ( int arr [ ] , int N ) { double result = 0.0 ; int sum = 0 ; for ( int i = 0 ; i < N ; i ++ ) sum += arr [ i ] ; for ( int n = 1 ; n <= N ; n ++ ) result += ( double ) ( sum * ( nCr ( N - 1 , n - 1 ) ) ) / n ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 5 , 7 } ; int N = arr . length ; System . out . println ( resultOfAllSubsets ( arr , N ) ) ; } }

def nCr ( n , k ) : NEW_LINE INDENT C = [ [ 0 for i in range ( k + 1 ) ] for j in range ( n + 1 ) ] NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = C [ i - 1 ] [ j - 1 ] + C [ i - 1 ] [ j ] NEW_LINE DEDENT DEDENT DEDENT return C [ n ] [ k ] NEW_LINE DEDENT def resultOfAllSubsets ( arr , N ) : NEW_LINE INDENT result = 0.0 NEW_LINE sum = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT for n in range ( 1 , N + 1 ) : NEW_LINE INDENT result += ( sum * ( nCr ( N - 1 , n - 1 ) ) ) / n NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 2 , 3 , 5 , 7 ] NEW_LINE N = len ( arr ) NEW_LINE print ( resultOfAllSubsets ( arr , N ) ) NEW_LINE


import java . io . * ; class GFG { static void solveWordWrap ( int arr [ ] , int n , int k ) { int i , j ; int currlen ; int cost ; int dp [ ] = new int [ n ] ; int ans [ ] = new int [ n ] ; dp [ n - 1 ] = 0 ; ans [ n - 1 ] = n - 1 ; for ( i = n - 2 ; i >= 0 ; i -- ) { currlen = - 1 ; dp [ i ] = Integer . MAX_VALUE ; for ( j = i ; j < n ; j ++ ) { currlen += ( arr [ j ] + 1 ) ; if ( currlen > k ) break ; if ( j == n - 1 ) cost = 0 ; else cost = ( k - currlen ) * ( k - currlen ) + dp [ j + 1 ] ; if ( cost < dp [ i ] ) { dp [ i ] = cost ; ans [ i ] = j ; } } } i = 0 ; while ( i < n ) { System . out . print ( ( i + 1 ) + " ▁ " + ( ans [ i ] + 1 ) + " ▁ " ) ; i = ans [ i ] + 1 ; } } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 2 , 5 } ; int n = arr . length ; int M = 6 ; solveWordWrap ( arr , n , M ) ; } }

import sys NEW_LINE def solveWordWrap ( arr , n , k ) : NEW_LINE INDENT dp = [ 0 ] * n NEW_LINE ans = [ 0 ] * n NEW_LINE dp [ n - 1 ] = 0 NEW_LINE ans [ n - 1 ] = n - 1 NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT currlen = - 1 NEW_LINE dp [ i ] = sys . maxsize NEW_LINE for j in range ( i , n ) : NEW_LINE INDENT currlen += ( arr [ j ] + 1 ) NEW_LINE if ( currlen > k ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( j == n - 1 ) : NEW_LINE INDENT cost = 0 NEW_LINE DEDENT else : NEW_LINE INDENT cost = ( ( k - currlen ) * ( k - currlen ) + dp [ j + 1 ] ) NEW_LINE DEDENT if ( cost < dp [ i ] ) : NEW_LINE INDENT dp [ i ] = cost NEW_LINE ans [ i ] = j NEW_LINE DEDENT DEDENT DEDENT i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT print ( i + 1 , ans [ i ] + 1 , end = " ▁ " ) NEW_LINE i = ans [ i ] + 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 2 , 2 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE M = 6 NEW_LINE solveWordWrap ( arr , n , M ) NEW_LINE DEDENT


import java . util . Arrays ; import java . io . * ; class GFG { static int N = 20 ; static int [ ] fact = new int [ N ] ; static void pre ( ) { fact [ 0 ] = 1 ; for ( int i = 1 ; i < N ; i ++ ) { fact [ i ] = i * fact [ i - 1 ] ; } } static int CountPermutation ( int a [ ] , int n ) { int ways = 1 ; Arrays . sort ( a ) ; int size = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( a [ i ] == a [ i - 1 ] ) { size ++ ; } else { ways *= fact [ size ] ; size = 1 ; } } ways *= fact [ size ] ; return ways ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 4 , 4 , 2 , 4 } ; int n = a . length ; pre ( ) ; System . out . println ( CountPermutation ( a , n ) ) ; } }

N = 20 NEW_LINE fact = [ 0 ] * N ; NEW_LINE def pre ( ) : NEW_LINE INDENT fact [ 0 ] = 1 ; NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT fact [ i ] = i * fact [ i - 1 ] ; NEW_LINE DEDENT DEDENT def CountPermutation ( a , n ) : NEW_LINE INDENT ways = 1 ; NEW_LINE a . sort ( ) ; NEW_LINE size = 1 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( a [ i ] == a [ i - 1 ] ) : NEW_LINE INDENT size += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ways *= fact [ size ] ; NEW_LINE size = 1 ; NEW_LINE DEDENT DEDENT ways *= fact [ size ] ; NEW_LINE return ways ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 1 , 2 , 4 , 4 , 2 , 4 ] ; NEW_LINE n = len ( a ) ; NEW_LINE pre ( ) ; NEW_LINE print ( CountPermutation ( a , n ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int [ ] fact = new int [ 14 ] ; static int size = 1 ; static void preCompute ( int N ) { fact [ 0 ] = 1 ; for ( int i = 1 ; fact [ i - 1 ] <= N ; i ++ ) { fact [ i ] = ( fact [ i - 1 ] * i ) ; size ++ ; } } static void findMin ( int N ) { preCompute ( N ) ; int originalN = N ; Vector < Integer > ans = new Vector < Integer > ( ) ; for ( int i = size - 1 ; i >= 0 ; i -- ) { while ( N >= fact [ i ] ) { N -= fact [ i ] ; ans . add ( fact [ i ] ) ; } } System . out . print ( ans . size ( ) + " \n " ) ; for ( int i = 0 ; i < ans . size ( ) ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 27 ; findMin ( n ) ; } }

fact = [ 0 ] * 14 NEW_LINE size = 1 NEW_LINE def preCompute ( N ) : NEW_LINE INDENT global size NEW_LINE fact [ 0 ] = 1 NEW_LINE i = 1 NEW_LINE while fact [ i - 1 ] <= N : NEW_LINE INDENT fact [ i ] = fact [ i - 1 ] * i NEW_LINE size += 1 NEW_LINE i += 1 NEW_LINE DEDENT DEDENT def findMin ( N ) : NEW_LINE INDENT preCompute ( N ) NEW_LINE originalN = N NEW_LINE ans = [ ] NEW_LINE for i in range ( size - 1 , - 1 , - 1 ) : NEW_LINE INDENT while ( N >= fact [ i ] ) : NEW_LINE INDENT N -= fact [ i ] NEW_LINE ans . append ( fact [ i ] ) NEW_LINE DEDENT DEDENT print ( len ( ans ) ) NEW_LINE for i in ans : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 27 NEW_LINE findMin ( n ) NEW_LINE


import java . text . * ; class GFG { static double f ( double x , double y ) { double v = y - 2 * x * x + 1 ; return v ; } static double predict ( double x , double y , double h ) { double y1p = y + h * f ( x , y ) ; return y1p ; } static double correct ( double x , double y , double x1 , double y1 , double h ) { double e = 0.00001 ; double y1c = y1 ; do { y1 = y1c ; y1c = y + 0.5 * h * ( f ( x , y ) + f ( x1 , y1 ) ) ; } while ( Math . abs ( y1c - y1 ) > e ) ; return y1c ; } static void printFinalValues ( double x , double xn , double y , double h ) { while ( x < xn ) { double x1 = x + h ; double y1p = predict ( x , y , h ) ; double y1c = correct ( x , y , x1 , y1p , h ) ; x = x1 ; y = y1c ; } DecimalFormat df = new DecimalFormat ( " # . # # # # # " ) ; System . out . println ( " The ▁ final ▁ value ▁ of ▁ y ▁ at ▁ x ▁ = ▁ " + x + " ▁ is ▁ : ▁ " + df . format ( y ) ) ; } public static void main ( String [ ] args ) { double x = 0 , y = 0.5 ; double xn = 1 ; double h = 0.2 ; printFinalValues ( x , xn , y , h ) ; } }

def f ( x , y ) : NEW_LINE INDENT v = y - 2 * x * x + 1 ; NEW_LINE return v ; NEW_LINE DEDENT def predict ( x , y , h ) : NEW_LINE INDENT y1p = y + h * f ( x , y ) ; NEW_LINE return y1p ; NEW_LINE DEDENT def correct ( x , y , x1 , y1 , h ) : NEW_LINE INDENT e = 0.00001 ; NEW_LINE y1c = y1 ; NEW_LINE while ( abs ( y1c - y1 ) > e + 1 ) : NEW_LINE INDENT y1 = y1c ; NEW_LINE y1c = y + 0.5 * h * ( f ( x , y ) + f ( x1 , y1 ) ) ; NEW_LINE DEDENT return y1c ; NEW_LINE DEDENT def printFinalValues ( x , xn , y , h ) : NEW_LINE INDENT while ( x < xn ) : NEW_LINE INDENT x1 = x + h ; NEW_LINE y1p = predict ( x , y , h ) ; NEW_LINE y1c = correct ( x , y , x1 , y1p , h ) ; NEW_LINE x = x1 ; NEW_LINE y = y1c ; NEW_LINE DEDENT print ( " The ▁ final ▁ value ▁ of ▁ y ▁ at ▁ x ▁ = " , int ( x ) , " is ▁ : " , y ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x = 0 ; y = 0.5 ; NEW_LINE xn = 1 ; NEW_LINE h = 0.2 ; NEW_LINE printFinalValues ( x , xn , y , h ) ; NEW_LINE DEDENT


class gfg { static int findMinSwaps ( int arr [ ] , int n ) { int noOfZeroes [ ] = new int [ n ] ; int i , count = 0 ; noOfZeroes [ n - 1 ] = 1 - arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) { noOfZeroes [ i ] = noOfZeroes [ i + 1 ] ; if ( arr [ i ] == 0 ) noOfZeroes [ i ] ++ ; } for ( i = 0 ; i < n ; i ++ ) { if ( arr [ i ] == 1 ) count += noOfZeroes [ i ] ; } return count ; } public static void main ( String args [ ] ) { int ar [ ] = { 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 } ; System . out . println ( findMinSwaps ( ar , ar . length ) ) ; } }

def findMinSwaps ( arr , n ) : NEW_LINE INDENT noOfZeroes = [ 0 ] * n NEW_LINE count = 0 NEW_LINE noOfZeroes [ n - 1 ] = 1 - arr [ n - 1 ] NEW_LINE for i in range ( n - 2 , - 1 , - 1 ) : NEW_LINE INDENT noOfZeroes [ i ] = noOfZeroes [ i + 1 ] NEW_LINE if ( arr [ i ] == 0 ) : NEW_LINE INDENT noOfZeroes [ i ] = noOfZeroes [ i ] + 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT count = count + noOfZeroes [ i ] NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 0 , 0 , 1 , 0 , 1 , 0 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMinSwaps ( arr , n ) ) NEW_LINE


import java . io . * ; class GFG { static void evaluate ( int n ) { if ( n == 1 || n == 2 ) System . out . println ( " No ▁ Pythagoras ▁ " + " Triplet ▁ exists " ) ; else if ( n % 2 == 0 ) { int var = 1 * n * n / 4 ; System . out . print ( " Pythagoras ▁ Triplets ▁ " + " exist ▁ i . e . ▁ " ) ; System . out . print ( n + " ▁ " ) ; System . out . print ( var - 1 + " ▁ " ) ; System . out . println ( var + 1 + " ▁ " ) ; } else if ( n % 2 != 0 ) { int var = 1 * n * n + 1 ; System . out . print ( " Pythagoras ▁ Triplets ▁ " + " exist ▁ i . e . ▁ " ) ; System . out . print ( n + " ▁ " ) ; System . out . print ( var / 2 - 1 + " ▁ " ) ; System . out . println ( var / 2 + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 22 ; evaluate ( n ) ; } }

def evaluate ( n ) : NEW_LINE INDENT if ( n == 1 or n == 2 ) : NEW_LINE INDENT print ( " No ▁ Pythagoras " + " ▁ Triplet ▁ exists " ) ; NEW_LINE DEDENT elif ( n % 2 == 0 ) : NEW_LINE INDENT var = n * n / 4 ; NEW_LINE print ( " Pythagoras ▁ Triplets " + " ▁ exist ▁ i . e . ▁ " , end = " " ) ; NEW_LINE print ( int ( n ) , " ▁ " , int ( var - 1 ) , " ▁ " , int ( var + 1 ) ) ; NEW_LINE DEDENT elif ( n % 2 != 0 ) : NEW_LINE INDENT var = n * n + 1 ; NEW_LINE print ( " Pythagoras ▁ Triplets ▁ " + " exist ▁ i . e . ▁ " , end = " " ) ; NEW_LINE print ( int ( n ) , " ▁ " , int ( var / 2 - 1 ) , " ▁ " , int ( var / 2 ) ) ; NEW_LINE DEDENT DEDENT n = 22 ; NEW_LINE evaluate ( n ) ; NEW_LINE


import java . util . * ; class GFG { static void min_visible_bottles ( int [ ] arr , int n ) { HashMap < Integer , Integer > mp = new HashMap < Integer , Integer > ( ) ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( mp . containsKey ( arr [ i ] ) ) { mp . put ( arr [ i ] , mp . get ( arr [ i ] ) + 1 ) ; } else { mp . put ( arr [ i ] , 1 ) ; } ans = Math . max ( ans , mp . get ( arr [ i ] ) ) ; } System . out . print ( " Minimum ▁ number ▁ of ▁ " + " Visible ▁ Bottles ▁ are : ▁ " + ans + " \n " ) ; } public static void main ( String [ ] args ) { int n = 8 ; int arr [ ] = { 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 } ; min_visible_bottles ( arr , n ) ; } }

def min_visible_bottles ( arr , n ) : NEW_LINE INDENT m = dict ( ) NEW_LINE ans = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT m [ arr [ i ] ] = m . get ( arr [ i ] , 0 ) + 1 NEW_LINE ans = max ( ans , m [ arr [ i ] ] ) NEW_LINE DEDENT print ( " Minimum ▁ number ▁ of " , " Visible ▁ Bottles ▁ are : ▁ " , ans ) NEW_LINE DEDENT n = 8 NEW_LINE arr = [ 1 , 1 , 2 , 3 , 4 , 5 , 5 , 4 ] NEW_LINE min_visible_bottles ( arr , n ) NEW_LINE


class GFG { static int findMinimumSubsequences ( String A , String B ) { int numberOfSubsequences = 1 ; int sizeOfB = B . length ( ) ; int sizeOfA = A . length ( ) ; int inf = 1000000 ; int [ ] [ ] next = new int [ 26 ] [ sizeOfB ] ; for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = 0 ; j < sizeOfB ; j ++ ) { next [ i ] [ j ] = inf ; } } for ( int i = 0 ; i < sizeOfB ; i ++ ) { next [ B . charAt ( i ) - ' a ' ] [ i ] = i ; } for ( int i = 0 ; i < 26 ; i ++ ) { for ( int j = sizeOfB - 2 ; j >= 0 ; j -- ) { if ( next [ i ] [ j ] == inf ) { next [ i ] [ j ] = next [ i ] [ j + 1 ] ; } } } int pos = 0 ; int i = 0 ; while ( i < sizeOfA ) { if ( pos == 0 && next [ A . charAt ( i ) - ' a ' ] [ pos ] == inf ) { numberOfSubsequences = - 1 ; break ; } else if ( pos < sizeOfB && next [ A . charAt ( i ) - ' a ' ] [ pos ] < inf ) { int nextIndex = next [ A . charAt ( i ) - ' a ' ] [ pos ] + 1 ; pos = nextIndex ; i ++ ; } else { numberOfSubsequences ++ ; pos = 0 ; } } return numberOfSubsequences ; } public static void main ( String [ ] args ) { String A = " aacbe " ; String B = " aceab " ; System . out . print ( findMinimumSubsequences ( A , B ) ) ; } }

def findMinimumSubsequences ( A , B ) : NEW_LINE INDENT numberOfSubsequences = 1 NEW_LINE sizeOfB = len ( B ) NEW_LINE sizeOfA = len ( A ) NEW_LINE inf = 1000000 NEW_LINE next = [ [ inf for i in range ( sizeOfB ) ] for i in range ( 26 ) ] NEW_LINE for i in range ( sizeOfB ) : NEW_LINE INDENT next [ ord ( B [ i ] ) - ord ( ' a ' ) ] [ i ] = i NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT for j in range ( sizeOfB - 2 , - 1 , - 1 ) : NEW_LINE INDENT if ( next [ i ] [ j ] == inf ) : NEW_LINE INDENT next [ i ] [ j ] = next [ i ] [ j + 1 ] NEW_LINE DEDENT DEDENT DEDENT pos = 0 NEW_LINE i = 0 NEW_LINE while ( i < sizeOfA ) : NEW_LINE INDENT if ( pos == 0 and next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] == inf ) : NEW_LINE INDENT numberOfSubsequences = - 1 NEW_LINE break NEW_LINE DEDENT elif ( pos < sizeOfB and next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] < inf ) : NEW_LINE INDENT nextIndex = next [ ord ( A [ i ] ) - ord ( ' a ' ) ] [ pos ] + 1 NEW_LINE pos = nextIndex NEW_LINE i += 1 NEW_LINE DEDENT else : NEW_LINE INDENT numberOfSubsequences += 1 NEW_LINE pos = 0 NEW_LINE DEDENT DEDENT return numberOfSubsequences NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT A = " aacbe " NEW_LINE B = " aceab " NEW_LINE print ( findMinimumSubsequences ( A , B ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static void printRoots ( int n ) { double theta = 3.14 * 2 / n ; for ( int k = 0 ; k < n ; k ++ ) { double real = Math . cos ( k * theta ) ; double img = Math . sin ( k * theta ) ; System . out . println ( real ) ; if ( img >= 0 ) System . out . println ( " ▁ + ▁ i ▁ " ) ; else System . out . println ( " ▁ - ▁ i ▁ " ) ; System . out . println ( Math . abs ( img ) ) ; } } public static void main ( String [ ] args ) { printRoots ( 1 ) ; printRoots ( 2 ) ; printRoots ( 3 ) ; } }

import math NEW_LINE def printRoots ( n ) : NEW_LINE INDENT theta = math . pi * 2 / n NEW_LINE for k in range ( 0 , n ) : NEW_LINE INDENT real = math . cos ( k * theta ) NEW_LINE img = math . sin ( k * theta ) NEW_LINE print ( real , end = " ▁ " ) NEW_LINE if ( img >= 0 ) : NEW_LINE INDENT print ( " ▁ + ▁ i ▁ " , end = " ▁ " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " ▁ - ▁ i ▁ " , end = " ▁ " ) NEW_LINE DEDENT print ( abs ( img ) ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT printRoots ( 1 ) NEW_LINE printRoots ( 2 ) NEW_LINE printRoots ( 3 ) NEW_LINE DEDENT


class GFG { static int INT_MAX = Integer . MAX_VALUE ; static int min_element ( int [ ] A , int N ) { int min = A [ 0 ] ; int i ; for ( i = 1 ; i < N ; i ++ ) { if ( min > A [ i ] ) { min = A [ i ] ; } } return min ; } static int countPartitions ( int [ ] A , int N ) { int count = 0 ; int i , j ; for ( i = 0 ; i < N ; i ++ ) { int min_elem = min_element ( A , N ) ; if ( min_elem == INT_MAX ) break ; count ++ ; for ( j = 0 ; j < N ; j ++ ) { if ( A [ j ] % min_elem == 0 ) A [ j ] = INT_MAX ; } } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 } ; int N = arr . length ; System . out . println ( countPartitions ( arr , N ) ) ; } }

import sys NEW_LINE INT_MAX = sys . maxsize ; NEW_LINE def countPartitions ( A , N ) : NEW_LINE INDENT count = 0 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT min_elem = min ( A ) ; NEW_LINE if ( min_elem == INT_MAX ) : NEW_LINE INDENT break ; NEW_LINE DEDENT count += 1 ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT if ( A [ i ] % min_elem == 0 ) : NEW_LINE INDENT A [ i ] = INT_MAX ; NEW_LINE DEDENT DEDENT DEDENT return count ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 7 , 6 , 5 , 4 , 3 , 2 , 2 , 3 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE print ( countPartitions ( arr , N ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static int [ ] pref = new int [ 100001 ] ; static int checkPalindrome ( int num ) { String str = String . valueOf ( num ) ; int l = 0 , r = str . length ( ) - 1 ; while ( l < r ) { if ( str . charAt ( l ) != str . charAt ( r ) ) { return 0 ; } l ++ ; r -- ; } return num ; } static void preCompute ( ) { for ( int i = 1 ; i <= 100000 ; ++ i ) { pref [ i ] = pref [ i - 1 ] + checkPalindrome ( i ) ; } } static void printSum ( int L , int R ) { System . out . print ( pref [ R ] - pref [ L - 1 ] + " \n " ) ; } static void printSumPalindromic ( int arr [ ] [ ] , int Q ) { preCompute ( ) ; for ( int i = 0 ; i < Q ; i ++ ) { printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) ; } } public static void main ( String [ ] args ) { int Q = 2 ; int arr [ ] [ ] = { { 10 , 13 } , { 12 , 21 } } ; printSumPalindromic ( arr , Q ) ; } }

pref = [ 0 ] * 100001 NEW_LINE def checkPalindrome ( num ) : NEW_LINE INDENT strr = str ( num ) NEW_LINE l = 0 NEW_LINE r = len ( strr ) - 1 NEW_LINE while ( l < r ) : NEW_LINE INDENT if ( strr [ l ] != strr [ r ] ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT l += 1 NEW_LINE r -= 1 NEW_LINE DEDENT return num NEW_LINE DEDENT def preCompute ( ) : NEW_LINE INDENT for i in range ( 1 , 100001 ) : NEW_LINE INDENT pref [ i ] = pref [ i - 1 ] + checkPalindrome ( i ) NEW_LINE DEDENT DEDENT def printSum ( L , R ) : NEW_LINE INDENT print ( pref [ R ] - pref [ L - 1 ] ) NEW_LINE DEDENT def printSumPalindromic ( arr , Q ) : NEW_LINE INDENT preCompute ( ) NEW_LINE for i in range ( Q ) : NEW_LINE INDENT printSum ( arr [ i ] [ 0 ] , arr [ i ] [ 1 ] ) NEW_LINE DEDENT DEDENT Q = 2 NEW_LINE arr = [ [ 10 , 13 ] , [ 12 , 21 ] ] NEW_LINE printSumPalindromic ( arr , Q ) NEW_LINE


class GFG { static int right = 2 ; static int left = 4 ; static int [ ] [ ] dp = new int [ left ] [ right ] ; static int findSubarraySum ( int ind , int flips , int n , int [ ] a , int k ) { if ( flips > k ) return ( int ) ( - 1e9 ) ; if ( ind == n ) return 0 ; if ( dp [ ind ] [ flips ] != - 1 ) return dp [ ind ] [ flips ] ; int ans = 0 ; ans = Math . max ( 0 , a [ ind ] + findSubarraySum ( ind + 1 , flips , n , a , k ) ) ; ans = Math . max ( ans , - a [ ind ] + findSubarraySum ( ind + 1 , flips + 1 , n , a , k ) ) ; return dp [ ind ] [ flips ] = ans ; } static int findMaxSubarraySum ( int [ ] a , int n , int k ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < k + 1 ; j ++ ) dp [ i ] [ j ] = - 1 ; int ans = ( int ) ( - 1e9 ) ; for ( int i = 0 ; i < n ; i ++ ) ans = Math . max ( ans , findSubarraySum ( i , 0 , n , a , k ) ) ; return ans ; } public static void main ( String [ ] args ) { int [ ] a = { - 1 , - 2 , - 100 , - 10 } ; int n = a . length ; int k = 1 ; System . out . println ( findMaxSubarraySum ( a , n , k ) ) ; } }

import numpy as np NEW_LINE right = 3 ; NEW_LINE left = 6 ; NEW_LINE dp = np . ones ( ( left , right ) ) NEW_LINE dp = - 1 * dp NEW_LINE def findSubarraySum ( ind , flips , n , a , k ) : NEW_LINE INDENT if ( flips > k ) : NEW_LINE INDENT return - 1e9 ; NEW_LINE DEDENT if ( ind == n ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( dp [ ind ] [ flips ] != - 1 ) : NEW_LINE INDENT return dp [ ind ] [ flips ] ; NEW_LINE DEDENT ans = 0 ; NEW_LINE ans = max ( 0 , a [ ind ] + findSubarraySum ( ind + 1 , flips , n , a , k ) ) ; NEW_LINE ans = max ( ans , - a [ ind ] + findSubarraySum ( ind + 1 , flips + 1 , n , a , k ) ) ; NEW_LINE dp [ ind ] [ flips ] = ans ; NEW_LINE return dp [ ind ] [ flips ] ; NEW_LINE DEDENT def findMaxSubarraySum ( a , n , k ) : NEW_LINE INDENT ans = - 1e9 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = max ( ans , findSubarraySum ( i , 0 , n , a , k ) ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ - 1 , - 2 , - 100 , - 10 ] ; NEW_LINE n = len ( a ) ; NEW_LINE k = 1 ; NEW_LINE print ( findMaxSubarraySum ( a , n , k ) ) ; NEW_LINE DEDENT


import java . util . * ; class GFG { static class pair { int first , second ; public pair ( int first , int second ) { this . first = first ; this . second = second ; } } static boolean checkconv ( int a [ ] , int b [ ] , int n ) { int [ ] c = new int [ n ] ; int flag = 0 ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = b [ i ] - a [ i ] ; } Vector < pair > idxs = new Vector < pair > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( c [ i ] != 0 ) idxs . add ( new pair ( i , c [ i ] ) ) ; } for ( int i = 0 ; i < idxs . size ( ) - 1 ; i ++ ) { if ( idxs . get ( i + 1 ) . first - idxs . get ( i ) . first != 1 || idxs . get ( i + 1 ) . second != idxs . get ( i ) . second ) { flag = 1 ; break ; } } return flag == 1 ? false : true ; } static int diffofarrays ( int a [ ] , int b [ ] , int n ) { int [ ] c = new int [ n ] ; int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) { c [ i ] = b [ i ] - a [ i ] ; } for ( int i = 0 ; i < n ; i ++ ) { if ( c [ i ] != 0 ) { ans = c [ i ] ; break ; } } return ans ; } public static void main ( String [ ] args ) { int A [ ] = { 3 , 7 , 1 , 4 , 0 , 2 , 2 } ; int B [ ] = { 3 , 7 , 3 , 6 , 2 , 2 , 2 } ; int arr_size = A . length ; if ( checkconv ( A , B , arr_size ) ) { System . out . print ( diffofarrays ( A , B , arr_size ) + " \n " ) ; } else System . out . print ( " NA " + " \n " ) ; } }

def checkconv ( a , b , n ) : NEW_LINE INDENT c = [ 0 ] * n ; flag = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ i ] = b [ i ] - a [ i ] ; NEW_LINE DEDENT idxs = [ ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( c [ i ] != 0 ) : NEW_LINE INDENT idxs . append ( ( i , c [ i ] ) ) ; NEW_LINE DEDENT DEDENT for i in range ( len ( idxs ) - 1 ) : NEW_LINE INDENT if ( idxs [ i + 1 ] [ 0 ] - idxs [ i ] [ 0 ] != 1 or idxs [ i + 1 ] [ 1 ] != idxs [ i ] [ 1 ] ) : NEW_LINE INDENT flag = 1 ; NEW_LINE break ; NEW_LINE DEDENT DEDENT return not flag ; NEW_LINE DEDENT def diffofarrays ( a , b , n ) : NEW_LINE INDENT c = [ 0 ] * n ; NEW_LINE ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT c [ i ] = b [ i ] - a [ i ] ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT if ( c [ i ] != 0 ) : NEW_LINE INDENT ans = c [ i ] ; NEW_LINE break ; NEW_LINE DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 7 , 1 , 4 , 0 , 2 , 2 ] ; NEW_LINE B = [ 3 , 7 , 3 , 6 , 2 , 2 , 2 ] ; NEW_LINE arr_size = len ( A ) ; NEW_LINE if ( checkconv ( A , B , arr_size ) ) : NEW_LINE INDENT print ( diffofarrays ( A , B , arr_size ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NA " ) ; NEW_LINE DEDENT DEDENT


public class GFG { public static int getRightMin ( int arr [ ] , int n ) { int min = arr [ 0 ] ; int rightMin = 0 ; int i = 1 ; while ( i < n ) { if ( arr [ i ] == min ) rightMin = i ; i *= 2 ; } i = rightMin + 1 ; while ( i < n && arr [ i ] == min ) { rightMin = i ; i ++ ; } return rightMin ; } public static int getLeftMax ( int arr [ ] , int n ) { int max = arr [ n - 1 ] ; int leftMax = n - 1 ; int i = n - 2 ; while ( i > 0 ) { if ( arr [ i ] == max ) leftMax = i ; i /= 2 ; } i = leftMax - 1 ; while ( i >= 0 && arr [ i ] == max ) { leftMax = i ; i -- ; } return leftMax ; } public static void main ( String [ ] args ) { int arr [ ] = { 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 } ; int n = arr . length ; System . out . println ( " Minimum ▁ left ▁ : ▁ " + 0 ) ; System . out . println ( " Minimum ▁ right ▁ : ▁ " + getRightMin ( arr , n ) ) ; System . out . println ( " Maximum ▁ left ▁ : ▁ " + getLeftMax ( arr , n ) ) ; System . out . println ( " Maximum ▁ right ▁ : ▁ " + ( n - 1 ) ) ; } }

def getRightMin ( arr , n ) : NEW_LINE INDENT min = arr [ 0 ] NEW_LINE rightMin = 0 NEW_LINE i = 1 NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( arr [ i ] == min ) : NEW_LINE INDENT rightMin = i NEW_LINE DEDENT i *= 2 NEW_LINE DEDENT i = rightMin + 1 NEW_LINE while ( i < n and arr [ i ] == min ) : NEW_LINE INDENT rightMin = i NEW_LINE i += 1 NEW_LINE DEDENT return rightMin NEW_LINE DEDENT def getLeftMax ( arr , n ) : NEW_LINE INDENT max = arr [ n - 1 ] NEW_LINE leftMax = n - 1 NEW_LINE i = n - 2 NEW_LINE while ( i > 0 ) : NEW_LINE INDENT if ( arr [ i ] == max ) : NEW_LINE INDENT leftMax = i NEW_LINE DEDENT i = int ( i / 2 ) NEW_LINE DEDENT i = leftMax - 1 NEW_LINE while ( i >= 0 and arr [ i ] == max ) : NEW_LINE INDENT leftMax = i NEW_LINE i -= 1 NEW_LINE DEDENT return leftMax NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 0 , 0 , 1 , 2 , 5 , 5 , 6 , 8 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Minimum ▁ left ▁ : " , 0 ) NEW_LINE print ( " Minimum ▁ right ▁ : " , getRightMin ( arr , n ) ) NEW_LINE print ( " Maximum ▁ left ▁ : " , getLeftMax ( arr , n ) ) NEW_LINE print ( " Maximum ▁ right ▁ : " , ( n - 1 ) ) NEW_LINE DEDENT


import java . io . * ; class GFG { static int MaxNumbers ( int a [ ] , int n ) { int [ ] fre = { 0 , 0 , 0 } ; for ( int i = 0 ; i < n ; i ++ ) { a [ i ] %= 3 ; fre [ a [ i ] ] ++ ; } int ans = fre [ 0 ] ; int k = Math . min ( fre [ 1 ] , fre [ 2 ] ) ; ans += k ; fre [ 1 ] -= k ; fre [ 2 ] -= k ; ans += fre [ 1 ] / 3 + fre [ 2 ] / 3 ; return ans ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 } ; int n = a . length ; System . out . println ( MaxNumbers ( a , n ) ) ; } }

def MaxNumbers ( a , n ) : NEW_LINE INDENT fre = [ 0 for i in range ( 3 ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT a [ i ] %= 3 NEW_LINE fre [ a [ i ] ] += 1 NEW_LINE DEDENT ans = fre [ 0 ] NEW_LINE k = min ( fre [ 1 ] , fre [ 2 ] ) NEW_LINE ans += k NEW_LINE fre [ 1 ] -= k NEW_LINE fre [ 2 ] -= k NEW_LINE ans += fre [ 1 ] // 3 + fre [ 2 ] // 3 NEW_LINE return ans NEW_LINE DEDENT a = [ 1 , 4 , 10 , 7 , 11 , 2 , 8 , 5 , 9 ] NEW_LINE n = len ( a ) NEW_LINE print ( MaxNumbers ( a , n ) ) NEW_LINE