id
stringlengths
2
6
java
stringlengths
48
5.92k
python
stringlengths
33
11.1k
T39900
import java . io . * ; import java . util . * ; class GfG { static int minimumSwaps ( int [ ] arr ) { int count = 0 ; int i = 0 ; while ( i < arr . length ) { if ( arr [ i ] != i + 1 ) { while ( arr [ i ] != i + 1 ) { int temp = 0 ; temp = arr [ arr [ i ] - 1 ] ; arr [ arr [ i ] - 1 ] = arr [ i ] ; arr [ i ] = temp ; count ++ ; } } i ++ ; } return count ; } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 4 , 1 , 5 } ; System . out . println ( minimumSwaps ( arr ) ) ; } }
def minimumSwaps ( arr ) : NEW_LINE INDENT count = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < len ( arr ) ) : NEW_LINE INDENT if ( arr [ i ] != i + 1 ) : NEW_LINE INDENT while ( arr [ i ] != i + 1 ) : NEW_LINE INDENT temp = 0 ; NEW_LINE temp = arr [ arr [ i ] - 1 ] ; NEW_LINE arr [ arr [ i ] - 1 ] = arr [ i ] ; NEW_LINE arr [ i ] = temp ; NEW_LINE count += 1 ; NEW_LINE DEDENT DEDENT i += 1 ; NEW_LINE DEDENT return count ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 4 , 1 , 5 ] ; NEW_LINE print ( minimumSwaps ( arr ) ) ; NEW_LINE DEDENT
T39901
import java . util . HashSet ; import javafx . util . Pair ; class GFG { static int getPairs ( int arr [ ] , int n ) { HashSet < Pair > h = new HashSet < Pair > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) { Pair < Integer , Integer > p = new Pair < > ( arr [ i ] , arr [ j ] ) ; h . add ( p ) ; } } return h . size ( ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 } ; int n = arr . length ; System . out . println ( getPairs ( arr , n ) ) ; } }
def getPairs ( arr , n ) : NEW_LINE INDENT h = set ( ) NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT h . add ( ( arr [ i ] , arr [ j ] ) ) ; NEW_LINE DEDENT DEDENT return len ( h ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 2 , 4 , 2 , 5 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( getPairs ( arr , n ) ) NEW_LINE DEDENT
T39902
public class GFG { static int min ( int x , int y , int z ) { if ( x < y ) return ( x < z ) ? x : z ; else return ( y < z ) ? y : z ; } static int minCost ( int cost [ ] [ ] , int m , int n ) { if ( n < 0 || m < 0 ) return Integer . MAX_VALUE ; else if ( m == 0 && n == 0 ) return cost [ m ] [ n ] ; else return cost [ m ] [ n ] + min ( minCost ( cost , m - 1 , n - 1 ) , minCost ( cost , m - 1 , n ) , minCost ( cost , m , n - 1 ) ) ; } public static void main ( String args [ ] ) { int cost [ ] [ ] = { { 1 , 2 , 3 } , { 4 , 8 , 2 } , { 1 , 5 , 3 } } ; System . out . print ( minCost ( cost , 2 , 2 ) ) ; } }
R = 3 NEW_LINE C = 3 NEW_LINE import sys NEW_LINE def minCost ( cost , m , n ) : NEW_LINE INDENT if ( n < 0 or m < 0 ) : NEW_LINE INDENT return sys . maxsize NEW_LINE DEDENT elif ( m == 0 and n == 0 ) : NEW_LINE INDENT return cost [ m ] [ n ] NEW_LINE DEDENT else : NEW_LINE INDENT return cost [ m ] [ n ] + min ( minCost ( cost , m - 1 , n - 1 ) , minCost ( cost , m - 1 , n ) , minCost ( cost , m , n - 1 ) ) NEW_LINE DEDENT DEDENT def min ( x , y , z ) : NEW_LINE INDENT if ( x < y ) : NEW_LINE INDENT return x if ( x < z ) else z NEW_LINE DEDENT else : NEW_LINE INDENT return y if ( y < z ) else z NEW_LINE DEDENT DEDENT cost = [ [ 1 , 2 , 3 ] , [ 4 , 8 , 2 ] , [ 1 , 5 , 3 ] ] NEW_LINE print ( minCost ( cost , 2 , 2 ) ) NEW_LINE
T39903
class GFG { static String recursiveFun ( int n ) { if ( n == 1 ) return " int " ; return " gcd ( int , ▁ " + recursiveFun ( n - 1 ) + " ) " ; } public static void main ( String [ ] arg ) { int n = 5 ; System . out . println ( recursiveFun ( n ) ) ; } }
def recursiveFun ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return " int " NEW_LINE DEDENT return " gcd ( int , ▁ " + recursiveFun ( n - 1 ) + " ) " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 5 NEW_LINE print ( recursiveFun ( n ) ) NEW_LINE DEDENT
T39904
class GFG { static boolean isPalindrome ( String str ) { int len = str . length ( ) ; for ( int i = 0 ; i < len / 2 ; i ++ ) { if ( str . charAt ( i ) != str . charAt ( len - 1 - i ) ) return false ; } return true ; } static boolean createStringAndCheckPalindrome ( int N ) { String sub = " " + N , res_str = " " ; int sum = 0 ; while ( N > 0 ) { int digit = N % 10 ; sum += digit ; N = N / 10 ; } while ( res_str . length ( ) < sum ) res_str += sub ; if ( res_str . length ( ) > sum ) res_str = res_str . substring ( 0 , sum ) ; if ( isPalindrome ( res_str ) ) return true ; return false ; } public static void main ( String args [ ] ) { int N = 10101 ; if ( createStringAndCheckPalindrome ( N ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
def isPalindrome ( s ) : NEW_LINE INDENT l = len ( s ) NEW_LINE for i in range ( l // 2 ) : NEW_LINE INDENT if ( s [ i ] != s [ l - 1 - i ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def createStringAndCheckPalindrome ( N ) : NEW_LINE INDENT sub = " " + chr ( N ) NEW_LINE res_str = " " NEW_LINE sum = 0 NEW_LINE while ( N > 0 ) : NEW_LINE INDENT digit = N % 10 NEW_LINE sum += digit NEW_LINE N = N // 10 NEW_LINE DEDENT while ( len ( res_str ) < sum ) : NEW_LINE INDENT res_str += sub NEW_LINE DEDENT if ( len ( res_str ) > sum ) : NEW_LINE INDENT res_str = res_str [ 0 : sum ] NEW_LINE DEDENT if ( isPalindrome ( res_str ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 10101 NEW_LINE if ( createStringAndCheckPalindrome ( N ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
T39905
import java . io . * ; class GFG { static void rearrange ( int arr [ ] , int n ) { int j = 0 , temp ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] < 0 ) { if ( i != j ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } j ++ ; } } } static void printArray ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { - 1 , 2 , - 3 , 4 , 5 , 6 , - 7 , 8 , 9 } ; int n = arr . length ; rearrange ( arr , n ) ; printArray ( arr , n ) ; } }
def rearrange ( arr , n ) : NEW_LINE INDENT j = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT if ( arr [ i ] < 0 ) : NEW_LINE INDENT temp = arr [ i ] NEW_LINE arr [ i ] = arr [ j ] NEW_LINE arr [ j ] = temp NEW_LINE j = j + 1 NEW_LINE DEDENT DEDENT print ( arr ) NEW_LINE DEDENT arr = [ - 1 , 2 , - 3 , 4 , 5 , 6 , - 7 , 8 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE rearrange ( arr , n ) NEW_LINE
T39906
import java . util . * ; class GFG { static int countSubstrs ( String s1 , String s2 ) { int ans = 0 ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) { String s3 = " " ; char [ ] s4 = s1 . toCharArray ( ) ; for ( int j = i ; j < s1 . length ( ) ; j ++ ) { s3 += s4 [ j ] ; if ( s2 . indexOf ( s3 ) != - 1 ) ans ++ ; } } return ans ; } public static void main ( String [ ] args ) { String s1 = " aab " , s2 = " aaaab " ; System . out . println ( countSubstrs ( s1 , s2 ) ) ; } }
def countSubstrs ( s1 , s2 ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT s3 = " " NEW_LINE for j in range ( i , len ( s1 ) ) : NEW_LINE INDENT s3 += s1 [ j ] NEW_LINE if s2 . find ( s3 ) != - 1 : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT DEDENT DEDENT return ans NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " aab " NEW_LINE s2 = " aaaab " NEW_LINE print ( countSubstrs ( s1 , s2 ) ) NEW_LINE DEDENT
T39907
import java . io . * ; class GFG { static int findSum ( int N , int K ) { int ans = 0 ; for ( int i = 1 ; i <= N ; i ++ ) ans += ( i % K ) ; return ans ; } static public void main ( String [ ] args ) { int N = 10 , K = 2 ; System . out . println ( findSum ( N , K ) ) ; } }
def findSum ( N , K ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( 1 , N + 1 ) : NEW_LINE INDENT ans += ( i % K ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT N = 10 ; NEW_LINE K = 2 ; NEW_LINE print ( findSum ( N , K ) ) ; NEW_LINE
T39908
import java . io . * ; class GFG { static void printGroups ( int n ) { int x = 1 ; int y = n * n ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 1 ; j <= n / 2 ; j ++ ) { System . out . print ( " { ▁ " + x + " , ▁ " + y + " } ▁ " ) ; x ++ ; y -- ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 4 ; printGroups ( n ) ; } }
def printGroups ( n ) : NEW_LINE INDENT x = 1 NEW_LINE y = n * n NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( 1 , n // 2 + 1 ) : NEW_LINE INDENT print ( " { " , x , " , " , y , " } " , end = " ▁ " ) NEW_LINE x += 1 NEW_LINE y -= 1 NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 NEW_LINE printGroups ( n ) NEW_LINE DEDENT
T39909
public class Fibonacci { static int findIndex ( int n ) { float fibo = 2.078087F * ( float ) Math . log ( n ) + 1.672276F ; return Math . round ( fibo ) ; } public static void main ( String [ ] args ) { int result = findIndex ( 55 ) ; System . out . println ( result ) ; } }
import math NEW_LINE def findIndex ( n ) : NEW_LINE INDENT fibo = 2.078087 * math . log ( n ) + 1.672276 NEW_LINE return round ( fibo ) NEW_LINE DEDENT n = 21 NEW_LINE print ( findIndex ( n ) ) NEW_LINE
T39910
import java . io . * ; class GFG { static int sum_series ( int n ) { int nSquare = n * n ; return nSquare * ( nSquare - 1 ) / 4 ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( sum_series ( n ) ) ; } }
def sum_series ( n ) : NEW_LINE INDENT nSquare = n * n NEW_LINE return int ( nSquare * ( nSquare - 1 ) / 4 ) NEW_LINE DEDENT n = 2 NEW_LINE print ( sum_series ( n ) ) NEW_LINE
T39911
import java . io . * ; class GFG { int Circumference ( int a ) { return 4 * a ; } public static void main ( String args [ ] ) { GFG obj = new GFG ( ) ; int a = 5 ; System . out . println ( " Circumference ▁ of ▁ " + " a ▁ square ▁ is ▁ " + obj . Circumference ( a ) ) ; } }
def Circumference ( a ) : NEW_LINE INDENT return ( 4 * a ) NEW_LINE DEDENT a = 5 NEW_LINE c = Circumference ( a ) NEW_LINE print ( " Circumference ▁ of ▁ a ▁ " + " square ▁ is ▁ % ▁ d " % ( c ) ) NEW_LINE
T39912
class GFG { final static int INT_MIN = Integer . MIN_VALUE ; static int maxSum ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; int limit = 2 * sum + 1 ; int dp [ ] [ ] = new int [ n + 1 ] [ limit ] ; for ( int i = 0 ; i < n + 1 ; i ++ ) { for ( int j = 0 ; j < limit ; j ++ ) dp [ i ] [ j ] = INT_MIN ; } dp [ 0 ] [ sum ] = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { for ( int j = 0 ; j < limit ; j ++ ) { if ( ( j - a [ i - 1 ] ) >= 0 && dp [ i - 1 ] [ j - a [ i - 1 ] ] != INT_MIN ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - a [ i - 1 ] ] + a [ i - 1 ] ) ; if ( ( j + a [ i - 1 ] ) < limit && dp [ i - 1 ] [ j + a [ i - 1 ] ] != INT_MIN ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j + a [ i - 1 ] ] ) ; if ( dp [ i - 1 ] [ j ] != INT_MIN ) dp [ i ] [ j ] = Math . max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j ] ) ; } } return dp [ n ] [ sum ] ; } public static void main ( String [ ] args ) { int n = 4 ; int [ ] a = { 1 , 2 , 3 , 6 } ; System . out . println ( maxSum ( a , n ) ) ; } }
import numpy as np NEW_LINE import sys NEW_LINE INT_MIN = - ( sys . maxsize - 1 ) NEW_LINE def maxSum ( a , n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += a [ i ] ; NEW_LINE DEDENT limit = 2 * sum + 1 ; NEW_LINE dp = np . zeros ( ( n + 1 , limit ) ) ; NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( limit ) : NEW_LINE INDENT dp [ i ] [ j ] = INT_MIN ; NEW_LINE DEDENT DEDENT dp [ 0 ] [ sum ] = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT for j in range ( limit ) : NEW_LINE INDENT if ( ( j - a [ i - 1 ] ) >= 0 and dp [ i - 1 ] [ j - a [ i - 1 ] ] != INT_MIN ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j - a [ i - 1 ] ] + a [ i - 1 ] ) ; NEW_LINE DEDENT if ( ( j + a [ i - 1 ] ) < limit and dp [ i - 1 ] [ j + a [ i - 1 ] ] != INT_MIN ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j + a [ i - 1 ] ] ) ; NEW_LINE DEDENT if ( dp [ i - 1 ] [ j ] != INT_MIN ) : NEW_LINE INDENT dp [ i ] [ j ] = max ( dp [ i ] [ j ] , dp [ i - 1 ] [ j ] ) ; NEW_LINE DEDENT DEDENT DEDENT return dp [ n ] [ sum ] ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 4 ; NEW_LINE a = [ 1 , 2 , 3 , 6 ] ; NEW_LINE print ( maxSum ( a , n ) ) ; NEW_LINE DEDENT
T39913
public class GFG { static int minParentheses ( String p ) { int bal = 0 ; int ans = 0 ; for ( int i = 0 ; i < p . length ( ) ; ++ i ) { bal += p . charAt ( i ) == ' ( ' ? 1 : - 1 ; if ( bal == - 1 ) { ans += 1 ; bal += 1 ; } } return bal + ans ; } public static void main ( String args [ ] ) { String p = " ( ) ) " ; System . out . println ( minParentheses ( p ) ) ; } }
def minParentheses ( p ) : NEW_LINE INDENT bal = 0 NEW_LINE ans = 0 NEW_LINE for i in range ( 0 , len ( p ) ) : NEW_LINE INDENT if ( p [ i ] == ' ( ' ) : NEW_LINE INDENT bal += 1 NEW_LINE DEDENT else : NEW_LINE INDENT bal += - 1 NEW_LINE DEDENT if ( bal == - 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE bal += 1 NEW_LINE DEDENT DEDENT return bal + ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT p = " ( ) ) " NEW_LINE print ( minParentheses ( p ) ) NEW_LINE DEDENT
T39914
class GFG { static void printCoins ( int arr [ ] , int n ) { int oddSum = 0 ; for ( int i = 0 ; i < n ; i += 2 ) oddSum += arr [ i ] ; int evenSum = 0 ; for ( int i = 1 ; i < n ; i += 2 ) evenSum += arr [ i ] ; int start = ( ( oddSum > evenSum ) ? 0 : 1 ) ; for ( int i = start ; i < n ; i += 2 ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 8 , 15 , 3 , 7 } ; int n = arr1 . length ; printCoins ( arr1 , n ) ; System . out . println ( ) ; int arr2 [ ] = { 2 , 2 , 2 , 2 } ; n = arr2 . length ; printCoins ( arr2 , n ) ; System . out . println ( ) ; int arr3 [ ] = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . length ; printCoins ( arr3 , n ) ; } }
def printCoins ( arr , n ) : NEW_LINE INDENT oddSum = 0 NEW_LINE for i in range ( 0 , n , 2 ) : NEW_LINE INDENT oddSum += arr [ i ] NEW_LINE DEDENT evenSum = 0 NEW_LINE for i in range ( 1 , n , 2 ) : NEW_LINE INDENT evenSum += arr [ i ] NEW_LINE DEDENT if oddSum > evenSum : NEW_LINE INDENT start = 0 NEW_LINE DEDENT else : NEW_LINE INDENT start = 1 NEW_LINE DEDENT for i in range ( start , n , 2 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr1 = [ 8 , 15 , 3 , 7 ] NEW_LINE n = len ( arr1 ) NEW_LINE printCoins ( arr1 , n ) NEW_LINE print ( ) NEW_LINE arr2 = [ 2 , 2 , 2 , 2 ] NEW_LINE n = len ( arr2 ) NEW_LINE printCoins ( arr2 , n ) NEW_LINE print ( ) NEW_LINE arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ] NEW_LINE n = len ( arr3 ) NEW_LINE printCoins ( arr3 , n ) NEW_LINE DEDENT
T39915
class GFG { public static int Valid ( int a , int b , int c , int d ) { if ( a + b + c + d == 360 ) return 1 ; return 0 ; } public static void main ( String [ ] args ) { int a = 80 , b = 70 , c = 100 , d = 110 ; if ( Valid ( a , b , c , d ) == 1 ) System . out . println ( " Valid ▁ quadilateral " ) ; else System . out . println ( " Invalid ▁ quadilateral " ) ; } }
def Valid ( a , b , c , d ) : NEW_LINE INDENT if ( a + b + c + d == 360 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT a = 80 ; b = 70 ; c = 100 ; d = 110 ; NEW_LINE if ( Valid ( a , b , c , d ) ) : NEW_LINE INDENT print ( " Valid ▁ quadilateral " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid ▁ quadilateral " ) ; NEW_LINE DEDENT
T39916
class GFG { static void Cholesky_Decomposition ( int [ ] [ ] matrix , int n ) { int [ ] [ ] lower = new int [ n ] [ n ] ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j <= i ; j ++ ) { int sum = 0 ; if ( j == i ) { for ( int k = 0 ; k < j ; k ++ ) sum += ( int ) Math . pow ( lower [ j ] [ k ] , 2 ) ; lower [ j ] [ j ] = ( int ) Math . sqrt ( matrix [ j ] [ j ] - sum ) ; } else { for ( int k = 0 ; k < j ; k ++ ) sum += ( lower [ i ] [ k ] * lower [ j ] [ k ] ) ; lower [ i ] [ j ] = ( matrix [ i ] [ j ] - sum ) / lower [ j ] [ j ] ; } } } System . out . println ( " ▁ Lower ▁ Triangular\t ▁ Transpose " ) ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = 0 ; j < n ; j ++ ) System . out . print ( lower [ i ] [ j ] + " \t " ) ; System . out . print ( " " ) ; for ( int j = 0 ; j < n ; j ++ ) System . out . print ( lower [ j ] [ i ] + " \t " ) ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int n = 3 ; int [ ] [ ] matrix = new int [ ] [ ] { { 4 , 12 , - 16 } , { 12 , 37 , - 43 } , { - 16 , - 43 , 98 } } ; Cholesky_Decomposition ( matrix , n ) ; } }
import math NEW_LINE MAX = 100 ; NEW_LINE def Cholesky_Decomposition ( matrix , n ) : NEW_LINE INDENT lower = [ [ 0 for x in range ( n + 1 ) ] for y in range ( n + 1 ) ] ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 ) : NEW_LINE INDENT sum1 = 0 ; NEW_LINE if ( j == i ) : NEW_LINE INDENT for k in range ( j ) : NEW_LINE INDENT sum1 += pow ( lower [ j ] [ k ] , 2 ) ; NEW_LINE DEDENT lower [ j ] [ j ] = int ( math . sqrt ( matrix [ j ] [ j ] - sum1 ) ) ; NEW_LINE DEDENT else : NEW_LINE INDENT for k in range ( j ) : NEW_LINE INDENT sum1 += ( lower [ i ] [ k ] * lower [ j ] [ k ] ) ; NEW_LINE DEDENT if ( lower [ j ] [ j ] > 0 ) : NEW_LINE INDENT lower [ i ] [ j ] = int ( ( matrix [ i ] [ j ] - sum1 ) / lower [ j ] [ j ] ) ; NEW_LINE DEDENT DEDENT DEDENT DEDENT print ( " Lower ▁ Triangular\t\tTranspose " ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( n ) : NEW_LINE INDENT print ( lower [ i ] [ j ] , end = " \t " ) ; NEW_LINE DEDENT print ( " " , end = " \t " ) ; NEW_LINE for j in range ( n ) : NEW_LINE INDENT print ( lower [ j ] [ i ] , end = " \t " ) ; NEW_LINE DEDENT print ( " " ) ; NEW_LINE DEDENT DEDENT n = 3 ; NEW_LINE matrix = [ [ 4 , 12 , - 16 ] , [ 12 , 37 , - 43 ] , [ - 16 , - 43 , 98 ] ] ; NEW_LINE Cholesky_Decomposition ( matrix , n ) ; NEW_LINE
T39917
import java . util . * ; class GFG { static int findGreatest ( int arr [ ] , int n ) { Map < Integer , Integer > m = new HashMap < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( m . containsKey ( arr [ i ] ) ) { m . put ( arr [ i ] , m . get ( arr [ i ] ) + 1 ) ; } else { m . put ( arr [ i ] , m . get ( arr [ i ] ) ) ; } } Arrays . sort ( arr ) ; for ( int i = n - 1 ; i > 1 ; i -- ) { for ( int j = 0 ; j < i && arr [ j ] <= Math . sqrt ( arr [ i ] ) ; j ++ ) { if ( arr [ i ] % arr [ j ] == 0 ) { int result = arr [ i ] / arr [ j ] ; if ( result != arr [ j ] && m . get ( result ) == null || m . get ( result ) > 0 ) { return arr [ i ] ; } else if ( result == arr [ j ] && m . get ( result ) > 1 ) { return arr [ i ] ; } } } } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 17 , 2 , 1 , 15 , 30 } ; int n = arr . length ; System . out . println ( findGreatest ( arr , n ) ) ; } }
from math import sqrt NEW_LINE def findGreatest ( arr , n ) : NEW_LINE INDENT m = dict ( ) NEW_LINE for i in arr : NEW_LINE INDENT m [ i ] = m . get ( i , 0 ) + 1 NEW_LINE DEDENT arr = sorted ( arr ) NEW_LINE for i in range ( n - 1 , 0 , - 1 ) : NEW_LINE INDENT j = 0 NEW_LINE while ( j < i and arr [ j ] <= sqrt ( arr [ i ] ) ) : NEW_LINE INDENT if ( arr [ i ] % arr [ j ] == 0 ) : NEW_LINE INDENT result = arr [ i ] // arr [ j ] NEW_LINE if ( result != arr [ j ] and ( result in m . keys ( ) ) and m [ result ] > 0 ) : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT elif ( result == arr [ j ] and ( result in m . keys ( ) ) and m [ result ] > 1 ) : NEW_LINE INDENT return arr [ i ] NEW_LINE DEDENT DEDENT j += 1 NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 17 , 2 , 1 , 15 , 30 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findGreatest ( arr , n ) ) NEW_LINE
T39918
import java . util . * ; class GFG { static int sortByFreq ( Integer [ ] arr , int n ) { int maxE = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { maxE = Math . max ( maxE , arr [ i ] ) ; } int freq [ ] = new int [ maxE + 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { freq [ arr [ i ] ] ++ ; } int cnt = 0 ; for ( int i = 0 ; i <= maxE ; i ++ ) { if ( freq [ i ] > 0 ) { int value = 100000 - i ; arr [ cnt ] = 100000 * freq [ i ] + value ; cnt ++ ; } } return cnt ; } static void printSortedArray ( Integer [ ] arr , int cnt ) { for ( int i = 0 ; i < cnt ; i ++ ) { int frequency = arr [ i ] / 100000 ; int value = 100000 - ( arr [ i ] % 100000 ) ; for ( int j = 0 ; j < frequency ; j ++ ) { System . out . print ( value + " ▁ " ) ; } } } public static void main ( String [ ] args ) { Integer arr [ ] = { 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 } ; int n = arr . length ; int cnt = sortByFreq ( arr , n ) ; Arrays . sort ( arr , Collections . reverseOrder ( ) ) ; printSortedArray ( arr , cnt ) ; } }
def sortByFreq ( arr , n ) : NEW_LINE INDENT maxE = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT maxE = max ( maxE , arr [ i ] ) NEW_LINE DEDENT freq = [ 0 ] * ( maxE + 1 ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT freq [ arr [ i ] ] += 1 ; NEW_LINE DEDENT cnt = 0 ; NEW_LINE for i in range ( maxE + 1 ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT value = 100000 - i ; NEW_LINE arr [ cnt ] = 100000 * freq [ i ] + value ; NEW_LINE cnt += 1 ; NEW_LINE DEDENT DEDENT return cnt ; NEW_LINE DEDENT def printSortedArray ( arr , cnt ) : NEW_LINE INDENT for i in range ( cnt ) : NEW_LINE INDENT frequency = arr [ i ] / 100000 ; NEW_LINE value = 100000 - ( arr [ i ] % 100000 ) ; NEW_LINE for j in range ( int ( frequency ) ) : NEW_LINE INDENT print ( value , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 4 , 5 , 6 , 4 , 2 , 2 , 8 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE cnt = sortByFreq ( arr , n ) ; NEW_LINE arr . sort ( reverse = True ) NEW_LINE printSortedArray ( arr , cnt ) ; NEW_LINE DEDENT
T39919
import java . io . * ; class GFG { static int countMinimumMoves ( int arr [ ] , int n , int k ) { int i ; for ( i = k - 1 ; i < n ; i ++ ) if ( arr [ i ] != arr [ k - 1 ] ) return - 1 ; for ( i = k - 1 ; i >= 0 ; i -- ) if ( arr [ i ] != arr [ k - 1 ] ) return i + 1 ; return 0 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 } ; int K = 4 ; int n = arr . length ; System . out . print ( countMinimumMoves ( arr , n , K ) ) ; } }
def countMinimumMoves ( arr , n , k ) : NEW_LINE INDENT for i in range ( k - 1 , n ) : NEW_LINE INDENT if ( arr [ i ] != arr [ k - 1 ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT for i in range ( k - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( arr [ i ] != arr [ k - 1 ] ) : NEW_LINE INDENT return i + 1 NEW_LINE DEDENT DEDENT return 0 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 3 , 4 ] NEW_LINE K = 4 NEW_LINE n = len ( arr ) NEW_LINE print ( countMinimumMoves ( arr , n , K ) ) NEW_LINE DEDENT
T39920
class GFG { static class Node { char data ; Node prev ; Node next ; } ; static int size ( Node head_ref ) { Node curr = head_ref ; int sz = 0 ; while ( curr != null ) { curr = curr . next ; sz ++ ; } return sz ; } static void printList ( Node node ) { while ( node . next != null ) { System . out . print ( node . data + " ▁ " + " < = > " + " ▁ " ) ; node = node . next ; } System . out . print ( node . data ) ; } static Node push ( Node head_ref , char new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . prev = null ; new_node . next = head_ref ; if ( head_ref != null ) head_ref . prev = new_node ; head_ref = new_node ; return head_ref ; } static Node rotate ( Node head_ref , int N , int sz ) { N = N % sz ; N = sz - N ; if ( N == 0 ) return null ; Node current = head_ref ; int count = 1 ; while ( count < N && current != null ) { current = current . next ; count ++ ; } if ( current == null ) return null ; Node NthNode = current ; while ( current . next != null ) current = current . next ; current . next = head_ref ; head_ref . prev = current ; head_ref = NthNode . next ; head_ref . prev = null ; NthNode . next = null ; return head_ref ; } public static void main ( String [ ] args ) { Node head = null ; head = push ( head , ' e ' ) ; head = push ( head , ' d ' ) ; head = push ( head , ' c ' ) ; head = push ( head , ' b ' ) ; head = push ( head , ' a ' ) ; int N = 2 ; int sz = size ( head ) ; System . out . println ( " Given ▁ Doubly ▁ linked ▁ list ▁ " ) ; printList ( head ) ; head = rotate ( head , N , sz ) ; System . out . println ( " \n Rotated ▁ Linked ▁ list ▁ clockwise ▁ " ) ; printList ( head ) ; } }
class Node : NEW_LINE INDENT def __init__ ( self , next = None , prev = None , data = None ) : NEW_LINE INDENT self . next = next NEW_LINE self . prev = prev NEW_LINE self . data = data NEW_LINE DEDENT DEDENT def push ( head , new_data ) : NEW_LINE INDENT new_node = Node ( data = new_data ) NEW_LINE new_node . next = head NEW_LINE new_node . prev = None NEW_LINE if head is not None : NEW_LINE INDENT head . prev = new_node NEW_LINE DEDENT head = new_node NEW_LINE return head NEW_LINE DEDENT def size ( head ) : NEW_LINE INDENT node = head NEW_LINE sz = 0 NEW_LINE while ( node is not None ) : NEW_LINE INDENT sz += 1 NEW_LINE node = node . next NEW_LINE DEDENT return sz NEW_LINE DEDENT def printList ( head ) : NEW_LINE INDENT node = head NEW_LINE print ( " Given ▁ linked ▁ list " ) NEW_LINE while ( node is not None ) : NEW_LINE INDENT print ( node . data , end = " ▁ " ) , NEW_LINE last = node NEW_LINE node = node . next NEW_LINE DEDENT DEDENT def rotate ( start , N ) : NEW_LINE INDENT if N == 0 : NEW_LINE INDENT return NEW_LINE DEDENT current = start NEW_LINE count = 1 NEW_LINE while count < N and current != None : NEW_LINE INDENT current = current . next NEW_LINE count += 1 NEW_LINE DEDENT if current == None : NEW_LINE INDENT return NEW_LINE DEDENT NthNode = current NEW_LINE while current . next != None : NEW_LINE INDENT current = current . next NEW_LINE DEDENT current . next = start NEW_LINE start . prev = current NEW_LINE start = NthNode . next NEW_LINE start . prev = None NEW_LINE NthNode . next = None NEW_LINE return start NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT head = None NEW_LINE head = push ( head , ' e ' ) NEW_LINE head = push ( head , ' d ' ) NEW_LINE head = push ( head , ' c ' ) NEW_LINE head = push ( head , ' b ' ) NEW_LINE head = push ( head , ' a ' ) NEW_LINE printList ( head ) NEW_LINE print ( " \n " ) NEW_LINE N = 2 NEW_LINE sz = size ( head ) NEW_LINE N = N % sz ; NEW_LINE N = sz - N ; NEW_LINE head = rotate ( head , N ) NEW_LINE printList ( head ) NEW_LINE DEDENT
T39921
class GFG { static boolean isDigitPresent ( int x , int d ) { while ( x > 0 ) { if ( x % 10 == d ) break ; x = x / 10 ; } return ( x > 0 ) ; } static void printNumbers ( int n , int d ) { for ( int i = 0 ; i <= n ; i ++ ) if ( i == d || isDigitPresent ( i , d ) ) System . out . print ( i + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 47 , d = 7 ; printNumbers ( n , d ) ; } }
def isDigitPresent ( x , d ) : NEW_LINE INDENT while ( x > 0 ) : NEW_LINE INDENT if ( x % 10 == d ) : NEW_LINE INDENT break NEW_LINE DEDENT x = x / 10 NEW_LINE DEDENT return ( x > 0 ) NEW_LINE DEDENT def printNumbers ( n , d ) : NEW_LINE INDENT for i in range ( 0 , n + 1 ) : NEW_LINE INDENT if ( i == d or isDigitPresent ( i , d ) ) : NEW_LINE INDENT print ( i , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT n = 47 NEW_LINE d = 7 NEW_LINE print ( " The ▁ number ▁ of ▁ values ▁ are " ) NEW_LINE printNumbers ( n , d ) NEW_LINE
T39922
class GFG { static int calculate ( int x , int k , int m ) { int result = x ; k -- ; while ( k -- > 0 ) { result = ( int ) Math . pow ( result , x ) ; if ( result > m ) result %= m ; } return result ; } public static void main ( String args [ ] ) { int x = 5 , k = 2 , m = 3 ; System . out . println ( calculate ( x , k , m ) ) ; } }
import math NEW_LINE def calculate ( x , k , m ) : NEW_LINE INDENT result = x ; NEW_LINE k = k - 1 ; NEW_LINE while ( k ) : NEW_LINE INDENT result = math . pow ( result , x ) ; NEW_LINE if ( result > m ) : NEW_LINE INDENT result = result % m ; NEW_LINE DEDENT k = k - 1 ; NEW_LINE DEDENT return int ( result ) ; NEW_LINE DEDENT x = 5 ; NEW_LINE k = 2 ; NEW_LINE m = 3 ; NEW_LINE print ( calculate ( x , k , m ) ) ; NEW_LINE
T39923
import java . util . * ; class GFG { static int printMinimumProduct ( int arr [ ] , int n ) { int first_min = Math . min ( arr [ 0 ] , arr [ 1 ] ) ; int second_min = Math . max ( arr [ 0 ] , arr [ 1 ] ) ; for ( int i = 2 ; i < n ; i ++ ) { if ( arr [ i ] < first_min ) { second_min = first_min ; first_min = arr [ i ] ; } else if ( arr [ i ] < second_min ) second_min = arr [ i ] ; } return first_min * second_min ; } public static void main ( String [ ] args ) { int a [ ] = { 11 , 8 , 5 , 7 , 5 , 100 } ; int n = a . length ; System . out . print ( printMinimumProduct ( a , n ) ) ; } }
def printMinimumProduct ( arr , n ) : NEW_LINE INDENT first_min = min ( arr [ 0 ] , arr [ 1 ] ) NEW_LINE second_min = max ( arr [ 0 ] , arr [ 1 ] ) NEW_LINE for i in range ( 2 , n ) : NEW_LINE INDENT if ( arr [ i ] < first_min ) : NEW_LINE INDENT second_min = first_min NEW_LINE first_min = arr [ i ] NEW_LINE DEDENT elif ( arr [ i ] < second_min ) : NEW_LINE INDENT second_min = arr [ i ] NEW_LINE DEDENT DEDENT return first_min * second_min NEW_LINE DEDENT a = [ 11 , 8 , 5 , 7 , 5 , 100 ] NEW_LINE n = len ( a ) NEW_LINE print ( printMinimumProduct ( a , n ) ) NEW_LINE
T39924
class GFG { static int odd_digits ( int n ) { if ( n < 10 ) return n ; else if ( n / 10 < 10 ) return 9 ; else if ( n / 100 < 10 ) return 9 + n - 99 ; else if ( n / 1000 < 10 ) return 9 + 900 ; else if ( n / 10000 < 10 ) return 909 + n - 9999 ; else return 90909 ; } public static void main ( String [ ] args ) { int n = 893 ; System . out . println ( odd_digits ( n ) ) ; } }
def odd_digits ( n ) : NEW_LINE INDENT if ( n < 10 ) : NEW_LINE INDENT return n ; NEW_LINE DEDENT elif ( n / 10 < 10 ) : NEW_LINE INDENT return 9 ; NEW_LINE DEDENT elif ( n / 100 < 10 ) : NEW_LINE INDENT return 9 + n - 99 ; NEW_LINE DEDENT elif ( n / 1000 < 10 ) : NEW_LINE INDENT return 9 + 900 ; NEW_LINE DEDENT elif ( n / 10000 < 10 ) : NEW_LINE INDENT return 909 + n - 9999 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 90909 ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 893 ; NEW_LINE print ( odd_digits ( n ) ) ; NEW_LINE DEDENT
T39925
class GFG { static int nthTerm ( int N ) { return ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
def nthTerm ( N ) : NEW_LINE INDENT return ( ( 2 * N + 3 ) * ( 2 * N + 3 ) - 2 * N ) ; NEW_LINE DEDENT n = 4 NEW_LINE print ( nthTerm ( n ) ) NEW_LINE
T39926
import java . io . * ; class GFG { static void findNthTerm ( int n ) { n = n * 2 ; int a = 1 , b = 1 , c = - 1 * n ; int d = b * b - 4 * a * c ; double sqrt_val = Math . sqrt ( Math . abs ( d ) ) ; int x1 = ( int ) ( ( - b + sqrt_val ) / ( 2 * a ) ) ; int x2 = ( int ) ( ( - b - sqrt_val ) / ( 2 * a ) ) ; if ( x1 >= 1 ) System . out . println ( ( char ) ( ' a ' + x1 ) ) ; else if ( x2 >= 1 ) System . out . println ( ( char ) ( ' a ' + x2 ) ) ; } public static void main ( String [ ] args ) { int n = 12 ; findNthTerm ( n ) ; n = 288 ; findNthTerm ( n ) ; } }
import math NEW_LINE def findNthTerm ( n ) : NEW_LINE INDENT n = n * 2 NEW_LINE a = 1 NEW_LINE b = 1 NEW_LINE c = - 1 * n NEW_LINE d = b * b - 4 * a * c NEW_LINE sqrt_val = math . sqrt ( abs ( d ) ) NEW_LINE x1 = ( - b + sqrt_val ) // ( 2 * a ) NEW_LINE x2 = ( - b - sqrt_val ) // ( 2 * a ) NEW_LINE x1 = int ( x1 ) NEW_LINE x2 = int ( x2 ) NEW_LINE if ( x1 >= 1 ) : NEW_LINE INDENT print ( chr ( 97 + x1 ) ) NEW_LINE DEDENT elif ( x2 >= 1 ) : NEW_LINE INDENT print ( chr ( 97 + x2 ) ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 12 NEW_LINE findNthTerm ( n ) NEW_LINE n = 288 NEW_LINE findNthTerm ( n ) NEW_LINE DEDENT
T39927
public class Char_frequency { static final int SIZE = 26 ; static void printCharWithFreq ( String str ) { int n = str . length ( ) ; int [ ] freq = new int [ SIZE ] ; for ( int i = 0 ; i < n ; i ++ ) freq [ str . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq [ str . charAt ( i ) - ' a ' ] != 0 ) { System . out . print ( str . charAt ( i ) ) ; System . out . print ( freq [ str . charAt ( i ) - ' a ' ] + " ▁ " ) ; freq [ str . charAt ( i ) - ' a ' ] = 0 ; } } } public static void main ( String args [ ] ) { String str = " geeksforgeeks " ; printCharWithFreq ( str ) ; } }
import numpy as np NEW_LINE def prCharWithFreq ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE freq = np . zeros ( 26 , dtype = np . int ) NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 0 , n ) : NEW_LINE INDENT if ( freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] != 0 ) : NEW_LINE INDENT print ( str [ i ] , freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] , end = " ▁ " ) NEW_LINE freq [ ord ( str [ i ] ) - ord ( ' a ' ) ] = 0 NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks " ; NEW_LINE prCharWithFreq ( str ) ; NEW_LINE DEDENT
T39928
import java . util . * ; class solution { static class Node { int key ; Node left , right ; boolean isThreaded ; } ; static Node createThreaded ( Node root ) { if ( root == null ) return null ; if ( root . left == null && root . right == null ) return root ; if ( root . left != null ) { Node l = createThreaded ( root . left ) ; l . right = root ; l . isThreaded = true ; } if ( root . right == null ) return root ; return createThreaded ( root . right ) ; } static Node leftMost ( Node root ) { while ( root != null && root . left != null ) root = root . left ; return root ; } static void inOrder ( Node root ) { if ( root == null ) return ; Node cur = leftMost ( root ) ; while ( cur != null ) { System . out . print ( cur . key + " ▁ " ) ; if ( cur . isThreaded ) cur = cur . right ; else cur = leftMost ( cur . right ) ; } } static Node newNode ( int key ) { Node temp = new Node ( ) ; temp . left = temp . right = null ; temp . key = key ; return temp ; } public static void main ( String args [ ] ) { Node root = newNode ( 1 ) ; root . left = newNode ( 2 ) ; root . right = newNode ( 3 ) ; root . left . left = newNode ( 4 ) ; root . left . right = newNode ( 5 ) ; root . right . left = newNode ( 6 ) ; root . right . right = newNode ( 7 ) ; createThreaded ( root ) ; System . out . println ( " Inorder ▁ traversal ▁ of ▁ created ▁ " + " threaded ▁ tree ▁ is \n " ) ; inOrder ( root ) ; } }
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . left = self . right = None NEW_LINE self . key = key NEW_LINE self . isThreaded = None NEW_LINE DEDENT DEDENT def createThreaded ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return None NEW_LINE DEDENT if root . left == None and root . right == None : NEW_LINE INDENT return root NEW_LINE DEDENT if root . left != None : NEW_LINE INDENT l = createThreaded ( root . left ) NEW_LINE l . right = root NEW_LINE l . isThreaded = True NEW_LINE DEDENT if root . right == None : NEW_LINE INDENT return root NEW_LINE DEDENT return createThreaded ( root . right ) NEW_LINE DEDENT def leftMost ( root ) : NEW_LINE INDENT while root != None and root . left != None : NEW_LINE INDENT root = root . left NEW_LINE DEDENT return root NEW_LINE DEDENT def inOrder ( root ) : NEW_LINE INDENT if root == None : NEW_LINE INDENT return NEW_LINE DEDENT cur = leftMost ( root ) NEW_LINE while cur != None : NEW_LINE INDENT print ( cur . key , end = " ▁ " ) NEW_LINE if cur . isThreaded : NEW_LINE INDENT cur = cur . right NEW_LINE DEDENT else : NEW_LINE INDENT cur = leftMost ( cur . right ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 1 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 3 ) NEW_LINE root . left . left = newNode ( 4 ) NEW_LINE root . left . right = newNode ( 5 ) NEW_LINE root . right . left = newNode ( 6 ) NEW_LINE root . right . right = newNode ( 7 ) NEW_LINE createThreaded ( root ) NEW_LINE print ( " Inorder ▁ traversal ▁ of ▁ created " , " threaded ▁ tree ▁ is " ) NEW_LINE inOrder ( root ) NEW_LINE DEDENT
T39929
import java . util . HashMap ; import java . util . Map ; class GfG { static int sumFirst ( int a [ ] , int n ) { HashMap < Integer , Integer > mp = new HashMap < > ( ) ; int suf = 0 ; for ( int i = n - 1 ; i >= 0 ; i -- ) { suf += a [ i ] ; mp . put ( suf , i ) ; } int pre = 0 , maxi = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { pre += a [ i ] ; if ( mp . containsKey ( pre ) && mp . get ( pre ) > i ) { if ( pre > maxi ) { maxi = pre ; } } } if ( maxi == - 1 ) return 0 ; else return maxi ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 3 , 2 , 1 , 4 } ; int n = a . length ; System . out . println ( sumFirst ( a , n ) ) ; } }
def sumFirst ( a , n ) : NEW_LINE INDENT mp = { i : 0 for i in range ( 7 ) } NEW_LINE suf = 0 NEW_LINE i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT suf += a [ i ] NEW_LINE mp [ suf ] = i NEW_LINE i -= 1 NEW_LINE DEDENT pre = 0 NEW_LINE maxi = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT pre += a [ i ] NEW_LINE if ( mp [ pre ] > i ) : NEW_LINE INDENT if ( pre > maxi ) : NEW_LINE INDENT maxi = pre NEW_LINE DEDENT DEDENT DEDENT if ( maxi == - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT else : NEW_LINE INDENT return maxi NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 1 , 3 , 2 , 1 , 4 ] NEW_LINE n = len ( a ) NEW_LINE print ( sumFirst ( a , n ) ) NEW_LINE DEDENT
T39930
import java . util . * ; class GFG { static double sum ( int n ) { if ( n == 0 ) return 1 ; double ans = 1 / ( double ) Math . pow ( 3 , n ) + sum ( n - 1 ) ; return ans ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( sum ( n ) ) ; } }
def sum ( n ) : NEW_LINE INDENT if n == 0 : NEW_LINE INDENT return 1 NEW_LINE DEDENT return 1 / pow ( 3 , n ) + sum ( n - 1 ) NEW_LINE DEDENT n = 5 ; NEW_LINE print ( sum ( n ) ) ; NEW_LINE
T39931
import java . io . * ; class GFG { static void pattern ( int min_stars , int p_height ) { int p_space ; p_space = p_height - 1 ; int i , j , k , n , x ; x = 1 ; for ( i = 0 ; i < p_height ; i ++ ) { for ( j = p_space ; j > i ; j -- ) { System . out . print ( " ▁ " ) ; } for ( k = 0 ; k < min_stars ; k ++ ) System . out . print ( " * " ) ; for ( n = ( p_height + p_height - 2 ) ; n >= x ; n -- ) System . out . print ( " ▁ " ) ; for ( k = 0 ; k < min_stars ; k ++ ) System . out . print ( " * " ) ; min_stars = min_stars + 2 ; x = x + 2 ; System . out . println ( ) ; } } public static void main ( String [ ] args ) { int min_stars = 1 ; int p_height = 5 ; pattern ( min_stars , p_height ) ; } }
def pattern ( min_stars , p_height ) : NEW_LINE INDENT p_space = p_height - 1 NEW_LINE x = 1 NEW_LINE for i in range ( 0 , p_height ) : NEW_LINE INDENT for j in range ( p_space , i , - 1 ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT for k in range ( 0 , min_stars ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT for n in range ( ( p_height + p_height - 2 ) , x - 1 , - 1 ) : NEW_LINE INDENT print ( " ▁ " , end = " " ) NEW_LINE DEDENT for k in range ( 0 , min_stars ) : NEW_LINE INDENT print ( " * " , end = " " ) NEW_LINE DEDENT min_stars = min_stars + 2 NEW_LINE x = x + 2 NEW_LINE print ( " " ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT min_stars = 1 NEW_LINE p_height = 5 NEW_LINE pattern ( min_stars , p_height ) NEW_LINE DEDENT
T39932
import java . io . * ; import java . util . * ; class GFG { static void findCart ( int arr1 [ ] , int arr2 [ ] , int n , int n1 ) { for ( int i = 0 ; i < n ; i ++ ) for ( int j = 0 ; j < n1 ; j ++ ) System . out . print ( " { " + arr1 [ i ] + " , ▁ " + arr2 [ j ] + " } , ▁ " ) ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 1 , 2 , 3 } ; int arr2 [ ] = { 4 , 5 , 6 } ; int n1 = arr1 . length ; int n2 = arr2 . length ; findCart ( arr1 , arr2 , n1 , n2 ) ; } }
def findCart ( arr1 , arr2 , n , n1 ) : NEW_LINE INDENT for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( 0 , n1 ) : NEW_LINE INDENT print ( " { " , arr1 [ i ] , " , ▁ " , arr2 [ j ] , " } , ▁ " , sep = " " , end = " " ) NEW_LINE DEDENT DEDENT DEDENT arr1 = [ 1 , 2 , 3 ] NEW_LINE arr2 = [ 4 , 5 , 6 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE n2 = len ( arr2 ) NEW_LINE findCart ( arr1 , arr2 , n1 , n2 ) ; NEW_LINE
T39933
import java . io . * ; import java . util . * ; class Node { int data ; Node left , right ; Node ( int key ) { data = key ; left = right = null ; } } class GFG { static int evenOddLevelDifference ( Node root ) { if ( root == null ) return 0 ; Queue < Node > q = new LinkedList < > ( ) ; q . add ( root ) ; int level = 0 ; int evenSum = 0 , oddSum = 0 ; while ( q . size ( ) != 0 ) { int size = q . size ( ) ; level ++ ; while ( size > 0 ) { Node temp = q . remove ( ) ; if ( level % 2 == 0 ) evenSum += temp . data ; else oddSum += temp . data ; if ( temp . left != null ) q . add ( temp . left ) ; if ( temp . right != null ) q . add ( temp . right ) ; size -- ; } } return ( oddSum - evenSum ) ; } public static void main ( String args [ ] ) { Node root = new Node ( 5 ) ; root . left = new Node ( 2 ) ; root . right = new Node ( 6 ) ; root . left . left = new Node ( 1 ) ; root . left . right = new Node ( 4 ) ; root . left . right . left = new Node ( 3 ) ; root . right . right = new Node ( 8 ) ; root . right . right . right = new Node ( 9 ) ; root . right . right . left = new Node ( 7 ) ; System . out . println ( " diffence ▁ between ▁ sums ▁ is ▁ " + evenOddLevelDifference ( root ) ) ; } }
class newNode : NEW_LINE INDENT def __init__ ( self , key ) : NEW_LINE INDENT self . data = key NEW_LINE self . left = None NEW_LINE self . right = None NEW_LINE DEDENT DEDENT def evenOddLevelDifference ( root ) : NEW_LINE INDENT if ( not root ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT q = [ ] NEW_LINE q . append ( root ) NEW_LINE level = 0 NEW_LINE evenSum = 0 NEW_LINE oddSum = 0 NEW_LINE while ( len ( q ) ) : NEW_LINE INDENT size = len ( q ) NEW_LINE level += 1 NEW_LINE while ( size > 0 ) : NEW_LINE INDENT temp = q [ 0 ] NEW_LINE q . pop ( 0 ) NEW_LINE if ( level % 2 == 0 ) : NEW_LINE INDENT evenSum += temp . data NEW_LINE DEDENT else : NEW_LINE INDENT oddSum += temp . data NEW_LINE DEDENT if ( temp . left ) : NEW_LINE INDENT q . append ( temp . left ) NEW_LINE DEDENT if ( temp . right ) : NEW_LINE INDENT q . append ( temp . right ) NEW_LINE DEDENT size -= 1 NEW_LINE DEDENT DEDENT return ( oddSum - evenSum ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT root = newNode ( 5 ) NEW_LINE root . left = newNode ( 2 ) NEW_LINE root . right = newNode ( 6 ) NEW_LINE root . left . left = newNode ( 1 ) NEW_LINE root . left . right = newNode ( 4 ) NEW_LINE root . left . right . left = newNode ( 3 ) NEW_LINE root . right . right = newNode ( 8 ) NEW_LINE root . right . right . right = newNode ( 9 ) NEW_LINE root . right . right . left = newNode ( 7 ) NEW_LINE result = evenOddLevelDifference ( root ) NEW_LINE print ( " Diffence ▁ between ▁ sums ▁ is " , result ) NEW_LINE DEDENT
T39934
import java . io . * ; class GFG { static boolean isTwoAlter ( String s ) { for ( int i = 0 ; i < s . length ( ) - 2 ; i ++ ) { if ( s . charAt ( i ) != s . charAt ( i + 2 ) ) { return false ; } } if ( s . charAt ( 0 ) == s . charAt ( 1 ) ) return false ; return true ; } public static void main ( String [ ] args ) { String str = " ABAB " ; if ( isTwoAlter ( str ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
def isTwoAlter ( s ) : NEW_LINE INDENT for i in range ( len ( s ) - 2 ) : NEW_LINE INDENT if ( s [ i ] != s [ i + 2 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if ( s [ 0 ] == s [ 1 ] ) : NEW_LINE INDENT return False NEW_LINE DEDENT return True NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " ABAB " NEW_LINE if ( isTwoAlter ( str ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT DEDENT
T39935
import java . util . * ; class GFG { static int checkSparse ( int n ) { if ( ( n & ( n >> 1 ) ) >= 1 ) return 0 ; return 1 ; } public static void main ( String [ ] args ) { System . out . println ( checkSparse ( 72 ) ) ; System . out . println ( checkSparse ( 12 ) ) ; System . out . println ( checkSparse ( 2 ) ) ; System . out . println ( checkSparse ( 3 ) ) ; } }
def checkSparse ( n ) : NEW_LINE INDENT if ( n & ( n >> 1 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT return 1 NEW_LINE DEDENT print ( checkSparse ( 72 ) ) NEW_LINE print ( checkSparse ( 12 ) ) NEW_LINE print ( checkSparse ( 2 ) ) NEW_LINE print ( checkSparse ( 30 ) ) NEW_LINE
T39936
import java . util . * ; class GFG { static void division_of_string ( char [ ] str , int k ) { int n = str . length ; Map < Character , Boolean > has = new HashMap < > ( ) ; int ans = 0 , cnt = 0 , i = 0 ; while ( i < n ) { if ( ! has . containsKey ( str [ i ] ) ) { cnt ++ ; has . put ( str [ i ] , true ) ; } if ( cnt == k ) { ans = i ; break ; } i ++ ; } has . clear ( ) ; cnt = 0 ; while ( i < n ) { if ( ! has . containsKey ( str [ i ] ) ) { cnt ++ ; has . put ( str [ i ] , true ) ; } if ( cnt == k ) { break ; } i ++ ; } if ( cnt < k ) { System . out . println ( " Not ▁ possible " ) ; } else { i = 0 ; while ( i <= ans ) { System . out . print ( str [ i ] ) ; i ++ ; } System . out . println ( " " ) ; while ( i < n ) { System . out . print ( str [ i ] ) ; i ++ ; } System . out . println ( " " ) ; } System . out . println ( " " ) ; } public static void main ( String [ ] args ) { String str = " geeksforgeeks " ; int k = 4 ; division_of_string ( str . toCharArray ( ) , k ) ; } }
def division_of_string ( string , k ) : NEW_LINE INDENT n = len ( string ) ; NEW_LINE has = { } ; NEW_LINE cnt = 0 ; i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if string [ i ] not in has : NEW_LINE INDENT cnt += 1 ; NEW_LINE has [ string [ i ] ] = True ; NEW_LINE DEDENT if ( cnt == k ) : NEW_LINE INDENT ans = i ; NEW_LINE break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT has . clear ( ) ; NEW_LINE cnt = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT if ( string [ i ] not in has ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE has [ string [ i ] ] = True ; NEW_LINE DEDENT if ( cnt == k ) : NEW_LINE INDENT break ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT if ( cnt < k ) : NEW_LINE INDENT print ( " Not ▁ possible " , end = " " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT i = 0 ; NEW_LINE while ( i <= ans ) : NEW_LINE INDENT print ( string [ i ] , end = " " ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT print ( ) ; NEW_LINE while ( i < n ) : NEW_LINE INDENT print ( string [ i ] , end = " " ) ; NEW_LINE i += 1 ; NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " geeksforgeeks " ; NEW_LINE k = 4 ; NEW_LINE division_of_string ( string , k ) ; NEW_LINE DEDENT
T39937
class GFG { static int findExtra ( int arr1 [ ] , int arr2 [ ] , int n ) { int index = n ; int left = 0 , right = n - 1 ; while ( left <= right ) { int mid = ( left + right ) / 2 ; if ( arr2 [ mid ] == arr1 [ mid ] ) left = mid + 1 ; else { index = mid ; right = mid - 1 ; } } return index ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 , 13 } ; int arr2 [ ] = { 2 , 4 , 6 , 8 , 10 , 12 } ; int n = arr2 . length ; System . out . println ( findExtra ( arr1 , arr2 , n ) ) ; } }
def findExtra ( arr1 , arr2 , n ) : NEW_LINE INDENT index = n NEW_LINE left = 0 NEW_LINE right = n - 1 NEW_LINE while ( left <= right ) : NEW_LINE INDENT mid = ( int ) ( ( left + right ) / 2 ) NEW_LINE if ( arr2 [ mid ] == arr1 [ mid ] ) : NEW_LINE INDENT left = mid + 1 NEW_LINE DEDENT else : NEW_LINE INDENT index = mid NEW_LINE right = mid - 1 NEW_LINE DEDENT DEDENT return index NEW_LINE DEDENT arr1 = [ 2 , 4 , 6 , 8 , 10 , 12 , 13 ] NEW_LINE arr2 = [ 2 , 4 , 6 , 8 , 10 , 12 ] NEW_LINE n = len ( arr2 ) NEW_LINE print ( findExtra ( arr1 , arr2 , n ) ) NEW_LINE
T39938
class GFG { static boolean possibleToReach ( int a , int b ) { int c = ( int ) Math . cbrt ( a * b ) ; int re1 = a / c ; int re2 = b / c ; if ( ( re1 * re1 * re2 == a ) && ( re2 * re2 * re1 == b ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int A = 60 , B = 450 ; if ( possibleToReach ( A , B ) ) System . out . println ( " yes " ) ; else System . out . println ( " no " ) ; } }
import numpy as np NEW_LINE def possibleToReach ( a , b ) : NEW_LINE INDENT c = np . cbrt ( a * b ) NEW_LINE re1 = a // c NEW_LINE re2 = b // c NEW_LINE if ( ( re1 * re1 * re2 == a ) and ( re2 * re2 * re1 == b ) ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = 60 NEW_LINE B = 450 NEW_LINE if ( possibleToReach ( A , B ) ) : NEW_LINE INDENT print ( " yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " no " ) NEW_LINE DEDENT DEDENT
T39939
import java . util . * ; import java . lang . * ; import java . io . * ; class GFG { static long NthTerm ( long n ) { long x = ( 3 * n * n ) % 1000000009 ; return ( x - n + 1000000009 ) % 1000000009 ; } public static void main ( String args [ ] ) { long N = 4 ; System . out . println ( NthTerm ( N ) ) ; } }
def NthTerm ( N ) : NEW_LINE INDENT x = ( 3 * N * N ) % 1000000009 NEW_LINE return ( ( x - N + 1000000009 ) % 1000000009 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 4 NEW_LINE print ( NthTerm ( N ) ) NEW_LINE DEDENT
T39940
import java . util . * ; class Solution { static final int MAX = 10000 ; static int hashTable [ ] = new int [ MAX ] ; static int minOperations ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n ; i ++ ) hashTable [ arr [ i ] ] ++ ; int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( hashTable [ arr [ i ] ] != 0 ) { for ( int j = i ; j < n ; j ++ ) if ( arr [ j ] % arr [ i ] == 0 ) hashTable [ arr [ j ] ] = 0 ; res ++ ; } } return res ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 } ; int n = arr . length ; System . out . print ( minOperations ( arr , n ) ) ; } }
MAX = 10000 NEW_LINE hashTable = [ 0 ] * MAX NEW_LINE def minOperations ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT hashTable [ arr [ i ] ] += 1 NEW_LINE DEDENT res = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( hashTable [ arr [ i ] ] ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT if ( arr [ j ] % arr [ i ] == 0 ) : NEW_LINE INDENT hashTable [ arr [ j ] ] = 0 NEW_LINE DEDENT DEDENT res += 1 NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 4 , 6 , 2 , 8 , 7 , 21 , 24 , 49 , 44 ] NEW_LINE n = len ( arr ) NEW_LINE print ( minOperations ( arr , n ) ) NEW_LINE DEDENT
T39941
class GFG { static int checkOdd ( String number ) { int n = number . length ( ) ; int num = number . charAt ( n - 1 ) - '0' ; return ( num & 1 ) ; } static int splitIntoOdds ( String number ) { int numLen = number . length ( ) ; int splitDP [ ] = new int [ numLen + 1 ] ; for ( int i = 0 ; i < numLen + 1 ; i ++ ) splitDP [ i ] = - 1 ; for ( int i = 1 ; i <= numLen ; i ++ ) { if ( i <= 9 && ( checkOdd ( number . substring ( 0 , i ) ) == 1 ) ) splitDP [ i ] = 1 ; if ( splitDP [ i ] != - 1 ) { for ( int j = 1 ; j <= 9 && i + j <= numLen ; j ++ ) { if ( checkOdd ( number . substring ( i , i + j ) ) == 1 ) { if ( splitDP [ i + j ] == - 1 ) splitDP [ i + j ] = 1 + splitDP [ i ] ; else splitDP [ i + j ] = Math . min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) ; } } } } return splitDP [ numLen ] ; } public static void main ( String [ ] args ) { System . out . println ( splitIntoOdds ( "123456789123456789123" ) ) ; } }
def checkOdd ( number ) : NEW_LINE INDENT n = len ( number ) NEW_LINE num = ord ( number [ n - 1 ] ) - 48 NEW_LINE return ( num & 1 ) NEW_LINE DEDENT def splitIntoOdds ( number ) : NEW_LINE INDENT numLen = len ( number ) NEW_LINE splitDP = [ - 1 for i in range ( numLen + 1 ) ] NEW_LINE for i in range ( 1 , numLen + 1 ) : NEW_LINE INDENT if ( i <= 9 and checkOdd ( number [ 0 : i ] ) > 0 ) : NEW_LINE INDENT splitDP [ i ] = 1 NEW_LINE DEDENT if ( splitDP [ i ] != - 1 ) : NEW_LINE INDENT for j in range ( 1 , 10 ) : NEW_LINE INDENT if ( i + j > numLen ) : NEW_LINE INDENT break ; NEW_LINE DEDENT if ( checkOdd ( number [ i : i + j ] ) ) : NEW_LINE INDENT if ( splitDP [ i + j ] == - 1 ) : NEW_LINE INDENT splitDP [ i + j ] = 1 + splitDP [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT splitDP [ i + j ] = min ( splitDP [ i + j ] , 1 + splitDP [ i ] ) NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT return splitDP [ numLen ] NEW_LINE DEDENT print ( splitIntoOdds ( "123456789123456789123" ) ) NEW_LINE
T39942
import java . util . * ; class GFG { static void findIntegers ( int n , int x , int y ) { ArrayList < Integer > ans = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) ans . add ( 1 ) ; if ( y - ( n - 1 ) <= 0 ) { System . out . print ( " - 1" ) ; return ; } ans . add ( y - ( n - 1 ) ) ; int store = 0 ; for ( int i = 0 ; i < n ; i ++ ) store += ans . get ( i ) * ans . get ( i ) ; if ( store < x ) { System . out . print ( " - 1" ) ; return ; } for ( int i = 0 ; i < n ; i ++ ) System . out . print ( ans . get ( i ) + " ▁ " ) ; } public static void main ( String [ ] args ) { int n = 3 , x = 254 , y = 18 ; findIntegers ( n , x , y ) ; } }
def findIntegers ( n , x , y ) : NEW_LINE INDENT ans = [ ] NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT ans . append ( 1 ) NEW_LINE DEDENT if ( y - ( n - 1 ) <= 0 ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return NEW_LINE DEDENT ans . append ( y - ( n - 1 ) ) NEW_LINE store = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT store += ans [ i ] * ans [ i ] NEW_LINE DEDENT if ( store < x ) : NEW_LINE INDENT print ( " - 1" , end = " " ) NEW_LINE return ; NEW_LINE DEDENT for i in range ( n ) : NEW_LINE INDENT print ( ans [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT n , x , y = 3 , 254 , 18 NEW_LINE findIntegers ( n , x , y ) NEW_LINE
T39943
import java . io . * ; import java . util . * ; class Graph { private int V ; private LinkedList < Integer > adj [ ] ; Graph ( int v ) { V = v ; adj = new LinkedList [ v ] ; for ( int i = 0 ; i < v ; ++ i ) adj [ i ] = new LinkedList ( ) ; } void addEdge ( int v , int w ) { adj [ v ] . add ( w ) ; } void DFSUtil ( int v , boolean visited [ ] ) { visited [ v ] = true ; System . out . print ( v + " ▁ " ) ; Iterator < Integer > i = adj [ v ] . listIterator ( ) ; while ( i . hasNext ( ) ) { int n = i . next ( ) ; if ( ! visited [ n ] ) DFSUtil ( n , visited ) ; } } void DFS ( ) { boolean visited [ ] = new boolean [ V ] ; for ( int i = 0 ; i < V ; ++ i ) if ( visited [ i ] == false ) DFSUtil ( i , visited ) ; } public static void main ( String args [ ] ) { Graph g = new Graph ( 4 ) ; g . addEdge ( 0 , 1 ) ; g . addEdge ( 0 , 2 ) ; g . addEdge ( 1 , 2 ) ; g . addEdge ( 2 , 0 ) ; g . addEdge ( 2 , 3 ) ; g . addEdge ( 3 , 3 ) ; System . out . println ( " Following ▁ is ▁ Depth ▁ First ▁ Traversal " ) ; g . DFS ( ) ; } }
from collections import defaultdict NEW_LINE class Graph : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . graph = defaultdict ( list ) NEW_LINE DEDENT def addEdge ( self , u , v ) : NEW_LINE INDENT self . graph [ u ] . append ( v ) NEW_LINE DEDENT def DFSUtil ( self , v , visited ) : NEW_LINE INDENT visited [ v ] = True NEW_LINE print v , NEW_LINE for i in self . graph [ v ] : NEW_LINE INDENT if visited [ i ] == False : NEW_LINE INDENT self . DFSUtil ( i , visited ) NEW_LINE DEDENT DEDENT DEDENT def DFS ( self ) : NEW_LINE INDENT V = len ( self . graph ) NEW_LINE visited = [ False ] * ( V ) NEW_LINE for i in range ( V ) : NEW_LINE INDENT if visited [ i ] == False : NEW_LINE INDENT self . DFSUtil ( i , visited ) NEW_LINE DEDENT DEDENT DEDENT DEDENT g = Graph ( ) NEW_LINE g . addEdge ( 0 , 1 ) NEW_LINE g . addEdge ( 0 , 2 ) NEW_LINE g . addEdge ( 1 , 2 ) NEW_LINE g . addEdge ( 2 , 0 ) NEW_LINE g . addEdge ( 2 , 3 ) NEW_LINE g . addEdge ( 3 , 3 ) NEW_LINE print " Following ▁ is ▁ Depth ▁ First ▁ Traversal " NEW_LINE g . DFS ( ) NEW_LINE
T39944
class GFG { static void smallestPermute ( int n ) { char res [ ] = new char [ n + 1 ] ; if ( n % 2 == 0 ) { for ( int i = 0 ; i < n ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = ( char ) ( 48 + i + 2 ) ; else res [ i ] = ( char ) ( 48 + i ) ; } } else { for ( int i = 0 ; i < n - 2 ; i ++ ) { if ( i % 2 == 0 ) res [ i ] = ( char ) ( 48 + i + 2 ) ; else res [ i ] = ( char ) ( 48 + i ) ; } res [ n - 1 ] = ( char ) ( 48 + n - 2 ) ; res [ n - 2 ] = ( char ) ( 48 + n ) ; res [ n - 3 ] = ( char ) ( 48 + n - 1 ) ; } res [ n ] = ' \0' ; for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( res [ i ] ) ; } } public static void main ( String [ ] args ) { int n = 7 ; smallestPermute ( n ) ; } }
def smallestPermute ( n ) : NEW_LINE INDENT res = [ " " ] * ( n + 1 ) NEW_LINE if ( n % 2 == 0 ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT res [ i ] = chr ( 48 + i + 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT res [ i ] = chr ( 48 + i ) NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT for i in range ( n - 2 ) : NEW_LINE INDENT if ( i % 2 == 0 ) : NEW_LINE INDENT res [ i ] = chr ( 48 + i + 2 ) NEW_LINE DEDENT else : NEW_LINE INDENT res [ i ] = chr ( 48 + i ) NEW_LINE DEDENT DEDENT res [ n - 1 ] = chr ( 48 + n - 2 ) NEW_LINE res [ n - 2 ] = chr ( 48 + n ) NEW_LINE res [ n - 3 ] = chr ( 48 + n - 1 ) NEW_LINE DEDENT res = ' ' . join ( res ) NEW_LINE return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 7 NEW_LINE print ( smallestPermute ( n ) ) NEW_LINE DEDENT
T39945
import java . util . * ; class GFG { static String FirstAndLast ( String str ) { String [ ] arrOfStr = str . split ( " ▁ " ) ; String res = " " ; for ( String a : arrOfStr ) { res += a . substring ( 1 , a . length ( ) - 1 ) + " ▁ " ; } return res ; } public static void main ( String args [ ] ) { String str = " Geeks ▁ for ▁ Geeks " ; System . out . println ( str ) ; System . out . println ( FirstAndLast ( str ) ) ; } }
def FirstAndLast ( string ) : NEW_LINE INDENT arrOfStr = string . split ( ) ; NEW_LINE res = " " ; NEW_LINE for a in arrOfStr : NEW_LINE INDENT res += a [ 1 : len ( a ) - 1 ] + " ▁ " ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " Geeks ▁ for ▁ Geeks " ; NEW_LINE print ( string ) ; NEW_LINE print ( FirstAndLast ( string ) ) ; NEW_LINE DEDENT
T39946
class Main { static int search ( int arr [ ] , int l , int h , int key ) { if ( l > h ) return - 1 ; int mid = ( l + h ) / 2 ; if ( arr [ mid ] == key ) return mid ; if ( arr [ l ] <= arr [ mid ] ) { if ( key >= arr [ l ] && key <= arr [ mid ] ) return search ( arr , l , mid - 1 , key ) ; return search ( arr , mid + 1 , h , key ) ; } if ( key >= arr [ mid ] && key <= arr [ h ] ) return search ( arr , mid + 1 , h , key ) ; return search ( arr , l , mid - 1 , key ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 4 , 5 , 6 , 7 , 8 , 9 , 1 , 2 , 3 } ; int n = arr . length ; int key = 6 ; int i = search ( arr , 0 , n - 1 , key ) ; if ( i != - 1 ) System . out . println ( " Index : ▁ " + i ) ; else System . out . println ( " Key ▁ not ▁ found " ) ; } }
def search ( arr , l , h , key ) : NEW_LINE INDENT if l > h : NEW_LINE INDENT return - 1 NEW_LINE DEDENT mid = ( l + h ) // 2 NEW_LINE if arr [ mid ] == key : NEW_LINE INDENT return mid NEW_LINE DEDENT if arr [ l ] <= arr [ mid ] : NEW_LINE INDENT if key >= arr [ l ] and key <= arr [ mid ] : NEW_LINE INDENT return search ( arr , l , mid - 1 , key ) NEW_LINE DEDENT return search ( arr , mid + 1 , h , key ) NEW_LINE DEDENT if key >= arr [ mid ] and key <= arr [ h ] : NEW_LINE INDENT return search ( a , mid + 1 , h , key ) NEW_LINE DEDENT return search ( arr , l , mid - 1 , key ) NEW_LINE DEDENT arr = [ 4 , 5 , 6 , 7 , 8 , 9 , 1 , 2 , 3 ] NEW_LINE key = 6 NEW_LINE i = search ( arr , 0 , len ( arr ) - 1 , key ) NEW_LINE if i != - 1 : NEW_LINE INDENT print ( " Index : ▁ % d " % i ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Key ▁ not ▁ found " ) NEW_LINE DEDENT
T39947
import java . util . * ; public class SortExample { static void mySort ( Integer [ ] arr ) { int n = arr . length ; Arrays . sort ( arr , 0 , n / 2 ) ; Arrays . sort ( arr , n / 2 , n , Collections . reverseOrder ( ) ) ; } public static void main ( String [ ] args ) { Integer [ ] arr = { 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , 7 } ; mySort ( arr ) ; System . out . printf ( " Modified ▁ arr [ ] ▁ : ▁ % s " , Arrays . toString ( arr ) ) ; } }
def mySort ( arr , n ) : NEW_LINE INDENT arr1 = arr [ : n // 2 ] NEW_LINE arr2 = arr [ n // 2 : ] NEW_LINE arr1 . sort ( ) NEW_LINE arr2 . sort ( reverse = True ) NEW_LINE return arr1 + arr2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 4 , 6 , 2 , 1 , 3 , 8 , 9 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE arr = mySort ( arr , n ) NEW_LINE print ( " Modified ▁ Array ▁ : ▁ " ) NEW_LINE print ( arr ) NEW_LINE DEDENT
T39948
import java . util . * ; class GFG { static class Node { int data ; Node next ; } ; static Node push ( Node head_ref , int new_data ) { Node new_node = new Node ( ) ; new_node . data = new_data ; new_node . next = ( head_ref ) ; ( head_ref ) = new_node ; return head_ref ; } static int getCount ( Node head ) { int count = 0 ; Node current = head ; while ( current != null ) { count ++ ; current = current . next ; } return count ; } static int getNth ( Node head , int n ) { Node curr = head ; for ( int i = 0 ; i < n - 1 && curr != null ; i ++ ) curr = curr . next ; return curr . data ; } static void printReverse ( Node head ) { Stack < Node > stk = new Stack < Node > ( ) ; Node ptr = head ; while ( ptr != null ) { stk . push ( ptr ) ; ptr = ptr . next ; } while ( stk . size ( ) > 0 ) { System . out . print ( stk . peek ( ) . data + " ▁ " ) ; stk . pop ( ) ; } System . out . println ( " \n " ) ; } public static void main ( String args [ ] ) { Node head = null ; head = push ( head , 5 ) ; head = push ( head , 4 ) ; head = push ( head , 3 ) ; head = push ( head , 2 ) ; head = push ( head , 1 ) ; printReverse ( head ) ; } }
class Node : NEW_LINE INDENT def __init__ ( self , next = None , data = None ) : NEW_LINE INDENT self . next = next NEW_LINE self . data = data NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE return head_ref NEW_LINE DEDENT def getCount ( head ) : NEW_LINE INDENT count = 0 NEW_LINE current = head NEW_LINE while ( current != None ) : NEW_LINE INDENT count = count + 1 NEW_LINE current = current . next NEW_LINE DEDENT return count NEW_LINE DEDENT def getNth ( head , n ) : NEW_LINE INDENT curr = head NEW_LINE i = 0 NEW_LINE while ( i < n - 1 and curr != None ) : NEW_LINE INDENT curr = curr . next NEW_LINE i = i + 1 NEW_LINE DEDENT return curr . data NEW_LINE DEDENT def printReverse ( head ) : NEW_LINE INDENT stk = [ ] NEW_LINE ptr = head NEW_LINE while ( ptr != None ) : NEW_LINE INDENT stk . append ( ptr ) NEW_LINE ptr = ptr . next NEW_LINE DEDENT while ( len ( stk ) > 0 ) : NEW_LINE INDENT print ( stk [ - 1 ] . data , end = " ▁ " ) NEW_LINE stk . pop ( ) NEW_LINE DEDENT print ( " ▁ " ) NEW_LINE DEDENT head = None NEW_LINE head = push ( head , 5 ) NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 3 ) NEW_LINE head = push ( head , 2 ) NEW_LINE head = push ( head , 1 ) NEW_LINE printReverse ( head ) NEW_LINE
T39949
import java . util . * ; class solution { static int no_of_ways ( String s ) { int n = s . length ( ) ; int count_left = 0 , count_right = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( s . charAt ( i ) == s . charAt ( 0 ) ) { ++ count_left ; } else break ; } for ( int i = n - 1 ; i >= 0 ; -- i ) { if ( s . charAt ( i ) == s . charAt ( n - 1 ) ) { ++ count_right ; } else break ; } if ( s . charAt ( 0 ) == s . charAt ( n - 1 ) ) return ( ( count_left + 1 ) * ( count_right + 1 ) ) ; else return ( count_left + count_right + 1 ) ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; System . out . println ( no_of_ways ( s ) ) ; } }
def no_of_ways ( s ) : NEW_LINE INDENT n = len ( s ) NEW_LINE count_left = 0 NEW_LINE count_right = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT if ( s [ i ] == s [ 0 ] ) : NEW_LINE INDENT count_left += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT i = n - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( s [ i ] == s [ n - 1 ] ) : NEW_LINE INDENT count_right += 1 NEW_LINE DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT if ( s [ 0 ] == s [ n - 1 ] ) : NEW_LINE INDENT return ( ( count_left + 1 ) * ( count_right + 1 ) ) NEW_LINE DEDENT else : NEW_LINE INDENT return ( count_left + count_right + 1 ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE print ( no_of_ways ( s ) ) NEW_LINE DEDENT
T39950
class GFG { static boolean productEqual ( int n ) { if ( n < 10 ) return false ; int prodOdd = 1 , prodEven = 1 ; while ( n > 0 ) { int digit = n % 10 ; prodOdd *= digit ; n /= 10 ; if ( n == 0 ) break ; digit = n % 10 ; prodEven *= digit ; n /= 10 ; } if ( prodEven == prodOdd ) return true ; return false ; } public static void main ( String args [ ] ) { int n = 4324 ; if ( productEqual ( n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
def productEqual ( n ) : NEW_LINE INDENT if n < 10 : NEW_LINE INDENT return False NEW_LINE DEDENT prodOdd = 1 ; prodEven = 1 NEW_LINE while n > 0 : NEW_LINE INDENT digit = n % 10 NEW_LINE prodOdd *= digit NEW_LINE n = n // 10 NEW_LINE if n == 0 : NEW_LINE INDENT break ; NEW_LINE DEDENT digit = n % 10 NEW_LINE prodEven *= digit NEW_LINE n = n // 10 NEW_LINE DEDENT if prodOdd == prodEven : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT n = 4324 NEW_LINE if productEqual ( n ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
T39951
import java . util . * ; class GFG { static int nCr ( int n , int r ) { if ( r > n / 2 ) r = n - r ; int answer = 1 ; for ( int i = 1 ; i <= r ; i ++ ) { answer *= ( n - r + i ) ; answer /= i ; } return answer ; } static float binomialProbability ( int n , int k , float p ) { return nCr ( n , k ) * ( float ) Math . pow ( p , k ) * ( float ) Math . pow ( 1 - p , n - k ) ; } public static void main ( String [ ] args ) { int n = 10 ; int k = 5 ; float p = ( float ) 1.0 / 3 ; float probability = binomialProbability ( n , k , p ) ; System . out . print ( " Probability ▁ of ▁ " + k ) ; System . out . print ( " ▁ heads ▁ when ▁ a ▁ coin ▁ is ▁ tossed ▁ " + n ) ; System . out . println ( " ▁ times ▁ where ▁ probability ▁ of ▁ each ▁ head ▁ is ▁ " + p ) ; System . out . println ( " ▁ is ▁ = ▁ " + probability ) ; } }
def nCr ( n , r ) : NEW_LINE INDENT if ( r > n / 2 ) : NEW_LINE INDENT r = n - r ; NEW_LINE DEDENT answer = 1 ; NEW_LINE for i in range ( 1 , r + 1 ) : NEW_LINE INDENT answer *= ( n - r + i ) ; NEW_LINE answer /= i ; NEW_LINE DEDENT return answer ; NEW_LINE DEDENT def binomialProbability ( n , k , p ) : NEW_LINE INDENT return ( nCr ( n , k ) * pow ( p , k ) * pow ( 1 - p , n - k ) ) ; NEW_LINE DEDENT n = 10 ; NEW_LINE k = 5 ; NEW_LINE p = 1.0 / 3 ; NEW_LINE probability = binomialProbability ( n , k , p ) ; NEW_LINE print ( " Probability ▁ of " , k , " heads ▁ when ▁ a ▁ coin ▁ is ▁ tossed " , end = " ▁ " ) ; NEW_LINE print ( n , " times ▁ where ▁ probability ▁ of ▁ each ▁ head ▁ is " , round ( p , 6 ) ) ; NEW_LINE print ( " is ▁ = ▁ " , round ( probability , 6 ) ) ; NEW_LINE
T39952
import java . io . * ; class GFG { static int countEvenOdd ( char num [ ] , int n ) { int even_count = 0 ; int odd_count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { int x = num [ i ] - 48 ; if ( x % 2 == 0 ) even_count ++ ; else odd_count ++ ; } System . out . println ( " Even ▁ count ▁ : ▁ " + even_count ) ; System . out . println ( " Odd ▁ count ▁ : ▁ " + odd_count ) ; if ( even_count % 2 == 0 && odd_count % 2 != 0 ) return 1 ; else return 0 ; } public static void main ( String [ ] args ) { char num [ ] = { 1 , 2 , 3 } ; int n = num . length ; int t = countEvenOdd ( num , n ) ; if ( t == 1 ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
def countEvenOdd ( num , n ) : NEW_LINE INDENT even_count = 0 ; NEW_LINE odd_count = 0 ; NEW_LINE num = list ( str ( num ) ) NEW_LINE for i in num : NEW_LINE INDENT if i in ( '0' , '2' , '4' , '6' , '8' ) : NEW_LINE INDENT even_count += 1 NEW_LINE DEDENT else : NEW_LINE INDENT odd_count += 1 NEW_LINE DEDENT DEDENT print ( " Even ▁ count ▁ : ▁ " , even_count ) ; NEW_LINE print ( " Odd ▁ count ▁ : ▁ " , odd_count ) ; NEW_LINE if ( even_count % 2 == 0 and odd_count % 2 != 0 ) : NEW_LINE INDENT return 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT DEDENT num = ( 1 , 2 , 3 ) ; NEW_LINE n = len ( num ) ; NEW_LINE t = countEvenOdd ( num , n ) ; NEW_LINE if t == 1 : NEW_LINE INDENT print ( " YES " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) ; NEW_LINE DEDENT
T39953
class GFG { static int countSubstring ( String S , int n ) { int ans = 0 ; int i = 0 ; while ( i < n ) { int cnt0 = 0 , cnt1 = 0 ; if ( S . charAt ( i ) == '0' ) { while ( i < n && S . charAt ( i ) == '0' ) { cnt0 ++ ; i ++ ; } int j = i ; while ( j < n && S . charAt ( j ) == '1' ) { cnt1 ++ ; j ++ ; } } else { while ( i < n && S . charAt ( i ) == '1' ) { cnt1 ++ ; i ++ ; } int j = i ; while ( j < n && S . charAt ( j ) == '0' ) { cnt0 ++ ; j ++ ; } } ans += Math . min ( cnt0 , cnt1 ) ; } return ans ; } static public void main ( String args [ ] ) { String S = "0001110010" ; int n = S . length ( ) ; System . out . println ( countSubstring ( S , n ) ) ; } }
def countSubstring ( S , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( i < n ) : NEW_LINE INDENT cnt0 = 0 ; cnt1 = 0 ; NEW_LINE if ( S [ i ] == '0' ) : NEW_LINE INDENT while ( i < n and S [ i ] == '0' ) : NEW_LINE INDENT cnt0 += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT j = i ; NEW_LINE while ( j < n and S [ j ] == '1' ) : NEW_LINE INDENT cnt1 += 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT while ( i < n and S [ i ] == '1' ) : NEW_LINE INDENT cnt1 += 1 ; NEW_LINE i += 1 ; NEW_LINE DEDENT j = i ; NEW_LINE while ( j < n and S [ j ] == '0' ) : NEW_LINE INDENT cnt0 += 1 ; NEW_LINE j += 1 ; NEW_LINE DEDENT DEDENT ans += min ( cnt0 , cnt1 ) ; NEW_LINE DEDENT return ans ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT S = "0001110010" ; NEW_LINE n = len ( S ) ; NEW_LINE print ( countSubstring ( S , n ) ) ; NEW_LINE DEDENT
T39954
class GFG { static String minNum ( char num [ ] , int k ) { int len = num . length ; if ( len == 0 || k == 0 ) { String num_str = new String ( num ) ; return num_str ; } if ( len == 1 ) return "0" ; if ( num [ 0 ] != '1' ) { num [ 0 ] = '1' ; k -- ; } int i = 1 ; while ( k > 0 && i < len ) { if ( num [ i ] != '0' ) { num [ i ] = '0' ; k -- ; } i ++ ; } String num_str = new String ( num ) ; return num_str ; } public static void main ( String args [ ] ) { String num = "91945" ; int k = 3 ; System . out . println ( minNum ( num . toCharArray ( ) , k ) ) ; } }
def minNum ( num , k ) : NEW_LINE INDENT len_ = len ( num ) NEW_LINE if len_ == 0 or k == 0 : NEW_LINE INDENT return num NEW_LINE DEDENT if len_ == 1 : NEW_LINE INDENT return "0" NEW_LINE DEDENT if num [ 0 ] != '1' : NEW_LINE INDENT num = '1' + num [ 1 : ] NEW_LINE k -= 1 NEW_LINE DEDENT i = 1 NEW_LINE while k > 0 and i < len_ : NEW_LINE INDENT if num [ i ] != '0' : NEW_LINE INDENT num = num [ : i ] + '0' + num [ i + 1 : ] NEW_LINE k -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return num NEW_LINE DEDENT num = "91945" NEW_LINE k = 3 NEW_LINE print ( minNum ( num , k ) ) NEW_LINE
T39955
class GFG { static boolean checkPattern ( String str , String pattern ) { int len = pattern . length ( ) ; if ( str . length ( ) < len ) { return false ; } for ( int i = 0 ; i < len - 1 ; i ++ ) { char x = pattern . charAt ( i ) ; char y = pattern . charAt ( i + 1 ) ; int last = str . lastIndexOf ( x ) ; int first = str . indexOf ( y ) ; if ( last == - 1 || first == - 1 || last > first ) { return false ; } } return true ; } public static void main ( String [ ] args ) { String str = " engineers ▁ rock " ; String pattern = " gsr " ; System . out . println ( checkPattern ( str , pattern ) ) ; } }
def checkPattern ( string , pattern ) : NEW_LINE INDENT l = len ( pattern ) NEW_LINE if len ( string ) < l : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( l - 1 ) : NEW_LINE INDENT x = pattern [ i ] NEW_LINE y = pattern [ i + 1 ] NEW_LINE last = string . rindex ( x ) NEW_LINE first = string . index ( y ) NEW_LINE if last == - 1 or first == - 1 or last > first : 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 = " gsr " NEW_LINE print ( checkPattern ( string , pattern ) ) NEW_LINE DEDENT
T39956
import java . util . * ; public class GeeksForGeeks { public static int longestAr ( int n , int arr [ ] ) { Hashtable < Integer , Integer > count = new Hashtable < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( count . containsKey ( arr [ i ] ) ) count . put ( arr [ i ] , count . get ( arr [ i ] ) + 1 ) ; else count . put ( arr [ i ] , 1 ) ; } Set < Integer > kset = count . keySet ( ) ; Iterator < Integer > it = kset . iterator ( ) ; int max = 0 ; while ( it . hasNext ( ) ) { int a = ( int ) it . next ( ) ; int cur = 0 ; int cur1 = 0 ; int cur2 = 0 ; if ( count . containsKey ( a + 1 ) ) cur1 = count . get ( a + 1 ) ; if ( count . containsKey ( a - 1 ) ) cur2 = count . get ( a - 1 ) ; cur = count . get ( a ) + Math . max ( cur1 , cur2 ) ; if ( cur > max ) max = cur ; } return ( max ) ; } public static void main ( String [ ] args ) { int n = 8 ; int arr [ ] = { 2 , 2 , 3 , 5 , 5 , 6 , 6 , 6 } ; int maxLen = longestAr ( n , arr ) ; System . out . println ( maxLen ) ; } }
def longestAr ( n , arr ) : NEW_LINE INDENT count = dict ( ) NEW_LINE for i in arr : NEW_LINE INDENT count [ i ] = count . get ( i , 0 ) + 1 NEW_LINE DEDENT kset = count . keys ( ) NEW_LINE maxm = 0 NEW_LINE for it in list ( kset ) : NEW_LINE INDENT a = it NEW_LINE cur = 0 NEW_LINE cur1 = 0 NEW_LINE cur2 = 0 NEW_LINE if ( ( a + 1 ) in count ) : NEW_LINE INDENT cur1 = count [ a + 1 ] NEW_LINE DEDENT if ( ( a - 1 ) in count ) : NEW_LINE INDENT cur2 = count [ a - 1 ] NEW_LINE DEDENT cur = count [ a ] + max ( cur1 , cur2 ) NEW_LINE if ( cur > maxm ) : NEW_LINE INDENT maxm = cur NEW_LINE DEDENT DEDENT return maxm NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 8 NEW_LINE arr = [ 2 , 2 , 3 , 5 , 5 , 6 , 6 , 6 ] NEW_LINE maxLen = longestAr ( n , arr ) NEW_LINE print ( maxLen ) NEW_LINE DEDENT
T39957
import java . io . * ; class GFG { static int toggleBitsFromLToR ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( n ^ num ) ; } static int unsetBitsInGivenRange ( int n , int l , int r ) { int num = ( 1 << ( 4 * 8 - 1 ) ) - 1 ; num = toggleBitsFromLToR ( num , l , r ) ; return ( n & num ) ; } public static void main ( String [ ] args ) { int n = 42 ; int l = 2 , r = 5 ; System . out . println ( unsetBitsInGivenRange ( n , l , r ) ) ; } }
def toggleBitsFromLToR ( n , l , r ) : NEW_LINE INDENT num = ( ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ) NEW_LINE return ( n ^ num ) NEW_LINE DEDENT def unsetBitsInGivenRange ( n , l , r ) : NEW_LINE INDENT num = ( 1 << ( 4 * 8 - 1 ) ) - 1 NEW_LINE num = toggleBitsFromLToR ( num , l , r ) NEW_LINE return ( n & num ) NEW_LINE DEDENT n = 42 NEW_LINE l = 2 NEW_LINE r = 5 NEW_LINE print ( unsetBitsInGivenRange ( n , l , r ) ) NEW_LINE
T39958
import java . io . * ; class GFG { public static boolean checkdigit ( int n , int k ) { while ( n != 0 ) { int rem = n % 10 ; if ( rem == k ) return true ; n = n / 10 ; } return false ; } public static int findNthNumber ( int n , int k ) { for ( int i = k + 1 , count = 1 ; count < n ; i ++ ) { if ( checkdigit ( i , k ) || ( i % k == 0 ) ) count ++ ; if ( count == n ) return i ; } return - 1 ; } public static void main ( String [ ] args ) { int n = 10 , k = 2 ; System . out . println ( findNthNumber ( n , k ) ) ; } }
def checkdigit ( n , k ) : NEW_LINE INDENT while ( n ) : NEW_LINE INDENT rem = n % 10 NEW_LINE if ( rem == k ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT n = n / 10 NEW_LINE DEDENT return 0 NEW_LINE DEDENT def findNthNumber ( n , k ) : NEW_LINE INDENT i = k + 1 NEW_LINE count = 1 NEW_LINE while ( count < n ) : NEW_LINE INDENT if ( checkdigit ( i , k ) or ( i % k == 0 ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT if ( count == n ) : NEW_LINE INDENT return i NEW_LINE DEDENT i += 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT n = 10 NEW_LINE k = 2 NEW_LINE print ( findNthNumber ( n , k ) ) NEW_LINE
T39959
import java . util . Arrays ; class GFG { final static int MAX5 = 100 ; static int maximumZeros ( int arr [ ] , int n , int k ) { int subset [ ] [ ] = new int [ k + 1 ] [ MAX5 + 5 ] ; for ( int [ ] row : subset ) { Arrays . fill ( row , - 1 ) ; } subset [ 0 ] [ 0 ] = 0 ; for ( int p = 0 ; p < n ; p ++ ) { int pw2 = 0 , pw5 = 0 ; while ( arr [ p ] % 2 == 0 ) { pw2 ++ ; arr [ p ] /= 2 ; } while ( arr [ p ] % 5 == 0 ) { pw5 ++ ; arr [ p ] /= 5 ; } for ( int i = k - 1 ; i >= 0 ; i -- ) { for ( int j = 0 ; j < MAX5 ; j ++ ) { if ( subset [ i ] [ j ] != - 1 ) { subset [ i + 1 ] [ j + pw5 ] = Math . max ( subset [ i + 1 ] [ j + pw5 ] , subset [ i ] [ j ] + pw2 ) ; } } } } int ans = 0 ; for ( int i = 0 ; i < MAX5 ; i ++ ) { ans = Math . max ( ans , Math . min ( i , subset [ k ] [ i ] ) ) ; } return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 50 , 4 , 20 } ; int k = 2 ; int n = arr . length ; System . out . println ( maximumZeros ( arr , n , k ) ) ; } }
MAX5 = 100 NEW_LINE def maximumZeros ( arr , n , k ) : NEW_LINE INDENT global MAX5 NEW_LINE subset = [ [ - 1 ] * ( MAX5 + 5 ) for _ in range ( k + 1 ) ] NEW_LINE subset [ 0 ] [ 0 ] = 0 NEW_LINE for p in arr : NEW_LINE INDENT pw2 , pw5 = 0 , 0 NEW_LINE while not p % 2 : NEW_LINE INDENT pw2 += 1 NEW_LINE p //= 2 NEW_LINE DEDENT while not p % 5 : NEW_LINE INDENT pw5 += 1 NEW_LINE p //= 5 NEW_LINE DEDENT for i in range ( k - 1 , - 1 , - 1 ) : NEW_LINE INDENT for j in range ( MAX5 ) : NEW_LINE INDENT if subset [ i ] [ j ] != - 1 : NEW_LINE INDENT subset [ i + 1 ] [ j + pw5 ] = ( max ( subset [ i + 1 ] [ j + pw5 ] , ( subset [ i ] [ j ] + pw2 ) ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT ans = 0 NEW_LINE for i in range ( MAX5 ) : NEW_LINE INDENT ans = max ( ans , min ( i , subset [ k ] [ i ] ) ) NEW_LINE DEDENT return ans NEW_LINE DEDENT arr = [ 50 , 4 , 20 ] NEW_LINE k = 2 NEW_LINE n = len ( arr ) NEW_LINE print ( maximumZeros ( arr , n , k ) ) NEW_LINE
T39960
import java . io . * ; class GFG { static int arrSize = 51 ; static int dp [ ] = new int [ arrSize ] ; static boolean v [ ] = new boolean [ arrSize ] ; static int sumMax ( int i , int arr [ ] , int n ) { if ( i >= n - 1 ) return 0 ; if ( v [ i ] ) return dp [ i ] ; v [ i ] = true ; dp [ i ] = Math . max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) ; return dp [ i ] ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 , 1 } ; int n = arr . length ; System . out . println ( sumMax ( 0 , arr , n ) ) ; } }
arrSize = 51 NEW_LINE dp = [ 0 for i in range ( arrSize ) ] NEW_LINE v = [ False for i in range ( arrSize ) ] NEW_LINE def sumMax ( i , arr , n ) : NEW_LINE INDENT if ( i >= n - 1 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( v [ i ] ) : NEW_LINE INDENT return dp [ i ] NEW_LINE DEDENT v [ i ] = True NEW_LINE dp [ i ] = max ( arr [ i ] + arr [ i + 1 ] + sumMax ( i + 3 , arr , n ) , sumMax ( i + 1 , arr , n ) ) NEW_LINE return dp [ i ] NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE print ( sumMax ( 0 , arr , n ) ) NEW_LINE DEDENT
T39961
class GFG { static long powermod ( long x , long y , long p ) { long res = 1 ; x = x % p ; while ( y > 0 ) { if ( ( y & 1L ) > 0 ) res = ( res * x ) % p ; y = y >> 1L ; x = ( x * x ) % p ; } return res ; } static long modInverse ( long a , long m ) { long m0 = m , t , q ; long x0 = 0 , x1 = 1 ; if ( m == 1 ) return 0 ; while ( a > 1 ) { q = a / m ; t = m ; m = a % m ; a = t ; t = x0 ; x0 = x1 - q * x0 ; x1 = t ; } if ( x1 < 0 ) x1 += m0 ; return x1 ; } static long evaluteExpression ( long n ) { long firstsum = 0 , mod = 10 ; for ( long i = 2 , j = 0 ; ( 1L << j ) <= n ; i *= i , ++ j ) firstsum = ( firstsum + i ) % mod ; long secondsum = ( powermod ( 4L , n + 1 , mod ) - 1 ) * modInverse ( 3L , mod ) ; return ( firstsum * secondsum ) % mod ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . println ( evaluteExpression ( n ) ) ; n = 10 ; System . out . println ( evaluteExpression ( n ) ) ; } }
def powermod ( x , y , p ) : NEW_LINE INDENT res = 1 ; NEW_LINE x = x % p ; NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( ( y & 1 ) > 0 ) : NEW_LINE INDENT res = ( res * x ) % p ; NEW_LINE DEDENT y = y >> 1 ; NEW_LINE x = ( x * x ) % p ; NEW_LINE DEDENT return res ; NEW_LINE DEDENT def modInverse ( a , m ) : NEW_LINE INDENT m0 = m ; NEW_LINE x0 = 0 ; NEW_LINE x1 = 1 ; NEW_LINE if ( m == 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT while ( a > 1 ) : NEW_LINE INDENT q = int ( a / m ) ; NEW_LINE t = m ; NEW_LINE m = a % m ; NEW_LINE a = t ; NEW_LINE t = x0 ; NEW_LINE x0 = x1 - q * x0 ; NEW_LINE x1 = t ; NEW_LINE DEDENT if ( x1 < 0 ) : NEW_LINE INDENT x1 += m0 ; NEW_LINE DEDENT return x1 ; NEW_LINE DEDENT def evaluteExpression ( n ) : NEW_LINE INDENT firstsum = 0 ; NEW_LINE mod = 10 ; NEW_LINE i = 2 ; NEW_LINE j = 0 ; NEW_LINE while ( ( 1 << j ) <= n ) : NEW_LINE INDENT firstsum = ( firstsum + i ) % mod ; NEW_LINE i *= i ; NEW_LINE j += 1 ; NEW_LINE DEDENT secondsum = ( powermod ( 4 , n + 1 , mod ) - 1 ) * modInverse ( 3 , mod ) ; NEW_LINE return ( firstsum * secondsum ) % mod ; NEW_LINE DEDENT n = 3 ; NEW_LINE print ( evaluteExpression ( n ) ) ; NEW_LINE n = 10 ; NEW_LINE print ( evaluteExpression ( n ) ) ; NEW_LINE
T39962
public class GFG { static double calculateAlternateSum ( int n ) { if ( n <= 0 ) return 0 ; int fibo [ ] = new int [ n + 1 ] ; fibo [ 0 ] = 0 ; fibo [ 1 ] = 1 ; double sum = Math . pow ( fibo [ 0 ] , 2 ) + Math . pow ( fibo [ 1 ] , 2 ) ; for ( int i = 2 ; i <= n ; i ++ ) { fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] ; if ( i % 2 == 0 ) sum -= fibo [ i ] ; else sum += fibo [ i ] ; } return sum ; } public static void main ( String args [ ] ) { int n = 8 ; System . out . println ( " Alternating ▁ Fibonacci ▁ Sum ▁ upto ▁ " + n + " ▁ terms : ▁ " + calculateAlternateSum ( n ) ) ; } }
def calculateAlternateSum ( n ) : NEW_LINE INDENT if ( n <= 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT fibo = [ 0 ] * ( n + 1 ) NEW_LINE fibo [ 0 ] = 0 NEW_LINE fibo [ 1 ] = 1 NEW_LINE sum = pow ( fibo [ 0 ] , 2 ) + pow ( fibo [ 1 ] , 2 ) NEW_LINE for i in range ( 2 , n + 1 ) : NEW_LINE INDENT fibo [ i ] = fibo [ i - 1 ] + fibo [ i - 2 ] NEW_LINE if ( i % 2 == 0 ) : NEW_LINE INDENT sum -= fibo [ i ] NEW_LINE DEDENT else : NEW_LINE INDENT sum += fibo [ i ] NEW_LINE DEDENT DEDENT return sum NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 8 NEW_LINE print ( " Alternating ▁ Fibonacci ▁ Sum ▁ upto ▁ " , n , " ▁ terms : ▁ " , calculateAlternateSum ( n ) ) NEW_LINE DEDENT
T39963
import java . util . * ; class solution { static int countPieces ( int N ) { return 2 * N ; } public static void main ( String args [ ] ) { int N = 100 ; System . out . println ( countPieces ( N ) ) ; } }
def countPieces ( N ) : NEW_LINE INDENT return 2 * N NEW_LINE DEDENT N = 100 NEW_LINE print ( countPieces ( N ) ) NEW_LINE
T39964
class GFG { static void findWinner ( int n ) { if ( ( n - 1 ) % 6 == 0 ) { System . out . println ( " Second ▁ Player ▁ wins ▁ the ▁ game " ) ; } else { System . out . println ( " First ▁ Player ▁ wins ▁ the ▁ game " ) ; } } public static void main ( String [ ] args ) { int n = 7 ; findWinner ( n ) ; } }
def findWinner ( n ) : NEW_LINE INDENT if ( ( n - 1 ) % 6 == 0 ) : NEW_LINE INDENT print ( " Second ▁ Player ▁ wins ▁ the ▁ game " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " First ▁ Player ▁ wins ▁ the ▁ game " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 7 ; NEW_LINE findWinner ( n ) ; NEW_LINE DEDENT
T39965
class GFG { static int msb ( int x ) { int ret = 0 ; while ( ( x >> ( ret + 1 ) ) != 0 ) ret ++ ; return ret ; } static int xorRange ( int l , int r ) { int max_bit = msb ( r ) ; int mul = 2 ; int ans = 0 ; for ( int i = 1 ; i <= max_bit ; i ++ ) { if ( ( l / mul ) * mul == ( r / mul ) * mul ) { if ( ( ( l & ( 1 << i ) ) != 0 ) && ( r - l + 1 ) % 2 == 1 ) ans += mul ; mul *= 2 ; continue ; } int odd_c = 0 ; if ( ( ( l & ( 1 << i ) ) != 0 ) && l % 2 == 1 ) odd_c = ( odd_c ^ 1 ) ; if ( ( ( r & ( 1 << i ) ) != 0 ) && r % 2 == 0 ) odd_c = ( odd_c ^ 1 ) ; if ( odd_c != 0 ) ans += mul ; mul *= 2 ; } int zero_bit_cnt = zero_bit_cnt = ( r - l + 1 ) / 2 ; if ( l % 2 == 1 && r % 2 == 1 ) zero_bit_cnt ++ ; if ( zero_bit_cnt % 2 == 1 ) ans ++ ; return ans ; } public static void main ( String args [ ] ) { int l = 1 , r = 4 ; System . out . print ( xorRange ( l , r ) ) ; } }
def msb ( x ) : NEW_LINE INDENT ret = 0 NEW_LINE while ( ( x >> ( ret + 1 ) ) != 0 ) : NEW_LINE INDENT ret = ret + 1 NEW_LINE DEDENT return ret NEW_LINE DEDENT def xorRange ( l , r ) : NEW_LINE INDENT max_bit = msb ( r ) NEW_LINE mul = 2 NEW_LINE ans = 0 NEW_LINE for i in range ( 1 , max_bit + 1 ) : NEW_LINE INDENT if ( ( l // mul ) * mul == ( r // mul ) * mul ) : NEW_LINE INDENT if ( ( ( ( l & ( 1 << i ) ) != 0 ) and ( r - l + 1 ) % 2 == 1 ) ) : NEW_LINE INDENT ans = ans + mul NEW_LINE DEDENT mul = mul * 2 NEW_LINE continue NEW_LINE DEDENT odd_c = 0 NEW_LINE if ( ( ( l & ( 1 << i ) ) != 0 ) and l % 2 == 1 ) : NEW_LINE INDENT odd_c = ( odd_c ^ 1 ) NEW_LINE DEDENT if ( ( ( r & ( 1 << i ) ) != 0 ) and r % 2 == 0 ) : NEW_LINE INDENT odd_c = ( odd_c ^ 1 ) NEW_LINE DEDENT if ( odd_c ) : NEW_LINE INDENT ans = ans + mul NEW_LINE DEDENT mul = mul * 2 NEW_LINE DEDENT zero_bit_cnt = ( r - l + 1 ) // 2 NEW_LINE if ( ( l % 2 == 1 ) and ( r % 2 == 1 ) ) : NEW_LINE INDENT zero_bit_cnt = zero_bit_cnt + 1 NEW_LINE DEDENT if ( zero_bit_cnt % 2 == 1 ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT l = 1 NEW_LINE r = 4 NEW_LINE print ( xorRange ( l , r ) ) NEW_LINE
T39966
import java . lang . * ; import java . util . * ; class GFG { static final int MAX_CHAR = 26 ; public static int minChanges ( String str ) { int n = str . length ( ) ; if ( n > MAX_CHAR ) return - 1 ; int dist_count = 0 ; int count [ ] = new int [ MAX_CHAR ] ; for ( int i = 0 ; i < MAX_CHAR ; i ++ ) count [ i ] = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( count [ str . charAt ( i ) - ' a ' ] == 0 ) dist_count ++ ; count [ str . charAt ( i ) - ' a ' ] ++ ; } return ( n - dist_count ) ; } public static void main ( String [ ] args ) { String str = " aebaecedabbee " ; System . out . println ( minChanges ( str ) ) ; } }
MAX_CHAR = [ 26 ] NEW_LINE def minChanges ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE if ( n > MAX_CHAR [ 0 ] ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT dist_count = 0 NEW_LINE count = [ 0 ] * MAX_CHAR [ 0 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( count [ ord ( str [ i ] ) - ord ( ' a ' ) ] == 0 ) : NEW_LINE INDENT dist_count += 1 NEW_LINE DEDENT count [ ( ord ( str [ i ] ) - ord ( ' a ' ) ) ] += 1 NEW_LINE DEDENT return ( n - dist_count ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str = " aebaecedabbee " NEW_LINE print ( minChanges ( str ) ) NEW_LINE DEDENT
T39967
public class GFG { static int minStepToDeleteString ( String str ) { int N = str . length ( ) ; int [ ] [ ] dp = new int [ N + 1 ] [ N + 1 ] ; for ( int i = 0 ; i <= N ; i ++ ) for ( int j = 0 ; j <= N ; j ++ ) dp [ i ] [ j ] = 0 ; for ( int len = 1 ; len <= N ; len ++ ) { for ( int i = 0 , j = len - 1 ; j < N ; i ++ , j ++ ) { if ( len == 1 ) dp [ i ] [ j ] = 1 ; else { dp [ i ] [ j ] = 1 + dp [ i + 1 ] [ j ] ; if ( str . charAt ( i ) == str . charAt ( i + 1 ) ) dp [ i ] [ j ] = Math . min ( 1 + dp [ i + 2 ] [ j ] , dp [ i ] [ j ] ) ; for ( int K = i + 2 ; K <= j ; K ++ ) if ( str . charAt ( i ) == str . charAt ( K ) ) dp [ i ] [ j ] = Math . min ( dp [ i + 1 ] [ K - 1 ] + dp [ K + 1 ] [ j ] , dp [ i ] [ j ] ) ; } } } return dp [ 0 ] [ N - 1 ] ; } public static void main ( String args [ ] ) { String str = "2553432" ; System . out . println ( minStepToDeleteString ( str ) ) ; } }
def minStepToDeleteString ( str ) : NEW_LINE INDENT N = len ( str ) NEW_LINE dp = [ [ 0 for x in range ( N + 1 ) ] for y in range ( N + 1 ) ] NEW_LINE for l in range ( 1 , N + 1 ) : NEW_LINE INDENT i = 0 NEW_LINE j = l - 1 NEW_LINE while j < N : NEW_LINE INDENT if ( l == 1 ) : NEW_LINE INDENT dp [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT dp [ i ] [ j ] = 1 + dp [ i + 1 ] [ j ] NEW_LINE if ( str [ i ] == str [ i + 1 ] ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( 1 + dp [ i + 2 ] [ j ] , dp [ i ] [ j ] ) NEW_LINE DEDENT for K in range ( i + 2 , j + 1 ) : NEW_LINE INDENT if ( str [ i ] == str [ K ] ) : NEW_LINE INDENT dp [ i ] [ j ] = min ( dp [ i + 1 ] [ K - 1 ] + dp [ K + 1 ] [ j ] , dp [ i ] [ j ] ) NEW_LINE DEDENT DEDENT DEDENT i += 1 NEW_LINE j += 1 NEW_LINE DEDENT DEDENT return dp [ 0 ] [ N - 1 ] NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = "2553432" NEW_LINE print ( minStepToDeleteString ( str ) ) NEW_LINE DEDENT
T39968
import java . io . * ; class GFG { static void printPossible ( int a , int b , int c ) { if ( ( a + b + c ) % 2 != 0 || a + b < c ) System . out . println ( " NO " ) ; else System . out . println ( " YES " ) ; } public static void main ( String [ ] args ) { int a = 2 , b = 4 , c = 2 ; printPossible ( a , b , c ) ; } }
def printPossible ( a , b , c ) : NEW_LINE INDENT if ( ( a + b + c ) % 2 != 0 or a + b < c ) : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2 NEW_LINE b = 4 NEW_LINE c = 2 NEW_LINE printPossible ( a , b , c ) NEW_LINE DEDENT
T39969
import java . util . Arrays ; import java . io . * ; class GFG { static boolean check ( int H , int S ) { return H * H >= 4 * S ; } static int findPairs ( int H [ ] , int n , int S [ ] , int m ) { int count = 0 ; Arrays . sort ( H ) ; Arrays . sort ( S ) ; int index = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int start = 0 ; int end = m - 1 ; while ( start <= end ) { int mid = start + ( end - start ) / 2 ; if ( check ( H [ i ] , S [ mid ] ) ) { index = mid ; start = mid + 1 ; } else { end = mid - 1 ; } } if ( index != - 1 ) { count += index + 1 ; } } return count ; } public static void main ( String [ ] args ) { int H [ ] = { 1 , 6 , 4 } ; int n = H . length ; int S [ ] = { 23 , 3 , 42 , 14 } ; int m = S . length ; System . out . println ( findPairs ( H , n , S , m ) ) ; } }
def check ( H , S ) : NEW_LINE INDENT return H * H >= 4 * S ; NEW_LINE DEDENT def findPairs ( H , n , S , m ) : NEW_LINE INDENT count = 0 ; NEW_LINE H . sort ( ) ; NEW_LINE S . sort ( ) ; NEW_LINE index = - 1 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT start = 0 ; NEW_LINE end = m - 1 ; NEW_LINE while ( start <= end ) : NEW_LINE INDENT mid = int ( start + ( end - start ) / 2 ) ; NEW_LINE if ( check ( H [ i ] , S [ mid ] ) ) : NEW_LINE INDENT index = mid ; NEW_LINE start = mid + 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT end = mid - 1 ; NEW_LINE DEDENT DEDENT if ( index != - 1 ) : NEW_LINE INDENT count += index + 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT H = [ 1 , 6 , 4 ] ; NEW_LINE n = len ( H ) ; NEW_LINE S = [ 23 , 3 , 42 , 14 ] ; NEW_LINE m = len ( S ) ; NEW_LINE print ( findPairs ( H , n , S , m ) ) ; NEW_LINE
T39970
class GFG { static long evenFib ( int n ) { if ( n < 1 ) return n ; if ( n == 1 ) return 2 ; return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) ; } public static void main ( String [ ] args ) { int n = 7 ; System . out . println ( evenFib ( n ) ) ; } }
def evenFib ( n ) : NEW_LINE INDENT if ( n < 1 ) : NEW_LINE INDENT return n NEW_LINE DEDENT if ( n == 1 ) : NEW_LINE INDENT return 2 NEW_LINE DEDENT return ( ( 4 * evenFib ( n - 1 ) ) + evenFib ( n - 2 ) ) NEW_LINE DEDENT n = 7 NEW_LINE print ( evenFib ( n ) ) NEW_LINE
T39971
import java . io . * ; class GFG { static int frequencyOfSmallest ( int n , int arr [ ] ) { int mn = arr [ 0 ] , freq = 1 ; for ( int i = 1 ; i < n ; i ++ ) { if ( arr [ i ] < mn ) { mn = arr [ i ] ; freq = 1 ; } else if ( arr [ i ] == mn ) freq ++ ; } return freq ; } public static void main ( String [ ] args ) { int N = 5 ; int arr [ ] = { 3 , 2 , 3 , 4 , 4 } ; System . out . println ( frequencyOfSmallest ( N , arr ) ) ; } }
def frequencyOfSmallest ( n , arr ) : NEW_LINE INDENT mn = arr [ 0 ] NEW_LINE freq = 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ i ] < mn ) : NEW_LINE INDENT mn = arr [ i ] NEW_LINE freq = 1 NEW_LINE DEDENT elif ( arr [ i ] == mn ) : NEW_LINE INDENT freq += 1 NEW_LINE DEDENT DEDENT return freq NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 5 NEW_LINE arr = [ 3 , 2 , 3 , 4 , 4 ] NEW_LINE print ( frequencyOfSmallest ( N , arr ) ) NEW_LINE DEDENT
T39972
import java . util . Arrays ; public class Improve { static int number_of_tower ( int house [ ] , int range , int n ) { Arrays . sort ( house ) ; int numOfTower = 0 ; int i = 0 ; while ( i < n ) { numOfTower ++ ; int loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; -- i ; loc = house [ i ] + range ; while ( i < n && house [ i ] <= loc ) i ++ ; } return numOfTower ; } public static void main ( String args [ ] ) { int house [ ] = { 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 } ; int range = 2 ; int n = house . length ; System . out . println ( number_of_tower ( house , range , n ) ) ; } }
def number_of_tower ( house , r , n ) : NEW_LINE INDENT house . sort ( ) NEW_LINE numOfTower = 0 NEW_LINE i = 0 NEW_LINE while ( i < n ) : NEW_LINE INDENT numOfTower += 1 NEW_LINE loc = house [ i ] + r NEW_LINE while ( i < n and house [ i ] <= loc ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT i -= 1 NEW_LINE loc = house [ i ] + r NEW_LINE while ( i < n and house [ i ] <= loc ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT DEDENT return numOfTower NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT house = [ 7 , 2 , 4 , 6 , 5 , 9 , 12 , 11 ] NEW_LINE r = 2 NEW_LINE n = len ( house ) NEW_LINE print ( number_of_tower ( house , r , n ) ) NEW_LINE DEDENT
T39973
import java . util . Arrays ; class Test { static String isKSortedArray ( int arr [ ] , int n , int k ) { int aux [ ] = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) aux [ i ] = arr [ i ] ; Arrays . sort ( aux ) ; for ( int i = 0 ; i < n ; i ++ ) { int j = Arrays . binarySearch ( aux , arr [ i ] ) ; if ( Math . abs ( i - j ) > k ) return " No " ; } return " Yes " ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 2 , 1 , 5 , 6 , 4 } ; int k = 2 ; System . out . println ( " Is ▁ it ▁ a ▁ k ▁ sorted ▁ array ▁ ? : ▁ " + isKSortedArray ( arr , arr . length , k ) ) ; } }
def binarySearch ( arr , low , high , x ) : NEW_LINE INDENT while ( low <= high ) : NEW_LINE INDENT mid = int ( ( low + high ) / 2 ) NEW_LINE if ( arr [ mid ] == x ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( arr [ mid ] > x ) : NEW_LINE INDENT high = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT DEDENT def isKSortedArray ( arr , n , k ) : NEW_LINE INDENT aux = [ 0 for i in range ( n ) ] NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT aux [ i ] = arr [ i ] NEW_LINE DEDENT aux . sort ( reverse = False ) NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT j = binarySearch ( aux , 0 , n - 1 , arr [ i ] ) NEW_LINE if ( abs ( i - j ) > k ) : NEW_LINE INDENT return " No " NEW_LINE DEDENT DEDENT return " Yes " NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 1 , 5 , 6 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( " Is ▁ it ▁ a ▁ k ▁ sorted ▁ array ? : " , isKSortedArray ( arr , n , k ) ) NEW_LINE DEDENT
T39974
class GFG { static boolean check ( int p , int n ) { int temp = p , count = 0 , f = 5 ; while ( f <= temp ) { count += temp / f ; f = f * 5 ; } return ( count >= n ) ; } static int findNum ( int n ) { if ( n == 1 ) return 5 ; int low = 0 ; int high = 5 * n ; while ( low < high ) { int mid = ( low + high ) >> 1 ; if ( check ( mid , n ) ) high = mid ; else low = mid + 1 ; } return low ; } public static void main ( String [ ] args ) { int n = 6 ; System . out . println ( findNum ( n ) ) ; } }
def check ( p , n ) : NEW_LINE INDENT temp = p NEW_LINE count = 0 NEW_LINE f = 5 NEW_LINE while ( f <= temp ) : NEW_LINE INDENT count += temp / f NEW_LINE f = f * 5 NEW_LINE DEDENT return ( count >= n ) NEW_LINE DEDENT def findNum ( n ) : NEW_LINE INDENT if ( n == 1 ) : NEW_LINE INDENT return 5 NEW_LINE DEDENT low = 0 NEW_LINE high = 5 * n NEW_LINE while ( low < high ) : NEW_LINE INDENT mid = ( low + high ) >> 1 NEW_LINE if ( check ( mid , n ) ) : NEW_LINE INDENT high = mid NEW_LINE DEDENT else : NEW_LINE INDENT low = mid + 1 NEW_LINE DEDENT DEDENT return low NEW_LINE DEDENT n = 6 NEW_LINE print ( findNum ( n ) ) NEW_LINE
T39975
import java . io . * ; class GFG { static void printCombinations ( int a [ ] , int n , int m ) { for ( int i = 0 ; i < ( 1 << n ) ; i ++ ) { int sum = 0 ; int num = 1 << ( n - 1 ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & num ) > 0 ) sum += a [ j ] ; else sum += ( - 1 * a [ j ] ) ; num = num >> 1 ; } if ( sum % m == 0 ) { num = 1 << ( n - 1 ) ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( i & num ) > 0 ) System . out . print ( " + " + a [ j ] + " ▁ " ) ; else System . out . print ( " - " + a [ j ] + " ▁ " ) ; num = num >> 1 ; } System . out . println ( ) ; } } } public static void main ( String args [ ] ) { int a [ ] = { 3 , 5 , 6 , 8 } ; int n = a . length ; int m = 5 ; printCombinations ( a , n , m ) ; } }
def printCombinations ( a , n , m ) : NEW_LINE INDENT for i in range ( 0 , ( 1 << n ) ) : NEW_LINE INDENT sum = 0 NEW_LINE num = 1 << ( n - 1 ) NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( i & num ) > 0 ) : NEW_LINE INDENT sum += a [ j ] NEW_LINE DEDENT else : NEW_LINE INDENT sum += ( - 1 * a [ j ] ) NEW_LINE DEDENT num = num >> 1 NEW_LINE DEDENT if ( sum % m == 0 ) : NEW_LINE INDENT num = 1 << ( n - 1 ) NEW_LINE for j in range ( 0 , n ) : NEW_LINE INDENT if ( ( i & num ) > 0 ) : NEW_LINE INDENT print ( " + " , a [ j ] , end = " ▁ " , sep = " " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " - " , a [ j ] , end = " ▁ " , sep = " " ) NEW_LINE DEDENT num = num >> 1 NEW_LINE DEDENT print ( " " ) NEW_LINE DEDENT DEDENT DEDENT a = [ 3 , 5 , 6 , 8 ] NEW_LINE n = len ( a ) NEW_LINE m = 5 NEW_LINE printCombinations ( a , n , m ) NEW_LINE
T39976
class GFG { static int maxOR ( int L , int R ) { int maximum = Integer . MIN_VALUE ; for ( int i = L ; i < R ; i ++ ) for ( int j = i + 1 ; j <= R ; j ++ ) maximum = Math . max ( maximum , ( i | j ) ) ; return maximum ; } public static void main ( String [ ] args ) { int L = 4 , R = 5 ; System . out . println ( maxOR ( L , R ) ) ; } }
def maxOR ( L , R ) : NEW_LINE INDENT maximum = - 10 ** 9 NEW_LINE for i in range ( L , R ) : NEW_LINE INDENT for j in range ( i + 1 , R + 1 ) : NEW_LINE INDENT maximum = max ( maximum , ( i | j ) ) NEW_LINE DEDENT DEDENT return maximum NEW_LINE DEDENT L = 4 NEW_LINE R = 5 NEW_LINE print ( maxOR ( L , R ) ) NEW_LINE
T39977
class GFG { static float circlearea ( float R ) { if ( R < 0 ) return - 1 ; float a = ( float ) ( ( 3.14 * R * R ) / 4 ) ; return a ; } public static void main ( String [ ] args ) { float R = 2 ; System . out . println ( circlearea ( R ) ) ; } }
def circlearea ( R ) : NEW_LINE INDENT if ( R < 0 ) : NEW_LINE INDENT return - 1 ; NEW_LINE DEDENT a = ( 3.14 * R * R ) / 4 ; NEW_LINE return a ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT R = 2 ; NEW_LINE print ( circlearea ( R ) ) ; NEW_LINE DEDENT
T39978
import java . util . * ; class GFG { public static double findMean ( int a [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += a [ i ] ; return ( double ) sum / ( double ) n ; } public static double findMedian ( int a [ ] , int n ) { Arrays . sort ( a ) ; if ( n % 2 != 0 ) return ( double ) a [ n / 2 ] ; return ( double ) ( a [ ( n - 1 ) / 2 ] + a [ n / 2 ] ) / 2.0 ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 } ; int n = a . length ; System . out . println ( " Mean ▁ = ▁ " + findMean ( a , n ) ) ; System . out . println ( " Median ▁ = ▁ " + findMedian ( a , n ) ) ; } }
def findMean ( a , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT sum += a [ i ] NEW_LINE DEDENT return float ( sum / n ) NEW_LINE DEDENT def findMedian ( a , n ) : NEW_LINE INDENT sorted ( a ) NEW_LINE if n % 2 != 0 : NEW_LINE INDENT return float ( a [ n / 2 ] ) NEW_LINE DEDENT return float ( ( a [ int ( ( n - 1 ) / 2 ) ] + a [ int ( n / 2 ) ] ) / 2.0 ) NEW_LINE DEDENT a = [ 1 , 3 , 4 , 2 , 7 , 5 , 8 , 6 ] NEW_LINE n = len ( a ) NEW_LINE print ( " Mean ▁ = " , findMean ( a , n ) ) NEW_LINE print ( " Median ▁ = " , findMedian ( a , n ) ) NEW_LINE
T39979
import java . io . * ; class GFG { static double squareRoot ( double n ) { return Math . pow ( 2 , 0.5 * ( Math . log ( n ) / Math . log ( 2 ) ) ) ; } public static void main ( String [ ] args ) { double n = 12 ; System . out . println ( squareRoot ( n ) ) ; } }
import math NEW_LINE def squareRoot ( n ) : NEW_LINE INDENT return pow ( 2 , 0.5 * math . log2 ( n ) ) NEW_LINE DEDENT n = 12 NEW_LINE print ( squareRoot ( n ) ) NEW_LINE
T39980
import java . io . * ; class GFG { static int __gcd ( int a , int b ) { if ( a == 0 || b == 0 ) return 0 ; if ( a == b ) return a ; if ( a > b ) return __gcd ( a - b , b ) ; return __gcd ( a , b - a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / __gcd ( a , b ) ; } static void printArray ( int a [ ] , int n ) { System . out . print ( a [ 0 ] + " ▁ " ) ; for ( int i = 0 ; i < n - 1 ; i ++ ) System . out . print ( lcm ( a [ i ] , a [ i + 1 ] ) + " ▁ " ) ; System . out . print ( a [ n - 1 ] ) ; } public static void main ( String [ ] args ) { int a [ ] = { 1 , 2 , 3 } ; int n = a . length ; printArray ( a , n ) ; } }
def __gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 or b == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( a == b ) : NEW_LINE INDENT return a NEW_LINE DEDENT if ( a > b ) : NEW_LINE INDENT return __gcd ( a - b , b ) NEW_LINE DEDENT return __gcd ( a , b - a ) NEW_LINE DEDENT def lcm ( a , b ) : NEW_LINE INDENT return ( a * b ) / __gcd ( a , b ) NEW_LINE DEDENT def printArray ( a , n ) : NEW_LINE INDENT print ( str ( a [ 0 ] ) + " ▁ " ) NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT print ( str ( lcm ( a [ i ] , a [ i + 1 ] ) ) + " ▁ " ) NEW_LINE DEDENT print ( a [ n - 1 ] ) NEW_LINE DEDENT a = [ 1 , 2 , 3 ] NEW_LINE n = len ( a ) NEW_LINE printArray ( a , n ) NEW_LINE
T39981
class GFG { static void findMajority ( int arr [ ] , int n ) { int len = 32 ; int number = 0 ; for ( int i = 0 ; i < len ; i ++ ) { int count = 0 ; for ( int j = 0 ; j < n ; j ++ ) { if ( ( arr [ j ] & ( 1 << i ) ) != 0 ) count ++ ; } if ( count > ( n / 2 ) ) number += ( 1 << i ) ; } int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == number ) count ++ ; if ( count > ( n / 2 ) ) System . out . println ( number ) ; else System . out . println ( " Majority ▁ Element ▁ Not ▁ Present " ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 4 , 2 , 4 , 4 , 2 , 4 , 4 } ; int n = arr . length ; findMajority ( arr , n ) ; } }
def findMajority ( arr , n ) : NEW_LINE INDENT Len = 32 NEW_LINE number = 0 NEW_LINE for i in range ( Len ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( n ) : NEW_LINE INDENT if ( arr [ j ] & ( 1 << i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count > ( n // 2 ) ) : NEW_LINE INDENT number += ( 1 << i ) NEW_LINE DEDENT DEDENT count = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == number ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( count > ( n // 2 ) ) : NEW_LINE INDENT print ( number ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Majority ▁ Element ▁ Not ▁ Present " ) NEW_LINE DEDENT DEDENT arr = [ 3 , 3 , 4 , 2 , 4 , 4 , 2 , 4 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE findMajority ( arr , n ) NEW_LINE
T39982
import java . util . * ; import java . io . * ; class GFG { static void findElements ( int arr [ ] , int n ) { Arrays . sort ( arr ) ; for ( int i = 0 ; i < n - 2 ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 2 , - 6 , 3 , 5 , 1 } ; int n = arr . length ; findElements ( arr , n ) ; } }
def findElements ( arr , n ) : NEW_LINE INDENT arr . sort ( ) NEW_LINE for i in range ( 0 , n - 2 ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 2 , - 6 , 3 , 5 , 1 ] NEW_LINE n = len ( arr ) NEW_LINE findElements ( arr , n ) NEW_LINE
T39983
class GFG { static void printArray ( int arr [ ] , int size ) { for ( int i = 0 ; i < size ; i ++ ) { System . out . printf ( " % d ▁ " , arr [ i ] ) ; } System . out . printf ( " \n " ) ; return ; } static int getSuccessor ( int arr [ ] , int k , int n ) { int p = k - 1 ; while ( arr [ p ] == n ) { p -- ; if ( p < 0 ) { break ; } } if ( p < 0 ) { return 0 ; } arr [ p ] = arr [ p ] + 1 ; for ( int i = p + 1 ; i < k ; i ++ ) { arr [ i ] = 1 ; } return 1 ; } static void printSequences ( int n , int k ) { int [ ] arr = new int [ k ] ; for ( int i = 0 ; i < k ; i ++ ) { arr [ i ] = 1 ; } while ( true ) { printArray ( arr , k ) ; if ( getSuccessor ( arr , k , n ) == 0 ) { break ; } } } public static void main ( String [ ] args ) { int n = 3 ; int k = 2 ; printSequences ( n , k ) ; } }
def printArray ( arr , size ) : NEW_LINE INDENT for i in range ( size ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE return NEW_LINE DEDENT def getSuccessor ( arr , k , n ) : NEW_LINE INDENT p = k - 1 NEW_LINE while ( arr [ p ] == n and 0 <= p < k ) : NEW_LINE INDENT p -= 1 NEW_LINE DEDENT if ( p < 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT arr [ p ] = arr [ p ] + 1 NEW_LINE i = p + 1 NEW_LINE while ( i < k ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE i += 1 NEW_LINE DEDENT return 1 NEW_LINE DEDENT def printSequences ( n , k ) : NEW_LINE INDENT arr = [ 0 ] * k NEW_LINE for i in range ( k ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT while ( 1 ) : NEW_LINE INDENT printArray ( arr , k ) NEW_LINE if ( getSuccessor ( arr , k , n ) == 0 ) : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT n = 3 NEW_LINE k = 2 NEW_LINE printSequences ( n , k ) NEW_LINE
T39984
class GFG { static int countOperations ( int n ) { int i = 2 ; while ( ( i * i ) < n && ( n % i ) > 0 ) i += 1 ; if ( ( i * i ) > n ) i = n ; return ( 1 + ( n - i ) / 2 ) ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( countOperations ( n ) ) ; } }
def countOperations ( n ) : NEW_LINE INDENT i = 2 NEW_LINE while ( ( i * i ) < n and ( n % i ) ) : NEW_LINE INDENT i += 1 NEW_LINE DEDENT if ( ( i * i ) > n ) : NEW_LINE INDENT i = n NEW_LINE DEDENT return ( 1 + ( n - i ) // 2 ) NEW_LINE DEDENT n = 5 NEW_LINE print ( countOperations ( n ) ) NEW_LINE
T39985
class GFG { final static long mod = 1000000007 ; static long fact ( long n ) { if ( n == 1 ) return 1 ; else return ( fact ( n - 1 ) * n ) % mod ; } static long countPairs ( int m , int n ) { long ans = fact ( 2 * m + n - 1 ) / ( fact ( n - 1 ) * fact ( 2 * m ) ) ; return ( ans % mod ) ; } public static void main ( String [ ] args ) { int n = 5 , m = 3 ; System . out . println ( countPairs ( m , n ) ) ; } }
from math import factorial as fact NEW_LINE def countPairs ( m , n ) : NEW_LINE INDENT ans = fact ( 2 * m + n - 1 ) // ( fact ( n - 1 ) * fact ( 2 * m ) ) NEW_LINE return ( ans % ( 10 ** 9 + 7 ) ) NEW_LINE DEDENT n , m = 5 , 3 NEW_LINE print ( countPairs ( m , n ) ) NEW_LINE
T39986
import java . io . * ; class GFG { static int count ( int n ) { if ( n < 4 ) return - 1 ; int rem = n % 4 ; if ( rem == 0 ) return n / 4 ; if ( rem == 1 ) { if ( n < 9 ) return - 1 ; return ( n - 9 ) / 4 + 1 ; } if ( rem == 2 ) return ( n - 6 ) / 4 + 1 ; if ( rem == 3 ) { if ( n < 15 ) return - 1 ; return ( n - 15 ) / 4 + 2 ; } return 0 ; } public static void main ( String [ ] args ) { int n = 90 ; System . out . println ( count ( n ) ) ; n = 143 ; System . out . println ( count ( n ) ) ; } }
def count ( n ) : NEW_LINE INDENT if ( n < 4 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT rem = n % 4 NEW_LINE if ( rem == 0 ) : NEW_LINE INDENT return n // 4 NEW_LINE DEDENT if ( rem == 1 ) : NEW_LINE INDENT if ( n < 9 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( n - 9 ) // 4 + 1 NEW_LINE DEDENT if ( rem == 2 ) : NEW_LINE INDENT return ( n - 6 ) // 4 + 1 NEW_LINE DEDENT if ( rem == 3 ) : NEW_LINE INDENT if ( n < 15 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT return ( n - 15 ) // 4 + 2 NEW_LINE DEDENT DEDENT n = 90 NEW_LINE print ( count ( n ) ) NEW_LINE n = 143 NEW_LINE print ( count ( n ) ) NEW_LINE
T39987
class GFG { static int magicIndex ( int arr [ ] , int start , int end ) { if ( start > end ) return - 1 ; int midIndex = ( start + end ) / 2 ; int midValue = arr [ midIndex ] ; if ( midIndex == midValue ) return midIndex ; int left = magicIndex ( arr , start , Math . min ( midValue , midIndex - 1 ) ) ; if ( left >= 0 ) return left ; return magicIndex ( arr , Math . max ( midValue , midIndex + 1 ) , end ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 10 , - 5 , 2 , 2 , 2 , 3 , 4 , 7 , 9 , 12 , 13 } ; int n = arr . length ; int index = magicIndex ( arr , 0 , n - 1 ) ; if ( index == - 1 ) System . out . print ( " No ▁ Magic ▁ Index " ) ; else System . out . print ( " Magic ▁ Index ▁ is ▁ : ▁ " + index ) ; } }
def magicIndex ( arr , start , end ) : NEW_LINE INDENT if ( start > end ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT midIndex = int ( ( start + end ) / 2 ) NEW_LINE midValue = arr [ midIndex ] NEW_LINE if ( midIndex == midValue ) : NEW_LINE INDENT return midIndex NEW_LINE DEDENT left = magicIndex ( arr , start , min ( midValue , midIndex - 1 ) ) NEW_LINE if ( left >= 0 ) : NEW_LINE INDENT return left NEW_LINE DEDENT return magicIndex ( arr , max ( midValue , midIndex + 1 ) , end ) NEW_LINE DEDENT arr = [ - 10 , - 5 , 2 , 2 , 2 , 3 , 4 , 7 , 9 , 12 , 13 ] NEW_LINE n = len ( arr ) NEW_LINE index = magicIndex ( arr , 0 , n - 1 ) NEW_LINE if ( index == - 1 ) : NEW_LINE INDENT print ( " No ▁ Magic ▁ Index " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Magic ▁ Index ▁ is ▁ : " , index ) NEW_LINE DEDENT
T39988
class LinkedList { static Node head ; class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } Node kAltReverse ( Node head , int k ) { return _kAltReverse ( head , k , true ) ; } Node _kAltReverse ( Node node , int k , boolean b ) { if ( node == null ) { return null ; } int count = 1 ; Node prev = null ; Node current = node ; Node next = null ; while ( current != null && count <= k ) { next = current . next ; if ( b == true ) { current . next = prev ; } prev = current ; current = next ; count ++ ; } if ( b == true ) { node . next = _kAltReverse ( current , k , ! b ) ; return prev ; } else { prev . next = _kAltReverse ( current , k , ! b ) ; return node ; } } void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } void push ( int newdata ) { Node mynode = new Node ( newdata ) ; mynode . next = head ; head = mynode ; } public static void main ( String [ ] args ) { LinkedList list = new LinkedList ( ) ; for ( int i = 20 ; i > 0 ; i -- ) { list . push ( i ) ; } System . out . println ( " Given ▁ Linked ▁ List ▁ : " ) ; list . printList ( head ) ; head = list . kAltReverse ( head , 3 ) ; System . out . println ( " " ) ; System . out . println ( " Modified ▁ Linked ▁ List ▁ : " ) ; list . printList ( head ) ; } }
class node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = next NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = node ( 0 ) NEW_LINE new_node . data = new_data NEW_LINE new_node . next = ( head_ref ) NEW_LINE ( head_ref ) = new_node NEW_LINE return head_ref NEW_LINE DEDENT def kAltReverse ( head , k ) : NEW_LINE INDENT return _kAltReverse ( head , k , True ) NEW_LINE DEDENT def _kAltReverse ( Node , k , b ) : NEW_LINE INDENT if ( Node == None ) : NEW_LINE INDENT return None NEW_LINE DEDENT count = 1 NEW_LINE prev = None NEW_LINE current = Node NEW_LINE next = None NEW_LINE while ( current != None and count <= k ) : NEW_LINE INDENT next = current . next NEW_LINE if ( b == True ) : NEW_LINE INDENT current . next = prev NEW_LINE DEDENT prev = current NEW_LINE current = next NEW_LINE count = count + 1 NEW_LINE DEDENT if ( b == True ) : NEW_LINE INDENT Node . next = _kAltReverse ( current , k , not b ) NEW_LINE return prev NEW_LINE DEDENT else : NEW_LINE INDENT prev . next = _kAltReverse ( current , k , not b ) NEW_LINE return Node NEW_LINE DEDENT DEDENT def printList ( node ) : NEW_LINE INDENT count = 0 NEW_LINE while ( node != None ) : NEW_LINE INDENT print ( node . data , end = " ▁ " ) NEW_LINE node = node . next NEW_LINE count = count + 1 NEW_LINE DEDENT DEDENT head = None NEW_LINE i = 20 NEW_LINE while ( i > 0 ) : NEW_LINE INDENT head = push ( head , i ) NEW_LINE i = i - 1 NEW_LINE DEDENT print ( " Given ▁ linked ▁ list ▁ " ) NEW_LINE printList ( head ) NEW_LINE head = kAltReverse ( head , 3 ) NEW_LINE print ( " \n Modified ▁ Linked ▁ list ▁ " ) NEW_LINE printList ( head ) NEW_LINE
T39989
class GFG { static void gouldSequence ( int n ) { for ( int row_num = 1 ; row_num <= n ; row_num ++ ) { int count = 1 ; int c = 1 ; for ( int i = 1 ; i <= row_num ; i ++ ) { c = c * ( row_num - i ) / i ; if ( c % 2 == 1 ) count ++ ; } System . out . print ( count + " ▁ " ) ; } } public static void main ( String [ ] args ) { int n = 16 ; gouldSequence ( n ) ; } }
def gouldSequence ( n ) : NEW_LINE INDENT for row_num in range ( 1 , n ) : NEW_LINE INDENT count = 1 NEW_LINE c = 1 NEW_LINE for i in range ( 1 , row_num ) : NEW_LINE INDENT c = c * ( row_num - i ) / i NEW_LINE if ( c % 2 == 1 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT print ( count , end = " ▁ " ) NEW_LINE DEDENT DEDENT n = 16 ; NEW_LINE gouldSequence ( n ) NEW_LINE
T39990
import java . io . * ; class GFG { public static int first ( int arr [ ] , int low , int high , int x , int n ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == 0 || x > arr [ mid - 1 ] ) && arr [ mid ] == x ) return mid ; else if ( x > arr [ mid ] ) return first ( arr , ( mid + 1 ) , high , x , n ) ; else return first ( arr , low , ( mid - 1 ) , x , n ) ; } return - 1 ; } public static int last ( int arr [ ] , int low , int high , int x , int n ) { if ( high >= low ) { int mid = low + ( high - low ) / 2 ; if ( ( mid == n - 1 || x < arr [ mid + 1 ] ) && arr [ mid ] == x ) return mid ; else if ( x < arr [ mid ] ) return last ( arr , low , ( mid - 1 ) , x , n ) ; else return last ( arr , ( mid + 1 ) , high , x , n ) ; } return - 1 ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 } ; int n = arr . length ; int x = 8 ; System . out . println ( " First ▁ Occurrence ▁ = ▁ " + first ( arr , 0 , n - 1 , x , n ) ) ; System . out . println ( " Last ▁ Occurrence ▁ = ▁ " + last ( arr , 0 , n - 1 , x , n ) ) ; } }
def first ( arr , low , high , x , n ) : NEW_LINE INDENT if ( high >= low ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( ( mid == 0 or x > arr [ mid - 1 ] ) and arr [ mid ] == x ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( x > arr [ mid ] ) : NEW_LINE INDENT return first ( arr , ( mid + 1 ) , high , x , n ) NEW_LINE DEDENT else : NEW_LINE INDENT return first ( arr , low , ( mid - 1 ) , x , n ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT def last ( arr , low , high , x , n ) : NEW_LINE INDENT if ( high >= low ) : NEW_LINE INDENT mid = low + ( high - low ) // 2 NEW_LINE if ( ( mid == n - 1 or x < arr [ mid + 1 ] ) and arr [ mid ] == x ) : NEW_LINE INDENT return mid NEW_LINE DEDENT elif ( x < arr [ mid ] ) : NEW_LINE INDENT return last ( arr , low , ( mid - 1 ) , x , n ) NEW_LINE DEDENT else : NEW_LINE INDENT return last ( arr , ( mid + 1 ) , high , x , n ) NEW_LINE DEDENT DEDENT return - 1 NEW_LINE DEDENT arr = [ 1 , 2 , 2 , 2 , 2 , 3 , 4 , 7 , 8 , 8 ] NEW_LINE n = len ( arr ) NEW_LINE x = 8 NEW_LINE print ( " First ▁ Occurrence ▁ = ▁ " , first ( arr , 0 , n - 1 , x , n ) ) NEW_LINE print ( " Last ▁ Occurrence ▁ = ▁ " , last ( arr , 0 , n - 1 , x , n ) ) NEW_LINE
T39991
class GFG { static int assignValue ( int a , int b , int x ) { int arr [ ] = { a , b } ; return ( arr [ x ] ) ; } public static void main ( String [ ] args ) { int y = assignValue ( 3 , 7 , 0 ) ; System . out . println ( y ) ; } }
def assignValue ( a , b , x ) : NEW_LINE INDENT arr = [ a , b ] NEW_LINE return ( arr [ x ] ) NEW_LINE DEDENT y = assignValue ( 3 , 7 , 0 ) NEW_LINE print ( y ) NEW_LINE
T39992
class GFG { static int findNthEvenDigitNumber ( int n ) { int count = 0 ; for ( int i = 0 ; ; i ++ ) { int curr = i ; boolean isCurrEvenDigit = true ; while ( curr != 0 ) { if ( curr % 10 == 1 || curr % 10 == 3 || curr % 10 == 5 || curr % 10 == 7 || curr % 10 == 9 ) isCurrEvenDigit = false ; curr = curr / 10 ; } if ( isCurrEvenDigit == true ) count ++ ; if ( count == n ) return i ; } } public static void main ( String [ ] args ) { System . out . println ( findNthEvenDigitNumber ( 2 ) ) ; System . out . println ( findNthEvenDigitNumber ( 10 ) ) ; } }
def findNthEvenDigitNumber ( n ) : NEW_LINE INDENT count = 0 ; NEW_LINE i = 0 ; NEW_LINE while ( True ) : NEW_LINE INDENT curr = i ; NEW_LINE isCurrEvenDigit = True ; NEW_LINE while ( curr != 0 ) : NEW_LINE INDENT if ( curr % 10 == 1 or curr % 10 == 3 or curr % 10 == 5 or curr % 10 == 7 or curr % 10 == 9 ) : NEW_LINE INDENT isCurrEvenDigit = False ; NEW_LINE DEDENT curr = curr // 10 ; NEW_LINE DEDENT if ( isCurrEvenDigit == True ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT if ( count == n ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT i += 1 ; NEW_LINE DEDENT DEDENT print ( findNthEvenDigitNumber ( 2 ) ) ; NEW_LINE print ( findNthEvenDigitNumber ( 10 ) ) ; NEW_LINE
T39993
class GFG { static int power ( long x , long y , long p ) { int res = 1 ; x = x % p ; while ( y > 0 ) { long r = y & 1 ; if ( r == 1 ) res = ( res * ( int ) x ) % ( int ) p ; y = y >> 1 ; x = ( x * x ) % p ; } return res ; } static int numberOfDigits ( int x ) { int i = 0 ; while ( x != 0 ) { x /= 10 ; i ++ ; } return i ; } static void LastTwoDigit ( int n ) { System . out . print ( " Last ▁ " + 2 + " ▁ digits ▁ of ▁ " + 2 + " ^ " ) ; System . out . print ( n + " ▁ = ▁ " ) ; int temp = 1 ; for ( int i = 1 ; i <= 2 ; i ++ ) temp *= 10 ; temp = power ( 2 , n , temp ) ; for ( int i = 0 ; i < ( 2 - numberOfDigits ( temp ) ) ; i ++ ) System . out . print ( 0 + " ▁ " ) ; if ( temp != 0 ) System . out . println ( temp ) ; } public static void main ( String [ ] args ) { int n = 72 ; LastTwoDigit ( n ) ; } }
def power ( x , y , p ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % p NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % p NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % p NEW_LINE DEDENT return res NEW_LINE DEDENT def numberOfDigits ( x ) : NEW_LINE INDENT i = 0 NEW_LINE while ( x ) : NEW_LINE INDENT x //= 10 NEW_LINE i += 1 NEW_LINE DEDENT return i NEW_LINE DEDENT def LastTwoDigit ( n ) : NEW_LINE INDENT print ( " Last ▁ " + str ( 2 ) + " ▁ digits ▁ of ▁ " + str ( 2 ) , end = " " ) NEW_LINE print ( " ^ " + str ( n ) + " ▁ = ▁ " , end = " " ) NEW_LINE temp = 1 NEW_LINE for i in range ( 1 , 3 ) : NEW_LINE INDENT temp *= 10 NEW_LINE DEDENT temp = power ( 2 , n , temp ) NEW_LINE for i in range ( 2 - numberOfDigits ( temp ) ) : NEW_LINE INDENT print ( 0 , end = " " ) NEW_LINE DEDENT if temp : NEW_LINE INDENT print ( temp ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 72 NEW_LINE LastTwoDigit ( n ) NEW_LINE DEDENT
T39994
import java . util . * ; class GFG { static int rightmostNonZero ( int a [ ] , int n ) { int c5 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { while ( a [ i ] > 0 && a [ i ] % 5 == 0 ) { a [ i ] /= 5 ; c5 ++ ; } } for ( int i = 0 ; i < n ; i ++ ) { while ( c5 != 0 && a [ i ] > 0 && ( a [ i ] & 1 ) == 0 ) { a [ i ] >>= 1 ; c5 -- ; } } int ans = 1 ; for ( int i = 0 ; i < n ; i ++ ) { ans = ( ans * a [ i ] % 10 ) % 10 ; } if ( c5 != 0 ) ans = ( ans * 5 ) % 10 ; if ( ans != 0 ) return ans ; return - 1 ; } public static void main ( String args [ ] ) { int a [ ] = { 7 , 42 , 11 , 64 } ; int n = a . length ; System . out . println ( rightmostNonZero ( a , n ) ) ; } }
def rightmostNonZero ( a , n ) : NEW_LINE INDENT c5 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT while ( a [ i ] > 0 and a [ i ] % 5 == 0 ) : NEW_LINE INDENT a [ i ] //= 5 NEW_LINE c5 += 1 NEW_LINE DEDENT DEDENT for i in range ( n ) : NEW_LINE INDENT while ( c5 and a [ i ] > 0 and ( a [ i ] & 1 ) == 0 ) : NEW_LINE INDENT a [ i ] >>= 1 NEW_LINE c5 -= 1 NEW_LINE DEDENT DEDENT ans = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT ans = ( ans * a [ i ] % 10 ) % 10 NEW_LINE DEDENT if ( c5 ) : NEW_LINE INDENT ans = ( ans * 5 ) % 10 NEW_LINE DEDENT if ( ans ) : NEW_LINE INDENT return ans NEW_LINE DEDENT return - 1 NEW_LINE DEDENT a = [ 7 , 42 , 11 , 64 ] NEW_LINE n = len ( a ) NEW_LINE print ( rightmostNonZero ( a , n ) ) NEW_LINE
T39995
class LinkedList { static Node head ; class Node { int data ; Node next ; Node ( int d ) { data = d ; next = null ; } } Node kAltReverse ( Node node , int k ) { Node current = node ; Node next = null , prev = null ; int count = 0 ; while ( current != null && count < k ) { next = current . next ; current . next = prev ; prev = current ; current = next ; count ++ ; } if ( node != null ) { node . next = current ; } count = 0 ; while ( count < k - 1 && current != null ) { current = current . next ; count ++ ; } if ( current != null ) { current . next = kAltReverse ( current . next , k ) ; } return prev ; } void printList ( Node node ) { while ( node != null ) { System . out . print ( node . data + " ▁ " ) ; node = node . next ; } } void push ( int newdata ) { Node mynode = new Node ( newdata ) ; mynode . next = head ; head = mynode ; } public static void main ( String [ ] args ) { LinkedList list = new LinkedList ( ) ; for ( int i = 20 ; i > 0 ; i -- ) { list . push ( i ) ; } System . out . println ( " Given ▁ Linked ▁ List ▁ : " ) ; list . printList ( head ) ; head = list . kAltReverse ( head , 3 ) ; System . out . println ( " " ) ; System . out . println ( " Modified ▁ Linked ▁ List ▁ : " ) ; list . printList ( head ) ; } }
import math NEW_LINE class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def kAltReverse ( head , k ) : NEW_LINE INDENT current = head NEW_LINE next = None NEW_LINE prev = None NEW_LINE count = 0 NEW_LINE while ( current != None and count < k ) : NEW_LINE INDENT next = current . next NEW_LINE current . next = prev NEW_LINE prev = current NEW_LINE current = next NEW_LINE count = count + 1 ; NEW_LINE DEDENT if ( head != None ) : NEW_LINE INDENT head . next = current NEW_LINE DEDENT count = 0 NEW_LINE while ( count < k - 1 and current != None ) : NEW_LINE INDENT current = current . next NEW_LINE count = count + 1 NEW_LINE DEDENT if ( current != None ) : NEW_LINE INDENT current . next = kAltReverse ( current . next , k ) NEW_LINE DEDENT return prev NEW_LINE DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = head_ref NEW_LINE head_ref = new_node NEW_LINE return head_ref NEW_LINE DEDENT def prList ( node ) : NEW_LINE INDENT count = 0 NEW_LINE while ( node != None ) : NEW_LINE INDENT print ( node . data , end = " ▁ " ) NEW_LINE node = node . next NEW_LINE count = count + 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT head = None NEW_LINE for i in range ( 20 , 0 , - 1 ) : NEW_LINE INDENT head = push ( head , i ) NEW_LINE DEDENT print ( " Given ▁ linked ▁ list ▁ " ) NEW_LINE prList ( head ) NEW_LINE head = kAltReverse ( head , 3 ) NEW_LINE print ( " \n Modified ▁ Linked ▁ list " ) NEW_LINE prList ( head ) NEW_LINE DEDENT
T39996
import java . io . * ; class GFG { static int GCD ( int a , int b ) { if ( b == 0 ) return a ; return GCD ( b , a % b ) ; } static int multiplicativeOrder ( int A , int N ) { if ( GCD ( A , N ) != 1 ) return - 1 ; int result = 1 ; int K = 1 ; while ( K < N ) { result = ( result * A ) % N ; if ( result == 1 ) return K ; K ++ ; } return - 1 ; } public static void main ( String args [ ] ) { int A = 4 , N = 7 ; System . out . println ( multiplicativeOrder ( A , N ) ) ; } }
def GCD ( a , b ) : NEW_LINE INDENT if ( b == 0 ) : NEW_LINE INDENT return a NEW_LINE DEDENT return GCD ( b , a % b ) NEW_LINE DEDENT def multiplicativeOrder ( A , N ) : NEW_LINE INDENT if ( GCD ( A , N ) != 1 ) : NEW_LINE INDENT return - 1 NEW_LINE DEDENT result = 1 NEW_LINE K = 1 NEW_LINE while ( K < N ) : NEW_LINE INDENT result = ( result * A ) % N NEW_LINE if ( result == 1 ) : NEW_LINE INDENT return K NEW_LINE DEDENT K = K + 1 NEW_LINE DEDENT return - 1 NEW_LINE DEDENT A = 4 NEW_LINE N = 7 NEW_LINE print ( multiplicativeOrder ( A , N ) ) NEW_LINE
T39997
import java . io . * ; class GFG { static long calculateSum ( int n ) { long sum = 0 ; sum = 1 << n ; return ( sum - 1 ) ; } public static void main ( String [ ] args ) { int n = 10 ; System . out . println ( " Sum ▁ of ▁ all ▁ elements : " + calculateSum ( n ) ) ; } }
def calculateSum ( n ) : NEW_LINE INDENT sum = 0 NEW_LINE sum = 1 << n ; NEW_LINE return ( sum - 1 ) NEW_LINE DEDENT n = 10 NEW_LINE print ( " Sum ▁ of ▁ all ▁ elements : " , calculateSum ( n ) ) NEW_LINE
T39998
import java . util . Random ; public class GFG { static int res = 0 ; static int count = 0 ; static int selectRandom ( int x ) { count ++ ; if ( count == 1 ) res = x ; else { Random r = new Random ( ) ; int i = r . nextInt ( count ) ; if ( i == count - 1 ) res = x ; } return res ; } public static void main ( String [ ] args ) { int stream [ ] = { 1 , 2 , 3 , 4 } ; int n = stream . length ; for ( int i = 0 ; i < n ; i ++ ) System . out . println ( " Random ▁ number ▁ from ▁ first ▁ " + ( i + 1 ) + " ▁ numbers ▁ is ▁ " + selectRandom ( stream [ i ] ) ) ; } }
import random NEW_LINE def selectRandom ( x ) : NEW_LINE INDENT res = 0 ; NEW_LINE count = 0 ; NEW_LINE count += 1 ; NEW_LINE if ( count == 1 ) : NEW_LINE INDENT res = x ; NEW_LINE DEDENT else : NEW_LINE INDENT i = random . randrange ( count ) ; NEW_LINE if ( i == count - 1 ) : NEW_LINE INDENT res = x ; NEW_LINE DEDENT DEDENT return res ; NEW_LINE DEDENT stream = [ 1 , 2 , 3 , 4 ] ; NEW_LINE n = len ( stream ) ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT print ( " Random ▁ number ▁ from ▁ first " , ( i + 1 ) , " numbers ▁ is " , selectRandom ( stream [ i ] ) ) ; NEW_LINE DEDENT
T39999
import java . util . * ; import java . lang . * ; public class GfG { public static boolean areAllBitsSet ( long n ) { if ( n == 0 ) return false ; if ( ( ( n + 1 ) & n ) == 0 ) return true ; return false ; } public static boolean isOnesComplementOfOther ( long a , long b ) { return areAllBitsSet ( a ^ b ) ; } public static void main ( String argc [ ] ) { long a = 10 , b = 5 ; if ( isOnesComplementOfOther ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
def areAllBitsSet ( n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( ( ( n + 1 ) & n ) == 0 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT return False ; NEW_LINE DEDENT def isOnesComplementOfOther ( a , b ) : NEW_LINE INDENT return areAllBitsSet ( a ^ b ) NEW_LINE DEDENT a = 1 NEW_LINE b = 14 NEW_LINE if ( isOnesComplementOfOther ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT