Tuesday, 12 December 2023

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 once in the given array. Return the answer in ascending order. If no such element is found, return list containing [-1]

Note: The extra space is only for the array to be returned. Try and perform all operations within the provided array. 

Example 1:

Input:
N = 4
a[] = {0,3,1,2}
Output: 
-1 Explanation:
There is no repeating element in the array. Therefore output is -1.

Example 2:

Input:
N = 5
a[] = {2,3,1,2,3}
Output: 
2 3  Explanation:
2 and 3 occur more than once in the given array.

Your Task:
Complete the function duplicates() which takes array a[] and n as input as parameters and returns a list of elements that occur more than once in the given array in a sorted manner. 

Expected Time Complexity: O(n).
Expected Auxiliary Space: O(n).

Constraints:
1 <= N <= 105
0 <= A[i] <= N-1, for each valid i


SOLUTION:-

class Solution:

    def duplicates(self, arr, n): 

        l=list()

        d={}

        for c in arr:

            d.setdefault(c,0)

            d[c]=d[c]+1

        for k,v in d.items():

            if(v>1):

                l.append(k)

        if len(l)==0:

            l=[-1]

        else:

            l.sort()

        return l

                

    # code here

        



#{ 

 # Driver Code Starts

if(__name__=='__main__'):

    t = int(input())

    for i in range(t):

        n = int(input())

        arr = list(map(int, input().strip().split()))

        res = Solution().duplicates(arr, n)

        for i in res:

            print(i,end=" ")

        print()




# } Driver Code Ends


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)

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...