0% found this document useful (0 votes)
11 views4 pages

Coding Solutions (TY-IT-3) SVIT

Uploaded by

meetdave642
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

Coding Solutions (TY-IT-3) SVIT

Uploaded by

meetdave642
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Sardar Vallabhbhai Patel Institute of Technology, Vasad

Training & Placement Cell, IT Department


Answer Key for Mock Placement
Sem-4 AY: 2023-24

Please Note: The solution for Coding Test is provided here in two languages — C and Java.

CODING TEST
SY IT-3
1.Search an element in array

Problem:
You are given an array A of size N and an element X. Your task is to find whether the array A
contains the element X or not.
Solution:

C
#include <stdio.h>

int main(void) {
// your code goes here
int n,x;
scanf("%d%d",&n,&x);
int arr[n];
for(int i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int flag = 0;
for(int i = 0;i<n;i++)
{
if(arr[i] == x)
{
flag = 1;
break;
}
}
if(flag ==1)
{
printf("YES\n");
}
else
{
printf("NO\n");
}
}

Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 4-03-24
Java

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);

int n = scanner.nextInt();
int x = scanner.nextInt();

int[] arr = new int[n];


for (int i = 0; i < n; i++) {
arr[i] = scanner.nextInt();
}

int flag = 0;
for (int i = 0; i < n; i++) {
if (arr[i] == x) {
flag = 1;
break;
}
}

if (flag == 1) {
System.out.println("YES");
} else {
System.out.println("NO");
}

scanner.close();
}
}

2. Alfred and The Happy String

Problem:

Alfred has a string S with him. Chef is happy if the string contains a contiguous substring of
length strictly greater than 22 in which all its characters are vowels.

Determine whether Alfred is happy or not.

Note that, in english alphabet, vowels are a, e, i, o, and u.

Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 4-03-24
Solution:

#include<stdio.h>
int main()
{
int n,i,t,c=0;
scanf("%d",&t);
for(n=0;n<t;n++)
{
c=0;
char s[1000];

scanf("%s",s);
for(i=0;s[i]!='\0';i++)
{
if((s[i]=='a'||s[i]=='e'||s[i]=='i'||s[i]=='o'||s[i]=='u')&&
(s[i+1]=='a'||s[i+1]=='e'||s[i+1]=='i'||s[i+1]=='o'||s[i+1]=='u')&&
(s[i+2]=='a'||s[i+2]=='e'||s[i+2]=='i'||s[i+2]=='o'||s[i+2]=='u'))
c++;
}
if(c!=0)
printf("Happy");
else
printf("Sad");
printf("\n");
}
}

Java

import java.util.Scanner;

public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
scanner.nextLine(); // Consume newline character after reading integer

for (int n = 0; n < t; n++) {


int c = 0;
String s = scanner.nextLine();

for (int i = 0; i < s.length() - 2; i++) {


if ((isVowel(s.charAt(i)) && isVowel(s.charAt(i + 1)) &&
isVowel(s.charAt(i + 2)))) {
c++;
}

Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 4-03-24
}

if (c != 0)
System.out.println("Happy");
else
System.out.println("Sad");
}
scanner.close();
}

// Method to check if a character is a vowel


private static boolean isVowel(char ch) {
return ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u';
}
}

Prepared by Kashish Zaveri, Samarth Joshi, Tarun Ray, Unnati Suryawanshi (T&P Coordinators) 4-03-24

You might also like