Posts

Showing posts from February, 2018

First & Last X Digits

Java code: import java.util.*; public class Hello {     public static void main(String[] args) {         //Your Code Here Scanner s=new Scanner(System.in); String st; int x,l,m=0,f=0; st=s.next(); char ch[]=st.toCharArray(); x=s.nextInt(); l=ch.length-x; for(int i=0;i<ch.length;i++) {     if(ch[i]!='0'||f==1)     {         if(m<x)         {             f=1;             System.out.print(ch[i]);             m++;         }     } } System.out.println(); for(int i=l;i<ch.length;i++) System.out.print(ch[i]);     } }

Odd Numbers - Reversed Order

C Code: #include<stdio.h> #include <stdlib.h> int main() { int n; scanf("%d",&n); int a[n],i,j; for(i=0;i<n;i++) {     scanf("%d",&a[i]); } for(i=n-1;i>=0;i--) {     if(a[i]%2!=0)     {         printf("%d ",a[i]);     } } } Java code: import java.util.*; public class Hello {     public static void main(String[] args) {         //Your Code Here Scanner s=new Scanner(System.in); int n=s.nextInt(); int a[]=new int [n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); for(int i=n-1;i>=0;i--) {     if(a[i]%2!=0)     System.out.print(a[i]+" "); }     } } C++ code: #include <iostream> using namespace std; int main() { int n,i; cin>>n; int a[n]; for(i=0;i<n;i++) {     cin>>a[i]; } for(i=n-1;i>=0;i--) { ...

Sum Of First and Last Alternate Elements

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 n,l=1; n=s.nextInt(); int a[]=new int[n]; for(int i=0;i<n;i++) a[i]=s.nextInt(); for(int i=0;i<n/2;i++) { System.out.print(a[i]+a[n-l]+" "); l++; }     } }

Digits Printer

Image
Java Code: import java.util.*; public class Hello {     public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.next(); char ch[]=s.toCharArray(); int i,j,k,l=s.length(); Arrays.sort(ch); for(i=l-1;i>0;i--)     System.out.print(ch[i]); if(ch[0]!='0'&&ch[1]!='0')     System.out.print(ch[0]); else if(ch[0]=='0'&&ch[1]!='0') System.out.print(ch[0]); } }

Username Domain Extension

Image
Java Code: Method I: import java.util.*; public class Hello {     public static void main(String[] args) { String s; Scanner sc=new Scanner(System.in); s=sc.next(); int a=0,b=0,l=s.length(),i; for(i=0;i<l;i++) {     if(s.charAt(i)=='@')     {         a=i;     }     if(s.charAt(i)=='.')     b=i; } for(i=b+1;i<l;i++) System.out.print(s.charAt(i)); System.out.println(); for(i=a+1;i<b;i++) System.out.print(s.charAt(i)); System.out.println(); for(i=0;i<a;i++) System.out.print(s.charAt(i)); } } Method II: import java.util.*; public class Hello {     public static void main(String[] args) { Scanner s=new Scanner(System.in);         String str=s.nextLine();         String ch[]=str.split("@|\\.");         for...

Maximum Product

Image
C code: #include<stdio.h> #include <stdlib.h> int main() { int n,i,j,temp; scanf("%d",&n); int a[n]; for(i=0;i<n;i++) {     scanf("%d ",&a[i]); } for(i=0;i<n;i++) {     for(j=i+1;j<n;j++)     {         if(a[i]>a[j])         {             temp=a[i];             a[i]=a[j];             a[j]=temp;         }     } } if(a[0]*a[1]>a[n-1]*a[n-2]) {     printf("%d %d",a[0],a[1]); } else printf("%d %d",a[n-2],a[n-1]); } Java Code: import java.util.*; public class Hello {     public static void main(String[] args) {         Scanner s...

Two Strings - L Joint

Image
Java Code: import java.util.*; public class Hello {     public static void main(String[] args) {         //Your Code Here Scanner s=new Scanner(System.in); String str,st; str=s.next(); st=s.next(); char ch[]=str.toCharArray(); char c[]=st.toCharArray(); int a=ch.length,b=c.length,n=0,n1=0,count=0,q,w; char c1[]=new char[a+b]; for(int i=0;i<a;i++) {     c1[i]=ch[i];     count++; } for(int i=0;i<b;i++) {     c1[count]=c[i];     count++; } if(ch[a-1]==c[0]) {     n=a;     n1=b;     q=0;     w=1; } else {     n=b;     n1=a;     q=a;     w=0; } for(int i=0;i<n-1;i++) {     for(int j=0;j<n1;j++)     {         if(j==0)         System.out.print(c1[q]); ...

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++;          }...

Sum of Unit and Most Significant Digits

Image
  Java Code: Method I: import java.util.*; public class Hello {     public static void main(String[] args) {         String n;         Scanner s=new Scanner(System.in);         n=s.nextLine();         int l=n.length();         int m=Character.getNumericValue(n.charAt(0));         int ls=Character.getNumericValue(n.charAt(l-1));          System.out.print(m+ls);     } }  Method II :  import java.util.*; public class Hello {     public static void main(String[] args) { Scanner s=new Scanner(System.in); int n,l,m=0; n=s.nextInt(); l=n%10; while(n>0) {     m=n%10;     n/=10; } System.out.print(l+m);     } }

Merge Two Sorted Arrays

Image
Java Code: import java.util.*; public class Hello {     public static void main(String[] args) {     int s1,s2;     Scanner s=new Scanner(System.in);     s1=s.nextInt();     s2=s.nextInt();     int a[]=new int [s1+s2];     int i,j;     for(i=0;i<s1;i++)     {         a[i]=s.nextInt();     }     for(i=s1;i<s1+s2;i++)     {         a[i]=s.nextInt();     }     Arrays.sort(a);     for(i=0;i<s1+s2;i++)     System.out.print(a[i]+" ");     } }

Maximum Target Hits in Sequence

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 n,c=0;         n=s.nextInt();         int a[]=new int[n];         int b[]=new int[n];         for(int i=0;i<n;i++)         a[i]=s.nextInt();         for(int i=0;i<n;i++)         {             if(i+1<n)             {                 if(a[i+1]>a[i])    ...