Cocubes Questions Q Second largest Number: CoCubes Coding Question
1. PROBLEM STTEMENT: !ou are gi"en a #unction$ int %indSecondLargestNumber&i %indSecondLargestNumber&int nt a$int b$int c'( T)e #unction ta*es t)ree integers +a+$ +b+$+c+ as in,ut .-m,lement t)e #unction to #ind and return t)e second largest number.
CO-N/: int %indSeconLargestNumber&int a$int b$int c'( 0
ns2er: 3include 4stdio.)5 int main&' 0 int a$b$c$lar$seclar(
scan#&67d7d7d6$8a$8b$8c'(
i#&a5b' 0 i#&a5c' lar9a( else seclar9a(
i#&b5c' 0 i#&b5a' lar9a( else seclar9b( i#&c5a' 0 i#&c5b' lar9c( else seclar9c( ,rint#&6second largest number :7d6$seclar'(
Q Re"erse arra : CoCubes coding ;uestion Ques. !ou are gi"en a #unction$int< Re"erserra&int< arr$ int lengt)'( T)e #unction ta*es an integer arra and its lengt) as in,ut. -m,lement t)e #unction to return t)e arra suc) t)at t)e arra is re"ersed i.e. t)e #irst element o# t)e arra occu,ies t)e last ,osition$ second element occu,ies t)e second last ,osition and so on. Note: T)e re=arrangement is to be done in=,lace i.e ou cannot use anot)er arra. ssum,tion: !ou ma assume t)at t)e arra is o# e"en lengt). E>am,le: -n,ut: ? @ A ? 1D 1 D Out,ut: D 1 1D ? A @ ? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< Program <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< 3include4stdio.)5 < %unction to re"erse arrFG #rom start to end< "oid r"ereserra&int arrFG$ int start$ int end' 0 int tem,( 2)ile &start 4 end' 0 tem, 9 arrFstartG( arrFstartG 9 arrFendG( arrFendG 9 tem,( startHH( end==( < Itilit t)at ,rints out an arra on a line < "oid ,rintrra&int arrFG$ int siJe' 0 int i( #or &i9( i 4 siJe( iHH' ,rint#&67d 6$ arrFiG'( ,rint#&6Kn6'( < ri"er #unction to test abo"e #unctions < int main&' 0 int arrFG 9 01$ ?$ $ @$ D$ A( ,rintrra&arr$ A'( r"ereserra&arr$ $ D'(
,rint#&6Re"ersed arra is Kn6'( ,rintrra&arr$ A'( return ( Q %ind sum lea"ing our ro2 and col: CoCubes coding ;uestion
PROBLEM STTEMENT: !ou are gi"en a #unction$ int %indSumLea"ingOutRo2Col&int<< arr$ int m$ int n$ int i$int '( T)e #unction ta*es a t2o=dimensional arra +arr+$ its number o# ro2s +m+$ its number o# columns +n+ and integers +i+ and ++ as in,ut. -m,lement t)e #unction to #ind and return t)e sum o# elements o# t)e arra lea"ing out t)e elements o# t)e i and column. T)e algorit)m to #ind t)e sum is as #ollo2s: 1.-terate o"er e"er ro2 e>ce,t #or i ro2$and *ee, on adding eac) element e>ce,t #or t)e elements o# column to a "ariable +sum+. NOTE: 1.Ro2 and column indices start #rom . ?. alue o# i and #rom 1. CO-N/: int %indSumLea"ingOutRo2Col&int<< arr$ int m$ int n$int '( 0 < 2rite our code )ere < <<<<<<<<<<<<<<<<<<<<<<< Program <<<<<<<<<<<<<<<<<<<<<<< < < C ,rogram to acce,t a matri> o# order M > N and #ind t)e sum < o# eac) ro2 and eac) column o# a matri> < 3include 4stdio.)5 "oid main &' 0 static int arraF1GF1G( int i$ $ m$ n$ sum 9 ( ,rint#&6Enter t)e order o# t)e matri>Kn6'( scan#&67d 7d6$ 8m$ 8n'( ,rint#&6Enter t)e co=e##icients o# t)e matri>Kn6'( #or &i 9 ( i 4 m( HHi' 0 #or & 9 ( 4 n( HH' 0 scan#&67d6$ 8arraFiGFG'( #or &i 9 ( i 4 m( HHi' 0 #or & 9 ( 4 n( HH' 0 sum 9 sum H arraFiGFG (
,rint#&6Sum o# t)e 7d ro2 is 9 7dKn6$ i$ sum'( sum 9 ( sum 9 ( #or & 9 ( 4 n( HH' 0 #or &i 9 ( i 4 m( HHi' 0 sum 9 sum H arraFiGFG( ,rint#&6Sum o# t)e 7d column is 9 7dKn6$ $ sum'( sum 9 (
Q Printing all t)e Leaders in an rra rite a ,rogram to ,rint all t)e LEERS in t)e arra. n element is leader i# it is greater t)an all t)e elements to its rig)t side. nd t)e rig)tmost element is al2as a leader. %or e>am,le int t)e arra 01A$ 1$ @$ $ $ $ leaders are 1$ and it) CHH 3include4iostream5 using names,ace std(
Q? Ma>imum di##erence bet2een t2o elements suc) t)at larger element a,,ears a#ter t)e
smaller number /i"en an arra arrFG o# integers$ #ind out t)e di##erence bet2een an t2o elements suc) t)at larger element a,,ears a#ter t)e smaller number in arrFG. E>am,les: -# arra is F?$ $ 1$ A$ @$ $ 1G t)en returned "alue s)ould be &i## bet2een 1 and ?'. -# arra is F V$ $ D$ A$ $ ? G t)en returned "alue s)ould be ? &i## bet2een V and ' Time Com,le>it: O&nW?' u>iliar S,ace: O&1' Ise t2o loo,s. -n t)e outer loo,$ ,ic* elements one b one and in t)e inner loo, calculate t)e di##erence o# t)e ,ic*ed element 2it) e"er ot)er element in t)e arra and com,are t)e di##erence 2it) t)e ma>imum di##erence calculated so #ar.
C Xa"a
C 3include < T)e #unction assumes t)at t)ere are at least t2o elements in arra. T)e #unction returns a negati"e "alue i# t)e arra is sorted in decreasing order. Returns i# elements are e;ual < int ma>i##&int arrFG$ int arrYsiJe' 0 int ma>Ydi## 9 arrF1G Z arrFG( int i$ ( #or &i 9 ( i 4 arrYsiJe( iHH' 0 #or & 9 iH1( 4 arrYsiJe( HH' 0 i# &arrFG Z arrFiG 5 ma>Ydi##' ma>Ydi## 9 arrFG Z arrFiG( return ma>Ydi##( < ri"er ,rogram to test abo"e #unction < int main&' 0 int arrFG 9 01$ ?$ $ 1$ 11( ,rint#&UMa>imum di##erence is 7d$ ma>i##&arr$ D''( getc)ar&'( return ( Q Longest Pre#i> Su##i> /i"en a string o# c)aracter$ #ind t)e lengt) o# longest ,ro,er ,re#i> 2)ic) is also a ,ro,er su##i>. E>am,le:
S 9 abab l,s is ? because$ ab.. is ,re#i> and ..ab is also a su##i>. -n,ut: %irst line is T number o# test cases. 149T491. Eac) test case )as one line denoting t)e string o# lengt) less t)an 1. E>,ected time com,e>it is O&N'. Out,ut: Print lengt) o# longest ,ro,er ,re#i> 2)ic) is also a ,ro,er su##i>. E>am,le: -n,ut: ? abab aaaa Out,ut: ? CHH 3include 4 bits stdcHH.) 5 using names,ace std( int l,s&string'( int main&' 0 code int T( cin 55 T( getc)ar&'( 2)ile &TZ' 0 string s( cin 55 s( ,rint#&U7dKn$ l,s&s''( return ( int l,s&string s' 0 int n 9 s.siJe&'( int l,sFnG( int i 9 1$ 9 ( l,sFG 9 ( 2)ile &i 4 n' 0 i# &sFiG 99 sFG' 0 HH( l,sFiG 9 ( iHH( else 0 i# & 9 ' 9 l,sF Z 1G( else 0 l,sFiG 9 ( iHH( return l,sFn Z 1G(
Q@ %ind t)e number closest to n and di"isible b m /i"en t2o integers n and m. T)e ,roblem is to #ind t)e number closest to n and di"isible b m. -# t)ere are more t)an one suc) number$ t)en out,ut t)e one )a"ing ma>imum absolute "alue. -# n is com,letel di"isible b m$ t)en out,ut n onl. Time com,le>it o# O&1' is re;uired. Constraints: m 9 e #ind "alue o# nm. Let t)is "alue be ;. T)en 2e #ind closest o# t2o ,ossibilities. One is ; < m ot)er is &m < &; H 1'' or &m < &; Z 1'' de,ending on 2)et)er one o# t)e gi"en t2o numbers is negati"e or not. lgorit)m: closestNumber&n$ m' eclare ;$ n1$ n? ; 9 n m n1 9 m < ;
i# &n < m' 5 n? 9 m < &; H 1' else n? 9 m < &; = 1' i# abs&n=n1' 4 abs&n=n?' return n1 return n?
CHH CHH im,lementation to #ind t)e number closest to n and di"isible b m 3include 4bitsstdcHH.)5 using names,ace std( #unction to #ind t)e number closest to n and di"isible b m int closestNumber&int n$ int m' 0 #ind t)e ;uotient int ; 9 n m( 1st ,ossible closest number int n1 9 m < ;( ?nd ,ossible closest number int n? 9 &n < m' 5 &m < &; H 1'' : &m < &; Z 1''( i# true$ t)en n1 is t)e re;uired closest number i# &abs&n Z n1' 4 abs&n Z n?'' return n1( else n? is t)e re;uired closest number return n?(
ri"er ,rogram to test abo"e int main&' 0 int n 9 1$ m 9 @( cout 44 closestNumber&n$ m' 44 endl( n 9 =1D( m 9 A( cout 44 closestNumber&n$ m' 44 endl( n 9 ( m 9 ( cout 44 closestNumber&n$ m' 44 endl( n 9 1( m 9 =V( cout 44 closestNumber&n$ m' 44 endl( return ( QD
/i"en a string consisting o# onl $ 1$ $ B$ C 2)ere 9 N B 9 OR C 9 [OR Calculate t)e "alue o# t)e string assuming no order o# ,recedence and e"aluation is done #rom le#t to rig)t. Constraints Z T)e lengt) o# string 2ill be odd. -t 2ill al2as be a "alid string. E>am,le$ 1 2ill not be gi"en as an in,ut. E>am,les: -n,ut : 1B1 Out,ut : 1 1 N OR 1 9 1 -n,ut : 1C1B1B Out,ut : CCHH CHH ,rogram to e"aluate "alue o# an e>,ression. 3include 4bitsstdcHH.)5 using names,ace std( int e"aluateBoolE>,r&string s' 0 int n 9 s.lengt)&'( Tra"erse all o,erands b um,ing a c)aracter a#ter e"er iteration. #or &int i 9 ( i 4 n( i H9 ?' 0 -# o,erator ne>t to current o,erand is N. i# &sFi H 1G 99' 0
i# &sFi H ?G 99\\ sFiG 99' sFi H ?G 9( else sFi H ?G 91( -# o,erator ne>t to current o,erand is OR. else i# &sFi H 1G 99B' 0 i# &sFi H ?G 991\\ sFiG 991' sFi H ?G 91( else sFi H ?G 9( -# o,erator ne>t to current o,erand is [OR &ssuming a "alid in,ut' else 0 i# &sFi H ?G 99 sFiG' sFi H ?G 9( else sFi H ?G 91 return sFn Z 1G =]( ri"er code int main&' 0 string s 9 U1C1B1B( cout 44 e"aluateBoolE>,r&s'( return ( QA Ques. 1 rite a ,rogram to #ind out total mar*s obtained b a student i# t)e student gets mar*s #or t)e correct ans2er and =1 #or t)e 2rong ans2er QV !oure gi"en a #unction Z c)ar am,le Z
in,ut Z aaaaabbbccccccccdaa OutPut Z a@bcda? Q !oure gi"en a #unction$ c)ar < Con"ertToPalindrome&c)ar< str' T)e #unction acce,ts a string str$ im,lement t)e #unction to #ind and return t)e minimum c)aracters re;uired to a,,end at t)e end o# str to ma*e it a ,alindrome ssum,tions Z T)e string 2ill onl contain lo2ercase Englis) l,)abets Note Z -# string is alread a ,alindrome t)en return NILL !ou )a"e to #ind t)e minimum c)aracters re;uired to a,,end at t)e end o# t)e string to ma*e it a ,alindrome E>am,le Z -n,ut Z abcdc Out,ut Z ba