Coding
Coding
Coding
#include <iostream>
#include <unordered_set>
string autoStr;
for (int i = 0;
i < autoStr.size();
i++)
{
// Initialise count as 0
cnt = 0;
// It is an
// Autobiographical
// number
cnt++;
}
return false;
}
return true;
}
int main()
{
char *num = "1210";
int result = FindAutoCount(num);
std::cout << result;
return 0;
}
Problem Description :
The function accepts two positive integers ‘r’ and ‘unit’ and a positive integer array
‘arr’ of size ‘n’ as its argument ‘r’ represents the number of rats present in an area,
‘unit’ is the amount of food each rat consumes and each ith element of array ‘arr’
represents the amount of food present in ‘i+1’ house number, where 0 <= i
Note:
Example:
Input:
r: 7
unit: 2
n: 8
arr: 2 8 3 5 7 4 1 2
Output:
Explanation:
Total amount of food required for all rats = r * unit
= 7 * 2 = 14.
The amount of food in 1st houses = 2+8+3+5 = 18. Since, amount of food in 1st 4
houses is sufficient for all the rats. Thus, output is 4.
#include<stdio.h>
int calculate(int r, int unit, int arr[],int n)
{
if(n==0)
return -1;
int totalFoodrequired = r*unit;
int foodTillNow=0;
int house=0;
for (house=0;house<n;++house)
{
foodTillNow+=arr[house];
if(foodTillNow>=totalFoodrequired)
{
break;
}
}
if(totalFoodrequired>foodTillNow)
return 0;
return house+1;
int main()
{
int r;
scanf ("%d", &r);
int unit;
scanf ("%d", &unit);
int n;
scanf ("%d", &n);
int arr[n];
for (int i = 0; i < n; ++i)
{
scanf ("%d", &arr[i]);
}
printf ("%d", calculate (r, unit, arr, n));
return 0;
}
Problem Description :
The Binary number system only uses two digits, 0 and 1 and number system can be
called binary string. You are required to implement the following function:
The function accepts a string str as its argument. The string str consists of binary
digits eparated with an alphabet as follows:
You are required to calculate the result of the string str, scanning the string to right
taking one opearation at a time, and return the same.
Note:
Input:
str: 1C0C1C1A0B1
Output:
1
Explanation:
The alphabets in str when expanded becomes “1 XOR 0 XOR 1 XOR 1 AND 0 OR 1”,
result of the expression becomes 1, hence 1 is returned.
Sample Input:
0C1A1B1C1C1B0A0
Output:
0
#include<stdio.h>
#include<string.h>
int main ()
{
char str[100];
fgets (str, sizeof (str), stdin);
int len = strlen (str);
if (str[len - 1] == '\n')
{
str[len - 1] = '\0'; // Remove the newline character
len--; // Decrement the length
}
int result = OperationsBinaryString (str);
printf ("%d\n", result);
return 0;
}
You are given a function.
int CheckPassword(char str[], int n);
The function accepts string str of size n as an argument. Implement the function
which returns 1 if given string str is valid password else 0.
str is a valid password if it satisfies the below conditions.
– At least 4 characters
– At least one numeric digit
– At Least one Capital Letter
– Must not have space or slash (/)
– Starting character must not be a number
Assumption:
Input string will not be empty.
Example:
Input 1:
aA1_67
Input 2:
a987 abC012
Output 1:
1
Output 2:
0
#include<stdio.h>
#include<string.h>
}
a++;
}
return cap>0 && nu>0;
}
int main()
{
char str[100];
fgets (str, sizeof (str), stdin);
int len = strlen (str);
if (str[len - 1] == '\n')
{
str[len - 1] = '\0'; // Remove the newline character
len--; // Decrement the length
}
int result = CheckPassword (str, len);
printf ("%d\n", result);
return 0;
}
You are given a function,
int findCount(int arr[], int length, int num, int diff);
The function accepts an integer array ‘arr’, its length and two integer variables ‘num’
and ‘diff’. Implement this function to find and return the number of elements of ‘arr’
having an absolute difference of less than or equal to ‘diff’ with ‘num’.
Note: In case there is no element in ‘arr’ whose absolute difference with ‘num’ is less
than or equal to ‘diff’, return -1.
Example:
Input:
arr: 12 3 14 56 77 13
num: 13
diff: 2
Output:
3
Explanation:
Elements of ‘arr’ having absolute difference of less than or equal to ‘diff’ i.e. 2 with
‘num’ i.e. 13 are 12, 13 and 14.
#include<stdio.h>
#include<stdlib.h>
def differenceofSum(n. m)
The function accepts two integers n, m as arguments Find the sum of all numbers in
range from 1 to m(both inclusive) that are not divisible by n. Return difference
between sum of integers not divisible by n with sum of numbers divisible by n.
Assumption:
Example
Input
n:4
m:20
Output
90
Explanation
Sample Input
n:3
m:10
Sample Output
19
#include<stdio.h>
int differenceofSum (int n, int m)
{
int i, sum1 = 0, sum2 = 0;
for (i = 1; i <= m; i++)
{
if (i % n == 0)
{
sum1 = sum1 + i;
}
else
{
sum2 = sum2 + i;
}
}
if (sum2 > sum1)
return sum2 - sum1;
else
return sum1 - sum2;
}
int main ()
{
int n, m;
int result;
scanf ("%d", &n);
scanf ("%d", &m);
result = differenceofSum (n, m);
printf ("%d", result);
return 0;
}
You are required to implement the following Function
def LargeSmallSum(arr)
The function accepts an integers arr of size ’length’ as its arguments you are
required to return the sum of second largest element from the even positions and
second smallest from the odd position of given ‘arr’
Assumption:
NOTE
Example
Input
arr:3 2 1 7 5 4
Output
Explanation
Sample Input
arr:1 8 0 2 3 5 6
Sample Output
8
#include <stdio.h>
int productSmallestPair(int *array, int n, int sum)
{
int answer, temp, i, j, check;
if (n < 2)
{
answer = -1;
}
else
{
for (i = 0; i < n; i++)
{
for (j = i + 1; j < n; j++)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
check = array[0] + array[1];
if (check <= sum)
answer = array[0] * array[1];
else
answer = 0;
}
return answer;
}
int main()
{
int n, sum, result, i;
scanf("%d", &sum);
scanf("%d", &n);
int array[n];
for (i = 0; i < n; i++)
{
scanf("%d", &array[i]);
}
result = productSmallestPair(array, n, sum);
printf("%d", result);
return 0;
}
N-base notation is a system for writing numbers that uses only n different symbols,
This symbols are the first n symbols from the given notation list(Including the
symbol for o) Decimal to n base notation are (0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8,
9:9, 10:A,11:B and so on upto 35:Z)
The function accept positive integer n and num Implement the function to calculate
the n-base equivalent of num and return the same as a string
Steps:
1. Divide the decimal number by n,Treat the division as the integer division
2. Write the the remainder (in n-base notation)
3. Divide the quotient again by n, Treat the division as integer division
4. Repeat step 2 and 3 until the quotient is 0
5. The n-base value is the sequence of the remainders from last to first
Assumption:
1 < n < = 36
Example
Input
n: 12
num: 718
Output
4BA
Explanation
num Divisor quotient remainder
Sample Input
n: 21
num: 5678
Sample Output
CI8
The function accepts a string “str” of length ‘n’, that contains alphabets and hyphens
(-). Implement the function to move all hyphens(-) in the string to the front of the
given string.
Example :-
Input:
o str.Move-Hyphens-to-Front
Output:
o —MoveHyphenstoFront
Explanation:-
The string “Move-Hyphens -to-front” has 3 hyphens (-), which are moved to the front
of the string, this output is “— MoveHyphen”
Sample Input
Str: String-Compare
Sample Output-
-StringCompare
JAVA
import java.util.*;
class Solution
{
public static String moveHyphen (String str, int n)
{
String res = "";
for (int i = 0; i < n; i++)
{
if (str.charAt (i) == '-')
res = str.charAt (i) + res;
else
res = res + str.charAt (i);
}
return res;
}
public static void main (String[]args)
{
Scanner sc = new Scanner (System.in);
String str = sc.next ();
System.out.println (moveHyphen (str, str.length ()));
}
}
Problem Statement
A carry is a digit that is transferred to left if sum of digits exceeds 9 while adding two
numbers from right-to-left one digit at a time
The functions accepts two numbers ‘num1’ and ‘num2’ as its arguments. You are
required to calculate and return the total number of carries generated while adding
digits of two numbers ‘num1’ and ‘ num2’.
Assumption: num1, num2>=0
Example:
Input
o Num 1: 451
o Num 2: 349
Output
o 2
Explanation:
Adding ‘num 1’ and ‘num 2’ right-to-left results in 2 carries since ( 1+9) is 10. 1 is
carried and (5+4=1) is 10, again 1 is carried. Hence 2 is returned.
Sample Input
Num 1: 23
Num 2: 563
Sample Output
0 #include<stdio.h>
int numberOfCarries(int num1, int num2)
{
int carry = 0,sum, p, q, count=0;
while((num1!=0)&&(num2!=0))
{
p=num1%10;
q=num2%10;
sum = carry+p+q;
if (sum>9)
{
carry =1;
count++;
}
else{
carry =0;
}
num1=num1/10;
num2 = num2/10;
}
while(num1!=0)
{
p = num1%10;
sum = carry +p;
if(sum>9)
{
carry =1;
count ++;
}
else
carry =0;
num1=num1/10;
}
while(num2!=0)
{
q=num2%10;
sum = carry +q;
if(sum>9)
{
carry =1;
count ++;
}
else carry =0;
num2 = num2/10;
}
return 0;
}
int main ()
{
int x, y, a;
scanf ("%d", &x);
scanf ("%d", &y);
a = numberOfCarries (x, y);
printf ("%d", a);
return 0;
}