id
stringlengths
2
6
java
stringlengths
48
5.92k
python
stringlengths
33
11.1k
T39800
class GfG { static void printSubSets ( int numOfBits , int num ) { if ( num >= 0 ) { System . out . print ( " { ▁ " ) ; subset ( numOfBits - 1 , num , numOfBits ) ; System . out . println ( " } " ) ; printSubSets ( numOfBits , num - 1 ) ; } else return ; } static void subset ( int nthBit , int num , int numOfBits ) { if ( nthBit >= 0 ) { if ( ( num & ( 1 << nthBit ) ) != 0 ) { System . out . print ( numOfBits - nthBit + " ▁ " ) ; } subset ( nthBit - 1 , num , numOfBits ) ; } else return ; } public static void main ( String [ ] args ) { int n = 4 ; printSubSets ( n , ( int ) ( Math . pow ( 2 , n ) ) - 1 ) ; } }
def printSubsets ( numOfBits , num ) : NEW_LINE INDENT if num >= 0 : NEW_LINE INDENT print ( " { " , end = " ▁ " ) NEW_LINE subset ( numOfBits - 1 , num , numOfBits ) NEW_LINE print ( " } " ) NEW_LINE printSubsets ( numOfBits , num - 1 ) NEW_LINE DEDENT else : NEW_LINE INDENT return NEW_LINE DEDENT DEDENT def subset ( nthBit , num , numOfBits ) : NEW_LINE INDENT if nthBit >= 0 : NEW_LINE INDENT if num & ( 1 << nthBit ) != 0 : NEW_LINE INDENT print ( numOfBits - nthBit , end = " ▁ " ) NEW_LINE DEDENT subset ( nthBit - 1 , num , numOfBits ) NEW_LINE DEDENT else : NEW_LINE INDENT return NEW_LINE DEDENT DEDENT n = 4 NEW_LINE printSubsets ( n , 2 ** n - 1 ) NEW_LINE
T39801
import java . util . * ; class solution { static void section ( double x1 , double x2 , double y1 , double y2 , double z1 , double z2 , double m , double n ) { double x = ( ( m * x2 ) + ( n * x1 ) ) / ( m + n ) ; double y = ( ( m * y2 ) + ( n * y1 ) ) / ( m + n ) ; double z = ( ( m * z2 ) + ( n * z1 ) ) / ( m + n ) ; System . out . print ( " ( " + x + " , ▁ " ) ; System . out . print ( y + " , ▁ " ) ; System . out . println ( z + " ) " ) ; } public static void main ( String arr [ ] ) { double x1 = 2 , x2 = 4 , y1 = - 1 , y2 = 3 , z1 = 4 , z2 = 2 , m = 2 , n = 3 ; section ( x1 , x2 , y1 , y2 , z1 , z2 , m , n ) ; } }
def section ( x1 , x2 , y1 , y2 , z1 , z2 , m , n ) : NEW_LINE INDENT x = ( ( m * x2 ) + ( n * x1 ) ) / ( m + n ) NEW_LINE y = ( ( m * y2 ) + ( n * y1 ) ) / ( m + n ) NEW_LINE z = ( ( m * z2 ) + ( n * z1 ) ) / ( m + n ) NEW_LINE print ( " ( " , x , " , " , y , " , " , z , " ) " ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT x1 = 2 NEW_LINE x2 = 4 NEW_LINE y1 = - 1 NEW_LINE y2 = 3 NEW_LINE z1 = 4 NEW_LINE z2 = 2 NEW_LINE m = 2 NEW_LINE n = 3 NEW_LINE section ( x1 , x2 , y1 , y2 , z1 , z2 , m , n ) NEW_LINE DEDENT
T39802
class GFG { static int MAX = 100000 ; static int findSumofEle ( int arr1 [ ] , int m , int arr2 [ ] , int n ) { int hash [ ] = new int [ MAX ] ; for ( int i = 0 ; i < n ; i ++ ) { hash [ arr2 [ i ] ] ++ ; } for ( int i = 1 ; i < MAX ; i ++ ) { hash [ i ] = hash [ i ] + hash [ i - 1 ] ; } int maximumFreq = 0 ; for ( int i = 0 ; i < m ; i ++ ) { maximumFreq = Math . max ( maximumFreq , hash [ arr1 [ i ] ] ) ; } int sumOfElements = 0 ; for ( int i = 0 ; i < m ; i ++ ) { sumOfElements += ( maximumFreq == hash [ arr1 [ i ] ] ) ? arr1 [ i ] : 0 ; } return sumOfElements ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 2 , 5 , 6 , 8 } ; int arr2 [ ] = { 4 , 10 } ; int m = arr1 . length ; int n = arr2 . length ; System . out . println ( findSumofEle ( arr1 , m , arr2 , n ) ) ; } }
MAX = 100000 NEW_LINE def findSumofEle ( arr1 , m , arr2 , n ) : NEW_LINE INDENT hash = [ 0 for i in range ( MAX ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT hash [ arr2 [ i ] ] += 1 NEW_LINE DEDENT for i in range ( 1 , MAX , 1 ) : NEW_LINE INDENT hash [ i ] = hash [ i ] + hash [ i - 1 ] NEW_LINE DEDENT maximumFreq = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT maximumFreq = max ( maximumFreq , hash [ arr1 [ i ] ] ) NEW_LINE DEDENT sumOfElements = 0 NEW_LINE for i in range ( m ) : NEW_LINE INDENT if ( maximumFreq == hash [ arr1 [ i ] ] ) : NEW_LINE INDENT sumOfElements += arr1 [ i ] NEW_LINE DEDENT DEDENT return sumOfElements NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr1 = [ 2 , 5 , 6 , 8 ] NEW_LINE arr2 = [ 4 , 10 ] NEW_LINE m = len ( arr1 ) NEW_LINE n = len ( arr2 ) NEW_LINE print ( findSumofEle ( arr1 , m , arr2 , n ) ) NEW_LINE DEDENT
T39803
import java . util . * ; class GFG { static boolean isWordPresent ( String sentence , String word ) { word = transform ( word ) ; sentence = transform ( sentence ) ; String [ ] s = sentence . split ( " ▁ " ) ; for ( String temp : s ) { if ( temp . compareTo ( word ) == 0 ) { return true ; } } return false ; } static String transform ( String word ) { return word . toUpperCase ( ) ; } public static void main ( String [ ] args ) { String s = " Geeks ▁ for ▁ Geeks " ; String word = " geeks " ; if ( isWordPresent ( s , word ) ) System . out . print ( " Yes " ) ; else System . out . print ( " No " ) ; } }
def isWordPresent ( sentence , word ) : NEW_LINE INDENT word = word . upper ( ) NEW_LINE sentence = sentence . upper ( ) NEW_LINE s = sentence . split ( ) ; NEW_LINE for temp in s : NEW_LINE INDENT if ( temp == word ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT DEDENT return False ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " Geeks ▁ for ▁ Geeks " ; NEW_LINE word = " geeks " ; NEW_LINE if ( isWordPresent ( s , word ) ) : NEW_LINE INDENT print ( " Yes " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) ; NEW_LINE DEDENT DEDENT
T39804
class GFG { static int n = 3 ; static int dp [ ] [ ] = new int [ n ] [ n ] ; static int [ ] [ ] v = new int [ n ] [ n ] ; static int minSteps ( int i , int j , int arr [ ] [ ] ) { if ( i == n - 1 && j == n - 1 ) { return 0 ; } if ( i > n - 1 || j > n - 1 ) { return 9999999 ; } if ( v [ i ] [ j ] == 1 ) { return dp [ i ] [ j ] ; } v [ i ] [ j ] = 1 ; dp [ i ] [ j ] = 1 + Math . min ( minSteps ( i + arr [ i ] [ j ] , j , arr ) , minSteps ( i , j + arr [ i ] [ j ] , arr ) ) ; return dp [ i ] [ j ] ; } public static void main ( String [ ] args ) { int arr [ ] [ ] = { { 2 , 1 , 2 } , { 1 , 1 , 1 } , { 1 , 1 , 1 } } ; int ans = minSteps ( 0 , 0 , arr ) ; if ( ans >= 9999999 ) { System . out . println ( - 1 ) ; } else { System . out . println ( ans ) ; } } }
import numpy as np ; NEW_LINE n = 3 NEW_LINE dp = np . zeros ( ( n , n ) ) ; NEW_LINE v = np . zeros ( ( n , n ) ) ; NEW_LINE def minSteps ( i , j , arr ) : NEW_LINE INDENT if ( i == n - 1 and j == n - 1 ) : NEW_LINE INDENT return 0 ; NEW_LINE DEDENT if ( i > n - 1 or j > n - 1 ) : NEW_LINE INDENT return 9999999 ; NEW_LINE DEDENT if ( v [ i ] [ j ] ) : NEW_LINE INDENT return dp [ i ] [ j ] ; NEW_LINE DEDENT v [ i ] [ j ] = 1 ; NEW_LINE dp [ i ] [ j ] = 1 + min ( minSteps ( i + arr [ i ] [ j ] , j , arr ) , minSteps ( i , j + arr [ i ] [ j ] , arr ) ) ; NEW_LINE return dp [ i ] [ j ] ; NEW_LINE DEDENT arr = [ [ 2 , 1 , 2 ] , [ 1 , 1 , 1 ] , [ 1 , 1 , 1 ] ] ; NEW_LINE ans = minSteps ( 0 , 0 , arr ) ; NEW_LINE if ( ans >= 9999999 ) : NEW_LINE INDENT print ( - 1 ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( ans ) ; NEW_LINE DEDENT
T39805
class GFG { public static long SubArraySum ( int arr [ ] , int n ) { long result = 0 ; for ( int i = 0 ; i < n ; i ++ ) result += ( arr [ i ] * ( i + 1 ) * ( n - i ) ) ; return result ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 } ; int n = arr . length ; System . out . println ( " Sum ▁ of ▁ SubArray ▁ " + SubArraySum ( arr , n ) ) ; } }
def SubArraySum ( arr , n ) : NEW_LINE INDENT result = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT result += ( arr [ i ] * ( i + 1 ) * ( n - i ) ) NEW_LINE DEDENT return result NEW_LINE DEDENT arr = [ 1 , 2 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Sum ▁ of ▁ SubArray ▁ : ▁ " , SubArraySum ( arr , n ) ) NEW_LINE
T39806
import java . util . Arrays ; class GFG { static int minRadius ( int k , int [ ] x , int [ ] y , int n ) { int [ ] dis = new int [ n ] ; for ( int i = 0 ; i < n ; i ++ ) dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; Arrays . sort ( dis ) ; return dis [ k - 1 ] ; } public static void main ( String [ ] args ) { int k = 3 ; int [ ] x = { 1 , - 1 , 1 } ; int [ ] y = { 1 , - 1 , - 1 } ; int n = x . length ; System . out . println ( minRadius ( k , x , y , n ) ) ; } }
def minRadius ( k , x , y , n ) : NEW_LINE INDENT dis = [ 0 ] * n NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT dis [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] NEW_LINE DEDENT dis . sort ( ) NEW_LINE return dis [ k - 1 ] NEW_LINE DEDENT k = 3 NEW_LINE x = [ 1 , - 1 , 1 ] NEW_LINE y = [ 1 , - 1 , - 1 ] NEW_LINE n = len ( x ) NEW_LINE print ( minRadius ( k , x , y , n ) ) NEW_LINE
T39807
import java . util . * ; class GFG { static void findPerm ( int n , Vector < Integer > differences ) { Vector < Integer > ans = new Vector < Integer > ( ) ; ans . clear ( ) ; ans . add ( 0 ) ; int x = 0 ; for ( int i = 0 ; i <= n - 2 ; ++ i ) { int diff = differences . get ( i ) ; x = x + diff ; ans . add ( x ) ; } Vector < Integer > anss = new Vector < Integer > ( ) ; for ( Integer obj : ans ) anss . add ( obj ) ; Collections . sort ( ans ) ; int flag = - 1 ; for ( int i = 1 ; i <= n - 1 ; ++ i ) { int res = ans . get ( i ) - ans . get ( i - 1 ) ; if ( res != 1 ) { flag = 0 ; } } if ( flag == 0 ) { System . out . print ( - 1 ) ; return ; } else { Map < Integer , Integer > mpp = new HashMap < > ( ) ; mpp . clear ( ) ; int j = 1 ; Vector < Integer > value_at_index = new Vector < Integer > ( ) ; for ( Integer x1 : ans ) { mpp . put ( x1 , j ) ; ++ j ; } for ( Integer x2 : anss ) { value_at_index . add ( mpp . get ( x2 ) ) ; } for ( Integer x3 : value_at_index ) { System . out . print ( x3 + " ▁ " ) ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { Vector < Integer > differences = new Vector < Integer > ( ) ; differences . add ( 2 ) ; differences . add ( - 3 ) ; differences . add ( 2 ) ; int n = differences . size ( ) + 1 ; findPerm ( n , differences ) ; } }
def findPerm ( n , differences ) : NEW_LINE INDENT ans = [ ] NEW_LINE ans . append ( 0 ) NEW_LINE x = 0 NEW_LINE for i in range ( n - 1 ) : NEW_LINE INDENT diff = differences [ i ] NEW_LINE x = x + diff NEW_LINE ans . append ( x ) NEW_LINE DEDENT anss = ans NEW_LINE ans = sorted ( ans ) NEW_LINE flag = - 1 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT res = ans [ i ] - ans [ i - 1 ] NEW_LINE if ( res != 1 ) : NEW_LINE INDENT flag = 0 NEW_LINE DEDENT DEDENT if ( flag == 0 ) : NEW_LINE INDENT print ( " - 1" ) NEW_LINE return NEW_LINE DEDENT else : NEW_LINE INDENT mpp = dict ( ) NEW_LINE j = 1 NEW_LINE value_at_index = [ ] NEW_LINE for x in ans : NEW_LINE INDENT mpp [ x ] = j NEW_LINE j += 1 NEW_LINE DEDENT for x in anss : NEW_LINE INDENT value_at_index . append ( mpp [ x ] ) NEW_LINE DEDENT for x in value_at_index : NEW_LINE INDENT print ( x , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT differences = [ ] NEW_LINE differences . append ( 2 ) NEW_LINE differences . append ( - 3 ) NEW_LINE differences . append ( 2 ) NEW_LINE n = len ( differences ) + 1 NEW_LINE findPerm ( n , differences ) NEW_LINE
T39808
class GFG { static final int INT_SIZE = 32 ; static class TrieNode { int value ; TrieNode [ ] Child = new TrieNode [ 2 ] ; public TrieNode ( ) { value = 0 ; Child [ 0 ] = null ; Child [ 1 ] = null ; } } static TrieNode root ; static void insert ( int key ) { TrieNode temp = root ; for ( int i = INT_SIZE - 1 ; i >= 0 ; i -- ) { int current_bit = ( key & ( 1 << i ) ) >= 1 ? 1 : 0 ; if ( temp != null && temp . Child [ current_bit ] == null ) temp . Child [ current_bit ] = new TrieNode ( ) ; temp = temp . Child [ current_bit ] ; } temp . value = key ; } static int minXORUtil ( int key ) { TrieNode temp = root ; for ( int i = INT_SIZE - 1 ; i >= 0 ; i -- ) { int current_bit = ( key & ( 1 << i ) ) >= 1 ? 1 : 0 ; if ( temp . Child [ current_bit ] != null ) temp = temp . Child [ current_bit ] ; else if ( temp . Child [ 1 - current_bit ] != null ) temp = temp . Child [ 1 - current_bit ] ; } return key ^ temp . value ; } static int minXOR ( int arr [ ] , int n ) { int min_xor = Integer . MAX_VALUE ; root = new TrieNode ( ) ; insert ( arr [ 0 ] ) ; for ( int i = 1 ; i < n ; i ++ ) { min_xor = Math . min ( min_xor , minXORUtil ( arr [ i ] ) ) ; insert ( arr [ i ] ) ; } return min_xor ; } public static void main ( String args [ ] ) { int arr [ ] = { 9 , 5 , 3 } ; int n = arr . length ; System . out . println ( minXOR ( arr , n ) ) ; } }
class TrieNode : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . child = [ None ] * 2 NEW_LINE self . value = None NEW_LINE DEDENT DEDENT class Trie : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . root = self . getNode ( ) NEW_LINE DEDENT def getNode ( self ) : NEW_LINE INDENT return TrieNode ( ) NEW_LINE DEDENT def insert ( self , key ) : NEW_LINE INDENT temp = self . root NEW_LINE for i in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT curr = ( key >> i ) & ( 1 ) NEW_LINE if ( temp . child [ curr ] is None ) : NEW_LINE INDENT temp . child [ curr ] = self . getNode ( ) NEW_LINE DEDENT temp = temp . child [ curr ] NEW_LINE DEDENT temp . value = key NEW_LINE DEDENT def xorUtil ( self , key ) : NEW_LINE INDENT temp = self . root NEW_LINE for i in range ( 31 , - 1 , - 1 ) : NEW_LINE INDENT curr = ( key >> i ) & 1 NEW_LINE if ( temp . child [ curr ] is not None ) : NEW_LINE INDENT temp = temp . child [ curr ] NEW_LINE DEDENT elif ( temp . child [ 1 - curr ] is not None ) : NEW_LINE INDENT temp = temp . child [ 1 - curr ] NEW_LINE DEDENT DEDENT return temp . value ^ key NEW_LINE DEDENT DEDENT def minXor ( arr ) : NEW_LINE INDENT m = 2 ** 30 NEW_LINE trie = Trie ( ) NEW_LINE trie . insert ( arr [ 0 ] ) NEW_LINE for i in range ( 1 , len ( arr ) ) : NEW_LINE INDENT m = min ( m , trie . xorUtil ( arr [ i ] ) ) NEW_LINE trie . insert ( arr [ i ] ) NEW_LINE DEDENT return m NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT sample = [ 9 , 5 , 3 ] NEW_LINE print ( minXor ( sample ) ) NEW_LINE DEDENT
T39809
import javafx . util . Pair ; class GFG { static Pair < Double , Double > findFoot ( double a , double b , double c , double x1 , double y1 ) { double temp = - 1 * ( a * x1 + b * y1 + c ) / ( a * a + b * b ) ; double x = temp * a + x1 ; double y = temp * b + y1 ; return new Pair ( x , y ) ; } public static void main ( String [ ] args ) { double a = 0.0 ; double b = 1.0 ; double c = - 2 ; double x1 = 3.0 ; double y1 = 3.0 ; Pair < Double , Double > foot = findFoot ( a , b , c , x1 , y1 ) ; System . out . println ( foot . getKey ( ) + " ▁ " + foot . getValue ( ) ) ; } }
def findFoot ( a , b , c , x1 , y1 ) : NEW_LINE INDENT temp = ( - 1 * ( a * x1 + b * y1 + c ) // ( a * a + b * b ) ) NEW_LINE x = temp * a + x1 NEW_LINE y = temp * b + y1 NEW_LINE return ( x , y ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b , c = 0.0 , 1.0 , - 2 NEW_LINE x1 , y1 = 3.0 , 3.0 NEW_LINE foot = findFoot ( a , b , c , x1 , y1 ) NEW_LINE print ( int ( foot [ 0 ] ) , int ( foot [ 1 ] ) ) NEW_LINE DEDENT
T39810
import java . io . * ; class GFG { static int MAX = 100 ; static int summation ( int n ) { return n << ( n - 1 ) ; } public static void main ( String [ ] args ) { int n = 2 ; System . out . println ( summation ( n ) ) ; } }
def summation ( n ) : NEW_LINE INDENT return n << ( n - 1 ) ; NEW_LINE DEDENT n = 2 ; NEW_LINE print ( summation ( n ) ) ; NEW_LINE
T39811
class GFG { static int sz = ( int ) 1e5 ; static boolean [ ] isPrime = new boolean [ sz + 1 ] ; static void sieve ( ) { for ( int i = 0 ; i < sz + 1 ; i ++ ) isPrime [ i ] = true ; isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( int i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( int j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } static void printArray ( int arr [ ] , int len ) { for ( int i = 0 ; i < len ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } } static void removePrimes ( int arr [ ] , int len ) { sieve ( ) ; for ( int i = 0 ; i < len ; i ++ ) { if ( isPrime [ arr [ i ] ] ) { for ( int j = i ; j < len - 1 ; j ++ ) { arr [ j ] = arr [ j + 1 ] ; } i -- ; len -- ; } } printArray ( arr , len ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 4 , 6 , 5 , 3 , 8 , 7 , 10 , 11 , 14 , 15 } ; int len = arr . length ; removePrimes ( arr , len ) ; } }
sz = 10 ** 5 NEW_LINE isPrime = [ True for i in range ( sz + 1 ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT isPrime [ 0 ] = isPrime [ 1 ] = False NEW_LINE i = 2 NEW_LINE while i * i < sz : NEW_LINE INDENT if ( isPrime [ i ] ) : NEW_LINE INDENT for j in range ( i * i , sz , i ) : NEW_LINE INDENT isPrime [ j ] = False NEW_LINE DEDENT DEDENT i += 1 NEW_LINE DEDENT DEDENT def prArray ( arr , lenn ) : NEW_LINE INDENT for i in range ( lenn ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT def removePrimes ( arr , lenn ) : NEW_LINE INDENT sieve ( ) NEW_LINE i = 0 NEW_LINE while i < lenn : NEW_LINE INDENT if ( isPrime [ arr [ i ] ] ) : NEW_LINE INDENT for j in range ( i , lenn - 1 ) : NEW_LINE INDENT arr [ j ] = arr [ j + 1 ] NEW_LINE DEDENT i -= 1 NEW_LINE lenn -= 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT prArray ( arr , lenn ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 4 , 6 , 5 , 3 , 8 , 7 , 10 , 11 , 14 , 15 ] NEW_LINE lenn = len ( arr ) NEW_LINE removePrimes ( arr , lenn ) NEW_LINE DEDENT
T39812
public class GFG { static int maxPrefix ( String s , String t ) { int count = 0 ; for ( int i = 0 ; i < t . length ( ) ; i ++ ) { if ( count == s . length ( ) ) break ; if ( t . charAt ( i ) == s . charAt ( count ) ) count ++ ; } return count ; } public static void main ( String args [ ] ) { String S = " digger " ; String T = " biggerdiagram " ; System . out . println ( maxPrefix ( S , T ) ) ; } }
def maxPrefix ( s , t ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , len ( t ) ) : NEW_LINE INDENT if ( count == len ( s ) ) : NEW_LINE INDENT break NEW_LINE DEDENT if ( t [ i ] == s [ count ] ) : NEW_LINE INDENT count = count + 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT S = " digger " NEW_LINE T = " biggerdiagram " NEW_LINE print ( maxPrefix ( S , T ) ) NEW_LINE
T39813
import java . io . * ; class GFG { static int nthTerm ( int n ) { return ( int ) Math . pow ( n , 2 ) + 4 * n ; } public static void main ( String [ ] args ) { int N = 4 ; System . out . println ( nthTerm ( N ) ) ; } }
def nthTerm ( n ) : NEW_LINE INDENT return n ** 2 + 4 * n ; NEW_LINE DEDENT N = 4 NEW_LINE print ( nthTerm ( N ) ) NEW_LINE
T39814
import java . io . * ; class GFG { static void MinSteps ( int SourceX , int SourceY , int DestX , int DestY ) { System . out . println ( Math . max ( Math . abs ( SourceX - DestX ) , Math . abs ( SourceY - DestY ) ) ) ; while ( ( SourceX != DestX ) || ( SourceY != DestY ) ) { if ( SourceX < DestX ) { System . out . print ( ' U ' ) ; SourceX ++ ; } if ( SourceX > DestX ) { System . out . println ( ' D ' ) ; SourceX -- ; } if ( SourceY > DestY ) { System . out . print ( ' L ' ) ; SourceY -- ; } if ( SourceY < DestY ) { System . out . print ( ' R ' ) ; SourceY ++ ; } System . out . println ( ) ; } } public static void main ( String [ ] args ) { int sourceX = 4 , sourceY = 4 ; int destinationX = 7 , destinationY = 0 ; MinSteps ( sourceX , sourceY , destinationX , destinationY ) ; } }
def MinSteps ( SourceX , SourceY , DestX , DestY ) : NEW_LINE INDENT print ( max ( abs ( SourceX - DestX ) , abs ( SourceY - DestY ) ) ) NEW_LINE while ( ( SourceX != DestX ) or ( SourceY != DestY ) ) : NEW_LINE INDENT if ( SourceX < DestX ) : NEW_LINE INDENT print ( ' U ' , end = " " ) NEW_LINE SourceX += 1 NEW_LINE DEDENT if ( SourceX > DestX ) : NEW_LINE INDENT print ( ' D ' , end = " " ) NEW_LINE SourceX -= 1 NEW_LINE DEDENT if ( SourceY > DestY ) : NEW_LINE INDENT print ( ' L ' ) NEW_LINE SourceY -= 1 NEW_LINE DEDENT if ( SourceY < DestY ) : NEW_LINE INDENT print ( ' R ' , end = " " ) NEW_LINE SourceY += 1 NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sourceX = 4 NEW_LINE sourceY = 4 NEW_LINE destinationX = 7 NEW_LINE destinationY = 0 NEW_LINE MinSteps ( sourceX , sourceY , destinationX , destinationY ) NEW_LINE DEDENT
T39815
import java . util . * ; class GFG { public static void preprocess ( int p [ ] , int x [ ] , int y [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) p [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] ; Arrays . sort ( p ) ; } public static int query ( int p [ ] , int n , int rad ) { int start = 0 , end = n - 1 ; while ( ( end - start ) > 1 ) { int mid = ( start + end ) / 2 ; double tp = Math . sqrt ( p [ mid ] ) ; if ( tp > ( rad * 1.0 ) ) end = mid - 1 ; else start = mid ; } double tp1 = Math . sqrt ( p [ start ] ) ; double tp2 = Math . sqrt ( p [ end ] ) ; if ( tp1 > ( rad * 1.0 ) ) return 0 ; else if ( tp2 <= ( rad * 1.0 ) ) return end + 1 ; else return start + 1 ; } public static void main ( String [ ] args ) { int x [ ] = { 1 , 2 , 3 , - 1 , 4 } ; int y [ ] = { 1 , 2 , 3 , - 1 , 4 } ; int n = x . length ; int p [ ] = new int [ n ] ; preprocess ( p , x , y , n ) ; System . out . println ( query ( p , n , 3 ) ) ; System . out . println ( query ( p , n , 32 ) ) ; } }
import math NEW_LINE def preprocess ( p , x , y , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT p [ i ] = x [ i ] * x [ i ] + y [ i ] * y [ i ] NEW_LINE DEDENT p . sort ( ) NEW_LINE DEDENT def query ( p , n , rad ) : NEW_LINE INDENT start = 0 NEW_LINE end = n - 1 NEW_LINE while ( ( end - start ) > 1 ) : NEW_LINE INDENT mid = ( start + end ) // 2 NEW_LINE tp = math . sqrt ( p [ mid ] ) NEW_LINE if ( tp > ( rad * 1.0 ) ) : NEW_LINE INDENT end = mid - 1 NEW_LINE DEDENT else : NEW_LINE INDENT start = mid NEW_LINE DEDENT DEDENT tp1 = math . sqrt ( p [ start ] ) NEW_LINE tp2 = math . sqrt ( p [ end ] ) NEW_LINE if ( tp1 > ( rad * 1.0 ) ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT elif ( tp2 <= ( rad * 1.0 ) ) : NEW_LINE INDENT return end + 1 NEW_LINE DEDENT else : NEW_LINE INDENT return start + 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = [ 1 , 2 , 3 , - 1 , 4 ] NEW_LINE y = [ 1 , 2 , 3 , - 1 , 4 ] NEW_LINE n = len ( x ) NEW_LINE p = [ 0 ] * n NEW_LINE preprocess ( p , x , y , n ) NEW_LINE print ( query ( p , n , 3 ) ) NEW_LINE print ( query ( p , n , 32 ) ) NEW_LINE DEDENT
T39816
import java . io . * ; class GFG { static int maxSum ( int arr [ ] , int n ) { int cum_sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) cum_sum += arr [ i ] ; int curr_val = 0 ; for ( int i = 0 ; i < n ; i ++ ) curr_val += i * arr [ i ] ; int res = curr_val ; for ( int i = 1 ; i < n ; i ++ ) { int next_val = curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 ) ; curr_val = next_val ; res = Math . max ( res , next_val ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 3 , 1 , 2 } ; int n = arr . length ; System . out . println ( maxSum ( arr , n ) ) ; } }
def maxSum ( arr , n ) : NEW_LINE INDENT cum_sum = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT cum_sum += arr [ i ] NEW_LINE DEDENT curr_val = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT curr_val += i * arr [ i ] NEW_LINE DEDENT res = curr_val NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT next_val = ( curr_val - ( cum_sum - arr [ i - 1 ] ) + arr [ i - 1 ] * ( n - 1 ) ) NEW_LINE curr_val = next_val NEW_LINE res = max ( res , next_val ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 8 , 3 , 1 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxSum ( arr , n ) ) NEW_LINE
T39817
import java . util . * ; class GFG { static void getNumbers ( int a [ ] , int n ) { HashMap < Integer , Integer > freq = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) if ( freq . containsKey ( a [ i ] ) ) { freq . put ( a [ i ] , freq . get ( a [ i ] ) + 1 ) ; } else { freq . put ( a [ i ] , 1 ) ; } int maxi1 = Arrays . stream ( a ) . max ( ) . getAsInt ( ) ; for ( int i = 1 ; i * i <= maxi1 ; i ++ ) { if ( maxi1 % i == 0 && freq . containsKey ( i ) && freq . get ( i ) != 0 ) { freq . put ( i , freq . get ( i ) - 1 ) ; if ( i != ( maxi1 / i ) && freq . containsKey ( maxi1 / i ) && freq . get ( maxi1 / i ) != 0 ) freq . put ( maxi1 / i , freq . get ( maxi1 / i ) - 1 ) ; } } int maxi2 = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { if ( freq . get ( a [ i ] ) != 0 ) maxi2 = Math . max ( maxi2 , a [ i ] ) ; } System . out . print ( maxi1 + " ▁ " + maxi2 ) ; } public static void main ( String [ ] args ) { int a [ ] = { 10 , 2 , 8 , 1 , 2 , 4 , 1 , 20 , 4 , 5 } ; int n = a . length ; getNumbers ( a , n ) ; } }
from math import sqrt NEW_LINE def getNumbers ( a , n ) : NEW_LINE INDENT freq = { } ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] not in freq . keys ( ) : NEW_LINE INDENT freq [ a [ i ] ] = 0 NEW_LINE DEDENT freq [ a [ i ] ] += 1 NEW_LINE DEDENT maxi1 = max ( a ) NEW_LINE for i in range ( 1 , int ( sqrt ( maxi1 ) ) + 1 ) : NEW_LINE INDENT if ( maxi1 % i == 0 and freq [ i ] in freq . keys ( ) and freq [ i ] != 0 ) : NEW_LINE INDENT freq [ i ] -= 1 NEW_LINE if ( i != ( maxi1 // i ) and freq [ maxi1 // i ] in freq . keys ( ) and freq [ maxi1 // i ] != 0 ) : NEW_LINE INDENT freq [ maxi1 // i ] -= 1 NEW_LINE DEDENT DEDENT DEDENT maxi2 = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( freq [ a [ i ] ] != 0 ) : NEW_LINE INDENT maxi2 = max ( maxi2 , a [ i ] ) NEW_LINE DEDENT DEDENT print ( maxi1 , maxi2 ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 10 , 2 , 8 , 1 , 2 , 4 , 1 , 20 , 4 , 5 ] NEW_LINE n = len ( a ) NEW_LINE getNumbers ( a , n ) NEW_LINE DEDENT
T39818
import java . io . * ; import java . util . * ; import java . util . Arrays ; class GFG { static boolean check ( String s , int l ) { int freq [ ] = new int [ 26 ] ; Arrays . fill ( freq , 0 ) ; for ( int i = 0 ; i < l ; i ++ ) { freq [ s . charAt ( i ) - ' a ' ] ++ ; } for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] >= 2 ) return true ; } return false ; } public static void main ( String args [ ] ) { String s = " geeksforgeeks " ; int l = s . length ( ) ; if ( check ( s , l ) ) System . out . print ( " YES " ) ; else System . out . print ( " NO " ) ; } }
def check ( s , l ) : NEW_LINE INDENT freq = [ 0 for i in range ( 26 ) ] NEW_LINE for i in range ( l ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] >= 2 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = " geeksforgeeks " NEW_LINE l = len ( s ) NEW_LINE if ( check ( s , l ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT
T39819
import java . io . * ; class GFG { static char findExtraCharcter ( String strA , String strB ) { int res = 0 , i ; for ( i = 0 ; i < strA . length ( ) ; i ++ ) { res ^= strA . charAt ( i ) ; } for ( i = 0 ; i < strB . length ( ) ; i ++ ) { res ^= strB . charAt ( i ) ; } return ( ( char ) ( res ) ) ; } public static void main ( String args [ ] ) { String strA = " abcd " ; String strB = " cbdad " ; System . out . println ( findExtraCharcter ( strA , strB ) ) ; } }
def findExtraCharcter ( strA , strB ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 0 , len ( strA ) ) : NEW_LINE INDENT res = res ^ ( ord ) ( strA [ i ] ) NEW_LINE DEDENT for i in range ( 0 , len ( strB ) ) : NEW_LINE INDENT res = res ^ ( ord ) ( strB [ i ] ) NEW_LINE DEDENT return ( ( chr ) ( res ) ) ; NEW_LINE DEDENT strA = " abcd " NEW_LINE strB = " cbdad " NEW_LINE print ( findExtraCharcter ( strA , strB ) ) NEW_LINE
T39820
class GFG { static int find_k ( int a , int b ) { if ( ( a + b ) % 2 == 0 ) return ( ( a + b ) / 2 ) ; return - 1 ; } public static void main ( String [ ] args ) { int a = 2 , b = 16 ; System . out . println ( find_k ( a , b ) ) ; } }
def find_k ( a , b ) : NEW_LINE INDENT if ( ( a + b ) % 2 == 0 ) : NEW_LINE INDENT return ( ( a + b ) // 2 ) ; NEW_LINE DEDENT return - 1 ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 2 ; b = 16 ; NEW_LINE print ( find_k ( a , b ) ) ; NEW_LINE DEDENT
T39821
class GFG { static int findMinDel ( int [ ] arr , int n ) { int min_num = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) min_num = Math . min ( arr [ i ] , min_num ) ; int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == min_num ) cnt ++ ; return n - cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 3 , 2 } ; int n = arr . length ; System . out . print ( findMinDel ( arr , n ) ) ; } }
import sys NEW_LINE def findMinDel ( arr , n ) : NEW_LINE INDENT min_num = sys . maxsize ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT min_num = min ( arr [ i ] , min_num ) ; NEW_LINE DEDENT cnt = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] == min_num ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT return n - cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 3 , 3 , 2 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( findMinDel ( arr , n ) ) ; NEW_LINE DEDENT
T39822
import static java . lang . Math . abs ; import java . util . * ; class GFG { static int solution ( Vector < Integer > arr , int x ) { Collections . sort ( arr ) ; int closestSum = Integer . MAX_VALUE ; for ( int i = 0 ; i < arr . size ( ) - 2 ; i ++ ) { int ptr1 = i + 1 , ptr2 = arr . size ( ) - 1 ; while ( ptr1 < ptr2 ) { int sum = arr . get ( i ) + arr . get ( ptr1 ) + arr . get ( ptr2 ) ; if ( abs ( x - sum ) < abs ( x - closestSum ) ) { closestSum = sum ; } if ( sum > x ) { ptr2 -- ; } else { ptr1 ++ ; } } } return closestSum ; } public static void main ( String [ ] args ) { Vector arr = new Vector ( Arrays . asList ( - 1 , 2 , 1 , - 4 ) ) ; int x = 1 ; System . out . println ( solution ( arr , x ) ) ; } }
import sys NEW_LINE def solution ( arr , x ) : NEW_LINE INDENT arr . sort ( ) ; NEW_LINE closestSum = sys . maxsize ; NEW_LINE for i in range ( len ( arr ) - 2 ) : NEW_LINE INDENT ptr1 = i + 1 ; ptr2 = len ( arr ) - 1 ; NEW_LINE while ( ptr1 < ptr2 ) : NEW_LINE INDENT sum = arr [ i ] + arr [ ptr1 ] + arr [ ptr2 ] ; NEW_LINE if ( abs ( x - sum ) < abs ( x - closestSum ) ) : NEW_LINE INDENT closestSum = sum ; NEW_LINE DEDENT if ( sum > x ) : NEW_LINE INDENT ptr2 -= 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT ptr1 += 1 ; NEW_LINE DEDENT DEDENT DEDENT return closestSum ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 1 , 2 , 1 , - 4 ] ; NEW_LINE x = 1 ; NEW_LINE print ( solution ( arr , x ) ) ; NEW_LINE DEDENT
T39823
import java . util . * ; import java . lang . * ; public class GfG { public static int findOccurrences ( String str1 , String substr1 ) { int n = str1 . length ( ) ; char [ ] str = str1 . toCharArray ( ) ; char [ ] substr = substr1 . toCharArray ( ) ; int [ ] preLeft = new int [ n ] ; int [ ] preRight = new int [ n ] ; if ( str [ 0 ] == substr [ 0 ] ) preLeft [ 0 ] ++ ; for ( int i = 1 ; i < n ; i ++ ) { if ( str [ i ] == substr [ 0 ] ) preLeft [ i ] = preLeft [ i - 1 ] + 1 ; else preLeft [ i ] = preLeft [ i - 1 ] ; } if ( str [ n - 1 ] == substr [ 2 ] ) preRight [ n - 1 ] ++ ; for ( int i = n - 2 ; i >= 0 ; i -- ) { if ( str [ i ] == substr [ 2 ] ) preRight [ i ] = preRight [ i + 1 ] + 1 ; else preRight [ i ] = preRight [ i + 1 ] ; } int counter = 0 ; for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( str [ i ] == str [ 1 ] ) { int total = preLeft [ i - 1 ] * preRight [ i + 1 ] ; counter += total ; } } return counter ; } public static void main ( String argc [ ] ) { String str = " GFGFGYSYIOIWIN " ; String substr = " GFG " ; System . out . println ( findOccurrences ( str , substr ) ) ; } }
def findOccurrences ( str1 , substr ) : NEW_LINE INDENT n = len ( str1 ) NEW_LINE preLeft = [ 0 for i in range ( n ) ] NEW_LINE preRight = [ 0 for i in range ( n ) ] NEW_LINE if ( str1 [ 0 ] == substr [ 0 ] ) : NEW_LINE INDENT preLeft [ 0 ] += 1 NEW_LINE DEDENT for i in range ( 1 , n ) : NEW_LINE INDENT if ( str1 [ i ] == substr [ 0 ] ) : NEW_LINE INDENT preLeft [ i ] = preLeft [ i - 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT preLeft [ i ] = preLeft [ i - 1 ] NEW_LINE DEDENT DEDENT if ( str1 [ n - 1 ] == substr [ 2 ] ) : NEW_LINE INDENT preRight [ n - 1 ] += 1 NEW_LINE DEDENT i = n - 2 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT if ( str1 [ i ] == substr [ 2 ] ) : NEW_LINE INDENT preRight [ i ] = preRight [ i + 1 ] + 1 NEW_LINE DEDENT else : NEW_LINE INDENT preRight [ i ] = preRight [ i + 1 ] NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT counter = 0 NEW_LINE for i in range ( 1 , n - 1 ) : NEW_LINE INDENT if ( str1 [ i ] == str1 [ 1 ] ) : NEW_LINE INDENT total = preLeft [ i - 1 ] * preRight [ i + 1 ] NEW_LINE counter += total NEW_LINE DEDENT DEDENT return counter NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT str1 = " GFGFGYSYIOIWIN " NEW_LINE substr = " GFG " NEW_LINE print ( findOccurrences ( str1 , substr ) ) NEW_LINE DEDENT
T39824
import java . util . * ; class GFG { static int N = 100000 ; static int prime [ ] = new int [ N ] ; static int divi [ ] = new int [ N ] ; static int sum [ ] = new int [ N ] ; static void SieveOfEratosthenes ( ) { for ( int i = 0 ; i < N ; i ++ ) prime [ i ] = 1 ; prime [ 0 ] = prime [ 1 ] = 0 ; for ( int p = 2 ; p * p < N ; p ++ ) { if ( prime [ p ] == 1 ) { for ( int i = p * p ; i < N ; i += p ) prime [ i ] = 0 ; } } } static void DivisorCount ( ) { for ( int i = 1 ; i < N ; i ++ ) { for ( int j = i ; j < N ; j += i ) { divi [ j ] ++ ; } } } static void pre ( ) { for ( int i = 1 ; i < N ; i ++ ) { if ( prime [ divi [ i ] ] == 1 ) { sum [ i ] = i ; } } for ( int i = 1 ; i < N ; i ++ ) sum [ i ] += sum [ i - 1 ] ; } public static void main ( String args [ ] ) { int l = 5 , r = 8 ; SieveOfEratosthenes ( ) ; DivisorCount ( ) ; pre ( ) ; System . out . println ( sum [ r ] - sum [ l - 1 ] ) ; } }
from math import sqrt NEW_LINE N = 100000 ; NEW_LINE prime = [ 1 ] * N ; NEW_LINE divi = [ 0 ] * N ; NEW_LINE sum = [ 0 ] * N ; NEW_LINE def SieveOfEratosthenes ( ) : NEW_LINE INDENT for i in range ( N ) : NEW_LINE INDENT prime [ i ] = 1 ; NEW_LINE DEDENT prime [ 0 ] = prime [ 1 ] = 0 ; NEW_LINE for p in range ( 2 , int ( sqrt ( N ) ) + 1 ) : NEW_LINE INDENT if ( prime [ p ] == 1 ) : NEW_LINE INDENT for i in range ( p * p , N , p ) : NEW_LINE INDENT prime [ i ] = 0 ; NEW_LINE DEDENT DEDENT DEDENT DEDENT def DivisorCount ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT for j in range ( i , N , i ) : NEW_LINE INDENT divi [ j ] += 1 ; NEW_LINE DEDENT DEDENT DEDENT def pre ( ) : NEW_LINE INDENT for i in range ( 1 , N ) : NEW_LINE INDENT if ( prime [ divi [ i ] ] == 1 ) : NEW_LINE INDENT sum [ i ] = i ; NEW_LINE DEDENT DEDENT for i in range ( 1 , N ) : NEW_LINE INDENT sum [ i ] += sum [ i - 1 ] ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT l = 5 ; r = 8 ; NEW_LINE SieveOfEratosthenes ( ) ; NEW_LINE DivisorCount ( ) ; NEW_LINE pre ( ) ; NEW_LINE print ( sum [ r ] - sum [ l - 1 ] ) ; NEW_LINE DEDENT
T39825
import java . util . * ; class GFG { static void compareVal ( int x , int y ) { double a = y * Math . log ( x ) ; double b = x * Math . log ( y ) ; if ( a > b ) System . out . print ( x + " ^ " + y + " ▁ > ▁ " + y + " ^ " + x ) ; else if ( a < b ) System . out . print ( x + " ^ " + y + " ▁ < ▁ " + y + " ^ " + x ) ; else if ( a == b ) System . out . print ( x + " ^ " + y + " ▁ = ▁ " + y + " ^ " + x ) ; } public static void main ( String [ ] args ) { int x = 4 , y = 5 ; compareVal ( x , y ) ; } }
from math import log NEW_LINE def compareVal ( x , y ) : NEW_LINE INDENT a = y * log ( x ) ; NEW_LINE b = x * log ( y ) ; NEW_LINE if ( a > b ) : NEW_LINE INDENT print ( x , " ^ " , y , " > " , y , " ^ " , x ) ; NEW_LINE DEDENT elif ( a < b ) : NEW_LINE INDENT print ( x , " ^ " , y , " < " , y , " ^ " , x ) ; NEW_LINE DEDENT elif ( a == b ) : NEW_LINE INDENT print ( x , " ^ " , y , " = " , y , " ^ " , x ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 4 ; y = 5 ; NEW_LINE compareVal ( x , y ) ; NEW_LINE DEDENT
T39826
import java . io . * ; class GFG { static int division ( int num1 , int num2 ) { if ( num1 == 0 ) return 0 ; if ( num2 == 0 ) return Integer . MAX_VALUE ; boolean negResult = false ; if ( num1 < 0 ) { num1 = - num1 ; if ( num2 < 0 ) num2 = - num2 ; else negResult = true ; } else if ( num2 < 0 ) { num2 = - num2 ; negResult = true ; } int quotient = 0 ; while ( num1 >= num2 ) { num1 = num1 - num2 ; quotient ++ ; } if ( negResult ) quotient = - quotient ; return quotient ; } public static void main ( String [ ] args ) { int num1 = 13 , num2 = 2 ; System . out . println ( division ( num1 , num2 ) ) ; } }
def division ( num1 , num2 ) : NEW_LINE INDENT if ( num1 == 0 ) : return 0 NEW_LINE if ( num2 == 0 ) : return INT_MAX NEW_LINE negResult = 0 NEW_LINE if ( num1 < 0 ) : NEW_LINE INDENT num1 = - num1 NEW_LINE if ( num2 < 0 ) : NEW_LINE INDENT num2 = - num2 NEW_LINE DEDENT else : NEW_LINE INDENT negResult = true NEW_LINE DEDENT DEDENT elif ( num2 < 0 ) : NEW_LINE INDENT num2 = - num2 NEW_LINE negResult = true NEW_LINE DEDENT quotient = 0 NEW_LINE while ( num1 >= num2 ) : NEW_LINE INDENT num1 = num1 - num2 NEW_LINE quotient += 1 NEW_LINE DEDENT if ( negResult ) : NEW_LINE INDENT quotient = - quotient NEW_LINE DEDENT return quotient NEW_LINE DEDENT num1 = 13 ; num2 = 2 NEW_LINE print ( division ( num1 , num2 ) ) NEW_LINE
T39827
class GFG { static int countNonIncreasing ( int arr [ ] , int n ) { int cnt = 0 ; int len = 1 ; for ( int i = 0 ; i < n - 1 ; ++ i ) { if ( arr [ i + 1 ] >= arr [ i ] ) len ++ ; else { cnt += ( ( ( len + 1 ) * len ) / 2 ) ; len = 1 ; } } if ( len > 1 ) cnt += ( ( ( len - 1 ) * len ) / 2 ) ; return cnt ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 3 , 7 , 1 , 1 } ; int n = arr . length ; System . out . println ( countNonIncreasing ( arr , n ) ) ; } }
def countNonIncreasing ( arr , n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE len = 1 ; NEW_LINE for i in range ( 0 , n - 1 ) : NEW_LINE INDENT if ( arr [ i + 1 ] >= arr [ i ] ) : NEW_LINE INDENT len += 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT cnt += ( ( ( len + 1 ) * len ) / 2 ) ; NEW_LINE len = 1 ; NEW_LINE DEDENT DEDENT if ( len > 1 ) : NEW_LINE INDENT cnt += ( ( ( len - 1 ) * len ) / 2 ) ; NEW_LINE DEDENT return int ( cnt ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 5 , 2 , 3 , 7 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countNonIncreasing ( arr , n ) ) ; NEW_LINE DEDENT
T39828
class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static int smallestDivisor ( int x ) { if ( x % 2 == 0 ) return 2 ; for ( int i = 3 ; i * i <= x ; i += 2 ) { if ( x % i == 0 ) return i ; } return x ; } static int smallestInteger ( int [ ] arr , int n ) { int gcd = 0 ; for ( int i = 0 ; i < n ; i ++ ) gcd = __gcd ( gcd , arr [ i ] ) ; return smallestDivisor ( gcd ) ; } public static void main ( String [ ] args ) { int [ ] arr = { 2 , 4 , 8 } ; int n = arr . length ; System . out . println ( smallestInteger ( arr , n ) ) ; } }
from math import sqrt , gcd NEW_LINE def smallestDivisor ( x ) : NEW_LINE INDENT if ( x % 2 == 0 ) : NEW_LINE INDENT return 2 ; NEW_LINE DEDENT for i in range ( 3 , int ( sqrt ( x ) ) + 1 , 2 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return i ; NEW_LINE DEDENT DEDENT return x NEW_LINE DEDENT def smallestInteger ( arr , n ) : NEW_LINE INDENT __gcd = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT __gcd = gcd ( __gcd , arr [ i ] ) ; NEW_LINE DEDENT return smallestDivisor ( __gcd ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 2 , 4 , 8 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( smallestInteger ( arr , n ) ) ; NEW_LINE DEDENT
T39829
public class GFG { static int countDivisors ( int n ) { int x = 0 , ans = 1 ; while ( n % 2 == 0 ) { x ++ ; n = n / 2 ; } ans = ans * ( x + 1 ) ; for ( int i = 3 ; i <= Math . sqrt ( n ) ; i = i + 2 ) { x = 0 ; while ( n % i == 0 ) { x ++ ; n = n / i ; } ans = ans * ( x + 1 ) ; } if ( n > 2 ) ans = ans * 2 ; return ans ; } static int getTotalCount ( int n , int k ) { int k_count = countDivisors ( k ) ; int count = 0 ; for ( int i = 1 ; i < n ; i ++ ) if ( k_count == countDivisors ( i ) ) count ++ ; if ( k < n ) count = count - 1 ; return count ; } public static void main ( String [ ] args ) { int n = 500 , k = 6 ; System . out . println ( getTotalCount ( n , k ) ) ; } }
def countDivisors ( n ) : NEW_LINE INDENT x , ans = 0 , 1 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT x += 1 NEW_LINE n = n / 2 NEW_LINE DEDENT ans = ans * ( x + 1 ) NEW_LINE for i in range ( 3 , int ( n ** 1 / 2 ) + 1 , 2 ) : NEW_LINE INDENT x = 0 NEW_LINE while ( n % i == 0 ) : NEW_LINE INDENT x += 1 NEW_LINE n = n / i NEW_LINE DEDENT ans = ans * ( x + 1 ) NEW_LINE DEDENT if ( n > 2 ) : NEW_LINE INDENT ans = ans * 2 NEW_LINE DEDENT return ans NEW_LINE DEDENT def getTotalCount ( n , k ) : NEW_LINE INDENT k_count = countDivisors ( k ) NEW_LINE count = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( k_count == countDivisors ( i ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT if ( k < n ) : NEW_LINE INDENT count = count - 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n , k = 500 , 6 NEW_LINE print ( getTotalCount ( n , k ) ) NEW_LINE DEDENT
T39830
class GFG { static int distinctChars ( String s ) { int freq [ ] = new int [ 26 ] ; int count = 0 ; for ( int i = 0 ; i < s . length ( ) ; i ++ ) freq [ s . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < 26 ; i ++ ) { if ( freq [ i ] > 0 ) count ++ ; } return count ; } static int waysToSplit ( String s ) { int n = s . length ( ) ; int answer = 0 ; for ( int i = 1 ; i < n ; i ++ ) { String left = s . substring ( 0 , i ) ; String right = s . substring ( i , n ) ; if ( distinctChars ( left ) == distinctChars ( right ) ) answer ++ ; } return answer ; } public static void main ( String [ ] args ) { String s = " ababa " ; System . out . print ( waysToSplit ( s ) ) ; } }
def distinctChars ( s ) : NEW_LINE INDENT freq = [ 0 ] * 26 ; NEW_LINE count = 0 ; NEW_LINE for i in range ( len ( s ) ) : NEW_LINE INDENT freq [ ord ( s [ i ] ) - ord ( ' a ' ) ] += 1 ; NEW_LINE DEDENT for i in range ( 26 ) : NEW_LINE INDENT if ( freq [ i ] > 0 ) : NEW_LINE INDENT count += 1 ; NEW_LINE DEDENT DEDENT return count ; NEW_LINE DEDENT def waysToSplit ( s ) : NEW_LINE INDENT n = len ( s ) ; NEW_LINE answer = 0 ; NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT left = s [ 0 : i ] ; NEW_LINE right = s [ i : n ] ; NEW_LINE if ( distinctChars ( left ) == distinctChars ( right ) ) : NEW_LINE INDENT answer += 1 ; NEW_LINE DEDENT DEDENT return answer ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " ababa " ; NEW_LINE print ( waysToSplit ( s ) ) ; NEW_LINE DEDENT
T39831
class GFG { static int findMax ( int arr [ ] , int n ) { int res = 0 ; int i , j ; for ( i = 0 ; i < n ; i ++ ) { int count = 0 ; for ( j = 0 ; j < i ; j ++ ) { if ( arr [ j ] % arr [ i ] == 0 ) count += 1 ; } res = Math . max ( count , res ) ; } return res ; } public static void main ( String [ ] args ) { int arr [ ] = { 8 , 1 , 28 , 4 , 2 , 6 , 7 } ; int n = arr . length ; System . out . println ( findMax ( arr , n ) ) ; } }
def findMax ( arr , n ) : NEW_LINE INDENT res = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT count = 0 NEW_LINE for j in range ( 0 , i ) : NEW_LINE INDENT if arr [ j ] % arr [ i ] == 0 : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT res = max ( count , res ) NEW_LINE DEDENT return res NEW_LINE DEDENT arr = [ 8 , 1 , 28 , 4 , 2 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( findMax ( arr , n ) ) NEW_LINE
T39832
class GFG { static int maxBitElement ( int arr [ ] , int n ) { int num = 0 , max = - 1 ; for ( int i = 0 ; i < n ; i ++ ) { int cnt = Integer . bitCount ( arr [ i ] ) ; if ( cnt > max ) { max = cnt ; num = arr [ i ] ; } } return num ; } public static void main ( String [ ] args ) { int arr [ ] = { 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 } ; int n = arr . length ; System . out . print ( maxBitElement ( arr , n ) ) ; } }
def maxBitElement ( arr , n ) : NEW_LINE INDENT num = 0 NEW_LINE max = - 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT cnt = bin ( arr [ i ] ) . count ( '1' ) NEW_LINE if ( cnt > max ) : NEW_LINE INDENT max = cnt NEW_LINE num = arr [ i ] NEW_LINE DEDENT DEDENT return num NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 3 , 2 , 4 , 7 , 1 , 10 , 5 , 8 , 9 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE print ( maxBitElement ( arr , n ) ) NEW_LINE DEDENT
T39833
import java . io . * ; class GFG { static int maxPrimeFactors ( int n ) { int num = n ; int maxPrime = - 1 ; while ( n % 2 == 0 ) { maxPrime = 2 ; n /= 2 ; } for ( int i = 3 ; i <= Math . sqrt ( n ) ; i += 2 ) { while ( n % i == 0 ) { maxPrime = i ; n = n / i ; } } if ( n > 2 ) { maxPrime = n ; } int sum = maxPrime + num ; return sum ; } public static void main ( String [ ] args ) { int n = 19 ; System . out . println ( maxPrimeFactors ( n ) ) ; } }
from math import sqrt NEW_LINE def maxPrimeFactors ( n ) : NEW_LINE INDENT num = n NEW_LINE maxPrime = - 1 ; NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT maxPrime = 2 NEW_LINE n = n / 2 NEW_LINE DEDENT p = int ( sqrt ( n ) + 1 ) NEW_LINE for i in range ( 3 , p , 2 ) : NEW_LINE INDENT while ( n % i == 0 ) : NEW_LINE INDENT maxPrime = i NEW_LINE n = n / i NEW_LINE DEDENT DEDENT if ( n > 2 ) : NEW_LINE INDENT maxPrime = n NEW_LINE DEDENT sum = maxPrime + num NEW_LINE return sum NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 19 NEW_LINE print ( maxPrimeFactors ( n ) ) NEW_LINE DEDENT
T39834
import java . util . Arrays ; class GFG { static int MOD = 1000000007 ; static int max = 101 ; static long C [ ] [ ] = new long [ max ] [ max ] ; static long power ( long x , long y ) { long res = 1 ; x = x % MOD ; while ( y > 0 ) { if ( y % 2 == 1 ) { res = ( res * x ) % MOD ; } y = y >> 1 ; x = ( x * x ) % MOD ; } return res % MOD ; } static void combi ( int n , int k ) { int i , j ; for ( i = 0 ; i <= n ; i ++ ) { for ( j = 0 ; j <= Math . min ( i , k ) ; j ++ ) { if ( j == 0 || j == i ) C [ i ] [ j ] = 1 ; else C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] % MOD + C [ i - 1 ] [ j ] % MOD ) % MOD ; } } } static long product ( long a [ ] , int n , int k ) { long ans = 1 ; Arrays . sort ( a ) ; long powa = C [ n - 1 ] [ k - 1 ] ; for ( int i = 0 ; i < n ; i ++ ) { long powla = C [ i ] [ k - 1 ] ; long powfa = C [ n - i - 1 ] [ k - 1 ] ; long powe = ( ( powa % MOD ) - ( powla + powfa ) % MOD + MOD ) % MOD ; long mul = power ( a [ i ] , powe ) % MOD ; ans = ( ( ans % MOD ) * ( mul % MOD ) ) % MOD ; } return ans % MOD ; } public static void main ( String [ ] args ) { combi ( 100 , 100 ) ; long arr [ ] = { 1 , 2 , 3 , 4 } ; int n = arr . length ; int k = 3 ; long ans = product ( arr , n , k ) ; System . out . println ( ans ) ; } }
MOD = 1000000007 NEW_LINE max = 101 NEW_LINE C = [ [ 0 for i in range ( max ) ] for j in range ( max ) ] NEW_LINE def power ( x , y ) : NEW_LINE INDENT res = 1 NEW_LINE x = x % MOD NEW_LINE while ( y > 0 ) : NEW_LINE INDENT if ( y & 1 ) : NEW_LINE INDENT res = ( res * x ) % MOD NEW_LINE DEDENT y = y >> 1 NEW_LINE x = ( x * x ) % MOD NEW_LINE DEDENT return res % MOD NEW_LINE DEDENT def combi ( n , k ) : NEW_LINE INDENT for i in range ( n + 1 ) : NEW_LINE INDENT for j in range ( min ( i , k ) + 1 ) : NEW_LINE INDENT if ( j == 0 or j == i ) : NEW_LINE INDENT C [ i ] [ j ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT C [ i ] [ j ] = ( C [ i - 1 ] [ j - 1 ] % MOD + C [ i - 1 ] [ j ] % MOD ) % MOD NEW_LINE DEDENT DEDENT DEDENT DEDENT def product ( a , n , k ) : NEW_LINE INDENT ans = 1 NEW_LINE a . sort ( reverse = False ) NEW_LINE powa = C [ n - 1 ] [ k - 1 ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT powla = C [ i ] [ k - 1 ] NEW_LINE powfa = C [ n - i - 1 ] [ k - 1 ] NEW_LINE powe = ( ( powa % MOD ) - ( powla + powfa ) % MOD + MOD ) % MOD NEW_LINE mul = power ( a [ i ] , powe ) % MOD NEW_LINE ans = ( ( ans % MOD ) * ( mul % MOD ) ) % MOD NEW_LINE DEDENT return ans % MOD NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT combi ( 100 , 100 ) NEW_LINE arr = [ 1 , 2 , 3 , 4 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE ans = product ( arr , n , k ) NEW_LINE print ( ans ) NEW_LINE DEDENT
T39835
import java . util . Arrays ; class GFG { static int search ( int arr [ ] , int x , int n ) { for ( int i = 0 ; i < n ; i ++ ) if ( arr [ i ] == x ) return i ; return - 1 ; } static void printPostOrder ( int in1 [ ] , int pre [ ] , int n ) { int root = search ( in1 , pre [ 0 ] , n ) ; if ( root != 0 ) printPostOrder ( in1 , Arrays . copyOfRange ( pre , 1 , n ) , root ) ; if ( root != n - 1 ) printPostOrder ( Arrays . copyOfRange ( in1 , root + 1 , n ) , Arrays . copyOfRange ( pre , 1 + root , n ) , n - root - 1 ) ; System . out . print ( pre [ 0 ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int in1 [ ] = { 4 , 2 , 5 , 1 , 3 , 6 } ; int pre [ ] = { 1 , 2 , 4 , 5 , 3 , 6 } ; int n = in1 . length ; System . out . println ( " Postorder ▁ traversal ▁ " ) ; printPostOrder ( in1 , pre , n ) ; } }
def printpostorder ( inorder , preorder , n ) : NEW_LINE INDENT if preorder [ 0 ] in inorder : NEW_LINE INDENT root = inorder . index ( preorder [ 0 ] ) NEW_LINE DEDENT if root != 0 : NEW_LINE INDENT printpostorder ( inorder [ : root ] , preorder [ 1 : root + 1 ] , len ( inorder [ : root ] ) ) NEW_LINE DEDENT if root != n - 1 : NEW_LINE INDENT printpostorder ( inorder [ root + 1 : ] , preorder [ root + 1 : ] , len ( inorder [ root + 1 : ] ) ) NEW_LINE DEDENT print preorder [ 0 ] , NEW_LINE DEDENT inorder = [ 4 , 2 , 5 , 1 , 3 , 6 ] ; NEW_LINE preorder = [ 1 , 2 , 4 , 5 , 3 , 6 ] ; NEW_LINE n = len ( inorder ) NEW_LINE print " Postorder ▁ traversal ▁ " NEW_LINE printpostorder ( inorder , preorder , n ) NEW_LINE
T39836
import java . io . * ; class GFG { static int getMaxNum ( int a , int b , int c ) { if ( b % c == 0 ) return b ; int x = ( ( b / c ) * c ) ; if ( x >= a && x <= b ) return x ; else return - 1 ; } public static void main ( String [ ] args ) { int a = 2 , b = 10 , c = 3 ; System . out . println ( getMaxNum ( a , b , c ) ) ; } }
def getMaxNum ( a , b , c ) : NEW_LINE INDENT if ( b % c == 0 ) : NEW_LINE INDENT return b NEW_LINE DEDENT x = ( ( b // c ) * c ) NEW_LINE if ( x >= a and x <= b ) : NEW_LINE INDENT return x NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT a , b , c = 2 , 10 , 3 NEW_LINE print ( getMaxNum ( a , b , c ) ) NEW_LINE
T39837
import java . lang . * ; class GFG { static boolean isDivBy9 ( int n ) { if ( n == 0 || n == 9 ) return true ; if ( n < 9 ) return false ; return isDivBy9 ( ( int ) ( n >> 3 ) - ( int ) ( n & 7 ) ) ; } public static void main ( String arg [ ] ) { for ( int i = 0 ; i < 100 ; i ++ ) if ( isDivBy9 ( i ) ) System . out . print ( i + " ▁ " ) ; } }
def isDivBy9 ( n ) : NEW_LINE INDENT if ( n == 0 or n == 9 ) : NEW_LINE INDENT return True NEW_LINE DEDENT if ( n < 9 ) : NEW_LINE INDENT return False NEW_LINE DEDENT return isDivBy9 ( ( int ) ( n >> 3 ) - ( int ) ( n & 7 ) ) NEW_LINE DEDENT for i in range ( 100 ) : NEW_LINE INDENT if ( isDivBy9 ( i ) ) : NEW_LINE INDENT print ( i , " ▁ " , end = " " ) NEW_LINE DEDENT DEDENT
T39838
import java . util . * ; class GFG { static Vector missing_elements ( Vector vec ) { Vector mis = new Vector ( ) ; for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { int temp = Math . abs ( ( int ) vec . get ( i ) ) - 1 ; if ( ( int ) vec . get ( temp ) > 0 ) vec . set ( temp , - ( int ) vec . get ( temp ) ) ; else vec . set ( temp , vec . get ( temp ) ) ; } for ( int i = 0 ; i < vec . size ( ) ; i ++ ) { if ( ( int ) vec . get ( i ) > 0 ) mis . add ( i + 1 ) ; } return mis ; } public static void main ( String args [ ] ) { Vector vec = new Vector ( ) ; vec . add ( 3 ) ; vec . add ( 3 ) ; vec . add ( 3 ) ; vec . add ( 5 ) ; vec . add ( 1 ) ; Vector miss_ele = missing_elements ( vec ) ; for ( int i = 0 ; i < miss_ele . size ( ) ; i ++ ) System . out . print ( miss_ele . get ( i ) + " ▁ " ) ; } }
def missing_elements ( vec ) : NEW_LINE INDENT mis = [ ] NEW_LINE for i in range ( len ( vec ) ) : NEW_LINE INDENT temp = abs ( vec [ i ] ) - 1 NEW_LINE if vec [ temp ] > 0 : NEW_LINE INDENT vec [ temp ] = - vec [ temp ] NEW_LINE DEDENT DEDENT for i in range ( len ( vec ) ) : NEW_LINE INDENT if ( vec [ i ] > 0 ) : NEW_LINE INDENT mis . append ( i + 1 ) NEW_LINE DEDENT DEDENT return mis NEW_LINE DEDENT vec = [ 3 , 3 , 3 , 5 , 1 ] NEW_LINE miss_ele = missing_elements ( vec ) NEW_LINE for i in range ( len ( miss_ele ) ) : NEW_LINE INDENT print ( miss_ele [ i ] , end = " ▁ " ) NEW_LINE DEDENT
T39839
class GFG { static void print ( int arr [ ] , int n ) { for ( int i = 0 ; i < n ; i ++ ) { System . out . print ( arr [ i ] + " ▁ " ) ; } System . out . println ( ) ; } static void swapMax ( int [ ] arr , int target_position , int current_position ) { int aux = 0 ; for ( int i = current_position ; i > target_position ; i -- ) { aux = arr [ i - 1 ] ; arr [ i - 1 ] = arr [ i ] ; arr [ i ] = aux ; } } static void maximizeArray ( int [ ] arr , int length , int swaps ) { if ( swaps == 0 ) return ; for ( int i = 0 ; i < length ; i ++ ) { int max_index = 0 , max = Integer . MIN_VALUE ; int limit = ( swaps + i ) > length ? length : swaps + i ; for ( int j = i ; j <= limit ; j ++ ) { if ( arr [ j ] > max ) { max = arr [ j ] ; max_index = j ; } } swaps -= ( max_index - i ) ; swapMax ( arr , i , max_index ) ; if ( swaps == 0 ) break ; } } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 9 , 8 , 1 , 4 , 9 , 9 , 9 } ; int length = arr . length ; int swaps = 4 ; maximizeArray ( arr , length , swaps ) ; print ( arr , length ) ; } }
import sys NEW_LINE def print_ele ( arr , n ) : NEW_LINE INDENT for i in range ( n ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT print ( ) ; NEW_LINE DEDENT def swapMax ( arr , target_position , current_position ) : NEW_LINE INDENT aux = 0 ; NEW_LINE for i in range ( current_position , target_position , - 1 ) : NEW_LINE INDENT aux = arr [ i - 1 ] ; NEW_LINE arr [ i - 1 ] = arr [ i ] ; NEW_LINE arr [ i ] = aux ; NEW_LINE DEDENT DEDENT def maximizeArray ( arr , length , swaps ) : NEW_LINE INDENT if ( swaps == 0 ) : NEW_LINE INDENT return ; NEW_LINE DEDENT for i in range ( length ) : NEW_LINE INDENT max_index = 0 ; max = - ( sys . maxsize - 1 ) ; NEW_LINE if ( swaps + i ) > length : NEW_LINE INDENT limit = length NEW_LINE DEDENT else : NEW_LINE INDENT limit = swaps + i NEW_LINE DEDENT for j in range ( i , limit + 1 ) : NEW_LINE INDENT if ( arr [ j ] > max ) : NEW_LINE INDENT max = arr [ j ] ; NEW_LINE max_index = j ; NEW_LINE DEDENT DEDENT swaps -= ( max_index - i ) ; NEW_LINE swapMax ( arr , i , max_index ) ; NEW_LINE if ( swaps == 0 ) : NEW_LINE INDENT break ; NEW_LINE DEDENT DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 1 , 2 , 9 , 8 , 1 , 4 , 9 , 9 , 9 ] ; NEW_LINE length = len ( arr ) ; NEW_LINE swaps = 4 ; NEW_LINE maximizeArray ( arr , length , swaps ) ; NEW_LINE print_ele ( arr , length ) ; NEW_LINE DEDENT
T39840
class GFG { static String smallestString ( int N , int [ ] A ) { char ch = ' a ' ; String S = " " ; if ( N < 1 || A [ 0 ] != 1 ) { S = " - 1" ; return S ; } S += ch ; ch ++ ; for ( int i = 1 ; i < N ; i ++ ) { int diff = A [ i ] - A [ i - 1 ] ; if ( diff > 1 || diff < 0 || A [ i ] > 26 ) { S = " - 1" ; return S ; } else if ( diff == 0 ) S += ' a ' ; else { S += ch ; ch ++ ; } } return S ; } public static void main ( String args [ ] ) { int [ ] arr = { 1 , 1 , 2 , 3 , 3 } ; int n = arr . length ; System . out . println ( smallestString ( n , arr ) ) ; } }
def smallestString ( N , A ) : NEW_LINE INDENT ch = ' a ' NEW_LINE S = " " NEW_LINE if ( N < 1 or A [ 0 ] != 1 ) : NEW_LINE INDENT S = " - 1" NEW_LINE return S NEW_LINE DEDENT S += str ( ch ) NEW_LINE ch = chr ( ord ( ch ) + 1 ) NEW_LINE for i in range ( 1 , N ) : NEW_LINE INDENT diff = A [ i ] - A [ i - 1 ] NEW_LINE if ( diff > 1 or diff < 0 or A [ i ] > 26 ) : NEW_LINE INDENT S = " - 1" NEW_LINE return S NEW_LINE DEDENT elif ( diff == 0 ) : NEW_LINE INDENT S += ' a ' NEW_LINE DEDENT else : NEW_LINE INDENT S += ch NEW_LINE ch = chr ( ord ( ch ) + 1 ) NEW_LINE DEDENT DEDENT return S NEW_LINE DEDENT arr = [ 1 , 1 , 2 , 3 , 3 ] NEW_LINE n = len ( arr ) NEW_LINE print ( smallestString ( n , arr ) ) NEW_LINE
T39841
import java . io . * ; class GFG { static int countSolutions ( int a ) { int count = 0 ; for ( int i = 0 ; i <= a ; i ++ ) { if ( a == ( i + ( a ^ i ) ) ) count ++ ; } return count ; } public static void main ( String [ ] args ) { int a = 3 ; System . out . println ( countSolutions ( a ) ) ; } }
def countSolutions ( a ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( a + 1 ) : NEW_LINE INDENT if ( a == ( i + ( a ^ i ) ) ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 NEW_LINE print ( countSolutions ( a ) ) NEW_LINE DEDENT
T39842
class GfG { static void printElements ( int arr [ ] , int n ) { for ( int i = 1 ; i < n - 1 ; i ++ ) { if ( arr [ i ] > arr [ i - 1 ] && arr [ i ] > arr [ i + 1 ] ) System . out . print ( arr [ i ] + " ▁ " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 } ; int n = arr . length ; printElements ( arr , n ) ; } }
def printElements ( arr , n ) : NEW_LINE INDENT for i in range ( 1 , n - 1 , 1 ) : NEW_LINE INDENT if ( arr [ i ] > arr [ i - 1 ] and arr [ i ] > arr [ i + 1 ] ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 1 , 5 , 4 , 9 , 8 , 7 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE printElements ( arr , n ) NEW_LINE DEDENT
T39843
import java . io . * ; public class GFG { static String findString ( int n , int k ) { String res = " " ; for ( int i = 0 ; i < k ; i ++ ) res = res + ( char ) ( ' a ' + i ) ; int count = 0 ; for ( int i = 0 ; i < n - k ; i ++ ) { res = res + ( char ) ( ' a ' + count ) ; count ++ ; if ( count == k ) count = 0 ; } return res ; } static public void main ( String [ ] args ) { int n = 5 , k = 2 ; System . out . println ( findString ( n , k ) ) ; } }
def findString ( n , k ) : NEW_LINE INDENT res = " " NEW_LINE for i in range ( k ) : NEW_LINE INDENT res = res + chr ( ord ( ' a ' ) + i ) NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( n - k ) : NEW_LINE INDENT res = res + chr ( ord ( ' a ' ) + count ) NEW_LINE count += 1 NEW_LINE if ( count == k ) : NEW_LINE INDENT count = 0 ; NEW_LINE DEDENT DEDENT return res NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 NEW_LINE k = 2 NEW_LINE print ( findString ( n , k ) ) NEW_LINE DEDENT
T39844
class GFG { static int count_pairs ( int a [ ] , int b [ ] , int n , int m ) { int odd1 = 0 , even1 = 0 ; int odd2 = 0 , even2 = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % 2 == 1 ) { odd1 ++ ; } else { even1 ++ ; } } for ( int i = 0 ; i < m ; i ++ ) { if ( b [ i ] % 2 == 1 ) { odd2 ++ ; } else { even2 ++ ; } } int pairs = Math . min ( odd1 , even2 ) + Math . min ( odd2 , even1 ) ; return pairs ; } public static void main ( String [ ] args ) { int a [ ] = { 9 , 14 , 6 , 2 , 11 } ; int b [ ] = { 8 , 4 , 7 , 20 } ; int n = a . length ; int m = b . length ; System . out . println ( count_pairs ( a , b , n , m ) ) ; } }
def count_pairs ( a , b , n , m ) : NEW_LINE INDENT odd1 = 0 NEW_LINE even1 = 0 NEW_LINE odd2 = 0 NEW_LINE even2 = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % 2 ) : NEW_LINE INDENT odd1 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even1 += 1 NEW_LINE DEDENT DEDENT for i in range ( m ) : NEW_LINE INDENT if ( b [ i ] % 2 ) : NEW_LINE INDENT odd2 += 1 NEW_LINE DEDENT else : NEW_LINE INDENT even2 += 1 NEW_LINE DEDENT DEDENT pairs = ( min ( odd1 , even2 ) + min ( odd2 , even1 ) ) NEW_LINE return pairs NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT a = [ 9 , 14 , 6 , 2 , 11 ] NEW_LINE b = [ 8 , 4 , 7 , 20 ] NEW_LINE n = len ( a ) NEW_LINE m = len ( b ) NEW_LINE print ( count_pairs ( a , b , n , m ) ) NEW_LINE DEDENT
T39845
import java . util . Arrays ; class GFG { public static void countFreq ( int arr [ ] , int n ) { boolean visited [ ] = new boolean [ n ] ; Arrays . fill ( visited , false ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( visited [ i ] == true ) continue ; int count = 1 ; for ( int j = i + 1 ; j < n ; j ++ ) { if ( arr [ i ] == arr [ j ] ) { visited [ j ] = true ; count ++ ; } } System . out . println ( arr [ i ] + " ▁ " + count ) ; } } public static void main ( String [ ] args ) { int arr [ ] = new int [ ] { 10 , 20 , 20 , 10 , 10 , 20 , 5 , 20 } ; int n = arr . length ; countFreq ( arr , n ) ; } }
def countFreq ( arr , n ) : NEW_LINE INDENT visited = [ False for i in range ( n ) ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( visited [ i ] == True ) : NEW_LINE INDENT continue NEW_LINE DEDENT count = 1 NEW_LINE for j in range ( i + 1 , n , 1 ) : NEW_LINE INDENT if ( arr [ i ] == arr [ j ] ) : NEW_LINE INDENT visited [ j ] = True NEW_LINE count += 1 NEW_LINE DEDENT DEDENT print ( arr [ i ] , count ) NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 10 , 20 , 20 , 10 , 10 , 20 , 5 , 20 ] NEW_LINE n = len ( arr ) NEW_LINE countFreq ( arr , n ) NEW_LINE DEDENT
T39846
import java . util . * ; class GFG { static int reverseNum ( int n ) { int rem , rev = 0 ; while ( n > 0 ) { rem = n % 10 ; rev = rev * 10 + rem ; n /= 10 ; } return rev ; } static boolean isPalindrom ( int num ) { return num == reverseNum ( num ) ; } static int nthPalindrome ( int n , int k ) { int num = ( int ) Math . pow ( 10 , k - 1 ) ; while ( true ) { if ( isPalindrom ( num ) ) -- n ; if ( n == 0 ) break ; ++ num ; } return num ; } public static void main ( String [ ] args ) { int n = 6 , k = 5 ; System . out . println ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " + nthPalindrome ( n , k ) ) ; n = 10 ; k = 6 ; System . out . println ( n + " th ▁ palindrome ▁ of ▁ " + k + " ▁ digit ▁ = ▁ " + nthPalindrome ( n , k ) ) ; } }
import math ; NEW_LINE def reverseNum ( n ) : NEW_LINE INDENT rev = 0 ; NEW_LINE while ( n ) : NEW_LINE INDENT rem = n % 10 ; NEW_LINE rev = ( rev * 10 ) + rem ; NEW_LINE n = int ( n / 10 ) ; NEW_LINE DEDENT return rev ; NEW_LINE DEDENT def isPalindrom ( num ) : NEW_LINE INDENT return num == reverseNum ( num ) ; NEW_LINE DEDENT def nthPalindrome ( n , k ) : NEW_LINE INDENT num = math . pow ( 10 , k - 1 ) ; NEW_LINE while ( True ) : NEW_LINE INDENT if ( isPalindrom ( num ) ) : NEW_LINE INDENT n -= 1 ; NEW_LINE DEDENT if ( not n ) : NEW_LINE INDENT break ; NEW_LINE DEDENT num += 1 ; NEW_LINE DEDENT return int ( num ) ; NEW_LINE DEDENT n = 6 ; NEW_LINE k = 5 ; NEW_LINE print ( n , " th ▁ palindrome ▁ of " , k , " digit ▁ = " , nthPalindrome ( n , k ) ) ; NEW_LINE n = 10 ; NEW_LINE k = 6 ; NEW_LINE print ( n , " th ▁ palindrome ▁ of " , k , " digit ▁ = " , nthPalindrome ( n , k ) ) ; NEW_LINE
T39847
class GFG { static int count9s ( char number [ ] ) { int n = number . length ; int d [ ] = new int [ 9 ] ; d [ 0 ] = 1 ; int result = 0 ; int mod_sum = 0 , continuous_zero = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( ( number [ i ] - '0' ) == 0 ) { continuous_zero ++ ; } else { continuous_zero = 0 ; } mod_sum += ( number [ i ] - '0' ) ; mod_sum %= 9 ; result += d [ mod_sum ] ; d [ mod_sum ] ++ ; result -= continuous_zero ; } return result ; } public static void main ( String [ ] args ) { System . out . println ( count9s ( "01809" . toCharArray ( ) ) ) ; System . out . println ( count9s ( "1809" . toCharArray ( ) ) ) ; System . out . println ( count9s ( "4189" . toCharArray ( ) ) ) ; } }
def count9s ( number ) : NEW_LINE INDENT n = len ( number ) NEW_LINE d = [ 0 for i in range ( 9 ) ] NEW_LINE d [ 0 ] = 1 NEW_LINE result = 0 NEW_LINE mod_sum = 0 NEW_LINE continuous_zero = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( ord ( number [ i ] ) - ord ( '0' ) == 0 ) : NEW_LINE INDENT continuous_zero += 1 NEW_LINE DEDENT else : NEW_LINE INDENT continuous_zero = 0 NEW_LINE DEDENT mod_sum += ord ( number [ i ] ) - ord ( '0' ) NEW_LINE mod_sum %= 9 NEW_LINE result += d [ mod_sum ] NEW_LINE d [ mod_sum ] += 1 NEW_LINE result -= continuous_zero NEW_LINE DEDENT return result NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT print ( count9s ( "01809" ) ) NEW_LINE print ( count9s ( "1809" ) ) NEW_LINE print ( count9s ( "4189" ) ) NEW_LINE DEDENT
T39848
import java . * ; public class GFG { static int countPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i - 1 ; j >= 0 ; j -- ) if ( arr [ i ] * arr [ j ] > arr [ i ] + arr [ j ] ) ans ++ ; return ans ; } public static void main ( String args [ ] ) { int arr [ ] = { 3 , 4 , 5 } ; int n = arr . length ; System . out . println ( countPairs ( arr , n ) ) ; } }
def countPairs ( arr , n ) : NEW_LINE INDENT ans = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT j = i - 1 NEW_LINE while ( j >= 0 ) : NEW_LINE INDENT if ( arr [ i ] * arr [ j ] > arr [ i ] + arr [ j ] ) : NEW_LINE INDENT ans = ans + 1 NEW_LINE DEDENT j = j - 1 NEW_LINE DEDENT DEDENT return ans NEW_LINE DEDENT arr = [ 3 , 4 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE k = countPairs ( arr , n ) NEW_LINE print ( k ) NEW_LINE
T39849
class GFG { static int negProdSubArr ( int arr [ ] , int n ) { int positive = 1 , negative = 0 ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) arr [ i ] = 1 ; else arr [ i ] = - 1 ; if ( i > 0 ) arr [ i ] *= arr [ i - 1 ] ; if ( arr [ i ] == 1 ) positive ++ ; else negative ++ ; } return ( positive * negative ) ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , - 4 , - 3 , 2 , - 5 } ; int n = arr . length ; System . out . println ( negProdSubArr ( arr , n ) ) ; } }
def negProdSubArr ( arr , n ) : NEW_LINE INDENT positive = 1 NEW_LINE negative = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT arr [ i ] = 1 NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i ] = - 1 NEW_LINE DEDENT if ( i > 0 ) : NEW_LINE INDENT arr [ i ] *= arr [ i - 1 ] NEW_LINE DEDENT if ( arr [ i ] == 1 ) : NEW_LINE INDENT positive += 1 NEW_LINE DEDENT else : NEW_LINE INDENT negative += 1 NEW_LINE DEDENT DEDENT return ( positive * negative ) NEW_LINE DEDENT arr = [ 5 , - 4 , - 3 , 2 , - 5 ] NEW_LINE n = len ( arr ) NEW_LINE print ( negProdSubArr ( arr , n ) ) NEW_LINE
T39850
import java . util . * ; class GFG { static void permuteRec ( String str , int n , int index , String curr ) { if ( index == n ) { return ; } System . out . println ( curr ) ; for ( int i = index + 1 ; i < n ; i ++ ) { curr += str . charAt ( i ) ; permuteRec ( str , n , i , curr ) ; curr = curr . substring ( 0 , curr . length ( ) - 1 ) ; } return ; } static void powerSet ( String str ) { char [ ] arr = str . toCharArray ( ) ; Arrays . sort ( arr ) ; permuteRec ( new String ( arr ) , str . length ( ) , - 1 , " " ) ; } public static void main ( String [ ] args ) { String str = " cab " ; powerSet ( str ) ; } }
def permuteRec ( string , n , index = - 1 , curr = " " ) : NEW_LINE INDENT if index == n : NEW_LINE INDENT return NEW_LINE DEDENT if len ( curr ) > 0 : NEW_LINE INDENT print ( curr ) NEW_LINE DEDENT for i in range ( index + 1 , n ) : NEW_LINE INDENT curr += string [ i ] NEW_LINE permuteRec ( string , n , i , curr ) NEW_LINE curr = curr [ : len ( curr ) - 1 ] NEW_LINE DEDENT DEDENT def powerSet ( string ) : NEW_LINE INDENT string = ' ' . join ( sorted ( string ) ) NEW_LINE permuteRec ( string , len ( string ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT string = " cab " NEW_LINE powerSet ( string ) NEW_LINE DEDENT
T39851
class GFG { public static int countPairs ( int n ) { if ( n == 2 ) return 4 ; int num = ( ( n / 2 ) + 1 ) ; int max = n % num ; int count = n - max ; return count ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( countPairs ( n ) ) ; } }
def countPairs ( n ) : NEW_LINE INDENT if ( n == 2 ) : NEW_LINE INDENT return 4 NEW_LINE DEDENT num = ( ( n // 2 ) + 1 ) ; NEW_LINE max = n % num ; NEW_LINE count = n - max ; NEW_LINE return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE DEDENT print ( countPairs ( n ) ) ; NEW_LINE
T39852
import java . util . * ; class Solution { static class Node { int data ; Node link ; } static class Queue { Node front , rear ; } static void enQueue ( Queue q , int value ) { Node temp = new Node ( ) ; temp . data = value ; if ( q . front == null ) q . front = temp ; else q . rear . link = temp ; q . rear = temp ; q . rear . link = q . front ; } static int deQueue ( Queue q ) { if ( q . front == null ) { System . out . printf ( " Queue ▁ is ▁ empty " ) ; return Integer . MIN_VALUE ; } int value ; if ( q . front == q . rear ) { value = q . front . data ; q . front = null ; q . rear = null ; } else { Node temp = q . front ; value = temp . data ; q . front = q . front . link ; q . rear . link = q . front ; } return value ; } static void displayQueue ( Queue q ) { Node temp = q . front ; System . out . printf ( " \n Elements ▁ in ▁ Circular ▁ Queue ▁ are : ▁ " ) ; while ( temp . link != q . front ) { System . out . printf ( " % d ▁ " , temp . data ) ; temp = temp . link ; } System . out . printf ( " % d " , temp . data ) ; } public static void main ( String args [ ] ) { Queue q = new Queue ( ) ; q . front = q . rear = null ; enQueue ( q , 14 ) ; enQueue ( q , 22 ) ; enQueue ( q , 6 ) ; displayQueue ( q ) ; System . out . printf ( " \n Deleted ▁ value ▁ = ▁ % d " , deQueue ( q ) ) ; System . out . printf ( " \n Deleted ▁ value ▁ = ▁ % d " , deQueue ( q ) ) ; displayQueue ( q ) ; enQueue ( q , 9 ) ; enQueue ( q , 20 ) ; displayQueue ( q ) ; } }
class Node : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT self . data = None NEW_LINE self . link = None NEW_LINE DEDENT DEDENT class Queue : NEW_LINE INDENT def __init__ ( self ) : NEW_LINE INDENT front = None NEW_LINE rear = None NEW_LINE DEDENT DEDENT def enQueue ( q , value ) : NEW_LINE INDENT temp = Node ( ) NEW_LINE temp . data = value NEW_LINE if ( q . front == None ) : NEW_LINE INDENT q . front = temp NEW_LINE DEDENT else : NEW_LINE INDENT q . rear . link = temp NEW_LINE DEDENT q . rear = temp NEW_LINE q . rear . link = q . front NEW_LINE DEDENT def deQueue ( q ) : NEW_LINE INDENT if ( q . front == None ) : NEW_LINE INDENT print ( " Queue ▁ is ▁ empty " ) NEW_LINE return - 999999999999 NEW_LINE DEDENT value = None NEW_LINE if ( q . front == q . rear ) : NEW_LINE INDENT value = q . front . data NEW_LINE q . front = None NEW_LINE q . rear = None NEW_LINE DEDENT else : NEW_LINE INDENT temp = q . front NEW_LINE value = temp . data NEW_LINE q . front = q . front . link NEW_LINE q . rear . link = q . front NEW_LINE DEDENT return value NEW_LINE DEDENT def displayQueue ( q ) : NEW_LINE INDENT temp = q . front NEW_LINE print ( " Elements ▁ in ▁ Circular ▁ Queue ▁ are : ▁ " , end = " ▁ " ) NEW_LINE while ( temp . link != q . front ) : NEW_LINE INDENT print ( temp . data , end = " ▁ " ) NEW_LINE temp = temp . link NEW_LINE DEDENT print ( temp . data ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT q = Queue ( ) NEW_LINE q . front = q . rear = None NEW_LINE enQueue ( q , 14 ) NEW_LINE enQueue ( q , 22 ) NEW_LINE enQueue ( q , 6 ) NEW_LINE displayQueue ( q ) NEW_LINE print ( " Deleted ▁ value ▁ = ▁ " , deQueue ( q ) ) NEW_LINE print ( " Deleted ▁ value ▁ = ▁ " , deQueue ( q ) ) NEW_LINE displayQueue ( q ) NEW_LINE enQueue ( q , 9 ) NEW_LINE enQueue ( q , 20 ) NEW_LINE displayQueue ( q ) NEW_LINE DEDENT
T39853
import java . lang . * ; public class Main { static long findSmallestNonZeroY ( long A_num ) { String A_binary = Long . toBinaryString ( A_num ) ; long B = 1 ; int len = A_binary . length ( ) ; int no_ones = Long . bitCount ( A_num ) ; if ( len == no_ones ) { return A_num + 1 ; } for ( int i = 0 ; i < len ; i ++ ) { char ch = A_binary . charAt ( len - i - 1 ) ; if ( ch == '0' ) { B = ( long ) Math . pow ( 2.0 , ( double ) i ) ; break ; } } return B ; } public static void main ( String [ ] args ) { long X = findSmallestNonZeroY ( 10 ) ; System . out . println ( X ) ; } }
def findSmallestNonZeroY ( A_num ) : NEW_LINE INDENT A_binary = bin ( A_num ) NEW_LINE B = 1 NEW_LINE length = len ( A_binary ) ; NEW_LINE no_ones = ( A_binary ) . count ( '1' ) ; NEW_LINE if length == no_ones : NEW_LINE INDENT return A_num + 1 ; NEW_LINE DEDENT for i in range ( length ) : NEW_LINE INDENT ch = A_binary [ length - i - 1 ] ; NEW_LINE if ( ch == '0' ) : NEW_LINE INDENT B = pow ( 2.0 , i ) ; NEW_LINE break ; NEW_LINE DEDENT DEDENT return B ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT X = findSmallestNonZeroY ( 10 ) ; NEW_LINE print ( X ) NEW_LINE DEDENT
T39854
import java . util . * ; class GFG { static int x = 26 ; static int mod = 3001 ; static void CntSubstr ( char [ ] s , int l ) { int hash = 0 ; for ( int i = 0 ; i < l ; i ++ ) { hash = ( hash * x + ( s [ i ] - 97 ) ) % mod ; } int pow_l = 1 ; for ( int i = 0 ; i < l - 1 ; i ++ ) { pow_l = ( pow_l * x ) % mod ; } HashSet < Integer > result = new HashSet < Integer > ( ) ; result . add ( hash ) ; for ( int i = l ; i < s . length ; i ++ ) { hash = ( ( hash - pow_l * ( s [ i - l ] - 97 ) + 2 * mod ) * x + ( s [ i ] - 97 ) ) % mod ; result . add ( hash ) ; } System . out . println ( result . size ( ) ) ; } public static void main ( String [ ] args ) { String s = " abcba " ; int l = 2 ; CntSubstr ( s . toCharArray ( ) , l ) ; } }
x = 26 NEW_LINE mod = 3001 NEW_LINE def CntSubstr ( s , l ) : NEW_LINE INDENT hash = 0 ; NEW_LINE for i in range ( l ) : NEW_LINE INDENT hash = ( hash * x + ( ord ( s [ i ] ) - 97 ) ) % mod ; NEW_LINE DEDENT pow_l = 1 ; NEW_LINE for i in range ( l - 1 ) : NEW_LINE INDENT pow_l = ( pow_l * x ) % mod ; NEW_LINE DEDENT result = set ( ) ; NEW_LINE result . add ( hash ) ; NEW_LINE for i in range ( l , len ( s ) ) : NEW_LINE INDENT hash = ( ( hash - pow_l * ( ord ( s [ i - l ] ) - 97 ) + 2 * mod ) * x + ( ord ( s [ i ] ) - 97 ) ) % mod ; NEW_LINE result . add ( hash ) ; NEW_LINE DEDENT print ( len ( result ) ) ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = " abcba " ; NEW_LINE l = 2 ; NEW_LINE CntSubstr ( s , l ) ; NEW_LINE DEDENT
T39855
import java . util . Arrays ; class GFG { static int sumArr ( int arr [ ] , int n ) { int sum = 0 ; for ( int i = 0 ; i < n ; i ++ ) sum += arr [ i ] ; return sum ; } static int maxSum ( int arr [ ] , int n , int k ) { Arrays . sort ( arr ) ; int i = 0 ; while ( i < n && k > 0 && arr [ i ] < 0 ) { arr [ i ] *= - 1 ; k -- ; i ++ ; } if ( k % 2 == 1 ) { int min = 0 ; for ( i = 1 ; i < n ; i ++ ) if ( arr [ min ] > arr [ i ] ) min = i ; arr [ min ] *= - 1 ; } return sumArr ( arr , n ) ; } public static void main ( String [ ] args ) { int arr [ ] = { - 5 , 4 , 1 , 3 , 2 } ; int n = arr . length ; int k = 4 ; System . out . println ( maxSum ( arr , n , k ) ) ; } }
def sumArr ( arr , n ) : NEW_LINE INDENT sum = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT sum += arr [ i ] NEW_LINE DEDENT return sum NEW_LINE DEDENT def maxSum ( arr , n , k ) : NEW_LINE INDENT arr . sort ( reverse = False ) NEW_LINE i = 0 NEW_LINE while ( i < n and k > 0 and arr [ i ] < 0 ) : NEW_LINE INDENT arr [ i ] *= - 1 NEW_LINE k -= 1 NEW_LINE i += 1 NEW_LINE DEDENT if ( k % 2 == 1 ) : NEW_LINE INDENT min = 0 NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT if ( arr [ min ] > arr [ i ] ) : NEW_LINE INDENT min = i NEW_LINE DEDENT DEDENT arr [ min ] *= - 1 NEW_LINE DEDENT return sumArr ( arr , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ - 5 , 4 , 1 , 3 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 4 NEW_LINE print ( maxSum ( arr , n , k ) ) NEW_LINE DEDENT
T39856
import java . util . * ; import java . util . Arrays ; class GFG { static int mod = 1000000007 ; static int dp [ ] [ ] [ ] = new int [ 1005 ] [ 105 ] [ 2 ] ; static int powers [ ] = new int [ 1005 ] ; static int powersModk [ ] = new int [ 1005 ] ; static int calculate ( int pos , int rem , int z , int k , int n ) { if ( rem == 0 && z != 0 ) { if ( pos != n ) return ( powers [ n - pos - 1 ] * 9 ) % mod ; else return 1 ; } if ( pos == n ) return 0 ; if ( dp [ pos ] [ rem ] [ z ] != - 1 ) return dp [ pos ] [ rem ] [ z ] ; int count = 0 ; for ( int i = 0 ; i < 10 ; i ++ ) { if ( i == 0 ) count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , z , k , n ) ) ) % mod ; else count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , 1 , k , n ) ) ) % mod ; } return dp [ pos ] [ rem ] [ z ] = count ; } static int countNumbers ( int n , int k ) { int st = 1 ; int i ; for ( i = 0 ; i <= n ; i ++ ) { powers [ i ] = st ; st *= 10 ; st %= mod ; } st = 1 ; for ( i = 0 ; i <= n ; i ++ ) { powersModk [ i ] = st ; st *= 10 ; st %= mod ; } for ( int [ ] [ ] row : dp ) { for ( int [ ] innerRow : row ) { Arrays . fill ( innerRow , - 1 ) ; } } ; return calculate ( 0 , 0 , 0 , k , n ) ; } public static void main ( String [ ] args ) { int N = 2 ; int K = 2 ; System . out . print ( countNumbers ( N , K ) ) ; } }
mod = 1000000007 NEW_LINE dp = [ [ [ - 1 for i in range ( 2 ) ] for i in range ( 105 ) ] for i in range ( 1005 ) ] NEW_LINE powers = [ 0 ] * 1005 NEW_LINE powersModk = [ 0 ] * 1005 NEW_LINE def calculate ( pos , rem , z , k , n ) : NEW_LINE INDENT if ( rem == 0 and z ) : NEW_LINE INDENT if ( pos != n ) : NEW_LINE INDENT return ( powers [ n - pos - 1 ] * 9 ) % mod NEW_LINE DEDENT else : NEW_LINE INDENT return 1 NEW_LINE DEDENT DEDENT if ( pos == n ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( dp [ pos ] [ rem ] [ z ] != - 1 ) : NEW_LINE INDENT return dp [ pos ] [ rem ] [ z ] NEW_LINE DEDENT count = 0 NEW_LINE for i in range ( 10 ) : NEW_LINE INDENT if ( i == 0 ) : NEW_LINE INDENT count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , z , k , n ) ) ) % mod NEW_LINE DEDENT else : NEW_LINE INDENT count = ( count + ( calculate ( pos + 1 , ( rem + ( i * powersModk [ pos ] ) % k ) % k , 1 , k , n ) ) ) % mod NEW_LINE DEDENT DEDENT dp [ pos ] [ rem ] [ z ] = count NEW_LINE return count NEW_LINE DEDENT def countNumbers ( n , k ) : NEW_LINE INDENT st = 1 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT powers [ i ] = st NEW_LINE st *= 10 NEW_LINE st %= mod NEW_LINE DEDENT st = 1 NEW_LINE for i in range ( n + 1 ) : NEW_LINE INDENT powersModk [ i ] = st NEW_LINE st *= 10 NEW_LINE st %= mod NEW_LINE DEDENT return calculate ( 0 , 0 , 0 , k , n ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 2 NEW_LINE K = 2 NEW_LINE print ( countNumbers ( N , K ) ) NEW_LINE DEDENT
T39857
class GFG { static int MAX = 256 ; static boolean canMakeStr2 ( String str1 , String str2 ) { int [ ] count = new int [ MAX ] ; char [ ] str3 = str1 . toCharArray ( ) ; for ( int i = 0 ; i < str3 . length ; i ++ ) count [ str3 [ i ] ] ++ ; char [ ] str4 = str2 . toCharArray ( ) ; for ( int i = 0 ; i < str4 . length ; i ++ ) { if ( count [ str4 [ i ] ] == 0 ) return false ; count [ str4 [ i ] ] -- ; } return true ; } static public void main ( String [ ] args ) { String str1 = " geekforgeeks " ; String str2 = " for " ; if ( canMakeStr2 ( str1 , str2 ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
MAX = 256 NEW_LINE def canMakeStr2 ( s1 , s2 ) : NEW_LINE INDENT count = { s1 [ i ] : 0 for i in range ( len ( s1 ) ) } NEW_LINE for i in range ( len ( s1 ) ) : NEW_LINE INDENT count [ s1 [ i ] ] += 1 NEW_LINE DEDENT for i in range ( len ( s2 ) ) : NEW_LINE INDENT if count [ s2 [ i ] ] == 0 : NEW_LINE INDENT return False NEW_LINE DEDENT count [ s2 [ i ] ] -= 1 NEW_LINE DEDENT return True NEW_LINE DEDENT s1 = " geekforgeeks " NEW_LINE s2 = " for " NEW_LINE if canMakeStr2 ( s1 , s2 ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
T39858
class GFG { public static double maxEdges ( double N ) { double edges = 0 ; edges = Math . floor ( ( N * N ) / 4 ) ; return edges ; } public static void main ( String [ ] args ) { double N = 5 ; System . out . println ( maxEdges ( N ) ) ; } }
def maxEdges ( N ) : NEW_LINE INDENT edges = 0 ; NEW_LINE edges = ( N * N ) // 4 ; NEW_LINE return edges ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT N = 5 ; NEW_LINE print ( maxEdges ( N ) ) ; NEW_LINE DEDENT
T39859
class GFG { static class Node { int data ; Node prev , next ; } ; static Node push ( Node head_ref , int 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 makeOddNode ( Node head_ref , int A [ ] , int n ) { Node ptr = head_ref ; Node next ; int i = 0 ; while ( ptr != null ) { next = ptr . next ; if ( ptr . data % 2 == 0 ) { ptr . data = A [ i ] ; i ++ ; } ptr = next ; } return head_ref ; } static void printList ( Node head ) { while ( head != null ) { System . out . print ( head . data + " ▁ " ) ; head = head . next ; } } public static void main ( String args [ ] ) { Node head = null ; int Arr [ ] = { 3 , 5 , 23 , 17 , 1 } ; head = push ( head , 4 ) ; head = push ( head , 7 ) ; head = push ( head , 8 ) ; head = push ( head , 9 ) ; head = push ( head , 6 ) ; int n = Arr . length ; System . out . print ( " Original ▁ List : ▁ " ) ; printList ( head ) ; System . out . println ( ) ; head = makeOddNode ( head , Arr , n ) ; System . out . print ( " New ▁ odd ▁ List : ▁ " ) ; printList ( head ) ; } }
class Node : NEW_LINE INDENT def __init__ ( self , data ) : NEW_LINE INDENT self . data = data NEW_LINE self . prev = None NEW_LINE self . next = None NEW_LINE DEDENT DEDENT def push ( head_ref , new_data ) : NEW_LINE INDENT new_node = Node ( new_data ) NEW_LINE new_node . next = head_ref NEW_LINE if head_ref != None : NEW_LINE INDENT head_ref . prev = new_node NEW_LINE DEDENT head_ref = new_node NEW_LINE return head_ref NEW_LINE DEDENT def makeOddNode ( head_ref , A , n ) : NEW_LINE INDENT ptr = head_ref NEW_LINE i = 0 NEW_LINE while ptr != None : NEW_LINE INDENT next = ptr . next NEW_LINE if ptr . data % 2 == 0 : NEW_LINE INDENT ptr . data = A [ i ] NEW_LINE i += 1 NEW_LINE DEDENT ptr = next NEW_LINE DEDENT DEDENT def printList ( head ) : NEW_LINE INDENT while head != None : NEW_LINE INDENT print ( head . data , end = " ▁ " ) NEW_LINE head = head . next NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT head = None NEW_LINE Arr = [ 3 , 5 , 23 , 17 , 1 ] NEW_LINE head = push ( head , 4 ) NEW_LINE head = push ( head , 7 ) NEW_LINE head = push ( head , 8 ) NEW_LINE head = push ( head , 9 ) NEW_LINE head = push ( head , 6 ) NEW_LINE n = len ( Arr ) NEW_LINE print ( " Original ▁ List : " , end = " ▁ " ) NEW_LINE printList ( head ) NEW_LINE print ( ) NEW_LINE makeOddNode ( head , Arr , n ) NEW_LINE print ( " New ▁ odd ▁ List : " , end = " ▁ " ) NEW_LINE printList ( head ) NEW_LINE DEDENT
T39860
import java . io . * ; import java . util . * ; class GFG { public static int MAXN = 1000001 ; public static int [ ] spf = new int [ MAXN ] ; static void sieve ( ) { for ( int i = 1 ; i < MAXN ; i ++ ) spf [ i ] = i ; for ( int i = 4 ; i < MAXN ; i += 2 ) spf [ i ] = 2 ; for ( int i = 3 ; i * i < MAXN ; i ++ ) { if ( spf [ i ] == i ) { for ( int j = i * i ; j < MAXN ; j += i ) if ( spf [ j ] == j ) spf [ j ] = i ; } } } static int countSubArray ( int arr [ ] , int n ) { int [ ] ind = new int [ MAXN ] ; Arrays . fill ( ind , - 1 ) ; int count = 0 ; int last_ind = 0 ; for ( int i = 0 ; i < n ; ++ i ) { while ( arr [ i ] > 1 ) { int div = spf [ arr [ i ] ] ; last_ind = Math . max ( last_ind , ind [ div ] ) ; ind [ div ] = i + 1 ; arr [ i ] /= div ; } count += i - last_ind + 1 ; } return count ; } public static void main ( String [ ] args ) { sieve ( ) ; int arr [ ] = { 2 , 3 , 9 } ; int n = arr . length ; System . out . println ( countSubArray ( arr , n ) ) ; int arr1 [ ] = { 2 , 3 , 5 , 15 , 7 , 2 } ; int n1 = arr1 . length ; System . out . println ( countSubArray ( arr1 , n1 ) ) ; } }
from math import sqrt NEW_LINE MAXN = 1000001 NEW_LINE spf = [ 0 for i in range ( MAXN ) ] NEW_LINE def sieve ( ) : NEW_LINE INDENT for i in range ( 1 , MAXN , 1 ) : NEW_LINE INDENT spf [ i ] = i NEW_LINE DEDENT for i in range ( 4 , MAXN , 2 ) : NEW_LINE INDENT spf [ i ] = 2 NEW_LINE DEDENT k = int ( sqrt ( MAXN ) ) NEW_LINE for i in range ( 3 , k , 1 ) : NEW_LINE INDENT if ( spf [ i ] == i ) : NEW_LINE INDENT for j in range ( i * i , MAXN , i ) : NEW_LINE INDENT if ( spf [ j ] == j ) : NEW_LINE INDENT spf [ j ] = i NEW_LINE DEDENT DEDENT DEDENT DEDENT DEDENT def countSubArray ( arr , n ) : NEW_LINE INDENT ind = [ - 1 for i in range ( MAXN ) ] NEW_LINE count = 0 NEW_LINE last_ind = 0 NEW_LINE for i in range ( 0 , n , 1 ) : NEW_LINE INDENT while ( arr [ i ] > 1 ) : NEW_LINE INDENT div = spf [ arr [ i ] ] NEW_LINE last_ind = max ( last_ind , ind [ div ] ) NEW_LINE ind [ div ] = i + 1 NEW_LINE arr [ i ] = int ( arr [ i ] / div ) NEW_LINE DEDENT count += i - last_ind + 1 NEW_LINE DEDENT return count NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT sieve ( ) NEW_LINE arr = [ 2 , 3 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( countSubArray ( arr , n ) ) NEW_LINE arr1 = [ 2 , 3 , 5 , 15 , 7 , 2 ] NEW_LINE n1 = len ( arr1 ) NEW_LINE print ( countSubArray ( arr1 , n1 ) ) NEW_LINE DEDENT
T39861
import java . util . * ; class GFG { static boolean areElementsContiguous ( int arr [ ] , int n ) { int max = Integer . MIN_VALUE ; int min = Integer . MAX_VALUE ; for ( int i = 0 ; i < n ; i ++ ) { max = Math . max ( max , arr [ i ] ) ; min = Math . min ( min , arr [ i ] ) ; } int m = max - min + 1 ; if ( m > n ) return false ; boolean visited [ ] = new boolean [ n ] ; for ( int i = 0 ; i < n ; i ++ ) visited [ arr [ i ] - min ] = true ; for ( int i = 0 ; i < m ; i ++ ) if ( visited [ i ] == false ) return false ; return true ; } public static void main ( String [ ] args ) { int arr [ ] = { 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 } ; int n = arr . length ; if ( areElementsContiguous ( arr , n ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
def areElementsContiguous ( arr , n ) : NEW_LINE INDENT max1 = max ( arr ) NEW_LINE min1 = min ( arr ) NEW_LINE m = max1 - min1 + 1 NEW_LINE if ( m > n ) : NEW_LINE INDENT return False NEW_LINE DEDENT visited = [ 0 ] * m NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT visited [ arr [ i ] - min1 ] = True NEW_LINE DEDENT for i in range ( 0 , m ) : NEW_LINE INDENT if ( visited [ i ] == False ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT arr = [ 5 , 2 , 3 , 6 , 4 , 4 , 6 , 6 ] NEW_LINE n = len ( arr ) NEW_LINE if ( areElementsContiguous ( arr , n ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
T39862
class GFG { static char [ ] alphabets = " abcdefghijklmnopqrstuvwxyz " . toCharArray ( ) ; static String conversion ( String charSet , char [ ] str1 ) { String s2 = " " ; for ( char i : str1 ) s2 += alphabets [ charSet . indexOf ( i ) ] ; return s2 ; } public static void main ( String [ ] args ) { String charSet = " qwertyuiopasdfghjklzxcvbnm " ; String str1 = " egrt " ; System . out . print ( conversion ( charSet , str1 . toCharArray ( ) ) ) ; } }
def conversion ( charSet , str1 ) : NEW_LINE INDENT s2 = " " NEW_LINE for i in str1 : NEW_LINE INDENT s2 += alphabets [ charSet . index ( i ) ] NEW_LINE DEDENT return s2 NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT alphabets = " abcdefghijklmnopqrstuvwxyz " NEW_LINE charSet = " qwertyuiopasdfghjklzxcvbnm " NEW_LINE str1 = " egrt " NEW_LINE print ( conversion ( charSet , str1 ) ) NEW_LINE DEDENT
T39863
import java . io . * ; import java . util . * ; public class GFG { static int findElement ( int [ ] arr , int n ) { int [ ] leftMax = new int [ n ] ; leftMax [ 0 ] = Integer . MIN_VALUE ; for ( int i = 1 ; i < n ; i ++ ) leftMax [ i ] = Math . max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) ; int rightMin = Integer . MAX_VALUE ; for ( int i = n - 1 ; i >= 0 ; i -- ) { if ( leftMax [ i ] < arr [ i ] && rightMin > arr [ i ] ) return i ; rightMin = Math . min ( rightMin , arr [ i ] ) ; } return - 1 ; } public static void main ( String args [ ] ) { int [ ] arr = { 5 , 1 , 4 , 3 , 6 , 8 , 10 , 7 , 9 } ; int n = arr . length ; System . out . println ( " Index ▁ of ▁ the ▁ element ▁ is ▁ " + findElement ( arr , n ) ) ; } }
def findElement ( arr , n ) : NEW_LINE INDENT leftMax = [ None ] * n NEW_LINE leftMax [ 0 ] = float ( ' - inf ' ) NEW_LINE for i in range ( 1 , n ) : NEW_LINE INDENT leftMax [ i ] = max ( leftMax [ i - 1 ] , arr [ i - 1 ] ) NEW_LINE DEDENT rightMin = float ( ' inf ' ) NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if leftMax [ i ] < arr [ i ] and rightMin > arr [ i ] : NEW_LINE INDENT return i NEW_LINE DEDENT rightMin = min ( rightMin , arr [ i ] ) NEW_LINE DEDENT return - 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ 5 , 1 , 4 , 3 , 6 , 8 , 10 , 7 , 9 ] NEW_LINE n = len ( arr ) NEW_LINE print ( " Index ▁ of ▁ the ▁ element ▁ is " , findElement ( arr , n ) ) NEW_LINE DEDENT
T39864
import java . io . * ; import java . util . * ; class GFG { static ArrayList < Integer > set = new ArrayList < Integer > ( ) ; static ArrayList < Integer > prime = new ArrayList < Integer > ( ) ; static boolean isPrime ( int x ) { int sqroot = ( int ) Math . sqrt ( x ) ; if ( x == 1 ) return false ; for ( int i = 2 ; i <= sqroot ; i ++ ) if ( x % i == 0 ) return false ; return true ; } static void display ( ) { int length = set . size ( ) ; for ( int i = 0 ; i < length ; i ++ ) System . out . print ( set . get ( i ) + " ▁ " ) ; System . out . println ( ) ; } static void primeSum ( int total , int N , int S , int index ) { if ( total == S && set . size ( ) == N ) { display ( ) ; return ; } if ( total > S || index == prime . size ( ) || set . size ( ) >= N ) return ; set . add ( prime . get ( index ) ) ; primeSum ( total + prime . get ( index ) , N , S , index + 1 ) ; set . remove ( set . size ( ) - 1 ) ; primeSum ( total , N , S , index + 1 ) ; } static void allPrime ( int N , int S , int P ) { for ( int i = P + 1 ; i <= S ; i ++ ) { if ( isPrime ( i ) ) prime . add ( i ) ; } if ( prime . size ( ) < N ) return ; primeSum ( 0 , N , S , 0 ) ; } public static void main ( String args [ ] ) { int S = 54 , N = 2 , P = 3 ; allPrime ( N , S , P ) ; } }
import math NEW_LINE set = [ ] NEW_LINE prime = [ ] NEW_LINE def isPrime ( x ) : NEW_LINE INDENT sqroot = int ( math . sqrt ( x ) ) NEW_LINE flag = True NEW_LINE if ( x == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT for i in range ( 2 , sqroot + 1 ) : NEW_LINE INDENT if ( x % i == 0 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return True NEW_LINE DEDENT def display ( ) : NEW_LINE INDENT global set , prime NEW_LINE length = len ( set ) NEW_LINE for i in range ( 0 , length ) : NEW_LINE INDENT print ( set [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT def primeSum ( total , N , S , index ) : NEW_LINE INDENT global set , prime NEW_LINE if ( total == S and len ( set ) == N ) : NEW_LINE INDENT display ( ) NEW_LINE return NEW_LINE DEDENT if ( total > S or index == len ( prime ) ) : NEW_LINE INDENT return NEW_LINE DEDENT set . append ( prime [ index ] ) NEW_LINE primeSum ( total + prime [ index ] , N , S , index + 1 ) NEW_LINE set . pop ( ) NEW_LINE primeSum ( total , N , S , index + 1 ) NEW_LINE DEDENT def allPrime ( N , S , P ) : NEW_LINE INDENT global set , prime NEW_LINE for i in range ( P + 1 , S + 1 ) : NEW_LINE INDENT if ( isPrime ( i ) ) : NEW_LINE INDENT prime . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( prime ) < N ) : NEW_LINE INDENT return NEW_LINE DEDENT primeSum ( 0 , N , S , 0 ) NEW_LINE DEDENT S = 54 NEW_LINE N = 2 NEW_LINE P = 3 NEW_LINE allPrime ( N , S , P ) NEW_LINE
T39865
import java . util . * ; class GFG { static boolean canBeEqual ( char [ ] a , char [ ] b , int n ) { Vector < Character > A = new Vector < > ( ) ; Vector < Character > B = new Vector < > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] != b [ i ] ) { A . add ( a [ i ] ) ; B . add ( b [ i ] ) ; } } if ( A . size ( ) == B . size ( ) && B . size ( ) == 0 ) return true ; if ( A . size ( ) == B . size ( ) && B . size ( ) == 2 ) { if ( A . get ( 0 ) == A . get ( 1 ) && B . get ( 0 ) == B . get ( 1 ) ) return true ; } return false ; } public static void main ( String [ ] args ) { char [ ] A = " SEEKSFORGEEKS " . toCharArray ( ) ; char [ ] B = " GEEKSFORGEEKG " . toCharArray ( ) ; if ( canBeEqual ( A , B , A . length ) ) System . out . printf ( " Yes " ) ; else System . out . printf ( " No " ) ; } }
def canBeEqual ( a , b , n ) : NEW_LINE INDENT A = [ ] NEW_LINE B = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if a [ i ] != b [ i ] : NEW_LINE INDENT A . append ( a [ i ] ) NEW_LINE B . append ( b [ i ] ) NEW_LINE DEDENT DEDENT if len ( A ) == len ( B ) == 0 : NEW_LINE INDENT return True NEW_LINE DEDENT if len ( A ) == len ( B ) == 2 : NEW_LINE INDENT if A [ 0 ] == A [ 1 ] and B [ 0 ] == B [ 1 ] : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT A = ' SEEKSFORGEEKS ' NEW_LINE B = ' GEEKSFORGEEKG ' NEW_LINE if ( canBeEqual ( A , B , len ( A ) ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
T39866
public class Haversine { static double haversine ( double lat1 , double lon1 , double lat2 , double lon2 ) { double dLat = Math . toRadians ( lat2 - lat1 ) ; double dLon = Math . toRadians ( lon2 - lon1 ) ; lat1 = Math . toRadians ( lat1 ) ; lat2 = Math . toRadians ( lat2 ) ; double a = Math . pow ( Math . sin ( dLat / 2 ) , 2 ) + Math . pow ( Math . sin ( dLon / 2 ) , 2 ) * Math . cos ( lat1 ) * Math . cos ( lat2 ) ; double rad = 6371 ; double c = 2 * Math . asin ( Math . sqrt ( a ) ) ; return rad * c ; } public static void main ( String [ ] args ) { double lat1 = 51.5007 ; double lon1 = 0.1246 ; double lat2 = 40.6892 ; double lon2 = 74.0445 ; System . out . println ( haversine ( lat1 , lon1 , lat2 , lon2 ) + " ▁ K . M . " ) ; } }
import math NEW_LINE def haversine ( lat1 , lon1 , lat2 , lon2 ) : NEW_LINE INDENT dLat = ( lat2 - lat1 ) * math . pi / 180.0 NEW_LINE dLon = ( lon2 - lon1 ) * math . pi / 180.0 NEW_LINE lat1 = ( lat1 ) * math . pi / 180.0 NEW_LINE lat2 = ( lat2 ) * math . pi / 180.0 NEW_LINE a = ( pow ( math . sin ( dLat / 2 ) , 2 ) + pow ( math . sin ( dLon / 2 ) , 2 ) * math . cos ( lat1 ) * math . cos ( lat2 ) ) ; NEW_LINE rad = 6371 NEW_LINE c = 2 * math . asin ( math . sqrt ( a ) ) NEW_LINE return rad * c NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT lat1 = 51.5007 NEW_LINE lon1 = 0.1246 NEW_LINE lat2 = 40.6892 NEW_LINE lon2 = 74.0445 NEW_LINE print ( haversine ( lat1 , lon1 , lat2 , lon2 ) , " K . M . " ) NEW_LINE DEDENT
T39867
public class GFG { static float trianglearea ( float a , float b ) { if ( a < 0 || b < 0 ) return - 1 ; float area = ( float ) ( 3 * Math . sqrt ( 3 ) * Math . pow ( a , 2 ) ) / ( 4 * b ) ; return area ; } public static void main ( String [ ] args ) { float a = 4 , b = 2 ; System . out . println ( trianglearea ( a , b ) ) ; } }
from math import * NEW_LINE def trianglearea ( a , b ) : NEW_LINE INDENT if a < 0 or b < 0 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT area = ( 3 * sqrt ( 3 ) * pow ( a , 2 ) ) / ( 4 * b ) NEW_LINE return area NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a , b = 4 , 2 NEW_LINE print ( round ( trianglearea ( a , b ) , 4 ) ) NEW_LINE DEDENT
T39868
import java . io . * ; class GFG { static long evenPowerSum ( int n ) { long sum = 0 ; for ( int i = 1 ; i <= n ; i ++ ) { int j = 2 * i ; sum = sum + ( j * j * j * j ) ; } return sum ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . println ( evenPowerSum ( n ) ) ; } }
def evenPowerSum ( n ) : NEW_LINE INDENT sum = 0 ; NEW_LINE for i in range ( 1 , n + 1 ) : NEW_LINE INDENT j = 2 * i ; NEW_LINE sum = sum + ( j * j * j * j ) ; NEW_LINE DEDENT return sum ; NEW_LINE DEDENT n = 5 ; NEW_LINE print ( evenPowerSum ( n ) ) ; NEW_LINE
T39869
import java . util . * ; class GFG { public static int findMaximumPieces ( int n ) { int x = n / 2 ; return ( ( x + 1 ) * ( n - x + 1 ) ) ; } public static void main ( String [ ] args ) { int n = 3 ; System . out . print ( " Max ▁ number ▁ of ▁ pieces ▁ for ▁ n ▁ = ▁ " + n + " ▁ is ▁ " + findMaximumPieces ( 3 ) ) ; } }
def findMaximumPieces ( n ) : NEW_LINE INDENT x = n // 2 NEW_LINE return ( ( x + 1 ) * ( n - x + 1 ) ) NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 3 NEW_LINE print ( " Max ▁ number ▁ of ▁ pieces ▁ for ▁ n ▁ = ▁ " + str ( n ) + " ▁ is ▁ " + str ( findMaximumPieces ( 3 ) ) ) NEW_LINE DEDENT
T39870
class GFG { static int difference ( int n ) { int S , res ; S = ( n * ( n + 1 ) ) / 2 ; res = S * ( S - 1 ) ; return res ; } public static void main ( String [ ] args ) { int n = 5 ; System . out . print ( difference ( n ) ) ; } }
def difference ( n ) : NEW_LINE INDENT S = ( n * ( n + 1 ) ) // 2 ; NEW_LINE res = S * ( S - 1 ) ; NEW_LINE return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT n = 5 ; NEW_LINE print ( difference ( n ) ) ; NEW_LINE DEDENT
T39871
import java . util . * ; class GFG { static void printPalindrome ( Vector < String > left , String mid , Vector < String > right ) { for ( String x : left ) System . out . print ( x ) ; System . out . print ( mid ) ; Collections . reverse ( right ) ; for ( String x : right ) System . out . print ( x ) ; System . out . println ( ) ; } static void findPalindrome ( Vector < String > S , int N , int M ) { HashSet < String > dict = new HashSet < String > ( ) ; for ( int i = 0 ; i < M ; i ++ ) { dict . add ( S . get ( i ) ) ; } Vector < String > left = new Vector < String > ( ) , right = new Vector < String > ( ) ; String mid = " " ; for ( int i = 0 ; i < N ; i ++ ) { String t = S . get ( i ) ; t = reverse ( t ) ; if ( t == S . get ( i ) ) mid = t ; else if ( dict . contains ( t ) ) { left . add ( S . get ( i ) ) ; right . add ( t ) ; dict . remove ( S . get ( i ) ) ; dict . remove ( t ) ; } } printPalindrome ( left , mid , right ) ; } static String reverse ( String input ) { char [ ] a = input . toCharArray ( ) ; int l , r = a . length - 1 ; for ( l = 0 ; l < r ; l ++ , r -- ) { char temp = a [ l ] ; a [ l ] = a [ r ] ; a [ r ] = temp ; } return String . valueOf ( a ) ; } public static void main ( String [ ] args ) { String arr [ ] = { " tab " , " one " , " bat " } ; Vector < String > S = new Vector < String > ( Arrays . asList ( arr ) ) ; int M = 3 ; int N = S . size ( ) ; findPalindrome ( S , N , M ) ; } }
def printPalindrome ( left , mid , right ) : NEW_LINE INDENT for x in left : NEW_LINE INDENT print ( x , end = " " ) NEW_LINE DEDENT print ( mid , end = " " ) NEW_LINE right = right [ : : - 1 ] NEW_LINE for x in right : NEW_LINE INDENT print ( x , end = " " ) NEW_LINE DEDENT print ( ' \n ' , end = " " ) NEW_LINE DEDENT def findPalindrome ( S , N , M ) : NEW_LINE INDENT d = set ( ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT d . add ( S [ i ] ) NEW_LINE DEDENT left = [ ] NEW_LINE right = [ ] NEW_LINE mid = " " NEW_LINE for i in range ( N ) : NEW_LINE INDENT t = S [ i ] NEW_LINE t = t [ : : - 1 ] NEW_LINE if ( t == S [ i ] ) : NEW_LINE INDENT mid = t NEW_LINE DEDENT elif ( t in d ) : NEW_LINE INDENT left . append ( S [ i ] ) NEW_LINE right . append ( t ) NEW_LINE d . remove ( S [ i ] ) NEW_LINE d . remove ( t ) NEW_LINE DEDENT DEDENT printPalindrome ( left , mid , right ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT S = [ " tab " , " one " , " bat " ] NEW_LINE M = 3 NEW_LINE N = len ( S ) NEW_LINE findPalindrome ( S , N , M ) NEW_LINE DEDENT
T39872
import java . io . * ; class GFG { static long count_of_ways ( long n ) { long count = 0 ; count = ( n + 1 ) * ( n + 2 ) / 2 ; return count ; } public static void main ( String [ ] args ) { long n = 3 ; System . out . println ( count_of_ways ( n ) ) ; } }
def count_of_ways ( n ) : NEW_LINE INDENT count = 0 NEW_LINE count = ( n + 1 ) * ( n + 2 ) // 2 NEW_LINE return count NEW_LINE DEDENT n = 3 NEW_LINE print ( count_of_ways ( n ) ) NEW_LINE
T39873
static void leftRotate ( int arr [ ] , int d , int n ) { int i , j ; if ( d == 0 || d == n ) return ; i = d ; j = n - d ; while ( i != j ) { if ( i < j ) { swap ( arr , d - i , d + j - i , i ) ; j -= i ; } else { swap ( arr , d - i , d , j ) ; i -= j ; } } swap ( arr , d - i , d , i ) ; }
def leftRotate ( arr , d , n ) : NEW_LINE INDENT if ( d == 0 or d == n ) : NEW_LINE INDENT return ; NEW_LINE DEDENT i = d NEW_LINE j = n - d NEW_LINE while ( i != j ) : NEW_LINE INDENT if ( i < j ) : NEW_LINE INDENT swap ( arr , d - i , d + j - i , i ) NEW_LINE j -= i NEW_LINE DEDENT else : NEW_LINE INDENT swap ( arr , d - i , d , j ) NEW_LINE i -= j NEW_LINE DEDENT DEDENT swap ( arr , d - i , d , i ) NEW_LINE DEDENT
T39874
public class GFG { static int solve ( int A [ ] , int B [ ] , int n ) { int cnt = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i ; j < n ; j ++ ) { if ( Integer . bitCount ( A [ i ] & A [ j ] ) == B [ j ] ) { cnt ++ ; } } } return cnt ; } public static void main ( String [ ] args ) { int A [ ] = { 2 , 3 , 1 , 4 , 5 } ; int B [ ] = { 2 , 2 , 1 , 4 , 2 } ; int size = A . length ; System . out . println ( solve ( A , B , size ) ) ; } }
def solve ( A , B , n ) : NEW_LINE INDENT cnt = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i , n ) : NEW_LINE INDENT c = A [ i ] & A [ j ] NEW_LINE if ( bin ( c ) . count ( '1' ) == B [ j ] ) : NEW_LINE INDENT cnt += 1 ; NEW_LINE DEDENT DEDENT DEDENT return cnt ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 2 , 3 , 1 , 4 , 5 ] ; NEW_LINE B = [ 2 , 2 , 1 , 4 , 2 ] ; NEW_LINE size = len ( A ) ; NEW_LINE print ( solve ( A , B , size ) ) ; NEW_LINE DEDENT
T39875
import java . util . * ; class GFG { static int primeProduct ( int arr [ ] , int n ) { int max_val = Arrays . stream ( arr ) . max ( ) . getAsInt ( ) ; Vector < Boolean > prime = new Vector < Boolean > ( max_val + 1 ) ; for ( int i = 0 ; i < max_val + 1 ; i ++ ) prime . add ( i , Boolean . TRUE ) ; prime . add ( 0 , Boolean . FALSE ) ; prime . add ( 1 , Boolean . FALSE ) ; for ( int p = 2 ; p * p <= max_val ; p ++ ) { if ( prime . get ( p ) == true ) { for ( int i = p * 2 ; i <= max_val ; i += p ) prime . add ( i , Boolean . FALSE ) ; } } int prod = 1 ; for ( int i = 0 ; i < n ; i ++ ) if ( prime . get ( arr [ i ] ) ) prod *= arr [ i ] ; return prod ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 } ; int n = arr . length ; System . out . print ( primeProduct ( arr , n ) ) ; } }
import math as mt NEW_LINE def primeProduct ( arr , n ) : NEW_LINE INDENT max_val = max ( arr ) NEW_LINE prime = [ True for i in range ( max_val + 1 ) ] NEW_LINE prime [ 0 ] = False NEW_LINE prime [ 1 ] = False NEW_LINE for p in range ( mt . ceil ( mt . sqrt ( max_val ) ) ) : NEW_LINE INDENT if prime [ p ] : NEW_LINE INDENT for i in range ( p * 2 , max_val + 1 , p ) : NEW_LINE INDENT prime [ i ] = False NEW_LINE DEDENT DEDENT DEDENT prod = 1 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if prime [ arr [ i ] ] : NEW_LINE INDENT prod *= arr [ i ] NEW_LINE DEDENT DEDENT return prod NEW_LINE DEDENT arr = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 ] NEW_LINE n = len ( arr ) NEW_LINE print ( primeProduct ( arr , n ) ) NEW_LINE
T39876
import java . util . * ; class GFG { static int maxDifference ( int arr [ ] , int N , int k ) { int M , S = 0 , S1 = 0 , max_difference = 0 ; for ( int i = 0 ; i < N ; i ++ ) S += arr [ i ] ; int temp ; for ( int i = 0 ; i < N ; i ++ ) { for ( int j = i + 1 ; j < N ; j ++ ) { if ( arr [ i ] < arr [ j ] ) { temp = arr [ i ] ; arr [ i ] = arr [ j ] ; arr [ j ] = temp ; } } } M = Math . max ( k , N - k ) ; for ( int i = 0 ; i < M ; i ++ ) S1 += arr [ i ] ; max_difference = S1 - ( S - S1 ) ; return max_difference ; } public static void main ( String args [ ] ) { int arr [ ] = { 8 , 4 , 5 , 2 , 10 } ; int N = arr . length ; int k = 2 ; System . out . println ( maxDifference ( arr , N , k ) ) ; } }
def maxDifference ( arr , N , k ) : NEW_LINE INDENT S = 0 NEW_LINE S1 = 0 NEW_LINE max_difference = 0 NEW_LINE for i in range ( N ) : NEW_LINE INDENT S += arr [ i ] NEW_LINE DEDENT arr . sort ( reverse = True ) NEW_LINE M = max ( k , N - k ) NEW_LINE for i in range ( M ) : NEW_LINE INDENT S1 += arr [ i ] NEW_LINE DEDENT max_difference = S1 - ( S - S1 ) NEW_LINE return max_difference NEW_LINE DEDENT arr = [ 8 , 4 , 5 , 2 , 10 ] NEW_LINE N = len ( arr ) NEW_LINE k = 2 NEW_LINE print ( maxDifference ( arr , N , k ) ) NEW_LINE
T39877
public class GFG { static void replace_elements ( int arr [ ] , int n ) { int pos = 0 ; for ( int i = 0 ; i < n ; i ++ ) { arr [ pos ++ ] = arr [ i ] ; while ( pos > 1 && arr [ pos - 2 ] == arr [ pos - 1 ] ) { pos -- ; arr [ pos - 1 ] ++ ; } } for ( int i = 0 ; i < pos ; i ++ ) System . out . print ( arr [ i ] + " ▁ " ) ; } public static void main ( String args [ ] ) { int arr [ ] = { 6 , 4 , 3 , 4 , 3 , 3 , 5 } ; int n = arr . length ; replace_elements ( arr , n ) ; } }
from __future__ import print_function NEW_LINE def replace_elements ( arr , n ) : NEW_LINE INDENT pos = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT arr [ pos ] = arr [ i ] NEW_LINE pos = pos + 1 NEW_LINE while ( pos > 1 and arr [ pos - 2 ] == arr [ pos - 1 ] ) : NEW_LINE INDENT pos -= 1 NEW_LINE arr [ pos - 1 ] += 1 NEW_LINE DEDENT DEDENT for i in range ( 0 , pos ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) NEW_LINE DEDENT DEDENT arr = [ 6 , 4 , 3 , 4 , 3 , 3 , 5 ] NEW_LINE n = len ( arr ) NEW_LINE replace_elements ( arr , n ) NEW_LINE
T39878
import java . util . * ; import java . io . * ; class GFG { static int countPairsWithDiffK ( int arr [ ] , int n , int k ) { int count = 0 ; for ( int i = 0 ; i < n ; i ++ ) { for ( int j = i + 1 ; j < n ; j ++ ) if ( arr [ i ] - arr [ j ] == k || arr [ j ] - arr [ i ] == k ) count ++ ; } return count ; } public static void main ( String args [ ] ) { int arr [ ] = { 1 , 5 , 3 , 4 , 2 } ; int n = arr . length ; int k = 3 ; System . out . println ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " + countPairsWithDiffK ( arr , n , k ) ) ; } }
def countPairsWithDiffK ( arr , n , k ) : NEW_LINE INDENT count = 0 NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if arr [ i ] - arr [ j ] == k or arr [ j ] - arr [ i ] == k : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT DEDENT return count NEW_LINE DEDENT arr = [ 1 , 5 , 3 , 4 , 2 ] NEW_LINE n = len ( arr ) NEW_LINE k = 3 NEW_LINE print ( " Count ▁ of ▁ pairs ▁ with ▁ given ▁ diff ▁ is ▁ " , countPairsWithDiffK ( arr , n , k ) ) NEW_LINE
T39879
class GFG { public static int countSetBits ( int n ) { if ( n == 0 ) return 0 ; else return ( n & 1 ) + countSetBits ( n >> 1 ) ; } static boolean isPossible ( int a , int b ) { int cntA = countSetBits ( a ) ; int cntB = countSetBits ( b ) ; if ( cntA == cntB ) return true ; return false ; } public static void main ( String [ ] args ) { int a = 3 , b = 9 ; if ( isPossible ( a , b ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
def bitsoncount ( x ) : NEW_LINE INDENT return bin ( x ) . count ( '1' ) NEW_LINE DEDENT def isPossible ( a , b ) : NEW_LINE INDENT cntA = bitsoncount ( a ) ; NEW_LINE cntB = bitsoncount ( b ) ; NEW_LINE if ( cntA == cntB ) : NEW_LINE INDENT return True NEW_LINE DEDENT return False NEW_LINE DEDENT a = 3 NEW_LINE b = 9 NEW_LINE if ( isPossible ( a , b ) ) : NEW_LINE INDENT print ( " Yes " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No " ) NEW_LINE DEDENT
T39880
class GFG { static int minOperations ( int [ ] A , int n ) { if ( ( n & 1 ) > 0 ) return - 1 ; int zeros = 0 , consZeros = 0 , ones = 0 ; for ( int i = 0 ; i < n ; ++ i ) { if ( A [ i ] == 0 ) zeros ++ ; else ones ++ ; if ( i + 1 < n ) { if ( A [ i ] == 0 && A [ i + 1 ] == 0 ) consZeros ++ ; } } if ( A [ 0 ] == A [ n - 1 ] && A [ 0 ] == 0 ) consZeros ++ ; if ( zeros == ones ) return consZeros ; else return - 1 ; } public static void main ( String [ ] args ) { int [ ] A = new int [ ] { 1 , 1 , 0 , 0 } ; int n = A . length ; System . out . println ( minOperations ( A , n ) ) ; } }
def minOperations ( A , n ) : NEW_LINE INDENT if n & 1 : NEW_LINE INDENT return - 1 NEW_LINE DEDENT zeros , consZeros , ones = 0 , 0 , 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT if A [ i ] : NEW_LINE INDENT zeros += 1 NEW_LINE DEDENT else : NEW_LINE INDENT ones += 1 NEW_LINE DEDENT if ( i + 1 < n ) : NEW_LINE INDENT if A [ i ] == 0 and A [ i + 1 ] == 0 : NEW_LINE INDENT consZeros += 1 NEW_LINE DEDENT DEDENT DEDENT if A [ 0 ] == A [ n - 1 ] and A [ 0 ] == 0 : NEW_LINE INDENT consZeros += 1 NEW_LINE DEDENT if zeros == ones : NEW_LINE INDENT return consZeros NEW_LINE DEDENT else : NEW_LINE INDENT return - 1 NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 1 , 1 , 0 , 0 ] NEW_LINE n = len ( A ) NEW_LINE print ( minOperations ( A , n ) ) NEW_LINE DEDENT
T39881
import java . io . * ; class GFG { static boolean isequal ( String str ) { int n = str . length ( ) ; int num = 0 , x = 1 , i = n - 1 ; for ( i = n - 1 ; i >= 0 ; i -- ) { if ( '0' <= str . charAt ( i ) && str . charAt ( i ) <= '9' ) { num = ( str . charAt ( i ) - '0' ) * x + num ; x = x * 10 ; if ( num >= n ) return false ; } else break ; } return num == i + 1 ; } static public void main ( String [ ] args ) { String str = " geeksforgeeks13" ; if ( isequal ( str ) ) System . out . println ( " Yes " ) ; else System . out . println ( " No " ) ; } }
def isequal ( str ) : NEW_LINE INDENT n = len ( str ) NEW_LINE num = 0 NEW_LINE x = 1 NEW_LINE i = n - 1 NEW_LINE for i in range ( n - 1 , - 1 , - 1 ) : NEW_LINE INDENT if ( '0' <= str [ i ] and str [ i ] <= '9' ) : NEW_LINE INDENT num = ( ord ( str [ i ] ) - ord ( '0' ) ) * x + num NEW_LINE x = x * 10 NEW_LINE if ( num >= n ) : NEW_LINE INDENT return false NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT return num == i + 1 NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT str = " geeksforgeeks13" NEW_LINE print ( " Yes " ) if isequal ( str ) else print ( " No " ) NEW_LINE DEDENT
T39882
import java . util . * ; class GFG { static int sizeSubSet ( int a [ ] , int k , int n ) { Arrays . sort ( a ) ; HashMap < Integer , Integer > s = new HashMap < Integer , Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] % k != 0 || s . get ( a [ i ] / k ) == null ) s . put ( a [ i ] , s . get ( a [ i ] ) == null ? 1 : s . get ( a [ i ] ) + 1 ) ; } return s . size ( ) ; } public static void main ( String args [ ] ) { int a [ ] = { 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 } ; int n = a . length ; int k = 2 ; System . out . println ( sizeSubSet ( a , k , n ) ) ; } }
import math as mt NEW_LINE def sizeSubSet ( a , k , n ) : NEW_LINE INDENT a . sort ( ) NEW_LINE s = set ( ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] % k != 0 or a [ i ] // k not in s ) : NEW_LINE INDENT s . add ( a [ i ] ) NEW_LINE DEDENT DEDENT return len ( s ) NEW_LINE DEDENT a = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] NEW_LINE n = len ( a ) NEW_LINE k = 2 NEW_LINE print ( sizeSubSet ( a , k , n ) ) NEW_LINE
T39883
public class GFG { static void reverse ( int [ ] a , int n , int k ) { if ( k > n ) { System . out . println ( " Invalid ▁ k " ) ; return ; } for ( int i = 0 ; i < k / 2 ; i ++ ) { int tempswap = a [ i ] ; a [ i ] = a [ k - i - 1 ] ; a [ k - i - 1 ] = tempswap ; } } public static void main ( String args [ ] ) { int [ ] a = { 1 , 2 , 3 , 4 , 5 , 6 } ; int n = a . length , k = 4 ; reverse ( a , n , k ) ; for ( int i = 0 ; i < n ; ++ i ) System . out . print ( a [ i ] + " ▁ " ) ; } }
from __future__ import print_function NEW_LINE def reverse ( a , n , k ) : NEW_LINE INDENT if ( k > n ) : NEW_LINE INDENT print ( " Invalid ▁ k " ) NEW_LINE return NEW_LINE DEDENT for i in range ( 0 , ( int ) ( k / 2 ) ) : NEW_LINE INDENT temp = a [ i ] NEW_LINE a [ i ] = a [ k - i - 1 ] NEW_LINE a [ k - i - 1 ] = temp NEW_LINE DEDENT DEDENT a = [ 1 , 2 , 3 , 4 , 5 , 6 ] NEW_LINE n = len ( a ) NEW_LINE k = 4 NEW_LINE reverse ( a , n , k ) ; NEW_LINE for i in range ( 0 , n ) : NEW_LINE INDENT print ( a [ i ] , end = " ▁ " ) NEW_LINE DEDENT
T39884
import java . io . * ; class GFG { static int optimalStrategyOfGame ( int arr [ ] , int n ) { int table [ ] [ ] = new int [ n ] [ n ] ; int gap , i , j , x , y , z ; for ( gap = 0 ; gap < n ; ++ gap ) { for ( i = 0 , j = gap ; j < n ; ++ i , ++ j ) { x = ( ( i + 2 ) <= j ) ? table [ i + 2 ] [ j ] : 0 ; y = ( ( i + 1 ) <= ( j - 1 ) ) ? table [ i + 1 ] [ j - 1 ] : 0 ; z = ( i <= ( j - 2 ) ) ? table [ i ] [ j - 2 ] : 0 ; table [ i ] [ j ] = Math . max ( arr [ i ] + Math . min ( x , y ) , arr [ j ] + Math . min ( y , z ) ) ; } } return table [ 0 ] [ n - 1 ] ; } public static void main ( String [ ] args ) { int arr1 [ ] = { 8 , 15 , 3 , 7 } ; int n = arr1 . length ; System . out . println ( " " + optimalStrategyOfGame ( arr1 , n ) ) ; int arr2 [ ] = { 2 , 2 , 2 , 2 } ; n = arr2 . length ; System . out . println ( " " + optimalStrategyOfGame ( arr2 , n ) ) ; int arr3 [ ] = { 20 , 30 , 2 , 2 , 2 , 10 } ; n = arr3 . length ; System . out . println ( " " + optimalStrategyOfGame ( arr3 , n ) ) ; } }
def optimalStrategyOfGame ( arr , n ) : NEW_LINE INDENT table = [ [ 0 for i in range ( n ) ] for i in range ( n ) ] NEW_LINE for gap in range ( n ) : NEW_LINE INDENT for j in range ( gap , n ) : NEW_LINE INDENT i = j - gap NEW_LINE x = 0 NEW_LINE if ( ( i + 2 ) <= j ) : NEW_LINE INDENT x = table [ i + 2 ] [ j ] NEW_LINE DEDENT y = 0 NEW_LINE if ( ( i + 1 ) <= ( j - 1 ) ) : NEW_LINE INDENT y = table [ i + 1 ] [ j - 1 ] NEW_LINE DEDENT z = 0 NEW_LINE if ( i <= ( j - 2 ) ) : NEW_LINE INDENT z = table [ i ] [ j - 2 ] NEW_LINE DEDENT table [ i ] [ j ] = max ( arr [ i ] + min ( x , y ) , arr [ j ] + min ( y , z ) ) NEW_LINE DEDENT DEDENT return table [ 0 ] [ n - 1 ] NEW_LINE DEDENT arr1 = [ 8 , 15 , 3 , 7 ] NEW_LINE n = len ( arr1 ) NEW_LINE print ( optimalStrategyOfGame ( arr1 , n ) ) NEW_LINE arr2 = [ 2 , 2 , 2 , 2 ] NEW_LINE n = len ( arr2 ) NEW_LINE print ( optimalStrategyOfGame ( arr2 , n ) ) NEW_LINE arr3 = [ 20 , 30 , 2 , 2 , 2 , 10 ] NEW_LINE n = len ( arr3 ) NEW_LINE print ( optimalStrategyOfGame ( arr3 , n ) ) NEW_LINE
T39885
import java . util . * ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int lcm ( int a , int b ) { return ( a * b ) / gcd ( a , b ) ; } static int countPairs ( int arr [ ] , int n ) { int ans = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) ans ++ ; return ans ; } public static void main ( String [ ] args ) { int arr [ ] = { 1 , 1 , 1 } ; int n = arr . length ; System . out . print ( countPairs ( arr , n ) ) ; } }
def gcd ( a , b ) : NEW_LINE INDENT if ( a == 0 ) : NEW_LINE INDENT return b ; NEW_LINE DEDENT return gcd ( b % a , a ) ; NEW_LINE DEDENT def lcm ( a , b ) : NEW_LINE INDENT return ( a * b ) / gcd ( a , b ) ; NEW_LINE DEDENT def countPairs ( arr , n ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if ( lcm ( arr [ i ] , arr [ j ] ) == gcd ( arr [ i ] , arr [ j ] ) ) : NEW_LINE INDENT ans += 1 ; NEW_LINE DEDENT DEDENT DEDENT return ans ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 1 , 1 ] ; NEW_LINE n = len ( arr ) ; NEW_LINE print ( countPairs ( arr , n ) ) ; NEW_LINE DEDENT
T39886
class GFG { static int findmax ( int [ ] A ) { int r = A [ 0 ] ; for ( int i = 1 ; i < A . length ; i ++ ) r = Math . max ( r , A [ i ] ) ; return r ; } static boolean isPossible ( int [ ] A , int n , int H , int K ) { int time = 0 ; for ( int i = 0 ; i < n ; ++ i ) time += ( A [ i ] - 1 ) / K + 1 ; return time <= H ; } static int minJobSpeed ( int [ ] A , int n , int H ) { if ( H < n ) return - 1 ; int max = findmax ( A ) ; int lo = 1 , hi = max ; while ( lo < hi ) { int mi = lo + ( hi - lo ) / 2 ; if ( ! isPossible ( A , n , H , mi ) ) lo = mi + 1 ; else hi = mi ; } return lo ; } public static void main ( String [ ] args ) { int [ ] A = { 3 , 6 , 7 , 11 } ; int H = 8 ; int n = A . length ; System . out . println ( minJobSpeed ( A , n , H ) ) ; } }
def isPossible ( A , n , H , K ) : NEW_LINE INDENT time = 0 NEW_LINE for i in range ( n ) : NEW_LINE INDENT time += ( A [ i ] - 1 ) // K + 1 NEW_LINE DEDENT return time <= H NEW_LINE DEDENT def minJobSpeed ( A , n , H ) : NEW_LINE INDENT if H < n : NEW_LINE INDENT return - 1 NEW_LINE DEDENT Max = max ( A ) NEW_LINE lo , hi = 1 , Max NEW_LINE while lo < hi : NEW_LINE INDENT mi = lo + ( hi - lo ) // 2 NEW_LINE if not isPossible ( A , n , H , mi ) : NEW_LINE INDENT lo = mi + 1 NEW_LINE DEDENT else : NEW_LINE INDENT hi = mi NEW_LINE DEDENT DEDENT return lo NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT A = [ 3 , 6 , 7 , 11 ] NEW_LINE H = 8 NEW_LINE n = len ( A ) NEW_LINE print ( minJobSpeed ( A , n , H ) ) NEW_LINE DEDENT
T39887
import java . util . Scanner ; class Pattern { static void display ( int n ) { int i , j , k ; for ( i = 1 ; i <= n ; i ++ ) { for ( j = 1 , k = i ; j <= i ; j ++ , k -- ) { if ( k % 2 == 0 ) { System . out . print ( j ) ; } else { System . out . print ( " * " ) ; } } System . out . print ( " \n " ) ; } } public static void main ( String [ ] args ) { int n = 5 ; display ( n ) ; } }
def display ( n ) : NEW_LINE INDENT for i in range ( 1 , n + 1 ) : NEW_LINE INDENT k = i NEW_LINE for j in range ( 1 , i + 1 ) : NEW_LINE INDENT if k % 2 == 0 : NEW_LINE INDENT print ( j , end = ' ' ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( ' * ' , end = ' ' ) NEW_LINE DEDENT k -= 1 NEW_LINE DEDENT print ( ) NEW_LINE DEDENT DEDENT n = 5 NEW_LINE display ( n ) NEW_LINE
T39888
class GFG { static boolean isPower ( int x , int y ) { int res1 = ( int ) ( Math . log ( y ) / Math . log ( x ) ) ; double res2 = Math . log ( y ) / Math . log ( x ) ; return ( res1 == res2 ) ; } static int countPower ( int arr [ ] , int n ) { int res = 0 ; for ( int i = 0 ; i < n ; i ++ ) for ( int j = i + 1 ; j < n ; j ++ ) if ( isPower ( arr [ i ] , arr [ j ] ) || isPower ( arr [ j ] , arr [ i ] ) ) res ++ ; return res ; } public static void main ( String [ ] args ) { int a [ ] = { 16 , 2 , 3 , 9 } ; int n = a . length ; System . out . println ( countPower ( a , n ) ) ; } }
from math import log NEW_LINE def isPower ( x , y ) : NEW_LINE INDENT res1 = log ( y ) // log ( x ) ; NEW_LINE res2 = log ( y ) / log ( x ) ; NEW_LINE return ( res1 == res2 ) ; NEW_LINE DEDENT def countPower ( arr , n ) : NEW_LINE INDENT res = 0 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT for j in range ( i + 1 , n ) : NEW_LINE INDENT if isPower ( arr [ i ] , arr [ j ] ) or isPower ( arr [ j ] , arr [ i ] ) : NEW_LINE INDENT res += 1 ; NEW_LINE DEDENT DEDENT DEDENT return res ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 16 , 2 , 3 , 9 ] ; NEW_LINE n = len ( a ) ; NEW_LINE print ( countPower ( a , n ) ) ; NEW_LINE DEDENT
T39889
import java . io . * ; import java . util . * ; class GFG { static int MAX = 1000 ; static int f [ ] = new int [ MAX ] ; static int fib ( int n ) { Arrays . fill ( f , 0 ) ; if ( n == 0 ) return 0 ; if ( n == 1 || n == 2 ) return ( f [ n ] = 1 ) ; if ( f [ n ] == 1 ) return f [ n ] ; int k ; if ( ( n & 1 ) == 1 ) k = ( n + 1 ) / 2 ; else k = n / 2 ; if ( ( n & 1 ) == 1 ) f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) ; else f [ n ] = ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) ; return f [ n ] ; } static int calculateSum ( int n ) { return fib ( n + 2 ) - 1 ; } public static void main ( String args [ ] ) { int n = 4 ; System . out . println ( " Sum ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " + calculateSum ( n ) ) ; } }
MAX = 1000 NEW_LINE f = [ 0 ] * MAX NEW_LINE def fib ( n ) : NEW_LINE INDENT n = int ( n ) NEW_LINE if ( n == 0 ) : NEW_LINE INDENT return 0 NEW_LINE DEDENT if ( n == 1 or n == 2 ) : NEW_LINE INDENT return ( 1 ) NEW_LINE DEDENT if ( f [ n ] == True ) : NEW_LINE INDENT return f [ n ] NEW_LINE DEDENT k = ( n + 1 ) / 2 if ( n & 1 ) else n / 2 NEW_LINE f [ n ] = ( fib ( k ) * fib ( k ) + fib ( k - 1 ) * fib ( k - 1 ) ) if ( n & 1 ) else ( 2 * fib ( k - 1 ) + fib ( k ) ) * fib ( k ) NEW_LINE return f [ n ] NEW_LINE DEDENT def calculateSum ( n ) : NEW_LINE INDENT return fib ( n + 2 ) - 1 NEW_LINE DEDENT n = 4 NEW_LINE print ( " Sum ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : " , calculateSum ( n ) ) NEW_LINE
T39890
import java . util . * ; class GFG { static int __gcd ( int a , int b ) { if ( b == 0 ) return a ; return __gcd ( b , a % b ) ; } static boolean coprime ( int a , int b ) { if ( __gcd ( a , b ) == 1 ) return true ; else return false ; } static boolean isPrime ( int n ) { if ( n <= 1 ) return false ; if ( n == 2 || n == 3 ) return true ; for ( int i = 2 ; i * i <= n ; i ++ ) if ( n % i == 0 ) return false ; return true ; } static void findNumbers ( int a , int b , int n ) { boolean possible = true ; if ( ! coprime ( a , b ) ) possible = false ; int c1 = 1 ; int c2 = 1 ; int num1 , num2 ; HashSet < Integer > st = new HashSet < Integer > ( ) ; if ( possible ) { while ( ( int ) st . size ( ) != n ) { num1 = a + ( c1 * b ) ; if ( isPrime ( num1 ) ) { st . add ( num1 ) ; } c1 ++ ; num2 = b + ( c2 * a ) ; if ( isPrime ( num2 ) ) { st . add ( num2 ) ; } c2 ++ ; } for ( int i : st ) System . out . print ( i + " ▁ " ) ; } else System . out . print ( " - 1" ) ; } public static void main ( String [ ] args ) { int a = 3 ; int b = 5 ; int n = 4 ; findNumbers ( a , b , n ) ; } }
from math import gcd , sqrt NEW_LINE def coprime ( a , b ) : NEW_LINE INDENT if ( gcd ( a , b ) == 1 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT else : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT def isPrime ( n ) : NEW_LINE INDENT if ( n <= 1 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT if ( n == 2 or n == 3 ) : NEW_LINE INDENT return True ; NEW_LINE DEDENT for i in range ( 2 , int ( sqrt ( n ) ) + 1 ) : NEW_LINE INDENT if ( n % i == 0 ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT def findNumbers ( a , b , n ) : NEW_LINE INDENT possible = True ; NEW_LINE if ( not coprime ( a , b ) ) : NEW_LINE INDENT possible = False ; NEW_LINE DEDENT c1 = 1 ; NEW_LINE c2 = 1 ; NEW_LINE num1 = 0 ; NEW_LINE num2 = 0 ; NEW_LINE st = set ( ) ; NEW_LINE if ( possible ) : NEW_LINE INDENT while ( len ( st ) != n ) : NEW_LINE INDENT num1 = a + ( c1 * b ) ; NEW_LINE if ( isPrime ( num1 ) ) : NEW_LINE INDENT st . add ( num1 ) ; NEW_LINE DEDENT c1 += 1 ; NEW_LINE num2 = b + ( c2 * a ) ; NEW_LINE if ( isPrime ( num2 ) ) : NEW_LINE INDENT st . add ( num2 ) ; NEW_LINE DEDENT c2 += 1 ; NEW_LINE DEDENT for i in st : NEW_LINE INDENT print ( i , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT else : NEW_LINE INDENT print ( " - 1" ) ; NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = 3 ; NEW_LINE b = 5 ; NEW_LINE n = 4 ; NEW_LINE findNumbers ( a , b , n ) ; NEW_LINE DEDENT
T39891
import java . io . * ; class GFG { static void translate ( char str [ ] ) { for ( int i = 1 ; i < str . length ; i ++ ) { if ( str [ i - 1 ] == ' A ' && str [ i ] == ' B ' ) { str [ i - 1 ] = ' C ' ; int j ; for ( j = i ; j < str . length - 1 ; j ++ ) str [ j ] = str [ j + 1 ] ; str [ j ] = ' ▁ ' ; } } return ; } public static void main ( String args [ ] ) { String st = " helloABworldABGfG " ; char str [ ] = st . toCharArray ( ) ; translate ( str ) ; System . out . println ( " The ▁ modified ▁ string ▁ is ▁ : " ) ; System . out . println ( str ) ; } }
def translate ( st ) : NEW_LINE INDENT for i in range ( 1 , len ( st ) ) : NEW_LINE INDENT if ( st [ i - 1 ] == ' A ' and st [ i ] == ' B ' ) : NEW_LINE INDENT st [ i - 1 ] = ' C ' NEW_LINE for j in range ( i , len ( st ) - 1 ) : NEW_LINE INDENT st [ j ] = st [ j + 1 ] NEW_LINE DEDENT st [ len ( st ) - 1 ] = ' ▁ ' NEW_LINE DEDENT DEDENT return NEW_LINE DEDENT st = list ( " helloABworldABGfG " ) NEW_LINE translate ( st ) NEW_LINE print ( " The ▁ modified ▁ string ▁ is ▁ : " ) NEW_LINE print ( ' ' . join ( st ) ) NEW_LINE
T39892
import java . io . * ; class GFG { static int leonardo ( int n ) { if ( n == 0 || n == 1 ) return 1 ; return ( leonardo ( n - 1 ) + leonardo ( n - 2 ) + 1 ) ; } public static void main ( String args [ ] ) { System . out . println ( leonardo ( 3 ) ) ; } }
def leonardo ( n ) : NEW_LINE INDENT if ( n == 0 or n == 1 ) : NEW_LINE INDENT return 1 NEW_LINE DEDENT return ( leonardo ( n - 1 ) + leonardo ( n - 2 ) + 1 ) ; NEW_LINE DEDENT print ( leonardo ( 3 ) ) NEW_LINE
T39893
import java . io . * ; class GFG { static boolean isValidSeq ( int [ ] a , int n ) { int nodes = n + 2 ; for ( int i = 0 ; i < n ; i ++ ) { if ( a [ i ] < 1 || a [ i ] > nodes ) return false ; } return true ; } public static void main ( String [ ] args ) { int a [ ] = { 4 , 1 , 3 , 4 } ; int n = a . length ; if ( isValidSeq ( a , n ) ) System . out . println ( " Valid " ) ; else System . out . print ( " Invalid " ) ; } }
def isValidSeq ( a , n ) : NEW_LINE INDENT nodes = n + 2 ; NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( a [ i ] < 1 or a [ i ] > nodes ) : NEW_LINE INDENT return False ; NEW_LINE DEDENT DEDENT return True ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT a = [ 4 , 1 , 3 , 4 ] ; NEW_LINE n = len ( a ) ; NEW_LINE if ( isValidSeq ( a , n ) ) : NEW_LINE INDENT print ( " Valid " ) ; NEW_LINE DEDENT else : NEW_LINE INDENT print ( " Invalid " ) ; NEW_LINE DEDENT DEDENT
T39894
class GFG { static String allBitsSetInTheGivenRange ( int n , int l , int r ) { int num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; int new_num = n & num ; if ( num == new_num ) return " Yes " ; return " No " ; } public static void main ( String [ ] args ) { int n = 22 ; int l = 2 , r = 3 ; System . out . print ( allBitsSetInTheGivenRange ( n , l , r ) ) ; } }
def allBitsSetInTheGivenRange ( n , l , r ) : NEW_LINE INDENT num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) NEW_LINE new_num = n & num NEW_LINE if ( num == new_num ) : NEW_LINE INDENT return " Yes " NEW_LINE DEDENT return " No " NEW_LINE DEDENT n , l , r = 22 , 2 , 3 NEW_LINE print ( allBitsSetInTheGivenRange ( n , l , r ) ) NEW_LINE
T39895
public class Similar_strings { static int countManipulations ( String s1 , String s2 ) { int count = 0 ; int char_count [ ] = new int [ 26 ] ; for ( int i = 0 ; i < s1 . length ( ) ; i ++ ) char_count [ s1 . charAt ( i ) - ' a ' ] ++ ; for ( int i = 0 ; i < s2 . length ( ) ; i ++ ) if ( char_count [ s2 . charAt ( i ) - ' a ' ] -- <= 0 ) count ++ ; return count ; } public static void main ( String [ ] args ) { String s1 = " ddcf " ; String s2 = " cedk " ; System . out . println ( countManipulations ( s1 , s2 ) ) ; } }
def countManipulations ( s1 , s2 ) : NEW_LINE INDENT count = 0 NEW_LINE char_count = [ 0 ] * 26 NEW_LINE for i in range ( 26 ) : NEW_LINE INDENT char_count [ i ] = 0 NEW_LINE DEDENT for i in range ( len ( s1 ) ) : NEW_LINE INDENT char_count [ ord ( s1 [ i ] ) - ord ( ' a ' ) ] += 1 NEW_LINE DEDENT for i in range ( len ( s2 ) ) : NEW_LINE INDENT char_count [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] -= 1 NEW_LINE if ( char_count [ ord ( s2 [ i ] ) - ord ( ' a ' ) ] < 0 ) : NEW_LINE INDENT count += 1 NEW_LINE DEDENT DEDENT return count NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s1 = " ddcf " NEW_LINE s2 = " cedk " NEW_LINE print ( countManipulations ( s1 , s2 ) ) NEW_LINE DEDENT
T39896
import java . util . * ; class GFG { static int maximum = Integer . MIN_VALUE , x , ans = Integer . MAX_VALUE ; static Vector < Vector < Integer > > graph = new Vector < Vector < Integer > > ( ) ; static Vector < Integer > weight = new Vector < Integer > ( ) ; static int __builtin_popcount ( int x ) { int c = 0 ; for ( int i = 0 ; i < 60 ; i ++ ) if ( ( ( x >> i ) & 1 ) != 0 ) c ++ ; return c ; } static void dfs ( int node , int parent ) { int a = __builtin_popcount ( weight . get ( node ) + x ) ; if ( maximum < a ) { maximum = a ; ans = node ; } else if ( maximum == a ) ans = Math . min ( ans , node ) ; for ( int i = 0 ; i < graph . get ( node ) . size ( ) ; i ++ ) { if ( graph . get ( node ) . get ( i ) == parent ) continue ; dfs ( graph . get ( node ) . get ( i ) , node ) ; } } public static void main ( String args [ ] ) { x = 15 ; weight . add ( 0 ) ; weight . add ( 5 ) ; weight . add ( 10 ) ; ; weight . add ( 11 ) ; ; weight . add ( 8 ) ; weight . add ( 6 ) ; for ( int i = 0 ; i < 100 ; i ++ ) graph . add ( new Vector < Integer > ( ) ) ; graph . get ( 1 ) . add ( 2 ) ; graph . get ( 2 ) . add ( 3 ) ; graph . get ( 2 ) . add ( 4 ) ; graph . get ( 1 ) . add ( 5 ) ; dfs ( 1 , 1 ) ; System . out . println ( ans ) ; } }
from sys import maxsize NEW_LINE maximum , x , ans = - maxsize , None , maxsize NEW_LINE graph = [ [ ] for i in range ( 100 ) ] NEW_LINE weight = [ 0 ] * 100 NEW_LINE def dfs ( node , parent ) : NEW_LINE INDENT global x , ans , graph , weight , maximum NEW_LINE a = bin ( weight [ node ] + x ) . count ( '1' ) NEW_LINE if maximum < a : NEW_LINE INDENT maximum = a NEW_LINE ans = node NEW_LINE DEDENT elif maximum == a : NEW_LINE INDENT ans = min ( ans , node ) NEW_LINE DEDENT for to in graph [ node ] : NEW_LINE INDENT if to == parent : NEW_LINE INDENT continue NEW_LINE DEDENT dfs ( to , node ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT x = 15 NEW_LINE weight [ 1 ] = 5 NEW_LINE weight [ 2 ] = 10 NEW_LINE weight [ 3 ] = 11 NEW_LINE weight [ 4 ] = 8 NEW_LINE weight [ 5 ] = 6 NEW_LINE graph [ 1 ] . append ( 2 ) NEW_LINE graph [ 2 ] . append ( 3 ) NEW_LINE graph [ 2 ] . append ( 4 ) NEW_LINE graph [ 1 ] . append ( 5 ) NEW_LINE dfs ( 1 , 1 ) NEW_LINE print ( ans ) NEW_LINE DEDENT
T39897
import java . lang . * ; class GFG { static final int MAX = 100001 ; static long phi [ ] = new long [ MAX ] ; static long result [ ] = new long [ MAX ] ; static void computeTotient ( ) { phi [ 1 ] = 1 ; for ( int i = 2 ; i < MAX ; i ++ ) { if ( phi [ i ] == 0 ) { phi [ i ] = i - 1 ; for ( int j = ( i << 1 ) ; j < MAX ; j += i ) { if ( phi [ j ] == 0 ) phi [ j ] = j ; phi [ j ] = ( phi [ j ] / i ) * ( i - 1 ) ; } } } } static void sumOfGcdPairs ( ) { computeTotient ( ) ; for ( int i = 1 ; i < MAX ; ++ i ) { for ( int j = 2 ; i * j < MAX ; ++ j ) result [ i * j ] += i * phi [ j ] ; } for ( int i = 2 ; i < MAX ; i ++ ) result [ i ] += result [ i - 1 ] ; } public static void main ( String [ ] args ) { sumOfGcdPairs ( ) ; int N = 4 ; System . out . println ( " Summation ▁ of ▁ " + N + " ▁ = ▁ " + result [ N ] ) ; N = 12 ; System . out . println ( " Summation ▁ of ▁ " + N + " ▁ = ▁ " + result [ N ] ) ; N = 5000 ; System . out . print ( " Summation ▁ of ▁ " + N + " ▁ = ▁ " + + result [ N ] ) ; } }
MAX = 100001 NEW_LINE phi = [ 0 ] * MAX NEW_LINE result = [ 0 ] * MAX NEW_LINE def computeTotient ( ) : NEW_LINE INDENT phi [ 1 ] = 1 NEW_LINE for i in range ( 2 , MAX ) : NEW_LINE INDENT if not phi [ i ] : NEW_LINE INDENT phi [ i ] = i - 1 NEW_LINE for j in range ( i << 1 , MAX , i ) : NEW_LINE INDENT if not phi [ j ] : NEW_LINE INDENT phi [ j ] = j NEW_LINE DEDENT phi [ j ] = ( ( phi [ j ] // i ) * ( i - 1 ) ) NEW_LINE DEDENT DEDENT DEDENT DEDENT def sumOfGcdPairs ( ) : NEW_LINE INDENT computeTotient ( ) NEW_LINE for i in range ( MAX ) : NEW_LINE INDENT for j in range ( 2 , MAX ) : NEW_LINE INDENT if i * j >= MAX : NEW_LINE INDENT break NEW_LINE DEDENT result [ i * j ] += i * phi [ j ] NEW_LINE DEDENT DEDENT for i in range ( 2 , MAX ) : NEW_LINE INDENT result [ i ] += result [ i - 1 ] NEW_LINE DEDENT DEDENT sumOfGcdPairs ( ) NEW_LINE N = 4 NEW_LINE print ( " Summation ▁ of " , N , " = " , result [ N ] ) NEW_LINE N = 12 NEW_LINE print ( " Summation ▁ of " , N , " = " , result [ N ] ) NEW_LINE N = 5000 NEW_LINE print ( " Summation ▁ of " , N , " = " , result [ N ] ) NEW_LINE
T39898
class GFG { static int getsum ( int x ) { return ( x * ( x + 1 ) ) / 2 ; } static int countJumps ( int n ) { n = Math . abs ( n ) ; int ans = 0 ; while ( getsum ( ans ) < n || ( ( getsum ( ans ) - n ) & 1 ) > 0 ) ans ++ ; return ans ; } public static void main ( String args [ ] ) { int n = 9 ; System . out . println ( countJumps ( n ) ) ; } }
def getsum ( x ) : NEW_LINE INDENT return int ( ( x * ( x + 1 ) ) / 2 ) NEW_LINE DEDENT def countJumps ( n ) : NEW_LINE INDENT n = abs ( n ) NEW_LINE ans = 0 NEW_LINE while ( getsum ( ans ) < n or ( getsum ( ans ) - n ) & 1 ) : NEW_LINE INDENT ans += 1 NEW_LINE DEDENT return ans NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 9 NEW_LINE print ( countJumps ( n ) ) NEW_LINE DEDENT
T39899
class GFG { static boolean isPowerful ( int n ) { while ( n % 2 == 0 ) { int power = 0 ; while ( n % 2 == 0 ) { n /= 2 ; power ++ ; } if ( power == 1 ) return false ; } for ( int factor = 3 ; factor <= Math . sqrt ( n ) ; factor += 2 ) { int power = 0 ; while ( n % factor == 0 ) { n = n / factor ; power ++ ; } if ( power == 1 ) return false ; } return ( n == 1 ) ; } static boolean isPower ( int a ) { if ( a == 1 ) return true ; for ( int i = 2 ; i * i <= a ; i ++ ) { double val = Math . log ( a ) / Math . log ( i ) ; if ( ( val - ( int ) val ) < 0.00000001 ) return true ; } return false ; } static boolean isAchillesNumber ( int n ) { if ( isPowerful ( n ) && ! isPower ( n ) ) return true ; else return false ; } public static void main ( String [ ] args ) { int n = 72 ; if ( isAchillesNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; n = 36 ; if ( isAchillesNumber ( n ) ) System . out . println ( " YES " ) ; else System . out . println ( " NO " ) ; } }
from math import sqrt , log NEW_LINE def isPowerful ( n ) : NEW_LINE INDENT while ( n % 2 == 0 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % 2 == 0 ) : NEW_LINE INDENT n /= 2 NEW_LINE power += 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT p = int ( sqrt ( n ) ) + 1 NEW_LINE for factor in range ( 3 , p , 2 ) : NEW_LINE INDENT power = 0 NEW_LINE while ( n % factor == 0 ) : NEW_LINE INDENT n = n / factor NEW_LINE power += 1 NEW_LINE DEDENT if ( power == 1 ) : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT return ( n == 1 ) NEW_LINE DEDENT def isPower ( a ) : NEW_LINE INDENT if ( a == 1 ) : NEW_LINE INDENT return True NEW_LINE DEDENT p = int ( sqrt ( a ) ) + 1 NEW_LINE for i in range ( 2 , a , 1 ) : NEW_LINE INDENT val = log ( a ) / log ( i ) NEW_LINE if ( ( val - int ( val ) ) < 0.00000001 ) : NEW_LINE INDENT return True NEW_LINE DEDENT DEDENT return False NEW_LINE DEDENT def isAchillesNumber ( n ) : NEW_LINE INDENT if ( isPowerful ( n ) == True and isPower ( n ) == False ) : NEW_LINE INDENT return True NEW_LINE DEDENT else : NEW_LINE INDENT return False NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT n = 72 NEW_LINE if ( isAchillesNumber ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT n = 36 NEW_LINE if ( isAchillesNumber ( n ) ) : NEW_LINE INDENT print ( " YES " ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " NO " ) NEW_LINE DEDENT DEDENT