Thursday, 23 November 2023

Program(c):-Given a String S, reverse the string without reversing its individual words.

HAY THANK FOR VISIT

Given a String S, reverse the string without reversing its individual words. Words are separated by dots.

Example 1:

Input:
S = i.like.this.program.very.much
Output: much.very.program.this.like.i
Explanation: After reversing the whole
string(not individual words), the input
string becomes
much.very.program.this.like.i

Example 2:

Input:
S = pqr.mno
Output: mno.pqr
Explanation: After reversing the whole
string , the input string becomes
mno.pqr


Your Task:
You dont need to read input or print anything. Complete the function reverseWords() which takes string S as input parameter and returns a string containing the words in reversed order. Each word in the returning string should also be separated by '.' 


Expected Time Complexity: O(|S|)
Expected Auxiliary Space: O(|S|)


Constraints:
1 <= |S| <= 105

SOLUTION :-

//{ Driver Code Starts

import java.util.*;

import java.lang.*;

import java.io.*;

class GFG {

    public static void main(String[] args) {


        Scanner sc = new Scanner(System.in);

        int t = sc.nextInt();

        while (t > 0) {

            String s = sc.next();

            Solution obj = new Solution();

            System.out.println(obj.reverseWords(s));

            t--;

        }

    }

}


// } Driver Code Ends




class Solution 

{

    //Function to reverse words in a given string.

    String reverseWords(String S)

    {

        int l=S.length();

        

        String sa ="";

        String s1="";

        int p=0;

        for(int i=l-1;i>=0;i--)

        {

            if(S.charAt(i)!='.')

            {

                s1=S.charAt(i)+s1;

            }

            else

            {

             sa=sa+s1+".";

             s1="";

             p++;

            }

        }

        sa=sa+s1;

        return sa;

    }

}

PROGRAM(C):- To find the greatest and least elements from an array.

HAY THANK FOR VISIT

Given an array A of size N of integers. Your task is to find the minimum and maximum elements in the array.

 

Example 1:

Input:
N = 6
A[] = {3, 2, 1, 56, 10000, 167}
Output: 1 10000
Explanation: minimum and maximum elements of array are 1 and 10000.

 

Example 2:

Input:
N = 5
A[]  = {1, 345, 234, 21, 56789}
Output: 1 56789
Explanation: minimum and maximum element of array are 1 and 56789.

 

Your Task:  
You don't need to read input or print anything. Your task is to complete the function getMinMax() which takes the array A[] and its size N as inputs and returns the minimum and maximum element of the array.

 

Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)

 

Constraints:
1 <= N <= 105
1 <= Ai <=1012


SOLUTION:-



#include <stdio.h>

struct pair {
    long long int min;
    long long int max;
};

struct pair getMinMax(long long int arr[], long long int n) ;

int main() {
    long long int t, n, a[100002], i;
    struct pair minmax;

    scanf("%lld", &t);
    while (t--) {
        scanf("%lld", &n);

        for (i = 0; i < n; i++) scanf("%lld", &a[i]);
        minmax = getMinMax(a, n);
        printf("%lld %lld\n", minmax.min, minmax.max);
    }
    return 0;
}
// } Driver Code Ends


// User function Template for C

struct pair getMinMax(long long int arr[], long long int n) {
    struct pair minMax;
    int s=arr[0];
    int g=arr[0];
    for(int i=1;i<n;i++)
    {
        if(g<arr[i])
          g=arr[i];
        if(s>arr[i])
          s=arr[i];
    }
    minMax.min=s;
    minMax.max=g;
    return minMax;
    
}

PROGRAM(PYTHON) :-TO CHECK WEATHER THE NUMBER FORMES BY LAST DIGIT OF AN ARRAY IS DIVISIBLE BY 10 OR NOT??

HAY THANK FOR VISIT
Problem

You are provided an array  of size  that contains non-negative integers. Your task is to determine whether the number that is formed by selecting the last digit of all the N numbers is divisible by 10.

Note: View the sample explanation section for more clarification.

Input format

  • First line: A single integer  denoting the size of array 
  • Second line:  space-separated integers.

Output format

If the number is divisible by 10, then print . Otherwise, print .

Constraints
11050[]105

Sample Input
5
85 25 65 21 84
Sample Output
No
Time Limit: 1
Memory Limit: 256
Source Limit:
Explanation

Last digit of 85 is 5.
Last digit of 25 is 5.
Last digit of 65 is 5.
Last digit of 21 is 1.
Last digit of 84 is 4.
Therefore the number formed is 55514 which is not divisible by 10.


SOLUTION:-


num=0;
d=0;
N = int(input())
data = [int(x) for x in input().split()]
for i in range(N):
    d=data[i]
    num=num*10+d
if num%10==0:
    ans="Yes"
else:
    ans="No"

# Write your code here
# ans =

print(ans)

Wednesday, 22 November 2023

C Program to print a 2d array in snake form :-

HAY THANK FOR VISIT!!!!!!!!

Q:-Given a matrix of size N x N. Print the elements of the matrix in the snake like pattern depicted below.

Example 1:

Input:
N = 3 
matrix[][] = {{45, 48, 54},
             {21, 89, 87}
             {70, 78, 15}}
Output: 
45 48 54 87 89 21 70 78 15  Explanation: Matrix is as below: 45 48 54 21 89 87 70 78 15 Printing it in snake pattern will lead to the output as 45 48 54 87 89 21 70 78 15.

Example 2:

Input:
N = 2
matrix[][] = {{1, 2},
              {3, 4}}
Output: 
1 2 4 3 Explanation: Matrix is as below: 1 2  3 4 Printing it in snake pattern will give output as 1 2 4 3.

Your Task:
You dont need to read input or print anything. Complete the function snakePattern() that takes matrix as input parameter and returns a list of integers in order of the values visited in the snake pattern. 

Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(N * N) for the resultant list only.

Constraints:
1 <= N <= 103
1 <= mat[i][j] <= 109




Solution:-
//{ Driver Code Starts
//Initial Template for C

#include <stdio.h>


// } Driver Code Ends
//User function Template for C


//Function to return list of integers visited in snake pattern in matrix.
int* snakePattern(int n, int matrix[][n])
{   
    int i,j,k,x,t[n];
    
    for(i=0;i<n;i++)
    {

        if((i%2)==0)
        {  
            
            for(j=0;j<n;j++)
            {
               
            matrix[i][j] =matrix[i][j];
                
            }
          
        }
        else
        {   x=0;
            for(k=n-1;k>=0;k--)
            {     
                t[x] =matrix[i][k];
                x++;
            }
            
             for(j=0;j<n;j++)
            {
               
               matrix[i][j]=t[j];
                
            }
            
            
        }
         
    }
    return matrix;
}

//{ Driver Code Starts.

int main()
{
    int tc;
scanf("%d", &tc);
while(tc--){
int n;
scanf("%d", &n);
int matrix[n][n];
 
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
scanf("%d", &matrix[i][j]);
}
}
int *result = snakePattern(n, matrix);
for(int i = 0; i < n*n; i++)
printf("%d ", result[i]);
printf("\n");
}
return 0;
}
// } Driver Code Ends

Program(python):-Find duplicates in an array (Geeks for geek problem)

QUESTION:- Given an array  a  of size  N  which contains elements from  0  to  N-1 , you need to find all the elements occurring more than o...