Posts

N Integers – Sum S – All Combinations(Weekly Test)

A number S is passed as input. Also N positive unique integers are passed as input to the program. One or more numbers (out of these N integers) can be added to form the number S. Several such combinations are possible and the program must print the count C of such combinations. You need to optimize the code so that it executes within the given time (failing which Time exceeded Error will be obtained). Input Format: The first line will contain S and N separated by a space. The second line will contain the value of N positive integers, with the values separated by a space. Output Format: The first line will contain the the count C Boundary Conditions: 1 <= S <= 99999 2 <= N <= 50 Example Input/Output 1: Input: 10 5 1 2 3 4 5 Output: 3 Explanation: The three combinations which add up to 10 are 1 4 5 2 3 5 1 2 3 4  Code: #include<stdio.h> #include<string.h> short int subset[999][999],cnt=0; void print(int a[...

Number-X Largest Digits Ascending

Image
  C Code: #include<stdio.h> #include <stdlib.h> int main() { long long int a,n,i=0,x=0,c[1000000],z=0,j=0,d,b[1000000]; scanf("%lld%lld",&a,&n); while(a>0) {     b[j]=a%10;     a=a/10;     j++; } d=j; for(i=9;i>=1,z<n;i--) {     for(j=0;j<=d;j++)     {         if(i==b[j])         {             c[x]=i;             x++;             z++;             break;         }     } } for(i=n-1;i>=0;i--) {     printf("%lld",c[i]); } } C++ Code:  Java Code: import java.util.*; public cl...

Multiples of X,Y and X+Y Till U

Image
Java code: import java.util.*; public class Hello {     public static void main(String[] args) {         //Your Code Here Scanner s=new Scanner(System.in); int x,y,u,a; x=s.nextInt(); y=s.nextInt(); u=s.nextInt(); a=x+y; for(int i=1;i<u;i++) {     if(i%x==0||i%y==0||i%a==0)     System.out.print(i+" "); } System.out.println(); for(int i=u-1;i>=1;i--) {     if(i%a==0||i%x==0||i%y==0)     System.out.print(i+" "); }     } }

First and Last X Digits Equal

Image
  Java Code: import java.util.*; public class Hello {     public static void main(String[] args) {         Scanner s=new Scanner(System.in);         String str=s.next();         int  n=s.nextInt();         char ch[]=str.toCharArray();         int l=str.length();        int i,j,k=l-n-1;         String s1="",s2="";         for(i=0;i<n;i++)             s1+=ch[i];         for(j=k+1;j<l;j++)             s2+=ch[j];         if(s1.equals(s2))         System.out.print("Yes");         else ...

Tennis Contest

Image
  C Code: #include<stdio.h> #include <stdlib.h> int main() { int n,i,j,k,l=0,m,b[100],c[100],d[100]; char a[100][10]; scanf("%d",&n); for(i=0;i<n;i++) {     scanf("%d%s",&b[i],&a[i]); } for(i=0;i<n-1;i++) {     scanf("%dvs%d",&c[i],&d[i]); } for(i=n-2;i>=0;i--) {     if(c[i]==c[n-2])     {         l=d[i];         for(j=0;j<n;j++)         {             if(l==b[j])             {                 printf("%s\n",a[j]);                 break;             }  ...

Unique String Representations

Image
  C Code: #include<stdio.h> #include <stdlib.h> int main() { char s[22]; scanf("%s",s); unsigned long long l=strlen(s); unsigned long long i,j,c,x=1,t=1,flag=0; for(i=0;i<l;i++) {     c=1;     for(j=i+1;j<l;j++)     {         if(s[i]==s[j]&&s[j]!='*')         {             c++;             s[j]='*';         }     }     s[i]='*';     unsigned long long m=1;     if(c!=1)     {         for(int k=1;k<=c;k++)         {             m*=k;         }    ...

Print February Calendar

Image
  C++ Code: #include <iostream> #include<string> using namespace std; int main(){  int i,j,k,m,n,s,t=0,u,v,year,month;  string day;  cin>>year>>day;  if(year%400==0||(year%4==0&&year%100!=0))  {      month=29;  }  else  month=28;  if(day=="MON")s=5;  if(day=="TUE")s=6;  if(day=="WED")s=7;  if(day=="THU")s=1;  if(day=="FRI")s=2;  if(day=="SAT")s=3;  if(day=="SUN")s=4;  int a[8][8]={0};  t=1;  for(i=1;i<6;i++)  {      if(i==1)      {          for(j=s;j<8;j++)          {              a[i][j]=t;              t++;          }...